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