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 "ui/base/clipboard/custom_data_helper.h" 6 7 #include <utility> 8 9 #include "base/pickle.h" 10 #include "base/strings/utf_string_conversions.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 namespace ui { 14 15 namespace { 16 17 void PrepareEmptyTestData(Pickle* pickle) { 18 std::map<string16, string16> data; 19 WriteCustomDataToPickle(data, pickle); 20 } 21 22 void PrepareTestData(Pickle* pickle) { 23 std::map<string16, string16> data; 24 data.insert(std::make_pair(ASCIIToUTF16("abc"), string16())); 25 data.insert(std::make_pair(ASCIIToUTF16("de"), ASCIIToUTF16("1"))); 26 data.insert(std::make_pair(ASCIIToUTF16("f"), ASCIIToUTF16("23"))); 27 WriteCustomDataToPickle(data, pickle); 28 } 29 30 TEST(CustomDataHelperTest, EmptyReadTypes) { 31 Pickle pickle; 32 PrepareEmptyTestData(&pickle); 33 34 std::vector<string16> types; 35 ReadCustomDataTypes(pickle.data(), pickle.size(), &types); 36 EXPECT_EQ(0u, types.size()); 37 } 38 39 TEST(CustomDataHelperTest, EmptyReadSingleType) { 40 Pickle pickle; 41 PrepareEmptyTestData(&pickle); 42 43 string16 result; 44 ReadCustomDataForType(pickle.data(), 45 pickle.size(), 46 ASCIIToUTF16("f"), 47 &result); 48 EXPECT_EQ(string16(), result); 49 } 50 51 TEST(CustomDataHelperTest, EmptyReadMap) { 52 Pickle pickle; 53 PrepareEmptyTestData(&pickle); 54 55 std::map<string16, string16> result; 56 ReadCustomDataIntoMap(pickle.data(), pickle.size(), &result); 57 EXPECT_EQ(0u, result.size()); 58 } 59 60 TEST(CustomDataHelperTest, ReadTypes) { 61 Pickle pickle; 62 PrepareTestData(&pickle); 63 64 std::vector<string16> types; 65 ReadCustomDataTypes(pickle.data(), pickle.size(), &types); 66 67 std::vector<string16> expected; 68 expected.push_back(ASCIIToUTF16("abc")); 69 expected.push_back(ASCIIToUTF16("de")); 70 expected.push_back(ASCIIToUTF16("f")); 71 EXPECT_EQ(expected, types); 72 } 73 74 TEST(CustomDataHelperTest, ReadSingleType) { 75 Pickle pickle; 76 PrepareTestData(&pickle); 77 78 string16 result; 79 ReadCustomDataForType(pickle.data(), 80 pickle.size(), 81 ASCIIToUTF16("abc"), 82 &result); 83 EXPECT_EQ(string16(), result); 84 85 ReadCustomDataForType(pickle.data(), 86 pickle.size(), 87 ASCIIToUTF16("de"), 88 &result); 89 EXPECT_EQ(ASCIIToUTF16("1"), result); 90 91 ReadCustomDataForType(pickle.data(), 92 pickle.size(), 93 ASCIIToUTF16("f"), 94 &result); 95 EXPECT_EQ(ASCIIToUTF16("23"), result); 96 } 97 98 TEST(CustomDataHelperTest, ReadMap) { 99 Pickle pickle; 100 PrepareTestData(&pickle); 101 102 std::map<string16, string16> result; 103 ReadCustomDataIntoMap(pickle.data(), pickle.size(), &result); 104 105 std::map<string16, string16> expected; 106 expected.insert(std::make_pair(ASCIIToUTF16("abc"), string16())); 107 expected.insert(std::make_pair(ASCIIToUTF16("de"), ASCIIToUTF16("1"))); 108 expected.insert(std::make_pair(ASCIIToUTF16("f"), ASCIIToUTF16("23"))); 109 EXPECT_EQ(expected, result); 110 } 111 112 TEST(CustomDataHelperTest, BadReadTypes) { 113 // ReadCustomDataTypes makes the additional guarantee that the contents of the 114 // result vector will not change if the input is malformed. 115 std::vector<string16> expected; 116 expected.push_back(ASCIIToUTF16("abc")); 117 expected.push_back(ASCIIToUTF16("de")); 118 expected.push_back(ASCIIToUTF16("f")); 119 120 Pickle malformed; 121 malformed.WriteUInt64(1000); 122 malformed.WriteString16(ASCIIToUTF16("hello")); 123 malformed.WriteString16(ASCIIToUTF16("world")); 124 std::vector<string16> actual(expected); 125 ReadCustomDataTypes(malformed.data(), malformed.size(), &actual); 126 EXPECT_EQ(expected, actual); 127 128 Pickle malformed2; 129 malformed2.WriteUInt64(1); 130 malformed2.WriteString16(ASCIIToUTF16("hello")); 131 std::vector<string16> actual2(expected); 132 ReadCustomDataTypes(malformed2.data(), malformed2.size(), &actual2); 133 EXPECT_EQ(expected, actual2); 134 } 135 136 TEST(CustomDataHelperTest, BadPickle) { 137 string16 result_data; 138 std::map<string16, string16> result_map; 139 140 Pickle malformed; 141 malformed.WriteUInt64(1000); 142 malformed.WriteString16(ASCIIToUTF16("hello")); 143 malformed.WriteString16(ASCIIToUTF16("world")); 144 145 ReadCustomDataForType(malformed.data(), 146 malformed.size(), 147 ASCIIToUTF16("f"), 148 &result_data); 149 ReadCustomDataIntoMap(malformed.data(), malformed.size(), &result_map); 150 EXPECT_EQ(0u, result_data.size()); 151 EXPECT_EQ(0u, result_map.size()); 152 153 Pickle malformed2; 154 malformed2.WriteUInt64(1); 155 malformed2.WriteString16(ASCIIToUTF16("hello")); 156 157 ReadCustomDataForType(malformed2.data(), 158 malformed2.size(), 159 ASCIIToUTF16("f"), 160 &result_data); 161 ReadCustomDataIntoMap(malformed2.data(), malformed2.size(), &result_map); 162 EXPECT_EQ(0u, result_data.size()); 163 EXPECT_EQ(0u, result_map.size()); 164 } 165 166 } // namespace 167 168 } // namespace ui 169