OLD | NEW |
1 // Copyright (c) 2011 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 "base/synchronization/waitable_event.h" |
12 #include "chrome/browser/extensions/extension_pref_store.h" | 13 #include "chrome/browser/extensions/extension_pref_store.h" |
13 #include "chrome/browser/extensions/extension_pref_value_map.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" |
| 22 #include "chrome/test/signaling_task.h" |
21 #include "content/browser/browser_thread.h" | 23 #include "content/browser/browser_thread.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
26 // Mock ExtensionPrefs class with artificial clock to guarantee that no two | 28 // Mock ExtensionPrefs class with artificial clock to guarantee that no two |
27 // extensions get the same installation time stamp and we can reliably | 29 // extensions get the same installation time stamp and we can reliably |
28 // assert the installation order in the tests below. | 30 // assert the installation order in the tests below. |
29 class MockExtensionPrefs : public ExtensionPrefs { | 31 class MockExtensionPrefs : public ExtensionPrefs { |
30 public: | 32 public: |
(...skipping 27 matching lines...) Expand all Loading... |
58 TestExtensionPrefs::~TestExtensionPrefs() {} | 60 TestExtensionPrefs::~TestExtensionPrefs() {} |
59 | 61 |
60 void TestExtensionPrefs::RecreateExtensionPrefs() { | 62 void TestExtensionPrefs::RecreateExtensionPrefs() { |
61 // We persist and reload the PrefService's PrefStores because this process | 63 // We persist and reload the PrefService's PrefStores because this process |
62 // deletes all empty dictionaries. The ExtensionPrefs implementation | 64 // deletes all empty dictionaries. The ExtensionPrefs implementation |
63 // needs to be able to handle this situation. | 65 // needs to be able to handle this situation. |
64 if (pref_service_.get()) { | 66 if (pref_service_.get()) { |
65 // The PrefService writes its persistent file on the file thread, so we | 67 // The PrefService writes its persistent file on the file thread, so we |
66 // need to wait for any pending I/O to complete before creating a new | 68 // need to wait for any pending I/O to complete before creating a new |
67 // PrefService. | 69 // PrefService. |
68 MessageLoop file_loop; | 70 base::WaitableEvent io_finished(false, false); |
69 BrowserThread file_thread(BrowserThread::FILE, &file_loop); | |
70 pref_service_->SavePersistentPrefs(); | 71 pref_service_->SavePersistentPrefs(); |
71 file_loop.RunAllPending(); | 72 EXPECT_TRUE(BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 73 new SignalingTask(&io_finished))); |
| 74 |
| 75 // If the FILE thread is in fact the current thread (possible in testing |
| 76 // scenarios), we have to ensure the task has a chance to run. If the FILE |
| 77 // thread is a different thread, the test must ensure that thread is running |
| 78 // (otherwise the Wait below will hang). |
| 79 MessageLoop::current()->RunAllPending(); |
| 80 |
| 81 EXPECT_TRUE(io_finished.Wait()); |
72 } | 82 } |
73 | 83 |
74 extension_pref_value_map_.reset(new ExtensionPrefValueMap); | 84 extension_pref_value_map_.reset(new ExtensionPrefValueMap); |
75 PrefServiceMockBuilder builder; | 85 PrefServiceMockBuilder builder; |
76 builder.WithUserFilePrefs(preferences_file_); | 86 builder.WithUserFilePrefs(preferences_file_); |
77 builder.WithExtensionPrefs( | 87 builder.WithExtensionPrefs( |
78 new ExtensionPrefStore(extension_pref_value_map_.get(), false)); | 88 new ExtensionPrefStore(extension_pref_value_map_.get(), false)); |
79 pref_service_.reset(builder.Create()); | 89 pref_service_.reset(builder.Create()); |
80 ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); | 90 ExtensionPrefs::RegisterUserPrefs(pref_service_.get()); |
81 | 91 |
(...skipping 30 matching lines...) Expand all Loading... |
112 | 122 |
113 std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) { | 123 std::string TestExtensionPrefs::AddExtensionAndReturnId(std::string name) { |
114 scoped_refptr<Extension> extension(AddExtension(name)); | 124 scoped_refptr<Extension> extension(AddExtension(name)); |
115 return extension->id(); | 125 return extension->id(); |
116 } | 126 } |
117 | 127 |
118 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const { | 128 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const { |
119 return pref_service_->CreateIncognitoPrefService( | 129 return pref_service_->CreateIncognitoPrefService( |
120 new ExtensionPrefStore(extension_pref_value_map_.get(), true)); | 130 new ExtensionPrefStore(extension_pref_value_map_.get(), true)); |
121 } | 131 } |
OLD | NEW |