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.
5 #include "ui/base/clipboard/custom_data_helper.h"
9 #include "base/pickle.h"
10 #include "base/utf_string_conversions.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 void PrepareEmptyTestData(Pickle
* pickle
) {
18 std::map
<string16
, string16
> data
;
19 WriteCustomDataToPickle(data
, pickle
);
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
);
30 TEST(CustomDataHelperTest
, EmptyReadTypes
) {
32 PrepareEmptyTestData(&pickle
);
34 std::vector
<string16
> types
;
35 ReadCustomDataTypes(pickle
.data(), pickle
.size(), &types
);
36 EXPECT_EQ(0u, types
.size());
39 TEST(CustomDataHelperTest
, EmptyReadSingleType
) {
41 PrepareEmptyTestData(&pickle
);
44 ReadCustomDataForType(pickle
.data(),
48 EXPECT_EQ(string16(), result
);
51 TEST(CustomDataHelperTest
, EmptyReadMap
) {
53 PrepareEmptyTestData(&pickle
);
55 std::map
<string16
, string16
> result
;
56 ReadCustomDataIntoMap(pickle
.data(), pickle
.size(), &result
);
57 EXPECT_EQ(0u, result
.size());
60 TEST(CustomDataHelperTest
, ReadTypes
) {
62 PrepareTestData(&pickle
);
64 std::vector
<string16
> types
;
65 ReadCustomDataTypes(pickle
.data(), pickle
.size(), &types
);
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
);
74 TEST(CustomDataHelperTest
, ReadSingleType
) {
76 PrepareTestData(&pickle
);
79 ReadCustomDataForType(pickle
.data(),
83 EXPECT_EQ(string16(), result
);
85 ReadCustomDataForType(pickle
.data(),
89 EXPECT_EQ(ASCIIToUTF16("1"), result
);
91 ReadCustomDataForType(pickle
.data(),
95 EXPECT_EQ(ASCIIToUTF16("23"), result
);
98 TEST(CustomDataHelperTest
, ReadMap
) {
100 PrepareTestData(&pickle
);
102 std::map
<string16
, string16
> result
;
103 ReadCustomDataIntoMap(pickle
.data(), pickle
.size(), &result
);
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
);
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"));
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
);
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
);
136 TEST(CustomDataHelperTest
, BadPickle
) {
137 string16 result_data
;
138 std::map
<string16
, string16
> result_map
;
141 malformed
.WriteUInt64(1000);
142 malformed
.WriteString16(ASCIIToUTF16("hello"));
143 malformed
.WriteString16(ASCIIToUTF16("world"));
145 ReadCustomDataForType(malformed
.data(),
149 ReadCustomDataIntoMap(malformed
.data(), malformed
.size(), &result_map
);
150 EXPECT_EQ(0u, result_data
.size());
151 EXPECT_EQ(0u, result_map
.size());
154 malformed2
.WriteUInt64(1);
155 malformed2
.WriteString16(ASCIIToUTF16("hello"));
157 ReadCustomDataForType(malformed2
.data(),
161 ReadCustomDataIntoMap(malformed2
.data(), malformed2
.size(), &result_map
);
162 EXPECT_EQ(0u, result_data
.size());
163 EXPECT_EQ(0u, result_map
.size());