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
3 // found in the LICENSE file.
5 #include "base/json/json_reader.h"
6 #include "base/json/json_writer.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "extensions/browser/value_store/value_store_change.h"
10 #include "extensions/common/value_builder.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using base::DictionaryValue
;
15 using extensions::DictionaryBuilder
;
16 using extensions::ListBuilder
;
20 TEST(ValueStoreChangeTest
, NullOldValue
) {
21 ValueStoreChange
change("key", NULL
, new base::StringValue("value"));
23 EXPECT_EQ("key", change
.key());
24 EXPECT_EQ(NULL
, change
.old_value());
26 scoped_ptr
<base::Value
> expected(new base::StringValue("value"));
27 EXPECT_TRUE(change
.new_value()->Equals(expected
.get()));
31 TEST(ValueStoreChangeTest
, NullNewValue
) {
32 ValueStoreChange
change("key", new base::StringValue("value"), NULL
);
34 EXPECT_EQ("key", change
.key());
36 scoped_ptr
<base::Value
> expected(new base::StringValue("value"));
37 EXPECT_TRUE(change
.old_value()->Equals(expected
.get()));
39 EXPECT_EQ(NULL
, change
.new_value());
42 TEST(ValueStoreChangeTest
, NonNullValues
) {
43 ValueStoreChange
change("key",
44 new base::StringValue("old_value"),
45 new base::StringValue("new_value"));
47 EXPECT_EQ("key", change
.key());
49 scoped_ptr
<base::Value
> expected(new base::StringValue("old_value"));
50 EXPECT_TRUE(change
.old_value()->Equals(expected
.get()));
53 scoped_ptr
<base::Value
> expected(new base::StringValue("new_value"));
54 EXPECT_TRUE(change
.new_value()->Equals(expected
.get()));
58 TEST(ValueStoreChangeTest
, ToJson
) {
59 // Create a mildly complicated structure that has dots in it.
60 scoped_ptr
<base::DictionaryValue
> value
= DictionaryBuilder()
62 .Set("key.with.dots", "value.with.dots")
63 .Set("tricked", DictionaryBuilder()
64 .Set("you", "nodots"))
65 .Set("tricked.you", "with.dots")
68 ValueStoreChangeList change_list
;
69 change_list
.push_back(
70 ValueStoreChange("key", value
->DeepCopy(), value
->DeepCopy()));
71 change_list
.push_back(
72 ValueStoreChange("key.with.dots", value
->DeepCopy(), value
->DeepCopy()));
74 std::string json
= ValueStoreChange::ToJson(change_list
);
75 scoped_ptr
<base::Value
> from_json(base::JSONReader::Read(json
));
76 ASSERT_TRUE(from_json
.get());
78 DictionaryBuilder
v1(*value
);
79 DictionaryBuilder
v2(*value
);
80 DictionaryBuilder
v3(*value
);
81 DictionaryBuilder
v4(*value
);
82 scoped_ptr
<base::DictionaryValue
> expected_from_json
= DictionaryBuilder()
83 .Set("key", DictionaryBuilder()
86 .Set("key.with.dots", DictionaryBuilder()
91 EXPECT_TRUE(from_json
->Equals(expected_from_json
.get()));