| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "content/browser/web_contents/web_contents_impl.h" | 7 #include "content/browser/web_contents/web_contents_impl.h" |
| 8 #include "content/public/browser/notification_types.h" | 8 #include "content/public/browser/notification_types.h" |
| 9 #include "content/public/common/content_switches.h" | 9 #include "content/public/common/content_switches.h" |
| 10 #include "content/public/test/content_browser_test.h" | 10 #include "content/public/test/content_browser_test.h" |
| 11 #include "content/public/test/content_browser_test_utils.h" | 11 #include "content/public/test/content_browser_test_utils.h" |
| 12 #include "content/public/test/test_navigation_observer.h" | 12 #include "content/public/test/test_navigation_observer.h" |
| 13 #include "content/public/test/test_utils.h" | 13 #include "content/public/test/test_utils.h" |
| 14 #include "content/shell/browser/shell.h" | 14 #include "content/shell/browser/shell.h" |
| 15 #include "content/test/content_browser_test_utils_internal.h" | 15 #include "content/test/content_browser_test_utils_internal.h" |
| 16 #include "net/dns/mock_host_resolver.h" | 16 #include "net/dns/mock_host_resolver.h" |
| 17 #include "third_party/WebKit/public/web/WebFindOptions.h" | 17 #include "third_party/WebKit/public/web/WebFindOptions.h" |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 const int kInvalidId = -1; | 23 const int kInvalidId = -1; |
| 24 | 24 |
| 25 // The results of a find request. | 25 // The results of a find request. |
| 26 struct FindResults { | 26 struct FindResults { |
| 27 FindResults(int request_id, int number_of_matches, int active_match_ordinal) | 27 int request_id = kInvalidId; |
| 28 : request_id(request_id), | 28 int number_of_matches = 0; |
| 29 number_of_matches(number_of_matches), | 29 int active_match_ordinal = 0; |
| 30 active_match_ordinal(active_match_ordinal) {} | |
| 31 FindResults() : FindResults(kInvalidId, 0, 0) {} | |
| 32 | |
| 33 int request_id; | |
| 34 int number_of_matches; | |
| 35 int active_match_ordinal; | |
| 36 }; | 30 }; |
| 37 | 31 |
| 38 } // namespace | 32 } // namespace |
| 39 | 33 |
| 40 class TestWebContentsDelegate : public WebContentsDelegate { | 34 class TestWebContentsDelegate : public WebContentsDelegate { |
| 41 public: | 35 public: |
| 42 TestWebContentsDelegate() | 36 TestWebContentsDelegate() |
| 43 : last_request_id_(kInvalidId), | 37 : last_request_id_(kInvalidId), |
| 44 last_finished_request_id_(kInvalidId), | 38 last_finished_request_id_(kInvalidId), |
| 45 next_reply_received_(false), | 39 next_reply_received_(false), |
| 46 record_replies_(false), | |
| 47 waiting_for_(NOTHING) {} | 40 waiting_for_(NOTHING) {} |
| 48 ~TestWebContentsDelegate() override {} | 41 ~TestWebContentsDelegate() override {} |
| 49 | 42 |
| 50 // Returns the current find results. | 43 // Returns the current find results. |
| 51 const FindResults& GetFindResults() const { | 44 FindResults GetFindResults() { |
| 52 return current_results_; | 45 return current_results_; |
| 53 } | 46 } |
| 54 | 47 |
| 55 // Waits for all pending replies to be received. | 48 // Waits for all pending replies to be received. |
| 56 void WaitForFinalReply() { | 49 void WaitForFinalReply() { |
| 57 if (last_finished_request_id_ >= last_request_id_) | 50 if (last_finished_request_id_ >= last_request_id_) |
| 58 return; | 51 return; |
| 59 | 52 |
| 60 WaitFor(FINAL_REPLY); | 53 WaitFor(FINAL_REPLY); |
| 61 } | 54 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 76 void MarkNextReply() { | 69 void MarkNextReply() { |
| 77 next_reply_received_ = false; | 70 next_reply_received_ = false; |
| 78 } | 71 } |
| 79 | 72 |
| 80 // Called when a new find request is issued, so the delegate knows the last | 73 // Called when a new find request is issued, so the delegate knows the last |
| 81 // request ID. | 74 // request ID. |
| 82 void UpdateLastRequest(int request_id) { | 75 void UpdateLastRequest(int request_id) { |
| 83 last_request_id_ = request_id; | 76 last_request_id_ = request_id; |
| 84 } | 77 } |
| 85 | 78 |
| 86 // From when this function is called, all replies coming in via FindReply() | |
| 87 // will be recorded. These replies can be retrieved via GetReplyRecord(). | |
| 88 void StartReplyRecord() { | |
| 89 reply_record_.clear(); | |
| 90 record_replies_ = true; | |
| 91 } | |
| 92 | |
| 93 // Retreives the results from the find replies recorded since the last call to | |
| 94 // StartReplyRecord(). Calling this function also stops the recording new find | |
| 95 // replies. | |
| 96 const std::vector<FindResults>& GetReplyRecord() { | |
| 97 record_replies_ = false; | |
| 98 return reply_record_; | |
| 99 } | |
| 100 | |
| 101 #if defined(OS_ANDROID) | 79 #if defined(OS_ANDROID) |
| 102 // Waits for all of the find match rects to be received. | 80 // Waits for all of the find match rects to be received. |
| 103 void WaitForMatchRects() { | 81 void WaitForMatchRects() { |
| 104 WaitFor(MATCH_RECTS); | 82 WaitFor(MATCH_RECTS); |
| 105 } | 83 } |
| 106 | 84 |
| 107 const std::vector<gfx::RectF>& find_match_rects() const { | 85 const std::vector<gfx::RectF>& find_match_rects() const { |
| 108 return find_match_rects_; | 86 return find_match_rects_; |
| 109 } | 87 } |
| 110 | 88 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 123 #endif | 101 #endif |
| 124 }; | 102 }; |
| 125 | 103 |
| 126 // WebContentsDelegate override. | 104 // WebContentsDelegate override. |
| 127 void FindReply(WebContents* web_contents, | 105 void FindReply(WebContents* web_contents, |
| 128 int request_id, | 106 int request_id, |
| 129 int number_of_matches, | 107 int number_of_matches, |
| 130 const gfx::Rect& selection_rect, | 108 const gfx::Rect& selection_rect, |
| 131 int active_match_ordinal, | 109 int active_match_ordinal, |
| 132 bool final_update) override { | 110 bool final_update) override { |
| 133 if (record_replies_) { | |
| 134 reply_record_.emplace_back( | |
| 135 request_id, number_of_matches, active_match_ordinal); | |
| 136 } | |
| 137 | |
| 138 // Update the current results. | 111 // Update the current results. |
| 139 if (request_id > current_results_.request_id) | 112 if (request_id > current_results_.request_id) |
| 140 current_results_.request_id = request_id; | 113 current_results_.request_id = request_id; |
| 141 if (number_of_matches != -1) | 114 if (number_of_matches != -1) |
| 142 current_results_.number_of_matches = number_of_matches; | 115 current_results_.number_of_matches = number_of_matches; |
| 143 if (active_match_ordinal != -1) | 116 if (active_match_ordinal != -1) |
| 144 current_results_.active_match_ordinal = active_match_ordinal; | 117 current_results_.active_match_ordinal = active_match_ordinal; |
| 145 | 118 |
| 146 if (!final_update) | 119 if (!final_update) |
| 147 return; | 120 return; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 | 180 |
| 208 // The ID of the last find request issued. | 181 // The ID of the last find request issued. |
| 209 int last_request_id_; | 182 int last_request_id_; |
| 210 | 183 |
| 211 // The ID of the last find request to finish (all replies received). | 184 // The ID of the last find request to finish (all replies received). |
| 212 int last_finished_request_id_; | 185 int last_finished_request_id_; |
| 213 | 186 |
| 214 // Indicates whether the next reply after MarkNextReply() has been received. | 187 // Indicates whether the next reply after MarkNextReply() has been received. |
| 215 bool next_reply_received_; | 188 bool next_reply_received_; |
| 216 | 189 |
| 217 // Indicates whether the find results from incoming find replies are currently | |
| 218 // being recorded. | |
| 219 bool record_replies_; | |
| 220 | |
| 221 // A record of all find replies that have come in via FindReply() since | |
| 222 // StartReplyRecor() was last called. | |
| 223 std::vector<FindResults> reply_record_; | |
| 224 | |
| 225 // Indicates what |message_loop_runner_| is waiting for, if anything. | 190 // Indicates what |message_loop_runner_| is waiting for, if anything. |
| 226 WaitingFor waiting_for_; | 191 WaitingFor waiting_for_; |
| 227 | 192 |
| 228 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | 193 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 229 | 194 |
| 230 DISALLOW_COPY_AND_ASSIGN(TestWebContentsDelegate); | 195 DISALLOW_COPY_AND_ASSIGN(TestWebContentsDelegate); |
| 231 }; | 196 }; |
| 232 | 197 |
| 233 class FindRequestManagerTest : public ContentBrowserTest, | 198 class FindRequestManagerTest : public ContentBrowserTest, |
| 234 public testing::WithParamInterface<bool> { | 199 public testing::WithParamInterface<bool> { |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 | 481 |
| 517 Find("result", options); | 482 Find("result", options); |
| 518 delegate()->WaitForFinalReply(); | 483 delegate()->WaitForFinalReply(); |
| 519 | 484 |
| 520 results = delegate()->GetFindResults(); | 485 results = delegate()->GetFindResults(); |
| 521 EXPECT_EQ(last_request_id(), results.request_id); | 486 EXPECT_EQ(last_request_id(), results.request_id); |
| 522 EXPECT_EQ(8, results.number_of_matches); | 487 EXPECT_EQ(8, results.number_of_matches); |
| 523 EXPECT_EQ(4, results.active_match_ordinal); | 488 EXPECT_EQ(4, results.active_match_ordinal); |
| 524 } | 489 } |
| 525 | 490 |
| 526 IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, MAYBE(FindInPage_Issue627799)) { | |
| 527 LoadAndWait("/find_in_long_page.html"); | |
| 528 | |
| 529 blink::WebFindOptions options; | |
| 530 Find("42", options); | |
| 531 delegate()->WaitForFinalReply(); | |
| 532 | |
| 533 FindResults results = delegate()->GetFindResults(); | |
| 534 EXPECT_EQ(last_request_id(), results.request_id); | |
| 535 EXPECT_EQ(970, results.number_of_matches); | |
| 536 EXPECT_EQ(1, results.active_match_ordinal); | |
| 537 | |
| 538 delegate()->StartReplyRecord(); | |
| 539 options.findNext = true; | |
| 540 options.forward = false; | |
| 541 Find("42", options); | |
| 542 delegate()->WaitForFinalReply(); | |
| 543 | |
| 544 // This is the crux of the issue that this test guards against. Searching | |
| 545 // across the frame boundary should not cause the frame to be re-scoped. If | |
| 546 // the re-scope occurs, then we will see the number of matches change in one | |
| 547 // of the recorded find replies. | |
| 548 for (auto& reply : delegate()->GetReplyRecord()) { | |
| 549 EXPECT_EQ(last_request_id(), reply.request_id); | |
| 550 EXPECT_TRUE(reply.number_of_matches == kInvalidId || | |
| 551 reply.number_of_matches == results.number_of_matches); | |
| 552 } | |
| 553 } | |
| 554 | |
| 555 #if defined(OS_ANDROID) | 491 #if defined(OS_ANDROID) |
| 556 // Tests requesting find match rects. | 492 // Tests requesting find match rects. |
| 557 IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, MAYBE(FindMatchRects)) { | 493 IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, MAYBE(FindMatchRects)) { |
| 558 LoadAndWait("/find_in_page.html"); | 494 LoadAndWait("/find_in_page.html"); |
| 559 | 495 |
| 560 blink::WebFindOptions default_options; | 496 blink::WebFindOptions default_options; |
| 561 Find("result", default_options); | 497 Find("result", default_options); |
| 562 delegate()->WaitForFinalReply(); | 498 delegate()->WaitForFinalReply(); |
| 563 EXPECT_EQ(19, delegate()->GetFindResults().number_of_matches); | 499 EXPECT_EQ(19, delegate()->GetFindResults().number_of_matches); |
| 564 | 500 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 delegate()->MarkNextReply(); | 594 delegate()->MarkNextReply(); |
| 659 contents()->ActivateNearestFindResult( | 595 contents()->ActivateNearestFindResult( |
| 660 rects[order[i]].CenterPoint().x(), rects[order[i]].CenterPoint().y()); | 596 rects[order[i]].CenterPoint().x(), rects[order[i]].CenterPoint().y()); |
| 661 delegate()->WaitForNextReply(); | 597 delegate()->WaitForNextReply(); |
| 662 EXPECT_EQ(order[i] + 1, delegate()->GetFindResults().active_match_ordinal); | 598 EXPECT_EQ(order[i] + 1, delegate()->GetFindResults().active_match_ordinal); |
| 663 } | 599 } |
| 664 } | 600 } |
| 665 #endif // defined(OS_ANDROID) | 601 #endif // defined(OS_ANDROID) |
| 666 | 602 |
| 667 } // namespace content | 603 } // namespace content |
| OLD | NEW |