OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/message_loop_proxy.h" |
10 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/browser_thread.h" | 12 #include "chrome/browser/browser_thread.h" |
13 #include "chrome/browser/extensions/extension_pref_store.h" | 13 #include "chrome/browser/extensions/extension_pref_store.h" |
| 14 #include "chrome/browser/extensions/extension_pref_value_map.h" |
14 #include "chrome/browser/extensions/extension_prefs.h" | 15 #include "chrome/browser/extensions/extension_prefs.h" |
15 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
16 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 17 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
17 #include "chrome/browser/prefs/pref_value_store.h" | 18 #include "chrome/browser/prefs/pref_value_store.h" |
18 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
19 #include "chrome/common/extensions/extension_constants.h" | 20 #include "chrome/common/extensions/extension_constants.h" |
20 #include "chrome/common/json_pref_store.h" | 21 #include "chrome/common/json_pref_store.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // Mock ExtensionPrefs class with artificial clock to guarantee that no two | 26 // Mock ExtensionPrefs class with artificial clock to guarantee that no two |
26 // extensions get the same installation time stamp and we can reliably | 27 // extensions get the same installation time stamp and we can reliably |
27 // assert the installation order in the tests below. | 28 // assert the installation order in the tests below. |
28 class MockExtensionPrefs : public ExtensionPrefs { | 29 class MockExtensionPrefs : public ExtensionPrefs { |
29 public: | 30 public: |
30 MockExtensionPrefs(PrefService* prefs, | 31 MockExtensionPrefs(PrefService* prefs, |
31 const FilePath& root_dir_, | 32 const FilePath& root_dir, |
32 ExtensionPrefStore* pref_store) | 33 ExtensionPrefValueMap* extension_pref_value_map) |
33 : ExtensionPrefs(prefs, root_dir_, pref_store), | 34 : ExtensionPrefs(prefs, root_dir, extension_pref_value_map), |
34 currentTime(base::Time::Now()) {} | 35 currentTime(base::Time::Now()) {} |
35 ~MockExtensionPrefs() {} | 36 ~MockExtensionPrefs() {} |
36 | 37 |
37 protected: | 38 protected: |
38 mutable base::Time currentTime; | 39 mutable base::Time currentTime; |
39 | 40 |
40 virtual base::Time GetCurrentTime() const { | 41 virtual base::Time GetCurrentTime() const { |
41 currentTime += base::TimeDelta::FromSeconds(10); | 42 currentTime += base::TimeDelta::FromSeconds(10); |
42 return currentTime; | 43 return currentTime; |
43 } | 44 } |
(...skipping 19 matching lines...) Expand all Loading... |
63 if (pref_service_.get()) { | 64 if (pref_service_.get()) { |
64 // The PrefService writes its persistent file on the file thread, so we | 65 // The PrefService writes its persistent file on the file thread, so we |
65 // need to wait for any pending I/O to complete before creating a new | 66 // need to wait for any pending I/O to complete before creating a new |
66 // PrefService. | 67 // PrefService. |
67 MessageLoop file_loop; | 68 MessageLoop file_loop; |
68 BrowserThread file_thread(BrowserThread::FILE, &file_loop); | 69 BrowserThread file_thread(BrowserThread::FILE, &file_loop); |
69 pref_service_->SavePersistentPrefs(); | 70 pref_service_->SavePersistentPrefs(); |
70 file_loop.RunAllPending(); | 71 file_loop.RunAllPending(); |
71 } | 72 } |
72 | 73 |
73 ExtensionPrefStore* pref_store = new ExtensionPrefStore; | 74 extension_pref_value_map_.reset(new ExtensionPrefValueMap); |
74 PrefServiceMockBuilder builder; | 75 PrefServiceMockBuilder builder; |
75 builder.WithUserFilePrefs(preferences_file_); | 76 builder.WithUserFilePrefs(preferences_file_); |
76 builder.WithExtensionPrefs(pref_store); | 77 builder.WithExtensionPrefs( |
| 78 new ExtensionPrefStore(extension_pref_value_map_.get(), false)); |
77 pref_service_.reset(builder.Create()); | 79 pref_service_.reset(builder.Create()); |
78 ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); | 80 ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); |
79 prefs_.reset(new MockExtensionPrefs(pref_service_.get(), temp_dir_.path(), | 81 |
80 pref_store)); | 82 prefs_.reset(new MockExtensionPrefs(pref_service_.get(), |
| 83 temp_dir_.path(), |
| 84 extension_pref_value_map_.get())); |
81 } | 85 } |
82 | 86 |
83 scoped_refptr<Extension> TestExtensionPrefs::AddExtension(std::string name) { | 87 scoped_refptr<Extension> TestExtensionPrefs::AddExtension(std::string name) { |
84 DictionaryValue dictionary; | 88 DictionaryValue dictionary; |
85 dictionary.SetString(extension_manifest_keys::kName, name); | 89 dictionary.SetString(extension_manifest_keys::kName, name); |
86 dictionary.SetString(extension_manifest_keys::kVersion, "0.1"); | 90 dictionary.SetString(extension_manifest_keys::kVersion, "0.1"); |
87 return AddExtensionWithManifest(dictionary, Extension::INTERNAL); | 91 return AddExtensionWithManifest(dictionary, Extension::INTERNAL); |
88 } | 92 } |
89 | 93 |
90 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest( | 94 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest( |
(...skipping 12 matching lines...) Expand all Loading... |
103 const bool kInitialIncognitoEnabled = false; | 107 const bool kInitialIncognitoEnabled = false; |
104 prefs_->OnExtensionInstalled(extension, Extension::ENABLED, | 108 prefs_->OnExtensionInstalled(extension, Extension::ENABLED, |
105 kInitialIncognitoEnabled); | 109 kInitialIncognitoEnabled); |
106 return extension; | 110 return extension; |
107 } | 111 } |
108 | 112 |
109 std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) { | 113 std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) { |
110 scoped_refptr<Extension> extension(AddExtension(name)); | 114 scoped_refptr<Extension> extension(AddExtension(name)); |
111 return extension->id(); | 115 return extension->id(); |
112 } | 116 } |
| 117 |
| 118 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const { |
| 119 return pref_service_->CreateIncognitoPrefService( |
| 120 new ExtensionPrefStore(extension_pref_value_map_.get(), true)); |
| 121 } |
OLD | NEW |