#filter substitution package @ANDROID_PACKAGE_NAME@.tests; import @ANDROID_PACKAGE_NAME@.*; import android.app.Activity; import android.database.Cursor; import android.view.MenuItem; import android.view.View; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.TabHost; 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/mobile", "https://addons.mozilla.org/en-US/android/" }; @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(); 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"); 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"); } } public void runAwesomeScreenTest() { final long PAINT_CLEAR_DELAY = 1000; // milliseconds blockForGeckoReady(); // Open the bookmark list and check the root folder view ListView bookmarksList = openBookmarksList(); // 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, "bookmarks list has 4 children (the default bookmarks)"); for (int i = 0; i < bookmarksList.getChildCount(); i++) { Cursor c = (Cursor)bookmarksList.getItemAtPosition(i); String url = c.getString(c.getColumnIndexOrThrow("url")); mAsserter.ok(Arrays.binarySearch(defaultBookmarks, url) > -1, "Find default bookmark", "Default bookmark for " + url + " found"); } insertOneBookmark(); mSolo.waitForText(BOOKMARK_TITLE); mAsserter.is(bookmarksList.getAdapter().getCount(), 5, "bookmarks list has 5 children (the default bookmarks and the new one)"); // Click on the bookmark we created and wait for the bookmarked page to load Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint(); View child = bookmarksList.getChildAt(1); mSolo.clickOnView(child); paintExpecter.blockUntilClear(PAINT_CLEAR_DELAY); // Clean up the bookmark we created deleteBookmark(); } private boolean waitForBookmarked(final boolean isBookmarked) { waitForTest(new BooleanTest() { 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 addBookmark"); } catch(java.lang.reflect.InvocationTargetException ex) { mAsserter.is(true, false, "Error calling addBookmark"); } return false; } }, MAX_WAIT_MS); 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"); } } private ListView openBookmarksList() { Activity awesomeBarActivity = clickOnAwesomeBar(); // Click the "Bookmarks" tab to switch to bookmarks list mSolo.clickOnText("Bookmarks"); final ArrayList 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"); } 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); } private void cleanUpBookmark() { // Go back to the page we bookmarked loadAndPaint(BOOKMARK_URL); toggleBookmark(); mAsserter.is(mSolo.waitForText("Bookmark removed"), true, "bookmark removed successfully"); } }