Home | History | Annotate | Download | only in systemui
      1 /*
      2  * Copyright (C) 2019 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
     18 
     19 import android.testing.AndroidTestingRunner
     20 import androidx.test.filters.SmallTest
     21 import org.junit.Assert.assertEquals
     22 import org.junit.Before
     23 import org.junit.Test
     24 import org.junit.runner.RunWith
     25 import org.mockito.ArgumentMatchers.any
     26 import org.mockito.Mock
     27 import org.mockito.Mockito.never
     28 import org.mockito.Mockito.verify
     29 import org.mockito.MockitoAnnotations
     30 import java.io.FileDescriptor
     31 import java.io.PrintWriter
     32 
     33 @RunWith(AndroidTestingRunner::class)
     34 @SmallTest
     35 class DumpControllerTest : SysuiTestCase() {
     36 
     37     private lateinit var controller: DumpController
     38     @Mock private lateinit var callback1: Dumpable
     39     @Mock private lateinit var callback2: Dumpable
     40     @Mock private lateinit var fd: FileDescriptor
     41     @Mock private lateinit var pw: PrintWriter
     42     private val args = emptyArray<String>()
     43 
     44     @Before
     45     fun setUp() {
     46         MockitoAnnotations.initMocks(this)
     47 
     48         controller = DumpController()
     49 //        Debug.waitForDebugger()
     50     }
     51 
     52     @Test
     53     fun testListenerOnlyAddedOnce() {
     54         controller.apply {
     55             addListener(callback1)
     56             addListener(callback1)
     57         }
     58         assertEquals(1, controller.numListeners)
     59 
     60         controller.dump(fd, pw, args)
     61         verify(callback1 /* only once */).dump(fd, pw, args)
     62     }
     63 
     64     @Test
     65     fun testListenersCalledOnDump() {
     66         controller.apply {
     67             addListener(callback1)
     68             addListener(callback2)
     69         }
     70 
     71         controller.dump(fd, pw, args)
     72 
     73         verify(callback1 /* only once */).dump(fd, pw, args)
     74         verify(callback2 /* only once */).dump(fd, pw, args)
     75     }
     76 
     77     @Test
     78     fun testRemoveListener() {
     79         controller.apply {
     80             addListener(callback1)
     81             addListener(callback2)
     82             removeListener(callback1)
     83         }
     84 
     85         controller.dump(fd, pw, args)
     86 
     87         verify(callback1, never()).dump(any(), any(), any())
     88         verify(callback2 /* only once */).dump(fd, pw, args)
     89     }
     90 }
     91