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

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

Issue 1464463004: Componentize Unit Test for gcm_profile_service Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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
« no previous file with comments | « components/gcm_driver/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_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/prefs/testing_pref_service.h"
13 #include "base/run_loop.h" 14 #include "base/run_loop.h"
14 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" 15 #include "base/test/test_simple_task_runner.h"
15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.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) 16 #if defined(OS_CHROMEOS)
21 #include "chromeos/dbus/dbus_thread_manager.h" 17 #include "chromeos/dbus/dbus_thread_manager.h"
22 #endif 18 #endif
23 #include "components/gcm_driver/fake_gcm_app_handler.h" 19 #include "components/gcm_driver/fake_gcm_app_handler.h"
24 #include "components/gcm_driver/fake_gcm_client.h" 20 #include "components/gcm_driver/fake_gcm_client.h"
25 #include "components/gcm_driver/fake_gcm_client_factory.h" 21 #include "components/gcm_driver/fake_gcm_client_factory.h"
22 #include "components/gcm_driver/gcm_channel_status_syncer.h"
26 #include "components/gcm_driver/gcm_client.h" 23 #include "components/gcm_driver/gcm_client.h"
27 #include "components/gcm_driver/gcm_client_factory.h" 24 #include "components/gcm_driver/gcm_client_factory.h"
28 #include "components/gcm_driver/gcm_driver.h" 25 #include "components/gcm_driver/gcm_driver.h"
29 #include "components/pref_registry/pref_registry_syncable.h" 26 #include "components/pref_registry/pref_registry_syncable.h"
27 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
28 #include "components/signin/core/browser/fake_signin_manager.h"
30 #include "components/signin/core/browser/signin_manager.h" 29 #include "components/signin/core/browser/signin_manager.h"
31 #include "content/public/browser/browser_context.h" 30 #include "components/signin/core/browser/test_signin_client.h"
32 #include "content/public/browser/browser_thread.h" 31 #include "content/public/browser/browser_thread.h"
32
33 #include "content/public/test/test_browser_thread_bundle.h" 33 #include "content/public/test/test_browser_thread_bundle.h"
34 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
35 35
36 namespace gcm { 36 namespace gcm {
37 37
38 namespace { 38 namespace {
39 39
40 const char kTestAppID[] = "TestApp"; 40 const char kTestAppID[] = "TestApp";
41 const char kUserID[] = "user"; 41 const char kUserID[] = "user";
42 42
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 43 } // namespace
72 44
73 class GCMProfileServiceTest : public testing::Test { 45 class GCMProfileServiceTest : public testing::Test {
74 protected: 46 protected:
75 GCMProfileServiceTest(); 47 GCMProfileServiceTest();
76 ~GCMProfileServiceTest() override; 48 ~GCMProfileServiceTest() override;
77 49
78 // testing::Test: 50 // testing::Test:
79 void SetUp() override; 51 void SetUp() override;
80 void TearDown() override; 52 void TearDown() override;
(...skipping 18 matching lines...) Expand all
99 GCMDriver* driver() const { return gcm_profile_service_->driver(); } 71 GCMDriver* driver() const { return gcm_profile_service_->driver(); }
100 std::string registration_id() const { return registration_id_; } 72 std::string registration_id() const { return registration_id_; }
101 GCMClient::Result registration_result() const { return registration_result_; } 73 GCMClient::Result registration_result() const { return registration_result_; }
102 GCMClient::Result unregistration_result() const { 74 GCMClient::Result unregistration_result() const {
103 return unregistration_result_; 75 return unregistration_result_;
104 } 76 }
105 std::string send_message_id() const { return send_message_id_; } 77 std::string send_message_id() const { return send_message_id_; }
106 GCMClient::Result send_result() const { return send_result_; } 78 GCMClient::Result send_result() const { return send_result_; }
107 79
108 private: 80 private:
81 base::ScopedTempDir temp_dir_;
109 content::TestBrowserThreadBundle thread_bundle_; 82 content::TestBrowserThreadBundle thread_bundle_;
110 scoped_ptr<TestingProfile> profile_; 83 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
84 base::Thread io_thread_;
droger 2015/11/25 09:19:20 Remove task_runner_ and io_thread_ and use a base:
Jitu( very slow this week) 2015/12/14 13:40:37 Done.
85
86 TestingPrefServiceSimple prefs_;
87 // scoped_ptr<TestSigninClient> signin_client_;
88 scoped_ptr<FakeSigninManagerBase> signin_manager_;
89 // scoped_ptr<AccountTrackerService> account_tracker_;
90 scoped_ptr<ProfileOAuth2TokenService> fake_token_service_;
111 GCMProfileService* gcm_profile_service_; 91 GCMProfileService* gcm_profile_service_;
112 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; 92 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_;
113 93
114 std::string registration_id_; 94 std::string registration_id_;
115 GCMClient::Result registration_result_; 95 GCMClient::Result registration_result_;
116 GCMClient::Result unregistration_result_; 96 GCMClient::Result unregistration_result_;
117 std::string send_message_id_; 97 std::string send_message_id_;
118 GCMClient::Result send_result_; 98 GCMClient::Result send_result_;
119 99
120 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest); 100 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest);
121 }; 101 };
122 102
123 GCMProfileServiceTest::GCMProfileServiceTest() 103 GCMProfileServiceTest::GCMProfileServiceTest()
124 : gcm_profile_service_(NULL), 104 : task_runner_(new base::TestSimpleTaskRunner()),
105 io_thread_("IOThread"),
106 gcm_profile_service_(NULL),
125 gcm_app_handler_(new FakeGCMAppHandler), 107 gcm_app_handler_(new FakeGCMAppHandler),
126 registration_result_(GCMClient::UNKNOWN_ERROR), 108 registration_result_(GCMClient::UNKNOWN_ERROR),
127 send_result_(GCMClient::UNKNOWN_ERROR) { 109 send_result_(GCMClient::UNKNOWN_ERROR) {}
128 }
129 110
130 GCMProfileServiceTest::~GCMProfileServiceTest() { 111 GCMProfileServiceTest::~GCMProfileServiceTest() {
112 VLOG(0) << "JITU" << __FUNCTION__;
131 } 113 }
132 114
133 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const { 115 FakeGCMClient* GCMProfileServiceTest::GetGCMClient() const {
134 return static_cast<FakeGCMClient*>( 116 return static_cast<FakeGCMClient*>(
135 gcm_profile_service_->driver()->GetGCMClientForTesting()); 117 gcm_profile_service_->driver()->GetGCMClientForTesting());
136 } 118 }
137 119
138 void GCMProfileServiceTest::SetUp() { 120 void GCMProfileServiceTest::SetUp() {
121 VLOG(0) << "JITU" << __FUNCTION__;
122 GCMChannelStatusSyncer::RegisterPrefs(prefs_.registry());
123 io_thread_.Start();
124 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
125 // Setup account tracker.
126 scoped_ptr<TestSigninClient> client;
127 client.reset(new TestSigninClient(&prefs_));
128 AccountTrackerService tracker;
129 // tracker.Initialize(client.get());
130 signin_manager_.reset(new FakeSigninManagerBase(client.get(), &tracker));
131 // signin_manager_->Initialize(&prefs_);
132
133 fake_token_service_.reset(new FakeProfileOAuth2TokenService());
134 VLOG(0) << "JITU" << __FUNCTION__;
135
139 #if defined(OS_CHROMEOS) 136 #if defined(OS_CHROMEOS)
140 // Create a DBus thread manager setter for its side effect. 137 // Create a DBus thread manager setter for its side effect.
141 // Ignore the return value. 138 // Ignore the return value.
142 chromeos::DBusThreadManager::GetSetterForTesting(); 139 chromeos::DBusThreadManager::GetSetterForTesting();
143 #endif 140 #endif
144 TestingProfile::Builder builder; 141 // TestingProfile::Builder builder;
145 profile_ = builder.Build(); 142 // profile_ = builder.Build();
146 } 143 }
147 144
148 void GCMProfileServiceTest::TearDown() { 145 void GCMProfileServiceTest::TearDown() {
146 VLOG(0) << "JITU::" << __FUNCTION__;
149 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID); 147 gcm_profile_service_->driver()->RemoveAppHandler(kTestAppID);
150 } 148 }
151 149
152 void GCMProfileServiceTest::CreateGCMProfileService() { 150 void GCMProfileServiceTest::CreateGCMProfileService() {
153 gcm_profile_service_ = static_cast<GCMProfileService*>( 151 net::URLRequestContextGetter* request_context =
154 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( 152 new net::TestURLRequestContextGetter(io_thread_.task_runner());
droger 2015/11/25 09:19:20 Use message_loop_.task_runner()
Jitu( very slow this week) 2015/12/14 13:40:37 Done.
155 profile_.get(), 153 VLOG(0) << "JITU::" << __FUNCTION__;
156 &BuildGCMProfileService)); 154
157 gcm_profile_service_->driver()->AddAppHandler( 155 base::SequencedWorkerPool* worker_pool =
158 kTestAppID, gcm_app_handler_.get()); 156 content::BrowserThread::GetBlockingPool();
157 VLOG(0) << "JITU::" << __FUNCTION__;
158 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner(
159 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
160 worker_pool->GetSequenceToken(),
161 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
162 VLOG(0) << "JITU::" << __FUNCTION__;
163 gcm_profile_service_ = new gcm::GCMProfileService(
164 &prefs_, temp_dir_.path(), request_context,
165 version_info::Channel::UNKNOWN,
166 scoped_ptr<ProfileIdentityProvider>(
167 new ProfileIdentityProvider(signin_manager_.get(),
168 fake_token_service_.get(),
169 base::Closure()))
170 .Pass(),
171 scoped_ptr<gcm::GCMClientFactory>(
172 new gcm::FakeGCMClientFactory(base::ThreadTaskRunnerHandle::Get(),
173 io_thread_.task_runner()))
174 .Pass(),
175 base::ThreadTaskRunnerHandle::Get(), io_thread_.task_runner(),
176 blocking_task_runner);
177 gcm_profile_service_->driver()->AddAppHandler(kTestAppID,
178 gcm_app_handler_.get());
159 } 179 }
160 180
161 void GCMProfileServiceTest::RegisterAndWaitForCompletion( 181 void GCMProfileServiceTest::RegisterAndWaitForCompletion(
162 const std::vector<std::string>& sender_ids) { 182 const std::vector<std::string>& sender_ids) {
183 VLOG(0) << "JITU::" << __FUNCTION__;
163 base::RunLoop run_loop; 184 base::RunLoop run_loop;
164 gcm_profile_service_->driver()->Register( 185 gcm_profile_service_->driver()->Register(
165 kTestAppID, 186 kTestAppID, sender_ids,
166 sender_ids,
167 base::Bind(&GCMProfileServiceTest::RegisterCompleted, 187 base::Bind(&GCMProfileServiceTest::RegisterCompleted,
168 base::Unretained(this), 188 base::Unretained(this), run_loop.QuitClosure()));
169 run_loop.QuitClosure()));
170 run_loop.Run(); 189 run_loop.Run();
171 } 190 }
172 191
173 void GCMProfileServiceTest::UnregisterAndWaitForCompletion() { 192 void GCMProfileServiceTest::UnregisterAndWaitForCompletion() {
193 VLOG(0) << "JITU::" << __FUNCTION__;
174 base::RunLoop run_loop; 194 base::RunLoop run_loop;
175 gcm_profile_service_->driver()->Unregister( 195 gcm_profile_service_->driver()->Unregister(
176 kTestAppID, 196 kTestAppID, base::Bind(&GCMProfileServiceTest::UnregisterCompleted,
177 base::Bind(&GCMProfileServiceTest::UnregisterCompleted, 197 base::Unretained(this), run_loop.QuitClosure()));
178 base::Unretained(this),
179 run_loop.QuitClosure()));
180 run_loop.Run(); 198 run_loop.Run();
181 } 199 }
182 200
183 void GCMProfileServiceTest::SendAndWaitForCompletion( 201 void GCMProfileServiceTest::SendAndWaitForCompletion(
184 const OutgoingMessage& message) { 202 const OutgoingMessage& message) {
203 VLOG(0) << "JITU::" << __FUNCTION__;
185 base::RunLoop run_loop; 204 base::RunLoop run_loop;
186 gcm_profile_service_->driver()->Send( 205 gcm_profile_service_->driver()->Send(
187 kTestAppID, 206 kTestAppID, kUserID, message,
188 kUserID, 207 base::Bind(&GCMProfileServiceTest::SendCompleted, base::Unretained(this),
189 message,
190 base::Bind(&GCMProfileServiceTest::SendCompleted,
191 base::Unretained(this),
192 run_loop.QuitClosure())); 208 run_loop.QuitClosure()));
193 run_loop.Run(); 209 run_loop.Run();
194 } 210 }
195 211
196 void GCMProfileServiceTest::RegisterCompleted( 212 void GCMProfileServiceTest::RegisterCompleted(
197 const base::Closure& callback, 213 const base::Closure& callback,
198 const std::string& registration_id, 214 const std::string& registration_id,
199 GCMClient::Result result) { 215 GCMClient::Result result) {
216 VLOG(0) << "JITU::" << __FUNCTION__;
200 registration_id_ = registration_id; 217 registration_id_ = registration_id;
201 registration_result_ = result; 218 registration_result_ = result;
202 callback.Run(); 219 callback.Run();
203 } 220 }
204 221
205 void GCMProfileServiceTest::UnregisterCompleted( 222 void GCMProfileServiceTest::UnregisterCompleted(const base::Closure& callback,
206 const base::Closure& callback, 223 GCMClient::Result result) {
207 GCMClient::Result result) { 224 VLOG(0) << "JITU::" << __FUNCTION__;
208 unregistration_result_ = result; 225 unregistration_result_ = result;
209 callback.Run(); 226 callback.Run();
210 } 227 }
211 228
212 void GCMProfileServiceTest::SendCompleted( 229 void GCMProfileServiceTest::SendCompleted(const base::Closure& callback,
213 const base::Closure& callback, 230 const std::string& message_id,
214 const std::string& message_id, 231 GCMClient::Result result) {
215 GCMClient::Result result) { 232 VLOG(0) << "JITU::" << __FUNCTION__;
216 send_message_id_ = message_id; 233 send_message_id_ = message_id;
217 send_result_ = result; 234 send_result_ = result;
218 callback.Run(); 235 callback.Run();
219 } 236 }
220 237
221 TEST_F(GCMProfileServiceTest, RegisterAndUnregister) { 238 TEST_F(GCMProfileServiceTest, RegisterAndUnregister) {
239 VLOG(0) << "JITU::" << __FUNCTION__;
222 CreateGCMProfileService(); 240 CreateGCMProfileService();
223 241
224 std::vector<std::string> sender_ids; 242 std::vector<std::string> sender_ids;
225 sender_ids.push_back("sender"); 243 sender_ids.push_back("sender");
226 RegisterAndWaitForCompletion(sender_ids); 244 RegisterAndWaitForCompletion(sender_ids);
227 245
228 std::string expected_registration_id = 246 std::string expected_registration_id =
229 FakeGCMClient::GenerateGCMRegistrationID(sender_ids); 247 FakeGCMClient::GenerateGCMRegistrationID(sender_ids);
230 EXPECT_EQ(expected_registration_id, registration_id()); 248 EXPECT_EQ(expected_registration_id, registration_id());
231 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); 249 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
232
233 UnregisterAndWaitForCompletion(); 250 UnregisterAndWaitForCompletion();
234 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); 251 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
235 } 252 }
236 253
237 TEST_F(GCMProfileServiceTest, Send) { 254 TEST_F(GCMProfileServiceTest, Send) {
255
256 VLOG(0) << "JITU::" << __FUNCTION__;
238 CreateGCMProfileService(); 257 CreateGCMProfileService();
239 258
240 OutgoingMessage message; 259 OutgoingMessage message;
241 message.id = "1"; 260 message.id = "1";
242 message.data["key1"] = "value1"; 261 message.data["key1"] = "value1";
243 SendAndWaitForCompletion(message); 262 SendAndWaitForCompletion(message);
244 263
245 EXPECT_EQ(message.id, send_message_id()); 264 EXPECT_EQ(message.id, send_message_id());
246 EXPECT_EQ(GCMClient::SUCCESS, send_result()); 265 EXPECT_EQ(GCMClient::SUCCESS, send_result());
247 } 266 }
248 267
249 } // namespace gcm 268 } // namespace gcm
OLDNEW
« no previous file with comments | « components/gcm_driver/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698