Jeff Chang | 2aaa91a | 2018-11-08 18:04:16 +0800 | [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.statusbar.phone; |
| 18 | |
| 19 | import static org.junit.Assert.assertNotNull; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.mockito.Mockito.mock; |
| 22 | |
| 23 | import android.graphics.PixelFormat; |
| 24 | import android.hardware.display.DisplayManager; |
| 25 | import android.hardware.display.VirtualDisplay; |
| 26 | import android.media.ImageReader; |
Jeff Chang | fa9ba88 | 2019-01-23 19:20:01 +0800 | [diff] [blame] | 27 | import android.os.SystemClock; |
Jeff Chang | 2aaa91a | 2018-11-08 18:04:16 +0800 | [diff] [blame] | 28 | import android.testing.AndroidTestingRunner; |
| 29 | import android.testing.TestableLooper.RunWithLooper; |
Jeff Chang | fa9ba88 | 2019-01-23 19:20:01 +0800 | [diff] [blame] | 30 | import android.util.Log; |
Jeff Chang | 2aaa91a | 2018-11-08 18:04:16 +0800 | [diff] [blame] | 31 | import android.view.Display; |
| 32 | import android.view.DisplayInfo; |
| 33 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 34 | import androidx.test.filters.SmallTest; |
| 35 | |
Jeff Chang | 2aaa91a | 2018-11-08 18:04:16 +0800 | [diff] [blame] | 36 | import com.android.systemui.SysuiTestCase; |
| 37 | import com.android.systemui.SysuiTestableContext; |
| 38 | import com.android.systemui.statusbar.CommandQueue; |
| 39 | |
| 40 | import org.junit.After; |
| 41 | import org.junit.Before; |
| 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | |
Jeff Chang | fa9ba88 | 2019-01-23 19:20:01 +0800 | [diff] [blame] | 45 | import java.util.function.Predicate; |
| 46 | |
Jeff Chang | 2aaa91a | 2018-11-08 18:04:16 +0800 | [diff] [blame] | 47 | /** atest NavigationBarButtonTest */ |
| 48 | @RunWith(AndroidTestingRunner.class) |
| 49 | @RunWithLooper |
| 50 | @SmallTest |
| 51 | public class NavigationBarButtonTest extends SysuiTestCase { |
| 52 | |
Jeff Chang | fa9ba88 | 2019-01-23 19:20:01 +0800 | [diff] [blame] | 53 | private static final String TAG = "NavigationBarButtonTest"; |
Jeff Chang | 2aaa91a | 2018-11-08 18:04:16 +0800 | [diff] [blame] | 54 | private ImageReader mReader; |
| 55 | private NavigationBarView mNavBar; |
| 56 | private VirtualDisplay mVirtualDisplay; |
| 57 | |
| 58 | @Before |
| 59 | public void setup() { |
Charles Chen | 3dedec3 | 2019-01-24 22:19:37 +0800 | [diff] [blame] | 60 | mContext.putComponent(CommandQueue.class, mock(CommandQueue.class)); |
Jeff Chang | 2aaa91a | 2018-11-08 18:04:16 +0800 | [diff] [blame] | 61 | 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 Chang | fa9ba88 | 2019-01-23 19:20:01 +0800 | [diff] [blame] | 87 | waitForDisplayReady(mVirtualDisplay.getDisplay().getDisplayId()); |
| 88 | |
Jeff Chang | 2aaa91a | 2018-11-08 18:04:16 +0800 | [diff] [blame] | 89 | return mVirtualDisplay.getDisplay(); |
| 90 | } |
| 91 | |
Jeff Chang | fa9ba88 | 2019-01-23 19:20:01 +0800 | [diff] [blame] | 92 | 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 Chang | 2aaa91a | 2018-11-08 18:04:16 +0800 | [diff] [blame] | 112 | @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 | |