diff options
-rw-r--r-- | AndroidManifest.xml | 10 | ||||
-rw-r--r-- | tests/src/se/leap/bitmaskclient/test/ConnectionManager.java | 33 | ||||
-rw-r--r-- | tests/src/se/leap/bitmaskclient/test/testConfigurationWizard.java | 10 | ||||
-rw-r--r-- | tests/src/se/leap/bitmaskclient/test/testDashboard.java | 50 |
4 files changed, 55 insertions, 48 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 815ddf87..6738a90c 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -22,9 +22,13 @@ <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> - <!-- if you want to run the unit test for Dashboard you will need that you uncomment the following line --> - <!-- <uses-permission android:name="android.permission.WRITE_SETTINGS" /> --> - + + <!-- if you want to run test, this permissions are needed. Gradle will get rid of them once we implement it. --> + <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> + <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> + <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/> + <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> + <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <!-- <uses-permission android:name="com.android.vending.BILLING" /> --> 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..f1cbff19 --- /dev/null +++ b/tests/src/se/leap/bitmaskclient/test/ConnectionManager.java @@ -0,0 +1,33 @@ +package se.leap.bitmaskclient.test; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.wifi.WifiManager; +import android.util.Log; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class ConnectionManager { + static void setMobileDataEnabled(boolean enabled, Context context) { + final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + Method[] methods = conman.getClass().getMethods(); + for (Method method : methods) { + if (method.getName().equals("setMobileDataEnabled")) { + method.setAccessible(true); + try { + method.invoke(conman, enabled); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + } + + static void setWifiEnabled(boolean enabled, Context context) { + WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + wifiManager.setWifiEnabled(enabled); + } +} 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 b6c23084..a1d1ffac 100644 --- a/tests/src/se/leap/bitmaskclient/test/testDashboard.java +++ b/tests/src/se/leap/bitmaskclient/test/testDashboard.java @@ -1,8 +1,5 @@ package se.leap.bitmaskclient.test; -import se.leap.bitmaskclient.ConfigurationWizard; -import se.leap.bitmaskclient.Dashboard; -import se.leap.bitmaskclient.R; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -10,8 +7,12 @@ import android.content.IntentFilter; import android.provider.Settings; import android.test.ActivityInstrumentationTestCase2; import android.util.Log; - import com.jayway.android.robotium.solo.Solo; +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> { @@ -25,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 @@ -48,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"); @@ -96,34 +94,6 @@ public class testDashboard extends ActivityInstrumentationTestCase2<Dashboard> { solo.clickOnMenuItem("Switch provider"); solo.waitForActivity(ConfigurationWizard.class); solo.goBack(); - - solo.clickOnMenuItem("Switch provider"); - solo.waitForActivity(ConfigurationWizard.class); - solo.goBack(); } - - private void setAirplaneMode(boolean airplane_mode) { - Context context = solo.getCurrentActivity().getApplicationContext(); - boolean isEnabled = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1; - Log.d("AirplaneMode", "Service state: " + isEnabled); - Settings.System.putInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, airplane_mode ? 1 : 0); - - // Post an intent to reload - Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); - intent.putExtra("state", airplane_mode); - Log.d("AirplaneMode", "New Service state: " + !isEnabled); - solo.getCurrentActivity().sendBroadcast(intent); - - IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE"); - - BroadcastReceiver receiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - boolean isEnabled = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1; - Log.d("AirplaneMode", "Service state changed: " + isEnabled); - } - }; - - context.registerReceiver(receiver, intentFilter); } } |