1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/run_loop.h"
6 #include "chrome/browser/extensions/api/gcm/gcm_api.h"
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
10 #include "chrome/browser/services/gcm/gcm_client_factory.h"
11 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
12 #include "chrome/common/extensions/features/feature_channel.h"
13 #include "chrome/test/base/ui_test_utils.h"
17 const char kEventsExtension[] = "gcm/events";
21 namespace extensions {
23 class GcmApiTest : public ExtensionApiTest {
25 GcmApiTest() : fake_gcm_profile_service_(NULL) {}
28 virtual void SetUpOnMainThread() OVERRIDE;
30 void StartCollecting();
32 const Extension* LoadTestExtension(const std::string& extension_path,
33 const std::string& page_name);
34 gcm::FakeGCMProfileService* service() const;
35 bool ShouldSkipTest() const;
38 gcm::FakeGCMProfileService* fake_gcm_profile_service_;
41 void GcmApiTest::SetUpOnMainThread() {
42 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory(
43 browser()->profile(), &gcm::FakeGCMProfileService::Build);
44 fake_gcm_profile_service_ = static_cast<gcm::FakeGCMProfileService*>(
45 gcm::GCMProfileServiceFactory::GetInstance()->GetForProfile(
46 browser()->profile()));
47 gcm::FakeGCMProfileService::EnableGCMForTesting();
50 void GcmApiTest::StartCollecting() {
51 service()->set_collect(true);
54 gcm::FakeGCMProfileService* GcmApiTest::service() const {
55 return fake_gcm_profile_service_;
58 const Extension* GcmApiTest::LoadTestExtension(
59 const std::string& extension_path,
60 const std::string& page_name) {
61 // TODO(jianli): Once the GCM API enters stable, remove |channel|.
62 ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_UNKNOWN);
64 const Extension* extension =
65 LoadExtension(test_data_dir_.AppendASCII(extension_path));
67 ui_test_utils::NavigateToURL(
68 browser(), extension->GetResourceURL(page_name));
73 bool GcmApiTest::ShouldSkipTest() const {
74 // TODO(jianli): Remove this once the GCM API enters stable.
75 return chrome::VersionInfo::GetChannel() ==
76 chrome::VersionInfo::CHANNEL_STABLE;
79 IN_PROC_BROWSER_TEST_F(GcmApiTest, RegisterValidation) {
83 ASSERT_TRUE(RunExtensionTest("gcm/functions/register_validation"));
86 IN_PROC_BROWSER_TEST_F(GcmApiTest, Register) {
91 ASSERT_TRUE(RunExtensionTest("gcm/functions/register"));
93 // SHA1 of the public key provided in manifest.json.
94 EXPECT_EQ("26469186F238EE08FA71C38311C6990F61D40DCA",
95 service()->last_registered_cert());
96 const std::vector<std::string>& sender_ids =
97 service()->last_registered_sender_ids();
98 EXPECT_TRUE(std::find(sender_ids.begin(), sender_ids.end(), "Sender1") !=
100 EXPECT_TRUE(std::find(sender_ids.begin(), sender_ids.end(), "Sender2") !=
104 IN_PROC_BROWSER_TEST_F(GcmApiTest, SendValidation) {
105 if (ShouldSkipTest())
108 ASSERT_TRUE(RunExtensionTest("gcm/functions/send"));
111 IN_PROC_BROWSER_TEST_F(GcmApiTest, SendMessageData) {
112 if (ShouldSkipTest())
116 ASSERT_TRUE(RunExtensionTest("gcm/functions/send_message_data"));
118 EXPECT_EQ("destination-id", service()->last_receiver_id());
119 const gcm::GCMClient::OutgoingMessage& message =
120 service()->last_sent_message();
121 gcm::GCMClient::MessageData::const_iterator iter;
123 EXPECT_TRUE((iter = message.data.find("key1")) != message.data.end());
124 EXPECT_EQ("value1", iter->second);
126 EXPECT_TRUE((iter = message.data.find("key2")) != message.data.end());
127 EXPECT_EQ("value2", iter->second);
130 IN_PROC_BROWSER_TEST_F(GcmApiTest, OnMessagesDeleted) {
131 ResultCatcher catcher;
132 catcher.RestrictToProfile(profile());
134 const extensions::Extension* extension =
135 LoadTestExtension(kEventsExtension, "on_messages_deleted.html");
136 ASSERT_TRUE(extension);
138 GcmJsEventRouter router(profile());
139 router.OnMessagesDeleted(extension->id());
140 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
143 IN_PROC_BROWSER_TEST_F(GcmApiTest, OnMessage) {
144 ResultCatcher catcher;
145 catcher.RestrictToProfile(profile());
147 const extensions::Extension* extension =
148 LoadTestExtension(kEventsExtension, "on_message.html");
149 ASSERT_TRUE(extension);
151 GcmJsEventRouter router(profile());
153 gcm::GCMClient::IncomingMessage message;
154 message.data["property1"] = "value1";
155 message.data["property2"] = "value2";
156 router.OnMessage(extension->id(), message);
158 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
161 IN_PROC_BROWSER_TEST_F(GcmApiTest, OnSendError) {
162 ResultCatcher catcher;
163 catcher.RestrictToProfile(profile());
165 const extensions::Extension* extension =
166 LoadTestExtension(kEventsExtension, "on_send_error.html");
167 ASSERT_TRUE(extension);
169 GcmJsEventRouter router(profile());
170 router.OnSendError(extension->id(), "error_message_1",
171 gcm::GCMClient::ASYNC_OPERATION_PENDING);
172 router.OnSendError(extension->id(), "error_message_2",
173 gcm::GCMClient::SERVER_ERROR);
174 router.OnSendError(extension->id(), "error_message_3",
175 gcm::GCMClient::NETWORK_ERROR);
176 router.OnSendError(extension->id(), "error_message_4",
177 gcm::GCMClient::UNKNOWN_ERROR);
178 router.OnSendError(extension->id(), "error_message_5",
179 gcm::GCMClient::TTL_EXCEEDED);
181 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
184 IN_PROC_BROWSER_TEST_F(GcmApiTest, Incognito) {
185 if (ShouldSkipTest())
188 ResultCatcher catcher;
189 catcher.RestrictToProfile(profile());
190 ResultCatcher incognito_catcher;
191 incognito_catcher.RestrictToProfile(profile()->GetOffTheRecordProfile());
193 ASSERT_TRUE(RunExtensionTestIncognito("gcm/functions/incognito"));
195 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
196 EXPECT_TRUE(incognito_catcher.GetNextResult()) << incognito_catcher.message();
199 } // namespace extensions