1 /*
2  *  Copyright (C) 2022 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 
18 package com.android.systemui.lifecycle
19 
20 import androidx.arch.core.executor.ArchTaskExecutor
21 import androidx.arch.core.executor.TaskExecutor
22 import org.junit.rules.TestWatcher
23 import org.junit.runner.Description
24 
25 /**
26  * Test rule that makes ArchTaskExecutor main thread assertions pass. There is one such assert
27  * in LifecycleRegistry.
28  */
29 class InstantTaskExecutorRule : TestWatcher() {
30     // TODO(b/240620122): This is a copy of
31     //  androidx/arch/core/executor/testing/InstantTaskExecutorRule which should be replaced
32     //  with a dependency on the real library once b/ is cleared.
startingnull33     override fun starting(description: Description) {
34         super.starting(description)
35         ArchTaskExecutor.getInstance()
36             .setDelegate(
37                 object : TaskExecutor() {
38                     override fun executeOnDiskIO(runnable: Runnable) {
39                         runnable.run()
40                     }
41 
42                     override fun postToMainThread(runnable: Runnable) {
43                         runnable.run()
44                     }
45 
46                     override fun isMainThread(): Boolean {
47                         return true
48                     }
49                 }
50             )
51     }
52 
finishednull53     override fun finished(description: Description) {
54         super.finished(description)
55         ArchTaskExecutor.getInstance().setDelegate(null)
56     }
57 }
58