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 android.hardware.devicestate.DeviceState 19 import androidx.test.ext.junit.runners.AndroidJUnit4 20 import androidx.test.filters.SmallTest 21 import com.android.internal.R 22 import com.android.systemui.SysuiTestCase 23 import com.android.systemui.statusbar.phone.FoldStateListener.OnFoldStateChangeListener 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 29 import org.mockito.Mockito.times 30 import org.mockito.Mockito.verify 31 import org.mockito.MockitoAnnotations.initMocks 32 33 @RunWith(AndroidJUnit4::class) 34 @SmallTest 35 class FoldStateListenerTest : SysuiTestCase() { 36 37 @Mock 38 private lateinit var listener: OnFoldStateChangeListener 39 private lateinit var sut: FoldStateListener 40 41 @Before setUpnull42 fun setUp() { 43 initMocks(this) 44 setFoldedStates(DEVICE_STATE_FOLDED.identifier) 45 setGoToSleepStates(DEVICE_STATE_FOLDED.identifier) 46 sut = FoldStateListener(mContext, listener) 47 } 48 49 @Test onStateChanged_stateFolded_notifiesWithFoldedAndGoingToSleepnull50 fun onStateChanged_stateFolded_notifiesWithFoldedAndGoingToSleep() { 51 sut.onDeviceStateChanged(DEVICE_STATE_FOLDED) 52 53 verify(listener).onFoldStateChanged(FOLDED, WILL_GO_TO_SLEEP) 54 } 55 56 @Test onStateChanged_stateHalfFolded_notifiesWithNotFoldedAndNotGoingToSleepnull57 fun onStateChanged_stateHalfFolded_notifiesWithNotFoldedAndNotGoingToSleep() { 58 sut.onDeviceStateChanged(DEVICE_STATE_HALF_FOLDED) 59 60 verify(listener).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 61 } 62 63 @Test onStateChanged_stateUnfolded_notifiesWithNotFoldedAndNotGoingToSleepnull64 fun onStateChanged_stateUnfolded_notifiesWithNotFoldedAndNotGoingToSleep() { 65 sut.onDeviceStateChanged(DEVICE_STATE_UNFOLDED) 66 67 verify(listener).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 68 } 69 70 @Test onStateChanged_stateUnfoldedThenHalfFolded_notifiesOncenull71 fun onStateChanged_stateUnfoldedThenHalfFolded_notifiesOnce() { 72 sut.onDeviceStateChanged(DEVICE_STATE_UNFOLDED) 73 sut.onDeviceStateChanged(DEVICE_STATE_HALF_FOLDED) 74 75 verify(listener, times(1)).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 76 } 77 78 @Test onStateChanged_stateHalfFoldedThenUnfolded_notifiesOncenull79 fun onStateChanged_stateHalfFoldedThenUnfolded_notifiesOnce() { 80 sut.onDeviceStateChanged(DEVICE_STATE_HALF_FOLDED) 81 sut.onDeviceStateChanged(DEVICE_STATE_UNFOLDED) 82 83 verify(listener, times(1)).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 84 } 85 86 @Test onStateChanged_stateHalfFoldedThenFolded_notifiesTwicenull87 fun onStateChanged_stateHalfFoldedThenFolded_notifiesTwice() { 88 sut.onDeviceStateChanged(DEVICE_STATE_HALF_FOLDED) 89 sut.onDeviceStateChanged(DEVICE_STATE_FOLDED) 90 91 val inOrder = Mockito.inOrder(listener) 92 inOrder.verify(listener).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 93 inOrder.verify(listener).onFoldStateChanged(FOLDED, WILL_GO_TO_SLEEP) 94 } 95 96 @Test onStateChanged_stateFoldedThenHalfFolded_notifiesTwicenull97 fun onStateChanged_stateFoldedThenHalfFolded_notifiesTwice() { 98 sut.onDeviceStateChanged(DEVICE_STATE_FOLDED) 99 sut.onDeviceStateChanged(DEVICE_STATE_HALF_FOLDED) 100 101 val inOrder = Mockito.inOrder(listener) 102 inOrder.verify(listener).onFoldStateChanged(FOLDED, WILL_GO_TO_SLEEP) 103 inOrder.verify(listener).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 104 } 105 setGoToSleepStatesnull106 private fun setGoToSleepStates(vararg states: Int) { 107 mContext.orCreateTestableResources.addOverride( 108 R.array.config_deviceStatesOnWhichToSleep, 109 states 110 ) 111 } 112 setFoldedStatesnull113 private fun setFoldedStates(vararg states: Int) { 114 mContext.orCreateTestableResources.addOverride( 115 R.array.config_foldedDeviceStates, 116 states 117 ) 118 } 119 120 companion object { 121 private val DEVICE_STATE_FOLDED = DeviceState( 122 DeviceState.Configuration.Builder(123 /* id */, "FOLDED" /* name */) 123 .build() 124 ) 125 private val DEVICE_STATE_HALF_FOLDED = DeviceState( 126 DeviceState.Configuration.Builder(456 /* id */, "HALF_FOLDED" /* name */) 127 .build() 128 ) 129 private val DEVICE_STATE_UNFOLDED = DeviceState( 130 DeviceState.Configuration.Builder(789 /* id */, "UNFOLDED" /* name */) 131 .build() 132 ) 133 134 private const val FOLDED = true 135 private const val NOT_FOLDED = false 136 137 private const val WILL_GO_TO_SLEEP = true 138 private const val WILL_NOT_SLEEP = false 139 } 140 } 141