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

Side by Side Diff: chrome/browser/extensions/extension_updater_unittest.cc

Issue 1695018: Adding ExtensionPrefs methods for storing update-when-idle data.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 7 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 | Annotate | Revision Log
OLDNEW
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 <map> 5 #include <map>
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/rand_util.h" 8 #include "base/rand_util.h"
9 #include "base/scoped_ptr.h" 9 #include "base/scoped_ptr.h"
10 #include "base/stl_util-inl.h" 10 #include "base/stl_util-inl.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "base/thread.h" 12 #include "base/thread.h"
13 #include "base/version.h" 13 #include "base/version.h"
14 #include "chrome/browser/chrome_thread.h" 14 #include "chrome/browser/chrome_thread.h"
15 #include "chrome/browser/extensions/extension_updater.h" 15 #include "chrome/browser/extensions/extension_updater.h"
16 #include "chrome/browser/extensions/extensions_service.h" 16 #include "chrome/browser/extensions/extensions_service.h"
17 #include "chrome/browser/json_pref_store.h" 17 #include "chrome/browser/extensions/test_extension_prefs.h"
18 #include "chrome/browser/net/test_url_fetcher_factory.h" 18 #include "chrome/browser/net/test_url_fetcher_factory.h"
19 #include "chrome/browser/pref_service.h" 19 #include "chrome/browser/pref_service.h"
20 #include "chrome/common/extensions/extension.h" 20 #include "chrome/common/extensions/extension.h"
21 #include "chrome/common/extensions/extension_constants.h" 21 #include "chrome/common/extensions/extension_constants.h"
22 #include "chrome/common/extensions/extension_error_reporter.h" 22 #include "chrome/common/extensions/extension_error_reporter.h"
23 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
24 #include "net/base/escape.h" 24 #include "net/base/escape.h"
25 #include "net/base/load_flags.h" 25 #include "net/base/load_flags.h"
26 #include "net/url_request/url_request_status.h" 26 #include "net/url_request/url_request_status.h"
27 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "libxml/globals.h" 28 #include "libxml/globals.h"
29 29
30 #if defined(OS_MACOSX) 30 #if defined(OS_MACOSX)
31 // These tests crash frequently on Mac 10.5 Tests debug. http://crbug.com/26035. 31 // These tests crash frequently on Mac 10.5 Tests debug. http://crbug.com/26035.
32 #define MAYBE_TestBlacklistDownloading DISABLED_TestBlacklistDownloading 32 #define MAYBE_TestBlacklistDownloading DISABLED_TestBlacklistDownloading
33 #define MAYBE_TestBlacklistUpdateCheckRequests DISABLED_TestBlacklistUpdateCheck Requests 33 #define MAYBE_TestBlacklistUpdateCheckRequests DISABLED_TestBlacklistUpdateCheck Requests
34 #else 34 #else
35 #define MAYBE_TestBlacklistDownloading TestBlacklistDownloading 35 #define MAYBE_TestBlacklistDownloading TestBlacklistDownloading
36 #define MAYBE_TestBlacklistUpdateCheckRequests TestBlacklistUpdateCheckRequests 36 #define MAYBE_TestBlacklistUpdateCheckRequests TestBlacklistUpdateCheckRequests
37 #endif 37 #endif
38 38
39 using base::Time; 39 using base::Time;
40 using base::TimeDelta; 40 using base::TimeDelta;
41 41
42 static int expected_load_flags = 42 static int expected_load_flags =
43 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES; 43 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES;
44 44
45 // Do-nothing base class for further specialized test classes. 45 // Base class for further specialized test classes.
46 class MockService : public ExtensionUpdateService { 46 class MockService : public ExtensionUpdateService {
47 public: 47 public:
48 MockService() {} 48 MockService() {}
49 virtual ~MockService() {} 49 virtual ~MockService() {}
50 50
51 virtual const ExtensionList* extensions() const { 51 virtual const ExtensionList* extensions() const {
52 EXPECT_TRUE(false); 52 EXPECT_TRUE(false);
53 return NULL; 53 return NULL;
54 } 54 }
55 55
(...skipping 16 matching lines...) Expand all
72 virtual void UpdateExtensionBlacklist( 72 virtual void UpdateExtensionBlacklist(
73 const std::vector<std::string>& blacklist) { 73 const std::vector<std::string>& blacklist) {
74 EXPECT_TRUE(false); 74 EXPECT_TRUE(false);
75 } 75 }
76 76
77 virtual bool HasInstalledExtensions() { 77 virtual bool HasInstalledExtensions() {
78 EXPECT_TRUE(false); 78 EXPECT_TRUE(false);
79 return false; 79 return false;
80 } 80 }
81 81
82 virtual void SetLastPingDay(const std::string& extension_id, 82 virtual ExtensionPrefs* extension_prefs() { return prefs_.prefs(); }
83 const Time& time) {
84 last_ping_days_[extension_id] = time;
85 }
86 83
87 virtual Time LastPingDay(const std::string& extension_id) const { 84 PrefService* pref_service() { return prefs_.pref_service(); }
88 std::map<std::string, Time>::const_iterator i =
89 last_ping_days_.find(extension_id);
90 if (i != last_ping_days_.end())
91 return i->second;
92 85
93 return Time(); 86 // Creates test extensions and inserts them into list. The name and
94 } 87 // version are all based on their index. If |update_url| is non-null, it
95 88 // will be used as the update_url for each extension.
96 virtual void SetBlacklistLastPingDay(const Time& time) { 89 void CreateTestExtensions(int count, ExtensionList *list,
97 blacklist_last_ping_day_ = time; 90 const std::string* update_url) {
98 } 91 for (int i = 1; i <= count; i++) {
99 92 DictionaryValue manifest;
100 virtual Time BlacklistLastPingDay() const { 93 manifest.SetString(extension_manifest_keys::kVersion,
101 return blacklist_last_ping_day_; 94 StringPrintf("%d.0.0.0", i));
95 manifest.SetString(extension_manifest_keys::kName,
96 StringPrintf("Extension %d", i));
97 if (update_url)
98 manifest.SetString(extension_manifest_keys::kUpdateURL, *update_url);
99 Extension* e = prefs_.AddExtensionWithManifest(manifest);
100 ASSERT_TRUE(e != NULL);
101 list->push_back(e);
102 }
102 } 103 }
103 104
104 protected: 105 protected:
105 PendingExtensionMap pending_extensions_; 106 PendingExtensionMap pending_extensions_;
107 TestExtensionPrefs prefs_;
106 108
107 private: 109 private:
108 std::map<std::string, Time> last_ping_days_;
109 Time blacklist_last_ping_day_;
110 DISALLOW_COPY_AND_ASSIGN(MockService); 110 DISALLOW_COPY_AND_ASSIGN(MockService);
111 }; 111 };
112 112
113 // Class that contains a PrefService and handles cleanup of a temporary file
114 // backing it.
115 class ScopedTempPrefService {
116 public:
117 ScopedTempPrefService() {
118 // Make sure different tests won't use the same prefs file. It will cause
119 // problem when different tests are running in parallel.
120 temp_dir_.CreateUniqueTempDir();
121 FilePath pref_file = temp_dir_.path().AppendASCII("prefs");
122 prefs_.reset(new PrefService(new JsonPrefStore(pref_file)));
123 }
124 113
125 ~ScopedTempPrefService() {} 114 std::string GenerateId(std::string input) {
126 115 std::string result;
127 PrefService* get() { 116 EXPECT_TRUE(Extension::GenerateId(input, &result));
128 return prefs_.get(); 117 return result;
129 }
130
131 private:
132 // Ordering matters, we want |prefs_| to be destroyed before |temp_dir_|.
133 ScopedTempDir temp_dir_;
134 scoped_ptr<PrefService> prefs_;
135 };
136
137 // Creates test extensions and inserts them into list. The name and
138 // version are all based on their index. If |update_url| is non-null, it
139 // will be used as the update_url for each extension.
140 void CreateTestExtensions(int count, ExtensionList *list,
141 const std::string* update_url) {
142 for (int i = 1; i <= count; i++) {
143 DictionaryValue input;
144 #if defined(OS_WIN)
145 FilePath path(StringPrintf(L"c:\\extension%i", i));
146 #else
147 FilePath path(StringPrintf("/extension%i", i));
148 #endif
149 Extension* e = new Extension(path);
150 e->set_location(Extension::INTERNAL);
151 input.SetString(extension_manifest_keys::kVersion,
152 StringPrintf("%d.0.0.0", i));
153 input.SetString(extension_manifest_keys::kName,
154 StringPrintf("Extension %d", i));
155 if (update_url)
156 input.SetString(extension_manifest_keys::kUpdateURL, *update_url);
157 std::string error;
158 EXPECT_TRUE(e->InitFromValue(input, false, &error));
159 list->push_back(e);
160 }
161 } 118 }
162 119
163 // Creates test pending extensions and inserts them into list. The 120 // Creates test pending extensions and inserts them into list. The
164 // name and version are all based on their index. 121 // name and version are all based on their index.
165 void CreateTestPendingExtensions(int count, const GURL& update_url, 122 void CreateTestPendingExtensions(int count, const GURL& update_url,
166 PendingExtensionMap* pending_extensions) { 123 PendingExtensionMap* pending_extensions) {
167 for (int i = 1; i <= count; i++) { 124 for (int i = 1; i <= count; i++) {
168 bool is_theme = (i % 2) == 0; 125 bool is_theme = (i % 2) == 0;
169 const bool kInstallSilently = true; 126 const bool kInstallSilently = true;
170 scoped_ptr<Version> version( 127 scoped_ptr<Version> version(
171 Version::GetVersionFromString(StringPrintf("%d.0.0.0", i))); 128 Version::GetVersionFromString(StringPrintf("%d.0.0.0", i)));
172 ASSERT_TRUE(version.get()); 129 ASSERT_TRUE(version.get());
173 (*pending_extensions)[StringPrintf("extension%i", i)] = 130 std::string id = GenerateId(StringPrintf("extension%i", i));
131 (*pending_extensions)[id] =
174 PendingExtensionInfo(update_url, *version, 132 PendingExtensionInfo(update_url, *version,
175 is_theme, kInstallSilently); 133 is_theme, kInstallSilently);
176 } 134 }
177 } 135 }
178 136
179 class ServiceForManifestTests : public MockService { 137 class ServiceForManifestTests : public MockService {
180 public: 138 public:
181 ServiceForManifestTests() : has_installed_extensions_(false) {} 139 ServiceForManifestTests() : has_installed_extensions_(false) {}
182 140
183 virtual ~ServiceForManifestTests() {} 141 virtual ~ServiceForManifestTests() {}
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 static void TestExtensionUpdateCheckRequests(bool pending) { 287 static void TestExtensionUpdateCheckRequests(bool pending) {
330 // Create an extension with an update_url. 288 // Create an extension with an update_url.
331 ServiceForManifestTests service; 289 ServiceForManifestTests service;
332 std::string update_url("http://foo.com/bar"); 290 std::string update_url("http://foo.com/bar");
333 ExtensionList extensions; 291 ExtensionList extensions;
334 PendingExtensionMap pending_extensions; 292 PendingExtensionMap pending_extensions;
335 if (pending) { 293 if (pending) {
336 CreateTestPendingExtensions(1, GURL(update_url), &pending_extensions); 294 CreateTestPendingExtensions(1, GURL(update_url), &pending_extensions);
337 service.set_pending_extensions(pending_extensions); 295 service.set_pending_extensions(pending_extensions);
338 } else { 296 } else {
339 CreateTestExtensions(1, &extensions, &update_url); 297 service.CreateTestExtensions(1, &extensions, &update_url);
340 service.set_extensions(extensions); 298 service.set_extensions(extensions);
341 } 299 }
342 300
343 // Setup and start the updater. 301 // Setup and start the updater.
344 MessageLoop message_loop; 302 MessageLoop message_loop;
345 ChromeThread io_thread(ChromeThread::IO); 303 ChromeThread io_thread(ChromeThread::IO);
346 io_thread.Start(); 304 io_thread.Start();
347 305
348 TestURLFetcherFactory factory; 306 TestURLFetcherFactory factory;
349 URLFetcher::set_factory(&factory); 307 URLFetcher::set_factory(&factory);
350 ScopedTempPrefService prefs;
351 scoped_refptr<ExtensionUpdater> updater = 308 scoped_refptr<ExtensionUpdater> updater =
352 new ExtensionUpdater(&service, prefs.get(), 60*60*24); 309 new ExtensionUpdater(&service, service.pref_service(), 60*60*24);
353 updater->Start(); 310 updater->Start();
354 311
355 // Tell the update that it's time to do update checks. 312 // Tell the update that it's time to do update checks.
356 SimulateTimerFired(updater.get()); 313 SimulateTimerFired(updater.get());
357 314
358 // Get the url our mock fetcher was asked to fetch. 315 // Get the url our mock fetcher was asked to fetch.
359 TestURLFetcher* fetcher = 316 TestURLFetcher* fetcher =
360 factory.GetFetcherByID(ExtensionUpdater::kManifestFetcherId); 317 factory.GetFetcherByID(ExtensionUpdater::kManifestFetcherId);
361 const GURL& url = fetcher->original_url(); 318 const GURL& url = fetcher->original_url();
362 EXPECT_FALSE(url.is_empty()); 319 EXPECT_FALSE(url.is_empty());
(...skipping 30 matching lines...) Expand all
393 static void TestBlacklistUpdateCheckRequests() { 350 static void TestBlacklistUpdateCheckRequests() {
394 ServiceForManifestTests service; 351 ServiceForManifestTests service;
395 352
396 // Setup and start the updater. 353 // Setup and start the updater.
397 MessageLoop message_loop; 354 MessageLoop message_loop;
398 ChromeThread io_thread(ChromeThread::IO); 355 ChromeThread io_thread(ChromeThread::IO);
399 io_thread.Start(); 356 io_thread.Start();
400 357
401 TestURLFetcherFactory factory; 358 TestURLFetcherFactory factory;
402 URLFetcher::set_factory(&factory); 359 URLFetcher::set_factory(&factory);
403 ScopedTempPrefService prefs;
404 scoped_refptr<ExtensionUpdater> updater = 360 scoped_refptr<ExtensionUpdater> updater =
405 new ExtensionUpdater(&service, prefs.get(), 60*60*24); 361 new ExtensionUpdater(&service, service.pref_service(), 60*60*24);
406 updater->Start(); 362 updater->Start();
407 363
408 // Tell the updater that it's time to do update checks. 364 // Tell the updater that it's time to do update checks.
409 SimulateTimerFired(updater.get()); 365 SimulateTimerFired(updater.get());
410 366
411 // No extensions installed, so nothing should have been fetched. 367 // No extensions installed, so nothing should have been fetched.
412 TestURLFetcher* fetcher = 368 TestURLFetcher* fetcher =
413 factory.GetFetcherByID(ExtensionUpdater::kManifestFetcherId); 369 factory.GetFetcherByID(ExtensionUpdater::kManifestFetcherId);
414 EXPECT_TRUE(fetcher == NULL); 370 EXPECT_TRUE(fetcher == NULL);
415 371
(...skipping 26 matching lines...) Expand all
442 EXPECT_EQ("com.google.crx.blacklist", params["id"]); 398 EXPECT_EQ("com.google.crx.blacklist", params["id"]);
443 EXPECT_EQ("0", params["v"]); 399 EXPECT_EQ("0", params["v"]);
444 EXPECT_EQ("", params["uc"]); 400 EXPECT_EQ("", params["uc"]);
445 EXPECT_TRUE(ContainsKey(params, "ping")); 401 EXPECT_TRUE(ContainsKey(params, "ping"));
446 } 402 }
447 403
448 static void TestDetermineUpdates() { 404 static void TestDetermineUpdates() {
449 // Create a set of test extensions 405 // Create a set of test extensions
450 ServiceForManifestTests service; 406 ServiceForManifestTests service;
451 ExtensionList tmp; 407 ExtensionList tmp;
452 CreateTestExtensions(3, &tmp, NULL); 408 service.CreateTestExtensions(3, &tmp, NULL);
453 service.set_extensions(tmp); 409 service.set_extensions(tmp);
454 410
455 MessageLoop message_loop; 411 MessageLoop message_loop;
456 ScopedTempPrefService prefs;
457 scoped_refptr<ExtensionUpdater> updater = 412 scoped_refptr<ExtensionUpdater> updater =
458 new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); 413 new ExtensionUpdater(&service, service.pref_service(),
414 kUpdateFrequencySecs);
459 415
460 // Check passing an empty list of parse results to DetermineUpdates 416 // Check passing an empty list of parse results to DetermineUpdates
461 ManifestFetchData fetch_data(GURL("http://localhost/foo")); 417 ManifestFetchData fetch_data(GURL("http://localhost/foo"));
462 UpdateManifest::Results updates; 418 UpdateManifest::Results updates;
463 std::vector<int> updateable = updater->DetermineUpdates(fetch_data, 419 std::vector<int> updateable = updater->DetermineUpdates(fetch_data,
464 updates); 420 updates);
465 EXPECT_TRUE(updateable.empty()); 421 EXPECT_TRUE(updateable.empty());
466 422
467 // Create two updates - expect that DetermineUpdates will return the first 423 // Create two updates - expect that DetermineUpdates will return the first
468 // one (v1.0 installed, v1.1 available) but not the second one (both 424 // one (v1.0 installed, v1.1 available) but not the second one (both
(...skipping 15 matching lines...) Expand all
484 } 440 }
485 441
486 static void TestDetermineUpdatesPending() { 442 static void TestDetermineUpdatesPending() {
487 // Create a set of test extensions 443 // Create a set of test extensions
488 ServiceForManifestTests service; 444 ServiceForManifestTests service;
489 PendingExtensionMap pending_extensions; 445 PendingExtensionMap pending_extensions;
490 CreateTestPendingExtensions(3, GURL(), &pending_extensions); 446 CreateTestPendingExtensions(3, GURL(), &pending_extensions);
491 service.set_pending_extensions(pending_extensions); 447 service.set_pending_extensions(pending_extensions);
492 448
493 MessageLoop message_loop; 449 MessageLoop message_loop;
494 ScopedTempPrefService prefs;
495 scoped_refptr<ExtensionUpdater> updater = 450 scoped_refptr<ExtensionUpdater> updater =
496 new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); 451 new ExtensionUpdater(&service, service.pref_service(),
452 kUpdateFrequencySecs);
497 453
498 ManifestFetchData fetch_data(GURL("http://localhost/foo")); 454 ManifestFetchData fetch_data(GURL("http://localhost/foo"));
499 UpdateManifest::Results updates; 455 UpdateManifest::Results updates;
500 for (PendingExtensionMap::const_iterator it = pending_extensions.begin(); 456 for (PendingExtensionMap::const_iterator it = pending_extensions.begin();
501 it != pending_extensions.end(); ++it) { 457 it != pending_extensions.end(); ++it) {
502 fetch_data.AddExtension(it->first, 458 fetch_data.AddExtension(it->first,
503 it->second.version.GetString(), 459 it->second.version.GetString(),
504 ManifestFetchData::kNeverPinged); 460 ManifestFetchData::kNeverPinged);
505 AddParseResult(it->first, 461 AddParseResult(it->first,
506 "1.1", "http://localhost/e1_1.1.crx", &updates); 462 "1.1", "http://localhost/e1_1.1.crx", &updates);
(...skipping 12 matching lines...) Expand all
519 ChromeThread ui_thread(ChromeThread::UI, &ui_loop); 475 ChromeThread ui_thread(ChromeThread::UI, &ui_loop);
520 ChromeThread file_thread(ChromeThread::FILE); 476 ChromeThread file_thread(ChromeThread::FILE);
521 file_thread.Start(); 477 file_thread.Start();
522 ChromeThread io_thread(ChromeThread::IO); 478 ChromeThread io_thread(ChromeThread::IO);
523 io_thread.Start(); 479 io_thread.Start();
524 480
525 TestURLFetcherFactory factory; 481 TestURLFetcherFactory factory;
526 TestURLFetcher* fetcher = NULL; 482 TestURLFetcher* fetcher = NULL;
527 URLFetcher::set_factory(&factory); 483 URLFetcher::set_factory(&factory);
528 ServiceForDownloadTests service; 484 ServiceForDownloadTests service;
529 ScopedTempPrefService prefs;
530 scoped_refptr<ExtensionUpdater> updater = 485 scoped_refptr<ExtensionUpdater> updater =
531 new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); 486 new ExtensionUpdater(&service, service.pref_service(),
487 kUpdateFrequencySecs);
532 488
533 GURL url1("http://localhost/manifest1"); 489 GURL url1("http://localhost/manifest1");
534 GURL url2("http://localhost/manifest2"); 490 GURL url2("http://localhost/manifest2");
535 491
536 // Request 2 update checks - the first should begin immediately and the 492 // Request 2 update checks - the first should begin immediately and the
537 // second one should be queued up. 493 // second one should be queued up.
538 ManifestFetchData* fetch1 = new ManifestFetchData(url1); 494 ManifestFetchData* fetch1 = new ManifestFetchData(url1);
539 ManifestFetchData* fetch2 = new ManifestFetchData(url2); 495 ManifestFetchData* fetch2 = new ManifestFetchData(url2);
540 fetch1->AddExtension("1111", "1.0", 0); 496 fetch1->AddExtension("1111", "1.0", 0);
541 fetch2->AddExtension("12345", "2.0", ManifestFetchData::kNeverPinged); 497 fetch2->AddExtension("12345", "2.0", ManifestFetchData::kNeverPinged);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 ChromeThread ui_thread(ChromeThread::UI, &ui_loop); 539 ChromeThread ui_thread(ChromeThread::UI, &ui_loop);
584 ChromeThread file_thread(ChromeThread::FILE); 540 ChromeThread file_thread(ChromeThread::FILE);
585 file_thread.Start(); 541 file_thread.Start();
586 ChromeThread io_thread(ChromeThread::IO); 542 ChromeThread io_thread(ChromeThread::IO);
587 io_thread.Start(); 543 io_thread.Start();
588 544
589 TestURLFetcherFactory factory; 545 TestURLFetcherFactory factory;
590 TestURLFetcher* fetcher = NULL; 546 TestURLFetcher* fetcher = NULL;
591 URLFetcher::set_factory(&factory); 547 URLFetcher::set_factory(&factory);
592 ServiceForDownloadTests service; 548 ServiceForDownloadTests service;
593 ScopedTempPrefService prefs;
594 scoped_refptr<ExtensionUpdater> updater = 549 scoped_refptr<ExtensionUpdater> updater =
595 new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); 550 new ExtensionUpdater(&service, service.pref_service(),
551 kUpdateFrequencySecs);
596 552
597 GURL test_url("http://localhost/extension.crx"); 553 GURL test_url("http://localhost/extension.crx");
598 554
599 std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 555 std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
600 std::string hash = ""; 556 std::string hash = "";
601 scoped_ptr<Version> version(Version::GetVersionFromString("0.0.1")); 557 scoped_ptr<Version> version(Version::GetVersionFromString("0.0.1"));
602 ASSERT_TRUE(version.get()); 558 ASSERT_TRUE(version.get());
603 updater->FetchUpdatedExtension(id, test_url, hash, version->GetString()); 559 updater->FetchUpdatedExtension(id, test_url, hash, version->GetString());
604 560
605 if (pending) { 561 if (pending) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
641 static void TestBlacklistDownloading() { 597 static void TestBlacklistDownloading() {
642 MessageLoop message_loop; 598 MessageLoop message_loop;
643 ChromeThread ui_thread(ChromeThread::UI, &message_loop); 599 ChromeThread ui_thread(ChromeThread::UI, &message_loop);
644 ChromeThread io_thread(ChromeThread::IO); 600 ChromeThread io_thread(ChromeThread::IO);
645 io_thread.Start(); 601 io_thread.Start();
646 602
647 TestURLFetcherFactory factory; 603 TestURLFetcherFactory factory;
648 TestURLFetcher* fetcher = NULL; 604 TestURLFetcher* fetcher = NULL;
649 URLFetcher::set_factory(&factory); 605 URLFetcher::set_factory(&factory);
650 ServiceForBlacklistTests service; 606 ServiceForBlacklistTests service;
651 ScopedTempPrefService prefs;
652 scoped_refptr<ExtensionUpdater> updater = 607 scoped_refptr<ExtensionUpdater> updater =
653 new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); 608 new ExtensionUpdater(&service, service.pref_service(),
654 prefs.get()-> 609 kUpdateFrequencySecs);
610 service.pref_service()->
655 RegisterStringPref(prefs::kExtensionBlacklistUpdateVersion, L"0"); 611 RegisterStringPref(prefs::kExtensionBlacklistUpdateVersion, L"0");
656 GURL test_url("http://localhost/extension.crx"); 612 GURL test_url("http://localhost/extension.crx");
657 613
658 std::string id = "com.google.crx.blacklist"; 614 std::string id = "com.google.crx.blacklist";
659 615
660 std::string hash = 616 std::string hash =
661 "2CE109E9D0FAF820B2434E166297934E6177B65AB9951DBC3E204CAD4689B39C"; 617 "2CE109E9D0FAF820B2434E166297934E6177B65AB9951DBC3E204CAD4689B39C";
662 618
663 std::string version = "0.0.1"; 619 std::string version = "0.0.1";
664
665 updater->FetchUpdatedExtension(id, test_url, hash, version); 620 updater->FetchUpdatedExtension(id, test_url, hash, version);
666 621
667 // Call back the ExtensionUpdater with a 200 response and some test data 622 // Call back the ExtensionUpdater with a 200 response and some test data
668 std::string extension_data("aaabbb"); 623 std::string extension_data("aaabbb");
669 fetcher = factory.GetFetcherByID(ExtensionUpdater::kExtensionFetcherId); 624 fetcher = factory.GetFetcherByID(ExtensionUpdater::kExtensionFetcherId);
670 EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL); 625 EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
671 EXPECT_TRUE(fetcher->load_flags() == expected_load_flags); 626 EXPECT_TRUE(fetcher->load_flags() == expected_load_flags);
672 fetcher->delegate()->OnURLFetchComplete( 627 fetcher->delegate()->OnURLFetchComplete(
673 fetcher, test_url, URLRequestStatus(), 200, ResponseCookies(), 628 fetcher, test_url, URLRequestStatus(), 200, ResponseCookies(),
674 extension_data); 629 extension_data);
675 630
676 message_loop.RunAllPending(); 631 message_loop.RunAllPending();
677 632
678 // The updater should have called extension service to process the 633 // The updater should have called extension service to process the
679 // blacklist. 634 // blacklist.
680 EXPECT_TRUE(service.processed_blacklist()); 635 EXPECT_TRUE(service.processed_blacklist());
681 636
682 EXPECT_EQ(version, WideToASCII(prefs.get()-> 637 EXPECT_EQ(version, WideToASCII(service.pref_service()->
683 GetString(prefs::kExtensionBlacklistUpdateVersion))); 638 GetString(prefs::kExtensionBlacklistUpdateVersion)));
684 639
685 URLFetcher::set_factory(NULL); 640 URLFetcher::set_factory(NULL);
686 } 641 }
687 642
688 static void TestMultipleExtensionDownloading() { 643 static void TestMultipleExtensionDownloading() {
689 MessageLoopForUI message_loop; 644 MessageLoopForUI message_loop;
690 ChromeThread ui_thread(ChromeThread::UI, &message_loop); 645 ChromeThread ui_thread(ChromeThread::UI, &message_loop);
691 ChromeThread file_thread(ChromeThread::FILE, &message_loop); 646 ChromeThread file_thread(ChromeThread::FILE, &message_loop);
692 ChromeThread io_thread(ChromeThread::IO); 647 ChromeThread io_thread(ChromeThread::IO);
693 io_thread.Start(); 648 io_thread.Start();
694 649
695 TestURLFetcherFactory factory; 650 TestURLFetcherFactory factory;
696 TestURLFetcher* fetcher = NULL; 651 TestURLFetcher* fetcher = NULL;
697 URLFetcher::set_factory(&factory); 652 URLFetcher::set_factory(&factory);
698 ServiceForDownloadTests service; 653 ServiceForDownloadTests service;
699 ScopedTempPrefService prefs;
700 scoped_refptr<ExtensionUpdater> updater = 654 scoped_refptr<ExtensionUpdater> updater =
701 new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); 655 new ExtensionUpdater(&service, service.pref_service(),
656 kUpdateFrequencySecs);
702 657
703 GURL url1("http://localhost/extension1.crx"); 658 GURL url1("http://localhost/extension1.crx");
704 GURL url2("http://localhost/extension2.crx"); 659 GURL url2("http://localhost/extension2.crx");
705 660
706 std::string id1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 661 std::string id1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
707 std::string id2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; 662 std::string id2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
708 663
709 std::string hash1 = ""; 664 std::string hash1 = "";
710 std::string hash2 = ""; 665 std::string hash2 = "";
711 666
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
758 static void TestGalleryRequests(int ping_days) { 713 static void TestGalleryRequests(int ping_days) {
759 TestURLFetcherFactory factory; 714 TestURLFetcherFactory factory;
760 URLFetcher::set_factory(&factory); 715 URLFetcher::set_factory(&factory);
761 716
762 // Set up 2 mock extensions, one with a google.com update url and one 717 // Set up 2 mock extensions, one with a google.com update url and one
763 // without. 718 // without.
764 ServiceForManifestTests service; 719 ServiceForManifestTests service;
765 ExtensionList tmp; 720 ExtensionList tmp;
766 GURL url1("http://clients2.google.com/service/update2/crx"); 721 GURL url1("http://clients2.google.com/service/update2/crx");
767 GURL url2("http://www.somewebsite.com"); 722 GURL url2("http://www.somewebsite.com");
768 CreateTestExtensions(1, &tmp, &url1.possibly_invalid_spec()); 723 service.CreateTestExtensions(1, &tmp, &url1.possibly_invalid_spec());
769 CreateTestExtensions(1, &tmp, &url2.possibly_invalid_spec()); 724 service.CreateTestExtensions(1, &tmp, &url2.possibly_invalid_spec());
770 EXPECT_EQ(2u, tmp.size()); 725 EXPECT_EQ(2u, tmp.size());
771 service.set_extensions(tmp); 726 service.set_extensions(tmp);
772 727
773 Time now = Time::Now(); 728 Time now = Time::Now();
774 if (ping_days == 0) { 729 if (ping_days == 0) {
775 service.SetLastPingDay(tmp[0]->id(), now - TimeDelta::FromSeconds(15)); 730 service.extension_prefs()->SetLastPingDay(
731 tmp[0]->id(), now - TimeDelta::FromSeconds(15));
776 } else if (ping_days > 0) { 732 } else if (ping_days > 0) {
777 Time last_ping_day = 733 Time last_ping_day =
778 now - TimeDelta::FromDays(ping_days) - TimeDelta::FromSeconds(15); 734 now - TimeDelta::FromDays(ping_days) - TimeDelta::FromSeconds(15);
779 service.SetLastPingDay(tmp[0]->id(), last_ping_day); 735 service.extension_prefs()->SetLastPingDay(tmp[0]->id(), last_ping_day);
780 } 736 }
781 737
782 MessageLoop message_loop; 738 MessageLoop message_loop;
783 ScopedTempPrefService prefs;
784 scoped_refptr<ExtensionUpdater> updater = 739 scoped_refptr<ExtensionUpdater> updater =
785 new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); 740 new ExtensionUpdater(&service, service.pref_service(),
741 kUpdateFrequencySecs);
786 updater->set_blacklist_checks_enabled(false); 742 updater->set_blacklist_checks_enabled(false);
787 743
788 // Make the updater do manifest fetching, and note the urls it tries to 744 // Make the updater do manifest fetching, and note the urls it tries to
789 // fetch. 745 // fetch.
790 std::vector<GURL> fetched_urls; 746 std::vector<GURL> fetched_urls;
791 updater->CheckNow(); 747 updater->CheckNow();
792 TestURLFetcher* fetcher = 748 TestURLFetcher* fetcher =
793 factory.GetFetcherByID(ExtensionUpdater::kManifestFetcherId); 749 factory.GetFetcherByID(ExtensionUpdater::kManifestFetcherId);
794 EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL); 750 EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
795 fetched_urls.push_back(fetcher->original_url()); 751 fetched_urls.push_back(fetcher->original_url());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
827 783
828 STLDeleteElements(&tmp); 784 STLDeleteElements(&tmp);
829 } 785 }
830 786
831 // This makes sure that the extension updater properly stores the results 787 // This makes sure that the extension updater properly stores the results
832 // of a <daystart> tag from a manifest fetch in one of two cases: 1) This is 788 // of a <daystart> tag from a manifest fetch in one of two cases: 1) This is
833 // the first time we fetched the extension, or 2) We sent a ping value of 789 // the first time we fetched the extension, or 2) We sent a ping value of
834 // >= 1 day for the extension. 790 // >= 1 day for the extension.
835 static void TestHandleManifestResults() { 791 static void TestHandleManifestResults() {
836 ServiceForManifestTests service; 792 ServiceForManifestTests service;
837 ScopedTempPrefService prefs;
838 scoped_refptr<ExtensionUpdater> updater = 793 scoped_refptr<ExtensionUpdater> updater =
839 new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); 794 new ExtensionUpdater(&service, service.pref_service(),
795 kUpdateFrequencySecs);
840 796
841 GURL update_url("http://www.google.com/manifest"); 797 GURL update_url("http://www.google.com/manifest");
842 ExtensionList tmp; 798 ExtensionList tmp;
843 CreateTestExtensions(1, &tmp, &update_url.spec()); 799 service.CreateTestExtensions(1, &tmp, &update_url.spec());
844 service.set_extensions(tmp); 800 service.set_extensions(tmp);
845 801
846 ManifestFetchData fetch_data(update_url); 802 ManifestFetchData fetch_data(update_url);
847 Extension* extension = tmp[0]; 803 Extension* extension = tmp[0];
848 fetch_data.AddExtension(extension->id(), extension->VersionString(), 804 fetch_data.AddExtension(extension->id(), extension->VersionString(),
849 ManifestFetchData::kNeverPinged); 805 ManifestFetchData::kNeverPinged);
850 UpdateManifest::Results results; 806 UpdateManifest::Results results;
851 results.daystart_elapsed_seconds = 750; 807 results.daystart_elapsed_seconds = 750;
852 808
853 updater->HandleManifestResults(fetch_data, results); 809 updater->HandleManifestResults(fetch_data, results);
854 Time last_ping_day = service.LastPingDay(extension->id()); 810 Time last_ping_day =
811 service.extension_prefs()->LastPingDay(extension->id());
855 EXPECT_FALSE(last_ping_day.is_null()); 812 EXPECT_FALSE(last_ping_day.is_null());
856 int64 seconds_diff = (Time::Now() - last_ping_day).InSeconds(); 813 int64 seconds_diff = (Time::Now() - last_ping_day).InSeconds();
857 EXPECT_LT(seconds_diff - results.daystart_elapsed_seconds, 5); 814 EXPECT_LT(seconds_diff - results.daystart_elapsed_seconds, 5);
858 815
859 STLDeleteElements(&tmp); 816 STLDeleteElements(&tmp);
860 } 817 }
861 }; 818 };
862 819
863 // Because we test some private methods of ExtensionUpdater, it's easer for the 820 // Because we test some private methods of ExtensionUpdater, it's easer for the
864 // actual test code to live in ExtenionUpdaterTest methods instead of TEST_F 821 // actual test code to live in ExtenionUpdaterTest methods instead of TEST_F
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
917 ExtensionUpdaterTest::TestHandleManifestResults(); 874 ExtensionUpdaterTest::TestHandleManifestResults();
918 } 875 }
919 876
920 TEST(ExtensionUpdaterTest, TestManifestFetchesBuilderAddExtension) { 877 TEST(ExtensionUpdaterTest, TestManifestFetchesBuilderAddExtension) {
921 MockService service; 878 MockService service;
922 ManifestFetchesBuilder builder(&service); 879 ManifestFetchesBuilder builder(&service);
923 880
924 // Non-internal non-external extensions should be rejected. 881 // Non-internal non-external extensions should be rejected.
925 { 882 {
926 ExtensionList extensions; 883 ExtensionList extensions;
927 CreateTestExtensions(1, &extensions, NULL); 884 service.CreateTestExtensions(1, &extensions, NULL);
928 ASSERT_FALSE(extensions.empty()); 885 ASSERT_FALSE(extensions.empty());
929 extensions[0]->set_location(Extension::INVALID); 886 extensions[0]->set_location(Extension::INVALID);
930 builder.AddExtension(*extensions[0]); 887 builder.AddExtension(*extensions[0]);
931 EXPECT_TRUE(builder.GetFetches().empty()); 888 EXPECT_TRUE(builder.GetFetches().empty());
932 STLDeleteElements(&extensions); 889 STLDeleteElements(&extensions);
933 } 890 }
934 891
935 scoped_ptr<Version> version(Version::GetVersionFromString("0")); 892 scoped_ptr<Version> version(Version::GetVersionFromString("0"));
936 ASSERT_TRUE(version.get()); 893 ASSERT_TRUE(version.get());
937 894
938 // Extensions with invalid update URLs should be rejected. 895 // Extensions with invalid update URLs should be rejected.
939 builder.AddPendingExtension( 896 builder.AddPendingExtension(
940 "id", PendingExtensionInfo(GURL("http:google.com:foo"), 897 GenerateId("foo"), PendingExtensionInfo(GURL("http:google.com:foo"),
941 *version, false, false)); 898 *version, false, false));
942 EXPECT_TRUE(builder.GetFetches().empty()); 899 EXPECT_TRUE(builder.GetFetches().empty());
943 900
944 // Extensions with empty IDs should be rejected. 901 // Extensions with empty IDs should be rejected.
945 builder.AddPendingExtension( 902 builder.AddPendingExtension(
946 "", PendingExtensionInfo(GURL(), *version, false, false)); 903 "", PendingExtensionInfo(GURL(), *version, false, false));
947 EXPECT_TRUE(builder.GetFetches().empty()); 904 EXPECT_TRUE(builder.GetFetches().empty());
948 905
949 // TODO(akalin): Test that extensions with empty update URLs 906 // TODO(akalin): Test that extensions with empty update URLs
950 // converted from user scripts are rejected. 907 // converted from user scripts are rejected.
951 908
952 // Extensions with empty update URLs should have a default one 909 // Extensions with empty update URLs should have a default one
953 // filled in. 910 // filled in.
954 builder.AddPendingExtension( 911 builder.AddPendingExtension(
955 "id", PendingExtensionInfo(GURL(), *version, false, false)); 912 GenerateId("foo"), PendingExtensionInfo(GURL(), *version, false, false));
956 std::vector<ManifestFetchData*> fetches = builder.GetFetches(); 913 std::vector<ManifestFetchData*> fetches = builder.GetFetches();
957 ASSERT_EQ(1u, fetches.size()); 914 ASSERT_EQ(1u, fetches.size());
958 scoped_ptr<ManifestFetchData> fetch(fetches[0]); 915 scoped_ptr<ManifestFetchData> fetch(fetches[0]);
959 fetches.clear(); 916 fetches.clear();
960 EXPECT_FALSE(fetch->base_url().is_empty()); 917 EXPECT_FALSE(fetch->base_url().is_empty());
961 EXPECT_FALSE(fetch->full_url().is_empty()); 918 EXPECT_FALSE(fetch->full_url().is_empty());
962 } 919 }
963 920
964 // TODO(asargent) - (http://crbug.com/12780) add tests for: 921 // TODO(asargent) - (http://crbug.com/12780) add tests for:
965 // -prodversionmin (shouldn't update if browser version too old) 922 // -prodversionmin (shouldn't update if browser version too old)
966 // -manifests & updates arriving out of order / interleaved 923 // -manifests & updates arriving out of order / interleaved
967 // -Profile::GetDefaultRequestContext() returning null 924 // -Profile::GetDefaultRequestContext() returning null
968 // (should not crash, but just do check later) 925 // (should not crash, but just do check later)
969 // -malformed update url (empty, file://, has query, has a # fragment, etc.) 926 // -malformed update url (empty, file://, has query, has a # fragment, etc.)
970 // -An extension gets uninstalled while updates are in progress (so it doesn't 927 // -An extension gets uninstalled while updates are in progress (so it doesn't
971 // "come back from the dead") 928 // "come back from the dead")
972 // -An extension gets manually updated to v3 while we're downloading v2 (ie 929 // -An extension gets manually updated to v3 while we're downloading v2 (ie
973 // you don't get downgraded accidentally) 930 // you don't get downgraded accidentally)
974 // -An update manifest mentions multiple updates 931 // -An update manifest mentions multiple updates
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_updater.cc ('k') | chrome/browser/extensions/extensions_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698