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
{
12 const char kEmptyJsonString
[] = "{}";
13 const char kEmptyJsonFileString
[] = "{\n\n}\n";
14 const char kProperJsonString
[] =
20 " \"some_String\": \"1337\",\n"
21 " \"some_int\": 42,\n"
22 " \"the_list\": [ \"val1\", \"val2\" ]\n"
24 const char kPoorlyFormedJsonString
[] = "{\"key\":";
25 const char kTestKey
[] = "test_key";
26 const char kTestValue
[] = "test_value";
27 const char kTempfileName
[] = "temp";
31 TEST(DeserializeFromJson
, EmptyString
) {
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
{
82 // Create a temporary file
83 base::CreateNewTempDirectory("", &dir_
);
84 file_
= dir_
.Append(kTempfileName
);
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
)));
101 ReadFileToString(file_
, &result
);
106 base::FilePath file_
;
109 DISALLOW_COPY_AND_ASSIGN(ScopedTempFile
);
112 TEST(DeserializeJsonFromFile
, NoFile
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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