1e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot/*
2e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot * Copyright (C) 2015 The Android Open Source Project
3e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot *
4e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot * Licensed under the Apache License, Version 2.0 (the "License");
5e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot * you may not use this file except in compliance with the License.
6e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot * You may obtain a copy of the License at
7e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot *
8e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot *      http://www.apache.org/licenses/LICENSE-2.0
9e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot *
10e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot * Unless required by applicable law or agreed to in writing, software
11e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot * distributed under the License is distributed on an "AS IS" BASIS,
12e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot * See the License for the specific language governing permissions and
14e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot * limitations under the License.
15e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot */
16e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
17e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotpackage com.android.managedprovisioning.task;
18e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
19e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.content.pm.ActivityInfo;
20e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.content.pm.ApplicationInfo;
21e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.content.pm.IPackageDeleteObserver;
22e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.content.pm.IPackageManager;
23e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.content.pm.PackageInfo;
24e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.content.pm.PackageManager;
25e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.content.pm.PackageManager.NameNotFoundException;
26e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.content.pm.ParceledListSlice;
27e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.content.pm.ResolveInfo;
28e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.content.pm.ServiceInfo;
29e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.content.res.Resources;
30e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.content.Context;
31e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.content.Intent;
32e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.os.RemoteException;
33e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.test.AndroidTestCase;
34e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.test.mock.MockPackageManager;
35e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.test.suitebuilder.annotation.SmallTest;
36e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport android.view.inputmethod.InputMethodInfo;
37e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
38e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport com.android.internal.view.IInputMethodManager;
39e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
40e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport java.io.File;
41e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport java.util.Arrays;
42e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport java.util.ArrayList;
43e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport java.util.Collections;
44e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport java.util.HashSet;
45e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport java.util.List;
46e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport java.util.Set;
47e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
48e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport static org.mockito.Mockito.any;
49e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport static org.mockito.Mockito.anyInt;
50e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport static org.mockito.Mockito.anyString;
51e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport static org.mockito.Mockito.doAnswer;
52e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport static org.mockito.Mockito.eq;
53e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport static org.mockito.Mockito.mock;
54e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport static org.mockito.Mockito.times;
55e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport static org.mockito.Mockito.verify;
56e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport static org.mockito.Mockito.when;
57e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
58e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport org.mockito.invocation.InvocationOnMock;
59e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport org.mockito.stubbing.Answer;
60e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport org.mockito.Mock;
61e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotimport org.mockito.MockitoAnnotations;
62e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
63e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevotpublic class DeleteNonRequiredAppsTaskTest extends AndroidTestCase {
64e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
65e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private @Mock Resources mResources;
66e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private @Mock IPackageManager mIPackageManager;
67e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private @Mock IInputMethodManager mIInputMethodManager;
68e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private @Mock DeleteNonRequiredAppsTask.Callback mCallback;
69e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private @Mock Context mTestContext;
70e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
71e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private FakePackageManager mPackageManager;
72e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
73e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private static final String TEST_MDM_PACKAGE_NAME = "mdm.package.name";
74e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private static final int TEST_USER_ID = 0;
75e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private Set<String> mDeletedApps;
76e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private String[] mSystemAppsWithLauncher;
77e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private Set<String> mInstalledApplications;
78e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
79e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    @Override
80e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    protected void setUp() throws Exception {
81e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        // this is necessary for mockito to work
82e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
83e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
84e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        MockitoAnnotations.initMocks(this);
85e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
86e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        mPackageManager = new FakePackageManager();
87e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
88e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        when(mTestContext.getResources()).thenReturn(mResources);
89e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        when(mTestContext.getPackageManager()).thenReturn(mPackageManager);
90e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        when(mTestContext.getFilesDir()).thenReturn(getContext().getFilesDir());
91e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
92e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        mDeletedApps = new HashSet<String>();
93e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
94e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemInputMethods();
95e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setRequiredAppsManagedDevice();
96e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setVendorRequiredAppsManagedDevice();
97e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setDisallowedAppsManagedDevice();
98e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setVendorDisallowedAppsManagedDevice();
99e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setRequiredAppsManagedProfile();
100e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setVendorRequiredAppsManagedProfile();
101e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setDisallowedAppsManagedProfile();
102e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setVendorDisallowedAppsManagedProfile();
103a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setRequiredAppsManagedUser();
104a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setVendorRequiredAppsManagedUser();
105a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setDisallowedAppsManagedUser();
106a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setVendorDisallowedAppsManagedUser();
107e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
108e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
109e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    // We run most methods for device owner only, and we'll assume they also work for profile owner.
110e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    @SmallTest
111e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    public void testOnlyAppsWithLauncherDeletedByDefault() {
112e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher("app.a");
113e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps( "app.a", "app.b");
114e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
115e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.DEVICE_OWNER, true, false);
116e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
117e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps("app.a");
118e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(1)).onSuccess();
119e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
120e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
121e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    @SmallTest
122e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    public void testDeviceOwnerRequiredAppsNotDeleted() {
123e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher("app.a", "app.b", "app.c");
124e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps("app.a", "app.b", "app.c");
125e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setRequiredAppsManagedDevice("app.a");
126e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setVendorRequiredAppsManagedDevice("app.b");
127e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
128e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.DEVICE_OWNER, true, false);
129e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
130e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps("app.c");
131e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(1)).onSuccess();
132e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
133e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
134e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    @SmallTest
135e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    public void testProfileOwnerRequiredAppsNotDeleted() {
136e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher("app.a", "app.b", "app.c");
137e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps("app.a", "app.b", "app.c");
138e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setRequiredAppsManagedProfile("app.a");
139e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setVendorRequiredAppsManagedProfile("app.b");
140e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
141e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.PROFILE_OWNER, true, false);
142e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
143e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps("app.c");
144e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(1)).onSuccess();
145e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
146e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
147e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    @SmallTest
148a87044107965ce669350ff528687bf94d73ae248Alan Treadway    public void testManagedUserRequiredAppsNotDeleted() {
149a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setSystemAppsWithLauncher("app.a", "app.b", "app.c");
150a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setInstalledSystemApps("app.a", "app.b", "app.c");
151a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setRequiredAppsManagedUser("app.a");
152a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setVendorRequiredAppsManagedUser("app.b");
153a87044107965ce669350ff528687bf94d73ae248Alan Treadway
154a87044107965ce669350ff528687bf94d73ae248Alan Treadway        runTask(DeleteNonRequiredAppsTask.MANAGED_USER, true, false);
155a87044107965ce669350ff528687bf94d73ae248Alan Treadway
156a87044107965ce669350ff528687bf94d73ae248Alan Treadway        assertDeletedApps("app.c");
157a87044107965ce669350ff528687bf94d73ae248Alan Treadway        verify(mCallback, times(1)).onSuccess();
158a87044107965ce669350ff528687bf94d73ae248Alan Treadway    }
159a87044107965ce669350ff528687bf94d73ae248Alan Treadway
160a87044107965ce669350ff528687bf94d73ae248Alan Treadway    @SmallTest
161e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    public void testMdmNotDeleted() {
162e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher(TEST_MDM_PACKAGE_NAME);
163e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps(TEST_MDM_PACKAGE_NAME);
164e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
165e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.DEVICE_OWNER, true, false);
166e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
167e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps();
168e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(1)).onSuccess();
169e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
170e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
171e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    @SmallTest
172e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    public void testDisallowedAppsDeletedEvenIfNoLauncher() {
173e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher();
174e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps("app.a", "app.b", "app.c");
175e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setDisallowedAppsManagedDevice("app.a");
176e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setVendorDisallowedAppsManagedDevice("app.b");
177e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
178e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.DEVICE_OWNER, true, false);
179e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
180e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps("app.a", "app.b");
181e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(1)).onSuccess();
182e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
183e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
184e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    @SmallTest
185e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    public void testDeviceOwnerImesNotDeleted() {
186e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher("app.a", "app.b");
187e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps("app.a", "app.b");
188e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemInputMethods("app.a");
189e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
190e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.DEVICE_OWNER, true, false);
191e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
192e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps("app.b");
193e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(1)).onSuccess();
194e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
195e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
196e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    @SmallTest
197e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    public void testProfileOwnerImesStillDeleted() {
198e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher("app.a", "app.b");
199e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps("app.a", "app.b");
200e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemInputMethods("app.a");
201e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
202e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.PROFILE_OWNER, true, false);
203e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
204e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps("app.a", "app.b");
205e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(1)).onSuccess();
206e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
207e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
208e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    @SmallTest
209a87044107965ce669350ff528687bf94d73ae248Alan Treadway    public void testManagedUserImesNotDeleted() {
210a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setSystemAppsWithLauncher("app.a", "app.b");
211a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setInstalledSystemApps("app.a", "app.b");
212a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setSystemInputMethods("app.a");
213a87044107965ce669350ff528687bf94d73ae248Alan Treadway
214a87044107965ce669350ff528687bf94d73ae248Alan Treadway        runTask(DeleteNonRequiredAppsTask.MANAGED_USER, true, false);
215a87044107965ce669350ff528687bf94d73ae248Alan Treadway
216a87044107965ce669350ff528687bf94d73ae248Alan Treadway        assertDeletedApps("app.b");
217a87044107965ce669350ff528687bf94d73ae248Alan Treadway        verify(mCallback, times(1)).onSuccess();
218a87044107965ce669350ff528687bf94d73ae248Alan Treadway    }
219a87044107965ce669350ff528687bf94d73ae248Alan Treadway
220a87044107965ce669350ff528687bf94d73ae248Alan Treadway    @SmallTest
221e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    public void testLeaveAllAppsEnabled() {
222e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher("app.a");
223e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps("app.a");
224e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
225e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.DEVICE_OWNER, true, true);
226e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
227e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps(); //assert that no app has been deleted.
228e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(1)).onSuccess();
229e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
230e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
231e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    @SmallTest
232e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    public void testNewAppsDeletedAfterOta() {
233e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher();
234e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps("app.a");
235e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
236e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.DEVICE_OWNER, true, false);
237e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
238e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(1)).onSuccess();
239e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps(); //assert that no app has been deleted.
240e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
241e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        // Now, an OTA happens and installs app.b with a launcher
242e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher("app.b");
243e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps("app.a", "app.b");
244e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
245e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.DEVICE_OWNER, false, false);
246e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
247e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps("app.b");
248e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(2)).onSuccess();
249e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
250e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
251e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    @SmallTest
252e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    public void testExistingAppsNotDeletedAgainAfterOta() {
253e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher("app.a");
254e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps("app.a");
255e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
256e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.DEVICE_OWNER, true, false);
257e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
258e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(1)).onSuccess();
259e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps("app.a");
260e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        mDeletedApps.clear();
261e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
262e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.DEVICE_OWNER, false, false);
263e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
264e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps();
265e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(2)).onSuccess();
266e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
267e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
268e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    @SmallTest
269e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    public void testWhenNoSystemAppsFileFound() {
270e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher("app.a");
271e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps("app.a");
272e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
273e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.DEVICE_OWNER, true, false);
274e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
275e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(1)).onSuccess();
276e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps("app.a");
277e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        mDeletedApps.clear();
278e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
279e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher("app.a", "app.b");
280e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps("app.a", "app.b");
281e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
282e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        // Now, we set a wrong value to mTestContext.getFilesDir. So it should not find the system apps
283e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        // file. So it should not delete any app, but call onError().
284e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        when(mTestContext.getFilesDir()).thenReturn(new File(""));
285e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.DEVICE_OWNER, false, false);
286e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
287e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertDeletedApps();
288e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(1)).onError();
289e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
290e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
291e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    @SmallTest
292e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    public void testWhenDeletionFails() {
293e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setSystemAppsWithLauncher("app.a");
294e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setInstalledSystemApps("app.a");
295e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        mPackageManager.setDeletionSucceeds(false);
296e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        runTask(DeleteNonRequiredAppsTask.DEVICE_OWNER, true, false);
297e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        verify(mCallback, times(1)).onError();
298e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
299e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
300e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void setRequiredAppsManagedDevice(String... apps) {
301e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setStringArray(com.android.managedprovisioning.R.array.required_apps_managed_device,
302e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                apps);
303e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
304e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
305e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void setVendorRequiredAppsManagedDevice(String... apps) {
306e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setStringArray(com.android.managedprovisioning.R.array.vendor_required_apps_managed_device,
307e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                apps);
308e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
309e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
310e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void setDisallowedAppsManagedDevice(String... apps) {
311e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setStringArray(com.android.managedprovisioning.R.array.disallowed_apps_managed_device,
312e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                apps);
313e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
314e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
315e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void setVendorDisallowedAppsManagedDevice(String... apps) {
316e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setStringArray(
317e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                com.android.managedprovisioning.R.array.vendor_disallowed_apps_managed_device,
318e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                apps);
319e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
320e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
321e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void setRequiredAppsManagedProfile(String... apps) {
322e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setStringArray(com.android.managedprovisioning.R.array.required_apps_managed_profile,
323e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                apps);
324e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
325e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
326e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void setVendorRequiredAppsManagedProfile(String... apps) {
327e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setStringArray(com.android.managedprovisioning.R.array.vendor_required_apps_managed_profile,
328e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                apps);
329e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
330e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
331e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void setDisallowedAppsManagedProfile(String... apps) {
332e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setStringArray(com.android.managedprovisioning.R.array.disallowed_apps_managed_profile,
333e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                apps);
334e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
335e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
336e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void setVendorDisallowedAppsManagedProfile(String... apps) {
337e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        setStringArray(
338e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                com.android.managedprovisioning.R.array.vendor_disallowed_apps_managed_profile,
339e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                apps);
340e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
341e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
342a87044107965ce669350ff528687bf94d73ae248Alan Treadway    private void setRequiredAppsManagedUser(String... apps) {
343a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setStringArray(com.android.managedprovisioning.R.array.required_apps_managed_user,
344a87044107965ce669350ff528687bf94d73ae248Alan Treadway                apps);
345a87044107965ce669350ff528687bf94d73ae248Alan Treadway    }
346a87044107965ce669350ff528687bf94d73ae248Alan Treadway
347a87044107965ce669350ff528687bf94d73ae248Alan Treadway    private void setVendorRequiredAppsManagedUser(String... apps) {
348a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setStringArray(com.android.managedprovisioning.R.array.vendor_required_apps_managed_user,
349a87044107965ce669350ff528687bf94d73ae248Alan Treadway                apps);
350a87044107965ce669350ff528687bf94d73ae248Alan Treadway    }
351a87044107965ce669350ff528687bf94d73ae248Alan Treadway
352a87044107965ce669350ff528687bf94d73ae248Alan Treadway    private void setDisallowedAppsManagedUser(String... apps) {
353a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setStringArray(com.android.managedprovisioning.R.array.disallowed_apps_managed_user,
354a87044107965ce669350ff528687bf94d73ae248Alan Treadway                apps);
355a87044107965ce669350ff528687bf94d73ae248Alan Treadway    }
356a87044107965ce669350ff528687bf94d73ae248Alan Treadway
357a87044107965ce669350ff528687bf94d73ae248Alan Treadway    private void setVendorDisallowedAppsManagedUser(String... apps) {
358a87044107965ce669350ff528687bf94d73ae248Alan Treadway        setStringArray(
359a87044107965ce669350ff528687bf94d73ae248Alan Treadway                com.android.managedprovisioning.R.array.vendor_disallowed_apps_managed_user,
360a87044107965ce669350ff528687bf94d73ae248Alan Treadway                apps);
361a87044107965ce669350ff528687bf94d73ae248Alan Treadway    }
362a87044107965ce669350ff528687bf94d73ae248Alan Treadway
363e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void runTask(int type, boolean newProfile, boolean leaveAllSystemAppsEnabled) {
364e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        DeleteNonRequiredAppsTask task = new DeleteNonRequiredAppsTask(mTestContext,
365e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                mIPackageManager, mIInputMethodManager, TEST_MDM_PACKAGE_NAME, type, newProfile,
366e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                TEST_USER_ID, leaveAllSystemAppsEnabled, mCallback);
367e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        task.run();
368e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
369e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
370e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void setStringArray(int resourceId, String[] strs) {
371e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        when(mResources.getStringArray(eq(resourceId)))
372e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                .thenReturn(strs);
373e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
374e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
375e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void assertDeletedApps(String... appArray) {
376e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        assertEquals(setFromArray(appArray), mDeletedApps);
377e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
378e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
379e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void setInstalledSystemApps(String... installedApps) {
380e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        List<ApplicationInfo> applications = new ArrayList<ApplicationInfo>();
381e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        for (String app : installedApps) {
382e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            ApplicationInfo aInfo = new ApplicationInfo();
383e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            aInfo.flags = ApplicationInfo.FLAG_SYSTEM;
384e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            aInfo.packageName = app;
385e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            applications.add(aInfo);
386e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        }
387e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        try {
388e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            when(mIPackageManager.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES,
389e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                    TEST_USER_ID)).thenReturn(new ParceledListSlice(applications));
390e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        } catch (RemoteException e) {
391e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            fail(e.toString());
392e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        }
393e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        mInstalledApplications = setFromArray(installedApps);
394e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
395e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
396e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void setSystemInputMethods(String... packageNames) {
397e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        List<InputMethodInfo> inputMethods = new ArrayList<InputMethodInfo>();
398e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        for (String packageName : packageNames) {
399e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            ApplicationInfo aInfo = new ApplicationInfo();
400e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            aInfo.flags = ApplicationInfo.FLAG_SYSTEM;
401e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            ServiceInfo serviceInfo = new ServiceInfo();
402e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            serviceInfo.applicationInfo = aInfo;
403e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            serviceInfo.packageName = packageName;
404e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            serviceInfo.name = "";
405e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            ResolveInfo ri = new ResolveInfo();
406e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            ri.serviceInfo = serviceInfo;
407e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            InputMethodInfo inputMethodInfo = new InputMethodInfo(ri, false, null, null, 0, false);
408e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            inputMethods.add(inputMethodInfo);
409e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        }
410e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        try {
411e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            when(mIInputMethodManager.getInputMethodList()).thenReturn(inputMethods);
412e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        } catch (RemoteException e) {
413e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            fail(e.toString());
414e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        }
415e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
416e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
417e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private void setSystemAppsWithLauncher(String... apps) {
418e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        mSystemAppsWithLauncher = apps;
419e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
420e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
421e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    private <T> Set<T> setFromArray(T[] array) {
422e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        return new HashSet<T>(Arrays.asList(array));
423e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
424e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
425e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    class FakePackageManager extends MockPackageManager {
426e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        private boolean mDeletionSucceeds = true;
427e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
428e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        void setDeletionSucceeds(boolean deletionSucceeds) {
429e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            mDeletionSucceeds = deletionSucceeds;
430e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        }
431e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
432e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        @Override
433e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer,
434e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                int flags, int userId) {
435e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            if (mDeletionSucceeds) {
436e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                mDeletedApps.add(packageName);
437e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            }
438e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            assertTrue((flags & PackageManager.DELETE_SYSTEM_APP) != 0);
439e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            assertEquals(TEST_USER_ID, userId);
440e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
441e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            int resultCode;
442e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            if (mDeletionSucceeds) {
443e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                resultCode = PackageManager.DELETE_SUCCEEDED;
444e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            } else {
445e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                resultCode = PackageManager.DELETE_FAILED_INTERNAL_ERROR;
446e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            }
447e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
448e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            try {
449e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                observer.packageDeleted(packageName, resultCode);
450e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            } catch (RemoteException e) {
451e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                fail(e.toString());
452e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            }
453e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        }
454e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
455e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        @Override
456e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent, int flags, int userId) {
457e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            assertTrue("Expected an intent with action ACTION_MAIN and category CATEGORY_LAUNCHER",
458e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                    intent.filterEquals(
459e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                    new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)));
46050076cd3bb730b285114318a0faaaf3f81442eafKenny Guy            assertTrue("Expected the flag MATCH_UNINSTALLED_PACKAGES",
46150076cd3bb730b285114318a0faaaf3f81442eafKenny Guy                    (flags & PackageManager.MATCH_UNINSTALLED_PACKAGES) != 0);
46250076cd3bb730b285114318a0faaaf3f81442eafKenny Guy            assertTrue("Expected the flag MATCH_DISABLED_COMPONENTS",
46350076cd3bb730b285114318a0faaaf3f81442eafKenny Guy                    (flags & PackageManager.MATCH_DISABLED_COMPONENTS) != 0);
46450076cd3bb730b285114318a0faaaf3f81442eafKenny Guy            assertTrue("Expected the flag MATCH_ENCRYPTION_AWARE_AND_UNAWARE",
46550076cd3bb730b285114318a0faaaf3f81442eafKenny Guy                    (flags & PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE) != 0);
466e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            assertEquals(userId, TEST_USER_ID);
467e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            List<ResolveInfo> result = new ArrayList<ResolveInfo>();
468e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            for (String packageName : mSystemAppsWithLauncher) {
469e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                ActivityInfo ai = new ActivityInfo();
470e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                ai.packageName = packageName;
471e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                ResolveInfo ri = new ResolveInfo();
472e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                ri.activityInfo  = ai;
473e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                result.add(ri);
474e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            }
475e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            return result;
476e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        }
477e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
478e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        @Override
479e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
480e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                throws NameNotFoundException {
481e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            if (mInstalledApplications.contains(packageName)) {
482e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot                return new PackageInfo();
483e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            }
484e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot            throw new NameNotFoundException();
485e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot        }
486e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot    }
487e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot}
488e36926b4933d60a31858fd8c15249ac400d16752Nicolas Prevot
489