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 android.arch.lifecycle; 18 19import static android.arch.lifecycle.Lifecycle.State.CREATED; 20 21import static org.hamcrest.MatcherAssert.assertThat; 22import static org.hamcrest.Matchers.is; 23import static org.hamcrest.Matchers.isIn; 24import static org.hamcrest.Matchers.notNullValue; 25 26import android.arch.lifecycle.Lifecycle.Event; 27import android.arch.lifecycle.Lifecycle.State; 28import android.arch.lifecycle.testapp.SimpleAppLifecycleTestActivity; 29import android.arch.lifecycle.testapp.SimpleAppLifecycleTestActivity.TestEventType; 30import android.support.test.filters.LargeTest; 31import android.support.test.rule.ActivityTestRule; 32import android.support.test.runner.AndroidJUnit4; 33import android.util.Pair; 34 35import org.junit.After; 36import org.junit.Before; 37import org.junit.Rule; 38import org.junit.Test; 39import org.junit.runner.RunWith; 40 41import java.util.List; 42 43@LargeTest 44@RunWith(AndroidJUnit4.class) 45public class SimpleAppFullLifecycleTest { 46 47 @SuppressWarnings("unchecked") 48 private static final Pair[] EXPECTED_EVENTS_CONSTRUCTION = 49 new Pair[] { 50 new Pair(TestEventType.PROCESS_EVENT, Event.ON_CREATE), 51 new Pair(TestEventType.ACTIVITY_EVENT, Event.ON_CREATE), 52 new Pair(TestEventType.PROCESS_EVENT, Event.ON_START), 53 new Pair(TestEventType.ACTIVITY_EVENT, Event.ON_START), 54 new Pair(TestEventType.PROCESS_EVENT, Event.ON_RESUME), 55 new Pair(TestEventType.ACTIVITY_EVENT, Event.ON_RESUME), 56 }; 57 58 @SuppressWarnings("unchecked") 59 private static final Pair[] EXPECTED_EVENTS_DESTRUCTION = 60 new Pair[]{ 61 62 new Pair(TestEventType.ACTIVITY_EVENT, Event.ON_PAUSE), 63 new Pair(TestEventType.ACTIVITY_EVENT, Event.ON_STOP), 64 new Pair(TestEventType.ACTIVITY_EVENT, Event.ON_DESTROY), 65 66 new Pair(TestEventType.PROCESS_EVENT, Event.ON_PAUSE), 67 new Pair(TestEventType.PROCESS_EVENT, Event.ON_STOP), 68 }; 69 @Rule 70 public ActivityTestRule<SimpleAppLifecycleTestActivity> activityTestRule = 71 new ActivityTestRule<>(SimpleAppLifecycleTestActivity.class, false, false); 72 73 @Before 74 public void setup() { 75 // cool down period, so application state will become DESTROYED 76 try { 77 Thread.sleep(ProcessLifecycleOwner.TIMEOUT_MS * 2); 78 } catch (InterruptedException e) { 79 e.printStackTrace(); 80 } 81 SimpleAppLifecycleTestActivity.startProcessObserver(); 82 } 83 84 @After 85 public void tearDown() { 86 SimpleAppLifecycleTestActivity.stopProcessObserver(); 87 } 88 89 @Test 90 public void testFullLifecycle() throws InterruptedException { 91 State currentState = ProcessLifecycleOwner.get().getLifecycle().getCurrentState(); 92 assertThat(currentState, is(CREATED)); 93 activityTestRule.launchActivity(null); 94 List<Pair<TestEventType, Event>> events = SimpleAppLifecycleTestActivity.awaitForEvents(); 95 assertThat("Failed to await for events", events, notNullValue()); 96 //noinspection ConstantConditions 97 assertThat(events.subList(0, 6).toArray(), is(EXPECTED_EVENTS_CONSTRUCTION)); 98 99 // TODO: bug 35122523 100 for (Pair<TestEventType, Event> event: events.subList(6, 11)) { 101 assertThat(event, isIn(EXPECTED_EVENTS_DESTRUCTION)); 102 } 103 } 104 105} 106