1 // Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "include/cef_task.h"
6 #include "include/cef_values.h"
7 #include "tests/ceftests/test_handler.h"
8 #include "tests/ceftests/test_util.h"
9 #include "tests/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 // Dictionary test keys.
14 const char* kNullKey = "null_key";
15 const char* kBoolKey = "bool_key";
16 const char* kIntKey = "int_key";
17 const char* kDoubleKey = "double_key";
18 const char* kStringKey = "string_key";
19 const char* kBinaryKey = "binary_key";
20 const char* kDictionaryKey = "dict_key";
21 const char* kListKey = "list_key";
22
23 // List test indexes.
24 enum {
25 kNullIndex = 0,
26 kBoolIndex,
27 kIntIndex,
28 kDoubleIndex,
29 kStringIndex,
30 kBinaryIndex,
31 kDictionaryIndex,
32 kListIndex,
33 };
34
35 // Dictionary/list test values.
36 const bool kBoolValue = true;
37 const int kIntValue = 12;
38 const double kDoubleValue = 4.5432;
39 const char* kStringValue = "My string value";
40
41 // BINARY TEST HELPERS
42
43 // Test a binary value.
TestBinary(CefRefPtr<CefBinaryValue> value,char * data,size_t data_size)44 void TestBinary(CefRefPtr<CefBinaryValue> value, char* data, size_t data_size) {
45 // Testing requires strings longer than 15 characters.
46 EXPECT_GT(data_size, 15U);
47
48 EXPECT_EQ(data_size, value->GetSize());
49
50 char* buff = new char[data_size + 1];
51 char old_char;
52
53 // Test full read.
54 memset(buff, 0, data_size + 1);
55 EXPECT_EQ(data_size, value->GetData(buff, data_size, 0));
56 EXPECT_TRUE(!strcmp(buff, data));
57
58 // Test partial read with offset.
59 memset(buff, 0, data_size + 1);
60 old_char = data[15];
61 data[15] = 0;
62 EXPECT_EQ(10U, value->GetData(buff, 10, 5));
63 EXPECT_TRUE(!strcmp(buff, data + 5));
64 data[15] = old_char;
65
66 // Test that changes to the original data have no effect.
67 memset(buff, 0, data_size + 1);
68 old_char = data[0];
69 data[0] = '.';
70 EXPECT_EQ(1U, value->GetData(buff, 1, 0));
71 EXPECT_EQ(old_char, buff[0]);
72 data[0] = old_char;
73
74 // Test copy.
75 CefRefPtr<CefBinaryValue> copy = value->Copy();
76 TestBinaryEqual(copy, value);
77
78 delete[] buff;
79 }
80
81 // Used to test access of binary data on a different thread.
82 class BinaryTask : public CefTask {
83 public:
BinaryTask(CefRefPtr<CefBinaryValue> value,char * data,size_t data_size)84 BinaryTask(CefRefPtr<CefBinaryValue> value, char* data, size_t data_size)
85 : value_(value), data_(data), data_size_(data_size) {}
86
Execute()87 void Execute() override { TestBinary(value_, data_, data_size_); }
88
89 private:
90 CefRefPtr<CefBinaryValue> value_;
91 char* data_;
92 size_t data_size_;
93
94 IMPLEMENT_REFCOUNTING(BinaryTask);
95 };
96
97 // DICTIONARY TEST HELPERS
98
99 // Test dictionary null value.
TestDictionaryNull(CefRefPtr<CefDictionaryValue> value)100 void TestDictionaryNull(CefRefPtr<CefDictionaryValue> value) {
101 EXPECT_FALSE(value->HasKey(kNullKey));
102 EXPECT_TRUE(value->SetNull(kNullKey));
103 EXPECT_TRUE(value->HasKey(kNullKey));
104 EXPECT_EQ(VTYPE_NULL, value->GetType(kNullKey));
105 }
106
107 // Test dictionary bool value.
TestDictionaryBool(CefRefPtr<CefDictionaryValue> value)108 void TestDictionaryBool(CefRefPtr<CefDictionaryValue> value) {
109 EXPECT_FALSE(value->HasKey(kBoolKey));
110 EXPECT_TRUE(value->SetBool(kBoolKey, kBoolValue));
111 EXPECT_TRUE(value->HasKey(kBoolKey));
112 EXPECT_EQ(VTYPE_BOOL, value->GetType(kBoolKey));
113 EXPECT_EQ(kBoolValue, value->GetBool(kBoolKey));
114 }
115
116 // Test dictionary int value.
TestDictionaryInt(CefRefPtr<CefDictionaryValue> value)117 void TestDictionaryInt(CefRefPtr<CefDictionaryValue> value) {
118 EXPECT_FALSE(value->HasKey(kIntKey));
119 EXPECT_TRUE(value->SetInt(kIntKey, kIntValue));
120 EXPECT_TRUE(value->HasKey(kIntKey));
121 EXPECT_EQ(VTYPE_INT, value->GetType(kIntKey));
122 EXPECT_EQ(kIntValue, value->GetInt(kIntKey));
123 }
124
125 // Test dictionary double value.
TestDictionaryDouble(CefRefPtr<CefDictionaryValue> value)126 void TestDictionaryDouble(CefRefPtr<CefDictionaryValue> value) {
127 EXPECT_FALSE(value->HasKey(kDoubleKey));
128 EXPECT_TRUE(value->SetDouble(kDoubleKey, kDoubleValue));
129 EXPECT_TRUE(value->HasKey(kDoubleKey));
130 EXPECT_EQ(VTYPE_DOUBLE, value->GetType(kDoubleKey));
131 EXPECT_EQ(kDoubleValue, value->GetDouble(kDoubleKey));
132 }
133
134 // Test dictionary string value.
TestDictionaryString(CefRefPtr<CefDictionaryValue> value)135 void TestDictionaryString(CefRefPtr<CefDictionaryValue> value) {
136 EXPECT_FALSE(value->HasKey(kStringKey));
137 EXPECT_TRUE(value->SetString(kStringKey, kStringValue));
138 EXPECT_TRUE(value->HasKey(kStringKey));
139 EXPECT_EQ(VTYPE_STRING, value->GetType(kStringKey));
140 EXPECT_EQ(kStringValue, value->GetString(kStringKey).ToString());
141 }
142
143 // Test dictionary binary value.
TestDictionaryBinary(CefRefPtr<CefDictionaryValue> value,char * binary_data,size_t binary_data_size,CefRefPtr<CefBinaryValue> & binary_value)144 void TestDictionaryBinary(CefRefPtr<CefDictionaryValue> value,
145 char* binary_data,
146 size_t binary_data_size,
147 CefRefPtr<CefBinaryValue>& binary_value) {
148 binary_value = CefBinaryValue::Create(binary_data, binary_data_size);
149 EXPECT_TRUE(binary_value.get());
150 EXPECT_TRUE(binary_value->IsValid());
151 EXPECT_FALSE(binary_value->IsOwned());
152 EXPECT_FALSE(value->HasKey(kBinaryKey));
153 EXPECT_TRUE(value->SetBinary(kBinaryKey, binary_value));
154 EXPECT_FALSE(binary_value->IsValid()); // Value should be detached
155 EXPECT_TRUE(value->HasKey(kBinaryKey));
156 EXPECT_EQ(VTYPE_BINARY, value->GetType(kBinaryKey));
157 binary_value = value->GetBinary(kBinaryKey);
158 EXPECT_TRUE(binary_value.get());
159 EXPECT_TRUE(binary_value->IsValid());
160 EXPECT_TRUE(binary_value->IsOwned());
161 TestBinary(binary_value, binary_data, binary_data_size);
162 }
163
164 // Test dictionary dictionary value.
TestDictionaryDictionary(CefRefPtr<CefDictionaryValue> value,CefRefPtr<CefDictionaryValue> & dictionary_value)165 void TestDictionaryDictionary(CefRefPtr<CefDictionaryValue> value,
166 CefRefPtr<CefDictionaryValue>& dictionary_value) {
167 dictionary_value = CefDictionaryValue::Create();
168 EXPECT_TRUE(dictionary_value.get());
169 EXPECT_TRUE(dictionary_value->IsValid());
170 EXPECT_FALSE(dictionary_value->IsOwned());
171 EXPECT_FALSE(dictionary_value->IsReadOnly());
172 EXPECT_TRUE(dictionary_value->SetInt(kIntKey, kIntValue));
173 EXPECT_EQ(1U, dictionary_value->GetSize());
174 EXPECT_FALSE(value->HasKey(kDictionaryKey));
175 EXPECT_TRUE(value->SetDictionary(kDictionaryKey, dictionary_value));
176 EXPECT_FALSE(dictionary_value->IsValid()); // Value should be detached
177 EXPECT_TRUE(value->HasKey(kDictionaryKey));
178 EXPECT_EQ(VTYPE_DICTIONARY, value->GetType(kDictionaryKey));
179 dictionary_value = value->GetDictionary(kDictionaryKey);
180 EXPECT_TRUE(dictionary_value.get());
181 EXPECT_TRUE(dictionary_value->IsValid());
182 EXPECT_TRUE(dictionary_value->IsOwned());
183 EXPECT_FALSE(dictionary_value->IsReadOnly());
184 EXPECT_EQ(1U, dictionary_value->GetSize());
185 EXPECT_EQ(kIntValue, dictionary_value->GetInt(kIntKey));
186 }
187
188 // Test dictionary list value.
TestDictionaryList(CefRefPtr<CefDictionaryValue> value,CefRefPtr<CefListValue> & list_value)189 void TestDictionaryList(CefRefPtr<CefDictionaryValue> value,
190 CefRefPtr<CefListValue>& list_value) {
191 list_value = CefListValue::Create();
192 EXPECT_TRUE(list_value.get());
193 EXPECT_TRUE(list_value->IsValid());
194 EXPECT_FALSE(list_value->IsOwned());
195 EXPECT_FALSE(list_value->IsReadOnly());
196 EXPECT_TRUE(list_value->SetInt(0, kIntValue));
197 EXPECT_EQ(1U, list_value->GetSize());
198 EXPECT_FALSE(value->HasKey(kListKey));
199 EXPECT_TRUE(value->SetList(kListKey, list_value));
200 EXPECT_FALSE(list_value->IsValid()); // Value should be detached
201 EXPECT_TRUE(value->HasKey(kListKey));
202 EXPECT_EQ(VTYPE_LIST, value->GetType(kListKey));
203 list_value = value->GetList(kListKey);
204 EXPECT_TRUE(list_value.get());
205 EXPECT_TRUE(list_value->IsValid());
206 EXPECT_TRUE(list_value->IsOwned());
207 EXPECT_FALSE(list_value->IsReadOnly());
208 EXPECT_EQ(1U, list_value->GetSize());
209 EXPECT_EQ(kIntValue, list_value->GetInt(0));
210 }
211
212 // Test dictionary value.
TestDictionary(CefRefPtr<CefDictionaryValue> value,char * binary_data,size_t binary_data_size)213 void TestDictionary(CefRefPtr<CefDictionaryValue> value,
214 char* binary_data,
215 size_t binary_data_size) {
216 CefRefPtr<CefBinaryValue> binary_value;
217 CefRefPtr<CefDictionaryValue> dictionary_value;
218 CefRefPtr<CefListValue> list_value;
219
220 // Test the size.
221 EXPECT_EQ(0U, value->GetSize());
222
223 TestDictionaryNull(value);
224 TestDictionaryBool(value);
225 TestDictionaryInt(value);
226 TestDictionaryDouble(value);
227 TestDictionaryString(value);
228 TestDictionaryBinary(value, binary_data, binary_data_size, binary_value);
229 TestDictionaryDictionary(value, dictionary_value);
230 TestDictionaryList(value, list_value);
231
232 // Test the size.
233 EXPECT_EQ(8U, value->GetSize());
234
235 // Test copy.
236 CefRefPtr<CefDictionaryValue> copy = value->Copy(false);
237 TestDictionaryEqual(value, copy);
238
239 // Test removal.
240 EXPECT_TRUE(value->Remove(kNullKey));
241 EXPECT_FALSE(value->HasKey(kNullKey));
242
243 EXPECT_TRUE(value->Remove(kBoolKey));
244 EXPECT_FALSE(value->HasKey(kBoolKey));
245
246 EXPECT_TRUE(value->Remove(kIntKey));
247 EXPECT_FALSE(value->HasKey(kIntKey));
248
249 EXPECT_TRUE(value->Remove(kDoubleKey));
250 EXPECT_FALSE(value->HasKey(kDoubleKey));
251
252 EXPECT_TRUE(value->Remove(kStringKey));
253 EXPECT_FALSE(value->HasKey(kStringKey));
254
255 EXPECT_TRUE(value->Remove(kBinaryKey));
256 EXPECT_FALSE(value->HasKey(kBinaryKey));
257 EXPECT_FALSE(binary_value->IsValid()); // Value should be detached
258
259 EXPECT_TRUE(value->Remove(kDictionaryKey));
260 EXPECT_FALSE(value->HasKey(kDictionaryKey));
261 EXPECT_FALSE(dictionary_value->IsValid()); // Value should be detached
262
263 EXPECT_TRUE(value->Remove(kListKey));
264 EXPECT_FALSE(value->HasKey(kListKey));
265 EXPECT_FALSE(list_value->IsValid()); // Value should be detached
266
267 // Test the size.
268 EXPECT_EQ(0U, value->GetSize());
269
270 // Re-add some values.
271 TestDictionaryNull(value);
272 TestDictionaryBool(value);
273 TestDictionaryDictionary(value, dictionary_value);
274
275 // Test the size.
276 EXPECT_EQ(3U, value->GetSize());
277
278 // Clear the values.
279 EXPECT_TRUE(value->Clear());
280 EXPECT_EQ(0U, value->GetSize());
281 EXPECT_FALSE(dictionary_value->IsValid()); // Value should be detached
282 }
283
284 // Used to test access of dictionary data on a different thread.
285 class DictionaryTask : public CefTask {
286 public:
DictionaryTask(CefRefPtr<CefDictionaryValue> value,char * binary_data,size_t binary_data_size)287 DictionaryTask(CefRefPtr<CefDictionaryValue> value,
288 char* binary_data,
289 size_t binary_data_size)
290 : value_(value),
291 binary_data_(binary_data),
292 binary_data_size_(binary_data_size) {}
293
Execute()294 void Execute() override {
295 TestDictionary(value_, binary_data_, binary_data_size_);
296 }
297
298 private:
299 CefRefPtr<CefDictionaryValue> value_;
300 char* binary_data_;
301 size_t binary_data_size_;
302
303 IMPLEMENT_REFCOUNTING(DictionaryTask);
304 };
305
306 // LIST TEST HELPERS
307
308 // Test list null value.
TestListNull(CefRefPtr<CefListValue> value,size_t index)309 void TestListNull(CefRefPtr<CefListValue> value, size_t index) {
310 CefValueType type = value->GetType(index);
311 EXPECT_TRUE(type == VTYPE_INVALID || type == VTYPE_NULL);
312
313 EXPECT_TRUE(value->SetNull(index));
314 EXPECT_EQ(VTYPE_NULL, value->GetType(index));
315 }
316
317 // Test list bool value.
TestListBool(CefRefPtr<CefListValue> value,size_t index)318 void TestListBool(CefRefPtr<CefListValue> value, size_t index) {
319 CefValueType type = value->GetType(index);
320 EXPECT_TRUE(type == VTYPE_INVALID || type == VTYPE_NULL);
321
322 EXPECT_TRUE(value->SetBool(index, kBoolValue));
323 EXPECT_EQ(VTYPE_BOOL, value->GetType(index));
324 EXPECT_EQ(kBoolValue, value->GetBool(index));
325 }
326
327 // Test list int value.
TestListInt(CefRefPtr<CefListValue> value,size_t index)328 void TestListInt(CefRefPtr<CefListValue> value, size_t index) {
329 CefValueType type = value->GetType(index);
330 EXPECT_TRUE(type == VTYPE_INVALID || type == VTYPE_NULL);
331
332 EXPECT_TRUE(value->SetInt(index, kIntValue));
333 EXPECT_EQ(VTYPE_INT, value->GetType(index));
334 EXPECT_EQ(kIntValue, value->GetInt(index));
335 }
336
337 // Test list double value.
TestListDouble(CefRefPtr<CefListValue> value,size_t index)338 void TestListDouble(CefRefPtr<CefListValue> value, size_t index) {
339 CefValueType type = value->GetType(index);
340 EXPECT_TRUE(type == VTYPE_INVALID || type == VTYPE_NULL);
341
342 EXPECT_TRUE(value->SetDouble(index, kDoubleValue));
343 EXPECT_EQ(VTYPE_DOUBLE, value->GetType(index));
344 EXPECT_EQ(kDoubleValue, value->GetDouble(index));
345 }
346
347 // Test list string value.
TestListString(CefRefPtr<CefListValue> value,size_t index)348 void TestListString(CefRefPtr<CefListValue> value, size_t index) {
349 CefValueType type = value->GetType(index);
350 EXPECT_TRUE(type == VTYPE_INVALID || type == VTYPE_NULL);
351
352 EXPECT_TRUE(value->SetString(index, kStringValue));
353 EXPECT_EQ(VTYPE_STRING, value->GetType(index));
354 EXPECT_EQ(kStringValue, value->GetString(index).ToString());
355 }
356
357 // Test list binary value.
TestListBinary(CefRefPtr<CefListValue> value,size_t index,char * binary_data,size_t binary_data_size,CefRefPtr<CefBinaryValue> & binary_value)358 void TestListBinary(CefRefPtr<CefListValue> value,
359 size_t index,
360 char* binary_data,
361 size_t binary_data_size,
362 CefRefPtr<CefBinaryValue>& binary_value) {
363 binary_value = CefBinaryValue::Create(binary_data, binary_data_size);
364 EXPECT_TRUE(binary_value.get());
365 EXPECT_TRUE(binary_value->IsValid());
366 EXPECT_FALSE(binary_value->IsOwned());
367
368 CefValueType type = value->GetType(index);
369 EXPECT_TRUE(type == VTYPE_INVALID || type == VTYPE_NULL);
370
371 EXPECT_TRUE(value->SetBinary(index, binary_value));
372 EXPECT_FALSE(binary_value->IsValid()); // Value should be detached
373 EXPECT_EQ(VTYPE_BINARY, value->GetType(index));
374 binary_value = value->GetBinary(index);
375 EXPECT_TRUE(binary_value.get());
376 EXPECT_TRUE(binary_value->IsValid());
377 EXPECT_TRUE(binary_value->IsOwned());
378 TestBinary(binary_value, binary_data, binary_data_size);
379 }
380
381 // Test list dictionary value.
TestListDictionary(CefRefPtr<CefListValue> value,size_t index,CefRefPtr<CefDictionaryValue> & dictionary_value)382 void TestListDictionary(CefRefPtr<CefListValue> value,
383 size_t index,
384 CefRefPtr<CefDictionaryValue>& dictionary_value) {
385 dictionary_value = CefDictionaryValue::Create();
386 EXPECT_TRUE(dictionary_value.get());
387 EXPECT_TRUE(dictionary_value->IsValid());
388 EXPECT_FALSE(dictionary_value->IsOwned());
389 EXPECT_FALSE(dictionary_value->IsReadOnly());
390 EXPECT_TRUE(dictionary_value->SetInt(kIntKey, kIntValue));
391 EXPECT_EQ(1U, dictionary_value->GetSize());
392
393 CefValueType type = value->GetType(index);
394 EXPECT_TRUE(type == VTYPE_INVALID || type == VTYPE_NULL);
395
396 EXPECT_TRUE(value->SetDictionary(index, dictionary_value));
397 EXPECT_FALSE(dictionary_value->IsValid()); // Value should be detached
398 EXPECT_EQ(VTYPE_DICTIONARY, value->GetType(index));
399 dictionary_value = value->GetDictionary(index);
400 EXPECT_TRUE(dictionary_value.get());
401 EXPECT_TRUE(dictionary_value->IsValid());
402 EXPECT_TRUE(dictionary_value->IsOwned());
403 EXPECT_FALSE(dictionary_value->IsReadOnly());
404 EXPECT_EQ(1U, dictionary_value->GetSize());
405 EXPECT_EQ(kIntValue, dictionary_value->GetInt(kIntKey));
406 }
407
408 // Test list list value.
TestListList(CefRefPtr<CefListValue> value,size_t index,CefRefPtr<CefListValue> & list_value)409 void TestListList(CefRefPtr<CefListValue> value,
410 size_t index,
411 CefRefPtr<CefListValue>& list_value) {
412 list_value = CefListValue::Create();
413 EXPECT_TRUE(list_value.get());
414 EXPECT_TRUE(list_value->IsValid());
415 EXPECT_FALSE(list_value->IsOwned());
416 EXPECT_FALSE(list_value->IsReadOnly());
417 EXPECT_TRUE(list_value->SetInt(0, kIntValue));
418 EXPECT_EQ(1U, list_value->GetSize());
419
420 CefValueType type = value->GetType(index);
421 EXPECT_TRUE(type == VTYPE_INVALID || type == VTYPE_NULL);
422
423 EXPECT_TRUE(value->SetList(index, list_value));
424 EXPECT_FALSE(list_value->IsValid()); // Value should be detached
425 EXPECT_EQ(VTYPE_LIST, value->GetType(index));
426 list_value = value->GetList(index);
427 EXPECT_TRUE(list_value.get());
428 EXPECT_TRUE(list_value->IsValid());
429 EXPECT_TRUE(list_value->IsOwned());
430 EXPECT_FALSE(list_value->IsReadOnly());
431 EXPECT_EQ(1U, list_value->GetSize());
432 EXPECT_EQ(kIntValue, list_value->GetInt(0));
433 }
434
435 // Test list value.
TestList(CefRefPtr<CefListValue> value,char * binary_data,size_t binary_data_size)436 void TestList(CefRefPtr<CefListValue> value,
437 char* binary_data,
438 size_t binary_data_size) {
439 CefRefPtr<CefBinaryValue> binary_value;
440 CefRefPtr<CefDictionaryValue> dictionary_value;
441 CefRefPtr<CefListValue> list_value;
442
443 // Test the size.
444 EXPECT_EQ(0U, value->GetSize());
445
446 // Set the size.
447 EXPECT_TRUE(value->SetSize(8));
448 EXPECT_EQ(8U, value->GetSize());
449
450 EXPECT_EQ(VTYPE_NULL, value->GetType(kNullIndex));
451 TestListNull(value, kNullIndex);
452 EXPECT_EQ(VTYPE_NULL, value->GetType(kBoolIndex));
453 TestListBool(value, kBoolIndex);
454 EXPECT_EQ(VTYPE_NULL, value->GetType(kIntIndex));
455 TestListInt(value, kIntIndex);
456 EXPECT_EQ(VTYPE_NULL, value->GetType(kDoubleIndex));
457 TestListDouble(value, kDoubleIndex);
458 EXPECT_EQ(VTYPE_NULL, value->GetType(kStringIndex));
459 TestListString(value, kStringIndex);
460 EXPECT_EQ(VTYPE_NULL, value->GetType(kBinaryIndex));
461 TestListBinary(value, kBinaryIndex, binary_data, binary_data_size,
462 binary_value);
463 EXPECT_EQ(VTYPE_NULL, value->GetType(kDictionaryIndex));
464 TestListDictionary(value, kDictionaryIndex, dictionary_value);
465 EXPECT_EQ(VTYPE_NULL, value->GetType(kListIndex));
466 TestListList(value, kListIndex, list_value);
467
468 // Test the size.
469 EXPECT_EQ(8U, value->GetSize());
470
471 // Test various operations with invalid index.
472 EXPECT_FALSE(list_value->Remove(9U));
473 EXPECT_TRUE(list_value->GetType(10U) == VTYPE_INVALID);
474 EXPECT_FALSE(list_value->GetValue(11U).get() != nullptr &&
475 list_value->GetValue(11U)->IsValid());
476
477 // Test copy.
478 CefRefPtr<CefListValue> copy = value->Copy();
479 TestListEqual(value, copy);
480
481 // Test removal (in reverse order so indexes stay valid).
482 EXPECT_TRUE(value->Remove(kListIndex));
483 EXPECT_EQ(7U, value->GetSize());
484 EXPECT_FALSE(list_value->IsValid()); // Value should be detached
485
486 EXPECT_TRUE(value->Remove(kDictionaryIndex));
487 EXPECT_EQ(6U, value->GetSize());
488 EXPECT_FALSE(dictionary_value->IsValid()); // Value should be detached
489
490 EXPECT_TRUE(value->Remove(kBinaryIndex));
491 EXPECT_EQ(5U, value->GetSize());
492 EXPECT_FALSE(binary_value->IsValid()); // Value should be detached
493
494 EXPECT_TRUE(value->Remove(kStringIndex));
495 EXPECT_EQ(4U, value->GetSize());
496
497 EXPECT_TRUE(value->Remove(kDoubleIndex));
498 EXPECT_EQ(3U, value->GetSize());
499
500 EXPECT_TRUE(value->Remove(kIntIndex));
501 EXPECT_EQ(2U, value->GetSize());
502
503 EXPECT_TRUE(value->Remove(kBoolIndex));
504 EXPECT_EQ(1U, value->GetSize());
505
506 EXPECT_TRUE(value->Remove(kNullIndex));
507 EXPECT_EQ(0U, value->GetSize());
508
509 // Re-add some values.
510 EXPECT_EQ(VTYPE_INVALID, value->GetType(0));
511 TestListNull(value, 0);
512 EXPECT_EQ(VTYPE_INVALID, value->GetType(1));
513 TestListBool(value, 1);
514 EXPECT_EQ(VTYPE_INVALID, value->GetType(2));
515 TestListList(value, 2, list_value);
516
517 // Test the size.
518 EXPECT_EQ(3U, value->GetSize());
519
520 // Clear the values.
521 EXPECT_TRUE(value->Clear());
522 EXPECT_EQ(0U, value->GetSize());
523 EXPECT_FALSE(list_value->IsValid()); // Value should be detached
524
525 // Add some values in random order.
526 EXPECT_EQ(VTYPE_INVALID, value->GetType(2));
527 TestListInt(value, 2);
528 EXPECT_EQ(VTYPE_NULL, value->GetType(0));
529 TestListBool(value, 0);
530 EXPECT_EQ(VTYPE_NULL, value->GetType(1));
531 TestListList(value, 1, list_value);
532
533 EXPECT_EQ(VTYPE_BOOL, value->GetType(0));
534 EXPECT_EQ(VTYPE_LIST, value->GetType(1));
535 EXPECT_EQ(VTYPE_INT, value->GetType(2));
536
537 // Test the size.
538 EXPECT_EQ(3U, value->GetSize());
539
540 // Clear some values.
541 EXPECT_TRUE(value->SetSize(1));
542 EXPECT_EQ(1U, value->GetSize());
543 EXPECT_FALSE(list_value->IsValid()); // Value should be detached
544
545 EXPECT_EQ(VTYPE_BOOL, value->GetType(0));
546 EXPECT_EQ(VTYPE_INVALID, value->GetType(1));
547 EXPECT_EQ(VTYPE_INVALID, value->GetType(2));
548
549 // Clear all values.
550 EXPECT_TRUE(value->Clear());
551 EXPECT_EQ(0U, value->GetSize());
552 }
553
554 // Used to test access of list data on a different thread.
555 class ListTask : public CefTask {
556 public:
ListTask(CefRefPtr<CefListValue> value,char * binary_data,size_t binary_data_size)557 ListTask(CefRefPtr<CefListValue> value,
558 char* binary_data,
559 size_t binary_data_size)
560 : value_(value),
561 binary_data_(binary_data),
562 binary_data_size_(binary_data_size) {}
563
Execute()564 void Execute() override { TestList(value_, binary_data_, binary_data_size_); }
565
566 private:
567 CefRefPtr<CefListValue> value_;
568 char* binary_data_;
569 size_t binary_data_size_;
570
571 IMPLEMENT_REFCOUNTING(ListTask);
572 };
573
CreateAndCompareCopy(CefRefPtr<CefValue> value)574 void CreateAndCompareCopy(CefRefPtr<CefValue> value) {
575 CefRefPtr<CefValue> value2 = value->Copy();
576 EXPECT_TRUE(value->IsEqual(value));
577 EXPECT_TRUE(value->IsSame(value));
578 EXPECT_TRUE(value2->IsEqual(value2));
579 EXPECT_TRUE(value2->IsSame(value2));
580 EXPECT_TRUE(value->IsEqual(value2));
581 EXPECT_FALSE(value->IsSame(value2));
582 EXPECT_TRUE(value2->IsEqual(value));
583 EXPECT_FALSE(value2->IsSame(value));
584 }
585
CreateBinaryValue()586 CefRefPtr<CefBinaryValue> CreateBinaryValue() {
587 char binary_data[] = "This is my test data";
588 const size_t binary_data_size = sizeof(binary_data) - 1;
589
590 CefRefPtr<CefBinaryValue> binary_value =
591 CefBinaryValue::Create(binary_data, binary_data_size);
592 EXPECT_TRUE(binary_value.get());
593 EXPECT_TRUE(binary_value->IsValid());
594 EXPECT_FALSE(binary_value->IsOwned());
595 TestBinary(binary_value, binary_data, binary_data_size);
596 return binary_value;
597 }
598
CreateListValue()599 CefRefPtr<CefListValue> CreateListValue() {
600 CefRefPtr<CefListValue> list_value = CefListValue::Create();
601 EXPECT_TRUE(list_value.get());
602 EXPECT_TRUE(list_value->IsValid());
603 EXPECT_FALSE(list_value->IsOwned());
604 EXPECT_FALSE(list_value->IsReadOnly());
605 EXPECT_TRUE(list_value->SetInt(0, kIntValue));
606 EXPECT_TRUE(list_value->SetInt(1, kDoubleValue));
607 return list_value;
608 }
609
610 const char* kKey1 = "key1";
611 const char* kKey2 = "key2";
612
CreateDictionaryValue()613 CefRefPtr<CefDictionaryValue> CreateDictionaryValue() {
614 // Create the dictionary.
615 CefRefPtr<CefDictionaryValue> dict_value = CefDictionaryValue::Create();
616 EXPECT_TRUE(dict_value.get());
617 EXPECT_TRUE(dict_value->IsValid());
618 EXPECT_FALSE(dict_value->IsOwned());
619 EXPECT_FALSE(dict_value->IsReadOnly());
620 EXPECT_TRUE(dict_value->SetInt(kKey1, kIntValue));
621 EXPECT_TRUE(dict_value->SetInt(kKey2, kDoubleValue));
622 return dict_value;
623 }
624
625 } // namespace
626
627 // Test binary value access.
TEST(ValuesTest,BinaryAccess)628 TEST(ValuesTest, BinaryAccess) {
629 char data[] = "This is my test data";
630
631 CefRefPtr<CefBinaryValue> value =
632 CefBinaryValue::Create(data, sizeof(data) - 1);
633 EXPECT_TRUE(value.get());
634 EXPECT_TRUE(value->IsValid());
635 EXPECT_FALSE(value->IsOwned());
636
637 // Test on this thread.
638 TestBinary(value, data, sizeof(data) - 1);
639 }
640
641 // Test binary value access on a different thread.
TEST(ValuesTest,BinaryAccessOtherThread)642 TEST(ValuesTest, BinaryAccessOtherThread) {
643 char data[] = "This is my test data";
644
645 CefRefPtr<CefBinaryValue> value =
646 CefBinaryValue::Create(data, sizeof(data) - 1);
647 EXPECT_TRUE(value.get());
648 EXPECT_TRUE(value->IsValid());
649 EXPECT_FALSE(value->IsOwned());
650
651 // Test on a different thread.
652 CefPostTask(TID_UI, new BinaryTask(value, data, sizeof(data) - 1));
653 WaitForUIThread();
654 }
655
656 // Test dictionary value access.
TEST(ValuesTest,DictionaryAccess)657 TEST(ValuesTest, DictionaryAccess) {
658 CefRefPtr<CefDictionaryValue> value = CefDictionaryValue::Create();
659 EXPECT_TRUE(value.get());
660 EXPECT_TRUE(value->IsValid());
661 EXPECT_FALSE(value->IsOwned());
662 EXPECT_FALSE(value->IsReadOnly());
663
664 char binary_data[] = "This is my test data";
665
666 // Test on this thread.
667 TestDictionary(value, binary_data, sizeof(binary_data) - 1);
668 }
669
670 // Test dictionary value access on a different thread.
TEST(ValuesTest,DictionaryAccessOtherThread)671 TEST(ValuesTest, DictionaryAccessOtherThread) {
672 CefRefPtr<CefDictionaryValue> value = CefDictionaryValue::Create();
673 EXPECT_TRUE(value.get());
674 EXPECT_TRUE(value->IsValid());
675 EXPECT_FALSE(value->IsOwned());
676 EXPECT_FALSE(value->IsReadOnly());
677
678 char binary_data[] = "This is my test data";
679
680 // Test on a different thread.
681 CefPostTask(TID_UI,
682 new DictionaryTask(value, binary_data, sizeof(binary_data) - 1));
683 WaitForUIThread();
684 }
685
686 // Test dictionary value nested detachment
TEST(ValuesTest,DictionaryDetachment)687 TEST(ValuesTest, DictionaryDetachment) {
688 CefRefPtr<CefDictionaryValue> value = CefDictionaryValue::Create();
689 EXPECT_TRUE(value.get());
690 EXPECT_TRUE(value->IsValid());
691 EXPECT_FALSE(value->IsOwned());
692 EXPECT_FALSE(value->IsReadOnly());
693
694 CefRefPtr<CefDictionaryValue> dictionary_value = CefDictionaryValue::Create();
695 CefRefPtr<CefDictionaryValue> dictionary_value2 =
696 CefDictionaryValue::Create();
697 CefRefPtr<CefDictionaryValue> dictionary_value3 =
698 CefDictionaryValue::Create();
699
700 dictionary_value2->SetDictionary(kDictionaryKey, dictionary_value3);
701 EXPECT_FALSE(dictionary_value3->IsValid());
702 dictionary_value->SetDictionary(kDictionaryKey, dictionary_value2);
703 EXPECT_FALSE(dictionary_value2->IsValid());
704 value->SetDictionary(kDictionaryKey, dictionary_value);
705 EXPECT_FALSE(dictionary_value->IsValid());
706
707 dictionary_value = value->GetDictionary(kDictionaryKey);
708 EXPECT_TRUE(dictionary_value.get());
709 EXPECT_TRUE(dictionary_value->IsValid());
710
711 dictionary_value2 = dictionary_value->GetDictionary(kDictionaryKey);
712 EXPECT_TRUE(dictionary_value2.get());
713 EXPECT_TRUE(dictionary_value2->IsValid());
714
715 dictionary_value3 = dictionary_value2->GetDictionary(kDictionaryKey);
716 EXPECT_TRUE(dictionary_value3.get());
717 EXPECT_TRUE(dictionary_value3->IsValid());
718
719 EXPECT_TRUE(value->Remove(kDictionaryKey));
720 EXPECT_FALSE(dictionary_value->IsValid());
721 EXPECT_FALSE(dictionary_value2->IsValid());
722 EXPECT_FALSE(dictionary_value3->IsValid());
723 }
724
725 // Test list value access.
TEST(ValuesTest,ListAccess)726 TEST(ValuesTest, ListAccess) {
727 CefRefPtr<CefListValue> value = CefListValue::Create();
728 EXPECT_TRUE(value.get());
729 EXPECT_TRUE(value->IsValid());
730 EXPECT_FALSE(value->IsOwned());
731 EXPECT_FALSE(value->IsReadOnly());
732
733 char binary_data[] = "This is my test data";
734
735 // Test on this thread.
736 TestList(value, binary_data, sizeof(binary_data) - 1);
737 }
738
739 // Test list value access on a different thread.
TEST(ValuesTest,ListAccessOtherThread)740 TEST(ValuesTest, ListAccessOtherThread) {
741 CefRefPtr<CefListValue> value = CefListValue::Create();
742 EXPECT_TRUE(value.get());
743 EXPECT_TRUE(value->IsValid());
744 EXPECT_FALSE(value->IsOwned());
745 EXPECT_FALSE(value->IsReadOnly());
746
747 char binary_data[] = "This is my test data";
748
749 // Test on a different thread.
750 CefPostTask(TID_UI,
751 new ListTask(value, binary_data, sizeof(binary_data) - 1));
752 WaitForUIThread();
753 }
754
755 // Test list value nested detachment
TEST(ValuesTest,ListDetachment)756 TEST(ValuesTest, ListDetachment) {
757 CefRefPtr<CefListValue> value = CefListValue::Create();
758 EXPECT_TRUE(value.get());
759 EXPECT_TRUE(value->IsValid());
760 EXPECT_FALSE(value->IsOwned());
761 EXPECT_FALSE(value->IsReadOnly());
762
763 CefRefPtr<CefListValue> list_value = CefListValue::Create();
764 CefRefPtr<CefListValue> list_value2 = CefListValue::Create();
765 CefRefPtr<CefListValue> list_value3 = CefListValue::Create();
766
767 list_value2->SetList(0, list_value3);
768 EXPECT_FALSE(list_value3->IsValid());
769 list_value->SetList(0, list_value2);
770 EXPECT_FALSE(list_value2->IsValid());
771 value->SetList(0, list_value);
772 EXPECT_FALSE(list_value->IsValid());
773
774 list_value = value->GetList(0);
775 EXPECT_TRUE(list_value.get());
776 EXPECT_TRUE(list_value->IsValid());
777
778 list_value2 = list_value->GetList(0);
779 EXPECT_TRUE(list_value2.get());
780 EXPECT_TRUE(list_value2->IsValid());
781
782 list_value3 = list_value2->GetList(0);
783 EXPECT_TRUE(list_value3.get());
784 EXPECT_TRUE(list_value3->IsValid());
785
786 EXPECT_TRUE(value->Remove(0));
787 EXPECT_FALSE(list_value->IsValid());
788 EXPECT_FALSE(list_value2->IsValid());
789 EXPECT_FALSE(list_value3->IsValid());
790 }
791
792 // Test get/set of a CefValue simple types.
TEST(ValuesTest,ValueSimple)793 TEST(ValuesTest, ValueSimple) {
794 CefRefPtr<CefValue> value = CefValue::Create();
795 EXPECT_TRUE(value.get());
796 EXPECT_TRUE(value->IsValid());
797 EXPECT_FALSE(value->IsReadOnly());
798 EXPECT_FALSE(value->IsOwned());
799 EXPECT_EQ(VTYPE_NULL, value->GetType());
800 CreateAndCompareCopy(value);
801
802 EXPECT_TRUE(value->SetBool(true));
803 EXPECT_EQ(VTYPE_BOOL, value->GetType());
804 EXPECT_TRUE(value->GetBool());
805 EXPECT_TRUE(value->IsValid());
806 EXPECT_FALSE(value->IsReadOnly());
807 EXPECT_FALSE(value->IsOwned());
808 CreateAndCompareCopy(value);
809
810 EXPECT_TRUE(value->SetBool(false));
811 EXPECT_EQ(VTYPE_BOOL, value->GetType());
812 EXPECT_FALSE(value->GetBool());
813 EXPECT_TRUE(value->IsValid());
814 EXPECT_FALSE(value->IsReadOnly());
815 EXPECT_FALSE(value->IsOwned());
816 CreateAndCompareCopy(value);
817
818 EXPECT_TRUE(value->SetInt(3));
819 EXPECT_EQ(VTYPE_INT, value->GetType());
820 EXPECT_EQ(3, value->GetInt());
821 EXPECT_TRUE(value->IsValid());
822 EXPECT_FALSE(value->IsReadOnly());
823 EXPECT_FALSE(value->IsOwned());
824 CreateAndCompareCopy(value);
825
826 EXPECT_TRUE(value->SetDouble(5.665));
827 EXPECT_EQ(VTYPE_DOUBLE, value->GetType());
828 EXPECT_EQ(5.665, value->GetDouble());
829 EXPECT_TRUE(value->IsValid());
830 EXPECT_FALSE(value->IsReadOnly());
831 EXPECT_FALSE(value->IsOwned());
832 CreateAndCompareCopy(value);
833
834 const char* str = "Test string";
835 EXPECT_TRUE(value->SetString(str));
836 EXPECT_EQ(VTYPE_STRING, value->GetType());
837 EXPECT_STREQ(str, value->GetString().ToString().data());
838 EXPECT_TRUE(value->IsValid());
839 EXPECT_FALSE(value->IsReadOnly());
840 EXPECT_FALSE(value->IsOwned());
841 CreateAndCompareCopy(value);
842
843 EXPECT_TRUE(value->SetNull());
844 EXPECT_EQ(VTYPE_NULL, value->GetType());
845 EXPECT_TRUE(value->IsValid());
846 EXPECT_FALSE(value->IsReadOnly());
847 EXPECT_FALSE(value->IsOwned());
848 CreateAndCompareCopy(value);
849 }
850
851 // Test association of a CefValue simple type with a CefListValue.
TEST(ValuesTest,ValueSimpleToList)852 TEST(ValuesTest, ValueSimpleToList) {
853 const double double_value = 5.665;
854
855 CefRefPtr<CefValue> value = CefValue::Create();
856 EXPECT_TRUE(value->SetDouble(double_value));
857
858 // Add the value to the target list.
859 CefRefPtr<CefListValue> target_list = CefListValue::Create();
860 EXPECT_TRUE(target_list->SetValue(0, value));
861
862 // Test the value in the target list.
863 EXPECT_EQ(VTYPE_DOUBLE, target_list->GetType(0));
864 EXPECT_EQ(double_value, target_list->GetDouble(0));
865
866 // Get the value from the target list.
867 CefRefPtr<CefValue> value2 = target_list->GetValue(0);
868 EXPECT_TRUE(value2.get());
869 EXPECT_FALSE(value2->IsOwned());
870 EXPECT_FALSE(value2->IsReadOnly());
871 EXPECT_EQ(VTYPE_DOUBLE, value2->GetType());
872 EXPECT_EQ(double_value, value2->GetDouble());
873
874 // Values are equal but not the same.
875 EXPECT_TRUE(value->IsEqual(value2));
876 EXPECT_TRUE(value2->IsEqual(value));
877 EXPECT_FALSE(value->IsSame(value2));
878 EXPECT_FALSE(value2->IsSame(value));
879
880 // Change the value in the target list.
881 EXPECT_TRUE(target_list->SetInt(0, 5));
882 EXPECT_EQ(VTYPE_INT, target_list->GetType(0));
883 EXPECT_EQ(5, target_list->GetInt(0));
884
885 // The other values are still valid.
886 EXPECT_TRUE(value->IsValid());
887 EXPECT_TRUE(value2->IsValid());
888 }
889
890 // Test association of a CefValue simple type with a CefDictionaryValue.
TEST(ValuesTest,ValueSimpleToDictionary)891 TEST(ValuesTest, ValueSimpleToDictionary) {
892 const double double_value = 5.665;
893 const char* key = "key";
894
895 CefRefPtr<CefValue> value = CefValue::Create();
896 EXPECT_TRUE(value->SetDouble(double_value));
897
898 // Add the value to the target dictionary.
899 CefRefPtr<CefDictionaryValue> target_dict = CefDictionaryValue::Create();
900 EXPECT_TRUE(target_dict->SetValue(key, value));
901
902 // Test the value in the target dictionary.
903 EXPECT_EQ(VTYPE_DOUBLE, target_dict->GetType(key));
904 EXPECT_EQ(double_value, target_dict->GetDouble(key));
905
906 // Get the value from the target dictionary.
907 CefRefPtr<CefValue> value2 = target_dict->GetValue(key);
908 EXPECT_TRUE(value2.get());
909 EXPECT_FALSE(value2->IsOwned());
910 EXPECT_FALSE(value2->IsReadOnly());
911 EXPECT_EQ(VTYPE_DOUBLE, value2->GetType());
912 EXPECT_EQ(double_value, value2->GetDouble());
913
914 // Values are equal but not the same.
915 EXPECT_TRUE(value->IsEqual(value2));
916 EXPECT_TRUE(value2->IsEqual(value));
917 EXPECT_FALSE(value->IsSame(value2));
918 EXPECT_FALSE(value2->IsSame(value));
919
920 // Change the value in the target dictionary.
921 EXPECT_TRUE(target_dict->SetInt(key, 5));
922 EXPECT_EQ(VTYPE_INT, target_dict->GetType(key));
923 EXPECT_EQ(5, target_dict->GetInt(key));
924
925 // The other values are still valid.
926 EXPECT_TRUE(value->IsValid());
927 EXPECT_TRUE(value2->IsValid());
928 }
929
930 // Test get/set of a CefValue binary type.
TEST(ValuesTest,ValueBinary)931 TEST(ValuesTest, ValueBinary) {
932 // Create the binary.
933 CefRefPtr<CefBinaryValue> binary_value = CreateBinaryValue();
934
935 // Create the value.
936 CefRefPtr<CefValue> value = CefValue::Create();
937 EXPECT_TRUE(value->SetBinary(binary_value));
938 EXPECT_EQ(VTYPE_BINARY, value->GetType());
939 EXPECT_TRUE(value->IsValid());
940 EXPECT_TRUE(value->IsReadOnly()); // Binary values are always read-only.
941 EXPECT_FALSE(value->IsOwned());
942 CreateAndCompareCopy(value);
943
944 // Get the binary reference from the value.
945 CefRefPtr<CefBinaryValue> binary_value2 = value->GetBinary();
946 EXPECT_TRUE(binary_value2.get());
947 EXPECT_TRUE(binary_value2->IsValid());
948 EXPECT_FALSE(binary_value2->IsOwned());
949
950 // The binaries are the same and equal.
951 TestBinaryEqual(binary_value, binary_value2);
952 EXPECT_TRUE(binary_value->IsSame(binary_value2));
953 EXPECT_TRUE(binary_value2->IsSame(binary_value));
954 }
955
956 // Test association of a CefValue binary with a CefListValue.
TEST(ValuesTest,ValueBinaryToList)957 TEST(ValuesTest, ValueBinaryToList) {
958 // Create the binary.
959 CefRefPtr<CefBinaryValue> binary_value = CreateBinaryValue();
960
961 // Add the binary to a value.
962 CefRefPtr<CefValue> value = CefValue::Create();
963 EXPECT_TRUE(value->SetBinary(binary_value));
964
965 // Add the value to the target list.
966 CefRefPtr<CefListValue> target_list = CefListValue::Create();
967 EXPECT_TRUE(target_list->SetValue(0, value));
968
969 // The binary value is now owned by the target list.
970 EXPECT_FALSE(binary_value->IsValid());
971
972 // The value is still valid and points to the new reference list.
973 EXPECT_TRUE(value->IsValid());
974
975 CefRefPtr<CefBinaryValue> binary_value2 = value->GetBinary();
976 CefRefPtr<CefBinaryValue> binary_value3 = target_list->GetBinary(0);
977 CefRefPtr<CefValue> value2 = target_list->GetValue(0);
978 CefRefPtr<CefBinaryValue> binary_value4 = value2->GetBinary();
979
980 // All values are owned by the target list.
981 EXPECT_TRUE(value->IsOwned());
982 EXPECT_TRUE(value2->IsOwned());
983 EXPECT_TRUE(binary_value2->IsOwned());
984 EXPECT_TRUE(binary_value3->IsOwned());
985 EXPECT_TRUE(binary_value4->IsOwned());
986
987 // All values are the same.
988 EXPECT_TRUE(binary_value2->IsSame(binary_value3));
989 TestBinaryEqual(binary_value2, binary_value3);
990 EXPECT_TRUE(binary_value2->IsSame(binary_value4));
991 TestBinaryEqual(binary_value2, binary_value4);
992
993 // Change the value to something else.
994 EXPECT_TRUE(target_list->SetInt(0, kIntValue));
995 EXPECT_EQ(VTYPE_INT, target_list->GetType(0));
996 EXPECT_EQ(kIntValue, target_list->GetInt(0));
997
998 // Now the references are invalid.
999 EXPECT_FALSE(value->IsValid());
1000 EXPECT_FALSE(value2->IsValid());
1001 EXPECT_FALSE(binary_value2->IsValid());
1002 EXPECT_FALSE(binary_value3->IsValid());
1003 EXPECT_FALSE(binary_value4->IsValid());
1004
1005 // Verify that adding a binary to a list directly invalidates both the binary
1006 // and the value that references it.
1007 binary_value = CreateBinaryValue();
1008 value = CefValue::Create();
1009 EXPECT_TRUE(value->SetBinary(binary_value));
1010 target_list->SetBinary(0, binary_value);
1011 EXPECT_FALSE(binary_value->IsValid());
1012 EXPECT_FALSE(value->IsValid());
1013 }
1014
1015 // Test association of a CefValue binary with a CefDictionaryValue.
TEST(ValuesTest,ValueBinaryToDictionary)1016 TEST(ValuesTest, ValueBinaryToDictionary) {
1017 const char* key = "key";
1018
1019 // Create the binary.
1020 CefRefPtr<CefBinaryValue> binary_value = CreateBinaryValue();
1021
1022 // Add the binary to a value.
1023 CefRefPtr<CefValue> value = CefValue::Create();
1024 EXPECT_TRUE(value->SetBinary(binary_value));
1025
1026 // Add the value to the target dictionary.
1027 CefRefPtr<CefDictionaryValue> target_dict = CefDictionaryValue::Create();
1028 EXPECT_TRUE(target_dict->SetValue(key, value));
1029
1030 // The list value is now owned by the target dictionary.
1031 EXPECT_FALSE(binary_value->IsValid());
1032
1033 // The value is still valid and points to the new reference list.
1034 EXPECT_TRUE(value->IsValid());
1035
1036 CefRefPtr<CefBinaryValue> binary_value2 = value->GetBinary();
1037 CefRefPtr<CefBinaryValue> binary_value3 = target_dict->GetBinary(key);
1038 CefRefPtr<CefValue> value2 = target_dict->GetValue(key);
1039 CefRefPtr<CefBinaryValue> binary_value4 = value2->GetBinary();
1040
1041 // All values are owned by the target dictionary.
1042 EXPECT_TRUE(value->IsOwned());
1043 EXPECT_TRUE(value2->IsOwned());
1044 EXPECT_TRUE(binary_value2->IsOwned());
1045 EXPECT_TRUE(binary_value3->IsOwned());
1046 EXPECT_TRUE(binary_value4->IsOwned());
1047
1048 // All values are the same.
1049 EXPECT_TRUE(binary_value2->IsSame(binary_value3));
1050 TestBinaryEqual(binary_value2, binary_value3);
1051 EXPECT_TRUE(binary_value2->IsSame(binary_value4));
1052 TestBinaryEqual(binary_value2, binary_value4);
1053
1054 // Change the value to something else.
1055 EXPECT_TRUE(target_dict->SetInt(key, kIntValue));
1056 EXPECT_EQ(VTYPE_INT, target_dict->GetType(key));
1057 EXPECT_EQ(kIntValue, target_dict->GetInt(key));
1058
1059 // Now the references are invalid.
1060 EXPECT_FALSE(value->IsValid());
1061 EXPECT_FALSE(value2->IsValid());
1062 EXPECT_FALSE(binary_value2->IsValid());
1063 EXPECT_FALSE(binary_value3->IsValid());
1064 EXPECT_FALSE(binary_value4->IsValid());
1065
1066 // Verify that adding a binary to a dictionary directly invalidates both the
1067 // binary and the value that references it.
1068 binary_value = CreateBinaryValue();
1069 value = CefValue::Create();
1070 EXPECT_TRUE(value->SetBinary(binary_value));
1071 target_dict->SetBinary(key, binary_value);
1072 EXPECT_FALSE(binary_value->IsValid());
1073 EXPECT_FALSE(value->IsValid());
1074 }
1075
1076 // Test get/set of a CefValue list type.
TEST(ValuesTest,ValueList)1077 TEST(ValuesTest, ValueList) {
1078 // Create the list.
1079 CefRefPtr<CefListValue> list_value = CreateListValue();
1080
1081 // Create the value.
1082 CefRefPtr<CefValue> value = CefValue::Create();
1083 EXPECT_TRUE(value->SetList(list_value));
1084 EXPECT_EQ(VTYPE_LIST, value->GetType());
1085 EXPECT_TRUE(value->IsValid());
1086 EXPECT_FALSE(value->IsReadOnly());
1087 EXPECT_FALSE(value->IsOwned());
1088 CreateAndCompareCopy(value);
1089
1090 // Get the list reference from the value.
1091 CefRefPtr<CefListValue> list_value2 = value->GetList();
1092 EXPECT_TRUE(list_value2.get());
1093 EXPECT_TRUE(list_value2->IsValid());
1094 EXPECT_FALSE(list_value2->IsOwned());
1095 EXPECT_FALSE(list_value2->IsReadOnly());
1096
1097 // The lists are the same and equal.
1098 TestListEqual(list_value, list_value2);
1099 EXPECT_TRUE(list_value->IsSame(list_value2));
1100 EXPECT_TRUE(list_value2->IsSame(list_value));
1101
1102 // Change a value in one list and verify that it's changed in the other list.
1103 EXPECT_TRUE(list_value->SetString(0, kStringValue));
1104 EXPECT_EQ(VTYPE_STRING, list_value->GetType(0));
1105 EXPECT_STREQ(kStringValue, list_value->GetString(0).ToString().data());
1106 }
1107
1108 // Test association of a CefValue list with a CefListValue.
TEST(ValuesTest,ValueListToList)1109 TEST(ValuesTest, ValueListToList) {
1110 // Create the list.
1111 CefRefPtr<CefListValue> list_value = CreateListValue();
1112
1113 // Add the list to a value.
1114 CefRefPtr<CefValue> value = CefValue::Create();
1115 EXPECT_TRUE(value->SetList(list_value));
1116
1117 // Add the value to the target list.
1118 CefRefPtr<CefListValue> target_list = CefListValue::Create();
1119 EXPECT_TRUE(target_list->SetValue(0, value));
1120
1121 // The list value is now owned by the target list.
1122 EXPECT_FALSE(list_value->IsValid());
1123
1124 // The value is still valid and points to the new reference list.
1125 EXPECT_TRUE(value->IsValid());
1126
1127 CefRefPtr<CefListValue> list_value2 = value->GetList();
1128 CefRefPtr<CefListValue> list_value3 = target_list->GetList(0);
1129 CefRefPtr<CefValue> value2 = target_list->GetValue(0);
1130 CefRefPtr<CefListValue> list_value4 = value2->GetList();
1131
1132 // All values are owned by the target list.
1133 EXPECT_TRUE(value->IsOwned());
1134 EXPECT_TRUE(value2->IsOwned());
1135 EXPECT_TRUE(list_value2->IsOwned());
1136 EXPECT_TRUE(list_value3->IsOwned());
1137 EXPECT_TRUE(list_value4->IsOwned());
1138
1139 // All values are the same.
1140 EXPECT_TRUE(list_value2->IsSame(list_value3));
1141 TestListEqual(list_value2, list_value3);
1142 EXPECT_TRUE(list_value2->IsSame(list_value4));
1143 TestListEqual(list_value2, list_value4);
1144
1145 // Change the value to something else.
1146 EXPECT_TRUE(target_list->SetInt(0, kIntValue));
1147 EXPECT_EQ(VTYPE_INT, target_list->GetType(0));
1148 EXPECT_EQ(kIntValue, target_list->GetInt(0));
1149
1150 // Now the references are invalid.
1151 EXPECT_FALSE(value->IsValid());
1152 EXPECT_FALSE(value2->IsValid());
1153 EXPECT_FALSE(list_value2->IsValid());
1154 EXPECT_FALSE(list_value3->IsValid());
1155 EXPECT_FALSE(list_value4->IsValid());
1156
1157 // Verify that adding a list to a list directly invalidates both the list
1158 // and the value that references it.
1159 list_value = CreateListValue();
1160 value = CefValue::Create();
1161 EXPECT_TRUE(value->SetList(list_value));
1162 target_list->SetList(0, list_value);
1163 EXPECT_FALSE(list_value->IsValid());
1164 EXPECT_FALSE(value->IsValid());
1165 }
1166
1167 // Test association of a CefValue list with a CefDictionaryValue.
TEST(ValuesTest,ValueListToDictionary)1168 TEST(ValuesTest, ValueListToDictionary) {
1169 const char* key = "key";
1170
1171 // Create the list.
1172 CefRefPtr<CefListValue> list_value = CreateListValue();
1173
1174 // Add the list to a value.
1175 CefRefPtr<CefValue> value = CefValue::Create();
1176 EXPECT_TRUE(value->SetList(list_value));
1177
1178 // Add the value to the target dictionary.
1179 CefRefPtr<CefDictionaryValue> target_dict = CefDictionaryValue::Create();
1180 EXPECT_TRUE(target_dict->SetValue(key, value));
1181
1182 // The list value is now owned by the target dictionary.
1183 EXPECT_FALSE(list_value->IsValid());
1184
1185 // The value is still valid and points to the new reference list.
1186 EXPECT_TRUE(value->IsValid());
1187
1188 CefRefPtr<CefListValue> list_value2 = value->GetList();
1189 CefRefPtr<CefListValue> list_value3 = target_dict->GetList(key);
1190 CefRefPtr<CefValue> value2 = target_dict->GetValue(key);
1191 CefRefPtr<CefListValue> list_value4 = value2->GetList();
1192
1193 // All values are owned by the target dictionary.
1194 EXPECT_TRUE(value->IsOwned());
1195 EXPECT_TRUE(value2->IsOwned());
1196 EXPECT_TRUE(list_value2->IsOwned());
1197 EXPECT_TRUE(list_value3->IsOwned());
1198 EXPECT_TRUE(list_value4->IsOwned());
1199
1200 // All values are the same.
1201 EXPECT_TRUE(list_value2->IsSame(list_value3));
1202 TestListEqual(list_value2, list_value3);
1203 EXPECT_TRUE(list_value2->IsSame(list_value4));
1204 TestListEqual(list_value2, list_value4);
1205
1206 // Change the value to something else.
1207 EXPECT_TRUE(target_dict->SetInt(key, kIntValue));
1208 EXPECT_EQ(VTYPE_INT, target_dict->GetType(key));
1209 EXPECT_EQ(kIntValue, target_dict->GetInt(key));
1210
1211 // Now the references are invalid.
1212 EXPECT_FALSE(value->IsValid());
1213 EXPECT_FALSE(value2->IsValid());
1214 EXPECT_FALSE(list_value2->IsValid());
1215 EXPECT_FALSE(list_value3->IsValid());
1216 EXPECT_FALSE(list_value4->IsValid());
1217
1218 // Verify that adding a list to a dictionary directly invalidates both the
1219 // list and the value that references it.
1220 list_value = CreateListValue();
1221 value = CefValue::Create();
1222 EXPECT_TRUE(value->SetList(list_value));
1223 target_dict->SetList(key, list_value);
1224 EXPECT_FALSE(list_value->IsValid());
1225 EXPECT_FALSE(value->IsValid());
1226 }
1227
1228 // Test get/set of a CefValue dictionary type.
TEST(ValuesTest,ValueDictionary)1229 TEST(ValuesTest, ValueDictionary) {
1230 // Create the dictionary.
1231 CefRefPtr<CefDictionaryValue> dict_value = CreateDictionaryValue();
1232
1233 // Create the value.
1234 CefRefPtr<CefValue> value = CefValue::Create();
1235 EXPECT_TRUE(value->SetDictionary(dict_value));
1236 EXPECT_EQ(VTYPE_DICTIONARY, value->GetType());
1237 EXPECT_TRUE(value->IsValid());
1238 EXPECT_FALSE(value->IsReadOnly());
1239 EXPECT_FALSE(value->IsOwned());
1240 CreateAndCompareCopy(value);
1241
1242 // Get the dictionary reference from the value.
1243 CefRefPtr<CefDictionaryValue> dict_value2 = value->GetDictionary();
1244 EXPECT_TRUE(dict_value2.get());
1245 EXPECT_TRUE(dict_value2->IsValid());
1246 EXPECT_FALSE(dict_value2->IsOwned());
1247 EXPECT_FALSE(dict_value2->IsReadOnly());
1248
1249 // The dictionaries are the same and equal.
1250 TestDictionaryEqual(dict_value, dict_value2);
1251 EXPECT_TRUE(dict_value->IsSame(dict_value2));
1252 EXPECT_TRUE(dict_value2->IsSame(dict_value));
1253
1254 // Change a value in one dictionary and verify that it's changed in the other
1255 // dictionary.
1256 EXPECT_TRUE(dict_value->SetString(kKey1, kStringValue));
1257 EXPECT_EQ(VTYPE_STRING, dict_value->GetType(kKey1));
1258 EXPECT_STREQ(kStringValue, dict_value->GetString(kKey1).ToString().data());
1259 }
1260
1261 // Test association of a CefValue dictionary with a CefListValue.
TEST(ValuesTest,ValueDictionaryToList)1262 TEST(ValuesTest, ValueDictionaryToList) {
1263 // Create the dictionary.
1264 CefRefPtr<CefDictionaryValue> dict_value = CreateDictionaryValue();
1265
1266 // Add the list to a value.
1267 CefRefPtr<CefValue> value = CefValue::Create();
1268 EXPECT_TRUE(value->SetDictionary(dict_value));
1269
1270 // Add the value to the target list.
1271 CefRefPtr<CefListValue> target_list = CefListValue::Create();
1272 EXPECT_TRUE(target_list->SetValue(0, value));
1273
1274 // The list value is now owned by the target list.
1275 EXPECT_FALSE(dict_value->IsValid());
1276
1277 // The value is still valid and points to the new reference list.
1278 EXPECT_TRUE(value->IsValid());
1279
1280 CefRefPtr<CefDictionaryValue> dict_value2 = value->GetDictionary();
1281 CefRefPtr<CefDictionaryValue> dict_value3 = target_list->GetDictionary(0);
1282 CefRefPtr<CefValue> value2 = target_list->GetValue(0);
1283 CefRefPtr<CefDictionaryValue> dict_value4 = value2->GetDictionary();
1284
1285 // All values are owned by the target list.
1286 EXPECT_TRUE(value->IsOwned());
1287 EXPECT_TRUE(value2->IsOwned());
1288 EXPECT_TRUE(dict_value2->IsOwned());
1289 EXPECT_TRUE(dict_value3->IsOwned());
1290 EXPECT_TRUE(dict_value4->IsOwned());
1291
1292 // All values are the same.
1293 EXPECT_TRUE(dict_value2->IsSame(dict_value3));
1294 TestDictionaryEqual(dict_value2, dict_value3);
1295 EXPECT_TRUE(dict_value2->IsSame(dict_value4));
1296 TestDictionaryEqual(dict_value2, dict_value4);
1297
1298 // Change the value to something else.
1299 EXPECT_TRUE(target_list->SetInt(0, kIntValue));
1300 EXPECT_EQ(VTYPE_INT, target_list->GetType(0));
1301 EXPECT_EQ(kIntValue, target_list->GetInt(0));
1302
1303 // Now the references are invalid.
1304 EXPECT_FALSE(value->IsValid());
1305 EXPECT_FALSE(value2->IsValid());
1306 EXPECT_FALSE(dict_value2->IsValid());
1307 EXPECT_FALSE(dict_value3->IsValid());
1308 EXPECT_FALSE(dict_value4->IsValid());
1309
1310 // Verify that adding a dictionary to a list directly invalidates both the
1311 // dictionary and the value that references it.
1312 dict_value = CreateDictionaryValue();
1313 value = CefValue::Create();
1314 EXPECT_TRUE(value->SetDictionary(dict_value));
1315 target_list->SetDictionary(0, dict_value);
1316 EXPECT_FALSE(dict_value->IsValid());
1317 EXPECT_FALSE(value->IsValid());
1318 }
1319
1320 // Test association of a CefValue dictionary with a CefDictionaryValue.
TEST(ValuesTest,ValueDictionaryToDictionary)1321 TEST(ValuesTest, ValueDictionaryToDictionary) {
1322 const char* key = "key";
1323
1324 // Create the dictionary.
1325 CefRefPtr<CefDictionaryValue> dict_value = CreateDictionaryValue();
1326
1327 // Add the list to a value.
1328 CefRefPtr<CefValue> value = CefValue::Create();
1329 EXPECT_TRUE(value->SetDictionary(dict_value));
1330
1331 // Add the value to the target dictionary.
1332 CefRefPtr<CefDictionaryValue> target_dict = CefDictionaryValue::Create();
1333 EXPECT_TRUE(target_dict->SetValue(key, value));
1334
1335 // The list value is now owned by the target dictionary.
1336 EXPECT_FALSE(dict_value->IsValid());
1337
1338 // The value is still valid and points to the new reference list.
1339 EXPECT_TRUE(value->IsValid());
1340
1341 CefRefPtr<CefDictionaryValue> dict_value2 = value->GetDictionary();
1342 CefRefPtr<CefDictionaryValue> dict_value3 = target_dict->GetDictionary(key);
1343 CefRefPtr<CefValue> value2 = target_dict->GetValue(key);
1344 CefRefPtr<CefDictionaryValue> dict_value4 = value2->GetDictionary();
1345
1346 // All values are owned by the target dictionary.
1347 EXPECT_TRUE(value->IsOwned());
1348 EXPECT_TRUE(value2->IsOwned());
1349 EXPECT_TRUE(dict_value2->IsOwned());
1350 EXPECT_TRUE(dict_value3->IsOwned());
1351 EXPECT_TRUE(dict_value4->IsOwned());
1352
1353 // All values are the same.
1354 EXPECT_TRUE(dict_value2->IsSame(dict_value3));
1355 TestDictionaryEqual(dict_value2, dict_value3);
1356 EXPECT_TRUE(dict_value2->IsSame(dict_value4));
1357 TestDictionaryEqual(dict_value2, dict_value4);
1358
1359 // Change the value to something else.
1360 EXPECT_TRUE(target_dict->SetInt(key, kIntValue));
1361 EXPECT_EQ(VTYPE_INT, target_dict->GetType(key));
1362 EXPECT_EQ(kIntValue, target_dict->GetInt(key));
1363
1364 // Now the references are invalid.
1365 EXPECT_FALSE(value->IsValid());
1366 EXPECT_FALSE(value2->IsValid());
1367 EXPECT_FALSE(dict_value2->IsValid());
1368 EXPECT_FALSE(dict_value3->IsValid());
1369 EXPECT_FALSE(dict_value4->IsValid());
1370
1371 // Verify that adding a dictionary to a dictionary directly invalidates both
1372 // the dictionary and the value that references it.
1373 dict_value = CreateDictionaryValue();
1374 value = CefValue::Create();
1375 EXPECT_TRUE(value->SetDictionary(dict_value));
1376 target_dict->SetDictionary(key, dict_value);
1377 EXPECT_FALSE(dict_value->IsValid());
1378 EXPECT_FALSE(value->IsValid());
1379 }
1380