OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
7 #include "chrome/browser/extensions/extension_updater.h" | 7 #include "chrome/browser/extensions/extension_updater.h" |
8 #include "chrome/browser/extensions/extensions_service.h" | 8 #include "chrome/browser/extensions/extensions_service.h" |
9 #include "chrome/browser/net/test_url_fetcher_factory.h" | 9 #include "chrome/browser/net/test_url_fetcher_factory.h" |
10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 public: | 87 public: |
88 MockService() {} | 88 MockService() {} |
89 virtual ~MockService() {} | 89 virtual ~MockService() {} |
90 | 90 |
91 virtual const ExtensionList* extensions() const { | 91 virtual const ExtensionList* extensions() const { |
92 EXPECT_TRUE(false); | 92 EXPECT_TRUE(false); |
93 return NULL; | 93 return NULL; |
94 } | 94 } |
95 | 95 |
96 virtual void UpdateExtension(const std::string& id, | 96 virtual void UpdateExtension(const std::string& id, |
97 const FilePath& extension_path, | 97 const FilePath& extension_path) { |
98 bool alert_on_error, | |
99 ExtensionInstallCallback* callback) { | |
100 EXPECT_TRUE(false); | 98 EXPECT_TRUE(false); |
101 } | 99 } |
102 | 100 |
103 virtual Extension* GetExtensionById(const std::string& id) { | 101 virtual Extension* GetExtensionById(const std::string& id) { |
104 EXPECT_TRUE(false); | 102 EXPECT_TRUE(false); |
105 return NULL; | 103 return NULL; |
106 } | 104 } |
107 | 105 |
108 private: | 106 private: |
109 DISALLOW_COPY_AND_ASSIGN(MockService); | 107 DISALLOW_COPY_AND_ASSIGN(MockService); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 void set_extensions(ExtensionList extensions) { | 151 void set_extensions(ExtensionList extensions) { |
154 extensions_ = extensions; | 152 extensions_ = extensions; |
155 } | 153 } |
156 | 154 |
157 private: | 155 private: |
158 ExtensionList extensions_; | 156 ExtensionList extensions_; |
159 }; | 157 }; |
160 | 158 |
161 class ServiceForDownloadTests : public MockService { | 159 class ServiceForDownloadTests : public MockService { |
162 public: | 160 public: |
163 explicit ServiceForDownloadTests() : callback_(NULL) {} | |
164 virtual ~ServiceForDownloadTests() { | |
165 // expect that cleanup happened via FireInstallCallback | |
166 EXPECT_EQ(NULL, callback_); | |
167 EXPECT_TRUE(install_path_.empty()); | |
168 } | |
169 | |
170 virtual void UpdateExtension(const std::string& id, | 161 virtual void UpdateExtension(const std::string& id, |
171 const FilePath& extension_path, | 162 const FilePath& extension_path) { |
172 bool alert_on_error, | |
173 ExtensionInstallCallback* callback) { | |
174 // Since this mock only has support for one oustanding update, ensure | |
175 // that the callback doesn't need to be run. | |
176 EXPECT_TRUE(install_path_.empty()); | |
177 EXPECT_EQ(NULL, callback_); | |
178 | |
179 extension_id_ = id; | 163 extension_id_ = id; |
180 install_path_ = extension_path; | 164 install_path_ = extension_path; |
181 callback_ = callback; | |
182 } | |
183 | |
184 void FireInstallCallback() { | |
185 EXPECT_TRUE(callback_ != NULL); | |
186 callback_->Run(install_path_, static_cast<Extension*>(NULL)); | |
187 delete callback_; | |
188 callback_ = NULL; | |
189 install_path_ = FilePath(); | |
190 } | 165 } |
191 | 166 |
192 const std::string& extension_id() { return extension_id_; } | 167 const std::string& extension_id() { return extension_id_; } |
193 const FilePath& install_path() { return install_path_; } | 168 const FilePath& install_path() { return install_path_; } |
194 | 169 |
195 private: | 170 private: |
196 std::string extension_id_; | 171 std::string extension_id_; |
197 FilePath install_path_; | 172 FilePath install_path_; |
198 ExtensionInstallCallback* callback_; | |
199 }; | 173 }; |
200 | 174 |
201 static const int kUpdateFrequencySecs = 15; | 175 static const int kUpdateFrequencySecs = 15; |
202 | 176 |
203 // Takes a string with KEY=VALUE parameters separated by '&' in |params| and | 177 // Takes a string with KEY=VALUE parameters separated by '&' in |params| and |
204 // puts the key/value pairs into |result|. For keys with no value, the empty | 178 // puts the key/value pairs into |result|. For keys with no value, the empty |
205 // string is used. So for "a=1&b=foo&c", result would map "a" to "1", "b" to | 179 // string is used. So for "a=1&b=foo&c", result would map "a" to "1", "b" to |
206 // "foo", and "c" to "". | 180 // "foo", and "c" to "". |
207 static void ExtractParameters(const std::string params, | 181 static void ExtractParameters(const std::string params, |
208 std::map<std::string, std::string>* result) { | 182 std::map<std::string, std::string>* result) { |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 | 383 |
410 // Expect that ExtensionUpdater asked the mock extensions service to install | 384 // Expect that ExtensionUpdater asked the mock extensions service to install |
411 // a file with the test data for the right id. | 385 // a file with the test data for the right id. |
412 EXPECT_EQ(id, service.extension_id()); | 386 EXPECT_EQ(id, service.extension_id()); |
413 FilePath tmpfile_path = service.install_path(); | 387 FilePath tmpfile_path = service.install_path(); |
414 EXPECT_FALSE(tmpfile_path.empty()); | 388 EXPECT_FALSE(tmpfile_path.empty()); |
415 std::string file_contents; | 389 std::string file_contents; |
416 EXPECT_TRUE(file_util::ReadFileToString(tmpfile_path, &file_contents)); | 390 EXPECT_TRUE(file_util::ReadFileToString(tmpfile_path, &file_contents)); |
417 EXPECT_TRUE(extension_data == file_contents); | 391 EXPECT_TRUE(extension_data == file_contents); |
418 | 392 |
419 service.FireInstallCallback(); | |
420 | |
421 message_loop.RunAllPending(); | |
422 | |
423 // Make sure the temp file is cleaned up | |
424 EXPECT_FALSE(file_util::PathExists(tmpfile_path)); | |
425 | |
426 URLFetcher::set_factory(NULL); | 393 URLFetcher::set_factory(NULL); |
427 } | 394 } |
428 | 395 |
429 static void TestMultipleExtensionDownloading() { | 396 static void TestMultipleExtensionDownloading() { |
430 MessageLoopForUI message_loop; | 397 MessageLoopForUI message_loop; |
431 TestURLFetcherFactory factory; | 398 TestURLFetcherFactory factory; |
432 TestURLFetcher* fetcher = NULL; | 399 TestURLFetcher* fetcher = NULL; |
433 URLFetcher::set_factory(&factory); | 400 URLFetcher::set_factory(&factory); |
434 ServiceForDownloadTests service; | 401 ServiceForDownloadTests service; |
435 scoped_refptr<ExtensionUpdater> updater = | 402 scoped_refptr<ExtensionUpdater> updater = |
(...skipping 11 matching lines...) Expand all Loading... |
447 | 414 |
448 // Make the first fetch complete. | 415 // Make the first fetch complete. |
449 std::string extension_data1("whatever"); | 416 std::string extension_data1("whatever"); |
450 fetcher = factory.GetFetcherByID(ExtensionUpdater::kExtensionFetcherId); | 417 fetcher = factory.GetFetcherByID(ExtensionUpdater::kExtensionFetcherId); |
451 EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL); | 418 EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL); |
452 fetcher->delegate()->OnURLFetchComplete( | 419 fetcher->delegate()->OnURLFetchComplete( |
453 fetcher, url1, URLRequestStatus(), 200, ResponseCookies(), | 420 fetcher, url1, URLRequestStatus(), 200, ResponseCookies(), |
454 extension_data1); | 421 extension_data1); |
455 message_loop.RunAllPending(); | 422 message_loop.RunAllPending(); |
456 | 423 |
457 // Expect that the service was asked to do an install with the right data, | 424 // Expect that the service was asked to do an install with the right data. |
458 // and fire the callback indicating the install finished. | |
459 FilePath tmpfile_path = service.install_path(); | 425 FilePath tmpfile_path = service.install_path(); |
460 EXPECT_FALSE(tmpfile_path.empty()); | 426 EXPECT_FALSE(tmpfile_path.empty()); |
461 EXPECT_EQ(id1, service.extension_id()); | 427 EXPECT_EQ(id1, service.extension_id()); |
462 service.FireInstallCallback(); | |
463 | |
464 // Make sure the tempfile got cleaned up. | |
465 message_loop.RunAllPending(); | 428 message_loop.RunAllPending(); |
466 EXPECT_FALSE(tmpfile_path.empty()); | |
467 EXPECT_FALSE(file_util::PathExists(tmpfile_path)); | |
468 | 429 |
469 // Make sure the second fetch finished and asked the service to do an | 430 // Make sure the second fetch finished and asked the service to do an |
470 // update. | 431 // update. |
471 std::string extension_data2("whatever2"); | 432 std::string extension_data2("whatever2"); |
472 fetcher = factory.GetFetcherByID(ExtensionUpdater::kExtensionFetcherId); | 433 fetcher = factory.GetFetcherByID(ExtensionUpdater::kExtensionFetcherId); |
473 EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL); | 434 EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL); |
474 fetcher->delegate()->OnURLFetchComplete( | 435 fetcher->delegate()->OnURLFetchComplete( |
475 fetcher, url2, URLRequestStatus(), 200, ResponseCookies(), | 436 fetcher, url2, URLRequestStatus(), 200, ResponseCookies(), |
476 extension_data2); | 437 extension_data2); |
477 message_loop.RunAllPending(); | 438 message_loop.RunAllPending(); |
478 EXPECT_EQ(id2, service.extension_id()); | 439 EXPECT_EQ(id2, service.extension_id()); |
479 EXPECT_FALSE(service.install_path().empty()); | 440 EXPECT_FALSE(service.install_path().empty()); |
480 | 441 |
481 // Make sure the correct crx contents were passed for the update call. | 442 // Make sure the correct crx contents were passed for the update call. |
482 std::string file_contents; | 443 std::string file_contents; |
483 EXPECT_TRUE(file_util::ReadFileToString(service.install_path(), | 444 EXPECT_TRUE(file_util::ReadFileToString(service.install_path(), |
484 &file_contents)); | 445 &file_contents)); |
485 EXPECT_TRUE(extension_data2 == file_contents); | 446 EXPECT_TRUE(extension_data2 == file_contents); |
486 service.FireInstallCallback(); | |
487 message_loop.RunAllPending(); | |
488 } | 447 } |
489 }; | 448 }; |
490 | 449 |
491 // Because we test some private methods of ExtensionUpdater, it's easer for the | 450 // Because we test some private methods of ExtensionUpdater, it's easer for the |
492 // actual test code to live in ExtenionUpdaterTest methods instead of TEST_F | 451 // actual test code to live in ExtenionUpdaterTest methods instead of TEST_F |
493 // subclasses where friendship with ExtenionUpdater is not inherited. | 452 // subclasses where friendship with ExtenionUpdater is not inherited. |
494 | 453 |
495 TEST(ExtensionUpdaterTest, TestXmlParsing) { | 454 TEST(ExtensionUpdaterTest, TestXmlParsing) { |
496 ExtensionUpdaterTest::TestXmlParsing(); | 455 ExtensionUpdaterTest::TestXmlParsing(); |
497 } | 456 } |
(...skipping 23 matching lines...) Expand all Loading... |
521 // -prodversionmin (shouldn't update if browser version too old) | 480 // -prodversionmin (shouldn't update if browser version too old) |
522 // -manifests & updates arriving out of order / interleaved | 481 // -manifests & updates arriving out of order / interleaved |
523 // -Profile::GetDefaultRequestContext() returning null | 482 // -Profile::GetDefaultRequestContext() returning null |
524 // (should not crash, but just do check later) | 483 // (should not crash, but just do check later) |
525 // -malformed update url (empty, file://, has query, has a # fragment, etc.) | 484 // -malformed update url (empty, file://, has query, has a # fragment, etc.) |
526 // -An extension gets uninstalled while updates are in progress (so it doesn't | 485 // -An extension gets uninstalled while updates are in progress (so it doesn't |
527 // "come back from the dead") | 486 // "come back from the dead") |
528 // -An extension gets manually updated to v3 while we're downloading v2 (ie | 487 // -An extension gets manually updated to v3 while we're downloading v2 (ie |
529 // you don't get downgraded accidentally) | 488 // you don't get downgraded accidentally) |
530 // -An update manifest mentions multiple updates | 489 // -An update manifest mentions multiple updates |
OLD | NEW |