Add controls in dev options to quarantine apps.

Bug: 297934650
Test: m -j RunSettingsRoboTests
Test: atest SettingsUnitTests
Change-Id: Ic0f35b370c04dd4ed3baaf3610b46ff1b37a0463
This commit is contained in:
Sudheer Shanka
2023-08-27 04:45:45 +00:00
parent fcf31b668d
commit abbb5c4dc3
14 changed files with 857 additions and 1 deletions

View File

@@ -0,0 +1,95 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.development.quarantine;
import static org.mockito.AdditionalMatchers.aryEq;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import androidx.test.core.app.ApplicationProvider;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class QuarantinedAppsScreenControllerTest {
private static final String PREF_KEY = "quarantined_apps_screen";
private static final String TEST_PACKAGE = "com.example.test.pkg";
private static final int TEST_APP_ID = 1234;
private static final int TEST_USER_ID = 10;
private Context mContext;
private QuarantinedAppsScreenController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
mController = new QuarantinedAppsScreenController(mContext, PREF_KEY);
}
@Test
public void testOnPreferenceChange() {
final Context userContext = mock(Context.class);
doReturn(userContext).when(mContext).createContextAsUser(
eq(UserHandle.of(TEST_USER_ID)), anyInt());
final PackageManager packageManager = mock(PackageManager.class);
doReturn(packageManager).when(userContext).getPackageManager();
final AppEntry entry = createAppEntry(TEST_PACKAGE, TEST_APP_ID, TEST_USER_ID);
final QuarantinedAppPreference preference = new QuarantinedAppPreference(mContext, entry);
mController.onPreferenceChange(preference, true);
verify(packageManager).setPackagesSuspended(aryEq(new String[] {TEST_PACKAGE}), eq(true),
any(), any(), any(),
eq(PackageManager.FLAG_SUSPEND_QUARANTINED));
mController.onPreferenceChange(preference, false);
verify(packageManager).setPackagesSuspended(aryEq(new String[] {TEST_PACKAGE}), eq(false),
any(), any(), any(),
eq(PackageManager.FLAG_SUSPEND_QUARANTINED));
}
private AppEntry createAppEntry(String packageName, int appId, int userId) {
final AppEntry entry = mock(AppEntry.class);
entry.info = createApplicationInfo(packageName, appId, userId);
entry.extraInfo = false;
return entry;
}
private ApplicationInfo createApplicationInfo(String packageName, int appId, int userId) {
final ApplicationInfo info = new ApplicationInfo();
info.packageName = packageName;
info.uid = UserHandle.getUid(userId, appId);
return info;
}
}