gecko-dev/mobile/android/base/tests/testBookmark.java.in

174 lines
7.2 KiB
Java

#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;
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;
private String[] defaultBookmarks = new String[] {
"about:firefox",
"about:home",
"http://support.mozilla.org/en-US/products/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 = getBookmarksList(ABOUT_HOME_URL, 4);
mAsserter.ok(bookmarksList != null, "checking that bookmarks list exists", "bookmarks list exists and has 4 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(defaultBookmarks, url) > -1,
"Find default bookmark", "Default bookmark for " + url + " found");
}
insertOneBookmark();
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);
mAsserter.ok(child != null, "list item can be retrieved", child != null ? child.toString() : "null!");
mSolo.clickOnView(child);
paintExpecter.blockUntilClear(PAINT_CLEAR_DELAY);
paintExpecter.unregisterListener();
// Clean up the bookmark we created
deleteBookmark();
}
private boolean waitForBookmarked(final boolean isBookmarked) {
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 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");
}
}
// This method opens the menu and selects the "Bookmark" menu item
private void toggleBookmark() {
getInstrumentation().waitForIdleSync();
mActions.sendSpecialKey(Actions.SpecialKey.MENU);
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");
}
}