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.bookmark; |
| 6 |
| 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
| 8 |
| 9 import android.content.Context; |
| 10 import android.test.FlakyTest; |
| 11 import android.test.suitebuilder.annotation.SmallTest; |
| 12 import android.widget.Button; |
| 13 import android.widget.EditText; |
| 14 import android.widget.TextView; |
| 15 |
| 16 import com.google.android.apps.chrome.R; |
| 17 |
| 18 import org.chromium.base.test.util.Feature; |
| 19 import org.chromium.chrome.browser.ChromeBrowserProvider.BookmarkNode; |
| 20 import org.chromium.chrome.browser.ChromeBrowserProviderClient; |
| 21 import org.chromium.chrome.test.ChromeTabbedActivityTestBase; |
| 22 import org.chromium.chrome.test.util.BookmarkTestUtils; |
| 23 import org.chromium.chrome.test.util.ChromeTabUtils; |
| 24 import org.chromium.chrome.test.util.TestHttpServerClient; |
| 25 import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils; |
| 26 |
| 27 import java.util.concurrent.atomic.AtomicReference; |
| 28 |
| 29 /** |
| 30 * Tests the ManageBookmarkActivity, which allows users to add and edit bookmark
s. |
| 31 */ |
| 32 public class ManageBookmarkActivityTest extends ChromeTabbedActivityTestBase { |
| 33 private static final long PAGE_LOAD_WAIT_TIMEOUT_SECONDS = scaleTimeout(10); |
| 34 private static final String DOM_DISTILLER_SCHEME = "chrome-distiller"; |
| 35 |
| 36 @Override |
| 37 protected void tearDown() throws Exception { |
| 38 super.tearDown(); |
| 39 |
| 40 // Normally the data is cleared on start up to allow debugging, but addi
ng bookmarks |
| 41 // via the test prevents adding them in the phone so we need to also cle
ar the app data |
| 42 // on shutdown. |
| 43 // TODO(tedchoc): figure out why. |
| 44 // |
| 45 // Disable for debugging a particular test. |
| 46 clearAppData(); |
| 47 } |
| 48 |
| 49 @Override |
| 50 public void startMainActivity() throws InterruptedException { |
| 51 startMainActivityFromLauncher(); |
| 52 } |
| 53 |
| 54 private String getMobileBookmarkFolderName() { |
| 55 Context context = getActivity(); |
| 56 // Purposely not run on the UI thread as the ChromeBrowserProvider does
not allow |
| 57 // execution on the UI thread. |
| 58 long mobileBookmarkFolderId = |
| 59 ChromeBrowserProviderClient.getMobileBookmarksFolderId(context); |
| 60 assertNotSame("Mobile bookmark folder ID returned as INVALID", |
| 61 ChromeBrowserProviderClient.INVALID_BOOKMARK_ID, mobileBookmarkF
olderId); |
| 62 BookmarkNode node = ChromeBrowserProviderClient.getBookmarkNode( |
| 63 context, mobileBookmarkFolderId, ChromeBrowserProviderClient.GET
_NODE); |
| 64 assertNotNull("Mobile bookmark node was null.", node); |
| 65 return node.name(); |
| 66 } |
| 67 |
| 68 private void assertFolderText(final AddEditBookmarkFragment addEditFragment, |
| 69 String expectedText) { |
| 70 final AtomicReference<String> actualTextContainer = new AtomicReference<
String>(); |
| 71 getInstrumentation().runOnMainSync(new Runnable() { |
| 72 @Override |
| 73 public void run() { |
| 74 Button button = (Button) addEditFragment.getView().findViewById( |
| 75 R.id.bookmark_folder_select); |
| 76 actualTextContainer.set(button.getText().toString()); |
| 77 } |
| 78 }); |
| 79 assertEquals(expectedText, actualTextContainer.get()); |
| 80 } |
| 81 |
| 82 @SmallTest |
| 83 @Feature({"Bookmarks", "Main"}) |
| 84 public void testAddBookmark() throws InterruptedException { |
| 85 ManageBookmarkActivity addActivity = BookmarkTestUtils |
| 86 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity())
; |
| 87 final AddEditBookmarkFragment addFragment = BookmarkTestUtils.loadAddEdi
tFragment( |
| 88 addActivity); |
| 89 BookmarkTestUtils.clickOkButton(this, addFragment); |
| 90 final ManageBookmarkActivity editActivity = BookmarkTestUtils |
| 91 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity())
; |
| 92 BookmarkTestUtils.loadAddEditFragment(editActivity); |
| 93 assertTrue("Expected " |
| 94 + "title to contain 'edit' when changing an existing bookmark", |
| 95 ((TextView) editActivity.findViewById(R.id.bookmark_action_title
)) |
| 96 .getText().toString().toLowerCase().contains("edit")); |
| 97 } |
| 98 |
| 99 @SmallTest |
| 100 @Feature({"Bookmarks"}) |
| 101 public void testAddInvalidBookmark() throws InterruptedException { |
| 102 // Tests that we do not crash while adding a bookmark with an invalid ur
l. |
| 103 ManageBookmarkActivity addActivity = BookmarkTestUtils |
| 104 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity())
; |
| 105 final AddEditBookmarkFragment addFragment = BookmarkTestUtils.loadAddEdi
tFragment( |
| 106 addActivity); |
| 107 |
| 108 getInstrumentation().runOnMainSync(new Runnable() { |
| 109 @Override |
| 110 public void run() { |
| 111 ((EditText) addFragment.getView().findViewById(R.id.bookmark_url
_input)) |
| 112 .setText("INVALID_URL"); |
| 113 } |
| 114 }); |
| 115 BookmarkTestUtils.clickOkButton(this, addFragment); |
| 116 getInstrumentation().waitForIdleSync(); |
| 117 } |
| 118 |
| 119 @SmallTest |
| 120 @Feature({"Bookmarks"}) |
| 121 public void testMobileFolderIsDefault() throws InterruptedException { |
| 122 String mobileBookmarksName = getMobileBookmarkFolderName(); |
| 123 |
| 124 // Initial "Add Bookmark" ui should have default |
| 125 ManageBookmarkActivity addActivity = BookmarkTestUtils |
| 126 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity())
; |
| 127 AddEditBookmarkFragment addFragment = BookmarkTestUtils.loadAddEditFragm
ent(addActivity); |
| 128 assertFolderText(addFragment, mobileBookmarksName); |
| 129 |
| 130 BookmarkTestUtils.clickSelectFolderButton(this, addFragment); |
| 131 getInstrumentation().waitForIdleSync(); |
| 132 final SelectBookmarkFolderFragment selectedFolder = BookmarkTestUtils.lo
adSelectFragment( |
| 133 addActivity); |
| 134 |
| 135 BookmarkTestUtils.clickNewFolderButton(this, selectedFolder); |
| 136 getInstrumentation().waitForIdleSync(); |
| 137 final AddEditBookmarkFragment addNewFolderFragment = BookmarkTestUtils |
| 138 .loadAddFolderFragment(addActivity); |
| 139 assertFolderText(addNewFolderFragment, mobileBookmarksName); |
| 140 } |
| 141 |
| 142 @SmallTest |
| 143 @Feature({"Bookmarks"}) |
| 144 public void testAddFolder() throws InterruptedException { |
| 145 ManageBookmarkActivity addActivity = BookmarkTestUtils |
| 146 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity())
; |
| 147 final AddEditBookmarkFragment addFragment = |
| 148 BookmarkTestUtils.loadAddEditFragment(addActivity); |
| 149 |
| 150 BookmarkTestUtils.clickSelectFolderButton(this, addFragment); |
| 151 getInstrumentation().waitForIdleSync(); |
| 152 SelectBookmarkFolderFragment selectedFolder = BookmarkTestUtils.loadSele
ctFragment( |
| 153 addActivity); |
| 154 |
| 155 BookmarkTestUtils.clickNewFolderButton(this, selectedFolder); |
| 156 getInstrumentation().waitForIdleSync(); |
| 157 final AddEditBookmarkFragment addNewFolderFragment = |
| 158 BookmarkTestUtils.loadAddFolderFragment(addActivity); |
| 159 |
| 160 final String testFolderName = "Test Folder"; |
| 161 getInstrumentation().runOnMainSync(new Runnable() { |
| 162 @Override |
| 163 public void run() { |
| 164 ((EditText) addNewFolderFragment.getView().findViewById(R.id.boo
kmark_title_input)) |
| 165 .setText(testFolderName); |
| 166 } |
| 167 }); |
| 168 BookmarkTestUtils.clickOkButton(this, addNewFolderFragment); |
| 169 assertFolderText(addFragment, testFolderName); |
| 170 } |
| 171 |
| 172 /** |
| 173 * Add bookmark in reader mode and see if the saved URL is the original one. |
| 174 * TODO(wychen): test enhanced bookmark as well? |
| 175 */ |
| 176 // @SmallTest |
| 177 // @Feature({"Bookmarks"}) |
| 178 // crbug.com/464794 |
| 179 @FlakyTest |
| 180 public void testAddReaderModeBookmark() throws InterruptedException { |
| 181 String url = TestHttpServerClient.getUrl("chrome/test/data/android/googl
e.html"); |
| 182 String distillerUrl = |
| 183 DomDistillerUrlUtils.getDistillerViewUrlFromUrl(DOM_DISTILLER_SC
HEME, url); |
| 184 loadUrl(distillerUrl); |
| 185 ChromeTabUtils.waitForTabPageLoaded(getActivity().getActivityTab(), dist
illerUrl); |
| 186 |
| 187 ManageBookmarkActivity addActivity = BookmarkTestUtils |
| 188 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity())
; |
| 189 BookmarkTestUtils.loadAddEditFragment(addActivity); |
| 190 |
| 191 assertEquals(url, |
| 192 ((TextView) addActivity.findViewById(R.id.bookmark_url_input)) |
| 193 .getText().toString()); |
| 194 } |
| 195 |
| 196 /** |
| 197 * Add an existing bookmark in reader mode and see if it is recognized as ex
isting. |
| 198 */ |
| 199 // @SmallTest |
| 200 // @Feature({"Bookmarks"}) |
| 201 // crbug.com/464794 |
| 202 @FlakyTest |
| 203 public void testAddExistingReaderModeBookmark() throws InterruptedException
{ |
| 204 String url = TestHttpServerClient.getUrl("chrome/test/data/android/googl
e.html"); |
| 205 String distillerUrl = |
| 206 DomDistillerUrlUtils.getDistillerViewUrlFromUrl(DOM_DISTILLER_SC
HEME, url); |
| 207 loadUrl(distillerUrl); |
| 208 ChromeTabUtils.waitForTabPageLoaded(getActivity().getActivityTab(), dist
illerUrl); |
| 209 |
| 210 ManageBookmarkActivity addActivity = BookmarkTestUtils |
| 211 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity())
; |
| 212 final AddEditBookmarkFragment addFragment = BookmarkTestUtils.loadAddEdi
tFragment( |
| 213 addActivity); |
| 214 |
| 215 assertTrue("Expected " |
| 216 + "title to contain 'add' when adding a new bookmark", |
| 217 ((TextView) addActivity.findViewById(R.id.bookmark_action_title)
) |
| 218 .getText().toString().toLowerCase().contains("add")); |
| 219 BookmarkTestUtils.clickOkButton(this, addFragment); |
| 220 |
| 221 |
| 222 final ManageBookmarkActivity editActivity = BookmarkTestUtils |
| 223 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity())
; |
| 224 BookmarkTestUtils.loadAddEditFragment(editActivity); |
| 225 |
| 226 assertTrue("Expected " |
| 227 + "title to contain 'edit' when changing an existing bookmark", |
| 228 ((TextView) editActivity.findViewById(R.id.bookmark_action_title
)) |
| 229 .getText().toString().toLowerCase().contains("edit")); |
| 230 } |
| 231 } |
OLD | NEW |