blob: 5c8071885c0de7f5d91613aaf95e380c6cc9e7be [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.support.test.filters.SmallTest;
29import android.testing.AndroidTestingRunner;
30import android.testing.TestableLooper.RunWithLooper;
Jeff Changfa9ba882019-01-23 19:20:01 +080031import android.util.Log;
Jeff Chang2aaa91a2018-11-08 18:04:16 +080032import android.view.Display;
33import android.view.DisplayInfo;
34
35import com.android.systemui.SysuiTestCase;
36import com.android.systemui.SysuiTestableContext;
37import com.android.systemui.statusbar.CommandQueue;
38
39import org.junit.After;
40import org.junit.Before;
41import org.junit.Test;
42import org.junit.runner.RunWith;
43
Jeff Changfa9ba882019-01-23 19:20:01 +080044import java.util.function.Predicate;
45
Jeff Chang2aaa91a2018-11-08 18:04:16 +080046/** atest NavigationBarButtonTest */
47@RunWith(AndroidTestingRunner.class)
48@RunWithLooper
49@SmallTest
50public class NavigationBarButtonTest extends SysuiTestCase {
51
Jeff Changfa9ba882019-01-23 19:20:01 +080052 private static final String TAG = "NavigationBarButtonTest";
Jeff Chang2aaa91a2018-11-08 18:04:16 +080053 private ImageReader mReader;
54 private NavigationBarView mNavBar;
55 private VirtualDisplay mVirtualDisplay;
56
57 @Before
58 public void setup() {
Charles Chen3dedec32019-01-24 22:19:37 +080059 mContext.putComponent(CommandQueue.class, mock(CommandQueue.class));
Jeff Chang2aaa91a2018-11-08 18:04:16 +080060 final Display display = createVirtualDisplay();
61 final SysuiTestableContext context =
62 (SysuiTestableContext) mContext.createDisplayContext(display);
63 context.putComponent(CommandQueue.class, mock(CommandQueue.class));
64
65 mNavBar = new NavigationBarView(context, null);
66 }
67
68 private Display createVirtualDisplay() {
69 final String displayName = "NavVirtualDisplay";
70 final DisplayInfo displayInfo = new DisplayInfo();
71 mContext.getDisplay().getDisplayInfo(displayInfo);
72
73 final DisplayManager displayManager = mContext.getSystemService(DisplayManager.class);
74
75 mReader = ImageReader.newInstance(displayInfo.logicalWidth,
76 displayInfo.logicalHeight, PixelFormat.RGBA_8888, 2);
77
78 assertNotNull("ImageReader must not be null", mReader);
79
80 mVirtualDisplay = displayManager.createVirtualDisplay(displayName, displayInfo.logicalWidth,
81 displayInfo.logicalHeight, displayInfo.logicalDensityDpi, mReader.getSurface(),
82 0 /*flags*/);
83
84 assertNotNull("virtual display must not be null", mVirtualDisplay);
85
Jeff Changfa9ba882019-01-23 19:20:01 +080086 waitForDisplayReady(mVirtualDisplay.getDisplay().getDisplayId());
87
Jeff Chang2aaa91a2018-11-08 18:04:16 +080088 return mVirtualDisplay.getDisplay();
89 }
90
Jeff Changfa9ba882019-01-23 19:20:01 +080091 private void waitForDisplayReady(int displayId) {
92 waitForDisplayCondition(displayId, state -> state);
93 }
94
95 private void waitForDisplayCondition(int displayId, Predicate<Boolean> condition) {
96 for (int retry = 1; retry <= 10; retry++) {
97 if (condition.test(isDisplayOn(displayId))) {
98 return;
99 }
100 Log.i(TAG, "Waiting for virtual display ready ... retry = " + retry);
101 SystemClock.sleep(500);
102 }
103 }
104
105 private boolean isDisplayOn(int displayId) {
106 DisplayManager displayManager = mContext.getSystemService(DisplayManager.class);
107 Display display = displayManager.getDisplay(displayId);
108 return display != null && display.getState() == Display.STATE_ON;
109 }
110
Jeff Chang2aaa91a2018-11-08 18:04:16 +0800111 @After
112 public void tearDown() {
113 releaseDisplay();
114 }
115
116 private void releaseDisplay() {
117 mVirtualDisplay.release();
118 mReader.close();
119 }
120
121 @Test
122 public void testRecentsButtonDisabledOnSecondaryDisplay() {
123 assertTrue("The recents button must be disabled",
124 mNavBar.isRecentsButtonDisabled());
125 }
126}
127
128
129