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

Side by Side Diff: ui/base/clipboard/clipboard_unittest.cc

Issue 558913003: Remove clipboard argument from ScopedClipboardWriter constructor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Don't leak a clipboard on Windows Created 6 years, 3 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 namespace ui { 43 namespace ui {
44 44
45 class ClipboardTest : public PlatformTest { 45 class ClipboardTest : public PlatformTest {
46 public: 46 public:
47 #if defined(USE_AURA) 47 #if defined(USE_AURA)
48 ClipboardTest() : event_source_(ui::PlatformEventSource::CreateDefault()) {} 48 ClipboardTest() : event_source_(ui::PlatformEventSource::CreateDefault()) {}
49 #else 49 #else
50 ClipboardTest() {} 50 ClipboardTest() {}
51 #endif 51 #endif
52 52
53 virtual ~ClipboardTest() {
54 ui::Clipboard::DestroyClipboardForCurrentThread();
55 }
56
53 static void WriteObjectsToClipboard(ui::Clipboard* clipboard, 57 static void WriteObjectsToClipboard(ui::Clipboard* clipboard,
54 const Clipboard::ObjectMap& objects) { 58 const Clipboard::ObjectMap& objects) {
55 clipboard->WriteObjects(ui::CLIPBOARD_TYPE_COPY_PASTE, objects); 59 clipboard->WriteObjects(ui::CLIPBOARD_TYPE_COPY_PASTE, objects);
56 } 60 }
57 61
58 protected: 62 protected:
59 Clipboard& clipboard() { return clipboard_; } 63 Clipboard& clipboard() { return *ui::Clipboard::GetForCurrentThread(); }
60 64
61 void WriteObjectsToClipboard(const Clipboard::ObjectMap& objects) { 65 void WriteObjectsToClipboard(const Clipboard::ObjectMap& objects) {
62 WriteObjectsToClipboard(&clipboard(), objects); 66 WriteObjectsToClipboard(&clipboard(), objects);
63 } 67 }
64 68
65 private: 69 private:
66 base::MessageLoopForUI message_loop_; 70 base::MessageLoopForUI message_loop_;
67 #if defined(USE_AURA) 71 #if defined(USE_AURA)
68 scoped_ptr<PlatformEventSource> event_source_; 72 scoped_ptr<PlatformEventSource> event_source_;
69 #endif 73 #endif
70 Clipboard clipboard_;
71 }; 74 };
72 75
73 namespace { 76 namespace {
74 77
75 bool MarkupMatches(const base::string16& expected_markup, 78 bool MarkupMatches(const base::string16& expected_markup,
76 const base::string16& actual_markup) { 79 const base::string16& actual_markup) {
77 return actual_markup.find(expected_markup) != base::string16::npos; 80 return actual_markup.find(expected_markup) != base::string16::npos;
78 } 81 }
79 82
80 } // namespace 83 } // namespace
81 84
82 TEST_F(ClipboardTest, ClearTest) { 85 TEST_F(ClipboardTest, ClearTest) {
83 { 86 {
84 ScopedClipboardWriter clipboard_writer(&clipboard(), 87 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
85 CLIPBOARD_TYPE_COPY_PASTE);
86 clipboard_writer.WriteText(ASCIIToUTF16("clear me")); 88 clipboard_writer.WriteText(ASCIIToUTF16("clear me"));
87 } 89 }
88 90
89 clipboard().Clear(CLIPBOARD_TYPE_COPY_PASTE); 91 clipboard().Clear(CLIPBOARD_TYPE_COPY_PASTE);
90 92
91 EXPECT_FALSE(clipboard().IsFormatAvailable( 93 EXPECT_FALSE(clipboard().IsFormatAvailable(
92 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); 94 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
93 EXPECT_FALSE(clipboard().IsFormatAvailable( 95 EXPECT_FALSE(clipboard().IsFormatAvailable(
94 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); 96 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
95 } 97 }
96 98
97 TEST_F(ClipboardTest, TextTest) { 99 TEST_F(ClipboardTest, TextTest) {
98 base::string16 text(ASCIIToUTF16("This is a base::string16!#$")), text_result; 100 base::string16 text(ASCIIToUTF16("This is a base::string16!#$")), text_result;
99 std::string ascii_text; 101 std::string ascii_text;
100 102
101 { 103 {
102 ScopedClipboardWriter clipboard_writer(&clipboard(), 104 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
103 CLIPBOARD_TYPE_COPY_PASTE);
104 clipboard_writer.WriteText(text); 105 clipboard_writer.WriteText(text);
105 } 106 }
106 107
107 EXPECT_TRUE(clipboard().IsFormatAvailable( 108 EXPECT_TRUE(clipboard().IsFormatAvailable(
108 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); 109 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
109 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetPlainTextFormatType(), 110 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetPlainTextFormatType(),
110 CLIPBOARD_TYPE_COPY_PASTE)); 111 CLIPBOARD_TYPE_COPY_PASTE));
111 clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result); 112 clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result);
112 113
113 EXPECT_EQ(text, text_result); 114 EXPECT_EQ(text, text_result);
114 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text); 115 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text);
115 EXPECT_EQ(UTF16ToUTF8(text), ascii_text); 116 EXPECT_EQ(UTF16ToUTF8(text), ascii_text);
116 } 117 }
117 118
118 TEST_F(ClipboardTest, HTMLTest) { 119 TEST_F(ClipboardTest, HTMLTest) {
119 base::string16 markup(ASCIIToUTF16("<string>Hi!</string>")), markup_result; 120 base::string16 markup(ASCIIToUTF16("<string>Hi!</string>")), markup_result;
120 base::string16 plain(ASCIIToUTF16("Hi!")), plain_result; 121 base::string16 plain(ASCIIToUTF16("Hi!")), plain_result;
121 std::string url("http://www.example.com/"), url_result; 122 std::string url("http://www.example.com/"), url_result;
122 123
123 { 124 {
124 ScopedClipboardWriter clipboard_writer(&clipboard(), 125 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
125 CLIPBOARD_TYPE_COPY_PASTE);
126 clipboard_writer.WriteText(plain); 126 clipboard_writer.WriteText(plain);
127 clipboard_writer.WriteHTML(markup, url); 127 clipboard_writer.WriteHTML(markup, url);
128 } 128 }
129 129
130 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(), 130 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(),
131 CLIPBOARD_TYPE_COPY_PASTE)); 131 CLIPBOARD_TYPE_COPY_PASTE));
132 uint32 ignored; 132 uint32 ignored;
133 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, &url_result, 133 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, &url_result,
134 &ignored, &ignored); 134 &ignored, &ignored);
135 EXPECT_PRED2(MarkupMatches, markup, markup_result); 135 EXPECT_PRED2(MarkupMatches, markup, markup_result);
136 #if defined(OS_WIN) 136 #if defined(OS_WIN)
137 // TODO(playmobil): It's not clear that non windows clipboards need to support 137 // TODO(playmobil): It's not clear that non windows clipboards need to support
138 // this. 138 // this.
139 EXPECT_EQ(url, url_result); 139 EXPECT_EQ(url, url_result);
140 #endif // defined(OS_WIN) 140 #endif // defined(OS_WIN)
141 } 141 }
142 142
143 TEST_F(ClipboardTest, RTFTest) { 143 TEST_F(ClipboardTest, RTFTest) {
144 std::string rtf = 144 std::string rtf =
145 "{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\n" 145 "{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\n"
146 "This is some {\\b bold} text.\\par\n" 146 "This is some {\\b bold} text.\\par\n"
147 "}"; 147 "}";
148 148
149 { 149 {
150 ScopedClipboardWriter clipboard_writer(&clipboard(), 150 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
151 CLIPBOARD_TYPE_COPY_PASTE);
152 clipboard_writer.WriteRTF(rtf); 151 clipboard_writer.WriteRTF(rtf);
153 } 152 }
154 153
155 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetRtfFormatType(), 154 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetRtfFormatType(),
156 CLIPBOARD_TYPE_COPY_PASTE)); 155 CLIPBOARD_TYPE_COPY_PASTE));
157 std::string result; 156 std::string result;
158 clipboard().ReadRTF(CLIPBOARD_TYPE_COPY_PASTE, &result); 157 clipboard().ReadRTF(CLIPBOARD_TYPE_COPY_PASTE, &result);
159 EXPECT_EQ(rtf, result); 158 EXPECT_EQ(rtf, result);
160 } 159 }
161 160
162 // TODO(dnicoara) Enable test once Ozone implements clipboard support: 161 // TODO(dnicoara) Enable test once Ozone implements clipboard support:
163 // crbug.com/361707 162 // crbug.com/361707
164 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && !defined(USE_OZONE) 163 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && !defined(USE_OZONE)
165 TEST_F(ClipboardTest, MultipleBufferTest) { 164 TEST_F(ClipboardTest, MultipleBufferTest) {
166 base::string16 text(ASCIIToUTF16("Standard")), text_result; 165 base::string16 text(ASCIIToUTF16("Standard")), text_result;
167 base::string16 markup(ASCIIToUTF16("<string>Selection</string>")); 166 base::string16 markup(ASCIIToUTF16("<string>Selection</string>"));
168 std::string url("http://www.example.com/"), url_result; 167 std::string url("http://www.example.com/"), url_result;
169 168
170 { 169 {
171 ScopedClipboardWriter clipboard_writer(&clipboard(), 170 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
172 CLIPBOARD_TYPE_COPY_PASTE);
173 clipboard_writer.WriteText(text); 171 clipboard_writer.WriteText(text);
174 } 172 }
175 173
176 { 174 {
177 ScopedClipboardWriter clipboard_writer(&clipboard(), 175 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_SELECTION);
178 CLIPBOARD_TYPE_SELECTION);
179 clipboard_writer.WriteHTML(markup, url); 176 clipboard_writer.WriteHTML(markup, url);
180 } 177 }
181 178
182 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetPlainTextFormatType(), 179 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetPlainTextFormatType(),
183 CLIPBOARD_TYPE_COPY_PASTE)); 180 CLIPBOARD_TYPE_COPY_PASTE));
184 EXPECT_FALSE(clipboard().IsFormatAvailable( 181 EXPECT_FALSE(clipboard().IsFormatAvailable(
185 Clipboard::GetPlainTextFormatType(), 182 Clipboard::GetPlainTextFormatType(),
186 CLIPBOARD_TYPE_SELECTION)); 183 CLIPBOARD_TYPE_SELECTION));
187 184
188 EXPECT_FALSE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(), 185 EXPECT_FALSE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(),
(...skipping 15 matching lines...) Expand all
204 } 201 }
205 #endif 202 #endif
206 203
207 TEST_F(ClipboardTest, TrickyHTMLTest) { 204 TEST_F(ClipboardTest, TrickyHTMLTest) {
208 base::string16 markup(ASCIIToUTF16("<em>Bye!<!--EndFragment --></em>")), 205 base::string16 markup(ASCIIToUTF16("<em>Bye!<!--EndFragment --></em>")),
209 markup_result; 206 markup_result;
210 std::string url, url_result; 207 std::string url, url_result;
211 base::string16 plain(ASCIIToUTF16("Bye!")), plain_result; 208 base::string16 plain(ASCIIToUTF16("Bye!")), plain_result;
212 209
213 { 210 {
214 ScopedClipboardWriter clipboard_writer(&clipboard(), 211 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
215 CLIPBOARD_TYPE_COPY_PASTE);
216 clipboard_writer.WriteText(plain); 212 clipboard_writer.WriteText(plain);
217 clipboard_writer.WriteHTML(markup, url); 213 clipboard_writer.WriteHTML(markup, url);
218 } 214 }
219 215
220 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(), 216 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(),
221 CLIPBOARD_TYPE_COPY_PASTE)); 217 CLIPBOARD_TYPE_COPY_PASTE));
222 uint32 ignored; 218 uint32 ignored;
223 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, &url_result, 219 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, &url_result,
224 &ignored, &ignored); 220 &ignored, &ignored);
225 EXPECT_PRED2(MarkupMatches, markup, markup_result); 221 EXPECT_PRED2(MarkupMatches, markup, markup_result);
226 #if defined(OS_WIN) 222 #if defined(OS_WIN)
227 // TODO(playmobil): It's not clear that non windows clipboards need to support 223 // TODO(playmobil): It's not clear that non windows clipboards need to support
228 // this. 224 // this.
229 EXPECT_EQ(url, url_result); 225 EXPECT_EQ(url, url_result);
230 #endif // defined(OS_WIN) 226 #endif // defined(OS_WIN)
231 } 227 }
232 228
233 #if defined(OS_WIN) 229 #if defined(OS_WIN)
234 TEST_F(ClipboardTest, UniodeHTMLTest) { 230 TEST_F(ClipboardTest, UniodeHTMLTest) {
235 base::string16 markup(UTF8ToUTF16("<div>A \xc3\xb8 \xe6\xb0\xb4</div>")), 231 base::string16 markup(UTF8ToUTF16("<div>A \xc3\xb8 \xe6\xb0\xb4</div>")),
236 markup_result; 232 markup_result;
237 std::string url, url_result; 233 std::string url, url_result;
238 234
239 { 235 {
240 ScopedClipboardWriter clipboard_writer(&clipboard(), 236 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
241 CLIPBOARD_TYPE_COPY_PASTE);
242 clipboard_writer.WriteHTML(markup, url); 237 clipboard_writer.WriteHTML(markup, url);
243 } 238 }
244 239
245 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(), 240 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(),
246 CLIPBOARD_TYPE_COPY_PASTE)); 241 CLIPBOARD_TYPE_COPY_PASTE));
247 uint32 fragment_start; 242 uint32 fragment_start;
248 uint32 fragment_end; 243 uint32 fragment_end;
249 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, &url_result, 244 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, &url_result,
250 &fragment_start, &fragment_end); 245 &fragment_start, &fragment_end);
251 EXPECT_PRED2(MarkupMatches, markup, markup_result); 246 EXPECT_PRED2(MarkupMatches, markup, markup_result);
252 EXPECT_EQ(url, url_result); 247 EXPECT_EQ(url, url_result);
253 // Make sure that fragment indices were adjusted when converting. 248 // Make sure that fragment indices were adjusted when converting.
254 EXPECT_EQ(36, fragment_start); 249 EXPECT_EQ(36, fragment_start);
255 EXPECT_EQ(52, fragment_end); 250 EXPECT_EQ(52, fragment_end);
256 } 251 }
257 #endif // defined(OS_WIN) 252 #endif // defined(OS_WIN)
258 253
259 // TODO(estade): Port the following test (decide what target we use for urls) 254 // TODO(estade): Port the following test (decide what target we use for urls)
260 #if !defined(OS_POSIX) || defined(OS_MACOSX) 255 #if !defined(OS_POSIX) || defined(OS_MACOSX)
261 TEST_F(ClipboardTest, BookmarkTest) { 256 TEST_F(ClipboardTest, BookmarkTest) {
262 base::string16 title(ASCIIToUTF16("The Example Company")), title_result; 257 base::string16 title(ASCIIToUTF16("The Example Company")), title_result;
263 std::string url("http://www.example.com/"), url_result; 258 std::string url("http://www.example.com/"), url_result;
264 259
265 { 260 {
266 ScopedClipboardWriter clipboard_writer(&clipboard(), 261 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
267 CLIPBOARD_TYPE_COPY_PASTE);
268 clipboard_writer.WriteBookmark(title, url); 262 clipboard_writer.WriteBookmark(title, url);
269 } 263 }
270 264
271 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetUrlWFormatType(), 265 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetUrlWFormatType(),
272 CLIPBOARD_TYPE_COPY_PASTE)); 266 CLIPBOARD_TYPE_COPY_PASTE));
273 clipboard().ReadBookmark(&title_result, &url_result); 267 clipboard().ReadBookmark(&title_result, &url_result);
274 EXPECT_EQ(title, title_result); 268 EXPECT_EQ(title, title_result);
275 EXPECT_EQ(url, url_result); 269 EXPECT_EQ(url, url_result);
276 } 270 }
277 #endif // defined(OS_WIN) 271 #endif // defined(OS_WIN)
278 272
279 TEST_F(ClipboardTest, MultiFormatTest) { 273 TEST_F(ClipboardTest, MultiFormatTest) {
280 base::string16 text(ASCIIToUTF16("Hi!")), text_result; 274 base::string16 text(ASCIIToUTF16("Hi!")), text_result;
281 base::string16 markup(ASCIIToUTF16("<strong>Hi!</string>")), markup_result; 275 base::string16 markup(ASCIIToUTF16("<strong>Hi!</string>")), markup_result;
282 std::string url("http://www.example.com/"), url_result; 276 std::string url("http://www.example.com/"), url_result;
283 std::string ascii_text; 277 std::string ascii_text;
284 278
285 { 279 {
286 ScopedClipboardWriter clipboard_writer(&clipboard(), 280 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
287 CLIPBOARD_TYPE_COPY_PASTE);
288 clipboard_writer.WriteHTML(markup, url); 281 clipboard_writer.WriteHTML(markup, url);
289 clipboard_writer.WriteText(text); 282 clipboard_writer.WriteText(text);
290 } 283 }
291 284
292 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(), 285 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(),
293 CLIPBOARD_TYPE_COPY_PASTE)); 286 CLIPBOARD_TYPE_COPY_PASTE));
294 EXPECT_TRUE(clipboard().IsFormatAvailable( 287 EXPECT_TRUE(clipboard().IsFormatAvailable(
295 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); 288 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
296 EXPECT_TRUE(clipboard().IsFormatAvailable( 289 EXPECT_TRUE(clipboard().IsFormatAvailable(
297 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); 290 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
298 uint32 ignored; 291 uint32 ignored;
299 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, &url_result, 292 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, &url_result,
300 &ignored, &ignored); 293 &ignored, &ignored);
301 EXPECT_PRED2(MarkupMatches, markup, markup_result); 294 EXPECT_PRED2(MarkupMatches, markup, markup_result);
302 #if defined(OS_WIN) 295 #if defined(OS_WIN)
303 // TODO(playmobil): It's not clear that non windows clipboards need to support 296 // TODO(playmobil): It's not clear that non windows clipboards need to support
304 // this. 297 // this.
305 EXPECT_EQ(url, url_result); 298 EXPECT_EQ(url, url_result);
306 #endif // defined(OS_WIN) 299 #endif // defined(OS_WIN)
307 clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result); 300 clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result);
308 EXPECT_EQ(text, text_result); 301 EXPECT_EQ(text, text_result);
309 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text); 302 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text);
310 EXPECT_EQ(UTF16ToUTF8(text), ascii_text); 303 EXPECT_EQ(UTF16ToUTF8(text), ascii_text);
311 } 304 }
312 305
313 TEST_F(ClipboardTest, URLTest) { 306 TEST_F(ClipboardTest, URLTest) {
314 base::string16 url(ASCIIToUTF16("http://www.google.com/")); 307 base::string16 url(ASCIIToUTF16("http://www.google.com/"));
315 308
316 { 309 {
317 ScopedClipboardWriter clipboard_writer(&clipboard(), 310 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
318 CLIPBOARD_TYPE_COPY_PASTE);
319 clipboard_writer.WriteURL(url); 311 clipboard_writer.WriteURL(url);
320 } 312 }
321 313
322 EXPECT_TRUE(clipboard().IsFormatAvailable( 314 EXPECT_TRUE(clipboard().IsFormatAvailable(
323 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); 315 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
324 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetPlainTextFormatType(), 316 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetPlainTextFormatType(),
325 CLIPBOARD_TYPE_COPY_PASTE)); 317 CLIPBOARD_TYPE_COPY_PASTE));
326 base::string16 text_result; 318 base::string16 text_result;
327 clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result); 319 clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result);
328 320
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 } 527 }
536 528
537 TEST_F(ClipboardTest, DataTest) { 529 TEST_F(ClipboardTest, DataTest) {
538 const ui::Clipboard::FormatType kFormat = 530 const ui::Clipboard::FormatType kFormat =
539 ui::Clipboard::GetFormatType("chromium/x-test-format"); 531 ui::Clipboard::GetFormatType("chromium/x-test-format");
540 std::string payload("test string"); 532 std::string payload("test string");
541 Pickle write_pickle; 533 Pickle write_pickle;
542 write_pickle.WriteString(payload); 534 write_pickle.WriteString(payload);
543 535
544 { 536 {
545 ScopedClipboardWriter clipboard_writer(&clipboard(), 537 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
546 CLIPBOARD_TYPE_COPY_PASTE);
547 clipboard_writer.WritePickledData(write_pickle, kFormat); 538 clipboard_writer.WritePickledData(write_pickle, kFormat);
548 } 539 }
549 540
550 ASSERT_TRUE(clipboard().IsFormatAvailable( 541 ASSERT_TRUE(clipboard().IsFormatAvailable(
551 kFormat, CLIPBOARD_TYPE_COPY_PASTE)); 542 kFormat, CLIPBOARD_TYPE_COPY_PASTE));
552 std::string output; 543 std::string output;
553 clipboard().ReadData(kFormat, &output); 544 clipboard().ReadData(kFormat, &output);
554 ASSERT_FALSE(output.empty()); 545 ASSERT_FALSE(output.empty());
555 546
556 Pickle read_pickle(output.data(), output.size()); 547 Pickle read_pickle(output.data(), output.size());
(...skipping 10 matching lines...) Expand all
567 Pickle write_pickle1; 558 Pickle write_pickle1;
568 write_pickle1.WriteString(payload1); 559 write_pickle1.WriteString(payload1);
569 560
570 const ui::Clipboard::FormatType kFormat2 = 561 const ui::Clipboard::FormatType kFormat2 =
571 ui::Clipboard::GetFormatType("chromium/x-test-format2"); 562 ui::Clipboard::GetFormatType("chromium/x-test-format2");
572 std::string payload2("test string2"); 563 std::string payload2("test string2");
573 Pickle write_pickle2; 564 Pickle write_pickle2;
574 write_pickle2.WriteString(payload2); 565 write_pickle2.WriteString(payload2);
575 566
576 { 567 {
577 ScopedClipboardWriter clipboard_writer(&clipboard(), 568 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
578 CLIPBOARD_TYPE_COPY_PASTE);
579 clipboard_writer.WritePickledData(write_pickle1, kFormat1); 569 clipboard_writer.WritePickledData(write_pickle1, kFormat1);
580 // overwrite the previous pickle for fun 570 // overwrite the previous pickle for fun
581 clipboard_writer.WritePickledData(write_pickle2, kFormat2); 571 clipboard_writer.WritePickledData(write_pickle2, kFormat2);
582 } 572 }
583 573
584 ASSERT_TRUE(clipboard().IsFormatAvailable( 574 ASSERT_TRUE(clipboard().IsFormatAvailable(
585 kFormat2, CLIPBOARD_TYPE_COPY_PASTE)); 575 kFormat2, CLIPBOARD_TYPE_COPY_PASTE));
586 576
587 // Check string 2. 577 // Check string 2.
588 std::string output2; 578 std::string output2;
589 clipboard().ReadData(kFormat2, &output2); 579 clipboard().ReadData(kFormat2, &output2);
590 ASSERT_FALSE(output2.empty()); 580 ASSERT_FALSE(output2.empty());
591 581
592 Pickle read_pickle2(output2.data(), output2.size()); 582 Pickle read_pickle2(output2.data(), output2.size());
593 PickleIterator iter2(read_pickle2); 583 PickleIterator iter2(read_pickle2);
594 std::string unpickled_string2; 584 std::string unpickled_string2;
595 ASSERT_TRUE(read_pickle2.ReadString(&iter2, &unpickled_string2)); 585 ASSERT_TRUE(read_pickle2.ReadString(&iter2, &unpickled_string2));
596 EXPECT_EQ(payload2, unpickled_string2); 586 EXPECT_EQ(payload2, unpickled_string2);
597 587
598 { 588 {
599 ScopedClipboardWriter clipboard_writer(&clipboard(), 589 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
600 CLIPBOARD_TYPE_COPY_PASTE);
601 clipboard_writer.WritePickledData(write_pickle2, kFormat2); 590 clipboard_writer.WritePickledData(write_pickle2, kFormat2);
602 // overwrite the previous pickle for fun 591 // overwrite the previous pickle for fun
603 clipboard_writer.WritePickledData(write_pickle1, kFormat1); 592 clipboard_writer.WritePickledData(write_pickle1, kFormat1);
604 } 593 }
605 594
606 ASSERT_TRUE(clipboard().IsFormatAvailable( 595 ASSERT_TRUE(clipboard().IsFormatAvailable(
607 kFormat1, CLIPBOARD_TYPE_COPY_PASTE)); 596 kFormat1, CLIPBOARD_TYPE_COPY_PASTE));
608 597
609 // Check string 1. 598 // Check string 1.
610 std::string output1; 599 std::string output1;
(...skipping 11 matching lines...) Expand all
622 TEST_F(ClipboardTest, HyperlinkTest) { 611 TEST_F(ClipboardTest, HyperlinkTest) {
623 const std::string kTitle("The <Example> Company's \"home page\""); 612 const std::string kTitle("The <Example> Company's \"home page\"");
624 const std::string kUrl("http://www.example.com?x=3&lt=3#\"'<>"); 613 const std::string kUrl("http://www.example.com?x=3&lt=3#\"'<>");
625 const std::string kExpectedHtml( 614 const std::string kExpectedHtml(
626 "<a href=\"http://www.example.com?x=3&amp;lt=3#&quot;&#39;&lt;&gt;\">" 615 "<a href=\"http://www.example.com?x=3&amp;lt=3#&quot;&#39;&lt;&gt;\">"
627 "The &lt;Example&gt; Company&#39;s &quot;home page&quot;</a>"); 616 "The &lt;Example&gt; Company&#39;s &quot;home page&quot;</a>");
628 617
629 std::string url_result; 618 std::string url_result;
630 base::string16 html_result; 619 base::string16 html_result;
631 { 620 {
632 ScopedClipboardWriter clipboard_writer(&clipboard(), 621 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
633 CLIPBOARD_TYPE_COPY_PASTE);
634 clipboard_writer.WriteHyperlink(ASCIIToUTF16(kTitle), kUrl); 622 clipboard_writer.WriteHyperlink(ASCIIToUTF16(kTitle), kUrl);
635 } 623 }
636 624
637 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(), 625 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(),
638 CLIPBOARD_TYPE_COPY_PASTE)); 626 CLIPBOARD_TYPE_COPY_PASTE));
639 uint32 ignored; 627 uint32 ignored;
640 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &html_result, &url_result, 628 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &html_result, &url_result,
641 &ignored, &ignored); 629 &ignored, &ignored);
642 EXPECT_PRED2(MarkupMatches, ASCIIToUTF16(kExpectedHtml), html_result); 630 EXPECT_PRED2(MarkupMatches, ASCIIToUTF16(kExpectedHtml), html_result);
643 } 631 }
644 #endif 632 #endif
645 633
646 #if defined(OS_WIN) // Windows only tests. 634 #if defined(OS_WIN) // Windows only tests.
647 TEST_F(ClipboardTest, WebSmartPasteTest) { 635 TEST_F(ClipboardTest, WebSmartPasteTest) {
648 { 636 {
649 ScopedClipboardWriter clipboard_writer(&clipboard(), 637 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
650 CLIPBOARD_TYPE_COPY_PASTE);
651 clipboard_writer.WriteWebSmartPaste(); 638 clipboard_writer.WriteWebSmartPaste();
652 } 639 }
653 640
654 EXPECT_TRUE(clipboard().IsFormatAvailable( 641 EXPECT_TRUE(clipboard().IsFormatAvailable(
655 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); 642 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
656 } 643 }
657 644
658 void HtmlTestHelper(const std::string& cf_html, 645 void HtmlTestHelper(const std::string& cf_html,
659 const std::string& expected_html) { 646 const std::string& expected_html) {
660 std::string html; 647 std::string html;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
694 "<p>Foo</p>\r\n" 681 "<p>Foo</p>\r\n"
695 "</body>\r\n" 682 "</body>\r\n"
696 "</html>\r\n\r\n", 683 "</html>\r\n\r\n",
697 "<p>Foo</p>"); 684 "<p>Foo</p>");
698 } 685 }
699 #endif // defined(OS_WIN) 686 #endif // defined(OS_WIN)
700 687
701 // Test writing all formats we have simultaneously. 688 // Test writing all formats we have simultaneously.
702 TEST_F(ClipboardTest, WriteEverything) { 689 TEST_F(ClipboardTest, WriteEverything) {
703 { 690 {
704 ScopedClipboardWriter writer(&clipboard(), CLIPBOARD_TYPE_COPY_PASTE); 691 ScopedClipboardWriter writer(CLIPBOARD_TYPE_COPY_PASTE);
705 writer.WriteText(UTF8ToUTF16("foo")); 692 writer.WriteText(UTF8ToUTF16("foo"));
706 writer.WriteURL(UTF8ToUTF16("foo")); 693 writer.WriteURL(UTF8ToUTF16("foo"));
707 writer.WriteHTML(UTF8ToUTF16("foo"), "bar"); 694 writer.WriteHTML(UTF8ToUTF16("foo"), "bar");
708 writer.WriteBookmark(UTF8ToUTF16("foo"), "bar"); 695 writer.WriteBookmark(UTF8ToUTF16("foo"), "bar");
709 writer.WriteHyperlink(ASCIIToUTF16("foo"), "bar"); 696 writer.WriteHyperlink(ASCIIToUTF16("foo"), "bar");
710 writer.WriteWebSmartPaste(); 697 writer.WriteWebSmartPaste();
711 // Left out: WriteFile, WriteFiles, WriteBitmapFromPixels, WritePickledData. 698 // Left out: WriteFile, WriteFiles, WriteBitmapFromPixels, WritePickledData.
712 } 699 }
713 700
714 // Passes if we don't crash. 701 // Passes if we don't crash.
715 } 702 }
716 703
717 // TODO(dcheng): Fix this test for Android. It's rather involved, since the 704 // TODO(dcheng): Fix this test for Android. It's rather involved, since the
718 // clipboard change listener is posted to the Java message loop, and spinning 705 // clipboard change listener is posted to the Java message loop, and spinning
719 // that loop from C++ to trigger the callback in the test requires a non-trivial 706 // that loop from C++ to trigger the callback in the test requires a non-trivial
720 // amount of additional work. 707 // amount of additional work.
721 #if !defined(OS_ANDROID) 708 #if !defined(OS_ANDROID)
722 // Simple test that the sequence number appears to change when the clipboard is 709 // Simple test that the sequence number appears to change when the clipboard is
723 // written to. 710 // written to.
724 // TODO(dcheng): Add a version to test CLIPBOARD_TYPE_SELECTION. 711 // TODO(dcheng): Add a version to test CLIPBOARD_TYPE_SELECTION.
725 TEST_F(ClipboardTest, GetSequenceNumber) { 712 TEST_F(ClipboardTest, GetSequenceNumber) {
726 const uint64 first_sequence_number = 713 const uint64 first_sequence_number =
727 clipboard().GetSequenceNumber(CLIPBOARD_TYPE_COPY_PASTE); 714 clipboard().GetSequenceNumber(CLIPBOARD_TYPE_COPY_PASTE);
728 715
729 { 716 {
730 ScopedClipboardWriter writer(&clipboard(), CLIPBOARD_TYPE_COPY_PASTE); 717 ScopedClipboardWriter writer(CLIPBOARD_TYPE_COPY_PASTE);
731 writer.WriteText(UTF8ToUTF16("World")); 718 writer.WriteText(UTF8ToUTF16("World"));
732 } 719 }
733 720
734 // On some platforms, the sequence number is updated by a UI callback so pump 721 // On some platforms, the sequence number is updated by a UI callback so pump
735 // the message loop to make sure we get the notification. 722 // the message loop to make sure we get the notification.
736 base::RunLoop().RunUntilIdle(); 723 base::RunLoop().RunUntilIdle();
737 724
738 const uint64 second_sequence_number = 725 const uint64 second_sequence_number =
739 clipboard().GetSequenceNumber(CLIPBOARD_TYPE_COPY_PASTE); 726 clipboard().GetSequenceNumber(CLIPBOARD_TYPE_COPY_PASTE);
740 727
741 EXPECT_NE(first_sequence_number, second_sequence_number); 728 EXPECT_NE(first_sequence_number, second_sequence_number);
742 } 729 }
743 #endif 730 #endif
744 731
745 #if defined(OS_ANDROID) 732 #if defined(OS_ANDROID)
746 733
747 // Test that if another application writes some text to the pasteboard the 734 // Test that if another application writes some text to the pasteboard the
748 // clipboard properly invalidates other types. 735 // clipboard properly invalidates other types.
749 TEST_F(ClipboardTest, InternalClipboardInvalidation) { 736 TEST_F(ClipboardTest, InternalClipboardInvalidation) {
750 // Write a Webkit smart paste tag to our clipboard. 737 // Write a Webkit smart paste tag to our clipboard.
751 { 738 {
752 ScopedClipboardWriter clipboard_writer(&clipboard(), 739 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
753 CLIPBOARD_TYPE_COPY_PASTE);
754 clipboard_writer.WriteWebSmartPaste(); 740 clipboard_writer.WriteWebSmartPaste();
755 } 741 }
756 EXPECT_TRUE(clipboard().IsFormatAvailable( 742 EXPECT_TRUE(clipboard().IsFormatAvailable(
757 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); 743 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
758 744
759 // 745 //
760 // Simulate that another application copied something in the Clipboard 746 // Simulate that another application copied something in the Clipboard
761 // 747 //
762 std::string new_value("Some text copied by some other app"); 748 std::string new_value("Some text copied by some other app");
763 using base::android::ConvertUTF8ToJavaString; 749 using base::android::ConvertUTF8ToJavaString;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
805 EXPECT_TRUE(clipboard().IsFormatAvailable( 791 EXPECT_TRUE(clipboard().IsFormatAvailable(
806 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); 792 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
807 793
808 // Make sure the text is what we inserted while simulating the other app 794 // Make sure the text is what we inserted while simulating the other app
809 std::string contents; 795 std::string contents;
810 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &contents); 796 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &contents);
811 EXPECT_EQ(contents, new_value); 797 EXPECT_EQ(contents, new_value);
812 } 798 }
813 #endif 799 #endif
814 } // namespace ui 800 } // namespace ui
OLDNEW
« no previous file with comments | « content/renderer/scoped_clipboard_writer_glue.cc ('k') | ui/base/clipboard/scoped_clipboard_writer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698