1 /* 2 * Copyright (C) 2019 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 android.app.cts; 18 19 import static androidx.test.InstrumentationRegistry.getInstrumentation; 20 21 import static org.junit.Assert.assertTrue; 22 import static org.junit.Assume.assumeFalse; 23 24 import android.app.StatusBarManager; 25 import android.app.StatusBarManager.DisableInfo; 26 import android.app.UiAutomation; 27 import android.content.Context; 28 import android.content.pm.PackageManager; 29 import android.view.KeyEvent; 30 31 import androidx.test.InstrumentationRegistry; 32 import androidx.test.filters.SmallTest; 33 import androidx.test.runner.AndroidJUnit4; 34 35 import org.junit.After; 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 @RunWith(AndroidJUnit4.class) 41 @SmallTest 42 public class StatusBarManagerTest { 43 private static final String PERMISSION_STATUS_BAR = "android.permission.STATUS_BAR"; 44 45 private StatusBarManager mStatusBarManager; 46 private Context mContext; 47 private UiAutomation mUiAutomation; 48 isWatch()49 private boolean isWatch() { 50 return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH); 51 } 52 53 /** 54 * Setup 55 * @throws Exception 56 */ 57 @Before setUp()58 public void setUp() throws Exception { 59 mContext = InstrumentationRegistry.getContext(); 60 assumeFalse("Status bar service not supported", isWatch()); 61 mStatusBarManager = (StatusBarManager) mContext.getSystemService( 62 Context.STATUS_BAR_SERVICE); 63 mUiAutomation = getInstrumentation().getUiAutomation(); 64 mUiAutomation.adoptShellPermissionIdentity(PERMISSION_STATUS_BAR); 65 } 66 67 @After tearDown()68 public void tearDown() { 69 70 if (mStatusBarManager != null) { 71 // Adopt again since tests could've dropped it 72 mUiAutomation.adoptShellPermissionIdentity(PERMISSION_STATUS_BAR); 73 mStatusBarManager.setDisabledForSetup(false); 74 } 75 mUiAutomation.dropShellPermissionIdentity(); 76 } 77 78 79 /** 80 * Test StatusBarManager.setDisabledForSetup(true) 81 * @throws Exception 82 */ 83 @Test testDisableForSetup_setDisabledTrue()84 public void testDisableForSetup_setDisabledTrue() throws Exception { 85 mStatusBarManager.setDisabledForSetup(true); 86 87 // Check for the default set of disable flags 88 assertDefaultFlagsArePresent(mStatusBarManager.getDisableInfo()); 89 } 90 assertDefaultFlagsArePresent(DisableInfo info)91 private void assertDefaultFlagsArePresent(DisableInfo info) { 92 assertTrue(info.isNotificationPeekingDisabled()); 93 assertTrue(info.isNavigateToHomeDisabled()); 94 assertTrue(info.isStatusBarExpansionDisabled()); 95 assertTrue(info.isRecentsDisabled()); 96 assertTrue(info.isSearchDisabled()); 97 } 98 99 /** 100 * Test StatusBarManager.setDisabledForSetup(false) 101 * @throws Exception 102 */ 103 @Test testDisableForSetup_setDisabledFalse()104 public void testDisableForSetup_setDisabledFalse() throws Exception { 105 // First disable, then re-enable 106 mStatusBarManager.setDisabledForSetup(true); 107 mStatusBarManager.setDisabledForSetup(false); 108 109 DisableInfo info = mStatusBarManager.getDisableInfo(); 110 111 assertTrue("Invalid disableFlags", info.areAllComponentsEnabled()); 112 } 113 114 @Test testDisableForSimLock_setDisabledTrue()115 public void testDisableForSimLock_setDisabledTrue() throws Exception { 116 mStatusBarManager.setExpansionDisabledForSimNetworkLock(true); 117 118 // Check for the default set of disable flags 119 assertTrue(mStatusBarManager.getDisableInfo().isStatusBarExpansionDisabled()); 120 } 121 122 @Test testDisableForSimLock_setDisabledFalse()123 public void testDisableForSimLock_setDisabledFalse() throws Exception { 124 // First disable, then re-enable 125 mStatusBarManager.setExpansionDisabledForSimNetworkLock(true); 126 mStatusBarManager.setExpansionDisabledForSimNetworkLock(false); 127 128 DisableInfo info = mStatusBarManager.getDisableInfo(); 129 assertTrue("Invalid disableFlags", info.areAllComponentsEnabled()); 130 } 131 132 @Test(expected = SecurityException.class) testCollapsePanels_withoutStatusBarPermission_throws()133 public void testCollapsePanels_withoutStatusBarPermission_throws() throws Exception { 134 // We've adopted shell identity for STATUS_BAR in setUp(), so drop it now 135 mUiAutomation.dropShellPermissionIdentity(); 136 137 mStatusBarManager.collapsePanels(); 138 } 139 140 @Test testCollapsePanels_withStatusBarPermission_doesNotThrow()141 public void testCollapsePanels_withStatusBarPermission_doesNotThrow() throws Exception { 142 // We've adopted shell identity for STATUS_BAR in setUp() 143 144 mStatusBarManager.collapsePanels(); 145 146 // Nothing thrown, passed 147 } 148 149 @Test(expected = SecurityException.class) testTogglePanel_withoutStatusBarPermission_throws()150 public void testTogglePanel_withoutStatusBarPermission_throws() throws Exception { 151 // We've adopted shell identity for STATUS_BAR in setUp(), so drop it now 152 mUiAutomation.dropShellPermissionIdentity(); 153 154 mStatusBarManager.togglePanel(); 155 } 156 157 @Test testTogglePanel_withStatusBarPermission_doesNotThrow()158 public void testTogglePanel_withStatusBarPermission_doesNotThrow() throws Exception { 159 // We've adopted shell identity for STATUS_BAR in setUp() 160 161 mStatusBarManager.togglePanel(); 162 163 // Nothing thrown, passed 164 } 165 166 @Test(expected = SecurityException.class) testHandleSystemKey_withoutStatusBarPermission_throws()167 public void testHandleSystemKey_withoutStatusBarPermission_throws() throws Exception { 168 // We've adopted shell identity for STATUS_BAR in setUp(), so drop it now 169 mUiAutomation.dropShellPermissionIdentity(); 170 171 mStatusBarManager.handleSystemKey(KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP); 172 } 173 174 @Test testHandleSystemKey_withStatusBarPermission_doesNotThrow()175 public void testHandleSystemKey_withStatusBarPermission_doesNotThrow() throws Exception { 176 // We've adopted shell identity for STATUS_BAR in setUp() 177 178 mStatusBarManager.handleSystemKey(KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP); 179 180 // Nothing thrown, passed 181 } 182 } 183