Adds test case: disable mobile network function

Bug: 189722358
Test: atest com.android.settings.network.telephony.MobileDataPreferenceControllerComponentTest
Change-Id: I524d2c26a3c8193e5dc3eb620596baedaae5a635
This commit is contained in:
Syaoran Kuo
2021-06-25 12:17:06 +08:00
parent 0470dba81f
commit 9c957e4c46
2 changed files with 184 additions and 5 deletions

View File

@@ -0,0 +1,148 @@
/*
* 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.network.telephony;
import static com.android.settings.testutils.CommonUtils.set_wifi_enabled;
import static com.google.common.truth.Truth.assertThat;
import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.test.core.app.ActivityScenario;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;
import com.android.settings.testutils.CommonUtils;
import com.android.settings.testutils.UiUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class MobileDataPreferenceControllerComponentTest {
public static final int TIMEOUT = 2000;
private static final int SUBSCRIPTION_ID = 2;
public final String TAG = this.getClass().getName();
private final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation();
private final WifiManager mWifiManager =
(WifiManager) mInstrumentation.getTargetContext().getSystemService(
Context.WIFI_SERVICE);
private final TelephonyManager mTelephonyManager =
(TelephonyManager) mInstrumentation.getTargetContext().getSystemService(
Context.TELEPHONY_SERVICE);
@Rule
public ActivityScenarioRule<com.android.settings.network.telephony.MobileNetworkActivity>
rule = new ActivityScenarioRule<>(
new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
private boolean mOriginDataEnabled;
private boolean mOriginWifiEnabled;
@Before
public void setUp() {
mOriginWifiEnabled = mWifiManager.isWifiEnabled();
Log.d(TAG, "setup! mTelephonyManager = " + mTelephonyManager);
// Disable wifi
set_wifi_enabled(false);
// Enable mobile data
mOriginDataEnabled = mTelephonyManager.isDataEnabled();
if (!mOriginDataEnabled) {
mTelephonyManager.enableDataConnectivity();
}
}
/**
* Tests the mobile network is disabled.
* Precondition:
* Disabled wifi, and enabled mobile network.
* Steps:
* 1. Launch mobile data page.
* 2. Turn off mobile data from switch.
* [Check]
* - Mobile data is turned off via TelephonyManager.
* - Open socket connection https://www.google.net and check the connection failed.
*/
@Test
public void test_disable_mobile_network() {
ActivityScenario scenario = rule.getScenario();
scenario.onActivity(activity -> {
try {
URL url = new URL("https://www.google.net");
MobileDataPreferenceController controller = new MobileDataPreferenceController(
mInstrumentation.getTargetContext(), "mobile_data");
FragmentManager manager = ((FragmentActivity) activity).getSupportFragmentManager();
controller.init(manager, SUBSCRIPTION_ID);
Log.d(TAG, "Start to click ");
controller.setChecked(false);
Log.d(TAG, "Set Checked, wait for fully close.");
// Assert the configuration is set.
assertThat(UiUtils.waitUntilCondition(10000,
() -> !mTelephonyManager.isDataEnabled())).isTrue();
// Assert the network is not connectable.
assertThat(UiUtils.waitUntilCondition(3000,
() -> {
try {
return CommonUtils.connectToURL(url);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return false;
})).isFalse();
} catch (IOException e) {
}
});
}
@After
public void tearDown() {
// Restore wifi status wifi
set_wifi_enabled(mOriginWifiEnabled);
// Restore mobile data status
if (mOriginDataEnabled != mTelephonyManager.isDataEnabled()) {
if (mOriginDataEnabled) {
mTelephonyManager.enableDataConnectivity();
} else {
mTelephonyManager.disableDataConnectivity();
}
}
}
}

View File

@@ -20,8 +20,10 @@ import android.app.Activity;
import android.app.Instrumentation;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.os.PowerManager;
import android.os.StrictMode;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
@@ -35,14 +37,18 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import javax.net.ssl.HttpsURLConnection;
public class CommonUtils {
private static final String TAG = CommonUtils.class.getSimpleName();
private static Instrumentation sInstrumentation =
private static final Instrumentation sInstrumentation =
InstrumentationRegistry.getInstrumentation();
private static PowerManager sPowerManager =
private static final WifiManager sWifiManager =
(WifiManager) sInstrumentation.getTargetContext().getSystemService(
Context.WIFI_SERVICE);
private static final PowerManager sPowerManager =
(PowerManager) sInstrumentation.getTargetContext().getSystemService(
Context.POWER_SERVICE);
@@ -74,7 +80,9 @@ public class CommonUtils {
}
}
public static boolean connectToURL(URL url) {
public static boolean connectToURL(URL url) throws ExecutionException, InterruptedException {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
HttpURLConnection connection = null;
try {
connection = (HttpsURLConnection) url.openConnection();
@@ -90,17 +98,19 @@ public class CommonUtils {
while (null != (line = reader.readLine())) {
response.append(line);
}
Log.d(TAG, "Connection success! " + response.toString());
return true;
}
} catch (Exception e) {
Log.d(TAG, e.getMessage());
Log.e(TAG, e.toString());
e.printStackTrace();
return false;
} finally {
if (null != connection) {
connection.disconnect();
}
}
Log.d(TAG, "End, return false.");
return false;
}
@@ -132,4 +142,25 @@ public class CommonUtils {
// After power on screen, need to unlock and goto home page.
AdbUtils.shell("input keyevent KEYCODE_MENU");
}
/**
* Sets wifi status to given enable / disable via ADB command.
*/
public static void set_wifi_enabled(boolean enable) {
final int timeoutMsec = 10000;
Log.d(TAG, "Set wifi status to " + enable);
if (sWifiManager.isWifiEnabled() != enable) {
AdbUtils.shell("svc wifi " + (enable ? "enable" : "disable"));
if (!UiUtils.waitUntilCondition(timeoutMsec,
() -> sWifiManager.isWifiEnabled() == enable)) {
Log.e(TAG, "Cannot set wifi to " + (enable ? "enabl" : "disable") + ", timeout "
+ timeoutMsec + " (ms).");
Log.e(TAG, "See logcat for more information.");
}
Log.d(TAG, "After configuration wifi status = " + sWifiManager.isWifiEnabled());
} else {
Log.d(TAG, "Wifi is enable is already " + enable + ", no need to change.");
}
}
}