Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: content/browser/find_request_manager_browsertest.cc

Issue 1910073002: Revert of Implement the basic testing infrastructure for FindRequestManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/browser/web_contents/web_contents_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 namespace {
21
22 const int kInvalidId = -1;
23
24 // The results of a find request.
25 struct FindResults {
26 int request_id = kInvalidId;
27 int number_of_matches = 0;
28 int active_match_ordinal = 0;
29 };
30
31 } // namespace
32
33 class TestWebContentsDelegate : public WebContentsDelegate {
34 public:
35 TestWebContentsDelegate()
36 : last_finished_request_id_(kInvalidId),
37 waiting_request_id_(kInvalidId) {}
38 ~TestWebContentsDelegate() override {}
39
40 // Waits for the final reply to the find request with ID |request_id|.
41 void WaitForFinalReply(int request_id) {
42 if (last_finished_request_id_ == request_id)
43 return;
44
45 waiting_request_id_ = request_id;
46 find_message_loop_runner_ = new content::MessageLoopRunner;
47 find_message_loop_runner_->Run();
48 }
49
50 // Returns the current find results.
51 FindResults GetFindResults() {
52 return current_results_;
53 }
54
55 private:
56 // WebContentsDelegate override.
57 void FindReply(WebContents* web_contents,
58 int request_id,
59 int number_of_matches,
60 const gfx::Rect& selection_rect,
61 int active_match_ordinal,
62 bool final_update) override {
63 // Update the current results.
64 if (request_id > current_results_.request_id)
65 current_results_.request_id = request_id;
66 if (number_of_matches != -1)
67 current_results_.number_of_matches = number_of_matches;
68 if (active_match_ordinal != -1)
69 current_results_.active_match_ordinal = active_match_ordinal;
70
71 if (final_update)
72 last_finished_request_id_ = request_id;
73
74 // If we are waiting for a final reply and this is it, stop waiting.
75 if (find_message_loop_runner_.get() &&
76 last_finished_request_id_ >= waiting_request_id_) {
77 find_message_loop_runner_->Quit();
78 }
79 }
80
81 // The latest known results from the current find request.
82 FindResults current_results_;
83
84 // The ID of the last find request to finish (all replies received).
85 int last_finished_request_id_;
86
87 // If waiting using |find_message_loop_runner_|, this is the ID of the find
88 // request being waited for.
89 int waiting_request_id_;
90
91 scoped_refptr<content::MessageLoopRunner> find_message_loop_runner_;
92
93 DISALLOW_COPY_AND_ASSIGN(TestWebContentsDelegate);
94 };
95
96 class FindRequestManagerTest : public ContentBrowserTest {
97 public:
98 FindRequestManagerTest()
99 : normal_delegate_(nullptr),
100 last_request_id_(0) {}
101 ~FindRequestManagerTest() override {}
102
103 void SetUpOnMainThread() override {
104 host_resolver()->AddRule("*", "127.0.0.1");
105 ASSERT_TRUE(embedded_test_server()->Start());
106
107 // Swap the WebContents's delegate for our test delegate.
108 normal_delegate_ = contents()->GetDelegate();
109 contents()->SetDelegate(&test_delegate_);
110 }
111
112 void TearDownOnMainThread() override {
113 // Swap the WebContents's delegate back to its usual delegate.
114 contents()->SetDelegate(normal_delegate_);
115 }
116
117 void SetUpCommandLine(base::CommandLine* command_line) override {
118 IsolateAllSitesForTesting(command_line);
119 }
120
121 protected:
122 // Navigate to |url| and wait for it to finish loading.
123 void LoadAndWait(const std::string& url) {
124 TestNavigationObserver navigation_observer(contents());
125 NavigateToURL(shell(), embedded_test_server()->GetURL("a.com", url));
126 }
127
128 void Find(const std::string& search_text,
129 const blink::WebFindOptions& options) {
130 contents()->Find(++last_request_id_,
131 base::UTF8ToUTF16(search_text),
132 options);
133 }
134
135 void WaitForFinalReply() const {
136 delegate()->WaitForFinalReply(last_request_id_);
137 }
138
139 WebContents* contents() const {
140 return shell()->web_contents();
141 }
142
143 TestWebContentsDelegate* delegate() const {
144 return static_cast<TestWebContentsDelegate*>(contents()->GetDelegate());
145 }
146
147 int last_request_id() const {
148 return last_request_id_;
149 }
150
151 private:
152 TestWebContentsDelegate test_delegate_;
153 WebContentsDelegate* normal_delegate_;
154
155 // The ID of the last find request requested.
156 int last_request_id_;
157
158 DISALLOW_COPY_AND_ASSIGN(FindRequestManagerTest);
159 };
160
161 // Test basic find-in-page functionality (such as searching forward and
162 // backward) and check for correct results at each step.
163 IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, Basic) {
164 LoadAndWait("/find_in_page.html");
165
166 blink::WebFindOptions options;
167 Find("result", options);
168 WaitForFinalReply();
169
170 FindResults results = delegate()->GetFindResults();
171 EXPECT_EQ(last_request_id(), results.request_id);
172 EXPECT_EQ(19, results.number_of_matches);
173 EXPECT_EQ(1, results.active_match_ordinal);
174
175 options.findNext = true;
176 for (int i = 2; i <= 10; ++i) {
177 Find("result", options);
178 WaitForFinalReply();
179
180 results = delegate()->GetFindResults();
181 EXPECT_EQ(last_request_id(), results.request_id);
182 EXPECT_EQ(19, results.number_of_matches);
183 EXPECT_EQ(i, results.active_match_ordinal);
184 }
185
186 options.forward = false;
187 for (int i = 9; i >= 5; --i) {
188 Find("result", options);
189 WaitForFinalReply();
190
191 results = delegate()->GetFindResults();
192 EXPECT_EQ(last_request_id(), results.request_id);
193 EXPECT_EQ(19, results.number_of_matches);
194 EXPECT_EQ(i, results.active_match_ordinal);
195 }
196 }
197
198 // Tests searching for a word character-by-character, as would typically be done
199 // by a user typing into the find bar.
200 IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, CharacterByCharacter) {
201 LoadAndWait("/find_in_page.html");
202
203 blink::WebFindOptions default_options;
204 Find("r", default_options);
205 Find("re", default_options);
206 Find("res", default_options);
207 Find("resu", default_options);
208 Find("resul", default_options);
209 Find("result", default_options);
210 WaitForFinalReply();
211
212 FindResults results = delegate()->GetFindResults();
213 EXPECT_EQ(last_request_id(), results.request_id);
214 EXPECT_EQ(19, results.number_of_matches);
215 EXPECT_EQ(1, results.active_match_ordinal);
216 }
217
218 // Test sending a large number of find requests subsequently.
219 IN_PROC_BROWSER_TEST_F(FindRequestManagerTest, RapidFire) {
220 LoadAndWait("/find_in_page.html");
221
222 blink::WebFindOptions options;
223 Find("result", options);
224
225 options.findNext = true;
226 for (int i = 2; i <= 1000; ++i)
227 Find("result", options);
228 WaitForFinalReply();
229
230 FindResults results = delegate()->GetFindResults();
231 EXPECT_EQ(last_request_id(), results.request_id);
232 EXPECT_EQ(19, results.number_of_matches);
233 EXPECT_EQ(last_request_id() % results.number_of_matches,
234 results.active_match_ordinal);
235 }
236
237 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/browser/web_contents/web_contents_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698