1/* 2 * Copyright (C) 2017 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 17package androidx.core.transition 18 19import android.support.test.InstrumentationRegistry 20import android.support.test.filters.SdkSuppress 21import android.support.test.rule.ActivityTestRule 22import android.transition.Fade 23import android.transition.Transition 24import android.transition.TransitionManager 25import android.view.View 26import android.view.ViewGroup 27import android.widget.ImageView 28import androidx.core.TestActivity 29import androidx.core.ktx.test.R 30import org.junit.Assert.assertTrue 31import org.junit.Before 32import org.junit.Rule 33import org.junit.Test 34import java.util.concurrent.CountDownLatch 35import java.util.concurrent.TimeUnit 36import java.util.concurrent.atomic.AtomicBoolean 37 38@SdkSuppress(minSdkVersion = 19) 39class TransitionTest { 40 @JvmField @Rule val rule = ActivityTestRule<TestActivity>(TestActivity::class.java) 41 42 private lateinit var transition: Transition 43 44 @Before fun setup() { 45 transition = Fade().setDuration(50) 46 } 47 48 @Test fun testDoOnStart() { 49 val called = AtomicBoolean() 50 transition.doOnStart { 51 called.set(true) 52 } 53 54 startTransition(transition) 55 assertTrue(called.get()) 56 } 57 58 @Test fun testDoOnEnd() { 59 val called = AtomicBoolean() 60 transition.doOnEnd { 61 called.set(true) 62 } 63 64 val latch = CountDownLatch(1) 65 transition.addListener(object : Transition.TransitionListener { 66 override fun onTransitionEnd(transition: Transition?) { 67 latch.countDown() 68 } 69 70 override fun onTransitionResume(transition: Transition?) = Unit 71 override fun onTransitionPause(transition: Transition?) = Unit 72 override fun onTransitionCancel(transition: Transition?) = Unit 73 override fun onTransitionStart(transition: Transition?) = Unit 74 }) 75 76 startTransition(transition) 77 78 assertTrue(latch.await(1, TimeUnit.SECONDS)) 79 InstrumentationRegistry.getInstrumentation().waitForIdleSync() 80 81 assertTrue(called.get()) 82 } 83 84 private fun startTransition(t: Transition) { 85 rule.runOnUiThread { 86 val sceneRoot = rule.activity.findViewById<ViewGroup>(R.id.root) 87 val view = rule.activity.findViewById<ImageView>(R.id.image_view) 88 89 TransitionManager.beginDelayedTransition(sceneRoot, t) 90 91 view.visibility = View.INVISIBLE 92 } 93 InstrumentationRegistry.getInstrumentation().waitForIdleSync() 94 } 95} 96