michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import org.mozilla.gecko.sync.Utils; michael@0: michael@0: import android.content.ContentResolver; michael@0: import android.content.ContentValues; michael@0: import android.net.Uri; michael@0: import android.view.View; michael@0: import android.widget.ListAdapter; michael@0: import android.widget.ListView; michael@0: import android.widget.TextView; michael@0: michael@0: import com.jayway.android.robotium.solo.Condition; michael@0: michael@0: public class testBookmarkFolders extends AboutHomeTest { michael@0: private static String DESKTOP_BOOKMARK_URL; michael@0: michael@0: public void testBookmarkFolders() { michael@0: DESKTOP_BOOKMARK_URL = getAbsoluteUrl(StringHelper.ROBOCOP_BLANK_PAGE_02_URL); michael@0: michael@0: setUpDesktopBookmarks(); michael@0: checkBookmarkList(); michael@0: } michael@0: michael@0: private void checkBookmarkList() { michael@0: openAboutHomeTab(AboutHomeTabs.BOOKMARKS); michael@0: waitForText(StringHelper.DESKTOP_FOLDER_LABEL); michael@0: clickOnBookmarkFolder(StringHelper.DESKTOP_FOLDER_LABEL); michael@0: waitForText(StringHelper.TOOLBAR_FOLDER_LABEL); michael@0: michael@0: // Verify the number of folders displayed in the Desktop Bookmarks folder is correct michael@0: ListView desktopFolderContent = findListViewWithTag("bookmarks"); michael@0: ListAdapter adapter = desktopFolderContent.getAdapter(); michael@0: if (mDevice.type.equals("tablet")) { // On tablets it's 4 folders and 1 view for top padding michael@0: mAsserter.is(adapter.getCount(), 5, "Checking that the correct number of folders is displayed in the Desktop Bookmarks folder"); michael@0: } else { // On phones it's just the 4 folders michael@0: mAsserter.is(adapter.getCount(), 4, "Checking that the correct number of folders is displayed in the Desktop Bookmarks folder"); michael@0: } michael@0: michael@0: clickOnBookmarkFolder(StringHelper.TOOLBAR_FOLDER_LABEL); michael@0: michael@0: // Go up in the bookmark folder hierarchy michael@0: clickOnBookmarkFolder(StringHelper.TOOLBAR_FOLDER_LABEL); michael@0: mAsserter.ok(waitForText(StringHelper.BOOKMARKS_MENU_FOLDER_LABEL), "Going up in the folder hierarchy", "We are back in the Desktop Bookmarks folder"); michael@0: michael@0: clickOnBookmarkFolder(StringHelper.DESKTOP_FOLDER_LABEL); michael@0: mAsserter.ok(waitForText(StringHelper.DESKTOP_FOLDER_LABEL), "Going up in the folder hierarchy", "We are back in the main Bookmarks List View"); michael@0: michael@0: clickOnBookmarkFolder(StringHelper.DESKTOP_FOLDER_LABEL); michael@0: clickOnBookmarkFolder(StringHelper.TOOLBAR_FOLDER_LABEL); michael@0: isBookmarkDisplayed(DESKTOP_BOOKMARK_URL); michael@0: michael@0: // Open the bookmark from a bookmark folder hierarchy michael@0: loadBookmark(DESKTOP_BOOKMARK_URL); michael@0: waitForText(StringHelper.ROBOCOP_BLANK_PAGE_02_TITLE); michael@0: verifyPageTitle(StringHelper.ROBOCOP_BLANK_PAGE_02_TITLE); michael@0: openAboutHomeTab(AboutHomeTabs.BOOKMARKS); michael@0: michael@0: // Check that folders don't have a context menu michael@0: boolean success = waitForCondition(new Condition() { michael@0: @Override michael@0: public boolean isSatisfied() { michael@0: View desktopFolder = getBookmarkFolderView(StringHelper.DESKTOP_FOLDER_LABEL); michael@0: if (desktopFolder == null) { michael@0: return false; michael@0: } michael@0: mSolo.clickLongOnView(desktopFolder); michael@0: return true; } michael@0: }, MAX_WAIT_MS); michael@0: michael@0: mAsserter.ok(success, "Trying to long click on the Desktop Bookmarks","Desktop Bookmarks folder could not be long clicked"); michael@0: michael@0: final String contextMenuString = StringHelper.BOOKMARK_CONTEXT_MENU_ITEMS[0]; michael@0: mAsserter.ok(!waitForText(contextMenuString), "Folders do not have context menus", "The context menu was not opened"); michael@0: michael@0: // Even if no context menu is opened long clicking a folder still opens it. We need to close it. michael@0: clickOnBookmarkFolder(StringHelper.DESKTOP_FOLDER_LABEL); michael@0: } michael@0: michael@0: private void clickOnBookmarkFolder(final String folderName) { michael@0: boolean success = waitForCondition(new Condition() { michael@0: @Override michael@0: public boolean isSatisfied() { michael@0: View bookmarksFolder = getBookmarkFolderView(folderName); michael@0: if (bookmarksFolder == null) { michael@0: return false; michael@0: } michael@0: mSolo.waitForView(bookmarksFolder); michael@0: mSolo.clickOnView(bookmarksFolder); michael@0: return true; michael@0: } michael@0: }, MAX_WAIT_MS); michael@0: mAsserter.ok(success, "Trying to click on the " + folderName + " folder","The " + folderName + " folder was clicked"); michael@0: } michael@0: michael@0: private View getBookmarkFolderView(String folderName) { michael@0: openAboutHomeTab(AboutHomeTabs.BOOKMARKS); michael@0: mSolo.hideSoftKeyboard(); michael@0: getInstrumentation().waitForIdleSync(); michael@0: michael@0: ListView bookmarksTabList = findListViewWithTag("bookmarks"); michael@0: if (!waitForNonEmptyListToLoad(bookmarksTabList)) { michael@0: return null; michael@0: } michael@0: michael@0: ListAdapter adapter = bookmarksTabList.getAdapter(); michael@0: if (adapter == null) { michael@0: return null; michael@0: } michael@0: michael@0: for (int i = 0; i < adapter.getCount(); i++ ) { michael@0: View bookmarkView = bookmarksTabList.getChildAt(i); michael@0: if (bookmarkView instanceof TextView) { michael@0: TextView folderTextView = (TextView) bookmarkView; michael@0: if (folderTextView.getText().equals(folderName)) { michael@0: return bookmarkView; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return null; michael@0: } michael@0: michael@0: // Add a bookmark in the Desktop folder so we can check the folder navigation in the bookmarks page michael@0: private void setUpDesktopBookmarks() { michael@0: blockForGeckoReady(); michael@0: michael@0: // Get the folder id of the StringHelper.DESKTOP_FOLDER_LABEL folder michael@0: Long desktopFolderId = mDatabaseHelper.getFolderIdFromGuid("toolbar"); michael@0: michael@0: // Generate a Guid for the bookmark michael@0: final String generatedGuid = Utils.generateGuid(); michael@0: mAsserter.ok((generatedGuid != null), "Generating a random Guid for the bookmark", "We could not generate a Guid for the bookmark"); michael@0: michael@0: // Insert the bookmark michael@0: ContentResolver resolver = getActivity().getContentResolver(); michael@0: Uri bookmarksUri = mDatabaseHelper.buildUri(DatabaseHelper.BrowserDataType.BOOKMARKS); michael@0: michael@0: long now = System.currentTimeMillis(); michael@0: ContentValues values = new ContentValues(); michael@0: values.put("title", StringHelper.ROBOCOP_BLANK_PAGE_02_TITLE); michael@0: values.put("url", DESKTOP_BOOKMARK_URL); michael@0: values.put("parent", desktopFolderId); michael@0: values.put("modified", now); michael@0: values.put("type", 1); michael@0: values.put("guid", generatedGuid); michael@0: values.put("position", 10); michael@0: values.put("created", now); michael@0: michael@0: int updated = resolver.update(bookmarksUri, michael@0: values, michael@0: "url = ?", michael@0: new String[] { DESKTOP_BOOKMARK_URL }); michael@0: if (updated == 0) { michael@0: Uri uri = resolver.insert(bookmarksUri, values); michael@0: mAsserter.ok(true, "Inserted at: ", uri.toString()); michael@0: } else { michael@0: mAsserter.ok(false, "Failed to insert the Desktop bookmark", "Something went wrong"); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public void tearDown() throws Exception { michael@0: mDatabaseHelper.deleteBookmark(DESKTOP_BOOKMARK_URL); michael@0: super.tearDown(); michael@0: } michael@0: }