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/files/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"
18 class OSExchangeDataTest
: public PlatformTest
{
21 : event_source_(ui::PlatformEventSource::CreateDefault()) {}
24 base::MessageLoopForUI message_loop_
;
25 scoped_ptr
<PlatformEventSource
> event_source_
;
28 TEST_F(OSExchangeDataTest
, StringDataGetAndSet
) {
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/";
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
) {
51 std::string url_spec
= "http://www.google.com/";
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
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
) {
78 base::string16 string
= base::ASCIIToUTF16("I can has cheezburger?");
79 data
.SetString(string
);
80 std::string url_spec
= "http://www.google.com/";
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
);
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
) {
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(¤t_directory
));
106 data
.SetFilename(current_directory
);
109 EXPECT_FALSE(data
.HasURL(OSExchangeData::DO_NOT_CONVERT_FILENAMES
));
111 base::string16 actual_title
;
112 EXPECT_FALSE(data
.GetURLAndTitle(
113 OSExchangeData::DO_NOT_CONVERT_FILENAMES
, &actual_url
, &actual_title
));
114 EXPECT_EQ(GURL(), actual_url
);
115 EXPECT_EQ(base::string16(), actual_title
);
119 // Filename to URL conversion is not implemented on ChromeOS or on non-X11 Linux
121 #if defined(OS_CHROMEOS) || (defined(OS_LINUX) && !defined(USE_X11))
122 const bool expected_success
= false;
123 const GURL expected_url
;
125 const bool expected_success
= true;
126 const GURL
expected_url(net::FilePathToFileURL(current_directory
));
128 EXPECT_EQ(expected_success
, data
.HasURL(OSExchangeData::CONVERT_FILENAMES
));
130 base::string16 actual_title
;
134 OSExchangeData::CONVERT_FILENAMES
, &actual_url
, &actual_title
));
135 // Some Mac OS versions return the URL in file://localhost form instead
136 // of file:///, so we compare the url's path not its absolute string.
137 EXPECT_EQ(expected_url
.path(), actual_url
.path());
138 EXPECT_EQ(base::string16(), actual_title
);
140 EXPECT_TRUE(data
.HasFile());
141 base::FilePath actual_path
;
142 EXPECT_TRUE(data
.GetFilename(&actual_path
));
143 EXPECT_EQ(current_directory
, actual_path
);
146 TEST_F(OSExchangeDataTest
, TestPickledData
) {
147 const OSExchangeData::CustomFormat kTestFormat
=
148 ui::Clipboard::GetFormatType("application/vnd.chromium.test");
151 saved_pickle
.WriteInt(1);
152 saved_pickle
.WriteInt(2);
154 data
.SetPickledData(kTestFormat
, saved_pickle
);
156 OSExchangeData
copy(data
.provider().Clone());
157 EXPECT_TRUE(copy
.HasCustomFormat(kTestFormat
));
159 Pickle restored_pickle
;
160 EXPECT_TRUE(copy
.GetPickledData(kTestFormat
, &restored_pickle
));
161 PickleIterator
iterator(restored_pickle
);
163 EXPECT_TRUE(iterator
.ReadInt(&value
));
165 EXPECT_TRUE(iterator
.ReadInt(&value
));
169 #if defined(USE_AURA)
170 TEST_F(OSExchangeDataTest
, TestHTML
) {
172 GURL
url("http://www.google.com/");
173 base::string16 html
= base::ASCIIToUTF16(
175 "<b>bold.</b> <i><b>This is bold italic.</b></i>\n"
177 data
.SetHtml(html
, url
);
179 OSExchangeData
copy(data
.provider().Clone());
180 base::string16 read_html
;
181 EXPECT_TRUE(copy
.GetHtml(&read_html
, &url
));
182 EXPECT_EQ(html
, read_html
);