Enable Enterprise enrollment on desktop builds.
[chromium-blink-merge.git] / chrome / browser / extensions / api / gcm / gcm_apitest.cc
blob0ce8663ff12ce2115d5434f3b4ee5022392556a5
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/prefs/pref_service.h"
6 #include "base/run_loop.h"
7 #include "chrome/browser/extensions/api/gcm/gcm_api.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/extension_gcm_app_handler.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
12 #include "chrome/browser/services/gcm/gcm_client_factory.h"
13 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/ui_test_utils.h"
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 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
63 virtual 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(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 // Enable GCM such that tests could be run on all channels.
87 browser()->profile()->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled, true);
89 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory(
90 browser()->profile(), &gcm::FakeGCMProfileService::Build);
91 fake_gcm_profile_service_ = static_cast<gcm::FakeGCMProfileService*>(
92 gcm::GCMProfileServiceFactory::GetInstance()->GetForProfile(
93 browser()->profile()));
95 ExtensionApiTest::SetUpOnMainThread();
98 void GcmApiTest::StartCollecting() {
99 service()->set_collect(true);
102 gcm::FakeGCMProfileService* GcmApiTest::service() const {
103 return fake_gcm_profile_service_;
106 const Extension* GcmApiTest::LoadTestExtension(
107 const std::string& extension_path,
108 const std::string& page_name) {
109 const Extension* extension =
110 LoadExtension(test_data_dir_.AppendASCII(extension_path));
111 if (extension) {
112 ui_test_utils::NavigateToURL(
113 browser(), extension->GetResourceURL(page_name));
115 return extension;
118 IN_PROC_BROWSER_TEST_F(GcmApiTest, RegisterValidation) {
119 ASSERT_TRUE(RunExtensionTest("gcm/functions/register_validation"));
122 IN_PROC_BROWSER_TEST_F(GcmApiTest, Register) {
123 StartCollecting();
124 ASSERT_TRUE(RunExtensionTest("gcm/functions/register"));
126 const std::vector<std::string>& sender_ids =
127 service()->last_registered_sender_ids();
128 EXPECT_TRUE(std::find(sender_ids.begin(), sender_ids.end(), "Sender1") !=
129 sender_ids.end());
130 EXPECT_TRUE(std::find(sender_ids.begin(), sender_ids.end(), "Sender2") !=
131 sender_ids.end());
134 IN_PROC_BROWSER_TEST_F(GcmApiTest, Unregister) {
135 service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS);
136 service()->AddExpectedUnregisterResponse(gcm::GCMClient::SERVER_ERROR);
138 ASSERT_TRUE(RunExtensionTest("gcm/functions/unregister"));
141 IN_PROC_BROWSER_TEST_F(GcmApiTest, SendValidation) {
142 ASSERT_TRUE(RunExtensionTest("gcm/functions/send"));
145 IN_PROC_BROWSER_TEST_F(GcmApiTest, SendMessageData) {
146 StartCollecting();
147 ASSERT_TRUE(RunExtensionTest("gcm/functions/send_message_data"));
149 EXPECT_EQ("destination-id", service()->last_receiver_id());
150 const gcm::GCMClient::OutgoingMessage& message =
151 service()->last_sent_message();
152 gcm::GCMClient::MessageData::const_iterator iter;
154 EXPECT_TRUE((iter = message.data.find("key1")) != message.data.end());
155 EXPECT_EQ("value1", iter->second);
157 EXPECT_TRUE((iter = message.data.find("key2")) != message.data.end());
158 EXPECT_EQ("value2", iter->second);
161 IN_PROC_BROWSER_TEST_F(GcmApiTest, OnMessagesDeleted) {
162 ResultCatcher catcher;
163 catcher.RestrictToProfile(profile());
165 const extensions::Extension* extension =
166 LoadTestExtension(kEventsExtension, "on_messages_deleted.html");
167 ASSERT_TRUE(extension);
169 extensions::ExtensionGCMAppHandler app_handler(profile());
170 app_handler.OnMessagesDeleted(extension->id());
171 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
174 IN_PROC_BROWSER_TEST_F(GcmApiTest, OnMessage) {
175 ResultCatcher catcher;
176 catcher.RestrictToProfile(profile());
178 const extensions::Extension* extension =
179 LoadTestExtension(kEventsExtension, "on_message.html");
180 ASSERT_TRUE(extension);
182 extensions::ExtensionGCMAppHandler app_handler(profile());
184 gcm::GCMClient::IncomingMessage message;
185 message.data["property1"] = "value1";
186 message.data["property2"] = "value2";
187 // First message is sent without a collapse key.
188 app_handler.OnMessage(extension->id(), message);
190 // Second message carries the same data and a collapse key.
191 message.collapse_key = "collapseKeyValue";
192 app_handler.OnMessage(extension->id(), message);
194 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
197 IN_PROC_BROWSER_TEST_F(GcmApiTest, OnSendError) {
198 ResultCatcher catcher;
199 catcher.RestrictToProfile(profile());
201 const extensions::Extension* extension =
202 LoadTestExtension(kEventsExtension, "on_send_error.html");
203 ASSERT_TRUE(extension);
205 std::string total_expected_messages = "5";
206 extensions::ExtensionGCMAppHandler app_handler(profile());
207 app_handler.OnSendError(
208 extension->id(),
209 CreateErrorDetails("error_message_1",
210 gcm::GCMClient::ASYNC_OPERATION_PENDING,
211 total_expected_messages));
212 app_handler.OnSendError(
213 extension->id(),
214 CreateErrorDetails("error_message_2",
215 gcm::GCMClient::SERVER_ERROR,
216 total_expected_messages));
217 app_handler.OnSendError(
218 extension->id(),
219 CreateErrorDetails("error_message_3",
220 gcm::GCMClient::NETWORK_ERROR,
221 total_expected_messages));
222 app_handler.OnSendError(
223 extension->id(),
224 CreateErrorDetails("error_message_4",
225 gcm::GCMClient::UNKNOWN_ERROR,
226 total_expected_messages));
227 app_handler.OnSendError(
228 extension->id(),
229 CreateErrorDetails("error_message_5",
230 gcm::GCMClient::TTL_EXCEEDED,
231 total_expected_messages));
233 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
236 IN_PROC_BROWSER_TEST_F(GcmApiTest, Incognito) {
237 ResultCatcher catcher;
238 catcher.RestrictToProfile(profile());
239 ResultCatcher incognito_catcher;
240 incognito_catcher.RestrictToProfile(profile()->GetOffTheRecordProfile());
242 ASSERT_TRUE(RunExtensionTestIncognito("gcm/functions/incognito"));
244 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
245 EXPECT_TRUE(incognito_catcher.GetNextResult()) << incognito_catcher.message();
248 } // namespace extensions