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 com.android.systemui.util.SysuiLifecycle.viewAttachLifecycle; |
| 27 | |
| 28 | import static org.mockito.ArgumentMatchers.eq; |
| 29 | import static org.mockito.Mockito.mock; |
| 30 | import static org.mockito.Mockito.verify; |
| 31 | |
| 32 | import android.support.test.filters.SmallTest; |
| 33 | import android.testing.AndroidTestingRunner; |
| 34 | import android.testing.TestableLooper; |
| 35 | import android.testing.TestableLooper.RunWithLooper; |
| 36 | import android.testing.ViewUtils; |
| 37 | import android.view.View; |
| 38 | |
| 39 | import androidx.lifecycle.LifecycleEventObserver; |
| 40 | import androidx.lifecycle.LifecycleOwner; |
| 41 | |
| 42 | import com.android.systemui.SysuiTestCase; |
| 43 | |
| 44 | import org.junit.Test; |
| 45 | import org.junit.runner.RunWith; |
| 46 | |
| 47 | @RunWithLooper |
| 48 | @RunWith(AndroidTestingRunner.class) |
| 49 | @SmallTest |
| 50 | public class SysuiLifecycleTest extends SysuiTestCase { |
| 51 | |
| 52 | @Test |
| 53 | public void testAttach() { |
| 54 | View v = new View(mContext); |
| 55 | LifecycleEventObserver observer = mock(LifecycleEventObserver.class); |
| 56 | LifecycleOwner lifecycle = viewAttachLifecycle(v); |
| 57 | lifecycle.getLifecycle().addObserver(observer); |
| 58 | |
| 59 | ViewUtils.attachView(v); |
| 60 | TestableLooper.get(this).processAllMessages(); |
| 61 | |
| 62 | verify(observer).onStateChanged(eq(lifecycle), eq(ON_CREATE)); |
| 63 | verify(observer).onStateChanged(eq(lifecycle), eq(ON_START)); |
| 64 | verify(observer).onStateChanged(eq(lifecycle), eq(ON_RESUME)); |
| 65 | |
| 66 | ViewUtils.detachView(v); |
| 67 | TestableLooper.get(this).processAllMessages(); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void testDetach() { |
| 72 | View v = new View(mContext); |
| 73 | LifecycleEventObserver observer = mock(LifecycleEventObserver.class); |
| 74 | LifecycleOwner lifecycle = viewAttachLifecycle(v); |
| 75 | lifecycle.getLifecycle().addObserver(observer); |
| 76 | |
| 77 | ViewUtils.attachView(v); |
| 78 | TestableLooper.get(this).processAllMessages(); |
| 79 | |
| 80 | ViewUtils.detachView(v); |
| 81 | TestableLooper.get(this).processAllMessages(); |
| 82 | |
| 83 | verify(observer).onStateChanged(eq(lifecycle), eq(ON_PAUSE)); |
| 84 | verify(observer).onStateChanged(eq(lifecycle), eq(ON_STOP)); |
| 85 | verify(observer).onStateChanged(eq(lifecycle), eq(ON_DESTROY)); |
| 86 | } |
| 87 | } |