When Retrier succeeds, record errors it encountered.
[chromium-blink-merge.git] / ui / base / clipboard / custom_data_helper_unittest.cc
blobbf9263ec1d10df6743ae38e45035fb094bc379db
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"
7 #include <utility>
9 #include "base/pickle.h"
10 #include "base/utf_string_conversions.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace ui {
15 namespace {
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) {
31 Pickle pickle;
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) {
40 Pickle pickle;
41 PrepareEmptyTestData(&pickle);
43 string16 result;
44 ReadCustomDataForType(pickle.data(),
45 pickle.size(),
46 ASCIIToUTF16("f"),
47 &result);
48 EXPECT_EQ(string16(), result);
51 TEST(CustomDataHelperTest, EmptyReadMap) {
52 Pickle pickle;
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) {
61 Pickle pickle;
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) {
75 Pickle pickle;
76 PrepareTestData(&pickle);
78 string16 result;
79 ReadCustomDataForType(pickle.data(),
80 pickle.size(),
81 ASCIIToUTF16("abc"),
82 &result);
83 EXPECT_EQ(string16(), result);
85 ReadCustomDataForType(pickle.data(),
86 pickle.size(),
87 ASCIIToUTF16("de"),
88 &result);
89 EXPECT_EQ(ASCIIToUTF16("1"), result);
91 ReadCustomDataForType(pickle.data(),
92 pickle.size(),
93 ASCIIToUTF16("f"),
94 &result);
95 EXPECT_EQ(ASCIIToUTF16("23"), result);
98 TEST(CustomDataHelperTest, ReadMap) {
99 Pickle pickle;
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"));
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);
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);
136 TEST(CustomDataHelperTest, BadPickle) {
137 string16 result_data;
138 std::map<string16, string16> result_map;
140 Pickle malformed;
141 malformed.WriteUInt64(1000);
142 malformed.WriteString16(ASCIIToUTF16("hello"));
143 malformed.WriteString16(ASCIIToUTF16("world"));
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());
153 Pickle malformed2;
154 malformed2.WriteUInt64(1);
155 malformed2.WriteString16(ASCIIToUTF16("hello"));
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());
166 } // namespace
168 } // namespace ui