blob: 2cf6ff3a58c100dc633d3bdaeff24d93b7ece034 [file] [log] [blame]
Jorim Jaggib6030952018-10-23 18:31:52 +02001/*
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 android.view;
18
Tiger Huang332793b2019-10-29 23:21:27 +080019import static android.view.InsetsState.ITYPE_STATUS_BAR;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090020
Tarandeep Singh92d2dd32019-08-07 14:45:01 -070021import static junit.framework.TestCase.assertFalse;
22import static junit.framework.TestCase.assertTrue;
23
Jorim Jaggib6030952018-10-23 18:31:52 +020024import static org.mockito.ArgumentMatchers.eq;
Jorim Jaggib6030952018-10-23 18:31:52 +020025import static org.mockito.Mockito.reset;
26import static org.mockito.Mockito.verify;
27import static org.mockito.Mockito.verifyZeroInteractions;
28
Tarandeep Singh5c071ef2019-03-18 15:57:53 -070029import android.app.Instrumentation;
30import android.content.Context;
Tarandeep Singh215929b2019-01-11 18:24:37 -080031import android.graphics.Point;
Jorim Jaggib6030952018-10-23 18:31:52 +020032import android.platform.test.annotations.Presubmit;
Jorim Jaggib6030952018-10-23 18:31:52 +020033import android.view.SurfaceControl.Transaction;
Tarandeep Singh5c071ef2019-03-18 15:57:53 -070034import android.view.WindowManager.BadTokenException;
35import android.view.WindowManager.LayoutParams;
36import android.widget.TextView;
Jorim Jaggib6030952018-10-23 18:31:52 +020037
Tarandeep Singh5c071ef2019-03-18 15:57:53 -070038import androidx.test.InstrumentationRegistry;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090039import androidx.test.runner.AndroidJUnit4;
40
Jorim Jaggib6030952018-10-23 18:31:52 +020041import org.junit.Before;
42import org.junit.Test;
43import org.junit.runner.RunWith;
44import org.mockito.Mock;
Tarandeep Singh92d2dd32019-08-07 14:45:01 -070045import org.mockito.Mockito;
Jorim Jaggib6030952018-10-23 18:31:52 +020046import org.mockito.MockitoAnnotations;
47
Tadashi G. Takaoka82eb7a52019-03-26 18:22:01 +090048/**
49 * Tests for {@link InsetsSourceConsumer}.
50 *
51 * <p>Build/Install/Run:
52 * atest FrameworksCoreTests:InsetsSourceConsumerTest
53 *
54 * <p>This test class is a part of Window Manager Service tests and specified in
55 * {@link com.android.server.wm.test.filters.FrameworksTestsFilter}.
56 */
Jorim Jaggib6030952018-10-23 18:31:52 +020057@Presubmit
Jorim Jaggib6030952018-10-23 18:31:52 +020058@RunWith(AndroidJUnit4.class)
59public class InsetsSourceConsumerTest {
60
61 private InsetsSourceConsumer mConsumer;
62
63 private SurfaceSession mSession = new SurfaceSession();
64 private SurfaceControl mLeash;
65 @Mock Transaction mMockTransaction;
Tarandeep Singh92d2dd32019-08-07 14:45:01 -070066 private InsetsSource mSpyInsetsSource;
Jorim Jaggib6030952018-10-23 18:31:52 +020067
68 @Before
69 public void setup() {
70 MockitoAnnotations.initMocks(this);
71 mLeash = new SurfaceControl.Builder(mSession)
72 .setName("testSurface")
73 .build();
Tarandeep Singh5c071ef2019-03-18 15:57:53 -070074 final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
75 instrumentation.runOnMainSync(() -> {
76 final Context context = instrumentation.getTargetContext();
77 // cannot mock ViewRootImpl since it's final.
78 final ViewRootImpl viewRootImpl = new ViewRootImpl(context, context.getDisplay());
79 try {
80 viewRootImpl.setView(new TextView(context), new LayoutParams(), null);
81 } catch (BadTokenException e) {
82 // activity isn't running, lets ignore BadTokenException.
83 }
Tarandeep Singh92d2dd32019-08-07 14:45:01 -070084 InsetsState state = new InsetsState();
Tiger Huang332793b2019-10-29 23:21:27 +080085 mSpyInsetsSource = Mockito.spy(new InsetsSource(ITYPE_STATUS_BAR));
Tarandeep Singh92d2dd32019-08-07 14:45:01 -070086 state.addSource(mSpyInsetsSource);
87
Tiger Huang332793b2019-10-29 23:21:27 +080088 mConsumer = new InsetsSourceConsumer(ITYPE_STATUS_BAR, state,
Tarandeep Singh5c071ef2019-03-18 15:57:53 -070089 () -> mMockTransaction, new InsetsController(viewRootImpl));
90 });
91 instrumentation.waitForIdleSync();
92
Tiger Huang332793b2019-10-29 23:21:27 +080093 mConsumer.setControl(new InsetsSourceControl(ITYPE_STATUS_BAR, mLeash, new Point()));
Jorim Jaggib6030952018-10-23 18:31:52 +020094 }
95
96 @Test
97 public void testHide() {
98 mConsumer.hide();
Tarandeep Singh92d2dd32019-08-07 14:45:01 -070099 assertFalse("Consumer should not be visible", mConsumer.isVisible());
100 verify(mSpyInsetsSource).setVisible(eq(false));
Jorim Jaggib6030952018-10-23 18:31:52 +0200101 }
102
103 @Test
104 public void testShow() {
Jorim Jaggib6030952018-10-23 18:31:52 +0200105 mConsumer.show();
Tarandeep Singh92d2dd32019-08-07 14:45:01 -0700106 assertTrue("Consumer should be visible", mConsumer.isVisible());
107 verify(mSpyInsetsSource).setVisible(eq(true));
Jorim Jaggib6030952018-10-23 18:31:52 +0200108 }
109
110 @Test
111 public void testRestore() {
112 mConsumer.setControl(null);
113 reset(mMockTransaction);
114 mConsumer.hide();
115 verifyZeroInteractions(mMockTransaction);
Tiger Huang332793b2019-10-29 23:21:27 +0800116 mConsumer.setControl(new InsetsSourceControl(ITYPE_STATUS_BAR, mLeash, new Point()));
Jorim Jaggib6030952018-10-23 18:31:52 +0200117 verify(mMockTransaction).hide(eq(mLeash));
118 }
119}