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/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/prefs/pref_value_store.h" | 14 #include "chrome/browser/prefs/pref_value_store.h" |
| 15 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/extensions/extension_constants.h" | 16 #include "chrome/common/extensions/extension_constants.h" |
| 17 #include "chrome/common/json_pref_store.h" | 17 #include "chrome/common/json_pref_store.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 // Mock ExtensionPrefs class with artificial clock to guarantee that no two | |
| 21 // extensions get the same installation time stamp and we can reliably | |
| 22 // assert the installation order in the tests below. | |
| 23 class TestingExtensionPrefs : public ExtensionPrefs { | |
|
Aaron Boodman
2010/11/23 20:35:13
Maybe name this MockExtensionPrefs to distinguish
battre (please use the other)
2010/11/30 17:46:53
Done.
| |
| 24 public: | |
| 25 TestingExtensionPrefs(PrefService* prefs, const FilePath& root_dir_) | |
| 26 : ExtensionPrefs(prefs, root_dir_), currentTime(base::Time::Now()) {} | |
| 27 ~TestingExtensionPrefs() {} | |
| 28 | |
| 29 protected: | |
| 30 mutable base::Time currentTime; | |
| 31 | |
| 32 virtual base::Time GetCurrentTime() const { | |
| 33 currentTime += base::TimeDelta::FromSeconds(10); | |
| 34 return currentTime; | |
| 35 } | |
| 36 }; | |
| 37 | |
| 20 TestExtensionPrefs::TestExtensionPrefs() { | 38 TestExtensionPrefs::TestExtensionPrefs() { |
| 21 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | 39 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 22 preferences_file_ = temp_dir_.path().AppendASCII("Preferences"); | 40 preferences_file_ = temp_dir_.path().AppendASCII("Preferences"); |
| 23 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); | 41 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); |
| 24 EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_)); | 42 EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_)); |
| 25 | 43 |
| 26 RecreateExtensionPrefs(); | 44 RecreateExtensionPrefs(); |
| 27 } | 45 } |
| 28 | 46 |
| 29 TestExtensionPrefs::~TestExtensionPrefs() {} | 47 TestExtensionPrefs::~TestExtensionPrefs() {} |
| 30 | 48 |
| 31 void TestExtensionPrefs::RecreateExtensionPrefs() { | 49 void TestExtensionPrefs::RecreateExtensionPrefs() { |
| 32 if (pref_service_.get()) { | 50 if (pref_service_.get()) { |
| 33 // The PrefService writes its persistent file on the file thread, so we | 51 // 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 | 52 // need to wait for any pending I/O to complete before creating a new |
| 35 // PrefService. | 53 // PrefService. |
| 36 MessageLoop file_loop; | 54 MessageLoop file_loop; |
| 37 BrowserThread file_thread(BrowserThread::FILE, &file_loop); | 55 BrowserThread file_thread(BrowserThread::FILE, &file_loop); |
| 38 pref_service_->SavePersistentPrefs(); | 56 pref_service_->SavePersistentPrefs(); |
| 39 file_loop.RunAllPending(); | 57 file_loop.RunAllPending(); |
| 40 } | 58 } |
| 41 | 59 |
| 42 // Create a |PrefService| instance that contains only user defined values. | 60 // Create a |PrefService| instance that contains only user defined values. |
| 43 pref_service_.reset(PrefService::CreateUserPrefService(preferences_file_)); | 61 pref_service_.reset(PrefService::CreateUserPrefService(preferences_file_)); |
| 44 ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); | 62 ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); |
| 45 prefs_.reset(new ExtensionPrefs(pref_service_.get(), temp_dir_.path())); | 63 prefs_.reset(new TestingExtensionPrefs(pref_service_.get(), |
| 64 temp_dir_.path())); | |
| 46 } | 65 } |
| 47 | 66 |
| 48 scoped_refptr<Extension> TestExtensionPrefs::AddExtension(std::string name) { | 67 scoped_refptr<Extension> TestExtensionPrefs::AddExtension(std::string name) { |
| 49 DictionaryValue dictionary; | 68 DictionaryValue dictionary; |
| 50 dictionary.SetString(extension_manifest_keys::kName, name); | 69 dictionary.SetString(extension_manifest_keys::kName, name); |
| 51 dictionary.SetString(extension_manifest_keys::kVersion, "0.1"); | 70 dictionary.SetString(extension_manifest_keys::kVersion, "0.1"); |
| 52 return AddExtensionWithManifest(dictionary, Extension::INTERNAL); | 71 return AddExtensionWithManifest(dictionary, Extension::INTERNAL); |
| 53 } | 72 } |
| 54 | 73 |
| 55 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest( | 74 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest( |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 68 const bool kInitialIncognitoEnabled = false; | 87 const bool kInitialIncognitoEnabled = false; |
| 69 prefs_->OnExtensionInstalled(extension, Extension::ENABLED, | 88 prefs_->OnExtensionInstalled(extension, Extension::ENABLED, |
| 70 kInitialIncognitoEnabled); | 89 kInitialIncognitoEnabled); |
| 71 return extension; | 90 return extension; |
| 72 } | 91 } |
| 73 | 92 |
| 74 std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) { | 93 std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) { |
| 75 scoped_refptr<Extension> extension(AddExtension(name)); | 94 scoped_refptr<Extension> extension(AddExtension(name)); |
| 76 return extension->id(); | 95 return extension->id(); |
| 77 } | 96 } |
| OLD | NEW |