OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // TODO(kalman): More tests: |
| 6 // - Keys/values containing dots. |
| 7 // - More than one active settings object. |
| 8 // - Failure cases. |
| 9 // - Test with existing data (i.e. shut down leveldb storage, re-start). |
| 10 |
| 11 #include "chrome/browser/extensions/extension_settings_storage_unittest.h" |
| 12 |
| 13 #include "base/bind.h" |
| 14 #include "base/json/json_writer.h" |
| 15 #include "base/file_util.h" |
| 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/message_loop.h" |
| 18 #include "base/values.h" |
| 19 #include "chrome/browser/extensions/extension_settings.h" |
| 20 #include "content/browser/browser_thread.h" |
| 21 |
| 22 // Define macro to get the __LINE__ expansion. |
| 23 #define NEW_CALLBACK(expected) (new AssertEqualsCallback((expected), __LINE__)) |
| 24 |
| 25 namespace { |
| 26 |
| 27 // Callback from storage methods which performs the test assertions. |
| 28 class AssertEqualsCallback : public ExtensionSettingsStorage::Callback { |
| 29 public: |
| 30 AssertEqualsCallback(DictionaryValue* expected, int line) |
| 31 : expected_(expected), line_(line), called_(false) { |
| 32 } |
| 33 |
| 34 ~AssertEqualsCallback() { |
| 35 // Need to DCHECK since ASSERT_* can't be used from destructors. |
| 36 DCHECK(called_); |
| 37 } |
| 38 |
| 39 virtual void OnSuccess(DictionaryValue* actual) OVERRIDE { |
| 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 41 ASSERT_FALSE(called_) << "Callback has already been called"; |
| 42 called_ = true; |
| 43 ASSERT_TRUE(expected_->Equals(actual)) << "Values are different:\n" << |
| 44 "Line: " << line_ << "\n" << |
| 45 "Expected: " << GetJson(expected_) << |
| 46 "Got: " << GetJson(actual); |
| 47 delete actual; |
| 48 } |
| 49 |
| 50 virtual void OnFailure(const std::string& message) OVERRIDE { |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 52 ASSERT_FALSE(called_) << "Callback has already been called"; |
| 53 called_ = true; |
| 54 // No tests allow failure (yet). |
| 55 ASSERT_TRUE(false) << "Callback failed on line " << line_; |
| 56 } |
| 57 |
| 58 private: |
| 59 std::string GetJson(Value* value) { |
| 60 std::string json; |
| 61 base::JSONWriter::Write(value, true, &json); |
| 62 return json; |
| 63 } |
| 64 |
| 65 DictionaryValue* expected_; |
| 66 int line_; |
| 67 bool called_; |
| 68 }; |
| 69 |
| 70 } // namespace |
| 71 |
| 72 ExtensionSettingsStorageTest::ExtensionSettingsStorageTest() |
| 73 : key1_("foo"), key2_("bar"), key3_("baz") { |
| 74 val1_ = Value::CreateStringValue(key1_ + "Value"); |
| 75 val2_ = Value::CreateStringValue(key2_ + "Value"); |
| 76 val3_ = Value::CreateStringValue(key3_ + "Value"); |
| 77 |
| 78 emptyList_ = new ListValue(); |
| 79 |
| 80 list1_ = new ListValue(); |
| 81 list1_->Append(Value::CreateStringValue(key1_)); |
| 82 |
| 83 list2_ = new ListValue(); |
| 84 list2_->Append(Value::CreateStringValue(key2_)); |
| 85 |
| 86 list12_ = new ListValue(); |
| 87 list12_->Append(Value::CreateStringValue(key1_)); |
| 88 list12_->Append(Value::CreateStringValue(key2_)); |
| 89 |
| 90 list13_ = new ListValue(); |
| 91 list13_->Append(Value::CreateStringValue(key1_)); |
| 92 list13_->Append(Value::CreateStringValue(key3_)); |
| 93 |
| 94 list123_ = new ListValue(); |
| 95 list123_->Append(Value::CreateStringValue(key1_)); |
| 96 list123_->Append(Value::CreateStringValue(key2_)); |
| 97 list123_->Append(Value::CreateStringValue(key3_)); |
| 98 |
| 99 emptyDict_ = new DictionaryValue(); |
| 100 |
| 101 dict1_ = new DictionaryValue(); |
| 102 dict1_->Set(key1_, val1_->DeepCopy()); |
| 103 |
| 104 dict12_ = new DictionaryValue(); |
| 105 dict12_->Set(key1_, val1_->DeepCopy()); |
| 106 dict12_->Set(key2_, val2_->DeepCopy()); |
| 107 |
| 108 dict123_ = new DictionaryValue(); |
| 109 dict123_->Set(key1_, val1_->DeepCopy()); |
| 110 dict123_->Set(key2_, val2_->DeepCopy()); |
| 111 dict123_->Set(key3_, val3_->DeepCopy()); |
| 112 } |
| 113 |
| 114 ExtensionSettingsStorageTest::~ExtensionSettingsStorageTest() { |
| 115 delete val1_; |
| 116 delete val2_; |
| 117 delete val3_; |
| 118 delete emptyList_; |
| 119 delete list1_; |
| 120 delete list2_; |
| 121 delete list12_; |
| 122 delete list13_; |
| 123 delete list123_; |
| 124 delete emptyDict_; |
| 125 delete dict1_; |
| 126 delete dict12_; |
| 127 delete dict123_; |
| 128 } |
| 129 |
| 130 void ExtensionSettingsStorageTest::SetUp() { |
| 131 ui_message_loop_ = new MessageLoopForUI(); |
| 132 // Use the same message loop for the UI and FILE threads, giving a test |
| 133 // pattern where storage API calls get posted to the same message loop (the |
| 134 // current one), then all run with MessageLoop::current()->RunAllPending(). |
| 135 ui_thread_ = new BrowserThread(BrowserThread::UI, MessageLoop::current()); |
| 136 file_thread_ = new BrowserThread(BrowserThread::FILE, MessageLoop::current()); |
| 137 |
| 138 FilePath temp_dir; |
| 139 file_util::CreateNewTempDirectory(FilePath::StringType(), &temp_dir); |
| 140 settings_ = new ExtensionSettings(temp_dir); |
| 141 |
| 142 storage_ = NULL; |
| 143 (GetParam())( |
| 144 settings_, |
| 145 "fakeExtension", |
| 146 base::Bind( |
| 147 &ExtensionSettingsStorageTest::SetStorage, |
| 148 base::Unretained(this))); |
| 149 MessageLoop::current()->RunAllPending(); |
| 150 DCHECK(storage_ != NULL); |
| 151 } |
| 152 |
| 153 void ExtensionSettingsStorageTest::TearDown() { |
| 154 settings_ = NULL; |
| 155 delete file_thread_; |
| 156 delete ui_thread_; |
| 157 delete ui_message_loop_; |
| 158 } |
| 159 |
| 160 void ExtensionSettingsStorageTest::SetStorage( |
| 161 ExtensionSettingsStorage* storage) { |
| 162 storage_ = storage; |
| 163 } |
| 164 |
| 165 TEST_P(ExtensionSettingsStorageTest, GetWhenEmpty) { |
| 166 storage_->Get(key1_, NEW_CALLBACK(emptyDict_)); |
| 167 storage_->Get(key2_, NEW_CALLBACK(emptyDict_)); |
| 168 storage_->Get(key3_, NEW_CALLBACK(emptyDict_)); |
| 169 storage_->Get(*emptyList_, NEW_CALLBACK(emptyDict_)); |
| 170 storage_->Get(*list1_, NEW_CALLBACK(emptyDict_)); |
| 171 storage_->Get(*list123_, NEW_CALLBACK(emptyDict_)); |
| 172 storage_->Get(NEW_CALLBACK(emptyDict_)); |
| 173 MessageLoop::current()->RunAllPending(); |
| 174 } |
| 175 |
| 176 TEST_P(ExtensionSettingsStorageTest, GetWithSingleValue) { |
| 177 storage_->Set(key1_, *val1_, NEW_CALLBACK(dict1_)); |
| 178 MessageLoop::current()->RunAllPending(); |
| 179 |
| 180 storage_->Get(key1_, NEW_CALLBACK(dict1_)); |
| 181 storage_->Get(key2_, NEW_CALLBACK(emptyDict_)); |
| 182 storage_->Get(key3_, NEW_CALLBACK(emptyDict_)); |
| 183 storage_->Get(*emptyList_, NEW_CALLBACK(emptyDict_)); |
| 184 storage_->Get(*list1_, NEW_CALLBACK(dict1_)); |
| 185 storage_->Get(*list2_, NEW_CALLBACK(emptyDict_)); |
| 186 storage_->Get(*list123_, NEW_CALLBACK(dict1_)); |
| 187 storage_->Get(NEW_CALLBACK(dict1_)); |
| 188 MessageLoop::current()->RunAllPending(); |
| 189 } |
| 190 |
| 191 TEST_P(ExtensionSettingsStorageTest, GetWithMultipleValues) { |
| 192 storage_->Set(*dict12_, NEW_CALLBACK(dict12_)); |
| 193 MessageLoop::current()->RunAllPending(); |
| 194 |
| 195 storage_->Get(key1_, NEW_CALLBACK(dict1_)); |
| 196 storage_->Get(key3_, NEW_CALLBACK(emptyDict_)); |
| 197 storage_->Get(*emptyList_, NEW_CALLBACK(emptyDict_)); |
| 198 storage_->Get(*list1_, NEW_CALLBACK(dict1_)); |
| 199 storage_->Get(*list13_, NEW_CALLBACK(dict1_)); |
| 200 storage_->Get(*list12_, NEW_CALLBACK(dict12_)); |
| 201 storage_->Get(*list123_, NEW_CALLBACK(dict12_)); |
| 202 storage_->Get(NEW_CALLBACK(dict12_)); |
| 203 MessageLoop::current()->RunAllPending(); |
| 204 } |
| 205 |
| 206 TEST_P(ExtensionSettingsStorageTest, RemoveWhenEmpty) { |
| 207 storage_->Remove(key1_, NEW_CALLBACK(emptyDict_)); |
| 208 MessageLoop::current()->RunAllPending(); |
| 209 |
| 210 storage_->Get(key1_, NEW_CALLBACK(emptyDict_)); |
| 211 storage_->Get(*list1_, NEW_CALLBACK(emptyDict_)); |
| 212 storage_->Get(NEW_CALLBACK(emptyDict_)); |
| 213 MessageLoop::current()->RunAllPending(); |
| 214 } |
| 215 |
| 216 TEST_P(ExtensionSettingsStorageTest, RemoveWithSingleValue) { |
| 217 storage_->Set(key1_, *val1_, NEW_CALLBACK(dict1_)); |
| 218 MessageLoop::current()->RunAllPending(); |
| 219 storage_->Remove(key1_, NEW_CALLBACK(emptyDict_)); |
| 220 MessageLoop::current()->RunAllPending(); |
| 221 |
| 222 storage_->Get(key1_, NEW_CALLBACK(emptyDict_)); |
| 223 storage_->Get(key2_, NEW_CALLBACK(emptyDict_)); |
| 224 storage_->Get(*list1_, NEW_CALLBACK(emptyDict_)); |
| 225 storage_->Get(*list12_, NEW_CALLBACK(emptyDict_)); |
| 226 storage_->Get(NEW_CALLBACK(emptyDict_)); |
| 227 MessageLoop::current()->RunAllPending(); |
| 228 } |
| 229 |
| 230 TEST_P(ExtensionSettingsStorageTest, RemoveWithMultipleValues) { |
| 231 storage_->Set(*dict123_, NEW_CALLBACK(dict123_)); |
| 232 MessageLoop::current()->RunAllPending(); |
| 233 storage_->Remove(key3_, NEW_CALLBACK(emptyDict_)); |
| 234 MessageLoop::current()->RunAllPending(); |
| 235 |
| 236 storage_->Get(key1_, NEW_CALLBACK(dict1_)); |
| 237 storage_->Get(key3_, NEW_CALLBACK(emptyDict_)); |
| 238 storage_->Get(*emptyList_, NEW_CALLBACK(emptyDict_)); |
| 239 storage_->Get(*list1_, NEW_CALLBACK(dict1_)); |
| 240 storage_->Get(*list13_, NEW_CALLBACK(dict1_)); |
| 241 storage_->Get(*list12_, NEW_CALLBACK(dict12_)); |
| 242 storage_->Get(*list123_, NEW_CALLBACK(dict12_)); |
| 243 storage_->Get(NEW_CALLBACK(dict12_)); |
| 244 |
| 245 storage_->Remove(*list2_, NEW_CALLBACK(emptyDict_)); |
| 246 MessageLoop::current()->RunAllPending(); |
| 247 |
| 248 storage_->Get(key1_, NEW_CALLBACK(dict1_)); |
| 249 storage_->Get(key2_, NEW_CALLBACK(emptyDict_)); |
| 250 storage_->Get(key3_, NEW_CALLBACK(emptyDict_)); |
| 251 storage_->Get(*emptyList_, NEW_CALLBACK(emptyDict_)); |
| 252 storage_->Get(*list1_, NEW_CALLBACK(dict1_)); |
| 253 storage_->Get(*list2_, NEW_CALLBACK(emptyDict_)); |
| 254 storage_->Get(*list123_, NEW_CALLBACK(dict1_)); |
| 255 storage_->Get(NEW_CALLBACK(dict1_)); |
| 256 MessageLoop::current()->RunAllPending(); |
| 257 } |
| 258 |
| 259 TEST_P(ExtensionSettingsStorageTest, SetWhenOverwriting) { |
| 260 DictionaryValue key1Val2; |
| 261 key1Val2.Set(key1_, val2_->DeepCopy()); |
| 262 storage_->Set(key1_, *val2_, NEW_CALLBACK(&key1Val2)); |
| 263 MessageLoop::current()->RunAllPending(); |
| 264 |
| 265 storage_->Set(*dict12_, NEW_CALLBACK(dict12_)); |
| 266 MessageLoop::current()->RunAllPending(); |
| 267 |
| 268 storage_->Get(key1_, NEW_CALLBACK(dict1_)); |
| 269 storage_->Get(key3_, NEW_CALLBACK(emptyDict_)); |
| 270 storage_->Get(*emptyList_, NEW_CALLBACK(emptyDict_)); |
| 271 storage_->Get(*list1_, NEW_CALLBACK(dict1_)); |
| 272 storage_->Get(*list13_, NEW_CALLBACK(dict1_)); |
| 273 storage_->Get(*list12_, NEW_CALLBACK(dict12_)); |
| 274 storage_->Get(*list123_, NEW_CALLBACK(dict12_)); |
| 275 storage_->Get(NEW_CALLBACK(dict12_)); |
| 276 MessageLoop::current()->RunAllPending(); |
| 277 } |
| 278 |
| 279 TEST_P(ExtensionSettingsStorageTest, ClearWhenEmpty) { |
| 280 storage_->Clear(NEW_CALLBACK(emptyDict_)); |
| 281 MessageLoop::current()->RunAllPending(); |
| 282 |
| 283 storage_->Get(key1_, NEW_CALLBACK(emptyDict_)); |
| 284 storage_->Get(*list1_, NEW_CALLBACK(emptyDict_)); |
| 285 storage_->Get(NEW_CALLBACK(emptyDict_)); |
| 286 MessageLoop::current()->RunAllPending(); |
| 287 } |
| 288 |
| 289 TEST_P(ExtensionSettingsStorageTest, ClearWhenNotEmpty) { |
| 290 storage_->Set(*dict12_, NEW_CALLBACK(dict12_)); |
| 291 MessageLoop::current()->RunAllPending(); |
| 292 |
| 293 storage_->Clear(NEW_CALLBACK(emptyDict_)); |
| 294 MessageLoop::current()->RunAllPending(); |
| 295 |
| 296 storage_->Get(key1_, NEW_CALLBACK(emptyDict_)); |
| 297 storage_->Get(*list1_, NEW_CALLBACK(emptyDict_)); |
| 298 storage_->Get(NEW_CALLBACK(emptyDict_)); |
| 299 MessageLoop::current()->RunAllPending(); |
| 300 } |
OLD | NEW |