| 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 Profile* profile() { return &profile_; } | 137 Profile* profile() { return &profile_; } |
| 134 bool completion_callback_called() { return completion_callback_called_; } | 138 bool completion_callback_called() { return completion_callback_called_; } |
| 135 Offliner::RequestStatus request_status() { return request_status_; } | 139 Offliner::RequestStatus request_status() { return request_status_; } |
| 136 bool SaveInProgress() const { return model_->mock_saving(); } | 140 bool SaveInProgress() const { return model_->mock_saving(); } |
| 137 MockOfflinePageModel* model() const { return model_; } | 141 MockOfflinePageModel* model() const { return model_; } |
| 138 const base::HistogramTester& histograms() const { return histogram_tester_; } | 142 const base::HistogramTester& histograms() const { return histogram_tester_; } |
| 139 | 143 |
| 140 void CompleteLoading() { | 144 void CompleteLoading() { |
| 141 // For some reason, setting loading to True will call DidStopLoading | 145 // For some reason, setting loading to True will call DidStopLoading |
| 142 // on the observers. | 146 // on the observers. |
| 143 offliner()->web_contents_tester()->TestSetIsLoading(true); | 147 offliner()->web_contents_tester()->TestSetIsLoading(true); |
| 144 } | 148 } |
| 145 | 149 |
| 146 void PumpLoop() { base::RunLoop().RunUntilIdle(); } | 150 void PumpLoop() { base::RunLoop().RunUntilIdle(); } |
| 147 | 151 |
| 148 private: | 152 private: |
| 149 void OnCompletion(const SavePageRequest& request, | 153 void OnCompletion(const SavePageRequest& request, |
| 150 Offliner::RequestStatus status); | 154 Offliner::RequestStatus status); |
| 155 void OnProgress(const SavePageRequest& request, int64_t bytes); |
| 151 content::TestBrowserThreadBundle thread_bundle_; | 156 content::TestBrowserThreadBundle thread_bundle_; |
| 152 TestingProfile profile_; | 157 TestingProfile profile_; |
| 153 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; | 158 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; |
| 154 MockOfflinePageModel* model_; | 159 MockOfflinePageModel* model_; |
| 155 bool completion_callback_called_; | 160 bool completion_callback_called_; |
| 156 Offliner::RequestStatus request_status_; | 161 Offliner::RequestStatus request_status_; |
| 157 base::HistogramTester histogram_tester_; | 162 base::HistogramTester histogram_tester_; |
| 158 | 163 |
| 159 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest); | 164 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest); |
| 160 }; | 165 }; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 173 } | 178 } |
| 174 | 179 |
| 175 void BackgroundLoaderOfflinerTest::OnCompletion( | 180 void BackgroundLoaderOfflinerTest::OnCompletion( |
| 176 const SavePageRequest& request, | 181 const SavePageRequest& request, |
| 177 Offliner::RequestStatus status) { | 182 Offliner::RequestStatus status) { |
| 178 DCHECK(!completion_callback_called_); // Expect 1 callback per request. | 183 DCHECK(!completion_callback_called_); // Expect 1 callback per request. |
| 179 completion_callback_called_ = true; | 184 completion_callback_called_ = true; |
| 180 request_status_ = status; | 185 request_status_ = status; |
| 181 } | 186 } |
| 182 | 187 |
| 188 void BackgroundLoaderOfflinerTest::OnProgress(const SavePageRequest& request, |
| 189 int64_t bytes) {} |
| 190 |
| 183 TEST_F(BackgroundLoaderOfflinerTest, | 191 TEST_F(BackgroundLoaderOfflinerTest, |
| 184 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) { | 192 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) { |
| 185 base::Time creation_time = base::Time::Now(); | 193 base::Time creation_time = base::Time::Now(); |
| 186 ClientId custom_tabs_client_id("custom_tabs", "88"); | 194 ClientId custom_tabs_client_id("custom_tabs", "88"); |
| 187 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, | 195 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, |
| 188 creation_time, kUserRequested); | 196 creation_time, kUserRequested); |
| 189 | 197 |
| 190 profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); | 198 profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); |
| 191 EXPECT_FALSE(offliner()->LoadAndSave(request, callback())); | 199 EXPECT_FALSE(offliner()->LoadAndSave(request, completion_callback(), |
| 200 progress_callback())); |
| 192 histograms().ExpectBucketCount( | 201 histograms().ExpectBucketCount( |
| 193 "OfflinePages.Background.CctApiDisableStatus", | 202 "OfflinePages.Background.CctApiDisableStatus", |
| 194 static_cast<int>(OfflinePagesCctApiPrerenderAllowedStatus:: | 203 static_cast<int>(OfflinePagesCctApiPrerenderAllowedStatus:: |
| 195 THIRD_PARTY_COOKIES_DISABLED), | 204 THIRD_PARTY_COOKIES_DISABLED), |
| 196 1); | 205 1); |
| 197 histograms().ExpectBucketCount("OfflinePages.Background.CctApiDisableStatus", | 206 histograms().ExpectBucketCount("OfflinePages.Background.CctApiDisableStatus", |
| 198 0 /* PRERENDER_ALLOWED */, 0); | 207 0 /* PRERENDER_ALLOWED */, 0); |
| 199 } | 208 } |
| 200 | 209 |
| 201 TEST_F(BackgroundLoaderOfflinerTest, | 210 TEST_F(BackgroundLoaderOfflinerTest, |
| 202 LoadAndSaveNetworkPredictionDisabledForCustomTabs) { | 211 LoadAndSaveNetworkPredictionDisabledForCustomTabs) { |
| 203 base::Time creation_time = base::Time::Now(); | 212 base::Time creation_time = base::Time::Now(); |
| 204 ClientId custom_tabs_client_id("custom_tabs", "88"); | 213 ClientId custom_tabs_client_id("custom_tabs", "88"); |
| 205 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, | 214 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, |
| 206 creation_time, kUserRequested); | 215 creation_time, kUserRequested); |
| 207 | 216 |
| 208 profile()->GetPrefs()->SetInteger( | 217 profile()->GetPrefs()->SetInteger( |
| 209 prefs::kNetworkPredictionOptions, | 218 prefs::kNetworkPredictionOptions, |
| 210 chrome_browser_net::NETWORK_PREDICTION_NEVER); | 219 chrome_browser_net::NETWORK_PREDICTION_NEVER); |
| 211 EXPECT_FALSE(offliner()->LoadAndSave(request, callback())); | 220 EXPECT_FALSE(offliner()->LoadAndSave(request, completion_callback(), |
| 221 progress_callback())); |
| 212 histograms().ExpectBucketCount( | 222 histograms().ExpectBucketCount( |
| 213 "OfflinePages.Background.CctApiDisableStatus", | 223 "OfflinePages.Background.CctApiDisableStatus", |
| 214 static_cast<int>(OfflinePagesCctApiPrerenderAllowedStatus:: | 224 static_cast<int>(OfflinePagesCctApiPrerenderAllowedStatus:: |
| 215 NETWORK_PREDICTION_DISABLED), | 225 NETWORK_PREDICTION_DISABLED), |
| 216 1); | 226 1); |
| 217 histograms().ExpectBucketCount( | 227 histograms().ExpectBucketCount( |
| 218 "OfflinePages.Background.CctApiDisableStatus", | 228 "OfflinePages.Background.CctApiDisableStatus", |
| 219 static_cast<int>( | 229 static_cast<int>( |
| 220 OfflinePagesCctApiPrerenderAllowedStatus::PRERENDER_ALLOWED), | 230 OfflinePagesCctApiPrerenderAllowedStatus::PRERENDER_ALLOWED), |
| 221 0); | 231 0); |
| 222 } | 232 } |
| 223 | 233 |
| 224 TEST_F(BackgroundLoaderOfflinerTest, LoadAndSaveStartsLoading) { | 234 TEST_F(BackgroundLoaderOfflinerTest, LoadAndSaveStartsLoading) { |
| 225 base::Time creation_time = base::Time::Now(); | 235 base::Time creation_time = base::Time::Now(); |
| 226 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 236 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 227 kUserRequested); | 237 kUserRequested); |
| 228 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 238 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 239 progress_callback())); |
| 229 EXPECT_TRUE(offliner()->is_loading()); | 240 EXPECT_TRUE(offliner()->is_loading()); |
| 230 EXPECT_FALSE(SaveInProgress()); | 241 EXPECT_FALSE(SaveInProgress()); |
| 231 EXPECT_FALSE(completion_callback_called()); | 242 EXPECT_FALSE(completion_callback_called()); |
| 232 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); | 243 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); |
| 233 } | 244 } |
| 234 | 245 |
| 235 TEST_F(BackgroundLoaderOfflinerTest, CompleteLoadingInitiatesSave) { | 246 TEST_F(BackgroundLoaderOfflinerTest, CompleteLoadingInitiatesSave) { |
| 236 base::Time creation_time = base::Time::Now(); | 247 base::Time creation_time = base::Time::Now(); |
| 237 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 248 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 238 kUserRequested); | 249 kUserRequested); |
| 239 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 250 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 251 progress_callback())); |
| 240 CompleteLoading(); | 252 CompleteLoading(); |
| 241 PumpLoop(); | 253 PumpLoop(); |
| 242 EXPECT_FALSE(completion_callback_called()); | 254 EXPECT_FALSE(completion_callback_called()); |
| 243 EXPECT_TRUE(SaveInProgress()); | 255 EXPECT_TRUE(SaveInProgress()); |
| 244 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); | 256 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); |
| 245 } | 257 } |
| 246 | 258 |
| 247 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoading) { | 259 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoading) { |
| 248 base::Time creation_time = base::Time::Now(); | 260 base::Time creation_time = base::Time::Now(); |
| 249 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 261 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 250 kUserRequested); | 262 kUserRequested); |
| 251 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 263 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 264 progress_callback())); |
| 252 offliner()->Cancel(); | 265 offliner()->Cancel(); |
| 253 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. | 266 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. |
| 254 } | 267 } |
| 255 | 268 |
| 256 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) { | 269 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) { |
| 257 base::Time creation_time = base::Time::Now(); | 270 base::Time creation_time = base::Time::Now(); |
| 258 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 271 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 259 kUserRequested); | 272 kUserRequested); |
| 260 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 273 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 274 progress_callback())); |
| 261 CompleteLoading(); | 275 CompleteLoading(); |
| 262 PumpLoop(); | 276 PumpLoop(); |
| 263 offliner()->Cancel(); | 277 offliner()->Cancel(); |
| 264 | 278 |
| 265 // Subsequent save callback cause no crash. | 279 // Subsequent save callback cause no crash. |
| 266 model()->CompleteSavingAsArchiveCreationFailed(); | 280 model()->CompleteSavingAsArchiveCreationFailed(); |
| 267 PumpLoop(); | 281 PumpLoop(); |
| 268 EXPECT_FALSE(completion_callback_called()); | 282 EXPECT_FALSE(completion_callback_called()); |
| 269 EXPECT_FALSE(SaveInProgress()); | 283 EXPECT_FALSE(SaveInProgress()); |
| 270 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. | 284 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. |
| 271 } | 285 } |
| 272 | 286 |
| 273 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) { | 287 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) { |
| 274 base::Time creation_time = base::Time::Now(); | 288 base::Time creation_time = base::Time::Now(); |
| 275 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 289 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 276 kUserRequested); | 290 kUserRequested); |
| 277 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 291 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 292 progress_callback())); |
| 278 | 293 |
| 279 CompleteLoading(); | 294 CompleteLoading(); |
| 280 PumpLoop(); | 295 PumpLoop(); |
| 281 model()->CompleteSavingAsArchiveCreationFailed(); | 296 model()->CompleteSavingAsArchiveCreationFailed(); |
| 282 PumpLoop(); | 297 PumpLoop(); |
| 283 | 298 |
| 284 EXPECT_TRUE(completion_callback_called()); | 299 EXPECT_TRUE(completion_callback_called()); |
| 285 EXPECT_EQ(Offliner::RequestStatus::SAVE_FAILED, request_status()); | 300 EXPECT_EQ(Offliner::RequestStatus::SAVE_FAILED, request_status()); |
| 286 EXPECT_FALSE(offliner()->is_loading()); | 301 EXPECT_FALSE(offliner()->is_loading()); |
| 287 EXPECT_FALSE(SaveInProgress()); | 302 EXPECT_FALSE(SaveInProgress()); |
| 288 } | 303 } |
| 289 | 304 |
| 290 TEST_F(BackgroundLoaderOfflinerTest, LoadAndSaveSuccess) { | 305 TEST_F(BackgroundLoaderOfflinerTest, LoadAndSaveSuccess) { |
| 291 base::Time creation_time = base::Time::Now(); | 306 base::Time creation_time = base::Time::Now(); |
| 292 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 307 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 293 kUserRequested); | 308 kUserRequested); |
| 294 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 309 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 310 progress_callback())); |
| 295 | 311 |
| 296 CompleteLoading(); | 312 CompleteLoading(); |
| 297 PumpLoop(); | 313 PumpLoop(); |
| 298 model()->CompleteSavingAsSuccess(); | 314 model()->CompleteSavingAsSuccess(); |
| 299 PumpLoop(); | 315 PumpLoop(); |
| 300 | 316 |
| 301 EXPECT_TRUE(completion_callback_called()); | 317 EXPECT_TRUE(completion_callback_called()); |
| 302 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); | 318 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); |
| 303 EXPECT_FALSE(offliner()->is_loading()); | 319 EXPECT_FALSE(offliner()->is_loading()); |
| 304 EXPECT_FALSE(SaveInProgress()); | 320 EXPECT_FALSE(SaveInProgress()); |
| 305 } | 321 } |
| 306 | 322 |
| 307 TEST_F(BackgroundLoaderOfflinerTest, FailsOnInvalidURL) { | 323 TEST_F(BackgroundLoaderOfflinerTest, FailsOnInvalidURL) { |
| 308 base::Time creation_time = base::Time::Now(); | 324 base::Time creation_time = base::Time::Now(); |
| 309 SavePageRequest request(kRequestId, kFileUrl, kClientId, creation_time, | 325 SavePageRequest request(kRequestId, kFileUrl, kClientId, creation_time, |
| 310 kUserRequested); | 326 kUserRequested); |
| 311 EXPECT_FALSE(offliner()->LoadAndSave(request, callback())); | 327 EXPECT_FALSE(offliner()->LoadAndSave(request, completion_callback(), |
| 328 progress_callback())); |
| 312 } | 329 } |
| 313 | 330 |
| 314 TEST_F(BackgroundLoaderOfflinerTest, ReturnsOnRenderCrash) { | 331 TEST_F(BackgroundLoaderOfflinerTest, ReturnsOnRenderCrash) { |
| 315 base::Time creation_time = base::Time::Now(); | 332 base::Time creation_time = base::Time::Now(); |
| 316 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 333 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 317 kUserRequested); | 334 kUserRequested); |
| 318 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 335 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 336 progress_callback())); |
| 319 offliner()->RenderProcessGone( | 337 offliner()->RenderProcessGone( |
| 320 base::TerminationStatus::TERMINATION_STATUS_PROCESS_CRASHED); | 338 base::TerminationStatus::TERMINATION_STATUS_PROCESS_CRASHED); |
| 321 | 339 |
| 322 EXPECT_TRUE(completion_callback_called()); | 340 EXPECT_TRUE(completion_callback_called()); |
| 323 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_NEXT, request_status()); | 341 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_NEXT, request_status()); |
| 324 } | 342 } |
| 325 | 343 |
| 326 TEST_F(BackgroundLoaderOfflinerTest, ReturnsOnRenderKilled) { | 344 TEST_F(BackgroundLoaderOfflinerTest, ReturnsOnRenderKilled) { |
| 327 base::Time creation_time = base::Time::Now(); | 345 base::Time creation_time = base::Time::Now(); |
| 328 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 346 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 329 kUserRequested); | 347 kUserRequested); |
| 330 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 348 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 349 progress_callback())); |
| 331 offliner()->RenderProcessGone( | 350 offliner()->RenderProcessGone( |
| 332 base::TerminationStatus::TERMINATION_STATUS_PROCESS_WAS_KILLED); | 351 base::TerminationStatus::TERMINATION_STATUS_PROCESS_WAS_KILLED); |
| 333 | 352 |
| 334 EXPECT_TRUE(completion_callback_called()); | 353 EXPECT_TRUE(completion_callback_called()); |
| 335 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); | 354 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); |
| 336 } | 355 } |
| 337 | 356 |
| 338 TEST_F(BackgroundLoaderOfflinerTest, ReturnsOnWebContentsDestroyed) { | 357 TEST_F(BackgroundLoaderOfflinerTest, ReturnsOnWebContentsDestroyed) { |
| 339 base::Time creation_time = base::Time::Now(); | 358 base::Time creation_time = base::Time::Now(); |
| 340 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 359 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 341 kUserRequested); | 360 kUserRequested); |
| 342 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 361 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 362 progress_callback())); |
| 343 offliner()->WebContentsDestroyed(); | 363 offliner()->WebContentsDestroyed(); |
| 344 | 364 |
| 345 EXPECT_TRUE(completion_callback_called()); | 365 EXPECT_TRUE(completion_callback_called()); |
| 346 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); | 366 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); |
| 347 } | 367 } |
| 348 | 368 |
| 349 TEST_F(BackgroundLoaderOfflinerTest, FailsOnErrorPage) { | 369 TEST_F(BackgroundLoaderOfflinerTest, FailsOnErrorPage) { |
| 350 base::Time creation_time = base::Time::Now(); | 370 base::Time creation_time = base::Time::Now(); |
| 351 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 371 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 352 kUserRequested); | 372 kUserRequested); |
| 353 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 373 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 374 progress_callback())); |
| 354 | 375 |
| 355 // Create handle with net error code. | 376 // Create handle with net error code. |
| 356 // Called after calling LoadAndSave so we have web_contents to work with. | 377 // Called after calling LoadAndSave so we have web_contents to work with. |
| 357 std::unique_ptr<content::NavigationHandle> handle( | 378 std::unique_ptr<content::NavigationHandle> handle( |
| 358 content::NavigationHandle::CreateNavigationHandleForTesting( | 379 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 359 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, | 380 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, |
| 360 net::Error::ERR_NAME_NOT_RESOLVED)); | 381 net::Error::ERR_NAME_NOT_RESOLVED)); |
| 361 // Call DidFinishNavigation with handle that contains error. | 382 // Call DidFinishNavigation with handle that contains error. |
| 362 offliner()->DidFinishNavigation(handle.get()); | 383 offliner()->DidFinishNavigation(handle.get()); |
| 363 // NavigationHandle is always destroyed after finishing navigation. | 384 // NavigationHandle is always destroyed after finishing navigation. |
| 364 handle.reset(); | 385 handle.reset(); |
| 365 offliner()->DidStopLoading(); | 386 offliner()->DidStopLoading(); |
| 366 PumpLoop(); | 387 PumpLoop(); |
| 367 | 388 |
| 368 EXPECT_TRUE(completion_callback_called()); | 389 EXPECT_TRUE(completion_callback_called()); |
| 369 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); | 390 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); |
| 370 } | 391 } |
| 371 | 392 |
| 372 TEST_F(BackgroundLoaderOfflinerTest, OnlySavesOnceOnMultipleLoads) { | 393 TEST_F(BackgroundLoaderOfflinerTest, OnlySavesOnceOnMultipleLoads) { |
| 373 base::Time creation_time = base::Time::Now(); | 394 base::Time creation_time = base::Time::Now(); |
| 374 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 395 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 375 kUserRequested); | 396 kUserRequested); |
| 376 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 397 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 398 progress_callback())); |
| 377 // First load | 399 // First load |
| 378 CompleteLoading(); | 400 CompleteLoading(); |
| 379 // Second load | 401 // Second load |
| 380 offliner()->DidStopLoading(); | 402 offliner()->DidStopLoading(); |
| 381 PumpLoop(); | 403 PumpLoop(); |
| 382 model()->CompleteSavingAsSuccess(); | 404 model()->CompleteSavingAsSuccess(); |
| 383 PumpLoop(); | 405 PumpLoop(); |
| 384 | 406 |
| 385 EXPECT_TRUE(completion_callback_called()); | 407 EXPECT_TRUE(completion_callback_called()); |
| 386 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); | 408 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); |
| 387 EXPECT_FALSE(offliner()->is_loading()); | 409 EXPECT_FALSE(offliner()->is_loading()); |
| 388 EXPECT_FALSE(SaveInProgress()); | 410 EXPECT_FALSE(SaveInProgress()); |
| 389 } | 411 } |
| 390 | 412 |
| 391 } // namespace offline_pages | 413 } // namespace offline_pages |
| OLD | NEW |