Merge "Test for issue #28156248: Receiving connectivity receiver..." into nyc-dev
am: 9a36b2a * commit '9a36b2aab7fba6948527b59596b148fe6f0221a0': Test for issue #28156248: Receiving connectivity receiver... Change-Id: I4d0b54ee9e2f74f5a2ded4edb376e1ea157acc5e
This commit is contained in:
@@ -33,6 +33,12 @@
|
||||
<application>
|
||||
<uses-library android:name="android.test.runner" />
|
||||
<uses-library android:name="org.apache.http.legacy" android:required="false" />
|
||||
|
||||
<receiver android:name=".ConnectivityReceiver">
|
||||
<intent-filter>
|
||||
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
</application>
|
||||
|
||||
<instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
|
||||
|
||||
@@ -361,6 +361,36 @@ public class ConnectivityManagerTest extends AndroidTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests reporting of connectivity changed.
|
||||
*/
|
||||
public void testConnectivityChanged() {
|
||||
// We are going to ensure that we *don't* see the connectivity in the manifest.
|
||||
ConnectivityReceiver.prepare();
|
||||
|
||||
// We will toggle the state of wifi to generate a connectivity change.
|
||||
final boolean previousWifiEnabledState = mWifiManager.isWifiEnabled();
|
||||
if (previousWifiEnabledState) {
|
||||
disconnectFromWifi();
|
||||
} else {
|
||||
connectToWifi();
|
||||
}
|
||||
|
||||
// The connectivity broadcast has been sent; push through a terminal broadcast
|
||||
// to wait for in the receive to confirm it didn't see the connectivity change.
|
||||
Intent finalIntent = new Intent(ConnectivityReceiver.FINAL_ACTION);
|
||||
finalIntent.setClass(mContext, ConnectivityReceiver.class);
|
||||
mContext.sendBroadcast(finalIntent);
|
||||
assertFalse(ConnectivityReceiver.waitForBroadcast());
|
||||
|
||||
// Now restore previous state.
|
||||
if (previousWifiEnabledState) {
|
||||
connectToWifi();
|
||||
} else {
|
||||
disconnectFromWifi();
|
||||
}
|
||||
}
|
||||
|
||||
/** Enable WiFi and wait for it to become connected to a network. */
|
||||
private void connectToWifi() {
|
||||
ConnectivityActionReceiver receiver = new ConnectivityActionReceiver(
|
||||
|
||||
69
tests/cts/net/src/android/net/cts/ConnectivityReceiver.java
Normal file
69
tests/cts/net/src/android/net/cts/ConnectivityReceiver.java
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (C) 2016 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 android.net.cts;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ConnectivityReceiver extends BroadcastReceiver {
|
||||
static boolean sReceivedConnectivity;
|
||||
static boolean sReceivedFinal;
|
||||
static CountDownLatch sLatch;
|
||||
|
||||
static void prepare() {
|
||||
synchronized (ConnectivityReceiver.class) {
|
||||
sReceivedConnectivity = sReceivedFinal = false;
|
||||
sLatch = new CountDownLatch(1);
|
||||
}
|
||||
}
|
||||
|
||||
static boolean waitForBroadcast() {
|
||||
try {
|
||||
sLatch.await(30, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
synchronized (ConnectivityReceiver.class) {
|
||||
sLatch = null;
|
||||
if (!sReceivedFinal) {
|
||||
throw new IllegalStateException("Never received final broadcast");
|
||||
}
|
||||
return sReceivedConnectivity;
|
||||
}
|
||||
}
|
||||
|
||||
static final String FINAL_ACTION = "android.net.cts.action.FINAL";
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Log.i("ConnectivityReceiver", "Received: " + intent.getAction());
|
||||
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
|
||||
sReceivedConnectivity = true;
|
||||
} else if (FINAL_ACTION.equals(intent.getAction())) {
|
||||
sReceivedFinal = true;
|
||||
if (sLatch != null) {
|
||||
sLatch.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user