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 com.android.systemui.dump.DumpManager 22 import org.junit.Assert.assertFalse 23 import org.junit.Assert.assertTrue 24 import org.junit.Before 25 import org.junit.Test 26 import org.junit.runner.RunWith 27 import org.mockito.Mock 28 import org.mockito.Mockito.mock 29 import org.mockito.Mockito.never 30 import org.mockito.Mockito.verify 31 import org.mockito.MockitoAnnotations 32 33 @RunWith(AndroidTestingRunner::class) 34 @SmallTest 35 class BootCompleteCacheTest : SysuiTestCase() { 36 37 private lateinit var bootCompleteCache: BootCompleteCacheImpl 38 @Mock 39 private lateinit var bootCompleteListener: BootCompleteCache.BootCompleteListener 40 41 @Before setUpnull42 fun setUp() { 43 MockitoAnnotations.initMocks(this) 44 45 bootCompleteCache = BootCompleteCacheImpl(mock(DumpManager::class.java)) 46 } 47 48 @Test testFlagChangenull49 fun testFlagChange() { 50 assertFalse(bootCompleteCache.isBootComplete()) 51 52 bootCompleteCache.setBootComplete() 53 54 assertTrue(bootCompleteCache.isBootComplete()) 55 } 56 57 @Test testDoubleSetIsNoOpnull58 fun testDoubleSetIsNoOp() { 59 assertFalse(bootCompleteCache.isBootComplete()) 60 61 bootCompleteCache.setBootComplete() 62 bootCompleteCache.setBootComplete() 63 64 assertTrue(bootCompleteCache.isBootComplete()) 65 } 66 67 @Test testAddListenerGivesCurrentState_falsenull68 fun testAddListenerGivesCurrentState_false() { 69 val boot = bootCompleteCache.addListener(bootCompleteListener) 70 assertFalse(boot) 71 } 72 73 @Test testAddListenerGivesCurrentState_truenull74 fun testAddListenerGivesCurrentState_true() { 75 bootCompleteCache.setBootComplete() 76 val boot = bootCompleteCache.addListener(bootCompleteListener) 77 assertTrue(boot) 78 } 79 80 @Test testListenerCalledOnBootCompletenull81 fun testListenerCalledOnBootComplete() { 82 bootCompleteCache.addListener(bootCompleteListener) 83 84 bootCompleteCache.setBootComplete() 85 verify(bootCompleteListener).onBootComplete() 86 } 87 88 @Test testListenerCalledOnBootComplete_onlyOncenull89 fun testListenerCalledOnBootComplete_onlyOnce() { 90 bootCompleteCache.addListener(bootCompleteListener) 91 92 bootCompleteCache.setBootComplete() 93 bootCompleteCache.setBootComplete() 94 95 verify(bootCompleteListener).onBootComplete() 96 } 97 98 @Test testListenerNotCalledIfBootIsAlreadyCompletenull99 fun testListenerNotCalledIfBootIsAlreadyComplete() { 100 bootCompleteCache.setBootComplete() 101 102 bootCompleteCache.addListener(bootCompleteListener) 103 104 bootCompleteCache.setBootComplete() 105 106 verify(bootCompleteListener, never()).onBootComplete() 107 } 108 109 @Test testListenerRemovedNotCallednull110 fun testListenerRemovedNotCalled() { 111 bootCompleteCache.addListener(bootCompleteListener) 112 113 bootCompleteCache.removeListener(bootCompleteListener) 114 115 bootCompleteCache.setBootComplete() 116 117 verify(bootCompleteListener, never()).onBootComplete() 118 } 119 }