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

279 lines
10 KiB
Java

#filter substitution
package @ANDROID_PACKAGE_NAME@.tests;
import @ANDROID_PACKAGE_NAME@.*;
import android.app.Activity;
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 java.util.Arrays;
import java.util.ArrayList;
import java.io.File;
/**
* Tests the Bookamrks Tab
* - opening the bookmarks tab
* - items look correct
* - clicking on an item
* - long tapping on an item
* - editing the name, url and keyword of a bookmark from the context menu
* - removing a bookmark
*/
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
addOrUpdateBookmark("BOOKMARK_TITLE", url, false);
testList(url);
testContextMenu(url);
}
private void testList(String url) {
View child;
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--) {
child = list.getChildAt(i);
compareRow(child, i == 0 ? 1 : 2, 1);
}
child = list.getChildAt(0);
mSolo.clickOnView(child);
mSolo.waitForText("Bookmarks Toolbar");
mAsserter.is(list.getChildCount(), 4, "desktop folder has correct number of children");
count = list.getChildCount();
for (int i = count - 1; i >= 0; i--) {
child = list.getChildAt(i);
compareRow(child, 1, i == 0 ? 0 : 1);
}
child = list.getChildAt(1);
mSolo.clickOnView(child);
mSolo.waitForText("BOOKMARK_TITLE");
mAsserter.is(list.getChildCount(), 2, "toolbar folder has correct number of children");
count = list.getChildCount();
for (int i = count - 1; i >= 0; i--) {
child = list.getChildAt(i);
compareRow(child, i == 0 ? 1:2, i == 0 ? 0:1);
}
// Test backing out of the folder using the back button
mActions.sendSpecialKey(Actions.SpecialKey.BACK);
mAsserter.ok(mSolo.waitForText("Bookmarks Toolbar"), "Back moved up one level", "");
mActions.sendSpecialKey(Actions.SpecialKey.BACK);
mAsserter.ok(mSolo.waitForText("about:home"), "Back moved up one level", "");
mActions.sendSpecialKey(Actions.SpecialKey.BACK);
}
private void testContextMenu(String url) {
list = getBookmarksList();
mSolo.waitForText(url);
// wait for the bookmarks list to be populated
View child;
mFirstChild = null;
boolean success = waitForTest(new BooleanTest() {
public boolean test() {
mFirstChild = list.getChildAt(1);
if (mFirstChild == null) {
return false;
}
if (mFirstChild instanceof android.view.ViewGroup) {
ViewGroup group = (ViewGroup)mFirstChild;
if (group.getChildCount() < 1) {
return false;
}
for (int i = 0; i < group.getChildCount(); i++) {
View grandChild = group.getChildAt(i);
if (grandChild instanceof android.widget.TextView) {
mAsserter.ok(true, "found TextView:", ((android.widget.TextView)grandChild).getText().toString());
}
}
} else {
mAsserter.dumpLog("first child not a ViewGroup: "+mFirstChild);
return false;
}
return true;
}
}, MAX_WAIT_MS);
if (success == true && mFirstChild != null) {
mAsserter.dumpLog("clickLongOnView: "+mFirstChild);
// long tap on a bookmark should show a context menu with an edit option
mSolo.clickLongOnView(mFirstChild);
// TODO: Test clicking these does the right thing
mAsserter.ok(mSolo.waitForText("Open in New Tab"), "Context menu has New Tab option", "Open in New Tab");
mAsserter.ok(mSolo.searchText("Share", true), "Context menu has Share option", "Share");
mAsserter.ok(mSolo.searchText("Edit", true), "Context menu has Edit option", "Edit");
mAsserter.ok(mSolo.searchText("Remove", true), "Context menu has Remove option", "Remove");
mAsserter.ok(mSolo.searchText("Add to Home Screen", true), "Context menu has Add to Home Screen option", "Add to Home Screen");
// press back to exit the context menu
mActions.sendSpecialKey(Actions.SpecialKey.BACK);
// test long tap on a folder, this should fail but will still open the folder
child = list.getChildAt(0);
mSolo.clickLongOnView(child);
mAsserter.is(mSolo.waitForText("Open in New Tab"), false, "Folders have no context menu");
} else {
mAsserter.ok(false, "waiting for bookmark item", "bookmark item available");
}
list = getBookmarksList();
// Test edit bookmark name
editBookmark(1,0," Bookmark Name ");
mAsserter.is(checkBookmarkEdit(1," Bookmark Name "), true, "Bookmark Name was changed");
// Test edit bookmark link
editBookmark(1,1," Bookmark Link ");
mAsserter.is(checkBookmarkEdit(1,"Bookmark Link"), true, "Bookmark Link was changed");
// Test edit bookmark keyword
editBookmark(1,2," Bookmark Keyword ");
mAsserter.is(checkBookmarkEdit(1,"Bookmark Keyword"), true, "Bookmark Keyword was changed");
// Remove Bookmark from Context Menu
mSolo.waitForText("Bookmarks");
child = list.getChildAt(1);
mSolo.clickLongOnView(child);
mSolo.waitForText("Open in New Tab");
mSolo.clickOnText("Remove");
// Wait for the toaster notification
mSolo.waitForText("Bookmark Removed");
// Verify Bookmark is removed
child = list.getChildAt(1);
mSolo.clickLongOnView(child);
mSolo.waitForText("Open in New Tab");
mAsserter.is(mSolo.searchText("Bookmark Name"), false, "Removed bookmark has been deleted");
mActions.sendSpecialKey(Actions.SpecialKey.BACK); // Exit the Context Menu
// back again to exit the awesomebar
mActions.sendSpecialKey(Actions.SpecialKey.BACK);
}
private void editBookmark(int bookmarkIndex, int fieldIndex, String addedText) {
// Open the Edit Bookmark context menu
View child;
mSolo.clickOnText("Bookmarks");
child = list.getChildAt(bookmarkIndex);
mSolo.waitForText("Desktop Bookmarks");
mSolo.clickLongOnView(child);
mSolo.waitForText("Open in New Tab");
mSolo.clickOnText("Edit");
mSolo.waitForText("Edit Bookmark");
// Clear the Field
mSolo.clearEditText(fieldIndex);
// Enter the new text
mSolo.clickOnEditText(fieldIndex);
mActions.sendKeys(addedText);
mSolo.clickOnText("OK");
mSolo.waitForText("Bookmark Updated");
mActions.sendSpecialKey(Actions.SpecialKey.BACK); // close the VKB
}
private boolean checkBookmarkEdit(int bookmarkIndex, String addedText) {
Device mDevice = new Device();
// Open the Edit Bookmark context menu
View child;
mSolo.clickOnText("Bookmarks");
child = list.getChildAt(bookmarkIndex);
mSolo.waitForText("Desktop Bookmarks");
mSolo.clickLongOnView(child);
mSolo.waitForText("Open in New Tab");
mSolo.clickOnText("Edit");
mSolo.waitForText("Edit Bookmark");
// If the OS is not Gingerbread the vkb will be opened so we need to close it in order to press the "Cancel" button
if (!(mDevice.version.equals("2.x"))) {
mActions.sendSpecialKey(Actions.SpecialKey.BACK); // close the VKB
}
// Check if the new text was added
if (mSolo.searchText(addedText)) {
mSolo.clickOnText("Cancel");
mSolo.waitForText("Desktop Bookmarks");
mActions.sendSpecialKey(Actions.SpecialKey.BACK); // close the VKB
return true;
} else {
mSolo.clickOnText("Cancel");
mSolo.waitForText("Desktop Bookmarks");
mActions.sendSpecialKey(Actions.SpecialKey.BACK); // close the VKB
return false;
}
}
private void compareRow(View child, int numTextViews, int numImageViews) {
ArrayList<View> views = mSolo.getViews(child);
ArrayList<ImageView> imageViews = new ArrayList<ImageView>();
ArrayList<TextView> textViews = new ArrayList<TextView>();
for (int j = 0; j < views.size(); j++) {
View v = views.get(j);
if (v instanceof TextView) {
TextView t = (TextView)v;
textViews.add(t);
String string = t.getText().toString();
mAsserter.ok(!TextUtils.isEmpty(string), "TextView is filled in", string);
} else if (v instanceof ImageView) {
imageViews.add((ImageView)v);
}
}
int visible = 0;
for (int j = 0; j < imageViews.size(); j++) {
ImageView img = imageViews.get(j);
visible += (img.getVisibility() == View.VISIBLE) ? 1 : 0;
}
mAsserter.is(visible, numImageViews, "Correct number of ImageViews visible");
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");
}
public void tearDown() throws Exception {
deleteBookmark("BOOKMARK_TITLE");
super.tearDown();
}
}