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

Side by Side Diff: base/clipboard_unittest.cc

Issue 159815: Refactor bookmark clipboard code to be cross platform. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix UMR Created 11 years, 4 months 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/clipboard.h" 8 #include "base/clipboard.h"
9 #include "base/gfx/size.h" 9 #include "base/gfx/size.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/pickle.h"
11 #include "base/scoped_clipboard_writer.h" 12 #include "base/scoped_clipboard_writer.h"
12 #include "base/string_util.h" 13 #include "base/string_util.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h" 15 #include "testing/platform_test.h"
15 16
16 #if defined(OS_WIN) 17 #if defined(OS_WIN)
17 class ClipboardTest : public PlatformTest { 18 class ClipboardTest : public PlatformTest {
18 protected: 19 protected:
19 virtual void SetUp() { 20 virtual void SetUp() {
20 message_loop_.reset(new MessageLoopForUI()); 21 message_loop_.reset(new MessageLoopForUI());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 Clipboard clipboard; 53 Clipboard clipboard;
53 54
54 string16 text(ASCIIToUTF16("This is a string16!#$")), text_result; 55 string16 text(ASCIIToUTF16("This is a string16!#$")), text_result;
55 std::string ascii_text; 56 std::string ascii_text;
56 57
57 { 58 {
58 ScopedClipboardWriter clipboard_writer(&clipboard); 59 ScopedClipboardWriter clipboard_writer(&clipboard);
59 clipboard_writer.WriteText(text); 60 clipboard_writer.WriteText(text);
60 } 61 }
61 62
62 EXPECT_TRUE(clipboard.IsFormatAvailable( 63 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetPlainTextWFormatType())) ;
sky 2009/08/04 16:14:29 nit > 80 chars
63 Clipboard::GetPlainTextWFormatType())); 64 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetPlainTextFormatType()));
64 EXPECT_TRUE(clipboard.IsFormatAvailable(
65 Clipboard::GetPlainTextFormatType()));
66 clipboard.ReadText(&text_result); 65 clipboard.ReadText(&text_result);
67 66
68 EXPECT_EQ(text, text_result); 67 EXPECT_EQ(text, text_result);
69 clipboard.ReadAsciiText(&ascii_text); 68 clipboard.ReadAsciiText(&ascii_text);
70 EXPECT_EQ(UTF16ToUTF8(text), ascii_text); 69 EXPECT_EQ(UTF16ToUTF8(text), ascii_text);
71 } 70 }
72 71
73 TEST_F(ClipboardTest, HTMLTest) { 72 TEST_F(ClipboardTest, HTMLTest) {
74 Clipboard clipboard; 73 Clipboard clipboard;
75 74
76 string16 markup(ASCIIToUTF16("<string>Hi!</string>")), markup_result; 75 string16 markup(ASCIIToUTF16("<string>Hi!</string>")), markup_result;
77 std::string url("http://www.example.com/"), url_result; 76 std::string url("http://www.example.com/"), url_result;
78 77
79 { 78 {
80 ScopedClipboardWriter clipboard_writer(&clipboard); 79 ScopedClipboardWriter clipboard_writer(&clipboard);
81 clipboard_writer.WriteHTML(markup, url); 80 clipboard_writer.WriteHTML(markup, url);
82 } 81 }
83 82
84 EXPECT_EQ(true, clipboard.IsFormatAvailable( 83 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetHtmlFormatType()));
85 Clipboard::GetHtmlFormatType()));
86 clipboard.ReadHTML(&markup_result, &url_result); 84 clipboard.ReadHTML(&markup_result, &url_result);
87 EXPECT_EQ(markup, markup_result); 85 EXPECT_EQ(markup, markup_result);
88 #if defined(OS_WIN) 86 #if defined(OS_WIN)
89 // TODO(playmobil): It's not clear that non windows clipboards need to support 87 // TODO(playmobil): It's not clear that non windows clipboards need to support
90 // this. 88 // this.
91 EXPECT_EQ(url, url_result); 89 EXPECT_EQ(url, url_result);
92 #endif // defined(OS_WIN) 90 #endif // defined(OS_WIN)
93 } 91 }
94 92
95 TEST_F(ClipboardTest, TrickyHTMLTest) { 93 TEST_F(ClipboardTest, TrickyHTMLTest) {
96 Clipboard clipboard; 94 Clipboard clipboard;
97 95
98 string16 markup(ASCIIToUTF16("<em>Bye!<!--EndFragment --></em>")), 96 string16 markup(ASCIIToUTF16("<em>Bye!<!--EndFragment --></em>")),
99 markup_result; 97 markup_result;
100 std::string url, url_result; 98 std::string url, url_result;
101 99
102 { 100 {
103 ScopedClipboardWriter clipboard_writer(&clipboard); 101 ScopedClipboardWriter clipboard_writer(&clipboard);
104 clipboard_writer.WriteHTML(markup, url); 102 clipboard_writer.WriteHTML(markup, url);
105 } 103 }
106 104
107 EXPECT_EQ(true, clipboard.IsFormatAvailable( 105 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetHtmlFormatType()));
108 Clipboard::GetHtmlFormatType()));
109 clipboard.ReadHTML(&markup_result, &url_result); 106 clipboard.ReadHTML(&markup_result, &url_result);
110 EXPECT_EQ(markup, markup_result); 107 EXPECT_EQ(markup, markup_result);
111 #if defined(OS_WIN) 108 #if defined(OS_WIN)
112 // TODO(playmobil): It's not clear that non windows clipboards need to support 109 // TODO(playmobil): It's not clear that non windows clipboards need to support
113 // this. 110 // this.
114 EXPECT_EQ(url, url_result); 111 EXPECT_EQ(url, url_result);
115 #endif // defined(OS_WIN) 112 #endif // defined(OS_WIN)
116 } 113 }
117 114
118 // TODO(estade): Port the following test (decide what target we use for urls) 115 // TODO(estade): Port the following test (decide what target we use for urls)
119 #if !defined(OS_LINUX) 116 #if !defined(OS_LINUX)
120 TEST_F(ClipboardTest, BookmarkTest) { 117 TEST_F(ClipboardTest, BookmarkTest) {
121 Clipboard clipboard; 118 Clipboard clipboard;
122 119
123 string16 title(ASCIIToUTF16("The Example Company")), title_result; 120 string16 title(ASCIIToUTF16("The Example Company")), title_result;
124 std::string url("http://www.example.com/"), url_result; 121 std::string url("http://www.example.com/"), url_result;
125 122
126 { 123 {
127 ScopedClipboardWriter clipboard_writer(&clipboard); 124 ScopedClipboardWriter clipboard_writer(&clipboard);
128 clipboard_writer.WriteBookmark(title, url); 125 clipboard_writer.WriteBookmark(title, url);
129 } 126 }
130 127
131 EXPECT_EQ(true, 128 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetUrlWFormatType()));
132 clipboard.IsFormatAvailable(Clipboard::GetUrlWFormatType()));
133 clipboard.ReadBookmark(&title_result, &url_result); 129 clipboard.ReadBookmark(&title_result, &url_result);
134 EXPECT_EQ(title, title_result); 130 EXPECT_EQ(title, title_result);
135 EXPECT_EQ(url, url_result); 131 EXPECT_EQ(url, url_result);
136 } 132 }
137 #endif // defined(OS_WIN) 133 #endif // defined(OS_WIN)
138 134
139 TEST_F(ClipboardTest, MultiFormatTest) { 135 TEST_F(ClipboardTest, MultiFormatTest) {
140 Clipboard clipboard; 136 Clipboard clipboard;
141 137
142 string16 text(ASCIIToUTF16("Hi!")), text_result; 138 string16 text(ASCIIToUTF16("Hi!")), text_result;
143 string16 markup(ASCIIToUTF16("<strong>Hi!</string>")), markup_result; 139 string16 markup(ASCIIToUTF16("<strong>Hi!</string>")), markup_result;
144 std::string url("http://www.example.com/"), url_result; 140 std::string url("http://www.example.com/"), url_result;
145 std::string ascii_text; 141 std::string ascii_text;
146 142
147 { 143 {
148 ScopedClipboardWriter clipboard_writer(&clipboard); 144 ScopedClipboardWriter clipboard_writer(&clipboard);
149 clipboard_writer.WriteHTML(markup, url); 145 clipboard_writer.WriteHTML(markup, url);
150 clipboard_writer.WriteText(text); 146 clipboard_writer.WriteText(text);
151 } 147 }
152 148
153 EXPECT_EQ(true, 149 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetHtmlFormatType()));
154 clipboard.IsFormatAvailable(Clipboard::GetHtmlFormatType())); 150 EXPECT_TRUE(clipboard.IsFormatAvailable(
155 EXPECT_EQ(true, clipboard.IsFormatAvailable(
156 Clipboard::GetPlainTextWFormatType())); 151 Clipboard::GetPlainTextWFormatType()));
157 EXPECT_EQ(true, clipboard.IsFormatAvailable( 152 EXPECT_TRUE(clipboard.IsFormatAvailable(
158 Clipboard::GetPlainTextFormatType())); 153 Clipboard::GetPlainTextFormatType()));
159 clipboard.ReadHTML(&markup_result, &url_result); 154 clipboard.ReadHTML(&markup_result, &url_result);
160 EXPECT_EQ(markup, markup_result); 155 EXPECT_EQ(markup, markup_result);
161 #if defined(OS_WIN) 156 #if defined(OS_WIN)
162 // TODO(playmobil): It's not clear that non windows clipboards need to support 157 // TODO(playmobil): It's not clear that non windows clipboards need to support
163 // this. 158 // this.
164 EXPECT_EQ(url, url_result); 159 EXPECT_EQ(url, url_result);
165 #endif // defined(OS_WIN) 160 #endif // defined(OS_WIN)
166 clipboard.ReadText(&text_result); 161 clipboard.ReadText(&text_result);
167 EXPECT_EQ(text, text_result); 162 EXPECT_EQ(text, text_result);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 214
220 std::vector<FilePath> out_files; 215 std::vector<FilePath> out_files;
221 clipboard.ReadFiles(&out_files); 216 clipboard.ReadFiles(&out_files);
222 217
223 EXPECT_EQ(files.size(), out_files.size()); 218 EXPECT_EQ(files.size(), out_files.size());
224 for (size_t i = 0; i < out_files.size(); ++i) 219 for (size_t i = 0; i < out_files.size(); ++i)
225 EXPECT_EQ(files[i].value(), out_files[i].value()); 220 EXPECT_EQ(files[i].value(), out_files[i].value());
226 } 221 }
227 #endif // !defined(OS_LINUX) 222 #endif // !defined(OS_LINUX)
228 223
224 #if defined(OS_WIN) || defined(OS_LINUX)
225 TEST_F(ClipboardTest, DataTest) {
226 Clipboard clipboard;
227 const char* format = "chromium/x-test-format";
228 std::string payload("test string");
229 Pickle write_pickle;
230 write_pickle.WriteString(payload);
231
232 {
233 ScopedClipboardWriter clipboard_writer(&clipboard);
234 clipboard_writer.WritePickledData(write_pickle, format);
235 }
236
237 ASSERT_TRUE(clipboard.IsFormatAvailableByString(format));
238 std::string output;
239 clipboard.ReadData(format, &output);
240 ASSERT_FALSE(output.empty());
241
242 Pickle read_pickle(output.data(), output.size());
243 void* iter = NULL;
244 std::string unpickled_string;
245 ASSERT_TRUE(read_pickle.ReadString(&iter, &unpickled_string));
246 EXPECT_EQ(payload, unpickled_string);
247 }
248 #endif
249
229 #if defined(OS_WIN) // Windows only tests. 250 #if defined(OS_WIN) // Windows only tests.
230 TEST_F(ClipboardTest, HyperlinkTest) { 251 TEST_F(ClipboardTest, HyperlinkTest) {
231 Clipboard clipboard; 252 Clipboard clipboard;
232 253
233 string16 title(ASCIIToUTF16("The Example Company")), title_result; 254 string16 title(ASCIIToUTF16("The Example Company")), title_result;
234 std::string url("http://www.example.com/"), url_result; 255 std::string url("http://www.example.com/"), url_result;
235 string16 html(ASCIIToUTF16("<a href=\"http://www.example.com/\">" 256 string16 html(ASCIIToUTF16("<a href=\"http://www.example.com/\">"
236 "The Example Company</a>")), html_result; 257 "The Example Company</a>")), html_result;
237 258
238 { 259 {
239 ScopedClipboardWriter clipboard_writer(&clipboard); 260 ScopedClipboardWriter clipboard_writer(&clipboard);
240 clipboard_writer.WriteHyperlink(title, url); 261 clipboard_writer.WriteHyperlink(title, url);
241 } 262 }
242 263
243 EXPECT_EQ(true, 264 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetUrlWFormatType()));
244 clipboard.IsFormatAvailable(Clipboard::GetUrlWFormatType())); 265 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetHtmlFormatType()));
245 EXPECT_EQ(true,
246 clipboard.IsFormatAvailable(Clipboard::GetHtmlFormatType()));
247 clipboard.ReadBookmark(&title_result, &url_result); 266 clipboard.ReadBookmark(&title_result, &url_result);
248 EXPECT_EQ(title, title_result); 267 EXPECT_EQ(title, title_result);
249 EXPECT_EQ(url, url_result); 268 EXPECT_EQ(url, url_result);
250 clipboard.ReadHTML(&html_result, &url_result); 269 clipboard.ReadHTML(&html_result, &url_result);
251 EXPECT_EQ(html, html_result); 270 EXPECT_EQ(html, html_result);
252 } 271 }
253 272
254 TEST_F(ClipboardTest, WebSmartPasteTest) { 273 TEST_F(ClipboardTest, WebSmartPasteTest) {
255 Clipboard clipboard; 274 Clipboard clipboard;
256 275
257 { 276 {
258 ScopedClipboardWriter clipboard_writer(&clipboard); 277 ScopedClipboardWriter clipboard_writer(&clipboard);
259 clipboard_writer.WriteWebSmartPaste(); 278 clipboard_writer.WriteWebSmartPaste();
260 } 279 }
261 280
262 EXPECT_EQ(true, clipboard.IsFormatAvailable( 281 EXPECT_TRUE(clipboard.IsFormatAvailable(
263 Clipboard::GetWebKitSmartPasteFormatType())); 282 Clipboard::GetWebKitSmartPasteFormatType()));
264 } 283 }
265 284
266 TEST_F(ClipboardTest, BitmapTest) { 285 TEST_F(ClipboardTest, BitmapTest) {
267 unsigned int fake_bitmap[] = { 286 unsigned int fake_bitmap[] = {
268 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89, 287 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89,
269 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568, 288 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568,
270 0x91E9F63A, 0xC31EA14F, 0x69AB32DF, 0x643A3FD1, 289 0x91E9F63A, 0xC31EA14F, 0x69AB32DF, 0x643A3FD1,
271 }; 290 };
272 291
273 Clipboard clipboard; 292 Clipboard clipboard;
274 293
275 { 294 {
276 ScopedClipboardWriter clipboard_writer(&clipboard); 295 ScopedClipboardWriter clipboard_writer(&clipboard);
277 clipboard_writer.WriteBitmapFromPixels(fake_bitmap, gfx::Size(3, 4)); 296 clipboard_writer.WriteBitmapFromPixels(fake_bitmap, gfx::Size(3, 4));
278 } 297 }
279 298
280 EXPECT_EQ(true, clipboard.IsFormatAvailable( 299 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetBitmapFormatType()));
281 Clipboard::GetBitmapFormatType()));
282 } 300 }
283 #endif // defined(OS_WIN) 301 #endif // defined(OS_WIN)
OLDNEW
« no previous file with comments | « base/clipboard_linux.cc ('k') | base/clipboard_win.cc » ('j') | base/clipboard_win.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698