OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.ntp; |
| 6 |
| 7 import android.test.suitebuilder.annotation.LargeTest; |
| 8 import android.text.TextUtils; |
| 9 import android.widget.EditText; |
| 10 import android.widget.LinearLayout; |
| 11 import android.widget.ListView; |
| 12 |
| 13 import com.google.android.apps.chrome.R; |
| 14 |
| 15 import org.chromium.base.ThreadUtils; |
| 16 import org.chromium.base.test.util.DisabledTest; |
| 17 import org.chromium.chrome.browser.BookmarksBridge.BookmarkItem; |
| 18 import org.chromium.chrome.browser.Tab; |
| 19 import org.chromium.chrome.browser.UrlConstants; |
| 20 import org.chromium.chrome.browser.bookmark.AddEditBookmarkFragment; |
| 21 import org.chromium.chrome.browser.bookmark.ManageBookmarkActivity; |
| 22 import org.chromium.chrome.browser.bookmark.SelectBookmarkFolderFragment; |
| 23 import org.chromium.chrome.test.ChromeTabbedActivityTestBase; |
| 24 import org.chromium.chrome.test.util.ActivityUtils; |
| 25 import org.chromium.chrome.test.util.BookmarkTestUtils; |
| 26 import org.chromium.chrome.test.util.ChromeTabUtils; |
| 27 import org.chromium.chrome.test.util.TestHttpServerClient; |
| 28 import org.chromium.content.browser.test.util.Criteria; |
| 29 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 30 import org.chromium.content.browser.test.util.TestTouchUtils; |
| 31 import org.chromium.content.browser.test.util.TouchCommon; |
| 32 import org.chromium.content.browser.test.util.UiUtils; |
| 33 |
| 34 import java.util.concurrent.Callable; |
| 35 |
| 36 /** |
| 37 * Tests for the old bookmarks page. |
| 38 */ |
| 39 public class BookmarksPageTest extends ChromeTabbedActivityTestBase { |
| 40 |
| 41 private static final String TEST_PAGE = |
| 42 TestHttpServerClient.getUrl("chrome/test/data/android/about.html"); |
| 43 private static final String TEST_PAGE_TITLE = "About"; |
| 44 private static final String TEST_FOLDER_TITLE = "Test Folder"; |
| 45 private static final String TEST_PAGE_TITLE_2 = "About 2"; |
| 46 private static final String MOBILE_BOOKMARKS_TITLE = "Mobile bookmarks"; |
| 47 private static final String BOOKMARKS_TITLE = "Bookmarks"; |
| 48 |
| 49 private ListView mBookmarksList; |
| 50 private LinearLayout mHierarchyLayout; |
| 51 |
| 52 @Override |
| 53 public void startMainActivity() throws InterruptedException { |
| 54 startMainActivityOnBlankPage(); |
| 55 } |
| 56 |
| 57 private void addBookmark() throws InterruptedException { |
| 58 loadUrl(TEST_PAGE); |
| 59 BookmarkTestUtils.addCurrentUrlAsBookmark(this, getActivity()); |
| 60 loadMobileBookmarksPage(); |
| 61 } |
| 62 |
| 63 private void addFolderAndAddBookmark() throws InterruptedException { |
| 64 loadUrl(TEST_PAGE); |
| 65 ManageBookmarkActivity addActivity = BookmarkTestUtils.selectBookmarkIte
mFromMenu( |
| 66 getInstrumentation(), getActivity()); |
| 67 final AddEditBookmarkFragment addFragment = |
| 68 BookmarkTestUtils.loadAddEditFragment(addActivity); |
| 69 |
| 70 BookmarkTestUtils.clickSelectFolderButton(this, addFragment); |
| 71 SelectBookmarkFolderFragment selectedFolder = BookmarkTestUtils.loadSele
ctFragment( |
| 72 addActivity); |
| 73 |
| 74 BookmarkTestUtils.clickNewFolderButton(this, selectedFolder); |
| 75 final AddEditBookmarkFragment addNewFolderFragment = |
| 76 BookmarkTestUtils.loadAddFolderFragment(addActivity); |
| 77 |
| 78 getInstrumentation().runOnMainSync(new Runnable() { |
| 79 @Override |
| 80 public void run() { |
| 81 ((EditText) addNewFolderFragment.getView().findViewById(R.id.boo
kmark_title_input)) |
| 82 .setText(TEST_FOLDER_TITLE); |
| 83 } |
| 84 }); |
| 85 BookmarkTestUtils.clickOkButton(this, addNewFolderFragment); |
| 86 BookmarkTestUtils.clickOkButton(this, addFragment); |
| 87 loadMobileBookmarksPage(); |
| 88 } |
| 89 |
| 90 private void loadMobileBookmarksPage() throws InterruptedException { |
| 91 final String mobileFolderUrl = UrlConstants.BOOKMARKS_FOLDER_URL + "2"; |
| 92 loadUrl(mobileFolderUrl); |
| 93 Tab tab = getActivity().getActivityTab(); |
| 94 assertTrue(tab.getNativePage() instanceof BookmarksPage); |
| 95 mHierarchyLayout = (LinearLayout) getActivity().findViewById( |
| 96 R.id.bookmark_folder_structure); |
| 97 mBookmarksList = (ListView) getActivity().findViewById(R.id.bookmarks_li
st_view); |
| 98 } |
| 99 |
| 100 private void openBookmarkInCurrentTab(final BookmarkItemView itemView) |
| 101 throws InterruptedException { |
| 102 ChromeTabUtils.waitForTabPageLoaded(getActivity().getActivityTab(), new
Runnable() { |
| 103 @Override |
| 104 public void run() { |
| 105 TouchCommon.singleClickView(itemView); |
| 106 } |
| 107 }); |
| 108 BookmarkTestUtils.assertUrlBarEquals( |
| 109 getActivity(), "urlBar string not matching the bookmarked page",
TEST_PAGE); |
| 110 } |
| 111 |
| 112 private void addBookmarkAndLongClickForContextMenu() throws InterruptedExcep
tion { |
| 113 addBookmark(); |
| 114 BookmarkItemView itemView = (BookmarkItemView) BookmarkTestUtils.getView
WithText( |
| 115 mBookmarksList, TEST_PAGE_TITLE); |
| 116 TouchCommon.longPressView(itemView, itemView.getWidth() / 2, itemView.ge
tHeight() / 2); |
| 117 } |
| 118 |
| 119 private String getCurrentFolderTitle() { |
| 120 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<String>
() { |
| 121 @Override |
| 122 public String call() throws Exception { |
| 123 return ((BookmarkFolderHierarchyItem) mHierarchyLayout.getChildA
t( |
| 124 mHierarchyLayout.getChildCount() - 1)).getText().toStrin
g(); |
| 125 } |
| 126 }); |
| 127 } |
| 128 |
| 129 private void clickFolderInFolderHierarchy(final String folderToSelect) |
| 130 throws InterruptedException { |
| 131 CriteriaHelper.pollForCriteria(new Criteria() { |
| 132 @Override |
| 133 public boolean isSatisfied() { |
| 134 return (BookmarkTestUtils.getViewWithText(mHierarchyLayout, |
| 135 folderToSelect) != null); |
| 136 } |
| 137 }); |
| 138 final BookmarkFolderHierarchyItem itemView = |
| 139 (BookmarkFolderHierarchyItem) BookmarkTestUtils.getViewWithText( |
| 140 mHierarchyLayout, folderToSelect); |
| 141 TouchCommon.singleClickView(itemView); |
| 142 assertEquals(folderToSelect, getCurrentFolderTitle()); |
| 143 } |
| 144 |
| 145 private void clickFolderInBookmarksList(final String folderToSelect) |
| 146 throws InterruptedException { |
| 147 CriteriaHelper.pollForCriteria(new Criteria() { |
| 148 @Override |
| 149 public boolean isSatisfied() { |
| 150 return (BookmarkTestUtils.getViewWithText(mBookmarksList, folder
ToSelect) != null); |
| 151 } |
| 152 }); |
| 153 final BookmarkItemView itemView = (BookmarkItemView) BookmarkTestUtils.g
etViewWithText( |
| 154 mBookmarksList, folderToSelect); |
| 155 TouchCommon.singleClickView(itemView); |
| 156 assertEquals(folderToSelect, getCurrentFolderTitle()); |
| 157 } |
| 158 |
| 159 private boolean isItemPresentInBookmarksList(final String expectedTitle) { |
| 160 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Boolean
>() { |
| 161 @Override |
| 162 public Boolean call() throws Exception { |
| 163 for (int i = 0; i < mBookmarksList.getCount(); i++) { |
| 164 String actualTitle = |
| 165 ((BookmarkItem) mBookmarksList.getItemAtPosition(i))
.getTitle(); |
| 166 if (TextUtils.equals(actualTitle, expectedTitle)) { |
| 167 return true; |
| 168 } |
| 169 } |
| 170 return false; |
| 171 } |
| 172 }); |
| 173 } |
| 174 |
| 175 @LargeTest |
| 176 public void testCreateAndOpenBookmark() throws InterruptedException { |
| 177 addBookmark(); |
| 178 // Assert "About" item is listed in the bookmarks list. |
| 179 assertTrue(isItemPresentInBookmarksList(TEST_PAGE_TITLE)); |
| 180 // Click the item "About". |
| 181 openBookmarkInCurrentTab((BookmarkItemView) BookmarkTestUtils.getViewWit
hText( |
| 182 mBookmarksList, TEST_PAGE_TITLE)); |
| 183 } |
| 184 |
| 185 @LargeTest |
| 186 public void testNavigateFoldersInFolderHierarchy() throws InterruptedExcepti
on { |
| 187 addFolderAndAddBookmark(); |
| 188 // Click on "Mobile bookmarks" in the Folder hierarchy. |
| 189 clickFolderInFolderHierarchy(MOBILE_BOOKMARKS_TITLE); |
| 190 // Assert "Test Folder" is listed in the bookmarks list. |
| 191 assertTrue(isItemPresentInBookmarksList(TEST_FOLDER_TITLE)); |
| 192 // Click on "Bookmarks" in the Folder hierarchy. |
| 193 clickFolderInFolderHierarchy(BOOKMARKS_TITLE); |
| 194 // Assert "Desktop Bookmarks" is listed in the bookmarks list. |
| 195 assertTrue(isItemPresentInBookmarksList(MOBILE_BOOKMARKS_TITLE)); |
| 196 } |
| 197 |
| 198 /* |
| 199 @LargeTest |
| 200 |
| 201 Disabled because of repeated flakes on ICS bot. |
| 202 http://crbug.com/384126 |
| 203 */ |
| 204 @DisabledTest |
| 205 public void testNavigateFoldersInBookmarksListView() throws InterruptedExcep
tion { |
| 206 addFolderAndAddBookmark(); |
| 207 // Click on "Bookmarks" in the Folder hierarchy. |
| 208 clickFolderInFolderHierarchy(BOOKMARKS_TITLE); |
| 209 // Assert "Mobile Bookmarks" is listed in the bookmarks list. |
| 210 assertTrue(isItemPresentInBookmarksList(MOBILE_BOOKMARKS_TITLE)); |
| 211 // Click on "Mobile bookmarks" in the bookmarks list view. |
| 212 clickFolderInBookmarksList(MOBILE_BOOKMARKS_TITLE); |
| 213 // Assert "Test Folder" is listed in the bookmarks list. |
| 214 assertTrue(isItemPresentInBookmarksList(TEST_FOLDER_TITLE)); |
| 215 // Click on "Test Folder" in the bookmarks list view. |
| 216 clickFolderInBookmarksList(TEST_FOLDER_TITLE); |
| 217 // Assert "About" is listed in the bookmarks list. |
| 218 assertTrue(isItemPresentInBookmarksList(TEST_PAGE_TITLE)); |
| 219 } |
| 220 |
| 221 @LargeTest |
| 222 public void testContextMenuOptionOpenInANewTab() throws InterruptedException
{ |
| 223 addBookmark(); |
| 224 BookmarkItemView itemView = (BookmarkItemView) BookmarkTestUtils.getView
WithText( |
| 225 mBookmarksList, TEST_PAGE_TITLE); |
| 226 invokeContextMenuAndOpenInANewTab(itemView, BookmarkItemView.ID_OPEN_IN_
NEW_TAB, false, |
| 227 TEST_PAGE); |
| 228 } |
| 229 |
| 230 @LargeTest |
| 231 public void testContextMenuOptionOpenInAnIncognitoTab() throws InterruptedEx
ception { |
| 232 addBookmark(); |
| 233 BookmarkItemView itemView = (BookmarkItemView) BookmarkTestUtils.getView
WithText( |
| 234 mBookmarksList, TEST_PAGE_TITLE); |
| 235 invokeContextMenuAndOpenInANewTab(itemView, BookmarkItemView.ID_OPEN_IN_
INCOGNITO_TAB, true, |
| 236 TEST_PAGE); |
| 237 } |
| 238 |
| 239 @LargeTest |
| 240 public void testContextMenuOptionEditBookmark() throws InterruptedException
{ |
| 241 addBookmarkAndLongClickForContextMenu(); |
| 242 // Invoke the "Edit Bookmark" context menu option. |
| 243 final ManageBookmarkActivity activity = ActivityUtils.waitForActivity( |
| 244 getInstrumentation(), ManageBookmarkActivity.class, |
| 245 new Runnable() { |
| 246 @Override |
| 247 public void run() { |
| 248 getInstrumentation().invokeContextMenuAction( |
| 249 getActivity(), BookmarkItemView.ID_EDIT, 0); |
| 250 } |
| 251 } |
| 252 ); |
| 253 UiUtils.settleDownUI(getInstrumentation()); |
| 254 // Edit the bookmark title. |
| 255 getInstrumentation().runOnMainSync(new Runnable() { |
| 256 @Override |
| 257 public void run() { |
| 258 ((EditText) activity.findViewById(R.id.bookmark_title_input)) |
| 259 .setText(TEST_PAGE_TITLE_2); |
| 260 } |
| 261 }); |
| 262 TestTouchUtils.clickView(this, activity.findViewById(R.id.ok)); |
| 263 // Assert "About 2" is listed in the bookmarks list. |
| 264 assertTrue(isItemPresentInBookmarksList(TEST_PAGE_TITLE_2)); |
| 265 } |
| 266 |
| 267 @LargeTest |
| 268 public void testContextMenuOptionDeleteBookmark() throws InterruptedExceptio
n { |
| 269 addBookmarkAndLongClickForContextMenu(); |
| 270 // Invoke the "Delete Bookmark" context menu option. |
| 271 getInstrumentation().invokeContextMenuAction( |
| 272 getActivity(), BookmarkItemView.ID_DELETE, 0); |
| 273 UiUtils.settleDownUI(getInstrumentation()); |
| 274 // Assert no bookmarks exist in the current folder. |
| 275 assertTrue(mBookmarksList.getCount() == 0); |
| 276 } |
| 277 } |
OLD | NEW |