| 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/message_loop_proxy.h" |
| 9 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 11 #include "chrome/browser/browser_thread.h" | 12 #include "chrome/browser/browser_thread.h" |
| 13 #include "chrome/browser/extensions/extension_pref_store.h" |
| 12 #include "chrome/browser/extensions/extension_prefs.h" | 14 #include "chrome/browser/extensions/extension_prefs.h" |
| 13 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
| 16 #include "chrome/browser/prefs/pref_service_mock_builder.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" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 22 |
| 23 namespace { |
| 24 |
| 20 // Mock ExtensionPrefs class with artificial clock to guarantee that no two | 25 // Mock ExtensionPrefs class with artificial clock to guarantee that no two |
| 21 // extensions get the same installation time stamp and we can reliably | 26 // extensions get the same installation time stamp and we can reliably |
| 22 // assert the installation order in the tests below. | 27 // assert the installation order in the tests below. |
| 23 class MockExtensionPrefs : public ExtensionPrefs { | 28 class MockExtensionPrefs : public ExtensionPrefs { |
| 24 public: | 29 public: |
| 25 MockExtensionPrefs(PrefService* prefs, const FilePath& root_dir_) | 30 MockExtensionPrefs(PrefService* prefs, |
| 26 : ExtensionPrefs(prefs, root_dir_), | 31 const FilePath& root_dir_, |
| 27 currentTime(base::Time::Now()) | 32 ExtensionPrefStore* pref_store) |
| 28 {} | 33 : ExtensionPrefs(prefs, root_dir_, pref_store), |
| 34 currentTime(base::Time::Now()) {} |
| 29 ~MockExtensionPrefs() {} | 35 ~MockExtensionPrefs() {} |
| 30 | 36 |
| 31 protected: | 37 protected: |
| 32 mutable base::Time currentTime; | 38 mutable base::Time currentTime; |
| 33 | 39 |
| 34 virtual base::Time GetCurrentTime() const { | 40 virtual base::Time GetCurrentTime() const { |
| 35 currentTime += base::TimeDelta::FromSeconds(10); | 41 currentTime += base::TimeDelta::FromSeconds(10); |
| 36 return currentTime; | 42 return currentTime; |
| 37 } | 43 } |
| 38 }; | 44 }; |
| 39 | 45 |
| 46 } // namespace |
| 47 |
| 40 TestExtensionPrefs::TestExtensionPrefs() : pref_service_(NULL) { | 48 TestExtensionPrefs::TestExtensionPrefs() : pref_service_(NULL) { |
| 41 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | 49 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 42 preferences_file_ = temp_dir_.path().AppendASCII("Preferences"); | 50 preferences_file_ = temp_dir_.path().AppendASCII("Preferences"); |
| 43 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); | 51 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); |
| 44 EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_)); | 52 EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_)); |
| 45 | 53 |
| 46 RecreateExtensionPrefs(); | 54 RecreateExtensionPrefs(); |
| 47 } | 55 } |
| 48 | 56 |
| 49 TestExtensionPrefs::~TestExtensionPrefs() {} | 57 TestExtensionPrefs::~TestExtensionPrefs() {} |
| 50 | 58 |
| 51 void TestExtensionPrefs::RecreateExtensionPrefs() { | 59 void TestExtensionPrefs::RecreateExtensionPrefs() { |
| 52 // We persist and reload the PrefService's PrefStores because this process | 60 // We persist and reload the PrefService's PrefStores because this process |
| 53 // deletes all empty dictionaries. The ExtensionPrefs implementation | 61 // deletes all empty dictionaries. The ExtensionPrefs implementation |
| 54 // needs to be able to handle this situation. | 62 // needs to be able to handle this situation. |
| 55 if (pref_service_.get()) { | 63 if (pref_service_.get()) { |
| 56 // The PrefService writes its persistent file on the file thread, so we | 64 // The PrefService writes its persistent file on the file thread, so we |
| 57 // need to wait for any pending I/O to complete before creating a new | 65 // need to wait for any pending I/O to complete before creating a new |
| 58 // PrefService. | 66 // PrefService. |
| 59 MessageLoop file_loop; | 67 MessageLoop file_loop; |
| 60 BrowserThread file_thread(BrowserThread::FILE, &file_loop); | 68 BrowserThread file_thread(BrowserThread::FILE, &file_loop); |
| 61 pref_service_->SavePersistentPrefs(); | 69 pref_service_->SavePersistentPrefs(); |
| 62 file_loop.RunAllPending(); | 70 file_loop.RunAllPending(); |
| 63 } | 71 } |
| 64 | 72 |
| 65 // Create a |PrefService| instance that contains only user defined values. | 73 ExtensionPrefStore* pref_store = new ExtensionPrefStore; |
| 66 pref_service_.reset(PrefService::CreateUserPrefService(preferences_file_)); | 74 PrefServiceMockBuilder builder; |
| 75 builder.WithUserFilePrefs(preferences_file_); |
| 76 builder.WithExtensionPrefs(pref_store); |
| 77 pref_service_.reset(builder.Create()); |
| 67 ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); | 78 ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); |
| 68 prefs_.reset(new MockExtensionPrefs(pref_service_.get(), temp_dir_.path())); | 79 prefs_.reset(new MockExtensionPrefs(pref_service_.get(), temp_dir_.path(), |
| 80 pref_store)); |
| 69 } | 81 } |
| 70 | 82 |
| 71 scoped_refptr<Extension> TestExtensionPrefs::AddExtension(std::string name) { | 83 scoped_refptr<Extension> TestExtensionPrefs::AddExtension(std::string name) { |
| 72 DictionaryValue dictionary; | 84 DictionaryValue dictionary; |
| 73 dictionary.SetString(extension_manifest_keys::kName, name); | 85 dictionary.SetString(extension_manifest_keys::kName, name); |
| 74 dictionary.SetString(extension_manifest_keys::kVersion, "0.1"); | 86 dictionary.SetString(extension_manifest_keys::kVersion, "0.1"); |
| 75 return AddExtensionWithManifest(dictionary, Extension::INTERNAL); | 87 return AddExtensionWithManifest(dictionary, Extension::INTERNAL); |
| 76 } | 88 } |
| 77 | 89 |
| 78 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest( | 90 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 91 const bool kInitialIncognitoEnabled = false; | 103 const bool kInitialIncognitoEnabled = false; |
| 92 prefs_->OnExtensionInstalled(extension, Extension::ENABLED, | 104 prefs_->OnExtensionInstalled(extension, Extension::ENABLED, |
| 93 kInitialIncognitoEnabled); | 105 kInitialIncognitoEnabled); |
| 94 return extension; | 106 return extension; |
| 95 } | 107 } |
| 96 | 108 |
| 97 std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) { | 109 std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) { |
| 98 scoped_refptr<Extension> extension(AddExtension(name)); | 110 scoped_refptr<Extension> extension(AddExtension(name)); |
| 99 return extension->id(); | 111 return extension->id(); |
| 100 } | 112 } |
| OLD | NEW |