Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [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 |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 14 | * limitations under the License |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 15 | */ |
| 16 | |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 17 | package com.android.systemui.statusbar.notification.row; |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 18 | |
| 19 | import static android.app.AppOpsManager.OP_CAMERA; |
| 20 | import static android.app.AppOpsManager.OP_RECORD_AUDIO; |
| 21 | import static android.app.AppOpsManager.OP_SYSTEM_ALERT_WINDOW; |
| 22 | |
| 23 | import static junit.framework.Assert.assertEquals; |
| 24 | import static junit.framework.Assert.assertTrue; |
| 25 | |
| 26 | import static org.mockito.Mockito.any; |
| 27 | import static org.mockito.Mockito.anyBoolean; |
| 28 | import static org.mockito.Mockito.anyInt; |
| 29 | import static org.mockito.Mockito.anyString; |
| 30 | import static org.mockito.Mockito.eq; |
| 31 | import static org.mockito.Mockito.mock; |
| 32 | import static org.mockito.Mockito.times; |
| 33 | import static org.mockito.Mockito.verify; |
| 34 | import static org.mockito.Mockito.when; |
| 35 | |
| 36 | import android.app.Notification; |
| 37 | import android.content.pm.ApplicationInfo; |
| 38 | import android.content.pm.PackageInfo; |
| 39 | import android.content.pm.PackageManager; |
| 40 | import android.graphics.drawable.Drawable; |
| 41 | import android.os.UserHandle; |
| 42 | import android.service.notification.StatusBarNotification; |
| 43 | import android.test.suitebuilder.annotation.SmallTest; |
| 44 | import android.testing.AndroidTestingRunner; |
| 45 | import android.testing.UiThreadTest; |
| 46 | import android.util.ArraySet; |
| 47 | import android.view.LayoutInflater; |
| 48 | import android.view.View; |
| 49 | import android.widget.ImageView; |
| 50 | import android.widget.TextView; |
| 51 | |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 52 | import com.android.internal.logging.testing.UiEventLoggerFake; |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 53 | import com.android.systemui.R; |
| 54 | import com.android.systemui.SysuiTestCase; |
| 55 | |
| 56 | import org.junit.Before; |
| 57 | import org.junit.Test; |
| 58 | import org.junit.runner.RunWith; |
| 59 | |
| 60 | import java.util.concurrent.CountDownLatch; |
| 61 | |
| 62 | @SmallTest |
| 63 | @RunWith(AndroidTestingRunner.class) |
| 64 | @UiThreadTest |
| 65 | public class AppOpsInfoTest extends SysuiTestCase { |
| 66 | private static final String TEST_PACKAGE_NAME = "test_package"; |
| 67 | private static final int TEST_UID = 1; |
| 68 | |
| 69 | private AppOpsInfo mAppOpsInfo; |
| 70 | private final PackageManager mMockPackageManager = mock(PackageManager.class); |
| 71 | private final NotificationGuts mGutsParent = mock(NotificationGuts.class); |
| 72 | private StatusBarNotification mSbn; |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 73 | private UiEventLoggerFake mUiEventLogger = new UiEventLoggerFake(); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 74 | |
| 75 | @Before |
| 76 | public void setUp() throws Exception { |
| 77 | // Inflate the layout |
| 78 | final LayoutInflater layoutInflater = LayoutInflater.from(mContext); |
| 79 | mAppOpsInfo = (AppOpsInfo) layoutInflater.inflate(R.layout.app_ops_info, null); |
| 80 | mAppOpsInfo.setGutsParent(mGutsParent); |
| 81 | |
| 82 | // PackageManager must return a packageInfo and applicationInfo. |
| 83 | final PackageInfo packageInfo = new PackageInfo(); |
| 84 | packageInfo.packageName = TEST_PACKAGE_NAME; |
| 85 | when(mMockPackageManager.getPackageInfo(eq(TEST_PACKAGE_NAME), anyInt())) |
| 86 | .thenReturn(packageInfo); |
| 87 | final ApplicationInfo applicationInfo = new ApplicationInfo(); |
| 88 | applicationInfo.uid = TEST_UID; // non-zero |
| 89 | when(mMockPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn( |
| 90 | applicationInfo); |
| 91 | |
| 92 | mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, 0, |
| 93 | new Notification(), UserHandle.CURRENT, null, 0); |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testBindNotification_SetsTextApplicationName() { |
| 98 | when(mMockPackageManager.getApplicationLabel(any())).thenReturn("App Name"); |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 99 | mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, new ArraySet<>()); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 100 | final TextView textView = mAppOpsInfo.findViewById(R.id.pkgname); |
| 101 | assertTrue(textView.getText().toString().contains("App Name")); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testBindNotification_SetsPackageIcon() { |
| 106 | final Drawable iconDrawable = mock(Drawable.class); |
| 107 | when(mMockPackageManager.getApplicationIcon(any(ApplicationInfo.class))) |
| 108 | .thenReturn(iconDrawable); |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 109 | mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, new ArraySet<>()); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 110 | final ImageView iconView = mAppOpsInfo.findViewById(R.id.pkgicon); |
| 111 | assertEquals(iconDrawable, iconView.getDrawable()); |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void testBindNotification_SetsOnClickListenerForSettings() throws Exception { |
| 116 | ArraySet<Integer> expectedOps = new ArraySet<>(); |
| 117 | expectedOps.add(OP_CAMERA); |
| 118 | final CountDownLatch latch = new CountDownLatch(1); |
| 119 | mAppOpsInfo.bindGuts(mMockPackageManager, (View v, String pkg, int uid, |
| 120 | ArraySet<Integer> ops) -> { |
| 121 | assertEquals(TEST_PACKAGE_NAME, pkg); |
| 122 | assertEquals(expectedOps, ops); |
| 123 | assertEquals(TEST_UID, uid); |
| 124 | latch.countDown(); |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 125 | }, mSbn, mUiEventLogger, expectedOps); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 126 | |
| 127 | final View settingsButton = mAppOpsInfo.findViewById(R.id.settings); |
| 128 | settingsButton.performClick(); |
| 129 | // Verify that listener was triggered. |
| 130 | assertEquals(0, latch.getCount()); |
| 131 | } |
| 132 | |
| 133 | @Test |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 134 | public void testBindNotification_LogsOpen() throws Exception { |
| 135 | mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, new ArraySet<>()); |
| 136 | assertEquals(1, mUiEventLogger.numLogs()); |
| 137 | assertEquals(NotificationAppOpsEvent.NOTIFICATION_APP_OPS_OPEN.getId(), |
| 138 | mUiEventLogger.eventId(0)); |
| 139 | } |
| 140 | |
| 141 | @Test |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 142 | public void testOk() { |
| 143 | ArraySet<Integer> expectedOps = new ArraySet<>(); |
| 144 | expectedOps.add(OP_CAMERA); |
| 145 | final CountDownLatch latch = new CountDownLatch(1); |
| 146 | mAppOpsInfo.bindGuts(mMockPackageManager, (View v, String pkg, int uid, |
| 147 | ArraySet<Integer> ops) -> { |
| 148 | assertEquals(TEST_PACKAGE_NAME, pkg); |
| 149 | assertEquals(expectedOps, ops); |
| 150 | assertEquals(TEST_UID, uid); |
| 151 | latch.countDown(); |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 152 | }, mSbn, mUiEventLogger, expectedOps); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 153 | |
| 154 | final View okButton = mAppOpsInfo.findViewById(R.id.ok); |
| 155 | okButton.performClick(); |
| 156 | assertEquals(1, latch.getCount()); |
Dave Mankoff | c97a0e6 | 2020-06-01 12:13:05 -0400 | [diff] [blame] | 157 | verify(mGutsParent, times(1)).closeControls(eq(okButton), anyBoolean()); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void testPrompt_camera() { |
| 162 | ArraySet<Integer> expectedOps = new ArraySet<>(); |
| 163 | expectedOps.add(OP_CAMERA); |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 164 | mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 165 | TextView prompt = mAppOpsInfo.findViewById(R.id.prompt); |
| 166 | assertEquals("This app is using the camera.", prompt.getText()); |
| 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void testPrompt_mic() { |
| 171 | ArraySet<Integer> expectedOps = new ArraySet<>(); |
| 172 | expectedOps.add(OP_RECORD_AUDIO); |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 173 | mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 174 | TextView prompt = mAppOpsInfo.findViewById(R.id.prompt); |
| 175 | assertEquals("This app is using the microphone.", prompt.getText()); |
| 176 | } |
| 177 | |
| 178 | @Test |
| 179 | public void testPrompt_overlay() { |
| 180 | ArraySet<Integer> expectedOps = new ArraySet<>(); |
| 181 | expectedOps.add(OP_SYSTEM_ALERT_WINDOW); |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 182 | mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 183 | TextView prompt = mAppOpsInfo.findViewById(R.id.prompt); |
| 184 | assertEquals("This app is displaying over other apps on your screen.", prompt.getText()); |
| 185 | } |
| 186 | |
| 187 | @Test |
| 188 | public void testPrompt_camera_mic() { |
| 189 | ArraySet<Integer> expectedOps = new ArraySet<>(); |
| 190 | expectedOps.add(OP_CAMERA); |
| 191 | expectedOps.add(OP_RECORD_AUDIO); |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 192 | mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 193 | TextView prompt = mAppOpsInfo.findViewById(R.id.prompt); |
| 194 | assertEquals("This app is using the microphone and camera.", prompt.getText()); |
| 195 | } |
| 196 | |
| 197 | @Test |
| 198 | public void testPrompt_camera_mic_overlay() { |
| 199 | ArraySet<Integer> expectedOps = new ArraySet<>(); |
| 200 | expectedOps.add(OP_CAMERA); |
| 201 | expectedOps.add(OP_RECORD_AUDIO); |
| 202 | expectedOps.add(OP_SYSTEM_ALERT_WINDOW); |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 203 | mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 204 | TextView prompt = mAppOpsInfo.findViewById(R.id.prompt); |
| 205 | assertEquals("This app is displaying over other apps on your screen and using" |
| 206 | + " the microphone and camera.", prompt.getText()); |
| 207 | } |
| 208 | |
| 209 | @Test |
| 210 | public void testPrompt_camera_overlay() { |
| 211 | ArraySet<Integer> expectedOps = new ArraySet<>(); |
| 212 | expectedOps.add(OP_CAMERA); |
| 213 | expectedOps.add(OP_SYSTEM_ALERT_WINDOW); |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 214 | mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 215 | TextView prompt = mAppOpsInfo.findViewById(R.id.prompt); |
| 216 | assertEquals("This app is displaying over other apps on your screen and using" |
| 217 | + " the camera.", prompt.getText()); |
| 218 | } |
| 219 | |
| 220 | @Test |
| 221 | public void testPrompt_mic_overlay() { |
| 222 | ArraySet<Integer> expectedOps = new ArraySet<>(); |
| 223 | expectedOps.add(OP_RECORD_AUDIO); |
| 224 | expectedOps.add(OP_SYSTEM_ALERT_WINDOW); |
Will Brockman | 4567a4c | 2020-06-21 17:19:50 -0400 | [diff] [blame] | 225 | mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 226 | TextView prompt = mAppOpsInfo.findViewById(R.id.prompt); |
| 227 | assertEquals("This app is displaying over other apps on your screen and using" |
| 228 | + " the microphone.", prompt.getText()); |
| 229 | } |
| 230 | } |