Bug 823165 - fix robocop tests to work on panda boards. r=gbrown
authorJoel Maher <jmaher@mozilla.com>
Sat, 19 Jan 2013 11:49:47 -0500
changeset 119366 1f42182ac5b3ab58290c861d313fe33f97a470eb
parent 119365 c059c73cd16c2c335bc9a6490a328a55be28eb23
child 119367 6d14b59b7ce51ff109c84253b7fb092aa42e2ad4
push id24197
push userryanvm@gmail.com
push dateSun, 20 Jan 2013 05:25:28 +0000
treeherdermozilla-central@1d122eaa9070 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgbrown
bugs823165
milestone21.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 823165 - fix robocop tests to work on panda boards. r=gbrown
mobile/android/base/tests/BaseTest.java.in
mobile/android/base/tests/testBookmark.java.in
mobile/android/base/tests/testBookmarklets.java.in
mobile/android/base/tests/testBookmarksTab.java.in
--- a/mobile/android/base/tests/BaseTest.java.in
+++ b/mobile/android/base/tests/BaseTest.java.in
@@ -2,30 +2,35 @@
 package @ANDROID_PACKAGE_NAME@.tests;
 
 import com.jayway.android.robotium.solo.Solo;
 import @ANDROID_PACKAGE_NAME@.*;
 
 import android.app.Activity;
 import android.content.pm.ActivityInfo;
 import android.app.Instrumentation;
+import android.content.ContentResolver;
 import android.content.ContentValues;
+import android.content.ContentUris;
 import android.content.Intent;
 import android.content.res.AssetManager;
 import android.database.Cursor;
+import android.net.Uri;
 import android.os.SystemClock;
 import android.test.ActivityInstrumentationTestCase2;
 import java.io.File;
 import java.io.InputStream;
 import java.io.IOException;
 import java.lang.reflect.Method;
 import java.util.HashMap;
 import android.view.View;
 import android.os.Build;
 import android.util.DisplayMetrics;
+import android.widget.ListView;
+import java.util.ArrayList;
 
 /**
  *  A convenient base class suitable for most Robocop tests.
  */
 abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
     public static final int TEST_MOCHITEST = 0;
     public static final int TEST_TALOS = 1;
 
@@ -39,16 +44,17 @@ abstract class BaseTest extends Activity
     protected Solo mSolo;
     protected Driver mDriver;
     protected Assert mAsserter;
     protected Actions mActions;
     protected String mBaseUrl;
     protected String mRawBaseUrl;
     private String mLogFile;
     protected String mProfile;
+    private static ListView bookmarkList;
 
     protected void blockForGeckoReady() {
         try {
             Actions.EventExpecter geckoReadyExpector = mActions.expectGeckoEvent("Gecko:Ready");
             ClassLoader classLoader = getActivity().getClassLoader();
             Class appsCls = classLoader.loadClass("org.mozilla.gecko.GeckoApp");
             Class launchStateCls = classLoader.loadClass("org.mozilla.gecko.GeckoApp$LaunchState");
             Method checkLaunchState =  appsCls.getMethod("checkLaunchState", launchStateCls);
@@ -381,16 +387,102 @@ abstract class BaseTest extends Activity
     public final void verifyTabCount(int expectedTabCount) {
         Activity activity = getActivity();
         Element tabCount = mDriver.findElement(activity, "tabs_count");
         String tabCountText = tabCount.getText();
         int tabCountInt = Integer.parseInt(tabCountText);
         mAsserter.is(tabCountInt, expectedTabCount, "The correct number of tabs are opened");
     }
 
+    public ListView getBookmarksList() {
+        Activity awesomeBarActivity = clickOnAwesomeBar();
+        mSolo.clickOnText("Bookmarks");
+
+        final ArrayList<ListView> views = mSolo.getCurrentListViews();
+
+        bookmarkList = null;
+        boolean success = waitForTest(new BooleanTest() {
+            public boolean test() {
+               for (ListView view : views) {
+                   if (view.getTag().equals("bookmarks")) {
+                       bookmarkList = view;
+                       return true;
+                   }
+               }
+               return false;
+            }
+        }, MAX_WAIT_MS);
+        return bookmarkList;
+    }
+
+    public long addOrUpdateBookmark(String title, String url, boolean bookmarklet) {
+        ContentResolver resolver = getActivity().getContentResolver();
+        Uri bookmarksUri = Uri.parse("content://@ANDROID_PACKAGE_NAME@.db.browser/bookmarks");
+        bookmarksUri = bookmarksUri.buildUpon().appendQueryParameter("profile", "default").build();
+        long folderId = -1;
+        Cursor c = null;
+
+        // NOTE: this is hardcoded to toolbar/mobile and triggered from bookmarklet because the existing
+        //       testcases were written this way
+        String location = "toolbar";
+        if (bookmarklet) {
+            location = "mobile";
+        }
+
+        try {
+            c = resolver.query(bookmarksUri,
+                               new String[] { "_id" },
+                               "guid = ?",
+                               new String[] { location },
+                               null);
+
+            if (c.moveToFirst())
+                folderId = c.getLong(c.getColumnIndexOrThrow("_id"));
+        } finally {
+            if (c != null) {
+                c.close();
+            }
+        }
+
+        ContentValues values = new ContentValues();
+        values.put("title", title);
+        values.put("url", url);
+        values.put("parent", folderId);
+        long now = System.currentTimeMillis();
+        values.put("modified", now);
+
+        if (!bookmarklet) {
+            values.put("type", 1);
+            values.put("guid", url);
+            values.put("position", 10);
+            values.put("created", now);
+        }
+
+        int updated = resolver.update(bookmarksUri,
+                                      values,
+                                      "url = ?",
+                                      new String[] { url });
+        if (updated == 0) {
+            Uri uri = resolver.insert(bookmarksUri, values);
+            mAsserter.ok(true, "Inserted at: ", uri.toString());
+            return ContentUris.parseId(uri);
+        }
+
+        return ContentUris.parseId(bookmarksUri);
+    }
+
+    public void deleteBookmark(String title) {
+
+        ContentResolver resolver = getActivity().getContentResolver();
+        Uri uri = Uri.parse("content://@ANDROID_PACKAGE_NAME@.db.browser/bookmarks");
+        uri = uri.buildUpon().appendQueryParameter("profile", "default")
+                             .appendQueryParameter("sync", "true").build();
+        resolver.delete(uri, "title = ?", new String[] { title });
+    }
+
     public void addTab(String url) {
         Element tabs = null;
         Element addTab = null;
 
         Activity activity = getActivity();
         tabs = mDriver.findElement(activity, "tabs");
         addTab = mDriver.findElement(activity, "add_tab");
         final int addTabId = addTab.getId();
--- a/mobile/android/base/tests/testBookmark.java.in
+++ b/mobile/android/base/tests/testBookmark.java.in
@@ -14,17 +14,16 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.lang.reflect.Method;
 import android.content.ContentResolver;
 
 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 ListView mList;
     private ClassLoader mClassLoader;
     private Method mAddBookmark;
     private Method mRemoveBookmark;
     private Method mIsBookmarked;
     private String[] defaultBookmarks = new String[] {
         "about:firefox",
         "about:home",
         "http://support.mozilla.org/en-US/products/mobile",
@@ -72,17 +71,17 @@ public class testBookmark extends PixelT
     }
 
     public void runAwesomeScreenTest() {
         final long PAINT_CLEAR_DELAY = 1000;  // milliseconds
 
         blockForGeckoReady();
 
         // Open the bookmark list and check the root folder view
-        ListView bookmarksList = openBookmarksList();
+        ListView bookmarksList = getBookmarksList();
 
         // Wait for bookmark to appear in list
         mSolo.waitForText(ABOUT_HOME_URL);
 
         mAsserter.ok(bookmarksList != null, "checking that bookmarks list exists", "bookmarks list exists");
 
         // No folders should be visible if no desktop bookmarks exist
         mAsserter.is(bookmarksList.getAdapter().getCount(), 4,
@@ -150,38 +149,16 @@ public class testBookmark extends PixelT
             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");
         }
     }
 
-    private ListView openBookmarksList() {
-        Activity awesomeBarActivity = clickOnAwesomeBar();
-
-        // Click the "Bookmarks" tab to switch to bookmarks list
-        mSolo.clickOnText("Bookmarks");
-
-        final ArrayList<ListView> views = mSolo.getCurrentListViews();
-        mList = null;
-        boolean success = waitForTest(new BooleanTest() {
-            public boolean test() {
-               for (ListView view : views) {
-                   if (view.getTag().equals("bookmarks")) {
-                       mList = view;
-                       return true;
-                   }
-               }
-               return false;
-            }
-        }, MAX_WAIT_MS);
-        return mList;
-    }
-
     // This method opens the menu and selects the "Bookmark" menu item
     private void toggleBookmark() {
         getInstrumentation().waitForIdleSync();
         mActions.sendSpecialKey(Actions.SpecialKey.MENU);
         mSolo.waitForText("Bookmark");
         mSolo.clickOnText("Bookmark");
     }
 
--- a/mobile/android/base/tests/testBookmarklets.java.in
+++ b/mobile/android/base/tests/testBookmarklets.java.in
@@ -1,18 +1,19 @@
 #filter substitution
 package @ANDROID_PACKAGE_NAME@.tests;
 
 import @ANDROID_PACKAGE_NAME@.*;
 import android.app.Activity;
-import android.content.ContentValues;
-import android.content.ContentResolver;
 import android.database.Cursor;
-import android.net.Uri;
-import android.provider.Browser;
+
+import android.view.View;
+import android.widget.ListView;
+import java.util.ArrayList;
+
 
 public class testBookmarklets extends PixelTest {
     @Override
     protected int getTestType() {
         return TEST_MOCHITEST;
     }
 
     public void testBookmarklets() {
@@ -33,63 +34,44 @@ public class testBookmarklets extends Pi
             public boolean test() {
                 return mSolo.searchButton("OK", true) || mSolo.searchText("12.34", true);
             }
         }, 3000);
         mAsserter.is(alerted, false, "Alert was not shown for user-entered bookmarklet");
 
         // add the bookmarklet to the database. there's currently no way to
         // add this using the UI, so we go through the content provider.
-        addOrUpdateBookmarklet(title, js);
+        addOrUpdateBookmark(title, js, true);
 
         // verify that bookmarklets clicked in awesomescreen work
-        Activity awesomeBarActivity = clickOnAwesomeBar();
-        mActions.sendSpecialKey(Actions.SpecialKey.RIGHT);
-        getInstrumentation().waitForIdleSync();
-        mSolo.clickOnText(title);
+        ListView bookmarks = getBookmarksList();
+
+        Boolean found = false;
+        if (bookmarks == null) {
+            mAsserter.is(true, true, "Did not find the bookmarks section in the awesomebar");
+        } else if (mSolo.waitForText(title)) {
+            for (int i = 0; i < bookmarks.getChildCount(); i++) {
+                Cursor c = (Cursor)bookmarks.getItemAtPosition(i);
+                String turl = c.getString(c.getColumnIndexOrThrow("url"));
+                if (turl.equals(js)) {
+                    found = true;
+                    mAsserter.is(1, 1, "Found bookmarklet added to bookmarks: " + js);
+                    mSolo.clickOnView(bookmarks.getChildAt(i));
+                }
+            }
+        } else {
+            mAsserter.is(true, true, "Did not find the title '" + title + "' of the bookmark in the list");
+        }
+        if (!found) {
+            mAsserter.is(found, true, "Found the bookmark: " + js + " and clicked on it");
+        }
+
         alerted = waitForTest(new BooleanTest() {
             public boolean test() {
                 return mSolo.searchButton("OK", true) && mSolo.searchText("12.34", true);
             }
         }, 3000);
         mAsserter.is(alerted, true, "Alert was shown for clicked bookmarklet");
 
         // remove the bookmarklet
-        removeBookmarklet(js);
-    }
-
-    private void addOrUpdateBookmarklet(String title, String url) {
-        ContentResolver resolver = getActivity().getContentResolver();
-        Uri bookmarksUri = Uri.parse("content://@ANDROID_PACKAGE_NAME@.db.browser/bookmarks");
-        bookmarksUri = bookmarksUri.buildUpon().appendQueryParameter("profile", "default").build();
-        long mobileFolderId = -1;
-
-        Cursor c = resolver.query(bookmarksUri,
-                                  new String[] { "_id" },
-                                  "guid = ?",
-                                  new String[] { "mobile" },
-                                  null);
-        if (c.moveToFirst())
-            mobileFolderId = c.getLong(c.getColumnIndexOrThrow("_id"));
-        c.close();
-
-        ContentValues values = new ContentValues();
-        values.put(Browser.BookmarkColumns.TITLE, title);
-        values.put("url", url);
-        values.put("parent", mobileFolderId);
-        values.put("modified", System.currentTimeMillis());
-
-        int updated = resolver.update(bookmarksUri,
-                                      values,
-                                      "url = ?",
-                                      new String[] { url });
-        if (updated == 0)
-            resolver.insert(bookmarksUri, values);
-    }
-
-    private void removeBookmarklet(String url) {
-        ContentResolver resolver = getActivity().getContentResolver();
-        Uri bookmarksUri = Uri.parse("content://@ANDROID_PACKAGE_NAME@.db.browser/bookmarks");
-        bookmarksUri = bookmarksUri.buildUpon().appendQueryParameter("profile", "default").build();
-
-        resolver.delete(bookmarksUri, "url = ?", new String[] { url });
+        deleteBookmark(js);
     }
 }
--- a/mobile/android/base/tests/testBookmarksTab.java.in
+++ b/mobile/android/base/tests/testBookmarksTab.java.in
@@ -1,61 +1,54 @@
 #filter substitution
 package @ANDROID_PACKAGE_NAME@.tests;
 
 import @ANDROID_PACKAGE_NAME@.*;
 import android.app.Activity;
-import android.content.ContentUris;
 import android.view.ViewGroup;
 import android.view.View;
 import android.widget.ListView;
 import android.widget.ExpandableListView;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 import android.widget.ImageView;
 import android.widget.TabHost;
 import android.text.TextUtils;
 
-import android.content.ContentValues;
-import android.content.ContentResolver;
-import android.database.Cursor;
-import android.net.Uri;
-
 import java.util.Arrays;
 import java.util.ArrayList;
 import java.io.File;
 
 /* Tests opening the bookmarks tab, that items look correct, clicking on an item
    and long tapping on an item
 */
 
 public class testBookmarksTab extends BaseTest {
     private View mFirstChild;
+    private ListView list;
 
     @Override
     protected int getTestType() {
         return TEST_MOCHITEST;
     }
 
     public void testBookmarksTab() {
         blockForGeckoReady();
         String url = "http://www.example.com";
 
         // add one page to desktop folders so that we can see them
-        addBookmark("BOOKMARK_TITLE", url);
+        addOrUpdateBookmark("BOOKMARK_TITLE", url, false);
 
         testList(url);
         testContextMenu(url);
     }
 
-    private ListView list;
-
     private void testList(String url) {
         View child;
-        list = getBookmarksList();
+        ListView list = getBookmarksList();
 
         mSolo.waitForText("Desktop Bookmarks");
         mAsserter.isnot(list, null, "checking that bookmarks list exists");
 
         int count = list.getChildCount();
         mAsserter.is(count, 5, "bookmarks list has 5 children (defaults + a folder)");
  
         for (int i = count - 1; i >= 0; i--) {
@@ -185,78 +178,13 @@ public class testBookmarksTab extends Ba
         visible = 0;
         for (int j = 0; j < textViews.size(); j++) {
             TextView text = textViews.get(j);
             visible += (text.getVisibility() == View.VISIBLE) ? 1 : 0;
         }
         mAsserter.is(textViews.size(), numTextViews, "Correct number of TextViews visible");
     }
 
-    private ListView getBookmarksList() {
-        Activity awesomeBarActivity = clickOnAwesomeBar();
-        mSolo.clickOnText("Bookmarks");
-
-        final ArrayList<ListView> views = mSolo.getCurrentListViews();
-
-        list = null;
-        boolean success = waitForTest(new BooleanTest() {
-            public boolean test() {
-               for (ListView view : views) {
-                   if (view.getTag().equals("bookmarks")) {
-                       list = view;
-                       return true;
-                   }
-               }
-               return false;
-            }
-        }, MAX_WAIT_MS);
-        return list;
-    }
-
-    private long addBookmark(String title, String url) {
-        ContentResolver resolver = getActivity().getContentResolver();
-        Uri bookmarksUri = Uri.parse("content://@ANDROID_PACKAGE_NAME@.db.browser/bookmarks");
-        bookmarksUri = bookmarksUri.buildUpon().appendQueryParameter("profile", "default").build();
-
-        long folderId = -1;
-        Cursor c = null;
-        try {
-            c = resolver.query(bookmarksUri,
-                               new String[] { "_id" },
-                               "guid = ?",
-                               new String[] { "toolbar" },
-                               null);
-
-            if (c.moveToFirst())
-                folderId = c.getLong(c.getColumnIndexOrThrow("_id"));
-        } finally {
-            if (c != null)
-                c.close();
-        }
-
-        ContentValues values = new ContentValues();
-        values.put("title", title);
-        values.put("url", url);
-        values.put("type", 1);
-        values.put("parent", folderId);
-        values.put("guid", url);
-        values.put("position", 10);
-        long now = System.currentTimeMillis();
-        values.put("created", now);
-        values.put("modified", now);
-
-        Uri uri = resolver.insert(bookmarksUri, values);
-        mAsserter.ok(true, "Inserted at: ", uri.toString());
-        return ContentUris.parseId(uri);
-    }
-
     public void tearDown() throws Exception {
-        ContentResolver resolver = getActivity().getContentResolver();
-        Uri uri = Uri.parse("content://@ANDROID_PACKAGE_NAME@.db.browser/bookmarks");
-        uri = uri.buildUpon().appendQueryParameter("profile", "default")
-                             .appendQueryParameter("sync", "true").build();
-        resolver.delete(uri, "title = ?", new String[] {
-            "BOOKMARK_TITLE"
-        });
-
+        deleteBookmark("BOOKMARK_TITLE");
         super.tearDown();
     }
 }