Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / base / dragdrop / os_exchange_data_unittest.cc
blobff0ddfc2c21cdcf72b0f133b9fb1ffdda5286ca9
1 // Copyright 2013 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 "base/file_util.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/pickle.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "net/base/filename_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h"
12 #include "ui/base/dragdrop/os_exchange_data.h"
13 #include "ui/events/platform/platform_event_source.h"
14 #include "url/gurl.h"
16 namespace ui {
18 class OSExchangeDataTest : public PlatformTest {
19 public:
20 OSExchangeDataTest()
21 : event_source_(ui::PlatformEventSource::CreateDefault()) {}
23 private:
24 base::MessageLoopForUI message_loop_;
25 scoped_ptr<PlatformEventSource> event_source_;
28 TEST_F(OSExchangeDataTest, StringDataGetAndSet) {
29 OSExchangeData data;
30 base::string16 input = base::ASCIIToUTF16("I can has cheezburger?");
31 EXPECT_FALSE(data.HasString());
32 data.SetString(input);
33 EXPECT_TRUE(data.HasString());
35 OSExchangeData data2(data.provider().Clone());
36 base::string16 output;
37 EXPECT_TRUE(data2.HasString());
38 EXPECT_TRUE(data2.GetString(&output));
39 EXPECT_EQ(input, output);
40 std::string url_spec = "http://www.goats.com/";
41 GURL url(url_spec);
42 base::string16 title;
43 EXPECT_FALSE(data2.GetURLAndTitle(
44 OSExchangeData::DO_NOT_CONVERT_FILENAMES, &url, &title));
45 // No URLs in |data|, so url should be untouched.
46 EXPECT_EQ(url_spec, url.spec());
49 TEST_F(OSExchangeDataTest, TestURLExchangeFormats) {
50 OSExchangeData data;
51 std::string url_spec = "http://www.google.com/";
52 GURL url(url_spec);
53 base::string16 url_title = base::ASCIIToUTF16("www.google.com");
54 EXPECT_FALSE(data.HasURL(OSExchangeData::DO_NOT_CONVERT_FILENAMES));
55 data.SetURL(url, url_title);
56 EXPECT_TRUE(data.HasURL(OSExchangeData::DO_NOT_CONVERT_FILENAMES));
58 OSExchangeData data2(data.provider().Clone());
60 // URL spec and title should match
61 GURL output_url;
62 base::string16 output_title;
63 EXPECT_TRUE(data2.HasURL(OSExchangeData::DO_NOT_CONVERT_FILENAMES));
64 EXPECT_TRUE(data2.GetURLAndTitle(
65 OSExchangeData::DO_NOT_CONVERT_FILENAMES, &output_url, &output_title));
66 EXPECT_EQ(url_spec, output_url.spec());
67 EXPECT_EQ(url_title, output_title);
68 base::string16 output_string;
70 // URL should be the raw text response
71 EXPECT_TRUE(data2.GetString(&output_string));
72 EXPECT_EQ(url_spec, base::UTF16ToUTF8(output_string));
75 // Test that setting the URL does not overwrite a previously set custom string.
76 TEST_F(OSExchangeDataTest, URLAndString) {
77 OSExchangeData data;
78 base::string16 string = base::ASCIIToUTF16("I can has cheezburger?");
79 data.SetString(string);
80 std::string url_spec = "http://www.google.com/";
81 GURL url(url_spec);
82 base::string16 url_title = base::ASCIIToUTF16("www.google.com");
83 data.SetURL(url, url_title);
85 base::string16 output_string;
86 EXPECT_TRUE(data.GetString(&output_string));
87 EXPECT_EQ(string, output_string);
89 GURL output_url;
90 base::string16 output_title;
91 EXPECT_TRUE(data.GetURLAndTitle(
92 OSExchangeData::DO_NOT_CONVERT_FILENAMES, &output_url, &output_title));
93 EXPECT_EQ(url_spec, output_url.spec());
94 EXPECT_EQ(url_title, output_title);
97 TEST_F(OSExchangeDataTest, TestFileToURLConversion) {
98 OSExchangeData data;
99 EXPECT_FALSE(data.HasURL(OSExchangeData::DO_NOT_CONVERT_FILENAMES));
100 EXPECT_FALSE(data.HasURL(OSExchangeData::CONVERT_FILENAMES));
101 EXPECT_FALSE(data.HasFile());
103 base::FilePath current_directory;
104 ASSERT_TRUE(base::GetCurrentDirectory(&current_directory));
106 data.SetFilename(current_directory);
108 EXPECT_FALSE(data.HasURL(OSExchangeData::DO_NOT_CONVERT_FILENAMES));
109 GURL actual_url;
110 base::string16 actual_title;
111 EXPECT_FALSE(data.GetURLAndTitle(
112 OSExchangeData::DO_NOT_CONVERT_FILENAMES, &actual_url, &actual_title));
113 EXPECT_EQ(GURL(), actual_url);
114 EXPECT_EQ(base::string16(), actual_title);
117 // Filename to URL conversion is not implemented on ChromeOS or on non-X11 Linux
118 // builds.
119 #if defined(OS_CHROMEOS) || (defined(OS_LINUX) && !defined(USE_X11))
120 const bool expected_success = false;
121 const GURL expected_url;
122 #else
123 const bool expected_success = true;
124 const GURL expected_url(net::FilePathToFileURL(current_directory));
125 #endif
126 EXPECT_EQ(expected_success, data.HasURL(OSExchangeData::CONVERT_FILENAMES));
127 GURL actual_url;
128 base::string16 actual_title;
129 EXPECT_EQ(
130 expected_success,
131 data.GetURLAndTitle(
132 OSExchangeData::CONVERT_FILENAMES, &actual_url, &actual_title));
133 EXPECT_EQ(expected_url, actual_url);
134 EXPECT_EQ(base::string16(), actual_title);
136 EXPECT_TRUE(data.HasFile());
137 base::FilePath actual_path;
138 EXPECT_TRUE(data.GetFilename(&actual_path));
139 EXPECT_EQ(current_directory, actual_path);
142 TEST_F(OSExchangeDataTest, TestPickledData) {
143 const OSExchangeData::CustomFormat kTestFormat =
144 ui::Clipboard::GetFormatType("application/vnd.chromium.test");
146 Pickle saved_pickle;
147 saved_pickle.WriteInt(1);
148 saved_pickle.WriteInt(2);
149 OSExchangeData data;
150 data.SetPickledData(kTestFormat, saved_pickle);
152 OSExchangeData copy(data.provider().Clone());
153 EXPECT_TRUE(copy.HasCustomFormat(kTestFormat));
155 Pickle restored_pickle;
156 EXPECT_TRUE(copy.GetPickledData(kTestFormat, &restored_pickle));
157 PickleIterator iterator(restored_pickle);
158 int value;
159 EXPECT_TRUE(restored_pickle.ReadInt(&iterator, &value));
160 EXPECT_EQ(1, value);
161 EXPECT_TRUE(restored_pickle.ReadInt(&iterator, &value));
162 EXPECT_EQ(2, value);
165 #if defined(USE_AURA)
166 TEST_F(OSExchangeDataTest, TestHTML) {
167 OSExchangeData data;
168 GURL url("http://www.google.com/");
169 base::string16 html = base::ASCIIToUTF16(
170 "<HTML>\n<BODY>\n"
171 "<b>bold.</b> <i><b>This is bold italic.</b></i>\n"
172 "</BODY>\n</HTML>");
173 data.SetHtml(html, url);
175 OSExchangeData copy(data.provider().Clone());
176 base::string16 read_html;
177 EXPECT_TRUE(copy.GetHtml(&read_html, &url));
178 EXPECT_EQ(html, read_html);
180 #endif
182 } // namespace ui