• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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 package com.android.systemui.demomode
18 
19 import android.content.Intent
20 import android.os.Bundle
21 import android.testing.TestableLooper
22 import androidx.test.ext.junit.runners.AndroidJUnit4
23 import androidx.test.filters.SmallTest
24 import com.android.systemui.SysuiTestCase
25 import com.android.systemui.demomode.DemoMode.ACTION_DEMO
26 import com.android.systemui.demomode.DemoMode.COMMAND_STATUS
27 import com.android.systemui.dump.DumpManager
28 import com.android.systemui.util.settings.FakeGlobalSettings
29 import com.google.common.truth.Truth.assertThat
30 import kotlinx.coroutines.launch
31 import kotlinx.coroutines.test.TestScope
32 import kotlinx.coroutines.test.UnconfinedTestDispatcher
33 import kotlinx.coroutines.test.runTest
34 import org.junit.Before
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 import org.mockito.Mock
38 import org.mockito.MockitoAnnotations
39 
40 @Suppress("EXPERIMENTAL_IS_NOT_ENABLED")
41 @RunWith(AndroidJUnit4::class)
42 @TestableLooper.RunWithLooper
43 @SmallTest
44 class DemoModeControllerTest : SysuiTestCase() {
45     private lateinit var underTest: DemoModeController
46 
47     @Mock private lateinit var dumpManager: DumpManager
48 
49     private val globalSettings = FakeGlobalSettings()
50 
51     private val testDispatcher = UnconfinedTestDispatcher()
52     private val testScope = TestScope(testDispatcher)
53 
54     @Before
55     fun setUp() {
56         allowTestableLooperAsMainThread()
57 
58         MockitoAnnotations.initMocks(this)
59 
60         globalSettings.putInt(DemoModeController.DEMO_MODE_ALLOWED, 1)
61         globalSettings.putInt(DemoModeController.DEMO_MODE_ON, 1)
62 
63         underTest =
64             DemoModeController(
65                 context = context,
66                 dumpManager = dumpManager,
67                 globalSettings = globalSettings,
68                 broadcastDispatcher = fakeBroadcastDispatcher
69             )
70 
71         underTest.initialize()
72     }
73 
74     @Test
75     fun demoCommandFlow_returnsArgs() =
76         testScope.runTest {
77             var latest: Bundle? = null
78             val flow = underTest.demoFlowForCommand(TEST_COMMAND)
79             val job = launch { flow.collect { latest = it } }
80 
81             sendDemoCommand(args = mapOf("key1" to "val1"))
82             assertThat(latest!!.getString("key1")).isEqualTo("val1")
83 
84             sendDemoCommand(args = mapOf("key2" to "val2"))
85             assertThat(latest!!.getString("key2")).isEqualTo("val2")
86 
87             job.cancel()
88         }
89 
90     private fun sendDemoCommand(command: String? = TEST_COMMAND, args: Map<String, String>) {
91         val intent = Intent(ACTION_DEMO)
92         intent.putExtra("command", command)
93         args.forEach { arg -> intent.putExtra(arg.key, arg.value) }
94 
95         fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, intent)
96     }
97 
98     companion object {
99         // Use a valid command until we properly fake out everything
100         const val TEST_COMMAND = COMMAND_STATUS
101     }
102 }
103