blob: 43d8b50bcf72ebc0a6a1201b03da9eee9ba2cb7a [file] [log] [blame]
Julia Reynoldsb5867452018-02-28 16:31:35 -05001/*
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 Shah20790b82018-07-02 17:21:04 -070014 * limitations under the License
Julia Reynoldsb5867452018-02-28 16:31:35 -050015 */
16
Rohan Shah20790b82018-07-02 17:21:04 -070017package com.android.systemui.statusbar.notification.row;
Julia Reynoldsb5867452018-02-28 16:31:35 -050018
19import static android.app.AppOpsManager.OP_CAMERA;
20import static android.app.AppOpsManager.OP_RECORD_AUDIO;
21import static android.app.AppOpsManager.OP_SYSTEM_ALERT_WINDOW;
22
23import static junit.framework.Assert.assertEquals;
24import static junit.framework.Assert.assertTrue;
25
26import static org.mockito.Mockito.any;
27import static org.mockito.Mockito.anyBoolean;
28import static org.mockito.Mockito.anyInt;
29import static org.mockito.Mockito.anyString;
30import static org.mockito.Mockito.eq;
31import static org.mockito.Mockito.mock;
32import static org.mockito.Mockito.times;
33import static org.mockito.Mockito.verify;
34import static org.mockito.Mockito.when;
35
36import android.app.Notification;
37import android.content.pm.ApplicationInfo;
38import android.content.pm.PackageInfo;
39import android.content.pm.PackageManager;
40import android.graphics.drawable.Drawable;
41import android.os.UserHandle;
42import android.service.notification.StatusBarNotification;
43import android.test.suitebuilder.annotation.SmallTest;
44import android.testing.AndroidTestingRunner;
45import android.testing.UiThreadTest;
46import android.util.ArraySet;
47import android.view.LayoutInflater;
48import android.view.View;
49import android.widget.ImageView;
50import android.widget.TextView;
51
Will Brockman4567a4c2020-06-21 17:19:50 -040052import com.android.internal.logging.testing.UiEventLoggerFake;
Julia Reynoldsb5867452018-02-28 16:31:35 -050053import com.android.systemui.R;
54import com.android.systemui.SysuiTestCase;
55
56import org.junit.Before;
57import org.junit.Test;
58import org.junit.runner.RunWith;
59
60import java.util.concurrent.CountDownLatch;
61
62@SmallTest
63@RunWith(AndroidTestingRunner.class)
64@UiThreadTest
65public 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 Brockman4567a4c2020-06-21 17:19:50 -040073 private UiEventLoggerFake mUiEventLogger = new UiEventLoggerFake();
Julia Reynoldsb5867452018-02-28 16:31:35 -050074
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 Brockman4567a4c2020-06-21 17:19:50 -040099 mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, new ArraySet<>());
Julia Reynoldsb5867452018-02-28 16:31:35 -0500100 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 Brockman4567a4c2020-06-21 17:19:50 -0400109 mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, new ArraySet<>());
Julia Reynoldsb5867452018-02-28 16:31:35 -0500110 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 Brockman4567a4c2020-06-21 17:19:50 -0400125 }, mSbn, mUiEventLogger, expectedOps);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500126
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 Brockman4567a4c2020-06-21 17:19:50 -0400134 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 Reynoldsb5867452018-02-28 16:31:35 -0500142 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 Brockman4567a4c2020-06-21 17:19:50 -0400152 }, mSbn, mUiEventLogger, expectedOps);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500153
154 final View okButton = mAppOpsInfo.findViewById(R.id.ok);
155 okButton.performClick();
156 assertEquals(1, latch.getCount());
Dave Mankoffc97a0e62020-06-01 12:13:05 -0400157 verify(mGutsParent, times(1)).closeControls(eq(okButton), anyBoolean());
Julia Reynoldsb5867452018-02-28 16:31:35 -0500158 }
159
160 @Test
161 public void testPrompt_camera() {
162 ArraySet<Integer> expectedOps = new ArraySet<>();
163 expectedOps.add(OP_CAMERA);
Will Brockman4567a4c2020-06-21 17:19:50 -0400164 mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500165 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 Brockman4567a4c2020-06-21 17:19:50 -0400173 mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500174 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 Brockman4567a4c2020-06-21 17:19:50 -0400182 mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500183 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 Brockman4567a4c2020-06-21 17:19:50 -0400192 mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500193 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 Brockman4567a4c2020-06-21 17:19:50 -0400203 mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500204 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 Brockman4567a4c2020-06-21 17:19:50 -0400214 mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500215 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 Brockman4567a4c2020-06-21 17:19:50 -0400225 mAppOpsInfo.bindGuts(mMockPackageManager, null, mSbn, mUiEventLogger, expectedOps);
Julia Reynoldsb5867452018-02-28 16:31:35 -0500226 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}