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.testing.AndroidTestingRunner 19 import androidx.test.filters.SmallTest 20 import com.android.internal.R 21 import com.android.systemui.SysuiTestCase 22 import com.android.systemui.statusbar.phone.FoldStateListener.OnFoldStateChangeListener 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 28 import org.mockito.Mockito.times 29 import org.mockito.Mockito.verify 30 import org.mockito.MockitoAnnotations.initMocks 31 32 @RunWith(AndroidTestingRunner::class) 33 @SmallTest 34 class FoldStateListenerTest : SysuiTestCase() { 35 36 @Mock 37 private lateinit var listener: OnFoldStateChangeListener 38 private lateinit var sut: FoldStateListener 39 40 @Before 41 fun setUp() { 42 initMocks(this) 43 setFoldedStates(DEVICE_STATE_FOLDED) 44 setGoToSleepStates(DEVICE_STATE_FOLDED) 45 sut = FoldStateListener(mContext, listener) 46 } 47 48 @Test 49 fun onStateChanged_stateFolded_notifiesWithFoldedAndGoingToSleep() { 50 sut.onStateChanged(DEVICE_STATE_FOLDED) 51 52 verify(listener).onFoldStateChanged(FOLDED, WILL_GO_TO_SLEEP) 53 } 54 55 @Test 56 fun onStateChanged_stateHalfFolded_notifiesWithNotFoldedAndNotGoingToSleep() { 57 sut.onStateChanged(DEVICE_STATE_HALF_FOLDED) 58 59 verify(listener).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 60 } 61 62 @Test 63 fun onStateChanged_stateUnfolded_notifiesWithNotFoldedAndNotGoingToSleep() { 64 sut.onStateChanged(DEVICE_STATE_UNFOLDED) 65 66 verify(listener).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 67 } 68 69 @Test 70 fun onStateChanged_stateUnfoldedThenHalfFolded_notifiesOnce() { 71 sut.onStateChanged(DEVICE_STATE_UNFOLDED) 72 sut.onStateChanged(DEVICE_STATE_HALF_FOLDED) 73 74 verify(listener, times(1)).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 75 } 76 77 @Test 78 fun onStateChanged_stateHalfFoldedThenUnfolded_notifiesOnce() { 79 sut.onStateChanged(DEVICE_STATE_HALF_FOLDED) 80 sut.onStateChanged(DEVICE_STATE_UNFOLDED) 81 82 verify(listener, times(1)).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 83 } 84 85 @Test 86 fun onStateChanged_stateHalfFoldedThenFolded_notifiesTwice() { 87 sut.onStateChanged(DEVICE_STATE_HALF_FOLDED) 88 sut.onStateChanged(DEVICE_STATE_FOLDED) 89 90 val inOrder = Mockito.inOrder(listener) 91 inOrder.verify(listener).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 92 inOrder.verify(listener).onFoldStateChanged(FOLDED, WILL_GO_TO_SLEEP) 93 } 94 95 @Test 96 fun onStateChanged_stateFoldedThenHalfFolded_notifiesTwice() { 97 sut.onStateChanged(DEVICE_STATE_FOLDED) 98 sut.onStateChanged(DEVICE_STATE_HALF_FOLDED) 99 100 val inOrder = Mockito.inOrder(listener) 101 inOrder.verify(listener).onFoldStateChanged(FOLDED, WILL_GO_TO_SLEEP) 102 inOrder.verify(listener).onFoldStateChanged(NOT_FOLDED, WILL_NOT_SLEEP) 103 } 104 105 private fun setGoToSleepStates(vararg states: Int) { 106 mContext.orCreateTestableResources.addOverride( 107 R.array.config_deviceStatesOnWhichToSleep, 108 states 109 ) 110 } 111 112 private fun setFoldedStates(vararg states: Int) { 113 mContext.orCreateTestableResources.addOverride( 114 R.array.config_foldedDeviceStates, 115 states 116 ) 117 } 118 119 companion object { 120 private const val DEVICE_STATE_FOLDED = 123 121 private const val DEVICE_STATE_HALF_FOLDED = 456 122 private const val DEVICE_STATE_UNFOLDED = 789 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