Add more documentation explaining main thread requirements.

Flag: TEST_ONLY
Test: n/a
Bug: 230027385
Change-Id: Iad38f946c2e75a3372253e57abb4df2f52e62ca5
This commit is contained in:
Brian Isganitis
2024-06-12 16:21:35 -04:00
parent 9eaae4b6a4
commit e16fc827d3

View File

@@ -16,6 +16,7 @@
package com.android.launcher3.taskbar
import android.app.Instrumentation
import android.app.PendingIntent
import android.content.IIntentSender
import android.content.Intent
@@ -37,6 +38,25 @@ import org.junit.runners.model.Statement
* Manages the Taskbar lifecycle for unit tests.
*
* See [InjectController] for grabbing controller(s) under test with minimal boilerplate.
*
* The rule interacts with [TaskbarManager] on the main thread. A good rule of thumb for tests is
* that code that is executed on the main thread in production should also happen on that thread
* when tested.
*
* `@UiThreadTest` is a simple way to run an entire test body on the main thread. But if a test
* executes code that appends message(s) to the main thread's `MessageQueue`, the annotation will
* prevent those messages from being processed until after the test body finishes.
*
* To test pending messages, instead use something like [Instrumentation.runOnMainSync] to perform
* only sections of the test body on the main thread synchronously:
* ```
* @Test
* fun example() {
* instrumentation.runOnMainSync { doWorkThatPostsMessage() }
* // Second lambda will not execute until message is processed.
* instrumentation.runOnMainSync { verifyMessageResults() }
* }
* ```
*/
class TaskbarUnitTestRule : MethodRule {
private val instrumentation = InstrumentationRegistry.getInstrumentation()