Merge "Bug fix: null string will cause exception in checkStringInAdbCommandOutput"
diff --git a/src/com/android/settings/network/ActiveSubsciptionsListener.java b/src/com/android/settings/network/ActiveSubscriptionsListener.java
similarity index 97%
rename from src/com/android/settings/network/ActiveSubsciptionsListener.java
rename to src/com/android/settings/network/ActiveSubscriptionsListener.java
index 26f6441..91a4c0a 100644
--- a/src/com/android/settings/network/ActiveSubsciptionsListener.java
+++ b/src/com/android/settings/network/ActiveSubscriptionsListener.java
@@ -39,7 +39,7 @@
 /**
  * A listener for active subscription change
  */
-public abstract class ActiveSubsciptionsListener
+public abstract class ActiveSubscriptionsListener
         extends SubscriptionManager.OnSubscriptionsChangedListener
         implements AutoCloseable {
 
@@ -73,7 +73,7 @@
      * @param looper {@code Looper} of this listener
      * @param context {@code Context} of this listener
      */
-    public ActiveSubsciptionsListener(Looper looper, Context context) {
+    public ActiveSubscriptionsListener(Looper looper, Context context) {
         this(looper, context, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
     }
 
@@ -84,7 +84,7 @@
      * @param context {@code Context} of this listener
      * @param subscriptionId for subscription on this listener
      */
-    public ActiveSubsciptionsListener(Looper looper, Context context, int subscriptionId) {
+    public ActiveSubscriptionsListener(Looper looper, Context context, int subscriptionId) {
         super(looper);
         mLooper = looper;
         mContext = context;
diff --git a/src/com/android/settings/network/ProxySubscriptionManager.java b/src/com/android/settings/network/ProxySubscriptionManager.java
index a89cc83..eb1a7d4 100644
--- a/src/com/android/settings/network/ProxySubscriptionManager.java
+++ b/src/com/android/settings/network/ProxySubscriptionManager.java
@@ -77,7 +77,7 @@
         mActiveSubscriptionsListeners =
                 new ArrayList<OnActiveSubscriptionChangedListener>();
 
-        mSubscriptionMonitor = new ActiveSubsciptionsListener(looper, context) {
+        mSubscriptionMonitor = new ActiveSubscriptionsListener(looper, context) {
             public void onChanged() {
                 notifyAllListeners();
             }
@@ -94,7 +94,7 @@
     }
 
     private Lifecycle mLifecycle;
-    private ActiveSubsciptionsListener mSubscriptionMonitor;
+    private ActiveSubscriptionsListener mSubscriptionMonitor;
     private GlobalSettingsChangeListener mAirplaneModeMonitor;
 
     private List<OnActiveSubscriptionChangedListener> mActiveSubscriptionsListeners;
diff --git a/src/com/android/settings/network/telephony/MobileNetworkSettings.java b/src/com/android/settings/network/telephony/MobileNetworkSettings.java
index f54c18b..adf399f 100644
--- a/src/com/android/settings/network/telephony/MobileNetworkSettings.java
+++ b/src/com/android/settings/network/telephony/MobileNetworkSettings.java
@@ -37,7 +37,7 @@
 import com.android.settings.R;
 import com.android.settings.datausage.BillingCyclePreferenceController;
 import com.android.settings.datausage.DataUsageSummaryPreferenceController;
-import com.android.settings.network.ActiveSubsciptionsListener;
+import com.android.settings.network.ActiveSubscriptionsListener;
 import com.android.settings.network.CarrierWifiTogglePreferenceController;
 import com.android.settings.network.telephony.cdma.CdmaSubscriptionPreferenceController;
 import com.android.settings.network.telephony.cdma.CdmaSystemSelectPreferenceController;
@@ -74,9 +74,9 @@
     private UserManager mUserManager;
     private String mClickedPrefKey;
 
-    private ActiveSubsciptionsListener mActiveSubsciptionsListener;
+    private ActiveSubscriptionsListener mActiveSubscriptionsListener;
     private boolean mDropFirstSubscriptionChangeNotify;
-    private int mActiveSubsciptionsListenerCount;
+    private int mActiveSubscriptionsListenerCount;
 
     public MobileNetworkSettings() {
         super(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
@@ -218,8 +218,8 @@
         super.onResume();
         // TODO: remove log after fixing b/182326102
         Log.d(LOG_TAG, "onResume() subId=" + mSubId);
-        if (mActiveSubsciptionsListener == null) {
-            mActiveSubsciptionsListener = new ActiveSubsciptionsListener(
+        if (mActiveSubscriptionsListener == null) {
+            mActiveSubscriptionsListener = new ActiveSubscriptionsListener(
                     getContext().getMainLooper(), getContext(), mSubId) {
                 public void onChanged() {
                     onSubscriptionDetailChanged();
@@ -227,7 +227,7 @@
             };
             mDropFirstSubscriptionChangeNotify = true;
         }
-        mActiveSubsciptionsListener.start();
+        mActiveSubscriptionsListener.start();
     }
 
     private void onSubscriptionDetailChanged() {
@@ -236,21 +236,21 @@
             Log.d(LOG_TAG, "Callback during onResume()");
             return;
         }
-        mActiveSubsciptionsListenerCount++;
-        if (mActiveSubsciptionsListenerCount != 1) {
+        mActiveSubscriptionsListenerCount++;
+        if (mActiveSubscriptionsListenerCount != 1) {
             return;
         }
 
         ThreadUtils.postOnMainThread(() -> {
-            mActiveSubsciptionsListenerCount = 0;
+            mActiveSubscriptionsListenerCount = 0;
             redrawPreferenceControllers();
         });
     }
 
     @Override
     public void onDestroy() {
-        if (mActiveSubsciptionsListener != null) {
-            mActiveSubsciptionsListener.stop();
+        if (mActiveSubscriptionsListener != null) {
+            mActiveSubscriptionsListener.stop();
         }
         super.onDestroy();
     }
diff --git a/tests/unit/src/com/android/settings/network/ActiveSubsciptionsListenerTest.java b/tests/unit/src/com/android/settings/network/ActiveSubscriptionsListenerTest.java
similarity index 94%
rename from tests/unit/src/com/android/settings/network/ActiveSubsciptionsListenerTest.java
rename to tests/unit/src/com/android/settings/network/ActiveSubscriptionsListenerTest.java
index efa21ae..27bc40c 100644
--- a/tests/unit/src/com/android/settings/network/ActiveSubsciptionsListenerTest.java
+++ b/tests/unit/src/com/android/settings/network/ActiveSubscriptionsListenerTest.java
@@ -49,7 +49,7 @@
 import java.util.List;
 
 @RunWith(AndroidJUnit4.class)
-public class ActiveSubsciptionsListenerTest {
+public class ActiveSubscriptionsListenerTest {
     private static final int SUB_ID1 = 3;
     private static final int SUB_ID2 = 7;
 
@@ -68,7 +68,7 @@
     private SubscriptionManager mSubscriptionManager;
     private List<SubscriptionInfo> mActiveSubscriptions;
 
-    private ActiveSubsciptionsListenerImpl mListener;
+    private ActiveSubscriptionsListenerImpl mListener;
     private BroadcastReceiver mReceiver;
 
     @Before
@@ -83,7 +83,7 @@
         addMockSubscription(SUB_ID2);
         doReturn(mActiveSubscriptions).when(mSubscriptionManager).getActiveSubscriptionInfoList();
 
-        mListener = spy(new ActiveSubsciptionsListenerImpl(Looper.getMainLooper(), mContext));
+        mListener = spy(new ActiveSubscriptionsListenerImpl(Looper.getMainLooper(), mContext));
         doReturn(mSubscriptionManager).when(mListener).getSubscriptionManager();
         mReceiver = mListener.getSubscriptionChangeReceiver();
     }
@@ -99,8 +99,8 @@
         mListener.close();
     }
 
-    public class ActiveSubsciptionsListenerImpl extends ActiveSubsciptionsListener {
-        public ActiveSubsciptionsListenerImpl(Looper looper, Context context) {
+    public class ActiveSubscriptionsListenerImpl extends ActiveSubscriptionsListener {
+        public ActiveSubscriptionsListenerImpl(Looper looper, Context context) {
             super(looper, context);
         }
 
diff --git a/tests/unit/src/com/android/settings/vpn2/AppPreferenceTest.java b/tests/unit/src/com/android/settings/vpn2/AppPreferenceTest.java
new file mode 100644
index 0000000..1f618cc
--- /dev/null
+++ b/tests/unit/src/com/android/settings/vpn2/AppPreferenceTest.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2021 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.vpn2;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.junit.Assert.assertFalse;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import android.app.admin.DevicePolicyManager;
+import android.content.Context;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.os.UserHandle;
+
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+/** Unittest for AppPreference */
+@RunWith(AndroidJUnit4.class)
+public class AppPreferenceTest {
+    // Additional mocking of the underying classes is necsesary if another user id is used.
+    private static final int USER_ID = UserHandle.USER_NULL;
+    private static final String PACKAGE_NAME = "test_package";
+    private static final String DIFFERENT_PACKAGE_NAME = "not_test_package";
+
+    @Mock
+    private DevicePolicyManager mDevicePolicyManager;
+
+    private Context mContext;
+    private AppPreference mAppPreference;
+
+    @Before
+    public void setUp() throws NameNotFoundException {
+        MockitoAnnotations.initMocks(this);
+
+        mContext = spy(ApplicationProvider.getApplicationContext());
+        doReturn(mContext).when(mContext).createContextAsUser(any(), anyInt());
+        doReturn(mContext).when(mContext).createPackageContextAsUser(any(), anyInt(), any());
+        when(mContext.getSystemService(DevicePolicyManager.class)).thenReturn(mDevicePolicyManager);
+    }
+
+    @Test
+    public void getPackageName_returnsAccuratePackageName() {
+        doReturn(DIFFERENT_PACKAGE_NAME).when(mDevicePolicyManager).getAlwaysOnVpnPackage();
+
+        mAppPreference = spy(new AppPreference(mContext, USER_ID, PACKAGE_NAME));
+        assertThat(mAppPreference.getPackageName()).isEqualTo(PACKAGE_NAME);
+    }
+
+    @Test
+    public void disableIfConfiguredByAdmin_packageNameNotEqualsAlwaysOn_shouldEnable() {
+        doReturn(DIFFERENT_PACKAGE_NAME).when(mDevicePolicyManager).getAlwaysOnVpnPackage();
+
+        mAppPreference = spy(new AppPreference(mContext, USER_ID, PACKAGE_NAME));
+        assertFalse(mAppPreference.isDisabledByAdmin());
+    }
+}