| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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) |
| 278 : max_cnt_(max_cnt), quit_closure_(quit_closure) {} | 290 : max_cnt_(max_cnt) {} |
| 279 | 291 |
| 280 void OnInstall( | 292 void OnInstall( |
| 281 const std::string& ids, | 293 const std::string& ids, |
| 282 const UpdateClient::CrxDataCallback& crx_data_callback, | 294 const UpdateClient::CrxDataCallback& crx_data_callback, |
| 283 const UpdateClient::CompletionCallback& completion_callback) { | 295 const UpdateClient::CompletionCallback& completion_callback) { |
| 284 completion_callback.Run(0); | 296 completion_callback.Run(0); |
| 285 static int cnt = 0; | 297 static int cnt = 0; |
| 286 ++cnt; | 298 ++cnt; |
| 287 if (cnt >= max_cnt_) | 299 if (cnt >= max_cnt_) { |
| 288 quit_closure_.Run(); | 300 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 301 FROM_HERE, base::Bind(&LoopHandler::Quit, base::Unretained(this))); |
| 302 } |
| 289 } | 303 } |
| 290 | 304 |
| 291 private: | 305 private: |
| 306 void Quit() { base::MessageLoop::current()->QuitWhenIdle(); } |
| 307 |
| 292 const int max_cnt_; | 308 const int max_cnt_; |
| 293 base::Closure quit_closure_; | |
| 294 }; | 309 }; |
| 295 | 310 |
| 296 base::HistogramTester ht; | 311 base::HistogramTester ht; |
| 297 | 312 |
| 298 auto config = configurator(); | 313 auto config = configurator(); |
| 299 config->SetInitialDelay(3600); | 314 config->SetInitialDelay(3600); |
| 300 | 315 |
| 301 auto& cus = component_updater(); | 316 auto& cus = component_updater(); |
| 302 | 317 |
| 318 // Tests calling OnDemand for an unregistered component. This call results in |
| 319 // an error, which is recorded by the OnDemandTester instance. Since the |
| 320 // component was not registered, the call is ignored for UMA metrics. |
| 321 OnDemandTester ondemand_tester_component_not_registered; |
| 322 ondemand_tester_component_not_registered.OnDemand( |
| 323 &cus, "ihfokbkgjpifnbbojhneepfflplebdkc"); |
| 324 |
| 303 const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; | 325 const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 304 EXPECT_FALSE(OnDemandTester::OnDemand(&cus, id)); | |
| 305 | |
| 306 scoped_refptr<MockInstaller> installer(new MockInstaller()); | |
| 307 | 326 |
| 308 using update_client::jebg_hash; | 327 using update_client::jebg_hash; |
| 309 CrxComponent crx_component; | 328 CrxComponent crx_component; |
| 310 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); | 329 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); |
| 311 crx_component.version = Version("0.9"); | 330 crx_component.version = Version("0.9"); |
| 312 crx_component.installer = installer; | 331 crx_component.installer = new MockInstaller(); |
| 313 | 332 |
| 314 LoopHandler loop_handler(1, quit_closure()); | 333 LoopHandler loop_handler(1); |
| 315 EXPECT_CALL(update_client(), | 334 EXPECT_CALL(update_client(), |
| 316 Install("jebgalgnebhfojomionfpkfelancnnkf", _, _)) | 335 Install("jebgalgnebhfojomionfpkfelancnnkf", _, _)) |
| 317 .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); | 336 .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); |
| 318 EXPECT_CALL(update_client(), Stop()).Times(1); | 337 EXPECT_CALL(update_client(), Stop()).Times(1); |
| 319 | 338 |
| 320 EXPECT_TRUE(cus.RegisterComponent(crx_component)); | 339 EXPECT_TRUE(cus.RegisterComponent(crx_component)); |
| 321 EXPECT_TRUE(OnDemandTester::OnDemand(&cus, id)); | 340 OnDemandTester ondemand_tester; |
| 341 ondemand_tester.OnDemand(&cus, id); |
| 322 | 342 |
| 323 RunThreads(); | 343 base::RunLoop().Run(); |
| 344 |
| 345 EXPECT_EQ( |
| 346 static_cast<int>(update_client::Error::ERROR_UPDATE_INVALID_ARGUMENT), |
| 347 ondemand_tester_component_not_registered.error()); |
| 348 EXPECT_EQ(0, ondemand_tester.error()); |
| 324 | 349 |
| 325 ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1); | 350 ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1); |
| 326 ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); | 351 ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); |
| 327 ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); | 352 ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); |
| 328 } | 353 } |
| 329 | 354 |
| 330 // Tests that throttling an update invokes UpdateClient::Install. | 355 // Tests that throttling an update invokes UpdateClient::Install. |
| 331 TEST_F(ComponentUpdaterTest, MaybeThrottle) { | 356 TEST_F(ComponentUpdaterTest, MaybeThrottle) { |
| 332 class LoopHandler { | 357 class LoopHandler { |
| 333 public: | 358 public: |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 base::Bind(&ComponentUpdaterTest::ReadyCallback)); | 400 base::Bind(&ComponentUpdaterTest::ReadyCallback)); |
| 376 | 401 |
| 377 RunThreads(); | 402 RunThreads(); |
| 378 | 403 |
| 379 ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1); | 404 ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1); |
| 380 ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); | 405 ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); |
| 381 ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); | 406 ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); |
| 382 } | 407 } |
| 383 | 408 |
| 384 } // namespace component_updater | 409 } // namespace component_updater |
| OLD | NEW |