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 TestExtensionPrefs::TestExtensionPrefs() { | 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 MockExtensionPrefs : public ExtensionPrefs { |
| 24 public: |
| 25 MockExtensionPrefs(PrefService* prefs, const FilePath& root_dir_) |
| 26 : ExtensionPrefs(prefs, root_dir_), |
| 27 currentTime(base::Time::Now()) |
| 28 {} |
| 29 ~MockExtensionPrefs() {} |
| 30 |
| 31 protected: |
| 32 mutable base::Time currentTime; |
| 33 |
| 34 virtual base::Time GetCurrentTime() const { |
| 35 currentTime += base::TimeDelta::FromSeconds(10); |
| 36 return currentTime; |
| 37 } |
| 38 }; |
| 39 |
| 40 TestExtensionPrefs::TestExtensionPrefs() : pref_service_(NULL) { |
21 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | 41 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
22 preferences_file_ = temp_dir_.path().AppendASCII("Preferences"); | 42 preferences_file_ = temp_dir_.path().AppendASCII("Preferences"); |
23 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); | 43 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); |
24 EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_)); | 44 EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_)); |
25 | 45 |
26 RecreateExtensionPrefs(); | 46 RecreateExtensionPrefs(); |
27 } | 47 } |
28 | 48 |
29 TestExtensionPrefs::~TestExtensionPrefs() {} | 49 TestExtensionPrefs::~TestExtensionPrefs() {} |
30 | 50 |
31 void TestExtensionPrefs::RecreateExtensionPrefs() { | 51 void TestExtensionPrefs::RecreateExtensionPrefs() { |
| 52 // We persist and reload the PrefService's PrefStores because this process |
| 53 // deletes all empty dictionaries. The ExtensionPrefs implementation |
| 54 // needs to be able to handle this situation. |
32 if (pref_service_.get()) { | 55 if (pref_service_.get()) { |
33 // The PrefService writes its persistent file on the file thread, so we | 56 // 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 | 57 // need to wait for any pending I/O to complete before creating a new |
35 // PrefService. | 58 // PrefService. |
36 MessageLoop file_loop; | 59 MessageLoop file_loop; |
37 BrowserThread file_thread(BrowserThread::FILE, &file_loop); | 60 BrowserThread file_thread(BrowserThread::FILE, &file_loop); |
38 pref_service_->SavePersistentPrefs(); | 61 pref_service_->SavePersistentPrefs(); |
39 file_loop.RunAllPending(); | 62 file_loop.RunAllPending(); |
40 } | 63 } |
41 | 64 |
42 // Create a |PrefService| instance that contains only user defined values. | 65 // Create a |PrefService| instance that contains only user defined values. |
43 pref_service_.reset(PrefService::CreateUserPrefService(preferences_file_)); | 66 pref_service_.reset(PrefService::CreateUserPrefService(preferences_file_)); |
44 ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); | 67 ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); |
45 prefs_.reset(new ExtensionPrefs(pref_service_.get(), temp_dir_.path())); | 68 prefs_.reset(new MockExtensionPrefs(pref_service_.get(), temp_dir_.path())); |
46 } | 69 } |
47 | 70 |
48 scoped_refptr<Extension> TestExtensionPrefs::AddExtension(std::string name) { | 71 scoped_refptr<Extension> TestExtensionPrefs::AddExtension(std::string name) { |
49 DictionaryValue dictionary; | 72 DictionaryValue dictionary; |
50 dictionary.SetString(extension_manifest_keys::kName, name); | 73 dictionary.SetString(extension_manifest_keys::kName, name); |
51 dictionary.SetString(extension_manifest_keys::kVersion, "0.1"); | 74 dictionary.SetString(extension_manifest_keys::kVersion, "0.1"); |
52 return AddExtensionWithManifest(dictionary, Extension::INTERNAL); | 75 return AddExtensionWithManifest(dictionary, Extension::INTERNAL); |
53 } | 76 } |
54 | 77 |
55 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest( | 78 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest( |
(...skipping 12 matching lines...) Expand all Loading... |
68 const bool kInitialIncognitoEnabled = false; | 91 const bool kInitialIncognitoEnabled = false; |
69 prefs_->OnExtensionInstalled(extension, Extension::ENABLED, | 92 prefs_->OnExtensionInstalled(extension, Extension::ENABLED, |
70 kInitialIncognitoEnabled); | 93 kInitialIncognitoEnabled); |
71 return extension; | 94 return extension; |
72 } | 95 } |
73 | 96 |
74 std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) { | 97 std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) { |
75 scoped_refptr<Extension> extension(AddExtension(name)); | 98 scoped_refptr<Extension> extension(AddExtension(name)); |
76 return extension->id(); | 99 return extension->id(); |
77 } | 100 } |
OLD | NEW |