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.
5 package org
.chromium
.chrome
.browser
.bookmark
;
7 import static org
.chromium
.base
.test
.util
.ScalableTimeout
.scaleTimeout
;
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
;
16 import com
.google
.android
.apps
.chrome
.R
;
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
;
27 import java
.util
.concurrent
.atomic
.AtomicReference
;
30 * Tests the ManageBookmarkActivity, which allows users to add and edit bookmarks.
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";
37 protected void tearDown() throws Exception
{
40 // Normally the data is cleared on start up to allow debugging, but adding bookmarks
41 // via the test prevents adding them in the phone so we need to also clear the app data
43 // TODO(tedchoc): figure out why.
45 // Disable for debugging a particular test.
50 public void startMainActivity() throws InterruptedException
{
51 startMainActivityFromLauncher();
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
, mobileBookmarkFolderId
);
62 BookmarkNode node
= ChromeBrowserProviderClient
.getBookmarkNode(
63 context
, mobileBookmarkFolderId
, ChromeBrowserProviderClient
.GET_NODE
);
64 assertNotNull("Mobile bookmark node was null.", node
);
68 private void assertFolderText(final AddEditBookmarkFragment addEditFragment
,
69 String expectedText
) {
70 final AtomicReference
<String
> actualTextContainer
= new AtomicReference
<String
>();
71 getInstrumentation().runOnMainSync(new Runnable() {
74 Button button
= (Button
) addEditFragment
.getView().findViewById(
75 R
.id
.bookmark_folder_select
);
76 actualTextContainer
.set(button
.getText().toString());
79 assertEquals(expectedText
, actualTextContainer
.get());
83 @Feature({"Bookmarks", "Main"})
84 public void testAddBookmark() throws InterruptedException
{
85 ManageBookmarkActivity addActivity
= BookmarkTestUtils
86 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity());
87 final AddEditBookmarkFragment addFragment
= BookmarkTestUtils
.loadAddEditFragment(
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"));
100 @Feature({"Bookmarks"})
101 public void testAddInvalidBookmark() throws InterruptedException
{
102 // Tests that we do not crash while adding a bookmark with an invalid url.
103 ManageBookmarkActivity addActivity
= BookmarkTestUtils
104 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity());
105 final AddEditBookmarkFragment addFragment
= BookmarkTestUtils
.loadAddEditFragment(
108 getInstrumentation().runOnMainSync(new Runnable() {
111 ((EditText
) addFragment
.getView().findViewById(R
.id
.bookmark_url_input
))
112 .setText("INVALID_URL");
115 BookmarkTestUtils
.clickOkButton(this, addFragment
);
116 getInstrumentation().waitForIdleSync();
120 @Feature({"Bookmarks"})
121 public void testMobileFolderIsDefault() throws InterruptedException
{
122 String mobileBookmarksName
= getMobileBookmarkFolderName();
124 // Initial "Add Bookmark" ui should have default
125 ManageBookmarkActivity addActivity
= BookmarkTestUtils
126 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity());
127 AddEditBookmarkFragment addFragment
= BookmarkTestUtils
.loadAddEditFragment(addActivity
);
128 assertFolderText(addFragment
, mobileBookmarksName
);
130 BookmarkTestUtils
.clickSelectFolderButton(this, addFragment
);
131 getInstrumentation().waitForIdleSync();
132 final SelectBookmarkFolderFragment selectedFolder
= BookmarkTestUtils
.loadSelectFragment(
135 BookmarkTestUtils
.clickNewFolderButton(this, selectedFolder
);
136 getInstrumentation().waitForIdleSync();
137 final AddEditBookmarkFragment addNewFolderFragment
= BookmarkTestUtils
138 .loadAddFolderFragment(addActivity
);
139 assertFolderText(addNewFolderFragment
, mobileBookmarksName
);
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
);
150 BookmarkTestUtils
.clickSelectFolderButton(this, addFragment
);
151 getInstrumentation().waitForIdleSync();
152 SelectBookmarkFolderFragment selectedFolder
= BookmarkTestUtils
.loadSelectFragment(
155 BookmarkTestUtils
.clickNewFolderButton(this, selectedFolder
);
156 getInstrumentation().waitForIdleSync();
157 final AddEditBookmarkFragment addNewFolderFragment
=
158 BookmarkTestUtils
.loadAddFolderFragment(addActivity
);
160 final String testFolderName
= "Test Folder";
161 getInstrumentation().runOnMainSync(new Runnable() {
164 ((EditText
) addNewFolderFragment
.getView().findViewById(R
.id
.bookmark_title_input
))
165 .setText(testFolderName
);
168 BookmarkTestUtils
.clickOkButton(this, addNewFolderFragment
);
169 assertFolderText(addFragment
, testFolderName
);
173 * Add bookmark in reader mode and see if the saved URL is the original one.
174 * TODO(wychen): test enhanced bookmark as well?
177 // @Feature({"Bookmarks"})
180 public void testAddReaderModeBookmark() throws InterruptedException
{
181 String url
= TestHttpServerClient
.getUrl("chrome/test/data/android/google.html");
182 String distillerUrl
=
183 DomDistillerUrlUtils
.getDistillerViewUrlFromUrl(DOM_DISTILLER_SCHEME
, url
);
184 loadUrl(distillerUrl
);
185 ChromeTabUtils
.waitForTabPageLoaded(getActivity().getActivityTab(), distillerUrl
);
187 ManageBookmarkActivity addActivity
= BookmarkTestUtils
188 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity());
189 BookmarkTestUtils
.loadAddEditFragment(addActivity
);
192 ((TextView
) addActivity
.findViewById(R
.id
.bookmark_url_input
))
193 .getText().toString());
197 * Add an existing bookmark in reader mode and see if it is recognized as existing.
200 // @Feature({"Bookmarks"})
203 public void testAddExistingReaderModeBookmark() throws InterruptedException
{
204 String url
= TestHttpServerClient
.getUrl("chrome/test/data/android/google.html");
205 String distillerUrl
=
206 DomDistillerUrlUtils
.getDistillerViewUrlFromUrl(DOM_DISTILLER_SCHEME
, url
);
207 loadUrl(distillerUrl
);
208 ChromeTabUtils
.waitForTabPageLoaded(getActivity().getActivityTab(), distillerUrl
);
210 ManageBookmarkActivity addActivity
= BookmarkTestUtils
211 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity());
212 final AddEditBookmarkFragment addFragment
= BookmarkTestUtils
.loadAddEditFragment(
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
);
222 final ManageBookmarkActivity editActivity
= BookmarkTestUtils
223 .selectBookmarkItemFromMenu(getInstrumentation(), getActivity());
224 BookmarkTestUtils
.loadAddEditFragment(editActivity
);
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"));