Skip to content

Commit

Permalink
Added a new class and two new methods setMobileData and setLocale
Browse files Browse the repository at this point in the history
  • Loading branch information
renas committed Feb 6, 2015
1 parent 360cb8f commit 320e3df
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
24 changes: 24 additions & 0 deletions robotium-solo/src/main/java/com/robotium/solo/Solo.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class Solo {
protected final ScreenshotTaker screenshotTaker;
protected final Instrumentation instrumentation;
protected final Zoomer zoomer;
protected final SystemUtils systemUtils;
protected String webUrl = null;
private final Config config;
public final static int LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; // 0
Expand Down Expand Up @@ -153,6 +154,7 @@ private Solo(Config config, Instrumentation instrumentation, Activity activity)
this.rotator = new Rotator(instrumentation);
this.presser = new Presser(viewFetcher, clicker, instrumentation, sleeper, waiter, dialogUtils);
this.textEnterer = new TextEnterer(instrumentation, clicker, dialogUtils);
this.systemUtils = new SystemUtils(instrumentation);
initialize();
}

Expand Down Expand Up @@ -1672,6 +1674,28 @@ public void rotateSmall(PointF center1, PointF center2)
}
rotator.generateRotateGesture(Rotator.SMALL, center1, center2);
}

/**
* Sets the device locale. Requires android.permission.CHANGE_CONFIGURATION in the AndroidManifest.xml of the application under test.
*
* @param language the language e.g. "en"
* @param country the country e.g. "US"
*/

public void setDeviceLocale(String language, String country){
systemUtils.setDeviceLocale(language, country);
}

/**
* Sets if mobile data should be turned on or off. Requires android.permission.CHANGE_NETWORK_STATE in the AndroidManifest.xml of the application under test.
*
* @param turnedOn true if mobile data is to be turned on and false if not
*/

public void setMobileData(Boolean turnedOn){
systemUtils.setMobileData(turnedOn);
}


/**
* Sets the date in a DatePicker matching the specified index.
Expand Down
70 changes: 70 additions & 0 deletions robotium-solo/src/main/java/com/robotium/solo/SystemUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.robotium.solo;


import java.lang.reflect.Method;
import java.util.Locale;
import android.app.Instrumentation;
import android.content.Context;
import android.net.ConnectivityManager;


/**
* Contains System methods. Examples are: setDeviceLocale(String language, String country),
* setMobileData(Boolean turnedOn).
*
* @author Renas Reda, renas.reda@robotium.com
*
*/

public class SystemUtils {
private Instrumentation instrumentation;

public SystemUtils(Instrumentation instrumentation){
this.instrumentation = instrumentation;
}

/**
* Sets the device locale. Requires android.permission.CHANGE_CONFIGURATION in the AndroidManifest.xml of the application under test.
*
* @param language the language e.g. "en"
* @param country the country e.g. "US"
*/

public void setDeviceLocale(String language, String country){

try {
Locale locale = new Locale(language, country);
Locale.setDefault(locale);

Class<?> activityManagerNative = Class.forName("android.app.ActivityManagerNative");
Object am=activityManagerNative.getMethod("getDefault").invoke(activityManagerNative);
Object config=am.getClass().getMethod("getConfiguration").invoke(am);
config.getClass().getDeclaredField("locale").set(config, locale);
config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);

am.getClass().getMethod("updateConfiguration",android.content.res.Configuration.class).invoke(am,config);

}catch (Exception e) {
e.printStackTrace();
}
}

/**
* Sets if mobile data should be turned on or off. Requires android.permission.CHANGE_NETWORK_STATE in the AndroidManifest.xml of the application under test.
*
* @param turnedOn true if mobile data is to be turned on and false if not
*/

public void setMobileData(Boolean turnedOn){
ConnectivityManager dataManager=(ConnectivityManager)instrumentation.getTargetContext().getSystemService(Context.CONNECTIVITY_SERVICE);

Method dataClass = null;
try {
dataClass = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
dataClass.setAccessible(true);
dataClass.invoke(dataManager, turnedOn);
} catch (Exception e) {
e.printStackTrace();
}
}
}

0 comments on commit 320e3df

Please sign in to comment.