Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/services/gcm/gcm_profile_service.h" | 5 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 protected: | 52 protected: |
| 53 GCMProfileServiceTest(); | 53 GCMProfileServiceTest(); |
| 54 virtual ~GCMProfileServiceTest(); | 54 virtual ~GCMProfileServiceTest(); |
| 55 | 55 |
| 56 // testing::Test: | 56 // testing::Test: |
| 57 virtual void SetUp() OVERRIDE; | 57 virtual void SetUp() OVERRIDE; |
| 58 virtual void TearDown() OVERRIDE; | 58 virtual void TearDown() OVERRIDE; |
| 59 | 59 |
| 60 FakeGCMClient* GetGCMClient() const; | 60 FakeGCMClient* GetGCMClient() const; |
| 61 | 61 |
| 62 void CreateGCMProfileService(); | |
| 63 void SignIn(); | |
| 64 | |
| 62 void RegisterAndWaitForCompletion(const std::vector<std::string>& sender_ids); | 65 void RegisterAndWaitForCompletion(const std::vector<std::string>& sender_ids); |
| 63 void UnregisterAndWaitForCompletion(); | 66 void UnregisterAndWaitForCompletion(); |
| 64 void SendAndWaitForCompletion(const GCMClient::OutgoingMessage& message); | 67 void SendAndWaitForCompletion(const GCMClient::OutgoingMessage& message); |
| 65 | 68 |
| 66 void RegisterCompleted(const base::Closure& callback, | 69 void RegisterCompleted(const base::Closure& callback, |
| 67 const std::string& registration_id, | 70 const std::string& registration_id, |
| 68 GCMClient::Result result); | 71 GCMClient::Result result); |
| 69 void UnregisterCompleted(const base::Closure& callback, | 72 void UnregisterCompleted(const base::Closure& callback, |
| 70 GCMClient::Result result); | 73 GCMClient::Result result); |
| 71 void SendCompleted(const base::Closure& callback, | 74 void SendCompleted(const base::Closure& callback, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const { | 112 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const { |
| 110 return static_cast<FakeGCMClient*>( | 113 return static_cast<FakeGCMClient*>( |
| 111 gcm_profile_service_->driver()->GetGCMClientForTesting()); | 114 gcm_profile_service_->driver()->GetGCMClientForTesting()); |
| 112 } | 115 } |
| 113 | 116 |
| 114 void GCMProfileServiceTest::SetUp() { | 117 void GCMProfileServiceTest::SetUp() { |
| 115 TestingProfile::Builder builder; | 118 TestingProfile::Builder builder; |
| 116 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), | 119 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), |
| 117 FakeSigninManager::Build); | 120 FakeSigninManager::Build); |
| 118 profile_ = builder.Build(); | 121 profile_ = builder.Build(); |
| 122 } | |
| 119 | 123 |
| 124 void GCMProfileServiceTest::TearDown() { | |
| 125 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID); | |
| 126 } | |
| 127 | |
| 128 void GCMProfileServiceTest::CreateGCMProfileService() { | |
| 120 gcm_profile_service_ = static_cast<GCMProfileService*>( | 129 gcm_profile_service_ = static_cast<GCMProfileService*>( |
| 121 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 130 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 122 profile_.get(), | 131 profile_.get(), |
| 123 &BuildGCMProfileService)); | 132 &BuildGCMProfileService)); |
| 124 gcm_profile_service_->driver()->AddAppHandler( | 133 gcm_profile_service_->driver()->AddAppHandler( |
| 125 kTestAppID, gcm_app_handler_.get()); | 134 kTestAppID, gcm_app_handler_.get()); |
| 135 } | |
| 126 | 136 |
| 137 void GCMProfileServiceTest::SignIn() { | |
| 127 FakeSigninManager* signin_manager = static_cast<FakeSigninManager*>( | 138 FakeSigninManager* signin_manager = static_cast<FakeSigninManager*>( |
| 128 SigninManagerFactory::GetInstance()->GetForProfile(profile_.get())); | 139 SigninManagerFactory::GetInstance()->GetForProfile(profile_.get())); |
| 129 signin_manager->SignIn(kTestAccountID); | 140 signin_manager->SignIn(kTestAccountID); |
| 130 base::RunLoop().RunUntilIdle(); | 141 base::RunLoop().RunUntilIdle(); |
| 131 } | 142 } |
| 132 | 143 |
| 133 void GCMProfileServiceTest::TearDown() { | |
| 134 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID); | |
| 135 } | |
| 136 | |
| 137 void GCMProfileServiceTest::RegisterAndWaitForCompletion( | 144 void GCMProfileServiceTest::RegisterAndWaitForCompletion( |
| 138 const std::vector<std::string>& sender_ids) { | 145 const std::vector<std::string>& sender_ids) { |
| 139 base::RunLoop run_loop; | 146 base::RunLoop run_loop; |
| 140 gcm_profile_service_->driver()->Register( | 147 gcm_profile_service_->driver()->Register( |
| 141 kTestAppID, | 148 kTestAppID, |
| 142 sender_ids, | 149 sender_ids, |
| 143 base::Bind(&GCMProfileServiceTest::RegisterCompleted, | 150 base::Bind(&GCMProfileServiceTest::RegisterCompleted, |
| 144 base::Unretained(this), | 151 base::Unretained(this), |
| 145 run_loop.QuitClosure())); | 152 run_loop.QuitClosure())); |
| 146 run_loop.Run(); | 153 run_loop.Run(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 | 194 |
| 188 void GCMProfileServiceTest::SendCompleted( | 195 void GCMProfileServiceTest::SendCompleted( |
| 189 const base::Closure& callback, | 196 const base::Closure& callback, |
| 190 const std::string& message_id, | 197 const std::string& message_id, |
| 191 GCMClient::Result result) { | 198 GCMClient::Result result) { |
| 192 send_message_id_ = message_id; | 199 send_message_id_ = message_id; |
| 193 send_result_ = result; | 200 send_result_ = result; |
| 194 callback.Run(); | 201 callback.Run(); |
| 195 } | 202 } |
| 196 | 203 |
| 204 TEST_F(GCMProfileServiceTest, CreateGCMProfileServiceBeforeSignIn) { | |
| 205 CreateGCMProfileService(); | |
| 206 EXPECT_FALSE(driver()->IsStarted()); | |
| 207 | |
| 208 SignIn(); | |
| 209 EXPECT_TRUE(driver()->IsStarted()); | |
| 210 } | |
| 211 | |
| 212 TEST_F(GCMProfileServiceTest, CreateGCMProfileServiceAfterSignIn) { | |
| 213 SignIn(); | |
| 214 // Note that we can't check if the GCM is started or not since the | |
|
bartfab (slow)
2014/06/13 11:27:21
Nit: s/the GCM/GCM/
jianli
2014/06/13 18:04:48
Done.
| |
| 215 // GCMProfileService that hosts the GCMDriver is not created yet. | |
| 216 | |
| 217 CreateGCMProfileService(); | |
| 218 EXPECT_TRUE(driver()->IsStarted()); | |
| 219 } | |
| 220 | |
| 197 TEST_F(GCMProfileServiceTest, RegisterAndUnregister) { | 221 TEST_F(GCMProfileServiceTest, RegisterAndUnregister) { |
| 222 CreateGCMProfileService(); | |
| 223 SignIn(); | |
| 224 | |
| 198 std::vector<std::string> sender_ids; | 225 std::vector<std::string> sender_ids; |
| 199 sender_ids.push_back("sender"); | 226 sender_ids.push_back("sender"); |
| 200 RegisterAndWaitForCompletion(sender_ids); | 227 RegisterAndWaitForCompletion(sender_ids); |
| 201 | 228 |
| 202 std::string expected_registration_id = | 229 std::string expected_registration_id = |
| 203 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); | 230 FakeGCMClient::GetRegistrationIdFromSenderIds(sender_ids); |
| 204 EXPECT_EQ(expected_registration_id, registration_id()); | 231 EXPECT_EQ(expected_registration_id, registration_id()); |
| 205 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | 232 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); |
| 206 | 233 |
| 207 UnregisterAndWaitForCompletion(); | 234 UnregisterAndWaitForCompletion(); |
| 208 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); | 235 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); |
| 209 } | 236 } |
| 210 | 237 |
| 211 TEST_F(GCMProfileServiceTest, Send) { | 238 TEST_F(GCMProfileServiceTest, Send) { |
| 239 CreateGCMProfileService(); | |
| 240 SignIn(); | |
| 241 | |
| 212 GCMClient::OutgoingMessage message; | 242 GCMClient::OutgoingMessage message; |
| 213 message.id = "1"; | 243 message.id = "1"; |
| 214 message.data["key1"] = "value1"; | 244 message.data["key1"] = "value1"; |
| 215 SendAndWaitForCompletion( message); | 245 SendAndWaitForCompletion( message); |
| 216 | 246 |
| 217 EXPECT_EQ(message.id, send_message_id()); | 247 EXPECT_EQ(message.id, send_message_id()); |
| 218 EXPECT_EQ(GCMClient::SUCCESS, send_result()); | 248 EXPECT_EQ(GCMClient::SUCCESS, send_result()); |
| 219 } | 249 } |
| 220 | 250 |
| 221 } // namespace gcm | 251 } // namespace gcm |
| OLD | NEW |