OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "components/open_from_clipboard/clipboard_recent_content_generic.h" | |
6 | |
7 #include <memory> | |
8 #include <utility> | |
9 | |
10 #include "base/strings/string16.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "base/time/time.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "third_party/skia/include/core/SkBitmap.h" | |
15 #include "ui/base/clipboard/clipboard.h" | |
16 #include "url/gurl.h" | |
17 | |
18 using ui::ClipboardType; | |
19 | |
20 class MockClipboard : public ui::Clipboard { | |
dcheng
2017/04/02 05:04:03
Does ui::TestClipboard help with this?
Mark P
2017/04/02 05:33:56
Yes, a ton. Removed basically all the code and in
| |
21 public: | |
22 MockClipboard(); | |
23 | |
24 void ReadText(ClipboardType type, base::string16* result) const override; | |
25 void ReadAsciiText(ClipboardType type, std::string* result) const override; | |
26 base::Time GetClipboardLastModifiedTime() const override; | |
27 | |
28 void SetText(const std::string& string); | |
29 void SetClipboardLastModifiedTime(const base::Time& time); | |
30 | |
31 // Empty required implementations of Clipboard. | |
32 void OnPreShutdown() override {} | |
33 uint64_t GetSequenceNumber(ClipboardType type) const override { return 0; } | |
34 bool IsFormatAvailable(const FormatType& format, | |
35 ClipboardType type) const override { | |
36 return true; | |
37 } | |
38 void Clear(ClipboardType type) override {} | |
39 void ReadAvailableTypes(ClipboardType type, | |
40 std::vector<base::string16>* types, | |
41 bool* contains_filenames) const override {} | |
42 void ReadHTML(ClipboardType type, | |
43 base::string16* markup, | |
44 std::string* src_url, | |
45 uint32_t* fragment_start, | |
46 uint32_t* fragment_end) const override {} | |
47 void ReadRTF(ClipboardType type, std::string* result) const override {} | |
48 SkBitmap ReadImage(ClipboardType type) const override { return SkBitmap(); } | |
49 void ReadCustomData(ClipboardType clipboard_type, | |
50 const base::string16& type, | |
51 base::string16* result) const override {} | |
52 void ReadBookmark(base::string16* title, std::string* url) const override {} | |
53 void ReadData(const FormatType& format, std::string* result) const override {} | |
54 | |
55 protected: | |
56 // Empty required implementations of Clipboard. | |
57 void WriteText(const char* text_data, size_t text_len) override {} | |
58 void WriteObjects(ClipboardType type, const ObjectMap& objects) override {} | |
59 void WriteHTML(const char* markup_data, | |
60 size_t markup_len, | |
61 const char* url_data, | |
62 size_t url_len) override {} | |
63 void WriteRTF(const char* rtf_data, size_t data_len) override {} | |
64 void WriteBookmark(const char* title_data, | |
65 size_t title_len, | |
66 const char* url_data, | |
67 size_t url_len) override {} | |
68 void WriteWebSmartPaste() override {} | |
69 void WriteBitmap(const SkBitmap& bitmap) override {} | |
70 void WriteData(const FormatType& format, | |
71 const char* data_data, | |
72 size_t data_len) override {} | |
73 | |
74 private: | |
75 std::string contents_; | |
76 base::Time last_modified_time_; | |
77 }; | |
78 | |
79 MockClipboard::MockClipboard() {} | |
80 | |
81 void MockClipboard::ReadText(ClipboardType type, base::string16* result) const { | |
82 (*result) = base::UTF8ToUTF16(contents_); | |
83 } | |
84 | |
85 void MockClipboard::ReadAsciiText(ClipboardType type, | |
86 std::string* result) const { | |
87 (*result) = contents_; | |
88 } | |
89 | |
90 base::Time MockClipboard::GetClipboardLastModifiedTime() const { | |
91 return last_modified_time_; | |
92 } | |
93 | |
94 void MockClipboard::SetText(const std::string& string) { | |
95 contents_ = string; | |
96 } | |
97 | |
98 void MockClipboard::SetClipboardLastModifiedTime(const base::Time& time) { | |
99 last_modified_time_ = time; | |
100 } | |
101 | |
102 class ClipboardRecentContentGenericTest : public testing::Test { | |
103 protected: | |
104 void SetUp() override { | |
105 mock_clipboard_ = new MockClipboard; | |
106 std::unique_ptr<ui::Clipboard> clipboard(mock_clipboard_); | |
107 ui::Clipboard::SetClipboardForCurrentThread(std::move(clipboard)); | |
108 } | |
109 | |
110 void TearDown() override { | |
111 ui::Clipboard::DestroyClipboardForCurrentThread(); | |
112 } | |
113 | |
114 MockClipboard* mock_clipboard_; | |
115 }; | |
116 | |
117 TEST_F(ClipboardRecentContentGenericTest, RecognizesURLs) { | |
118 struct { | |
119 std::string clipboard; | |
120 const bool expected_get_recent_url_value; | |
121 } test_data[] = { | |
122 {"www", false}, | |
123 {"query string", false}, | |
124 {"www.example.com", false}, | |
125 {"http://www.example.com/", true}, | |
126 // The missing trailing slash shouldn't matter. | |
127 {"http://www.example.com", true}, | |
128 {"https://another-example.com/", true}, | |
129 {"http://example.com/with-path/", true}, | |
130 {"about:version", true}, | |
131 {"data:,Hello%2C%20World!", true}, | |
132 // Certain schemes are not eligible to be suggested. | |
133 {"ftp://example.com/", false}, | |
134 // Leading and trailing spaces are okay, other spaces not. | |
135 {" http://leading.com", true}, | |
136 {" http://both.com/trailing ", true}, | |
137 {"http://malformed url", false}, | |
138 {"http://another.com/malformed url", false}, | |
139 // Internationalized domain names should work. | |
140 {"http://xn--c1yn36f", true}, | |
141 {" http://xn--c1yn36f/path ", true}, | |
142 {"http://xn--c1yn36f extra ", false}, | |
143 {"http://點看", true}, | |
144 {"http://點看/path", true}, | |
145 {" http://點看/path ", true}, | |
146 {" http://點看/path extra word", false}, | |
147 }; | |
148 | |
149 ClipboardRecentContentGeneric recent_content; | |
150 base::Time now = base::Time::Now(); | |
151 for (size_t i = 0; i < arraysize(test_data); ++i) { | |
152 mock_clipboard_->SetText(test_data[i].clipboard); | |
153 mock_clipboard_->SetClipboardLastModifiedTime( | |
154 now - base::TimeDelta::FromSeconds(10)); | |
155 GURL url; | |
156 EXPECT_EQ(test_data[i].expected_get_recent_url_value, | |
157 recent_content.GetRecentURLFromClipboard(&url)) | |
158 << "for input " << test_data[i].clipboard << url.spec() | |
159 << url.IsStandard(); | |
160 } | |
161 } | |
162 | |
163 TEST_F(ClipboardRecentContentGenericTest, OlderURLsNotSuggested) { | |
164 ClipboardRecentContentGeneric recent_content; | |
165 base::Time now = base::Time::Now(); | |
166 mock_clipboard_->SetText("http://example.com/"); | |
167 mock_clipboard_->SetClipboardLastModifiedTime( | |
168 now - base::TimeDelta::FromSeconds(10)); | |
169 GURL url; | |
170 EXPECT_TRUE(recent_content.GetRecentURLFromClipboard(&url)); | |
171 // If the last modified time is days ago, the URL shouldn't be suggested. | |
172 mock_clipboard_->SetClipboardLastModifiedTime(now - | |
173 base::TimeDelta::FromDays(2)); | |
174 EXPECT_FALSE(recent_content.GetRecentURLFromClipboard(&url)); | |
175 } | |
176 | |
177 TEST_F(ClipboardRecentContentGenericTest, GetClipboardContentAge) { | |
178 ClipboardRecentContentGeneric recent_content; | |
179 base::Time now = base::Time::Now(); | |
180 mock_clipboard_->SetText(" whether URL or not should not matter here."); | |
181 mock_clipboard_->SetClipboardLastModifiedTime( | |
182 now - base::TimeDelta::FromSeconds(32)); | |
183 base::TimeDelta age = recent_content.GetClipboardContentAge(); | |
184 // It's possible the GetClipboardContentAge() took some time, so allow a | |
185 // little slop (5 seconds) in this comparison; don't check for equality. | |
186 EXPECT_LT(age - base::TimeDelta::FromSeconds(32), | |
187 base::TimeDelta::FromSeconds(5)); | |
188 } | |
189 | |
190 TEST_F(ClipboardRecentContentGenericTest, SuppressClipboardContent) { | |
191 // Make sure the URL is suggested. | |
192 ClipboardRecentContentGeneric recent_content; | |
193 base::Time now = base::Time::Now(); | |
194 mock_clipboard_->SetText("http://example.com/"); | |
195 mock_clipboard_->SetClipboardLastModifiedTime( | |
196 now - base::TimeDelta::FromSeconds(10)); | |
197 GURL url; | |
198 EXPECT_TRUE(recent_content.GetRecentURLFromClipboard(&url)); | |
199 | |
200 // After suppressing it, it shouldn't be suggested. | |
201 recent_content.SuppressClipboardContent(); | |
202 EXPECT_FALSE(recent_content.GetRecentURLFromClipboard(&url)); | |
203 | |
204 // If the clipboard changes, even if to the same thing again, the content | |
205 // should be suggested again. | |
206 mock_clipboard_->SetText("http://example.com/"); | |
207 mock_clipboard_->SetClipboardLastModifiedTime( | |
208 now - base::TimeDelta::FromSeconds(5)); | |
209 EXPECT_TRUE(recent_content.GetRecentURLFromClipboard(&url)); | |
210 } | |
OLD | NEW |