Bug 899182 - Re-write testBookmark to work with the new about:home. r=lucasr, a=test-only
☠☠ backed out by 6c1ea36a42e6 ☠ ☠
authorAdrian Tamas <adrian.tamas@softvision.ro>
Mon, 19 Aug 2013 17:11:09 +0300
changeset 160601 4bb7318a5a3178fdaf85c74aee167017fe1f38ab
parent 160600 e5797cbf3b838c03157e369760767a7c4a871c0f
child 160602 ed7fc15243accc88e6e3870e7d0fa6429d2ef5bf
push id2961
push userlsblakk@mozilla.com
push dateMon, 28 Oct 2013 21:59:28 +0000
treeherdermozilla-beta@73ef4f13486f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslucasr, test-only
bugs899182
milestone26.0a2
Bug 899182 - Re-write testBookmark to work with the new about:home. r=lucasr, a=test-only
mobile/android/base/tests/AboutHomeTest.java.in
mobile/android/base/tests/BaseTest.java.in
mobile/android/base/tests/StringHelper.java.in
mobile/android/base/tests/robocop.ini
mobile/android/base/tests/testBookmark.java.in
--- a/mobile/android/base/tests/AboutHomeTest.java.in
+++ b/mobile/android/base/tests/AboutHomeTest.java.in
@@ -5,19 +5,23 @@ import com.jayway.android.robotium.solo.
 import @ANDROID_PACKAGE_NAME@.*;
 
 import android.content.ContentResolver;
 import android.database.Cursor;
 import android.net.Uri;
 import android.support.v4.view.ViewPager;
 import android.text.TextUtils;
 import android.view.View;
+import android.view.ViewGroup;
+import android.widget.GridView;
+import android.widget.LinearLayout;
 import android.widget.TabWidget;
 import android.widget.ListAdapter;
 import android.widget.ListView;
+import android.widget.TextView;
 
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 
 /**
  * This class is an extension of BaseTest that helps with interaction with about:home
  * This class contains methods that access the different tabs from about:home, methods that get information like history and bookmarks from the database, edit and remove bookmarks and history items
  * The purpose of this class is to collect all the logically connected methods that deal with about:home
@@ -51,16 +55,82 @@ abstract class AboutHomeTest extends Bas
      */
     protected ListView getHistoryList(String waitText, int expectedChildCount) {
         return null;
     }
     protected ListView getHistoryList(String waitText) {
         return null;
     }
 
+    // Returns true if the bookmark is displayed in the bookmarks tab, false otherwise - does not check in folders
+    protected boolean isBookmarkDisplayed(String url) {
+        View bookmark = getDisplayedBookmark(url);
+        if (bookmark != null) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    // Loads a bookmark by tapping on the bookmark view in the Bookmarks tab
+    protected void loadBookmark(String url) {
+        View bookmark = getDisplayedBookmark(url);
+        if (bookmark != null) {
+            Actions.EventExpecter contentEventExpecter = mActions.expectGeckoEvent("DOMContentLoaded");
+            mSolo.clickOnView(bookmark);
+            contentEventExpecter.blockForEvent();
+            contentEventExpecter.unregisterListener();
+        } else {
+            mAsserter.ok(false, url + " is not one of the displayed bookmarks", "Please make sure the url provided is ookmarked");
+        }
+    }
+
+    // Opens the bookmark context menu by long-tapping on it
+    protected void openBookmarkContextMenu(String url) {
+        View bookmark = getDisplayedBookmark(url);
+        if (bookmark != null) {
+            mSolo.clickLongOnView(bookmark);
+            waitForText("Share");
+        } else {
+            mAsserter.ok(false, url + " is not one of the displayed bookmarks", "Please make sure the url provided is bookmarked");
+        }
+    }
+
+    // @return the View associated with bookmark for the provided url or null if the link is not bookmarked
+    protected View getDisplayedBookmark(String url) {
+        openAboutHomeTab(AboutHomeTabs.BOOKMARKS);
+        ListView bookmarksTabList = findListViewWithTag("bookmarks");
+        waitForListToLoad(bookmarksTabList);
+        ListAdapter adapter = bookmarksTabList.getAdapter();
+        if (adapter != null) {
+            for (int i = 0; i < adapter.getCount(); i++ ) {
+                // I am unable to click the view taken with getView for some reason so getting the child at i
+                bookmarksTabList.smoothScrollToPosition(i);
+                View bookmarkView = bookmarksTabList.getChildAt(i);
+                if (bookmarkView instanceof android.widget.LinearLayout) {
+                    ViewGroup bookmarkItemView = (ViewGroup) bookmarkView;
+                    for (int j = 0 ; j < bookmarkItemView.getChildCount(); j++) {
+                         View bookmarkContent = bookmarkItemView.getChildAt(j);
+                         if (bookmarkContent instanceof android.widget.LinearLayout) {
+                             ViewGroup bookmarkItemLayout = (ViewGroup) bookmarkContent;
+                             for (int k = 0 ; k < bookmarkItemLayout.getChildCount(); k++) {
+                                  // Both the title and url are represented as text views so we can cast the view without any ssues
+                                  TextView bookmarkTextContent = (TextView)bookmarkItemLayout.getChildAt(k);
+                                  if (url.equals(bookmarkTextContent.getText().toString())) {
+                                      return bookmarkView;
+                                  }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
     /**
      * Waits for the given ListView to have a non-empty adapter.
      *
      * This method will return false if the given ListView or its adapter are null.
      */
     protected boolean waitForListToLoad(final ListView listView) {
         Condition listWaitCondition = new Condition() {
             @Override
--- a/mobile/android/base/tests/BaseTest.java.in
+++ b/mobile/android/base/tests/BaseTest.java.in
@@ -708,33 +708,23 @@ abstract class BaseTest extends Activity
                     mSolo.clickOnText("^Reload$");
                 }
             }
         }
 
         // DEPRECATED!
         // Use BaseTest.toggleBookmark() in new code.
         public void bookmark() {
-            if (devType.equals("tablet")) {
-                if (!osVersion.equals("4.x")){
-                    Element bookmarkBtn = mDriver.findElement(getActivity(), "bookmark");
+            mActions.sendSpecialKey(Actions.SpecialKey.MENU);
+            waitForText("^New Tab$");
+            if (mSolo.searchText("^Bookmark$")) {
+                // This is the Android 2.x so the button has text
+                mSolo.clickOnText("^Bookmark$");
+            } else {
+                Element bookmarkBtn = mDriver.findElement(getActivity(), "bookmark");
+                if (bookmarkBtn != null) {
+                    // We are on Android 4.x so the button is an image button
                     bookmarkBtn.click();
                 }
-                else {
-                    mActions.sendSpecialKey(Actions.SpecialKey.MENU);
-                    mSolo.waitForText("^New Tab$");
-                    mSolo.clickOnText("^Bookmark$");
-                }
             }
-            else {
-                mActions.sendSpecialKey(Actions.SpecialKey.MENU);
-                mSolo.waitForText("^New Tab$");
-                if (!osVersion.equals("2.x")) {
-                    Element bookmarkBtn = mDriver.findElement(getActivity(), "bookmark");
-                    bookmarkBtn.click();
-                }
-                else {
-                    mSolo.clickOnText("^Bookmark$");
-                }
-            }
-        }
+         }
     }
 }
--- a/mobile/android/base/tests/StringHelper.java.in
+++ b/mobile/android/base/tests/StringHelper.java.in
@@ -118,9 +118,21 @@ class StringHelper {
     // Android 3.0+
     public static final String TOOLS_LABEL = "Tools";
 
     // Android 2.3 and lower only
     public static final String MORE_LABEL = "More";
     public static final String RELOAD_LABEL = "Reload";
     public static final String FORWARD_LABEL = "Forward";
     public static final String BOOKMARK_LABEL = "Bookmark";
+
+    // Bookmark Toast Notification
+    public static final String BOOKMARK_ADDED_LABEL = "Bookmark added";
+    public static final String BOOKMARK_REMOVED_LABEL = "Bookmark removed";
+    public static final String BOOKMARK_UPDATED_LABEL = "Bookmark updated";
+    public static final String BOOKMARK_OPTIONS_LABEL = "Options";
+
+    // Bookmark Options Context Menu items
+    public static final String[] BOOKMARKS_OPTIONS_CONTEXTMENU_ITEMS = new String[] {
+    "Edit",
+    "Add to Home Screen"
+    };
 }
--- a/mobile/android/base/tests/robocop.ini
+++ b/mobile/android/base/tests/robocop.ini
@@ -1,11 +1,11 @@
 [testAwesomebar]
 # [testAwesomebarSwipes] # disabled on fig - bug 880060
-# [testBookmark] # disabled on fig - bug 880060
+[testBookmark]
 [testBookmarklets]
 [testBookmarkKeyword]
 [testBrowserSearchVisibility]
 [testJNI]
 # [testLoad] # see bug 851861
 [testNewTab]
 [testOrderedBroadcast]
 [testPrefsObserver]
--- a/mobile/android/base/tests/testBookmark.java.in
+++ b/mobile/android/base/tests/testBookmark.java.in
@@ -1,163 +1,82 @@
 #filter substitution
 package @ANDROID_PACKAGE_NAME@.tests;
 
 import @ANDROID_PACKAGE_NAME@.*;
-import android.database.Cursor;
+
 import android.view.View;
 import android.widget.ListView;
+
 import java.util.Arrays;
-import java.lang.reflect.Method;
-import android.content.ContentResolver;
+import java.util.ArrayList;
 
-public class testBookmark extends PixelTest  {
-    private static final String ABOUT_HOME_URL = "about:firefox";
-    private static String BOOKMARK_URL = "/robocop/robocop_blank_01.html";
-    private static String BOOKMARK_TITLE = "Browser Blank Page 01";
-    private ClassLoader mClassLoader;
-    private Method mAddBookmark;
-    private Method mRemoveBookmark;
-    private Method mIsBookmarked;
+public class testBookmark extends AboutHomeTest {
+    private static String BOOKMARK_URL;
+    private static int WAIT_FOR_BOOKMARKED_TIMEOUT = 10000;
+
+    Navigation nav;
 
     @Override
     protected int getTestType() {
         return TEST_MOCHITEST;
     }
 
     public void testBookmark() {
-        BOOKMARK_URL = getAbsoluteUrl(BOOKMARK_URL);
-
-        mClassLoader = getActivity().getApplicationContext().getClassLoader();
-        try {
-            Class browserDB = mClassLoader.loadClass("org.mozilla.gecko.db.BrowserDB");
-            mAddBookmark = browserDB.getMethod("addBookmark", ContentResolver.class, String.class, String.class);
-            mRemoveBookmark = browserDB.getMethod("removeBookmarksWithURL", ContentResolver.class, String.class);
-            mIsBookmarked = browserDB.getMethod("isBookmark", ContentResolver.class, String.class);
-        } catch (java.lang.ClassNotFoundException ex) {
-            mAsserter.is(true, false, "Unable to find class");
-        } catch (java.lang.NoSuchMethodException ex) {
-            mAsserter.is(true, false, "Unable to find method");
-        }
-
-        runAwesomeScreenTest();
+        BOOKMARK_URL = getAbsoluteUrl(StringHelper.ROBOCOP_BLANK_PAGE_01_URL);
+        nav = new Navigation(mDevice);
+        runAboutHomeTest();
         runMenuTest();
     }
 
     public void runMenuTest() {
-        try {
-            boolean isbookmark = (Boolean)mIsBookmarked.invoke(null, getActivity().getContentResolver(), BOOKMARK_URL);
-            mAsserter.is(isbookmark, false, "Page is not bookmarked initially");
-            setUpBookmark(); // loads the page, taps the star button, and waits for the "Bookmark Added" message
-            mAsserter.is(waitForBookmarked(true), true, "Tapping star button bookmarked page");
+        mAsserter.is(mDatabaseHelper.isBookmark(BOOKMARK_URL), false, "Page is not bookmarked initially");
+        setUpBookmark(); // loads the page, taps the star button, and waits for the "Bookmark Added" message
+        waitForBookmarked(true);
 
-            cleanUpBookmark(); // loads the page, taps the star button, and waits for the "Bookmark Removed" message
-            mAsserter.is(waitForBookmarked(false), false, "Tapping star button bookmarked page");
-        } catch(java.lang.IllegalAccessException ex) {
-            mAsserter.is(true, false, "Can not call addBookmark");
-        } catch(java.lang.reflect.InvocationTargetException ex) {
-            mAsserter.is(true, false, "Error calling addBookmark");
-        }
+        cleanUpBookmark(); // loads the page, taps the star button, and waits for the "Bookmark Removed" message
+        waitForBookmarked(false);
     }
 
-    public void runAwesomeScreenTest() {
-        final long PAINT_CLEAR_DELAY = 1000;  // milliseconds
-
+    public void runAboutHomeTest() {
         blockForGeckoReady();
-
-        /* Removed by Bug 896576 - [fig] Remove [getBookmarksList] from BaseTest
-        // Open the bookmark list and check the root folder view
-        ListView bookmarksList = getBookmarksList(ABOUT_HOME_URL, DEFAULT_BOOKMARKS_COUNT);
-
-        mAsserter.ok(bookmarksList != null, "checking that bookmarks list exists", "bookmarks list exists and has " +
-                DEFAULT_BOOKMARKS_COUNT + " children (the default bookmarks)");
-
-        // No folders should be visible if no desktop bookmarks exist
-        int count = bookmarksList.getAdapter().getCount();
-        for (int i = 0; i < count; i++) {
-            Cursor c = (Cursor)bookmarksList.getItemAtPosition(i);
-            String url = c.getString(c.getColumnIndexOrThrow("url"));
-            mAsserter.ok(Arrays.binarySearch(DEFAULT_BOOKMARKS_URLS, url) > -1,
-                         "Find default bookmark", "Default bookmark for " + url + " found");
+        for (String url:StringHelper.DEFAULT_BOOKMARKS_URLS) {
+            mAsserter.ok(mDatabaseHelper.isBookmark(url), "Checking that " + url + " is bookmarked by default", url + " is bookmarked");
         }
 
-        insertOneBookmark();
-        waitForText(BOOKMARK_TITLE);
-
-        final int bookmarksCount = DEFAULT_BOOKMARKS_COUNT + 1;
-        mAsserter.is(bookmarksList.getAdapter().getCount(), bookmarksCount, "bookmarks list has " +
-                bookmarksCount + " children (the default bookmarks and the new one)");
+        mDatabaseHelper.addOrUpdateMobileBookmark(StringHelper.ROBOCOP_BLANK_PAGE_01_TITLE, BOOKMARK_URL);
+        waitForBookmarked(true);
 
-        // Click on the bookmark we created and wait for the bookmarked page to load
-        Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint();
-        View child = bookmarksList.getChildAt(1);
-        mAsserter.ok(child != null, "list item can be retrieved", child != null ? child.toString() : "null!");
-        mSolo.clickOnView(child);
-        paintExpecter.blockUntilClear(PAINT_CLEAR_DELAY);
-        paintExpecter.unregisterListener();
+        mAsserter.ok(isBookmarkDisplayed(BOOKMARK_URL), "Checking if " + BOOKMARK_URL + " is displayed in the Bookmarks Tab", "The bookmark is displayed");
+        loadBookmark(BOOKMARK_URL);
+        waitForText(StringHelper.ROBOCOP_BLANK_PAGE_01_TITLE);
+        verifyPageTitle(StringHelper.ROBOCOP_BLANK_PAGE_01_TITLE);
 
-        // Clean up the bookmark we created
-        deleteBookmark();
-        */
+        mDatabaseHelper.deleteBookmark(BOOKMARK_URL);
+        waitForBookmarked(false);
     }
 
-    private boolean waitForBookmarked(final boolean isBookmarked) {
-        waitForTest(new BooleanTest() {
+    private void waitForBookmarked(final boolean isBookmarked) {
+        boolean bookmarked = waitForTest(new BooleanTest() {
             @Override
             public boolean test() {
-                try {
-                   return isBookmarked == (Boolean)mIsBookmarked.invoke(null, getActivity().getContentResolver(), BOOKMARK_URL);
-                } catch(java.lang.IllegalAccessException ex) {
-                    mAsserter.is(true, false, "Can not call isBookmarked");
-                } catch(java.lang.reflect.InvocationTargetException ex) {
-                    mAsserter.is(true, false, "Error calling isBookmarked");
-                }
-                return false;
+              return mDatabaseHelper.isBookmark(BOOKMARK_URL);
             }
-        }, 10000);
-        try {
-            Boolean res = (Boolean)mIsBookmarked.invoke(null, getActivity().getContentResolver(), BOOKMARK_URL);
-            return res.booleanValue();
-        } catch(java.lang.IllegalAccessException ex) {
-            mAsserter.is(true, false, "Can not call isBookmarked");
-        } catch(java.lang.reflect.InvocationTargetException ex) {
-            mAsserter.is(true, false, "Error calling isBookmarked");
-        }
-        return !isBookmarked;
-    }
-
-    private void insertOneBookmark() {
-        try {
-            mAddBookmark.invoke(null, getActivity().getContentResolver(), BOOKMARK_TITLE, BOOKMARK_URL);
-        } catch(java.lang.IllegalAccessException ex) {
-            mAsserter.is(true, false, "Can not call addBookmark");
-        } catch(java.lang.reflect.InvocationTargetException ex) {
-            mAsserter.is(true, false, "Error calling addBookmark");
-        }
-    }
-
-    private void deleteBookmark() {
-        try {
-            mRemoveBookmark.invoke(null, getActivity().getContentResolver(), BOOKMARK_URL);
-        } catch(java.lang.IllegalAccessException ex) {
-            mAsserter.is(true, false, "Can not call removeBookmark");
-        } catch(java.lang.reflect.InvocationTargetException ex) {
-            mAsserter.is(true, false, "Error calling removeBookmark");
-        }
+        }, WAIT_FOR_BOOKMARKED_TIMEOUT);
+        mAsserter.is(bookmarked, isBookmarked, BOOKMARK_URL + " was " + (bookmarked ? "added as a bookmark" : "removed from bookmarks"));
     }
 
     private void setUpBookmark() {
         // Bookmark a page for the test
-        loadAndPaint(BOOKMARK_URL);
-        toggleBookmark();
-        mAsserter.is(mSolo.waitForText("Bookmark added"), true, "bookmark added successfully");
-
-        // Navigate back to about:home for the test
-        loadAndPaint(ABOUT_HOME_URL);
+        loadUrl(BOOKMARK_URL);
+        waitForText(StringHelper.ROBOCOP_BLANK_PAGE_01_TITLE);
+        nav.bookmark();
+        mAsserter.is(waitForText(StringHelper.BOOKMARK_ADDED_LABEL), true, "bookmark added successfully");
     }
 
     private void cleanUpBookmark() {
         // Go back to the page we bookmarked
-        loadAndPaint(BOOKMARK_URL);
-        toggleBookmark();
-        mAsserter.is(mSolo.waitForText("Bookmark removed"), true, "bookmark removed successfully");
+        loadUrl(BOOKMARK_URL);
+        waitForText(StringHelper.ROBOCOP_BLANK_PAGE_01_TITLE);
+        nav.bookmark();
+        mAsserter.is(waitForText(StringHelper.BOOKMARK_REMOVED_LABEL), true, "bookmark removed successfully");
     }
 }