value_store_change_unittest.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
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"
12
13using base::DictionaryValue;
14using base::Value;
15using extensions::DictionaryBuilder;
16using extensions::ListBuilder;
17
18namespace {
19
20TEST(ValueStoreChangeTest, NullOldValue) {
21  ValueStoreChange change("key", NULL, base::Value::CreateStringValue("value"));
22
23  EXPECT_EQ("key", change.key());
24  EXPECT_EQ(NULL, change.old_value());
25  {
26    scoped_ptr<base::Value> expected(base::Value::CreateStringValue("value"));
27    EXPECT_TRUE(change.new_value()->Equals(expected.get()));
28  }
29}
30
31TEST(ValueStoreChangeTest, NullNewValue) {
32  ValueStoreChange change("key", base::Value::CreateStringValue("value"), NULL);
33
34  EXPECT_EQ("key", change.key());
35  {
36    scoped_ptr<base::Value> expected(base::Value::CreateStringValue("value"));
37    EXPECT_TRUE(change.old_value()->Equals(expected.get()));
38  }
39  EXPECT_EQ(NULL, change.new_value());
40}
41
42TEST(ValueStoreChangeTest, NonNullValues) {
43  ValueStoreChange change("key",
44                          base::Value::CreateStringValue("old_value"),
45                          base::Value::CreateStringValue("new_value"));
46
47  EXPECT_EQ("key", change.key());
48  {
49    scoped_ptr<base::Value> expected(
50        base::Value::CreateStringValue("old_value"));
51    EXPECT_TRUE(change.old_value()->Equals(expected.get()));
52  }
53  {
54    scoped_ptr<base::Value> expected(
55        base::Value::CreateStringValue("new_value"));
56    EXPECT_TRUE(change.new_value()->Equals(expected.get()));
57  }
58}
59
60TEST(ValueStoreChangeTest, ToJson) {
61  // Create a mildly complicated structure that has dots in it.
62  scoped_ptr<base::DictionaryValue> value = DictionaryBuilder()
63      .Set("key", "value")
64      .Set("key.with.dots", "value.with.dots")
65      .Set("tricked", DictionaryBuilder()
66          .Set("you", "nodots"))
67      .Set("tricked.you", "with.dots")
68      .Build();
69
70  ValueStoreChangeList change_list;
71  change_list.push_back(
72      ValueStoreChange("key", value->DeepCopy(), value->DeepCopy()));
73  change_list.push_back(
74      ValueStoreChange("key.with.dots", value->DeepCopy(), value->DeepCopy()));
75
76  std::string json = ValueStoreChange::ToJson(change_list);
77  scoped_ptr<base::Value> from_json(base::JSONReader::Read(json));
78  ASSERT_TRUE(from_json.get());
79
80  DictionaryBuilder v1(*value);
81  DictionaryBuilder v2(*value);
82  DictionaryBuilder v3(*value);
83  DictionaryBuilder v4(*value);
84  scoped_ptr<base::DictionaryValue> expected_from_json = DictionaryBuilder()
85      .Set("key", DictionaryBuilder()
86          .Set("oldValue", v1)
87          .Set("newValue", v2))
88      .Set("key.with.dots", DictionaryBuilder()
89          .Set("oldValue", v3)
90          .Set("newValue", v4))
91      .Build();
92
93  EXPECT_TRUE(from_json->Equals(expected_from_json.get()));
94}
95
96}  // namespace
97