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

Side by Side Diff: chrome/browser/extensions/settings/settings_frontend_unittest.cc

Issue 8497065: Extension Settings API: make it so that when leveldb storage areas fail to be (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 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) 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 "testing/gtest/include/gtest/gtest.h" 5 #include "testing/gtest/include/gtest/gtest.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/scoped_temp_dir.h" 11 #include "base/scoped_temp_dir.h"
12 #include "chrome/browser/extensions/settings/settings_frontend.h" 12 #include "chrome/browser/extensions/settings/settings_frontend.h"
13 #include "chrome/browser/extensions/settings/settings_storage.h" 13 #include "chrome/browser/extensions/settings/settings_storage.h"
14 #include "chrome/browser/extensions/settings/settings_test_util.h" 14 #include "chrome/browser/extensions/settings/settings_test_util.h"
15 #include "chrome/common/chrome_notification_types.h" 15 #include "chrome/common/chrome_notification_types.h"
16 #include "content/test/test_browser_thread.h" 16 #include "content/test/test_browser_thread.h"
17 17
18 namespace extensions { 18 namespace extensions {
19 19
20 using content::BrowserThread; 20 using content::BrowserThread;
21 21
22 using namespace settings_test_util; 22 using namespace settings_test_util;
23 23
24 class SettingsFrontendTest : public testing::Test { 24 namespace {
25
26 // SettingsStorageFactory which acts as a wrapper for other factories.
27 class ScopedSettingsStorageFactory : public SettingsStorageFactory {
25 public: 28 public:
26 SettingsFrontendTest() 29 explicit ScopedSettingsStorageFactory(SettingsStorageFactory* delegate)
30 : delegate_(delegate) {
31 DCHECK(delegate);
32 }
33
34 virtual ~ScopedSettingsStorageFactory() {}
35
36 void Reset(SettingsStorageFactory* delegate) {
37 DCHECK(delegate);
38 delegate_.reset(delegate);
39 }
40
41 virtual SettingsStorage* Create(
42 const FilePath& base_path, const std::string& extension_id) OVERRIDE {
43 return delegate_->Create(base_path, extension_id);
44 }
45
46 private:
47 scoped_ptr<SettingsStorageFactory> delegate_;
48 };
49
50 // A SettingsStorageFactory which always returns NULL.
51 class NullSettingsStorageFactory : public SettingsStorageFactory {
52 public:
53 virtual ~NullSettingsStorageFactory() {}
54
55 // SettingsStorageFactory implementation.
56 virtual SettingsStorage* Create(
57 const FilePath& base_path, const std::string& extension_id) OVERRIDE {
58 return NULL;
59 }
60 };
61
62 }
63
64 class ExtensionSettingsFrontendTest : public testing::Test {
65 public:
66 ExtensionSettingsFrontendTest()
27 : ui_thread_(BrowserThread::UI, MessageLoop::current()), 67 : ui_thread_(BrowserThread::UI, MessageLoop::current()),
28 file_thread_(BrowserThread::FILE, MessageLoop::current()) {} 68 file_thread_(BrowserThread::FILE, MessageLoop::current()) {}
29 69
30 virtual void SetUp() OVERRIDE { 70 virtual void SetUp() OVERRIDE {
31 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 71 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
32 profile_.reset(new MockProfile(temp_dir_.path())); 72 profile_.reset(new MockProfile(temp_dir_.path()));
33 frontend_.reset(new SettingsFrontend(profile_.get())); 73 ResetFrontend();
34 } 74 }
35 75
36 virtual void TearDown() OVERRIDE { 76 virtual void TearDown() OVERRIDE {
37 frontend_.reset(); 77 frontend_.reset();
38 profile_.reset(); 78 profile_.reset();
39 } 79 }
40 80
41 protected: 81 protected:
82 void ResetFrontend() {
83 storage_factory_ =
84 new ScopedSettingsStorageFactory(
85 new SettingsLeveldbStorage::Factory());
86 frontend_.reset(SettingsFrontend::Create(storage_factory_, profile_.get()));
87 }
88
42 ScopedTempDir temp_dir_; 89 ScopedTempDir temp_dir_;
43 scoped_ptr<MockProfile> profile_; 90 scoped_ptr<MockProfile> profile_;
44 scoped_ptr<SettingsFrontend> frontend_; 91 scoped_ptr<SettingsFrontend> frontend_;
45 92
93 // Owned by |frontend_|.
94 ScopedSettingsStorageFactory* storage_factory_;
95
46 private: 96 private:
47 MessageLoop message_loop_; 97 MessageLoop message_loop_;
48 content::TestBrowserThread ui_thread_; 98 content::TestBrowserThread ui_thread_;
49 content::TestBrowserThread file_thread_; 99 content::TestBrowserThread file_thread_;
50 }; 100 };
51 101
52 // Get a semblance of coverage for both extension and app settings by 102 // Get a semblance of coverage for both extension and app settings by
53 // alternating in each test. 103 // alternating in each test.
54 // TODO(kalman): explicitly test the two interact correctly. 104 // TODO(kalman): explicitly test the two interact correctly.
55 105
56 TEST_F(SettingsFrontendTest, SettingsPreservedAcrossReconstruction) { 106 TEST_F(ExtensionSettingsFrontendTest, SettingsPreservedAcrossReconstruction) {
57 const std::string id = "ext"; 107 const std::string id = "ext";
58 profile_->GetMockExtensionService()->AddExtension( 108 profile_->GetMockExtensionService()->AddExtension(
59 id, Extension::TYPE_EXTENSION); 109 id, Extension::TYPE_EXTENSION);
60 110
61 SettingsStorage* storage = GetStorage(id, frontend_.get()); 111 SettingsStorage* storage = GetStorage(id, frontend_.get());
62 112
63 // The correctness of Get/Set/Remove/Clear is tested elsewhere so no need to 113 // The correctness of Get/Set/Remove/Clear is tested elsewhere so no need to
64 // be too rigorous. 114 // be too rigorous.
65 { 115 {
66 StringValue bar("bar"); 116 StringValue bar("bar");
67 SettingsStorage::WriteResult result = storage->Set("foo", bar); 117 SettingsStorage::WriteResult result = storage->Set("foo", bar);
68 ASSERT_FALSE(result.HasError()); 118 ASSERT_FALSE(result.HasError());
69 } 119 }
70 120
71 { 121 {
72 SettingsStorage::ReadResult result = storage->Get(); 122 SettingsStorage::ReadResult result = storage->Get();
73 ASSERT_FALSE(result.HasError()); 123 ASSERT_FALSE(result.HasError());
74 EXPECT_FALSE(result.settings().empty()); 124 EXPECT_FALSE(result.settings().empty());
75 } 125 }
76 126
77 frontend_.reset(new SettingsFrontend(profile_.get())); 127 ResetFrontend();
78 storage = GetStorage(id, frontend_.get()); 128 storage = GetStorage(id, frontend_.get());
79 129
80 { 130 {
81 SettingsStorage::ReadResult result = storage->Get(); 131 SettingsStorage::ReadResult result = storage->Get();
82 ASSERT_FALSE(result.HasError()); 132 ASSERT_FALSE(result.HasError());
83 EXPECT_FALSE(result.settings().empty()); 133 EXPECT_FALSE(result.settings().empty());
84 } 134 }
85 } 135 }
86 136
87 TEST_F(SettingsFrontendTest, SettingsClearedOnUninstall) { 137 TEST_F(ExtensionSettingsFrontendTest, SettingsClearedOnUninstall) {
88 const std::string id = "ext"; 138 const std::string id = "ext";
89 profile_->GetMockExtensionService()->AddExtension( 139 profile_->GetMockExtensionService()->AddExtension(
90 id, Extension::TYPE_PACKAGED_APP); 140 id, Extension::TYPE_PACKAGED_APP);
91 141
92 SettingsStorage* storage = GetStorage(id, frontend_.get()); 142 SettingsStorage* storage = GetStorage(id, frontend_.get());
93 143
94 { 144 {
95 StringValue bar("bar"); 145 StringValue bar("bar");
96 SettingsStorage::WriteResult result = storage->Set("foo", bar); 146 SettingsStorage::WriteResult result = storage->Set("foo", bar);
97 ASSERT_FALSE(result.HasError()); 147 ASSERT_FALSE(result.HasError());
98 } 148 }
99 149
100 // This would be triggered by extension uninstall via an ExtensionDataDeleter. 150 // This would be triggered by extension uninstall via an ExtensionDataDeleter.
101 frontend_->DeleteStorageSoon(id); 151 frontend_->DeleteStorageSoon(id);
102 MessageLoop::current()->RunAllPending(); 152 MessageLoop::current()->RunAllPending();
103 153
104 // The storage area may no longer be valid post-uninstall, so re-request. 154 // The storage area may no longer be valid post-uninstall, so re-request.
105 storage = GetStorage(id, frontend_.get()); 155 storage = GetStorage(id, frontend_.get());
106 { 156 {
107 SettingsStorage::ReadResult result = storage->Get(); 157 SettingsStorage::ReadResult result = storage->Get();
108 ASSERT_FALSE(result.HasError()); 158 ASSERT_FALSE(result.HasError());
109 EXPECT_TRUE(result.settings().empty()); 159 EXPECT_TRUE(result.settings().empty());
110 } 160 }
111 } 161 }
112 162
113 TEST_F(SettingsFrontendTest, LeveldbDatabaseDeletedFromDiskOnClear) { 163 TEST_F(ExtensionSettingsFrontendTest, LeveldbDatabaseDeletedFromDiskOnClear) {
114 const std::string id = "ext"; 164 const std::string id = "ext";
115 profile_->GetMockExtensionService()->AddExtension( 165 profile_->GetMockExtensionService()->AddExtension(
116 id, Extension::TYPE_EXTENSION); 166 id, Extension::TYPE_EXTENSION);
117 167
118 SettingsStorage* storage = GetStorage(id, frontend_.get()); 168 SettingsStorage* storage = GetStorage(id, frontend_.get());
119 169
120 { 170 {
121 StringValue bar("bar"); 171 StringValue bar("bar");
122 SettingsStorage::WriteResult result = storage->Set("foo", bar); 172 SettingsStorage::WriteResult result = storage->Set("foo", bar);
123 ASSERT_FALSE(result.HasError()); 173 ASSERT_FALSE(result.HasError());
124 EXPECT_TRUE(file_util::PathExists(temp_dir_.path())); 174 EXPECT_TRUE(file_util::PathExists(temp_dir_.path()));
125 } 175 }
126 176
127 // Should need to both clear the database and delete the frontend for the 177 // Should need to both clear the database and delete the frontend for the
128 // leveldb database to be deleted from disk. 178 // leveldb database to be deleted from disk.
129 { 179 {
130 SettingsStorage::WriteResult result = storage->Clear(); 180 SettingsStorage::WriteResult result = storage->Clear();
131 ASSERT_FALSE(result.HasError()); 181 ASSERT_FALSE(result.HasError());
132 EXPECT_TRUE(file_util::PathExists(temp_dir_.path())); 182 EXPECT_TRUE(file_util::PathExists(temp_dir_.path()));
133 } 183 }
134 184
135 frontend_.reset(); 185 frontend_.reset();
136 MessageLoop::current()->RunAllPending(); 186 MessageLoop::current()->RunAllPending();
137 // TODO(kalman): Figure out why this fails, despite appearing to work. 187 // TODO(kalman): Figure out why this fails, despite appearing to work.
138 // Leaving this commented out rather than disabling the whole test so that the 188 // Leaving this commented out rather than disabling the whole test so that the
139 // deletion code paths are at least exercised. 189 // deletion code paths are at least exercised.
140 //EXPECT_FALSE(file_util::PathExists(temp_dir_.path())); 190 //EXPECT_FALSE(file_util::PathExists(temp_dir_.path()));
141 } 191 }
142 192
193 TEST_F(ExtensionSettingsFrontendTest,
194 LeveldbCreationFailureFailsAllOperations) {
195 const StringValue bar("bar");
196 const std::string id = "ext";
197 profile_->GetMockExtensionService()->AddExtension(
198 id, Extension::TYPE_EXTENSION);
199
200 storage_factory_->Reset(new NullSettingsStorageFactory());
201
202 SettingsStorage* storage = GetStorage(id, frontend_.get());
203 ASSERT_TRUE(storage != NULL);
204
205 EXPECT_TRUE(storage->Get().HasError());
206 EXPECT_TRUE(storage->Clear().HasError());
207 EXPECT_TRUE(storage->Set("foo", bar).HasError());
208 EXPECT_TRUE(storage->Remove("foo").HasError());
209
210 // For simplicity: just always fail those requests, even if the leveldb
211 // storage areas start working.
212 storage_factory_->Reset(new SettingsLeveldbStorage::Factory());
213
214 storage = GetStorage(id, frontend_.get());
215 ASSERT_TRUE(storage != NULL);
216
217 EXPECT_TRUE(storage->Get().HasError());
218 EXPECT_TRUE(storage->Clear().HasError());
219 EXPECT_TRUE(storage->Set("foo", bar).HasError());
220 EXPECT_TRUE(storage->Remove("foo").HasError());
221 }
222
143 } // namespace extensions 223 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/settings/settings_frontend.cc ('k') | chrome/browser/extensions/settings/settings_leveldb_storage.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698