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 "ui/base/dragdrop/os_exchange_data_provider_aurax11.h"
7 // Clean up X11 header polution
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/dragdrop/file_info.h"
16 #include "ui/events/platform/x11/x11_event_source.h"
19 const char kFileURL
[] = "file:///home/user/file.txt";
20 const char kFileName
[] = "/home/user/file.txt";
21 const char kGoogleTitle
[] = "Google";
22 const char kGoogleURL
[] = "http://www.google.com/";
26 class OSExchangeDataProviderAuraX11Test
: public testing::Test
{
28 OSExchangeDataProviderAuraX11Test() : event_source(gfx::GetXDisplay()) {}
30 void AddURLList(const std::string
& list_contents
) {
31 std::string contents_copy
= list_contents
;
32 scoped_refptr
<base::RefCountedMemory
> mem(
33 base::RefCountedString::TakeString(&contents_copy
));
35 provider
.format_map_
.Insert(
36 provider
.atom_cache_
.GetAtom(ui::Clipboard::kMimeTypeURIList
),
41 base::MessageLoopForUI message_loop
;
42 X11EventSource event_source
;
43 ui::OSExchangeDataProviderAuraX11 provider
;
46 TEST_F(OSExchangeDataProviderAuraX11Test
, MozillaURL
) {
47 // Check that we can get titled entries.
48 provider
.SetURL(GURL(kGoogleURL
), base::ASCIIToUTF16(kGoogleTitle
));
51 base::string16 out_str
;
52 EXPECT_TRUE(provider
.GetURLAndTitle(
53 OSExchangeData::DO_NOT_CONVERT_FILENAMES
, &out_gurl
, &out_str
));
54 EXPECT_EQ(base::ASCIIToUTF16(kGoogleTitle
), out_str
);
55 EXPECT_EQ(kGoogleURL
, out_gurl
.spec());
58 // Check that we can get non-titled entries.
59 provider
.SetURL(GURL(kGoogleURL
), base::string16());
62 base::string16 out_str
;
63 EXPECT_TRUE(provider
.GetURLAndTitle(
64 OSExchangeData::DO_NOT_CONVERT_FILENAMES
, &out_gurl
, &out_str
));
65 EXPECT_EQ(base::string16(), out_str
);
66 EXPECT_EQ(kGoogleURL
, out_gurl
.spec());
70 TEST_F(OSExchangeDataProviderAuraX11Test
, FilesArentURLs
) {
73 EXPECT_TRUE(provider
.HasFile());
74 EXPECT_TRUE(provider
.HasURL(ui::OSExchangeData::CONVERT_FILENAMES
));
75 EXPECT_FALSE(provider
.HasURL(ui::OSExchangeData::DO_NOT_CONVERT_FILENAMES
));
78 TEST_F(OSExchangeDataProviderAuraX11Test
, HTTPURLsArentFiles
) {
79 AddURLList(kGoogleURL
);
81 EXPECT_FALSE(provider
.HasFile());
82 EXPECT_TRUE(provider
.HasURL(ui::OSExchangeData::CONVERT_FILENAMES
));
83 EXPECT_TRUE(provider
.HasURL(ui::OSExchangeData::DO_NOT_CONVERT_FILENAMES
));
86 TEST_F(OSExchangeDataProviderAuraX11Test
, URIListWithBoth
) {
87 AddURLList("file:///home/user/file.txt\nhttp://www.google.com");
89 EXPECT_TRUE(provider
.HasFile());
90 EXPECT_TRUE(provider
.HasURL(ui::OSExchangeData::CONVERT_FILENAMES
));
91 EXPECT_TRUE(provider
.HasURL(ui::OSExchangeData::DO_NOT_CONVERT_FILENAMES
));
93 // We should only receive the file from GetFilenames().
94 std::vector
<FileInfo
> filenames
;
95 EXPECT_TRUE(provider
.GetFilenames(&filenames
));
96 ASSERT_EQ(1u, filenames
.size());
97 EXPECT_EQ(kFileName
, filenames
[0].path
.value());
99 // We should only receive the URL here.
101 base::string16 out_str
;
102 EXPECT_TRUE(provider
.GetURLAndTitle(
103 OSExchangeData::DO_NOT_CONVERT_FILENAMES
, &out_gurl
, &out_str
));
104 EXPECT_EQ(base::string16(), out_str
);
105 EXPECT_EQ(kGoogleURL
, out_gurl
.spec());
108 TEST_F(OSExchangeDataProviderAuraX11Test
, OnlyStringURLIsUnfiltered
) {
109 const base::string16 file_url
= base::UTF8ToUTF16(kFileURL
);
110 provider
.SetString(file_url
);
112 EXPECT_TRUE(provider
.HasString());
113 EXPECT_FALSE(provider
.HasURL(ui::OSExchangeData::DO_NOT_CONVERT_FILENAMES
));
116 TEST_F(OSExchangeDataProviderAuraX11Test
, StringAndURIListFilterString
) {
117 const base::string16 file_url
= base::UTF8ToUTF16(kFileURL
);
118 provider
.SetString(file_url
);
119 AddURLList(kFileURL
);
121 EXPECT_FALSE(provider
.HasString());
122 base::string16 out_str
;
123 EXPECT_FALSE(provider
.GetString(&out_str
));
125 EXPECT_TRUE(provider
.HasFile());