paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 1 | // Copyright 2016 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 | #include "base/command_line.h" |
| 6 | #include "base/strings/utf_string_conversions.h" |
Makoto Shimazu | 3266e2f5 | 2017-10-31 04:30:07 | [diff] [blame] | 7 | #include "build/build_config.h" |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 8 | #include "content/browser/web_contents/web_contents_impl.h" |
| 9 | #include "content/public/browser/notification_types.h" |
| 10 | #include "content/public/common/content_switches.h" |
paulmeyer | 3ac612d | 2016-09-30 19:21:06 | [diff] [blame] | 11 | #include "content/public/test/browser_test_utils.h" |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 12 | #include "content/public/test/content_browser_test.h" |
| 13 | #include "content/public/test/content_browser_test_utils.h" |
paulmeyer | da992f5 | 2017-01-27 17:11:28 | [diff] [blame] | 14 | #include "content/public/test/find_test_utils.h" |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 15 | #include "content/public/test/test_navigation_observer.h" |
| 16 | #include "content/public/test/test_utils.h" |
| 17 | #include "content/shell/browser/shell.h" |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 18 | #include "content/test/content_browser_test_utils_internal.h" |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 19 | #include "net/dns/mock_host_resolver.h" |
| 20 | #include "third_party/WebKit/public/web/WebFindOptions.h" |
| 21 | |
| 22 | namespace content { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | const int kInvalidId = -1; |
| 27 | |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 28 | } // namespace |
| 29 | |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 30 | class FindRequestManagerTest : public ContentBrowserTest, |
| 31 | public testing::WithParamInterface<bool> { |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 32 | public: |
| 33 | FindRequestManagerTest() |
| 34 | : normal_delegate_(nullptr), |
| 35 | last_request_id_(0) {} |
| 36 | ~FindRequestManagerTest() override {} |
| 37 | |
| 38 | void SetUpOnMainThread() override { |
| 39 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 40 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 41 | |
| 42 | // Swap the WebContents's delegate for our test delegate. |
| 43 | normal_delegate_ = contents()->GetDelegate(); |
| 44 | contents()->SetDelegate(&test_delegate_); |
| 45 | } |
| 46 | |
| 47 | void TearDownOnMainThread() override { |
| 48 | // Swap the WebContents's delegate back to its usual delegate. |
| 49 | contents()->SetDelegate(normal_delegate_); |
| 50 | } |
| 51 | |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 52 | #if !defined(OS_ANDROID) |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 53 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 54 | IsolateAllSitesForTesting(command_line); |
| 55 | } |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 56 | #endif |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 57 | |
| 58 | protected: |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 59 | // Navigates to |url| and waits for it to finish loading. |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 60 | void LoadAndWait(const std::string& url) { |
| 61 | TestNavigationObserver navigation_observer(contents()); |
| 62 | NavigateToURL(shell(), embedded_test_server()->GetURL("a.com", url)); |
paulmeyer | da992f5 | 2017-01-27 17:11:28 | [diff] [blame] | 63 | ASSERT_TRUE(navigation_observer.last_navigation_succeeded()); |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 64 | } |
| 65 | |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 66 | // Loads a multi-frame page. The page will have a full binary frame tree of |
| 67 | // height |height|. If |cross_process| is true, child frames will be loaded |
| 68 | // cross-process. |
| 69 | void LoadMultiFramePage(int height, bool cross_process) { |
| 70 | LoadAndWait("/find_in_page_multi_frame.html"); |
paulmeyer | 74f68c9 | 2016-08-12 22:34:13 | [diff] [blame] | 71 | FrameTreeNode* root = contents()->GetFrameTree()->root(); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 72 | LoadMultiFramePageChildFrames(height, cross_process, root); |
| 73 | } |
| 74 | |
| 75 | // Reloads the child frame cross-process. |
| 76 | void MakeChildFrameCrossProcess() { |
paulmeyer | 74f68c9 | 2016-08-12 22:34:13 | [diff] [blame] | 77 | FrameTreeNode* root = contents()->GetFrameTree()->root(); |
| 78 | FrameTreeNode* child = root->child_at(0); |
| 79 | GURL url(embedded_test_server()->GetURL( |
| 80 | "b.com", child->current_url().path())); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 81 | |
| 82 | TestNavigationObserver observer(shell()->web_contents()); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 83 | NavigateFrameToURL(child, url); |
| 84 | EXPECT_EQ(url, observer.last_navigation_url()); |
| 85 | EXPECT_TRUE(observer.last_navigation_succeeded()); |
| 86 | } |
| 87 | |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 88 | void Find(const std::string& search_text, |
| 89 | const blink::WebFindOptions& options) { |
paulmeyer | 2f06e61 | 2016-08-10 17:39:02 | [diff] [blame] | 90 | delegate()->UpdateLastRequest(++last_request_id_); |
| 91 | contents()->Find(last_request_id_, |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 92 | base::UTF8ToUTF16(search_text), |
| 93 | options); |
| 94 | } |
| 95 | |
paulmeyer | 74f68c9 | 2016-08-12 22:34:13 | [diff] [blame] | 96 | WebContentsImpl* contents() const { |
| 97 | return static_cast<WebContentsImpl*>(shell()->web_contents()); |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 98 | } |
| 99 | |
paulmeyer | da992f5 | 2017-01-27 17:11:28 | [diff] [blame] | 100 | FindTestWebContentsDelegate* delegate() const { |
| 101 | return static_cast<FindTestWebContentsDelegate*>(contents()->GetDelegate()); |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | int last_request_id() const { |
| 105 | return last_request_id_; |
| 106 | } |
| 107 | |
| 108 | private: |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 109 | // Helper function for LoadMultiFramePage. Loads child frames until the frame |
| 110 | // tree rooted at |root| is a full binary tree of height |height|. |
| 111 | void LoadMultiFramePageChildFrames(int height, |
| 112 | bool cross_process, |
| 113 | FrameTreeNode* root) { |
| 114 | if (height == 0) |
| 115 | return; |
| 116 | |
| 117 | std::string hostname = root->current_origin().host(); |
| 118 | if (cross_process) |
| 119 | hostname.insert(0, 1, 'a'); |
| 120 | GURL url(embedded_test_server()->GetURL(hostname, |
| 121 | "/find_in_page_multi_frame.html")); |
| 122 | |
| 123 | TestNavigationObserver observer(shell()->web_contents()); |
| 124 | |
| 125 | FrameTreeNode* child = root->child_at(0); |
| 126 | NavigateFrameToURL(child, url); |
| 127 | EXPECT_TRUE(observer.last_navigation_succeeded()); |
| 128 | LoadMultiFramePageChildFrames(height - 1, cross_process, child); |
| 129 | |
| 130 | child = root->child_at(1); |
| 131 | NavigateFrameToURL(child, url); |
| 132 | EXPECT_TRUE(observer.last_navigation_succeeded()); |
| 133 | LoadMultiFramePageChildFrames(height - 1, cross_process, child); |
| 134 | } |
| 135 | |
paulmeyer | da992f5 | 2017-01-27 17:11:28 | [diff] [blame] | 136 | FindTestWebContentsDelegate test_delegate_; |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 137 | WebContentsDelegate* normal_delegate_; |
| 138 | |
| 139 | // The ID of the last find request requested. |
| 140 | int last_request_id_; |
| 141 | |
| 142 | DISALLOW_COPY_AND_ASSIGN(FindRequestManagerTest); |
| 143 | }; |
| 144 | |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 145 | // Frames are made cross-process when the test param is set to |
| 146 | // true. Cross-process frames are not used on android. |
| 147 | #if defined(OS_ANDROID) |
| 148 | INSTANTIATE_TEST_CASE_P( |
| 149 | FindRequestManagerTests, FindRequestManagerTest, testing::Values(false)); |
| 150 | #else |
| 151 | INSTANTIATE_TEST_CASE_P( |
| 152 | FindRequestManagerTests, FindRequestManagerTest, testing::Bool()); |
| 153 | #endif |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 154 | |
torne | 65fee819 | 2016-10-25 12:09:03 | [diff] [blame] | 155 | // TODO(crbug.com/615291): These tests frequently fail on Android. |
| 156 | #if defined(OS_ANDROID) |
paulmeyer | cc9a00f9 | 2016-07-06 13:44:27 | [diff] [blame] | 157 | #define MAYBE(x) DISABLED_##x |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 158 | #else |
paulmeyer | cc9a00f9 | 2016-07-06 13:44:27 | [diff] [blame] | 159 | #define MAYBE(x) x |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 160 | #endif |
| 161 | |
| 162 | |
| 163 | // Tests basic find-in-page functionality (such as searching forward and |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 164 | // backward) and check for correct results at each step. |
paulmeyer | cc9a00f9 | 2016-07-06 13:44:27 | [diff] [blame] | 165 | IN_PROC_BROWSER_TEST_P(FindRequestManagerTest, MAYBE(Basic)) { |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 166 | LoadAndWait("/find_in_page.html"); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 167 | if (GetParam()) |
| 168 | MakeChildFrameCrossProcess(); |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 169 | |
| 170 | blink::WebFindOptions options; |
| 171 | Find("result", options); |
paulmeyer | 2f06e61 | 2016-08-10 17:39:02 | [diff] [blame] | 172 | delegate()->WaitForFinalReply(); |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 173 | |
| 174 | FindResults results = delegate()->GetFindResults(); |
| 175 | EXPECT_EQ(last_request_id(), results.request_id); |
| 176 | EXPECT_EQ(19, results.number_of_matches); |
| 177 | EXPECT_EQ(1, results.active_match_ordinal); |
| 178 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 179 | options.find_next = true; |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 180 | for (int i = 2; i <= 10; ++i) { |
| 181 | Find("result", options); |
paulmeyer | 2f06e61 | 2016-08-10 17:39:02 | [diff] [blame] | 182 | delegate()->WaitForFinalReply(); |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 183 | |
| 184 | results = delegate()->GetFindResults(); |
| 185 | EXPECT_EQ(last_request_id(), results.request_id); |
| 186 | EXPECT_EQ(19, results.number_of_matches); |
| 187 | EXPECT_EQ(i, results.active_match_ordinal); |
| 188 | } |
| 189 | |
| 190 | options.forward = false; |
| 191 | for (int i = 9; i >= 5; --i) { |
| 192 | Find("result", options); |
paulmeyer | 2f06e61 | 2016-08-10 17:39:02 | [diff] [blame] | 193 | delegate()->WaitForFinalReply(); |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 194 | |
| 195 | results = delegate()->GetFindResults(); |
| 196 | EXPECT_EQ(last_request_id(), results.request_id); |
| 197 | EXPECT_EQ(19, results.number_of_matches); |
| 198 | EXPECT_EQ(i, results.active_match_ordinal); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Tests searching for a word character-by-character, as would typically be done |
| 203 | // by a user typing into the find bar. |
paulmeyer | cc9a00f9 | 2016-07-06 13:44:27 | [diff] [blame] | 204 | IN_PROC_BROWSER_TEST_P(FindRequestManagerTest, MAYBE(CharacterByCharacter)) { |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 205 | LoadAndWait("/find_in_page.html"); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 206 | if (GetParam()) |
| 207 | MakeChildFrameCrossProcess(); |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 208 | |
| 209 | blink::WebFindOptions default_options; |
| 210 | Find("r", default_options); |
| 211 | Find("re", default_options); |
| 212 | Find("res", default_options); |
| 213 | Find("resu", default_options); |
| 214 | Find("resul", default_options); |
| 215 | Find("result", default_options); |
paulmeyer | 2f06e61 | 2016-08-10 17:39:02 | [diff] [blame] | 216 | delegate()->WaitForFinalReply(); |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 217 | |
| 218 | FindResults results = delegate()->GetFindResults(); |
| 219 | EXPECT_EQ(last_request_id(), results.request_id); |
| 220 | EXPECT_EQ(19, results.number_of_matches); |
| 221 | EXPECT_EQ(1, results.active_match_ordinal); |
| 222 | } |
| 223 | |
rjkroege | 4096df6 | 2016-12-16 00:49:08 | [diff] [blame] | 224 | // TODO(crbug.com/615291): This test frequently fails on Android. |
| 225 | // TODO(crbug.com/674742): This test is flaky on Win |
| 226 | #if defined(OS_ANDROID) || defined(OS_WIN) |
| 227 | #define MAYBE_RapidFire DISABLED_RapidFire |
| 228 | #else |
| 229 | #define MAYBE_RapidFire RapidFire |
| 230 | #endif |
| 231 | |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 232 | // Tests sending a large number of find requests subsequently. |
rjkroege | 4096df6 | 2016-12-16 00:49:08 | [diff] [blame] | 233 | IN_PROC_BROWSER_TEST_P(FindRequestManagerTest, MAYBE_RapidFire) { |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 234 | LoadAndWait("/find_in_page.html"); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 235 | if (GetParam()) |
| 236 | MakeChildFrameCrossProcess(); |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 237 | |
| 238 | blink::WebFindOptions options; |
| 239 | Find("result", options); |
| 240 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 241 | options.find_next = true; |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 242 | for (int i = 2; i <= 1000; ++i) |
| 243 | Find("result", options); |
paulmeyer | 2f06e61 | 2016-08-10 17:39:02 | [diff] [blame] | 244 | delegate()->WaitForFinalReply(); |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 245 | |
| 246 | FindResults results = delegate()->GetFindResults(); |
| 247 | EXPECT_EQ(last_request_id(), results.request_id); |
| 248 | EXPECT_EQ(19, results.number_of_matches); |
| 249 | EXPECT_EQ(last_request_id() % results.number_of_matches, |
| 250 | results.active_match_ordinal); |
| 251 | } |
| 252 | |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 253 | // Tests removing a frame during a find session. |
mek | 9843095 | 2016-11-29 01:34:46 | [diff] [blame] | 254 | // TODO(crbug.com/657331): Test is flaky on all platforms. |
| 255 | IN_PROC_BROWSER_TEST_P(FindRequestManagerTest, DISABLED_RemoveFrame) { |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 256 | LoadMultiFramePage(2 /* height */, GetParam() /* cross_process */); |
| 257 | |
| 258 | blink::WebFindOptions options; |
| 259 | Find("result", options); |
paulmeyer | dc6a644 | 2016-08-19 16:48:28 | [diff] [blame] | 260 | delegate()->WaitForFinalReply(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 261 | options.find_next = true; |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 262 | options.forward = false; |
| 263 | Find("result", options); |
| 264 | Find("result", options); |
| 265 | Find("result", options); |
| 266 | Find("result", options); |
| 267 | Find("result", options); |
paulmeyer | 2f06e61 | 2016-08-10 17:39:02 | [diff] [blame] | 268 | delegate()->WaitForFinalReply(); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 269 | |
| 270 | FindResults results = delegate()->GetFindResults(); |
| 271 | EXPECT_EQ(last_request_id(), results.request_id); |
| 272 | EXPECT_EQ(21, results.number_of_matches); |
| 273 | EXPECT_EQ(17, results.active_match_ordinal); |
| 274 | |
| 275 | // Remove a frame. |
paulmeyer | 74f68c9 | 2016-08-12 22:34:13 | [diff] [blame] | 276 | FrameTreeNode* root = contents()->GetFrameTree()->root(); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 277 | root->RemoveChild(root->child_at(0)); |
| 278 | |
| 279 | // The number of matches and active match ordinal should update automatically |
| 280 | // to exclude the matches from the removed frame. |
| 281 | results = delegate()->GetFindResults(); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 282 | EXPECT_EQ(12, results.number_of_matches); |
| 283 | EXPECT_EQ(8, results.active_match_ordinal); |
paulmeyer | 3ac612d | 2016-09-30 19:21:06 | [diff] [blame] | 284 | } |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 285 | |
paulmeyer | 3ac612d | 2016-09-30 19:21:06 | [diff] [blame] | 286 | // Tests adding a frame during a find session. |
mek | 9843095 | 2016-11-29 01:34:46 | [diff] [blame] | 287 | // TODO(crbug.com/657331): Test is flaky on all platforms. |
| 288 | IN_PROC_BROWSER_TEST_P(FindRequestManagerTest, DISABLED_AddFrame) { |
paulmeyer | 3ac612d | 2016-09-30 19:21:06 | [diff] [blame] | 289 | LoadMultiFramePage(2 /* height */, GetParam() /* cross_process */); |
| 290 | |
| 291 | blink::WebFindOptions options; |
| 292 | Find("result", options); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 293 | options.find_next = true; |
paulmeyer | 3ac612d | 2016-09-30 19:21:06 | [diff] [blame] | 294 | Find("result", options); |
| 295 | Find("result", options); |
| 296 | Find("result", options); |
| 297 | Find("result", options); |
| 298 | delegate()->WaitForFinalReply(); |
| 299 | |
| 300 | FindResults results = delegate()->GetFindResults(); |
| 301 | EXPECT_EQ(last_request_id(), results.request_id); |
| 302 | EXPECT_EQ(21, results.number_of_matches); |
| 303 | EXPECT_EQ(5, results.active_match_ordinal); |
| 304 | |
| 305 | // Add a frame. It contains 5 new matches. |
| 306 | std::string url = embedded_test_server()->GetURL( |
| 307 | GetParam() ? "b.com" : "a.com", "/find_in_simple_page.html").spec(); |
| 308 | std::string script = std::string() + |
| 309 | "var frame = document.createElement('iframe');" + |
| 310 | "frame.src = '" + url + "';" + |
| 311 | "document.body.appendChild(frame);"; |
| 312 | delegate()->MarkNextReply(); |
| 313 | ASSERT_TRUE(ExecuteScript(shell(), script)); |
| 314 | delegate()->WaitForNextReply(); |
| 315 | |
| 316 | // The number of matches should update automatically to include the matches |
| 317 | // from the newly added frame. |
| 318 | results = delegate()->GetFindResults(); |
| 319 | EXPECT_EQ(26, results.number_of_matches); |
| 320 | EXPECT_EQ(5, results.active_match_ordinal); |
| 321 | } |
| 322 | |
| 323 | // Tests adding a frame during a find session where there were previously no |
| 324 | // matches. |
| 325 | IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, MAYBE(AddFrameAfterNoMatches)) { |
| 326 | TestNavigationObserver navigation_observer(contents()); |
| 327 | NavigateToURL(shell(), GURL("about:blank")); |
| 328 | EXPECT_TRUE(navigation_observer.last_navigation_succeeded()); |
| 329 | |
| 330 | blink::WebFindOptions default_options; |
| 331 | Find("result", default_options); |
| 332 | delegate()->WaitForFinalReply(); |
| 333 | |
| 334 | // Initially, there are no matches on the page. |
| 335 | FindResults results = delegate()->GetFindResults(); |
| 336 | EXPECT_EQ(last_request_id(), results.request_id); |
| 337 | EXPECT_EQ(0, results.number_of_matches); |
| 338 | EXPECT_EQ(0, results.active_match_ordinal); |
| 339 | |
| 340 | // Add a frame. It contains 5 new matches. |
| 341 | std::string url = |
| 342 | embedded_test_server()->GetURL("/find_in_simple_page.html").spec(); |
| 343 | std::string script = std::string() + |
| 344 | "var frame = document.createElement('iframe');" + |
| 345 | "frame.src = '" + url + "';" + |
| 346 | "document.body.appendChild(frame);"; |
| 347 | delegate()->MarkNextReply(); |
| 348 | ASSERT_TRUE(ExecuteScript(shell(), script)); |
| 349 | delegate()->WaitForNextReply(); |
| 350 | |
| 351 | // The matches from the new frame should be found automatically, and the first |
| 352 | // match in the frame should be activated. |
| 353 | results = delegate()->GetFindResults(); |
| 354 | EXPECT_EQ(5, results.number_of_matches); |
| 355 | EXPECT_EQ(1, results.active_match_ordinal); |
| 356 | } |
| 357 | |
| 358 | // Tests a frame navigating to a different page during a find session. |
| 359 | IN_PROC_BROWSER_TEST_P(FindRequestManagerTest, MAYBE(NavigateFrame)) { |
| 360 | LoadMultiFramePage(2 /* height */, GetParam() /* cross_process */); |
| 361 | |
| 362 | blink::WebFindOptions options; |
| 363 | Find("result", options); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 364 | options.find_next = true; |
paulmeyer | 3ac612d | 2016-09-30 19:21:06 | [diff] [blame] | 365 | options.forward = false; |
| 366 | Find("result", options); |
| 367 | Find("result", options); |
| 368 | Find("result", options); |
| 369 | delegate()->WaitForFinalReply(); |
| 370 | |
| 371 | FindResults results = delegate()->GetFindResults(); |
| 372 | EXPECT_EQ(last_request_id(), results.request_id); |
| 373 | EXPECT_EQ(21, results.number_of_matches); |
| 374 | EXPECT_EQ(19, results.active_match_ordinal); |
| 375 | |
| 376 | // Navigate one of the empty frames to a page with 5 matches. |
| 377 | FrameTreeNode* root = |
| 378 | static_cast<WebContentsImpl*>(shell()->web_contents())-> |
| 379 | GetFrameTree()->root(); |
| 380 | GURL url(embedded_test_server()->GetURL( |
| 381 | GetParam() ? "b.com" : "a.com", "/find_in_simple_page.html")); |
| 382 | delegate()->MarkNextReply(); |
| 383 | TestNavigationObserver navigation_observer(contents()); |
| 384 | NavigateFrameToURL(root->child_at(0)->child_at(1)->child_at(0), url); |
| 385 | EXPECT_TRUE(navigation_observer.last_navigation_succeeded()); |
| 386 | delegate()->WaitForNextReply(); |
| 387 | |
| 388 | // The navigation results in an extra reply before the one we care about. This |
| 389 | // extra reply happens because the RenderFrameHost changes before it navigates |
| 390 | // (because the navigation is cross-origin). The first reply will not change |
| 391 | // the number of matches because the frame that is navigating was empty |
| 392 | // before. |
| 393 | if (delegate()->GetFindResults().number_of_matches == 21) { |
| 394 | delegate()->MarkNextReply(); |
| 395 | delegate()->WaitForNextReply(); |
| 396 | } |
| 397 | |
| 398 | // The number of matches and the active match ordinal should update |
| 399 | // automatically to include the new matches. |
| 400 | results = delegate()->GetFindResults(); |
| 401 | EXPECT_EQ(26, results.number_of_matches); |
| 402 | EXPECT_EQ(24, results.active_match_ordinal); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | // Tests Searching in a hidden frame. Matches in the hidden frame should be |
| 406 | // ignored. |
paulmeyer | cc9a00f9 | 2016-07-06 13:44:27 | [diff] [blame] | 407 | IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, MAYBE(HiddenFrame)) { |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 408 | LoadAndWait("/find_in_hidden_frame.html"); |
| 409 | |
| 410 | blink::WebFindOptions default_options; |
| 411 | Find("hello", default_options); |
paulmeyer | 2f06e61 | 2016-08-10 17:39:02 | [diff] [blame] | 412 | delegate()->WaitForFinalReply(); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 413 | FindResults results = delegate()->GetFindResults(); |
| 414 | |
| 415 | EXPECT_EQ(last_request_id(), results.request_id); |
| 416 | EXPECT_EQ(1, results.number_of_matches); |
| 417 | EXPECT_EQ(1, results.active_match_ordinal); |
| 418 | } |
| 419 | |
paulmeyer | 74f68c9 | 2016-08-12 22:34:13 | [diff] [blame] | 420 | // Tests that new matches can be found in dynamically added text. |
| 421 | IN_PROC_BROWSER_TEST_P(FindRequestManagerTest, MAYBE(FindNewMatches)) { |
| 422 | LoadAndWait("/find_in_dynamic_page.html"); |
| 423 | |
| 424 | blink::WebFindOptions options; |
| 425 | Find("result", options); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 426 | options.find_next = true; |
paulmeyer | 74f68c9 | 2016-08-12 22:34:13 | [diff] [blame] | 427 | Find("result", options); |
| 428 | Find("result", options); |
| 429 | delegate()->WaitForFinalReply(); |
| 430 | |
| 431 | FindResults results = delegate()->GetFindResults(); |
| 432 | EXPECT_EQ(last_request_id(), results.request_id); |
| 433 | EXPECT_EQ(3, results.number_of_matches); |
| 434 | EXPECT_EQ(3, results.active_match_ordinal); |
| 435 | |
| 436 | // Dynamically add new text to the page. This text contains 5 new matches for |
| 437 | // "result". |
| 438 | ASSERT_TRUE(ExecuteScript(contents()->GetMainFrame(), "addNewText()")); |
| 439 | |
| 440 | Find("result", options); |
| 441 | delegate()->WaitForFinalReply(); |
| 442 | |
| 443 | results = delegate()->GetFindResults(); |
| 444 | EXPECT_EQ(last_request_id(), results.request_id); |
| 445 | EXPECT_EQ(8, results.number_of_matches); |
| 446 | EXPECT_EQ(4, results.active_match_ordinal); |
| 447 | } |
| 448 | |
Makoto Shimazu | 3266e2f5 | 2017-10-31 04:30:07 | [diff] [blame] | 449 | // TODO(crbug.com/615291): These tests frequently fail on Android. |
| 450 | // TODO(crbug.com/779912): Flaky timeout on Win7 (dbg). |
| 451 | #if defined(OS_ANDROID) || (defined(OS_WIN) && !defined(NDEBUG)) |
| 452 | #define MAYBE_FindInPage_Issue627799 DISABLED_FindInPage_Issue627799 |
| 453 | #else |
| 454 | #define MAYBE_FindInPage_Issue627799 FindInPage_Issue627799 |
| 455 | #endif |
| 456 | |
| 457 | IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, MAYBE_FindInPage_Issue627799) { |
paulmeyer | dc6a644 | 2016-08-19 16:48:28 | [diff] [blame] | 458 | LoadAndWait("/find_in_long_page.html"); |
| 459 | |
| 460 | blink::WebFindOptions options; |
| 461 | Find("42", options); |
| 462 | delegate()->WaitForFinalReply(); |
| 463 | |
| 464 | FindResults results = delegate()->GetFindResults(); |
| 465 | EXPECT_EQ(last_request_id(), results.request_id); |
| 466 | EXPECT_EQ(970, results.number_of_matches); |
| 467 | EXPECT_EQ(1, results.active_match_ordinal); |
| 468 | |
| 469 | delegate()->StartReplyRecord(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 470 | options.find_next = true; |
paulmeyer | dc6a644 | 2016-08-19 16:48:28 | [diff] [blame] | 471 | options.forward = false; |
| 472 | Find("42", options); |
| 473 | delegate()->WaitForFinalReply(); |
| 474 | |
| 475 | // This is the crux of the issue that this test guards against. Searching |
| 476 | // across the frame boundary should not cause the frame to be re-scoped. If |
| 477 | // the re-scope occurs, then we will see the number of matches change in one |
| 478 | // of the recorded find replies. |
| 479 | for (auto& reply : delegate()->GetReplyRecord()) { |
| 480 | EXPECT_EQ(last_request_id(), reply.request_id); |
| 481 | EXPECT_TRUE(reply.number_of_matches == kInvalidId || |
| 482 | reply.number_of_matches == results.number_of_matches); |
| 483 | } |
| 484 | } |
| 485 | |
paulmeyer | d3b32d5 | 2016-09-07 22:24:55 | [diff] [blame] | 486 | IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, MAYBE(FindInPage_Issue644448)) { |
| 487 | TestNavigationObserver navigation_observer(contents()); |
| 488 | NavigateToURL(shell(), GURL("about:blank")); |
| 489 | EXPECT_TRUE(navigation_observer.last_navigation_succeeded()); |
| 490 | |
| 491 | blink::WebFindOptions default_options; |
| 492 | Find("result", default_options); |
| 493 | delegate()->WaitForFinalReply(); |
| 494 | |
| 495 | // Initially, there are no matches on the page. |
| 496 | FindResults results = delegate()->GetFindResults(); |
| 497 | EXPECT_EQ(last_request_id(), results.request_id); |
| 498 | EXPECT_EQ(0, results.number_of_matches); |
| 499 | EXPECT_EQ(0, results.active_match_ordinal); |
| 500 | |
| 501 | // Load a page with matches. |
| 502 | LoadAndWait("/find_in_simple_page.html"); |
| 503 | |
| 504 | Find("result", default_options); |
| 505 | delegate()->WaitForFinalReply(); |
| 506 | |
| 507 | // There should now be matches found. When the bug was present, there were |
| 508 | // still no matches found. |
| 509 | results = delegate()->GetFindResults(); |
| 510 | EXPECT_EQ(last_request_id(), results.request_id); |
| 511 | EXPECT_EQ(5, results.number_of_matches); |
paulmeyer | d3b32d5 | 2016-09-07 22:24:55 | [diff] [blame] | 512 | } |
| 513 | |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 514 | #if defined(OS_ANDROID) |
| 515 | // Tests requesting find match rects. |
paulmeyer | cc9a00f9 | 2016-07-06 13:44:27 | [diff] [blame] | 516 | IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, MAYBE(FindMatchRects)) { |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 517 | LoadAndWait("/find_in_page.html"); |
| 518 | |
| 519 | blink::WebFindOptions default_options; |
| 520 | Find("result", default_options); |
paulmeyer | 2f06e61 | 2016-08-10 17:39:02 | [diff] [blame] | 521 | delegate()->WaitForFinalReply(); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 522 | EXPECT_EQ(19, delegate()->GetFindResults().number_of_matches); |
| 523 | |
| 524 | // Request the find match rects. |
| 525 | contents()->RequestFindMatchRects(-1); |
| 526 | delegate()->WaitForMatchRects(); |
| 527 | const std::vector<gfx::RectF>& rects = delegate()->find_match_rects(); |
| 528 | |
| 529 | // The first match should be active. |
| 530 | EXPECT_EQ(rects[0], delegate()->active_match_rect()); |
| 531 | |
| 532 | // All results after the first two should be between them in find-in-page |
| 533 | // coordinates. This is because results 2 to 19 are inside an iframe located |
| 534 | // between results 0 and 1. This applies to the fixed div too. |
| 535 | EXPECT_LT(rects[0].y(), rects[1].y()); |
| 536 | for (int i = 2; i < 19; ++i) { |
| 537 | EXPECT_LT(rects[0].y(), rects[i].y()); |
| 538 | EXPECT_GT(rects[1].y(), rects[i].y()); |
| 539 | } |
| 540 | |
| 541 | // Result 3 should be below results 2 and 4. This is caused by the CSS |
| 542 | // transform in the containing div. If the transform doesn't work then result |
| 543 | // 3 will be between results 2 and 4. |
| 544 | EXPECT_GT(rects[3].y(), rects[2].y()); |
| 545 | EXPECT_GT(rects[3].y(), rects[4].y()); |
| 546 | |
| 547 | // Results 6, 7, 8 and 9 should be one below the other in that same order. If |
| 548 | // overflow:scroll is not properly handled then result 8 would be below result |
| 549 | // 9 or result 7 above result 6 depending on the scroll. |
| 550 | EXPECT_LT(rects[6].y(), rects[7].y()); |
| 551 | EXPECT_LT(rects[7].y(), rects[8].y()); |
| 552 | EXPECT_LT(rects[8].y(), rects[9].y()); |
| 553 | |
| 554 | // Results 11, 12, 13 and 14 should be between results 10 and 15, as they are |
| 555 | // inside the table. |
| 556 | EXPECT_GT(rects[11].y(), rects[10].y()); |
| 557 | EXPECT_GT(rects[12].y(), rects[10].y()); |
| 558 | EXPECT_GT(rects[13].y(), rects[10].y()); |
| 559 | EXPECT_GT(rects[14].y(), rects[10].y()); |
| 560 | EXPECT_LT(rects[11].y(), rects[15].y()); |
| 561 | EXPECT_LT(rects[12].y(), rects[15].y()); |
| 562 | EXPECT_LT(rects[13].y(), rects[15].y()); |
| 563 | EXPECT_LT(rects[14].y(), rects[15].y()); |
| 564 | |
| 565 | // Result 11 should be above results 12, 13 and 14 as it's in the table |
| 566 | // header. |
| 567 | EXPECT_LT(rects[11].y(), rects[12].y()); |
| 568 | EXPECT_LT(rects[11].y(), rects[13].y()); |
| 569 | EXPECT_LT(rects[11].y(), rects[14].y()); |
| 570 | |
| 571 | // Result 11 should also be right of results 12, 13 and 14 because of the |
| 572 | // colspan. |
| 573 | EXPECT_GT(rects[11].x(), rects[12].x()); |
| 574 | EXPECT_GT(rects[11].x(), rects[13].x()); |
| 575 | EXPECT_GT(rects[11].x(), rects[14].x()); |
| 576 | |
| 577 | // Result 12 should be left of results 11, 13 and 14 in the table layout. |
| 578 | EXPECT_LT(rects[12].x(), rects[11].x()); |
| 579 | EXPECT_LT(rects[12].x(), rects[13].x()); |
| 580 | EXPECT_LT(rects[12].x(), rects[14].x()); |
| 581 | |
| 582 | // Results 13, 12 and 14 should be one above the other in that order because |
| 583 | // of the rowspan and vertical-align: middle by default. |
| 584 | EXPECT_LT(rects[13].y(), rects[12].y()); |
| 585 | EXPECT_LT(rects[12].y(), rects[14].y()); |
| 586 | |
| 587 | // Result 16 should be below result 15. |
| 588 | EXPECT_GT(rects[15].y(), rects[14].y()); |
| 589 | |
| 590 | // Result 18 should be normalized with respect to the position:relative div, |
| 591 | // and not it's immediate containing div. Consequently, result 18 should be |
| 592 | // above result 17. |
| 593 | EXPECT_GT(rects[17].y(), rects[18].y()); |
| 594 | } |
| 595 | |
| 596 | // Tests activating the find match nearest to a given point. |
paulmeyer | cc9a00f9 | 2016-07-06 13:44:27 | [diff] [blame] | 597 | IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, |
| 598 | MAYBE(ActivateNearestFindMatch)) { |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 599 | LoadAndWait("/find_in_page.html"); |
| 600 | |
| 601 | blink::WebFindOptions default_options; |
| 602 | Find("result", default_options); |
paulmeyer | 2f06e61 | 2016-08-10 17:39:02 | [diff] [blame] | 603 | delegate()->WaitForFinalReply(); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 604 | EXPECT_EQ(19, delegate()->GetFindResults().number_of_matches); |
| 605 | |
| 606 | // Get the find match rects. |
| 607 | contents()->RequestFindMatchRects(-1); |
| 608 | delegate()->WaitForMatchRects(); |
| 609 | const std::vector<gfx::RectF>& rects = delegate()->find_match_rects(); |
| 610 | |
| 611 | // Activate matches via points inside each of the find match rects, in an |
| 612 | // arbitrary order. Check that the correct match becomes active after each |
| 613 | // activation. |
| 614 | int order[19] = |
| 615 | {11, 13, 2, 0, 16, 5, 7, 10, 6, 1, 15, 14, 9, 17, 18, 3, 8, 12, 4}; |
| 616 | for (int i = 0; i < 19; ++i) { |
paulmeyer | 2f06e61 | 2016-08-10 17:39:02 | [diff] [blame] | 617 | delegate()->MarkNextReply(); |
paulmeyer | c8cb7cb | 2016-06-07 01:14:19 | [diff] [blame] | 618 | contents()->ActivateNearestFindResult( |
| 619 | rects[order[i]].CenterPoint().x(), rects[order[i]].CenterPoint().y()); |
| 620 | delegate()->WaitForNextReply(); |
| 621 | EXPECT_EQ(order[i] + 1, delegate()->GetFindResults().active_match_ordinal); |
| 622 | } |
| 623 | } |
| 624 | #endif // defined(OS_ANDROID) |
paulmeyer | a41de3b | 2016-05-05 16:30:18 | [diff] [blame] | 625 | |
| 626 | } // namespace content |