| 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.enhancedbookmarks; | |
| 6 | |
| 7 import android.content.Intent; | |
| 8 import android.net.Uri; | |
| 9 import android.test.suitebuilder.annotation.SmallTest; | |
| 10 import android.text.TextUtils; | |
| 11 import android.view.View; | |
| 12 import android.view.ViewGroup; | |
| 13 import android.widget.TextView; | |
| 14 | |
| 15 import junit.framework.Assert; | |
| 16 | |
| 17 import org.chromium.base.ThreadUtils; | |
| 18 import org.chromium.base.test.util.CommandLineFlags; | |
| 19 import org.chromium.chrome.R; | |
| 20 import org.chromium.chrome.browser.ChromeActivity; | |
| 21 import org.chromium.chrome.browser.ChromeSwitches; | |
| 22 import org.chromium.chrome.browser.UrlConstants; | |
| 23 import org.chromium.chrome.browser.bookmark.BookmarksBridge.BookmarkItem; | |
| 24 import org.chromium.chrome.browser.bookmark.BookmarksBridge.BookmarkModelObserve
r; | |
| 25 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | |
| 26 import org.chromium.chrome.test.util.ActivityUtils; | |
| 27 import org.chromium.chrome.test.util.ChromeTabUtils; | |
| 28 import org.chromium.chrome.test.util.MenuUtils; | |
| 29 import org.chromium.chrome.test.util.TestHttpServerClient; | |
| 30 import org.chromium.components.bookmarks.BookmarkId; | |
| 31 import org.chromium.components.bookmarks.BookmarkType; | |
| 32 import org.chromium.content.browser.test.util.CallbackHelper; | |
| 33 import org.chromium.content.browser.test.util.TouchCommon; | |
| 34 import org.chromium.ui.base.DeviceFormFactor; | |
| 35 | |
| 36 import java.util.ArrayList; | |
| 37 import java.util.concurrent.Callable; | |
| 38 import java.util.concurrent.TimeoutException; | |
| 39 | |
| 40 /** | |
| 41 * Tests for the enhanced bookmark manager. | |
| 42 */ | |
| 43 public class EnhancedBookmarkTest extends ChromeActivityTestCaseBase<ChromeActiv
ity> { | |
| 44 | |
| 45 public EnhancedBookmarkTest() { | |
| 46 super(ChromeActivity.class); | |
| 47 } | |
| 48 | |
| 49 private static final String TEST_PAGE = TestHttpServerClient.getUrl( | |
| 50 "chrome/test/data/android/google.html"); | |
| 51 private static final String TEST_PAGE_TITLE = "The Google"; | |
| 52 | |
| 53 private EnhancedBookmarksModel mBookmarkModel; | |
| 54 protected EnhancedBookmarkRecyclerView mItemsContainer; | |
| 55 | |
| 56 @Override | |
| 57 public void startMainActivity() throws InterruptedException { | |
| 58 startMainActivityFromLauncher(); | |
| 59 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
| 60 @Override | |
| 61 public void run() { | |
| 62 mBookmarkModel = new EnhancedBookmarksModel( | |
| 63 getActivity().getActivityTab().getProfile()); | |
| 64 } | |
| 65 }); | |
| 66 waitForBookmarkModelLoaded(); | |
| 67 } | |
| 68 | |
| 69 private void waitForBookmarkModelLoaded() throws InterruptedException { | |
| 70 final CallbackHelper loadedCallback = new CallbackHelper(); | |
| 71 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
| 72 @Override | |
| 73 public void run() { | |
| 74 if (mBookmarkModel.isBookmarkModelLoaded()) loadedCallback.notif
yCalled(); | |
| 75 else { | |
| 76 mBookmarkModel.addObserver(new BookmarkModelObserver() { | |
| 77 @Override | |
| 78 public void bookmarkModelChanged() {} | |
| 79 | |
| 80 @Override | |
| 81 public void bookmarkModelLoaded() { | |
| 82 loadedCallback.notifyCalled(); | |
| 83 mBookmarkModel.removeObserver(this); | |
| 84 } | |
| 85 }); | |
| 86 } | |
| 87 } | |
| 88 }); | |
| 89 try { | |
| 90 loadedCallback.waitForCallback(0); | |
| 91 } catch (TimeoutException e) { | |
| 92 Assert.fail("Enhanced Bookmark model did not load: Timeout."); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 private void openBookmarkManager() throws InterruptedException { | |
| 97 if (DeviceFormFactor.isTablet(getActivity())) { | |
| 98 loadUrl(UrlConstants.BOOKMARKS_URL); | |
| 99 mItemsContainer = (EnhancedBookmarkRecyclerView) getActivity().findV
iewById( | |
| 100 R.id.eb_items_container); | |
| 101 } else { | |
| 102 // phone | |
| 103 EnhancedBookmarkActivity activity = ActivityUtils.waitForActivity(ge
tInstrumentation(), | |
| 104 EnhancedBookmarkActivity.class, new MenuUtils.MenuActivityTr
igger( | |
| 105 getInstrumentation(), getActivity(), R.id.all_bookma
rks_menu_id)); | |
| 106 mItemsContainer = (EnhancedBookmarkRecyclerView) activity.findViewBy
Id( | |
| 107 R.id.eb_items_container); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 private void openBookmarkManager(final String url) throws InterruptedExcepti
on { | |
| 112 if (DeviceFormFactor.isTablet(getActivity())) { | |
| 113 loadUrl(url); | |
| 114 mItemsContainer = (EnhancedBookmarkRecyclerView) getActivity().findV
iewById( | |
| 115 R.id.eb_items_container); | |
| 116 } else { | |
| 117 // phone | |
| 118 EnhancedBookmarkActivity activity = ActivityUtils.waitForActivity(ge
tInstrumentation(), | |
| 119 EnhancedBookmarkActivity.class, new Runnable() { | |
| 120 @Override | |
| 121 public void run() { | |
| 122 Intent intent = new Intent(getActivity(), | |
| 123 EnhancedBookmarkActivity.class); | |
| 124 intent.setData(Uri.parse(url)); | |
| 125 getActivity().startActivity(intent); | |
| 126 } | |
| 127 }); | |
| 128 mItemsContainer = (EnhancedBookmarkRecyclerView) activity.findViewBy
Id( | |
| 129 R.id.eb_items_container); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 private boolean isItemPresentInBookmarkList(final String expectedTitle) { | |
| 134 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Boolean
>() { | |
| 135 @Override | |
| 136 public Boolean call() throws Exception { | |
| 137 for (int i = 0; i < mItemsContainer.getAdapter().getItemCount();
i++) { | |
| 138 BookmarkId item = mItemsContainer.getAdapter().getItem(i); | |
| 139 if (item == null) continue; | |
| 140 | |
| 141 String actualTitle = mBookmarkModel.getBookmarkTitle(item); | |
| 142 if (TextUtils.equals(actualTitle, expectedTitle)) { | |
| 143 return true; | |
| 144 } | |
| 145 } | |
| 146 return false; | |
| 147 } | |
| 148 }); | |
| 149 } | |
| 150 | |
| 151 @SmallTest | |
| 152 public void testAddBookmark() throws InterruptedException { | |
| 153 loadUrl(TEST_PAGE); | |
| 154 // Click star button to bookmark the curent tab. | |
| 155 MenuUtils.invokeCustomMenuActionSync(getInstrumentation(), getActivity()
, | |
| 156 R.id.bookmark_this_page_id); | |
| 157 // All actions with EnhancedBookmarksModel needs to run on UI thread. | |
| 158 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
| 159 @Override | |
| 160 public void run() { | |
| 161 long bookmarkIdLong = getActivity().getActivityTab().getUserBook
markId(); | |
| 162 BookmarkId id = new BookmarkId(bookmarkIdLong, BookmarkType.NORM
AL); | |
| 163 assertTrue("The test page is not added as bookmark: ", | |
| 164 mBookmarkModel.doesBookmarkExist(id)); | |
| 165 BookmarkItem item = mBookmarkModel.getBookmarkById(id); | |
| 166 assertEquals(mBookmarkModel.getDefaultFolder(), item.getParentId
()); | |
| 167 assertEquals(TEST_PAGE, item.getUrl()); | |
| 168 assertEquals(TEST_PAGE_TITLE, item.getTitle()); | |
| 169 } | |
| 170 }); | |
| 171 } | |
| 172 | |
| 173 @SmallTest | |
| 174 public void testOpenBookmark() throws InterruptedException { | |
| 175 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
| 176 @Override | |
| 177 public void run() { | |
| 178 mBookmarkModel.addBookmark(mBookmarkModel.getDefaultFolder(), 0,
TEST_PAGE_TITLE, | |
| 179 TEST_PAGE); | |
| 180 } | |
| 181 }); | |
| 182 openBookmarkManager(); | |
| 183 assertTrue("Grid view does not contain added bookmark: ", | |
| 184 isItemPresentInBookmarkList(TEST_PAGE_TITLE)); | |
| 185 final View tile = getViewWithText(mItemsContainer, TEST_PAGE_TITLE); | |
| 186 ChromeTabUtils.waitForTabPageLoaded(getActivity().getActivityTab(), new
Runnable() { | |
| 187 @Override | |
| 188 public void run() { | |
| 189 TouchCommon.singleClickView(tile); | |
| 190 } | |
| 191 }); | |
| 192 assertEquals(TEST_PAGE_TITLE, getActivity().getActivityTab().getTitle())
; | |
| 193 } | |
| 194 | |
| 195 @SmallTest | |
| 196 public void testUrlComposition() { | |
| 197 BookmarkId mobileId = mBookmarkModel.getMobileFolderId(); | |
| 198 BookmarkId bookmarkBarId = mBookmarkModel.getDesktopFolderId(); | |
| 199 BookmarkId otherId = mBookmarkModel.getOtherFolderId(); | |
| 200 assertEquals("chrome-native://bookmarks/folder/" + mobileId, | |
| 201 EnhancedBookmarkUIState.createFolderUrl(mobileId).toString()); | |
| 202 assertEquals("chrome-native://bookmarks/folder/" + bookmarkBarId, | |
| 203 EnhancedBookmarkUIState.createFolderUrl(bookmarkBarId).toString(
)); | |
| 204 assertEquals("chrome-native://bookmarks/folder/" + otherId, | |
| 205 EnhancedBookmarkUIState.createFolderUrl(otherId).toString()); | |
| 206 | |
| 207 assertEquals("chrome-native://bookmarks/filter/OFFLINE_PAGES", EnhancedB
ookmarkUIState | |
| 208 .createFilterUrl(EnhancedBookmarkFilter.OFFLINE_PAGES, true).toS
tring()); | |
| 209 assertEquals( | |
| 210 "chrome-native://bookmarks/filter/OFFLINE_PAGES?persist=0", | |
| 211 EnhancedBookmarkUIState.createFilterUrl(EnhancedBookmarkFilter.O
FFLINE_PAGES, | |
| 212 false).toString()); | |
| 213 } | |
| 214 | |
| 215 @SmallTest | |
| 216 public void testOpenBookmarkManager() throws InterruptedException { | |
| 217 openBookmarkManager(); | |
| 218 EnhancedBookmarkDelegate delegate = mItemsContainer.getDelegateForTestin
g(); | |
| 219 assertEquals(EnhancedBookmarkUIState.STATE_ALL_BOOKMARKS, delegate.getCu
rrentState()); | |
| 220 assertEquals(UrlConstants.BOOKMARKS_URL, | |
| 221 EnhancedBookmarkUtils.getLastUsedUrl(getActivity())); | |
| 222 } | |
| 223 | |
| 224 @SmallTest | |
| 225 @CommandLineFlags.Add(ChromeSwitches.ENABLE_OFFLINE_PAGES) | |
| 226 public void testOpenBookmarkManagerInOfflinePagePersist() throws Interrupted
Exception { | |
| 227 EnhancedBookmarkUtils.setLastUsedUrl(getActivity(), UrlConstants.BOOKMAR
KS_URL); | |
| 228 String url = "chrome-native://bookmarks/filter/OFFLINE_PAGES"; | |
| 229 openBookmarkManager(url); | |
| 230 EnhancedBookmarkDelegate delegate = mItemsContainer.getDelegateForTestin
g(); | |
| 231 assertEquals(EnhancedBookmarkUIState.STATE_FILTER, delegate.getCurrentSt
ate()); | |
| 232 assertEquals(url, EnhancedBookmarkUtils.getLastUsedUrl(getActivity())); | |
| 233 } | |
| 234 | |
| 235 @SmallTest | |
| 236 @CommandLineFlags.Add(ChromeSwitches.ENABLE_OFFLINE_PAGES) | |
| 237 public void testOpenBookmarkManagerInOfflinePageNoPersist() throws Interrupt
edException { | |
| 238 EnhancedBookmarkUtils.setLastUsedUrl(getActivity(), UrlConstants.BOOKMAR
KS_URL); | |
| 239 String url = "chrome-native://bookmarks/filter/OFFLINE_PAGES?persist=0"; | |
| 240 openBookmarkManager(url); | |
| 241 EnhancedBookmarkDelegate delegate = mItemsContainer.getDelegateForTestin
g(); | |
| 242 assertEquals(EnhancedBookmarkUIState.STATE_FILTER, delegate.getCurrentSt
ate()); | |
| 243 assertEquals(UrlConstants.BOOKMARKS_URL, | |
| 244 EnhancedBookmarkUtils.getLastUsedUrl(getActivity())); | |
| 245 } | |
| 246 | |
| 247 @SmallTest | |
| 248 @CommandLineFlags.Add(ChromeSwitches.DISABLE_OFFLINE_PAGES) | |
| 249 public void testOpenBookmarkManagerInOfflinePageWhenDisabled() throws Interr
uptedException { | |
| 250 openBookmarkManager("chrome-native://bookmarks/filter/OFFLINE_PAGES"); | |
| 251 EnhancedBookmarkDelegate delegate = mItemsContainer.getDelegateForTestin
g(); | |
| 252 assertEquals(EnhancedBookmarkUIState.STATE_ALL_BOOKMARKS, delegate.getCu
rrentState()); | |
| 253 } | |
| 254 | |
| 255 /** | |
| 256 * Returns the View that has the given text. | |
| 257 * | |
| 258 * @param viewGroup The group to which the view belongs. | |
| 259 * @param expectedText The expected description text. | |
| 260 * @return The unique view, if one exists. Throws an exception if one doesn'
t exist. | |
| 261 */ | |
| 262 private static View getViewWithText(final ViewGroup viewGroup, final String
expectedText) { | |
| 263 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<View>()
{ | |
| 264 @Override | |
| 265 public View call() throws Exception { | |
| 266 ArrayList<View> outViews = new ArrayList<View>(); | |
| 267 ArrayList<View> matchingViews = new ArrayList<View>(); | |
| 268 viewGroup.findViewsWithText(outViews, expectedText, View.FIND_VI
EWS_WITH_TEXT); | |
| 269 // outViews includes all views whose text contains expectedText
as a | |
| 270 // case-insensitive substring. Filter these views to find only e
xact string matches. | |
| 271 for (View v : outViews) { | |
| 272 if (TextUtils.equals(((TextView) v).getText().toString(), ex
pectedText)) { | |
| 273 matchingViews.add(v); | |
| 274 } | |
| 275 } | |
| 276 Assert.assertEquals("Exactly one item should be present.", 1, ma
tchingViews.size()); | |
| 277 return matchingViews.get(0); | |
| 278 } | |
| 279 }); | |
| 280 } | |
| 281 } | |
| OLD | NEW |