blob: ebd266b87d6cc41e696d2ab464f90955f56f302c [file] [log] [blame]
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.qs;
16
Jason Monk3cfedd72016-12-09 09:31:37 -050017import static junit.framework.Assert.assertEquals;
18
19import static org.mockito.Mockito.mock;
20import static org.mockito.Mockito.when;
21
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010022import android.content.Context;
Jason Monk3cfedd72016-12-09 09:31:37 -050023import android.os.Handler;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010024import android.os.Looper;
25import android.support.test.runner.AndroidJUnit4;
26import android.test.suitebuilder.annotation.SmallTest;
27import android.text.SpannableStringBuilder;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010028import android.view.View;
29import android.view.ViewGroup;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010030import android.widget.TextView;
31
Jason Monk9c7844c2017-01-18 15:21:53 -050032import com.android.systemui.Dependency;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010033import com.android.systemui.R;
34import com.android.systemui.SysuiTestCase;
35import com.android.systemui.statusbar.policy.SecurityController;
Jason Monk340b0e52017-03-08 14:57:56 -050036import android.testing.LayoutInflaterBuilder;
37import android.testing.TestableImageView;
Jason Monk3cfedd72016-12-09 09:31:37 -050038
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010039import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42
phweiss4f70f102017-04-12 19:32:55 +020043/*
44 * Compile and run the whole SystemUI test suite:
45 runtest --path frameworks/base/packages/SystemUI/tests
46 *
47 * Compile and run just this class:
48 runtest --path \
49 frameworks/base/packages/SystemUI/tests/src/com/android/systemui/qs/QSSecurityFooterTest.java
50*/
51
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010052@SmallTest
53@RunWith(AndroidJUnit4.class)
Jason Monke5b770e2017-03-03 21:49:29 -050054public class QSSecurityFooterTest extends SysuiTestCase {
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010055
56 private final String MANAGING_ORGANIZATION = "organization";
57 private final String DEVICE_OWNER_PACKAGE = "TestDPC";
58 private final String VPN_PACKAGE = "TestVPN";
phweiss4f70f102017-04-12 19:32:55 +020059 private final String VPN_PACKAGE_2 = "TestVPN 2";
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010060
Jason Monk3cfedd72016-12-09 09:31:37 -050061 private ViewGroup mRootView;
62 private TextView mFooterText;
63 private TestableImageView mFooterIcon;
Jason Monke5b770e2017-03-03 21:49:29 -050064 private QSSecurityFooter mFooter;
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010065 private SecurityController mSecurityController = mock(SecurityController.class);
66
67 @Before
68 public void setUp() {
Jason Monk340b0e52017-03-08 14:57:56 -050069 mDependency.injectTestDependency(SecurityController.class, mSecurityController);
70 mDependency.injectTestDependency(Dependency.BG_LOOPER, Looper.getMainLooper());
Jason Monk3cfedd72016-12-09 09:31:37 -050071 mContext.addMockSystemService(Context.LAYOUT_INFLATER_SERVICE,
72 new LayoutInflaterBuilder(mContext)
73 .replace("ImageView", TestableImageView.class)
74 .build());
75 Handler h = new Handler(Looper.getMainLooper());
Jason Monke5b770e2017-03-03 21:49:29 -050076 h.post(() -> mFooter = new QSSecurityFooter(null, mContext));
Jason Monk3cfedd72016-12-09 09:31:37 -050077 waitForIdleSync(h);
78 mRootView = (ViewGroup) mFooter.getView();
79 mFooterText = (TextView) mRootView.findViewById(R.id.footer_text);
80 mFooterIcon = (TestableImageView) mRootView.findViewById(R.id.footer_icon);
Jason Monk9c7844c2017-01-18 15:21:53 -050081 mFooter.setHostEnvironment(null);
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010082 }
83
84 @Test
85 public void testUnmanaged() {
86 when(mSecurityController.isDeviceManaged()).thenReturn(false);
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010087 mFooter.refreshState();
88
89 waitForIdleSync(mFooter.mHandler);
Jason Monk3cfedd72016-12-09 09:31:37 -050090 assertEquals(View.GONE, mRootView.getVisibility());
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +010091 }
92
93 @Test
94 public void testManagedNoOwnerName() {
95 when(mSecurityController.isDeviceManaged()).thenReturn(true);
96 when(mSecurityController.getDeviceOwnerOrganizationName()).thenReturn(null);
97 mFooter.refreshState();
98
99 waitForIdleSync(mFooter.mHandler);
phweiss4f70f102017-04-12 19:32:55 +0200100 assertEquals(mContext.getString(R.string.quick_settings_disclosure_management),
101 mFooterText.getText());
Jason Monk3cfedd72016-12-09 09:31:37 -0500102 assertEquals(View.VISIBLE, mRootView.getVisibility());
phweiss4f70f102017-04-12 19:32:55 +0200103 assertEquals(View.VISIBLE, mFooterIcon.getVisibility());
104 // -1 == never set.
105 assertEquals(-1, mFooterIcon.getLastImageResource());
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100106 }
107
108 @Test
109 public void testManagedOwnerName() {
110 when(mSecurityController.isDeviceManaged()).thenReturn(true);
111 when(mSecurityController.getDeviceOwnerOrganizationName())
112 .thenReturn(MANAGING_ORGANIZATION);
113 mFooter.refreshState();
114
115 waitForIdleSync(mFooter.mHandler);
phweiss4f70f102017-04-12 19:32:55 +0200116 assertEquals(mContext.getString(R.string.quick_settings_disclosure_named_management,
117 MANAGING_ORGANIZATION),
Jason Monk3cfedd72016-12-09 09:31:37 -0500118 mFooterText.getText());
119 assertEquals(View.VISIBLE, mRootView.getVisibility());
phweiss4f70f102017-04-12 19:32:55 +0200120 assertEquals(View.VISIBLE, mFooterIcon.getVisibility());
121 // -1 == never set.
122 assertEquals(-1, mFooterIcon.getLastImageResource());
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100123 }
124
125 @Test
phweissa4e169e2016-11-24 16:20:57 +0100126 public void testNetworkLoggingEnabled() {
127 when(mSecurityController.isDeviceManaged()).thenReturn(true);
128 when(mSecurityController.isNetworkLoggingEnabled()).thenReturn(true);
phweissa4e169e2016-11-24 16:20:57 +0100129 mFooter.refreshState();
130
131 waitForIdleSync(mFooter.mHandler);
phweiss4f70f102017-04-12 19:32:55 +0200132 assertEquals(mContext.getString(R.string.quick_settings_disclosure_management_monitoring),
133 mFooterText.getText());
Jason Monk3cfedd72016-12-09 09:31:37 -0500134 assertEquals(View.VISIBLE, mFooterIcon.getVisibility());
135 // -1 == never set.
136 assertEquals(-1, mFooterIcon.getLastImageResource());
phweiss4f70f102017-04-12 19:32:55 +0200137
138 // Same situation, but with organization name set
139 when(mSecurityController.getDeviceOwnerOrganizationName())
140 .thenReturn(MANAGING_ORGANIZATION);
141 mFooter.refreshState();
142
143 waitForIdleSync(mFooter.mHandler);
144 assertEquals(mContext.getString(
145 R.string.quick_settings_disclosure_named_management_monitoring,
146 MANAGING_ORGANIZATION),
147 mFooterText.getText());
148 }
149
150 @Test
151 public void testManagedCACertsInstalled() {
152 when(mSecurityController.isDeviceManaged()).thenReturn(true);
153 when(mSecurityController.hasCACertInCurrentUser()).thenReturn(true);
154 mFooter.refreshState();
155
156 waitForIdleSync(mFooter.mHandler);
157 assertEquals(mContext.getString(R.string.quick_settings_disclosure_management_monitoring),
158 mFooterText.getText());
159 }
160
161 @Test
162 public void testManagedOneVpnEnabled() {
163 when(mSecurityController.isDeviceManaged()).thenReturn(true);
164 when(mSecurityController.isVpnEnabled()).thenReturn(true);
165 when(mSecurityController.getPrimaryVpnName()).thenReturn(VPN_PACKAGE);
166 mFooter.refreshState();
167
168 waitForIdleSync(mFooter.mHandler);
169 assertEquals(mContext.getString(R.string.quick_settings_disclosure_management_named_vpn,
170 VPN_PACKAGE),
171 mFooterText.getText());
172 assertEquals(View.VISIBLE, mFooterIcon.getVisibility());
173 assertEquals(R.drawable.ic_qs_vpn, mFooterIcon.getLastImageResource());
174
175 // Same situation, but with organization name set
176 when(mSecurityController.getDeviceOwnerOrganizationName())
177 .thenReturn(MANAGING_ORGANIZATION);
178 mFooter.refreshState();
179
180 waitForIdleSync(mFooter.mHandler);
181 assertEquals(mContext.getString(
182 R.string.quick_settings_disclosure_named_management_named_vpn,
183 MANAGING_ORGANIZATION, VPN_PACKAGE),
184 mFooterText.getText());
185 }
186
187 @Test
188 public void testManagedTwoVpnsEnabled() {
189 when(mSecurityController.isDeviceManaged()).thenReturn(true);
190 when(mSecurityController.isVpnEnabled()).thenReturn(true);
191 when(mSecurityController.getPrimaryVpnName()).thenReturn(VPN_PACKAGE);
192 when(mSecurityController.getWorkProfileVpnName()).thenReturn(VPN_PACKAGE_2);
193 mFooter.refreshState();
194
195 waitForIdleSync(mFooter.mHandler);
196 assertEquals(mContext.getString(R.string.quick_settings_disclosure_management_vpns),
197 mFooterText.getText());
198 assertEquals(View.VISIBLE, mFooterIcon.getVisibility());
199 assertEquals(R.drawable.ic_qs_vpn, mFooterIcon.getLastImageResource());
200
201 // Same situation, but with organization name set
202 when(mSecurityController.getDeviceOwnerOrganizationName())
203 .thenReturn(MANAGING_ORGANIZATION);
204 mFooter.refreshState();
205
206 waitForIdleSync(mFooter.mHandler);
207 assertEquals(mContext.getString(R.string.quick_settings_disclosure_named_management_vpns,
208 MANAGING_ORGANIZATION),
209 mFooterText.getText());
phweissa4e169e2016-11-24 16:20:57 +0100210 }
211
212 @Test
213 public void testNetworkLoggingAndVpnEnabled() {
214 when(mSecurityController.isDeviceManaged()).thenReturn(true);
215 when(mSecurityController.isNetworkLoggingEnabled()).thenReturn(true);
216 when(mSecurityController.isVpnEnabled()).thenReturn(true);
phweiss4f70f102017-04-12 19:32:55 +0200217 when(mSecurityController.getPrimaryVpnName()).thenReturn("VPN Test App");
phweissa4e169e2016-11-24 16:20:57 +0100218 mFooter.refreshState();
219
220 waitForIdleSync(mFooter.mHandler);
Jason Monk3cfedd72016-12-09 09:31:37 -0500221 assertEquals(View.VISIBLE, mFooterIcon.getVisibility());
phweiss4f70f102017-04-12 19:32:55 +0200222 assertEquals(R.drawable.ic_qs_vpn, mFooterIcon.getLastImageResource());
223 assertEquals(mContext.getString(R.string.quick_settings_disclosure_management_monitoring),
224 mFooterText.getText());
225 }
226
227 @Test
228 public void testWorkProfileCACertsInstalled() {
229 when(mSecurityController.isDeviceManaged()).thenReturn(false);
230 when(mSecurityController.hasCACertInWorkProfile()).thenReturn(true);
231 mFooter.refreshState();
232
233 waitForIdleSync(mFooter.mHandler);
Jason Monk3cfedd72016-12-09 09:31:37 -0500234 // -1 == never set.
235 assertEquals(-1, mFooterIcon.getLastImageResource());
phweiss4f70f102017-04-12 19:32:55 +0200236 assertEquals(mContext.getString(
237 R.string.quick_settings_disclosure_managed_profile_monitoring),
238 mFooterText.getText());
239
240 // Same situation, but with organization name set
241 when(mSecurityController.getWorkProfileOrganizationName())
242 .thenReturn(MANAGING_ORGANIZATION);
243 mFooter.refreshState();
244
245 waitForIdleSync(mFooter.mHandler);
246 assertEquals(mContext.getString(
247 R.string.quick_settings_disclosure_named_managed_profile_monitoring,
248 MANAGING_ORGANIZATION),
249 mFooterText.getText());
250 }
251
252 @Test
253 public void testCACertsInstalled() {
254 when(mSecurityController.isDeviceManaged()).thenReturn(false);
255 when(mSecurityController.hasCACertInCurrentUser()).thenReturn(true);
256 mFooter.refreshState();
257
258 waitForIdleSync(mFooter.mHandler);
259 // -1 == never set.
260 assertEquals(-1, mFooterIcon.getLastImageResource());
261 assertEquals(mContext.getString(R.string.quick_settings_disclosure_monitoring),
262 mFooterText.getText());
263 }
264
265 @Test
266 public void testTwoVpnsEnabled() {
267 when(mSecurityController.isVpnEnabled()).thenReturn(true);
268 when(mSecurityController.getPrimaryVpnName()).thenReturn(VPN_PACKAGE);
269 when(mSecurityController.getWorkProfileVpnName()).thenReturn(VPN_PACKAGE_2);
270 mFooter.refreshState();
271
272 waitForIdleSync(mFooter.mHandler);
273 assertEquals(R.drawable.ic_qs_vpn, mFooterIcon.getLastImageResource());
274 assertEquals(mContext.getString(R.string.quick_settings_disclosure_vpns),
275 mFooterText.getText());
276 }
277
278 @Test
279 public void testWorkProfileVpnEnabled() {
280 when(mSecurityController.isVpnEnabled()).thenReturn(true);
281 when(mSecurityController.getWorkProfileVpnName()).thenReturn(VPN_PACKAGE_2);
282 mFooter.refreshState();
283
284 waitForIdleSync(mFooter.mHandler);
285 assertEquals(R.drawable.ic_qs_vpn, mFooterIcon.getLastImageResource());
286 assertEquals(mContext.getString(
287 R.string.quick_settings_disclosure_managed_profile_named_vpn,
288 VPN_PACKAGE_2),
289 mFooterText.getText());
290 }
291
292 @Test
293 public void testVpnEnabled() {
294 when(mSecurityController.isVpnEnabled()).thenReturn(true);
295 when(mSecurityController.getPrimaryVpnName()).thenReturn(VPN_PACKAGE);
296 mFooter.refreshState();
297
298 waitForIdleSync(mFooter.mHandler);
299 assertEquals(R.drawable.ic_qs_vpn, mFooterIcon.getLastImageResource());
300 assertEquals(mContext.getString(R.string.quick_settings_disclosure_named_vpn,
301 VPN_PACKAGE),
302 mFooterText.getText());
303
304 when(mSecurityController.hasWorkProfile()).thenReturn(true);
305 mFooter.refreshState();
306
307 waitForIdleSync(mFooter.mHandler);
308 assertEquals(mContext.getString(
309 R.string.quick_settings_disclosure_personal_profile_named_vpn,
310 VPN_PACKAGE),
311 mFooterText.getText());
phweissa4e169e2016-11-24 16:20:57 +0100312 }
313
314 @Test
phweiss48e353c2017-04-19 20:14:51 +0200315 public void testGetManagementMessage() {
316 assertEquals(null, mFooter.getManagementMessage(false, MANAGING_ORGANIZATION));
317 assertEquals(mContext.getString(R.string.monitoring_description_named_management,
318 MANAGING_ORGANIZATION),
319 mFooter.getManagementMessage(true, MANAGING_ORGANIZATION));
320 assertEquals(mContext.getString(R.string.monitoring_description_management),
321 mFooter.getManagementMessage(true, null));
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100322 }
323
324 @Test
phweiss48e353c2017-04-19 20:14:51 +0200325 public void testGetCaCertsMessage() {
326 assertEquals(null, mFooter.getCaCertsMessage(true, false, false));
327 assertEquals(null, mFooter.getCaCertsMessage(false, false, false));
328 assertEquals(mContext.getString(R.string.monitoring_description_management_ca_certificate),
329 mFooter.getCaCertsMessage(true, true, true));
330 assertEquals(mContext.getString(R.string.monitoring_description_management_ca_certificate),
331 mFooter.getCaCertsMessage(true, false, true));
332 assertEquals(mContext.getString(
333 R.string.monitoring_description_managed_profile_ca_certificate),
334 mFooter.getCaCertsMessage(false, false, true));
335 assertEquals(mContext.getString(
336 R.string.monitoring_description_ca_certificate),
337 mFooter.getCaCertsMessage(false, true, false));
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100338 }
339
340 @Test
phweiss48e353c2017-04-19 20:14:51 +0200341 public void testGetNetworkLoggingMessage() {
342 assertEquals(null, mFooter.getNetworkLoggingMessage(false));
343 assertEquals(mContext.getString(R.string.monitoring_description_management_network_logging),
344 mFooter.getNetworkLoggingMessage(true));
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100345 }
346
347 @Test
phweiss48e353c2017-04-19 20:14:51 +0200348 public void testGetVpnMessage() {
349 assertEquals(null, mFooter.getVpnMessage(true, true, null, null));
350 assertEquals(addLink(mContext.getString(R.string.monitoring_description_two_named_vpns,
351 VPN_PACKAGE, VPN_PACKAGE_2)),
352 mFooter.getVpnMessage(true, true, VPN_PACKAGE, VPN_PACKAGE_2));
353 assertEquals(addLink(mContext.getString(R.string.monitoring_description_two_named_vpns,
354 VPN_PACKAGE, VPN_PACKAGE_2)),
355 mFooter.getVpnMessage(false, true, VPN_PACKAGE, VPN_PACKAGE_2));
356 assertEquals(addLink(mContext.getString(R.string.monitoring_description_named_vpn,
357 VPN_PACKAGE)),
358 mFooter.getVpnMessage(true, false, VPN_PACKAGE, null));
359 assertEquals(addLink(mContext.getString(R.string.monitoring_description_named_vpn,
360 VPN_PACKAGE)),
361 mFooter.getVpnMessage(false, false, VPN_PACKAGE, null));
362 assertEquals(addLink(mContext.getString(R.string.monitoring_description_named_vpn,
363 VPN_PACKAGE_2)),
364 mFooter.getVpnMessage(true, true, null, VPN_PACKAGE_2));
365 assertEquals(addLink(mContext.getString(
366 R.string.monitoring_description_managed_profile_named_vpn,
367 VPN_PACKAGE_2)),
368 mFooter.getVpnMessage(false, true, null, VPN_PACKAGE_2));
369 assertEquals(addLink(mContext.getString(
370 R.string.monitoring_description_personal_profile_named_vpn,
371 VPN_PACKAGE)),
372 mFooter.getVpnMessage(false, true, VPN_PACKAGE, null));
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100373 }
374
phweiss48e353c2017-04-19 20:14:51 +0200375 private CharSequence addLink(CharSequence description) {
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100376 final SpannableStringBuilder message = new SpannableStringBuilder();
phweiss48e353c2017-04-19 20:14:51 +0200377 message.append(description);
378 message.append(mContext.getString(R.string.monitoring_description_vpn_settings_separator));
379 message.append(mContext.getString(R.string.monitoring_description_vpn_settings),
380 mFooter.new VpnSpan(), 0);
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100381 return message;
382 }
383}