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" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "chrome/browser/services/gcm/fake_gcm_app_handler.h" | |
14 #include "chrome/browser/services/gcm/fake_gcm_client.h" | 15 #include "chrome/browser/services/gcm/fake_gcm_client.h" |
15 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h" | 16 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h" |
16 #include "chrome/browser/services/gcm/fake_signin_manager.h" | 17 #include "chrome/browser/services/gcm/fake_signin_manager.h" |
17 #include "chrome/browser/services/gcm/gcm_driver.h" | 18 #include "chrome/browser/services/gcm/gcm_driver.h" |
18 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 19 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
19 #include "chrome/browser/signin/signin_manager_factory.h" | 20 #include "chrome/browser/signin/signin_manager_factory.h" |
20 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
21 #include "components/gcm_driver/gcm_client_factory.h" | 22 #include "components/gcm_driver/gcm_client_factory.h" |
22 #include "components/pref_registry/pref_registry_syncable.h" | 23 #include "components/pref_registry/pref_registry_syncable.h" |
23 #include "content/public/browser/browser_context.h" | 24 #include "content/public/browser/browser_context.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
42 | 43 |
43 } // namespace | 44 } // namespace |
44 | 45 |
45 class GCMProfileServiceTest : public testing::Test { | 46 class GCMProfileServiceTest : public testing::Test { |
46 protected: | 47 protected: |
47 GCMProfileServiceTest(); | 48 GCMProfileServiceTest(); |
48 virtual ~GCMProfileServiceTest(); | 49 virtual ~GCMProfileServiceTest(); |
49 | 50 |
50 // testing::Test: | 51 // testing::Test: |
51 virtual void SetUp() OVERRIDE; | 52 virtual void SetUp() OVERRIDE; |
53 virtual void TearDown() OVERRIDE; | |
52 | 54 |
53 FakeGCMClient* GetGCMClient() const; | 55 FakeGCMClient* GetGCMClient() const; |
54 | 56 |
55 void RegisterAndWaitForCompletion(const std::vector<std::string>& sender_ids); | 57 void RegisterAndWaitForCompletion(const std::vector<std::string>& sender_ids); |
56 void UnregisterAndWaitForCompletion(); | 58 void UnregisterAndWaitForCompletion(); |
57 void SendAndWaitForCompletion(const GCMClient::OutgoingMessage& message); | 59 void SendAndWaitForCompletion(const GCMClient::OutgoingMessage& message); |
58 | 60 |
59 void RegisterCompleted(const base::Closure& callback, | 61 void RegisterCompleted(const base::Closure& callback, |
60 const std::string& registration_id, | 62 const std::string& registration_id, |
61 GCMClient::Result result); | 63 GCMClient::Result result); |
62 void UnregisterCompleted(const base::Closure& callback, | 64 void UnregisterCompleted(const base::Closure& callback, |
63 GCMClient::Result result); | 65 GCMClient::Result result); |
64 void SendCompleted(const base::Closure& callback, | 66 void SendCompleted(const base::Closure& callback, |
65 const std::string& message_id, | 67 const std::string& message_id, |
66 GCMClient::Result result); | 68 GCMClient::Result result); |
67 | 69 |
68 GCMDriver* driver() const { return gcm_profile_service_->driver(); } | 70 GCMDriver* driver() const { return gcm_profile_service_->driver(); } |
69 std::string registration_id() const { return registration_id_; } | 71 std::string registration_id() const { return registration_id_; } |
70 GCMClient::Result registration_result() const { return registration_result_; } | 72 GCMClient::Result registration_result() const { return registration_result_; } |
71 GCMClient::Result unregistration_result() const { | 73 GCMClient::Result unregistration_result() const { |
72 return unregistration_result_; | 74 return unregistration_result_; |
73 } | 75 } |
74 std::string send_message_id() const { return send_message_id_; } | 76 std::string send_message_id() const { return send_message_id_; } |
75 GCMClient::Result send_result() const { return send_result_; } | 77 GCMClient::Result send_result() const { return send_result_; } |
76 | 78 |
77 private: | 79 private: |
78 content::TestBrowserThreadBundle thread_bundle_; | 80 content::TestBrowserThreadBundle thread_bundle_; |
79 scoped_ptr<TestingProfile> profile_; | 81 scoped_ptr<TestingProfile> profile_; |
80 GCMProfileService* gcm_profile_service_; | 82 GCMProfileService* gcm_profile_service_; |
83 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; | |
81 | 84 |
82 std::string registration_id_; | 85 std::string registration_id_; |
83 GCMClient::Result registration_result_; | 86 GCMClient::Result registration_result_; |
84 GCMClient::Result unregistration_result_; | 87 GCMClient::Result unregistration_result_; |
85 std::string send_message_id_; | 88 std::string send_message_id_; |
86 GCMClient::Result send_result_; | 89 GCMClient::Result send_result_; |
87 | 90 |
88 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest); | 91 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest); |
89 }; | 92 }; |
90 | 93 |
91 GCMProfileServiceTest::GCMProfileServiceTest() | 94 GCMProfileServiceTest::GCMProfileServiceTest() |
92 : gcm_profile_service_(NULL), | 95 : gcm_profile_service_(NULL), |
96 gcm_app_handler_(new FakeGCMAppHandler), | |
93 registration_result_(GCMClient::UNKNOWN_ERROR), | 97 registration_result_(GCMClient::UNKNOWN_ERROR), |
94 send_result_(GCMClient::UNKNOWN_ERROR) { | 98 send_result_(GCMClient::UNKNOWN_ERROR) { |
95 } | 99 } |
96 | 100 |
97 GCMProfileServiceTest::~GCMProfileServiceTest() { | 101 GCMProfileServiceTest::~GCMProfileServiceTest() { |
98 } | 102 } |
99 | 103 |
100 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const { | 104 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const { |
101 return static_cast<FakeGCMClient*>( | 105 return static_cast<FakeGCMClient*>( |
102 gcm_profile_service_->driver()->GetGCMClientForTesting()); | 106 gcm_profile_service_->driver()->GetGCMClientForTesting()); |
103 } | 107 } |
104 | 108 |
105 void GCMProfileServiceTest::SetUp() { | 109 void GCMProfileServiceTest::SetUp() { |
106 TestingProfile::Builder builder; | 110 TestingProfile::Builder builder; |
107 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), | 111 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), |
108 FakeSigninManager::Build); | 112 FakeSigninManager::Build); |
109 profile_ = builder.Build(); | 113 profile_ = builder.Build(); |
110 | 114 |
111 gcm_profile_service_ = static_cast<GCMProfileService*>( | 115 gcm_profile_service_ = static_cast<GCMProfileService*>( |
112 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 116 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
113 profile_.get(), | 117 profile_.get(), |
114 &BuildGCMProfileService)); | 118 &BuildGCMProfileService)); |
119 gcm_profile_service_->driver()->AddAppHandler( | |
120 kTestAppID, gcm_app_handler_.get()); | |
115 | 121 |
116 FakeSigninManager* signin_manager = static_cast<FakeSigninManager*>( | 122 FakeSigninManager* signin_manager = static_cast<FakeSigninManager*>( |
117 SigninManagerFactory::GetInstance()->GetForProfile(profile_.get())); | 123 SigninManagerFactory::GetInstance()->GetForProfile(profile_.get())); |
118 signin_manager->SignIn(kTestAccountID); | 124 signin_manager->SignIn(kTestAccountID); |
119 base::RunLoop().RunUntilIdle(); | 125 base::RunLoop().RunUntilIdle(); |
120 } | 126 } |
121 | 127 |
128 void GCMProfileServiceTest::TearDown() { | |
129 profile_.reset(); | |
Nicolas Zea
2014/05/22 17:06:31
why is this necessary? profile_ is going to be des
jianli
2014/05/22 18:09:26
Yes, it is not needed. I forgot to remove app hand
| |
130 } | |
131 | |
122 void GCMProfileServiceTest::RegisterAndWaitForCompletion( | 132 void GCMProfileServiceTest::RegisterAndWaitForCompletion( |
123 const std::vector<std::string>& sender_ids) { | 133 const std::vector<std::string>& sender_ids) { |
124 base::RunLoop run_loop; | 134 base::RunLoop run_loop; |
125 gcm_profile_service_->driver()->Register( | 135 gcm_profile_service_->driver()->Register( |
126 kTestAppID, | 136 kTestAppID, |
127 sender_ids, | 137 sender_ids, |
128 base::Bind(&GCMProfileServiceTest::RegisterCompleted, | 138 base::Bind(&GCMProfileServiceTest::RegisterCompleted, |
129 base::Unretained(this), | 139 base::Unretained(this), |
130 run_loop.QuitClosure())); | 140 run_loop.QuitClosure())); |
131 run_loop.Run(); | 141 run_loop.Run(); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 GCMClient::OutgoingMessage message; | 207 GCMClient::OutgoingMessage message; |
198 message.id = "1"; | 208 message.id = "1"; |
199 message.data["key1"] = "value1"; | 209 message.data["key1"] = "value1"; |
200 SendAndWaitForCompletion( message); | 210 SendAndWaitForCompletion( message); |
201 | 211 |
202 EXPECT_EQ(message.id, send_message_id()); | 212 EXPECT_EQ(message.id, send_message_id()); |
203 EXPECT_EQ(GCMClient::SUCCESS, send_result()); | 213 EXPECT_EQ(GCMClient::SUCCESS, send_result()); |
204 } | 214 } |
205 | 215 |
206 } // namespace gcm | 216 } // namespace gcm |
OLD | NEW |