-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Toggle connectivity as Airplane Mode via UIAutomator (#291)
Simply turning off the WiFi doensn't "cut it." Likewise, disabling cellular connectivity is not possible without reflection. A durable approach is to toggle airplane mode via the system settings panel. UIAutomator can be used to achieve this. Refer: https://stackoverflow.com/q/27620976/695787
- Loading branch information
1 parent
0764253
commit 8cf6844
Showing
9 changed files
with
125 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...-tests/src/androidTest/java/com/amazonaws/mobileconnectors/appsync/util/AirplaneMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, | ||
* Inc. or its affiliates. All Rights Reserved. | ||
* <p> | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package com.amazonaws.mobileconnectors.appsync.util; | ||
|
||
import android.content.ContentResolver; | ||
import android.content.Intent; | ||
import android.provider.Settings; | ||
|
||
import android.support.test.uiautomator.By; | ||
import android.support.test.uiautomator.BySelector; | ||
import android.support.test.uiautomator.UiDevice; | ||
import android.support.test.uiautomator.Until; | ||
|
||
import static android.support.test.InstrumentationRegistry.getInstrumentation; | ||
|
||
public final class AirplaneMode { | ||
private static final int TIMEOUT_MS = 500; | ||
|
||
private AirplaneMode() {} | ||
|
||
public static void enable() { | ||
setAirplaneMode(true); | ||
} | ||
|
||
public static void disable() { | ||
setAirplaneMode(false); | ||
} | ||
|
||
public static boolean isEnabled() { | ||
ContentResolver contentResolver = getInstrumentation().getContext().getContentResolver(); | ||
int status = Settings.System.getInt(contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0); | ||
return status == 1; | ||
} | ||
|
||
private static void setAirplaneMode(boolean shouldEnable) { | ||
if (shouldEnable == isEnabled()) return; | ||
|
||
UiDevice device = UiDevice.getInstance(getInstrumentation()); | ||
device.openQuickSettings(); | ||
|
||
BySelector description = By.desc("Airplane mode"); | ||
device.wait(Until.hasObject(description), TIMEOUT_MS); | ||
device.findObject(description).click(); | ||
|
||
getInstrumentation().getContext() | ||
.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...rc/androidTest/java/com/amazonaws/mobileconnectors/appsync/util/InternetConnectivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, | ||
* Inc. or its affiliates. All Rights Reserved. | ||
* <p> | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package com.amazonaws.mobileconnectors.appsync.util; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.Socket; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public final class InternetConnectivity { | ||
private InternetConnectivity() {} | ||
|
||
public static void goOnline() { | ||
RetryStrategies.linear(AirplaneMode::disable, InternetConnectivity::isOnline, 3, 2); | ||
} | ||
|
||
public static void goOffline() { | ||
RetryStrategies.linear(AirplaneMode::enable, InternetConnectivity::isOffline, 3, 2); | ||
} | ||
|
||
private static boolean isOnline() { | ||
int connectionTimeoutMs = (int) TimeUnit.SECONDS.toMillis(2); | ||
String host = "amazon.com"; | ||
int port = 443; | ||
|
||
Socket socket = new Socket(); | ||
try { | ||
socket.connect(new InetSocketAddress(host, port), connectionTimeoutMs); | ||
socket.close(); | ||
return true; | ||
} catch (IOException socketFailure) { | ||
return false; | ||
} | ||
} | ||
|
||
private static boolean isOffline() { | ||
return !isOnline(); | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
...-appsync-tests/src/androidTest/java/com/amazonaws/mobileconnectors/appsync/util/Wifi.java
This file was deleted.
Oops, something went wrong.