1 /*
2  * Copyright (C) 2020 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.statusbar.commandline
18 
19 import android.test.suitebuilder.annotation.SmallTest
20 import android.testing.AndroidTestingRunner
21 import android.testing.TestableLooper
22 
23 import com.android.systemui.SysuiTestCase
24 
25 import org.mockito.ArgumentMatchers.anyList
26 import org.mockito.ArgumentMatchers.eq
27 import org.mockito.Mockito
28 import org.mockito.Mockito.mock
29 import org.mockito.Mockito.verify
30 import org.junit.Before
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 
34 import java.io.PrintWriter
35 import java.io.StringWriter
36 import java.util.concurrent.Executor
37 
38 private fun <T> anyObject(): T {
39     return Mockito.anyObject<T>()
40 }
41 
42 private fun <T : Any> safeEq(value: T): T = eq(value) ?: value
43 
44 @RunWith(AndroidTestingRunner::class)
45 @TestableLooper.RunWithLooper
46 @SmallTest
47 class CommandRegistryTest : SysuiTestCase() {
48     lateinit var registry: CommandRegistry
49     val inLineExecutor = object : Executor {
50         override fun execute(command: Runnable) {
51             command.run()
52         }
53     }
54 
55     val writer: PrintWriter = PrintWriter(StringWriter())
56 
57     @Before
58     fun setup() {
59         registry = CommandRegistry(context, inLineExecutor)
60     }
61 
62     @Test(expected = IllegalStateException::class)
63     fun testRegisterCommand_throwsWhenAlreadyRegistered() {
64         registry.registerCommand(COMMAND) { FakeCommand() }
65         // Should throw when registering the same command twice
66         registry.registerCommand(COMMAND) { FakeCommand() }
67     }
68 
69     @Test
70     fun testOnShellCommand() {
71         var fakeCommand = mock(Command::class.java)
72         registry.registerCommand(COMMAND) { fakeCommand }
73         registry.onShellCommand(writer, arrayOf(COMMAND))
74         verify(fakeCommand).execute(anyObject(), anyList())
75     }
76 
77     @Test
78     fun testArgsPassedToShellCommand() {
79         var fakeCommand = mock(Command::class.java)
80         registry.registerCommand(COMMAND) { fakeCommand }
81         registry.onShellCommand(writer, arrayOf(COMMAND, "arg1", "arg2", "arg3"))
82         verify(fakeCommand).execute(anyObject(), safeEq(listOf("arg1", "arg2", "arg3")))
83     }
84 
85     class FakeCommand() : Command {
86         override fun execute(pw: PrintWriter, args: List<String>) {
87         }
88 
89         override fun help(pw: PrintWriter) {
90         }
91     }
92 }
93 
94 private const val COMMAND = "test_command"
95