Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / extensions / api / gcm / gcm_apitest.cc
blob4abce7267d33bfbfef6a4fde1f50d7e72010127b
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;
18 namespace {
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;
30 switch (result) {
31 case gcm::GCMClient::ASYNC_OPERATION_PENDING:
32 error.additional_data["expectedErrorMessage"] =
33 "Asynchronous operation is pending.";
34 break;
35 case gcm::GCMClient::SERVER_ERROR:
36 error.additional_data["expectedErrorMessage"] = "Server error occurred.";
37 break;
38 case gcm::GCMClient::NETWORK_ERROR:
39 error.additional_data["expectedErrorMessage"] = "Network error occurred.";
40 break;
41 case gcm::GCMClient::TTL_EXCEEDED:
42 error.additional_data["expectedErrorMessage"] = "Time-to-live exceeded.";
43 break;
44 case gcm::GCMClient::UNKNOWN_ERROR:
45 default: // Default case is the same as UNKNOWN_ERROR
46 error.additional_data["expectedErrorMessage"] = "Unknown error occurred.";
47 break;
49 error.additional_data["totalMessages"] = total_messages;
50 return error;
53 } // namespace
55 namespace extensions {
57 class GcmApiTest : public ExtensionApiTest {
58 public:
59 GcmApiTest() : fake_gcm_profile_service_(NULL) {}
61 protected:
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;
71 private:
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));
108 if (extension) {
109 ui_test_utils::NavigateToURL(
110 browser(), extension->GetResourceURL(page_name));
112 return extension;
115 IN_PROC_BROWSER_TEST_F(GcmApiTest, RegisterValidation) {
116 ASSERT_TRUE(RunExtensionTest("gcm/functions/register_validation"));
119 IN_PROC_BROWSER_TEST_F(GcmApiTest, Register) {
120 StartCollecting();
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") !=
126 sender_ids.end());
127 EXPECT_TRUE(std::find(sender_ids.begin(), sender_ids.end(), "Sender2") !=
128 sender_ids.end());
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) {
143 StartCollecting();
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) {
160 StartCollecting();
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(
226 extension->id(),
227 CreateErrorDetails("error_message_1",
228 gcm::GCMClient::ASYNC_OPERATION_PENDING,
229 total_expected_messages));
230 app_handler.OnSendError(
231 extension->id(),
232 CreateErrorDetails("error_message_2",
233 gcm::GCMClient::SERVER_ERROR,
234 total_expected_messages));
235 app_handler.OnSendError(
236 extension->id(),
237 CreateErrorDetails("error_message_3",
238 gcm::GCMClient::NETWORK_ERROR,
239 total_expected_messages));
240 app_handler.OnSendError(
241 extension->id(),
242 CreateErrorDetails("error_message_4",
243 gcm::GCMClient::UNKNOWN_ERROR,
244 total_expected_messages));
245 app_handler.OnSendError(
246 extension->id(),
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