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_profile_service.h" | 5 #include "components/gcm_driver/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/message_loop/message_loop.h" |
| 14 #include "base/prefs/testing_pref_service.h" |
13 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
14 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 16 #include "base/single_thread_task_runner.h" |
15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 17 #include "base/test/test_simple_task_runner.h" |
16 #include "chrome/browser/signin/signin_manager_factory.h" | |
17 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | |
18 #include "chrome/common/channel_info.h" | |
19 #include "chrome/test/base/testing_profile.h" | |
20 #if defined(OS_CHROMEOS) | 18 #if defined(OS_CHROMEOS) |
21 #include "chromeos/dbus/dbus_thread_manager.h" | 19 #include "chromeos/dbus/dbus_thread_manager.h" |
22 #endif | 20 #endif |
23 #include "components/gcm_driver/fake_gcm_app_handler.h" | 21 #include "components/gcm_driver/fake_gcm_app_handler.h" |
24 #include "components/gcm_driver/fake_gcm_client.h" | 22 #include "components/gcm_driver/fake_gcm_client.h" |
25 #include "components/gcm_driver/fake_gcm_client_factory.h" | 23 #include "components/gcm_driver/fake_gcm_client_factory.h" |
| 24 #include "components/gcm_driver/gcm_channel_status_syncer.h" |
26 #include "components/gcm_driver/gcm_client.h" | 25 #include "components/gcm_driver/gcm_client.h" |
27 #include "components/gcm_driver/gcm_client_factory.h" | 26 #include "components/gcm_driver/gcm_client_factory.h" |
28 #include "components/gcm_driver/gcm_driver.h" | 27 #include "components/gcm_driver/gcm_driver.h" |
29 #include "components/pref_registry/pref_registry_syncable.h" | 28 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 29 #include "components/signin/core/browser/fake_signin_manager.h" |
30 #include "components/signin/core/browser/signin_manager.h" | 30 #include "components/signin/core/browser/signin_manager.h" |
31 #include "content/public/browser/browser_context.h" | 31 #include "components/signin/core/browser/test_signin_client.h" |
32 #include "content/public/browser/browser_thread.h" | |
33 #include "content/public/test/test_browser_thread_bundle.h" | |
34 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
35 | 33 |
36 namespace gcm { | 34 namespace gcm { |
37 | 35 |
38 namespace { | 36 namespace { |
39 | 37 |
40 const char kTestAppID[] = "TestApp"; | 38 const char kTestAppID[] = "TestApp"; |
41 const char kUserID[] = "user"; | 39 const char kUserID[] = "user"; |
42 | 40 |
43 scoped_ptr<KeyedService> BuildGCMProfileService( | |
44 content::BrowserContext* context) { | |
45 Profile* profile = Profile::FromBrowserContext(context); | |
46 base::SequencedWorkerPool* worker_pool = | |
47 content::BrowserThread::GetBlockingPool(); | |
48 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner( | |
49 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
50 worker_pool->GetSequenceToken(), | |
51 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); | |
52 return make_scoped_ptr(new gcm::GCMProfileService( | |
53 profile->GetPrefs(), profile->GetPath(), profile->GetRequestContext(), | |
54 chrome::GetChannel(), | |
55 scoped_ptr<ProfileIdentityProvider>(new ProfileIdentityProvider( | |
56 SigninManagerFactory::GetForProfile(profile), | |
57 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), | |
58 LoginUIServiceFactory::GetShowLoginPopupCallbackForProfile(profile))), | |
59 scoped_ptr<gcm::GCMClientFactory>(new gcm::FakeGCMClientFactory( | |
60 content::BrowserThread::GetMessageLoopProxyForThread( | |
61 content::BrowserThread::UI), | |
62 content::BrowserThread::GetMessageLoopProxyForThread( | |
63 content::BrowserThread::IO))), | |
64 content::BrowserThread::GetMessageLoopProxyForThread( | |
65 content::BrowserThread::UI), | |
66 content::BrowserThread::GetMessageLoopProxyForThread( | |
67 content::BrowserThread::IO), | |
68 blocking_task_runner)); | |
69 } | |
70 | |
71 } // namespace | 41 } // namespace |
72 | 42 |
73 class GCMProfileServiceTest : public testing::Test { | 43 class GCMProfileServiceTest : public testing::Test { |
74 protected: | 44 protected: |
75 GCMProfileServiceTest(); | 45 GCMProfileServiceTest(); |
76 ~GCMProfileServiceTest() override; | 46 ~GCMProfileServiceTest() override; |
77 | 47 |
78 // testing::Test: | 48 // testing::Test: |
79 void SetUp() override; | 49 void SetUp() override; |
80 void TearDown() override; | 50 void TearDown() override; |
(...skipping 18 matching lines...) Expand all Loading... |
99 GCMDriver* driver() const { return gcm_profile_service_->driver(); } | 69 GCMDriver* driver() const { return gcm_profile_service_->driver(); } |
100 std::string registration_id() const { return registration_id_; } | 70 std::string registration_id() const { return registration_id_; } |
101 GCMClient::Result registration_result() const { return registration_result_; } | 71 GCMClient::Result registration_result() const { return registration_result_; } |
102 GCMClient::Result unregistration_result() const { | 72 GCMClient::Result unregistration_result() const { |
103 return unregistration_result_; | 73 return unregistration_result_; |
104 } | 74 } |
105 std::string send_message_id() const { return send_message_id_; } | 75 std::string send_message_id() const { return send_message_id_; } |
106 GCMClient::Result send_result() const { return send_result_; } | 76 GCMClient::Result send_result() const { return send_result_; } |
107 | 77 |
108 private: | 78 private: |
109 content::TestBrowserThreadBundle thread_bundle_; | 79 base::ScopedTempDir temp_dir_; |
110 scoped_ptr<TestingProfile> profile_; | 80 base::MessageLoop message_loop_; |
| 81 TestingPrefServiceSimple prefs_; |
| 82 scoped_ptr<TestSigninClient> signin_client_; |
| 83 scoped_ptr<AccountTrackerService> tracker_; |
111 GCMProfileService* gcm_profile_service_; | 84 GCMProfileService* gcm_profile_service_; |
112 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; | 85 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; |
113 | 86 |
114 std::string registration_id_; | 87 std::string registration_id_; |
115 GCMClient::Result registration_result_; | 88 GCMClient::Result registration_result_; |
116 GCMClient::Result unregistration_result_; | 89 GCMClient::Result unregistration_result_; |
117 std::string send_message_id_; | 90 std::string send_message_id_; |
118 GCMClient::Result send_result_; | 91 GCMClient::Result send_result_; |
119 | 92 |
120 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest); | 93 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest); |
121 }; | 94 }; |
122 | 95 |
123 GCMProfileServiceTest::GCMProfileServiceTest() | 96 GCMProfileServiceTest::GCMProfileServiceTest() |
124 : gcm_profile_service_(NULL), | 97 : gcm_profile_service_(NULL), |
125 gcm_app_handler_(new FakeGCMAppHandler), | 98 gcm_app_handler_(new FakeGCMAppHandler), |
126 registration_result_(GCMClient::UNKNOWN_ERROR), | 99 registration_result_(GCMClient::UNKNOWN_ERROR), |
127 send_result_(GCMClient::UNKNOWN_ERROR) { | 100 send_result_(GCMClient::UNKNOWN_ERROR) { |
128 } | 101 } |
129 | 102 |
130 GCMProfileServiceTest::~GCMProfileServiceTest() { | 103 GCMProfileServiceTest::~GCMProfileServiceTest() { |
131 } | 104 } |
132 | 105 |
133 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const { | 106 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const { |
134 return static_cast<FakeGCMClient*>( | 107 return static_cast<FakeGCMClient*>( |
135 gcm_profile_service_->driver()->GetGCMClientForTesting()); | 108 gcm_profile_service_->driver()->GetGCMClientForTesting()); |
136 } | 109 } |
137 | 110 |
138 void GCMProfileServiceTest::SetUp() { | 111 void GCMProfileServiceTest::SetUp() { |
| 112 GCMChannelStatusSyncer::RegisterPrefs(prefs_.registry()); |
| 113 signin_client_.reset(new TestSigninClient(&prefs_)); |
| 114 tracker_.reset(new AccountTrackerService()); |
| 115 |
| 116 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 117 |
139 #if defined(OS_CHROMEOS) | 118 #if defined(OS_CHROMEOS) |
140 // Create a DBus thread manager setter for its side effect. | 119 // Create a DBus thread manager setter for its side effect. |
141 // Ignore the return value. | 120 // Ignore the return value. |
142 chromeos::DBusThreadManager::GetSetterForTesting(); | 121 chromeos::DBusThreadManager::GetSetterForTesting(); |
143 #endif | 122 #endif |
144 TestingProfile::Builder builder; | |
145 profile_ = builder.Build(); | |
146 } | 123 } |
147 | 124 |
148 void GCMProfileServiceTest::TearDown() { | 125 void GCMProfileServiceTest::TearDown() { |
149 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID); | 126 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID); |
150 } | 127 } |
151 | 128 |
152 void GCMProfileServiceTest::CreateGCMProfileService() { | 129 void GCMProfileServiceTest::CreateGCMProfileService() { |
153 gcm_profile_service_ = static_cast<GCMProfileService*>( | 130 scoped_refptr<base::SingleThreadTaskRunner> task_runner = |
154 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 131 message_loop_.task_runner(); |
155 profile_.get(), | 132 scoped_refptr<net::URLRequestContextGetter> request_context = |
156 &BuildGCMProfileService)); | 133 new net::TestURLRequestContextGetter(task_runner); |
157 gcm_profile_service_->driver()->AddAppHandler( | 134 gcm_profile_service_ = new gcm::GCMProfileService( |
158 kTestAppID, gcm_app_handler_.get()); | 135 &prefs_, temp_dir_.path(), request_context.get(), |
| 136 version_info::Channel::UNKNOWN, |
| 137 scoped_ptr<ProfileIdentityProvider>( |
| 138 new ProfileIdentityProvider( |
| 139 new FakeSigninManagerBase(signin_client_.get(), tracker_.get()), |
| 140 new FakeProfileOAuth2TokenService(), base::Closure())) |
| 141 .Pass(), |
| 142 scoped_ptr<gcm::GCMClientFactory>( |
| 143 new gcm::FakeGCMClientFactory(base::ThreadTaskRunnerHandle::Get(), |
| 144 task_runner)) |
| 145 .Pass(), |
| 146 base::ThreadTaskRunnerHandle::Get(), task_runner, task_runner); |
| 147 gcm_profile_service_->driver()->AddAppHandler(kTestAppID, |
| 148 gcm_app_handler_.get()); |
159 } | 149 } |
160 | 150 |
161 void GCMProfileServiceTest::RegisterAndWaitForCompletion( | 151 void GCMProfileServiceTest::RegisterAndWaitForCompletion( |
162 const std::vector<std::string>& sender_ids) { | 152 const std::vector<std::string>& sender_ids) { |
163 base::RunLoop run_loop; | 153 base::RunLoop run_loop; |
164 gcm_profile_service_->driver()->Register( | 154 gcm_profile_service_->driver()->Register( |
165 kTestAppID, | 155 kTestAppID, |
166 sender_ids, | 156 sender_ids, |
167 base::Bind(&GCMProfileServiceTest::RegisterCompleted, | 157 base::Bind(&GCMProfileServiceTest::RegisterCompleted, |
168 base::Unretained(this), | 158 base::Unretained(this), |
169 run_loop.QuitClosure())); | 159 run_loop.QuitClosure())); |
170 run_loop.Run(); | 160 run_loop.Run(); |
171 } | 161 } |
172 | 162 |
173 void GCMProfileServiceTest::UnregisterAndWaitForCompletion() { | 163 void GCMProfileServiceTest::UnregisterAndWaitForCompletion() { |
174 base::RunLoop run_loop; | 164 base::RunLoop run_loop; |
175 gcm_profile_service_->driver()->Unregister( | 165 gcm_profile_service_->driver()->Unregister( |
176 kTestAppID, | 166 kTestAppID, |
177 base::Bind(&GCMProfileServiceTest::UnregisterCompleted, | 167 base::Bind(&GCMProfileServiceTest::UnregisterCompleted, |
178 base::Unretained(this), | 168 base::Unretained(this), |
179 run_loop.QuitClosure())); | 169 run_loop.QuitClosure())); |
180 run_loop.Run(); | 170 run_loop.Run(); |
181 } | 171 } |
182 | 172 |
183 void GCMProfileServiceTest::SendAndWaitForCompletion( | 173 void GCMProfileServiceTest::SendAndWaitForCompletion( |
184 const OutgoingMessage& message) { | 174 const OutgoingMessage& message) { |
185 base::RunLoop run_loop; | 175 base::RunLoop run_loop; |
186 gcm_profile_service_->driver()->Send( | 176 gcm_profile_service_->driver()->Send( |
187 kTestAppID, | 177 kTestAppID, |
188 kUserID, | 178 kUserID, |
189 message, | 179 message, |
190 base::Bind(&GCMProfileServiceTest::SendCompleted, | 180 base::Bind(&GCMProfileServiceTest::SendCompleted, |
191 base::Unretained(this), | 181 base::Unretained(this), |
192 run_loop.QuitClosure())); | 182 run_loop.QuitClosure())); |
193 run_loop.Run(); | 183 run_loop.Run(); |
194 } | 184 } |
195 | 185 |
196 void GCMProfileServiceTest::RegisterCompleted( | 186 void GCMProfileServiceTest::RegisterCompleted( |
197 const base::Closure& callback, | 187 const base::Closure& callback, |
198 const std::string& registration_id, | 188 const std::string& registration_id, |
199 GCMClient::Result result) { | 189 GCMClient::Result result) { |
200 registration_id_ = registration_id; | 190 registration_id_ = registration_id; |
201 registration_result_ = result; | 191 registration_result_ = result; |
202 callback.Run(); | 192 callback.Run(); |
203 } | 193 } |
204 | 194 |
205 void GCMProfileServiceTest::UnregisterCompleted( | 195 void GCMProfileServiceTest::UnregisterCompleted( |
206 const base::Closure& callback, | 196 const base::Closure& callback, |
207 GCMClient::Result result) { | 197 GCMClient::Result result) { |
208 unregistration_result_ = result; | 198 unregistration_result_ = result; |
209 callback.Run(); | 199 callback.Run(); |
210 } | 200 } |
211 | 201 |
212 void GCMProfileServiceTest::SendCompleted( | 202 void GCMProfileServiceTest::SendCompleted( |
213 const base::Closure& callback, | 203 const base::Closure& callback, |
214 const std::string& message_id, | 204 const std::string& message_id, |
215 GCMClient::Result result) { | 205 GCMClient::Result result) { |
216 send_message_id_ = message_id; | 206 send_message_id_ = message_id; |
217 send_result_ = result; | 207 send_result_ = result; |
218 callback.Run(); | 208 callback.Run(); |
219 } | 209 } |
220 | 210 |
221 TEST_F(GCMProfileServiceTest, RegisterAndUnregister) { | 211 TEST_F(GCMProfileServiceTest, RegisterAndUnregister) { |
222 CreateGCMProfileService(); | 212 CreateGCMProfileService(); |
223 | 213 |
224 std::vector<std::string> sender_ids; | 214 std::vector<std::string> sender_ids; |
225 sender_ids.push_back("sender"); | 215 sender_ids.push_back("sender"); |
(...skipping 14 matching lines...) Expand all Loading... |
240 OutgoingMessage message; | 230 OutgoingMessage message; |
241 message.id = "1"; | 231 message.id = "1"; |
242 message.data["key1"] = "value1"; | 232 message.data["key1"] = "value1"; |
243 SendAndWaitForCompletion(message); | 233 SendAndWaitForCompletion(message); |
244 | 234 |
245 EXPECT_EQ(message.id, send_message_id()); | 235 EXPECT_EQ(message.id, send_message_id()); |
246 EXPECT_EQ(GCMClient::SUCCESS, send_result()); | 236 EXPECT_EQ(GCMClient::SUCCESS, send_result()); |
247 } | 237 } |
248 | 238 |
249 } // namespace gcm | 239 } // namespace gcm |
OLD | NEW |