| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 <memory> | |
| 6 | |
| 7 #include "base/files/file_util.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/pickle.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "build/build_config.h" | |
| 13 #include "net/base/filename_util.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 #include "testing/platform_test.h" | |
| 16 #include "ui/base/dragdrop/os_exchange_data.h" | |
| 17 #include "ui/base/dragdrop/os_exchange_data_provider_factory.h" | |
| 18 #include "ui/events/platform/platform_event_source.h" | |
| 19 #include "ui/views/mus/os_exchange_data_provider_mus.h" | |
| 20 #include "url/gurl.h" | |
| 21 | |
| 22 using ui::Clipboard; | |
| 23 using ui::OSExchangeData; | |
| 24 | |
| 25 namespace views { | |
| 26 | |
| 27 // This file is a copy/paste of the unit tests in os_exchange_data_unittest.cc, | |
| 28 // with additional SetUp() to force the mus override. I thought about changeing | |
| 29 // the OSExchangeData test suite to a parameterized test suite, but I've | |
| 30 // previously had problems with that feature of gtest. | |
| 31 | |
| 32 class OSExchangeDataProviderMusTest | |
| 33 : public PlatformTest, | |
| 34 public ui::OSExchangeDataProviderFactory::Factory { | |
| 35 public: | |
| 36 OSExchangeDataProviderMusTest() {} | |
| 37 | |
| 38 // Overridden from PlatformTest: | |
| 39 void SetUp() override { | |
| 40 PlatformTest::SetUp(); | |
| 41 ui::OSExchangeDataProviderFactory::SetFactory(this); | |
| 42 } | |
| 43 | |
| 44 void TearDown() override { | |
| 45 ui::OSExchangeDataProviderFactory::SetFactory(nullptr); | |
| 46 PlatformTest::TearDown(); | |
| 47 } | |
| 48 | |
| 49 // Overridden from ui::OSExchangeDataProviderFactory::Factory: | |
| 50 std::unique_ptr<OSExchangeData::Provider> BuildProvider() override { | |
| 51 return base::MakeUnique<OSExchangeDataProviderMus>(); | |
| 52 } | |
| 53 | |
| 54 private: | |
| 55 base::MessageLoopForUI message_loop_; | |
| 56 }; | |
| 57 | |
| 58 TEST_F(OSExchangeDataProviderMusTest, StringDataGetAndSet) { | |
| 59 OSExchangeData data; | |
| 60 base::string16 input = base::ASCIIToUTF16("I can has cheezburger?"); | |
| 61 EXPECT_FALSE(data.HasString()); | |
| 62 data.SetString(input); | |
| 63 EXPECT_TRUE(data.HasString()); | |
| 64 | |
| 65 OSExchangeData data2( | |
| 66 std::unique_ptr<OSExchangeData::Provider>(data.provider().Clone())); | |
| 67 base::string16 output; | |
| 68 EXPECT_TRUE(data2.HasString()); | |
| 69 EXPECT_TRUE(data2.GetString(&output)); | |
| 70 EXPECT_EQ(input, output); | |
| 71 std::string url_spec = "http://www.goats.com/"; | |
| 72 GURL url(url_spec); | |
| 73 base::string16 title; | |
| 74 EXPECT_FALSE(data2.GetURLAndTitle( | |
| 75 OSExchangeData::DO_NOT_CONVERT_FILENAMES, &url, &title)); | |
| 76 // No URLs in |data|, so url should be untouched. | |
| 77 EXPECT_EQ(url_spec, url.spec()); | |
| 78 } | |
| 79 | |
| 80 TEST_F(OSExchangeDataProviderMusTest, TestURLExchangeFormats) { | |
| 81 OSExchangeData data; | |
| 82 std::string url_spec = "http://www.google.com/"; | |
| 83 GURL url(url_spec); | |
| 84 base::string16 url_title = base::ASCIIToUTF16("www.google.com"); | |
| 85 EXPECT_FALSE(data.HasURL(OSExchangeData::DO_NOT_CONVERT_FILENAMES)); | |
| 86 data.SetURL(url, url_title); | |
| 87 EXPECT_TRUE(data.HasURL(OSExchangeData::DO_NOT_CONVERT_FILENAMES)); | |
| 88 | |
| 89 OSExchangeData data2( | |
| 90 std::unique_ptr<OSExchangeData::Provider>(data.provider().Clone())); | |
| 91 | |
| 92 // URL spec and title should match | |
| 93 GURL output_url; | |
| 94 base::string16 output_title; | |
| 95 EXPECT_TRUE(data2.HasURL(OSExchangeData::DO_NOT_CONVERT_FILENAMES)); | |
| 96 EXPECT_TRUE(data2.GetURLAndTitle( | |
| 97 OSExchangeData::DO_NOT_CONVERT_FILENAMES, &output_url, &output_title)); | |
| 98 EXPECT_EQ(url_spec, output_url.spec()); | |
| 99 EXPECT_EQ(url_title, output_title); | |
| 100 base::string16 output_string; | |
| 101 | |
| 102 // URL should be the raw text response | |
| 103 EXPECT_TRUE(data2.GetString(&output_string)); | |
| 104 EXPECT_EQ(url_spec, base::UTF16ToUTF8(output_string)); | |
| 105 } | |
| 106 | |
| 107 // Test that setting the URL does not overwrite a previously set custom string. | |
| 108 TEST_F(OSExchangeDataProviderMusTest, URLAndString) { | |
| 109 OSExchangeData data; | |
| 110 base::string16 string = base::ASCIIToUTF16("I can has cheezburger?"); | |
| 111 data.SetString(string); | |
| 112 std::string url_spec = "http://www.google.com/"; | |
| 113 GURL url(url_spec); | |
| 114 base::string16 url_title = base::ASCIIToUTF16("www.google.com"); | |
| 115 data.SetURL(url, url_title); | |
| 116 | |
| 117 base::string16 output_string; | |
| 118 EXPECT_TRUE(data.GetString(&output_string)); | |
| 119 EXPECT_EQ(string, output_string); | |
| 120 | |
| 121 GURL output_url; | |
| 122 base::string16 output_title; | |
| 123 EXPECT_TRUE(data.GetURLAndTitle( | |
| 124 OSExchangeData::DO_NOT_CONVERT_FILENAMES, &output_url, &output_title)); | |
| 125 EXPECT_EQ(url_spec, output_url.spec()); | |
| 126 EXPECT_EQ(url_title, output_title); | |
| 127 } | |
| 128 | |
| 129 TEST_F(OSExchangeDataProviderMusTest, TestFileToURLConversion) { | |
| 130 OSExchangeData data; | |
| 131 EXPECT_FALSE(data.HasURL(OSExchangeData::DO_NOT_CONVERT_FILENAMES)); | |
| 132 EXPECT_FALSE(data.HasURL(OSExchangeData::CONVERT_FILENAMES)); | |
| 133 EXPECT_FALSE(data.HasFile()); | |
| 134 | |
| 135 base::FilePath current_directory; | |
| 136 ASSERT_TRUE(base::GetCurrentDirectory(¤t_directory)); | |
| 137 | |
| 138 data.SetFilename(current_directory); | |
| 139 | |
| 140 { | |
| 141 EXPECT_FALSE(data.HasURL(OSExchangeData::DO_NOT_CONVERT_FILENAMES)); | |
| 142 GURL actual_url; | |
| 143 base::string16 actual_title; | |
| 144 EXPECT_FALSE(data.GetURLAndTitle( | |
| 145 OSExchangeData::DO_NOT_CONVERT_FILENAMES, &actual_url, &actual_title)); | |
| 146 EXPECT_EQ(GURL(), actual_url); | |
| 147 EXPECT_EQ(base::string16(), actual_title); | |
| 148 } | |
| 149 | |
| 150 { | |
| 151 EXPECT_TRUE(data.HasURL(OSExchangeData::CONVERT_FILENAMES)); | |
| 152 GURL actual_url; | |
| 153 base::string16 actual_title; | |
| 154 EXPECT_TRUE(data.GetURLAndTitle(OSExchangeData::CONVERT_FILENAMES, | |
| 155 &actual_url, &actual_title)); | |
| 156 // Some Mac OS versions return the URL in file://localhost form instead | |
| 157 // of file:///, so we compare the url's path not its absolute string. | |
| 158 EXPECT_EQ(net::FilePathToFileURL(current_directory).path(), | |
| 159 actual_url.path()); | |
| 160 EXPECT_EQ(base::string16(), actual_title); | |
| 161 } | |
| 162 EXPECT_TRUE(data.HasFile()); | |
| 163 base::FilePath actual_path; | |
| 164 EXPECT_TRUE(data.GetFilename(&actual_path)); | |
| 165 EXPECT_EQ(current_directory, actual_path); | |
| 166 } | |
| 167 | |
| 168 TEST_F(OSExchangeDataProviderMusTest, TestPickledData) { | |
| 169 const Clipboard::FormatType kTestFormat = | |
| 170 Clipboard::GetFormatType("application/vnd.chromium.test"); | |
| 171 | |
| 172 base::Pickle saved_pickle; | |
| 173 saved_pickle.WriteInt(1); | |
| 174 saved_pickle.WriteInt(2); | |
| 175 OSExchangeData data; | |
| 176 data.SetPickledData(kTestFormat, saved_pickle); | |
| 177 | |
| 178 OSExchangeData copy( | |
| 179 std::unique_ptr<OSExchangeData::Provider>(data.provider().Clone())); | |
| 180 EXPECT_TRUE(copy.HasCustomFormat(kTestFormat)); | |
| 181 | |
| 182 base::Pickle restored_pickle; | |
| 183 EXPECT_TRUE(copy.GetPickledData(kTestFormat, &restored_pickle)); | |
| 184 base::PickleIterator iterator(restored_pickle); | |
| 185 int value; | |
| 186 EXPECT_TRUE(iterator.ReadInt(&value)); | |
| 187 EXPECT_EQ(1, value); | |
| 188 EXPECT_TRUE(iterator.ReadInt(&value)); | |
| 189 EXPECT_EQ(2, value); | |
| 190 } | |
| 191 | |
| 192 #if defined(USE_AURA) | |
| 193 TEST_F(OSExchangeDataProviderMusTest, TestHTML) { | |
| 194 OSExchangeData data; | |
| 195 GURL url("http://www.google.com/"); | |
| 196 base::string16 html = base::ASCIIToUTF16( | |
| 197 "<HTML>\n<BODY>\n" | |
| 198 "<b>bold.</b> <i><b>This is bold italic.</b></i>\n" | |
| 199 "</BODY>\n</HTML>"); | |
| 200 data.SetHtml(html, url); | |
| 201 | |
| 202 OSExchangeData copy( | |
| 203 std::unique_ptr<OSExchangeData::Provider>(data.provider().Clone())); | |
| 204 base::string16 read_html; | |
| 205 EXPECT_TRUE(copy.GetHtml(&read_html, &url)); | |
| 206 EXPECT_EQ(html, read_html); | |
| 207 } | |
| 208 #endif | |
| 209 | |
| 210 } // namespace views | |
| 211 | |
| OLD | NEW |