Chromium Code Reviews| 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/public/browser/push_messaging_service.h" | 5 #include "content/public/browser/push_messaging_service.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | |
| 16 #include "chrome/browser/permissions/permission_manager.h" | 17 #include "chrome/browser/permissions/permission_manager.h" |
| 17 #include "chrome/browser/permissions/permission_manager_factory.h" | 18 #include "chrome/browser/permissions/permission_manager_factory.h" |
| 18 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" | 19 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" |
| 19 #include "chrome/browser/push_messaging/push_messaging_permission_context.h" | 20 #include "chrome/browser/push_messaging/push_messaging_permission_context.h" |
| 20 #include "chrome/browser/push_messaging/push_messaging_permission_context_factor y.h" | |
| 21 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" | 21 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" |
| 22 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" | 22 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" |
| 23 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" | 23 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
| 24 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 24 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 25 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
| 26 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
| 26 #include "components/gcm_driver/crypto/gcm_crypto_test_helpers.h" | 27 #include "components/gcm_driver/crypto/gcm_crypto_test_helpers.h" |
| 27 #include "components/gcm_driver/fake_gcm_client_factory.h" | 28 #include "components/gcm_driver/fake_gcm_client_factory.h" |
| 28 #include "components/gcm_driver/gcm_profile_service.h" | 29 #include "components/gcm_driver/gcm_profile_service.h" |
| 29 #include "content/public/common/push_event_payload.h" | 30 #include "content/public/common/push_event_payload.h" |
| 30 #include "content/public/common/push_subscription_options.h" | 31 #include "content/public/common/push_subscription_options.h" |
| 31 #include "content/public/test/test_browser_thread_bundle.h" | 32 #include "content/public/test/test_browser_thread_bundle.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 50 } | 51 } |
| 51 | 52 |
| 52 PermissionManager* GetPermissionManager() override { | 53 PermissionManager* GetPermissionManager() override { |
| 53 return PermissionManagerFactory::GetForProfile(this); | 54 return PermissionManagerFactory::GetForProfile(this); |
| 54 } | 55 } |
| 55 | 56 |
| 56 private: | 57 private: |
| 57 DISALLOW_COPY_AND_ASSIGN(PushMessagingTestingProfile); | 58 DISALLOW_COPY_AND_ASSIGN(PushMessagingTestingProfile); |
| 58 }; | 59 }; |
| 59 | 60 |
| 60 // Implementation of the PushMessagingPermissionContext that always allows usage | |
| 61 // of Push Messaging, regardless of the actual implementation. | |
| 62 class FakePushMessagingPermissionContext | |
| 63 : public PushMessagingPermissionContext { | |
| 64 public: | |
| 65 explicit FakePushMessagingPermissionContext(Profile* profile) | |
| 66 : PushMessagingPermissionContext(profile) {} | |
| 67 ~FakePushMessagingPermissionContext() override {} | |
| 68 | |
| 69 ContentSetting GetPermissionStatus( | |
| 70 const GURL& requesting_origin, | |
| 71 const GURL& embedding_origin) const override { | |
| 72 return CONTENT_SETTING_ALLOW; | |
| 73 } | |
| 74 }; | |
| 75 | |
| 76 scoped_ptr<KeyedService> BuildFakePushMessagingPermissionContext( | |
| 77 content::BrowserContext* context) { | |
| 78 return scoped_ptr<KeyedService>( | |
| 79 new FakePushMessagingPermissionContext(static_cast<Profile*>(context))); | |
| 80 } | |
| 81 | 61 |
| 82 scoped_ptr<KeyedService> BuildFakeGCMProfileService( | 62 scoped_ptr<KeyedService> BuildFakeGCMProfileService( |
| 83 content::BrowserContext* context) { | 63 content::BrowserContext* context) { |
| 84 return gcm::FakeGCMProfileService::Build(static_cast<Profile*>(context)); | 64 return gcm::FakeGCMProfileService::Build(static_cast<Profile*>(context)); |
| 85 } | 65 } |
| 86 | 66 |
| 87 } // namespace | 67 } // namespace |
| 88 | 68 |
| 89 class PushMessagingServiceTest : public ::testing::Test { | 69 class PushMessagingServiceTest : public ::testing::Test { |
| 90 public: | 70 public: |
| 91 PushMessagingServiceTest() { | 71 PushMessagingServiceTest() { |
| 92 // Override the permission context factory to always allow Push Messaging. | 72 // Always allow push notifications in the profile. |
| 93 PushMessagingPermissionContextFactory::GetInstance()->SetTestingFactory( | 73 HostContentSettingsMap* host_content_settings_map = |
| 94 &profile_, &BuildFakePushMessagingPermissionContext); | 74 HostContentSettingsMapFactory::GetForProfile(&profile_); |
| 75 host_content_settings_map->SetDefaultContentSetting( | |
| 76 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | |
| 77 host_content_settings_map->SetDefaultContentSetting( | |
| 78 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ALLOW); | |
|
mlamouri (slow - plz ping)
2016/04/12 11:36:40
Could we have a fake PermissionManager instead? No
| |
| 95 | 79 |
| 96 // Override the GCM Profile service so that we can send fake messages. | 80 // Override the GCM Profile service so that we can send fake messages. |
| 97 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory( | 81 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory( |
| 98 &profile_, &BuildFakeGCMProfileService); | 82 &profile_, &BuildFakeGCMProfileService); |
| 99 } | 83 } |
| 100 | 84 |
| 101 ~PushMessagingServiceTest() override {} | 85 ~PushMessagingServiceTest() override {} |
| 102 | 86 |
| 103 // Callback to use when the subscription may have been subscribed. | 87 // Callback to use when the subscription may have been subscribed. |
| 104 void DidRegister(std::string* subscription_id_out, | 88 void DidRegister(std::string* subscription_id_out, |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 // (6) Verify that the message, as received by the Push Messaging Service, has | 200 // (6) Verify that the message, as received by the Push Messaging Service, has |
| 217 // indeed been decrypted by the GCM Driver, and has been forwarded to the | 201 // indeed been decrypted by the GCM Driver, and has been forwarded to the |
| 218 // Service Worker that has been associated with the subscription. | 202 // Service Worker that has been associated with the subscription. |
| 219 EXPECT_EQ(app_identifier.app_id(), app_id); | 203 EXPECT_EQ(app_identifier.app_id(), app_id); |
| 220 EXPECT_EQ(origin, dispatched_origin); | 204 EXPECT_EQ(origin, dispatched_origin); |
| 221 EXPECT_EQ(service_worker_registration_id, kTestServiceWorkerId); | 205 EXPECT_EQ(service_worker_registration_id, kTestServiceWorkerId); |
| 222 | 206 |
| 223 EXPECT_FALSE(payload.is_null); | 207 EXPECT_FALSE(payload.is_null); |
| 224 EXPECT_EQ(kTestPayload, payload.data); | 208 EXPECT_EQ(kTestPayload, payload.data); |
| 225 } | 209 } |
| OLD | NEW |