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