• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.launcher3.util
18 
19 import androidx.test.filters.SmallTest
20 import com.android.launcher3.dagger.LauncherAppComponent
21 import com.android.launcher3.util.LauncherModelHelper.SandboxModelContext
22 import java.util.concurrent.TimeUnit.SECONDS
23 import kotlin.reflect.KFunction
24 import kotlin.reflect.full.memberFunctions
25 import org.junit.After
26 import org.junit.Assert
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 import platform.test.runner.parameterized.ParameterizedAndroidJunit4
30 import platform.test.runner.parameterized.Parameters
31 
32 @SmallTest
33 @RunWith(ParameterizedAndroidJunit4::class)
34 class DaggerSingletonDeadlockTest(val method: KFunction<*>, val methodName: String) {
35 
36     private val context = SandboxModelContext()
37 
38     @After
tearDownnull39     fun tearDown() {
40         context.onDestroy()
41     }
42 
43     /** Test to verify that the object can be created successfully on the main thread. */
44     @Test
objectCreationOnMainThreadnull45     fun objectCreationOnMainThread() {
46         Executors.MAIN_EXECUTOR.submit {
47                 method.call(context.appComponent).also(Assert::assertNotNull)
48             }
49             .get(10, SECONDS)
50     }
51 
52     /**
53      * Test to verify that the object can be created successfully on the background thread, when the
54      * main thread is blocked.
55      */
56     @Test
objectCreationOnBackgroundThreadnull57     fun objectCreationOnBackgroundThread() {
58         TestUtil.runOnExecutorSync(Executors.MAIN_EXECUTOR) {
59             Executors.THREAD_POOL_EXECUTOR.submit {
60                     method.call(context.appComponent).also(Assert::assertNotNull)
61                 }
62                 .get(10, SECONDS)
63         }
64     }
65 
66     companion object {
67         @Parameters(name = "{1}")
68         @JvmStatic
getTestMethodsnull69         fun getTestMethods() =
70             LauncherAppComponent::class
71                 .memberFunctions
72                 .filter { it.parameters.size == 1 }
<lambda>null73                 .map {
74                     arrayOf(it, if (it.name.startsWith("get")) it.name.substring(3) else it.name)
75                 }
76     }
77 }
78