| 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 "base/files/file_util.h" | 5 #include "base/files/file_util.h" |
| 6 #include "base/values.h" | 6 #include "base/values.h" |
| 7 #include "chromecast/base/scoped_temp_file.h" | 7 #include "chromecast/base/scoped_temp_file.h" |
| 8 #include "chromecast/base/serializers.h" | 8 #include "chromecast/base/serializers.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 " \"the_list\": [ \"val1\", \"val2\" ]\n" | 23 " \"the_list\": [ \"val1\", \"val2\" ]\n" |
| 24 "}\n"; | 24 "}\n"; |
| 25 const char kPoorlyFormedJsonString[] = "{\"key\":"; | 25 const char kPoorlyFormedJsonString[] = "{\"key\":"; |
| 26 const char kTestKey[] = "test_key"; | 26 const char kTestKey[] = "test_key"; |
| 27 const char kTestValue[] = "test_value"; | 27 const char kTestValue[] = "test_value"; |
| 28 | 28 |
| 29 } // namespace | 29 } // namespace |
| 30 | 30 |
| 31 TEST(DeserializeFromJson, EmptyString) { | 31 TEST(DeserializeFromJson, EmptyString) { |
| 32 std::string str; | 32 std::string str; |
| 33 scoped_ptr<base::Value> value = DeserializeFromJson(str); | 33 std::unique_ptr<base::Value> value = DeserializeFromJson(str); |
| 34 EXPECT_EQ(nullptr, value.get()); | 34 EXPECT_EQ(nullptr, value.get()); |
| 35 } | 35 } |
| 36 | 36 |
| 37 TEST(DeserializeFromJson, EmptyJsonObject) { | 37 TEST(DeserializeFromJson, EmptyJsonObject) { |
| 38 std::string str = kEmptyJsonString; | 38 std::string str = kEmptyJsonString; |
| 39 scoped_ptr<base::Value> value = DeserializeFromJson(str); | 39 std::unique_ptr<base::Value> value = DeserializeFromJson(str); |
| 40 EXPECT_NE(nullptr, value.get()); | 40 EXPECT_NE(nullptr, value.get()); |
| 41 } | 41 } |
| 42 | 42 |
| 43 TEST(DeserializeFromJson, ProperJsonObject) { | 43 TEST(DeserializeFromJson, ProperJsonObject) { |
| 44 std::string str = kProperJsonString; | 44 std::string str = kProperJsonString; |
| 45 scoped_ptr<base::Value> value = DeserializeFromJson(str); | 45 std::unique_ptr<base::Value> value = DeserializeFromJson(str); |
| 46 EXPECT_NE(nullptr, value.get()); | 46 EXPECT_NE(nullptr, value.get()); |
| 47 } | 47 } |
| 48 | 48 |
| 49 TEST(DeserializeFromJson, PoorlyFormedJsonObject) { | 49 TEST(DeserializeFromJson, PoorlyFormedJsonObject) { |
| 50 std::string str = kPoorlyFormedJsonString; | 50 std::string str = kPoorlyFormedJsonString; |
| 51 scoped_ptr<base::Value> value = DeserializeFromJson(str); | 51 std::unique_ptr<base::Value> value = DeserializeFromJson(str); |
| 52 EXPECT_EQ(nullptr, value.get()); | 52 EXPECT_EQ(nullptr, value.get()); |
| 53 } | 53 } |
| 54 | 54 |
| 55 TEST(SerializeToJson, BadValue) { | 55 TEST(SerializeToJson, BadValue) { |
| 56 base::BinaryValue value(scoped_ptr<char[]>(new char[12]), 12); | 56 base::BinaryValue value(std::unique_ptr<char[]>(new char[12]), 12); |
| 57 scoped_ptr<std::string> str = SerializeToJson(value); | 57 std::unique_ptr<std::string> str = SerializeToJson(value); |
| 58 EXPECT_EQ(nullptr, str.get()); | 58 EXPECT_EQ(nullptr, str.get()); |
| 59 } | 59 } |
| 60 | 60 |
| 61 TEST(SerializeToJson, EmptyValue) { | 61 TEST(SerializeToJson, EmptyValue) { |
| 62 base::DictionaryValue value; | 62 base::DictionaryValue value; |
| 63 scoped_ptr<std::string> str = SerializeToJson(value); | 63 std::unique_ptr<std::string> str = SerializeToJson(value); |
| 64 ASSERT_NE(nullptr, str.get()); | 64 ASSERT_NE(nullptr, str.get()); |
| 65 EXPECT_EQ(kEmptyJsonString, *str); | 65 EXPECT_EQ(kEmptyJsonString, *str); |
| 66 } | 66 } |
| 67 | 67 |
| 68 TEST(SerializeToJson, PopulatedValue) { | 68 TEST(SerializeToJson, PopulatedValue) { |
| 69 base::DictionaryValue orig_value; | 69 base::DictionaryValue orig_value; |
| 70 orig_value.SetString(kTestKey, kTestValue); | 70 orig_value.SetString(kTestKey, kTestValue); |
| 71 scoped_ptr<std::string> str = SerializeToJson(orig_value); | 71 std::unique_ptr<std::string> str = SerializeToJson(orig_value); |
| 72 ASSERT_NE(nullptr, str.get()); | 72 ASSERT_NE(nullptr, str.get()); |
| 73 | 73 |
| 74 scoped_ptr<base::Value> new_value = DeserializeFromJson(*str); | 74 std::unique_ptr<base::Value> new_value = DeserializeFromJson(*str); |
| 75 ASSERT_NE(nullptr, new_value.get()); | 75 ASSERT_NE(nullptr, new_value.get()); |
| 76 EXPECT_TRUE(new_value->Equals(&orig_value)); | 76 EXPECT_TRUE(new_value->Equals(&orig_value)); |
| 77 } | 77 } |
| 78 | 78 |
| 79 TEST(DeserializeJsonFromFile, NoFile) { | 79 TEST(DeserializeJsonFromFile, NoFile) { |
| 80 scoped_ptr<base::Value> value = | 80 std::unique_ptr<base::Value> value = |
| 81 DeserializeJsonFromFile(base::FilePath("/file/does/not/exist.json")); | 81 DeserializeJsonFromFile(base::FilePath("/file/does/not/exist.json")); |
| 82 EXPECT_EQ(nullptr, value.get()); | 82 EXPECT_EQ(nullptr, value.get()); |
| 83 } | 83 } |
| 84 | 84 |
| 85 TEST(DeserializeJsonFromFile, EmptyString) { | 85 TEST(DeserializeJsonFromFile, EmptyString) { |
| 86 ScopedTempFile temp; | 86 ScopedTempFile temp; |
| 87 EXPECT_EQ(static_cast<int>(strlen("")), temp.Write("")); | 87 EXPECT_EQ(static_cast<int>(strlen("")), temp.Write("")); |
| 88 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); | 88 std::unique_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); |
| 89 EXPECT_EQ(nullptr, value.get()); | 89 EXPECT_EQ(nullptr, value.get()); |
| 90 } | 90 } |
| 91 | 91 |
| 92 TEST(DeserializeJsonFromFile, EmptyJsonObject) { | 92 TEST(DeserializeJsonFromFile, EmptyJsonObject) { |
| 93 ScopedTempFile temp; | 93 ScopedTempFile temp; |
| 94 EXPECT_EQ(static_cast<int>(strlen(kEmptyJsonString)), | 94 EXPECT_EQ(static_cast<int>(strlen(kEmptyJsonString)), |
| 95 temp.Write(kEmptyJsonString)); | 95 temp.Write(kEmptyJsonString)); |
| 96 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); | 96 std::unique_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); |
| 97 EXPECT_NE(nullptr, value.get()); | 97 EXPECT_NE(nullptr, value.get()); |
| 98 } | 98 } |
| 99 | 99 |
| 100 TEST(DeserializeJsonFromFile, ProperJsonObject) { | 100 TEST(DeserializeJsonFromFile, ProperJsonObject) { |
| 101 ScopedTempFile temp; | 101 ScopedTempFile temp; |
| 102 EXPECT_EQ(static_cast<int>(strlen(kProperJsonString)), | 102 EXPECT_EQ(static_cast<int>(strlen(kProperJsonString)), |
| 103 temp.Write(kProperJsonString)); | 103 temp.Write(kProperJsonString)); |
| 104 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); | 104 std::unique_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); |
| 105 EXPECT_NE(nullptr, value.get()); | 105 EXPECT_NE(nullptr, value.get()); |
| 106 } | 106 } |
| 107 | 107 |
| 108 TEST(DeserializeJsonFromFile, PoorlyFormedJsonObject) { | 108 TEST(DeserializeJsonFromFile, PoorlyFormedJsonObject) { |
| 109 ScopedTempFile temp; | 109 ScopedTempFile temp; |
| 110 EXPECT_EQ(static_cast<int>(strlen(kPoorlyFormedJsonString)), | 110 EXPECT_EQ(static_cast<int>(strlen(kPoorlyFormedJsonString)), |
| 111 temp.Write(kPoorlyFormedJsonString)); | 111 temp.Write(kPoorlyFormedJsonString)); |
| 112 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); | 112 std::unique_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); |
| 113 EXPECT_EQ(nullptr, value.get()); | 113 EXPECT_EQ(nullptr, value.get()); |
| 114 } | 114 } |
| 115 | 115 |
| 116 TEST(SerializeJsonToFile, BadValue) { | 116 TEST(SerializeJsonToFile, BadValue) { |
| 117 ScopedTempFile temp; | 117 ScopedTempFile temp; |
| 118 | 118 |
| 119 base::BinaryValue value(scoped_ptr<char[]>(new char[12]), 12); | 119 base::BinaryValue value(std::unique_ptr<char[]>(new char[12]), 12); |
| 120 ASSERT_FALSE(SerializeJsonToFile(temp.path(), value)); | 120 ASSERT_FALSE(SerializeJsonToFile(temp.path(), value)); |
| 121 std::string str(temp.Read()); | 121 std::string str(temp.Read()); |
| 122 EXPECT_TRUE(str.empty()); | 122 EXPECT_TRUE(str.empty()); |
| 123 } | 123 } |
| 124 | 124 |
| 125 TEST(SerializeJsonToFile, EmptyValue) { | 125 TEST(SerializeJsonToFile, EmptyValue) { |
| 126 ScopedTempFile temp; | 126 ScopedTempFile temp; |
| 127 | 127 |
| 128 base::DictionaryValue value; | 128 base::DictionaryValue value; |
| 129 ASSERT_TRUE(SerializeJsonToFile(temp.path(), value)); | 129 ASSERT_TRUE(SerializeJsonToFile(temp.path(), value)); |
| 130 std::string str(temp.Read()); | 130 std::string str(temp.Read()); |
| 131 ASSERT_FALSE(str.empty()); | 131 ASSERT_FALSE(str.empty()); |
| 132 EXPECT_EQ(kEmptyJsonFileString, str); | 132 EXPECT_EQ(kEmptyJsonFileString, str); |
| 133 } | 133 } |
| 134 | 134 |
| 135 TEST(SerializeJsonToFile, PopulatedValue) { | 135 TEST(SerializeJsonToFile, PopulatedValue) { |
| 136 ScopedTempFile temp; | 136 ScopedTempFile temp; |
| 137 | 137 |
| 138 base::DictionaryValue orig_value; | 138 base::DictionaryValue orig_value; |
| 139 orig_value.SetString(kTestKey, kTestValue); | 139 orig_value.SetString(kTestKey, kTestValue); |
| 140 ASSERT_TRUE(SerializeJsonToFile(temp.path(), orig_value)); | 140 ASSERT_TRUE(SerializeJsonToFile(temp.path(), orig_value)); |
| 141 std::string str(temp.Read()); | 141 std::string str(temp.Read()); |
| 142 ASSERT_FALSE(str.empty()); | 142 ASSERT_FALSE(str.empty()); |
| 143 | 143 |
| 144 scoped_ptr<base::Value> new_value = DeserializeJsonFromFile(temp.path()); | 144 std::unique_ptr<base::Value> new_value = DeserializeJsonFromFile(temp.path()); |
| 145 ASSERT_NE(nullptr, new_value.get()); | 145 ASSERT_NE(nullptr, new_value.get()); |
| 146 EXPECT_TRUE(new_value->Equals(&orig_value)); | 146 EXPECT_TRUE(new_value->Equals(&orig_value)); |
| 147 } | 147 } |
| 148 | 148 |
| 149 } // namespace chromecast | 149 } // namespace chromecast |
| OLD | NEW |