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.lifecycle; 18 19import static androidx.lifecycle.TestUtils.OrderedTuples.CREATE; 20import static androidx.lifecycle.TestUtils.OrderedTuples.DESTROY; 21import static androidx.lifecycle.TestUtils.OrderedTuples.PAUSE; 22import static androidx.lifecycle.TestUtils.OrderedTuples.RESUME; 23import static androidx.lifecycle.TestUtils.OrderedTuples.START; 24import static androidx.lifecycle.TestUtils.OrderedTuples.STOP; 25import static androidx.lifecycle.TestUtils.flatMap; 26 27import static org.hamcrest.MatcherAssert.assertThat; 28import static org.hamcrest.Matchers.is; 29 30import android.app.Activity; 31import android.support.test.filters.LargeTest; 32import android.support.test.rule.ActivityTestRule; 33 34import androidx.core.util.Pair; 35import androidx.lifecycle.Lifecycle.Event; 36import androidx.lifecycle.testapp.CollectingLifecycleOwner; 37import androidx.lifecycle.testapp.CollectingSupportActivity; 38import androidx.lifecycle.testapp.FrameworkLifecycleRegistryActivity; 39import androidx.lifecycle.testapp.TestEvent; 40 41import org.junit.Rule; 42import org.junit.Test; 43import org.junit.runner.RunWith; 44import org.junit.runners.Parameterized; 45 46import java.util.List; 47 48@LargeTest 49@RunWith(Parameterized.class) 50public class ActivityFullLifecycleTest { 51 @Rule 52 public final ActivityTestRule<? extends CollectingLifecycleOwner> activityTestRule; 53 54 @Parameterized.Parameters 55 public static Class[] params() { 56 return new Class[]{CollectingSupportActivity.class, 57 FrameworkLifecycleRegistryActivity.class}; 58 } 59 60 public ActivityFullLifecycleTest(Class<? extends Activity> activityClass) { 61 //noinspection unchecked 62 activityTestRule = new ActivityTestRule(activityClass); 63 } 64 65 66 @Test 67 public void testFullLifecycle() throws Throwable { 68 CollectingLifecycleOwner owner = activityTestRule.getActivity(); 69 TestUtils.waitTillResumed(owner, activityTestRule); 70 activityTestRule.finishActivity(); 71 72 TestUtils.waitTillDestroyed(owner, activityTestRule); 73 List<Pair<TestEvent, Event>> results = owner.copyCollectedEvents(); 74 assertThat(results, is(flatMap(CREATE, START, RESUME, PAUSE, STOP, DESTROY))); 75 } 76} 77