Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/extensions/test_extension_prefs.h" | 5 #include "chrome/browser/extensions/test_extension_prefs.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/browser_thread.h" | 11 #include "chrome/browser/browser_thread.h" |
| 12 #include "chrome/browser/extensions/extension_prefs.h" | 12 #include "chrome/browser/extensions/extension_prefs.h" |
| 13 #include "chrome/browser/extensions/extension_pref_store.h" | |
| 14 #include "chrome/browser/prefs/browser_prefs.h" | |
| 15 #include "chrome/browser/prefs/dummy_pref_store.h" | |
| 13 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/prefs/pref_value_store.h" | 17 #include "chrome/browser/prefs/pref_value_store.h" |
| 15 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/extensions/extension_constants.h" | 19 #include "chrome/common/extensions/extension_constants.h" |
| 17 #include "chrome/common/json_pref_store.h" | 20 #include "chrome/common/json_pref_store.h" |
| 21 #include "chrome/test/testing_pref_service.h" | |
| 22 #include "chrome/test/testing_profile.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 24 |
| 20 TestExtensionPrefs::TestExtensionPrefs() { | 25 // Mock ExtensionPrefs class with artificial clock to guarantee that no two |
| 26 // extensions get the same installation time stamp and we can reliably | |
| 27 // assert the installation order in the tests below. | |
| 28 class MockExtensionPrefs : public ExtensionPrefs { | |
| 29 public: | |
| 30 MockExtensionPrefs(Profile* profile, | |
| 31 PrefService* prefs, | |
| 32 const FilePath& root_dir_) | |
| 33 : ExtensionPrefs(profile, prefs, root_dir_), | |
| 34 currentTime(base::Time::Now()) | |
| 35 {} | |
| 36 ~MockExtensionPrefs() {} | |
| 37 | |
| 38 protected: | |
| 39 mutable base::Time currentTime; | |
| 40 | |
| 41 virtual base::Time GetCurrentTime() const { | |
| 42 currentTime += base::TimeDelta::FromSeconds(10); | |
| 43 return currentTime; | |
| 44 } | |
| 45 }; | |
| 46 | |
| 47 class TestingProfileWithFixedPath : public TestingProfile { | |
| 48 public: | |
| 49 explicit TestingProfileWithFixedPath(const FilePath& path) : path_(path) {} | |
| 50 virtual ~TestingProfileWithFixedPath() {} | |
| 51 | |
| 52 virtual void CreateTestingPrefService() { | |
| 53 DCHECK(!prefs_.get()); | |
| 54 PrefStore* managed_platform_prefs = new DummyPrefStore(); | |
| 55 PrefStore* device_management_prefs = new DummyPrefStore(); | |
| 56 PrefStore* extension_prefs = new ExtensionPrefStore(this); | |
| 57 PrefStore* command_line_prefs = NULL; | |
| 58 PrefStore* user_prefs = new JsonPrefStore( | |
| 59 path_.AppendASCII("Preferences"), | |
| 60 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | |
| 61 PrefStore* recommended_prefs = NULL; | |
| 62 PrefStore* default_pref = new DummyPrefStore(); | |
| 63 testing_prefs_ = new TestingPrefService(managed_platform_prefs, | |
| 64 device_management_prefs, | |
| 65 extension_prefs, | |
| 66 command_line_prefs, | |
| 67 user_prefs, | |
| 68 recommended_prefs, | |
| 69 default_pref); | |
| 70 prefs_.reset(testing_prefs_); | |
| 71 Profile::RegisterUserPrefs(prefs_.get()); | |
| 72 browser::RegisterAllPrefs(prefs_.get(), prefs_.get()); | |
| 73 } | |
| 74 | |
| 75 virtual FilePath GetPath() { | |
| 76 return path_; | |
| 77 } | |
| 78 private: | |
| 79 FilePath path_; | |
| 80 }; | |
| 81 | |
| 82 TestExtensionPrefs::TestExtensionPrefs() : pref_service_(NULL) { | |
| 21 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | 83 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 22 preferences_file_ = temp_dir_.path().AppendASCII("Preferences"); | 84 preferences_file_ = temp_dir_.path().AppendASCII("Preferences"); |
| 23 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); | 85 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); |
| 24 EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_)); | 86 EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_)); |
| 25 | 87 |
| 26 RecreateExtensionPrefs(); | 88 RecreateExtensionPrefs(); |
| 27 } | 89 } |
| 28 | 90 |
| 29 TestExtensionPrefs::~TestExtensionPrefs() {} | 91 TestExtensionPrefs::~TestExtensionPrefs() {} |
| 30 | 92 |
| 31 void TestExtensionPrefs::RecreateExtensionPrefs() { | 93 void TestExtensionPrefs::RecreateExtensionPrefs() { |
| 32 if (pref_service_.get()) { | 94 if (pref_service_) { |
| 33 // The PrefService writes its persistent file on the file thread, so we | 95 // The PrefService writes its persistent file on the file thread, so we |
| 34 // need to wait for any pending I/O to complete before creating a new | 96 // need to wait for any pending I/O to complete before creating a new |
| 35 // PrefService. | 97 // PrefService. |
| 36 MessageLoop file_loop; | 98 MessageLoop file_loop; |
| 37 BrowserThread file_thread(BrowserThread::FILE, &file_loop); | 99 BrowserThread file_thread(BrowserThread::FILE, &file_loop); |
| 38 pref_service_->SavePersistentPrefs(); | 100 pref_service_->SavePersistentPrefs(); |
| 39 file_loop.RunAllPending(); | 101 file_loop.RunAllPending(); |
| 40 } | 102 } |
| 41 | 103 |
| 42 // Create a |PrefService| instance that contains only user defined values. | 104 profile_.reset(new TestingProfileWithFixedPath(temp_dir_.path())); |
| 43 pref_service_.reset(PrefService::CreateUserPrefService(preferences_file_)); | 105 pref_service_ = profile_->GetPrefs(); |
|
Mattias Nissler (ping if slow)
2010/12/01 10:36:36
Why doesn't this work any longer? Anyway, these te
battre (please use the other)
2010/12/01 17:44:38
@Why doesn't this work any longer?
The PrefService
| |
| 44 ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); | 106 prefs_.reset(new MockExtensionPrefs(profile_.get(), |
| 45 prefs_.reset(new ExtensionPrefs(pref_service_.get(), temp_dir_.path())); | 107 pref_service_, |
| 108 temp_dir_.path())); | |
| 46 } | 109 } |
| 47 | 110 |
| 48 scoped_refptr<Extension> TestExtensionPrefs::AddExtension(std::string name) { | 111 scoped_refptr<Extension> TestExtensionPrefs::AddExtension(std::string name) { |
| 49 DictionaryValue dictionary; | 112 DictionaryValue dictionary; |
| 50 dictionary.SetString(extension_manifest_keys::kName, name); | 113 dictionary.SetString(extension_manifest_keys::kName, name); |
| 51 dictionary.SetString(extension_manifest_keys::kVersion, "0.1"); | 114 dictionary.SetString(extension_manifest_keys::kVersion, "0.1"); |
| 52 return AddExtensionWithManifest(dictionary, Extension::INTERNAL); | 115 return AddExtensionWithManifest(dictionary, Extension::INTERNAL); |
| 53 } | 116 } |
| 54 | 117 |
| 55 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest( | 118 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest( |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 68 const bool kInitialIncognitoEnabled = false; | 131 const bool kInitialIncognitoEnabled = false; |
| 69 prefs_->OnExtensionInstalled(extension, Extension::ENABLED, | 132 prefs_->OnExtensionInstalled(extension, Extension::ENABLED, |
| 70 kInitialIncognitoEnabled); | 133 kInitialIncognitoEnabled); |
| 71 return extension; | 134 return extension; |
| 72 } | 135 } |
| 73 | 136 |
| 74 std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) { | 137 std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) { |
| 75 scoped_refptr<Extension> extension(AddExtension(name)); | 138 scoped_refptr<Extension> extension(AddExtension(name)); |
| 76 return extension->id(); | 139 return extension->id(); |
| 77 } | 140 } |
| OLD | NEW |