From 00dedc1f763093eb22bec0ff15c9fbdb7ca88d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Wed, 29 Jan 2014 17:09:12 +0100 Subject: 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. --- AndroidManifest.xml | 3 +- .../leap/bitmaskclient/test/ConnectionManager.java | 36 +++++++++++++++++++ .../test/testConfigurationWizard.java | 10 +++--- .../se/leap/bitmaskclient/test/testDashboard.java | 40 ++++------------------ 4 files changed, 49 insertions(+), 40 deletions(-) create mode 100644 tests/src/se/leap/bitmaskclient/test/ConnectionManager.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 9c154c04..505f6ee1 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -22,7 +22,8 @@ - + + 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 { @@ -24,6 +23,7 @@ public class testConfigurationWizard extends ActivityInstrumentationTestCase2 { @@ -30,7 +26,7 @@ public class testDashboard extends ActivityInstrumentationTestCase2 { 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 { 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 { 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) { - } - } } -- cgit v1.2.3