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.Event.ON_CREATE; 20import static android.arch.lifecycle.Lifecycle.Event.ON_DESTROY; 21import static android.arch.lifecycle.Lifecycle.Event.ON_PAUSE; 22import static android.arch.lifecycle.Lifecycle.Event.ON_RESUME; 23import static android.arch.lifecycle.Lifecycle.Event.ON_START; 24import static android.arch.lifecycle.Lifecycle.Event.ON_STOP; 25import static android.arch.lifecycle.Lifecycle.State.CREATED; 26import static android.arch.lifecycle.Lifecycle.State.INITIALIZED; 27import static android.arch.lifecycle.Lifecycle.State.RESUMED; 28import static android.arch.lifecycle.Lifecycle.State.STARTED; 29 30import static org.mockito.Mockito.mock; 31import static org.mockito.Mockito.reset; 32import static org.mockito.Mockito.when; 33 34import org.junit.Before; 35import org.junit.Test; 36import org.junit.runner.RunWith; 37import org.junit.runners.JUnit4; 38import org.mockito.InOrder; 39import org.mockito.Mockito; 40 41@RunWith(JUnit4.class) 42public class FullLifecycleObserverTest { 43 private LifecycleOwner mOwner; 44 private Lifecycle mLifecycle; 45 46 @Before 47 public void initMocks() { 48 mOwner = mock(LifecycleOwner.class); 49 mLifecycle = mock(Lifecycle.class); 50 when(mOwner.getLifecycle()).thenReturn(mLifecycle); 51 } 52 53 @Test 54 public void eachEvent() { 55 FullLifecycleObserver obj = mock(FullLifecycleObserver.class); 56 FullLifecycleObserverAdapter observer = new FullLifecycleObserverAdapter(obj); 57 when(mLifecycle.getCurrentState()).thenReturn(CREATED); 58 59 observer.onStateChanged(mOwner, ON_CREATE); 60 InOrder inOrder = Mockito.inOrder(obj); 61 inOrder.verify(obj).onCreate(mOwner); 62 reset(obj); 63 64 when(mLifecycle.getCurrentState()).thenReturn(STARTED); 65 observer.onStateChanged(mOwner, ON_START); 66 inOrder.verify(obj).onStart(mOwner); 67 reset(obj); 68 69 when(mLifecycle.getCurrentState()).thenReturn(RESUMED); 70 observer.onStateChanged(mOwner, ON_RESUME); 71 inOrder.verify(obj).onResume(mOwner); 72 reset(obj); 73 74 when(mLifecycle.getCurrentState()).thenReturn(STARTED); 75 observer.onStateChanged(mOwner, ON_PAUSE); 76 inOrder.verify(obj).onPause(mOwner); 77 reset(obj); 78 79 when(mLifecycle.getCurrentState()).thenReturn(CREATED); 80 observer.onStateChanged(mOwner, ON_STOP); 81 inOrder.verify(obj).onStop(mOwner); 82 reset(obj); 83 84 when(mLifecycle.getCurrentState()).thenReturn(INITIALIZED); 85 observer.onStateChanged(mOwner, ON_DESTROY); 86 inOrder.verify(obj).onDestroy(mOwner); 87 reset(obj); 88 } 89} 90