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/strings/utf_string_conversions.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using base::ASCIIToUTF16
;
19 void PrepareEmptyTestData(Pickle
* pickle
) {
20 std::map
<base::string16
, base::string16
> data
;
21 WriteCustomDataToPickle(data
, pickle
);
24 void PrepareTestData(Pickle
* pickle
) {
25 std::map
<base::string16
, base::string16
> data
;
26 data
.insert(std::make_pair(ASCIIToUTF16("abc"), base::string16()));
27 data
.insert(std::make_pair(ASCIIToUTF16("de"), ASCIIToUTF16("1")));
28 data
.insert(std::make_pair(ASCIIToUTF16("f"), ASCIIToUTF16("23")));
29 WriteCustomDataToPickle(data
, pickle
);
32 TEST(CustomDataHelperTest
, EmptyReadTypes
) {
34 PrepareEmptyTestData(&pickle
);
36 std::vector
<base::string16
> types
;
37 ReadCustomDataTypes(pickle
.data(), pickle
.size(), &types
);
38 EXPECT_EQ(0u, types
.size());
41 TEST(CustomDataHelperTest
, EmptyReadSingleType
) {
43 PrepareEmptyTestData(&pickle
);
45 base::string16 result
;
46 ReadCustomDataForType(pickle
.data(),
50 EXPECT_EQ(base::string16(), result
);
53 TEST(CustomDataHelperTest
, EmptyReadMap
) {
55 PrepareEmptyTestData(&pickle
);
57 std::map
<base::string16
, base::string16
> result
;
58 ReadCustomDataIntoMap(pickle
.data(), pickle
.size(), &result
);
59 EXPECT_EQ(0u, result
.size());
62 TEST(CustomDataHelperTest
, ReadTypes
) {
64 PrepareTestData(&pickle
);
66 std::vector
<base::string16
> types
;
67 ReadCustomDataTypes(pickle
.data(), pickle
.size(), &types
);
69 std::vector
<base::string16
> expected
;
70 expected
.push_back(ASCIIToUTF16("abc"));
71 expected
.push_back(ASCIIToUTF16("de"));
72 expected
.push_back(ASCIIToUTF16("f"));
73 EXPECT_EQ(expected
, types
);
76 TEST(CustomDataHelperTest
, ReadSingleType
) {
78 PrepareTestData(&pickle
);
80 base::string16 result
;
81 ReadCustomDataForType(pickle
.data(),
85 EXPECT_EQ(base::string16(), result
);
87 ReadCustomDataForType(pickle
.data(),
91 EXPECT_EQ(ASCIIToUTF16("1"), result
);
93 ReadCustomDataForType(pickle
.data(),
97 EXPECT_EQ(ASCIIToUTF16("23"), result
);
100 TEST(CustomDataHelperTest
, ReadMap
) {
102 PrepareTestData(&pickle
);
104 std::map
<base::string16
, base::string16
> result
;
105 ReadCustomDataIntoMap(pickle
.data(), pickle
.size(), &result
);
107 std::map
<base::string16
, base::string16
> expected
;
108 expected
.insert(std::make_pair(ASCIIToUTF16("abc"), base::string16()));
109 expected
.insert(std::make_pair(ASCIIToUTF16("de"), ASCIIToUTF16("1")));
110 expected
.insert(std::make_pair(ASCIIToUTF16("f"), ASCIIToUTF16("23")));
111 EXPECT_EQ(expected
, result
);
114 TEST(CustomDataHelperTest
, BadReadTypes
) {
115 // ReadCustomDataTypes makes the additional guarantee that the contents of the
116 // result vector will not change if the input is malformed.
117 std::vector
<base::string16
> expected
;
118 expected
.push_back(ASCIIToUTF16("abc"));
119 expected
.push_back(ASCIIToUTF16("de"));
120 expected
.push_back(ASCIIToUTF16("f"));
123 malformed
.WriteSizeT(1000);
124 malformed
.WriteString16(ASCIIToUTF16("hello"));
125 malformed
.WriteString16(ASCIIToUTF16("world"));
126 std::vector
<base::string16
> actual(expected
);
127 ReadCustomDataTypes(malformed
.data(), malformed
.size(), &actual
);
128 EXPECT_EQ(expected
, actual
);
131 malformed2
.WriteSizeT(1);
132 malformed2
.WriteString16(ASCIIToUTF16("hello"));
133 std::vector
<base::string16
> actual2(expected
);
134 ReadCustomDataTypes(malformed2
.data(), malformed2
.size(), &actual2
);
135 EXPECT_EQ(expected
, actual2
);
138 TEST(CustomDataHelperTest
, BadPickle
) {
139 base::string16 result_data
;
140 std::map
<base::string16
, base::string16
> result_map
;
143 malformed
.WriteSizeT(1000);
144 malformed
.WriteString16(ASCIIToUTF16("hello"));
145 malformed
.WriteString16(ASCIIToUTF16("world"));
147 ReadCustomDataForType(malformed
.data(),
151 ReadCustomDataIntoMap(malformed
.data(), malformed
.size(), &result_map
);
152 EXPECT_EQ(0u, result_data
.size());
153 EXPECT_EQ(0u, result_map
.size());
156 malformed2
.WriteSizeT(1);
157 malformed2
.WriteString16(ASCIIToUTF16("hello"));
159 ReadCustomDataForType(malformed2
.data(),
163 ReadCustomDataIntoMap(malformed2
.data(), malformed2
.size(), &result_map
);
164 EXPECT_EQ(0u, result_data
.size());
165 EXPECT_EQ(0u, result_map
.size());