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/extensions/extension_gcm_app_handler.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
11 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "extensions/test/result_catcher.h"
16 using extensions::ResultCatcher
;
20 const char kEventsExtension
[] = "gcm/events";
22 gcm::GCMClient::SendErrorDetails
CreateErrorDetails(
23 const std::string
& message_id
,
24 const gcm::GCMClient::Result result
,
25 const std::string
& total_messages
) {
26 gcm::GCMClient::SendErrorDetails error
;
27 error
.message_id
= message_id
;
28 error
.result
= result
;
29 error
.additional_data
["expectedMessageId"] = message_id
;
31 case gcm::GCMClient::ASYNC_OPERATION_PENDING
:
32 error
.additional_data
["expectedErrorMessage"] =
33 "Asynchronous operation is pending.";
35 case gcm::GCMClient::SERVER_ERROR
:
36 error
.additional_data
["expectedErrorMessage"] = "Server error occurred.";
38 case gcm::GCMClient::NETWORK_ERROR
:
39 error
.additional_data
["expectedErrorMessage"] = "Network error occurred.";
41 case gcm::GCMClient::TTL_EXCEEDED
:
42 error
.additional_data
["expectedErrorMessage"] = "Time-to-live exceeded.";
44 case gcm::GCMClient::UNKNOWN_ERROR
:
45 default: // Default case is the same as UNKNOWN_ERROR
46 error
.additional_data
["expectedErrorMessage"] = "Unknown error occurred.";
49 error
.additional_data
["totalMessages"] = total_messages
;
55 namespace extensions
{
57 class GcmApiTest
: public ExtensionApiTest
{
59 GcmApiTest() : fake_gcm_profile_service_(NULL
) {}
62 void SetUpCommandLine(base::CommandLine
* command_line
) override
;
63 void SetUpOnMainThread() override
;
65 void StartCollecting();
67 const Extension
* LoadTestExtension(const std::string
& extension_path
,
68 const std::string
& page_name
);
69 gcm::FakeGCMProfileService
* service() const;
72 gcm::FakeGCMProfileService
* fake_gcm_profile_service_
;
75 void GcmApiTest::SetUpCommandLine(base::CommandLine
* command_line
) {
76 // We now always create the GCMProfileService instance in
77 // ProfileSyncServiceFactory that is called when a profile is being
78 // initialized. In order to prevent it from being created, we add the switch
79 // to disable the sync logic.
80 command_line
->AppendSwitch(switches::kDisableSync
);
82 ExtensionApiTest::SetUpCommandLine(command_line
);
85 void GcmApiTest::SetUpOnMainThread() {
86 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory(
87 browser()->profile(), &gcm::FakeGCMProfileService::Build
);
88 fake_gcm_profile_service_
= static_cast<gcm::FakeGCMProfileService
*>(
89 gcm::GCMProfileServiceFactory::GetInstance()->GetForProfile(
90 browser()->profile()));
92 ExtensionApiTest::SetUpOnMainThread();
95 void GcmApiTest::StartCollecting() {
96 service()->set_collect(true);
99 gcm::FakeGCMProfileService
* GcmApiTest::service() const {
100 return fake_gcm_profile_service_
;
103 const Extension
* GcmApiTest::LoadTestExtension(
104 const std::string
& extension_path
,
105 const std::string
& page_name
) {
106 const Extension
* extension
=
107 LoadExtension(test_data_dir_
.AppendASCII(extension_path
));
109 ui_test_utils::NavigateToURL(
110 browser(), extension
->GetResourceURL(page_name
));
115 IN_PROC_BROWSER_TEST_F(GcmApiTest
, RegisterValidation
) {
116 ASSERT_TRUE(RunExtensionTest("gcm/functions/register_validation"));
119 IN_PROC_BROWSER_TEST_F(GcmApiTest
, Register
) {
121 ASSERT_TRUE(RunExtensionTest("gcm/functions/register"));
123 const std::vector
<std::string
>& sender_ids
=
124 service()->last_registered_sender_ids();
125 EXPECT_TRUE(std::find(sender_ids
.begin(), sender_ids
.end(), "Sender1") !=
127 EXPECT_TRUE(std::find(sender_ids
.begin(), sender_ids
.end(), "Sender2") !=
131 IN_PROC_BROWSER_TEST_F(GcmApiTest
, Unregister
) {
132 service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS
);
133 service()->AddExpectedUnregisterResponse(gcm::GCMClient::SERVER_ERROR
);
135 ASSERT_TRUE(RunExtensionTest("gcm/functions/unregister"));
138 IN_PROC_BROWSER_TEST_F(GcmApiTest
, SendValidation
) {
139 ASSERT_TRUE(RunExtensionTest("gcm/functions/send"));
142 IN_PROC_BROWSER_TEST_F(GcmApiTest
, SendMessageData
) {
144 ASSERT_TRUE(RunExtensionTest("gcm/functions/send_message_data"));
146 EXPECT_EQ("destination-id", service()->last_receiver_id());
147 const gcm::OutgoingMessage
& message
= service()->last_sent_message();
148 gcm::MessageData::const_iterator iter
;
150 EXPECT_EQ(100, message
.time_to_live
);
152 EXPECT_TRUE((iter
= message
.data
.find("key1")) != message
.data
.end());
153 EXPECT_EQ("value1", iter
->second
);
155 EXPECT_TRUE((iter
= message
.data
.find("key2")) != message
.data
.end());
156 EXPECT_EQ("value2", iter
->second
);
159 IN_PROC_BROWSER_TEST_F(GcmApiTest
, SendMessageDefaultTTL
) {
161 ASSERT_TRUE(RunExtensionTest("gcm/functions/send_message_default_ttl"));
163 EXPECT_EQ("destination-id", service()->last_receiver_id());
164 const gcm::OutgoingMessage
& message
= service()->last_sent_message();
165 gcm::MessageData::const_iterator iter
;
167 EXPECT_EQ(gcm::OutgoingMessage::kMaximumTTL
, message
.time_to_live
);
170 IN_PROC_BROWSER_TEST_F(GcmApiTest
, OnMessagesDeleted
) {
171 ResultCatcher catcher
;
172 catcher
.RestrictToBrowserContext(profile());
174 const extensions::Extension
* extension
=
175 LoadTestExtension(kEventsExtension
, "on_messages_deleted.html");
176 ASSERT_TRUE(extension
);
178 extensions::ExtensionGCMAppHandler
app_handler(profile());
179 app_handler
.OnMessagesDeleted(extension
->id());
180 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
183 IN_PROC_BROWSER_TEST_F(GcmApiTest
, OnMessage
) {
184 ResultCatcher catcher
;
185 catcher
.RestrictToBrowserContext(profile());
187 const extensions::Extension
* extension
=
188 LoadTestExtension(kEventsExtension
, "on_message.html");
189 ASSERT_TRUE(extension
);
191 extensions::ExtensionGCMAppHandler
app_handler(profile());
193 gcm::IncomingMessage message
;
194 message
.data
["property1"] = "value1";
195 message
.data
["property2"] = "value2";
196 // First message is sent without from and collapse key.
197 app_handler
.OnMessage(extension
->id(), message
);
199 // Second message is send with from.
200 message
.sender_id
= "12345678";
201 app_handler
.OnMessage(extension
->id(), message
);
203 // Third message is send with a collapse key.
204 message
.sender_id
.clear();
205 message
.collapse_key
= "collapseKeyValue";
206 app_handler
.OnMessage(extension
->id(), message
);
208 // Fourth message carries the same data, from and collapse key.
209 message
.sender_id
= "12345678";
210 app_handler
.OnMessage(extension
->id(), message
);
212 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
215 IN_PROC_BROWSER_TEST_F(GcmApiTest
, OnSendError
) {
216 ResultCatcher catcher
;
217 catcher
.RestrictToBrowserContext(profile());
219 const extensions::Extension
* extension
=
220 LoadTestExtension(kEventsExtension
, "on_send_error.html");
221 ASSERT_TRUE(extension
);
223 std::string total_expected_messages
= "5";
224 extensions::ExtensionGCMAppHandler
app_handler(profile());
225 app_handler
.OnSendError(
227 CreateErrorDetails("error_message_1",
228 gcm::GCMClient::ASYNC_OPERATION_PENDING
,
229 total_expected_messages
));
230 app_handler
.OnSendError(
232 CreateErrorDetails("error_message_2",
233 gcm::GCMClient::SERVER_ERROR
,
234 total_expected_messages
));
235 app_handler
.OnSendError(
237 CreateErrorDetails("error_message_3",
238 gcm::GCMClient::NETWORK_ERROR
,
239 total_expected_messages
));
240 app_handler
.OnSendError(
242 CreateErrorDetails("error_message_4",
243 gcm::GCMClient::UNKNOWN_ERROR
,
244 total_expected_messages
));
245 app_handler
.OnSendError(
247 CreateErrorDetails("error_message_5",
248 gcm::GCMClient::TTL_EXCEEDED
,
249 total_expected_messages
));
251 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
254 IN_PROC_BROWSER_TEST_F(GcmApiTest
, Incognito
) {
255 ResultCatcher catcher
;
256 catcher
.RestrictToBrowserContext(profile());
257 ResultCatcher incognito_catcher
;
258 incognito_catcher
.RestrictToBrowserContext(
259 profile()->GetOffTheRecordProfile());
261 ASSERT_TRUE(RunExtensionTestIncognito("gcm/functions/incognito"));
263 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
264 EXPECT_TRUE(incognito_catcher
.GetNextResult()) << incognito_catcher
.message();
267 } // namespace extensions