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 "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 "content/public/browser/browser_thread.h" | |
29 #include "content/public/browser/resource_request_info.h" | |
30 #include "content/public/browser/web_contents.h" | |
31 #include "content/public/common/resource_type.h" | |
32 #include "content/public/test/test_browser_thread_bundle.h" | |
33 #include "net/nqe/network_quality_estimator.h" | |
34 #include "net/url_request/url_request.h" | |
35 #include "net/url_request/url_request_context.h" | |
36 #include "net/url_request/url_request_job_factory_impl.h" | |
37 #include "net/url_request/url_request_test_util.h" | |
38 #include "testing/gtest/include/gtest/gtest.h" | |
39 | |
40 namespace offline_pages { | |
41 | |
42 namespace { | |
43 | |
44 const char kTestHttpUrl[] = "http://test.org/page1"; | |
45 const char kTestHttpsUrl[] = "https://test.org/page1"; | |
46 const GURL kTestPageUrl("http://test.org/page1"); | |
47 const GURL kTestPageUrl2("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 TestJobFactoryProtocolHandler | |
122 : public net::URLRequestJobFactory::ProtocolHandler { | |
123 public: | |
124 TestJobFactoryProtocolHandler( | |
125 std::unique_ptr<net::URLRequestInterceptor> interceptor, | |
126 content::WebContents* web_contents) | |
127 : interceptor_(std::move(interceptor)), | |
128 web_contents_(web_contents) {} | |
129 ~TestJobFactoryProtocolHandler() override {} | |
130 | |
131 net::URLRequestJob* MaybeCreateJob( | |
132 net::URLRequest* request, | |
133 net::NetworkDelegate* network_delegate) const override { | |
134 net::URLRequestJob* job = | |
135 interceptor_->MaybeInterceptRequest(request, network_delegate); | |
136 if (job) { | |
137 static_cast<OfflinePageRequestJob*>(job)->SetDelegateForTesting( | |
138 base::MakeUnique<OfflinePageRequestJobTestDelegate>( | |
139 web_contents_, kTabId)); | |
140 } | |
141 return job; | |
142 } | |
143 | |
144 private: | |
145 std::unique_ptr<net::URLRequestInterceptor> interceptor_; | |
146 content::WebContents* web_contents_; | |
147 | |
148 DISALLOW_COPY_AND_ASSIGN(TestJobFactoryProtocolHandler); | |
149 }; | |
150 | |
151 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { | |
152 public: | |
153 TestNetworkChangeNotifier() : online_(true) {} | |
154 ~TestNetworkChangeNotifier() override {} | |
155 | |
156 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() | |
157 const override { | |
158 return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN | |
159 : net::NetworkChangeNotifier::CONNECTION_NONE; | |
160 } | |
161 | |
162 void set_online(bool online) { online_ = online; } | |
163 | |
164 private: | |
165 bool online_; | |
166 | |
167 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); | |
168 }; | |
169 | |
170 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { | |
171 public: | |
172 explicit TestNetworkQualityEstimator( | |
173 const std::map<std::string, std::string>& variation_params) | |
174 : NetworkQualityEstimator( | |
175 std::unique_ptr<net::ExternalEstimateProvider>(), | |
176 variation_params), | |
177 effective_connection_type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {} | |
178 ~TestNetworkQualityEstimator() override {} | |
179 | |
180 net::EffectiveConnectionType GetEffectiveConnectionType() const override { | |
181 return effective_connection_type_; | |
182 } | |
183 | |
184 void set_effective_connection_type( | |
185 net::EffectiveConnectionType effective_connection_type) { | |
186 effective_connection_type_ = effective_connection_type; | |
187 } | |
188 | |
189 private: | |
190 net::EffectiveConnectionType effective_connection_type_; | |
191 | |
192 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); | |
193 }; | |
194 | |
195 class ScopedEnableProbihibitivelySlowNetwork { | |
196 public: | |
197 explicit ScopedEnableProbihibitivelySlowNetwork( | |
198 net::URLRequestContext* url_request_context) | |
199 : field_trial_list_(nullptr), | |
200 url_request_context_(url_request_context) { | |
201 previews::EnableOfflinePreviewsForTesting(); | |
202 | |
203 test_network_quality_estimator_ = | |
204 base::MakeUnique<TestNetworkQualityEstimator> | |
205 (network_quality_estimator_params_); | |
206 test_network_quality_estimator_->set_effective_connection_type( | |
207 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); | |
208 url_request_context_->set_network_quality_estimator( | |
209 test_network_quality_estimator_.get()); | |
210 } | |
211 | |
212 ~ScopedEnableProbihibitivelySlowNetwork() { | |
213 url_request_context_->set_network_quality_estimator(nullptr); | |
214 } | |
215 | |
216 private: | |
217 base::FieldTrialList field_trial_list_; | |
218 std::map<std::string, std::string> network_quality_estimator_params_; | |
219 std::unique_ptr<TestNetworkQualityEstimator> test_network_quality_estimator_; | |
220 net::URLRequestContext* url_request_context_; | |
221 }; | |
222 | |
223 class TestOfflinePageArchiver : public OfflinePageArchiver { | |
224 public: | |
225 TestOfflinePageArchiver(const GURL& url, | |
226 const base::FilePath& archive_file_path) | |
227 : url_(url), | |
228 archive_file_path_(archive_file_path) {} | |
229 ~TestOfflinePageArchiver() override {} | |
230 | |
231 void CreateArchive(const base::FilePath& archives_dir, | |
232 int64_t archive_id, | |
233 const CreateArchiveCallback& callback) override { | |
234 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
235 FROM_HERE, | |
236 base::Bind(callback, this, ArchiverResult::SUCCESSFULLY_CREATED, | |
237 url_, archive_file_path_, base::string16(), kTestFileSize)); | |
238 } | |
239 | |
240 private: | |
241 const GURL url_; | |
242 const base::FilePath archive_file_path_; | |
243 | |
244 DISALLOW_COPY_AND_ASSIGN(TestOfflinePageArchiver); | |
245 }; | |
246 | |
247 } // namespace | |
248 | |
249 class OfflinePageRequestJobTest : public testing::Test { | |
250 public: | |
251 OfflinePageRequestJobTest(); | |
252 ~OfflinePageRequestJobTest() override {} | |
253 | |
254 void SetUp() override; | |
255 | |
256 void SimulateHasNetworkConnectivity(bool has_connectivity); | |
257 void RunUntilIdle(); | |
258 void InterceptRequest(const GURL& url, | |
259 const std::string& extra_header_name, | |
260 const std::string& extra_header_value, | |
261 void* profile_id); | |
262 | |
263 std::unique_ptr<net::URLRequest> CreateRequest( | |
264 const GURL& url, | |
265 const std::string& method, | |
266 content::ResourceType resource_type); | |
267 | |
268 bool CreateRequestJobCheck( | |
269 const std::string& url, | |
270 const std::string& method, | |
271 content::ResourceType resource_type); | |
272 | |
273 void ExpectAggregatedRequestResultHistogram( | |
274 OfflinePageRequestJob::AggregatedRequestResult result); | |
275 | |
276 net::URLRequestContext* url_request_context() { | |
277 return &test_url_request_context_; | |
278 } | |
279 Profile* profile() { return profile_; } | |
280 OfflinePageTabHelper* offline_page_tab_helper() const { | |
281 return offline_page_tab_helper_; | |
282 } | |
283 int64_t offline_id() const { return offline_id_; } | |
284 int bytes_read() const { return bytes_read_; } | |
285 | |
286 private: | |
287 void OnSavePageDone(SavePageResult result, int64_t offline_id); | |
288 void ReadCompleted(int bytes_read); | |
289 | |
290 // Runs on IO thread. | |
291 void InterceptRequestOnIO(const GURL& url, | |
292 const std::string& extra_header_name, | |
293 const std::string& extra_header_value, | |
294 void* profile_id); | |
295 void ReadCompletedOnIO(int bytes_read); | |
296 | |
297 content::TestBrowserThreadBundle thread_bundle_; | |
298 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; | |
299 net::TestURLRequestContext test_url_request_context_; | |
300 net::URLRequestJobFactoryImpl url_request_job_factory_; | |
301 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_; | |
302 net::TestNetworkDelegate network_delegate_; | |
303 TestingProfileManager profile_manager_; | |
304 TestingProfile* profile_; | |
305 std::unique_ptr<content::WebContents> web_contents_; | |
306 base::HistogramTester histogram_tester_; | |
307 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. | |
308 std::unique_ptr<net::URLRequest> request_; | |
309 int64_t offline_id_; | |
310 int bytes_read_; | |
311 | |
312 DISALLOW_COPY_AND_ASSIGN(OfflinePageRequestJobTest); | |
313 }; | |
314 | |
315 OfflinePageRequestJobTest::OfflinePageRequestJobTest() | |
316 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | |
317 network_change_notifier_(new TestNetworkChangeNotifier()), | |
318 profile_manager_(TestingBrowserProcess::GetGlobal()), | |
319 offline_id_(-1), | |
320 bytes_read_(0) { | |
321 test_url_request_context_.set_job_factory(&url_request_job_factory_); | |
322 } | |
323 | |
324 void OfflinePageRequestJobTest::SetUp() { | |
325 // Create a test profile. | |
326 ASSERT_TRUE(profile_manager_.SetUp()); | |
327 profile_ = profile_manager_.CreateTestingProfile("Profile 1"); | |
328 | |
329 // Create a test web contents. | |
330 web_contents_.reset(content::WebContents::Create( | |
331 content::WebContents::CreateParams(profile_))); | |
332 OfflinePageTabHelper::CreateForWebContents(web_contents_.get()); | |
333 offline_page_tab_helper_ = | |
334 OfflinePageTabHelper::FromWebContents(web_contents_.get()); | |
335 | |
336 // Set up the factory for testing. | |
337 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( | |
338 profile(), BuildTestOfflinePageModel); | |
339 RunUntilIdle(); | |
340 | |
341 // Use a test archive file. | |
342 base::FilePath archive_file_path; | |
343 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &archive_file_path)); | |
344 archive_file_path = | |
345 archive_file_path.AppendASCII("offline_pages").AppendASCII("test.mhtml"); | |
346 std::unique_ptr<TestOfflinePageArchiver> archiver( | |
347 new TestOfflinePageArchiver(kTestPageUrl, archive_file_path)); | |
348 | |
349 // Save an offline page. | |
350 OfflinePageModel* model = | |
351 OfflinePageModelFactory::GetForBrowserContext(profile()); | |
352 model->SavePage( | |
353 kTestPageUrl, kTestClientId, 0, std::move(archiver), | |
354 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, | |
355 base::Unretained(this))); | |
356 RunUntilIdle(); | |
357 } | |
358 | |
359 void OfflinePageRequestJobTest::SimulateHasNetworkConnectivity( | |
360 bool online) { | |
361 network_change_notifier_->set_online(online); | |
362 } | |
363 | |
364 void OfflinePageRequestJobTest::RunUntilIdle() { | |
365 base::RunLoop().RunUntilIdle(); | |
366 } | |
367 | |
368 std::unique_ptr<net::URLRequest> OfflinePageRequestJobTest::CreateRequest( | |
369 const GURL& url, | |
370 const std::string& method, | |
371 content::ResourceType resource_type) { | |
372 url_request_delegate_ = base::MakeUnique<TestURLRequestDelegate>( | |
373 base::Bind(&OfflinePageRequestJobTest::ReadCompletedOnIO, | |
374 base::Unretained(this))); | |
375 | |
376 std::unique_ptr<net::URLRequest> request = | |
377 url_request_context()->CreateRequest( | |
378 url, net::DEFAULT_PRIORITY, url_request_delegate_.get()); | |
379 request->set_method(method); | |
380 | |
381 content::ResourceRequestInfo::AllocateForTesting( | |
382 request.get(), | |
383 resource_type, | |
384 nullptr, | |
385 1, /* render_process_id */ | |
386 -1, /* render_view_id */ | |
387 1, /* render_frame_id */ | |
388 true, /* is_main_frame */ | |
389 false, /* parent_is_main_frame */ | |
390 true, /* allow_download */ | |
391 true, /* is_async */ | |
392 false /* is_using_lofi */); | |
393 | |
394 return std::move(request); | |
395 } | |
396 | |
397 bool OfflinePageRequestJobTest::CreateRequestJobCheck( | |
398 const std::string& url, | |
399 const std::string& method, | |
400 content::ResourceType resource_type) { | |
401 std::unique_ptr<net::URLRequest> request = | |
402 CreateRequest(GURL(url), method, resource_type); | |
403 | |
404 std::unique_ptr<OfflinePageRequestJob> job( | |
405 OfflinePageRequestJob::Create( | |
406 &profile_, request.get(), &network_delegate_)); | |
mmenke
2016/08/22 23:40:05
I also recommend against this - I'd suggest hookin
jianli
2016/08/23 01:27:34
Done.
| |
407 return job.get() != nullptr; | |
408 } | |
409 | |
410 void OfflinePageRequestJobTest::ExpectAggregatedRequestResultHistogram( | |
411 OfflinePageRequestJob::AggregatedRequestResult result) { | |
412 histogram_tester_.ExpectUniqueSample( | |
413 kAggregatedRequestResultHistogram, static_cast<int>(result), 1); | |
414 } | |
415 | |
416 void OfflinePageRequestJobTest::OnSavePageDone(SavePageResult result, | |
417 int64_t offline_id) { | |
418 ASSERT_EQ(SavePageResult::SUCCESS, result); | |
419 offline_id_ = offline_id; | |
420 } | |
421 | |
422 void OfflinePageRequestJobTest::InterceptRequestOnIO( | |
423 const GURL& url, | |
424 const std::string& extra_header_name, | |
425 const std::string& extra_header_value, | |
426 void* profile_id) { | |
427 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
428 | |
429 std::unique_ptr<net::URLRequestInterceptor> interceptor( | |
430 new OfflinePageRequestInterceptor(profile_id)); | |
431 | |
432 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler> protocol_handler( | |
433 new TestJobFactoryProtocolHandler( | |
434 std::move(interceptor), web_contents_.get())); | |
435 | |
436 url_request_job_factory_.SetProtocolHandler( | |
437 "http", std::move(protocol_handler)); | |
mmenke
2016/08/22 23:40:05
I'd recommend against this approach. Think it's b
jianli
2016/08/23 01:27:34
Done.
| |
438 | |
439 request_ = CreateRequest(url, "GET", content::RESOURCE_TYPE_MAIN_FRAME); | |
440 if (!extra_header_name.empty()) { | |
441 request_->SetExtraRequestHeaderByName( | |
442 extra_header_name, extra_header_value, true); | |
443 } | |
444 request_->Start(); | |
445 } | |
446 | |
447 void OfflinePageRequestJobTest::InterceptRequest( | |
448 const GURL& url, | |
449 const std::string& extra_header_name, | |
450 const std::string& extra_header_value, | |
451 void* profile_id) { | |
452 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
453 | |
454 content::BrowserThread::PostTask( | |
455 content::BrowserThread::IO, FROM_HERE, | |
456 base::Bind(&OfflinePageRequestJobTest::InterceptRequestOnIO, | |
457 base::Unretained(this), url, extra_header_name, | |
458 extra_header_value, profile())); | |
459 } | |
460 | |
461 void OfflinePageRequestJobTest::ReadCompletedOnIO(int bytes_read) { | |
462 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
463 | |
464 content::BrowserThread::PostTask( | |
465 content::BrowserThread::UI, FROM_HERE, | |
466 base::Bind(&OfflinePageRequestJobTest::ReadCompleted, | |
467 base::Unretained(this), bytes_read)); | |
468 } | |
469 | |
470 void OfflinePageRequestJobTest::ReadCompleted(int bytes_read) { | |
471 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
472 | |
473 bytes_read_ = bytes_read; | |
474 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
475 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | |
476 } | |
477 | |
478 TEST_F(OfflinePageRequestJobTest, CreateRequestJob) { | |
479 EXPECT_TRUE(CreateRequestJobCheck( | |
480 kTestHttpUrl, "GET", content::RESOURCE_TYPE_MAIN_FRAME)); | |
481 EXPECT_TRUE(CreateRequestJobCheck( | |
482 kTestHttpsUrl, "GET", content::RESOURCE_TYPE_MAIN_FRAME)); | |
483 | |
484 EXPECT_FALSE(CreateRequestJobCheck( | |
485 "ftp://host/doc", "GET", content::RESOURCE_TYPE_MAIN_FRAME)); | |
486 EXPECT_FALSE(CreateRequestJobCheck( | |
487 "file:///path/doc", "GET", content::RESOURCE_TYPE_MAIN_FRAME)); | |
488 | |
489 EXPECT_FALSE(CreateRequestJobCheck( | |
490 kTestHttpUrl, "POST", content::RESOURCE_TYPE_MAIN_FRAME)); | |
491 EXPECT_FALSE(CreateRequestJobCheck( | |
492 kTestHttpUrl, "HEAD", content::RESOURCE_TYPE_MAIN_FRAME)); | |
493 | |
494 EXPECT_FALSE(CreateRequestJobCheck( | |
495 kTestHttpUrl, "GET", content::RESOURCE_TYPE_SUB_FRAME)); | |
496 EXPECT_FALSE(CreateRequestJobCheck( | |
497 kTestHttpUrl, "GET", content::RESOURCE_TYPE_IMAGE)); | |
498 } | |
499 | |
500 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnDisconnectedNetwork) { | |
501 SimulateHasNetworkConnectivity(false); | |
502 | |
503 InterceptRequest(kTestPageUrl, "", "", profile()); | |
504 base::RunLoop().Run(); | |
505 | |
506 EXPECT_EQ(kTestFileSize, bytes_read()); | |
507 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); | |
508 EXPECT_EQ(offline_id(), | |
509 offline_page_tab_helper()->offline_page()->offline_id); | |
510 ExpectAggregatedRequestResultHistogram( | |
511 OfflinePageRequestJob::AggregatedRequestResult:: | |
512 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK); | |
513 } | |
514 | |
515 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnDisconnectedNetwork) { | |
516 SimulateHasNetworkConnectivity(false); | |
517 | |
518 InterceptRequest(kTestPageUrl2, "", "", profile()); | |
519 base::RunLoop().Run(); | |
520 | |
521 EXPECT_EQ(0, bytes_read()); | |
522 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); | |
523 ExpectAggregatedRequestResultHistogram( | |
524 OfflinePageRequestJob::AggregatedRequestResult:: | |
525 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); | |
526 } | |
527 | |
528 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnProhibitivelySlowNetwork) { | |
529 SimulateHasNetworkConnectivity(true); | |
530 | |
531 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); | |
532 | |
533 InterceptRequest(kTestPageUrl, "", "", profile()); | |
534 base::RunLoop().Run(); | |
535 | |
536 EXPECT_EQ(kTestFileSize, bytes_read()); | |
537 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); | |
538 EXPECT_EQ(offline_id(), | |
539 offline_page_tab_helper()->offline_page()->offline_id); | |
540 ExpectAggregatedRequestResultHistogram( | |
541 OfflinePageRequestJob::AggregatedRequestResult:: | |
542 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK); | |
543 } | |
544 | |
545 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnProhibitivelySlowNetwork) { | |
546 SimulateHasNetworkConnectivity(true); | |
547 | |
548 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); | |
549 | |
550 InterceptRequest(kTestPageUrl2, "", "", profile()); | |
551 base::RunLoop().Run(); | |
552 | |
553 EXPECT_EQ(0, bytes_read()); | |
554 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); | |
555 ExpectAggregatedRequestResultHistogram( | |
556 OfflinePageRequestJob::AggregatedRequestResult:: | |
557 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK); | |
558 } | |
559 | |
560 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnFlakyNetwork) { | |
561 SimulateHasNetworkConnectivity(true); | |
562 | |
563 // When custom offline header exists and contains "reason=error", it means | |
564 // that net error is hit in last request due to flaky network. | |
565 InterceptRequest( | |
566 kTestPageUrl, | |
567 kLoadingOfflinePageHeader, | |
568 std::string(kLoadingOfflinePageReason) + kLoadingOfflinePageDueToNetError, | |
569 profile()); | |
570 base::RunLoop().Run(); | |
571 | |
572 EXPECT_EQ(kTestFileSize, bytes_read()); | |
573 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); | |
574 EXPECT_EQ(offline_id(), | |
575 offline_page_tab_helper()->offline_page()->offline_id); | |
576 ExpectAggregatedRequestResultHistogram( | |
577 OfflinePageRequestJob::AggregatedRequestResult:: | |
578 SHOW_OFFLINE_ON_FLAKY_NETWORK); | |
579 } | |
580 | |
581 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnFlakyNetwork) { | |
582 SimulateHasNetworkConnectivity(true); | |
583 | |
584 // When custom offline header exists and contains "reason=error", it means | |
585 // that net error is hit in last request due to flaky network. | |
586 InterceptRequest( | |
587 kTestPageUrl2, | |
588 kLoadingOfflinePageHeader, | |
589 std::string(kLoadingOfflinePageReason) + kLoadingOfflinePageDueToNetError, | |
590 profile()); | |
591 base::RunLoop().Run(); | |
592 | |
593 EXPECT_EQ(0, bytes_read()); | |
594 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); | |
595 ExpectAggregatedRequestResultHistogram( | |
596 OfflinePageRequestJob::AggregatedRequestResult:: | |
597 PAGE_NOT_FOUND_ON_FLAKY_NETWORK); | |
598 } | |
599 | |
600 TEST_F(OfflinePageRequestJobTest, ForceLoadOfflinePageOnConnectedNetwork) { | |
601 SimulateHasNetworkConnectivity(true); | |
602 | |
603 // When custom offline header exists and contains value other than | |
604 // "reason=error", it means that offline page is forced to load. | |
605 InterceptRequest( | |
606 kTestPageUrl, | |
607 kLoadingOfflinePageHeader, | |
608 std::string(kLoadingOfflinePageReason) + "download", | |
609 profile()); | |
610 base::RunLoop().Run(); | |
611 | |
612 EXPECT_EQ(kTestFileSize, bytes_read()); | |
613 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); | |
614 EXPECT_EQ(offline_id(), | |
615 offline_page_tab_helper()->offline_page()->offline_id); | |
616 ExpectAggregatedRequestResultHistogram( | |
617 OfflinePageRequestJob::AggregatedRequestResult:: | |
618 SHOW_OFFLINE_ON_CONNECTED_NETWORK); | |
619 } | |
620 | |
621 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnConnectedNetwork) { | |
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 kTestPageUrl2, | |
628 kLoadingOfflinePageHeader, | |
629 std::string(kLoadingOfflinePageReason) + "download", | |
630 profile()); | |
631 base::RunLoop().Run(); | |
632 | |
633 EXPECT_EQ(0, bytes_read()); | |
634 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); | |
635 ExpectAggregatedRequestResultHistogram( | |
636 OfflinePageRequestJob::AggregatedRequestResult:: | |
637 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK); | |
638 } | |
639 | |
640 TEST_F(OfflinePageRequestJobTest, DoNotLoadOfflinePageOnConnectedNetwork) { | |
641 SimulateHasNetworkConnectivity(true); | |
642 | |
643 InterceptRequest(kTestPageUrl, "", "", profile()); | |
644 base::RunLoop().Run(); | |
645 | |
646 EXPECT_EQ(0, bytes_read()); | |
647 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); | |
648 } | |
649 | |
650 } // namespace offline_pages | |
OLD | NEW |