1 /* 2 * Copyright (C) 2022 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.unfold 18 19 import android.testing.AndroidTestingRunner 20 import androidx.test.filters.SmallTest 21 import com.android.systemui.SysuiTestCase 22 import com.android.systemui.unfold.FoldStateLoggingProvider.FoldStateLoggingListener 23 import com.android.systemui.unfold.updates.FOLD_UPDATE_FINISH_CLOSED 24 import com.android.systemui.unfold.updates.FOLD_UPDATE_FINISH_FULL_OPEN 25 import com.android.systemui.unfold.updates.FOLD_UPDATE_FINISH_HALF_OPEN 26 import com.android.systemui.unfold.updates.FOLD_UPDATE_START_CLOSING 27 import com.android.systemui.unfold.updates.FOLD_UPDATE_START_OPENING 28 import com.android.systemui.unfold.updates.FoldStateProvider.FoldUpdate 29 import com.android.systemui.unfold.util.TestFoldStateProvider 30 import com.android.systemui.util.time.FakeSystemClock 31 import com.google.common.truth.Truth.assertThat 32 import org.junit.Before 33 import org.junit.Test 34 import org.junit.runner.RunWith 35 import org.mockito.MockitoAnnotations 36 37 @RunWith(AndroidTestingRunner::class) 38 @SmallTest 39 class FoldStateLoggingProviderTest : SysuiTestCase() { 40 41 private val testFoldStateProvider = TestFoldStateProvider() 42 private val fakeClock = FakeSystemClock() 43 44 private lateinit var foldStateLoggingProvider: FoldStateLoggingProvider 45 46 private val foldLoggingUpdates: MutableList<FoldStateChange> = arrayListOf() 47 48 private val foldStateLoggingListener = 49 object : FoldStateLoggingListener { onFoldUpdatenull50 override fun onFoldUpdate(foldStateUpdate: FoldStateChange) { 51 foldLoggingUpdates.add(foldStateUpdate) 52 } 53 } 54 55 @Before setUpnull56 fun setUp() { 57 MockitoAnnotations.initMocks(this) 58 59 foldStateLoggingProvider = 60 FoldStateLoggingProviderImpl(testFoldStateProvider, fakeClock).apply { 61 addCallback(foldStateLoggingListener) 62 init() 63 } 64 } 65 66 @Test onFoldUpdate_noPreviousOne_finishHalfOpen_nothingReportednull67 fun onFoldUpdate_noPreviousOne_finishHalfOpen_nothingReported() { 68 sendFoldUpdate(FOLD_UPDATE_FINISH_HALF_OPEN) 69 70 assertThat(foldLoggingUpdates).isEmpty() 71 } 72 73 @Test onFoldUpdate_noPreviousOne_finishFullOpen_nothingReportednull74 fun onFoldUpdate_noPreviousOne_finishFullOpen_nothingReported() { 75 sendFoldUpdate(FOLD_UPDATE_FINISH_FULL_OPEN) 76 77 assertThat(foldLoggingUpdates).isEmpty() 78 } 79 80 @Test onFoldUpdate_noPreviousOne_finishClosed_nothingReportednull81 fun onFoldUpdate_noPreviousOne_finishClosed_nothingReported() { 82 sendFoldUpdate(FOLD_UPDATE_FINISH_CLOSED) 83 84 assertThat(foldLoggingUpdates).isEmpty() 85 } 86 87 @Test onFoldUpdate_startOpening_fullOpen_changeReportednull88 fun onFoldUpdate_startOpening_fullOpen_changeReported() { 89 val dtTime = 10L 90 91 sendFoldUpdate(FOLD_UPDATE_START_OPENING) 92 fakeClock.advanceTime(dtTime) 93 sendFoldUpdate(FOLD_UPDATE_FINISH_FULL_OPEN) 94 95 assertThat(foldLoggingUpdates) 96 .containsExactly(FoldStateChange(FULLY_CLOSED, FULLY_OPENED, dtTime)) 97 } 98 99 @Test onFoldUpdate_startClosingThenFinishClosed_noInitialState_nothingReportednull100 fun onFoldUpdate_startClosingThenFinishClosed_noInitialState_nothingReported() { 101 val dtTime = 10L 102 103 sendFoldUpdate(FOLD_UPDATE_START_CLOSING) 104 fakeClock.advanceTime(dtTime) 105 sendFoldUpdate(FOLD_UPDATE_FINISH_CLOSED) 106 107 assertThat(foldLoggingUpdates).isEmpty() 108 } 109 110 @Test onFoldUpdate_startClosingThenFinishClosed_initiallyOpened_changeReportednull111 fun onFoldUpdate_startClosingThenFinishClosed_initiallyOpened_changeReported() { 112 val dtTime = 10L 113 sendFoldUpdate(FOLD_UPDATE_FINISH_FULL_OPEN) 114 115 sendFoldUpdate(FOLD_UPDATE_START_CLOSING) 116 fakeClock.advanceTime(dtTime) 117 sendFoldUpdate(FOLD_UPDATE_FINISH_CLOSED) 118 119 assertThat(foldLoggingUpdates) 120 .containsExactly(FoldStateChange(FULLY_OPENED, FULLY_CLOSED, dtTime)) 121 } 122 123 @Test onFoldUpdate_startOpeningThenHalf_initiallyClosed_changeReportednull124 fun onFoldUpdate_startOpeningThenHalf_initiallyClosed_changeReported() { 125 val dtTime = 10L 126 sendFoldUpdate(FOLD_UPDATE_FINISH_CLOSED) 127 128 sendFoldUpdate(FOLD_UPDATE_START_OPENING) 129 fakeClock.advanceTime(dtTime) 130 sendFoldUpdate(FOLD_UPDATE_FINISH_HALF_OPEN) 131 132 assertThat(foldLoggingUpdates) 133 .containsExactly(FoldStateChange(FULLY_CLOSED, HALF_OPENED, dtTime)) 134 } 135 136 @Test onFoldUpdate_startClosingThenHalf_initiallyOpened_changeReportednull137 fun onFoldUpdate_startClosingThenHalf_initiallyOpened_changeReported() { 138 val dtTime = 10L 139 sendFoldUpdate(FOLD_UPDATE_FINISH_FULL_OPEN) 140 141 sendFoldUpdate(FOLD_UPDATE_START_CLOSING) 142 fakeClock.advanceTime(dtTime) 143 sendFoldUpdate(FOLD_UPDATE_FINISH_HALF_OPEN) 144 145 assertThat(foldLoggingUpdates) 146 .containsExactly(FoldStateChange(FULLY_OPENED, HALF_OPENED, dtTime)) 147 } 148 149 @Test onFoldUpdate_foldThenUnfold_multipleReportednull150 fun onFoldUpdate_foldThenUnfold_multipleReported() { 151 val foldTime = 24L 152 val unfoldTime = 42L 153 val waitingTime = 424L 154 sendFoldUpdate(FOLD_UPDATE_FINISH_FULL_OPEN) 155 156 // Fold 157 sendFoldUpdate(FOLD_UPDATE_START_CLOSING) 158 fakeClock.advanceTime(foldTime) 159 sendFoldUpdate(FOLD_UPDATE_FINISH_CLOSED) 160 fakeClock.advanceTime(waitingTime) 161 // unfold 162 sendFoldUpdate(FOLD_UPDATE_START_OPENING) 163 fakeClock.advanceTime(unfoldTime) 164 sendFoldUpdate(FOLD_UPDATE_FINISH_FULL_OPEN) 165 166 assertThat(foldLoggingUpdates) 167 .containsExactly( 168 FoldStateChange(FULLY_OPENED, FULLY_CLOSED, foldTime), 169 FoldStateChange(FULLY_CLOSED, FULLY_OPENED, unfoldTime)) 170 } 171 172 @Test uninit_removesCallbacknull173 fun uninit_removesCallback() { 174 foldStateLoggingProvider.uninit() 175 176 assertThat(testFoldStateProvider.hasListeners).isFalse() 177 } 178 sendFoldUpdatenull179 private fun sendFoldUpdate(@FoldUpdate foldUpdate: Int) { 180 testFoldStateProvider.sendFoldUpdate(foldUpdate) 181 } 182 } 183