blob: 81e8abf5a0580ef2456167f71ecf0c3d3492a364 [file] [log] [blame]
Jeff Chang2aaa91a2018-11-08 18:04:16 +08001/*
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.statusbar.phone;
18
19import static org.junit.Assert.assertNotNull;
20import static org.junit.Assert.assertTrue;
21import static org.mockito.Mockito.mock;
22
23import android.graphics.PixelFormat;
24import android.hardware.display.DisplayManager;
25import android.hardware.display.VirtualDisplay;
26import android.media.ImageReader;
Jeff Changfa9ba882019-01-23 19:20:01 +080027import android.os.SystemClock;
Jeff Chang2aaa91a2018-11-08 18:04:16 +080028import android.testing.AndroidTestingRunner;
29import android.testing.TestableLooper.RunWithLooper;
Jeff Changfa9ba882019-01-23 19:20:01 +080030import android.util.Log;
Jeff Chang2aaa91a2018-11-08 18:04:16 +080031import android.view.Display;
32import android.view.DisplayInfo;
33
Brett Chabot84151d92019-02-27 15:37:59 -080034import androidx.test.filters.SmallTest;
35
Jeff Chang2aaa91a2018-11-08 18:04:16 +080036import com.android.systemui.SysuiTestCase;
37import com.android.systemui.SysuiTestableContext;
38import com.android.systemui.statusbar.CommandQueue;
39
40import org.junit.After;
41import org.junit.Before;
42import org.junit.Test;
43import org.junit.runner.RunWith;
44
Jeff Changfa9ba882019-01-23 19:20:01 +080045import java.util.function.Predicate;
46
Jeff Chang2aaa91a2018-11-08 18:04:16 +080047/** atest NavigationBarButtonTest */
48@RunWith(AndroidTestingRunner.class)
49@RunWithLooper
50@SmallTest
51public class NavigationBarButtonTest extends SysuiTestCase {
52
Jeff Changfa9ba882019-01-23 19:20:01 +080053 private static final String TAG = "NavigationBarButtonTest";
Jeff Chang2aaa91a2018-11-08 18:04:16 +080054 private ImageReader mReader;
55 private NavigationBarView mNavBar;
56 private VirtualDisplay mVirtualDisplay;
57
58 @Before
59 public void setup() {
Charles Chen3dedec32019-01-24 22:19:37 +080060 mContext.putComponent(CommandQueue.class, mock(CommandQueue.class));
Jeff Chang2aaa91a2018-11-08 18:04:16 +080061 final Display display = createVirtualDisplay();
62 final SysuiTestableContext context =
63 (SysuiTestableContext) mContext.createDisplayContext(display);
64 context.putComponent(CommandQueue.class, mock(CommandQueue.class));
65
66 mNavBar = new NavigationBarView(context, null);
67 }
68
69 private Display createVirtualDisplay() {
70 final String displayName = "NavVirtualDisplay";
71 final DisplayInfo displayInfo = new DisplayInfo();
72 mContext.getDisplay().getDisplayInfo(displayInfo);
73
74 final DisplayManager displayManager = mContext.getSystemService(DisplayManager.class);
75
76 mReader = ImageReader.newInstance(displayInfo.logicalWidth,
77 displayInfo.logicalHeight, PixelFormat.RGBA_8888, 2);
78
79 assertNotNull("ImageReader must not be null", mReader);
80
81 mVirtualDisplay = displayManager.createVirtualDisplay(displayName, displayInfo.logicalWidth,
82 displayInfo.logicalHeight, displayInfo.logicalDensityDpi, mReader.getSurface(),
83 0 /*flags*/);
84
85 assertNotNull("virtual display must not be null", mVirtualDisplay);
86
Jeff Changfa9ba882019-01-23 19:20:01 +080087 waitForDisplayReady(mVirtualDisplay.getDisplay().getDisplayId());
88
Jeff Chang2aaa91a2018-11-08 18:04:16 +080089 return mVirtualDisplay.getDisplay();
90 }
91
Jeff Changfa9ba882019-01-23 19:20:01 +080092 private void waitForDisplayReady(int displayId) {
93 waitForDisplayCondition(displayId, state -> state);
94 }
95
96 private void waitForDisplayCondition(int displayId, Predicate<Boolean> condition) {
97 for (int retry = 1; retry <= 10; retry++) {
98 if (condition.test(isDisplayOn(displayId))) {
99 return;
100 }
101 Log.i(TAG, "Waiting for virtual display ready ... retry = " + retry);
102 SystemClock.sleep(500);
103 }
104 }
105
106 private boolean isDisplayOn(int displayId) {
107 DisplayManager displayManager = mContext.getSystemService(DisplayManager.class);
108 Display display = displayManager.getDisplay(displayId);
109 return display != null && display.getState() == Display.STATE_ON;
110 }
111
Jeff Chang2aaa91a2018-11-08 18:04:16 +0800112 @After
113 public void tearDown() {
114 releaseDisplay();
115 }
116
117 private void releaseDisplay() {
118 mVirtualDisplay.release();
119 mReader.close();
120 }
121
122 @Test
123 public void testRecentsButtonDisabledOnSecondaryDisplay() {
124 assertTrue("The recents button must be disabled",
125 mNavBar.isRecentsButtonDisabled());
126 }
127}
128
129
130