Bug 725094 - Robocop: change error handling to throw fewer exceptions; r=jmaher

This commit is contained in:
Geoff Brown 2012-05-22 16:25:30 -07:00
Родитель 6d73d84e79
Коммит 653f6e51e8
9 изменённых файлов: 115 добавлений и 36 удалений

Просмотреть файл

@ -14,8 +14,7 @@ public interface Driver {
*
* @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
* @return The first matching element on the current context, or null if not found.
*/
Element findElement(Activity activity, String name);

Просмотреть файл

@ -11,13 +11,16 @@ package @ANDROID_PACKAGE_NAME@;
*/
public interface Element {
/** Click on the element */
void click();
/** Click on the element's view. Returns true on success. */
boolean click();
/** Returns true if the element is currently displayed */
boolean isDisplayed();
/** Returns the text currently displayed on the element */
/**
* Returns the text currently displayed on the element, or null
* if the text cannot be retrieved.
*/
String getText();
/** Returns the view ID */

Просмотреть файл

@ -169,14 +169,21 @@ public class FennecNativeDriver implements Driver {
return mGeckoWidth;
}
/** Find the named element in the list of known Fennec views.
* @return An Element representing the view, or null if the view is not found.
*/
public Element findElement(Activity activity, String name) {
if (name == null) {
throw new IllegalArgumentException("Can not findElements when passed a null");
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"Can not findElements when passed a null");
return null;
}
if (mLocators.containsKey(name)) {
return new FennecNativeElement(Integer.decode((String)mLocators.get(name)), activity, mSolo);
}
throw new RoboCopException("Element does not exist in the list");
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"findElement: Element '"+name+"' does not exist in the list");
return null;
}
public void startFrameRecording() {

Просмотреть файл

@ -34,19 +34,25 @@ public class FennecNativeElement implements Element {
return mId;
}
public void click() {
private boolean mClickSuccess;
public boolean click() {
final SynchronousQueue syncQueue = new SynchronousQueue();
mClickSuccess = false;
mActivity.runOnUiThread(
new Runnable() {
public void run() {
View view = (View)mActivity.findViewById(mId);
if(view != null) {
if (!view.performClick()) {
if (view != null) {
if (view.performClick()) {
mClickSuccess = true;
} else {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN,
"Robocop called click on an element with no listener");
}
} else {
throw new RoboCopException("click: unable to find view "+mId);
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"click: unable to find view "+mId);
}
syncQueue.offer(new Object());
}
@ -56,12 +62,14 @@ public class FennecNativeElement implements Element {
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
return mClickSuccess;
}
private Object mText;
public String getText() {
final SynchronousQueue syncQueue = new SynchronousQueue();
mText = null;
mActivity.runOnUiThread(
new Runnable() {
public void run() {
@ -83,9 +91,11 @@ public class FennecNativeElement implements Element {
} else if (v instanceof TextView) {
mText = ((TextView)v).getText();
} else if (v == null) {
throw new RoboCopException("getText: unable to find view "+mId);
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"getText: unable to find view "+mId);
} else {
throw new RoboCopException("getText: unhandled type for view "+mId);
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"getText: unhandled type for view "+mId);
}
syncQueue.offer(new Object());
} // end of run() method definition
@ -98,7 +108,9 @@ public class FennecNativeElement implements Element {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
if (mText == null) {
throw new RoboCopException("getText: Text is null for view "+mId);
FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN,
"getText: Text is null for view "+mId);
return null;
}
return mText.toString();
}

Просмотреть файл

@ -18,6 +18,9 @@ import java.io.IOException;
import java.util.HashMap;
/**
* A convenient base class suitable for most Robocop tests.
*/
abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
private static final String TARGET_PACKAGE_ID = "org.mozilla.gecko";
private static final String LAUNCH_ACTIVITY_FULL_CLASSNAME="@ANDROID_PACKAGE_NAME@.App";
@ -93,10 +96,18 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
super.tearDown();
}
/**
* Click on the specified element and return the resulting activity.
* @return The created activity, or null if the element cannot be clicked.
*/
protected final Activity getActivityFromClick(Element element) {
Instrumentation inst = getInstrumentation();
Instrumentation.ActivityMonitor monitor = inst.addMonitor((String)null, null, false);
element.click();
boolean clicked = element.click();
if (!clicked) {
mAsserter.ok(clicked != false, "checking that awesome bar clicked", "awesome bar was clicked");
return null;
}
// wait for click to take effect before waiting for activity
// (otherwise we sometimes get the previous activity)
getInstrumentation().waitForIdleSync();
@ -108,16 +119,31 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
return activity;
}
/**
* Click on the awesome bar element and return the resulting activity.
* @return The created activity, or null if the awesome bar cannot be clicked.
*/
protected final Activity clickOnAwesomeBar() {
Activity activity = null;
Element awesomebar = mDriver.findElement(mActivity, "awesome_bar");
return getActivityFromClick(awesomebar);
if (awesomebar != null) {
activity = getActivityFromClick(awesomebar);
if (activity == null) {
mAsserter.dumpLog("failed to click on awesome bar!");
}
}
return activity;
}
protected final void enterUrl(String url) {
Activity awesomeBarActivity = clickOnAwesomeBar();
Element urlbar = mDriver.findElement(awesomeBarActivity, "awesomebar_text");
mActions.sendKeys(url);
mAsserter.is(urlbar.getText(), url, "Awesomebar URL typed properly");
String urlbarText = null;
if (urlbar != null) {
urlbarText = urlbar.getText();
}
mAsserter.is(urlbarText, url, "Awesomebar URL typed properly");
}
protected final void hitEnterAndWait() {
@ -135,7 +161,11 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
protected final void verifyUrl(String url) {
Activity awesomeBarActivity = clickOnAwesomeBar();
Element urlbar = mDriver.findElement(awesomeBarActivity, "awesomebar_text");
mAsserter.is(urlbar.getText(), url, "Awesomebar URL stayed the same");
String urlbarText = null;
if (urlbar != null) {
urlbarText = urlbar.getText();
}
mAsserter.is(urlbarText, url, "Awesomebar URL stayed the same");
}
protected final String getAbsoluteUrl(String url) {

Просмотреть файл

@ -15,7 +15,7 @@ public class testAboutPage extends BaseTest {
loadUrl(url);
Element awesomebar = mDriver.findElement(getActivity(), "awesome_bar");
mAsserter.ok(awesomebar.getText().matches("About (Fennec|Nightly|Aurora|Firefox|Firefox Beta)"), "page title match", "about: page title is correct");
mAsserter.ok(awesomebar != null && awesomebar.getText() != null && awesomebar.getText().matches("About (Fennec|Nightly|Aurora|Firefox|Firefox Beta)"), "page title match", "about: page title is correct");
// Open a new page to remove the about: page from the current tab
url = getAbsoluteUrl("/robocop/robocop_blank_01.html");
@ -46,6 +46,6 @@ public class testAboutPage extends BaseTest {
// Grab the title to make sure the about: page was loaded
awesomebar = mDriver.findElement(getActivity(), "awesome_bar");
mAsserter.ok(awesomebar.getText().matches("About (Fennec|Nightly|Aurora|Firefox)"), "page title match", "about: page title is correct");
mAsserter.ok(awesomebar != null && awesomebar.getText() != null && awesomebar.getText().matches("About (Fennec|Nightly|Aurora|Firefox)"), "page title match", "about: page title is correct");
}
}

Просмотреть файл

@ -154,10 +154,12 @@ public class testBookmark extends BaseTest {
mSolo.clickOnText("Bookmarks");
Element bookmarkList = mDriver.findElement(awesomeBarActivity, "bookmarks_list");
ArrayList<ListView> lists = mSolo.getCurrentListViews();
for (ListView list : lists) {
if (list.getId() == bookmarkList.getId())
return list;
if (bookmarkList != null) {
ArrayList<ListView> lists = mSolo.getCurrentListViews();
for (ListView list : lists) {
if (list.getId() == bookmarkList.getId())
return list;
}
}
// Just return null if we can't find the bookmarks list view

Просмотреть файл

@ -9,34 +9,56 @@ public class testNewTab extends BaseTest {
setTestType("mochitest");
String url = getAbsoluteUrl("/robocop/robocop_blank_01.html");
String url2 = getAbsoluteUrl("/robocop/robocop_blank_02.html");
String tabCountText = null;
String urlbarText = null;
Activity activity = null;
Element urlbar = null;
mActions.expectGeckoEvent("Gecko:Ready").blockForEvent();
// Add one tab
Element tabs = mDriver.findElement(getActivity(), "tabs");
Activity activity = getActivityFromClick(tabs);
Element urlbar = mDriver.findElement(activity, "awesomebar_text");
mActions.sendKeys(url);
mAsserter.is(urlbar.getText(), url, "Awesomebar url is fine");
if (tabs != null) {
activity = getActivityFromClick(tabs);
urlbar = mDriver.findElement(activity, "awesomebar_text");
mActions.sendKeys(url);
if (urlbar != null) {
urlbarText = urlbar.getText();
}
}
mAsserter.is(urlbarText, url, "Awesomebar url is fine");
hitEnterAndWait();
// See tab count
Element tabCount = mDriver.findElement(getActivity(), "tabs_count");
mAsserter.is(tabCount.getText(), "2", "Number of tabs has increased");
if (tabCount != null) {
tabCountText = tabCount.getText();
}
mAsserter.is(tabCountText, "2", "Number of tabs has increased");
// Click tab list
activity = getActivityFromClick(tabs);
Element addTab = mDriver.findElement(activity, "add_tab");
// Add another tab. The new tab has its own awesome bar activity, so it
// is important to use the new activity to fetch the awesome bar text.
activity = getActivityFromClick(addTab);
mActions.sendKeys(url2);
urlbar = mDriver.findElement(activity, "awesomebar_text");
mAsserter.is(urlbar.getText(), url2, "URL is still fine");
urlbarText = null;
if (addTab != null) {
activity = getActivityFromClick(addTab);
mActions.sendKeys(url2);
urlbar = mDriver.findElement(activity, "awesomebar_text");
if (urlbar != null) {
urlbarText = urlbar.getText();
}
}
mAsserter.is(urlbarText, url2, "URL is still fine");
hitEnterAndWait();
// Check tab count another time.
mAsserter.is(tabCount.getText(), "3", "Number of tabs has increased");
tabCountText = null;
if (tabCount != null) {
tabCountText = tabCount.getText();
}
mAsserter.is(tabCountText, "3", "Number of tabs has increased");
}
}

Просмотреть файл

@ -35,7 +35,11 @@ public class testWebContentContextMenu extends BaseTest {
// See tab count
Element tabCount = mDriver.findElement(getActivity(), "tabs_count");
mAsserter.is(tabCount.getText(), "2", "Number of tabs has increased");
String tabCountText = null;
if (tabCount != null) {
tabCountText = tabCount.getText();
}
mAsserter.is(tabCountText, "2", "Number of tabs has increased");
// Load the test page again and test for 'Share Link' and 'Bookmark Link'
loadUrl(url);