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

Side by Side Diff: chrome/browser/android/offline_pages/offline_page_request_job_unittest.cc

Issue 2245733004: Serve offline page for online URL on disconnected or bad networks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix junit test Created 4 years, 3 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
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 "chrome/browser/android/offline_pages/offline_page_request_job.h"
6
7 #include "base/callback.h"
8 #include "base/feature_list.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/field_trial.h"
13 #include "base/path_service.h"
14 #include "base/run_loop.h"
15 #include "base/test/histogram_tester.h"
16 #include "base/threading/thread_task_runner_handle.h"
17 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
18 #include "chrome/browser/android/offline_pages/offline_page_request_interceptor. h"
19 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h"
20 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h "
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/test/base/testing_browser_process.h"
23 #include "chrome/test/base/testing_profile.h"
24 #include "chrome/test/base/testing_profile_manager.h"
25 #include "components/offline_pages/client_namespace_constants.h"
26 #include "components/offline_pages/offline_page_model.h"
27 #include "components/previews/previews_experiments.h"
28 #include "components/variations/variations_associated_data.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/resource_request_info.h"
31 #include "content/public/browser/web_contents.h"
32 #include "content/public/common/resource_type.h"
33 #include "content/public/test/test_browser_thread_bundle.h"
34 #include "net/nqe/network_quality_estimator.h"
35 #include "net/url_request/url_request.h"
36 #include "net/url_request/url_request_context.h"
37 #include "net/url_request/url_request_intercepting_job_factory.h"
38 #include "net/url_request/url_request_job_factory_impl.h"
39 #include "net/url_request/url_request_test_util.h"
40 #include "testing/gtest/include/gtest/gtest.h"
41
42 namespace offline_pages {
43
44 namespace {
45
46 const GURL kTestUrl("http://test.org/page1");
47 const GURL kTestUrl2("http://test.org/page2");
48 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234");
49 const int kTestFileSize = 444;
50 const int kTabId = 1;
51 const int kBufSize = 1024;
52 const char kAggregatedRequestResultHistogram[] =
53 "OfflinePages.AggregatedRequestResult";
54
55 class OfflinePageRequestJobTestDelegate :
56 public OfflinePageRequestJob::Delegate {
57 public:
58 OfflinePageRequestJobTestDelegate(content::WebContents* web_content,
59 int tab_id)
60 : web_content_(web_content),
61 tab_id_(tab_id) {}
62
63 content::ResourceRequestInfo::WebContentsGetter
64 GetWebContentsGetter(net::URLRequest* request) const override {
65 return base::Bind(&OfflinePageRequestJobTestDelegate::GetWebContents,
66 web_content_);
67 }
68
69 OfflinePageRequestJob::Delegate::TabIdGetter GetTabIdGetter() const override {
70 return base::Bind(&OfflinePageRequestJobTestDelegate::GetTabId,
71 tab_id_);
72 }
73
74 private:
75 static content::WebContents* GetWebContents(
76 content::WebContents* web_content) {
77 return web_content;
78 }
79
80 static bool GetTabId(
81 int tab_id_value, content::WebContents* web_content, int* tab_id) {
82 *tab_id = tab_id_value;
83 return true;
84 }
85
86 content::WebContents* web_content_;
87 int tab_id_;
88
89 DISALLOW_COPY_AND_ASSIGN(OfflinePageRequestJobTestDelegate);
90 };
91
92 class TestURLRequestDelegate : public net::URLRequest::Delegate {
93 public:
94 typedef base::Callback<void(int)> ReadCompletedCallback;
95
96 explicit TestURLRequestDelegate(const ReadCompletedCallback& callback)
97 : read_completed_callback_(callback),
98 buffer_(new net::IOBuffer(kBufSize)) {}
99
100 void OnResponseStarted(net::URLRequest* request) override {
101 if (!request->status().is_success()) {
102 read_completed_callback_.Run(0);
103 return;
104 }
105 int bytes_read = 0;
106 request->Read(buffer_.get(), kBufSize, &bytes_read);
107 }
108
109 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {
110 if (!read_completed_callback_.is_null())
111 read_completed_callback_.Run(bytes_read);
112 }
113
114 private:
115 ReadCompletedCallback read_completed_callback_;
116 scoped_refptr<net::IOBuffer> buffer_;
117
118 DISALLOW_COPY_AND_ASSIGN(TestURLRequestDelegate);
119 };
120
121 class TestURLRequestInterceptingJobFactory
122 : public net::URLRequestInterceptingJobFactory {
123 public:
124 TestURLRequestInterceptingJobFactory(
125 std::unique_ptr<net::URLRequestJobFactory> job_factory,
126 std::unique_ptr<net::URLRequestInterceptor> interceptor,
127 content::WebContents* web_contents)
128 : net::URLRequestInterceptingJobFactory(
129 std::move(job_factory), std::move(interceptor)),
130 web_contents_(web_contents) {}
131 ~TestURLRequestInterceptingJobFactory() override {}
132
133 net::URLRequestJob* MaybeCreateJobWithProtocolHandler(
134 const std::string& scheme,
135 net::URLRequest* request,
136 net::NetworkDelegate* network_delegate) const override {
137 net::URLRequestJob* job = net::URLRequestInterceptingJobFactory::
138 MaybeCreateJobWithProtocolHandler(scheme, request, network_delegate);
139 if (job) {
140 static_cast<OfflinePageRequestJob*>(job)->SetDelegateForTesting(
141 base::MakeUnique<OfflinePageRequestJobTestDelegate>(
142 web_contents_, kTabId));
143 }
144 return job;
145 }
146
147 private:
148 content::WebContents* web_contents_;
149
150 DISALLOW_COPY_AND_ASSIGN(TestURLRequestInterceptingJobFactory);
151 };
152
153 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier {
154 public:
155 TestNetworkChangeNotifier() : online_(true) {}
156 ~TestNetworkChangeNotifier() override {}
157
158 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType()
159 const override {
160 return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN
161 : net::NetworkChangeNotifier::CONNECTION_NONE;
162 }
163
164 void set_online(bool online) { online_ = online; }
165
166 private:
167 bool online_;
168
169 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier);
170 };
171
172 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator {
173 public:
174 explicit TestNetworkQualityEstimator(
175 const std::map<std::string, std::string>& variation_params)
176 : NetworkQualityEstimator(
177 std::unique_ptr<net::ExternalEstimateProvider>(),
178 variation_params),
179 effective_connection_type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {}
180 ~TestNetworkQualityEstimator() override {}
181
182 net::EffectiveConnectionType GetEffectiveConnectionType() const override {
183 return effective_connection_type_;
184 }
185
186 void set_effective_connection_type(
187 net::EffectiveConnectionType effective_connection_type) {
188 effective_connection_type_ = effective_connection_type;
189 }
190
191 private:
192 net::EffectiveConnectionType effective_connection_type_;
193
194 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator);
195 };
196
197 class ScopedEnableProbihibitivelySlowNetwork {
198 public:
199 explicit ScopedEnableProbihibitivelySlowNetwork(
200 net::URLRequestContext* url_request_context)
201 : field_trial_list_(nullptr),
202 url_request_context_(url_request_context) {
203 previews::EnableOfflinePreviewsForTesting();
204
205 test_network_quality_estimator_ =
206 base::MakeUnique<TestNetworkQualityEstimator>
207 (network_quality_estimator_params_);
208 test_network_quality_estimator_->set_effective_connection_type(
209 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
210 url_request_context_->set_network_quality_estimator(
211 test_network_quality_estimator_.get());
212 }
213
214 ~ScopedEnableProbihibitivelySlowNetwork() {
215 url_request_context_->set_network_quality_estimator(nullptr);
216 variations::testing::ClearAllVariationParams();
217 }
218
219 private:
220 base::FieldTrialList field_trial_list_;
221 std::map<std::string, std::string> network_quality_estimator_params_;
222 std::unique_ptr<TestNetworkQualityEstimator> test_network_quality_estimator_;
223 net::URLRequestContext* url_request_context_;
224 };
225
226 class TestOfflinePageArchiver : public OfflinePageArchiver {
227 public:
228 TestOfflinePageArchiver(const GURL& url,
229 const base::FilePath& archive_file_path)
230 : url_(url),
231 archive_file_path_(archive_file_path) {}
232 ~TestOfflinePageArchiver() override {}
233
234 void CreateArchive(const base::FilePath& archives_dir,
235 int64_t archive_id,
236 const CreateArchiveCallback& callback) override {
237 base::ThreadTaskRunnerHandle::Get()->PostTask(
238 FROM_HERE,
239 base::Bind(callback, this, ArchiverResult::SUCCESSFULLY_CREATED,
240 url_, archive_file_path_, base::string16(), kTestFileSize));
241 }
242
243 private:
244 const GURL url_;
245 const base::FilePath archive_file_path_;
246
247 DISALLOW_COPY_AND_ASSIGN(TestOfflinePageArchiver);
248 };
249
250 } // namespace
251
252 class OfflinePageRequestJobTest : public testing::Test {
253 public:
254 OfflinePageRequestJobTest();
255 ~OfflinePageRequestJobTest() override {}
256
257 void SetUp() override;
258
259 void SimulateHasNetworkConnectivity(bool has_connectivity);
260 void RunUntilIdle();
261
262 void InterceptRequest(const GURL& url,
263 const std::string& method,
264 const std::string& extra_header_name,
265 const std::string& extra_header_value,
266 content::ResourceType resource_type);
267
268 void ExpectAggregatedRequestResultHistogram(
269 OfflinePageRequestJob::AggregatedRequestResult result);
270
271 net::TestURLRequestContext* url_request_context() {
272 return test_url_request_context_.get();
273 }
274 Profile* profile() { return profile_; }
275 OfflinePageTabHelper* offline_page_tab_helper() const {
276 return offline_page_tab_helper_;
277 }
278 int64_t offline_id() const { return offline_id_; }
279 int bytes_read() const { return bytes_read_; }
280
281 private:
282 void OnSavePageDone(SavePageResult result, int64_t offline_id);
283 std::unique_ptr<net::URLRequest> CreateRequest(
284 const GURL& url,
285 const std::string& method,
286 content::ResourceType resource_type);
287 void ReadCompleted(int bytes_read);
288
289 // Runs on IO thread.
290 void InterceptRequestOnIO(const GURL& url,
291 const std::string& method,
292 const std::string& extra_header_name,
293 const std::string& extra_header_value,
294 content::ResourceType resource_type,
295 void* profile_id);
296 void ReadCompletedOnIO(int bytes_read);
297
298 content::TestBrowserThreadBundle thread_bundle_;
299 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_;
300 std::unique_ptr<net::TestURLRequestContext> test_url_request_context_;
301 net::URLRequestJobFactoryImpl url_request_job_factory_;
302 std::unique_ptr<net::URLRequestInterceptingJobFactory>
303 intercepting_job_factory_;
304 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_;
305 net::TestNetworkDelegate network_delegate_;
306 TestingProfileManager profile_manager_;
307 TestingProfile* profile_;
308 std::unique_ptr<content::WebContents> web_contents_;
309 base::HistogramTester histogram_tester_;
310 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned.
311 std::unique_ptr<net::URLRequest> request_;
312 int64_t offline_id_;
313 int bytes_read_;
314
315 DISALLOW_COPY_AND_ASSIGN(OfflinePageRequestJobTest);
316 };
317
318 OfflinePageRequestJobTest::OfflinePageRequestJobTest()
319 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
320 network_change_notifier_(new TestNetworkChangeNotifier()),
321 profile_manager_(TestingBrowserProcess::GetGlobal()),
322 offline_id_(-1),
323 bytes_read_(0) {
324 }
325
326 void OfflinePageRequestJobTest::SetUp() {
327 // Create a test profile.
328 ASSERT_TRUE(profile_manager_.SetUp());
329 profile_ = profile_manager_.CreateTestingProfile("Profile 1");
330
331 // Create a test web contents.
332 web_contents_.reset(content::WebContents::Create(
333 content::WebContents::CreateParams(profile_)));
334 OfflinePageTabHelper::CreateForWebContents(web_contents_.get());
335 offline_page_tab_helper_ =
336 OfflinePageTabHelper::FromWebContents(web_contents_.get());
337
338 // Set up the factory for testing.
339 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse(
340 profile(), BuildTestOfflinePageModel);
341 RunUntilIdle();
342
343 // Use a test archive file.
344 base::FilePath archive_file_path;
345 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &archive_file_path));
346 archive_file_path =
347 archive_file_path.AppendASCII("offline_pages").AppendASCII("test.mhtml");
348 std::unique_ptr<TestOfflinePageArchiver> archiver(
349 new TestOfflinePageArchiver(kTestUrl, archive_file_path));
350
351 // Save an offline page.
352 OfflinePageModel* model =
353 OfflinePageModelFactory::GetForBrowserContext(profile());
354 model->SavePage(
355 kTestUrl, kTestClientId, 0, std::move(archiver),
356 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone,
357 base::Unretained(this)));
358 RunUntilIdle();
359
360 // Create a context with delayed initialization.
361 test_url_request_context_.reset(new net::TestURLRequestContext(true));
362
363 // Install the interceptor.
364 std::unique_ptr<net::URLRequestInterceptor> interceptor(
365 new OfflinePageRequestInterceptor(profile_));
366 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl(
367 new net::URLRequestJobFactoryImpl());
368 intercepting_job_factory_.reset(new TestURLRequestInterceptingJobFactory(
369 std::move(job_factory_impl),
370 std::move(interceptor),
371 web_contents_.get()));
372
373 test_url_request_context_->set_job_factory(intercepting_job_factory_.get());
374 test_url_request_context_->Init();
375 }
376
377 void OfflinePageRequestJobTest::SimulateHasNetworkConnectivity(
378 bool online) {
379 network_change_notifier_->set_online(online);
380 }
381
382 void OfflinePageRequestJobTest::RunUntilIdle() {
383 base::RunLoop().RunUntilIdle();
384 }
385
386 std::unique_ptr<net::URLRequest> OfflinePageRequestJobTest::CreateRequest(
387 const GURL& url,
388 const std::string& method,
389 content::ResourceType resource_type) {
390 url_request_delegate_ = base::MakeUnique<TestURLRequestDelegate>(
391 base::Bind(&OfflinePageRequestJobTest::ReadCompletedOnIO,
392 base::Unretained(this)));
393
394 std::unique_ptr<net::URLRequest> request =
395 url_request_context()->CreateRequest(
396 url, net::DEFAULT_PRIORITY, url_request_delegate_.get());
397 request->set_method(method);
398
399 content::ResourceRequestInfo::AllocateForTesting(
400 request.get(),
401 resource_type,
402 nullptr,
403 1, /* render_process_id */
404 -1, /* render_view_id */
405 1, /* render_frame_id */
406 true, /* is_main_frame */
407 false, /* parent_is_main_frame */
408 true, /* allow_download */
409 true, /* is_async */
410 false /* is_using_lofi */);
411
412 return request;
413 }
414
415 void OfflinePageRequestJobTest::ExpectAggregatedRequestResultHistogram(
416 OfflinePageRequestJob::AggregatedRequestResult result) {
417 histogram_tester_.ExpectUniqueSample(
418 kAggregatedRequestResultHistogram, static_cast<int>(result), 1);
419 }
420
421 void OfflinePageRequestJobTest::OnSavePageDone(SavePageResult result,
422 int64_t offline_id) {
423 ASSERT_EQ(SavePageResult::SUCCESS, result);
424 offline_id_ = offline_id;
425 }
426
427 void OfflinePageRequestJobTest::InterceptRequestOnIO(
428 const GURL& url,
429 const std::string& method,
430 const std::string& extra_header_name,
431 const std::string& extra_header_value,
432 content::ResourceType resource_type,
433 void* profile_id) {
434 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
435
436 request_ = CreateRequest(url, method, resource_type);
437 if (!extra_header_name.empty()) {
438 request_->SetExtraRequestHeaderByName(
439 extra_header_name, extra_header_value, true);
440 }
441 request_->Start();
442 }
443
444 void OfflinePageRequestJobTest::InterceptRequest(
445 const GURL& url,
446 const std::string& method,
447 const std::string& extra_header_name,
448 const std::string& extra_header_value,
449 content::ResourceType resource_type) {
450 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
451
452 content::BrowserThread::PostTask(
453 content::BrowserThread::IO, FROM_HERE,
454 base::Bind(&OfflinePageRequestJobTest::InterceptRequestOnIO,
455 base::Unretained(this), url, method, extra_header_name,
456 extra_header_value, resource_type, profile()));
457 }
458
459 void OfflinePageRequestJobTest::ReadCompletedOnIO(int bytes_read) {
460 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
461
462 content::BrowserThread::PostTask(
463 content::BrowserThread::UI, FROM_HERE,
464 base::Bind(&OfflinePageRequestJobTest::ReadCompleted,
465 base::Unretained(this), bytes_read));
466 }
467
468 void OfflinePageRequestJobTest::ReadCompleted(int bytes_read) {
469 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
470
471 bytes_read_ = bytes_read;
472 base::ThreadTaskRunnerHandle::Get()->PostTask(
473 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
474 }
475
476 TEST_F(OfflinePageRequestJobTest, FailedToCreateRequestJob) {
477 SimulateHasNetworkConnectivity(false);
478
479 // Must be http/https URL.
480 InterceptRequest(GURL("ftp://host/doc"), "GET", "", "",
481 content::RESOURCE_TYPE_MAIN_FRAME);
482 base::RunLoop().Run();
483 EXPECT_EQ(0, bytes_read());
484 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
485
486 InterceptRequest(GURL("file:///path/doc"), "GET", "", "",
487 content::RESOURCE_TYPE_MAIN_FRAME);
488 base::RunLoop().Run();
489 EXPECT_EQ(0, bytes_read());
490 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
491
492 // Must be GET method.
493 InterceptRequest(
494 kTestUrl, "POST", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
495 base::RunLoop().Run();
496 EXPECT_EQ(0, bytes_read());
497 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
498
499 InterceptRequest(
500 kTestUrl, "HEAD", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
501 base::RunLoop().Run();
502 EXPECT_EQ(0, bytes_read());
503 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
504
505 // Must be main resource.
506 InterceptRequest(
507 kTestUrl, "POST", "", "", content::RESOURCE_TYPE_SUB_FRAME);
508 base::RunLoop().Run();
509 EXPECT_EQ(0, bytes_read());
510 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
511
512 InterceptRequest(
513 kTestUrl, "POST", "", "", content::RESOURCE_TYPE_IMAGE);
514 base::RunLoop().Run();
515 EXPECT_EQ(0, bytes_read());
516 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
517 }
518
519 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnDisconnectedNetwork) {
520 SimulateHasNetworkConnectivity(false);
521
522 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
523 base::RunLoop().Run();
524
525 EXPECT_EQ(kTestFileSize, bytes_read());
526 ASSERT_TRUE(offline_page_tab_helper()->offline_page());
527 EXPECT_EQ(offline_id(),
528 offline_page_tab_helper()->offline_page()->offline_id);
529 ExpectAggregatedRequestResultHistogram(
530 OfflinePageRequestJob::AggregatedRequestResult::
531 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK);
532 }
533
534 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnDisconnectedNetwork) {
535 SimulateHasNetworkConnectivity(false);
536
537 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
538 base::RunLoop().Run();
539
540 EXPECT_EQ(0, bytes_read());
541 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
542 ExpectAggregatedRequestResultHistogram(
543 OfflinePageRequestJob::AggregatedRequestResult::
544 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK);
545 }
546
547 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnProhibitivelySlowNetwork) {
548 SimulateHasNetworkConnectivity(true);
549
550 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context());
551
552 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
553 base::RunLoop().Run();
554
555 EXPECT_EQ(kTestFileSize, bytes_read());
556 ASSERT_TRUE(offline_page_tab_helper()->offline_page());
557 EXPECT_EQ(offline_id(),
558 offline_page_tab_helper()->offline_page()->offline_id);
559 ExpectAggregatedRequestResultHistogram(
560 OfflinePageRequestJob::AggregatedRequestResult::
561 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK);
562 }
563
564 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnProhibitivelySlowNetwork) {
565 SimulateHasNetworkConnectivity(true);
566
567 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context());
568
569 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
570 base::RunLoop().Run();
571
572 EXPECT_EQ(0, bytes_read());
573 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
574 ExpectAggregatedRequestResultHistogram(
575 OfflinePageRequestJob::AggregatedRequestResult::
576 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK);
577 }
578
579 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnFlakyNetwork) {
580 SimulateHasNetworkConnectivity(true);
581
582 // When custom offline header exists and contains "reason=error", it means
583 // that net error is hit in last request due to flaky network.
584 InterceptRequest(
585 kTestUrl,
586 "GET",
587 kLoadingOfflinePageHeader,
588 std::string(kLoadingOfflinePageReason) + kLoadingOfflinePageDueToNetError,
589 content::RESOURCE_TYPE_MAIN_FRAME);
590 base::RunLoop().Run();
591
592 EXPECT_EQ(kTestFileSize, bytes_read());
593 ASSERT_TRUE(offline_page_tab_helper()->offline_page());
594 EXPECT_EQ(offline_id(),
595 offline_page_tab_helper()->offline_page()->offline_id);
596 ExpectAggregatedRequestResultHistogram(
597 OfflinePageRequestJob::AggregatedRequestResult::
598 SHOW_OFFLINE_ON_FLAKY_NETWORK);
599 }
600
601 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnFlakyNetwork) {
602 SimulateHasNetworkConnectivity(true);
603
604 // When custom offline header exists and contains "reason=error", it means
605 // that net error is hit in last request due to flaky network.
606 InterceptRequest(
607 kTestUrl2,
608 "GET",
609 kLoadingOfflinePageHeader,
610 std::string(kLoadingOfflinePageReason) + kLoadingOfflinePageDueToNetError,
611 content::RESOURCE_TYPE_MAIN_FRAME);
612 base::RunLoop().Run();
613
614 EXPECT_EQ(0, bytes_read());
615 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
616 ExpectAggregatedRequestResultHistogram(
617 OfflinePageRequestJob::AggregatedRequestResult::
618 PAGE_NOT_FOUND_ON_FLAKY_NETWORK);
619 }
620
621 TEST_F(OfflinePageRequestJobTest, ForceLoadOfflinePageOnConnectedNetwork) {
622 SimulateHasNetworkConnectivity(true);
623
624 // When custom offline header exists and contains value other than
625 // "reason=error", it means that offline page is forced to load.
626 InterceptRequest(
627 kTestUrl,
628 "GET",
629 kLoadingOfflinePageHeader,
630 std::string(kLoadingOfflinePageReason) + "download",
631 content::RESOURCE_TYPE_MAIN_FRAME);
632 base::RunLoop().Run();
633
634 EXPECT_EQ(kTestFileSize, bytes_read());
635 ASSERT_TRUE(offline_page_tab_helper()->offline_page());
636 EXPECT_EQ(offline_id(),
637 offline_page_tab_helper()->offline_page()->offline_id);
638 ExpectAggregatedRequestResultHistogram(
639 OfflinePageRequestJob::AggregatedRequestResult::
640 SHOW_OFFLINE_ON_CONNECTED_NETWORK);
641 }
642
643 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnConnectedNetwork) {
644 SimulateHasNetworkConnectivity(true);
645
646 // When custom offline header exists and contains value other than
647 // "reason=error", it means that offline page is forced to load.
648 InterceptRequest(
649 kTestUrl2,
650 "GET",
651 kLoadingOfflinePageHeader,
652 std::string(kLoadingOfflinePageReason) + "download",
653 content::RESOURCE_TYPE_MAIN_FRAME);
654 base::RunLoop().Run();
655
656 EXPECT_EQ(0, bytes_read());
657 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
658 ExpectAggregatedRequestResultHistogram(
659 OfflinePageRequestJob::AggregatedRequestResult::
660 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK);
661 }
662
663 TEST_F(OfflinePageRequestJobTest, DoNotLoadOfflinePageOnConnectedNetwork) {
664 SimulateHasNetworkConnectivity(true);
665
666 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
667 base::RunLoop().Run();
668
669 EXPECT_EQ(0, bytes_read());
670 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
671 }
672
673 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698