Jason Monk | 8e4e4cb | 2018-12-04 11:10:24 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 com.android.systemui.util; |
| 18 | |
| 19 | import static androidx.lifecycle.Lifecycle.Event.ON_CREATE; |
| 20 | import static androidx.lifecycle.Lifecycle.Event.ON_DESTROY; |
| 21 | import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE; |
| 22 | import static androidx.lifecycle.Lifecycle.Event.ON_RESUME; |
| 23 | import static androidx.lifecycle.Lifecycle.Event.ON_START; |
| 24 | import static androidx.lifecycle.Lifecycle.Event.ON_STOP; |
| 25 | |
| 26 | import static org.mockito.ArgumentMatchers.eq; |
| 27 | import static org.mockito.Mockito.mock; |
| 28 | import static org.mockito.Mockito.verify; |
| 29 | |
Jason Monk | 8e4e4cb | 2018-12-04 11:10:24 -0500 | [diff] [blame] | 30 | import android.testing.AndroidTestingRunner; |
| 31 | import android.testing.TestableLooper; |
| 32 | import android.testing.TestableLooper.RunWithLooper; |
| 33 | |
| 34 | import androidx.lifecycle.LifecycleEventObserver; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 35 | import androidx.test.filters.SmallTest; |
Jason Monk | 8e4e4cb | 2018-12-04 11:10:24 -0500 | [diff] [blame] | 36 | |
| 37 | import com.android.systemui.SysuiBaseFragmentTest; |
| 38 | |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | |
| 42 | @RunWithLooper |
| 43 | @RunWith(AndroidTestingRunner.class) |
| 44 | @SmallTest |
| 45 | public class LifecycleFragmentTest extends SysuiBaseFragmentTest { |
| 46 | |
| 47 | public LifecycleFragmentTest() { |
| 48 | super(LifecycleFragment.class); |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void testCreateLifecycle() { |
| 53 | LifecycleFragment fragment = (LifecycleFragment) mFragment; |
| 54 | LifecycleEventObserver observer = mock(LifecycleEventObserver.class); |
| 55 | fragment.getLifecycle().addObserver(observer); |
| 56 | |
| 57 | mFragments.dispatchCreate(); |
| 58 | TestableLooper.get(this).processAllMessages(); |
| 59 | |
| 60 | verify(observer).onStateChanged(eq(fragment), eq(ON_CREATE)); |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void testResumeLifecycle() { |
| 65 | LifecycleFragment fragment = (LifecycleFragment) mFragment; |
| 66 | LifecycleEventObserver observer = mock(LifecycleEventObserver.class); |
| 67 | fragment.getLifecycle().addObserver(observer); |
| 68 | |
| 69 | mFragments.dispatchResume(); |
| 70 | TestableLooper.get(this).processAllMessages(); |
| 71 | |
| 72 | verify(observer).onStateChanged(eq(fragment), eq(ON_RESUME)); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testStartLifecycle() { |
| 77 | LifecycleFragment fragment = (LifecycleFragment) mFragment; |
| 78 | LifecycleEventObserver observer = mock(LifecycleEventObserver.class); |
| 79 | fragment.getLifecycle().addObserver(observer); |
| 80 | |
| 81 | mFragments.dispatchStart(); |
| 82 | TestableLooper.get(this).processAllMessages(); |
| 83 | |
| 84 | verify(observer).onStateChanged(eq(fragment), eq(ON_START)); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testStopLifecycle() { |
| 89 | LifecycleFragment fragment = (LifecycleFragment) mFragment; |
| 90 | LifecycleEventObserver observer = mock(LifecycleEventObserver.class); |
| 91 | fragment.getLifecycle().addObserver(observer); |
| 92 | |
| 93 | mFragments.dispatchStart(); |
| 94 | TestableLooper.get(this).processAllMessages(); |
| 95 | mFragments.dispatchStop(); |
| 96 | TestableLooper.get(this).processAllMessages(); |
| 97 | |
| 98 | verify(observer).onStateChanged(eq(fragment), eq(ON_STOP)); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testPauseLifecycle() { |
| 103 | LifecycleFragment fragment = (LifecycleFragment) mFragment; |
| 104 | LifecycleEventObserver observer = mock(LifecycleEventObserver.class); |
| 105 | fragment.getLifecycle().addObserver(observer); |
| 106 | |
| 107 | mFragments.dispatchResume(); |
| 108 | TestableLooper.get(this).processAllMessages(); |
| 109 | mFragments.dispatchPause(); |
| 110 | TestableLooper.get(this).processAllMessages(); |
| 111 | |
| 112 | verify(observer).onStateChanged(eq(fragment), eq(ON_PAUSE)); |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testDestroyLifecycle() { |
| 117 | LifecycleFragment fragment = (LifecycleFragment) mFragment; |
| 118 | LifecycleEventObserver observer = mock(LifecycleEventObserver.class); |
| 119 | fragment.getLifecycle().addObserver(observer); |
| 120 | |
| 121 | mFragments.dispatchCreate(); |
| 122 | TestableLooper.get(this).processAllMessages(); |
| 123 | mFragments.dispatchDestroy(); |
| 124 | TestableLooper.get(this).processAllMessages(); |
| 125 | mFragments = null; |
| 126 | |
| 127 | verify(observer).onStateChanged(eq(fragment), eq(ON_DESTROY)); |
| 128 | } |
| 129 | } |