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

Side by Side Diff: extensions/browser/api/storage/storage_frontend_unittest.cc

Issue 472343003: Use ApiUnitTest for storage API tests (storage_api_unittest and storage_frontend_unittest). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 3 months 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
« no previous file with comments | « extensions/browser/api/storage/storage_api_unittest.cc ('k') | extensions/extensions.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/test/test_browser_context.h"
11 #include "content/public/test/test_browser_thread.h" 13 #include "content/public/test/test_browser_thread.h"
14 #include "extensions/browser/api/extensions_api_client.h"
12 #include "extensions/browser/api/storage/leveldb_settings_storage_factory.h" 15 #include "extensions/browser/api/storage/leveldb_settings_storage_factory.h"
13 #include "extensions/browser/api/storage/settings_namespace.h" 16 #include "extensions/browser/api/storage/settings_namespace.h"
14 #include "extensions/browser/api/storage/settings_test_util.h" 17 #include "extensions/browser/api/storage/settings_test_util.h"
15 #include "extensions/browser/api/storage/storage_frontend.h" 18 #include "extensions/browser/api/storage/storage_frontend.h"
19 #include "extensions/browser/extensions_test.h"
16 #include "extensions/browser/value_store/value_store.h" 20 #include "extensions/browser/value_store/value_store.h"
17 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
18 22
19 using content::BrowserThread; 23 using content::BrowserThread;
20 24
21 namespace extensions { 25 namespace extensions {
22 26
23 namespace settings = settings_namespace; 27 namespace settings = settings_namespace;
24 namespace util = settings_test_util; 28 namespace util = settings_test_util;
25 29
26 namespace { 30 namespace {
27 31
28 // To save typing ValueStore::DEFAULTS everywhere. 32 // To save typing ValueStore::DEFAULTS everywhere.
29 const ValueStore::WriteOptions DEFAULTS = ValueStore::DEFAULTS; 33 const ValueStore::WriteOptions DEFAULTS = ValueStore::DEFAULTS;
30 34
31 // Creates a kilobyte of data.
32 scoped_ptr<base::Value> CreateKilobyte() {
33 std::string kilobyte_string;
34 for (int i = 0; i < 1024; ++i) {
35 kilobyte_string += "a";
36 }
37 return scoped_ptr<base::Value>(new base::StringValue(kilobyte_string));
38 }
39
40 // Creates a megabyte of data.
41 scoped_ptr<base::Value> CreateMegabyte() {
42 base::ListValue* megabyte = new base::ListValue();
43 for (int i = 0; i < 1000; ++i) {
44 megabyte->Append(CreateKilobyte().release());
45 }
46 return scoped_ptr<base::Value>(megabyte);
47 }
48
49 } // namespace 35 } // namespace
50 36
51 // A better name for this would be StorageFrontendTest, but the historical name 37 // A better name for this would be StorageFrontendTest, but the historical name
52 // has been ExtensionSettingsFrontendTest. In order to preserve crash/failure 38 // has been ExtensionSettingsFrontendTest. In order to preserve crash/failure
53 // history, the test names are unchanged. 39 // history, the test names are unchanged.
54 class ExtensionSettingsFrontendTest : public testing::Test { 40 class ExtensionSettingsFrontendTest : public ExtensionsTest {
55 public: 41 public:
56 ExtensionSettingsFrontendTest() 42 ExtensionSettingsFrontendTest()
57 : storage_factory_(new util::ScopedSettingsStorageFactory()), 43 : storage_factory_(new util::ScopedSettingsStorageFactory()),
58 ui_thread_(BrowserThread::UI, base::MessageLoop::current()), 44 ui_thread_(BrowserThread::UI, base::MessageLoop::current()),
59 file_thread_(BrowserThread::FILE, base::MessageLoop::current()) {} 45 file_thread_(BrowserThread::FILE, base::MessageLoop::current()) {}
60 46
61 virtual void SetUp() OVERRIDE { 47 virtual void SetUp() OVERRIDE {
48 ExtensionsTest::SetUp();
62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
63 profile_.reset(new util::MockProfile(temp_dir_.path()));
64 ResetFrontend(); 50 ResetFrontend();
65 } 51 }
66 52
67 virtual void TearDown() OVERRIDE { 53 virtual void TearDown() OVERRIDE {
68 frontend_.reset(); 54 frontend_.reset();
69 profile_.reset();
70 // Execute any pending deletion tasks. 55 // Execute any pending deletion tasks.
71 message_loop_.RunUntilIdle(); 56 message_loop_.RunUntilIdle();
57 ExtensionsTest::TearDown();
72 } 58 }
73 59
74 protected: 60 protected:
75 Profile* profile() { return profile_.get(); }
76
77 void ResetFrontend() { 61 void ResetFrontend() {
78 storage_factory_->Reset(new LeveldbSettingsStorageFactory()); 62 storage_factory_->Reset(new LeveldbSettingsStorageFactory());
79 frontend_.reset( 63 frontend_.reset(
80 StorageFrontend::CreateForTesting(storage_factory_, profile_.get())); 64 StorageFrontend::CreateForTesting(storage_factory_, browser_context()));
81 } 65 }
82 66
83 base::ScopedTempDir temp_dir_; 67 base::ScopedTempDir temp_dir_;
84 scoped_ptr<util::MockProfile> profile_;
85 scoped_ptr<StorageFrontend> frontend_; 68 scoped_ptr<StorageFrontend> frontend_;
86 scoped_refptr<util::ScopedSettingsStorageFactory> storage_factory_; 69 scoped_refptr<util::ScopedSettingsStorageFactory> storage_factory_;
87 70
88 private: 71 private:
89 base::MessageLoop message_loop_; 72 base::MessageLoop message_loop_;
90 content::TestBrowserThread ui_thread_; 73 content::TestBrowserThread ui_thread_;
91 content::TestBrowserThread file_thread_; 74 content::TestBrowserThread file_thread_;
75 ExtensionsAPIClient extensions_api_client_;
92 }; 76 };
93 77
94 // Get a semblance of coverage for both extension and app settings by 78 // Get a semblance of coverage for both extension and app settings by
95 // alternating in each test. 79 // alternating in each test.
96 // TODO(kalman): explicitly test the two interact correctly. 80 // TODO(kalman): explicitly test the two interact correctly.
97 81
98 // Tests that the frontend is set up correctly. 82 // Tests that the frontend is set up correctly.
99 TEST_F(ExtensionSettingsFrontendTest, Basics) { 83 TEST_F(ExtensionSettingsFrontendTest, Basics) {
100 // Local storage is always enabled. 84 // Local storage is always enabled.
101 EXPECT_TRUE(frontend_->IsStorageEnabled(settings::LOCAL)); 85 EXPECT_TRUE(frontend_->IsStorageEnabled(settings::LOCAL));
102 EXPECT_TRUE(frontend_->GetValueStoreCache(settings::LOCAL)); 86 EXPECT_TRUE(frontend_->GetValueStoreCache(settings::LOCAL));
103 87
104 // Invalid storage areas are not available. 88 // Invalid storage areas are not available.
105 EXPECT_FALSE(frontend_->IsStorageEnabled(settings::INVALID)); 89 EXPECT_FALSE(frontend_->IsStorageEnabled(settings::INVALID));
106 EXPECT_FALSE(frontend_->GetValueStoreCache(settings::INVALID)); 90 EXPECT_FALSE(frontend_->GetValueStoreCache(settings::INVALID));
107 } 91 }
108 92
109 TEST_F(ExtensionSettingsFrontendTest, SettingsPreservedAcrossReconstruction) { 93 TEST_F(ExtensionSettingsFrontendTest, SettingsPreservedAcrossReconstruction) {
110 const std::string id = "ext"; 94 const std::string id = "ext";
111 scoped_refptr<const Extension> extension = 95 scoped_refptr<const Extension> extension =
112 util::AddExtensionWithId(profile(), id, Manifest::TYPE_EXTENSION); 96 util::AddExtensionWithId(browser_context(), id, Manifest::TYPE_EXTENSION);
113 97
114 ValueStore* storage = util::GetStorage(extension, frontend_.get()); 98 ValueStore* storage =
99 util::GetStorage(extension, settings::LOCAL, frontend_.get());
115 100
116 // The correctness of Get/Set/Remove/Clear is tested elsewhere so no need to 101 // The correctness of Get/Set/Remove/Clear is tested elsewhere so no need to
117 // be too rigorous. 102 // be too rigorous.
118 { 103 {
119 base::StringValue bar("bar"); 104 base::StringValue bar("bar");
120 ValueStore::WriteResult result = storage->Set(DEFAULTS, "foo", bar); 105 ValueStore::WriteResult result = storage->Set(DEFAULTS, "foo", bar);
121 ASSERT_FALSE(result->HasError()); 106 ASSERT_FALSE(result->HasError());
122 } 107 }
123 108
124 { 109 {
125 ValueStore::ReadResult result = storage->Get(); 110 ValueStore::ReadResult result = storage->Get();
126 ASSERT_FALSE(result->HasError()); 111 ASSERT_FALSE(result->HasError());
127 EXPECT_FALSE(result->settings().empty()); 112 EXPECT_FALSE(result->settings().empty());
128 } 113 }
129 114
130 ResetFrontend(); 115 ResetFrontend();
131 storage = util::GetStorage(extension, frontend_.get()); 116 storage = util::GetStorage(extension, settings::LOCAL, frontend_.get());
132 117
133 { 118 {
134 ValueStore::ReadResult result = storage->Get(); 119 ValueStore::ReadResult result = storage->Get();
135 ASSERT_FALSE(result->HasError()); 120 ASSERT_FALSE(result->HasError());
136 EXPECT_FALSE(result->settings().empty()); 121 EXPECT_FALSE(result->settings().empty());
137 } 122 }
138 } 123 }
139 124
140 TEST_F(ExtensionSettingsFrontendTest, SettingsClearedOnUninstall) { 125 TEST_F(ExtensionSettingsFrontendTest, SettingsClearedOnUninstall) {
141 const std::string id = "ext"; 126 const std::string id = "ext";
142 scoped_refptr<const Extension> extension = util::AddExtensionWithId( 127 scoped_refptr<const Extension> extension = util::AddExtensionWithId(
143 profile(), id, Manifest::TYPE_LEGACY_PACKAGED_APP); 128 browser_context(), id, Manifest::TYPE_LEGACY_PACKAGED_APP);
144 129
145 ValueStore* storage = util::GetStorage(extension, frontend_.get()); 130 ValueStore* storage =
131 util::GetStorage(extension, settings::LOCAL, frontend_.get());
146 132
147 { 133 {
148 base::StringValue bar("bar"); 134 base::StringValue bar("bar");
149 ValueStore::WriteResult result = storage->Set(DEFAULTS, "foo", bar); 135 ValueStore::WriteResult result = storage->Set(DEFAULTS, "foo", bar);
150 ASSERT_FALSE(result->HasError()); 136 ASSERT_FALSE(result->HasError());
151 } 137 }
152 138
153 // This would be triggered by extension uninstall via a DataDeleter. 139 // This would be triggered by extension uninstall via a DataDeleter.
154 frontend_->DeleteStorageSoon(id); 140 frontend_->DeleteStorageSoon(id);
155 base::MessageLoop::current()->RunUntilIdle(); 141 base::MessageLoop::current()->RunUntilIdle();
156 142
157 // The storage area may no longer be valid post-uninstall, so re-request. 143 // The storage area may no longer be valid post-uninstall, so re-request.
158 storage = util::GetStorage(extension, frontend_.get()); 144 storage = util::GetStorage(extension, settings::LOCAL, frontend_.get());
159 { 145 {
160 ValueStore::ReadResult result = storage->Get(); 146 ValueStore::ReadResult result = storage->Get();
161 ASSERT_FALSE(result->HasError()); 147 ASSERT_FALSE(result->HasError());
162 EXPECT_TRUE(result->settings().empty()); 148 EXPECT_TRUE(result->settings().empty());
163 } 149 }
164 } 150 }
165 151
166 TEST_F(ExtensionSettingsFrontendTest, LeveldbDatabaseDeletedFromDiskOnClear) { 152 TEST_F(ExtensionSettingsFrontendTest, LeveldbDatabaseDeletedFromDiskOnClear) {
167 const std::string id = "ext"; 153 const std::string id = "ext";
168 scoped_refptr<const Extension> extension = 154 scoped_refptr<const Extension> extension =
169 util::AddExtensionWithId(profile(), id, Manifest::TYPE_EXTENSION); 155 util::AddExtensionWithId(browser_context(), id, Manifest::TYPE_EXTENSION);
170 156
171 ValueStore* storage = util::GetStorage(extension, frontend_.get()); 157 ValueStore* storage =
158 util::GetStorage(extension, settings::LOCAL, frontend_.get());
172 159
173 { 160 {
174 base::StringValue bar("bar"); 161 base::StringValue bar("bar");
175 ValueStore::WriteResult result = storage->Set(DEFAULTS, "foo", bar); 162 ValueStore::WriteResult result = storage->Set(DEFAULTS, "foo", bar);
176 ASSERT_FALSE(result->HasError()); 163 ASSERT_FALSE(result->HasError());
177 EXPECT_TRUE(base::PathExists(temp_dir_.path())); 164 EXPECT_TRUE(base::PathExists(temp_dir_.path()));
178 } 165 }
179 166
180 // Should need to both clear the database and delete the frontend for the 167 // Should need to both clear the database and delete the frontend for the
181 // leveldb database to be deleted from disk. 168 // leveldb database to be deleted from disk.
182 { 169 {
183 ValueStore::WriteResult result = storage->Clear(); 170 ValueStore::WriteResult result = storage->Clear();
184 ASSERT_FALSE(result->HasError()); 171 ASSERT_FALSE(result->HasError());
185 EXPECT_TRUE(base::PathExists(temp_dir_.path())); 172 EXPECT_TRUE(base::PathExists(temp_dir_.path()));
186 } 173 }
187 174
188 frontend_.reset(); 175 frontend_.reset();
189 base::MessageLoop::current()->RunUntilIdle(); 176 base::MessageLoop::current()->RunUntilIdle();
190 // TODO(kalman): Figure out why this fails, despite appearing to work. 177 // TODO(kalman): Figure out why this fails, despite appearing to work.
191 // Leaving this commented out rather than disabling the whole test so that the 178 // Leaving this commented out rather than disabling the whole test so that the
192 // deletion code paths are at least exercised. 179 // deletion code paths are at least exercised.
193 //EXPECT_FALSE(base::PathExists(temp_dir_.path())); 180 //EXPECT_FALSE(base::PathExists(temp_dir_.path()));
194 } 181 }
195 182
196 // Disabled (slow), http://crbug.com/322751 . 183 // Disabled (slow), http://crbug.com/322751 .
197 TEST_F(ExtensionSettingsFrontendTest, 184 TEST_F(ExtensionSettingsFrontendTest,
198 DISABLED_QuotaLimitsEnforcedCorrectlyForSyncAndLocal) { 185 DISABLED_QuotaLimitsEnforcedCorrectlyForSyncAndLocal) {
199 const std::string id = "ext"; 186 const std::string id = "ext";
200 scoped_refptr<const Extension> extension = 187 scoped_refptr<const Extension> extension =
201 util::AddExtensionWithId(profile(), id, Manifest::TYPE_EXTENSION); 188 util::AddExtensionWithId(browser_context(), id, Manifest::TYPE_EXTENSION);
202 189
203 ValueStore* sync_storage = 190 ValueStore* sync_storage =
204 util::GetStorage(extension, settings::SYNC, frontend_.get()); 191 util::GetStorage(extension, settings::SYNC, frontend_.get());
205 ValueStore* local_storage = 192 ValueStore* local_storage =
206 util::GetStorage(extension, settings::LOCAL, frontend_.get()); 193 util::GetStorage(extension, settings::LOCAL, frontend_.get());
207 194
208 // Sync storage should run out after ~100K. 195 // Sync storage should run out after ~100K.
209 scoped_ptr<base::Value> kilobyte = CreateKilobyte(); 196 scoped_ptr<base::Value> kilobyte = util::CreateKilobyte();
210 for (int i = 0; i < 100; ++i) { 197 for (int i = 0; i < 100; ++i) {
211 sync_storage->Set( 198 sync_storage->Set(DEFAULTS, base::StringPrintf("%d", i), *kilobyte);
212 ValueStore::DEFAULTS, base::StringPrintf("%d", i), *kilobyte);
213 } 199 }
214 200
215 EXPECT_TRUE(sync_storage->Set( 201 EXPECT_TRUE(sync_storage->Set(DEFAULTS, "WillError", *kilobyte)->HasError());
216 ValueStore::DEFAULTS, "WillError", *kilobyte)->HasError());
217 202
218 // Local storage shouldn't run out after ~100K. 203 // Local storage shouldn't run out after ~100K.
219 for (int i = 0; i < 100; ++i) { 204 for (int i = 0; i < 100; ++i) {
220 local_storage->Set( 205 local_storage->Set(DEFAULTS, base::StringPrintf("%d", i), *kilobyte);
221 ValueStore::DEFAULTS, base::StringPrintf("%d", i), *kilobyte);
222 } 206 }
223 207
224 EXPECT_FALSE(local_storage->Set( 208 EXPECT_FALSE(
225 ValueStore::DEFAULTS, "WontError", *kilobyte)->HasError()); 209 local_storage->Set(DEFAULTS, "WontError", *kilobyte)->HasError());
226 210
227 // Local storage should run out after ~5MB. 211 // Local storage should run out after ~5MB.
228 scoped_ptr<base::Value> megabyte = CreateMegabyte(); 212 scoped_ptr<base::Value> megabyte = util::CreateMegabyte();
229 for (int i = 0; i < 5; ++i) { 213 for (int i = 0; i < 5; ++i) {
230 local_storage->Set( 214 local_storage->Set(DEFAULTS, base::StringPrintf("%d", i), *megabyte);
231 ValueStore::DEFAULTS, base::StringPrintf("%d", i), *megabyte);
232 } 215 }
233 216
234 EXPECT_TRUE(local_storage->Set( 217 EXPECT_TRUE(local_storage->Set(DEFAULTS, "WillError", *megabyte)->HasError());
235 ValueStore::DEFAULTS, "WillError", *megabyte)->HasError());
236 }
237
238 // In other tests, we assume that the result of GetStorage is a pointer to the
239 // a Storage owned by a Frontend object, but for the unlimitedStorage case, this
240 // might not be true. So, write the tests in a "callback" style.
241 // We should really rewrite all tests to be asynchronous in this way.
242
243 static void UnlimitedSyncStorageTestCallback(ValueStore* sync_storage) {
244 // Sync storage should still run out after ~100K; the unlimitedStorage
245 // permission can't apply to sync.
246 scoped_ptr<base::Value> kilobyte = CreateKilobyte();
247 for (int i = 0; i < 100; ++i) {
248 sync_storage->Set(
249 ValueStore::DEFAULTS, base::StringPrintf("%d", i), *kilobyte);
250 }
251
252 EXPECT_TRUE(sync_storage->Set(
253 ValueStore::DEFAULTS, "WillError", *kilobyte)->HasError());
254 }
255
256 static void UnlimitedLocalStorageTestCallback(ValueStore* local_storage) {
257 // Local storage should never run out.
258 scoped_ptr<base::Value> megabyte = CreateMegabyte();
259 for (int i = 0; i < 7; ++i) {
260 local_storage->Set(
261 ValueStore::DEFAULTS, base::StringPrintf("%d", i), *megabyte);
262 }
263
264 EXPECT_FALSE(local_storage->Set(
265 ValueStore::DEFAULTS, "WontError", *megabyte)->HasError());
266 }
267
268 #if defined(OS_WIN)
269 // See: http://crbug.com/227296
270 #define MAYBE_UnlimitedStorageForLocalButNotSync \
271 DISABLED_UnlimitedStorageForLocalButNotSync
272 #else
273 #define MAYBE_UnlimitedStorageForLocalButNotSync \
274 UnlimitedStorageForLocalButNotSync
275 #endif
276
277 TEST_F(ExtensionSettingsFrontendTest,
278 MAYBE_UnlimitedStorageForLocalButNotSync) {
279 const std::string id = "ext";
280 std::set<std::string> permissions;
281 permissions.insert("unlimitedStorage");
282 scoped_refptr<const Extension> extension =
283 util::AddExtensionWithIdAndPermissions(
284 profile(), id, Manifest::TYPE_EXTENSION, permissions);
285
286 frontend_->RunWithStorage(
287 extension, settings::SYNC, base::Bind(&UnlimitedSyncStorageTestCallback));
288 frontend_->RunWithStorage(extension,
289 settings::LOCAL,
290 base::Bind(&UnlimitedLocalStorageTestCallback));
291
292 base::MessageLoop::current()->RunUntilIdle();
293 } 218 }
294 219
295 } // namespace extensions 220 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/storage/storage_api_unittest.cc ('k') | extensions/extensions.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698