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