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