Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(317)

Side by Side Diff: chrome/browser/extensions/test_extension_prefs.cc

Issue 11027070: Moved JsonPrefStore to use SequencedWorkerPool (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/message_loop_proxy.h" 12 #include "base/message_loop_proxy.h"
13 #include "base/prefs/json_pref_store.h" 13 #include "base/prefs/json_pref_store.h"
14 #include "base/run_loop.h"
15 #include "base/sequenced_task_runner.h"
14 #include "base/synchronization/waitable_event.h" 16 #include "base/synchronization/waitable_event.h"
15 #include "base/values.h" 17 #include "base/values.h"
16 #include "chrome/browser/extensions/extension_pref_store.h" 18 #include "chrome/browser/extensions/extension_pref_store.h"
17 #include "chrome/browser/extensions/extension_pref_value_map.h" 19 #include "chrome/browser/extensions/extension_pref_value_map.h"
18 #include "chrome/browser/extensions/extension_prefs.h" 20 #include "chrome/browser/extensions/extension_prefs.h"
19 #include "chrome/browser/prefs/pref_service.h" 21 #include "chrome/browser/prefs/pref_service.h"
20 #include "chrome/browser/prefs/pref_service_mock_builder.h" 22 #include "chrome/browser/prefs/pref_service_mock_builder.h"
21 #include "chrome/browser/prefs/pref_value_store.h" 23 #include "chrome/browser/prefs/pref_value_store.h"
22 #include "chrome/common/extensions/extension.h" 24 #include "chrome/common/extensions/extension.h"
23 #include "chrome/common/extensions/extension_manifest_constants.h" 25 #include "chrome/common/extensions/extension_manifest_constants.h"
24 #include "content/public/browser/browser_thread.h" 26 #include "content/public/browser/browser_thread.h"
25 #include "sync/api/string_ordinal.h" 27 #include "sync/api/string_ordinal.h"
26 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
27 29
28 using content::BrowserThread; 30 using content::BrowserThread;
29 31
30 namespace extensions { 32 namespace extensions {
31 33
32 namespace { 34 namespace {
33 35
36 void DoNothing() {}
37
34 // Mock ExtensionPrefs class with artificial clock to guarantee that no two 38 // Mock ExtensionPrefs class with artificial clock to guarantee that no two
35 // extensions get the same installation time stamp and we can reliably 39 // extensions get the same installation time stamp and we can reliably
36 // assert the installation order in the tests below. 40 // assert the installation order in the tests below.
37 class MockExtensionPrefs : public ExtensionPrefs { 41 class MockExtensionPrefs : public ExtensionPrefs {
38 public: 42 public:
39 MockExtensionPrefs(PrefService* prefs, 43 MockExtensionPrefs(PrefService* prefs,
40 const FilePath& root_dir, 44 const FilePath& root_dir,
41 ExtensionPrefValueMap* extension_pref_value_map) 45 ExtensionPrefValueMap* extension_pref_value_map)
42 : ExtensionPrefs(prefs, root_dir, extension_pref_value_map), 46 : ExtensionPrefs(prefs, root_dir, extension_pref_value_map),
43 currentTime(base::Time::Now()) {} 47 currentTime(base::Time::Now()) {}
44 ~MockExtensionPrefs() {} 48 ~MockExtensionPrefs() {}
45 49
46 protected: 50 protected:
47 mutable base::Time currentTime; 51 mutable base::Time currentTime;
48 52
49 virtual base::Time GetCurrentTime() const { 53 virtual base::Time GetCurrentTime() const {
50 currentTime += base::TimeDelta::FromSeconds(10); 54 currentTime += base::TimeDelta::FromSeconds(10);
51 return currentTime; 55 return currentTime;
52 } 56 }
53 }; 57 };
54 58
55 } // namespace 59 } // namespace
56 60
57 TestExtensionPrefs::TestExtensionPrefs() 61 TestExtensionPrefs::TestExtensionPrefs(
58 : pref_service_(NULL), 62 base::SequencedTaskRunner* task_runner) : pref_service_(NULL),
59 extensions_disabled_(false) { 63 task_runner_(task_runner),
64 extensions_disabled_(false) {
60 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); 65 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
61 preferences_file_ = temp_dir_.path().AppendASCII("Preferences"); 66 preferences_file_ = temp_dir_.path().AppendASCII("Preferences");
62 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); 67 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions");
63 EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_)); 68 EXPECT_TRUE(file_util::CreateDirectory(extensions_dir_));
64 69
65 RecreateExtensionPrefs(); 70 RecreateExtensionPrefs();
66 } 71 }
67 72
68 TestExtensionPrefs::~TestExtensionPrefs() {} 73 TestExtensionPrefs::~TestExtensionPrefs() {
74 }
69 75
70 void TestExtensionPrefs::RecreateExtensionPrefs() { 76 void TestExtensionPrefs::RecreateExtensionPrefs() {
71 // We persist and reload the PrefService's PrefStores because this process 77 // We persist and reload the PrefService's PrefStores because this process
72 // deletes all empty dictionaries. The ExtensionPrefs implementation 78 // deletes all empty dictionaries. The ExtensionPrefs implementation
73 // needs to be able to handle this situation. 79 // needs to be able to handle this situation.
74 if (pref_service_.get()) { 80 if (pref_service_.get()) {
75 // The PrefService writes its persistent file on the file thread, so we 81 // Commit a pending write (which posts a task to task_runner_) and wait for
76 // need to wait for any pending I/O to complete before creating a new 82 // it to finish.
77 // PrefService. 83 pref_service_->CommitPendingWrite();
78 base::WaitableEvent io_finished(false, false); 84 base::RunLoop run_loop;
79 pref_service_-> CommitPendingWrite(); 85 ASSERT_TRUE(
80 EXPECT_TRUE(BrowserThread::PostTask( 86 task_runner_->PostTaskAndReply(
81 BrowserThread::FILE, 87 FROM_HERE,
82 FROM_HERE, 88 base::Bind(&DoNothing),
83 base::Bind(&base::WaitableEvent::Signal, 89 run_loop.QuitClosure()));
84 base::Unretained(&io_finished)))); 90 run_loop.Run();
85
86 // If the FILE thread is in fact the current thread (possible in testing
87 // scenarios), we have to ensure the task has a chance to run. If the FILE
88 // thread is a different thread, the test must ensure that thread is running
89 // (otherwise the Wait below will hang).
90 MessageLoop::current()->RunAllPending();
91
92 io_finished.Wait();
93 } 91 }
94 92
95 extension_pref_value_map_.reset(new ExtensionPrefValueMap); 93 extension_pref_value_map_.reset(new ExtensionPrefValueMap);
96 PrefServiceMockBuilder builder; 94 PrefServiceMockBuilder builder;
97 builder.WithUserFilePrefs(preferences_file_); 95 builder.WithUserFilePrefs(preferences_file_, task_runner_);
98 builder.WithExtensionPrefs( 96 builder.WithExtensionPrefs(
99 new ExtensionPrefStore(extension_pref_value_map_.get(), false)); 97 new ExtensionPrefStore(extension_pref_value_map_.get(), false));
100 pref_service_.reset(builder.Create()); 98 pref_service_.reset(builder.Create());
101 ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); 99 ExtensionPrefs::RegisterUserPrefs(pref_service_.get());
102 100
103 prefs_.reset(new MockExtensionPrefs(pref_service_.get(), 101 prefs_.reset(new MockExtensionPrefs(pref_service_.get(),
104 temp_dir_.path(), 102 temp_dir_.path(),
105 extension_pref_value_map_.get())); 103 extension_pref_value_map_.get()));
106 prefs_->Init(extensions_disabled_); 104 prefs_->Init(extensions_disabled_);
107 } 105 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const { 156 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const {
159 return pref_service_->CreateIncognitoPrefService( 157 return pref_service_->CreateIncognitoPrefService(
160 new ExtensionPrefStore(extension_pref_value_map_.get(), true)); 158 new ExtensionPrefStore(extension_pref_value_map_.get(), true));
161 } 159 }
162 160
163 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) { 161 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) {
164 extensions_disabled_ = extensions_disabled; 162 extensions_disabled_ = extensions_disabled;
165 } 163 }
166 164
167 } // namespace extensions 165 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698