• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ui/base/dragdrop/os_exchange_data_provider_aurax11.h"
6 
7 // Clean up X11 header polution
8 #undef None
9 #undef Bool
10 
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"
17 #include "url/gurl.h"
18 
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/";
23 
24 namespace ui {
25 
26 class OSExchangeDataProviderAuraX11Test : public testing::Test {
27  public:
OSExchangeDataProviderAuraX11Test()28   OSExchangeDataProviderAuraX11Test() : event_source(gfx::GetXDisplay()) {}
29 
AddURLList(const std::string & list_contents)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));
34 
35     provider.format_map_.Insert(
36         provider.atom_cache_.GetAtom(ui::Clipboard::kMimeTypeURIList),
37         mem);
38   }
39 
40  protected:
41   base::MessageLoopForUI message_loop;
42   X11EventSource event_source;
43   ui::OSExchangeDataProviderAuraX11 provider;
44 };
45 
TEST_F(OSExchangeDataProviderAuraX11Test,MozillaURL)46 TEST_F(OSExchangeDataProviderAuraX11Test, MozillaURL) {
47   // Check that we can get titled entries.
48   provider.SetURL(GURL(kGoogleURL), base::ASCIIToUTF16(kGoogleTitle));
49   {
50     GURL out_gurl;
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());
56   }
57 
58   // Check that we can get non-titled entries.
59   provider.SetURL(GURL(kGoogleURL), base::string16());
60   {
61     GURL out_gurl;
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());
67   }
68 }
69 
TEST_F(OSExchangeDataProviderAuraX11Test,FilesArentURLs)70 TEST_F(OSExchangeDataProviderAuraX11Test, FilesArentURLs) {
71   AddURLList(kFileURL);
72 
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));
76 }
77 
TEST_F(OSExchangeDataProviderAuraX11Test,HTTPURLsArentFiles)78 TEST_F(OSExchangeDataProviderAuraX11Test, HTTPURLsArentFiles) {
79   AddURLList(kGoogleURL);
80 
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));
84 }
85 
TEST_F(OSExchangeDataProviderAuraX11Test,URIListWithBoth)86 TEST_F(OSExchangeDataProviderAuraX11Test, URIListWithBoth) {
87   AddURLList("file:///home/user/file.txt\nhttp://www.google.com");
88 
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));
92 
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());
98 
99   // We should only receive the URL here.
100   GURL out_gurl;
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());
106 }
107 
TEST_F(OSExchangeDataProviderAuraX11Test,OnlyStringURLIsUnfiltered)108 TEST_F(OSExchangeDataProviderAuraX11Test, OnlyStringURLIsUnfiltered) {
109   const base::string16 file_url = base::UTF8ToUTF16(kFileURL);
110   provider.SetString(file_url);
111 
112   EXPECT_TRUE(provider.HasString());
113   EXPECT_FALSE(provider.HasURL(ui::OSExchangeData::DO_NOT_CONVERT_FILENAMES));
114 }
115 
TEST_F(OSExchangeDataProviderAuraX11Test,StringAndURIListFilterString)116 TEST_F(OSExchangeDataProviderAuraX11Test, StringAndURIListFilterString) {
117   const base::string16 file_url = base::UTF8ToUTF16(kFileURL);
118   provider.SetString(file_url);
119   AddURLList(kFileURL);
120 
121   EXPECT_FALSE(provider.HasString());
122   base::string16 out_str;
123   EXPECT_FALSE(provider.GetString(&out_str));
124 
125   EXPECT_TRUE(provider.HasFile());
126 }
127 
128 }  // namespace ui
129