1 /* 2 * Copyright (C) 2023 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.intentresolver 18 19 import android.content.res.Resources 20 import android.view.View 21 import android.view.Window 22 import androidx.activity.ComponentActivity 23 import androidx.lifecycle.Lifecycle 24 import androidx.lifecycle.testing.TestLifecycleOwner 25 import kotlinx.coroutines.Dispatchers 26 import kotlinx.coroutines.ExperimentalCoroutinesApi 27 import kotlinx.coroutines.test.StandardTestDispatcher 28 import kotlinx.coroutines.test.TestCoroutineScheduler 29 import kotlinx.coroutines.test.resetMain 30 import kotlinx.coroutines.test.setMain 31 import org.junit.After 32 import org.junit.Before 33 import org.junit.Test 34 import org.mockito.Mockito.anyInt 35 import org.mockito.Mockito.never 36 import org.mockito.Mockito.times 37 import org.mockito.Mockito.verify 38 39 private const val TIMEOUT_MS = 200 40 41 @OptIn(ExperimentalCoroutinesApi::class) 42 class EnterTransitionAnimationDelegateTest { 43 private val elementName = "shared-element" 44 private val scheduler = TestCoroutineScheduler() 45 private val dispatcher = StandardTestDispatcher(scheduler) 46 private val lifecycleOwner = TestLifecycleOwner() 47 48 private val transitionTargetView = <lambda>null49 mock<View> { 50 // avoid the request-layout path in the delegate 51 whenever(isInLayout).thenReturn(true) 52 } 53 54 private val windowMock = mock<Window>() 55 private val resourcesMock = <lambda>null56 mock<Resources> { whenever(getInteger(anyInt())).thenReturn(TIMEOUT_MS) } 57 private val activity = <lambda>null58 mock<ComponentActivity> { 59 whenever(lifecycle).thenReturn(lifecycleOwner.lifecycle) 60 whenever(resources).thenReturn(resourcesMock) 61 whenever(isActivityTransitionRunning).thenReturn(true) 62 whenever(window).thenReturn(windowMock) 63 } 64 <lambda>null65 private val testSubject = EnterTransitionAnimationDelegate(activity) { transitionTargetView } 66 67 @Before setupnull68 fun setup() { 69 Dispatchers.setMain(dispatcher) 70 lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) 71 } 72 73 @After cleanupnull74 fun cleanup() { 75 lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) 76 Dispatchers.resetMain() 77 } 78 79 @Test test_postponeTransition_timeoutnull80 fun test_postponeTransition_timeout() { 81 testSubject.postponeTransition() 82 testSubject.markOffsetCalculated() 83 84 scheduler.advanceTimeBy(TIMEOUT_MS + 1L) 85 verify(activity, times(1)).startPostponedEnterTransition() 86 verify(windowMock, never()).setWindowAnimations(anyInt()) 87 } 88 89 @Test test_postponeTransition_animation_resumes_only_oncenull90 fun test_postponeTransition_animation_resumes_only_once() { 91 testSubject.postponeTransition() 92 testSubject.markOffsetCalculated() 93 testSubject.onTransitionElementReady(elementName) 94 testSubject.markOffsetCalculated() 95 testSubject.onTransitionElementReady(elementName) 96 97 scheduler.advanceTimeBy(TIMEOUT_MS + 1L) 98 verify(activity, times(1)).startPostponedEnterTransition() 99 } 100 101 @Test test_postponeTransition_resume_animation_conditionsnull102 fun test_postponeTransition_resume_animation_conditions() { 103 testSubject.postponeTransition() 104 verify(activity, never()).startPostponedEnterTransition() 105 106 testSubject.markOffsetCalculated() 107 verify(activity, never()).startPostponedEnterTransition() 108 109 testSubject.onAllTransitionElementsReady() 110 verify(activity, times(1)).startPostponedEnterTransition() 111 } 112 } 113