Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / chromecast / base / serializers_unittest.cc
blobd2d29ed6b0758752ee7f46551fce0bb377b718ae
1 // Copyright 2015 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/files/file_util.h"
6 #include "base/values.h"
7 #include "chromecast/base/serializers.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace chromecast {
11 namespace {
12 const char kEmptyJsonString[] = "{}";
13 const char kEmptyJsonFileString[] = "{\n\n}\n";
14 const char kProperJsonString[] =
15 "{\n"
16 " \"compound\": {\n"
17 " \"a\": 1,\n"
18 " \"b\": 2\n"
19 " },\n"
20 " \"some_String\": \"1337\",\n"
21 " \"some_int\": 42,\n"
22 " \"the_list\": [ \"val1\", \"val2\" ]\n"
23 "}\n";
24 const char kPoorlyFormedJsonString[] = "{\"key\":";
25 const char kTestKey[] = "test_key";
26 const char kTestValue[] = "test_value";
27 const char kTempfileName[] = "temp";
29 } // namespace
31 TEST(DeserializeFromJson, EmptyString) {
32 std::string str;
33 scoped_ptr<base::Value> value = DeserializeFromJson(str);
34 EXPECT_EQ(nullptr, value.get());
37 TEST(DeserializeFromJson, EmptyJsonObject) {
38 std::string str = kEmptyJsonString;
39 scoped_ptr<base::Value> value = DeserializeFromJson(str);
40 EXPECT_NE(nullptr, value.get());
43 TEST(DeserializeFromJson, ProperJsonObject) {
44 std::string str = kProperJsonString;
45 scoped_ptr<base::Value> value = DeserializeFromJson(str);
46 EXPECT_NE(nullptr, value.get());
49 TEST(DeserializeFromJson, PoorlyFormedJsonObject) {
50 std::string str = kPoorlyFormedJsonString;
51 scoped_ptr<base::Value> value = DeserializeFromJson(str);
52 EXPECT_EQ(nullptr, value.get());
55 TEST(SerializeToJson, BadValue) {
56 base::BinaryValue value(scoped_ptr<char[]>(new char[12]), 12);
57 scoped_ptr<std::string> str = SerializeToJson(value);
58 EXPECT_EQ(nullptr, str.get());
61 TEST(SerializeToJson, EmptyValue) {
62 base::DictionaryValue value;
63 scoped_ptr<std::string> str = SerializeToJson(value);
64 ASSERT_NE(nullptr, str.get());
65 EXPECT_EQ(kEmptyJsonString, *str);
68 TEST(SerializeToJson, PopulatedValue) {
69 base::DictionaryValue orig_value;
70 orig_value.SetString(kTestKey, kTestValue);
71 scoped_ptr<std::string> str = SerializeToJson(orig_value);
72 ASSERT_NE(nullptr, str.get());
74 scoped_ptr<base::Value> new_value = DeserializeFromJson(*str);
75 ASSERT_NE(nullptr, new_value.get());
76 EXPECT_TRUE(new_value->Equals(&orig_value));
79 class ScopedTempFile {
80 public:
81 ScopedTempFile() {
82 // Create a temporary file
83 base::CreateNewTempDirectory("", &dir_);
84 file_ = dir_.Append(kTempfileName);
87 ~ScopedTempFile() {
88 // Remove the temp directory.
89 base::DeleteFile(dir_, true);
92 const base::FilePath& file() const { return file_; }
93 const base::FilePath& dir() const { return dir_; }
95 std::size_t Write(const char* str) {
96 return static_cast<std::size_t>(base::WriteFile(file_, str, strlen(str)));
99 std::string Read() {
100 std::string result;
101 ReadFileToString(file_, &result);
102 return result;
105 private:
106 base::FilePath file_;
107 base::FilePath dir_;
109 DISALLOW_COPY_AND_ASSIGN(ScopedTempFile);
112 TEST(DeserializeJsonFromFile, NoFile) {
113 ScopedTempFile temp;
115 ASSERT_TRUE(base::IsDirectoryEmpty(temp.dir()));
116 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.file());
117 EXPECT_EQ(nullptr, value.get());
120 TEST(DeserializeJsonFromFile, EmptyString) {
121 ScopedTempFile temp;
122 EXPECT_EQ(strlen(""), temp.Write(""));
123 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.file());
124 EXPECT_EQ(nullptr, value.get());
127 TEST(DeserializeJsonFromFile, EmptyJsonObject) {
128 ScopedTempFile temp;
129 EXPECT_EQ(strlen(kEmptyJsonString), temp.Write(kEmptyJsonString));
130 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.file());
131 EXPECT_NE(nullptr, value.get());
134 TEST(DeserializeJsonFromFile, ProperJsonObject) {
135 ScopedTempFile temp;
136 EXPECT_EQ(strlen(kProperJsonString), temp.Write(kProperJsonString));
137 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.file());
138 EXPECT_NE(nullptr, value.get());
141 TEST(DeserializeJsonFromFile, PoorlyFormedJsonObject) {
142 ScopedTempFile temp;
143 EXPECT_EQ(strlen(kPoorlyFormedJsonString),
144 temp.Write(kPoorlyFormedJsonString));
145 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.file());
146 EXPECT_EQ(nullptr, value.get());
149 TEST(SerializeJsonToFile, BadValue) {
150 ScopedTempFile temp;
152 base::BinaryValue value(scoped_ptr<char[]>(new char[12]), 12);
153 ASSERT_FALSE(SerializeJsonToFile(temp.file(), value));
154 std::string str(temp.Read());
155 EXPECT_TRUE(str.empty());
158 TEST(SerializeJsonToFile, EmptyValue) {
159 ScopedTempFile temp;
161 base::DictionaryValue value;
162 ASSERT_TRUE(SerializeJsonToFile(temp.file(), value));
163 std::string str(temp.Read());
164 ASSERT_FALSE(str.empty());
165 EXPECT_EQ(kEmptyJsonFileString, str);
168 TEST(SerializeJsonToFile, PopulatedValue) {
169 ScopedTempFile temp;
171 base::DictionaryValue orig_value;
172 orig_value.SetString(kTestKey, kTestValue);
173 ASSERT_TRUE(SerializeJsonToFile(temp.file(), orig_value));
174 std::string str(temp.Read());
175 ASSERT_FALSE(str.empty());
177 scoped_ptr<base::Value> new_value = DeserializeJsonFromFile(temp.file());
178 ASSERT_NE(nullptr, new_value.get());
179 EXPECT_TRUE(new_value->Equals(&orig_value));
182 } // namespace chromecast