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.keyguard.ui.viewmodel 18 19 import androidx.test.ext.junit.runners.AndroidJUnit4 20 import androidx.test.filters.SmallTest 21 import com.android.systemui.RoboPilotTest 22 import com.android.systemui.SysuiTestCase 23 import com.android.systemui.keyguard.data.repository.FakeKeyguardTransitionRepository 24 import com.android.systemui.keyguard.domain.interactor.KeyguardTransitionInteractorFactory 25 import com.android.systemui.keyguard.shared.model.KeyguardState 26 import com.android.systemui.keyguard.shared.model.TransitionState 27 import com.android.systemui.keyguard.shared.model.TransitionStep 28 import com.google.common.collect.Range 29 import com.google.common.truth.Truth.assertThat 30 import kotlinx.coroutines.flow.launchIn 31 import kotlinx.coroutines.flow.onEach 32 import kotlinx.coroutines.test.TestScope 33 import kotlinx.coroutines.test.UnconfinedTestDispatcher 34 import kotlinx.coroutines.test.runTest 35 import org.junit.Before 36 import org.junit.Test 37 import org.junit.runner.RunWith 38 39 @SmallTest 40 @RoboPilotTest 41 @RunWith(AndroidJUnit4::class) 42 class OccludedToLockscreenTransitionViewModelTest : SysuiTestCase() { 43 private lateinit var underTest: OccludedToLockscreenTransitionViewModel 44 private lateinit var repository: FakeKeyguardTransitionRepository 45 46 @Before setUpnull47 fun setUp() { 48 repository = FakeKeyguardTransitionRepository() 49 val interactor = 50 KeyguardTransitionInteractorFactory.create( 51 scope = TestScope().backgroundScope, 52 repository = repository, 53 ) 54 .keyguardTransitionInteractor 55 underTest = OccludedToLockscreenTransitionViewModel(interactor) 56 } 57 58 @Test lockscreenFadeInnull59 fun lockscreenFadeIn() = 60 runTest(UnconfinedTestDispatcher()) { 61 val values = mutableListOf<Float>() 62 63 val job = underTest.lockscreenAlpha.onEach { values.add(it) }.launchIn(this) 64 65 repository.sendTransitionStep(step(0f, TransitionState.STARTED)) 66 repository.sendTransitionStep(step(0.1f)) 67 // Should start running here... 68 repository.sendTransitionStep(step(0.3f)) 69 repository.sendTransitionStep(step(0.4f)) 70 repository.sendTransitionStep(step(0.5f)) 71 repository.sendTransitionStep(step(0.6f)) 72 // ...up to here 73 repository.sendTransitionStep(step(0.8f)) 74 repository.sendTransitionStep(step(1f)) 75 76 assertThat(values.size).isEqualTo(5) 77 values.forEach { assertThat(it).isIn(Range.closed(0f, 1f)) } 78 79 job.cancel() 80 } 81 82 @Test lockscreenTranslationYnull83 fun lockscreenTranslationY() = 84 runTest(UnconfinedTestDispatcher()) { 85 val values = mutableListOf<Float>() 86 87 val pixels = 100 88 val job = 89 underTest.lockscreenTranslationY(pixels).onEach { values.add(it) }.launchIn(this) 90 91 repository.sendTransitionStep(step(0f, TransitionState.STARTED)) 92 repository.sendTransitionStep(step(0f)) 93 repository.sendTransitionStep(step(0.3f)) 94 repository.sendTransitionStep(step(0.5f)) 95 repository.sendTransitionStep(step(1f)) 96 97 assertThat(values.size).isEqualTo(5) 98 values.forEach { assertThat(it).isIn(Range.closed(-100f, 0f)) } 99 100 job.cancel() 101 } 102 103 @Test lockscreenTranslationYResettedAfterJobCancellednull104 fun lockscreenTranslationYResettedAfterJobCancelled() = 105 runTest(UnconfinedTestDispatcher()) { 106 val values = mutableListOf<Float>() 107 108 val pixels = 100 109 val job = 110 underTest.lockscreenTranslationY(pixels).onEach { values.add(it) }.launchIn(this) 111 repository.sendTransitionStep(step(0.5f, TransitionState.CANCELED)) 112 113 assertThat(values.last()).isEqualTo(0f) 114 115 job.cancel() 116 } 117 stepnull118 private fun step( 119 value: Float, 120 state: TransitionState = TransitionState.RUNNING 121 ): TransitionStep { 122 return TransitionStep( 123 from = KeyguardState.OCCLUDED, 124 to = KeyguardState.LOCKSCREEN, 125 value = value, 126 transitionState = state, 127 ownerName = "OccludedToLockscreenTransitionViewModelTest" 128 ) 129 } 130 } 131