| OLD | NEW |
| 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> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/metrics/field_trial.h" | 17 #include "base/metrics/field_trial.h" |
| 18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 20 #include "base/test/mock_entropy_provider.h" | 20 #include "base/test/mock_entropy_provider.h" |
| 21 #include "base/test/simple_test_clock.h" | 21 #include "base/test/simple_test_clock.h" |
| 22 #include "base/thread_task_runner_handle.h" | 22 #include "base/thread_task_runner_handle.h" |
| 23 #include "components/permissions/permission_status.mojom.h" | |
| 24 #include "content/browser/background_sync/background_sync_network_observer.h" | 23 #include "content/browser/background_sync/background_sync_network_observer.h" |
| 25 #include "content/browser/background_sync/background_sync_status.h" | 24 #include "content/browser/background_sync/background_sync_status.h" |
| 26 #include "content/browser/browser_thread_impl.h" | 25 #include "content/browser/browser_thread_impl.h" |
| 27 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 26 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 28 #include "content/browser/service_worker/service_worker_context_core.h" | 27 #include "content/browser/service_worker/service_worker_context_core.h" |
| 29 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 28 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 30 #include "content/browser/service_worker/service_worker_storage.h" | 29 #include "content/browser/service_worker/service_worker_storage.h" |
| 31 #include "content/browser/storage_partition_impl.h" | 30 #include "content/browser/storage_partition_impl.h" |
| 32 #include "content/public/browser/background_sync_parameters.h" | 31 #include "content/public/browser/background_sync_parameters.h" |
| 33 #include "content/public/browser/permission_type.h" | 32 #include "content/public/browser/permission_type.h" |
| 34 #include "content/public/test/background_sync_test_util.h" | 33 #include "content/public/test/background_sync_test_util.h" |
| 35 #include "content/public/test/test_browser_context.h" | 34 #include "content/public/test/test_browser_context.h" |
| 36 #include "content/public/test/test_browser_thread_bundle.h" | 35 #include "content/public/test/test_browser_thread_bundle.h" |
| 37 #include "content/test/mock_background_sync_controller.h" | 36 #include "content/test/mock_background_sync_controller.h" |
| 38 #include "content/test/mock_permission_manager.h" | 37 #include "content/test/mock_permission_manager.h" |
| 39 #include "content/test/test_background_sync_manager.h" | 38 #include "content/test/test_background_sync_manager.h" |
| 40 #include "net/base/network_change_notifier.h" | 39 #include "net/base/network_change_notifier.h" |
| 41 #include "testing/gmock/include/gmock/gmock.h" | 40 #include "testing/gmock/include/gmock/gmock.h" |
| 42 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 42 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat
us.mojom.h" |
| 43 | 43 |
| 44 namespace content { | 44 namespace content { |
| 45 | 45 |
| 46 namespace { | 46 namespace { |
| 47 | 47 |
| 48 using ::testing::Return; | 48 using ::testing::Return; |
| 49 using ::testing::_; | 49 using ::testing::_; |
| 50 | 50 |
| 51 const char kPattern1[] = "https://example.com/a"; | 51 const char kPattern1[] = "https://example.com/a"; |
| 52 const char kPattern2[] = "https://example.com/b"; | 52 const char kPattern2[] = "https://example.com/b"; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 122 |
| 123 // TODO(jkarlin): Create a new object with all of the necessary SW calls | 123 // TODO(jkarlin): Create a new object with all of the necessary SW calls |
| 124 // so that we can inject test versions instead of bringing up all of this | 124 // so that we can inject test versions instead of bringing up all of this |
| 125 // extra SW stuff. | 125 // extra SW stuff. |
| 126 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 126 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
| 127 | 127 |
| 128 std::unique_ptr<MockPermissionManager> mock_permission_manager( | 128 std::unique_ptr<MockPermissionManager> mock_permission_manager( |
| 129 new testing::NiceMock<MockPermissionManager>()); | 129 new testing::NiceMock<MockPermissionManager>()); |
| 130 ON_CALL(*mock_permission_manager, | 130 ON_CALL(*mock_permission_manager, |
| 131 GetPermissionStatus(PermissionType::BACKGROUND_SYNC, _, _)) | 131 GetPermissionStatus(PermissionType::BACKGROUND_SYNC, _, _)) |
| 132 .WillByDefault(Return(permissions::mojom::PermissionStatus::GRANTED)); | 132 .WillByDefault(Return(blink::mojom::PermissionStatus::GRANTED)); |
| 133 helper_->browser_context()->SetPermissionManager( | 133 helper_->browser_context()->SetPermissionManager( |
| 134 std::move(mock_permission_manager)); | 134 std::move(mock_permission_manager)); |
| 135 | 135 |
| 136 // Create a StoragePartition with the correct BrowserContext so that the | 136 // Create a StoragePartition with the correct BrowserContext so that the |
| 137 // BackgroundSyncManager can find the BrowserContext through it. | 137 // BackgroundSyncManager can find the BrowserContext through it. |
| 138 storage_partition_impl_.reset(new StoragePartitionImpl( | 138 storage_partition_impl_.reset(new StoragePartitionImpl( |
| 139 helper_->browser_context(), base::FilePath(), nullptr, nullptr, nullptr, | 139 helper_->browser_context(), base::FilePath(), nullptr, nullptr, nullptr, |
| 140 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, | 140 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, |
| 141 nullptr, nullptr, nullptr)); | 141 nullptr, nullptr, nullptr)); |
| 142 helper_->context_wrapper()->set_storage_partition( | 142 helper_->context_wrapper()->set_storage_partition( |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 EXPECT_FALSE(GetRegistration(sync_options_1_)); | 465 EXPECT_FALSE(GetRegistration(sync_options_1_)); |
| 466 } | 466 } |
| 467 | 467 |
| 468 TEST_F(BackgroundSyncManagerTest, RegisterPermissionDenied) { | 468 TEST_F(BackgroundSyncManagerTest, RegisterPermissionDenied) { |
| 469 GURL expected_origin = GURL(kPattern1).GetOrigin(); | 469 GURL expected_origin = GURL(kPattern1).GetOrigin(); |
| 470 MockPermissionManager* mock_permission_manager = GetPermissionManager(); | 470 MockPermissionManager* mock_permission_manager = GetPermissionManager(); |
| 471 | 471 |
| 472 EXPECT_CALL(*mock_permission_manager, | 472 EXPECT_CALL(*mock_permission_manager, |
| 473 GetPermissionStatus(PermissionType::BACKGROUND_SYNC, | 473 GetPermissionStatus(PermissionType::BACKGROUND_SYNC, |
| 474 expected_origin, expected_origin)) | 474 expected_origin, expected_origin)) |
| 475 .WillOnce(testing::Return(permissions::mojom::PermissionStatus::DENIED)); | 475 .WillOnce(testing::Return(blink::mojom::PermissionStatus::DENIED)); |
| 476 EXPECT_FALSE(Register(sync_options_1_)); | 476 EXPECT_FALSE(Register(sync_options_1_)); |
| 477 } | 477 } |
| 478 | 478 |
| 479 TEST_F(BackgroundSyncManagerTest, RegisterPermissionGranted) { | 479 TEST_F(BackgroundSyncManagerTest, RegisterPermissionGranted) { |
| 480 GURL expected_origin = GURL(kPattern1).GetOrigin(); | 480 GURL expected_origin = GURL(kPattern1).GetOrigin(); |
| 481 MockPermissionManager* mock_permission_manager = GetPermissionManager(); | 481 MockPermissionManager* mock_permission_manager = GetPermissionManager(); |
| 482 | 482 |
| 483 EXPECT_CALL(*mock_permission_manager, | 483 EXPECT_CALL(*mock_permission_manager, |
| 484 GetPermissionStatus(PermissionType::BACKGROUND_SYNC, | 484 GetPermissionStatus(PermissionType::BACKGROUND_SYNC, |
| 485 expected_origin, expected_origin)) | 485 expected_origin, expected_origin)) |
| 486 .WillOnce(testing::Return(permissions::mojom::PermissionStatus::GRANTED)); | 486 .WillOnce(testing::Return(blink::mojom::PermissionStatus::GRANTED)); |
| 487 EXPECT_TRUE(Register(sync_options_1_)); | 487 EXPECT_TRUE(Register(sync_options_1_)); |
| 488 } | 488 } |
| 489 | 489 |
| 490 TEST_F(BackgroundSyncManagerTest, TwoRegistrations) { | 490 TEST_F(BackgroundSyncManagerTest, TwoRegistrations) { |
| 491 EXPECT_TRUE(Register(sync_options_1_)); | 491 EXPECT_TRUE(Register(sync_options_1_)); |
| 492 EXPECT_TRUE(Register(sync_options_2_)); | 492 EXPECT_TRUE(Register(sync_options_2_)); |
| 493 } | 493 } |
| 494 | 494 |
| 495 TEST_F(BackgroundSyncManagerTest, GetRegistrationNonExisting) { | 495 TEST_F(BackgroundSyncManagerTest, GetRegistrationNonExisting) { |
| 496 EXPECT_FALSE(GetRegistration(sync_options_1_)); | 496 EXPECT_FALSE(GetRegistration(sync_options_1_)); |
| (...skipping 817 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1314 // Run it again. | 1314 // Run it again. |
| 1315 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); | 1315 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); |
| 1316 test_background_sync_manager_->delayed_task().Run(); | 1316 test_background_sync_manager_->delayed_task().Run(); |
| 1317 base::RunLoop().RunUntilIdle(); | 1317 base::RunLoop().RunUntilIdle(); |
| 1318 EXPECT_FALSE(GetRegistration(sync_options_1_)); | 1318 EXPECT_FALSE(GetRegistration(sync_options_1_)); |
| 1319 EXPECT_EQ(mojom::BackgroundSyncEventLastChance::IS_LAST_CHANCE, | 1319 EXPECT_EQ(mojom::BackgroundSyncEventLastChance::IS_LAST_CHANCE, |
| 1320 test_background_sync_manager_->last_chance()); | 1320 test_background_sync_manager_->last_chance()); |
| 1321 } | 1321 } |
| 1322 | 1322 |
| 1323 } // namespace content | 1323 } // namespace content |
| OLD | NEW |