Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: components/gcm_driver/gcm_driver_desktop_unittest.cc

Issue 330733002: Move IdentityProvider usage from GCMDriverDesktop to GCMProfileService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Patch Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 if (!driver_)
bartfab (slow) 2014/06/13 11:27:21 Nit: Why is this necessary? I do not see a single
jianli 2014/06/13 18:04:49 You're right. It is not needed. Previously I added
222 identity_provider_->LogIn(account_id); 213 return;
214 driver_->SignIn();
223 PumpIOLoop(); 215 PumpIOLoop();
224 PumpUILoop(); 216 PumpUILoop();
225 } 217 }
226 218
227 void GCMDriverTest::SignOut() { 219 void GCMDriverTest::SignOut() {
228 identity_provider_->LogOut(); 220 if (!driver_)
221 return;
222 driver_->Purge();
229 PumpIOLoop(); 223 PumpIOLoop();
230 PumpUILoop(); 224 PumpUILoop();
231 } 225 }
232 226
233 void GCMDriverTest::Register(const std::string& app_id, 227 void GCMDriverTest::Register(const std::string& app_id,
234 const std::vector<std::string>& sender_ids, 228 const std::vector<std::string>& sender_ids,
235 WaitToFinish wait_to_finish) { 229 WaitToFinish wait_to_finish) {
236 base::RunLoop run_loop; 230 base::RunLoop run_loop;
237 async_operation_completed_callback_ = run_loop.QuitClosure(); 231 async_operation_completed_callback_ = run_loop.QuitClosure();
238 driver_->Register(app_id, 232 driver_->Register(app_id,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 if (!async_operation_completed_callback_.is_null()) 284 if (!async_operation_completed_callback_.is_null())
291 async_operation_completed_callback_.Run(); 285 async_operation_completed_callback_.Run();
292 } 286 }
293 287
294 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) { 288 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) {
295 unregistration_result_ = result; 289 unregistration_result_ = result;
296 if (!async_operation_completed_callback_.is_null()) 290 if (!async_operation_completed_callback_.is_null())
297 async_operation_completed_callback_.Run(); 291 async_operation_completed_callback_.Run();
298 } 292 }
299 293
300 TEST_F(GCMDriverTest, CreateGCMDriverBeforeSignIn) { 294 TEST_F(GCMDriverTest, Create) {
301 // Create GCMDriver first. GCM is not started. 295 // Create GCMDriver first. GCM is not started.
302 CreateDriver(FakeGCMClient::NO_DELAY_START); 296 CreateDriver(FakeGCMClient::NO_DELAY_START);
303 EXPECT_FALSE(driver()->IsStarted()); 297 EXPECT_FALSE(driver()->IsStarted());
304 298
305 // Sign in. GCM is still not started. 299 // Sign in. GCM is still not started.
306 SignIn(kTestAccountID1); 300 SignIn(kTestAccountID1);
307 EXPECT_FALSE(driver()->IsStarted()); 301 EXPECT_FALSE(driver()->IsStarted());
308 302
309 // GCM will be started only after both sign-in and app handler being 303 // GCM will be started only after both sign-in and app handler being
310 AddAppHandlers(); 304 AddAppHandlers();
311 EXPECT_TRUE(driver()->IsStarted()); 305 EXPECT_TRUE(driver()->IsStarted());
312 } 306 }
313 307
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) { 308 TEST_F(GCMDriverTest, Shutdown) {
328 CreateDriver(FakeGCMClient::NO_DELAY_START); 309 CreateDriver(FakeGCMClient::NO_DELAY_START);
329 EXPECT_FALSE(HasAppHandlers()); 310 EXPECT_FALSE(HasAppHandlers());
330 311
331 AddAppHandlers(); 312 AddAppHandlers();
332 EXPECT_TRUE(HasAppHandlers()); 313 EXPECT_TRUE(HasAppHandlers());
333 314
334 driver()->Shutdown(); 315 driver()->Shutdown();
335 EXPECT_FALSE(HasAppHandlers()); 316 EXPECT_FALSE(HasAppHandlers());
336 } 317 }
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 873
893 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { 874 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) {
894 GetGCMClient()->DeleteMessages(kTestAppID1); 875 GetGCMClient()->DeleteMessages(kTestAppID1);
895 gcm_app_handler()->WaitForNotification(); 876 gcm_app_handler()->WaitForNotification();
896 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, 877 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT,
897 gcm_app_handler()->received_event()); 878 gcm_app_handler()->received_event());
898 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); 879 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
899 } 880 }
900 881
901 } // namespace gcm 882 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698