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 /** |
| 6 * This is a simple framework for a test of an Application. See |
| 7 * {@link android.test.ApplicationTestCase ApplicationTestCase} for more |
| 8 * information on how to write and extend Application tests. |
| 9 */ |
| 10 |
| 11 package org.chromium.chrome.browser.widget.findinpage; |
| 12 |
| 13 import android.test.suitebuilder.annotation.MediumTest; |
| 14 import android.test.suitebuilder.annotation.SmallTest; |
| 15 import android.view.KeyCharacterMap; |
| 16 import android.view.KeyEvent; |
| 17 import android.view.View; |
| 18 import android.widget.EditText; |
| 19 import android.widget.TextView; |
| 20 |
| 21 import com.google.android.apps.chrome.R; |
| 22 |
| 23 import org.chromium.base.ThreadUtils; |
| 24 import org.chromium.base.test.util.Feature; |
| 25 import org.chromium.chrome.test.ChromeTabbedActivityTestBase; |
| 26 import org.chromium.chrome.test.util.MenuUtils; |
| 27 import org.chromium.chrome.test.util.TestHttpServerClient; |
| 28 import org.chromium.content.browser.test.util.Criteria; |
| 29 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 30 import org.chromium.content.browser.test.util.KeyUtils; |
| 31 import org.chromium.content.browser.test.util.UiUtils; |
| 32 |
| 33 /** |
| 34 * Find in page tests. |
| 35 */ |
| 36 public class FindTest extends ChromeTabbedActivityTestBase { |
| 37 private static final String FILEPATH = "chrome/test/data/android/find/test.h
tml"; |
| 38 |
| 39 /** |
| 40 * Returns the FindResults text. |
| 41 */ |
| 42 private String waitForFindResults(final String expectedResult) throws Interr
uptedException { |
| 43 final TextView findResults = (TextView) getActivity().findViewById(R.id.
find_status); |
| 44 assertNotNull(expectedResult); |
| 45 assertNotNull(findResults); |
| 46 assertTrue(CriteriaHelper.pollForUIThreadCriteria(new Criteria() { |
| 47 @Override |
| 48 public boolean isSatisfied() { |
| 49 return expectedResult.equals(findResults.getText()); |
| 50 } |
| 51 })); |
| 52 return findResults.getText().toString(); |
| 53 } |
| 54 |
| 55 /** |
| 56 * Find in page by invoking the 'find in page' menu item. |
| 57 * |
| 58 * @throws InterruptedException |
| 59 */ |
| 60 private void findInPageFromMenu() throws InterruptedException { |
| 61 MenuUtils.invokeCustomMenuActionSync(getInstrumentation(), |
| 62 getActivity(), R.id.find_in_page_id); |
| 63 |
| 64 waitForFindInPageVisibility(true); |
| 65 } |
| 66 |
| 67 private void waitForFindInPageVisibility(final boolean visible) throws Inter
ruptedException { |
| 68 assertTrue(CriteriaHelper.pollForUIThreadCriteria(new Criteria() { |
| 69 @Override |
| 70 public boolean isSatisfied() { |
| 71 FindToolbar findToolbar = (FindToolbar) getActivity().findViewBy
Id( |
| 72 R.id.find_toolbar); |
| 73 |
| 74 boolean isVisible = findToolbar != null && findToolbar.isShown()
; |
| 75 return (visible == isVisible) && !findToolbar.isAnimating(); |
| 76 } |
| 77 })); |
| 78 } |
| 79 |
| 80 private String findStringInPage(final String query, String expectedResult) |
| 81 throws InterruptedException { |
| 82 findInPageFromMenu(); |
| 83 // FindToolbar should automatically get focus. |
| 84 assertTrue("FindToolbar should have focus", |
| 85 getActivity().findViewById(R.id.find_query).hasFocus()); |
| 86 final TextView findQueryText = (TextView) getActivity().findViewById(R.i
d.find_query); |
| 87 assertNotNull(findQueryText); |
| 88 // We have to send each key 1-by-1 to trigger the right listeners in the
toolbar. |
| 89 KeyCharacterMap keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.V
IRTUAL_KEYBOARD); |
| 90 final KeyEvent[] events = keyCharacterMap.getEvents(query.toCharArray())
; |
| 91 assertNotNull(events); |
| 92 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 93 @Override |
| 94 public void run() { |
| 95 for (int i = 0; i < events.length; i++) { |
| 96 if (!findQueryText.dispatchKeyEventPreIme(events[i])) { |
| 97 findQueryText.dispatchKeyEvent(events[i]); |
| 98 } |
| 99 } |
| 100 } |
| 101 }); |
| 102 return waitForFindResults(expectedResult); |
| 103 } |
| 104 |
| 105 private void loadTestAndVerifyFindInPage(String query, String expectedResult
) |
| 106 throws InterruptedException { |
| 107 loadUrl(TestHttpServerClient.getUrl(FILEPATH)); |
| 108 String findResults = findStringInPage(query, expectedResult); |
| 109 assertTrue("Expected: " + expectedResult + " Got: " + findResults + " fo
r: " |
| 110 + TestHttpServerClient.getUrl(FILEPATH), |
| 111 findResults.contains(expectedResult)); |
| 112 } |
| 113 |
| 114 /** |
| 115 * Verify Find In Page is not case sensitive. |
| 116 */ |
| 117 @MediumTest |
| 118 @Feature({"FindInPage", "Main"}) |
| 119 public void testFind() throws InterruptedException { |
| 120 loadTestAndVerifyFindInPage("pitts", "1/7"); |
| 121 } |
| 122 |
| 123 /** |
| 124 * Verify Find In Page with just one result. |
| 125 */ |
| 126 @MediumTest |
| 127 @Feature({"FindInPage"}) |
| 128 public void testFind101() throws InterruptedException { |
| 129 loadTestAndVerifyFindInPage("it", "1/101"); |
| 130 } |
| 131 |
| 132 /** |
| 133 * Verify Find In Page with a multi-line string. |
| 134 */ |
| 135 @MediumTest |
| 136 @Feature({"FindInPage"}) |
| 137 public void testFindMultiLine() throws InterruptedException { |
| 138 String multiLineSearchTerm = "This is the text of this document.\n" |
| 139 + " I am going to write the word \'Pitts\' 7 times. (That was on
e.)"; |
| 140 loadTestAndVerifyFindInPage(multiLineSearchTerm, "1/1"); |
| 141 } |
| 142 |
| 143 /** |
| 144 * Test for Find In Page with a multi-line string. Search string has an extr
a character |
| 145 * added to the end so it should not be found. |
| 146 */ |
| 147 @MediumTest |
| 148 @Feature({"FindInPage"}) |
| 149 public void testFindMultiLineFalse() throws InterruptedException { |
| 150 String multiLineSearchTerm = "aThis is the text of this document.\n" |
| 151 + " I am going to write the word \'Pitts\' 7 times. (That was on
e.)"; |
| 152 loadTestAndVerifyFindInPage(multiLineSearchTerm, "0/0"); |
| 153 } |
| 154 |
| 155 /** |
| 156 * Verify Find In Page Next button. |
| 157 */ |
| 158 @MediumTest |
| 159 @Feature({"FindInPage"}) |
| 160 public void testFindNext() throws InterruptedException { |
| 161 String query = "pitts"; |
| 162 loadTestAndVerifyFindInPage(query, "1/7"); |
| 163 // TODO(jaydeepmehta): Verify number of results and match against boxes
drawn. |
| 164 singleClickView(getActivity().findViewById(R.id.find_next_button)); |
| 165 waitForFindResults("2/7"); |
| 166 for (int i = 2; i <= 7; i++) { |
| 167 singleClickView(getActivity().findViewById(R.id.find_next_button)); |
| 168 } |
| 169 waitForFindResults("1/7"); |
| 170 } |
| 171 |
| 172 /** |
| 173 * Verify Find In Page Next/Previous button. |
| 174 */ |
| 175 @MediumTest |
| 176 @Feature({"FindInPage"}) |
| 177 public void testFindNextPrevious() throws InterruptedException { |
| 178 String query = "pitts"; |
| 179 loadTestAndVerifyFindInPage(query, "1/7"); |
| 180 // TODO(jaydeepmehta): Verify number of results and match against boxes
drawn. |
| 181 singleClickView(getActivity().findViewById(R.id.find_next_button)); |
| 182 waitForFindResults("2/7"); |
| 183 singleClickView(getActivity().findViewById(R.id.find_prev_button)); |
| 184 waitForFindResults("1/7"); |
| 185 } |
| 186 |
| 187 @MediumTest |
| 188 @Feature({"FindInPage"}) |
| 189 public void testResultsBarInitiallyVisible() throws InterruptedException { |
| 190 loadUrl(TestHttpServerClient.getUrl(FILEPATH)); |
| 191 findInPageFromMenu(); |
| 192 FindToolbar findToolbar = (FindToolbar) getActivity().findViewById(R.id.
find_toolbar); |
| 193 View resultBar = findToolbar.getFindResultBar(); |
| 194 assertEquals(View.VISIBLE, resultBar.getVisibility()); |
| 195 } |
| 196 |
| 197 @MediumTest |
| 198 @Feature({"FindInPage"}) |
| 199 public void testResultsBarVisibleAfterTypingText() throws InterruptedExcepti
on { |
| 200 loadUrl(TestHttpServerClient.getUrl(FILEPATH)); |
| 201 findInPageFromMenu(); |
| 202 FindToolbar findToolbar = (FindToolbar) getActivity().findViewById(R.id.
find_toolbar); |
| 203 View resultBar = findToolbar.getFindResultBar(); |
| 204 assertNotNull(resultBar); |
| 205 final TextView findQueryText = (TextView) findToolbar.findViewById(R.id.
find_query); |
| 206 assertNotNull(findQueryText); |
| 207 |
| 208 KeyUtils.singleKeyEventView(getInstrumentation(), findQueryText, KeyEven
t.KEYCODE_T); |
| 209 assertEquals(View.VISIBLE, resultBar.getVisibility()); |
| 210 KeyUtils.singleKeyEventView(getInstrumentation(), findQueryText, KeyEven
t.KEYCODE_DEL); |
| 211 assertEquals(View.VISIBLE, resultBar.getVisibility()); |
| 212 } |
| 213 |
| 214 /** |
| 215 * Verify Find In Page isn't dismissed and matches no results |
| 216 * if invoked with an empty string. |
| 217 */ |
| 218 @MediumTest |
| 219 @Feature({"FindInPage"}) |
| 220 public void testFindDismissOnEmptyString() throws InterruptedException { |
| 221 loadUrl(TestHttpServerClient.getUrl(FILEPATH)); |
| 222 findInPageFromMenu(); |
| 223 |
| 224 FindToolbar findToolbar = (FindToolbar) getActivity().findViewById(R.id.
find_toolbar); |
| 225 assertNotNull(findToolbar); |
| 226 assertEquals(View.VISIBLE, findToolbar.getVisibility()); |
| 227 final TextView findQueryText = (TextView) findToolbar.findViewById(R.id.
find_query); |
| 228 assertNotNull(findQueryText); |
| 229 KeyUtils.singleKeyEventView(getInstrumentation(), findQueryText, KeyEven
t.KEYCODE_T); |
| 230 KeyUtils.singleKeyEventView(getInstrumentation(), findQueryText, KeyEven
t.KEYCODE_DEL); |
| 231 KeyUtils.singleKeyEventView(getInstrumentation(), findQueryText, KeyEven
t.KEYCODE_ENTER); |
| 232 |
| 233 assertEquals(View.VISIBLE, findToolbar.getVisibility()); |
| 234 |
| 235 String findResults = waitForFindResults(""); |
| 236 assertEquals(0, findResults.length()); |
| 237 } |
| 238 |
| 239 /** |
| 240 * Verify FIP in IncognitoTabs. |
| 241 */ |
| 242 @SmallTest |
| 243 @Feature({"FindInPage"}) |
| 244 public void testFindNextPreviousIncognitoTab() throws InterruptedException { |
| 245 String query = "pitts"; |
| 246 newIncognitoTabFromMenu(); |
| 247 loadTestAndVerifyFindInPage(query, "1/7"); |
| 248 // TODO(jaydeepmehta): Verify number of results and match against boxes
drawn. |
| 249 singleClickView(getActivity().findViewById(R.id.find_next_button)); |
| 250 waitForFindResults("2/7"); |
| 251 singleClickView(getActivity().findViewById(R.id.find_prev_button)); |
| 252 waitForFindResults("1/7"); |
| 253 } |
| 254 |
| 255 /** |
| 256 * Verify Find in Page text isnt restored on Incognito Tabs. |
| 257 */ |
| 258 @MediumTest |
| 259 @Feature({"FindInPage"}) |
| 260 public void testFipTextNotRestoredIncognitoTab() throws InterruptedException
{ |
| 261 newIncognitoTabFromMenu(); |
| 262 loadTestAndVerifyFindInPage("pitts", "1/7"); |
| 263 // close the fip |
| 264 final View v = getActivity().findViewById(R.id.close_find_button); |
| 265 singleClickView(v); |
| 266 waitForFindInPageVisibility(false); |
| 267 |
| 268 // Reopen and check the text. |
| 269 findInPageFromMenu(); |
| 270 UiUtils.settleDownUI(getInstrumentation()); |
| 271 // Verify the text content. |
| 272 EditText e = (EditText) getActivity().findViewById(R.id.find_query); |
| 273 String myText = e.getText().toString(); |
| 274 assertTrue("expected empty string : " + myText, myText.isEmpty()); |
| 275 } |
| 276 |
| 277 @Override |
| 278 public void startMainActivity() throws InterruptedException { |
| 279 startMainActivityOnBlankPage(); |
| 280 } |
| 281 } |
OLD | NEW |