I'm working on a Push Notifications library for Android (http://deaconproject.org/) that needs to take action if network connectivity is interrupted or changed - namely, it needs to re-initiate a server connection or pause its operation until network connectivity is available. This seems to work fine using and Android BroadcastReceiver for "android.net.ConnectivityManager.CONNECTIVITY_ACTION".
My problem is in testing the library - I would like to automatically test the library's response to a broken network connection, or a transition from 3G to WiFi, under various configuration conditions. The problem is, I don't want to sit with the emulator and hit F8 all day.
Is there a way to programmatically manipulate network connections on Android from within a jUnit test without resorting to toggling Airplane Mode? I've already tried issuing commands to the emulator via the console, manipulating the GSM mode, etc, but while the phone state changes on the display, the Internet connection remains up.
Right click on project in the "Project Explorer" panel, chose "Run As" then "Run Configurations".
On the left side of the modally shown window chose "Android Application" and on the right side of the same window, chose tab "Target".
At the bottom of the window, in the "Emulator Launch Parameters" section you have plethora of options regarding internet connectivity.
I hope this helped.
Igor
A simple, but a bit long and heavy, solution is too add an abstraction layer over your wifi connection state monitor.
Add an Entity (NetworkStateManager), it should an application scoped singleton (see RoboGuice or Dagger to this cleanly, or access the unique instance via your application instance).
Add methods to register, unregister listeners to this entity, using Observable Observer design pattern.
Each activity will have to register to it during onStart, unregister in onStop.
The entity itself will be modified by a BroadCastReceiver.
That is fully testable, if you use a DI framework, you will be able to inject a drivable stub into your activities and see how they react to network state changes.
I've found the Android Dev Tools app really helpful.
It allows you to manipulate important system settings like network connectivity. It even allows you to set an interval in which it toggles wifi on and off automatically thus allowing you to test you network related broadcast receivers.
You might want to look at the emulator control view in eclipse too. I'm not having a lot of luck getting it to kill my data at the moment, but it looks like it should.
Unfortunately, there is no way to truly disable network access from within a unit test programmatically. I filed an Android enhancement request for this issue.