| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/offline_pages/background_loader_offliner.h" | 5 #include "chrome/browser/android/offline_pages/background_loader_offliner.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/test/histogram_tester.h" | 9 #include "base/test/histogram_tester.h" |
| 10 #include "base/test/scoped_mock_time_message_loop_task_runner.h" | 10 #include "base/test/scoped_mock_time_message_loop_task_runner.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 } | 119 } |
| 120 | 120 |
| 121 class BackgroundLoaderOfflinerTest : public testing::Test { | 121 class BackgroundLoaderOfflinerTest : public testing::Test { |
| 122 public: | 122 public: |
| 123 BackgroundLoaderOfflinerTest(); | 123 BackgroundLoaderOfflinerTest(); |
| 124 ~BackgroundLoaderOfflinerTest() override; | 124 ~BackgroundLoaderOfflinerTest() override; |
| 125 | 125 |
| 126 void SetUp() override; | 126 void SetUp() override; |
| 127 | 127 |
| 128 TestBackgroundLoaderOffliner* offliner() const { return offliner_.get(); } | 128 TestBackgroundLoaderOffliner* offliner() const { return offliner_.get(); } |
| 129 Offliner::CompletionCallback const callback() { | 129 Offliner::CompletionCallback const completion_callback() { |
| 130 return base::Bind(&BackgroundLoaderOfflinerTest::OnCompletion, | 130 return base::Bind(&BackgroundLoaderOfflinerTest::OnCompletion, |
| 131 base::Unretained(this)); | 131 base::Unretained(this)); |
| 132 } | 132 } |
| 133 Offliner::ProgressCallback const progress_callback() { |
| 134 return base::Bind(&BackgroundLoaderOfflinerTest::OnProgress, |
| 135 base::Unretained(this)); |
| 136 } |
| 133 Offliner::CancelCallback const cancel_callback() { | 137 Offliner::CancelCallback const cancel_callback() { |
| 134 return base::Bind(&BackgroundLoaderOfflinerTest::OnCancel, | 138 return base::Bind(&BackgroundLoaderOfflinerTest::OnCancel, |
| 135 base::Unretained(this)); | 139 base::Unretained(this)); |
| 136 } | 140 } |
| 137 Profile* profile() { return &profile_; } | 141 Profile* profile() { return &profile_; } |
| 138 bool completion_callback_called() { return completion_callback_called_; } | 142 bool completion_callback_called() { return completion_callback_called_; } |
| 139 Offliner::RequestStatus request_status() { return request_status_; } | 143 Offliner::RequestStatus request_status() { return request_status_; } |
| 140 bool cancel_callback_called() { return cancel_callback_called_; } | 144 bool cancel_callback_called() { return cancel_callback_called_; } |
| 141 bool SaveInProgress() const { return model_->mock_saving(); } | 145 bool SaveInProgress() const { return model_->mock_saving(); } |
| 142 MockOfflinePageModel* model() const { return model_; } | 146 MockOfflinePageModel* model() const { return model_; } |
| 143 const base::HistogramTester& histograms() const { return histogram_tester_; } | 147 const base::HistogramTester& histograms() const { return histogram_tester_; } |
| 144 | 148 |
| 145 void CompleteLoading() { | 149 void CompleteLoading() { |
| 146 // For some reason, setting loading to True will call DidStopLoading | 150 // For some reason, setting loading to True will call DidStopLoading |
| 147 // on the observers. | 151 // on the observers. |
| 148 offliner()->web_contents_tester()->TestSetIsLoading(true); | 152 offliner()->web_contents_tester()->TestSetIsLoading(true); |
| 149 } | 153 } |
| 150 | 154 |
| 151 void PumpLoop() { base::RunLoop().RunUntilIdle(); } | 155 void PumpLoop() { base::RunLoop().RunUntilIdle(); } |
| 152 | 156 |
| 153 private: | 157 private: |
| 154 void OnCompletion(const SavePageRequest& request, | 158 void OnCompletion(const SavePageRequest& request, |
| 155 Offliner::RequestStatus status); | 159 Offliner::RequestStatus status); |
| 160 void OnProgress(const SavePageRequest& request, int64_t bytes); |
| 156 void OnCancel(int64_t offline_id); | 161 void OnCancel(int64_t offline_id); |
| 157 content::TestBrowserThreadBundle thread_bundle_; | 162 content::TestBrowserThreadBundle thread_bundle_; |
| 158 TestingProfile profile_; | 163 TestingProfile profile_; |
| 159 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; | 164 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; |
| 160 MockOfflinePageModel* model_; | 165 MockOfflinePageModel* model_; |
| 161 bool completion_callback_called_; | 166 bool completion_callback_called_; |
| 162 bool cancel_callback_called_; | 167 bool cancel_callback_called_; |
| 163 Offliner::RequestStatus request_status_; | 168 Offliner::RequestStatus request_status_; |
| 164 base::HistogramTester histogram_tester_; | 169 base::HistogramTester histogram_tester_; |
| 165 | 170 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 181 } | 186 } |
| 182 | 187 |
| 183 void BackgroundLoaderOfflinerTest::OnCompletion( | 188 void BackgroundLoaderOfflinerTest::OnCompletion( |
| 184 const SavePageRequest& request, | 189 const SavePageRequest& request, |
| 185 Offliner::RequestStatus status) { | 190 Offliner::RequestStatus status) { |
| 186 DCHECK(!completion_callback_called_); // Expect 1 callback per request. | 191 DCHECK(!completion_callback_called_); // Expect 1 callback per request. |
| 187 completion_callback_called_ = true; | 192 completion_callback_called_ = true; |
| 188 request_status_ = status; | 193 request_status_ = status; |
| 189 } | 194 } |
| 190 | 195 |
| 196 void BackgroundLoaderOfflinerTest::OnProgress(const SavePageRequest& request, |
| 197 int64_t bytes) {} |
| 198 |
| 191 void BackgroundLoaderOfflinerTest::OnCancel(int64_t offline_id) { | 199 void BackgroundLoaderOfflinerTest::OnCancel(int64_t offline_id) { |
| 192 DCHECK(!cancel_callback_called_); | 200 DCHECK(!cancel_callback_called_); |
| 193 cancel_callback_called_ = true; | 201 cancel_callback_called_ = true; |
| 194 } | 202 } |
| 195 | 203 |
| 196 TEST_F(BackgroundLoaderOfflinerTest, | 204 TEST_F(BackgroundLoaderOfflinerTest, |
| 197 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) { | 205 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) { |
| 198 base::Time creation_time = base::Time::Now(); | 206 base::Time creation_time = base::Time::Now(); |
| 199 ClientId custom_tabs_client_id("custom_tabs", "88"); | 207 ClientId custom_tabs_client_id("custom_tabs", "88"); |
| 200 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, | 208 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, |
| 201 creation_time, kUserRequested); | 209 creation_time, kUserRequested); |
| 202 | 210 |
| 203 profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); | 211 profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); |
| 204 EXPECT_FALSE(offliner()->LoadAndSave(request, callback())); | 212 EXPECT_FALSE(offliner()->LoadAndSave(request, completion_callback(), |
| 213 progress_callback())); |
| 205 histograms().ExpectBucketCount( | 214 histograms().ExpectBucketCount( |
| 206 "OfflinePages.Background.CctApiDisableStatus", | 215 "OfflinePages.Background.CctApiDisableStatus", |
| 207 static_cast<int>(OfflinePagesCctApiPrerenderAllowedStatus:: | 216 static_cast<int>(OfflinePagesCctApiPrerenderAllowedStatus:: |
| 208 THIRD_PARTY_COOKIES_DISABLED), | 217 THIRD_PARTY_COOKIES_DISABLED), |
| 209 1); | 218 1); |
| 210 histograms().ExpectBucketCount("OfflinePages.Background.CctApiDisableStatus", | 219 histograms().ExpectBucketCount("OfflinePages.Background.CctApiDisableStatus", |
| 211 0 /* PRERENDER_ALLOWED */, 0); | 220 0 /* PRERENDER_ALLOWED */, 0); |
| 212 } | 221 } |
| 213 | 222 |
| 214 TEST_F(BackgroundLoaderOfflinerTest, | 223 TEST_F(BackgroundLoaderOfflinerTest, |
| 215 LoadAndSaveNetworkPredictionDisabledForCustomTabs) { | 224 LoadAndSaveNetworkPredictionDisabledForCustomTabs) { |
| 216 base::Time creation_time = base::Time::Now(); | 225 base::Time creation_time = base::Time::Now(); |
| 217 ClientId custom_tabs_client_id("custom_tabs", "88"); | 226 ClientId custom_tabs_client_id("custom_tabs", "88"); |
| 218 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, | 227 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, |
| 219 creation_time, kUserRequested); | 228 creation_time, kUserRequested); |
| 220 | 229 |
| 221 profile()->GetPrefs()->SetInteger( | 230 profile()->GetPrefs()->SetInteger( |
| 222 prefs::kNetworkPredictionOptions, | 231 prefs::kNetworkPredictionOptions, |
| 223 chrome_browser_net::NETWORK_PREDICTION_NEVER); | 232 chrome_browser_net::NETWORK_PREDICTION_NEVER); |
| 224 EXPECT_FALSE(offliner()->LoadAndSave(request, callback())); | 233 EXPECT_FALSE(offliner()->LoadAndSave(request, completion_callback(), |
| 234 progress_callback())); |
| 225 histograms().ExpectBucketCount( | 235 histograms().ExpectBucketCount( |
| 226 "OfflinePages.Background.CctApiDisableStatus", | 236 "OfflinePages.Background.CctApiDisableStatus", |
| 227 static_cast<int>(OfflinePagesCctApiPrerenderAllowedStatus:: | 237 static_cast<int>(OfflinePagesCctApiPrerenderAllowedStatus:: |
| 228 NETWORK_PREDICTION_DISABLED), | 238 NETWORK_PREDICTION_DISABLED), |
| 229 1); | 239 1); |
| 230 histograms().ExpectBucketCount( | 240 histograms().ExpectBucketCount( |
| 231 "OfflinePages.Background.CctApiDisableStatus", | 241 "OfflinePages.Background.CctApiDisableStatus", |
| 232 static_cast<int>( | 242 static_cast<int>( |
| 233 OfflinePagesCctApiPrerenderAllowedStatus::PRERENDER_ALLOWED), | 243 OfflinePagesCctApiPrerenderAllowedStatus::PRERENDER_ALLOWED), |
| 234 0); | 244 0); |
| 235 } | 245 } |
| 236 | 246 |
| 237 TEST_F(BackgroundLoaderOfflinerTest, LoadAndSaveStartsLoading) { | 247 TEST_F(BackgroundLoaderOfflinerTest, LoadAndSaveStartsLoading) { |
| 238 base::Time creation_time = base::Time::Now(); | 248 base::Time creation_time = base::Time::Now(); |
| 239 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 249 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 240 kUserRequested); | 250 kUserRequested); |
| 241 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 251 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 252 progress_callback())); |
| 242 EXPECT_TRUE(offliner()->is_loading()); | 253 EXPECT_TRUE(offliner()->is_loading()); |
| 243 EXPECT_FALSE(SaveInProgress()); | 254 EXPECT_FALSE(SaveInProgress()); |
| 244 EXPECT_FALSE(completion_callback_called()); | 255 EXPECT_FALSE(completion_callback_called()); |
| 245 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); | 256 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); |
| 246 } | 257 } |
| 247 | 258 |
| 248 TEST_F(BackgroundLoaderOfflinerTest, CompleteLoadingInitiatesSave) { | 259 TEST_F(BackgroundLoaderOfflinerTest, CompleteLoadingInitiatesSave) { |
| 249 base::Time creation_time = base::Time::Now(); | 260 base::Time creation_time = base::Time::Now(); |
| 250 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 261 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 251 kUserRequested); | 262 kUserRequested); |
| 252 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 263 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 264 progress_callback())); |
| 253 CompleteLoading(); | 265 CompleteLoading(); |
| 254 PumpLoop(); | 266 PumpLoop(); |
| 255 EXPECT_FALSE(completion_callback_called()); | 267 EXPECT_FALSE(completion_callback_called()); |
| 256 EXPECT_TRUE(SaveInProgress()); | 268 EXPECT_TRUE(SaveInProgress()); |
| 257 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); | 269 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); |
| 258 } | 270 } |
| 259 | 271 |
| 260 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoading) { | 272 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoading) { |
| 261 base::Time creation_time = base::Time::Now(); | 273 base::Time creation_time = base::Time::Now(); |
| 262 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 274 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 263 kUserRequested); | 275 kUserRequested); |
| 264 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 276 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 277 progress_callback())); |
| 265 offliner()->Cancel(cancel_callback()); | 278 offliner()->Cancel(cancel_callback()); |
| 266 PumpLoop(); | 279 PumpLoop(); |
| 267 EXPECT_TRUE(cancel_callback_called()); | 280 EXPECT_TRUE(cancel_callback_called()); |
| 268 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. | 281 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. |
| 269 } | 282 } |
| 270 | 283 |
| 271 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) { | 284 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) { |
| 272 base::Time creation_time = base::Time::Now(); | 285 base::Time creation_time = base::Time::Now(); |
| 273 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 286 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 274 kUserRequested); | 287 kUserRequested); |
| 275 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 288 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 289 progress_callback())); |
| 276 CompleteLoading(); | 290 CompleteLoading(); |
| 277 PumpLoop(); | 291 PumpLoop(); |
| 278 offliner()->Cancel(cancel_callback()); | 292 offliner()->Cancel(cancel_callback()); |
| 279 PumpLoop(); | 293 PumpLoop(); |
| 280 | 294 |
| 281 // Subsequent save callback cause no crash. | 295 // Subsequent save callback cause no crash. |
| 282 model()->CompleteSavingAsArchiveCreationFailed(); | 296 model()->CompleteSavingAsArchiveCreationFailed(); |
| 283 PumpLoop(); | 297 PumpLoop(); |
| 284 EXPECT_TRUE(cancel_callback_called()); | 298 EXPECT_TRUE(cancel_callback_called()); |
| 285 EXPECT_FALSE(completion_callback_called()); | 299 EXPECT_FALSE(completion_callback_called()); |
| 286 EXPECT_FALSE(SaveInProgress()); | 300 EXPECT_FALSE(SaveInProgress()); |
| 287 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. | 301 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. |
| 288 } | 302 } |
| 289 | 303 |
| 290 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) { | 304 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) { |
| 291 base::Time creation_time = base::Time::Now(); | 305 base::Time creation_time = base::Time::Now(); |
| 292 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 306 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 293 kUserRequested); | 307 kUserRequested); |
| 294 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 308 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 309 progress_callback())); |
| 295 | 310 |
| 296 CompleteLoading(); | 311 CompleteLoading(); |
| 297 PumpLoop(); | 312 PumpLoop(); |
| 298 model()->CompleteSavingAsArchiveCreationFailed(); | 313 model()->CompleteSavingAsArchiveCreationFailed(); |
| 299 PumpLoop(); | 314 PumpLoop(); |
| 300 | 315 |
| 301 EXPECT_TRUE(completion_callback_called()); | 316 EXPECT_TRUE(completion_callback_called()); |
| 302 EXPECT_EQ(Offliner::RequestStatus::SAVE_FAILED, request_status()); | 317 EXPECT_EQ(Offliner::RequestStatus::SAVE_FAILED, request_status()); |
| 303 EXPECT_FALSE(offliner()->is_loading()); | 318 EXPECT_FALSE(offliner()->is_loading()); |
| 304 EXPECT_FALSE(SaveInProgress()); | 319 EXPECT_FALSE(SaveInProgress()); |
| 305 } | 320 } |
| 306 | 321 |
| 307 TEST_F(BackgroundLoaderOfflinerTest, LoadAndSaveSuccess) { | 322 TEST_F(BackgroundLoaderOfflinerTest, LoadAndSaveSuccess) { |
| 308 base::Time creation_time = base::Time::Now(); | 323 base::Time creation_time = base::Time::Now(); |
| 309 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 324 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 310 kUserRequested); | 325 kUserRequested); |
| 311 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 326 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 327 progress_callback())); |
| 312 | 328 |
| 313 CompleteLoading(); | 329 CompleteLoading(); |
| 314 PumpLoop(); | 330 PumpLoop(); |
| 315 model()->CompleteSavingAsSuccess(); | 331 model()->CompleteSavingAsSuccess(); |
| 316 PumpLoop(); | 332 PumpLoop(); |
| 317 | 333 |
| 318 EXPECT_TRUE(completion_callback_called()); | 334 EXPECT_TRUE(completion_callback_called()); |
| 319 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); | 335 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); |
| 320 EXPECT_FALSE(offliner()->is_loading()); | 336 EXPECT_FALSE(offliner()->is_loading()); |
| 321 EXPECT_FALSE(SaveInProgress()); | 337 EXPECT_FALSE(SaveInProgress()); |
| 322 } | 338 } |
| 323 | 339 |
| 324 TEST_F(BackgroundLoaderOfflinerTest, FailsOnInvalidURL) { | 340 TEST_F(BackgroundLoaderOfflinerTest, FailsOnInvalidURL) { |
| 325 base::Time creation_time = base::Time::Now(); | 341 base::Time creation_time = base::Time::Now(); |
| 326 SavePageRequest request(kRequestId, kFileUrl, kClientId, creation_time, | 342 SavePageRequest request(kRequestId, kFileUrl, kClientId, creation_time, |
| 327 kUserRequested); | 343 kUserRequested); |
| 328 EXPECT_FALSE(offliner()->LoadAndSave(request, callback())); | 344 EXPECT_FALSE(offliner()->LoadAndSave(request, completion_callback(), |
| 345 progress_callback())); |
| 329 } | 346 } |
| 330 | 347 |
| 331 TEST_F(BackgroundLoaderOfflinerTest, ReturnsOnRenderCrash) { | 348 TEST_F(BackgroundLoaderOfflinerTest, ReturnsOnRenderCrash) { |
| 332 base::Time creation_time = base::Time::Now(); | 349 base::Time creation_time = base::Time::Now(); |
| 333 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 350 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 334 kUserRequested); | 351 kUserRequested); |
| 335 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 352 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 353 progress_callback())); |
| 336 offliner()->RenderProcessGone( | 354 offliner()->RenderProcessGone( |
| 337 base::TerminationStatus::TERMINATION_STATUS_PROCESS_CRASHED); | 355 base::TerminationStatus::TERMINATION_STATUS_PROCESS_CRASHED); |
| 338 | 356 |
| 339 EXPECT_TRUE(completion_callback_called()); | 357 EXPECT_TRUE(completion_callback_called()); |
| 340 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_NEXT, request_status()); | 358 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_NEXT, request_status()); |
| 341 } | 359 } |
| 342 | 360 |
| 343 TEST_F(BackgroundLoaderOfflinerTest, ReturnsOnRenderKilled) { | 361 TEST_F(BackgroundLoaderOfflinerTest, ReturnsOnRenderKilled) { |
| 344 base::Time creation_time = base::Time::Now(); | 362 base::Time creation_time = base::Time::Now(); |
| 345 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 363 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 346 kUserRequested); | 364 kUserRequested); |
| 347 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 365 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 366 progress_callback())); |
| 348 offliner()->RenderProcessGone( | 367 offliner()->RenderProcessGone( |
| 349 base::TerminationStatus::TERMINATION_STATUS_PROCESS_WAS_KILLED); | 368 base::TerminationStatus::TERMINATION_STATUS_PROCESS_WAS_KILLED); |
| 350 | 369 |
| 351 EXPECT_TRUE(completion_callback_called()); | 370 EXPECT_TRUE(completion_callback_called()); |
| 352 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); | 371 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); |
| 353 } | 372 } |
| 354 | 373 |
| 355 TEST_F(BackgroundLoaderOfflinerTest, ReturnsOnWebContentsDestroyed) { | 374 TEST_F(BackgroundLoaderOfflinerTest, ReturnsOnWebContentsDestroyed) { |
| 356 base::Time creation_time = base::Time::Now(); | 375 base::Time creation_time = base::Time::Now(); |
| 357 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 376 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 358 kUserRequested); | 377 kUserRequested); |
| 359 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 378 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 379 progress_callback())); |
| 360 offliner()->WebContentsDestroyed(); | 380 offliner()->WebContentsDestroyed(); |
| 361 | 381 |
| 362 EXPECT_TRUE(completion_callback_called()); | 382 EXPECT_TRUE(completion_callback_called()); |
| 363 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); | 383 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); |
| 364 } | 384 } |
| 365 | 385 |
| 366 TEST_F(BackgroundLoaderOfflinerTest, FailsOnErrorPage) { | 386 TEST_F(BackgroundLoaderOfflinerTest, FailsOnErrorPage) { |
| 367 base::Time creation_time = base::Time::Now(); | 387 base::Time creation_time = base::Time::Now(); |
| 368 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 388 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 369 kUserRequested); | 389 kUserRequested); |
| 370 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 390 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 391 progress_callback())); |
| 371 | 392 |
| 372 // Create handle with net error code. | 393 // Create handle with net error code. |
| 373 // Called after calling LoadAndSave so we have web_contents to work with. | 394 // Called after calling LoadAndSave so we have web_contents to work with. |
| 374 std::unique_ptr<content::NavigationHandle> handle( | 395 std::unique_ptr<content::NavigationHandle> handle( |
| 375 content::NavigationHandle::CreateNavigationHandleForTesting( | 396 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 376 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, | 397 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, |
| 377 net::Error::ERR_NAME_NOT_RESOLVED)); | 398 net::Error::ERR_NAME_NOT_RESOLVED)); |
| 378 // Call DidFinishNavigation with handle that contains error. | 399 // Call DidFinishNavigation with handle that contains error. |
| 379 offliner()->DidFinishNavigation(handle.get()); | 400 offliner()->DidFinishNavigation(handle.get()); |
| 380 // NavigationHandle is always destroyed after finishing navigation. | 401 // NavigationHandle is always destroyed after finishing navigation. |
| 381 handle.reset(); | 402 handle.reset(); |
| 382 offliner()->DidStopLoading(); | 403 offliner()->DidStopLoading(); |
| 383 PumpLoop(); | 404 PumpLoop(); |
| 384 | 405 |
| 385 EXPECT_TRUE(completion_callback_called()); | 406 EXPECT_TRUE(completion_callback_called()); |
| 386 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); | 407 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); |
| 387 } | 408 } |
| 388 | 409 |
| 389 TEST_F(BackgroundLoaderOfflinerTest, NoNextOnInternetDisconnected) { | 410 TEST_F(BackgroundLoaderOfflinerTest, NoNextOnInternetDisconnected) { |
| 390 base::Time creation_time = base::Time::Now(); | 411 base::Time creation_time = base::Time::Now(); |
| 391 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 412 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 392 kUserRequested); | 413 kUserRequested); |
| 393 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 414 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 415 progress_callback())); |
| 394 | 416 |
| 395 // Create handle with net error code. | 417 // Create handle with net error code. |
| 396 // Called after calling LoadAndSave so we have web_contents to work with. | 418 // Called after calling LoadAndSave so we have web_contents to work with. |
| 397 std::unique_ptr<content::NavigationHandle> handle( | 419 std::unique_ptr<content::NavigationHandle> handle( |
| 398 content::NavigationHandle::CreateNavigationHandleForTesting( | 420 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 399 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, | 421 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, |
| 400 net::Error::ERR_INTERNET_DISCONNECTED)); | 422 net::Error::ERR_INTERNET_DISCONNECTED)); |
| 401 // Call DidFinishNavigation with handle that contains error. | 423 // Call DidFinishNavigation with handle that contains error. |
| 402 offliner()->DidFinishNavigation(handle.get()); | 424 offliner()->DidFinishNavigation(handle.get()); |
| 403 // NavigationHandle is always destroyed after finishing navigation. | 425 // NavigationHandle is always destroyed after finishing navigation. |
| 404 handle.reset(); | 426 handle.reset(); |
| 405 offliner()->DidStopLoading(); | 427 offliner()->DidStopLoading(); |
| 406 PumpLoop(); | 428 PumpLoop(); |
| 407 | 429 |
| 408 EXPECT_TRUE(completion_callback_called()); | 430 EXPECT_TRUE(completion_callback_called()); |
| 409 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_NEXT, request_status()); | 431 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_NEXT, request_status()); |
| 410 } | 432 } |
| 411 | 433 |
| 412 TEST_F(BackgroundLoaderOfflinerTest, OnlySavesOnceOnMultipleLoads) { | 434 TEST_F(BackgroundLoaderOfflinerTest, OnlySavesOnceOnMultipleLoads) { |
| 413 base::Time creation_time = base::Time::Now(); | 435 base::Time creation_time = base::Time::Now(); |
| 414 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 436 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 415 kUserRequested); | 437 kUserRequested); |
| 416 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 438 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 439 progress_callback())); |
| 417 // First load | 440 // First load |
| 418 CompleteLoading(); | 441 CompleteLoading(); |
| 419 // Second load | 442 // Second load |
| 420 offliner()->DidStopLoading(); | 443 offliner()->DidStopLoading(); |
| 421 PumpLoop(); | 444 PumpLoop(); |
| 422 model()->CompleteSavingAsSuccess(); | 445 model()->CompleteSavingAsSuccess(); |
| 423 PumpLoop(); | 446 PumpLoop(); |
| 424 | 447 |
| 425 EXPECT_TRUE(completion_callback_called()); | 448 EXPECT_TRUE(completion_callback_called()); |
| 426 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); | 449 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); |
| 427 EXPECT_FALSE(offliner()->is_loading()); | 450 EXPECT_FALSE(offliner()->is_loading()); |
| 428 EXPECT_FALSE(SaveInProgress()); | 451 EXPECT_FALSE(SaveInProgress()); |
| 429 } | 452 } |
| 430 | 453 |
| 431 } // namespace offline_pages | 454 } // namespace offline_pages |
| OLD | NEW |