Add a webstorePrivate API to show a permission prompt for delegated bundle installs
[chromium-blink-merge.git] / chrome / browser / net / net_error_tab_helper_unittest.cc
blobe099754b8c77d2419e8dbccce52d26d5b1a75a0e
1 // Copyright 2013 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.
5 #include "chrome/browser/net/net_error_tab_helper.h"
7 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
8 #include "components/error_page/common/net_error_info.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/test/test_renderer_host.h"
11 #include "net/base/net_errors.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/base/page_transition_types.h"
15 using chrome_browser_net::NetErrorTabHelper;
16 using chrome_common_net::DnsProbeStatus;
18 class TestNetErrorTabHelper : public NetErrorTabHelper {
19 public:
20 TestNetErrorTabHelper()
21 : NetErrorTabHelper(NULL),
22 mock_probe_running_(false),
23 last_status_sent_(chrome_common_net::DNS_PROBE_MAX),
24 mock_sent_count_(0) {}
26 void FinishProbe(DnsProbeStatus status) {
27 EXPECT_TRUE(mock_probe_running_);
28 OnDnsProbeFinished(status);
29 mock_probe_running_ = false;
32 bool mock_probe_running() const { return mock_probe_running_; }
33 DnsProbeStatus last_status_sent() const { return last_status_sent_; }
34 int mock_sent_count() const { return mock_sent_count_; }
36 private:
37 void StartDnsProbe() override {
38 EXPECT_FALSE(mock_probe_running_);
39 mock_probe_running_ = true;
42 void SendInfo() override {
43 last_status_sent_ = dns_probe_status();
44 mock_sent_count_++;
47 bool mock_probe_running_;
48 DnsProbeStatus last_status_sent_;
49 int mock_sent_count_;
52 class NetErrorTabHelperTest : public ChromeRenderViewHostTestHarness {
53 protected:
54 enum MainFrame { SUB_FRAME, MAIN_FRAME };
55 enum ErrorPage { NORMAL_PAGE, ERROR_PAGE };
56 enum ErrorType { DNS_ERROR, OTHER_ERROR };
58 void SetUp() override {
59 ChromeRenderViewHostTestHarness::SetUp();
61 // This will simulate the initialization of the RenderFrame in the renderer
62 // process. This is needed because WebContents does not initialize a
63 // RenderFrame on construction, and the tests expect one to exist.
64 content::RenderFrameHostTester::For(main_rfh())
65 ->InitializeRenderFrameIfNeeded();
67 subframe_ = content::RenderFrameHostTester::For(main_rfh())
68 ->AppendChild("subframe");
70 tab_helper_.reset(new TestNetErrorTabHelper);
71 NetErrorTabHelper::set_state_for_testing(
72 NetErrorTabHelper::TESTING_FORCE_ENABLED);
75 void StartProvisionalLoad(MainFrame main_frame, ErrorPage error_page) {
76 tab_helper_->DidStartProvisionalLoadForFrame(
77 (main_frame == MAIN_FRAME) ? main_rfh() : subframe_,
78 bogus_url_, // validated_url
79 (error_page == ERROR_PAGE),
80 false); // is_iframe_srcdoc
83 void CommitProvisionalLoad(MainFrame main_frame) {
84 tab_helper_->DidCommitProvisionalLoadForFrame(
85 (main_frame == MAIN_FRAME) ? main_rfh() : subframe_,
86 bogus_url_, // url
87 ui::PAGE_TRANSITION_TYPED);
90 void FailProvisionalLoad(MainFrame main_frame, ErrorType error_type) {
91 int net_error;
93 if (error_type == DNS_ERROR)
94 net_error = net::ERR_NAME_NOT_RESOLVED;
95 else
96 net_error = net::ERR_TIMED_OUT;
98 tab_helper_->DidFailProvisionalLoad(
99 (main_frame == MAIN_FRAME) ? main_rfh() : subframe_,
100 bogus_url_, // validated_url
101 net_error,
102 base::string16());
105 void FinishProbe(DnsProbeStatus status) { tab_helper_->FinishProbe(status); }
107 bool probe_running() { return tab_helper_->mock_probe_running(); }
108 DnsProbeStatus last_status_sent() { return tab_helper_->last_status_sent(); }
109 int sent_count() { return tab_helper_->mock_sent_count(); }
111 private:
112 content::RenderFrameHost* subframe_;
113 scoped_ptr<TestNetErrorTabHelper> tab_helper_;
114 GURL bogus_url_;
117 TEST_F(NetErrorTabHelperTest, Null) {
118 EXPECT_FALSE(probe_running());
121 TEST_F(NetErrorTabHelperTest, MainFrameNonDnsError) {
122 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
123 FailProvisionalLoad(MAIN_FRAME, OTHER_ERROR);
124 EXPECT_FALSE(probe_running());
125 EXPECT_EQ(0, sent_count());
128 TEST_F(NetErrorTabHelperTest, NonMainFrameDnsError) {
129 StartProvisionalLoad(SUB_FRAME, NORMAL_PAGE);
130 FailProvisionalLoad(SUB_FRAME, DNS_ERROR);
131 EXPECT_FALSE(probe_running());
132 EXPECT_EQ(0, sent_count());
135 // Test complete DNS error page loads. Note that the helper can see two error
136 // page loads: Link Doctor loads an empty HTML page so the user knows something
137 // is going on, then fails over to the normal error page if and when Link
138 // Doctor fails to load or declines to provide a page.
140 TEST_F(NetErrorTabHelperTest, ProbeResponseBeforeFirstCommit) {
141 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
142 FailProvisionalLoad(MAIN_FRAME, DNS_ERROR);
143 EXPECT_TRUE(probe_running());
144 EXPECT_EQ(0, sent_count());
146 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
147 EXPECT_TRUE(probe_running());
148 EXPECT_EQ(0, sent_count());
150 FinishProbe(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
151 EXPECT_FALSE(probe_running());
152 EXPECT_EQ(0, sent_count());
154 CommitProvisionalLoad(MAIN_FRAME);
155 EXPECT_FALSE(probe_running());
156 EXPECT_EQ(1, sent_count());
157 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN, last_status_sent());
159 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
160 EXPECT_FALSE(probe_running());
161 EXPECT_EQ(1, sent_count());
163 CommitProvisionalLoad(MAIN_FRAME);
164 EXPECT_FALSE(probe_running());
165 EXPECT_EQ(2, sent_count());
166 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN, last_status_sent());
169 TEST_F(NetErrorTabHelperTest, ProbeResponseBetweenFirstAndSecondCommit) {
170 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
171 FailProvisionalLoad(MAIN_FRAME, DNS_ERROR);
172 EXPECT_TRUE(probe_running());
173 EXPECT_EQ(0, sent_count());
175 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
176 EXPECT_TRUE(probe_running());
177 EXPECT_EQ(0, sent_count());
179 CommitProvisionalLoad(MAIN_FRAME);
180 EXPECT_TRUE(probe_running());
181 EXPECT_EQ(1, sent_count());
182 EXPECT_EQ(chrome_common_net::DNS_PROBE_STARTED, last_status_sent());
184 FinishProbe(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
185 EXPECT_FALSE(probe_running());
186 EXPECT_EQ(2, sent_count());
187 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN, last_status_sent());
189 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
190 EXPECT_FALSE(probe_running());
191 EXPECT_EQ(2, sent_count());
193 CommitProvisionalLoad(MAIN_FRAME);
194 EXPECT_FALSE(probe_running());
195 EXPECT_EQ(3, sent_count());
196 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN, last_status_sent());
199 TEST_F(NetErrorTabHelperTest, ProbeResponseAfterSecondCommit) {
200 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
201 FailProvisionalLoad(MAIN_FRAME, DNS_ERROR);
202 EXPECT_TRUE(probe_running());
203 EXPECT_EQ(0, sent_count());
205 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
206 EXPECT_TRUE(probe_running());
207 EXPECT_EQ(0, sent_count());
209 CommitProvisionalLoad(MAIN_FRAME);
210 EXPECT_TRUE(probe_running());
211 EXPECT_EQ(1, sent_count());
212 EXPECT_EQ(chrome_common_net::DNS_PROBE_STARTED, last_status_sent());
214 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
215 EXPECT_TRUE(probe_running());
216 EXPECT_EQ(1, sent_count());
218 CommitProvisionalLoad(MAIN_FRAME);
219 EXPECT_TRUE(probe_running());
220 EXPECT_EQ(2, sent_count());
221 EXPECT_EQ(chrome_common_net::DNS_PROBE_STARTED, last_status_sent());
223 FinishProbe(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
224 EXPECT_FALSE(probe_running());
225 EXPECT_EQ(3, sent_count());
226 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN, last_status_sent());
229 // Send result even if a new page load has started; the error page is still
230 // visible, and the user might cancel the load.
231 TEST_F(NetErrorTabHelperTest, ProbeResponseAfterNewStart) {
232 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
233 FailProvisionalLoad(MAIN_FRAME, DNS_ERROR);
234 EXPECT_TRUE(probe_running());
235 EXPECT_EQ(0, sent_count());
237 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
238 EXPECT_TRUE(probe_running());
239 EXPECT_EQ(0, sent_count());
241 CommitProvisionalLoad(MAIN_FRAME);
242 EXPECT_TRUE(probe_running());
243 EXPECT_EQ(1, sent_count());
244 EXPECT_EQ(chrome_common_net::DNS_PROBE_STARTED, last_status_sent());
246 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
247 EXPECT_TRUE(probe_running());
248 EXPECT_EQ(1, sent_count());
250 CommitProvisionalLoad(MAIN_FRAME);
251 EXPECT_TRUE(probe_running());
252 EXPECT_EQ(2, sent_count());
253 EXPECT_EQ(chrome_common_net::DNS_PROBE_STARTED, last_status_sent());
255 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
256 EXPECT_TRUE(probe_running());
257 EXPECT_EQ(2, sent_count());
259 FinishProbe(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
260 EXPECT_FALSE(probe_running());
261 EXPECT_EQ(3, sent_count());
262 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN, last_status_sent());
265 // Don't send result if a new page has committed; the result would go to the
266 // wrong page, and the error page is gone anyway.
267 TEST_F(NetErrorTabHelperTest, ProbeResponseAfterNewCommit) {
268 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
269 FailProvisionalLoad(MAIN_FRAME, DNS_ERROR);
270 EXPECT_TRUE(probe_running());
271 EXPECT_EQ(0, sent_count());
273 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
274 EXPECT_TRUE(probe_running());
275 EXPECT_EQ(0, sent_count());
277 CommitProvisionalLoad(MAIN_FRAME);
278 EXPECT_TRUE(probe_running());
279 EXPECT_EQ(1, sent_count());
280 EXPECT_EQ(chrome_common_net::DNS_PROBE_STARTED, last_status_sent());
282 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
283 EXPECT_TRUE(probe_running());
284 EXPECT_EQ(1, sent_count());
286 CommitProvisionalLoad(MAIN_FRAME);
287 EXPECT_TRUE(probe_running());
288 EXPECT_EQ(2, sent_count());
289 EXPECT_EQ(chrome_common_net::DNS_PROBE_STARTED, last_status_sent());
291 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
292 EXPECT_TRUE(probe_running());
293 EXPECT_EQ(2, sent_count());
295 CommitProvisionalLoad(MAIN_FRAME);
296 EXPECT_TRUE(probe_running());
297 EXPECT_EQ(2, sent_count());
299 FinishProbe(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
300 EXPECT_FALSE(probe_running());
301 EXPECT_EQ(2, sent_count());
304 TEST_F(NetErrorTabHelperTest, MultipleDnsErrorsWithProbesWithoutErrorPages) {
305 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
306 FailProvisionalLoad(MAIN_FRAME, DNS_ERROR);
307 EXPECT_TRUE(probe_running());
308 EXPECT_EQ(0, sent_count());
310 FinishProbe(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
311 EXPECT_FALSE(probe_running());
312 EXPECT_EQ(0, sent_count());
314 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
315 FailProvisionalLoad(MAIN_FRAME, DNS_ERROR);
316 EXPECT_TRUE(probe_running());
317 EXPECT_EQ(0, sent_count());
319 FinishProbe(chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET);
320 EXPECT_FALSE(probe_running());
321 EXPECT_EQ(0, sent_count());
324 TEST_F(NetErrorTabHelperTest, MultipleDnsErrorsWithProbesAndErrorPages) {
325 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
326 FailProvisionalLoad(MAIN_FRAME, DNS_ERROR);
327 EXPECT_TRUE(probe_running());
328 EXPECT_EQ(0, sent_count());
330 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
331 CommitProvisionalLoad(MAIN_FRAME);
332 EXPECT_TRUE(probe_running());
333 EXPECT_EQ(1, sent_count());
334 EXPECT_EQ(chrome_common_net::DNS_PROBE_STARTED, last_status_sent());
336 FinishProbe(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
337 EXPECT_FALSE(probe_running());
338 EXPECT_EQ(2, sent_count());
339 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN, last_status_sent());
341 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
342 FailProvisionalLoad(MAIN_FRAME, DNS_ERROR);
343 EXPECT_TRUE(probe_running());
344 EXPECT_EQ(2, sent_count());
346 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
347 CommitProvisionalLoad(MAIN_FRAME);
348 EXPECT_TRUE(probe_running());
349 EXPECT_EQ(3, sent_count());
350 EXPECT_EQ(chrome_common_net::DNS_PROBE_STARTED, last_status_sent());
352 FinishProbe(chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET);
353 EXPECT_FALSE(probe_running());
354 EXPECT_EQ(4, sent_count());
355 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET,
356 last_status_sent());
359 // If multiple DNS errors occur in a row before a probe result, don't start
360 // multiple probes.
361 TEST_F(NetErrorTabHelperTest, CoalesceFailures) {
362 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
363 FailProvisionalLoad(MAIN_FRAME, DNS_ERROR);
364 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
365 CommitProvisionalLoad(MAIN_FRAME);
366 EXPECT_TRUE(probe_running());
367 EXPECT_EQ(1, sent_count());
368 EXPECT_EQ(chrome_common_net::DNS_PROBE_STARTED, last_status_sent());
370 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
371 FailProvisionalLoad(MAIN_FRAME, DNS_ERROR);
372 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
373 CommitProvisionalLoad(MAIN_FRAME);
374 EXPECT_TRUE(probe_running());
375 EXPECT_EQ(2, sent_count());
376 EXPECT_EQ(chrome_common_net::DNS_PROBE_STARTED, last_status_sent());
378 StartProvisionalLoad(MAIN_FRAME, NORMAL_PAGE);
379 FailProvisionalLoad(MAIN_FRAME, DNS_ERROR);
380 StartProvisionalLoad(MAIN_FRAME, ERROR_PAGE);
381 CommitProvisionalLoad(MAIN_FRAME);
382 EXPECT_TRUE(probe_running());
383 EXPECT_EQ(3, sent_count());
384 EXPECT_EQ(chrome_common_net::DNS_PROBE_STARTED, last_status_sent());
386 FinishProbe(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN);
387 EXPECT_FALSE(probe_running());
388 EXPECT_EQ(4, sent_count());
389 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN, last_status_sent());