| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/run_loop.h" | 5 #include "base/run_loop.h" |
| 6 #include "chrome/browser/extensions/api/gcm/gcm_api.h" | 6 #include "chrome/browser/extensions/api/gcm/gcm_api.h" |
| 7 #include "chrome/browser/extensions/extension_apitest.h" | 7 #include "chrome/browser/extensions/extension_apitest.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" | 9 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
| 10 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 10 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 11 #include "chrome/common/extensions/features/feature_channel.h" | 11 #include "chrome/common/extensions/features/feature_channel.h" |
| 12 #include "chrome/test/base/ui_test_utils.h" | 12 #include "chrome/test/base/ui_test_utils.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 const char kFunctionsTestExtension[] = "gcm/functions"; | |
| 17 const char kEventsExtension[] = "gcm/events"; | 16 const char kEventsExtension[] = "gcm/events"; |
| 18 | 17 |
| 19 } // namespace | 18 } // namespace |
| 20 | 19 |
| 21 namespace extensions { | 20 namespace extensions { |
| 22 | 21 |
| 23 class GcmApiTest : public ExtensionApiTest { | 22 class GcmApiTest : public ExtensionApiTest { |
| 24 public: | 23 public: |
| 25 GcmApiTest() : fake_gcm_profile_service_(NULL) {} | 24 GcmApiTest() : fake_gcm_profile_service_(NULL) {} |
| 26 | 25 |
| 27 protected: | 26 protected: |
| 27 virtual void SetUp() OVERRIDE; |
| 28 virtual void SetUpOnMainThread() OVERRIDE; | 28 virtual void SetUpOnMainThread() OVERRIDE; |
| 29 | 29 |
| 30 void StartCollecting(); | 30 void StartCollecting(); |
| 31 | 31 |
| 32 const Extension* LoadTestExtension(const std::string& extension_path, | 32 const Extension* LoadTestExtension(const std::string& extension_path, |
| 33 const std::string& page_name); | 33 const std::string& page_name); |
| 34 | 34 |
| 35 void WaitUntilIdle(); | |
| 36 | |
| 37 gcm::FakeGCMProfileService* service() const; | 35 gcm::FakeGCMProfileService* service() const; |
| 38 | 36 |
| 37 private: |
| 39 gcm::FakeGCMProfileService* fake_gcm_profile_service_; | 38 gcm::FakeGCMProfileService* fake_gcm_profile_service_; |
| 40 }; | 39 }; |
| 41 | 40 |
| 41 void GcmApiTest::SetUp() { |
| 42 // TODO(jianli): Once the GCM API enters stable, remove |channel|. |
| 43 ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_UNKNOWN); |
| 44 |
| 45 ExtensionApiTest::SetUp(); |
| 46 } |
| 47 |
| 42 void GcmApiTest::SetUpOnMainThread() { | 48 void GcmApiTest::SetUpOnMainThread() { |
| 43 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory( | 49 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory( |
| 44 browser()->profile(), &gcm::FakeGCMProfileService::Build); | 50 browser()->profile(), &gcm::FakeGCMProfileService::Build); |
| 45 fake_gcm_profile_service_ = static_cast<gcm::FakeGCMProfileService*>( | 51 fake_gcm_profile_service_ = static_cast<gcm::FakeGCMProfileService*>( |
| 46 gcm::GCMProfileServiceFactory::GetInstance()->GetForProfile( | 52 gcm::GCMProfileServiceFactory::GetInstance()->GetForProfile( |
| 47 browser()->profile())); | 53 browser()->profile())); |
| 48 gcm::FakeGCMProfileService::EnableGCMForTesting(); | 54 gcm::FakeGCMProfileService::EnableGCMForTesting(); |
| 49 } | 55 } |
| 50 | 56 |
| 51 void GcmApiTest::StartCollecting() { | 57 void GcmApiTest::StartCollecting() { |
| 52 service()->set_collect(true); | 58 service()->set_collect(true); |
| 53 } | 59 } |
| 54 | 60 |
| 55 void GcmApiTest::WaitUntilIdle() { | |
| 56 base::RunLoop run_loop; | |
| 57 run_loop.RunUntilIdle(); | |
| 58 } | |
| 59 | |
| 60 gcm::FakeGCMProfileService* GcmApiTest::service() const { | 61 gcm::FakeGCMProfileService* GcmApiTest::service() const { |
| 61 return fake_gcm_profile_service_; | 62 return fake_gcm_profile_service_; |
| 62 } | 63 } |
| 63 | 64 |
| 64 const Extension* GcmApiTest::LoadTestExtension( | 65 const Extension* GcmApiTest::LoadTestExtension( |
| 65 const std::string& extension_path, | 66 const std::string& extension_path, |
| 66 const std::string& page_name) { | 67 const std::string& page_name) { |
| 67 // TODO(jianli): Once the GCM API enters stable, remove |channel|. | |
| 68 ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_UNKNOWN); | |
| 69 const Extension* extension = | 68 const Extension* extension = |
| 70 LoadExtension(test_data_dir_.AppendASCII(extension_path)); | 69 LoadExtension(test_data_dir_.AppendASCII(extension_path)); |
| 71 if (extension) { | 70 if (extension) { |
| 72 ui_test_utils::NavigateToURL( | 71 ui_test_utils::NavigateToURL( |
| 73 browser(), extension->GetResourceURL(page_name)); | 72 browser(), extension->GetResourceURL(page_name)); |
| 74 } | 73 } |
| 75 return extension; | 74 return extension; |
| 76 } | 75 } |
| 77 | 76 |
| 78 // http://crbug.com/177163 | 77 IN_PROC_BROWSER_TEST_F(GcmApiTest, RegisterValidation) { |
| 79 #if defined(OS_WIN) | 78 ASSERT_TRUE(RunExtensionTest("gcm/functions/register_validation")); |
| 80 #define MAYBE_RegisterValidation DISABLED_RegisterValidation | |
| 81 #else | |
| 82 #define MAYBE_RegisterValidation RegisterValidation | |
| 83 #endif | |
| 84 IN_PROC_BROWSER_TEST_F(GcmApiTest, MAYBE_RegisterValidation) { | |
| 85 EXPECT_TRUE(RunExtensionSubtest(kFunctionsTestExtension, | |
| 86 "register_validation.html")); | |
| 87 } | 79 } |
| 88 | 80 |
| 89 IN_PROC_BROWSER_TEST_F(GcmApiTest, Register) { | 81 IN_PROC_BROWSER_TEST_F(GcmApiTest, Register) { |
| 90 StartCollecting(); | 82 StartCollecting(); |
| 91 const extensions::Extension* extension = | 83 ASSERT_TRUE(RunExtensionTest("gcm/functions/register")); |
| 92 LoadTestExtension(kFunctionsTestExtension, "register.html"); | |
| 93 ASSERT_TRUE(extension); | |
| 94 | 84 |
| 95 WaitUntilIdle(); | |
| 96 | |
| 97 EXPECT_EQ(extension->id(), service()->last_registered_app_id()); | |
| 98 // SHA1 of the public key provided in manifest.json. | 85 // SHA1 of the public key provided in manifest.json. |
| 99 EXPECT_EQ("26469186F238EE08FA71C38311C6990F61D40DCA", | 86 EXPECT_EQ("26469186F238EE08FA71C38311C6990F61D40DCA", |
| 100 service()->last_registered_cert()); | 87 service()->last_registered_cert()); |
| 101 const std::vector<std::string>& sender_ids = | 88 const std::vector<std::string>& sender_ids = |
| 102 service()->last_registered_sender_ids(); | 89 service()->last_registered_sender_ids(); |
| 103 EXPECT_TRUE(std::find(sender_ids.begin(), sender_ids.end(), "Sender1") != | 90 EXPECT_TRUE(std::find(sender_ids.begin(), sender_ids.end(), "Sender1") != |
| 104 sender_ids.end()); | 91 sender_ids.end()); |
| 105 EXPECT_TRUE(std::find(sender_ids.begin(), sender_ids.end(), "Sender2") != | 92 EXPECT_TRUE(std::find(sender_ids.begin(), sender_ids.end(), "Sender2") != |
| 106 sender_ids.end()); | 93 sender_ids.end()); |
| 107 } | 94 } |
| 108 | 95 |
| 109 // http://crbug.com/177163 | 96 IN_PROC_BROWSER_TEST_F(GcmApiTest, SendValidation) { |
| 110 #if defined(OS_WIN) | 97 ASSERT_TRUE(RunExtensionTest("gcm/functions/send")); |
| 111 #define MAYBE_SendValidation DISABLED_SendValidation | |
| 112 #else | |
| 113 #define MAYBE_SendValidation SendValidation | |
| 114 #endif | |
| 115 IN_PROC_BROWSER_TEST_F(GcmApiTest, MAYBE_SendValidation) { | |
| 116 EXPECT_TRUE(RunExtensionSubtest(kFunctionsTestExtension, "send.html")); | |
| 117 } | 98 } |
| 118 | 99 |
| 119 IN_PROC_BROWSER_TEST_F(GcmApiTest, SendMessageData) { | 100 IN_PROC_BROWSER_TEST_F(GcmApiTest, SendMessageData) { |
| 120 StartCollecting(); | 101 StartCollecting(); |
| 121 const extensions::Extension* extension = | 102 ASSERT_TRUE(RunExtensionTest("gcm/functions/send_message_data")); |
| 122 LoadTestExtension(kFunctionsTestExtension, "send_message_data.html"); | |
| 123 ASSERT_TRUE(extension); | |
| 124 | |
| 125 WaitUntilIdle(); | |
| 126 | 103 |
| 127 EXPECT_EQ("destination-id", service()->last_receiver_id()); | 104 EXPECT_EQ("destination-id", service()->last_receiver_id()); |
| 128 const gcm::GCMClient::OutgoingMessage& message = | 105 const gcm::GCMClient::OutgoingMessage& message = |
| 129 service()->last_sent_message(); | 106 service()->last_sent_message(); |
| 130 gcm::GCMClient::MessageData::const_iterator iter; | 107 gcm::GCMClient::MessageData::const_iterator iter; |
| 131 | 108 |
| 132 EXPECT_TRUE((iter = message.data.find("key1")) != message.data.end()); | 109 EXPECT_TRUE((iter = message.data.find("key1")) != message.data.end()); |
| 133 EXPECT_EQ("value1", iter->second); | 110 EXPECT_EQ("value1", iter->second); |
| 134 | 111 |
| 135 EXPECT_TRUE((iter = message.data.find("key2")) != message.data.end()); | 112 EXPECT_TRUE((iter = message.data.find("key2")) != message.data.end()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 gcm::GCMClient::NETWORK_ERROR); | 161 gcm::GCMClient::NETWORK_ERROR); |
| 185 router.OnSendError(extension->id(), "error_message_4", | 162 router.OnSendError(extension->id(), "error_message_4", |
| 186 gcm::GCMClient::UNKNOWN_ERROR); | 163 gcm::GCMClient::UNKNOWN_ERROR); |
| 187 router.OnSendError(extension->id(), "error_message_5", | 164 router.OnSendError(extension->id(), "error_message_5", |
| 188 gcm::GCMClient::TTL_EXCEEDED); | 165 gcm::GCMClient::TTL_EXCEEDED); |
| 189 | 166 |
| 190 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 167 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 191 } | 168 } |
| 192 | 169 |
| 193 } // namespace extensions | 170 } // namespace extensions |
| OLD | NEW |