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

164 lines
7.0 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;
@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();
/* 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");
}
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)");
// 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 isBookmarked");
} catch(java.lang.reflect.InvocationTargetException ex) {
mAsserter.is(true, false, "Error calling isBookmarked");
}
return false;
}
}, 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");
}
}
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");
}
}