| 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 <limits> | 5 #include <limits> |
| 6 #include <string> | 6 #include <string> |
| 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/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/test/histogram_tester.h" |
| 18 #include "base/test/sequenced_worker_pool_owner.h" | 19 #include "base/test/sequenced_worker_pool_owner.h" |
| 19 #include "base/thread_task_runner_handle.h" | 20 #include "base/thread_task_runner_handle.h" |
| 20 #include "base/values.h" | 21 #include "base/values.h" |
| 21 #include "components/component_updater/component_updater_service.h" | 22 #include "components/component_updater/component_updater_service.h" |
| 22 #include "components/component_updater/component_updater_service_internal.h" | 23 #include "components/component_updater/component_updater_service_internal.h" |
| 23 #include "components/update_client/test_configurator.h" | 24 #include "components/update_client/test_configurator.h" |
| 24 #include "components/update_client/test_installer.h" | 25 #include "components/update_client/test_installer.h" |
| 25 #include "components/update_client/update_client.h" | 26 #include "components/update_client/update_client.h" |
| 26 | 27 |
| 27 #include "testing/gmock/include/gmock/gmock.h" | 28 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 // Also tests that Uninstall is called when a component is unregistered. | 209 // Also tests that Uninstall is called when a component is unregistered. |
| 209 TEST_F(ComponentUpdaterTest, RegisterComponent) { | 210 TEST_F(ComponentUpdaterTest, RegisterComponent) { |
| 210 class LoopHandler { | 211 class LoopHandler { |
| 211 public: | 212 public: |
| 212 LoopHandler(int max_cnt, const base::Closure& quit_closure) | 213 LoopHandler(int max_cnt, const base::Closure& quit_closure) |
| 213 : max_cnt_(max_cnt), quit_closure_(quit_closure) {} | 214 : max_cnt_(max_cnt), quit_closure_(quit_closure) {} |
| 214 | 215 |
| 215 void OnUpdate(const std::vector<std::string>& ids, | 216 void OnUpdate(const std::vector<std::string>& ids, |
| 216 const UpdateClient::CrxDataCallback& crx_data_callback, | 217 const UpdateClient::CrxDataCallback& crx_data_callback, |
| 217 const UpdateClient::CompletionCallback& completion_callback) { | 218 const UpdateClient::CompletionCallback& completion_callback) { |
| 219 completion_callback.Run(0); |
| 218 static int cnt = 0; | 220 static int cnt = 0; |
| 219 ++cnt; | 221 ++cnt; |
| 220 if (cnt >= max_cnt_) | 222 if (cnt >= max_cnt_) |
| 221 quit_closure_.Run(); | 223 quit_closure_.Run(); |
| 222 } | 224 } |
| 223 | 225 |
| 224 private: | 226 private: |
| 225 const int max_cnt_; | 227 const int max_cnt_; |
| 226 base::Closure quit_closure_; | 228 base::Closure quit_closure_; |
| 227 }; | 229 }; |
| 228 | 230 |
| 231 base::HistogramTester ht; |
| 232 |
| 229 scoped_refptr<MockInstaller> installer(new MockInstaller()); | 233 scoped_refptr<MockInstaller> installer(new MockInstaller()); |
| 230 EXPECT_CALL(*installer, Uninstall()).WillOnce(Return(true)); | 234 EXPECT_CALL(*installer, Uninstall()).WillOnce(Return(true)); |
| 231 | 235 |
| 232 using update_client::jebg_hash; | 236 using update_client::jebg_hash; |
| 233 using update_client::abag_hash; | 237 using update_client::abag_hash; |
| 234 | 238 |
| 235 const std::string id1 = "abagagagagagagagagagagagagagagag"; | 239 const std::string id1 = "abagagagagagagagagagagagagagagag"; |
| 236 const std::string id2 = "jebgalgnebhfojomionfpkfelancnnkf"; | 240 const std::string id2 = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 237 std::vector<std::string> ids; | 241 std::vector<std::string> ids; |
| 238 ids.push_back(id1); | 242 ids.push_back(id1); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 253 EXPECT_CALL(update_client(), Update(ids, _, _)) | 257 EXPECT_CALL(update_client(), Update(ids, _, _)) |
| 254 .WillRepeatedly(Invoke(&loop_handler, &LoopHandler::OnUpdate)); | 258 .WillRepeatedly(Invoke(&loop_handler, &LoopHandler::OnUpdate)); |
| 255 | 259 |
| 256 EXPECT_CALL(update_client(), IsUpdating(id1)).Times(1); | 260 EXPECT_CALL(update_client(), IsUpdating(id1)).Times(1); |
| 257 EXPECT_CALL(update_client(), Stop()).Times(1); | 261 EXPECT_CALL(update_client(), Stop()).Times(1); |
| 258 | 262 |
| 259 EXPECT_TRUE(component_updater().RegisterComponent(crx_component1)); | 263 EXPECT_TRUE(component_updater().RegisterComponent(crx_component1)); |
| 260 EXPECT_TRUE(component_updater().RegisterComponent(crx_component2)); | 264 EXPECT_TRUE(component_updater().RegisterComponent(crx_component2)); |
| 261 | 265 |
| 262 RunThreads(); | 266 RunThreads(); |
| 267 EXPECT_TRUE(component_updater().UnregisterComponent(id1)); |
| 263 | 268 |
| 264 EXPECT_TRUE(component_updater().UnregisterComponent(id1)); | 269 ht.ExpectUniqueSample("ComponentUpdater.Calls.Count", 1, 2); |
| 270 ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 2); |
| 271 ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 2); |
| 265 } | 272 } |
| 266 | 273 |
| 267 // Tests that on-demand updates invoke UpdateClient::Install. | 274 // Tests that on-demand updates invoke UpdateClient::Install. |
| 268 TEST_F(ComponentUpdaterTest, OnDemandUpdate) { | 275 TEST_F(ComponentUpdaterTest, OnDemandUpdate) { |
| 269 class LoopHandler { | 276 class LoopHandler { |
| 270 public: | 277 public: |
| 271 LoopHandler(int max_cnt, const base::Closure& quit_closure) | 278 LoopHandler(int max_cnt, const base::Closure& quit_closure) |
| 272 : max_cnt_(max_cnt), quit_closure_(quit_closure) {} | 279 : max_cnt_(max_cnt), quit_closure_(quit_closure) {} |
| 273 | 280 |
| 274 void OnInstall( | 281 void OnInstall( |
| 275 const std::string& ids, | 282 const std::string& ids, |
| 276 const UpdateClient::CrxDataCallback& crx_data_callback, | 283 const UpdateClient::CrxDataCallback& crx_data_callback, |
| 277 const UpdateClient::CompletionCallback& completion_callback) { | 284 const UpdateClient::CompletionCallback& completion_callback) { |
| 285 completion_callback.Run(0); |
| 278 static int cnt = 0; | 286 static int cnt = 0; |
| 279 ++cnt; | 287 ++cnt; |
| 280 if (cnt >= max_cnt_) | 288 if (cnt >= max_cnt_) |
| 281 quit_closure_.Run(); | 289 quit_closure_.Run(); |
| 282 } | 290 } |
| 283 | 291 |
| 284 private: | 292 private: |
| 285 const int max_cnt_; | 293 const int max_cnt_; |
| 286 base::Closure quit_closure_; | 294 base::Closure quit_closure_; |
| 287 }; | 295 }; |
| 288 | 296 |
| 297 base::HistogramTester ht; |
| 298 |
| 289 auto config = configurator(); | 299 auto config = configurator(); |
| 290 config->SetInitialDelay(3600); | 300 config->SetInitialDelay(3600); |
| 291 | 301 |
| 292 auto& cus = component_updater(); | 302 auto& cus = component_updater(); |
| 293 | 303 |
| 294 const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; | 304 const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 295 EXPECT_FALSE(OnDemandTester::OnDemand(&cus, id)); | 305 EXPECT_FALSE(OnDemandTester::OnDemand(&cus, id)); |
| 296 | 306 |
| 297 scoped_refptr<MockInstaller> installer(new MockInstaller()); | 307 scoped_refptr<MockInstaller> installer(new MockInstaller()); |
| 298 | 308 |
| 299 using update_client::jebg_hash; | 309 using update_client::jebg_hash; |
| 300 CrxComponent crx_component; | 310 CrxComponent crx_component; |
| 301 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); | 311 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); |
| 302 crx_component.version = Version("0.9"); | 312 crx_component.version = Version("0.9"); |
| 303 crx_component.installer = installer; | 313 crx_component.installer = installer; |
| 304 | 314 |
| 305 LoopHandler loop_handler(1, quit_closure()); | 315 LoopHandler loop_handler(1, quit_closure()); |
| 306 EXPECT_CALL(update_client(), | 316 EXPECT_CALL(update_client(), |
| 307 Install("jebgalgnebhfojomionfpkfelancnnkf", _, _)) | 317 Install("jebgalgnebhfojomionfpkfelancnnkf", _, _)) |
| 308 .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); | 318 .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); |
| 309 EXPECT_CALL(update_client(), Stop()).Times(1); | 319 EXPECT_CALL(update_client(), Stop()).Times(1); |
| 310 | 320 |
| 311 EXPECT_TRUE(cus.RegisterComponent(crx_component)); | 321 EXPECT_TRUE(cus.RegisterComponent(crx_component)); |
| 312 EXPECT_TRUE(OnDemandTester::OnDemand(&cus, id)); | 322 EXPECT_TRUE(OnDemandTester::OnDemand(&cus, id)); |
| 313 | 323 |
| 314 RunThreads(); | 324 RunThreads(); |
| 325 |
| 326 ht.ExpectUniqueSample("ComponentUpdater.Calls.Count", 0, 1); |
| 327 ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); |
| 328 ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); |
| 315 } | 329 } |
| 316 | 330 |
| 317 // Tests that throttling an update invokes UpdateClient::Install. | 331 // Tests that throttling an update invokes UpdateClient::Install. |
| 318 TEST_F(ComponentUpdaterTest, MaybeThrottle) { | 332 TEST_F(ComponentUpdaterTest, MaybeThrottle) { |
| 319 class LoopHandler { | 333 class LoopHandler { |
| 320 public: | 334 public: |
| 321 LoopHandler(int max_cnt, const base::Closure& quit_closure) | 335 LoopHandler(int max_cnt, const base::Closure& quit_closure) |
| 322 : max_cnt_(max_cnt), quit_closure_(quit_closure) {} | 336 : max_cnt_(max_cnt), quit_closure_(quit_closure) {} |
| 323 | 337 |
| 324 void OnInstall( | 338 void OnInstall( |
| 325 const std::string& ids, | 339 const std::string& ids, |
| 326 const UpdateClient::CrxDataCallback& crx_data_callback, | 340 const UpdateClient::CrxDataCallback& crx_data_callback, |
| 327 const UpdateClient::CompletionCallback& completion_callback) { | 341 const UpdateClient::CompletionCallback& completion_callback) { |
| 342 completion_callback.Run(0); |
| 328 static int cnt = 0; | 343 static int cnt = 0; |
| 329 ++cnt; | 344 ++cnt; |
| 330 if (cnt >= max_cnt_) | 345 if (cnt >= max_cnt_) |
| 331 quit_closure_.Run(); | 346 quit_closure_.Run(); |
| 332 } | 347 } |
| 333 | 348 |
| 334 private: | 349 private: |
| 335 const int max_cnt_; | 350 const int max_cnt_; |
| 336 base::Closure quit_closure_; | 351 base::Closure quit_closure_; |
| 337 }; | 352 }; |
| 338 | 353 |
| 354 base::HistogramTester ht; |
| 355 |
| 339 auto config = configurator(); | 356 auto config = configurator(); |
| 340 config->SetInitialDelay(3600); | 357 config->SetInitialDelay(3600); |
| 341 | 358 |
| 342 scoped_refptr<MockInstaller> installer(new MockInstaller()); | 359 scoped_refptr<MockInstaller> installer(new MockInstaller()); |
| 343 | 360 |
| 344 using update_client::jebg_hash; | 361 using update_client::jebg_hash; |
| 345 CrxComponent crx_component; | 362 CrxComponent crx_component; |
| 346 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); | 363 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); |
| 347 crx_component.version = Version("0.9"); | 364 crx_component.version = Version("0.9"); |
| 348 crx_component.installer = installer; | 365 crx_component.installer = installer; |
| 349 | 366 |
| 350 LoopHandler loop_handler(1, quit_closure()); | 367 LoopHandler loop_handler(1, quit_closure()); |
| 351 EXPECT_CALL(update_client(), | 368 EXPECT_CALL(update_client(), |
| 352 Install("jebgalgnebhfojomionfpkfelancnnkf", _, _)) | 369 Install("jebgalgnebhfojomionfpkfelancnnkf", _, _)) |
| 353 .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); | 370 .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); |
| 354 EXPECT_CALL(update_client(), Stop()).Times(1); | 371 EXPECT_CALL(update_client(), Stop()).Times(1); |
| 355 | 372 |
| 356 EXPECT_TRUE(component_updater().RegisterComponent(crx_component)); | 373 EXPECT_TRUE(component_updater().RegisterComponent(crx_component)); |
| 357 component_updater().MaybeThrottle( | 374 component_updater().MaybeThrottle( |
| 358 "jebgalgnebhfojomionfpkfelancnnkf", | 375 "jebgalgnebhfojomionfpkfelancnnkf", |
| 359 base::Bind(&ComponentUpdaterTest::ReadyCallback)); | 376 base::Bind(&ComponentUpdaterTest::ReadyCallback)); |
| 360 | 377 |
| 361 RunThreads(); | 378 RunThreads(); |
| 379 |
| 380 ht.ExpectUniqueSample("ComponentUpdater.Calls.Count", 0, 1); |
| 381 ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); |
| 382 ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); |
| 362 } | 383 } |
| 363 | 384 |
| 364 } // namespace component_updater | 385 } // namespace component_updater |
| OLD | NEW |