-
-
Notifications
You must be signed in to change notification settings - Fork 955
Add start workers in LaunchActivity #6720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
19a7b7f
Add start workers in FrontendScreen
TimoPtr 0ea972e
Move the logic into the LaunchActivtiy
TimoPtr 6d9e728
Apply PR feedback
TimoPtr fbb8e95
Apply suggestions
TimoPtr 6af7901
Add unit test to verify the worker and update are properly invoked
TimoPtr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
app/src/test/kotlin/io/homeassistant/companion/android/launch/LaunchActivityTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package io.homeassistant.companion.android.launch | ||
|
|
||
| import androidx.lifecycle.Lifecycle | ||
| import androidx.test.core.app.ActivityScenario | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import androidx.work.testing.WorkManagerTestInitHelper | ||
| import dagger.hilt.android.testing.BindValue | ||
| import dagger.hilt.android.testing.HiltAndroidRule | ||
| import dagger.hilt.android.testing.HiltAndroidTest | ||
| import dagger.hilt.android.testing.HiltTestApplication | ||
| import dagger.hilt.android.testing.UninstallModules | ||
| import io.homeassistant.companion.android.common.data.servers.ServerManager | ||
| import io.homeassistant.companion.android.di.ServerManagerModule | ||
| import io.homeassistant.companion.android.sensors.SensorReceiver | ||
| import io.homeassistant.companion.android.sensors.SensorWorker | ||
| import io.homeassistant.companion.android.testing.unit.ConsoleLogRule | ||
| import io.homeassistant.companion.android.websocket.WebsocketManager | ||
| import io.mockk.Runs | ||
| import io.mockk.coEvery | ||
| import io.mockk.coVerify | ||
| import io.mockk.every | ||
| import io.mockk.just | ||
| import io.mockk.mockk | ||
| import io.mockk.mockkObject | ||
| import io.mockk.unmockkObject | ||
| import io.mockk.verify | ||
| import org.junit.After | ||
| import org.junit.Before | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config(application = HiltTestApplication::class) | ||
| @UninstallModules(ServerManagerModule::class) | ||
| @HiltAndroidTest | ||
| class LaunchActivityTest { | ||
|
|
||
| @get:Rule(order = 0) | ||
| val consoleLogRule = ConsoleLogRule() | ||
|
|
||
| @get:Rule(order = 1) | ||
| val hiltRule = HiltAndroidRule(this) | ||
|
|
||
| @BindValue | ||
| @JvmField | ||
| val serverManager: ServerManager = mockk(relaxed = true) { | ||
| coEvery { getServer(any<Int>()) } returns null | ||
| } | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| WorkManagerTestInitHelper.initializeTestWorkManager(ApplicationProvider.getApplicationContext()) | ||
| mockkObject(SensorWorker.Companion) | ||
| mockkObject(WebsocketManager.Companion) | ||
| mockkObject(SensorReceiver.Companion) | ||
| every { SensorWorker.start(any()) } just Runs | ||
| coEvery { WebsocketManager.start(any()) } just Runs | ||
| every { SensorReceiver.updateAllSensors(any()) } just Runs | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| unmockkObject(SensorWorker.Companion) | ||
| unmockkObject(WebsocketManager.Companion) | ||
| unmockkObject(SensorReceiver.Companion) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Given activity resumes then sensor worker and websocket manager are started`() { | ||
| ActivityScenario.launch(LaunchActivity::class.java).use { | ||
| verify { SensorWorker.start(any()) } | ||
| coVerify { WebsocketManager.start(any()) } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `Given activity pauses without finishing then all sensors are updated`() { | ||
| ActivityScenario.launch(LaunchActivity::class.java).use { scenario -> | ||
| scenario.moveToState(Lifecycle.State.STARTED) // triggers onPause | ||
| verify { SensorReceiver.updateAllSensors(any()) } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes no sense to me, and I wouldn't be surprised if it turns out to be flaky. The documentation mentions (including emphasis!):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's for the test library

for me when calling moveToState with STARTED it does invoke

pauseActivityI didn't see any flakiness on this test, I did run it multiple times without any issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the Google inconsistency... /s
In that case OK, no objection and it seems like the right way to test this.