Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / base / clipboard / custom_data_helper_unittest.cc
blob1ed53c667505b88debbf2c5b484d67f898692a54
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/strings/utf_string_conversions.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using base::ASCIIToUTF16;
15 namespace ui {
17 namespace {
19 void PrepareEmptyTestData(base::Pickle* pickle) {
20 std::map<base::string16, base::string16> data;
21 WriteCustomDataToPickle(data, pickle);
24 void PrepareTestData(base::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) {
33 base::Pickle pickle;
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) {
42 base::Pickle pickle;
43 PrepareEmptyTestData(&pickle);
45 base::string16 result;
46 ReadCustomDataForType(pickle.data(),
47 pickle.size(),
48 ASCIIToUTF16("f"),
49 &result);
50 EXPECT_EQ(base::string16(), result);
53 TEST(CustomDataHelperTest, EmptyReadMap) {
54 base::Pickle pickle;
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) {
63 base::Pickle pickle;
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) {
77 base::Pickle pickle;
78 PrepareTestData(&pickle);
80 base::string16 result;
81 ReadCustomDataForType(pickle.data(),
82 pickle.size(),
83 ASCIIToUTF16("abc"),
84 &result);
85 EXPECT_EQ(base::string16(), result);
87 ReadCustomDataForType(pickle.data(),
88 pickle.size(),
89 ASCIIToUTF16("de"),
90 &result);
91 EXPECT_EQ(ASCIIToUTF16("1"), result);
93 ReadCustomDataForType(pickle.data(),
94 pickle.size(),
95 ASCIIToUTF16("f"),
96 &result);
97 EXPECT_EQ(ASCIIToUTF16("23"), result);
100 TEST(CustomDataHelperTest, ReadMap) {
101 base::Pickle pickle;
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"));
122 base::Pickle malformed;
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);
130 base::Pickle malformed2;
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;
142 base::Pickle malformed;
143 malformed.WriteSizeT(1000);
144 malformed.WriteString16(ASCIIToUTF16("hello"));
145 malformed.WriteString16(ASCIIToUTF16("world"));
147 ReadCustomDataForType(malformed.data(),
148 malformed.size(),
149 ASCIIToUTF16("f"),
150 &result_data);
151 ReadCustomDataIntoMap(malformed.data(), malformed.size(), &result_map);
152 EXPECT_EQ(0u, result_data.size());
153 EXPECT_EQ(0u, result_map.size());
155 base::Pickle malformed2;
156 malformed2.WriteSizeT(1);
157 malformed2.WriteString16(ASCIIToUTF16("hello"));
159 ReadCustomDataForType(malformed2.data(),
160 malformed2.size(),
161 ASCIIToUTF16("f"),
162 &result_data);
163 ReadCustomDataIntoMap(malformed2.data(), malformed2.size(), &result_map);
164 EXPECT_EQ(0u, result_data.size());
165 EXPECT_EQ(0u, result_map.size());
168 } // namespace
170 } // namespace ui