Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / gcm / gcm_apitest.cc
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.
4
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"
14
15 namespace {
16
17 const char kEventsExtension[] = "gcm/events";
18
19 }  // namespace
20
21 namespace extensions {
22
23 class GcmApiTest : public ExtensionApiTest {
24  public:
25   GcmApiTest() : fake_gcm_profile_service_(NULL) {}
26
27  protected:
28   virtual void SetUpOnMainThread() OVERRIDE;
29
30   void StartCollecting();
31
32   const Extension* LoadTestExtension(const std::string& extension_path,
33                                      const std::string& page_name);
34   gcm::FakeGCMProfileService* service() const;
35   bool ShouldSkipTest() const;
36
37  private:
38   gcm::FakeGCMProfileService* fake_gcm_profile_service_;
39 };
40
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();
48 }
49
50 void GcmApiTest::StartCollecting() {
51   service()->set_collect(true);
52 }
53
54 gcm::FakeGCMProfileService* GcmApiTest::service() const {
55   return fake_gcm_profile_service_;
56 }
57
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);
63
64   const Extension* extension =
65       LoadExtension(test_data_dir_.AppendASCII(extension_path));
66   if (extension) {
67     ui_test_utils::NavigateToURL(
68         browser(), extension->GetResourceURL(page_name));
69   }
70   return extension;
71 }
72
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;
77 }
78
79 IN_PROC_BROWSER_TEST_F(GcmApiTest, RegisterValidation) {
80   if (ShouldSkipTest())
81     return;
82
83   ASSERT_TRUE(RunExtensionTest("gcm/functions/register_validation"));
84 }
85
86 IN_PROC_BROWSER_TEST_F(GcmApiTest, Register) {
87   if (ShouldSkipTest())
88     return;
89
90   StartCollecting();
91   ASSERT_TRUE(RunExtensionTest("gcm/functions/register"));
92
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") !=
99                   sender_ids.end());
100   EXPECT_TRUE(std::find(sender_ids.begin(), sender_ids.end(), "Sender2") !=
101                   sender_ids.end());
102 }
103
104 IN_PROC_BROWSER_TEST_F(GcmApiTest, SendValidation) {
105   if (ShouldSkipTest())
106     return;
107
108   ASSERT_TRUE(RunExtensionTest("gcm/functions/send"));
109 }
110
111 IN_PROC_BROWSER_TEST_F(GcmApiTest, SendMessageData) {
112   if (ShouldSkipTest())
113     return;
114
115   StartCollecting();
116   ASSERT_TRUE(RunExtensionTest("gcm/functions/send_message_data"));
117
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;
122
123   EXPECT_TRUE((iter = message.data.find("key1")) != message.data.end());
124   EXPECT_EQ("value1", iter->second);
125
126   EXPECT_TRUE((iter = message.data.find("key2")) != message.data.end());
127   EXPECT_EQ("value2", iter->second);
128 }
129
130 IN_PROC_BROWSER_TEST_F(GcmApiTest, OnMessagesDeleted) {
131   ResultCatcher catcher;
132   catcher.RestrictToProfile(profile());
133
134   const extensions::Extension* extension =
135       LoadTestExtension(kEventsExtension, "on_messages_deleted.html");
136   ASSERT_TRUE(extension);
137
138   GcmJsEventRouter router(profile());
139   router.OnMessagesDeleted(extension->id());
140   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
141 }
142
143 IN_PROC_BROWSER_TEST_F(GcmApiTest, OnMessage) {
144   ResultCatcher catcher;
145   catcher.RestrictToProfile(profile());
146
147   const extensions::Extension* extension =
148       LoadTestExtension(kEventsExtension, "on_message.html");
149   ASSERT_TRUE(extension);
150
151   GcmJsEventRouter router(profile());
152
153   gcm::GCMClient::IncomingMessage message;
154   message.data["property1"] = "value1";
155   message.data["property2"] = "value2";
156   router.OnMessage(extension->id(), message);
157
158   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
159 }
160
161 IN_PROC_BROWSER_TEST_F(GcmApiTest, OnSendError) {
162   ResultCatcher catcher;
163   catcher.RestrictToProfile(profile());
164
165   const extensions::Extension* extension =
166       LoadTestExtension(kEventsExtension, "on_send_error.html");
167   ASSERT_TRUE(extension);
168
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);
180
181   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
182 }
183
184 IN_PROC_BROWSER_TEST_F(GcmApiTest, Incognito) {
185   if (ShouldSkipTest())
186     return;
187
188   ResultCatcher catcher;
189   catcher.RestrictToProfile(profile());
190   ResultCatcher incognito_catcher;
191   incognito_catcher.RestrictToProfile(profile()->GetOffTheRecordProfile());
192
193   ASSERT_TRUE(RunExtensionTestIncognito("gcm/functions/incognito"));
194
195   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
196   EXPECT_TRUE(incognito_catcher.GetNextResult()) << incognito_catcher.message();
197 }
198
199 }  // namespace extensions