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
3 // found in the LICENSE file.
6 #include "base/bind_helpers.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/location.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/path_service.h"
14 #include "base/run_loop.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "base/values.h"
17 #include "base/version.h"
18 #include "components/update_client/crx_update_item.h"
19 #include "components/update_client/ping_manager.h"
20 #include "components/update_client/test/test_configurator.h"
21 #include "components/update_client/test/test_installer.h"
22 #include "components/update_client/update_checker.h"
23 #include "components/update_client/update_client_internal.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
30 namespace update_client
{
36 // Makes a copy of the file specified by |from_path| in a temporary directory
37 // and returns the path of the copy. Returns true if successful. Cleans up if
38 // there was an error creating the copy.
39 bool MakeTestFile(const FilePath
& from_path
, FilePath
* to_path
) {
42 CreateNewTempDirectory(FILE_PATH_LITERAL("update_client"), &temp_dir
);
47 result
= CreateTemporaryFileInDir(temp_dir
, &temp_file
);
51 result
= CopyFile(from_path
, temp_file
);
53 DeleteFile(temp_file
, false);
61 using Events
= UpdateClient::Observer::Events
;
63 class MockObserver
: public UpdateClient::Observer
{
65 MOCK_METHOD2(OnEvent
, void(Events event
, const std::string
&));
68 class FakePingManagerImpl
: public PingManager
{
70 explicit FakePingManagerImpl(const Configurator
& config
);
71 ~FakePingManagerImpl() override
;
73 void OnUpdateComplete(const CrxUpdateItem
* item
) override
;
75 const std::vector
<CrxUpdateItem
>& items() const;
78 std::vector
<CrxUpdateItem
> items_
;
79 DISALLOW_COPY_AND_ASSIGN(FakePingManagerImpl
);
82 FakePingManagerImpl::FakePingManagerImpl(const Configurator
& config
)
83 : PingManager(config
) {
86 FakePingManagerImpl::~FakePingManagerImpl() {
89 void FakePingManagerImpl::OnUpdateComplete(const CrxUpdateItem
* item
) {
90 items_
.push_back(*item
);
93 const std::vector
<CrxUpdateItem
>& FakePingManagerImpl::items() const {
100 using ::testing::AnyNumber
;
101 using ::testing::DoAll
;
102 using ::testing::InSequence
;
103 using ::testing::Invoke
;
104 using ::testing::Mock
;
105 using ::testing::Return
;
107 using content::BrowserThread
;
111 class UpdateClientTest
: public testing::Test
{
114 ~UpdateClientTest() override
;
116 void SetUp() override
;
117 void TearDown() override
;
122 // Returns the full path to a test file.
123 static base::FilePath
TestFilePath(const char* file
);
125 content::TestBrowserThreadBundle thread_bundle_
;
127 base::RunLoop runloop_
;
128 base::Closure quit_closure_
;
130 scoped_refptr
<update_client::TestConfigurator
> config_
;
133 DISALLOW_COPY_AND_ASSIGN(UpdateClientTest
);
136 UpdateClientTest::UpdateClientTest()
137 : config_(new TestConfigurator(
138 BrowserThread::GetBlockingPool()
139 ->GetSequencedTaskRunnerWithShutdownBehavior(
140 BrowserThread::GetBlockingPool()->GetSequenceToken(),
141 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
),
142 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
))) {
145 UpdateClientTest::~UpdateClientTest() {
148 void UpdateClientTest::SetUp() {
149 quit_closure_
= runloop_
.QuitClosure();
152 void UpdateClientTest::TearDown() {
155 void UpdateClientTest::RunThreads() {
159 base::FilePath
UpdateClientTest::TestFilePath(const char* file
) {
161 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
162 return path
.AppendASCII("components")
165 .AppendASCII("update_client")
169 // Tests the scenario where one update check is done for one CRX. The CRX
171 TEST_F(UpdateClientTest
, OneCrxNoUpdate
) {
172 class DataCallbackFake
{
174 static void Callback(const std::vector
<std::string
>& ids
,
175 std::vector
<CrxComponent
>* components
) {
177 crx
.name
= "test_jebg";
178 crx
.pk_hash
.assign(jebg_hash
, jebg_hash
+ arraysize(jebg_hash
));
179 crx
.version
= Version("0.9");
180 crx
.installer
= new TestInstaller
;
181 components
->push_back(crx
);
185 class CompletionCallbackFake
{
187 static void Callback(const base::Closure
& quit_closure
, int error
) {
193 class FakeUpdateChecker
: public UpdateChecker
{
195 static scoped_ptr
<UpdateChecker
> Create(const Configurator
& config
) {
196 return scoped_ptr
<UpdateChecker
>(new FakeUpdateChecker());
199 bool CheckForUpdates(
200 const std::vector
<CrxUpdateItem
*>& items_to_check
,
201 const std::string
& additional_attributes
,
202 const UpdateCheckCallback
& update_check_callback
) override
{
203 base::ThreadTaskRunnerHandle::Get()->PostTask(
204 FROM_HERE
, base::Bind(update_check_callback
, GURL(), 0, "",
205 UpdateResponse::Results()));
210 class FakeCrxDownloader
: public CrxDownloader
{
212 static scoped_ptr
<CrxDownloader
> Create(
213 bool is_background_download
,
214 net::URLRequestContextGetter
* context_getter
,
215 const scoped_refptr
<base::SequencedTaskRunner
>& url_fetcher_task_runner
,
216 const scoped_refptr
<base::SingleThreadTaskRunner
>&
217 background_task_runner
) {
218 return scoped_ptr
<CrxDownloader
>(new FakeCrxDownloader());
222 FakeCrxDownloader() : CrxDownloader(scoped_ptr
<CrxDownloader
>().Pass()) {}
223 ~FakeCrxDownloader() override
{}
225 void DoStartDownload(const GURL
& url
) override
{ EXPECT_TRUE(false); }
228 class FakePingManager
: public FakePingManagerImpl
{
230 explicit FakePingManager(const Configurator
& config
)
231 : FakePingManagerImpl(config
) {}
232 ~FakePingManager() override
{ EXPECT_TRUE(items().empty()); }
235 scoped_ptr
<PingManager
> ping_manager(new FakePingManager(*config_
));
236 scoped_ptr
<UpdateClient
> update_client(new UpdateClientImpl(
237 config_
, ping_manager
.Pass(), &FakeUpdateChecker::Create
,
238 &FakeCrxDownloader::Create
));
240 MockObserver observer
;
242 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES
,
243 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
244 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_NOT_UPDATED
,
245 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
247 update_client
->AddObserver(&observer
);
249 std::vector
<std::string
> ids
;
250 ids
.push_back(std::string("jebgalgnebhfojomionfpkfelancnnkf"));
252 update_client
->Update(
253 ids
, base::Bind(&DataCallbackFake::Callback
),
254 base::Bind(&CompletionCallbackFake::Callback
, quit_closure_
));
258 update_client
->RemoveObserver(&observer
);
261 // Tests the scenario where two CRXs are checked for updates. On CRX has
262 // an update, the other CRX does not.
263 TEST_F(UpdateClientTest
, TwoCrxUpdateNoUpdate
) {
264 class DataCallbackFake
{
266 static void Callback(const std::vector
<std::string
>& ids
,
267 std::vector
<CrxComponent
>* components
) {
269 crx1
.name
= "test_jebg";
270 crx1
.pk_hash
.assign(jebg_hash
, jebg_hash
+ arraysize(jebg_hash
));
271 crx1
.version
= Version("0.9");
272 crx1
.installer
= new TestInstaller
;
275 crx2
.name
= "test_abag";
276 crx2
.pk_hash
.assign(abag_hash
, abag_hash
+ arraysize(abag_hash
));
277 crx2
.version
= Version("2.2");
278 crx2
.installer
= new TestInstaller
;
280 components
->push_back(crx1
);
281 components
->push_back(crx2
);
285 class CompletionCallbackFake
{
287 static void Callback(const base::Closure
& quit_closure
, int error
) {
293 class FakeUpdateChecker
: public UpdateChecker
{
295 static scoped_ptr
<UpdateChecker
> Create(const Configurator
& config
) {
296 return scoped_ptr
<UpdateChecker
>(new FakeUpdateChecker());
299 bool CheckForUpdates(
300 const std::vector
<CrxUpdateItem
*>& items_to_check
,
301 const std::string
& additional_attributes
,
302 const UpdateCheckCallback
& update_check_callback
) override
{
304 Fake the following response:
306 <?xml version='1.0' encoding='UTF-8'?>
307 <response protocol='3.0'>
308 <app appid='jebgalgnebhfojomionfpkfelancnnkf'>
309 <updatecheck status='ok'>
311 <url codebase='http://localhost/download/'/>
313 <manifest version='1.0' prodversionmin='11.0.1.0'>
315 <package name='jebgalgnebhfojomionfpkfelancnnkf.crx'/>
322 UpdateResponse::Result::Manifest::Package package
;
323 package
.name
= "jebgalgnebhfojomionfpkfelancnnkf.crx";
325 UpdateResponse::Result result
;
326 result
.extension_id
= "jebgalgnebhfojomionfpkfelancnnkf";
327 result
.crx_urls
.push_back(GURL("http://localhost/download/"));
328 result
.manifest
.version
= "1.0";
329 result
.manifest
.browser_min_version
= "11.0.1.0";
330 result
.manifest
.packages
.push_back(package
);
332 UpdateResponse::Results results
;
333 results
.list
.push_back(result
);
335 base::ThreadTaskRunnerHandle::Get()->PostTask(
336 FROM_HERE
, base::Bind(update_check_callback
, GURL(), 0, "", results
));
341 class FakeCrxDownloader
: public CrxDownloader
{
343 static scoped_ptr
<CrxDownloader
> Create(
344 bool is_background_download
,
345 net::URLRequestContextGetter
* context_getter
,
346 const scoped_refptr
<base::SequencedTaskRunner
>& url_fetcher_task_runner
,
347 const scoped_refptr
<base::SingleThreadTaskRunner
>&
348 background_task_runner
) {
349 return scoped_ptr
<CrxDownloader
>(new FakeCrxDownloader());
353 FakeCrxDownloader() : CrxDownloader(scoped_ptr
<CrxDownloader
>().Pass()) {}
354 ~FakeCrxDownloader() override
{}
356 void DoStartDownload(const GURL
& url
) override
{
357 DownloadMetrics download_metrics
;
358 download_metrics
.url
= url
;
359 download_metrics
.downloader
= DownloadMetrics::kNone
;
360 download_metrics
.error
= 0;
361 download_metrics
.downloaded_bytes
= 1843;
362 download_metrics
.total_bytes
= 1843;
363 download_metrics
.download_time_ms
= 1000;
366 EXPECT_TRUE(MakeTestFile(
367 TestFilePath("jebgalgnebhfojomionfpkfelancnnkf.crx"), &path
));
371 result
.response
= path
;
372 result
.downloaded_bytes
= 1843;
373 result
.total_bytes
= 1843;
375 base::ThreadTaskRunnerHandle::Get()->PostTask(
376 FROM_HERE
, base::Bind(&FakeCrxDownloader::OnDownloadProgress
,
377 base::Unretained(this), result
));
379 base::ThreadTaskRunnerHandle::Get()->PostTask(
381 base::Bind(&FakeCrxDownloader::OnDownloadComplete
,
382 base::Unretained(this), true, result
, download_metrics
));
386 class FakePingManager
: public FakePingManagerImpl
{
388 explicit FakePingManager(const Configurator
& config
)
389 : FakePingManagerImpl(config
) {}
390 ~FakePingManager() override
{
391 const auto& ping_items
= items();
392 EXPECT_EQ(1U, ping_items
.size());
393 EXPECT_EQ("jebgalgnebhfojomionfpkfelancnnkf", ping_items
[0].id
);
394 EXPECT_TRUE(base::Version("0.9").Equals(ping_items
[0].previous_version
));
395 EXPECT_TRUE(base::Version("1.0").Equals(ping_items
[0].next_version
));
396 EXPECT_EQ(0, ping_items
[0].error_category
);
397 EXPECT_EQ(0, ping_items
[0].error_code
);
401 scoped_ptr
<PingManager
> ping_manager(new FakePingManager(*config_
));
402 scoped_ptr
<UpdateClient
> update_client(new UpdateClientImpl(
403 config_
, ping_manager
.Pass(), &FakeUpdateChecker::Create
,
404 &FakeCrxDownloader::Create
));
406 MockObserver observer
;
409 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES
,
410 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
411 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_FOUND
,
412 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
413 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING
,
414 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
415 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_READY
,
416 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
417 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATED
,
418 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
422 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES
,
423 "abagagagagagagagagagagagagagagag")).Times(1);
424 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_NOT_UPDATED
,
425 "abagagagagagagagagagagagagagagag")).Times(1);
428 update_client
->AddObserver(&observer
);
430 std::vector
<std::string
> ids
;
431 ids
.push_back(std::string("jebgalgnebhfojomionfpkfelancnnkf"));
432 ids
.push_back(std::string("abagagagagagagagagagagagagagagag"));
434 update_client
->Update(
435 ids
, base::Bind(&DataCallbackFake::Callback
),
436 base::Bind(&CompletionCallbackFake::Callback
, quit_closure_
));
440 update_client
->RemoveObserver(&observer
);
443 // Tests the update check for two CRXs scenario. Both CRXs have updates.
444 TEST_F(UpdateClientTest
, TwoCrxUpdate
) {
445 class DataCallbackFake
{
447 static void Callback(const std::vector
<std::string
>& ids
,
448 std::vector
<CrxComponent
>* components
) {
450 crx1
.name
= "test_jebg";
451 crx1
.pk_hash
.assign(jebg_hash
, jebg_hash
+ arraysize(jebg_hash
));
452 crx1
.version
= Version("0.9");
453 crx1
.installer
= new TestInstaller
;
456 crx2
.name
= "test_ihfo";
457 crx2
.pk_hash
.assign(ihfo_hash
, ihfo_hash
+ arraysize(ihfo_hash
));
458 crx2
.version
= Version("0.8");
459 crx2
.installer
= new TestInstaller
;
461 components
->push_back(crx1
);
462 components
->push_back(crx2
);
466 class CompletionCallbackFake
{
468 static void Callback(const base::Closure
& quit_closure
, int error
) {
474 class FakeUpdateChecker
: public UpdateChecker
{
476 static scoped_ptr
<UpdateChecker
> Create(const Configurator
& config
) {
477 return scoped_ptr
<UpdateChecker
>(new FakeUpdateChecker());
480 bool CheckForUpdates(
481 const std::vector
<CrxUpdateItem
*>& items_to_check
,
482 const std::string
& additional_attributes
,
483 const UpdateCheckCallback
& update_check_callback
) override
{
485 Fake the following response:
487 <?xml version='1.0' encoding='UTF-8'?>
488 <response protocol='3.0'>
489 <app appid='jebgalgnebhfojomionfpkfelancnnkf'>
490 <updatecheck status='ok'>
492 <url codebase='http://localhost/download/'/>
494 <manifest version='1.0' prodversionmin='11.0.1.0'>
496 <package name='jebgalgnebhfojomionfpkfelancnnkf.crx'/>
501 <app appid='ihfokbkgjpifnbbojhneepfflplebdkc'>
502 <updatecheck status='ok'>
504 <url codebase='http://localhost/download/'/>
506 <manifest version='1.0' prodversionmin='11.0.1.0'>
508 <package name='ihfokbkgjpifnbbojhneepfflplebdkc_1.crx'/>
515 UpdateResponse::Result::Manifest::Package package1
;
516 package1
.name
= "jebgalgnebhfojomionfpkfelancnnkf.crx";
518 UpdateResponse::Result result1
;
519 result1
.extension_id
= "jebgalgnebhfojomionfpkfelancnnkf";
520 result1
.crx_urls
.push_back(GURL("http://localhost/download/"));
521 result1
.manifest
.version
= "1.0";
522 result1
.manifest
.browser_min_version
= "11.0.1.0";
523 result1
.manifest
.packages
.push_back(package1
);
525 UpdateResponse::Result::Manifest::Package package2
;
526 package2
.name
= "ihfokbkgjpifnbbojhneepfflplebdkc_1.crx";
528 UpdateResponse::Result result2
;
529 result2
.extension_id
= "ihfokbkgjpifnbbojhneepfflplebdkc";
530 result2
.crx_urls
.push_back(GURL("http://localhost/download/"));
531 result2
.manifest
.version
= "1.0";
532 result2
.manifest
.browser_min_version
= "11.0.1.0";
533 result2
.manifest
.packages
.push_back(package2
);
535 UpdateResponse::Results results
;
536 results
.list
.push_back(result1
);
537 results
.list
.push_back(result2
);
539 base::ThreadTaskRunnerHandle::Get()->PostTask(
540 FROM_HERE
, base::Bind(update_check_callback
, GURL(), 0, "", results
));
545 class FakeCrxDownloader
: public CrxDownloader
{
547 static scoped_ptr
<CrxDownloader
> Create(
548 bool is_background_download
,
549 net::URLRequestContextGetter
* context_getter
,
550 const scoped_refptr
<base::SequencedTaskRunner
>& url_fetcher_task_runner
,
551 const scoped_refptr
<base::SingleThreadTaskRunner
>&
552 background_task_runner
) {
553 return scoped_ptr
<CrxDownloader
>(new FakeCrxDownloader());
557 FakeCrxDownloader() : CrxDownloader(scoped_ptr
<CrxDownloader
>().Pass()) {}
558 ~FakeCrxDownloader() override
{}
560 void DoStartDownload(const GURL
& url
) override
{
561 DownloadMetrics download_metrics
;
564 if (url
.path() == "/download/jebgalgnebhfojomionfpkfelancnnkf.crx") {
565 download_metrics
.url
= url
;
566 download_metrics
.downloader
= DownloadMetrics::kNone
;
567 download_metrics
.error
= 0;
568 download_metrics
.downloaded_bytes
= 1843;
569 download_metrics
.total_bytes
= 1843;
570 download_metrics
.download_time_ms
= 1000;
572 EXPECT_TRUE(MakeTestFile(
573 TestFilePath("jebgalgnebhfojomionfpkfelancnnkf.crx"), &path
));
576 result
.response
= path
;
577 result
.downloaded_bytes
= 1843;
578 result
.total_bytes
= 1843;
579 } else if (url
.path() ==
580 "/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx") {
581 download_metrics
.url
= url
;
582 download_metrics
.downloader
= DownloadMetrics::kNone
;
583 download_metrics
.error
= 0;
584 download_metrics
.downloaded_bytes
= 53638;
585 download_metrics
.total_bytes
= 53638;
586 download_metrics
.download_time_ms
= 2000;
588 EXPECT_TRUE(MakeTestFile(
589 TestFilePath("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"), &path
));
592 result
.response
= path
;
593 result
.downloaded_bytes
= 53638;
594 result
.total_bytes
= 53638;
599 base::ThreadTaskRunnerHandle::Get()->PostTask(
600 FROM_HERE
, base::Bind(&FakeCrxDownloader::OnDownloadProgress
,
601 base::Unretained(this), result
));
603 base::ThreadTaskRunnerHandle::Get()->PostTask(
605 base::Bind(&FakeCrxDownloader::OnDownloadComplete
,
606 base::Unretained(this), true, result
, download_metrics
));
610 class FakePingManager
: public FakePingManagerImpl
{
612 explicit FakePingManager(const Configurator
& config
)
613 : FakePingManagerImpl(config
) {}
614 ~FakePingManager() override
{
615 const auto& ping_items
= items();
616 EXPECT_EQ(2U, ping_items
.size());
617 EXPECT_EQ("jebgalgnebhfojomionfpkfelancnnkf", ping_items
[0].id
);
618 EXPECT_TRUE(base::Version("0.9").Equals(ping_items
[0].previous_version
));
619 EXPECT_TRUE(base::Version("1.0").Equals(ping_items
[0].next_version
));
620 EXPECT_EQ(0, ping_items
[0].error_category
);
621 EXPECT_EQ(0, ping_items
[0].error_code
);
622 EXPECT_EQ("ihfokbkgjpifnbbojhneepfflplebdkc", ping_items
[1].id
);
623 EXPECT_TRUE(base::Version("0.8").Equals(ping_items
[1].previous_version
));
624 EXPECT_TRUE(base::Version("1.0").Equals(ping_items
[1].next_version
));
625 EXPECT_EQ(0, ping_items
[1].error_category
);
626 EXPECT_EQ(0, ping_items
[1].error_code
);
630 scoped_ptr
<FakePingManager
> ping_manager(new FakePingManager(*config_
));
631 scoped_ptr
<UpdateClient
> update_client(new UpdateClientImpl(
632 config_
, ping_manager
.Pass(), &FakeUpdateChecker::Create
,
633 &FakeCrxDownloader::Create
));
635 MockObserver observer
;
638 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES
,
639 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
640 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_FOUND
,
641 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
642 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING
,
643 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
644 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_READY
,
645 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
646 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATED
,
647 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
651 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES
,
652 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
653 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_FOUND
,
654 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
655 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_WAIT
,
656 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
657 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING
,
658 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
659 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_READY
,
660 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
661 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATED
,
662 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
665 update_client
->AddObserver(&observer
);
667 std::vector
<std::string
> ids
;
668 ids
.push_back(std::string("jebgalgnebhfojomionfpkfelancnnkf"));
669 ids
.push_back(std::string("ihfokbkgjpifnbbojhneepfflplebdkc"));
671 update_client
->Update(
672 ids
, base::Bind(&DataCallbackFake::Callback
),
673 base::Bind(&CompletionCallbackFake::Callback
, quit_closure_
));
677 update_client
->RemoveObserver(&observer
);
680 // Tests the differential update scenario for one CRX.
681 TEST_F(UpdateClientTest
, OneCrxDiffUpdate
) {
682 class DataCallbackFake
{
684 static void Callback(const std::vector
<std::string
>& ids
,
685 std::vector
<CrxComponent
>* components
) {
686 static int num_calls
= 0;
688 // Must use the same stateful installer object.
689 static scoped_refptr
<CrxInstaller
> installer(
690 new VersionedTestInstaller());
695 crx
.name
= "test_ihfo";
696 crx
.pk_hash
.assign(ihfo_hash
, ihfo_hash
+ arraysize(ihfo_hash
));
697 crx
.installer
= installer
;
698 if (num_calls
== 1) {
699 crx
.version
= Version("0.8");
700 } else if (num_calls
== 2) {
701 crx
.version
= Version("1.0");
706 components
->push_back(crx
);
710 class CompletionCallbackFake
{
712 static void Callback(const base::Closure
& quit_closure
, int error
) {
718 class FakeUpdateChecker
: public UpdateChecker
{
720 static scoped_ptr
<UpdateChecker
> Create(const Configurator
& config
) {
721 return scoped_ptr
<UpdateChecker
>(new FakeUpdateChecker());
724 bool CheckForUpdates(
725 const std::vector
<CrxUpdateItem
*>& items_to_check
,
726 const std::string
& additional_attributes
,
727 const UpdateCheckCallback
& update_check_callback
) override
{
728 static int num_call
= 0;
731 UpdateResponse::Results results
;
735 Fake the following response:
736 <?xml version='1.0' encoding='UTF-8'?>
737 <response protocol='3.0'>
738 <app appid='ihfokbkgjpifnbbojhneepfflplebdkc'>
739 <updatecheck status='ok'>
741 <url codebase='http://localhost/download/'/>
743 <manifest version='1.0' prodversionmin='11.0.1.0'>
745 <package name='ihfokbkgjpifnbbojhneepfflplebdkc_1.crx'/>
752 UpdateResponse::Result::Manifest::Package package
;
753 package
.name
= "ihfokbkgjpifnbbojhneepfflplebdkc_1.crx";
754 package
.fingerprint
= "1";
755 UpdateResponse::Result result
;
756 result
.extension_id
= "ihfokbkgjpifnbbojhneepfflplebdkc";
757 result
.crx_urls
.push_back(GURL("http://localhost/download/"));
758 result
.manifest
.version
= "1.0";
759 result
.manifest
.browser_min_version
= "11.0.1.0";
760 result
.manifest
.packages
.push_back(package
);
761 results
.list
.push_back(result
);
762 } else if (num_call
== 2) {
764 Fake the following response:
765 <?xml version='1.0' encoding='UTF-8'?>
766 <response protocol='3.0'>
767 <app appid='ihfokbkgjpifnbbojhneepfflplebdkc'>
768 <updatecheck status='ok'>
770 <url codebase='http://localhost/download/'/>
771 <url codebasediff='http://localhost/download/'/>
773 <manifest version='2.0' prodversionmin='11.0.1.0'>
775 <package name='ihfokbkgjpifnbbojhneepfflplebdkc_2.crx'
776 namediff='ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx'
784 UpdateResponse::Result::Manifest::Package package
;
785 package
.name
= "ihfokbkgjpifnbbojhneepfflplebdkc_2.crx";
786 package
.namediff
= "ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx";
787 package
.fingerprint
= "22";
788 UpdateResponse::Result result
;
789 result
.extension_id
= "ihfokbkgjpifnbbojhneepfflplebdkc";
790 result
.crx_urls
.push_back(GURL("http://localhost/download/"));
791 result
.crx_diffurls
.push_back(GURL("http://localhost/download/"));
792 result
.manifest
.version
= "2.0";
793 result
.manifest
.browser_min_version
= "11.0.1.0";
794 result
.manifest
.packages
.push_back(package
);
795 results
.list
.push_back(result
);
800 base::ThreadTaskRunnerHandle::Get()->PostTask(
801 FROM_HERE
, base::Bind(update_check_callback
, GURL(), 0, "", results
));
806 class FakeCrxDownloader
: public CrxDownloader
{
808 static scoped_ptr
<CrxDownloader
> Create(
809 bool is_background_download
,
810 net::URLRequestContextGetter
* context_getter
,
811 const scoped_refptr
<base::SequencedTaskRunner
>& url_fetcher_task_runner
,
812 const scoped_refptr
<base::SingleThreadTaskRunner
>&
813 background_task_runner
) {
814 return scoped_ptr
<CrxDownloader
>(new FakeCrxDownloader());
818 FakeCrxDownloader() : CrxDownloader(scoped_ptr
<CrxDownloader
>().Pass()) {}
819 ~FakeCrxDownloader() override
{}
821 void DoStartDownload(const GURL
& url
) override
{
822 DownloadMetrics download_metrics
;
825 if (url
.path() == "/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx") {
826 download_metrics
.url
= url
;
827 download_metrics
.downloader
= DownloadMetrics::kNone
;
828 download_metrics
.error
= 0;
829 download_metrics
.downloaded_bytes
= 53638;
830 download_metrics
.total_bytes
= 53638;
831 download_metrics
.download_time_ms
= 2000;
833 EXPECT_TRUE(MakeTestFile(
834 TestFilePath("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"), &path
));
837 result
.response
= path
;
838 result
.downloaded_bytes
= 53638;
839 result
.total_bytes
= 53638;
840 } else if (url
.path() ==
841 "/download/ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx") {
842 download_metrics
.url
= url
;
843 download_metrics
.downloader
= DownloadMetrics::kNone
;
844 download_metrics
.error
= 0;
845 download_metrics
.downloaded_bytes
= 2105;
846 download_metrics
.total_bytes
= 2105;
847 download_metrics
.download_time_ms
= 1000;
849 EXPECT_TRUE(MakeTestFile(
850 TestFilePath("ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"), &path
));
853 result
.response
= path
;
854 result
.downloaded_bytes
= 2105;
855 result
.total_bytes
= 2105;
860 base::ThreadTaskRunnerHandle::Get()->PostTask(
861 FROM_HERE
, base::Bind(&FakeCrxDownloader::OnDownloadProgress
,
862 base::Unretained(this), result
));
864 base::ThreadTaskRunnerHandle::Get()->PostTask(
866 base::Bind(&FakeCrxDownloader::OnDownloadComplete
,
867 base::Unretained(this), true, result
, download_metrics
));
871 class FakePingManager
: public FakePingManagerImpl
{
873 explicit FakePingManager(const Configurator
& config
)
874 : FakePingManagerImpl(config
) {}
875 ~FakePingManager() override
{
876 const auto& ping_items
= items();
877 EXPECT_EQ(2U, ping_items
.size());
878 EXPECT_EQ("ihfokbkgjpifnbbojhneepfflplebdkc", ping_items
[0].id
);
879 EXPECT_TRUE(base::Version("0.8").Equals(ping_items
[0].previous_version
));
880 EXPECT_TRUE(base::Version("1.0").Equals(ping_items
[0].next_version
));
881 EXPECT_EQ(0, ping_items
[0].error_category
);
882 EXPECT_EQ(0, ping_items
[0].error_code
);
883 EXPECT_EQ("ihfokbkgjpifnbbojhneepfflplebdkc", ping_items
[1].id
);
884 EXPECT_TRUE(base::Version("1.0").Equals(ping_items
[1].previous_version
));
885 EXPECT_TRUE(base::Version("2.0").Equals(ping_items
[1].next_version
));
886 EXPECT_EQ(0, ping_items
[1].diff_error_category
);
887 EXPECT_EQ(0, ping_items
[1].diff_error_code
);
891 scoped_ptr
<FakePingManager
> ping_manager(new FakePingManager(*config_
));
892 scoped_ptr
<UpdateClient
> update_client(new UpdateClientImpl(
893 config_
, ping_manager
.Pass(), &FakeUpdateChecker::Create
,
894 &FakeCrxDownloader::Create
));
896 MockObserver observer
;
899 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES
,
900 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
901 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_FOUND
,
902 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
903 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING
,
904 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
905 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_READY
,
906 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
907 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATED
,
908 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
909 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES
,
910 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
911 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_FOUND
,
912 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
913 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING
,
914 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
915 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_READY
,
916 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
917 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATED
,
918 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
921 update_client
->AddObserver(&observer
);
923 std::vector
<std::string
> ids
;
924 ids
.push_back(std::string("ihfokbkgjpifnbbojhneepfflplebdkc"));
927 base::RunLoop runloop
;
928 update_client
->Update(
929 ids
, base::Bind(&DataCallbackFake::Callback
),
930 base::Bind(&CompletionCallbackFake::Callback
, runloop
.QuitClosure()));
935 base::RunLoop runloop
;
936 update_client
->Update(
937 ids
, base::Bind(&DataCallbackFake::Callback
),
938 base::Bind(&CompletionCallbackFake::Callback
, runloop
.QuitClosure()));
942 update_client
->RemoveObserver(&observer
);
945 // Tests the update scenario for one CRX where the CRX installer returns
947 TEST_F(UpdateClientTest
, OneCrxInstallError
) {
948 class MockInstaller
: public CrxInstaller
{
950 MOCK_METHOD1(OnUpdateError
, void(int error
));
951 MOCK_METHOD2(Install
,
952 bool(const base::DictionaryValue
& manifest
,
953 const base::FilePath
& unpack_path
));
954 MOCK_METHOD2(GetInstalledFile
,
955 bool(const std::string
& file
, base::FilePath
* installed_file
));
956 MOCK_METHOD0(Uninstall
, bool());
958 static void OnInstall(const base::DictionaryValue
& manifest
,
959 const base::FilePath
& unpack_path
) {
960 base::DeleteFile(unpack_path
, true);
964 ~MockInstaller() override
{}
967 class DataCallbackFake
{
969 static void Callback(const std::vector
<std::string
>& ids
,
970 std::vector
<CrxComponent
>* components
) {
971 scoped_refptr
<MockInstaller
> installer(new MockInstaller());
973 EXPECT_CALL(*installer
, OnUpdateError(_
)).Times(0);
974 EXPECT_CALL(*installer
, Install(_
, _
))
975 .WillOnce(DoAll(Invoke(MockInstaller::OnInstall
), Return(false)));
976 EXPECT_CALL(*installer
, GetInstalledFile(_
, _
)).Times(0);
977 EXPECT_CALL(*installer
, Uninstall()).Times(0);
980 crx
.name
= "test_jebg";
981 crx
.pk_hash
.assign(jebg_hash
, jebg_hash
+ arraysize(jebg_hash
));
982 crx
.version
= Version("0.9");
983 crx
.installer
= installer
;
984 components
->push_back(crx
);
988 class CompletionCallbackFake
{
990 static void Callback(const base::Closure
& quit_closure
, int error
) {
996 class FakeUpdateChecker
: public UpdateChecker
{
998 static scoped_ptr
<UpdateChecker
> Create(const Configurator
& config
) {
999 return scoped_ptr
<UpdateChecker
>(new FakeUpdateChecker());
1002 bool CheckForUpdates(
1003 const std::vector
<CrxUpdateItem
*>& items_to_check
,
1004 const std::string
& additional_attributes
,
1005 const UpdateCheckCallback
& update_check_callback
) override
{
1007 Fake the following response:
1009 <?xml version='1.0' encoding='UTF-8'?>
1010 <response protocol='3.0'>
1011 <app appid='jebgalgnebhfojomionfpkfelancnnkf'>
1012 <updatecheck status='ok'>
1014 <url codebase='http://localhost/download/'/>
1016 <manifest version='1.0' prodversionmin='11.0.1.0'>
1018 <package name='jebgalgnebhfojomionfpkfelancnnkf.crx'/>
1025 UpdateResponse::Result::Manifest::Package package
;
1026 package
.name
= "jebgalgnebhfojomionfpkfelancnnkf.crx";
1028 UpdateResponse::Result result
;
1029 result
.extension_id
= "jebgalgnebhfojomionfpkfelancnnkf";
1030 result
.crx_urls
.push_back(GURL("http://localhost/download/"));
1031 result
.manifest
.version
= "1.0";
1032 result
.manifest
.browser_min_version
= "11.0.1.0";
1033 result
.manifest
.packages
.push_back(package
);
1035 UpdateResponse::Results results
;
1036 results
.list
.push_back(result
);
1038 base::ThreadTaskRunnerHandle::Get()->PostTask(
1039 FROM_HERE
, base::Bind(update_check_callback
, GURL(), 0, "", results
));
1044 class FakeCrxDownloader
: public CrxDownloader
{
1046 static scoped_ptr
<CrxDownloader
> Create(
1047 bool is_background_download
,
1048 net::URLRequestContextGetter
* context_getter
,
1049 const scoped_refptr
<base::SequencedTaskRunner
>& url_fetcher_task_runner
,
1050 const scoped_refptr
<base::SingleThreadTaskRunner
>&
1051 background_task_runner
) {
1052 return scoped_ptr
<CrxDownloader
>(new FakeCrxDownloader());
1056 FakeCrxDownloader() : CrxDownloader(scoped_ptr
<CrxDownloader
>().Pass()) {}
1057 ~FakeCrxDownloader() override
{}
1059 void DoStartDownload(const GURL
& url
) override
{
1060 DownloadMetrics download_metrics
;
1061 download_metrics
.url
= url
;
1062 download_metrics
.downloader
= DownloadMetrics::kNone
;
1063 download_metrics
.error
= 0;
1064 download_metrics
.downloaded_bytes
= 1843;
1065 download_metrics
.total_bytes
= 1843;
1066 download_metrics
.download_time_ms
= 1000;
1069 EXPECT_TRUE(MakeTestFile(
1070 TestFilePath("jebgalgnebhfojomionfpkfelancnnkf.crx"), &path
));
1074 result
.response
= path
;
1075 result
.downloaded_bytes
= 1843;
1076 result
.total_bytes
= 1843;
1078 base::ThreadTaskRunnerHandle::Get()->PostTask(
1079 FROM_HERE
, base::Bind(&FakeCrxDownloader::OnDownloadProgress
,
1080 base::Unretained(this), result
));
1082 base::ThreadTaskRunnerHandle::Get()->PostTask(
1084 base::Bind(&FakeCrxDownloader::OnDownloadComplete
,
1085 base::Unretained(this), true, result
, download_metrics
));
1089 class FakePingManager
: public FakePingManagerImpl
{
1091 explicit FakePingManager(const Configurator
& config
)
1092 : FakePingManagerImpl(config
) {}
1093 ~FakePingManager() override
{
1094 const auto& ping_items
= items();
1095 EXPECT_EQ(1U, ping_items
.size());
1096 EXPECT_EQ("jebgalgnebhfojomionfpkfelancnnkf", ping_items
[0].id
);
1097 EXPECT_TRUE(base::Version("0.9").Equals(ping_items
[0].previous_version
));
1098 EXPECT_TRUE(base::Version("1.0").Equals(ping_items
[0].next_version
));
1099 EXPECT_EQ(3, ping_items
[0].error_category
); // kInstallError.
1100 EXPECT_EQ(9, ping_items
[0].error_code
); // kInstallerError.
1104 scoped_ptr
<PingManager
> ping_manager(new FakePingManager(*config_
));
1105 scoped_ptr
<UpdateClient
> update_client(new UpdateClientImpl(
1106 config_
, ping_manager
.Pass(), &FakeUpdateChecker::Create
,
1107 &FakeCrxDownloader::Create
));
1109 MockObserver observer
;
1112 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES
,
1113 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
1114 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_FOUND
,
1115 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
1116 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING
,
1117 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
1118 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_READY
,
1119 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
1120 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_NOT_UPDATED
,
1121 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
1124 update_client
->AddObserver(&observer
);
1126 std::vector
<std::string
> ids
;
1127 ids
.push_back(std::string("jebgalgnebhfojomionfpkfelancnnkf"));
1129 update_client
->Update(
1130 ids
, base::Bind(&DataCallbackFake::Callback
),
1131 base::Bind(&CompletionCallbackFake::Callback
, quit_closure_
));
1135 update_client
->RemoveObserver(&observer
);
1138 // Tests the fallback from differential to full update scenario for one CRX.
1139 TEST_F(UpdateClientTest
, OneCrxDiffUpdateFailsFullUpdateSucceeds
) {
1140 class DataCallbackFake
{
1142 static void Callback(const std::vector
<std::string
>& ids
,
1143 std::vector
<CrxComponent
>* components
) {
1144 static int num_calls
= 0;
1146 // Must use the same stateful installer object.
1147 static scoped_refptr
<CrxInstaller
> installer(
1148 new VersionedTestInstaller());
1153 crx
.name
= "test_ihfo";
1154 crx
.pk_hash
.assign(ihfo_hash
, ihfo_hash
+ arraysize(ihfo_hash
));
1155 crx
.installer
= installer
;
1156 if (num_calls
== 1) {
1157 crx
.version
= Version("0.8");
1158 } else if (num_calls
== 2) {
1159 crx
.version
= Version("1.0");
1164 components
->push_back(crx
);
1168 class CompletionCallbackFake
{
1170 static void Callback(const base::Closure
& quit_closure
, int error
) {
1171 EXPECT_EQ(0, error
);
1176 class FakeUpdateChecker
: public UpdateChecker
{
1178 static scoped_ptr
<UpdateChecker
> Create(const Configurator
& config
) {
1179 return scoped_ptr
<UpdateChecker
>(new FakeUpdateChecker());
1182 bool CheckForUpdates(
1183 const std::vector
<CrxUpdateItem
*>& items_to_check
,
1184 const std::string
& additional_attributes
,
1185 const UpdateCheckCallback
& update_check_callback
) override
{
1186 static int num_call
= 0;
1189 UpdateResponse::Results results
;
1191 if (num_call
== 1) {
1193 Fake the following response:
1194 <?xml version='1.0' encoding='UTF-8'?>
1195 <response protocol='3.0'>
1196 <app appid='ihfokbkgjpifnbbojhneepfflplebdkc'>
1197 <updatecheck status='ok'>
1199 <url codebase='http://localhost/download/'/>
1201 <manifest version='1.0' prodversionmin='11.0.1.0'>
1203 <package name='ihfokbkgjpifnbbojhneepfflplebdkc_1.crx'/>
1210 UpdateResponse::Result::Manifest::Package package
;
1211 package
.name
= "ihfokbkgjpifnbbojhneepfflplebdkc_1.crx";
1212 package
.fingerprint
= "1";
1213 UpdateResponse::Result result
;
1214 result
.extension_id
= "ihfokbkgjpifnbbojhneepfflplebdkc";
1215 result
.crx_urls
.push_back(GURL("http://localhost/download/"));
1216 result
.manifest
.version
= "1.0";
1217 result
.manifest
.browser_min_version
= "11.0.1.0";
1218 result
.manifest
.packages
.push_back(package
);
1219 results
.list
.push_back(result
);
1220 } else if (num_call
== 2) {
1222 Fake the following response:
1223 <?xml version='1.0' encoding='UTF-8'?>
1224 <response protocol='3.0'>
1225 <app appid='ihfokbkgjpifnbbojhneepfflplebdkc'>
1226 <updatecheck status='ok'>
1228 <url codebase='http://localhost/download/'/>
1229 <url codebasediff='http://localhost/download/'/>
1231 <manifest version='2.0' prodversionmin='11.0.1.0'>
1233 <package name='ihfokbkgjpifnbbojhneepfflplebdkc_2.crx'
1234 namediff='ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx'
1242 UpdateResponse::Result::Manifest::Package package
;
1243 package
.name
= "ihfokbkgjpifnbbojhneepfflplebdkc_2.crx";
1244 package
.namediff
= "ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx";
1245 package
.fingerprint
= "22";
1246 UpdateResponse::Result result
;
1247 result
.extension_id
= "ihfokbkgjpifnbbojhneepfflplebdkc";
1248 result
.crx_urls
.push_back(GURL("http://localhost/download/"));
1249 result
.crx_diffurls
.push_back(GURL("http://localhost/download/"));
1250 result
.manifest
.version
= "2.0";
1251 result
.manifest
.browser_min_version
= "11.0.1.0";
1252 result
.manifest
.packages
.push_back(package
);
1253 results
.list
.push_back(result
);
1258 base::ThreadTaskRunnerHandle::Get()->PostTask(
1259 FROM_HERE
, base::Bind(update_check_callback
, GURL(), 0, "", results
));
1264 class FakeCrxDownloader
: public CrxDownloader
{
1266 static scoped_ptr
<CrxDownloader
> Create(
1267 bool is_background_download
,
1268 net::URLRequestContextGetter
* context_getter
,
1269 const scoped_refptr
<base::SequencedTaskRunner
>& url_fetcher_task_runner
,
1270 const scoped_refptr
<base::SingleThreadTaskRunner
>&
1271 background_task_runner
) {
1272 return scoped_ptr
<CrxDownloader
>(new FakeCrxDownloader());
1276 FakeCrxDownloader() : CrxDownloader(scoped_ptr
<CrxDownloader
>().Pass()) {}
1277 ~FakeCrxDownloader() override
{}
1279 void DoStartDownload(const GURL
& url
) override
{
1280 DownloadMetrics download_metrics
;
1283 if (url
.path() == "/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx") {
1284 download_metrics
.url
= url
;
1285 download_metrics
.downloader
= DownloadMetrics::kNone
;
1286 download_metrics
.error
= 0;
1287 download_metrics
.downloaded_bytes
= 53638;
1288 download_metrics
.total_bytes
= 53638;
1289 download_metrics
.download_time_ms
= 2000;
1291 EXPECT_TRUE(MakeTestFile(
1292 TestFilePath("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"), &path
));
1295 result
.response
= path
;
1296 result
.downloaded_bytes
= 53638;
1297 result
.total_bytes
= 53638;
1298 } else if (url
.path() ==
1299 "/download/ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx") {
1300 // A download error is injected on this execution path.
1301 download_metrics
.url
= url
;
1302 download_metrics
.downloader
= DownloadMetrics::kNone
;
1303 download_metrics
.error
= -1;
1304 download_metrics
.downloaded_bytes
= 0;
1305 download_metrics
.total_bytes
= 2105;
1306 download_metrics
.download_time_ms
= 1000;
1308 EXPECT_TRUE(MakeTestFile(
1309 TestFilePath("ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"), &path
));
1312 result
.response
= path
;
1313 result
.downloaded_bytes
= 0;
1314 result
.total_bytes
= 2105;
1315 } else if (url
.path() ==
1316 "/download/ihfokbkgjpifnbbojhneepfflplebdkc_2.crx") {
1317 download_metrics
.url
= url
;
1318 download_metrics
.downloader
= DownloadMetrics::kNone
;
1319 download_metrics
.error
= 0;
1320 download_metrics
.downloaded_bytes
= 53855;
1321 download_metrics
.total_bytes
= 53855;
1322 download_metrics
.download_time_ms
= 1000;
1324 EXPECT_TRUE(MakeTestFile(
1325 TestFilePath("ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"), &path
));
1328 result
.response
= path
;
1329 result
.downloaded_bytes
= 53855;
1330 result
.total_bytes
= 53855;
1333 base::ThreadTaskRunnerHandle::Get()->PostTask(
1334 FROM_HERE
, base::Bind(&FakeCrxDownloader::OnDownloadProgress
,
1335 base::Unretained(this), result
));
1337 base::ThreadTaskRunnerHandle::Get()->PostTask(
1339 base::Bind(&FakeCrxDownloader::OnDownloadComplete
,
1340 base::Unretained(this), true, result
, download_metrics
));
1344 class FakePingManager
: public FakePingManagerImpl
{
1346 explicit FakePingManager(const Configurator
& config
)
1347 : FakePingManagerImpl(config
) {}
1348 ~FakePingManager() override
{
1349 const auto& ping_items
= items();
1350 EXPECT_EQ(2U, ping_items
.size());
1351 EXPECT_EQ("ihfokbkgjpifnbbojhneepfflplebdkc", ping_items
[0].id
);
1352 EXPECT_TRUE(base::Version("0.8").Equals(ping_items
[0].previous_version
));
1353 EXPECT_TRUE(base::Version("1.0").Equals(ping_items
[0].next_version
));
1354 EXPECT_EQ(0, ping_items
[0].error_category
);
1355 EXPECT_EQ(0, ping_items
[0].error_code
);
1356 EXPECT_EQ("ihfokbkgjpifnbbojhneepfflplebdkc", ping_items
[1].id
);
1357 EXPECT_TRUE(base::Version("1.0").Equals(ping_items
[1].previous_version
));
1358 EXPECT_TRUE(base::Version("2.0").Equals(ping_items
[1].next_version
));
1359 EXPECT_TRUE(ping_items
[1].diff_update_failed
);
1360 EXPECT_EQ(1, ping_items
[1].diff_error_category
); // kNetworkError.
1361 EXPECT_EQ(-1, ping_items
[1].diff_error_code
);
1365 scoped_ptr
<FakePingManager
> ping_manager(new FakePingManager(*config_
));
1366 scoped_ptr
<UpdateClient
> update_client(new UpdateClientImpl(
1367 config_
, ping_manager
.Pass(), &FakeUpdateChecker::Create
,
1368 &FakeCrxDownloader::Create
));
1370 MockObserver observer
;
1373 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES
,
1374 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
1375 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_FOUND
,
1376 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
1377 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING
,
1378 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
1379 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_READY
,
1380 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
1381 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATED
,
1382 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
1384 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES
,
1385 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
1386 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_FOUND
,
1387 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
1388 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING
,
1389 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
1390 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING
,
1391 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
1392 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATE_READY
,
1393 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
1394 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATED
,
1395 "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1);
1398 update_client
->AddObserver(&observer
);
1400 std::vector
<std::string
> ids
;
1401 ids
.push_back(std::string("ihfokbkgjpifnbbojhneepfflplebdkc"));
1404 base::RunLoop runloop
;
1405 update_client
->Update(
1406 ids
, base::Bind(&DataCallbackFake::Callback
),
1407 base::Bind(&CompletionCallbackFake::Callback
, runloop
.QuitClosure()));
1412 base::RunLoop runloop
;
1413 update_client
->Update(
1414 ids
, base::Bind(&DataCallbackFake::Callback
),
1415 base::Bind(&CompletionCallbackFake::Callback
, runloop
.QuitClosure()));
1419 update_client
->RemoveObserver(&observer
);
1422 } // namespace update_client