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

Side by Side Diff: components/component_updater/component_updater_service_unittest.cc

Issue 2257363002: Define EnabledComponentUpdates group policy for the component updater. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@callback
Patch Set: . Created 4 years, 4 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/component_updater/component_updater_service.h" 5 #include "components/component_updater/component_updater_service.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 120
121 scoped_refptr<TestConfigurator> config_; 121 scoped_refptr<TestConfigurator> config_;
122 scoped_refptr<MockUpdateClient> update_client_; 122 scoped_refptr<MockUpdateClient> update_client_;
123 std::unique_ptr<ComponentUpdateService> component_updater_; 123 std::unique_ptr<ComponentUpdateService> component_updater_;
124 124
125 DISALLOW_COPY_AND_ASSIGN(ComponentUpdaterTest); 125 DISALLOW_COPY_AND_ASSIGN(ComponentUpdaterTest);
126 }; 126 };
127 127
128 class OnDemandTester { 128 class OnDemandTester {
129 public: 129 public:
130 static bool OnDemand(ComponentUpdateService* cus, const std::string& id); 130 void OnDemand(ComponentUpdateService* cus, const std::string& id);
131 int error() const { return error_; }
132
133 private:
134 void OnDemandComplete(int error);
135
136 int error_ = 0;
131 }; 137 };
132 138
133 MockInstaller::MockInstaller() { 139 MockInstaller::MockInstaller() {
134 } 140 }
135 141
136 MockInstaller::~MockInstaller() { 142 MockInstaller::~MockInstaller() {
137 } 143 }
138 144
139 MockUpdateClient::MockUpdateClient() { 145 MockUpdateClient::MockUpdateClient() {
140 } 146 }
141 147
142 MockUpdateClient::~MockUpdateClient() { 148 MockUpdateClient::~MockUpdateClient() {
143 } 149 }
144 150
145 MockServiceObserver::MockServiceObserver() { 151 MockServiceObserver::MockServiceObserver() {
146 } 152 }
147 153
148 MockServiceObserver::~MockServiceObserver() { 154 MockServiceObserver::~MockServiceObserver() {
149 } 155 }
150 156
151 bool OnDemandTester::OnDemand(ComponentUpdateService* cus, 157 void OnDemandTester::OnDemand(ComponentUpdateService* cus,
152 const std::string& id) { 158 const std::string& id) {
153 return cus->GetOnDemandUpdater().OnDemandUpdate(id); 159 cus->GetOnDemandUpdater().OnDemandUpdate(
160 id,
161 base::Bind(&OnDemandTester::OnDemandComplete, base::Unretained(this)));
162 }
163
164 void OnDemandTester::OnDemandComplete(int error) {
165 error_ = error;
154 } 166 }
155 167
156 std::unique_ptr<ComponentUpdateService> TestComponentUpdateServiceFactory( 168 std::unique_ptr<ComponentUpdateService> TestComponentUpdateServiceFactory(
157 const scoped_refptr<Configurator>& config) { 169 const scoped_refptr<Configurator>& config) {
158 DCHECK(config); 170 DCHECK(config);
159 return base::WrapUnique(new CrxUpdateService(config, new MockUpdateClient())); 171 return base::WrapUnique(new CrxUpdateService(config, new MockUpdateClient()));
160 } 172 }
161 173
162 ComponentUpdaterTest::ComponentUpdaterTest() 174 ComponentUpdaterTest::ComponentUpdaterTest()
163 : worker_pool_( 175 : worker_pool_(
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 279
268 ht.ExpectUniqueSample("ComponentUpdater.Calls", 1, 2); 280 ht.ExpectUniqueSample("ComponentUpdater.Calls", 1, 2);
269 ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 2); 281 ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 2);
270 ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 2); 282 ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 2);
271 } 283 }
272 284
273 // Tests that on-demand updates invoke UpdateClient::Install. 285 // Tests that on-demand updates invoke UpdateClient::Install.
274 TEST_F(ComponentUpdaterTest, OnDemandUpdate) { 286 TEST_F(ComponentUpdaterTest, OnDemandUpdate) {
275 class LoopHandler { 287 class LoopHandler {
276 public: 288 public:
277 LoopHandler(int max_cnt, const base::Closure& quit_closure) 289 LoopHandler(int max_cnt) : max_cnt_(max_cnt) {}
278 : max_cnt_(max_cnt), quit_closure_(quit_closure) {}
279 290
280 void OnInstall( 291 void OnInstall(
281 const std::string& ids, 292 const std::string& ids,
282 const UpdateClient::CrxDataCallback& crx_data_callback, 293 const UpdateClient::CrxDataCallback& crx_data_callback,
283 const UpdateClient::CompletionCallback& completion_callback) { 294 const UpdateClient::CompletionCallback& completion_callback) {
284 completion_callback.Run(0); 295 completion_callback.Run(0);
285 static int cnt = 0; 296 static int cnt = 0;
286 ++cnt; 297 ++cnt;
287 if (cnt >= max_cnt_) 298 if (cnt >= max_cnt_) {
288 quit_closure_.Run(); 299 base::ThreadTaskRunnerHandle::Get()->PostTask(
300 FROM_HERE, base::Bind(&LoopHandler::Quit, base::Unretained(this)));
301 }
289 } 302 }
290 303
291 private: 304 private:
305 void Quit() { base::MessageLoop::current()->QuitWhenIdle(); }
306
292 const int max_cnt_; 307 const int max_cnt_;
293 base::Closure quit_closure_;
294 }; 308 };
295 309
296 base::HistogramTester ht; 310 base::HistogramTester ht;
297 311
298 auto config = configurator(); 312 auto config = configurator();
299 config->SetInitialDelay(3600); 313 config->SetInitialDelay(3600);
300 314
301 auto& cus = component_updater(); 315 auto& cus = component_updater();
302 316
317 // Tests calling OnDemand for an unregistered component. This call results in
318 // an error, which is recorded by the OnDemandTester instance. Since the
319 // component was not registered, the call is ignored for UMA metrics.
320 OnDemandTester ondemand_tester_component_not_registered;
321 ondemand_tester_component_not_registered.OnDemand(
322 &cus, "ihfokbkgjpifnbbojhneepfflplebdkc");
323
303 const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; 324 const std::string id = "jebgalgnebhfojomionfpkfelancnnkf";
304 EXPECT_FALSE(OnDemandTester::OnDemand(&cus, id));
305
306 scoped_refptr<MockInstaller> installer(new MockInstaller());
307 325
308 using update_client::jebg_hash; 326 using update_client::jebg_hash;
309 CrxComponent crx_component; 327 CrxComponent crx_component;
310 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); 328 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
311 crx_component.version = Version("0.9"); 329 crx_component.version = Version("0.9");
312 crx_component.installer = installer; 330 crx_component.installer = new MockInstaller();
313 331
314 LoopHandler loop_handler(1, quit_closure()); 332 LoopHandler loop_handler(1);
315 EXPECT_CALL(update_client(), 333 EXPECT_CALL(update_client(),
316 Install("jebgalgnebhfojomionfpkfelancnnkf", _, _)) 334 Install("jebgalgnebhfojomionfpkfelancnnkf", _, _))
317 .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); 335 .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall));
318 EXPECT_CALL(update_client(), Stop()).Times(1); 336 EXPECT_CALL(update_client(), Stop()).Times(1);
319 337
320 EXPECT_TRUE(cus.RegisterComponent(crx_component)); 338 EXPECT_TRUE(cus.RegisterComponent(crx_component));
321 EXPECT_TRUE(OnDemandTester::OnDemand(&cus, id)); 339 OnDemandTester ondemand_tester;
340 ondemand_tester.OnDemand(&cus, id);
322 341
323 RunThreads(); 342 base::RunLoop().Run();
343
344 EXPECT_EQ(
345 static_cast<int>(update_client::Error::ERROR_UPDATE_INVALID_ARGUMENT),
346 ondemand_tester_component_not_registered.error());
347 EXPECT_EQ(0, ondemand_tester.error());
324 348
325 ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1); 349 ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1);
326 ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); 350 ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1);
327 ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); 351 ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1);
328 } 352 }
329 353
330 // Tests that throttling an update invokes UpdateClient::Install. 354 // Tests that throttling an update invokes UpdateClient::Install.
331 TEST_F(ComponentUpdaterTest, MaybeThrottle) { 355 TEST_F(ComponentUpdaterTest, MaybeThrottle) {
332 class LoopHandler { 356 class LoopHandler {
333 public: 357 public:
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 base::Bind(&ComponentUpdaterTest::ReadyCallback)); 399 base::Bind(&ComponentUpdaterTest::ReadyCallback));
376 400
377 RunThreads(); 401 RunThreads();
378 402
379 ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1); 403 ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1);
380 ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); 404 ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1);
381 ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); 405 ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1);
382 } 406 }
383 407
384 } // namespace component_updater 408 } // namespace component_updater
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698