-
Notifications
You must be signed in to change notification settings - Fork 112
Collect renderings on context specified in WorkflowLayout.take #1424
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
+116
−28
Merged
Changes from all commits
Commits
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
65 changes: 65 additions & 0 deletions
65
...-android/src/androidTest/java/com/squareup/workflow1/ui/WorkflowLayoutInstrumentedTest.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,65 @@ | ||
| package com.squareup.workflow1.ui | ||
|
|
||
| import android.view.View | ||
| import androidx.lifecycle.Lifecycle | ||
| import androidx.lifecycle.testing.TestLifecycleOwner | ||
| import androidx.test.platform.app.InstrumentationRegistry | ||
| import com.google.common.truth.Truth.assertThat | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import org.junit.Test | ||
| import java.util.concurrent.CountDownLatch | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| /** | ||
| * Instrumented tests for [WorkflowLayout] that require a real Android environment. | ||
| * These tests verify behavior that cannot be properly tested with Robolectric, | ||
| * such as the main thread requirement for collecting renderings. | ||
| */ | ||
| @OptIn(ExperimentalCoroutinesApi::class) | ||
| internal class WorkflowLayoutInstrumentedTest { | ||
|
|
||
| @Test | ||
| fun throwsWhenCollectingOnBackgroundThread() { | ||
|
|
||
| var exception: Throwable? = null | ||
| val countDownLatch = CountDownLatch(1) | ||
|
|
||
| Thread.setDefaultUncaughtExceptionHandler { _, throwable -> | ||
| exception = throwable | ||
| countDownLatch.countDown() | ||
| } | ||
|
|
||
| val testLifeCycleOwner = TestLifecycleOwner(Lifecycle.State.CREATED) | ||
| val renderings = MutableStateFlow(TestScreen()) | ||
|
|
||
| val nonMainThreadDispatcher = Dispatchers.IO | ||
|
|
||
| val workflowLayout = WorkflowLayout(InstrumentationRegistry.getInstrumentation().context) | ||
| workflowLayout.take( | ||
| lifecycle = testLifeCycleOwner.lifecycle, | ||
| renderings = renderings, | ||
| collectionContext = nonMainThreadDispatcher | ||
| ) | ||
|
|
||
| // start the lifecycle. | ||
| testLifeCycleOwner.lifecycle.currentState = Lifecycle.State.STARTED | ||
|
|
||
| countDownLatch.await(1000, TimeUnit.MILLISECONDS) | ||
|
|
||
| assertThat(exception).isNotNull() | ||
| assertThat(exception).isInstanceOf(IllegalArgumentException::class.java) | ||
| assertThat(exception?.message).contains("Collection dispatch must happen on the main thread!") | ||
| } | ||
|
|
||
| /** | ||
| * Simple test screen for instrumented tests. | ||
| */ | ||
| private class TestScreen : AndroidScreen<TestScreen> { | ||
| override val viewFactory = | ||
| ScreenViewFactory.fromCode<TestScreen> { _, initialEnvironment, context, _ -> | ||
| ScreenViewHolder(initialEnvironment, View(context)) { _, _ -> } | ||
| } | ||
| } | ||
| } |
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
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
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.
Do we have any test coverage now? If you took out the withContext statement and just ran the block directly would any test fail? (If not, I think we should test that)
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.
Added test coverage for this, and testing also that we throw if its not a main thread dispatcher in the context.
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.
Whoops. Firebender had its own branch for the work we were doing and it did not get added.... stay tuned.
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.
Ok, added correctly now.