OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <vector> |
| 6 |
5 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
6 #include "base/values.h" | 8 #include "base/values.h" |
7 #include "chromecast/base/scoped_temp_file.h" | 9 #include "chromecast/base/scoped_temp_file.h" |
8 #include "chromecast/base/serializers.h" | 10 #include "chromecast/base/serializers.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
10 | 12 |
11 namespace chromecast { | 13 namespace chromecast { |
12 namespace { | 14 namespace { |
13 const char kEmptyJsonString[] = "{}"; | 15 const char kEmptyJsonString[] = "{}"; |
14 const char kEmptyJsonFileString[] = "{\n\n}\n"; | 16 const char kEmptyJsonFileString[] = "{\n\n}\n"; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 EXPECT_NE(nullptr, value.get()); | 48 EXPECT_NE(nullptr, value.get()); |
47 } | 49 } |
48 | 50 |
49 TEST(DeserializeFromJson, PoorlyFormedJsonObject) { | 51 TEST(DeserializeFromJson, PoorlyFormedJsonObject) { |
50 std::string str = kPoorlyFormedJsonString; | 52 std::string str = kPoorlyFormedJsonString; |
51 std::unique_ptr<base::Value> value = DeserializeFromJson(str); | 53 std::unique_ptr<base::Value> value = DeserializeFromJson(str); |
52 EXPECT_EQ(nullptr, value.get()); | 54 EXPECT_EQ(nullptr, value.get()); |
53 } | 55 } |
54 | 56 |
55 TEST(SerializeToJson, BadValue) { | 57 TEST(SerializeToJson, BadValue) { |
56 base::BinaryValue value(std::unique_ptr<char[]>(new char[12]), 12); | 58 base::BinaryValue value(std::vector<char>(12)); |
57 std::unique_ptr<std::string> str = SerializeToJson(value); | 59 std::unique_ptr<std::string> str = SerializeToJson(value); |
58 EXPECT_EQ(nullptr, str.get()); | 60 EXPECT_EQ(nullptr, str.get()); |
59 } | 61 } |
60 | 62 |
61 TEST(SerializeToJson, EmptyValue) { | 63 TEST(SerializeToJson, EmptyValue) { |
62 base::DictionaryValue value; | 64 base::DictionaryValue value; |
63 std::unique_ptr<std::string> str = SerializeToJson(value); | 65 std::unique_ptr<std::string> str = SerializeToJson(value); |
64 ASSERT_NE(nullptr, str.get()); | 66 ASSERT_NE(nullptr, str.get()); |
65 EXPECT_EQ(kEmptyJsonString, *str); | 67 EXPECT_EQ(kEmptyJsonString, *str); |
66 } | 68 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 ScopedTempFile temp; | 111 ScopedTempFile temp; |
110 EXPECT_EQ(static_cast<int>(strlen(kPoorlyFormedJsonString)), | 112 EXPECT_EQ(static_cast<int>(strlen(kPoorlyFormedJsonString)), |
111 temp.Write(kPoorlyFormedJsonString)); | 113 temp.Write(kPoorlyFormedJsonString)); |
112 std::unique_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); | 114 std::unique_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); |
113 EXPECT_EQ(nullptr, value.get()); | 115 EXPECT_EQ(nullptr, value.get()); |
114 } | 116 } |
115 | 117 |
116 TEST(SerializeJsonToFile, BadValue) { | 118 TEST(SerializeJsonToFile, BadValue) { |
117 ScopedTempFile temp; | 119 ScopedTempFile temp; |
118 | 120 |
119 base::BinaryValue value(std::unique_ptr<char[]>(new char[12]), 12); | 121 base::BinaryValue value(std::vector<char>(12)); |
120 ASSERT_FALSE(SerializeJsonToFile(temp.path(), value)); | 122 ASSERT_FALSE(SerializeJsonToFile(temp.path(), value)); |
121 std::string str(temp.Read()); | 123 std::string str(temp.Read()); |
122 EXPECT_TRUE(str.empty()); | 124 EXPECT_TRUE(str.empty()); |
123 } | 125 } |
124 | 126 |
125 TEST(SerializeJsonToFile, EmptyValue) { | 127 TEST(SerializeJsonToFile, EmptyValue) { |
126 ScopedTempFile temp; | 128 ScopedTempFile temp; |
127 | 129 |
128 base::DictionaryValue value; | 130 base::DictionaryValue value; |
129 ASSERT_TRUE(SerializeJsonToFile(temp.path(), value)); | 131 ASSERT_TRUE(SerializeJsonToFile(temp.path(), value)); |
(...skipping 10 matching lines...) Expand all Loading... |
140 ASSERT_TRUE(SerializeJsonToFile(temp.path(), orig_value)); | 142 ASSERT_TRUE(SerializeJsonToFile(temp.path(), orig_value)); |
141 std::string str(temp.Read()); | 143 std::string str(temp.Read()); |
142 ASSERT_FALSE(str.empty()); | 144 ASSERT_FALSE(str.empty()); |
143 | 145 |
144 std::unique_ptr<base::Value> new_value = DeserializeJsonFromFile(temp.path()); | 146 std::unique_ptr<base::Value> new_value = DeserializeJsonFromFile(temp.path()); |
145 ASSERT_NE(nullptr, new_value.get()); | 147 ASSERT_NE(nullptr, new_value.get()); |
146 EXPECT_TRUE(new_value->Equals(&orig_value)); | 148 EXPECT_TRUE(new_value->Equals(&orig_value)); |
147 } | 149 } |
148 | 150 |
149 } // namespace chromecast | 151 } // namespace chromecast |
OLD | NEW |