1 // Copyright (c) 2012 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.
5 #include "testing/gtest/include/gtest/gtest.h"
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "base/logging.h"
10 #include "base/values.h"
11 #include "chrome/browser/value_store/value_store_change.h"
12 #include "extensions/common/value_builder.h"
14 using base::DictionaryValue
;
16 using extensions::DictionaryBuilder
;
17 using extensions::ListBuilder
;
21 TEST(ValueStoreChangeTest
, NullOldValue
) {
22 ValueStoreChange
change("key", NULL
, base::Value::CreateStringValue("value"));
24 EXPECT_EQ("key", change
.key());
25 EXPECT_EQ(NULL
, change
.old_value());
27 scoped_ptr
<base::Value
> expected(base::Value::CreateStringValue("value"));
28 EXPECT_TRUE(change
.new_value()->Equals(expected
.get()));
32 TEST(ValueStoreChangeTest
, NullNewValue
) {
33 ValueStoreChange
change("key", base::Value::CreateStringValue("value"), NULL
);
35 EXPECT_EQ("key", change
.key());
37 scoped_ptr
<base::Value
> expected(base::Value::CreateStringValue("value"));
38 EXPECT_TRUE(change
.old_value()->Equals(expected
.get()));
40 EXPECT_EQ(NULL
, change
.new_value());
43 TEST(ValueStoreChangeTest
, NonNullValues
) {
44 ValueStoreChange
change("key",
45 base::Value::CreateStringValue("old_value"),
46 base::Value::CreateStringValue("new_value"));
48 EXPECT_EQ("key", change
.key());
50 scoped_ptr
<base::Value
> expected(
51 base::Value::CreateStringValue("old_value"));
52 EXPECT_TRUE(change
.old_value()->Equals(expected
.get()));
55 scoped_ptr
<base::Value
> expected(
56 base::Value::CreateStringValue("new_value"));
57 EXPECT_TRUE(change
.new_value()->Equals(expected
.get()));
61 TEST(ValueStoreChangeTest
, ToJson
) {
62 // Create a mildly complicated structure that has dots in it.
63 scoped_ptr
<base::DictionaryValue
> value
= DictionaryBuilder()
65 .Set("key.with.dots", "value.with.dots")
66 .Set("tricked", DictionaryBuilder()
67 .Set("you", "nodots"))
68 .Set("tricked.you", "with.dots")
71 ValueStoreChangeList change_list
;
72 change_list
.push_back(
73 ValueStoreChange("key", value
->DeepCopy(), value
->DeepCopy()));
74 change_list
.push_back(
75 ValueStoreChange("key.with.dots", value
->DeepCopy(), value
->DeepCopy()));
77 std::string json
= ValueStoreChange::ToJson(change_list
);
78 scoped_ptr
<base::Value
> from_json(base::JSONReader::Read(json
));
79 ASSERT_TRUE(from_json
.get());
81 DictionaryBuilder
v1(*value
);
82 DictionaryBuilder
v2(*value
);
83 DictionaryBuilder
v3(*value
);
84 DictionaryBuilder
v4(*value
);
85 scoped_ptr
<base::DictionaryValue
> expected_from_json
= DictionaryBuilder()
86 .Set("key", DictionaryBuilder()
89 .Set("key.with.dots", DictionaryBuilder()
94 EXPECT_TRUE(from_json
->Equals(expected_from_json
.get()));