Skip to content

Commit

Permalink
Extracted sleep methods to Sleeper class, so they can be non-static (…
Browse files Browse the repository at this point in the history
…and support custom configuration).
  • Loading branch information
hugojosefson committed Sep 16, 2010
1 parent 8e3ec78 commit 8fac33c
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 116 deletions.
Expand Up @@ -22,21 +22,23 @@ class ActivityUtils {
private final Instrumentation inst;
private ActivityMonitor activityMonitor;
private Activity activity;
private final Sleeper sleeper;
private ArrayList<Activity> activityList = new ArrayList<Activity>();

/**
* Constructor that takes in the instrumentation and the start activity.
*
* @param inst the {@link Instrumentation} instance.
* @param activity {@link Activity} the start activity
*
* @param inst the {@code Instrumentation} instance.
* @param activity the start {@code Activity}
* @param sleeper the {@code Sleeper} instance
*
*/

public ActivityUtils(Instrumentation inst, Activity activity) {
public ActivityUtils(Instrumentation inst, Activity activity, Sleeper sleeper) {
this.inst = inst;
this.activity = activity;
setupActivityMonitor();
this.sleeper = sleeper;
setupActivityMonitor();
}

/**
Expand Down Expand Up @@ -79,7 +81,7 @@ private void setupActivityMonitor() {
public void setActivityOrientation(int orientation)
{
if(activity.equals(getCurrentActivity()))
RobotiumUtils.sleep();
sleeper.sleep();
Activity activity = getCurrentActivity();
activity.setRequestedOrientation(orientation);
}
Expand All @@ -104,7 +106,7 @@ public Activity getCurrentActivity() {

public Activity getCurrentActivity(boolean shouldSleepFirst) {
if(shouldSleepFirst){
RobotiumUtils.sleep();
sleeper.sleep();
inst.waitForIdleSync();
}
Boolean found = false;
Expand Down
Expand Up @@ -13,17 +13,20 @@

class Asserter {
private final ActivityUtils activityUtils;
private final Sleeper sleeper;

/**
* Constructs this object.
*
* @param activityUtils the {@link ActivityUtils} instance.
* @param activityUtils the {@code ActivityUtils} instance.
* @param sleeper the {@code Sleeper} instance.
*
*/

public Asserter(ActivityUtils activityUtils) {
public Asserter(ActivityUtils activityUtils, Sleeper sleeper) {
this.activityUtils = activityUtils;
}
this.sleeper = sleeper;
}

/**
* Asserts that an expected {@link Activity} is currently active one.
Expand All @@ -35,7 +38,7 @@ public Asserter(ActivityUtils activityUtils) {

public void assertCurrentActivity(String message, String name)
{
RobotiumUtils.sleep();
sleeper.sleep();
Assert.assertEquals(message, name, activityUtils.getCurrentActivity()
.getClass().getSimpleName());

Expand All @@ -51,7 +54,7 @@ public void assertCurrentActivity(String message, String name)

public void assertCurrentActivity(String message, Class<? extends Activity> expectedClass)
{
RobotiumUtils.sleep();
sleeper.sleep();
Assert.assertEquals(message, expectedClass.getName(), activityUtils
.getCurrentActivity().getClass().getName());

Expand Down
Expand Up @@ -31,6 +31,7 @@ class Clicker {
private final Scroller scroller;
private final Instrumentation inst;
private final RobotiumUtils robotiumUtils;
private final Sleeper sleeper;
private int countMatches=0;
private final int TIMEOUT = 10000;
private final int CLICKTIMEOUT = 5000;
Expand All @@ -39,22 +40,24 @@ class Clicker {
/**
* Constructs this object.
*
* @param ativityUtils the {@link ActivityUtils} instance.
* @param viewFetcher the {@link ViewFetcher} instance.
* @param scroller the {@link Scroller} instance.
* @param robotiumUtils the {@link RobotiumUtils} instance.
* @param inst the {@link Instrumentation} instance.
*/
* @param ativityUtils the {@link com.jayway.android.robotium.solo.ActivityUtils} instance.
* @param viewFetcher the {@link com.jayway.android.robotium.solo.ViewFetcher} instance.
* @param scroller the {@link com.jayway.android.robotium.solo.Scroller} instance.
* @param robotiumUtils the {@link com.jayway.android.robotium.solo.RobotiumUtils} instance.
* @param inst the {@link android.app.Instrumentation} instance.
* @param sleeper
*/

public Clicker(ActivityUtils ativityUtils, ViewFetcher viewFetcher,
Scroller scroller, RobotiumUtils robotiumUtils, Instrumentation inst) {
Scroller scroller, RobotiumUtils robotiumUtils, Instrumentation inst, Sleeper sleeper) {

this.activityUtils = ativityUtils;
this.viewFetcher = viewFetcher;
this.scroller = scroller;
this.robotiumUtils = robotiumUtils;
this.inst = inst;
}
this.sleeper = sleeper;
}

/**
* Clicks on a specific coordinate on the screen
Expand Down Expand Up @@ -103,12 +106,12 @@ public void clickLongOnScreen(float x, float y) {
y + ViewConfiguration.getTouchSlop() / 2, 0);
inst.sendPointerSync(event);
inst.waitForIdleSync();
RobotiumUtils.sleep((int)(ViewConfiguration.getLongPressTimeout() * 1.5f));
sleeper.sleep((int)(ViewConfiguration.getLongPressTimeout() * 1.5f));
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);
inst.sendPointerSync(event);
inst.waitForIdleSync();
RobotiumUtils.sleep();
sleeper.sleep();

}

Expand Down Expand Up @@ -137,7 +140,7 @@ private void clickOnScreen(View view, boolean longClick) {
long now = System.currentTimeMillis();
final long endTime = now + CLICKTIMEOUT;
while ((!view.isShown() || view.isLayoutRequested()) && now < endTime) {
RobotiumUtils.sleep();
sleeper.sleep();
now = System.currentTimeMillis();
}
if(!view.isShown())
Expand All @@ -147,7 +150,7 @@ private void clickOnScreen(View view, boolean longClick) {
.getDefaultDisplay().getHeight() && scroller.scrollDown()) {
view.getLocationOnScreen(xy);
}
RobotiumUtils.sleepMini();
sleeper.sleepMini();
view.getLocationOnScreen(xy);
final int viewWidth = view.getWidth();
final int viewHeight = view.getHeight();
Expand Down Expand Up @@ -229,7 +232,7 @@ public void clickLongOnTextAndPress(String text, int index)
}
for(int i = 0; i < index; i++)
{
RobotiumUtils.sleepMini();
sleeper.sleepMini();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
}
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_ENTER);
Expand Down Expand Up @@ -284,7 +287,7 @@ public void clickLongOnText(String text, int match, boolean scroll)

public void clickOnMenuItem(String text)
{
RobotiumUtils.sleep();
sleeper.sleep();
inst.waitForIdleSync();
try{
robotiumUtils.sendKeyCode(KeyEvent.KEYCODE_MENU);
Expand All @@ -304,7 +307,7 @@ public void clickOnMenuItem(String text)

public void clickOnMenuItem(String text, boolean subMenu)
{
RobotiumUtils.sleep();
sleeper.sleep();
inst.waitForIdleSync();
TextView textMore = null;
int [] xy = new int[2];
Expand Down Expand Up @@ -551,11 +554,11 @@ public void clickOnEditText(int index) {
*/

public void goBack() {
RobotiumUtils.sleep();
sleeper.sleep();
inst.waitForIdleSync();
try {
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
RobotiumUtils.sleep();
sleeper.sleep();
} catch (Throwable e) {}
}

Expand Down Expand Up @@ -584,13 +587,13 @@ public ArrayList<TextView> clickInList(int line) {

public ArrayList<TextView> clickInList(int line, int index) {
robotiumUtils.waitForIdle();
RobotiumUtils.sleep();
sleeper.sleep();
long now = System.currentTimeMillis();
final long endTime = now + CLICKTIMEOUT;
int size = viewFetcher.getCurrentListViews().size();
while((size > 0 && size <index+1) && now < endTime)
{
RobotiumUtils.sleep();
sleeper.sleep();
}
if (now > endTime)
Assert.assertTrue("No ListView with index " + index + " is available", false);
Expand Down
Expand Up @@ -12,18 +12,20 @@
class DialogUtils {

private final ViewFetcher viewFetcher;
private final Sleeper sleeper;

/**
* Constructs this object.
*
* @param viewFetcher the {@link ViewFetcher} instance.
* @param viewFetcher the {@code ViewFetcher} instance.
* @param sleeper the {@code RobotiumUtils} instance.
*
*/

public DialogUtils(ViewFetcher viewFetcher) {
public DialogUtils(ViewFetcher viewFetcher, Sleeper sleeper) {
this.viewFetcher = viewFetcher;

}
this.sleeper = sleeper;
}


/**
Expand All @@ -34,7 +36,7 @@ public DialogUtils(ViewFetcher viewFetcher) {
*/

public boolean waitForDialogToClose(long timeout) {
RobotiumUtils.sleepMini();
sleeper.sleepMini();
int elements = viewFetcher.getWindowDecorViews().length;
long now = System.currentTimeMillis();
final long endTime = now + timeout;
Expand All @@ -48,7 +50,7 @@ public boolean waitForDialogToClose(long timeout) {
if(!viewFetcher.getActiveDecorView().isEnabled())
break;

RobotiumUtils.sleepMini();
sleeper.sleepMini();
now = System.currentTimeMillis();
}

Expand Down
Expand Up @@ -17,22 +17,25 @@ class Presser{
private final ViewFetcher viewFetcher;
private final Clicker clicker;
private final Instrumentation inst;
private final Sleeper sleeper;

/**
* Constructs this object.
*
* @param viewFetcher the {@link ViewFetcher} instance.
* @param clicker the {@link Clicker} instance.
* @param inst the {@link Instrumentation} instance.
* @param viewFetcher the {@code ViewFetcher} instance.
* @param clicker the {@code Clicker} instance.
* @param inst the {@code Instrumentation} instance.
* @param sleeper the {@code RobotiumUtils} instance.
*/

public Presser(ViewFetcher viewFetcher,
Clicker clicker, Instrumentation inst) {
Clicker clicker, Instrumentation inst, Sleeper sleeper) {

this.viewFetcher = viewFetcher;
this.clicker = clicker;
this.inst = inst;
}
this.sleeper = sleeper;
}


/**
Expand All @@ -46,33 +49,33 @@ public Presser(ViewFetcher viewFetcher,

public void pressMenuItem(int index) {
inst.waitForIdleSync();
RobotiumUtils.sleep();
sleeper.sleep();
try{
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
RobotiumUtils.sleepMini();
sleeper.sleepMini();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);
}catch(Throwable e){
Assert.assertTrue("Can not press the menu!", false);
}
if (index < 3) {
for (int i = 0; i < index; i++) {
RobotiumUtils.sleepMini();
sleeper.sleepMini();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
}
} else if (index >= 3 && index < 5) {
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);

for (int i = 3; i < index; i++) {
RobotiumUtils.sleepMini();
sleeper.sleepMini();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
}
} else if (index >= 5) {
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);

for (int i = 5; i < index; i++) {
RobotiumUtils.sleepMini();
sleeper.sleepMini();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
}
}
Expand All @@ -94,7 +97,7 @@ public void pressMenuItem(int index) {
public void pressSpinnerItem(int spinnerIndex, int itemIndex)
{
inst.waitForIdleSync();
RobotiumUtils.sleep();
sleeper.sleep();
clicker.clickOnScreen(viewFetcher.getCurrentSpinners().get(spinnerIndex));
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
boolean countingUp = true;
Expand All @@ -104,7 +107,7 @@ public void pressSpinnerItem(int spinnerIndex, int itemIndex)
}
for(int i = 0; i < itemIndex; i++)
{
RobotiumUtils.sleepMini();
sleeper.sleepMini();
if(countingUp){
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
}else{
Expand Down

0 comments on commit 8fac33c

Please sign in to comment.