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 EXPECT_FALSE(driver()->IsConnected()); | 298 EXPECT_FALSE(driver()->IsConnected()); |
309 EXPECT_FALSE(gcm_app_handler()->connected()); | 299 EXPECT_FALSE(gcm_app_handler()->connected()); |
310 | 300 |
311 // GCM will be started only after both sign-in and app handler being added. | 301 // GCM will be started only after both sign-in and app handler being added. |
312 AddAppHandlers(); | 302 AddAppHandlers(); |
313 EXPECT_TRUE(driver()->IsStarted()); | 303 EXPECT_TRUE(driver()->IsStarted()); |
314 PumpIOLoop(); | 304 PumpIOLoop(); |
315 EXPECT_TRUE(driver()->IsConnected()); | 305 EXPECT_TRUE(driver()->IsConnected()); |
316 EXPECT_TRUE(gcm_app_handler()->connected()); | 306 EXPECT_TRUE(gcm_app_handler()->connected()); |
317 } | 307 } |
318 | 308 |
319 TEST_F(GCMDriverTest, CreateGCMDriverAfterSignIn) { | |
320 // Sign in. Nothings happens since GCMDriver is not created. | |
321 SignIn(kTestAccountID1); | |
322 | |
323 // Create GCMDriver after sign-in. GCM is not started. | |
324 CreateDriver(FakeGCMClient::NO_DELAY_START); | |
325 EXPECT_FALSE(driver()->IsStarted()); | |
326 EXPECT_FALSE(driver()->IsConnected()); | |
327 EXPECT_FALSE(gcm_app_handler()->connected()); | |
328 | |
329 // GCM will be started only after both sign-in and app handler being added. | |
330 AddAppHandlers(); | |
331 EXPECT_TRUE(driver()->IsStarted()); | |
332 PumpIOLoop(); | |
333 EXPECT_TRUE(driver()->IsConnected()); | |
334 EXPECT_TRUE(gcm_app_handler()->connected()); | |
335 } | |
336 | |
337 TEST_F(GCMDriverTest, Shutdown) { | 309 TEST_F(GCMDriverTest, Shutdown) { |
338 CreateDriver(FakeGCMClient::NO_DELAY_START); | 310 CreateDriver(FakeGCMClient::NO_DELAY_START); |
339 EXPECT_FALSE(HasAppHandlers()); | 311 EXPECT_FALSE(HasAppHandlers()); |
340 | 312 |
341 AddAppHandlers(); | 313 AddAppHandlers(); |
342 EXPECT_TRUE(HasAppHandlers()); | 314 EXPECT_TRUE(HasAppHandlers()); |
343 | 315 |
344 driver()->Shutdown(); | 316 driver()->Shutdown(); |
345 EXPECT_FALSE(HasAppHandlers()); | 317 EXPECT_FALSE(HasAppHandlers()); |
346 EXPECT_FALSE(driver()->IsConnected()); | 318 EXPECT_FALSE(driver()->IsConnected()); |
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
904 | 876 |
905 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { | 877 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { |
906 GetGCMClient()->DeleteMessages(kTestAppID1); | 878 GetGCMClient()->DeleteMessages(kTestAppID1); |
907 gcm_app_handler()->WaitForNotification(); | 879 gcm_app_handler()->WaitForNotification(); |
908 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, | 880 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, |
909 gcm_app_handler()->received_event()); | 881 gcm_app_handler()->received_event()); |
910 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); | 882 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); |
911 } | 883 } |
912 | 884 |
913 } // namespace gcm | 885 } // namespace gcm |
OLD | NEW |