blob: 194f4ae51a62b921f37a9e16149a4143af5f6d19 [file] [log] [blame]
Benjamin Franz563707b2017-06-29 15:06:13 +01001/*
2 * Copyright 2017, 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
17package com.android.server.am;
18
19import static android.content.pm.ApplicationInfo.FLAG_SUSPENDED;
20import static org.junit.Assert.assertEquals;
21import static org.junit.Assert.assertFalse;
22import static org.junit.Assert.assertTrue;
23import static org.mockito.ArgumentMatchers.eq;
24import static org.mockito.ArgumentMatchers.nullable;
25import static org.mockito.Mockito.when;
26
27import android.app.KeyguardManager;
28import android.app.admin.DevicePolicyManagerInternal;
29import android.content.Context;
30import android.content.Intent;
31import android.content.pm.ActivityInfo;
32import android.content.pm.ApplicationInfo;
33import android.content.pm.UserInfo;
34import android.os.UserHandle;
35import android.os.UserManager;
36import android.support.test.filters.SmallTest;
37
38import com.android.internal.app.UnlaunchableAppActivity;
39import com.android.server.LocalServices;
40
41import org.junit.Before;
42import org.junit.Test;
43import org.mockito.Mock;
44import org.mockito.MockitoAnnotations;
45
46/**
47 * Unit tests for {@link ActivityStartInterceptorTest}.
48 *
49 * Build/Install/Run:
50 * bit FrameworksServicesTests:com.android.server.am.ActivityStartInterceptorTest
51 */
52@SmallTest
53public class ActivityStartInterceptorTest {
54 private static final int TEST_USER_ID = 1;
55 private static final int TEST_REAL_CALLING_UID = 2;
56 private static final int TEST_REAL_CALLING_PID = 3;
57 private static final String TEST_CALLING_PACKAGE = "com.test.caller";
58 private static final int TEST_START_FLAGS = 4;
59 private static final Intent ADMIN_SUPPORT_INTENT =
60 new Intent("com.test.ADMIN_SUPPORT");
61 private static final Intent CONFIRM_CREDENTIALS_INTENT =
62 new Intent("com.test.CONFIRM_CREDENTIALS");
63 private static final UserInfo PARENT_USER_INFO = new UserInfo(0 /* userId */, "parent",
64 0 /* flags */);
65 private static final String TEST_PACKAGE_NAME = "com.test.package";
66
67 @Mock
68 private Context mContext;
69 @Mock
70 private ActivityManagerService mService;
71 @Mock
72 private ActivityStackSupervisor mSupervisor;
73 @Mock
74 private DevicePolicyManagerInternal mDevicePolicyManager;
75 @Mock
76 private UserManager mUserManager;
77 @Mock
78 private UserController mUserController;
79 @Mock
80 private KeyguardManager mKeyguardManager;
81
82 private ActivityStartInterceptor mInterceptor;
83 private ActivityInfo mAInfo = new ActivityInfo();
84
85 @Before
86 public void setUp() {
87 // This property is used to allow mocking of package private classes with mockito
88 System.setProperty("dexmaker.share_classloader", "true");
89
90 MockitoAnnotations.initMocks(this);
91 mInterceptor = new ActivityStartInterceptor(mService, mSupervisor, mContext,
92 mUserController);
93 mInterceptor.setStates(TEST_USER_ID, TEST_REAL_CALLING_PID, TEST_REAL_CALLING_UID,
94 TEST_START_FLAGS, TEST_CALLING_PACKAGE);
95
96 // Mock DevicePolicyManagerInternal
97 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class);
98 LocalServices.addService(DevicePolicyManagerInternal.class,
99 mDevicePolicyManager);
100 when(mDevicePolicyManager
101 .createShowAdminSupportIntent(TEST_USER_ID, true))
102 .thenReturn(ADMIN_SUPPORT_INTENT);
103
104 // Mock UserManager
105 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
106 when(mUserManager.getProfileParent(TEST_USER_ID)).thenReturn(PARENT_USER_INFO);
107
108 // Mock KeyguardManager
109 when(mContext.getSystemService(Context.KEYGUARD_SERVICE)).thenReturn(mKeyguardManager);
110 when(mKeyguardManager.createConfirmDeviceCredentialIntent(
111 nullable(CharSequence.class), nullable(CharSequence.class), eq(TEST_USER_ID))).
112 thenReturn(CONFIRM_CREDENTIALS_INTENT);
113
114 // Initialise activity info
115 mAInfo.packageName = TEST_PACKAGE_NAME;
116 mAInfo.applicationInfo = new ApplicationInfo();
117 }
118
119 @Test
120 public void testSuspendedPackage() {
121 // GIVEN the package we're about to launch is currently suspended
122 mAInfo.applicationInfo.flags = FLAG_SUSPENDED;
123
124 // THEN calling intercept returns true
125 assertTrue(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null));
126
127 // THEN the returned intent is the admin support intent
128 assertEquals(ADMIN_SUPPORT_INTENT, mInterceptor.mIntent);
129 }
130
131 @Test
132 public void testInterceptQuietProfile() {
133 // GIVEN that the user the activity is starting as is currently in quiet mode
134 when(mUserManager.isQuietModeEnabled(eq(UserHandle.of(TEST_USER_ID)))).thenReturn(true);
135
136 // THEN calling intercept returns true
137 assertTrue(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null));
138
139 // THEN the returned intent is the quiet mode intent
140 assertTrue(UnlaunchableAppActivity.createInQuietModeDialogIntent(TEST_USER_ID)
141 .filterEquals(mInterceptor.mIntent));
142 }
143
144 @Test
145 public void testWorkChallenge() {
146 // GIVEN that the user the activity is starting as is currently locked
147 when(mUserController.shouldConfirmCredentials(TEST_USER_ID)).thenReturn(true);
148
149 // THEN calling intercept returns true
150 mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null);
151
152 // THEN the returned intent is the quiet mode intent
153 assertTrue(CONFIRM_CREDENTIALS_INTENT.filterEquals(mInterceptor.mIntent));
154 }
155
156 @Test
157 public void testNoInterception() {
158 // GIVEN that none of the interception conditions are met
159
160 // THEN calling intercept returns false
161 assertFalse(mInterceptor.intercept(null, null, mAInfo, null, null, 0, 0, null));
162 }
163}