| 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 "components/gcm_driver/gcm_driver_desktop.h" | 5 #include "components/gcm_driver/gcm_driver_desktop.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/test/test_simple_task_runner.h" | 15 #include "base/test/test_simple_task_runner.h" |
| 16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 17 #include "components/gcm_driver/fake_gcm_app_handler.h" | 17 #include "components/gcm_driver/fake_gcm_app_handler.h" |
| 18 #include "components/gcm_driver/fake_gcm_client.h" | 18 #include "components/gcm_driver/fake_gcm_client.h" |
| 19 #include "components/gcm_driver/fake_gcm_client_factory.h" | 19 #include "components/gcm_driver/fake_gcm_client_factory.h" |
| 20 #include "components/gcm_driver/gcm_app_handler.h" | 20 #include "components/gcm_driver/gcm_app_handler.h" |
| 21 #include "components/gcm_driver/gcm_client_factory.h" | 21 #include "components/gcm_driver/gcm_client_factory.h" |
| 22 #include "google_apis/gaia/fake_identity_provider.h" | |
| 23 #include "google_apis/gaia/fake_oauth2_token_service.h" | |
| 24 #include "net/url_request/url_request_context_getter.h" | 22 #include "net/url_request/url_request_context_getter.h" |
| 25 #include "net/url_request/url_request_test_util.h" | 23 #include "net/url_request/url_request_test_util.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 25 |
| 28 namespace gcm { | 26 namespace gcm { |
| 29 | 27 |
| 30 namespace { | 28 namespace { |
| 31 | 29 |
| 32 const char kTestAccountID1[] = "user1@example.com"; | 30 const char kTestAccountID1[] = "user1@example.com"; |
| 33 const char kTestAccountID2[] = "user2@example.com"; | 31 const char kTestAccountID2[] = "user2@example.com"; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 100 |
| 103 void WaitForAsyncOperation(); | 101 void WaitForAsyncOperation(); |
| 104 | 102 |
| 105 private: | 103 private: |
| 106 void RegisterCompleted(const std::string& registration_id, | 104 void RegisterCompleted(const std::string& registration_id, |
| 107 GCMClient::Result result); | 105 GCMClient::Result result); |
| 108 void SendCompleted(const std::string& message_id, GCMClient::Result result); | 106 void SendCompleted(const std::string& message_id, GCMClient::Result result); |
| 109 void UnregisterCompleted(GCMClient::Result result); | 107 void UnregisterCompleted(GCMClient::Result result); |
| 110 | 108 |
| 111 base::ScopedTempDir temp_dir_; | 109 base::ScopedTempDir temp_dir_; |
| 112 FakeOAuth2TokenService token_service_; | |
| 113 scoped_ptr<FakeIdentityProvider> identity_provider_owner_; | |
| 114 FakeIdentityProvider* identity_provider_; | |
| 115 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 110 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 116 base::MessageLoopForUI message_loop_; | 111 base::MessageLoopForUI message_loop_; |
| 117 base::Thread io_thread_; | 112 base::Thread io_thread_; |
| 118 scoped_ptr<GCMDriver> driver_; | 113 scoped_ptr<GCMDriver> driver_; |
| 119 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; | 114 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; |
| 120 | 115 |
| 121 base::Closure async_operation_completed_callback_; | 116 base::Closure async_operation_completed_callback_; |
| 122 | 117 |
| 123 std::string registration_id_; | 118 std::string registration_id_; |
| 124 GCMClient::Result registration_result_; | 119 GCMClient::Result registration_result_; |
| 125 std::string send_message_id_; | 120 std::string send_message_id_; |
| 126 GCMClient::Result send_result_; | 121 GCMClient::Result send_result_; |
| 127 GCMClient::Result unregistration_result_; | 122 GCMClient::Result unregistration_result_; |
| 128 | 123 |
| 129 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); | 124 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); |
| 130 }; | 125 }; |
| 131 | 126 |
| 132 GCMDriverTest::GCMDriverTest() | 127 GCMDriverTest::GCMDriverTest() |
| 133 : identity_provider_(NULL), | 128 : task_runner_(new base::TestSimpleTaskRunner()), |
| 134 task_runner_(new base::TestSimpleTaskRunner()), | |
| 135 io_thread_("IOThread"), | 129 io_thread_("IOThread"), |
| 136 registration_result_(GCMClient::UNKNOWN_ERROR), | 130 registration_result_(GCMClient::UNKNOWN_ERROR), |
| 137 send_result_(GCMClient::UNKNOWN_ERROR), | 131 send_result_(GCMClient::UNKNOWN_ERROR), |
| 138 unregistration_result_(GCMClient::UNKNOWN_ERROR) { | 132 unregistration_result_(GCMClient::UNKNOWN_ERROR) { |
| 139 identity_provider_owner_.reset(new FakeIdentityProvider(&token_service_)); | |
| 140 identity_provider_ = identity_provider_owner_.get(); | |
| 141 } | 133 } |
| 142 | 134 |
| 143 GCMDriverTest::~GCMDriverTest() { | 135 GCMDriverTest::~GCMDriverTest() { |
| 144 } | 136 } |
| 145 | 137 |
| 146 void GCMDriverTest::SetUp() { | 138 void GCMDriverTest::SetUp() { |
| 147 io_thread_.Start(); | 139 io_thread_.Start(); |
| 148 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 140 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 149 } | 141 } |
| 150 | 142 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 void GCMDriverTest::CreateDriver( | 181 void GCMDriverTest::CreateDriver( |
| 190 FakeGCMClient::StartMode gcm_client_start_mode) { | 182 FakeGCMClient::StartMode gcm_client_start_mode) { |
| 191 scoped_refptr<net::URLRequestContextGetter> request_context = | 183 scoped_refptr<net::URLRequestContextGetter> request_context = |
| 192 new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy()); | 184 new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy()); |
| 193 // TODO(johnme): Need equivalent test coverage of GCMDriverAndroid. | 185 // TODO(johnme): Need equivalent test coverage of GCMDriverAndroid. |
| 194 driver_.reset(new GCMDriverDesktop( | 186 driver_.reset(new GCMDriverDesktop( |
| 195 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory( | 187 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory( |
| 196 gcm_client_start_mode, | 188 gcm_client_start_mode, |
| 197 base::MessageLoopProxy::current(), | 189 base::MessageLoopProxy::current(), |
| 198 io_thread_.message_loop_proxy())).Pass(), | 190 io_thread_.message_loop_proxy())).Pass(), |
| 199 identity_provider_owner_.PassAs<IdentityProvider>(), | |
| 200 GCMClient::ChromeBuildInfo(), | 191 GCMClient::ChromeBuildInfo(), |
| 201 temp_dir_.path(), | 192 temp_dir_.path(), |
| 202 request_context, | 193 request_context, |
| 203 base::MessageLoopProxy::current(), | 194 base::MessageLoopProxy::current(), |
| 204 io_thread_.message_loop_proxy(), | 195 io_thread_.message_loop_proxy(), |
| 205 task_runner_)); | 196 task_runner_)); |
| 206 | 197 |
| 207 gcm_app_handler_.reset(new FakeGCMAppHandler); | 198 gcm_app_handler_.reset(new FakeGCMAppHandler); |
| 208 } | 199 } |
| 209 | 200 |
| 210 void GCMDriverTest::AddAppHandlers() { | 201 void GCMDriverTest::AddAppHandlers() { |
| 211 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); | 202 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); |
| 212 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); | 203 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); |
| 213 } | 204 } |
| 214 | 205 |
| 215 void GCMDriverTest::RemoveAppHandlers() { | 206 void GCMDriverTest::RemoveAppHandlers() { |
| 216 driver_->RemoveAppHandler(kTestAppID1); | 207 driver_->RemoveAppHandler(kTestAppID1); |
| 217 driver_->RemoveAppHandler(kTestAppID2); | 208 driver_->RemoveAppHandler(kTestAppID2); |
| 218 } | 209 } |
| 219 | 210 |
| 220 void GCMDriverTest::SignIn(const std::string& account_id) { | 211 void GCMDriverTest::SignIn(const std::string& account_id) { |
| 221 token_service_.AddAccount(account_id); | 212 driver_->OnSignedIn(); |
| 222 identity_provider_->LogIn(account_id); | |
| 223 PumpIOLoop(); | 213 PumpIOLoop(); |
| 224 PumpUILoop(); | 214 PumpUILoop(); |
| 225 } | 215 } |
| 226 | 216 |
| 227 void GCMDriverTest::SignOut() { | 217 void GCMDriverTest::SignOut() { |
| 228 identity_provider_->LogOut(); | 218 driver_->Purge(); |
| 229 PumpIOLoop(); | 219 PumpIOLoop(); |
| 230 PumpUILoop(); | 220 PumpUILoop(); |
| 231 } | 221 } |
| 232 | 222 |
| 233 void GCMDriverTest::Register(const std::string& app_id, | 223 void GCMDriverTest::Register(const std::string& app_id, |
| 234 const std::vector<std::string>& sender_ids, | 224 const std::vector<std::string>& sender_ids, |
| 235 WaitToFinish wait_to_finish) { | 225 WaitToFinish wait_to_finish) { |
| 236 base::RunLoop run_loop; | 226 base::RunLoop run_loop; |
| 237 async_operation_completed_callback_ = run_loop.QuitClosure(); | 227 async_operation_completed_callback_ = run_loop.QuitClosure(); |
| 238 driver_->Register(app_id, | 228 driver_->Register(app_id, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 if (!async_operation_completed_callback_.is_null()) | 280 if (!async_operation_completed_callback_.is_null()) |
| 291 async_operation_completed_callback_.Run(); | 281 async_operation_completed_callback_.Run(); |
| 292 } | 282 } |
| 293 | 283 |
| 294 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) { | 284 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) { |
| 295 unregistration_result_ = result; | 285 unregistration_result_ = result; |
| 296 if (!async_operation_completed_callback_.is_null()) | 286 if (!async_operation_completed_callback_.is_null()) |
| 297 async_operation_completed_callback_.Run(); | 287 async_operation_completed_callback_.Run(); |
| 298 } | 288 } |
| 299 | 289 |
| 300 TEST_F(GCMDriverTest, CreateGCMDriverBeforeSignIn) { | 290 TEST_F(GCMDriverTest, Create) { |
| 301 // Create GCMDriver first. GCM is not started. | 291 // Create GCMDriver first. GCM is not started. |
| 302 CreateDriver(FakeGCMClient::NO_DELAY_START); | 292 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 303 EXPECT_FALSE(driver()->IsStarted()); | 293 EXPECT_FALSE(driver()->IsStarted()); |
| 304 | 294 |
| 305 // Sign in. GCM is still not started. | 295 // Sign in. GCM is still not started. |
| 306 SignIn(kTestAccountID1); | 296 SignIn(kTestAccountID1); |
| 307 EXPECT_FALSE(driver()->IsStarted()); | 297 EXPECT_FALSE(driver()->IsStarted()); |
| 308 | 298 |
| 309 // GCM will be started only after both sign-in and app handler being | 299 // GCM will be started only after both sign-in and app handler being |
| 310 AddAppHandlers(); | 300 AddAppHandlers(); |
| 311 EXPECT_TRUE(driver()->IsStarted()); | 301 EXPECT_TRUE(driver()->IsStarted()); |
| 312 } | 302 } |
| 313 | 303 |
| 314 TEST_F(GCMDriverTest, CreateGCMDriverAfterSignIn) { | |
| 315 // Sign in. Nothings happens since GCMDriver is not created. | |
| 316 SignIn(kTestAccountID1); | |
| 317 | |
| 318 // Create GCMDriver after sign-in. GCM is not started. | |
| 319 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
| 320 EXPECT_FALSE(driver()->IsStarted()); | |
| 321 | |
| 322 // GCM will be started only after both sign-in and app handler being | |
| 323 AddAppHandlers(); | |
| 324 EXPECT_TRUE(driver()->IsStarted()); | |
| 325 } | |
| 326 | |
| 327 TEST_F(GCMDriverTest, Shutdown) { | 304 TEST_F(GCMDriverTest, Shutdown) { |
| 328 CreateDriver(FakeGCMClient::NO_DELAY_START); | 305 CreateDriver(FakeGCMClient::NO_DELAY_START); |
| 329 EXPECT_FALSE(HasAppHandlers()); | 306 EXPECT_FALSE(HasAppHandlers()); |
| 330 | 307 |
| 331 AddAppHandlers(); | 308 AddAppHandlers(); |
| 332 EXPECT_TRUE(HasAppHandlers()); | 309 EXPECT_TRUE(HasAppHandlers()); |
| 333 | 310 |
| 334 driver()->Shutdown(); | 311 driver()->Shutdown(); |
| 335 EXPECT_FALSE(HasAppHandlers()); | 312 EXPECT_FALSE(HasAppHandlers()); |
| 336 } | 313 } |
| (...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 892 | 869 |
| 893 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { | 870 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { |
| 894 GetGCMClient()->DeleteMessages(kTestAppID1); | 871 GetGCMClient()->DeleteMessages(kTestAppID1); |
| 895 gcm_app_handler()->WaitForNotification(); | 872 gcm_app_handler()->WaitForNotification(); |
| 896 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, | 873 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, |
| 897 gcm_app_handler()->received_event()); | 874 gcm_app_handler()->received_event()); |
| 898 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); | 875 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); |
| 899 } | 876 } |
| 900 | 877 |
| 901 } // namespace gcm | 878 } // namespace gcm |
| OLD | NEW |