Snap for 9042845 from 807d8a2e56ee46105ebde8d89e99c3cca3851f21 to mainline-permission-release

Change-Id: I093fb68537e78e24c5a04e1c0274f5f6907494b6
diff --git a/Android.bp b/Android.bp
index aa0f42d..a6cf966 100644
--- a/Android.bp
+++ b/Android.bp
@@ -397,6 +397,7 @@
     jarjar_rules: ":NetworkStackJarJarRules",
     use_embedded_native_libs: true,
     optimize: {
+        ignore_warnings: false,
         proguard_flags_files: ["proguard.flags"],
     },
 }
diff --git a/common/networkstackclient/Android.bp b/common/networkstackclient/Android.bp
index 038caad..d92badd 100644
--- a/common/networkstackclient/Android.bp
+++ b/common/networkstackclient/Android.bp
@@ -34,7 +34,7 @@
         java: {
             apex_available: [
                 "//apex_available:platform",
-                "com.android.bluetooth",
+                "com.android.btservices",
                 "com.android.wifi",
                 "com.android.tethering",
             ],
@@ -110,7 +110,7 @@
         java: {
             apex_available: [
                 "//apex_available:platform",
-                "com.android.bluetooth",
+                "com.android.btservices",
                 "com.android.wifi",
                 "com.android.tethering",
             ],
@@ -164,7 +164,7 @@
     visibility: ["//packages/modules/NetworkStack:__subpackages__"],
     apex_available: [
         "//apex_available:platform",
-        "com.android.bluetooth",
+        "com.android.btservices",
         "com.android.tethering",
         "com.android.wifi",
     ],
@@ -208,7 +208,7 @@
     ],
     apex_available: [
         "//apex_available:platform",
-        "com.android.bluetooth",
+        "com.android.btservices",
         "com.android.tethering",
         "com.android.wifi",
     ],
diff --git a/src/com/android/server/connectivity/NetworkMonitor.java b/src/com/android/server/connectivity/NetworkMonitor.java
index d98c5e4..da6c289 100755
--- a/src/com/android/server/connectivity/NetworkMonitor.java
+++ b/src/com/android/server/connectivity/NetworkMonitor.java
@@ -125,7 +125,6 @@
 import android.os.Process;
 import android.os.RemoteException;
 import android.os.SystemClock;
-import android.os.SystemProperties;
 import android.provider.DeviceConfig;
 import android.provider.Settings;
 import android.stats.connectivity.ProbeResult;
@@ -226,9 +225,7 @@
     private static final String TAG = NetworkMonitor.class.getSimpleName();
     private static final boolean DBG  = true;
     private static final boolean VDBG = false;
-    // TODO(b/185082309): For flaky test debug only, remove it after fixing.
-    private static final boolean DDBG_STALL = "cf_x86_auto-userdebug".equals(
-            SystemProperties.get("ro.build.flavor", ""));
+    private static final boolean DDBG_STALL = false;
     private static final boolean VDBG_STALL = Log.isLoggable(TAG, Log.DEBUG);
     private static final String DEFAULT_USER_AGENT    = "Mozilla/5.0 (X11; Linux x86_64) "
                                                       + "AppleWebKit/537.36 (KHTML, like Gecko) "
diff --git a/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java b/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java
index 6cc4bfb..dac555e 100644
--- a/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java
+++ b/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java
@@ -92,7 +92,6 @@
 import static org.mockito.Mockito.timeout;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
 
 import static java.lang.System.currentTimeMillis;
 import static java.util.Collections.singletonList;
@@ -301,6 +300,7 @@
 
     private static final int HANDLER_TIMEOUT_MS = 1000;
     private static final int TEST_MIN_STALL_EVALUATE_INTERVAL_MS = 500;
+    private static final int TEST_MIN_VALID_STALL_DNS_TIME_THRESHOLD_MS = 5000;
     private static final int STALL_EXPECTED_LAST_PROBE_TIME_MS =
             TEST_MIN_STALL_EVALUATE_INTERVAL_MS + HANDLER_TIMEOUT_MS;
     private static final NetworkAgentConfigShim TEST_AGENT_CONFIG =
@@ -475,46 +475,46 @@
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
-        when(mDependencies.getPrivateDnsBypassNetwork(any())).thenReturn(mCleartextDnsNetwork);
-        when(mDependencies.getDnsResolver()).thenReturn(mDnsResolver);
-        when(mDependencies.getRandom()).thenReturn(mRandom);
-        when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_MODE), anyInt()))
-                .thenReturn(Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT);
-        when(mDependencies.getDeviceConfigPropertyInt(any(), eq(CAPTIVE_PORTAL_USE_HTTPS),
-                anyInt())).thenReturn(1);
-        when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTP_URL), any()))
-                .thenReturn(TEST_HTTP_URL);
-        when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTPS_URL), any()))
-                .thenReturn(TEST_HTTPS_URL);
+        doReturn(mCleartextDnsNetwork).when(mDependencies).getPrivateDnsBypassNetwork(any());
+        doReturn(mDnsResolver).when(mDependencies).getDnsResolver();
+        doReturn(mRandom).when(mDependencies).getRandom();
+        doReturn(Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT).when(mDependencies)
+                .getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_MODE), anyInt());
+        doReturn(1).when(mDependencies)
+                .getDeviceConfigPropertyInt(any(), eq(CAPTIVE_PORTAL_USE_HTTPS), anyInt());
+        doReturn(TEST_HTTP_URL).when(mDependencies)
+                .getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTP_URL), any());
+        doReturn(TEST_HTTPS_URL).when(mDependencies)
+                .getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTPS_URL), any());
 
         doReturn(mCleartextDnsNetwork).when(mNetwork).getPrivateDnsBypassingCopy();
 
-        when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mCm);
-        when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephony);
-        when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifi);
-        when(mContext.getResources()).thenReturn(mResources);
+        doReturn(mCm).when(mContext).getSystemService(Context.CONNECTIVITY_SERVICE);
+        doReturn(mTelephony).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
+        doReturn(mWifi).when(mContext).getSystemService(Context.WIFI_SERVICE);
+        doReturn(mResources).when(mContext).getResources();
 
-        when(mServiceManager.getNotifier()).thenReturn(mNotifier);
+        doReturn(mNotifier).when(mServiceManager).getNotifier();
 
-        when(mTelephony.getDataNetworkType()).thenReturn(TelephonyManager.NETWORK_TYPE_LTE);
-        when(mTelephony.getNetworkOperator()).thenReturn(TEST_MCCMNC);
-        when(mTelephony.getSimOperator()).thenReturn(TEST_MCCMNC);
+        doReturn(TelephonyManager.NETWORK_TYPE_LTE).when(mTelephony).getDataNetworkType();
+        doReturn(TEST_MCCMNC).when(mTelephony).getNetworkOperator();
+        doReturn(TEST_MCCMNC).when(mTelephony).getSimOperator();
 
-        when(mResources.getString(anyInt())).thenReturn("");
-        when(mResources.getStringArray(anyInt())).thenReturn(new String[0]);
+        doReturn("").when(mResources).getString(anyInt());
+        doReturn(new String[0]).when(mResources).getStringArray(anyInt());
         doReturn(mConfiguration).when(mResources).getConfiguration();
-        when(mMccContext.getResources()).thenReturn(mMccResource);
+        doReturn(mMccResource).when(mMccContext).getResources();
 
         setFallbackUrl(TEST_FALLBACK_URL);
         setOtherFallbackUrls(TEST_OTHER_FALLBACK_URL);
         setFallbackSpecs(null); // Test with no fallback spec by default
-        when(mRandom.nextInt()).thenReturn(0);
+        doReturn(0).when(mRandom).nextInt();
 
-        when(mTstDependencies.getNetd()).thenReturn(mNetd);
+        doReturn(mNetd).when(mTstDependencies).getNetd();
         // DNS probe timeout should not be defined more than half of HANDLER_TIMEOUT_MS. Otherwise,
         // it will fail the test because of timeout expired for querying AAAA and A sequentially.
-        when(mResources.getInteger(eq(R.integer.config_captive_portal_dns_probe_timeout)))
-                .thenReturn(200);
+        doReturn(200).when(mResources)
+                .getInteger(eq(R.integer.config_captive_portal_dns_probe_timeout));
 
         doAnswer((invocation) -> {
             URL url = invocation.getArgument(0);
@@ -547,8 +547,8 @@
                     return null;
             }
         }).when(mCleartextDnsNetwork).openConnection(any());
-        when(mHttpConnection.getRequestProperties()).thenReturn(new ArrayMap<>());
-        when(mHttpsConnection.getRequestProperties()).thenReturn(new ArrayMap<>());
+        doReturn(new ArrayMap<>()).when(mHttpConnection).getRequestProperties();
+        doReturn(new ArrayMap<>()).when(mHttpsConnection).getRequestProperties();
 
         mFakeDns = new FakeDns();
         mFakeDns.startMocking();
@@ -560,15 +560,14 @@
         mFakeDns.setAnswer(PRIVATE_DNS_PROBE_HOST_SUFFIX, new String[]{"192.0.2.2"}, TYPE_A);
         mFakeDns.setAnswer(PRIVATE_DNS_PROBE_HOST_SUFFIX, new String[]{"2001:db8::1"}, TYPE_AAAA);
 
-        when(mContext.registerReceiver(any(BroadcastReceiver.class), any())).then((invocation) -> {
+        doAnswer((invocation) -> {
             mRegisteredReceivers.add(invocation.getArgument(0));
             return new Intent();
-        });
-        when(mContext.registerReceiver(any(BroadcastReceiver.class), any(), anyInt())).then(
-                (invocation) -> {
-                    mRegisteredReceivers.add(invocation.getArgument(0));
-                    return new Intent();
-                });
+        }).when(mContext).registerReceiver(any(BroadcastReceiver.class), any());
+        doAnswer((invocation) -> {
+            mRegisteredReceivers.add(invocation.getArgument(0));
+            return new Intent();
+        }).when(mContext).registerReceiver(any(BroadcastReceiver.class), any(), anyInt());
 
         doAnswer((invocation) -> {
             mRegisteredReceivers.remove(invocation.getArgument(0));
@@ -579,7 +578,7 @@
 
         setMinDataStallEvaluateInterval(TEST_MIN_STALL_EVALUATE_INTERVAL_MS);
         setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS);
-        setValidDataStallDnsTimeThreshold(500);
+        setValidDataStallDnsTimeThreshold(TEST_MIN_VALID_STALL_DNS_TIME_THRESHOLD_MS);
         setConsecutiveDnsTimeoutThreshold(5);
         mCreatedNetworkMonitors = new HashSet<>();
         mRegisteredReceivers = new HashSet<>();
@@ -677,7 +676,7 @@
         setNetworkCapabilities(nm, nc);
         HandlerUtils.waitForIdle(nm.getHandler(), HANDLER_TIMEOUT_MS);
         mCreatedNetworkMonitors.add(nm);
-        when(mTstDependencies.isTcpInfoParsingSupported()).thenReturn(false);
+        doReturn(false).when(mTstDependencies).isTcpInfoParsingSupported();
 
         return nm;
     }
@@ -890,8 +889,8 @@
         doReturn(TEST_HTTP_URL).when(mResources).getString(R.string.config_captive_portal_http_url);
         doReturn(TEST_HTTP_OTHER_URL2).when(mResources).getString(
                 R.string.default_captive_portal_http_url);
-        when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTP_URL), any()))
-                .thenReturn(TEST_HTTP_OTHER_URL1);
+        doReturn(TEST_HTTP_OTHER_URL1).when(mDependencies)
+                .getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTP_URL), any());
         final WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor();
         assertEquals(TEST_HTTP_URL, wnm.getCaptivePortalServerHttpUrl(mContext));
         // If config_captive_portal_http_url is unset and the global setting is set, the global
@@ -901,8 +900,8 @@
         // If both config_captive_portal_http_url and global setting are unset,
         // default_captive_portal_http_url is used. But the global setting will only be read in the
         // constructor.
-        when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTP_URL), any()))
-                .thenReturn(null);
+        doReturn(null).when(mDependencies)
+                .getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTP_URL), any());
         assertEquals(TEST_HTTP_OTHER_URL1, wnm.getCaptivePortalServerHttpUrl(mContext));
         // default_captive_portal_http_url is used when the configuration is applied in new NM.
         final WrappedNetworkMonitor wnm2 = makeCellNotMeteredNetworkMonitor();
@@ -1050,8 +1049,8 @@
         // Clear setting provider value. Verify it to get configuration from resource instead.
         setFallbackUrl(null);
         // Verify that getting resource with exception.
-        when(mResources.getStringArray(R.array.config_captive_portal_fallback_urls))
-                .thenThrow(Resources.NotFoundException.class);
+        doThrow(Resources.NotFoundException.class).when(mResources)
+                .getStringArray(R.array.config_captive_portal_fallback_urls);
         urls = wnm.makeCaptivePortalFallbackUrls(mContext);
         assertEquals(urls.length, 0);
 
@@ -1138,17 +1137,16 @@
                 DEFAULT_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT));
 
         // Set device config. Expect to get device config.
-        when(mDependencies.getDeviceConfigPropertyInt(any(),
-                eq(NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT), anyInt()))
-                        .thenReturn(1234);
+        doReturn(1234).when(mDependencies).getDeviceConfigPropertyInt(any(),
+                eq(NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT), anyInt());
         assertEquals(1234, wnm.getIntSetting(mContext,
                 R.integer.config_captive_portal_dns_probe_timeout,
                 NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT,
                 DEFAULT_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT));
 
         // Set config resource. Expect to get config resource.
-        when(mResources.getInteger(eq(R.integer.config_captive_portal_dns_probe_timeout)))
-                .thenReturn(5678);
+        doReturn(5678).when(mResources)
+                        .getInteger(eq(R.integer.config_captive_portal_dns_probe_timeout));
         assertEquals(5678, wnm.getIntSetting(mContext,
                 R.integer.config_captive_portal_dns_probe_timeout,
                 NetworkMonitor.CONFIG_CAPTIVE_PORTAL_DNS_PROBE_TIMEOUT,
@@ -1202,16 +1200,16 @@
 
     @Test
     public void testIsCaptivePortal_PrivateIpNotPortal_Enabled_IPv4() throws Exception {
-        when(mDependencies.isFeatureEnabled(any(), eq(DNS_PROBE_PRIVATE_IP_NO_INTERNET_VERSION)))
-                .thenReturn(true);
+        doReturn(true).when(mDependencies)
+                .isFeatureEnabled(any(), eq(DNS_PROBE_PRIVATE_IP_NO_INTERNET_VERSION));
         setupPrivateIpResponse("192.168.1.1");
         runFailedNetworkTest();
     }
 
     @Test
     public void testIsCaptivePortal_PrivateIpNotPortal_Enabled_IPv6() throws Exception {
-        when(mDependencies.isFeatureEnabled(any(), eq(DNS_PROBE_PRIVATE_IP_NO_INTERNET_VERSION)))
-                .thenReturn(true);
+        doReturn(true).when(mDependencies)
+                .isFeatureEnabled(any(), eq(DNS_PROBE_PRIVATE_IP_NO_INTERNET_VERSION));
         setupPrivateIpResponse("fec0:1234::1");
         runFailedNetworkTest();
     }
@@ -1268,7 +1266,7 @@
         setPortal302(mOtherFallbackConnection);
 
         // TEST_OTHER_FALLBACK_URL is third
-        when(mRandom.nextInt()).thenReturn(2);
+        doReturn(2).when(mRandom).nextInt();
 
         // First check always uses the first fallback URL: inconclusive
         final NetworkMonitor monitor = runFailedNetworkTest();
@@ -1613,7 +1611,7 @@
         setStatus(mHttpConnection, 500);
 
         // Use the 2nd fallback spec
-        when(mRandom.nextInt()).thenReturn(1);
+        doReturn(1).when(mRandom).nextInt();
     }
 
     @Test
@@ -1819,9 +1817,9 @@
     public void testIsDataStall_SkipEvaluateOnValidationNotRequiredNetwork() {
         // Make DNS and TCP stall condition satisfied.
         setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS | DATA_STALL_EVALUATION_TYPE_TCP);
-        when(mTstDependencies.isTcpInfoParsingSupported()).thenReturn(true);
-        when(mTst.getLatestReceivedCount()).thenReturn(0);
-        when(mTst.isDataStallSuspected()).thenReturn(true);
+        doReturn(true).when(mTstDependencies).isTcpInfoParsingSupported();
+        doReturn(0).when(mTst).getLatestReceivedCount();
+        doReturn(true).when(mTst).isDataStallSuspected();
         final WrappedNetworkMonitor nm = makeMonitor(CELL_NO_INTERNET_CAPABILITIES);
         nm.setLastProbeTime(SystemClock.elapsedRealtime() - 1000);
         makeDnsTimeoutEvent(nm, DEFAULT_DNS_TIMEOUT_THRESHOLD);
@@ -1854,21 +1852,21 @@
 
     @Test
     public void testIsDataStall_EvaluationTcp() throws Exception {
-        when(mTstDependencies.isTcpInfoParsingSupported()).thenReturn(true);
+        doReturn(true).when(mTstDependencies).isTcpInfoParsingSupported();
         // Evaluate TCP only. Expect ignoring DNS signal.
         setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_TCP);
         WrappedNetworkMonitor wrappedMonitor = makeMonitor(CELL_METERED_CAPABILITIES);
         assertFalse(wrappedMonitor.isDataStall());
         // Packet received.
-        when(mTst.getLatestReceivedCount()).thenReturn(5);
+        doReturn(5).when(mTst).getLatestReceivedCount();
         // Trigger a tcp event immediately.
         setTcpPollingInterval(0);
         wrappedMonitor.sendTcpPollingEvent();
         HandlerUtils.waitForIdle(wrappedMonitor.getHandler(), HANDLER_TIMEOUT_MS);
         assertFalse(wrappedMonitor.isDataStall());
 
-        when(mTst.getLatestReceivedCount()).thenReturn(0);
-        when(mTst.isDataStallSuspected()).thenReturn(true);
+        doReturn(0).when(mTst).getLatestReceivedCount();
+        doReturn(true).when(mTst).isDataStallSuspected();
         // Trigger a tcp event immediately.
         setTcpPollingInterval(0);
         wrappedMonitor.sendTcpPollingEvent();
@@ -1888,7 +1886,7 @@
         verify(mCallbacks).notifyDataStallSuspected(
                 matchDnsAndTcpDataStallParcelable(DEFAULT_DNS_TIMEOUT_THRESHOLD));
 
-        when(mTst.getLatestReceivedCount()).thenReturn(5);
+        doReturn(5).when(mTst).getLatestReceivedCount();
         // Trigger a tcp event immediately.
         setTcpPollingInterval(0);
         nm.sendTcpPollingEvent();
@@ -2010,7 +2008,7 @@
     public void setupAndLaunchCaptivePortalApp(final NetworkMonitor nm) throws Exception {
         setSslException(mHttpsConnection);
         setPortal302(mHttpConnection);
-        when(mHttpConnection.getHeaderField(eq("location"))).thenReturn(TEST_LOGIN_URL);
+        doReturn(TEST_LOGIN_URL).when(mHttpConnection).getHeaderField(eq("location"));
         notifyNetworkConnected(nm, CELL_METERED_CAPABILITIES);
 
         verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1))
@@ -2291,12 +2289,12 @@
     }
 
     private void setupTcpDataStall() {
-        when(mTstDependencies.isTcpInfoParsingSupported()).thenReturn(true);
-        when(mTst.getLatestReceivedCount()).thenReturn(0);
-        when(mTst.getLatestPacketFailPercentage()).thenReturn(TEST_TCP_FAIL_RATE);
-        when(mTst.getSentSinceLastRecv()).thenReturn(TEST_TCP_PACKET_COUNT);
-        when(mTst.isDataStallSuspected()).thenReturn(true);
-        when(mTst.pollSocketsInfo()).thenReturn(true);
+        doReturn(true).when(mTstDependencies).isTcpInfoParsingSupported();
+        doReturn(0).when(mTst).getLatestReceivedCount();
+        doReturn(TEST_TCP_FAIL_RATE).when(mTst).getLatestPacketFailPercentage();
+        doReturn(TEST_TCP_PACKET_COUNT).when(mTst).getSentSinceLastRecv();
+        doReturn(true).when(mTst).isDataStallSuspected();
+        doReturn(true).when(mTst).pollSocketsInfo();
     }
 
     private void verifySendDataStallDetectionStats(WrappedNetworkMonitor nm, int evalType,
@@ -2417,10 +2415,10 @@
     }
 
     private void setupTestWifiInfo() {
-        when(mWifi.getConnectionInfo()).thenReturn(mWifiInfo);
-        when(mWifiInfo.getRssi()).thenReturn(TEST_SIGNAL_STRENGTH);
+        doReturn(mWifiInfo).when(mWifi).getConnectionInfo();
+        doReturn(TEST_SIGNAL_STRENGTH).when(mWifiInfo).getRssi();
         // Set to 2.4G band. Map to DataStallEventProto.AP_BAND_2GHZ proto definition.
-        when(mWifiInfo.getFrequency()).thenReturn(2450);
+        doReturn(2450).when(mWifiInfo).getFrequency();
     }
 
     private void testDataStallMetricsWithCellular(int evalType) {
@@ -2631,7 +2629,7 @@
         setDismissPortalInValidatedNetwork(true);
         setSslException(mHttpsConnection);
         setPortal302(mHttpConnection);
-        when(mHttpConnection.getHeaderField(eq("location"))).thenReturn(locationUrl);
+        doReturn(locationUrl).when(mHttpConnection).getHeaderField(eq("location"));
         final NetworkMonitor nm = makeMonitor(CELL_METERED_CAPABILITIES);
         notifyNetworkConnected(nm, CELL_METERED_CAPABILITIES);
 
@@ -2832,8 +2830,9 @@
     public void testIsCaptivePortal_FromExternalSource() throws Exception {
         assumeTrue(CaptivePortalDataShimImpl.isSupported());
         assumeTrue(ShimUtils.isAtLeastS());
-        when(mDependencies.isFeatureEnabled(any(), eq(NAMESPACE_CONNECTIVITY),
-                eq(DISMISS_PORTAL_IN_VALIDATED_NETWORK), anyBoolean())).thenReturn(true);
+        doReturn(true).when(mDependencies)
+                .isFeatureEnabled(any(), eq(NAMESPACE_CONNECTIVITY),
+                        eq(DISMISS_PORTAL_IN_VALIDATED_NETWORK), anyBoolean());
         final NetworkMonitor monitor = makeMonitor(WIFI_NOT_METERED_CAPABILITIES);
 
         NetworkInformationShim networkShim = NetworkInformationShimImpl.newInstance();
@@ -2940,10 +2939,10 @@
 
     private void setupResourceForMultipleProbes() {
         // Configure the resource to send multiple probe.
-        when(mResources.getStringArray(R.array.config_captive_portal_https_urls))
-                .thenReturn(TEST_HTTPS_URLS);
-        when(mResources.getStringArray(R.array.config_captive_portal_http_urls))
-                .thenReturn(TEST_HTTP_URLS);
+        doReturn(TEST_HTTPS_URLS).when(mResources)
+                .getStringArray(R.array.config_captive_portal_https_urls);
+        doReturn(TEST_HTTP_URLS).when(mResources)
+                .getStringArray(R.array.config_captive_portal_http_urls);
     }
 
     private void makeDnsTimeoutEvent(WrappedNetworkMonitor wrappedMonitor, int count) {
@@ -2965,23 +2964,23 @@
     }
 
     private void setDataStallEvaluationType(int type) {
-        when(mDependencies.getDeviceConfigPropertyInt(any(),
-            eq(CONFIG_DATA_STALL_EVALUATION_TYPE), anyInt())).thenReturn(type);
+        doReturn(type).when(mDependencies).getDeviceConfigPropertyInt(any(),
+                eq(CONFIG_DATA_STALL_EVALUATION_TYPE), anyInt());
     }
 
     private void setMinDataStallEvaluateInterval(int time) {
-        when(mDependencies.getDeviceConfigPropertyInt(any(),
-            eq(CONFIG_DATA_STALL_MIN_EVALUATE_INTERVAL), anyInt())).thenReturn(time);
+        doReturn(time).when(mDependencies).getDeviceConfigPropertyInt(any(),
+                eq(CONFIG_DATA_STALL_MIN_EVALUATE_INTERVAL), anyInt());
     }
 
     private void setValidDataStallDnsTimeThreshold(int time) {
-        when(mDependencies.getDeviceConfigPropertyInt(any(),
-            eq(CONFIG_DATA_STALL_VALID_DNS_TIME_THRESHOLD), anyInt())).thenReturn(time);
+        doReturn(time).when(mDependencies).getDeviceConfigPropertyInt(any(),
+                eq(CONFIG_DATA_STALL_VALID_DNS_TIME_THRESHOLD), anyInt());
     }
 
     private void setConsecutiveDnsTimeoutThreshold(int num) {
-        when(mDependencies.getDeviceConfigPropertyInt(any(),
-            eq(CONFIG_DATA_STALL_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD), anyInt())).thenReturn(num);
+        doReturn(num).when(mDependencies).getDeviceConfigPropertyInt(any(),
+                eq(CONFIG_DATA_STALL_CONSECUTIVE_DNS_TIMEOUT_THRESHOLD), anyInt());
     }
 
     private void setTcpPollingInterval(int time) {
@@ -2990,28 +2989,28 @@
     }
 
     private void setFallbackUrl(String url) {
-        when(mDependencies.getSetting(any(),
-                eq(Settings.Global.CAPTIVE_PORTAL_FALLBACK_URL), any())).thenReturn(url);
+        doReturn(url).when(mDependencies).getSetting(any(),
+                eq(Settings.Global.CAPTIVE_PORTAL_FALLBACK_URL), any());
     }
 
     private void setOtherFallbackUrls(String urls) {
-        when(mDependencies.getDeviceConfigProperty(any(),
-                eq(CAPTIVE_PORTAL_OTHER_FALLBACK_URLS), any())).thenReturn(urls);
+        doReturn(urls).when(mDependencies).getDeviceConfigProperty(any(),
+                eq(CAPTIVE_PORTAL_OTHER_FALLBACK_URLS), any());
     }
 
     private void setFallbackSpecs(String specs) {
-        when(mDependencies.getDeviceConfigProperty(any(),
-                eq(CAPTIVE_PORTAL_FALLBACK_PROBE_SPECS), any())).thenReturn(specs);
+        doReturn(specs).when(mDependencies).getDeviceConfigProperty(any(),
+                eq(CAPTIVE_PORTAL_FALLBACK_PROBE_SPECS), any());
     }
 
     private void setCaptivePortalMode(int mode) {
-        when(mDependencies.getSetting(any(),
-                eq(Settings.Global.CAPTIVE_PORTAL_MODE), anyInt())).thenReturn(mode);
+        doReturn(mode).when(mDependencies).getSetting(any(),
+                eq(Settings.Global.CAPTIVE_PORTAL_MODE), anyInt());
     }
 
     private void setDismissPortalInValidatedNetwork(boolean enabled) {
-        when(mDependencies.isFeatureEnabled(any(), any(),
-                eq(DISMISS_PORTAL_IN_VALIDATED_NETWORK), anyBoolean())).thenReturn(enabled);
+        doReturn(enabled).when(mDependencies).isFeatureEnabled(any(), any(),
+                eq(DISMISS_PORTAL_IN_VALIDATED_NETWORK), anyBoolean());
     }
 
     private void setDeviceConfig(String key, String value) {
@@ -3170,8 +3169,8 @@
     }
 
     private void generateTestTcpStats(DataStallDetectionStats.Builder stats) {
-        when(mTst.getLatestPacketFailPercentage()).thenReturn(TEST_TCP_FAIL_RATE);
-        when(mTst.getSentSinceLastRecv()).thenReturn(TEST_TCP_PACKET_COUNT);
+        doReturn(TEST_TCP_FAIL_RATE).when(mTst).getLatestPacketFailPercentage();
+        doReturn(TEST_TCP_PACKET_COUNT).when(mTst).getSentSinceLastRecv();
         stats.setTcpFailRate(TEST_TCP_FAIL_RATE).setTcpSentSinceLastRecv(TEST_TCP_PACKET_COUNT);
     }