OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.bookmarks; | 5 package org.chromium.chrome.browser.bookmarks; |
6 | 6 |
| 7 import android.support.test.filters.MediumTest; |
7 import android.support.test.filters.SmallTest; | 8 import android.support.test.filters.SmallTest; |
8 import android.support.v7.widget.RecyclerView; | 9 import android.support.v7.widget.RecyclerView; |
9 import android.text.TextUtils; | 10 import android.text.TextUtils; |
10 import android.view.View; | 11 import android.view.View; |
11 import android.view.ViewGroup; | 12 import android.view.ViewGroup; |
12 import android.widget.TextView; | 13 import android.widget.TextView; |
13 | 14 |
14 import junit.framework.Assert; | 15 import junit.framework.Assert; |
15 | 16 |
16 import org.chromium.base.ThreadUtils; | 17 import org.chromium.base.ThreadUtils; |
17 import org.chromium.base.test.util.RetryOnFailure; | 18 import org.chromium.base.test.util.RetryOnFailure; |
18 import org.chromium.chrome.R; | 19 import org.chromium.chrome.R; |
19 import org.chromium.chrome.browser.ChromeActivity; | 20 import org.chromium.chrome.browser.ChromeActivity; |
20 import org.chromium.chrome.browser.UrlConstants; | 21 import org.chromium.chrome.browser.UrlConstants; |
21 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkItem; | 22 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkItem; |
22 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | 23 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
23 import org.chromium.chrome.test.util.ActivityUtils; | 24 import org.chromium.chrome.test.util.ActivityUtils; |
24 import org.chromium.chrome.test.util.BookmarkTestUtil; | 25 import org.chromium.chrome.test.util.BookmarkTestUtil; |
25 import org.chromium.chrome.test.util.ChromeTabUtils; | 26 import org.chromium.chrome.test.util.ChromeTabUtils; |
26 import org.chromium.chrome.test.util.MenuUtils; | 27 import org.chromium.chrome.test.util.MenuUtils; |
27 import org.chromium.components.bookmarks.BookmarkId; | 28 import org.chromium.components.bookmarks.BookmarkId; |
28 import org.chromium.components.bookmarks.BookmarkType; | 29 import org.chromium.components.bookmarks.BookmarkType; |
29 import org.chromium.content.browser.test.util.TouchCommon; | 30 import org.chromium.content.browser.test.util.TouchCommon; |
30 import org.chromium.net.test.EmbeddedTestServer; | 31 import org.chromium.net.test.EmbeddedTestServer; |
31 import org.chromium.ui.base.DeviceFormFactor; | 32 import org.chromium.ui.base.DeviceFormFactor; |
32 | 33 |
33 import java.util.ArrayList; | 34 import java.util.ArrayList; |
34 import java.util.concurrent.Callable; | 35 import java.util.concurrent.Callable; |
| 36 import java.util.concurrent.ExecutionException; |
35 | 37 |
36 /** | 38 /** |
37 * Tests for the bookmark manager. | 39 * Tests for the bookmark manager. |
38 */ | 40 */ |
39 @RetryOnFailure | 41 @RetryOnFailure |
40 public class BookmarkTest extends ChromeActivityTestCaseBase<ChromeActivity> { | 42 public class BookmarkTest extends ChromeActivityTestCaseBase<ChromeActivity> { |
41 | 43 |
42 public BookmarkTest() { | 44 public BookmarkTest() { |
43 super(ChromeActivity.class); | 45 super(ChromeActivity.class); |
44 } | 46 } |
45 | 47 |
46 private static final String TEST_PAGE = "/chrome/test/data/android/google.ht
ml"; | 48 private static final String TEST_PAGE_URL_GOOGLE = "/chrome/test/data/androi
d/google.html"; |
47 private static final String TEST_PAGE_TITLE = "The Google"; | 49 private static final String TEST_PAGE_TITLE_GOOGLE = "The Google"; |
| 50 private static final String TEST_PAGE_TITLE_GOOGLE2 = "Google"; |
| 51 private static final String TEST_PAGE_URL_FOO = "/chrome/test/data/android/t
est.html"; |
| 52 private static final String TEST_PAGE_TITLE_FOO = "Foo"; |
48 | 53 |
49 private BookmarkModel mBookmarkModel; | 54 private BookmarkModel mBookmarkModel; |
50 protected RecyclerView mItemsContainer; | 55 protected RecyclerView mItemsContainer; |
51 private String mTestPage; | 56 private String mTestPage; |
| 57 private String mTestPageFoo; |
52 private EmbeddedTestServer mTestServer; | 58 private EmbeddedTestServer mTestServer; |
53 | 59 |
54 @Override | 60 @Override |
55 protected void setUp() throws Exception { | 61 protected void setUp() throws Exception { |
56 super.setUp(); | 62 super.setUp(); |
57 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation
().getContext()); | 63 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation
().getContext()); |
58 mTestPage = mTestServer.getURL(TEST_PAGE); | 64 mTestPage = mTestServer.getURL(TEST_PAGE_URL_GOOGLE); |
| 65 mTestPageFoo = mTestServer.getURL(TEST_PAGE_URL_FOO); |
59 } | 66 } |
60 | 67 |
61 @Override | 68 @Override |
62 protected void tearDown() throws Exception { | 69 protected void tearDown() throws Exception { |
63 mTestServer.stopAndDestroyServer(); | 70 mTestServer.stopAndDestroyServer(); |
64 super.tearDown(); | 71 super.tearDown(); |
65 } | 72 } |
66 | 73 |
67 @Override | 74 @Override |
68 public void startMainActivity() throws InterruptedException { | 75 public void startMainActivity() throws InterruptedException { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 125 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
119 @Override | 126 @Override |
120 public void run() { | 127 public void run() { |
121 long bookmarkIdLong = getActivity().getActivityTab().getUserBook
markId(); | 128 long bookmarkIdLong = getActivity().getActivityTab().getUserBook
markId(); |
122 BookmarkId id = new BookmarkId(bookmarkIdLong, BookmarkType.NORM
AL); | 129 BookmarkId id = new BookmarkId(bookmarkIdLong, BookmarkType.NORM
AL); |
123 assertTrue("The test page is not added as bookmark: ", | 130 assertTrue("The test page is not added as bookmark: ", |
124 mBookmarkModel.doesBookmarkExist(id)); | 131 mBookmarkModel.doesBookmarkExist(id)); |
125 BookmarkItem item = mBookmarkModel.getBookmarkById(id); | 132 BookmarkItem item = mBookmarkModel.getBookmarkById(id); |
126 assertEquals(mBookmarkModel.getDefaultFolder(), item.getParentId
()); | 133 assertEquals(mBookmarkModel.getDefaultFolder(), item.getParentId
()); |
127 assertEquals(mTestPage, item.getUrl()); | 134 assertEquals(mTestPage, item.getUrl()); |
128 assertEquals(TEST_PAGE_TITLE, item.getTitle()); | 135 assertEquals(TEST_PAGE_TITLE_GOOGLE, item.getTitle()); |
129 } | 136 } |
130 }); | 137 }); |
131 } | 138 } |
132 | 139 |
133 @SmallTest | 140 @SmallTest |
134 public void testOpenBookmark() throws InterruptedException { | 141 public void testOpenBookmark() throws InterruptedException, ExecutionExcepti
on { |
135 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 142 addBookmark(TEST_PAGE_TITLE_GOOGLE, mTestPage); |
136 @Override | |
137 public void run() { | |
138 mBookmarkModel.addBookmark(mBookmarkModel.getDefaultFolder(), 0,
TEST_PAGE_TITLE, | |
139 mTestPage); | |
140 } | |
141 }); | |
142 openBookmarkManager(); | 143 openBookmarkManager(); |
143 assertTrue("Grid view does not contain added bookmark: ", | 144 assertTrue("Grid view does not contain added bookmark: ", |
144 isItemPresentInBookmarkList(TEST_PAGE_TITLE)); | 145 isItemPresentInBookmarkList(TEST_PAGE_TITLE_GOOGLE)); |
145 final View tile = getViewWithText(mItemsContainer, TEST_PAGE_TITLE); | 146 final View tile = getViewWithText(mItemsContainer, TEST_PAGE_TITLE_GOOGL
E); |
146 ChromeTabUtils.waitForTabPageLoaded(getActivity().getActivityTab(), new
Runnable() { | 147 ChromeTabUtils.waitForTabPageLoaded(getActivity().getActivityTab(), new
Runnable() { |
147 @Override | 148 @Override |
148 public void run() { | 149 public void run() { |
149 TouchCommon.singleClickView(tile); | 150 TouchCommon.singleClickView(tile); |
150 } | 151 } |
151 }); | 152 }); |
152 assertEquals(TEST_PAGE_TITLE, getActivity().getActivityTab().getTitle())
; | 153 assertEquals(TEST_PAGE_TITLE_GOOGLE, getActivity().getActivityTab().getT
itle()); |
153 } | 154 } |
154 | 155 |
155 @SmallTest | 156 @SmallTest |
156 public void testUrlComposition() { | 157 public void testUrlComposition() { |
157 BookmarkId mobileId = mBookmarkModel.getMobileFolderId(); | 158 BookmarkId mobileId = mBookmarkModel.getMobileFolderId(); |
158 BookmarkId bookmarkBarId = mBookmarkModel.getDesktopFolderId(); | 159 BookmarkId bookmarkBarId = mBookmarkModel.getDesktopFolderId(); |
159 BookmarkId otherId = mBookmarkModel.getOtherFolderId(); | 160 BookmarkId otherId = mBookmarkModel.getOtherFolderId(); |
160 assertEquals("chrome-native://bookmarks/folder/" + mobileId, | 161 assertEquals("chrome-native://bookmarks/folder/" + mobileId, |
161 BookmarkUIState.createFolderUrl(mobileId).toString()); | 162 BookmarkUIState.createFolderUrl(mobileId).toString()); |
162 assertEquals("chrome-native://bookmarks/folder/" + bookmarkBarId, | 163 assertEquals("chrome-native://bookmarks/folder/" + bookmarkBarId, |
163 BookmarkUIState.createFolderUrl(bookmarkBarId).toString()); | 164 BookmarkUIState.createFolderUrl(bookmarkBarId).toString()); |
164 assertEquals("chrome-native://bookmarks/folder/" + otherId, | 165 assertEquals("chrome-native://bookmarks/folder/" + otherId, |
165 BookmarkUIState.createFolderUrl(otherId).toString()); | 166 BookmarkUIState.createFolderUrl(otherId).toString()); |
166 } | 167 } |
167 | 168 |
168 @SmallTest | 169 @SmallTest |
169 public void testOpenBookmarkManager() throws InterruptedException { | 170 public void testOpenBookmarkManager() throws InterruptedException { |
170 openBookmarkManager(); | 171 openBookmarkManager(); |
171 BookmarkDelegate delegate = | 172 BookmarkDelegate delegate = |
172 ((BookmarkItemsAdapter) mItemsContainer.getAdapter()).getDelegat
eForTesting(); | 173 ((BookmarkItemsAdapter) mItemsContainer.getAdapter()).getDelegat
eForTesting(); |
173 assertEquals(BookmarkUIState.STATE_FOLDER, delegate.getCurrentState()); | 174 assertEquals(BookmarkUIState.STATE_FOLDER, delegate.getCurrentState()); |
174 assertEquals("chrome-native://bookmarks/folder/3", | 175 assertEquals("chrome-native://bookmarks/folder/3", |
175 BookmarkUtils.getLastUsedUrl(getActivity())); | 176 BookmarkUtils.getLastUsedUrl(getActivity())); |
176 } | 177 } |
177 | 178 |
| 179 @MediumTest |
| 180 public void testSearchBookmarks() throws Exception { |
| 181 BookmarkPromoHeader.setShouldShowForTests(); |
| 182 addBookmark(TEST_PAGE_TITLE_GOOGLE, mTestPage); |
| 183 addBookmark(TEST_PAGE_TITLE_FOO, mTestPageFoo); |
| 184 openBookmarkManager(); |
| 185 |
| 186 BookmarkItemsAdapter adapter = ((BookmarkItemsAdapter) mItemsContainer.g
etAdapter()); |
| 187 final BookmarkDelegate delegate = adapter.getDelegateForTesting(); |
| 188 |
| 189 assertEquals(BookmarkUIState.STATE_FOLDER, delegate.getCurrentState()); |
| 190 assertEquals("Wrong number of items before starting search.", 3, adapter
.getItemCount()); |
| 191 |
| 192 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 193 @Override |
| 194 public void run() { |
| 195 delegate.openSearchUI(); |
| 196 } |
| 197 }); |
| 198 |
| 199 assertEquals(BookmarkUIState.STATE_SEARCHING, delegate.getCurrentState()
); |
| 200 assertEquals("Wrong number of items after showing search UI. The promo s
hould be hidden.", |
| 201 2, adapter.getItemCount()); |
| 202 |
| 203 searchBookmarks("Google"); |
| 204 assertEquals("Wrong number of items after searching.", 1, |
| 205 mItemsContainer.getAdapter().getItemCount()); |
| 206 |
| 207 BookmarkId newBookmark = addBookmark(TEST_PAGE_TITLE_GOOGLE2, mTestPage)
; |
| 208 assertEquals("Wrong number of items after bookmark added while searching
.", 2, |
| 209 mItemsContainer.getAdapter().getItemCount()); |
| 210 |
| 211 removeBookmark(newBookmark); |
| 212 assertEquals("Wrong number of items after bookmark removed while searchi
ng.", 1, |
| 213 mItemsContainer.getAdapter().getItemCount()); |
| 214 |
| 215 searchBookmarks("Non-existent page"); |
| 216 assertEquals("Wrong number of items after searching for non-existent ite
m.", 0, |
| 217 mItemsContainer.getAdapter().getItemCount()); |
| 218 |
| 219 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 220 @Override |
| 221 public void run() { |
| 222 delegate.closeSearchUI(); |
| 223 } |
| 224 }); |
| 225 assertEquals("Wrong number of items after closing search UI.", 3, |
| 226 mItemsContainer.getAdapter().getItemCount()); |
| 227 assertEquals(BookmarkUIState.STATE_FOLDER, delegate.getCurrentState()); |
| 228 } |
| 229 |
178 /** | 230 /** |
179 * Returns the View that has the given text. | 231 * Returns the View that has the given text. |
180 * | 232 * |
181 * @param viewGroup The group to which the view belongs. | 233 * @param viewGroup The group to which the view belongs. |
182 * @param expectedText The expected description text. | 234 * @param expectedText The expected description text. |
183 * @return The unique view, if one exists. Throws an exception if one doesn'
t exist. | 235 * @return The unique view, if one exists. Throws an exception if one doesn'
t exist. |
184 */ | 236 */ |
185 private static View getViewWithText(final ViewGroup viewGroup, final String
expectedText) { | 237 private static View getViewWithText(final ViewGroup viewGroup, final String
expectedText) { |
186 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<View>()
{ | 238 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<View>()
{ |
187 @Override | 239 @Override |
188 public View call() throws Exception { | 240 public View call() throws Exception { |
189 ArrayList<View> outViews = new ArrayList<View>(); | 241 ArrayList<View> outViews = new ArrayList<View>(); |
190 ArrayList<View> matchingViews = new ArrayList<View>(); | 242 ArrayList<View> matchingViews = new ArrayList<View>(); |
191 viewGroup.findViewsWithText(outViews, expectedText, View.FIND_VI
EWS_WITH_TEXT); | 243 viewGroup.findViewsWithText(outViews, expectedText, View.FIND_VI
EWS_WITH_TEXT); |
192 // outViews includes all views whose text contains expectedText
as a | 244 // outViews includes all views whose text contains expectedText
as a |
193 // case-insensitive substring. Filter these views to find only e
xact string matches. | 245 // case-insensitive substring. Filter these views to find only e
xact string matches. |
194 for (View v : outViews) { | 246 for (View v : outViews) { |
195 if (TextUtils.equals(((TextView) v).getText().toString(), ex
pectedText)) { | 247 if (TextUtils.equals(((TextView) v).getText().toString(), ex
pectedText)) { |
196 matchingViews.add(v); | 248 matchingViews.add(v); |
197 } | 249 } |
198 } | 250 } |
199 Assert.assertEquals("Exactly one item should be present.", 1, ma
tchingViews.size()); | 251 Assert.assertEquals("Exactly one item should be present.", 1, ma
tchingViews.size()); |
200 return matchingViews.get(0); | 252 return matchingViews.get(0); |
201 } | 253 } |
202 }); | 254 }); |
203 } | 255 } |
| 256 |
| 257 private BookmarkId addBookmark(final String title, final String url) throws
ExecutionException { |
| 258 return ThreadUtils.runOnUiThreadBlocking(new Callable<BookmarkId>() { |
| 259 @Override |
| 260 public BookmarkId call() throws Exception { |
| 261 return mBookmarkModel.addBookmark(mBookmarkModel.getDefaultFolde
r(), 0, title, url); |
| 262 } |
| 263 }); |
| 264 } |
| 265 |
| 266 private void removeBookmark(final BookmarkId bookmarkId) { |
| 267 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 268 @Override |
| 269 public void run() { |
| 270 mBookmarkModel.deleteBookmark(bookmarkId); |
| 271 } |
| 272 }); |
| 273 } |
| 274 |
| 275 private void searchBookmarks(final String query) { |
| 276 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 277 @Override |
| 278 public void run() { |
| 279 ((BookmarkItemsAdapter) mItemsContainer.getAdapter()).search(que
ry); |
| 280 } |
| 281 }); |
| 282 } |
204 } | 283 } |
OLD | NEW |