Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Unified Diff: ui/base/clipboard/custom_data_helper_unittest.cc

Issue 8803005: Helpers to pickle custom data for web copy/paste and drag/drop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review comments Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/base/clipboard/custom_data_helper.cc ('k') | ui/ui.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/base/clipboard/custom_data_helper_unittest.cc
diff --git a/ui/base/clipboard/custom_data_helper_unittest.cc b/ui/base/clipboard/custom_data_helper_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..05df47e6354a15add185de597a6d97e618ab336e
--- /dev/null
+++ b/ui/base/clipboard/custom_data_helper_unittest.cc
@@ -0,0 +1,168 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/base/clipboard/custom_data_helper.h"
+
+#include <utility>
+
+#include "base/pickle.h"
+#include "base/utf_string_conversions.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace ui {
+
+namespace {
+
+void PrepareEmptyTestData(Pickle* pickle) {
+ std::map<string16, string16> data;
+ WriteCustomDataToPickle(data, pickle);
+}
+
+void PrepareTestData(Pickle* pickle) {
+ std::map<string16, string16> data;
+ data.insert(std::make_pair(ASCIIToUTF16("abc"), ASCIIToUTF16("")));
+ data.insert(std::make_pair(ASCIIToUTF16("de"), ASCIIToUTF16("1")));
+ data.insert(std::make_pair(ASCIIToUTF16("f"), ASCIIToUTF16("23")));
+ WriteCustomDataToPickle(data, pickle);
+}
+
+TEST(CustomDataHelperTest, EmptyReadTypes) {
+ Pickle pickle;
+ PrepareEmptyTestData(&pickle);
+
+ std::vector<string16> types;
+ ReadCustomDataTypes(pickle.data(), pickle.size(), &types);
+ EXPECT_EQ(0u, types.size());
+}
+
+TEST(CustomDataHelperTest, EmptyReadSingleType) {
+ Pickle pickle;
+ PrepareEmptyTestData(&pickle);
+
+ string16 result;
+ ReadCustomDataForType(pickle.data(),
+ pickle.size(),
+ ASCIIToUTF16("f"),
+ &result);
+ EXPECT_EQ(ASCIIToUTF16(""), result);
+}
+
+TEST(CustomDataHelperTest, EmptyReadMap) {
+ Pickle pickle;
+ PrepareEmptyTestData(&pickle);
+
+ std::map<string16, string16> result;
+ ReadCustomDataIntoMap(pickle.data(), pickle.size(), &result);
+ EXPECT_EQ(0u, result.size());
+}
+
+TEST(CustomDataHelperTest, ReadTypes) {
+ Pickle pickle;
+ PrepareTestData(&pickle);
+
+ std::vector<string16> types;
+ ReadCustomDataTypes(pickle.data(), pickle.size(), &types);
+
+ std::vector<string16> expected;
+ expected.push_back(ASCIIToUTF16("abc"));
+ expected.push_back(ASCIIToUTF16("de"));
+ expected.push_back(ASCIIToUTF16("f"));
+ EXPECT_EQ(expected, types);
+}
+
+TEST(CustomDataHelperTest, ReadSingleType) {
+ Pickle pickle;
+ PrepareTestData(&pickle);
+
+ string16 result;
+ ReadCustomDataForType(pickle.data(),
+ pickle.size(),
+ ASCIIToUTF16("abc"),
+ &result);
+ EXPECT_EQ(ASCIIToUTF16(""), result);
+
+ ReadCustomDataForType(pickle.data(),
+ pickle.size(),
+ ASCIIToUTF16("de"),
+ &result);
+ EXPECT_EQ(ASCIIToUTF16("1"), result);
+
+ ReadCustomDataForType(pickle.data(),
+ pickle.size(),
+ ASCIIToUTF16("f"),
+ &result);
+ EXPECT_EQ(ASCIIToUTF16("23"), result);
+}
+
+TEST(CustomDataHelperTest, ReadMap) {
+ Pickle pickle;
+ PrepareTestData(&pickle);
+
+ std::map<string16, string16> result;
+ ReadCustomDataIntoMap(pickle.data(), pickle.size(), &result);
+
+ std::map<string16, string16> expected;
+ expected.insert(std::make_pair(ASCIIToUTF16("abc"), ASCIIToUTF16("")));
+ expected.insert(std::make_pair(ASCIIToUTF16("de"), ASCIIToUTF16("1")));
+ expected.insert(std::make_pair(ASCIIToUTF16("f"), ASCIIToUTF16("23")));
+ EXPECT_EQ(expected, result);
+}
+
+TEST(CustomDataHelperTest, BadReadTypes) {
+ // ReadCustomDataTypes makes the additional guarantee that the contents of the
+ // result vector will not change if the input is malformed.
+ std::vector<string16> expected;
+ expected.push_back(ASCIIToUTF16("abc"));
+ expected.push_back(ASCIIToUTF16("de"));
+ expected.push_back(ASCIIToUTF16("f"));
+
+ Pickle malformed;
+ malformed.WriteSize(1000);
+ malformed.WriteString16(ASCIIToUTF16("hello"));
+ malformed.WriteString16(ASCIIToUTF16("world"));
+ std::vector<string16> actual(expected);
+ ReadCustomDataTypes(malformed.data(), malformed.size(), &actual);
+ EXPECT_EQ(expected, actual);
+
+ Pickle malformed2;
+ malformed2.WriteSize(1);
+ malformed2.WriteString16(ASCIIToUTF16("hello"));
+ std::vector<string16> actual2(expected);
+ ReadCustomDataTypes(malformed2.data(), malformed2.size(), &actual2);
+ EXPECT_EQ(expected, actual2);
+}
+
+TEST(CustomDataHelperTest, BadPickle) {
+ string16 result_data;
+ std::map<string16, string16> result_map;
+
+ Pickle malformed;
+ malformed.WriteSize(1000);
+ malformed.WriteString16(ASCIIToUTF16("hello"));
+ malformed.WriteString16(ASCIIToUTF16("world"));
+
+ ReadCustomDataForType(malformed.data(),
+ malformed.size(),
+ ASCIIToUTF16("f"),
+ &result_data);
+ ReadCustomDataIntoMap(malformed.data(), malformed.size(), &result_map);
+ EXPECT_EQ(0u, result_data.size());
+ EXPECT_EQ(0u, result_map.size());
+
+ Pickle malformed2;
+ malformed2.WriteSize(1);
+ malformed2.WriteString16(ASCIIToUTF16("hello"));
+
+ ReadCustomDataForType(malformed2.data(),
+ malformed2.size(),
+ ASCIIToUTF16("f"),
+ &result_data);
+ ReadCustomDataIntoMap(malformed2.data(), malformed2.size(), &result_map);
+ EXPECT_EQ(0u, result_data.size());
+ EXPECT_EQ(0u, result_map.size());
+}
+
+} // namespace
+
+} // namespace ui
« no previous file with comments | « ui/base/clipboard/custom_data_helper.cc ('k') | ui/ui.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698