blob: ce8085aa4862c0aac524f2bcf25c653ff576df89 [file] [log] [blame]
Jason Monk8e4e4cb2018-12-04 11:10:24 -05001/*
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
17package com.android.systemui.util;
18
19import static androidx.lifecycle.Lifecycle.Event.ON_CREATE;
20import static androidx.lifecycle.Lifecycle.Event.ON_DESTROY;
21import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
22import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
23import static androidx.lifecycle.Lifecycle.Event.ON_START;
24import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
25
26import static com.android.systemui.util.SysuiLifecycle.viewAttachLifecycle;
27
28import static org.mockito.ArgumentMatchers.eq;
29import static org.mockito.Mockito.mock;
30import static org.mockito.Mockito.verify;
31
Jason Monk8e4e4cb2018-12-04 11:10:24 -050032import android.testing.AndroidTestingRunner;
33import android.testing.TestableLooper;
34import android.testing.TestableLooper.RunWithLooper;
35import android.testing.ViewUtils;
36import android.view.View;
37
38import androidx.lifecycle.LifecycleEventObserver;
39import androidx.lifecycle.LifecycleOwner;
Brett Chabot84151d92019-02-27 15:37:59 -080040import androidx.test.filters.SmallTest;
Jason Monk8e4e4cb2018-12-04 11:10:24 -050041
42import com.android.systemui.SysuiTestCase;
43
44import org.junit.Test;
45import org.junit.runner.RunWith;
46
47@RunWithLooper
48@RunWith(AndroidTestingRunner.class)
49@SmallTest
50public 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}