| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/api/push_messaging/push_messaging_api.h" | 5 #include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h" |
| 6 | 6 |
| 7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
| 8 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler.h" | 8 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler.h" |
| 9 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_mapper.h" | 9 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_mapper.h" |
| 10 #include "chrome/browser/extensions/extension_apitest.h" | 10 #include "chrome/browser/extensions/extension_apitest.h" |
| 11 #include "chrome/browser/extensions/extension_test_message_listener.h" | 11 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 12 #include "chrome/browser/extensions/platform_app_launcher.h" | 12 #include "chrome/browser/extensions/platform_app_launcher.h" |
| 13 #include "chrome/browser/invalidation/fake_invalidation_service.h" |
| 14 #include "chrome/browser/invalidation/invalidation_service.h" |
| 15 #include "chrome/browser/invalidation/invalidation_service_factory.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/sync/profile_sync_service.h" | |
| 15 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 16 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 18 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
| 19 #include "google/cacheinvalidation/types.pb.h" | 20 #include "google/cacheinvalidation/types.pb.h" |
| 21 #include "sync/notifier/fake_invalidator.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 21 | 23 |
| 22 using ::testing::_; | 24 using ::testing::_; |
| 23 using ::testing::SaveArg; | 25 using ::testing::SaveArg; |
| 24 using ::testing::StrictMock; | 26 using ::testing::StrictMock; |
| 25 | 27 |
| 28 using invalidation::InvalidationServiceFactory; |
| 29 |
| 26 namespace extensions { | 30 namespace extensions { |
| 27 | 31 |
| 28 namespace { | 32 namespace { |
| 29 | 33 |
| 30 invalidation::ObjectId ExtensionAndSubchannelToObjectId( | 34 invalidation::ObjectId ExtensionAndSubchannelToObjectId( |
| 31 const std::string& extension_id, int subchannel_id) { | 35 const std::string& extension_id, int subchannel_id) { |
| 32 return invalidation::ObjectId( | 36 return invalidation::ObjectId( |
| 33 ipc::invalidation::ObjectSource::CHROME_PUSH_MESSAGING, | 37 ipc::invalidation::ObjectSource::CHROME_PUSH_MESSAGING, |
| 34 base::StringPrintf("U/%s/%d", extension_id.c_str(), subchannel_id)); | 38 base::StringPrintf("U/%s/%d", extension_id.c_str(), subchannel_id)); |
| 35 } | 39 } |
| 36 | 40 |
| 37 class MockInvalidationMapper : public PushMessagingInvalidationMapper { | 41 class MockInvalidationMapper : public PushMessagingInvalidationMapper { |
| 38 public: | 42 public: |
| 39 MockInvalidationMapper(); | 43 MockInvalidationMapper(); |
| 40 ~MockInvalidationMapper(); | 44 ~MockInvalidationMapper(); |
| 41 | 45 |
| 42 MOCK_METHOD1(SuppressInitialInvalidationsForExtension, | 46 MOCK_METHOD1(SuppressInitialInvalidationsForExtension, |
| 43 void(const std::string&)); | 47 void(const std::string&)); |
| 44 MOCK_METHOD1(RegisterExtension, void(const std::string&)); | 48 MOCK_METHOD1(RegisterExtension, void(const std::string&)); |
| 45 MOCK_METHOD1(UnregisterExtension, void(const std::string&)); | 49 MOCK_METHOD1(UnregisterExtension, void(const std::string&)); |
| 46 }; | 50 }; |
| 47 | 51 |
| 48 MockInvalidationMapper::MockInvalidationMapper() {} | 52 MockInvalidationMapper::MockInvalidationMapper() {} |
| 49 MockInvalidationMapper::~MockInvalidationMapper() {} | 53 MockInvalidationMapper::~MockInvalidationMapper() {} |
| 50 | 54 |
| 51 } // namespace | 55 } // namespace |
| 52 | 56 |
| 53 class PushMessagingApiTest : public ExtensionApiTest { | 57 class PushMessagingApiTest : public ExtensionApiTest { |
| 54 public: | 58 public: |
| 59 PushMessagingApiTest() |
| 60 : fake_invalidation_service_(NULL) { |
| 61 } |
| 62 |
| 55 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 63 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 56 ExtensionApiTest::SetUpCommandLine(command_line); | 64 ExtensionApiTest::SetUpCommandLine(command_line); |
| 57 } | 65 } |
| 58 | 66 |
| 67 virtual void SetUp() { |
| 68 fake_invalidation_service_ = |
| 69 InvalidationServiceFactory::GetInstance()-> |
| 70 BuildAndUseFakeInvalidationServiceForTest(profile()); |
| 71 } |
| 72 |
| 73 void EmitInvalidation( |
| 74 const invalidation::ObjectId& object_id, |
| 75 const std::string& payload) { |
| 76 fake_invalidation_service_->EmitInvalidationForTest(object_id, payload); |
| 77 } |
| 78 |
| 59 PushMessagingAPI* GetAPI() { | 79 PushMessagingAPI* GetAPI() { |
| 60 return PushMessagingAPI::Get(profile()); | 80 return PushMessagingAPI::Get(profile()); |
| 61 } | 81 } |
| 62 | 82 |
| 63 PushMessagingEventRouter* GetEventRouter() { | 83 PushMessagingEventRouter* GetEventRouter() { |
| 64 return PushMessagingAPI::Get(profile())->GetEventRouterForTest(); | 84 return PushMessagingAPI::Get(profile())->GetEventRouterForTest(); |
| 65 } | 85 } |
| 86 |
| 87 invalidation::FakeInvalidationService* fake_invalidation_service_; |
| 66 }; | 88 }; |
| 67 | 89 |
| 68 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, EventDispatch) { | 90 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, EventDispatch) { |
| 69 ResultCatcher catcher; | 91 ResultCatcher catcher; |
| 70 catcher.RestrictToProfile(profile()); | 92 catcher.RestrictToProfile(profile()); |
| 71 | 93 |
| 72 const extensions::Extension* extension = | 94 const extensions::Extension* extension = |
| 73 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); | 95 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); |
| 74 ASSERT_TRUE(extension); | 96 ASSERT_TRUE(extension); |
| 75 ui_test_utils::NavigateToURL( | 97 ui_test_utils::NavigateToURL( |
| 76 browser(), extension->GetResourceURL("event_dispatch.html")); | 98 browser(), extension->GetResourceURL("event_dispatch.html")); |
| 77 | 99 |
| 78 GetEventRouter()->TriggerMessageForTest(extension->id(), 1, "payload"); | 100 GetEventRouter()->TriggerMessageForTest(extension->id(), 1, "payload"); |
| 79 | 101 |
| 80 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 102 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 81 } | 103 } |
| 82 | 104 |
| 83 // Test that a push introduced into the sync code makes it to the extension | 105 // Test that a push introduced into the sync code makes it to the extension |
| 84 // that we install. | 106 // that we install. |
| 85 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, ReceivesPush) { | 107 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, ReceivesPush) { |
| 86 ResultCatcher catcher; | 108 ResultCatcher catcher; |
| 87 catcher.RestrictToProfile(profile()); | 109 catcher.RestrictToProfile(profile()); |
| 88 | 110 |
| 89 const extensions::Extension* extension = | 111 const extensions::Extension* extension = |
| 90 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); | 112 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); |
| 91 ASSERT_TRUE(extension); | 113 ASSERT_TRUE(extension); |
| 92 ui_test_utils::NavigateToURL( | 114 ui_test_utils::NavigateToURL( |
| 93 browser(), extension->GetResourceURL("event_dispatch.html")); | 115 browser(), extension->GetResourceURL("event_dispatch.html")); |
| 94 | 116 |
| 95 ProfileSyncService* pss = | |
| 96 ProfileSyncServiceFactory::GetForProfile(profile()); | |
| 97 ASSERT_TRUE(pss); | |
| 98 | |
| 99 // PushMessagingInvalidationHandler suppresses the initial invalidation on | 117 // PushMessagingInvalidationHandler suppresses the initial invalidation on |
| 100 // each subchannel at install, so trigger the suppressions first. | 118 // each subchannel at install, so trigger the suppressions first. |
| 101 for (int i = 0; i < 3; ++i) { | 119 for (int i = 0; i < 3; ++i) { |
| 102 pss->EmitInvalidationForTest( | 120 EmitInvalidation( |
| 103 ExtensionAndSubchannelToObjectId(extension->id(), i), std::string()); | 121 ExtensionAndSubchannelToObjectId(extension->id(), i), std::string()); |
| 104 } | 122 } |
| 105 | 123 |
| 106 pss->EmitInvalidationForTest( | 124 EmitInvalidation( |
| 107 ExtensionAndSubchannelToObjectId(extension->id(), 1), "payload"); | 125 ExtensionAndSubchannelToObjectId(extension->id(), 1), "payload"); |
| 108 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 126 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 109 } | 127 } |
| 110 | 128 |
| 111 // Checks that an extension with the pushMessaging permission gets automatically | 129 // Checks that an extension with the pushMessaging permission gets automatically |
| 112 // registered for invalidations when it is loaded. | 130 // registered for invalidations when it is loaded. |
| 113 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, AutoRegistration) { | 131 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, AutoRegistration) { |
| 114 scoped_ptr<StrictMock<MockInvalidationMapper> > mapper( | 132 scoped_ptr<StrictMock<MockInvalidationMapper> > mapper( |
| 115 new StrictMock<MockInvalidationMapper>); | 133 new StrictMock<MockInvalidationMapper>); |
| 116 StrictMock<MockInvalidationMapper>* unsafe_mapper = mapper.get(); | 134 StrictMock<MockInvalidationMapper>* unsafe_mapper = mapper.get(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 const extensions::Extension* extension = | 177 const extensions::Extension* extension = |
| 160 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); | 178 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); |
| 161 ASSERT_TRUE(extension); | 179 ASSERT_TRUE(extension); |
| 162 ui_test_utils::NavigateToURL( | 180 ui_test_utils::NavigateToURL( |
| 163 browser(), extension->GetResourceURL("get_channel_id.html")); | 181 browser(), extension->GetResourceURL("get_channel_id.html")); |
| 164 | 182 |
| 165 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 183 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 166 } | 184 } |
| 167 | 185 |
| 168 } // namespace extensions | 186 } // namespace extensions |
| OLD | NEW |