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