Index: chrome/browser/extensions/extension_updater_unittest.cc |
=================================================================== |
--- chrome/browser/extensions/extension_updater_unittest.cc (revision 45592) |
+++ chrome/browser/extensions/extension_updater_unittest.cc (working copy) |
@@ -14,7 +14,7 @@ |
#include "chrome/browser/chrome_thread.h" |
#include "chrome/browser/extensions/extension_updater.h" |
#include "chrome/browser/extensions/extensions_service.h" |
-#include "chrome/browser/json_pref_store.h" |
+#include "chrome/browser/extensions/test_extension_prefs.h" |
#include "chrome/browser/net/test_url_fetcher_factory.h" |
#include "chrome/browser/pref_service.h" |
#include "chrome/common/extensions/extension.h" |
@@ -42,7 +42,7 @@ |
static int expected_load_flags = |
net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES; |
-// Do-nothing base class for further specialized test classes. |
+// Base class for further specialized test classes. |
class MockService : public ExtensionUpdateService { |
public: |
MockService() {} |
@@ -79,85 +79,42 @@ |
return false; |
} |
- virtual void SetLastPingDay(const std::string& extension_id, |
- const Time& time) { |
- last_ping_days_[extension_id] = time; |
- } |
+ virtual ExtensionPrefs* extension_prefs() { return prefs_.prefs(); } |
- virtual Time LastPingDay(const std::string& extension_id) const { |
- std::map<std::string, Time>::const_iterator i = |
- last_ping_days_.find(extension_id); |
- if (i != last_ping_days_.end()) |
- return i->second; |
+ PrefService* pref_service() { return prefs_.pref_service(); } |
- return Time(); |
+ // Creates test extensions and inserts them into list. The name and |
+ // version are all based on their index. If |update_url| is non-null, it |
+ // will be used as the update_url for each extension. |
+ void CreateTestExtensions(int count, ExtensionList *list, |
+ const std::string* update_url) { |
+ for (int i = 1; i <= count; i++) { |
+ DictionaryValue manifest; |
+ manifest.SetString(extension_manifest_keys::kVersion, |
+ StringPrintf("%d.0.0.0", i)); |
+ manifest.SetString(extension_manifest_keys::kName, |
+ StringPrintf("Extension %d", i)); |
+ if (update_url) |
+ manifest.SetString(extension_manifest_keys::kUpdateURL, *update_url); |
+ Extension* e = prefs_.AddExtensionWithManifest(manifest); |
+ ASSERT_TRUE(e != NULL); |
+ list->push_back(e); |
+ } |
} |
- virtual void SetBlacklistLastPingDay(const Time& time) { |
- blacklist_last_ping_day_ = time; |
- } |
- |
- virtual Time BlacklistLastPingDay() const { |
- return blacklist_last_ping_day_; |
- } |
- |
protected: |
PendingExtensionMap pending_extensions_; |
+ TestExtensionPrefs prefs_; |
private: |
- std::map<std::string, Time> last_ping_days_; |
- Time blacklist_last_ping_day_; |
DISALLOW_COPY_AND_ASSIGN(MockService); |
}; |
-// Class that contains a PrefService and handles cleanup of a temporary file |
-// backing it. |
-class ScopedTempPrefService { |
- public: |
- ScopedTempPrefService() { |
- // Make sure different tests won't use the same prefs file. It will cause |
- // problem when different tests are running in parallel. |
- temp_dir_.CreateUniqueTempDir(); |
- FilePath pref_file = temp_dir_.path().AppendASCII("prefs"); |
- prefs_.reset(new PrefService(new JsonPrefStore(pref_file))); |
- } |
- ~ScopedTempPrefService() {} |
- |
- PrefService* get() { |
- return prefs_.get(); |
- } |
- |
- private: |
- // Ordering matters, we want |prefs_| to be destroyed before |temp_dir_|. |
- ScopedTempDir temp_dir_; |
- scoped_ptr<PrefService> prefs_; |
-}; |
- |
-// Creates test extensions and inserts them into list. The name and |
-// version are all based on their index. If |update_url| is non-null, it |
-// will be used as the update_url for each extension. |
-void CreateTestExtensions(int count, ExtensionList *list, |
- const std::string* update_url) { |
- for (int i = 1; i <= count; i++) { |
- DictionaryValue input; |
-#if defined(OS_WIN) |
- FilePath path(StringPrintf(L"c:\\extension%i", i)); |
-#else |
- FilePath path(StringPrintf("/extension%i", i)); |
-#endif |
- Extension* e = new Extension(path); |
- e->set_location(Extension::INTERNAL); |
- input.SetString(extension_manifest_keys::kVersion, |
- StringPrintf("%d.0.0.0", i)); |
- input.SetString(extension_manifest_keys::kName, |
- StringPrintf("Extension %d", i)); |
- if (update_url) |
- input.SetString(extension_manifest_keys::kUpdateURL, *update_url); |
- std::string error; |
- EXPECT_TRUE(e->InitFromValue(input, false, &error)); |
- list->push_back(e); |
- } |
+std::string GenerateId(std::string input) { |
+ std::string result; |
+ EXPECT_TRUE(Extension::GenerateId(input, &result)); |
+ return result; |
} |
// Creates test pending extensions and inserts them into list. The |
@@ -170,7 +127,8 @@ |
scoped_ptr<Version> version( |
Version::GetVersionFromString(StringPrintf("%d.0.0.0", i))); |
ASSERT_TRUE(version.get()); |
- (*pending_extensions)[StringPrintf("extension%i", i)] = |
+ std::string id = GenerateId(StringPrintf("extension%i", i)); |
+ (*pending_extensions)[id] = |
PendingExtensionInfo(update_url, *version, |
is_theme, kInstallSilently); |
} |
@@ -336,7 +294,7 @@ |
CreateTestPendingExtensions(1, GURL(update_url), &pending_extensions); |
service.set_pending_extensions(pending_extensions); |
} else { |
- CreateTestExtensions(1, &extensions, &update_url); |
+ service.CreateTestExtensions(1, &extensions, &update_url); |
service.set_extensions(extensions); |
} |
@@ -347,9 +305,8 @@ |
TestURLFetcherFactory factory; |
URLFetcher::set_factory(&factory); |
- ScopedTempPrefService prefs; |
scoped_refptr<ExtensionUpdater> updater = |
- new ExtensionUpdater(&service, prefs.get(), 60*60*24); |
+ new ExtensionUpdater(&service, service.pref_service(), 60*60*24); |
updater->Start(); |
// Tell the update that it's time to do update checks. |
@@ -400,9 +357,8 @@ |
TestURLFetcherFactory factory; |
URLFetcher::set_factory(&factory); |
- ScopedTempPrefService prefs; |
scoped_refptr<ExtensionUpdater> updater = |
- new ExtensionUpdater(&service, prefs.get(), 60*60*24); |
+ new ExtensionUpdater(&service, service.pref_service(), 60*60*24); |
updater->Start(); |
// Tell the updater that it's time to do update checks. |
@@ -449,13 +405,13 @@ |
// Create a set of test extensions |
ServiceForManifestTests service; |
ExtensionList tmp; |
- CreateTestExtensions(3, &tmp, NULL); |
+ service.CreateTestExtensions(3, &tmp, NULL); |
service.set_extensions(tmp); |
MessageLoop message_loop; |
- ScopedTempPrefService prefs; |
scoped_refptr<ExtensionUpdater> updater = |
- new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); |
+ new ExtensionUpdater(&service, service.pref_service(), |
+ kUpdateFrequencySecs); |
// Check passing an empty list of parse results to DetermineUpdates |
ManifestFetchData fetch_data(GURL("http://localhost/foo")); |
@@ -491,9 +447,9 @@ |
service.set_pending_extensions(pending_extensions); |
MessageLoop message_loop; |
- ScopedTempPrefService prefs; |
scoped_refptr<ExtensionUpdater> updater = |
- new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); |
+ new ExtensionUpdater(&service, service.pref_service(), |
+ kUpdateFrequencySecs); |
ManifestFetchData fetch_data(GURL("http://localhost/foo")); |
UpdateManifest::Results updates; |
@@ -526,9 +482,9 @@ |
TestURLFetcher* fetcher = NULL; |
URLFetcher::set_factory(&factory); |
ServiceForDownloadTests service; |
- ScopedTempPrefService prefs; |
scoped_refptr<ExtensionUpdater> updater = |
- new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); |
+ new ExtensionUpdater(&service, service.pref_service(), |
+ kUpdateFrequencySecs); |
GURL url1("http://localhost/manifest1"); |
GURL url2("http://localhost/manifest2"); |
@@ -590,9 +546,9 @@ |
TestURLFetcher* fetcher = NULL; |
URLFetcher::set_factory(&factory); |
ServiceForDownloadTests service; |
- ScopedTempPrefService prefs; |
scoped_refptr<ExtensionUpdater> updater = |
- new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); |
+ new ExtensionUpdater(&service, service.pref_service(), |
+ kUpdateFrequencySecs); |
GURL test_url("http://localhost/extension.crx"); |
@@ -648,10 +604,10 @@ |
TestURLFetcher* fetcher = NULL; |
URLFetcher::set_factory(&factory); |
ServiceForBlacklistTests service; |
- ScopedTempPrefService prefs; |
scoped_refptr<ExtensionUpdater> updater = |
- new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); |
- prefs.get()-> |
+ new ExtensionUpdater(&service, service.pref_service(), |
+ kUpdateFrequencySecs); |
+ service.pref_service()-> |
RegisterStringPref(prefs::kExtensionBlacklistUpdateVersion, L"0"); |
GURL test_url("http://localhost/extension.crx"); |
@@ -661,7 +617,6 @@ |
"2CE109E9D0FAF820B2434E166297934E6177B65AB9951DBC3E204CAD4689B39C"; |
std::string version = "0.0.1"; |
- |
updater->FetchUpdatedExtension(id, test_url, hash, version); |
// Call back the ExtensionUpdater with a 200 response and some test data |
@@ -679,7 +634,7 @@ |
// blacklist. |
EXPECT_TRUE(service.processed_blacklist()); |
- EXPECT_EQ(version, WideToASCII(prefs.get()-> |
+ EXPECT_EQ(version, WideToASCII(service.pref_service()-> |
GetString(prefs::kExtensionBlacklistUpdateVersion))); |
URLFetcher::set_factory(NULL); |
@@ -696,9 +651,9 @@ |
TestURLFetcher* fetcher = NULL; |
URLFetcher::set_factory(&factory); |
ServiceForDownloadTests service; |
- ScopedTempPrefService prefs; |
scoped_refptr<ExtensionUpdater> updater = |
- new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); |
+ new ExtensionUpdater(&service, service.pref_service(), |
+ kUpdateFrequencySecs); |
GURL url1("http://localhost/extension1.crx"); |
GURL url2("http://localhost/extension2.crx"); |
@@ -765,24 +720,25 @@ |
ExtensionList tmp; |
GURL url1("http://clients2.google.com/service/update2/crx"); |
GURL url2("http://www.somewebsite.com"); |
- CreateTestExtensions(1, &tmp, &url1.possibly_invalid_spec()); |
- CreateTestExtensions(1, &tmp, &url2.possibly_invalid_spec()); |
+ service.CreateTestExtensions(1, &tmp, &url1.possibly_invalid_spec()); |
+ service.CreateTestExtensions(1, &tmp, &url2.possibly_invalid_spec()); |
EXPECT_EQ(2u, tmp.size()); |
service.set_extensions(tmp); |
Time now = Time::Now(); |
if (ping_days == 0) { |
- service.SetLastPingDay(tmp[0]->id(), now - TimeDelta::FromSeconds(15)); |
+ service.extension_prefs()->SetLastPingDay( |
+ tmp[0]->id(), now - TimeDelta::FromSeconds(15)); |
} else if (ping_days > 0) { |
Time last_ping_day = |
now - TimeDelta::FromDays(ping_days) - TimeDelta::FromSeconds(15); |
- service.SetLastPingDay(tmp[0]->id(), last_ping_day); |
+ service.extension_prefs()->SetLastPingDay(tmp[0]->id(), last_ping_day); |
} |
MessageLoop message_loop; |
- ScopedTempPrefService prefs; |
scoped_refptr<ExtensionUpdater> updater = |
- new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); |
+ new ExtensionUpdater(&service, service.pref_service(), |
+ kUpdateFrequencySecs); |
updater->set_blacklist_checks_enabled(false); |
// Make the updater do manifest fetching, and note the urls it tries to |
@@ -834,13 +790,13 @@ |
// >= 1 day for the extension. |
static void TestHandleManifestResults() { |
ServiceForManifestTests service; |
- ScopedTempPrefService prefs; |
scoped_refptr<ExtensionUpdater> updater = |
- new ExtensionUpdater(&service, prefs.get(), kUpdateFrequencySecs); |
+ new ExtensionUpdater(&service, service.pref_service(), |
+ kUpdateFrequencySecs); |
GURL update_url("http://www.google.com/manifest"); |
ExtensionList tmp; |
- CreateTestExtensions(1, &tmp, &update_url.spec()); |
+ service.CreateTestExtensions(1, &tmp, &update_url.spec()); |
service.set_extensions(tmp); |
ManifestFetchData fetch_data(update_url); |
@@ -851,7 +807,8 @@ |
results.daystart_elapsed_seconds = 750; |
updater->HandleManifestResults(fetch_data, results); |
- Time last_ping_day = service.LastPingDay(extension->id()); |
+ Time last_ping_day = |
+ service.extension_prefs()->LastPingDay(extension->id()); |
EXPECT_FALSE(last_ping_day.is_null()); |
int64 seconds_diff = (Time::Now() - last_ping_day).InSeconds(); |
EXPECT_LT(seconds_diff - results.daystart_elapsed_seconds, 5); |
@@ -924,7 +881,7 @@ |
// Non-internal non-external extensions should be rejected. |
{ |
ExtensionList extensions; |
- CreateTestExtensions(1, &extensions, NULL); |
+ service.CreateTestExtensions(1, &extensions, NULL); |
ASSERT_FALSE(extensions.empty()); |
extensions[0]->set_location(Extension::INVALID); |
builder.AddExtension(*extensions[0]); |
@@ -937,8 +894,8 @@ |
// Extensions with invalid update URLs should be rejected. |
builder.AddPendingExtension( |
- "id", PendingExtensionInfo(GURL("http:google.com:foo"), |
- *version, false, false)); |
+ GenerateId("foo"), PendingExtensionInfo(GURL("http:google.com:foo"), |
+ *version, false, false)); |
EXPECT_TRUE(builder.GetFetches().empty()); |
// Extensions with empty IDs should be rejected. |
@@ -952,7 +909,7 @@ |
// Extensions with empty update URLs should have a default one |
// filled in. |
builder.AddPendingExtension( |
- "id", PendingExtensionInfo(GURL(), *version, false, false)); |
+ GenerateId("foo"), PendingExtensionInfo(GURL(), *version, false, false)); |
std::vector<ManifestFetchData*> fetches = builder.GetFetches(); |
ASSERT_EQ(1u, fetches.size()); |
scoped_ptr<ManifestFetchData> fetch(fetches[0]); |