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