Home | History | Annotate | Download | only in gcm
      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_profile_service_factory.h"
     11 #include "chrome/test/base/ui_test_utils.h"
     12 
     13 namespace {
     14 
     15 const char kFunctionsTestExtension[] = "gcm/functions";
     16 const char kEventsExtension[] = "gcm/events";
     17 
     18 }  // namespace
     19 
     20 namespace extensions {
     21 
     22 class GcmApiTest : public ExtensionApiTest {
     23  public:
     24   GcmApiTest() : fake_gcm_profile_service_(NULL) {}
     25 
     26  protected:
     27   void SetUpFakeService(bool collect);
     28 
     29   const Extension* LoadTestExtension(const std::string& extension_path,
     30                                      const std::string& page_name);
     31 
     32   void WaitUntilIdle();
     33 
     34   gcm::FakeGCMProfileService* service() const;
     35 
     36   gcm::FakeGCMProfileService* fake_gcm_profile_service_;
     37 };
     38 
     39 void GcmApiTest::SetUpFakeService(bool collect) {
     40   gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory(
     41       profile(), &gcm::FakeGCMProfileService::Build);
     42 
     43   fake_gcm_profile_service_ = static_cast<gcm::FakeGCMProfileService*>(
     44       gcm::GCMProfileServiceFactory::GetInstance()->GetForProfile(profile()));
     45   fake_gcm_profile_service_->set_collect(collect);
     46   gcm::FakeGCMProfileService::EnableGCMForTesting();
     47 }
     48 
     49 void GcmApiTest::WaitUntilIdle() {
     50   base::RunLoop run_loop;
     51   run_loop.RunUntilIdle();
     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   const Extension* extension =
     62       LoadExtension(test_data_dir_.AppendASCII(extension_path));
     63   if (extension) {
     64     ui_test_utils::NavigateToURL(
     65         browser(), extension->GetResourceURL(page_name));
     66   }
     67   return extension;
     68 }
     69 
     70 // http://crbug.com/177163 and http://crbug.com/324982
     71 #if defined(OS_WIN)
     72 #define MAYBE_RegisterValidation DISABLED_RegisterValidation
     73 #else
     74 #define MAYBE_RegisterValidation RegisterValidation
     75 #endif
     76 IN_PROC_BROWSER_TEST_F(GcmApiTest, MAYBE_RegisterValidation) {
     77   SetUpFakeService(false);
     78   EXPECT_TRUE(RunExtensionSubtest(kFunctionsTestExtension,
     79                                   "register_validation.html"));
     80 }
     81 
     82 // http://crbug.com/177163 and http://crbug.com/324982
     83 #if defined(OS_WIN)
     84 #define MAYBE_Register DISABLED_Register
     85 #else
     86 #define MAYBE_Register Register
     87 #endif
     88 IN_PROC_BROWSER_TEST_F(GcmApiTest, MAYBE_Register) {
     89   SetUpFakeService(true);
     90   const extensions::Extension* extension =
     91       LoadTestExtension(kFunctionsTestExtension, "register.html");
     92   ASSERT_TRUE(extension);
     93 
     94   WaitUntilIdle();
     95 
     96   EXPECT_EQ(extension->id(), service()->last_registered_app_id());
     97   // SHA1 of the public key provided in manifest.json.
     98   EXPECT_EQ("26469186F238EE08FA71C38311C6990F61D40DCA",
     99             service()->last_registered_cert());
    100   const std::vector<std::string>& sender_ids =
    101       service()->last_registered_sender_ids();
    102   EXPECT_TRUE(std::find(sender_ids.begin(), sender_ids.end(), "Sender1") !=
    103                   sender_ids.end());
    104   EXPECT_TRUE(std::find(sender_ids.begin(), sender_ids.end(), "Sender2") !=
    105                   sender_ids.end());
    106 }
    107 
    108 // http://crbug.com/177163 and http://crbug.com/324982
    109 #if defined(OS_WIN)
    110 #define MAYBE_SendValidation DISABLED_SendValidation
    111 #else
    112 #define MAYBE_SendValidation SendValidation
    113 #endif
    114 IN_PROC_BROWSER_TEST_F(GcmApiTest, MAYBE_SendValidation) {
    115   SetUpFakeService(false);
    116   EXPECT_TRUE(RunExtensionSubtest(kFunctionsTestExtension, "send.html"));
    117 }
    118 
    119 // http://crbug.com/177163 and http://crbug.com/324982
    120 #if defined(OS_WIN)
    121 #define MAYBE_SendMessageData DISABLED_SendMessageData
    122 #else
    123 #define MAYBE_SendMessageData SendMessageData
    124 #endif
    125 IN_PROC_BROWSER_TEST_F(GcmApiTest, MAYBE_SendMessageData) {
    126   SetUpFakeService(true);
    127   const extensions::Extension* extension =
    128       LoadTestExtension(kFunctionsTestExtension, "send_message_data.html");
    129   ASSERT_TRUE(extension);
    130 
    131   WaitUntilIdle();
    132 
    133   EXPECT_EQ("destination-id", service()->last_receiver_id());
    134   const gcm::GCMClient::OutgoingMessage& message =
    135       service()->last_sent_message();
    136   gcm::GCMClient::MessageData::const_iterator iter;
    137 
    138   EXPECT_TRUE((iter = message.data.find("key1")) != message.data.end());
    139   EXPECT_EQ("value1", iter->second);
    140 
    141   EXPECT_TRUE((iter = message.data.find("key2")) != message.data.end());
    142   EXPECT_EQ("value2", iter->second);
    143 }
    144 
    145 // http://crbug.com/177163 and http://crbug/324982
    146 #if defined(OS_WIN) && !defined(NDEBUG)
    147 #define MAYBE_OnMessagesDeleted DISABLED_OnMessagesDeleted
    148 #else
    149 #define MAYBE_OnMessagesDeleted OnMessagesDeleted
    150 #endif
    151 IN_PROC_BROWSER_TEST_F(GcmApiTest, MAYBE_OnMessagesDeleted) {
    152   ResultCatcher catcher;
    153   catcher.RestrictToProfile(profile());
    154 
    155   const extensions::Extension* extension =
    156       LoadTestExtension(kEventsExtension, "on_messages_deleted.html");
    157   ASSERT_TRUE(extension);
    158 
    159   GcmJsEventRouter router(profile());
    160   router.OnMessagesDeleted(extension->id());
    161   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
    162 }
    163 
    164 // http://crbug.com/177163 and http://crbug/324982
    165 #if defined(OS_WIN) && !defined(NDEBUG)
    166 #define MAYBE_OnMessage DISABLED_OnMessage
    167 #else
    168 #define MAYBE_OnMessage OnMessage
    169 #endif
    170 IN_PROC_BROWSER_TEST_F(GcmApiTest, MAYBE_OnMessage) {
    171   ResultCatcher catcher;
    172   catcher.RestrictToProfile(profile());
    173 
    174   const extensions::Extension* extension =
    175       LoadTestExtension(kEventsExtension, "on_message.html");
    176   ASSERT_TRUE(extension);
    177 
    178   GcmJsEventRouter router(profile());
    179 
    180   gcm::GCMClient::IncomingMessage message;
    181   message.data["property1"] = "value1";
    182   message.data["property2"] = "value2";
    183   router.OnMessage(extension->id(), message);
    184 
    185   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
    186 }
    187 
    188 // http://crbug.com/177163 and http://crbug/324982
    189 #if defined(OS_WIN) && !defined(NDEBUG)
    190 #define MAYBE_OnSendError DISABLED_OnSendError
    191 #else
    192 #define MAYBE_OnSendError OnSendError
    193 #endif
    194 IN_PROC_BROWSER_TEST_F(GcmApiTest, MAYBE_OnSendError) {
    195   ResultCatcher catcher;
    196   catcher.RestrictToProfile(profile());
    197 
    198   const extensions::Extension* extension =
    199       LoadTestExtension(kEventsExtension, "on_send_error.html");
    200   ASSERT_TRUE(extension);
    201 
    202   GcmJsEventRouter router(profile());
    203   router.OnSendError(extension->id(), "error_message_1",
    204       gcm::GCMClient::ASYNC_OPERATION_PENDING);
    205   router.OnSendError(extension->id(), "error_message_2",
    206       gcm::GCMClient::SERVER_ERROR);
    207   router.OnSendError(extension->id(), "error_message_3",
    208       gcm::GCMClient::NETWORK_ERROR);
    209   router.OnSendError(extension->id(), "error_message_4",
    210       gcm::GCMClient::UNKNOWN_ERROR);
    211   router.OnSendError(extension->id(), "error_message_5",
    212       gcm::GCMClient::TTL_EXCEEDED);
    213 
    214   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
    215 }
    216 
    217 }  // namespace extensions
    218