Bug 715369 - Work around Solo.getCurrentActivity() possibly returning the wrong object. r=jmaher

This commit is contained in:
Kartikaya Gupta 2012-01-05 21:36:17 -05:00
parent 4a223fd44d
commit 20a095db65
5 changed files with 34 additions and 28 deletions

View File

@ -38,17 +38,20 @@
* ***** END LICENSE BLOCK ***** */
package @ANDROID_PACKAGE_NAME@;
import java.util.List;
import android.app.Activity;
public interface Driver {
/**
* Find the first Element using the given method.
*
* @param activity The activity the element belongs to
* @param name The name of the element
* @return The first matching element on the current context
* @throws RoboCopException If no matching elements are found
*/
Element findElement(String name);
Element findElement(Activity activity, String name);
/**
* Sets up scroll handling so that data is received from the extension.

View File

@ -167,8 +167,7 @@ public class FennecNativeDriver implements Driver {
return geckoWidth;
}
@Override
public Element findElement(String name) {
public Element findElement(Activity activity, String name) {
if (name == null)
throw new IllegalArgumentException("Can not findElements when passed a null");
if (locators.containsKey(name)){

View File

@ -56,28 +56,26 @@ import com.jayway.android.robotium.solo.Solo;
import java.util.concurrent.SynchronousQueue;
public class FennecNativeElement implements Element {
private final Activity mActivity;
private Integer id;
private Activity currentActivity;
private Solo robocop;
public FennecNativeElement(Integer id, Activity activity, Solo solo){
this.id = id;
mActivity = activity;
robocop = solo;
currentActivity = activity;
}
public Integer getId() {
return id;
}
@Override
public void click() {
final SynchronousQueue syncQueue = new SynchronousQueue();
currentActivity = robocop.getCurrentActivity();
currentActivity.runOnUiThread(
mActivity.runOnUiThread(
new Runnable() {
public void run() {
View view = (View)currentActivity.findViewById(id);
View view = (View)mActivity.findViewById(id);
if(view != null) {
view.performClick();
} else {
@ -94,16 +92,13 @@ public class FennecNativeElement implements Element {
}
private Object text;
private Activity elementActivity;
@Override
public String getText() {
elementActivity = robocop.getCurrentActivity();
final SynchronousQueue syncQueue = new SynchronousQueue();
elementActivity.runOnUiThread(
mActivity.runOnUiThread(
new Runnable() {
public void run() {
View v = elementActivity.findViewById(id);
View v = mActivity.findViewById(id);
if(v instanceof EditText) {
EditText et = (EditText)v;
text = et.getEditableText();
@ -143,7 +138,6 @@ public class FennecNativeElement implements Element {
private boolean displayed;
@Override
public boolean isDisplayed() {
final SynchronousQueue syncQueue = new SynchronousQueue();
currentActivity = robocop.getCurrentActivity();

View File

@ -5,6 +5,7 @@ import com.jayway.android.robotium.solo.Solo;
import @ANDROID_PACKAGE_NAME@.*;
import android.app.Activity;
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.content.Intent;
import java.util.HashMap;
@ -47,7 +48,7 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
mActivity = getActivity();
// Set up Robotium.solo and Driver objects
mSolo = new Solo(getInstrumentation(), getActivity());
mSolo = new Solo(getInstrumentation());
mDriver = new FennecNativeDriver(mActivity, mSolo);
mActions = new FennecNativeActions(mActivity, mSolo, getInstrumentation());
mDriver.setLogFile((String)config.get("logfile"));
@ -67,13 +68,20 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
super.tearDown();
}
protected final Activity getActivityFromClick(Element element) {
Instrumentation inst = getInstrumentation();
Instrumentation.ActivityMonitor monitor = inst.addMonitor((String)null, null, false);
element.click();
return inst.waitForMonitor(monitor);
}
protected final void enterUrl(String url) {
mActions.expectGeckoEvent("Gecko:Ready").blockForEvent();
Element awesomebar = mDriver.findElement("awesome_bar");
awesomebar.click();
Element awesomebar = mDriver.findElement(mActivity, "awesome_bar");
Activity awesomeBarActivity = getActivityFromClick(awesomebar);
getInstrumentation().waitForIdleSync();
Element urlbar = mDriver.findElement("awesomebar_text");
Element urlbar = mDriver.findElement(awesomeBarActivity, "awesomebar_text");
mActions.sendKeys(url);
mAsserter.is(urlbar.getText(), url, "Awesomebar URL typed properly");
}
@ -91,10 +99,11 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
}
protected final void verifyUrl(String url) {
Element awesomebar = mDriver.findElement("awesome_bar");
Element urlbar = mDriver.findElement("awesomebar_text");
awesomebar.click();
Element awesomebar = mDriver.findElement(mActivity, "awesome_bar");
Activity awesomeBarActivity = getActivityFromClick(awesomebar);
getInstrumentation().waitForIdleSync();
Element urlbar = mDriver.findElement(awesomeBarActivity, "awesomebar_text");
mAsserter.is(urlbar.getText(), url, "Awesomebar URL stayed the same");
}
}

View File

@ -2,6 +2,7 @@
package @ANDROID_PACKAGE_NAME@.tests;
import @ANDROID_PACKAGE_NAME@.*;
import android.app.Activity;
public class testNewTab extends BaseTest {
public void testNewTab() {
@ -9,23 +10,23 @@ public class testNewTab extends BaseTest {
String url = "http://mochi.test:8888/tests/robocop/robocop_blank_01.html";
String url2 = "http://mochi.test:8888/tests/robocop/robocop_blank_02.html";
mActions.expectGeckoEvent("Gecko:Ready").blockForEvent();
Element tabs = mDriver.findElement("tabs");
Element tabs = mDriver.findElement(getActivity(), "tabs");
// Add one tab
tabs.click();
Activity awesomeBarActivity = getActivityFromClick(tabs);
Element urlbar = mDriver.findElement("awesomebar_text");
getInstrumentation().waitForIdleSync();
Element urlbar = mDriver.findElement(awesomeBarActivity, "awesomebar_text");
mActions.sendKeys(url);
mAsserter.is(urlbar.getText(), url, "Awesomebar url is fine");
hitEnterAndWait();
// See tab count
Element tabCount = mDriver.findElement("tabs_count");
Element tabCount = mDriver.findElement(getActivity(), "tabs_count");
mAsserter.is(tabCount.getText(), "2", "Number of tabs has increased");
// Click tab list
tabs.click();
Element addTab = mDriver.findElement("add_tab");
Activity tabTray = getActivityFromClick(tabs);
Element addTab = mDriver.findElement(tabTray, "add_tab");
//Add another tab
addTab.click();