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/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
17 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
18 #include "components/gcm_driver/fake_gcm_app_handler.h" | 18 #include "components/gcm_driver/fake_gcm_app_handler.h" |
19 #include "components/gcm_driver/fake_gcm_client.h" | 19 #include "components/gcm_driver/fake_gcm_client.h" |
20 #include "components/gcm_driver/fake_gcm_client_factory.h" | 20 #include "components/gcm_driver/fake_gcm_client_factory.h" |
21 #include "components/gcm_driver/gcm_app_handler.h" | 21 #include "components/gcm_driver/gcm_app_handler.h" |
22 #include "components/gcm_driver/gcm_client_factory.h" | 22 #include "components/gcm_driver/gcm_client_factory.h" |
| 23 #include "components/gcm_driver/gcm_connection_observer.h" |
23 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
24 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
27 namespace gcm { | 28 namespace gcm { |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 const char kTestAccountID1[] = "user1@example.com"; | 32 const char kTestAccountID1[] = "user1@example.com"; |
32 const char kTestAccountID2[] = "user2@example.com"; | 33 const char kTestAccountID2[] = "user2@example.com"; |
33 const char kTestAppID1[] = "TestApp1"; | 34 const char kTestAppID1[] = "TestApp1"; |
34 const char kTestAppID2[] = "TestApp2"; | 35 const char kTestAppID2[] = "TestApp2"; |
35 const char kUserID1[] = "user1"; | 36 const char kUserID1[] = "user1"; |
36 | 37 |
| 38 class FakeGCMConnectionObserver : public GCMConnectionObserver { |
| 39 public: |
| 40 FakeGCMConnectionObserver(); |
| 41 virtual ~FakeGCMConnectionObserver(); |
| 42 |
| 43 // gcm::GCMConnectionObserver implementation: |
| 44 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) OVERRIDE; |
| 45 virtual void OnDisconnected() OVERRIDE; |
| 46 |
| 47 bool connected() const { return connected_; } |
| 48 |
| 49 private: |
| 50 bool connected_; |
| 51 }; |
| 52 |
| 53 FakeGCMConnectionObserver::FakeGCMConnectionObserver() : connected_(false) { |
| 54 } |
| 55 |
| 56 FakeGCMConnectionObserver::~FakeGCMConnectionObserver() { |
| 57 } |
| 58 |
| 59 void FakeGCMConnectionObserver::OnConnected( |
| 60 const net::IPEndPoint& ip_endpoint) { |
| 61 connected_ = true; |
| 62 } |
| 63 |
| 64 void FakeGCMConnectionObserver::OnDisconnected() { |
| 65 connected_ = false; |
| 66 } |
| 67 |
37 void PumpCurrentLoop() { | 68 void PumpCurrentLoop() { |
38 base::MessageLoop::ScopedNestableTaskAllower | 69 base::MessageLoop::ScopedNestableTaskAllower |
39 nestable_task_allower(base::MessageLoop::current()); | 70 nestable_task_allower(base::MessageLoop::current()); |
40 base::RunLoop().RunUntilIdle(); | 71 base::RunLoop().RunUntilIdle(); |
41 } | 72 } |
42 | 73 |
43 void PumpUILoop() { | 74 void PumpUILoop() { |
44 PumpCurrentLoop(); | 75 PumpCurrentLoop(); |
45 } | 76 } |
46 | 77 |
(...skipping 14 matching lines...) Expand all Loading... |
61 | 92 |
62 GCMDriverTest(); | 93 GCMDriverTest(); |
63 virtual ~GCMDriverTest(); | 94 virtual ~GCMDriverTest(); |
64 | 95 |
65 // testing::Test: | 96 // testing::Test: |
66 virtual void SetUp() OVERRIDE; | 97 virtual void SetUp() OVERRIDE; |
67 virtual void TearDown() OVERRIDE; | 98 virtual void TearDown() OVERRIDE; |
68 | 99 |
69 GCMDriver* driver() { return driver_.get(); } | 100 GCMDriver* driver() { return driver_.get(); } |
70 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); } | 101 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); } |
| 102 FakeGCMConnectionObserver* gcm_connection_observer() { |
| 103 return gcm_connection_observer_.get(); |
| 104 } |
71 const std::string& registration_id() const { return registration_id_; } | 105 const std::string& registration_id() const { return registration_id_; } |
72 GCMClient::Result registration_result() const { return registration_result_; } | 106 GCMClient::Result registration_result() const { return registration_result_; } |
73 const std::string& send_message_id() const { return send_message_id_; } | 107 const std::string& send_message_id() const { return send_message_id_; } |
74 GCMClient::Result send_result() const { return send_result_; } | 108 GCMClient::Result send_result() const { return send_result_; } |
75 GCMClient::Result unregistration_result() const { | 109 GCMClient::Result unregistration_result() const { |
76 return unregistration_result_; | 110 return unregistration_result_; |
77 } | 111 } |
78 | 112 |
79 void PumpIOLoop(); | 113 void PumpIOLoop(); |
80 | 114 |
(...skipping 26 matching lines...) Expand all Loading... |
107 void SendCompleted(const std::string& message_id, GCMClient::Result result); | 141 void SendCompleted(const std::string& message_id, GCMClient::Result result); |
108 void UnregisterCompleted(GCMClient::Result result); | 142 void UnregisterCompleted(GCMClient::Result result); |
109 | 143 |
110 base::ScopedTempDir temp_dir_; | 144 base::ScopedTempDir temp_dir_; |
111 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 145 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
112 base::MessageLoopForUI message_loop_; | 146 base::MessageLoopForUI message_loop_; |
113 base::Thread io_thread_; | 147 base::Thread io_thread_; |
114 base::FieldTrialList field_trial_list_; | 148 base::FieldTrialList field_trial_list_; |
115 scoped_ptr<GCMDriver> driver_; | 149 scoped_ptr<GCMDriver> driver_; |
116 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; | 150 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; |
| 151 scoped_ptr<FakeGCMConnectionObserver> gcm_connection_observer_; |
117 | 152 |
118 base::Closure async_operation_completed_callback_; | 153 base::Closure async_operation_completed_callback_; |
119 | 154 |
120 std::string registration_id_; | 155 std::string registration_id_; |
121 GCMClient::Result registration_result_; | 156 GCMClient::Result registration_result_; |
122 std::string send_message_id_; | 157 std::string send_message_id_; |
123 GCMClient::Result send_result_; | 158 GCMClient::Result send_result_; |
124 GCMClient::Result unregistration_result_; | 159 GCMClient::Result unregistration_result_; |
125 | 160 |
126 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); | 161 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); |
(...skipping 13 matching lines...) Expand all Loading... |
140 | 175 |
141 void GCMDriverTest::SetUp() { | 176 void GCMDriverTest::SetUp() { |
142 io_thread_.Start(); | 177 io_thread_.Start(); |
143 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 178 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
144 } | 179 } |
145 | 180 |
146 void GCMDriverTest::TearDown() { | 181 void GCMDriverTest::TearDown() { |
147 if (!driver_) | 182 if (!driver_) |
148 return; | 183 return; |
149 | 184 |
| 185 if (gcm_connection_observer_.get()) |
| 186 driver_->RemoveConnectionObserver(gcm_connection_observer_.get()); |
150 driver_->Shutdown(); | 187 driver_->Shutdown(); |
151 driver_.reset(); | 188 driver_.reset(); |
152 PumpIOLoop(); | 189 PumpIOLoop(); |
153 | 190 |
154 io_thread_.Stop(); | 191 io_thread_.Stop(); |
155 } | 192 } |
156 | 193 |
157 void GCMDriverTest::PumpIOLoop() { | 194 void GCMDriverTest::PumpIOLoop() { |
158 base::RunLoop run_loop; | 195 base::RunLoop run_loop; |
159 io_thread_.message_loop_proxy()->PostTaskAndReply( | 196 io_thread_.message_loop_proxy()->PostTaskAndReply( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 base::MessageLoopProxy::current(), | 229 base::MessageLoopProxy::current(), |
193 io_thread_.message_loop_proxy())).Pass(), | 230 io_thread_.message_loop_proxy())).Pass(), |
194 GCMClient::ChromeBuildInfo(), | 231 GCMClient::ChromeBuildInfo(), |
195 temp_dir_.path(), | 232 temp_dir_.path(), |
196 request_context, | 233 request_context, |
197 base::MessageLoopProxy::current(), | 234 base::MessageLoopProxy::current(), |
198 io_thread_.message_loop_proxy(), | 235 io_thread_.message_loop_proxy(), |
199 task_runner_)); | 236 task_runner_)); |
200 | 237 |
201 gcm_app_handler_.reset(new FakeGCMAppHandler); | 238 gcm_app_handler_.reset(new FakeGCMAppHandler); |
| 239 gcm_connection_observer_.reset(new FakeGCMConnectionObserver); |
| 240 |
| 241 driver_->AddConnectionObserver(gcm_connection_observer_.get()); |
202 } | 242 } |
203 | 243 |
204 void GCMDriverTest::AddAppHandlers() { | 244 void GCMDriverTest::AddAppHandlers() { |
205 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); | 245 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); |
206 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); | 246 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); |
207 } | 247 } |
208 | 248 |
209 void GCMDriverTest::RemoveAppHandlers() { | 249 void GCMDriverTest::RemoveAppHandlers() { |
210 driver_->RemoveAppHandler(kTestAppID1); | 250 driver_->RemoveAppHandler(kTestAppID1); |
211 driver_->RemoveAppHandler(kTestAppID2); | 251 driver_->RemoveAppHandler(kTestAppID2); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 | 332 |
293 TEST_F(GCMDriverTest, Create) { | 333 TEST_F(GCMDriverTest, Create) { |
294 // Create GCMDriver first. GCM is not started. | 334 // Create GCMDriver first. GCM is not started. |
295 CreateDriver(FakeGCMClient::NO_DELAY_START); | 335 CreateDriver(FakeGCMClient::NO_DELAY_START); |
296 EXPECT_FALSE(driver()->IsStarted()); | 336 EXPECT_FALSE(driver()->IsStarted()); |
297 | 337 |
298 // Sign in. GCM is still not started. | 338 // Sign in. GCM is still not started. |
299 SignIn(kTestAccountID1); | 339 SignIn(kTestAccountID1); |
300 EXPECT_FALSE(driver()->IsStarted()); | 340 EXPECT_FALSE(driver()->IsStarted()); |
301 EXPECT_FALSE(driver()->IsConnected()); | 341 EXPECT_FALSE(driver()->IsConnected()); |
302 EXPECT_FALSE(gcm_app_handler()->connected()); | 342 EXPECT_FALSE(gcm_connection_observer()->connected()); |
303 | 343 |
304 // GCM will be started only after both sign-in and app handler being added. | 344 // GCM will be started only after both sign-in and app handler being added. |
305 AddAppHandlers(); | 345 AddAppHandlers(); |
306 EXPECT_TRUE(driver()->IsStarted()); | 346 EXPECT_TRUE(driver()->IsStarted()); |
307 PumpIOLoop(); | 347 PumpIOLoop(); |
308 EXPECT_TRUE(driver()->IsConnected()); | 348 EXPECT_TRUE(driver()->IsConnected()); |
309 EXPECT_TRUE(gcm_app_handler()->connected()); | 349 EXPECT_TRUE(gcm_connection_observer()->connected()); |
310 } | 350 } |
311 | 351 |
312 TEST_F(GCMDriverTest, CreateByFieldTrial) { | 352 TEST_F(GCMDriverTest, CreateByFieldTrial) { |
313 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("GCM", "Enabled")); | 353 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("GCM", "Enabled")); |
314 | 354 |
315 // Create GCMDriver first. GCM is not started. | 355 // Create GCMDriver first. GCM is not started. |
316 CreateDriver(FakeGCMClient::NO_DELAY_START); | 356 CreateDriver(FakeGCMClient::NO_DELAY_START); |
317 EXPECT_FALSE(driver()->IsStarted()); | 357 EXPECT_FALSE(driver()->IsStarted()); |
318 EXPECT_FALSE(driver()->IsConnected()); | 358 EXPECT_FALSE(driver()->IsConnected()); |
319 EXPECT_FALSE(gcm_app_handler()->connected()); | 359 EXPECT_FALSE(gcm_connection_observer()->connected()); |
320 | 360 |
321 // GCM will be started after app handler is added. | 361 // GCM will be started after app handler is added. |
322 AddAppHandlers(); | 362 AddAppHandlers(); |
323 EXPECT_TRUE(driver()->IsStarted()); | 363 EXPECT_TRUE(driver()->IsStarted()); |
324 PumpIOLoop(); | 364 PumpIOLoop(); |
325 EXPECT_TRUE(driver()->IsConnected()); | 365 EXPECT_TRUE(driver()->IsConnected()); |
326 EXPECT_TRUE(gcm_app_handler()->connected()); | 366 EXPECT_TRUE(gcm_connection_observer()->connected()); |
327 } | 367 } |
328 | 368 |
329 TEST_F(GCMDriverTest, Shutdown) { | 369 TEST_F(GCMDriverTest, Shutdown) { |
330 CreateDriver(FakeGCMClient::NO_DELAY_START); | 370 CreateDriver(FakeGCMClient::NO_DELAY_START); |
331 EXPECT_FALSE(HasAppHandlers()); | 371 EXPECT_FALSE(HasAppHandlers()); |
332 | 372 |
333 AddAppHandlers(); | 373 AddAppHandlers(); |
334 EXPECT_TRUE(HasAppHandlers()); | 374 EXPECT_TRUE(HasAppHandlers()); |
335 | 375 |
336 driver()->Shutdown(); | 376 driver()->Shutdown(); |
337 EXPECT_FALSE(HasAppHandlers()); | 377 EXPECT_FALSE(HasAppHandlers()); |
338 EXPECT_FALSE(driver()->IsConnected()); | 378 EXPECT_FALSE(driver()->IsConnected()); |
339 EXPECT_FALSE(gcm_app_handler()->connected()); | 379 EXPECT_FALSE(gcm_connection_observer()->connected()); |
340 } | 380 } |
341 | 381 |
342 TEST_F(GCMDriverTest, SignInAndSignOutOnGCMEnabled) { | 382 TEST_F(GCMDriverTest, SignInAndSignOutOnGCMEnabled) { |
343 // By default, GCM is enabled. | 383 // By default, GCM is enabled. |
344 CreateDriver(FakeGCMClient::NO_DELAY_START); | 384 CreateDriver(FakeGCMClient::NO_DELAY_START); |
345 AddAppHandlers(); | 385 AddAppHandlers(); |
346 | 386 |
347 // GCMClient should be started after sign-in. | 387 // GCMClient should be started after sign-in. |
348 SignIn(kTestAccountID1); | 388 SignIn(kTestAccountID1); |
349 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); | 389 EXPECT_EQ(FakeGCMClient::STARTED, GetGCMClient()->status()); |
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
882 | 922 |
883 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { | 923 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { |
884 GetGCMClient()->DeleteMessages(kTestAppID1); | 924 GetGCMClient()->DeleteMessages(kTestAppID1); |
885 gcm_app_handler()->WaitForNotification(); | 925 gcm_app_handler()->WaitForNotification(); |
886 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, | 926 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, |
887 gcm_app_handler()->received_event()); | 927 gcm_app_handler()->received_event()); |
888 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); | 928 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); |
889 } | 929 } |
890 | 930 |
891 } // namespace gcm | 931 } // namespace gcm |
OLD | NEW |