Merge "Add controls in dev options to quarantine apps." into main

This commit is contained in:
Sudheer Shanka
2023-09-21 22:47:30 +00:00
committed by Android (Google) Code Review
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;
}
}

View File

@@ -27,7 +27,7 @@ android_test {
"platform-test-annotations",
"truth-prebuilt",
"kotlinx_coroutines_test",
"flag-junit-base",
"flag-junit",
// Don't add SettingsLib libraries here - you can use them directly as they are in the
// instrumented Settings app.
],

View File

@@ -0,0 +1,90 @@
/*
* 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 com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class QuarantinedAppStateBridgeTest {
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_1 = 0;
private static final int TEST_USER_ID_2 = 10;
@Mock
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void updateExtraInfo_packageQuarantined() throws Exception {
setPackageQuarantined(TEST_PACKAGE, TEST_USER_ID_1, false);
setPackageQuarantined(TEST_PACKAGE, TEST_USER_ID_2, true);
final QuarantinedAppStateBridge bridge =
new QuarantinedAppStateBridge(mContext, null, null);
final AppEntry entry = mock(AppEntry.class);
bridge.updateExtraInfo(entry, TEST_PACKAGE, UserHandle.getUid(TEST_USER_ID_2, TEST_APP_ID));
assertThat(entry.extraInfo).isEqualTo(true);
}
@Test
public void updateExtraInfo_packageNotQuarantined() throws Exception {
setPackageQuarantined(TEST_PACKAGE, TEST_USER_ID_1, false);
setPackageQuarantined(TEST_PACKAGE, TEST_USER_ID_2, false);
final QuarantinedAppStateBridge bridge =
new QuarantinedAppStateBridge(mContext, null, null);
final AppEntry entry = mock(AppEntry.class);
bridge.updateExtraInfo(entry, TEST_PACKAGE, UserHandle.getUid(TEST_USER_ID_2, TEST_APP_ID));
assertThat(entry.extraInfo).isEqualTo(false);
}
private void setPackageQuarantined(String packageName, int userId, boolean quarantined)
throws Exception {
final Context userContext = mock(Context.class);
when(mContext.createContextAsUser(eq(UserHandle.of(userId)), anyInt()))
.thenReturn(userContext);
final PackageManager packageManager = mock(PackageManager.class);
when(userContext.getPackageManager()).thenReturn(packageManager);
when(packageManager.isPackageQuarantined(packageName)).thenReturn(quarantined);
}
}

View File

@@ -0,0 +1,69 @@
/*
* 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 com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static org.junit.Assert.assertEquals;
import android.content.Context;
import android.content.pm.Flags;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class QuarantinedAppsPreferenceControllerTest {
private static final String PREF_KEY = "quarantined_apps";
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
@Mock
private Context mContext;
private QuarantinedAppsPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new QuarantinedAppsPreferenceController(mContext, PREF_KEY);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_QUARANTINED_ENABLED)
public void testAvailabilityStatus_flagEnabled() {
assertEquals(mController.getAvailabilityStatus(), AVAILABLE);
}
@Test
@RequiresFlagsDisabled(Flags.FLAG_QUARANTINED_ENABLED)
public void testAvailabilityStatus_flagDisabled() {
assertEquals(mController.getAvailabilityStatus(), CONDITIONALLY_UNAVAILABLE);
}
}