views:

1162

answers:

2

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.

+1  A: 

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

Igor
Thank you for your answer - This is a step in the right direction. The emulator launch parameters can adjust network connectivity at boot time, but we're looking for a way to do that programmatically, at runtime. The library "under test" must respond to Android Broadcast Intents (such as android.net.ConnectivityManager.CONNECTIVITY_ACTION) in order to adjust its state to the network state of the device. What we need is a way to change the network state from within the JUnit test, so that we can verify that the Intents are received and acted on appropriately...
medicdave
A: 

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.

alt text

Chris
I have tried this option, though through the console ADB connection, but it doesn't seem to actually remove connectivity; despite my switching off the GSM data connection, network traffic still works. What I'm looking for here is a way to do it *programmatically* - within the scope of an automated test, such as an JUnit test.
medicdave
I've had the same problem (communication seems to continue). It sounds like you've found a way to script this via ADB shell (should it start working again or we stumble across the proper control commands). FYI I've also been able to toggle some network settings (same problem exists though) via telnet to the emu and the gsm data off and gsm voice off commands ala http://stackoverflow.com/questions/2577785/ringing-myself-using-android-emulator/2578245#2578245
Chris