Add controls in dev options to quarantine apps.

Bug: 297934650
Test: m -j RunSettingsRoboTests
Test: atest SettingsUnitTests
Change-Id: Ic0f35b370c04dd4ed3baaf3610b46ff1b37a0463
diff --git a/tests/robotests/src/com/android/settings/development/quarantine/QuarantinedAppsScreenControllerTest.java b/tests/robotests/src/com/android/settings/development/quarantine/QuarantinedAppsScreenControllerTest.java
new file mode 100644
index 0000000..32ad0ad
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/development/quarantine/QuarantinedAppsScreenControllerTest.java
@@ -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;
+    }
+}
diff --git a/tests/unit/Android.bp b/tests/unit/Android.bp
index c0d63e1..196b809 100644
--- a/tests/unit/Android.bp
+++ b/tests/unit/Android.bp
@@ -27,7 +27,7 @@
         "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.
     ],
diff --git a/tests/unit/src/com/android/settings/development/quarantine/QuarantinedAppStateBridgeTest.java b/tests/unit/src/com/android/settings/development/quarantine/QuarantinedAppStateBridgeTest.java
new file mode 100644
index 0000000..707d2b9
--- /dev/null
+++ b/tests/unit/src/com/android/settings/development/quarantine/QuarantinedAppStateBridgeTest.java
@@ -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);
+    }
+}
diff --git a/tests/unit/src/com/android/settings/development/quarantine/QuarantinedAppsPreferenceControllerTest.java b/tests/unit/src/com/android/settings/development/quarantine/QuarantinedAppsPreferenceControllerTest.java
new file mode 100644
index 0000000..33e4392
--- /dev/null
+++ b/tests/unit/src/com/android/settings/development/quarantine/QuarantinedAppsPreferenceControllerTest.java
@@ -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);
+    }
+}