Bug 969922 - Part 2: Replace Driver.findElement(Activity, String) with (Activity, int). r=mcomella

This commit is contained in:
Nick Alexander 2014-02-11 18:08:56 -08:00
parent 4d6c04a52b
commit 8c0e0c7863
10 changed files with 59 additions and 92 deletions

View File

@ -12,10 +12,10 @@ 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
* @param id The resource id of the element
* @return The first matching element on the current context, or null if not found.
*/
Element findElement(Activity activity, String name);
Element findElement(Activity activity, int id);
/**
* Sets up scroll handling so that data is received from the extension.

View File

@ -122,21 +122,12 @@ public class FennecNativeDriver implements Driver {
return mGeckoWidth;
}
/** Find the named element in the list of known Fennec views.
/** Find the element with given id.
*
* @return An Element representing the view, or null if the view is not found.
*/
public Element findElement(Activity activity, String name) {
if (name == 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);
}
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"findElement: Element '"+name+"' does not exist in the list");
return null;
public Element findElement(Activity activity, int id) {
return new FennecNativeElement(id, activity);
}
public void startFrameRecording() {

View File

@ -59,11 +59,6 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
public static final int MAX_WAIT_MS = 4500;
public static final int LONG_PRESS_TIME = 6000;
// IDs for UI views
private static final String BROWSER_TOOLBAR_ID = "browser_toolbar";
protected static final String URL_EDIT_TEXT_ID = "url_edit_text";
protected static final String URL_BAR_TITLE_ID = "url_bar_title";
private static Class<Activity> mLauncherActivityClass;
private Activity mActivity;
protected Solo mSolo;
@ -185,7 +180,7 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
*/
protected final void focusUrlBar() {
// Click on the browser toolbar to enter editing mode
final View toolbarView = mSolo.getView(BROWSER_TOOLBAR_ID);
final View toolbarView = mSolo.getView("browser_toolbar");
mSolo.clickOnView(toolbarView);
// Wait for highlighed text to gain focus
@ -206,7 +201,7 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
}
protected final void enterUrl(String url) {
final EditText urlEditView = (EditText) mSolo.getView(URL_EDIT_TEXT_ID);
final EditText urlEditView = (EditText) mSolo.getView("url_edit_text");
focusUrlBar();
@ -260,7 +255,7 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
}
public final void verifyUrl(String url) {
final EditText urlEditText = (EditText) mSolo.getView(URL_EDIT_TEXT_ID);
final EditText urlEditText = (EditText) mSolo.getView("url_edit_text");
String urlBarText = null;
if (urlEditText != null) {
// wait for a short time for the expected text, in case there is a delay
@ -503,7 +498,7 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
}
public final void verifyPageTitle(String title) {
final TextView urlBarTitle = (TextView) mSolo.getView(URL_BAR_TITLE_ID);
final TextView urlBarTitle = (TextView) mSolo.getView("url_bar_title");
String pageTitle = null;
if (urlBarTitle != null) {
// Wait for the title to make sure it has been displayed in case the view
@ -515,8 +510,7 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
}
public final void verifyTabCount(int expectedTabCount) {
Activity activity = getActivity();
Element tabCount = mDriver.findElement(activity, "tabs_counter");
Element tabCount = mDriver.findElement(getActivity(), R.id.tabs_counter);
String tabCountText = tabCount.getText();
int tabCountInt = Integer.parseInt(tabCountText);
mAsserter.is(tabCountInt, expectedTabCount, "The correct number of tabs are opened");
@ -574,11 +568,9 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
* @return List view in the tabs tray
*/
private final AdapterView<ListAdapter> getTabsList() {
Element tabs = mDriver.findElement(getActivity(), "tabs");
Element tabs = mDriver.findElement(getActivity(), R.id.tabs);
tabs.click();
Element listElem = mDriver.findElement(getActivity(), "normal_tabs");
int listId = listElem.getId();
return (AdapterView<ListAdapter>) getActivity().findViewById(listId);
return (AdapterView<ListAdapter>) getActivity().findViewById(R.id.normal_tabs);
}
/**
@ -639,8 +631,7 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
* @param index Index of tab to close
*/
public void closeTabAt(final int index) {
Element close = mDriver.findElement(getActivity(), "close");
View closeButton = getTabViewAt(index).findViewById(close.getId());
View closeButton = getTabViewAt(index).findViewById(R.id.close);
mSolo.clickOnView(closeButton);
}
@ -749,7 +740,7 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
Actions.EventExpecter pageShowExpecter = mActions.expectGeckoEvent("Content:PageShow");
if (devType.equals("tablet")) {
Element backBtn = mDriver.findElement(getActivity(), "back");
Element backBtn = mDriver.findElement(getActivity(), R.id.back);
backBtn.click();
} else {
mActions.sendSpecialKey(Actions.SpecialKey.BACK);
@ -763,13 +754,13 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
Actions.EventExpecter pageShowExpecter = mActions.expectGeckoEvent("Content:PageShow");
if (devType.equals("tablet")) {
Element fwdBtn = mDriver.findElement(getActivity(), "forward");
Element fwdBtn = mDriver.findElement(getActivity(), R.id.forward);
fwdBtn.click();
} else {
mActions.sendSpecialKey(Actions.SpecialKey.MENU);
waitForText("^New Tab$");
if (!osVersion.equals("2.x")) {
Element fwdBtn = mDriver.findElement(getActivity(), "forward");
Element fwdBtn = mDriver.findElement(getActivity(), R.id.forward);
fwdBtn.click();
} else {
mSolo.clickOnText("^Forward$");
@ -783,13 +774,13 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
public void reload() {
if (devType.equals("tablet")) {
Element reloadBtn = mDriver.findElement(getActivity(), "reload");
Element reloadBtn = mDriver.findElement(getActivity(), R.id.reload);
reloadBtn.click();
} else {
mActions.sendSpecialKey(Actions.SpecialKey.MENU);
waitForText("^New Tab$");
if (!osVersion.equals("2.x")) {
Element reloadBtn = mDriver.findElement(getActivity(), "reload");
Element reloadBtn = mDriver.findElement(getActivity(), R.id.reload);
reloadBtn.click();
} else {
mSolo.clickOnText("^Reload$");
@ -807,7 +798,7 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
// This is the Android 2.x so the button has text
mSolo.clickOnText("^Bookmark$");
} else {
Element bookmarkBtn = mDriver.findElement(getActivity(), "bookmark");
Element bookmarkBtn = mDriver.findElement(getActivity(), R.id.bookmark);
if (bookmarkBtn != null) {
// We are on Android 4.x so the button is an image button
bookmarkBtn.click();

View File

@ -13,7 +13,7 @@ public class testAboutPage extends PixelTest {
}
private void ensureTitleMatches(final String regex) {
Element urlBarTitle = mDriver.findElement(getActivity(), URL_BAR_TITLE_ID);
Element urlBarTitle = mDriver.findElement(getActivity(), R.id.url_bar_title);
mAsserter.isnot(urlBarTitle, null, "Got the URL bar title");
assertMatches(urlBarTitle.getText(), regex, "page title match");
}

View File

@ -30,7 +30,7 @@ public class testBookmarksPanel extends AboutHomeTest {
}
// Test that "Open in New Tab" works
final Element tabCount = mDriver.findElement(getActivity(), "tabs_counter");
final Element tabCount = mDriver.findElement(getActivity(), R.id.tabs_counter);
final int tabCountInt = Integer.parseInt(tabCount.getText());
Actions.EventExpecter tabEventExpecter = mActions.expectGeckoEvent("Tab:Added");
mSolo.clickOnText(StringHelper.BOOKMARK_CONTEXT_MENU_ITEMS[0]);

View File

@ -46,10 +46,10 @@ public class testFindInPage extends PixelTest {
public void findText(String text, int nrOfMatches){
selectMenuItem("Find in Page");
close = mDriver.findElement(getActivity(), "find_close");
close = mDriver.findElement(getActivity(), R.id.find_close);
boolean success = waitForTest ( new BooleanTest() {
public boolean test() {
next = mDriver.findElement(getActivity(), "find_next");
next = mDriver.findElement(getActivity(), R.id.find_next);
if (next != null) {
return true;
} else {

View File

@ -113,7 +113,7 @@ public final class testInputUrlBar extends BaseTest {
private void startEditingMode() {
focusUrlBar();
mUrlBarEditElement = mDriver.findElement(getActivity(), URL_EDIT_TEXT_ID);
mUrlBarEditElement = mDriver.findElement(getActivity(), R.id.url_edit_text);
final int id = mUrlBarEditElement.getId();
mUrlBarEditView = (EditText) getActivity().findViewById(id);
}

View File

@ -24,9 +24,9 @@ public class testNewTab extends BaseTest {
blockForGeckoReady();
Activity activity = getActivity();
tabCount = mDriver.findElement(activity, "tabs_counter");
tabs = mDriver.findElement(activity, "tabs");
addTab = mDriver.findElement(activity, "add_tab");
tabCount = mDriver.findElement(activity, R.id.tabs_counter);
tabs = mDriver.findElement(activity, R.id.tabs);
addTab = mDriver.findElement(activity, R.id.add_tab);
mAsserter.ok(tabCount != null &&
tabs != null &&
addTab != null,

View File

@ -37,9 +37,6 @@ public class testSearchSuggestions extends BaseTest {
final HashMap<String, ArrayList<String>> suggestMap = new HashMap<String, ArrayList<String>>();
buildSuggestMap(suggestMap);
final int suggestionLayoutId = mDriver.findElement(getActivity(), "suggestion_layout").getId();
final int suggestionTextId = mDriver.findElement(getActivity(), "suggestion_text").getId();
focusUrlBar();
for (int i = 0; i < TEST_QUERY.length(); i++) {
@ -65,7 +62,7 @@ public class testSearchSuggestions extends BaseTest {
@Override
public boolean test() {
// get the first suggestion row
ViewGroup suggestionGroup = (ViewGroup) getActivity().findViewById(suggestionLayoutId);
ViewGroup suggestionGroup = (ViewGroup) getActivity().findViewById(R.id.suggestion_layout);
if (suggestionGroup == null)
return false;
@ -75,7 +72,7 @@ public class testSearchSuggestions extends BaseTest {
if (queryChild == null || queryChild.getVisibility() == View.GONE)
return false;
String suggestion = ((TextView) queryChild.findViewById(suggestionTextId)).getText().toString();
String suggestion = ((TextView) queryChild.findViewById(R.id.suggestion_text)).getText().toString();
if (!suggestion.equals(expected.get(i)))
return false;
}

View File

@ -3,13 +3,7 @@ package org.mozilla.gecko.tests;
import org.mozilla.gecko.db.BrowserDB;
import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Test for thumbnail updates.
@ -20,20 +14,12 @@ import android.widget.TextView;
* - finally, test that BrowserDB.removeThumbnails drops the thumbnails
*/
public class testThumbnails extends BaseTest {
private int mTopSitesId;
private int mThumbnailId;
private int mTitleId;
@Override
protected int getTestType() {
return TEST_MOCHITEST;
}
public void testThumbnails() {
mTopSitesId = mDriver.findElement(getActivity(), "top_sites_grid").getId();
mThumbnailId = mDriver.findElement(getActivity(), "thumbnail").getId();
mTitleId = mDriver.findElement(getActivity(), "title").getId();
final String site1Url = getAbsoluteUrl("/robocop/robocop_404.sjs?type=changeColor");
final String site2Url = getAbsoluteUrl("/robocop/robocop_404.sjs?type=do404");
final String site1Title = "changeColor";
@ -96,33 +82,35 @@ public class testThumbnails extends BaseTest {
}
private int getTopSiteThumbnailColor(String title) {
ViewGroup topSites = (ViewGroup) getActivity().findViewById(mTopSitesId);
if (topSites != null) {
final int childCount = topSites.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = topSites.getChildAt(i);
if (child != null) {
TextView titleView = (TextView) child.findViewById(mTitleId);
if (titleView != null) {
if (titleView.getText().equals(title)) {
ImageView thumbnailView = (ImageView) child.findViewById(mThumbnailId);
if (thumbnailView != null) {
Bitmap thumbnail = ((BitmapDrawable) thumbnailView.getDrawable()).getBitmap();
return thumbnail.getPixel(0, 0);
} else {
mAsserter.dumpLog("getTopSiteThumbnailColor: unable to find mThumbnailId: "+mThumbnailId);
}
}
} else {
mAsserter.dumpLog("getTopSiteThumbnailColor: unable to find mTitleId: "+mTitleId);
}
} else {
mAsserter.dumpLog("getTopSiteThumbnailColor: skipped null child at index "+i);
}
}
} else {
mAsserter.dumpLog("getTopSiteThumbnailColor: unable to find mTopSitesId: "+mTopSitesId);
}
// This test is not currently run, so this just needs to compile.
return -1;
// ViewGroup topSites = (ViewGroup) getActivity().findViewById(mTopSitesId);
// if (topSites != null) {
// final int childCount = topSites.getChildCount();
// for (int i = 0; i < childCount; i++) {
// View child = topSites.getChildAt(i);
// if (child != null) {
// TextView titleView = (TextView) child.findViewById(R.id.title);
// if (titleView != null) {
// if (titleView.getText().equals(title)) {
// ImageView thumbnailView = (ImageView) child.findViewById(R.id.thumbnail);
// if (thumbnailView != null) {
// Bitmap thumbnail = ((BitmapDrawable) thumbnailView.getDrawable()).getBitmap();
// return thumbnail.getPixel(0, 0);
// } else {
// mAsserter.dumpLog("getTopSiteThumbnailColor: unable to find mThumbnailId: "+R.id.thumbnail);
// }
// }
// } else {
// mAsserter.dumpLog("getTopSiteThumbnailColor: unable to find R.id.title: "+R.id.title);
// }
// } else {
// mAsserter.dumpLog("getTopSiteThumbnailColor: skipped null child at index "+i);
// }
// }
// } else {
// mAsserter.dumpLog("getTopSiteThumbnailColor: unable to find mTopSitesId: " + mTopSitesId);
// }
// return -1;
}
}