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" |
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() 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 |
59 PushMessagingAPI* GetAPI() { | 87 PushMessagingAPI* GetAPI() { |
60 return PushMessagingAPI::Get(profile()); | 88 return PushMessagingAPI::Get(profile()); |
61 } | 89 } |
62 | 90 |
63 PushMessagingEventRouter* GetEventRouter() { | 91 PushMessagingEventRouter* GetEventRouter() { |
64 return PushMessagingAPI::Get(profile())->GetEventRouterForTest(); | 92 return PushMessagingAPI::Get(profile())->GetEventRouterForTest(); |
65 } | 93 } |
| 94 |
| 95 invalidation::FakeInvalidationService* fake_invalidation_service_; |
66 }; | 96 }; |
67 | 97 |
68 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, EventDispatch) { | 98 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, EventDispatch) { |
69 ResultCatcher catcher; | 99 ResultCatcher catcher; |
70 catcher.RestrictToProfile(profile()); | 100 catcher.RestrictToProfile(profile()); |
71 | 101 |
72 const extensions::Extension* extension = | 102 const extensions::Extension* extension = |
73 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); | 103 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); |
74 ASSERT_TRUE(extension); | 104 ASSERT_TRUE(extension); |
75 ui_test_utils::NavigateToURL( | 105 ui_test_utils::NavigateToURL( |
76 browser(), extension->GetResourceURL("event_dispatch.html")); | 106 browser(), extension->GetResourceURL("event_dispatch.html")); |
77 | 107 |
78 GetEventRouter()->TriggerMessageForTest(extension->id(), 1, "payload"); | 108 GetEventRouter()->TriggerMessageForTest(extension->id(), 1, "payload"); |
79 | 109 |
80 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 110 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
81 } | 111 } |
82 | 112 |
83 // Test that a push introduced into the sync code makes it to the extension | 113 // Test that a push introduced into the sync code makes it to the extension |
84 // that we install. | 114 // that we install. |
85 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, ReceivesPush) { | 115 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, ReceivesPush) { |
86 ResultCatcher catcher; | 116 ResultCatcher catcher; |
87 catcher.RestrictToProfile(profile()); | 117 catcher.RestrictToProfile(profile()); |
88 | 118 |
89 const extensions::Extension* extension = | 119 const extensions::Extension* extension = |
90 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); | 120 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); |
91 ASSERT_TRUE(extension); | 121 ASSERT_TRUE(extension); |
92 ui_test_utils::NavigateToURL( | 122 ui_test_utils::NavigateToURL( |
93 browser(), extension->GetResourceURL("event_dispatch.html")); | 123 browser(), extension->GetResourceURL("event_dispatch.html")); |
94 | 124 |
95 ProfileSyncService* pss = | |
96 ProfileSyncServiceFactory::GetForProfile(profile()); | |
97 ASSERT_TRUE(pss); | |
98 | |
99 // PushMessagingInvalidationHandler suppresses the initial invalidation on | 125 // PushMessagingInvalidationHandler suppresses the initial invalidation on |
100 // each subchannel at install, so trigger the suppressions first. | 126 // each subchannel at install, so trigger the suppressions first. |
101 for (int i = 0; i < 3; ++i) { | 127 for (int i = 0; i < 3; ++i) { |
102 pss->EmitInvalidationForTest( | 128 EmitInvalidation( |
103 ExtensionAndSubchannelToObjectId(extension->id(), i), std::string()); | 129 ExtensionAndSubchannelToObjectId(extension->id(), i), std::string()); |
104 } | 130 } |
105 | 131 |
106 pss->EmitInvalidationForTest( | 132 EmitInvalidation( |
107 ExtensionAndSubchannelToObjectId(extension->id(), 1), "payload"); | 133 ExtensionAndSubchannelToObjectId(extension->id(), 1), "payload"); |
108 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 134 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
109 } | 135 } |
110 | 136 |
111 // Checks that an extension with the pushMessaging permission gets automatically | 137 // Checks that an extension with the pushMessaging permission gets automatically |
112 // registered for invalidations when it is loaded. | 138 // registered for invalidations when it is loaded. |
113 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, AutoRegistration) { | 139 IN_PROC_BROWSER_TEST_F(PushMessagingApiTest, AutoRegistration) { |
114 scoped_ptr<StrictMock<MockInvalidationMapper> > mapper( | 140 scoped_ptr<StrictMock<MockInvalidationMapper> > mapper( |
115 new StrictMock<MockInvalidationMapper>); | 141 new StrictMock<MockInvalidationMapper>); |
116 StrictMock<MockInvalidationMapper>* unsafe_mapper = mapper.get(); | 142 StrictMock<MockInvalidationMapper>* unsafe_mapper = mapper.get(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 const extensions::Extension* extension = | 185 const extensions::Extension* extension = |
160 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); | 186 LoadExtension(test_data_dir_.AppendASCII("push_messaging")); |
161 ASSERT_TRUE(extension); | 187 ASSERT_TRUE(extension); |
162 ui_test_utils::NavigateToURL( | 188 ui_test_utils::NavigateToURL( |
163 browser(), extension->GetResourceURL("get_channel_id.html")); | 189 browser(), extension->GetResourceURL("get_channel_id.html")); |
164 | 190 |
165 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 191 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
166 } | 192 } |
167 | 193 |
168 } // namespace extensions | 194 } // namespace extensions |
OLD | NEW |