1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.systemui
18
19 import com.android.systemui.kosmos.Kosmos
20 import com.android.systemui.kosmos.testCase
21 import com.android.systemui.kosmos.useStandardTestDispatcher
22 import com.android.systemui.kosmos.useUnconfinedTestDispatcher
23
24 /**
25 * This definition, which uses standard dispatcher, is eventually going away.
26 *
27 * If you are calling this method, and want the new default behavior, call `testKosmosNew`, and you
28 * will be migrated to the new behavior (unconfined dispatcher). If you want to maintain the old
29 * behavior, directly call testKosmosNew().useStandardTestDispatcher().
30 *
31 * The migration will proceed in multiple steps:
32 * 1. All calls to testKosmos will be converted to testKosmosLegacy, maybe over several CLs.
33 * 2. When there are zero references to testKosmos, it will be briefly deleted
34 * 3. A new testKosmos will be introduced that uses unconfined test dispatcher
35 * 4. All callers to testKosmosNew that have been introduced since step 1 will be migrated to this
36 * new definition of testKosmos
37 * 5. testKosmosNew will be deleted
38 * 6. Over time, test authors will be encouraged to migrate away from testKosmosLegacy
39 *
40 * For details, see go/thetiger
41 */
42 // TODO(b/342622417)
testKosmosnull43 fun SysuiTestCase.testKosmos(): Kosmos = testKosmosLegacy()
44
45 /**
46 * Create a new Kosmos instance using the unconfined test dispatcher. See migration notes on
47 * [testKosmos]
48 */
49 fun SysuiTestCase.testKosmosNew(): Kosmos =
50 Kosmos().apply { testCase = this@testKosmosNew }.useUnconfinedTestDispatcher()
51
52 /**
53 * This should not be called directly. Instead, you can use:
54 * - testKosmosNew().useStandardTestDispatcher() to explicitly choose the standard dispatcher
55 * - testKosmosNew() to explicitly choose the unconfined dispatcher (which is the new sysui default)
56 *
57 * For details, see go/thetiger
58 */
59 @Deprecated("Do not call this directly. Use testKosmos() with dispatcher functions if needed.")
testKosmosLegacynull60 fun SysuiTestCase.testKosmosLegacy(): Kosmos =
61 Kosmos().useStandardTestDispatcher().apply { testCase = this@testKosmosLegacy }
62
63 /** Run [f] on the main thread and return its result once completed. */
runOnMainThreadAndWaitForIdleSyncnull64 fun <T : Any> SysuiTestCase.runOnMainThreadAndWaitForIdleSync(f: () -> T): T {
65 lateinit var result: T
66 context.mainExecutor.execute { result = f() }
67 waitForIdleSync()
68 return result
69 }
70