Fabian Kozynski | 1cb4aae | 2019-11-25 14:27:00 -0500 | [diff] [blame] | 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.assertFalse |
| 22 | import org.junit.Assert.assertTrue |
| 23 | import org.junit.Before |
| 24 | import org.junit.Test |
| 25 | import org.junit.runner.RunWith |
| 26 | import org.mockito.Mock |
| 27 | import org.mockito.Mockito.never |
| 28 | import org.mockito.Mockito.verify |
| 29 | import org.mockito.MockitoAnnotations |
| 30 | |
| 31 | @RunWith(AndroidTestingRunner::class) |
| 32 | @SmallTest |
| 33 | class BootCompleteCacheTest : SysuiTestCase() { |
| 34 | |
| 35 | private lateinit var bootCompleteCache: BootCompleteCacheImpl |
| 36 | @Mock |
| 37 | private lateinit var bootCompleteListener: BootCompleteCache.BootCompleteListener |
| 38 | @Mock |
| 39 | private lateinit var dumpController: DumpController |
| 40 | |
| 41 | @Before |
| 42 | fun setUp() { |
| 43 | MockitoAnnotations.initMocks(this) |
| 44 | |
| 45 | bootCompleteCache = BootCompleteCacheImpl(dumpController) |
| 46 | } |
| 47 | |
| 48 | @Test |
| 49 | fun testFlagChange() { |
| 50 | assertFalse(bootCompleteCache.isBootComplete()) |
| 51 | |
| 52 | bootCompleteCache.setBootComplete() |
| 53 | |
| 54 | assertTrue(bootCompleteCache.isBootComplete()) |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | fun testDoubleSetIsNoOp() { |
| 59 | assertFalse(bootCompleteCache.isBootComplete()) |
| 60 | |
| 61 | bootCompleteCache.setBootComplete() |
| 62 | bootCompleteCache.setBootComplete() |
| 63 | |
| 64 | assertTrue(bootCompleteCache.isBootComplete()) |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | fun testAddListenerGivesCurrentState_false() { |
| 69 | val boot = bootCompleteCache.addListener(bootCompleteListener) |
| 70 | assertFalse(boot) |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | fun testAddListenerGivesCurrentState_true() { |
| 75 | bootCompleteCache.setBootComplete() |
| 76 | val boot = bootCompleteCache.addListener(bootCompleteListener) |
| 77 | assertTrue(boot) |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | fun testListenerCalledOnBootComplete() { |
| 82 | bootCompleteCache.addListener(bootCompleteListener) |
| 83 | |
| 84 | bootCompleteCache.setBootComplete() |
| 85 | verify(bootCompleteListener).onBootComplete() |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | fun testListenerCalledOnBootComplete_onlyOnce() { |
| 90 | bootCompleteCache.addListener(bootCompleteListener) |
| 91 | |
| 92 | bootCompleteCache.setBootComplete() |
| 93 | bootCompleteCache.setBootComplete() |
| 94 | |
| 95 | verify(bootCompleteListener).onBootComplete() |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | fun testListenerNotCalledIfBootIsAlreadyComplete() { |
| 100 | bootCompleteCache.setBootComplete() |
| 101 | |
| 102 | bootCompleteCache.addListener(bootCompleteListener) |
| 103 | |
| 104 | bootCompleteCache.setBootComplete() |
| 105 | |
| 106 | verify(bootCompleteListener, never()).onBootComplete() |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | fun testListenerRemovedNotCalled() { |
| 111 | bootCompleteCache.addListener(bootCompleteListener) |
| 112 | |
| 113 | bootCompleteCache.removeListener(bootCompleteListener) |
| 114 | |
| 115 | bootCompleteCache.setBootComplete() |
| 116 | |
| 117 | verify(bootCompleteListener, never()).onBootComplete() |
| 118 | } |
| 119 | } |