Skip to content

Commit

Permalink
Increased stability in the assertCurrentActivity methods as well as i…
Browse files Browse the repository at this point in the history
…n pressSpinnerItem. Also moved waitForActivity to the Waiter class and some other minor refactoring
  • Loading branch information
renas committed May 26, 2011
1 parent 9592e4f commit 1f291c7
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 70 deletions.
Expand Up @@ -161,29 +161,6 @@ public Activity getCurrentActivity(boolean shouldSleepFirst) {
}
}

/**
* Waits for the given {@link Activity}.
*
* @param name the name of the {@code Activity} to wait for e.g. {@code "MyActivity"}
* @param timeout the amount of time in milliseconds to wait
* @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it does not
*
*/

public boolean waitForActivity(String name, int timeout)
{
long now = System.currentTimeMillis();
final long endTime = now + timeout;
while(!getCurrentActivity().getClass().getSimpleName().equals(name) && now < endTime)
{
now = System.currentTimeMillis();
}
if(now < endTime)
return true;

else
return false;
}

/**
* Returns to the given {@link Activity}.
Expand Down
Expand Up @@ -13,22 +13,22 @@

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

/**
* Constructs this object.
*
* @param activityUtils the {@code ActivityUtils} instance.
* @param sleeper the {@code Sleeper} instance.
* @param waiter the {@code Waiter} instance.
*
*/
public Asserter(ActivityUtils activityUtils, Sleeper sleeper) {

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

/**
/**
* Asserts that an expected {@link Activity} is currently active one.
*
* @param message the message that should be displayed if the assert fails
Expand All @@ -38,26 +38,24 @@ public Asserter(ActivityUtils activityUtils, Sleeper sleeper) {

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

Assert.assertTrue(message, waiter.waitForActivity(name));

}

/**
* Asserts that an expected {@link Activity} is currently active one.
*
* @param message the message that should be displayed if the assert fails
* @param expectedClass the {@code Class} object that is expected to be active e.g. {@code MyActivity.class}
*
*
*/

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

}

/**
Expand Down
Expand Up @@ -19,6 +19,8 @@ class Presser{
private final Clicker clicker;
private final Instrumentation inst;
private final Sleeper sleeper;
private final Waiter waiter;


/**
* Constructs this object.
Expand All @@ -27,15 +29,17 @@ class Presser{
* @param clicker the {@code Clicker} instance.
* @param inst the {@code Instrumentation} instance.
* @param sleeper the {@code Sleeper} instance.
* @param waiter the {@code Waiter} instance.
*/

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

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


Expand Down Expand Up @@ -110,13 +114,12 @@ public void pressMenuItem(int index, int itemsPerRow) {
* @param spinnerIndex the index of the {@code Spinner} menu to be used
* @param itemIndex the index of the {@code Spinner} item to be pressed relative to the currently selected item.
* A Negative number moves up on the {@code Spinner}, positive moves down
*
*
*/

public void pressSpinnerItem(int spinnerIndex, int itemIndex)
{
sleeper.sleep();
inst.waitForIdleSync();
waiter.waitForView(Spinner.class, spinnerIndex, false);
clicker.clickOnScreen(viewFetcher.getCurrentViews(Spinner.class).get(spinnerIndex));
try{
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
Expand All @@ -143,6 +146,4 @@ public void pressSpinnerItem(int spinnerIndex, int itemIndex)
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_ENTER);
}catch(SecurityException ignored){}
}


}
35 changes: 18 additions & 17 deletions robotium-solo/src/main/java/com/jayway/android/robotium/solo/Solo.java 100644 → 100755
Expand Up @@ -73,7 +73,7 @@ public class Solo {
private final Clicker clicker;
private final Presser presser;
private final Searcher searcher;
private final ActivityUtils activitiyUtils;
private final ActivityUtils activityUtils;
private final DialogUtils dialogUtils;
private final TextEnterer textEnterer;
private final Scroller scroller;
Expand Down Expand Up @@ -103,24 +103,25 @@ public class Solo {
* if no start activity is provided
*
*/

public Solo(Instrumentation instrumentation, Activity activity) {
this.instrumentation = instrumentation;
this.sleeper = new Sleeper();
this.activitiyUtils = new ActivityUtils(instrumentation, activity, sleeper);
this.setter = new Setter(activitiyUtils);
this.viewFetcher = new ViewFetcher(instrumentation, activitiyUtils, sleeper);
this.asserter = new Asserter(activitiyUtils, sleeper);
this.activityUtils = new ActivityUtils(instrumentation, activity, sleeper);
this.setter = new Setter(activityUtils);
this.viewFetcher = new ViewFetcher(instrumentation, activityUtils, sleeper);
this.dialogUtils = new DialogUtils(viewFetcher, sleeper);
this.scroller = new Scroller(instrumentation, activitiyUtils, viewFetcher, sleeper);
this.scroller = new Scroller(instrumentation, activityUtils, viewFetcher, sleeper);
this.searcher = new Searcher(viewFetcher, scroller, instrumentation, sleeper);
this.waiter = new Waiter(viewFetcher, searcher,scroller, sleeper);
this.waiter = new Waiter(activityUtils, viewFetcher, searcher,scroller, sleeper);
this.asserter = new Asserter(activityUtils, waiter);
this.checker = new Checker(viewFetcher, waiter);
this.robotiumUtils = new RobotiumUtils(instrumentation, sleeper);
this.clicker = new Clicker(viewFetcher, scroller,robotiumUtils, instrumentation, sleeper, waiter);
this.presser = new Presser(viewFetcher, clicker, instrumentation, sleeper);
this.presser = new Presser(viewFetcher, clicker, instrumentation, sleeper, waiter);
this.textEnterer = new TextEnterer(instrumentation, waiter);
}


/**
* Constructor that takes in the instrumentation.
Expand All @@ -140,7 +141,7 @@ public Solo(Instrumentation instrumentation) {
*/

public ActivityMonitor getActivityMonitor(){
return activitiyUtils.getActivityMonitor();
return activityUtils.getActivityMonitor();
}

/**
Expand Down Expand Up @@ -516,7 +517,7 @@ public boolean searchText(String text, int minimumNumberOfMatches, boolean scrol

public void setActivityOrientation(int orientation)
{
activitiyUtils.setActivityOrientation(orientation);
activityUtils.setActivityOrientation(orientation);
}

/**
Expand All @@ -528,7 +529,7 @@ public void setActivityOrientation(int orientation)

public ArrayList<Activity> getAllOpenedActivities()
{
return activitiyUtils.getAllOpenedActivities();
return activityUtils.getAllOpenedActivities();
}

/**
Expand All @@ -539,7 +540,7 @@ public ArrayList<Activity> getAllOpenedActivities()
*/

public Activity getCurrentActivity() {
Activity activity = activitiyUtils.getCurrentActivity();
Activity activity = activityUtils.getCurrentActivity();
return activity;
}

Expand Down Expand Up @@ -1820,7 +1821,7 @@ public void sendKey(int key)

public void goBackToActivity(String name)
{
activitiyUtils.goBackToActivity(name);
activityUtils.goBackToActivity(name);
}

/**
Expand All @@ -1834,7 +1835,7 @@ public void goBackToActivity(String name)

public boolean waitForActivity(String name, int timeout)
{
return activitiyUtils.waitForActivity(name, timeout);
return waiter.waitForActivity(name, timeout);
}

/**
Expand All @@ -1847,7 +1848,7 @@ public boolean waitForActivity(String name, int timeout)

public String getString(int resId)
{
return activitiyUtils.getString(resId);
return activityUtils.getString(resId);
}


Expand All @@ -1871,7 +1872,7 @@ public void sleep(int time)
*/

public void finalize() throws Throwable {
activitiyUtils.finalize();
activityUtils.finalize();
}

}
Empty file.
Expand Up @@ -13,7 +13,8 @@
*/

class Waiter {


private final ActivityUtils activityUtils;
private final ViewFetcher viewFetcher;
private final int TIMEOUT = 20000;
private final int SMALLTIMEOUT = 10000;
Expand All @@ -26,20 +27,58 @@ class Waiter {

/**
* Constructs this object.
*
* @param viewFetcher the {@code ViewFetcher} instance.
* @param searcher the {@code Searcher} instance.
* @param scroller the {@code Scroller} instance.
* @param sleeper the {@code Sleeper} instance.
*
* @param activityUtils the {@code ActivityUtils} instance
* @param viewFetcher the {@code ViewFetcher} instance
* @param searcher the {@code Searcher} instance
* @param scroller the {@code Scroller} instance
* @param sleeper the {@code Sleeper} instance
*/

public Waiter(ViewFetcher viewFetcher, Searcher searcher, Scroller scroller, Sleeper sleeper){
public Waiter(ActivityUtils activityUtils, ViewFetcher viewFetcher, Searcher searcher, Scroller scroller, Sleeper sleeper){
this.activityUtils = activityUtils;
this.viewFetcher = viewFetcher;
this.searcher = searcher;
this.scroller = scroller;
this.sleeper = sleeper;
matchCounter = new MatchCounter();
}

/**
* Waits for the given {@link Activity}.
*
* @param name the name of the {@code Activity} to wait for e.g. {@code "MyActivity"}
* @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it does not
*
*/

public boolean waitForActivity(String name){
return waitForActivity(name, SMALLTIMEOUT);
}

/**
* Waits for the given {@link Activity}.
*
* @param name the name of the {@code Activity} to wait for e.g. {@code "MyActivity"}
* @param timeout the amount of time in milliseconds to wait
* @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it does not
*
*/

public boolean waitForActivity(String name, int timeout)
{
long now = System.currentTimeMillis();
final long endTime = now + timeout;
while(!activityUtils.getCurrentActivity().getClass().getSimpleName().equals(name) && now < endTime)
{
now = System.currentTimeMillis();
}
if(now < endTime)
return true;

else
return false;
}

/**
* Waits for a view to be shown.
Expand Down

0 comments on commit 1f291c7

Please sign in to comment.