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

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

Issue 7524033: Add a scoper object for URLFetcher::Factory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 9 years, 4 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/string_number_conversions.h" 12 #include "base/string_number_conversions.h"
13 #include "base/string_split.h" 13 #include "base/string_split.h"
14 #include "base/string_util.h" 14 #include "base/string_util.h"
15 #include "base/stringprintf.h" 15 #include "base/stringprintf.h"
16 #include "base/threading/thread.h" 16 #include "base/threading/thread.h"
17 #include "base/version.h" 17 #include "base/version.h"
18 #include "chrome/browser/extensions/crx_installer.h" 18 #include "chrome/browser/extensions/crx_installer.h"
19 #include "chrome/browser/extensions/extension_error_reporter.h" 19 #include "chrome/browser/extensions/extension_error_reporter.h"
20 #include "chrome/browser/extensions/extension_sync_data.h" 20 #include "chrome/browser/extensions/extension_sync_data.h"
21 #include "chrome/browser/extensions/extension_updater.h" 21 #include "chrome/browser/extensions/extension_updater.h"
22 #include "chrome/browser/extensions/test_extension_prefs.h" 22 #include "chrome/browser/extensions/test_extension_prefs.h"
23 #include "chrome/browser/extensions/test_extension_service.h" 23 #include "chrome/browser/extensions/test_extension_service.h"
24 #include "chrome/browser/prefs/pref_service.h" 24 #include "chrome/browser/prefs/pref_service.h"
25 #include "chrome/common/extensions/extension.h" 25 #include "chrome/common/extensions/extension.h"
26 #include "chrome/common/extensions/extension_constants.h" 26 #include "chrome/common/extensions/extension_constants.h"
27 #include "chrome/common/pref_names.h" 27 #include "chrome/common/pref_names.h"
28 #include "chrome/test/testing_profile.h" 28 #include "chrome/test/testing_profile.h"
29 #include "content/browser/browser_thread.h" 29 #include "content/browser/browser_thread.h"
30 #include "content/common/test_url_fetcher_factory.h" 30 #include "content/test/test_url_fetcher_factory.h"
31 #include "libxml/globals.h" 31 #include "libxml/globals.h"
32 #include "net/base/escape.h" 32 #include "net/base/escape.h"
33 #include "net/base/load_flags.h" 33 #include "net/base/load_flags.h"
34 #include "net/url_request/url_request_status.h" 34 #include "net/url_request/url_request_status.h"
35 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
36 36
37 using base::Time; 37 using base::Time;
38 using base::TimeDelta; 38 using base::TimeDelta;
39 39
40 namespace { 40 namespace {
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 SetupPendingExtensionManagerForTest(1, GURL(update_url), 330 SetupPendingExtensionManagerForTest(1, GURL(update_url),
331 pending_extension_manager); 331 pending_extension_manager);
332 } else { 332 } else {
333 service.CreateTestExtensions(1, 1, &extensions, &update_url, 333 service.CreateTestExtensions(1, 1, &extensions, &update_url,
334 Extension::INTERNAL); 334 Extension::INTERNAL);
335 service.set_extensions(extensions); 335 service.set_extensions(extensions);
336 } 336 }
337 337
338 // Set up and start the updater. 338 // Set up and start the updater.
339 TestURLFetcherFactory factory; 339 TestURLFetcherFactory factory;
340 URLFetcher::set_factory(&factory);
341 ExtensionUpdater updater( 340 ExtensionUpdater updater(
342 &service, service.extension_prefs(), service.pref_service(), 341 &service, service.extension_prefs(), service.pref_service(),
343 service.profile(), 60*60*24); 342 service.profile(), 60*60*24);
344 updater.Start(); 343 updater.Start();
345 // Disable blacklist checks (tested elsewhere) so that we only see the 344 // Disable blacklist checks (tested elsewhere) so that we only see the
346 // update HTTP request. 345 // update HTTP request.
347 updater.set_blacklist_checks_enabled(false); 346 updater.set_blacklist_checks_enabled(false);
348 347
349 // Tell the update that it's time to do update checks. 348 // Tell the update that it's time to do update checks.
350 SimulateTimerFired(&updater); 349 SimulateTimerFired(&updater);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 static void TestBlacklistUpdateCheckRequests() { 382 static void TestBlacklistUpdateCheckRequests() {
384 ServiceForManifestTests service; 383 ServiceForManifestTests service;
385 384
386 // Setup and start the updater. 385 // Setup and start the updater.
387 MessageLoop message_loop; 386 MessageLoop message_loop;
388 BrowserThread ui_thread(BrowserThread::UI, &message_loop); 387 BrowserThread ui_thread(BrowserThread::UI, &message_loop);
389 BrowserThread io_thread(BrowserThread::IO); 388 BrowserThread io_thread(BrowserThread::IO);
390 io_thread.Start(); 389 io_thread.Start();
391 390
392 TestURLFetcherFactory factory; 391 TestURLFetcherFactory factory;
393 URLFetcher::set_factory(&factory);
394 ExtensionUpdater updater( 392 ExtensionUpdater updater(
395 &service, service.extension_prefs(), service.pref_service(), 393 &service, service.extension_prefs(), service.pref_service(),
396 service.profile(), 60*60*24); 394 service.profile(), 60*60*24);
397 updater.Start(); 395 updater.Start();
398 396
399 // Tell the updater that it's time to do update checks. 397 // Tell the updater that it's time to do update checks.
400 SimulateTimerFired(&updater); 398 SimulateTimerFired(&updater);
401 399
402 // Get the url our mock fetcher was asked to fetch. 400 // Get the url our mock fetcher was asked to fetch.
403 TestURLFetcher* fetcher = 401 TestURLFetcher* fetcher =
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 static void TestMultipleManifestDownloading() { 570 static void TestMultipleManifestDownloading() {
573 MessageLoop ui_loop; 571 MessageLoop ui_loop;
574 BrowserThread ui_thread(BrowserThread::UI, &ui_loop); 572 BrowserThread ui_thread(BrowserThread::UI, &ui_loop);
575 BrowserThread file_thread(BrowserThread::FILE); 573 BrowserThread file_thread(BrowserThread::FILE);
576 file_thread.Start(); 574 file_thread.Start();
577 BrowserThread io_thread(BrowserThread::IO); 575 BrowserThread io_thread(BrowserThread::IO);
578 io_thread.Start(); 576 io_thread.Start();
579 577
580 TestURLFetcherFactory factory; 578 TestURLFetcherFactory factory;
581 TestURLFetcher* fetcher = NULL; 579 TestURLFetcher* fetcher = NULL;
582 URLFetcher::set_factory(&factory);
583 scoped_ptr<ServiceForDownloadTests> service(new ServiceForDownloadTests); 580 scoped_ptr<ServiceForDownloadTests> service(new ServiceForDownloadTests);
584 ExtensionUpdater updater(service.get(), 581 ExtensionUpdater updater(service.get(),
585 service->extension_prefs(), 582 service->extension_prefs(),
586 service->pref_service(), 583 service->pref_service(),
587 service->profile(), 584 service->profile(),
588 kUpdateFrequencySecs); 585 kUpdateFrequencySecs);
589 updater.Start(); 586 updater.Start();
590 587
591 GURL url1("http://localhost/manifest1"); 588 GURL url1("http://localhost/manifest1");
592 GURL url2("http://localhost/manifest2"); 589 GURL url2("http://localhost/manifest2");
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
652 static void TestSingleExtensionDownloading(bool pending) { 649 static void TestSingleExtensionDownloading(bool pending) {
653 MessageLoop ui_loop; 650 MessageLoop ui_loop;
654 BrowserThread ui_thread(BrowserThread::UI, &ui_loop); 651 BrowserThread ui_thread(BrowserThread::UI, &ui_loop);
655 BrowserThread file_thread(BrowserThread::FILE); 652 BrowserThread file_thread(BrowserThread::FILE);
656 file_thread.Start(); 653 file_thread.Start();
657 BrowserThread io_thread(BrowserThread::IO); 654 BrowserThread io_thread(BrowserThread::IO);
658 io_thread.Start(); 655 io_thread.Start();
659 656
660 TestURLFetcherFactory factory; 657 TestURLFetcherFactory factory;
661 TestURLFetcher* fetcher = NULL; 658 TestURLFetcher* fetcher = NULL;
662 URLFetcher::set_factory(&factory);
663 scoped_ptr<ServiceForDownloadTests> service(new ServiceForDownloadTests); 659 scoped_ptr<ServiceForDownloadTests> service(new ServiceForDownloadTests);
664 ExtensionUpdater updater(service.get(), service->extension_prefs(), 660 ExtensionUpdater updater(service.get(), service->extension_prefs(),
665 service->pref_service(), 661 service->pref_service(),
666 service->profile(), 662 service->profile(),
667 kUpdateFrequencySecs); 663 kUpdateFrequencySecs);
668 updater.Start(); 664 updater.Start();
669 665
670 GURL test_url("http://localhost/extension.crx"); 666 GURL test_url("http://localhost/extension.crx");
671 667
672 std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 668 std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 EXPECT_EQ(id, service->extension_id()); 703 EXPECT_EQ(id, service->extension_id());
708 FilePath tmpfile_path = service->install_path(); 704 FilePath tmpfile_path = service->install_path();
709 EXPECT_FALSE(tmpfile_path.empty()); 705 EXPECT_FALSE(tmpfile_path.empty());
710 EXPECT_EQ(test_url, service->download_url()); 706 EXPECT_EQ(test_url, service->download_url());
711 EXPECT_EQ(extension_file_path, tmpfile_path); 707 EXPECT_EQ(extension_file_path, tmpfile_path);
712 708
713 // The FILE thread is needed for |service|'s cleanup, 709 // The FILE thread is needed for |service|'s cleanup,
714 // because of ImportantFileWriter. 710 // because of ImportantFileWriter.
715 file_thread.Start(); 711 file_thread.Start();
716 service.reset(); 712 service.reset();
717
718 URLFetcher::set_factory(NULL);
719 } 713 }
720 714
721 static void TestBlacklistDownloading() { 715 static void TestBlacklistDownloading() {
722 MessageLoop message_loop; 716 MessageLoop message_loop;
723 BrowserThread ui_thread(BrowserThread::UI, &message_loop); 717 BrowserThread ui_thread(BrowserThread::UI, &message_loop);
724 BrowserThread file_thread(BrowserThread::FILE, &message_loop); 718 BrowserThread file_thread(BrowserThread::FILE, &message_loop);
725 BrowserThread io_thread(BrowserThread::IO); 719 BrowserThread io_thread(BrowserThread::IO);
726 io_thread.Start(); 720 io_thread.Start();
727 721
728 TestURLFetcherFactory factory; 722 TestURLFetcherFactory factory;
729 TestURLFetcher* fetcher = NULL; 723 TestURLFetcher* fetcher = NULL;
730 URLFetcher::set_factory(&factory);
731 ServiceForBlacklistTests service; 724 ServiceForBlacklistTests service;
732 ExtensionUpdater updater( 725 ExtensionUpdater updater(
733 &service, service.extension_prefs(), service.pref_service(), 726 &service, service.extension_prefs(), service.pref_service(),
734 service.profile(), kUpdateFrequencySecs); 727 service.profile(), kUpdateFrequencySecs);
735 updater.Start(); 728 updater.Start();
736 GURL test_url("http://localhost/extension.crx"); 729 GURL test_url("http://localhost/extension.crx");
737 730
738 std::string id = "com.google.crx.blacklist"; 731 std::string id = "com.google.crx.blacklist";
739 732
740 std::string hash = 733 std::string hash =
(...skipping 16 matching lines...) Expand all
757 fetcher->delegate()->OnURLFetchComplete(fetcher); 750 fetcher->delegate()->OnURLFetchComplete(fetcher);
758 751
759 message_loop.RunAllPending(); 752 message_loop.RunAllPending();
760 753
761 // The updater should have called extension service to process the 754 // The updater should have called extension service to process the
762 // blacklist. 755 // blacklist.
763 EXPECT_TRUE(service.processed_blacklist()); 756 EXPECT_TRUE(service.processed_blacklist());
764 757
765 EXPECT_EQ(version, service.pref_service()-> 758 EXPECT_EQ(version, service.pref_service()->
766 GetString(prefs::kExtensionBlacklistUpdateVersion)); 759 GetString(prefs::kExtensionBlacklistUpdateVersion));
767
768 URLFetcher::set_factory(NULL);
769 } 760 }
770 761
771 // Two extensions are updated. If |updates_start_running| is true, the 762 // Two extensions are updated. If |updates_start_running| is true, the
772 // mock extensions service has UpdateExtension(...) return true, and 763 // mock extensions service has UpdateExtension(...) return true, and
773 // the test is responsible for creating fake CrxInstallers. Otherwise, 764 // the test is responsible for creating fake CrxInstallers. Otherwise,
774 // UpdateExtension() returns false, signaling install failures. 765 // UpdateExtension() returns false, signaling install failures.
775 static void TestMultipleExtensionDownloading(bool updates_start_running) { 766 static void TestMultipleExtensionDownloading(bool updates_start_running) {
776 MessageLoopForUI message_loop; 767 MessageLoopForUI message_loop;
777 BrowserThread ui_thread(BrowserThread::UI, &message_loop); 768 BrowserThread ui_thread(BrowserThread::UI, &message_loop);
778 BrowserThread file_thread(BrowserThread::FILE, &message_loop); 769 BrowserThread file_thread(BrowserThread::FILE, &message_loop);
779 BrowserThread io_thread(BrowserThread::IO); 770 BrowserThread io_thread(BrowserThread::IO);
780 io_thread.Start(); 771 io_thread.Start();
781 772
782 TestURLFetcherFactory factory; 773 TestURLFetcherFactory factory;
783 TestURLFetcher* fetcher = NULL; 774 TestURLFetcher* fetcher = NULL;
784 URLFetcher::set_factory(&factory);
785 ServiceForDownloadTests service; 775 ServiceForDownloadTests service;
786 ExtensionUpdater updater( 776 ExtensionUpdater updater(
787 &service, service.extension_prefs(), service.pref_service(), 777 &service, service.extension_prefs(), service.pref_service(),
788 service.profile(), kUpdateFrequencySecs); 778 service.profile(), kUpdateFrequencySecs);
789 updater.Start(); 779 updater.Start();
790 780
791 EXPECT_FALSE(updater.crx_install_is_running_); 781 EXPECT_FALSE(updater.crx_install_is_running_);
792 782
793 GURL url1("http://localhost/extension1.crx"); 783 GURL url1("http://localhost/extension1.crx");
794 GURL url2("http://localhost/extension2.crx"); 784 GURL url2("http://localhost/extension2.crx");
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 // on the delta between now and the last ping time (or in the case of active 894 // on the delta between now and the last ping time (or in the case of active
905 // pings, that delta plus whether the app has been active). 895 // pings, that delta plus whether the app has been active).
906 static void TestGalleryRequests(int rollcall_ping_days, 896 static void TestGalleryRequests(int rollcall_ping_days,
907 int active_ping_days, 897 int active_ping_days,
908 bool active_bit) { 898 bool active_bit) {
909 MessageLoop message_loop; 899 MessageLoop message_loop;
910 BrowserThread ui_thread(BrowserThread::UI, &message_loop); 900 BrowserThread ui_thread(BrowserThread::UI, &message_loop);
911 BrowserThread file_thread(BrowserThread::FILE, &message_loop); 901 BrowserThread file_thread(BrowserThread::FILE, &message_loop);
912 902
913 TestURLFetcherFactory factory; 903 TestURLFetcherFactory factory;
914 URLFetcher::set_factory(&factory);
915 904
916 // Set up 2 mock extensions, one with a google.com update url and one 905 // Set up 2 mock extensions, one with a google.com update url and one
917 // without. 906 // without.
918 ServiceForManifestTests service; 907 ServiceForManifestTests service;
919 ExtensionList tmp; 908 ExtensionList tmp;
920 GURL url1("http://clients2.google.com/service/update2/crx"); 909 GURL url1("http://clients2.google.com/service/update2/crx");
921 GURL url2("http://www.somewebsite.com"); 910 GURL url2("http://www.somewebsite.com");
922 service.CreateTestExtensions(1, 1, &tmp, &url1.possibly_invalid_spec(), 911 service.CreateTestExtensions(1, 1, &tmp, &url1.possibly_invalid_spec(),
923 Extension::INTERNAL); 912 Extension::INTERNAL);
924 service.CreateTestExtensions(2, 1, &tmp, &url2.possibly_invalid_spec(), 913 service.CreateTestExtensions(2, 1, &tmp, &url2.possibly_invalid_spec(),
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
1191 EXPECT_FALSE(fetch->full_url().is_empty()); 1180 EXPECT_FALSE(fetch->full_url().is_empty());
1192 } 1181 }
1193 1182
1194 TEST(ExtensionUpdaterTest, TestStartUpdateCheckMemory) { 1183 TEST(ExtensionUpdaterTest, TestStartUpdateCheckMemory) {
1195 MessageLoop message_loop; 1184 MessageLoop message_loop;
1196 BrowserThread ui_thread(BrowserThread::UI, &message_loop); 1185 BrowserThread ui_thread(BrowserThread::UI, &message_loop);
1197 BrowserThread file_thread(BrowserThread::FILE, &message_loop); 1186 BrowserThread file_thread(BrowserThread::FILE, &message_loop);
1198 1187
1199 ServiceForManifestTests service; 1188 ServiceForManifestTests service;
1200 TestURLFetcherFactory factory; 1189 TestURLFetcherFactory factory;
1201 URLFetcher::set_factory(&factory);
1202 ExtensionUpdater updater( 1190 ExtensionUpdater updater(
1203 &service, service.extension_prefs(), service.pref_service(), 1191 &service, service.extension_prefs(), service.pref_service(),
1204 service.profile(), kUpdateFrequencySecs); 1192 service.profile(), kUpdateFrequencySecs);
1205 updater.Start(); 1193 updater.Start();
1206 updater.StartUpdateCheck(new ManifestFetchData(GURL())); 1194 updater.StartUpdateCheck(new ManifestFetchData(GURL()));
1207 // This should delete the newly-created ManifestFetchData. 1195 // This should delete the newly-created ManifestFetchData.
1208 updater.StartUpdateCheck(new ManifestFetchData(GURL())); 1196 updater.StartUpdateCheck(new ManifestFetchData(GURL()));
1209 // This should add into |manifests_pending_|. 1197 // This should add into |manifests_pending_|.
1210 updater.StartUpdateCheck(new ManifestFetchData( 1198 updater.StartUpdateCheck(new ManifestFetchData(
1211 GURL("http://www.google.com"))); 1199 GURL("http://www.google.com")));
1212 // This should clear out |manifests_pending_|. 1200 // This should clear out |manifests_pending_|.
1213 updater.Stop(); 1201 updater.Stop();
1214 } 1202 }
1215 1203
1216 TEST(ExtensionUpdaterTest, TestCheckSoon) { 1204 TEST(ExtensionUpdaterTest, TestCheckSoon) {
1217 MessageLoop message_loop; 1205 MessageLoop message_loop;
1218 BrowserThread ui_thread(BrowserThread::UI, &message_loop); 1206 BrowserThread ui_thread(BrowserThread::UI, &message_loop);
1219 BrowserThread file_thread(BrowserThread::FILE, &message_loop); 1207 BrowserThread file_thread(BrowserThread::FILE, &message_loop);
1220 1208
1221 ServiceForManifestTests service; 1209 ServiceForManifestTests service;
1222 TestURLFetcherFactory factory; 1210 TestURLFetcherFactory factory;
1223 URLFetcher::set_factory(&factory);
1224 ExtensionUpdater updater( 1211 ExtensionUpdater updater(
1225 &service, service.extension_prefs(), service.pref_service(), 1212 &service, service.extension_prefs(), service.pref_service(),
1226 service.profile(), kUpdateFrequencySecs); 1213 service.profile(), kUpdateFrequencySecs);
1227 EXPECT_FALSE(updater.WillCheckSoon()); 1214 EXPECT_FALSE(updater.WillCheckSoon());
1228 updater.Start(); 1215 updater.Start();
1229 EXPECT_FALSE(updater.WillCheckSoon()); 1216 EXPECT_FALSE(updater.WillCheckSoon());
1230 updater.CheckSoon(); 1217 updater.CheckSoon();
1231 EXPECT_TRUE(updater.WillCheckSoon()); 1218 EXPECT_TRUE(updater.WillCheckSoon());
1232 updater.CheckSoon(); 1219 updater.CheckSoon();
1233 EXPECT_TRUE(updater.WillCheckSoon()); 1220 EXPECT_TRUE(updater.WillCheckSoon());
1234 ExtensionUpdaterTest::SimulateCheckSoon(updater, &message_loop); 1221 ExtensionUpdaterTest::SimulateCheckSoon(updater, &message_loop);
1235 EXPECT_FALSE(updater.WillCheckSoon()); 1222 EXPECT_FALSE(updater.WillCheckSoon());
1236 updater.CheckSoon(); 1223 updater.CheckSoon();
1237 EXPECT_TRUE(updater.WillCheckSoon()); 1224 EXPECT_TRUE(updater.WillCheckSoon());
1238 updater.Stop(); 1225 updater.Stop();
1239 EXPECT_FALSE(updater.WillCheckSoon()); 1226 EXPECT_FALSE(updater.WillCheckSoon());
1240 } 1227 }
1241 1228
1242 // TODO(asargent) - (http://crbug.com/12780) add tests for: 1229 // TODO(asargent) - (http://crbug.com/12780) add tests for:
1243 // -prodversionmin (shouldn't update if browser version too old) 1230 // -prodversionmin (shouldn't update if browser version too old)
1244 // -manifests & updates arriving out of order / interleaved 1231 // -manifests & updates arriving out of order / interleaved
1245 // -malformed update url (empty, file://, has query, has a # fragment, etc.) 1232 // -malformed update url (empty, file://, has query, has a # fragment, etc.)
1246 // -An extension gets uninstalled while updates are in progress (so it doesn't 1233 // -An extension gets uninstalled while updates are in progress (so it doesn't
1247 // "come back from the dead") 1234 // "come back from the dead")
1248 // -An extension gets manually updated to v3 while we're downloading v2 (ie 1235 // -An extension gets manually updated to v3 while we're downloading v2 (ie
1249 // you don't get downgraded accidentally) 1236 // you don't get downgraded accidentally)
1250 // -An update manifest mentions multiple updates 1237 // -An update manifest mentions multiple updates
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698