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

Side by Side Diff: content/browser/background_sync/background_sync_manager_unittest.cc

Issue 1549113002: Switch to standard integer types in content/browser/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 12 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "content/browser/background_sync/background_sync_manager.h" 5 #include "content/browser/background_sync/background_sync_manager.h"
6 6
7 #include <stdint.h>
8
7 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
8 #include "base/location.h" 10 #include "base/location.h"
9 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/macros.h"
10 #include "base/metrics/field_trial.h" 13 #include "base/metrics/field_trial.h"
11 #include "base/power_monitor/power_monitor.h" 14 #include "base/power_monitor/power_monitor.h"
12 #include "base/power_monitor/power_monitor_source.h" 15 #include "base/power_monitor/power_monitor_source.h"
13 #include "base/run_loop.h" 16 #include "base/run_loop.h"
14 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
15 #include "base/test/mock_entropy_provider.h" 18 #include "base/test/mock_entropy_provider.h"
16 #include "base/test/simple_test_clock.h" 19 #include "base/test/simple_test_clock.h"
17 #include "base/thread_task_runner_handle.h" 20 #include "base/thread_task_runner_handle.h"
18 #include "content/browser/background_sync/background_sync_network_observer.h" 21 #include "content/browser/background_sync/background_sync_network_observer.h"
19 #include "content/browser/background_sync/background_sync_registration_handle.h" 22 #include "content/browser/background_sync/background_sync_registration_handle.h"
(...skipping 15 matching lines...) Expand all
35 namespace content { 38 namespace content {
36 39
37 namespace { 40 namespace {
38 41
39 const char kPattern1[] = "https://example.com/a"; 42 const char kPattern1[] = "https://example.com/a";
40 const char kPattern2[] = "https://example.com/b"; 43 const char kPattern2[] = "https://example.com/b";
41 const char kScript1[] = "https://example.com/a/script.js"; 44 const char kScript1[] = "https://example.com/a/script.js";
42 const char kScript2[] = "https://example.com/b/script.js"; 45 const char kScript2[] = "https://example.com/b/script.js";
43 46
44 void RegisterServiceWorkerCallback(bool* called, 47 void RegisterServiceWorkerCallback(bool* called,
45 int64* store_registration_id, 48 int64_t* store_registration_id,
46 ServiceWorkerStatusCode status, 49 ServiceWorkerStatusCode status,
47 const std::string& status_message, 50 const std::string& status_message,
48 int64 registration_id) { 51 int64_t registration_id) {
49 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); 52 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
50 *called = true; 53 *called = true;
51 *store_registration_id = registration_id; 54 *store_registration_id = registration_id;
52 } 55 }
53 56
54 void FindServiceWorkerRegistrationCallback( 57 void FindServiceWorkerRegistrationCallback(
55 scoped_refptr<ServiceWorkerRegistration>* out_registration, 58 scoped_refptr<ServiceWorkerRegistration>* out_registration,
56 ServiceWorkerStatusCode status, 59 ServiceWorkerStatusCode status,
57 const scoped_refptr<ServiceWorkerRegistration>& registration) { 60 const scoped_refptr<ServiceWorkerRegistration>& registration) {
58 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); 61 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 const ServiceWorkerVersion::StatusCallback&)>; 166 const ServiceWorkerVersion::StatusCallback&)>;
164 167
165 explicit TestBackgroundSyncManager( 168 explicit TestBackgroundSyncManager(
166 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) 169 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context)
167 : BackgroundSyncManager(service_worker_context) { 170 : BackgroundSyncManager(service_worker_context) {
168 } 171 }
169 172
170 void DoInit() { Init(); } 173 void DoInit() { Init(); }
171 174
172 void StoreDataInBackendContinue( 175 void StoreDataInBackendContinue(
173 int64 sw_registration_id, 176 int64_t sw_registration_id,
174 const GURL& origin, 177 const GURL& origin,
175 const std::string& key, 178 const std::string& key,
176 const std::string& data, 179 const std::string& data,
177 const ServiceWorkerStorage::StatusCallback& callback) { 180 const ServiceWorkerStorage::StatusCallback& callback) {
178 BackgroundSyncManager::StoreDataInBackend(sw_registration_id, origin, key, 181 BackgroundSyncManager::StoreDataInBackend(sw_registration_id, origin, key,
179 data, callback); 182 data, callback);
180 } 183 }
181 184
182 void GetDataFromBackendContinue( 185 void GetDataFromBackendContinue(
183 const std::string& key, 186 const std::string& key,
(...skipping 26 matching lines...) Expand all
210 void set_has_main_frame_provider_host(bool value) { 213 void set_has_main_frame_provider_host(bool value) {
211 has_main_frame_provider_host_ = value; 214 has_main_frame_provider_host_ = value;
212 } 215 }
213 216
214 const BackgroundSyncParameters* background_sync_parameters() const { 217 const BackgroundSyncParameters* background_sync_parameters() const {
215 return parameters_.get(); 218 return parameters_.get();
216 } 219 }
217 220
218 protected: 221 protected:
219 void StoreDataInBackend( 222 void StoreDataInBackend(
220 int64 sw_registration_id, 223 int64_t sw_registration_id,
221 const GURL& origin, 224 const GURL& origin,
222 const std::string& key, 225 const std::string& key,
223 const std::string& data, 226 const std::string& data,
224 const ServiceWorkerStorage::StatusCallback& callback) override { 227 const ServiceWorkerStorage::StatusCallback& callback) override {
225 EXPECT_TRUE(continuation_.is_null()); 228 EXPECT_TRUE(continuation_.is_null());
226 if (corrupt_backend_) { 229 if (corrupt_backend_) {
227 base::ThreadTaskRunnerHandle::Get()->PostTask( 230 base::ThreadTaskRunnerHandle::Get()->PostTask(
228 FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); 231 FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED));
229 return; 232 return;
230 } 233 }
231 continuation_ = 234 continuation_ =
232 base::Bind(&TestBackgroundSyncManager::StoreDataInBackendContinue, 235 base::Bind(&TestBackgroundSyncManager::StoreDataInBackendContinue,
233 base::Unretained(this), sw_registration_id, origin, key, 236 base::Unretained(this), sw_registration_id, origin, key,
234 data, callback); 237 data, callback);
235 if (delay_backend_) 238 if (delay_backend_)
236 return; 239 return;
237 240
238 Continue(); 241 Continue();
239 } 242 }
240 243
241 void GetDataFromBackend( 244 void GetDataFromBackend(
242 const std::string& key, 245 const std::string& key,
243 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& 246 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
244 callback) override { 247 callback) override {
245 EXPECT_TRUE(continuation_.is_null()); 248 EXPECT_TRUE(continuation_.is_null());
246 if (corrupt_backend_) { 249 if (corrupt_backend_) {
247 base::ThreadTaskRunnerHandle::Get()->PostTask( 250 base::ThreadTaskRunnerHandle::Get()->PostTask(
248 FROM_HERE, 251 FROM_HERE,
249 base::Bind(callback, std::vector<std::pair<int64, std::string>>(), 252 base::Bind(callback, std::vector<std::pair<int64_t, std::string>>(),
250 SERVICE_WORKER_ERROR_FAILED)); 253 SERVICE_WORKER_ERROR_FAILED));
251 return; 254 return;
252 } 255 }
253 continuation_ = 256 continuation_ =
254 base::Bind(&TestBackgroundSyncManager::GetDataFromBackendContinue, 257 base::Bind(&TestBackgroundSyncManager::GetDataFromBackendContinue,
255 base::Unretained(this), key, callback); 258 base::Unretained(this), key, callback);
256 if (delay_backend_) 259 if (delay_backend_)
257 return; 260 return;
258 261
259 Continue(); 262 Continue();
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 background_sync_manager_.reset(); 478 background_sync_manager_.reset();
476 test_background_sync_manager_ = nullptr; 479 test_background_sync_manager_ = nullptr;
477 test_clock_ = nullptr; 480 test_clock_ = nullptr;
478 } 481 }
479 482
480 bool Register(const BackgroundSyncRegistrationOptions& sync_options) { 483 bool Register(const BackgroundSyncRegistrationOptions& sync_options) {
481 return RegisterWithServiceWorkerId(sw_registration_id_1_, sync_options); 484 return RegisterWithServiceWorkerId(sw_registration_id_1_, sync_options);
482 } 485 }
483 486
484 bool RegisterWithServiceWorkerId( 487 bool RegisterWithServiceWorkerId(
485 int64 sw_registration_id, 488 int64_t sw_registration_id,
486 const BackgroundSyncRegistrationOptions& options) { 489 const BackgroundSyncRegistrationOptions& options) {
487 bool was_called = false; 490 bool was_called = false;
488 background_sync_manager_->Register( 491 background_sync_manager_->Register(
489 sw_registration_id, options, true /* requested_from_service_worker */, 492 sw_registration_id, options, true /* requested_from_service_worker */,
490 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, 493 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
491 base::Unretained(this), &was_called)); 494 base::Unretained(this), &was_called));
492 base::RunLoop().RunUntilIdle(); 495 base::RunLoop().RunUntilIdle();
493 EXPECT_TRUE(was_called); 496 EXPECT_TRUE(was_called);
494 return callback_status_ == BACKGROUND_SYNC_STATUS_OK; 497 return callback_status_ == BACKGROUND_SYNC_STATUS_OK;
495 } 498 }
496 499
497 bool RegisterFromDocumentWithServiceWorkerId( 500 bool RegisterFromDocumentWithServiceWorkerId(
498 int64 sw_registration_id, 501 int64_t sw_registration_id,
499 const BackgroundSyncRegistrationOptions& options) { 502 const BackgroundSyncRegistrationOptions& options) {
500 bool was_called = false; 503 bool was_called = false;
501 background_sync_manager_->Register( 504 background_sync_manager_->Register(
502 sw_registration_id, options, false /* requested_from_service_worker */, 505 sw_registration_id, options, false /* requested_from_service_worker */,
503 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, 506 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
504 base::Unretained(this), &was_called)); 507 base::Unretained(this), &was_called));
505 base::RunLoop().RunUntilIdle(); 508 base::RunLoop().RunUntilIdle();
506 EXPECT_TRUE(was_called); 509 EXPECT_TRUE(was_called);
507 return callback_status_ == BACKGROUND_SYNC_STATUS_OK; 510 return callback_status_ == BACKGROUND_SYNC_STATUS_OK;
508 } 511 }
509 512
510 bool Unregister(BackgroundSyncRegistrationHandle* registration_handle) { 513 bool Unregister(BackgroundSyncRegistrationHandle* registration_handle) {
511 return UnregisterWithServiceWorkerId(sw_registration_id_1_, 514 return UnregisterWithServiceWorkerId(sw_registration_id_1_,
512 registration_handle); 515 registration_handle);
513 } 516 }
514 517
515 bool UnregisterWithServiceWorkerId( 518 bool UnregisterWithServiceWorkerId(
516 int64 sw_registration_id, 519 int64_t sw_registration_id,
517 BackgroundSyncRegistrationHandle* registration_handle) { 520 BackgroundSyncRegistrationHandle* registration_handle) {
518 bool was_called = false; 521 bool was_called = false;
519 registration_handle->Unregister( 522 registration_handle->Unregister(
520 sw_registration_id, 523 sw_registration_id,
521 base::Bind(&BackgroundSyncManagerTest::StatusCallback, 524 base::Bind(&BackgroundSyncManagerTest::StatusCallback,
522 base::Unretained(this), &was_called)); 525 base::Unretained(this), &was_called));
523 base::RunLoop().RunUntilIdle(); 526 base::RunLoop().RunUntilIdle();
524 EXPECT_TRUE(was_called); 527 EXPECT_TRUE(was_called);
525 return callback_status_ == BACKGROUND_SYNC_STATUS_OK; 528 return callback_status_ == BACKGROUND_SYNC_STATUS_OK;
526 } 529 }
(...skipping 21 matching lines...) Expand all
548 return callback_finished_state_; 551 return callback_finished_state_;
549 } 552 }
550 553
551 bool GetRegistration( 554 bool GetRegistration(
552 const BackgroundSyncRegistrationOptions& registration_options) { 555 const BackgroundSyncRegistrationOptions& registration_options) {
553 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_, 556 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_,
554 registration_options); 557 registration_options);
555 } 558 }
556 559
557 bool GetRegistrationWithServiceWorkerId( 560 bool GetRegistrationWithServiceWorkerId(
558 int64 sw_registration_id, 561 int64_t sw_registration_id,
559 const BackgroundSyncRegistrationOptions& registration_options) { 562 const BackgroundSyncRegistrationOptions& registration_options) {
560 bool was_called = false; 563 bool was_called = false;
561 background_sync_manager_->GetRegistration( 564 background_sync_manager_->GetRegistration(
562 sw_registration_id, registration_options.tag, 565 sw_registration_id, registration_options.tag,
563 registration_options.periodicity, 566 registration_options.periodicity,
564 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, 567 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
565 base::Unretained(this), &was_called)); 568 base::Unretained(this), &was_called));
566 base::RunLoop().RunUntilIdle(); 569 base::RunLoop().RunUntilIdle();
567 EXPECT_TRUE(was_called); 570 EXPECT_TRUE(was_called);
568 571
569 if (callback_status_ == BACKGROUND_SYNC_STATUS_OK) { 572 if (callback_status_ == BACKGROUND_SYNC_STATUS_OK) {
570 EXPECT_STREQ(registration_options.tag.c_str(), 573 EXPECT_STREQ(registration_options.tag.c_str(),
571 callback_registration_handle_->options()->tag.c_str()); 574 callback_registration_handle_->options()->tag.c_str());
572 } 575 }
573 576
574 return callback_status_ == BACKGROUND_SYNC_STATUS_OK; 577 return callback_status_ == BACKGROUND_SYNC_STATUS_OK;
575 } 578 }
576 579
577 bool GetRegistrations(SyncPeriodicity periodicity) { 580 bool GetRegistrations(SyncPeriodicity periodicity) {
578 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_, 581 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_,
579 periodicity); 582 periodicity);
580 } 583 }
581 584
582 bool GetRegistrationWithServiceWorkerId(int64 sw_registration_id, 585 bool GetRegistrationWithServiceWorkerId(int64_t sw_registration_id,
583 SyncPeriodicity periodicity) { 586 SyncPeriodicity periodicity) {
584 bool was_called = false; 587 bool was_called = false;
585 background_sync_manager_->GetRegistrations( 588 background_sync_manager_->GetRegistrations(
586 sw_registration_id, periodicity, 589 sw_registration_id, periodicity,
587 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationsCallback, 590 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationsCallback,
588 base::Unretained(this), &was_called)); 591 base::Unretained(this), &was_called));
589 base::RunLoop().RunUntilIdle(); 592 base::RunLoop().RunUntilIdle();
590 EXPECT_TRUE(was_called); 593 EXPECT_TRUE(was_called);
591 594
592 return callback_status_ == BACKGROUND_SYNC_STATUS_OK; 595 return callback_status_ == BACKGROUND_SYNC_STATUS_OK;
593 } 596 }
594 597
595 void StorageRegistrationCallback(ServiceWorkerStatusCode result) { 598 void StorageRegistrationCallback(ServiceWorkerStatusCode result) {
596 callback_sw_status_code_ = result; 599 callback_sw_status_code_ = result;
597 } 600 }
598 601
599 void UnregisterServiceWorker(uint64 sw_registration_id) { 602 void UnregisterServiceWorker(uint64_t sw_registration_id) {
600 bool called = false; 603 bool called = false;
601 helper_->context()->UnregisterServiceWorker( 604 helper_->context()->UnregisterServiceWorker(
602 PatternForSWId(sw_registration_id), 605 PatternForSWId(sw_registration_id),
603 base::Bind(&UnregisterServiceWorkerCallback, &called)); 606 base::Bind(&UnregisterServiceWorkerCallback, &called));
604 base::RunLoop().RunUntilIdle(); 607 base::RunLoop().RunUntilIdle();
605 EXPECT_TRUE(called); 608 EXPECT_TRUE(called);
606 } 609 }
607 610
608 GURL PatternForSWId(int64 sw_id) { 611 GURL PatternForSWId(int64_t sw_id) {
609 EXPECT_TRUE(sw_id == sw_registration_id_1_ || 612 EXPECT_TRUE(sw_id == sw_registration_id_1_ ||
610 sw_id == sw_registration_id_2_); 613 sw_id == sw_registration_id_2_);
611 return sw_id == sw_registration_id_1_ ? GURL(kPattern1) : GURL(kPattern2); 614 return sw_id == sw_registration_id_1_ ? GURL(kPattern1) : GURL(kPattern2);
612 } 615 }
613 616
614 void SetupForSyncEvent( 617 void SetupForSyncEvent(
615 const TestBackgroundSyncManager::OneShotCallback& callback) { 618 const TestBackgroundSyncManager::OneShotCallback& callback) {
616 test_background_sync_manager_->set_one_shot_callback(callback); 619 test_background_sync_manager_->set_one_shot_callback(callback);
617 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI); 620 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI);
618 } 621 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
663 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; 666 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
664 TestPowerSource* power_monitor_source_ = nullptr; // owned by power_monitor_ 667 TestPowerSource* power_monitor_source_ = nullptr; // owned by power_monitor_
665 scoped_ptr<base::PowerMonitor> power_monitor_; 668 scoped_ptr<base::PowerMonitor> power_monitor_;
666 scoped_ptr<EmbeddedWorkerTestHelper> helper_; 669 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
667 scoped_ptr<BackgroundSyncManager> background_sync_manager_; 670 scoped_ptr<BackgroundSyncManager> background_sync_manager_;
668 scoped_ptr<StoragePartitionImpl> storage_partition_impl_; 671 scoped_ptr<StoragePartitionImpl> storage_partition_impl_;
669 TestBackgroundSyncManager* test_background_sync_manager_ = nullptr; 672 TestBackgroundSyncManager* test_background_sync_manager_ = nullptr;
670 TestBackgroundSyncController* test_controller_; 673 TestBackgroundSyncController* test_controller_;
671 base::SimpleTestClock* test_clock_ = nullptr; 674 base::SimpleTestClock* test_clock_ = nullptr;
672 675
673 int64 sw_registration_id_1_; 676 int64_t sw_registration_id_1_;
674 int64 sw_registration_id_2_; 677 int64_t sw_registration_id_2_;
675 scoped_refptr<ServiceWorkerRegistration> sw_registration_1_; 678 scoped_refptr<ServiceWorkerRegistration> sw_registration_1_;
676 scoped_refptr<ServiceWorkerRegistration> sw_registration_2_; 679 scoped_refptr<ServiceWorkerRegistration> sw_registration_2_;
677 680
678 BackgroundSyncRegistrationOptions sync_options_1_; 681 BackgroundSyncRegistrationOptions sync_options_1_;
679 BackgroundSyncRegistrationOptions sync_options_2_; 682 BackgroundSyncRegistrationOptions sync_options_2_;
680 683
681 // Callback values. 684 // Callback values.
682 BackgroundSyncStatus callback_status_ = BACKGROUND_SYNC_STATUS_OK; 685 BackgroundSyncStatus callback_status_ = BACKGROUND_SYNC_STATUS_OK;
683 scoped_ptr<BackgroundSyncRegistrationHandle> callback_registration_handle_; 686 scoped_ptr<BackgroundSyncRegistrationHandle> callback_registration_handle_;
684 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> 687 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>
(...skipping 1339 matching lines...) Expand 10 before | Expand all | Expand 10 after
2024 // Run it again. 2027 // Run it again.
2025 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); 2028 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta());
2026 test_background_sync_manager_->delayed_task().Run(); 2029 test_background_sync_manager_->delayed_task().Run();
2027 base::RunLoop().RunUntilIdle(); 2030 base::RunLoop().RunUntilIdle();
2028 EXPECT_FALSE(GetRegistration(sync_options_1_)); 2031 EXPECT_FALSE(GetRegistration(sync_options_1_));
2029 EXPECT_EQ(BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_LAST_CHANCE, 2032 EXPECT_EQ(BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_LAST_CHANCE,
2030 test_background_sync_manager_->last_chance()); 2033 test_background_sync_manager_->last_chance());
2031 } 2034 }
2032 2035
2033 } // namespace content 2036 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698