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.test.util; |
| 6 |
| 7 import static org.chromium.chrome.browser.bookmark.ManageBookmarkActivity.ADD_FO
LDER_FRAGMENT_TAG; |
| 8 import static org.chromium.chrome.browser.bookmark.ManageBookmarkActivity.BASE_A
DD_EDIT_FRAGMENT_TAG; |
| 9 import static org.chromium.chrome.browser.bookmark.ManageBookmarkActivity.BASE_S
ELECT_FOLDER_FRAGMENT_TAG; |
| 10 |
| 11 import android.app.Instrumentation; |
| 12 import android.graphics.Bitmap; |
| 13 import android.graphics.Bitmap.CompressFormat; |
| 14 import android.graphics.BitmapFactory; |
| 15 import android.test.InstrumentationTestCase; |
| 16 import android.text.TextUtils; |
| 17 import android.util.Log; |
| 18 import android.view.View; |
| 19 import android.view.ViewGroup; |
| 20 import android.widget.ListView; |
| 21 import android.widget.TextView; |
| 22 |
| 23 import com.google.android.apps.chrome.R; |
| 24 |
| 25 import junit.framework.Assert; |
| 26 |
| 27 import org.chromium.base.ThreadUtils; |
| 28 import org.chromium.chrome.browser.ChromeTabbedActivity; |
| 29 import org.chromium.chrome.browser.bookmark.AddEditBookmarkFragment; |
| 30 import org.chromium.chrome.browser.bookmark.ManageBookmarkActivity; |
| 31 import org.chromium.chrome.browser.bookmark.SelectBookmarkFolderFragment; |
| 32 import org.chromium.chrome.browser.omnibox.UrlBar; |
| 33 import org.chromium.content.browser.test.util.CallbackHelper; |
| 34 import org.chromium.content.browser.test.util.Criteria; |
| 35 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 36 import org.chromium.content.browser.test.util.TestTouchUtils; |
| 37 import org.chromium.content.browser.test.util.UiUtils; |
| 38 |
| 39 import java.io.ByteArrayOutputStream; |
| 40 import java.io.IOException; |
| 41 import java.io.InputStream; |
| 42 import java.net.URL; |
| 43 import java.util.ArrayList; |
| 44 import java.util.Arrays; |
| 45 import java.util.concurrent.Callable; |
| 46 import java.util.concurrent.TimeoutException; |
| 47 |
| 48 /** |
| 49 * Set of utility functions shared between tests managing bookmarks. |
| 50 */ |
| 51 public class BookmarkTestUtils { |
| 52 private static final String TAG = "BookmarkTestUtils"; |
| 53 |
| 54 /** |
| 55 * Checks if two byte arrays are equal. Used to compare icons. |
| 56 * @return True if equal, false otherwise. |
| 57 */ |
| 58 public static boolean byteArrayEqual(byte[] byte1, byte[] byte2) { |
| 59 if (byte1 == null && byte2 != null) { |
| 60 return byte2.length == 0; |
| 61 } |
| 62 if (byte2 == null && byte1 != null) { |
| 63 return byte1.length == 0; |
| 64 } |
| 65 return Arrays.equals(byte1, byte2); |
| 66 } |
| 67 |
| 68 /** |
| 69 * Retrieves a byte array with the decoded image data of an icon. |
| 70 * @return Data of the icon. |
| 71 */ |
| 72 public static byte[] getIcon(String testPath) { |
| 73 ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 74 try { |
| 75 InputStream faviconStream = (InputStream) (new URL( |
| 76 TestHttpServerClient.getUrl(testPath))).getContent(); |
| 77 Bitmap faviconBitmap = BitmapFactory.decodeStream(faviconStream); |
| 78 faviconBitmap.compress(CompressFormat.PNG, 0, bos); |
| 79 } catch (IOException e) { |
| 80 Log.e(TAG, "Error trying to get the icon '" + testPath + "': " + e.g
etMessage()); |
| 81 } |
| 82 return bos.toByteArray(); |
| 83 } |
| 84 |
| 85 /** |
| 86 * Opens the add/edit bookmark menu. |
| 87 */ |
| 88 public static ManageBookmarkActivity selectBookmarkItemFromMenu( |
| 89 Instrumentation instrumentation, ChromeTabbedActivity mainActivity)
{ |
| 90 ManageBookmarkActivity activity = ActivityUtils |
| 91 .waitForActivity( |
| 92 instrumentation, |
| 93 ManageBookmarkActivity.class, |
| 94 new MenuUtils.MenuActivityTrigger( |
| 95 instrumentation, mainActivity, R.id.bookmark_thi
s_page_id)); |
| 96 instrumentation.waitForIdleSync(); |
| 97 return activity; |
| 98 } |
| 99 |
| 100 /** |
| 101 * Clicks the Select Folder button in the given AddEditBookmark dialog. |
| 102 * @throws InterruptedException |
| 103 */ |
| 104 public static void clickSelectFolderButton( |
| 105 InstrumentationTestCase instrumentationTestCase, |
| 106 AddEditBookmarkFragment addEditFragment) throws InterruptedException
{ |
| 107 CallbackHelper folderSelectCallback = new CallbackHelper(); |
| 108 initializeAddEditCallback(addEditFragment, folderSelectCallback); |
| 109 TestTouchUtils.clickView( |
| 110 instrumentationTestCase, |
| 111 addEditFragment.getView().findViewById(R.id.bookmark_folder_sele
ct)); |
| 112 try { |
| 113 folderSelectCallback.waitForCallback(0); |
| 114 } catch (TimeoutException e) { |
| 115 Assert.fail("Folder select did not occur"); |
| 116 } |
| 117 } |
| 118 |
| 119 /** |
| 120 * Clicks the New Folder button in the given SelectBookmarkFolder dialog. |
| 121 * @throws InterruptedException |
| 122 */ |
| 123 public static void clickNewFolderButton( |
| 124 InstrumentationTestCase instrumentationTestCase, |
| 125 SelectBookmarkFolderFragment selectFragment) throws InterruptedExcep
tion { |
| 126 CallbackHelper newFolderEvent = new CallbackHelper(); |
| 127 initializeNewFolderCallback(selectFragment, newFolderEvent); |
| 128 TestTouchUtils.clickView( |
| 129 instrumentationTestCase, |
| 130 selectFragment.getView().findViewById(R.id.new_folder_btn)); |
| 131 try { |
| 132 newFolderEvent.waitForCallback(0); |
| 133 } catch (TimeoutException e) { |
| 134 Assert.fail("Never received new folder event"); |
| 135 } |
| 136 } |
| 137 |
| 138 /** |
| 139 * Asserts that the URL bar contains the expected URL. |
| 140 */ |
| 141 public static void assertUrlBarEquals(ChromeTabbedActivity mainActivity, Str
ing msg, |
| 142 String expectedUrl) { |
| 143 final UrlBar urlBar = (UrlBar) mainActivity.findViewById(R.id.url_bar); |
| 144 // Ideally we should not be depending on UI to verify that navigation to
a url was |
| 145 // successful. UrlBar behavior is different for http and https urls. We
always show |
| 146 // the scheme for https:// urls but not for http://. |
| 147 String urlBarText = urlBar.getText().toString(); |
| 148 if (!urlBarText.startsWith("https")) { |
| 149 urlBarText = "http://" + urlBarText; |
| 150 } |
| 151 Assert.assertEquals(msg, expectedUrl, urlBarText); |
| 152 } |
| 153 |
| 154 private static void initializeAddEditCallback( |
| 155 final AddEditBookmarkFragment addEditFragment, |
| 156 final CallbackHelper callback) { |
| 157 // Proxy the messages sent from the fragment to detect if asynchronous o
perations succeeded |
| 158 // before finishing the fragment. |
| 159 final AddEditBookmarkFragment.OnActionListener originalListener = |
| 160 addEditFragment.getOnActionListenerForTest(); |
| 161 |
| 162 addEditFragment.setOnActionListener(new AddEditBookmarkFragment.OnAction
Listener() { |
| 163 @Override |
| 164 public void onCancel() { |
| 165 if (originalListener != null) { |
| 166 originalListener.onCancel(); |
| 167 } |
| 168 } |
| 169 |
| 170 @Override |
| 171 public void onNodeEdited(long id) { |
| 172 if (originalListener != null) { |
| 173 originalListener.onNodeEdited(id); |
| 174 } |
| 175 // Insert / update operation succeeded. |
| 176 callback.notifyCalled(); |
| 177 } |
| 178 |
| 179 @Override |
| 180 public void onFolderCreated(long id, String name) { |
| 181 if (originalListener != null) { |
| 182 originalListener.onFolderCreated(id, name); |
| 183 } |
| 184 // Insert / update operation succeeded. |
| 185 callback.notifyCalled(); |
| 186 } |
| 187 |
| 188 @Override |
| 189 public void triggerFolderSelection() { |
| 190 if (originalListener != null) { |
| 191 originalListener.triggerFolderSelection(); |
| 192 } |
| 193 // Bookmarks folder input button was clicked. |
| 194 callback.notifyCalled(); |
| 195 } |
| 196 |
| 197 @Override |
| 198 public void onRemove() { |
| 199 if (originalListener != null) { |
| 200 originalListener.onRemove(); |
| 201 } |
| 202 // Delete operation succeeded. |
| 203 callback.notifyCalled(); |
| 204 } |
| 205 |
| 206 @Override |
| 207 public void setBackEnabled(boolean enabled) { |
| 208 if (originalListener != null) { |
| 209 originalListener.setBackEnabled(enabled); |
| 210 } |
| 211 } |
| 212 }); |
| 213 } |
| 214 |
| 215 private static void initializeNewFolderCallback( |
| 216 final SelectBookmarkFolderFragment selectBookmarkFolderFragment, |
| 217 final CallbackHelper newFolderEvent) { |
| 218 // Proxy the messages sent from the fragment to detect if asynchronous o
perations succeeded |
| 219 // before finishing the fragment. |
| 220 final SelectBookmarkFolderFragment.OnActionListener originalListener = |
| 221 selectBookmarkFolderFragment.getOnActionListenerForTest(); |
| 222 |
| 223 selectBookmarkFolderFragment.setOnActionListener( |
| 224 new SelectBookmarkFolderFragment.OnActionListener() { |
| 225 @Override |
| 226 public void triggerNewFolderCreation( |
| 227 long selectedFolderId, String selectedFolderName) { |
| 228 if (originalListener != null) { |
| 229 originalListener.triggerNewFolderCreation( |
| 230 selectedFolderId, selectedFolderName); |
| 231 } |
| 232 // New folder button was clicked. |
| 233 newFolderEvent.notifyCalled(); |
| 234 } |
| 235 }); |
| 236 } |
| 237 |
| 238 public static AddEditBookmarkFragment loadAddEditFragment( |
| 239 ManageBookmarkActivity bookmarkActivity) |
| 240 throws InterruptedException { |
| 241 AddEditBookmarkFragment fragment = ActivityUtils.waitForFragment( |
| 242 bookmarkActivity, BASE_ADD_EDIT_FRAGMENT_TAG); |
| 243 waitForFoldersToLoad(fragment); |
| 244 return fragment; |
| 245 } |
| 246 |
| 247 public static AddEditBookmarkFragment loadAddFolderFragment( |
| 248 ManageBookmarkActivity bookmarkActivity) |
| 249 throws InterruptedException { |
| 250 AddEditBookmarkFragment fragment = ActivityUtils.waitForFragment( |
| 251 bookmarkActivity, ADD_FOLDER_FRAGMENT_TAG); |
| 252 waitForFoldersToLoad(fragment); |
| 253 return fragment; |
| 254 } |
| 255 |
| 256 public static SelectBookmarkFolderFragment loadSelectFragment( |
| 257 ManageBookmarkActivity bookmarkActivity) throws InterruptedException
{ |
| 258 SelectBookmarkFolderFragment fragment = ActivityUtils.waitForFragment( |
| 259 bookmarkActivity, BASE_SELECT_FOLDER_FRAGMENT_TAG); |
| 260 waitForFoldersToLoad(fragment); |
| 261 return fragment; |
| 262 } |
| 263 |
| 264 private static void waitForFoldersToLoad(final AddEditBookmarkFragment addFr
agment) |
| 265 throws InterruptedException { |
| 266 checkCriteria("Failed to load Fragment.", new Callable<Boolean>() { |
| 267 @Override |
| 268 public Boolean call() throws Exception { |
| 269 return !addFragment.isLoadingBookmarks(); |
| 270 } |
| 271 }); |
| 272 } |
| 273 |
| 274 private static void waitForFoldersToLoad(final SelectBookmarkFolderFragment
selectedFolder) |
| 275 throws InterruptedException { |
| 276 checkCriteria("Failed to load Fragment.", new Callable<Boolean>() { |
| 277 @Override |
| 278 public Boolean call() throws Exception { |
| 279 return selectedFolder.areFoldersLoaded(); |
| 280 } |
| 281 }); |
| 282 } |
| 283 |
| 284 private static void checkCriteria(String message, final Callable<Boolean> cr
iteria) |
| 285 throws InterruptedException { |
| 286 Assert.assertTrue(message, CriteriaHelper.pollForCriteria(new Criteria()
{ |
| 287 @Override |
| 288 public boolean isSatisfied() { |
| 289 try { |
| 290 return ThreadUtils.runOnUiThread(criteria).get(); |
| 291 } catch (Exception e) { |
| 292 Assert.fail(e.getMessage()); |
| 293 return false; |
| 294 } |
| 295 } |
| 296 })); |
| 297 } |
| 298 |
| 299 /** |
| 300 * Navigates to a URL in the current tab and bookmarks it through the add/ed
it bookmark UI. |
| 301 * @throws InterruptedException |
| 302 */ |
| 303 public static void addCurrentUrlAsBookmark(InstrumentationTestCase testCase, |
| 304 ChromeTabbedActivity mainActivity) |
| 305 throws InterruptedException { |
| 306 final ManageBookmarkActivity addActivity = |
| 307 selectBookmarkItemFromMenu(testCase.getInstrumentation(), mainAc
tivity); |
| 308 final AddEditBookmarkFragment addFragment = loadAddEditFragment(addActiv
ity); |
| 309 clickOkButton(testCase, addFragment); |
| 310 } |
| 311 |
| 312 /** |
| 313 * Returns a unique UI element that has the given description text. |
| 314 * |
| 315 * @param viewGroup The group to which the view belongs. |
| 316 * @param expectedText The expected description text. |
| 317 * @return The unique view, if one exists. Throws an exception if one doesn'
t exist. |
| 318 */ |
| 319 public static View getViewWithText(final ViewGroup viewGroup, final String e
xpectedText) { |
| 320 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<View>()
{ |
| 321 @Override |
| 322 public View call() throws Exception { |
| 323 ArrayList<View> outViews = new ArrayList<View>(); |
| 324 ArrayList<View> matchingViews = new ArrayList<View>(); |
| 325 viewGroup.findViewsWithText(outViews, expectedText, View.FIND_VI
EWS_WITH_TEXT); |
| 326 // outViews includes all views whose text contains expectedText
as a |
| 327 // case-insensitive substring. Filter these views to find only e
xact string matches. |
| 328 for (View v : outViews) { |
| 329 if (TextUtils.equals(((TextView) v).getText().toString(), ex
pectedText)) { |
| 330 matchingViews.add(v); |
| 331 } |
| 332 } |
| 333 Assert.assertEquals("Exactly one item should be present.", 1, ma
tchingViews.size()); |
| 334 return matchingViews.get(0); |
| 335 } |
| 336 }); |
| 337 } |
| 338 |
| 339 /** |
| 340 * Click the folder with the title @param folderTitle in the SelectBookmarkF
older fragment. |
| 341 * @throws InterruptedException |
| 342 */ |
| 343 public static void clickFolderItem(InstrumentationTestCase testCase, |
| 344 final SelectBookmarkFolderFragment fragment, String folderTitle) |
| 345 throws InterruptedException { |
| 346 final ListView foldersList = (ListView) fragment.getView().findViewById( |
| 347 R.id.bookmark_folder_list); |
| 348 TextView folderItem = |
| 349 (TextView) getViewWithText(foldersList, folderTitle); |
| 350 TestTouchUtils.clickView(testCase, folderItem); |
| 351 UiUtils.settleDownUI(testCase.getInstrumentation()); |
| 352 } |
| 353 |
| 354 /** |
| 355 * Click OK button on the AddEditBookmarkFragment fragment and wait for oper
ation to be |
| 356 * completed. |
| 357 * @throws InterruptedException |
| 358 */ |
| 359 public static void clickOkButton( |
| 360 InstrumentationTestCase testCase, AddEditBookmarkFragment fragment) |
| 361 throws InterruptedException { |
| 362 CallbackHelper okCallback = new CallbackHelper(); |
| 363 initializeAddEditCallback(fragment, okCallback); |
| 364 TestTouchUtils.clickView(testCase, fragment.getView().findViewById(R.id.
ok)); |
| 365 try { |
| 366 okCallback.waitForCallback(0); |
| 367 } catch (TimeoutException e) { |
| 368 Assert.fail("OK action never triggered an update"); |
| 369 } |
| 370 testCase.getInstrumentation().waitForIdleSync(); |
| 371 } |
| 372 } |
OLD | NEW |