Merge "Use ConnectionManager API to check if network is metered." into nyc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
b853005f3c
@@ -403,7 +403,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
|||||||
} else {
|
} else {
|
||||||
final NetworkInfo info = mCm.getActiveNetworkInfo();
|
final NetworkInfo info = mCm.getActiveNetworkInfo();
|
||||||
assertNotNull("Could not get active network", info);
|
assertNotNull("Could not get active network", info);
|
||||||
if (!info.isMetered()) {
|
if (!mCm.isActiveNetworkMetered()) {
|
||||||
Log.d(TAG, "Active network is not metered: " + info);
|
Log.d(TAG, "Active network is not metered: " + info);
|
||||||
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
|
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
|
||||||
Log.i(TAG, "Setting active WI-FI network as metered: " + info );
|
Log.i(TAG, "Setting active WI-FI network as metered: " + info );
|
||||||
@@ -415,9 +415,21 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
|||||||
assertActiveNetworkMetered(false); // Sanity check.
|
assertActiveNetworkMetered(false); // Sanity check.
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertActiveNetworkMetered(boolean expected) {
|
private void assertActiveNetworkMetered(boolean expected) throws Exception {
|
||||||
final NetworkInfo info = mCm.getActiveNetworkInfo();
|
final int maxTries = 5;
|
||||||
assertEquals("Wrong metered status for active network " + info, expected, info.isMetered());
|
NetworkInfo info = null;
|
||||||
|
for (int i = 1; i <= maxTries; i++) {
|
||||||
|
info = mCm.getActiveNetworkInfo();
|
||||||
|
if (info != null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Log.v(TAG, "No active network info on attempt #" + i
|
||||||
|
+ "; sleeping 1s before polling again");
|
||||||
|
Thread.sleep(SECOND_IN_MS);
|
||||||
|
}
|
||||||
|
assertNotNull("No active network after " + maxTries + " attempts", info);
|
||||||
|
assertEquals("Wrong metered status for active network " + info, expected,
|
||||||
|
mCm.isActiveNetworkMetered());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String setWifiMeteredStatus(boolean metered) throws Exception {
|
private String setWifiMeteredStatus(boolean metered) throws Exception {
|
||||||
|
|||||||
Reference in New Issue
Block a user