Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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" | |
| 7 #include "content/browser/web_contents/web_contents_impl.h" | |
| 8 #include "content/public/browser/notification_types.h" | |
| 9 #include "content/public/common/content_switches.h" | |
| 10 #include "content/public/test/content_browser_test.h" | |
| 11 #include "content/public/test/content_browser_test_utils.h" | |
| 12 #include "content/public/test/test_navigation_observer.h" | |
| 13 #include "content/public/test/test_utils.h" | |
| 14 #include "content/shell/browser/shell.h" | |
| 15 #include "net/dns/mock_host_resolver.h" | |
| 16 #include "third_party/WebKit/public/web/WebFindOptions.h" | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 class TestWebContentsDelegate : public WebContentsDelegate { | |
| 21 public: | |
| 22 TestWebContentsDelegate() | |
| 23 : last_finished_request_id_(kInvalidId), | |
| 24 waiting_request_id_(kInvalidId) {} | |
| 25 ~TestWebContentsDelegate() override {} | |
| 26 | |
| 27 // Waits for the final reply to the find request with ID |request_id|. | |
| 28 void WaitForFinalReply(int request_id) { | |
| 29 if (last_finished_request_id_ == request_id) | |
| 30 return; | |
| 31 | |
| 32 waiting_request_id_ = request_id; | |
| 33 find_message_loop_runner_ = new content::MessageLoopRunner; | |
| 34 find_message_loop_runner_->Run(); | |
| 35 } | |
| 36 | |
| 37 // The results of a find request. | |
| 38 struct FindResults { | |
| 39 int request_id = kInvalidId; | |
| 40 int number_of_matches = 0; | |
| 41 int active_match_ordinal = 0; | |
| 42 }; | |
| 43 | |
| 44 // Returns the current find results. | |
| 45 FindResults GetFindResults() { | |
| 46 return current_results_; | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 static const int kInvalidId = -1; | |
| 51 | |
| 52 // WebContentsDelegate override. | |
| 53 void FindReply(WebContents* web_contents, | |
| 54 int request_id, | |
| 55 int number_of_matches, | |
| 56 const gfx::Rect& selection_rect, | |
| 57 int active_match_ordinal, | |
| 58 bool final_update) override { | |
| 59 // Update the current results. | |
| 60 if (request_id > current_results_.request_id) | |
| 61 current_results_.request_id = request_id; | |
| 62 if (number_of_matches != -1) | |
| 63 current_results_.number_of_matches = number_of_matches; | |
| 64 if (active_match_ordinal != -1) | |
| 65 current_results_.active_match_ordinal = active_match_ordinal; | |
|
nasko
2016/04/18 21:02:39
Shouldn't we update all the parts of the result to
paulmeyer
2016/04/19 15:15:00
This is intentional. Multiple find IPCs are used t
nasko
2016/04/20 16:47:49
Acknowledged.
| |
| 66 | |
| 67 if (final_update) | |
| 68 last_finished_request_id_ = request_id; | |
| 69 | |
| 70 // If we are waiting for a final reply and this is it, stop waiting. | |
| 71 if (find_message_loop_runner_.get() && | |
| 72 last_finished_request_id_ >= waiting_request_id_) { | |
| 73 find_message_loop_runner_->Quit(); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 // The lastest known results from the current find request. | |
|
nasko
2016/04/18 21:02:40
nit: latest? or last?
paulmeyer
2016/04/19 15:15:01
Hahaha. "lastest", meaning "the most last".
It wa
| |
| 78 FindResults current_results_; | |
| 79 | |
| 80 // The ID of the last find request to finish (all replies received). | |
| 81 int last_finished_request_id_; | |
| 82 | |
| 83 // If waiting using |find_message_loop_runner_|, this is the ID of the find | |
| 84 // request being waited for. | |
| 85 int waiting_request_id_; | |
| 86 | |
| 87 scoped_refptr<content::MessageLoopRunner> find_message_loop_runner_; | |
| 88 }; | |
|
nasko
2016/04/18 21:02:40
DISALLOW_COPY_AND_ASSIGN?
paulmeyer
2016/04/19 15:15:01
Done.
| |
| 89 | |
| 90 using FindResults = TestWebContentsDelegate::FindResults; | |
|
nasko
2016/04/18 21:02:40
Instead of this, why not define the FindResults st
paulmeyer
2016/04/19 15:15:01
Done.
| |
| 91 | |
| 92 class FindRequestManagerTest : public ContentBrowserTest { | |
| 93 public: | |
| 94 FindRequestManagerTest() | |
| 95 : normal_delegate_(nullptr), | |
| 96 last_request_id_(0) {} | |
| 97 ~FindRequestManagerTest() override {} | |
| 98 | |
| 99 void SetUpOnMainThread() override { | |
| 100 host_resolver()->AddRule("*", "127.0.0.1"); | |
| 101 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 102 | |
| 103 // Swap the WebContents's delegate for our test delegate. | |
| 104 normal_delegate_ = contents()->GetDelegate(); | |
| 105 contents()->SetDelegate(new TestWebContentsDelegate()); | |
| 106 } | |
| 107 | |
| 108 void TearDownOnMainThread() override { | |
| 109 // Swap the WebContents's delegate back to its usual delegate. | |
| 110 contents()->SetDelegate(normal_delegate_); | |
| 111 } | |
| 112 | |
| 113 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 114 IsolateAllSitesForTesting(command_line); | |
| 115 } | |
| 116 | |
| 117 protected: | |
| 118 // Navigate to |url| and wait for it to finish loading. | |
| 119 void LoadAndWait(const std::string& url) { | |
| 120 TestNavigationObserver navigation_observer(contents()); | |
| 121 NavigateToURL(shell(), embedded_test_server()->GetURL("a.com", url)); | |
| 122 } | |
| 123 | |
| 124 WebContents* contents() { | |
| 125 return shell()->web_contents(); | |
| 126 } | |
| 127 | |
| 128 TestWebContentsDelegate* delegate() { | |
| 129 return static_cast<TestWebContentsDelegate*>(contents()->GetDelegate()); | |
| 130 } | |
| 131 | |
| 132 private: | |
| 133 TestWebContentsDelegate test_delegate_; | |
| 134 WebContentsDelegate* normal_delegate_; | |
| 135 | |
| 136 int last_request_id_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(FindRequestManagerTest); | |
| 139 }; | |
| 140 | |
| 141 // Test basic find-in-page functionality (such as searching forward and | |
| 142 // backward) and check for correct results at each step. | |
| 143 IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, Basic) { | |
| 144 LoadAndWait("/find_in_page.html"); | |
| 145 | |
| 146 blink::WebFindOptions options; | |
| 147 contents()->Find(1, base::UTF8ToUTF16("result"), options); | |
| 148 delegate()->WaitForFinalReply(1); | |
| 149 | |
| 150 FindResults results = delegate()->GetFindResults(); | |
| 151 EXPECT_EQ(1, results.request_id); | |
| 152 EXPECT_EQ(19, results.number_of_matches); | |
|
nasko
2016/04/18 21:02:39
Why is the total number of matches only 19? There
paulmeyer
2016/04/19 15:15:01
This is because "Result 19" does not actually matc
nasko
2016/04/20 16:47:49
Acknowledged.
| |
| 153 EXPECT_EQ(1, results.active_match_ordinal); | |
| 154 | |
| 155 options.findNext = true; | |
| 156 for (int i = 2; i <= 10; ++i) { | |
| 157 contents()->Find(i, base::UTF8ToUTF16("result"), options); | |
| 158 delegate()->WaitForFinalReply(i); | |
| 159 results = delegate()->GetFindResults(); | |
| 160 | |
| 161 EXPECT_EQ(i, results.request_id); | |
| 162 EXPECT_EQ(19, results.number_of_matches); | |
| 163 EXPECT_EQ(i, results.active_match_ordinal); | |
| 164 } | |
| 165 | |
| 166 options.forward = false; | |
| 167 for (int i = 11; i <= 15; ++i) { | |
| 168 contents()->Find(i, base::UTF8ToUTF16("result"), options); | |
| 169 delegate()->WaitForFinalReply(i); | |
| 170 results = delegate()->GetFindResults(); | |
| 171 | |
| 172 EXPECT_EQ(i, results.request_id); | |
| 173 EXPECT_EQ(19, results.number_of_matches); | |
| 174 EXPECT_EQ(20 - i, results.active_match_ordinal); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 // Tests searching for a word character-by-character, as would typically be done | |
| 179 // by a user typing into the find bar. | |
| 180 IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, CharacterByCharacter) { | |
| 181 LoadAndWait("/find_in_page.html"); | |
| 182 | |
| 183 blink::WebFindOptions default_options; | |
| 184 contents()->Find(1, base::UTF8ToUTF16("r"), default_options); | |
|
nasko
2016/04/18 21:02:40
minor nit: Instead of using hardcoded numbers, why
paulmeyer
2016/04/19 15:15:01
Okay, I've done something like that. I'd prefer to
nasko
2016/04/20 16:47:50
Thanks! This looks much cleaner.
| |
| 185 contents()->Find(2, base::UTF8ToUTF16("re"), default_options); | |
| 186 contents()->Find(3, base::UTF8ToUTF16("res"), default_options); | |
| 187 contents()->Find(4, base::UTF8ToUTF16("resu"), default_options); | |
| 188 contents()->Find(5, base::UTF8ToUTF16("resul"), default_options); | |
| 189 contents()->Find(6, base::UTF8ToUTF16("result"), default_options); | |
| 190 delegate()->WaitForFinalReply(6); | |
| 191 | |
| 192 FindResults results = delegate()->GetFindResults(); | |
| 193 EXPECT_EQ(6, results.request_id); | |
| 194 EXPECT_EQ(19, results.number_of_matches); | |
| 195 EXPECT_EQ(1, results.active_match_ordinal); | |
| 196 } | |
| 197 | |
| 198 // Test sending a large number of find requests subsequently. | |
| 199 IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, RapidFire) { | |
| 200 LoadAndWait("/find_in_page.html"); | |
| 201 | |
| 202 blink::WebFindOptions options; | |
| 203 contents()->Find(1, base::UTF8ToUTF16("result"), options); | |
| 204 | |
| 205 options.findNext = true; | |
| 206 for (int i = 2; i <= 1000; ++i) | |
| 207 contents()->Find(i, base::UTF8ToUTF16("result"), options); | |
| 208 delegate()->WaitForFinalReply(1000); | |
| 209 FindResults results = delegate()->GetFindResults(); | |
| 210 | |
| 211 EXPECT_EQ(1000, results.request_id); | |
| 212 EXPECT_EQ(19, results.number_of_matches); | |
| 213 EXPECT_EQ(1000 % results.number_of_matches, results.active_match_ordinal); | |
| 214 } | |
| 215 | |
| 216 } // namespace content | |
| OLD | NEW |