OLD | NEW |
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/platform_test.h" | 9 #include "base/platform_test.h" |
| 10 #include "base/scoped_clipboard_writer.h" |
10 #include "base/string_util.h" | 11 #include "base/string_util.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 typedef PlatformTest ClipboardTest; | 14 typedef PlatformTest ClipboardTest; |
14 | 15 |
15 TEST_F(ClipboardTest, ClearTest) { | 16 TEST_F(ClipboardTest, ClearTest) { |
16 Clipboard clipboard; | 17 Clipboard clipboard; |
17 | 18 |
18 clipboard.Clear(); | 19 { |
19 clipboard.WriteText(L"erase me"); | 20 ScopedClipboardWriter scw(&clipboard); |
20 clipboard.Clear(); | 21 scw.WriteText(std::wstring(L"clear me")); |
21 EXPECT_EQ(false, clipboard.IsFormatAvailable( | 22 } |
| 23 |
| 24 { |
| 25 ScopedClipboardWriter scw(&clipboard); |
| 26 scw.WriteHTML(std::wstring(L"<b>broom</b>"), ""); |
| 27 } |
| 28 |
| 29 EXPECT_FALSE(clipboard.IsFormatAvailable( |
| 30 Clipboard::GetPlainTextWFormatType())); |
| 31 EXPECT_FALSE(clipboard.IsFormatAvailable( |
22 Clipboard::GetPlainTextFormatType())); | 32 Clipboard::GetPlainTextFormatType())); |
23 EXPECT_EQ(false, clipboard.IsFormatAvailable( | |
24 Clipboard::GetHtmlFormatType())); | |
25 } | 33 } |
26 | 34 |
27 TEST_F(ClipboardTest, TextTest) { | 35 TEST_F(ClipboardTest, TextTest) { |
28 Clipboard clipboard; | 36 Clipboard clipboard; |
29 | 37 |
30 std::wstring text(L"This is a wstring!#$"), text_result; | 38 std::wstring text(L"This is a wstring!#$"), text_result; |
31 std::string ascii_text; | 39 std::string ascii_text; |
32 | 40 |
33 clipboard.Clear(); | 41 { |
34 clipboard.WriteText(text); | 42 ScopedClipboardWriter scw(&clipboard); |
35 EXPECT_EQ(true, clipboard.IsFormatAvailable( | 43 scw.WriteText(text); |
| 44 } |
| 45 |
| 46 EXPECT_TRUE(clipboard.IsFormatAvailable( |
36 Clipboard::GetPlainTextWFormatType())); | 47 Clipboard::GetPlainTextWFormatType())); |
37 EXPECT_EQ(true, clipboard.IsFormatAvailable( | 48 EXPECT_TRUE(clipboard.IsFormatAvailable( |
38 Clipboard::GetPlainTextFormatType())); | 49 Clipboard::GetPlainTextFormatType())); |
39 clipboard.ReadText(&text_result); | 50 clipboard.ReadText(&text_result); |
40 EXPECT_EQ(text, text_result); | 51 EXPECT_EQ(text, text_result); |
41 clipboard.ReadAsciiText(&ascii_text); | 52 clipboard.ReadAsciiText(&ascii_text); |
42 EXPECT_EQ(WideToUTF8(text), ascii_text); | 53 EXPECT_EQ(WideToUTF8(text), ascii_text); |
43 } | 54 } |
44 | 55 |
45 TEST_F(ClipboardTest, OverwriteTest) { | |
46 Clipboard clipboard; | |
47 | |
48 std::wstring text1(L"first string"), text2(L"second string"), text_result; | |
49 | |
50 clipboard.Clear(); | |
51 clipboard.WriteText(text1); | |
52 clipboard.WriteText(text2); | |
53 | |
54 EXPECT_TRUE(clipboard.IsFormatAvailable( | |
55 Clipboard::GetPlainTextWFormatType())); | |
56 clipboard.ReadText(&text_result); | |
57 EXPECT_EQ(text2, text_result); | |
58 } | |
59 | |
60 TEST_F(ClipboardTest, HTMLTest) { | 56 TEST_F(ClipboardTest, HTMLTest) { |
61 Clipboard clipboard; | 57 Clipboard clipboard; |
62 | 58 |
63 std::wstring markup(L"<strong>Hi!</string>"), markup_result; | 59 std::wstring markup(L"<string>Hi!</string>"), markup_result; |
64 std::string url("http://www.example.com/"), url_result; | 60 std::string url("http://www.example.com/"), url_result; |
65 | 61 |
66 clipboard.Clear(); | 62 { |
67 clipboard.WriteHTML(markup, url); | 63 ScopedClipboardWriter scw(&clipboard); |
| 64 scw.WriteHTML(markup, url); |
| 65 } |
| 66 |
68 EXPECT_EQ(true, clipboard.IsFormatAvailable( | 67 EXPECT_EQ(true, clipboard.IsFormatAvailable( |
69 Clipboard::GetHtmlFormatType())); | 68 Clipboard::GetHtmlFormatType())); |
70 clipboard.ReadHTML(&markup_result, &url_result); | 69 clipboard.ReadHTML(&markup_result, &url_result); |
71 EXPECT_EQ(markup, markup_result); | 70 EXPECT_EQ(markup, markup_result); |
72 #if defined(OS_WIN) | 71 #if defined(OS_WIN) |
73 // TODO(playmobil): It's not clear that non windows clipboards need to support | 72 // TODO(playmobil): It's not clear that non windows clipboards need to support |
74 // this. | 73 // this. |
75 EXPECT_EQ(url, url_result); | 74 EXPECT_EQ(url, url_result); |
76 #endif | 75 #endif |
77 } | 76 } |
78 | 77 |
79 TEST_F(ClipboardTest, TrickyHTMLTest) { | 78 TEST_F(ClipboardTest, TrickyHTMLTest) { |
80 Clipboard clipboard; | 79 Clipboard clipboard; |
81 | 80 |
82 std::wstring markup(L"<em>Bye!<!--EndFragment --></em>"), markup_result; | 81 std::wstring markup(L"<em>Bye!<!--EndFragment --></em>"), markup_result; |
83 std::string url, url_result; | 82 std::string url, url_result; |
84 | 83 |
85 clipboard.Clear(); | 84 { |
86 clipboard.WriteHTML(markup, url); | 85 ScopedClipboardWriter scw(&clipboard); |
| 86 scw.WriteHTML(markup, url); |
| 87 } |
| 88 |
87 EXPECT_EQ(true, clipboard.IsFormatAvailable( | 89 EXPECT_EQ(true, clipboard.IsFormatAvailable( |
88 Clipboard::GetHtmlFormatType())); | 90 Clipboard::GetHtmlFormatType())); |
89 clipboard.ReadHTML(&markup_result, &url_result); | 91 clipboard.ReadHTML(&markup_result, &url_result); |
90 EXPECT_EQ(markup, markup_result); | 92 EXPECT_EQ(markup, markup_result); |
91 #if defined(OS_WIN) | 93 #if defined(OS_WIN) |
92 // TODO(playmobil): It's not clear that non windows clipboards need to support | 94 // TODO(playmobil): It's not clear that non windows clipboards need to support |
93 // this. | 95 // this. |
94 EXPECT_EQ(url, url_result); | 96 EXPECT_EQ(url, url_result); |
95 #endif | 97 #endif |
96 } | 98 } |
97 | 99 |
98 // TODO(estade): Port the following test (decide what target we use for urls) | 100 // TODO(estade): Port the following test (decide what target we use for urls) |
99 #if !defined(OS_LINUX) | 101 #if !defined(OS_LINUX) |
100 TEST_F(ClipboardTest, BookmarkTest) { | 102 TEST_F(ClipboardTest, BookmarkTest) { |
101 Clipboard clipboard; | 103 Clipboard clipboard; |
102 | 104 |
103 std::wstring title(L"The Example Company"), title_result; | 105 std::wstring title(L"The Example Company"), title_result; |
104 std::string url("http://www.example.com/"), url_result; | 106 std::string url("http://www.example.com/"), url_result; |
105 | 107 |
106 clipboard.Clear(); | 108 { |
107 clipboard.WriteBookmark(title, url); | 109 ScopedClipboardWriter scw(&clipboard); |
| 110 scw.WriteBookmark(title, url); |
| 111 } |
| 112 |
108 EXPECT_EQ(true, | 113 EXPECT_EQ(true, |
109 clipboard.IsFormatAvailable(Clipboard::GetUrlWFormatType())); | 114 clipboard.IsFormatAvailable(Clipboard::GetUrlWFormatType())); |
110 clipboard.ReadBookmark(&title_result, &url_result); | 115 clipboard.ReadBookmark(&title_result, &url_result); |
111 EXPECT_EQ(title, title_result); | 116 EXPECT_EQ(title, title_result); |
112 EXPECT_EQ(url, url_result); | 117 EXPECT_EQ(url, url_result); |
113 } | 118 } |
114 #endif | 119 #endif |
115 | 120 |
116 TEST_F(ClipboardTest, MultiFormatTest) { | 121 TEST_F(ClipboardTest, MultiFormatTest) { |
117 Clipboard clipboard; | 122 Clipboard clipboard; |
118 | 123 |
119 std::wstring text(L"Hi!"), text_result; | 124 std::wstring text(L"Hi!"), text_result; |
120 std::wstring markup(L"<strong>Hi!</string>"), markup_result; | 125 std::wstring markup(L"<strong>Hi!</string>"), markup_result; |
121 std::string url("http://www.example.com/"), url_result; | 126 std::string url("http://www.example.com/"), url_result; |
122 std::string ascii_text; | 127 std::string ascii_text; |
123 | 128 |
124 clipboard.Clear(); | 129 { |
125 clipboard.WriteHTML(markup, url); | 130 ScopedClipboardWriter scw(&clipboard); |
126 clipboard.WriteText(text); | 131 scw.WriteHTML(markup, url); |
| 132 scw.WriteText(text); |
| 133 } |
| 134 |
127 EXPECT_EQ(true, | 135 EXPECT_EQ(true, |
128 clipboard.IsFormatAvailable(Clipboard::GetHtmlFormatType())); | 136 clipboard.IsFormatAvailable(Clipboard::GetHtmlFormatType())); |
129 EXPECT_EQ(true, clipboard.IsFormatAvailable( | 137 EXPECT_EQ(true, clipboard.IsFormatAvailable( |
130 Clipboard::GetPlainTextWFormatType())); | 138 Clipboard::GetPlainTextWFormatType())); |
131 EXPECT_EQ(true, clipboard.IsFormatAvailable( | 139 EXPECT_EQ(true, clipboard.IsFormatAvailable( |
132 Clipboard::GetPlainTextFormatType())); | 140 Clipboard::GetPlainTextFormatType())); |
133 clipboard.ReadHTML(&markup_result, &url_result); | 141 clipboard.ReadHTML(&markup_result, &url_result); |
134 EXPECT_EQ(markup, markup_result); | 142 EXPECT_EQ(markup, markup_result); |
135 #if defined(OS_WIN) | 143 #if defined(OS_WIN) |
136 // TODO(playmobil): It's not clear that non windows clipboards need to support | 144 // TODO(playmobil): It's not clear that non windows clipboards need to support |
137 // this. | 145 // this. |
138 EXPECT_EQ(url, url_result); | 146 EXPECT_EQ(url, url_result); |
139 #endif | 147 #endif |
140 clipboard.ReadText(&text_result); | 148 clipboard.ReadText(&text_result); |
141 EXPECT_EQ(text, text_result); | 149 EXPECT_EQ(text, text_result); |
142 clipboard.ReadAsciiText(&ascii_text); | 150 clipboard.ReadAsciiText(&ascii_text); |
143 EXPECT_EQ(WideToUTF8(text), ascii_text); | 151 EXPECT_EQ(WideToUTF8(text), ascii_text); |
144 } | 152 } |
145 | 153 |
146 // TODO(estade): Port the following tests (decide what targets we use for files) | 154 // TODO(estade): Port the following tests (decide what targets we use for files) |
147 #if !defined(OS_LINUX) | 155 #if !defined(OS_LINUX) |
148 // Files for this test don't actually need to exist on the file system, just | 156 // Files for this test don't actually need to exist on the file system, just |
149 // don't try to use a non-existent file you've retrieved from the clipboard. | 157 // don't try to use a non-existent file you've retrieved from the clipboard. |
150 TEST_F(ClipboardTest, FileTest) { | 158 TEST_F(ClipboardTest, FileTest) { |
151 Clipboard clipboard; | 159 Clipboard clipboard; |
152 clipboard.Clear(); | |
153 #if defined(OS_WIN) | 160 #if defined(OS_WIN) |
154 std::wstring file = L"C:\\Downloads\\My Downloads\\A Special File.txt"; | 161 std::wstring file = L"C:\\Downloads\\My Downloads\\A Special File.txt"; |
155 #else | 162 #else |
156 // OS X will print a warning message if we stick a non-existant file on the | 163 // OS X will print a warning message if we stick a non-existant file on the |
157 // clipboard. | 164 // clipboard. |
158 std::wstring file = L"/usr/bin/make"; | 165 std::wstring file = L"/usr/bin/make"; |
159 #endif | 166 #endif |
160 clipboard.WriteFile(file); | 167 |
| 168 { |
| 169 ScopedClipboardWriter scw(&clipboard); |
| 170 scw.WriteFile(file); |
| 171 } |
| 172 |
161 std::wstring out_file; | 173 std::wstring out_file; |
162 clipboard.ReadFile(&out_file); | 174 clipboard.ReadFile(&out_file); |
163 EXPECT_EQ(file, out_file); | 175 EXPECT_EQ(file, out_file); |
164 } | 176 } |
165 | 177 |
166 TEST_F(ClipboardTest, MultipleFilesTest) { | 178 TEST_F(ClipboardTest, MultipleFilesTest) { |
167 Clipboard clipboard; | 179 Clipboard clipboard; |
168 clipboard.Clear(); | 180 |
169 | |
170 #if defined(OS_WIN) | 181 #if defined(OS_WIN) |
171 std::wstring file1 = L"C:\\Downloads\\My Downloads\\File 1.exe"; | 182 std::wstring file1 = L"C:\\Downloads\\My Downloads\\File 1.exe"; |
172 std::wstring file2 = L"C:\\Downloads\\My Downloads\\File 2.pdf"; | 183 std::wstring file2 = L"C:\\Downloads\\My Downloads\\File 2.pdf"; |
173 std::wstring file3 = L"C:\\Downloads\\My Downloads\\File 3.doc"; | 184 std::wstring file3 = L"C:\\Downloads\\My Downloads\\File 3.doc"; |
174 #elif defined(OS_MACOSX) | 185 #elif defined(OS_MACOSX) |
175 // OS X will print a warning message if we stick a non-existant file on the | 186 // OS X will print a warning message if we stick a non-existant file on the |
176 // clipboard. | 187 // clipboard. |
177 std::wstring file1 = L"/usr/bin/make"; | 188 std::wstring file1 = L"/usr/bin/make"; |
178 std::wstring file2 = L"/usr/bin/man"; | 189 std::wstring file2 = L"/usr/bin/man"; |
179 std::wstring file3 = L"/usr/bin/perl"; | 190 std::wstring file3 = L"/usr/bin/perl"; |
180 #endif | 191 #endif |
181 std::vector<std::wstring> files; | 192 std::vector<std::wstring> files; |
182 files.push_back(file1); | 193 files.push_back(file1); |
183 files.push_back(file2); | 194 files.push_back(file2); |
184 files.push_back(file3); | 195 files.push_back(file3); |
185 clipboard.WriteFiles(files); | 196 |
| 197 { |
| 198 ScopedClipboardWriter scw(&clipboard); |
| 199 scw.WriteFiles(files); |
| 200 } |
186 | 201 |
187 std::vector<std::wstring> out_files; | 202 std::vector<std::wstring> out_files; |
188 clipboard.ReadFiles(&out_files); | 203 clipboard.ReadFiles(&out_files); |
189 | 204 |
190 EXPECT_EQ(files.size(), out_files.size()); | 205 EXPECT_EQ(files.size(), out_files.size()); |
191 for (size_t i = 0; i < out_files.size(); ++i) | 206 for (size_t i = 0; i < out_files.size(); ++i) |
192 EXPECT_EQ(files[i], out_files[i]); | 207 EXPECT_EQ(files[i], out_files[i]); |
193 } | 208 } |
194 #endif // !defined(OS_LINUX) | 209 #endif // !defined(OS_LINUX) |
195 | 210 |
196 #if defined(OS_WIN) // Windows only tests. | 211 #if defined(OS_WIN) // Windows only tests. |
197 TEST_F(ClipboardTest, HyperlinkTest) { | 212 TEST_F(ClipboardTest, HyperlinkTest) { |
198 Clipboard clipboard; | 213 Clipboard clipboard; |
199 | 214 |
200 std::wstring title(L"The Example Company"), title_result; | 215 std::wstring title(L"The Example Company"), title_result; |
201 std::string url("http://www.example.com/"), url_result; | 216 std::string url("http://www.example.com/"), url_result; |
202 std::wstring html(L"<a href=\"http://www.example.com/\">" | 217 std::wstring html(L"<a href=\"http://www.example.com/\">" |
203 L"The Example Company</a>"), html_result; | 218 L"The Example Company</a>"), html_result; |
204 | 219 |
205 clipboard.Clear(); | 220 { |
206 clipboard.WriteHyperlink(title, url); | 221 ScopedClipboardWriter scw(&clipboard); |
| 222 scw.WriteHyperlink(title, url); |
| 223 } |
| 224 |
207 EXPECT_EQ(true, | 225 EXPECT_EQ(true, |
208 clipboard.IsFormatAvailable(Clipboard::GetUrlWFormatType())); | 226 clipboard.IsFormatAvailable(Clipboard::GetUrlWFormatType())); |
209 EXPECT_EQ(true, | 227 EXPECT_EQ(true, |
210 clipboard.IsFormatAvailable(Clipboard::GetHtmlFormatType())); | 228 clipboard.IsFormatAvailable(Clipboard::GetHtmlFormatType())); |
211 clipboard.ReadBookmark(&title_result, &url_result); | 229 clipboard.ReadBookmark(&title_result, &url_result); |
212 EXPECT_EQ(title, title_result); | 230 EXPECT_EQ(title, title_result); |
213 EXPECT_EQ(url, url_result); | 231 EXPECT_EQ(url, url_result); |
214 clipboard.ReadHTML(&html_result, &url_result); | 232 clipboard.ReadHTML(&html_result, &url_result); |
215 EXPECT_EQ(html, html_result); | 233 EXPECT_EQ(html, html_result); |
216 //XXX EXPECT_FALSE(url_result.is_valid()); | 234 //XXX EXPECT_FALSE(url_result.is_valid()); |
217 } | 235 } |
218 | 236 |
219 TEST_F(ClipboardTest, WebSmartPasteTest) { | 237 TEST_F(ClipboardTest, WebSmartPasteTest) { |
220 Clipboard clipboard; | 238 Clipboard clipboard; |
221 | 239 |
222 clipboard.Clear(); | 240 { |
223 clipboard.WriteWebSmartPaste(); | 241 ScopedClipboardWriter scw(&clipboard); |
| 242 scw.WriteWebSmartPaste(); |
| 243 } |
| 244 |
224 EXPECT_EQ(true, clipboard.IsFormatAvailable( | 245 EXPECT_EQ(true, clipboard.IsFormatAvailable( |
225 Clipboard::GetWebKitSmartPasteFormatType())); | 246 Clipboard::GetWebKitSmartPasteFormatType())); |
226 } | 247 } |
227 | 248 |
228 TEST_F(ClipboardTest, BitmapTest) { | 249 TEST_F(ClipboardTest, BitmapTest) { |
229 unsigned int fake_bitmap[] = { | 250 unsigned int fake_bitmap[] = { |
230 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89, | 251 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89, |
231 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568, | 252 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568, |
232 0x91E9F63A, 0xC31EA14F, 0x69AB32DF, 0x643A3FD1, | 253 0x91E9F63A, 0xC31EA14F, 0x69AB32DF, 0x643A3FD1, |
233 }; | 254 }; |
234 | 255 |
235 Clipboard clipboard; | 256 Clipboard clipboard; |
236 | 257 |
237 clipboard.Clear(); | 258 { |
238 clipboard.WriteBitmap(fake_bitmap, gfx::Size(3, 4)); | 259 ScopedClipboardWriter scw(&clipboard); |
| 260 scw.WriteBitmapFromPixels(fake_bitmap, gfx::Size(3, 4)); |
| 261 } |
| 262 |
239 EXPECT_EQ(true, clipboard.IsFormatAvailable( | 263 EXPECT_EQ(true, clipboard.IsFormatAvailable( |
240 Clipboard::GetBitmapFormatType())); | 264 Clipboard::GetBitmapFormatType())); |
241 } | 265 } |
242 #endif | 266 #endif |
OLD | NEW |