Fix mobile network test cause process crashed.
Bug: 192428680 Test: atest com.android.settings.network.telephony.MobileDataPreferenceControllerComponentTest Change-Id: If51950381071b6cbafb98d3282c201f5872ebbe0
This commit is contained in:
@@ -24,6 +24,8 @@ import android.app.Instrumentation;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
|
import android.telecom.TelecomManager;
|
||||||
|
import android.telephony.SubscriptionManager;
|
||||||
import android.telephony.TelephonyManager;
|
import android.telephony.TelephonyManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -39,6 +41,7 @@ import com.android.settings.testutils.CommonUtils;
|
|||||||
import com.android.settings.testutils.UiUtils;
|
import com.android.settings.testutils.UiUtils;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
|
import org.junit.Assume;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -46,13 +49,12 @@ import org.junit.runner.RunWith;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(AndroidJUnit4.class)
|
||||||
@SmallTest
|
@SmallTest
|
||||||
public class MobileDataPreferenceControllerComponentTest {
|
public class MobileDataPreferenceControllerComponentTest {
|
||||||
public static final int TIMEOUT = 2000;
|
public static final int TIMEOUT = 2000;
|
||||||
private static final int SUBSCRIPTION_ID = 2;
|
private static int sSubscriptionId = 2;
|
||||||
public final String TAG = this.getClass().getName();
|
public final String TAG = this.getClass().getName();
|
||||||
private final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation();
|
private final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation();
|
||||||
private final WifiManager mWifiManager =
|
private final WifiManager mWifiManager =
|
||||||
@@ -61,6 +63,9 @@ public class MobileDataPreferenceControllerComponentTest {
|
|||||||
private final TelephonyManager mTelephonyManager =
|
private final TelephonyManager mTelephonyManager =
|
||||||
(TelephonyManager) mInstrumentation.getTargetContext().getSystemService(
|
(TelephonyManager) mInstrumentation.getTargetContext().getSystemService(
|
||||||
Context.TELEPHONY_SERVICE);
|
Context.TELEPHONY_SERVICE);
|
||||||
|
private final TelecomManager mTelecomManager =
|
||||||
|
(TelecomManager) mInstrumentation.getTargetContext().getSystemService(
|
||||||
|
Context.TELECOM_SERVICE);
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public ActivityScenarioRule<com.android.settings.network.telephony.MobileNetworkActivity>
|
public ActivityScenarioRule<com.android.settings.network.telephony.MobileNetworkActivity>
|
||||||
@@ -73,7 +78,6 @@ public class MobileDataPreferenceControllerComponentTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
mOriginWifiEnabled = mWifiManager.isWifiEnabled();
|
mOriginWifiEnabled = mWifiManager.isWifiEnabled();
|
||||||
Log.d(TAG, "setup! mTelephonyManager = " + mTelephonyManager);
|
|
||||||
// Disable wifi
|
// Disable wifi
|
||||||
set_wifi_enabled(false);
|
set_wifi_enabled(false);
|
||||||
|
|
||||||
@@ -82,6 +86,15 @@ public class MobileDataPreferenceControllerComponentTest {
|
|||||||
if (!mOriginDataEnabled) {
|
if (!mOriginDataEnabled) {
|
||||||
mTelephonyManager.enableDataConnectivity();
|
mTelephonyManager.enableDataConnectivity();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Current sim card is not available for data network.
|
||||||
|
sSubscriptionId = SubscriptionManager.getDefaultDataSubscriptionId();
|
||||||
|
Assume.assumeTrue("Device cannot mobile network! Should ignore test.",
|
||||||
|
sSubscriptionId != SubscriptionManager.INVALID_SUBSCRIPTION_ID);
|
||||||
|
|
||||||
|
int simState = mTelephonyManager.getSimState();
|
||||||
|
Assume.assumeTrue("Sim card is not ready. Expect: " + TelephonyManager.SIM_STATE_READY
|
||||||
|
+ ", Actual: " + simState, simState == TelephonyManager.SIM_STATE_READY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -104,7 +117,12 @@ public class MobileDataPreferenceControllerComponentTest {
|
|||||||
MobileDataPreferenceController controller = new MobileDataPreferenceController(
|
MobileDataPreferenceController controller = new MobileDataPreferenceController(
|
||||||
mInstrumentation.getTargetContext(), "mobile_data");
|
mInstrumentation.getTargetContext(), "mobile_data");
|
||||||
FragmentManager manager = ((FragmentActivity) activity).getSupportFragmentManager();
|
FragmentManager manager = ((FragmentActivity) activity).getSupportFragmentManager();
|
||||||
controller.init(manager, SUBSCRIPTION_ID);
|
controller.init(manager, sSubscriptionId);
|
||||||
|
|
||||||
|
// Make sure mobile network can connect at first.
|
||||||
|
assertThat(UiUtils.waitUntilCondition(1000,
|
||||||
|
() -> CommonUtils.connectToURL(url))).isTrue();
|
||||||
|
|
||||||
Log.d(TAG, "Start to click ");
|
Log.d(TAG, "Start to click ");
|
||||||
controller.setChecked(false);
|
controller.setChecked(false);
|
||||||
Log.d(TAG, "Set Checked, wait for fully close.");
|
Log.d(TAG, "Set Checked, wait for fully close.");
|
||||||
@@ -114,17 +132,9 @@ public class MobileDataPreferenceControllerComponentTest {
|
|||||||
() -> !mTelephonyManager.isDataEnabled())).isTrue();
|
() -> !mTelephonyManager.isDataEnabled())).isTrue();
|
||||||
|
|
||||||
// Assert the network is not connectable.
|
// Assert the network is not connectable.
|
||||||
assertThat(UiUtils.waitUntilCondition(3000,
|
assertThat(UiUtils.waitUntilCondition(1000,
|
||||||
() -> {
|
() -> CommonUtils.connectToURL(url))).isFalse();
|
||||||
try {
|
|
||||||
return CommonUtils.connectToURL(url);
|
|
||||||
} catch (ExecutionException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
})).isFalse();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -37,7 +37,6 @@ import java.io.InputStream;
|
|||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
|
|
||||||
import javax.net.ssl.HttpsURLConnection;
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
|
||||||
@@ -80,7 +79,7 @@ public class CommonUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean connectToURL(URL url) throws ExecutionException, InterruptedException {
|
public static boolean connectToURL(URL url) {
|
||||||
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
|
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
|
||||||
StrictMode.setThreadPolicy(policy);
|
StrictMode.setThreadPolicy(policy);
|
||||||
HttpURLConnection connection = null;
|
HttpURLConnection connection = null;
|
||||||
|
Reference in New Issue
Block a user