diff options
author | Parménides GV <parmegv@sdf.org> | 2014-01-29 17:09:12 +0100 |
---|---|---|
committer | Parménides GV <parmegv@sdf.org> | 2014-01-29 17:09:12 +0100 |
commit | 00dedc1f763093eb22bec0ff15c9fbdb7ca88d65 (patch) | |
tree | 4bc693874873dbc9dddc68f8e4b87dc73edf01e2 /tests/src/se/leap/bitmaskclient | |
parent | 8d1b47a498df5d0f4e4e619f8373aaa1ad4dcd90 (diff) |
New method to set on/off data connection.
Used in tests, but it's going to be used in the near future when
blocking data connections until EIP is on.
Diffstat (limited to 'tests/src/se/leap/bitmaskclient')
3 files changed, 47 insertions, 39 deletions
diff --git a/tests/src/se/leap/bitmaskclient/test/ConnectionManager.java b/tests/src/se/leap/bitmaskclient/test/ConnectionManager.java new file mode 100644 index 00000000..8722bb13 --- /dev/null +++ b/tests/src/se/leap/bitmaskclient/test/ConnectionManager.java @@ -0,0 +1,36 @@ +package se.leap.bitmaskclient.test; + +import android.content.Context; +import android.net.ConnectivityManager; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import android.util.Log; + +public class ConnectionManager { + static void setMobileDataEnabled(boolean enabled, Context context) { + final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + try { + final Class conmanClass = Class.forName(conman.getClass().getName()); + final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService"); + iConnectivityManagerField.setAccessible(true); + final Object iConnectivityManager = iConnectivityManagerField.get(conman); + final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName()); + final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); + setMobileDataEnabledMethod.setAccessible(true); + + setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled); + Log.d("testDashboard", "setMobileDataEnabled(" + enabled + ");"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } +} diff --git a/tests/src/se/leap/bitmaskclient/test/testConfigurationWizard.java b/tests/src/se/leap/bitmaskclient/test/testConfigurationWizard.java index 55fd87ae..847d8cdd 100644 --- a/tests/src/se/leap/bitmaskclient/test/testConfigurationWizard.java +++ b/tests/src/se/leap/bitmaskclient/test/testConfigurationWizard.java @@ -1,15 +1,14 @@ package se.leap.bitmaskclient.test; +import android.test.ActivityInstrumentationTestCase2; +import android.widget.ListView; +import com.jayway.android.robotium.solo.Solo; import java.io.IOException; - import se.leap.bitmaskclient.AboutActivity; import se.leap.bitmaskclient.ConfigurationWizard; import se.leap.bitmaskclient.ProviderDetailFragment; import se.leap.bitmaskclient.R; -import android.test.ActivityInstrumentationTestCase2; -import android.widget.ListView; - -import com.jayway.android.robotium.solo.Solo; +import se.leap.bitmaskclient.test.ConnectionManager; public class testConfigurationWizard extends ActivityInstrumentationTestCase2<ConfigurationWizard> { @@ -24,6 +23,7 @@ public class testConfigurationWizard extends ActivityInstrumentationTestCase2<Co protected void setUp() throws Exception { super.setUp(); solo = new Solo(getInstrumentation(), getActivity()); + ConnectionManager.setMobileDataEnabled(true, solo.getCurrentActivity().getApplicationContext()); } @Override diff --git a/tests/src/se/leap/bitmaskclient/test/testDashboard.java b/tests/src/se/leap/bitmaskclient/test/testDashboard.java index a6b25c0a..cdffcfab 100644 --- a/tests/src/se/leap/bitmaskclient/test/testDashboard.java +++ b/tests/src/se/leap/bitmaskclient/test/testDashboard.java @@ -1,21 +1,17 @@ package se.leap.bitmaskclient.test; - import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.net.ConnectivityManager; import android.provider.Settings; import android.test.ActivityInstrumentationTestCase2; import android.util.Log; import com.jayway.android.robotium.solo.Solo; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import se.leap.bitmaskclient.ConfigurationWizard; import se.leap.bitmaskclient.Dashboard; import se.leap.bitmaskclient.R; +import se.leap.bitmaskclient.test.ConnectionManager; import se.leap.openvpn.MainActivity; public class testDashboard extends ActivityInstrumentationTestCase2<Dashboard> { @@ -30,7 +26,7 @@ public class testDashboard extends ActivityInstrumentationTestCase2<Dashboard> { protected void setUp() throws Exception { super.setUp(); solo = new Solo(getInstrumentation(), getActivity()); - setAirplaneMode(false); + ConnectionManager.setMobileDataEnabled(true, solo.getCurrentActivity().getApplicationContext()); } @Override @@ -53,21 +49,18 @@ public class testDashboard extends ActivityInstrumentationTestCase2<Dashboard> { solo.clickOnView(solo.getView(R.id.eipSwitch)); if(!solo.waitForText("Not running! Connection not secure")) - fail(); - /* setAirplaneMode isn't working right now. - setAirplaneMode(true); - if(!solo.waitForLogMessage("Service state changed")) - fail(); + fail(); + + ConnectionManager.setMobileDataEnabled(false, solo.getCurrentActivity().getApplicationContext()); solo.clickOnView(solo.getView(R.id.eipSwitch)); if(!solo.waitForText("Initiating connection")) fail(); if(!solo.waitForText("Waiting for usable network")) fail(); - */ } - public void testLogInAndOut() { + public void testLogInAndOut() { long miliseconds_to_log_in = 40 * 1000; solo.clickOnActionBarItem(R.id.login_button); solo.enterText(0, "parmegvtest1"); @@ -112,25 +105,4 @@ public class testDashboard extends ActivityInstrumentationTestCase2<Dashboard> { solo.clickOnMenuItem("ICS OpenVPN Interface"); solo.waitForActivity(MainActivity.class); } - - private void setAirplaneMode(boolean airplane_mode) { - Context context = solo.getCurrentActivity().getApplicationContext(); - final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - try { - final Class conmanClass = Class.forName(conman.getClass().getName()); - final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService"); - iConnectivityManagerField.setAccessible(true); - final Object iConnectivityManager = iConnectivityManagerField.get(conman); - final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName()); - final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); - setMobileDataEnabledMethod.setAccessible(true); - - setMobileDataEnabledMethod.invoke(iConnectivityManager, !airplane_mode); - } catch (ClassNotFoundException e) { - } catch (NoSuchMethodException e) { - } catch (IllegalAccessException e) { - } catch (NoSuchFieldException e) { - } catch (InvocationTargetException e) { - } - } } |