1 /* 2 * Copyright (C) 2021 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 package com.android.systemui.statusbar.phone 17 18 import androidx.test.ext.junit.runners.AndroidJUnit4 19 import androidx.test.filters.SmallTest 20 import com.android.internal.R 21 import com.android.systemui.SysuiTestCase 22 import com.android.systemui.foldedDeviceStateList 23 import com.android.systemui.halfFoldedDeviceState 24 import com.android.systemui.kosmos.Kosmos 25 import com.android.systemui.statusbar.phone.FoldStateListener.OnFoldStateChangeListener 26 import com.android.systemui.unfoldedDeviceState 27 import org.junit.Before 28 import org.junit.Test 29 import org.junit.runner.RunWith 30 import org.mockito.Mock 31 import org.mockito.Mockito 32 import org.mockito.Mockito.times 33 import org.mockito.Mockito.verify 34 import org.mockito.MockitoAnnotations.initMocks 35 36 @RunWith(AndroidJUnit4::class) 37 @SmallTest 38 class FoldStateListenerTest : SysuiTestCase() { 39 40 @Mock private lateinit var listener: OnFoldStateChangeListener 41 private lateinit var sut: FoldStateListener 42 43 @Before setUpnull44 fun setUp() { 45 initMocks(this) 46 setFoldedStates(DEVICE_STATE_FOLDED.identifier) 47 setGoToSleepStates(DEVICE_STATE_FOLDED.identifier) 48 sut = FoldStateListener(mContext, listener) 49 } 50 51 @Test onStateChanged_stateFolded_notifiesWithFoldedAndGoingToSleepnull52 fun onStateChanged_stateFolded_notifiesWithFoldedAndGoingToSleep() { 53 sut.onDeviceStateChanged(DEVICE_STATE_FOLDED) 54 55 verify(listener).onFoldStateChanged(FOLDED, WILL_GO_TO_SLEEP) 56 } 57 58 @Test onStateChanged_stateHalfFolded_notifiesWithNotFoldedAndNotGoingToSleepnull59 fun onStateChanged_stateHalfFolded_notifiesWithNotFoldedAndNotGoingToSleep() { 60 sut.onDeviceStateChanged(DEVICE_STATE_HALF_FOLDED) 61 62 verify(listener).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 63 } 64 65 @Test onStateChanged_stateUnfolded_notifiesWithNotFoldedAndNotGoingToSleepnull66 fun onStateChanged_stateUnfolded_notifiesWithNotFoldedAndNotGoingToSleep() { 67 sut.onDeviceStateChanged(DEVICE_STATE_UNFOLDED) 68 69 verify(listener).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 70 } 71 72 @Test onStateChanged_stateUnfoldedThenHalfFolded_notifiesOncenull73 fun onStateChanged_stateUnfoldedThenHalfFolded_notifiesOnce() { 74 sut.onDeviceStateChanged(DEVICE_STATE_UNFOLDED) 75 sut.onDeviceStateChanged(DEVICE_STATE_HALF_FOLDED) 76 77 verify(listener, times(1)).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 78 } 79 80 @Test onStateChanged_stateHalfFoldedThenUnfolded_notifiesOncenull81 fun onStateChanged_stateHalfFoldedThenUnfolded_notifiesOnce() { 82 sut.onDeviceStateChanged(DEVICE_STATE_HALF_FOLDED) 83 sut.onDeviceStateChanged(DEVICE_STATE_UNFOLDED) 84 85 verify(listener, times(1)).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 86 } 87 88 @Test onStateChanged_stateHalfFoldedThenFolded_notifiesTwicenull89 fun onStateChanged_stateHalfFoldedThenFolded_notifiesTwice() { 90 sut.onDeviceStateChanged(DEVICE_STATE_HALF_FOLDED) 91 sut.onDeviceStateChanged(DEVICE_STATE_FOLDED) 92 93 val inOrder = Mockito.inOrder(listener) 94 inOrder.verify(listener).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 95 inOrder.verify(listener).onFoldStateChanged(FOLDED, WILL_GO_TO_SLEEP) 96 } 97 98 @Test onStateChanged_stateFoldedThenHalfFolded_notifiesTwicenull99 fun onStateChanged_stateFoldedThenHalfFolded_notifiesTwice() { 100 sut.onDeviceStateChanged(DEVICE_STATE_FOLDED) 101 sut.onDeviceStateChanged(DEVICE_STATE_HALF_FOLDED) 102 103 val inOrder = Mockito.inOrder(listener) 104 inOrder.verify(listener).onFoldStateChanged(FOLDED, WILL_GO_TO_SLEEP) 105 inOrder.verify(listener).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 106 } 107 setGoToSleepStatesnull108 private fun setGoToSleepStates(vararg states: Int) { 109 mContext.orCreateTestableResources.addOverride( 110 R.array.config_deviceStatesOnWhichToSleep, 111 states 112 ) 113 } 114 setFoldedStatesnull115 private fun setFoldedStates(vararg states: Int) { 116 mContext.orCreateTestableResources.addOverride(R.array.config_foldedDeviceStates, states) 117 } 118 119 companion object { 120 private val DEVICE_STATE_FOLDED = Kosmos().foldedDeviceStateList.first() 121 private val DEVICE_STATE_HALF_FOLDED = Kosmos().halfFoldedDeviceState 122 private val DEVICE_STATE_UNFOLDED = Kosmos().unfoldedDeviceState 123 124 private const val FOLDED = true 125 private const val NOT_FOLDED = false 126 127 private const val WILL_GO_TO_SLEEP = true 128 private const val WILL_NOT_SLEEP = false 129 } 130 } 131