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