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/time/time.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "ui/base/test/test_clipboard.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 class ClipboardRecentContentGenericTest : public testing::Test { |
| 17 protected: |
| 18 void SetUp() override { |
| 19 test_clipboard_ = new ui::TestClipboard; |
| 20 std::unique_ptr<ui::Clipboard> clipboard(test_clipboard_); |
| 21 ui::Clipboard::SetClipboardForCurrentThread(std::move(clipboard)); |
| 22 } |
| 23 |
| 24 void TearDown() override { |
| 25 ui::Clipboard::DestroyClipboardForCurrentThread(); |
| 26 } |
| 27 |
| 28 ui::TestClipboard* test_clipboard_; |
| 29 }; |
| 30 |
| 31 TEST_F(ClipboardRecentContentGenericTest, RecognizesURLs) { |
| 32 struct { |
| 33 std::string clipboard; |
| 34 const bool expected_get_recent_url_value; |
| 35 } test_data[] = { |
| 36 {"www", false}, |
| 37 {"query string", false}, |
| 38 {"www.example.com", false}, |
| 39 {"http://www.example.com/", true}, |
| 40 // The missing trailing slash shouldn't matter. |
| 41 {"http://www.example.com", true}, |
| 42 {"https://another-example.com/", true}, |
| 43 {"http://example.com/with-path/", true}, |
| 44 {"about:version", true}, |
| 45 {"data:,Hello%2C%20World!", true}, |
| 46 // Certain schemes are not eligible to be suggested. |
| 47 {"ftp://example.com/", false}, |
| 48 // Leading and trailing spaces are okay, other spaces not. |
| 49 {" http://leading.com", true}, |
| 50 {" http://both.com/trailing ", true}, |
| 51 {"http://malformed url", false}, |
| 52 {"http://another.com/malformed url", false}, |
| 53 // Internationalized domain names should work. |
| 54 {"http://xn--c1yn36f", true}, |
| 55 {" http://xn--c1yn36f/path ", true}, |
| 56 {"http://xn--c1yn36f extra ", false}, |
| 57 {"http://點看", true}, |
| 58 {"http://點看/path", true}, |
| 59 {" http://點看/path ", true}, |
| 60 {" http://點看/path extra word", false}, |
| 61 }; |
| 62 |
| 63 ClipboardRecentContentGeneric recent_content; |
| 64 base::Time now = base::Time::Now(); |
| 65 for (size_t i = 0; i < arraysize(test_data); ++i) { |
| 66 test_clipboard_->WriteText(test_data[i].clipboard.data(), |
| 67 test_data[i].clipboard.length()); |
| 68 test_clipboard_->SetClipboardLastModifiedTime( |
| 69 now - base::TimeDelta::FromSeconds(10)); |
| 70 GURL url; |
| 71 EXPECT_EQ(test_data[i].expected_get_recent_url_value, |
| 72 recent_content.GetRecentURLFromClipboard(&url)) |
| 73 << "for input " << test_data[i].clipboard; |
| 74 } |
| 75 } |
| 76 |
| 77 TEST_F(ClipboardRecentContentGenericTest, OlderURLsNotSuggested) { |
| 78 ClipboardRecentContentGeneric recent_content; |
| 79 base::Time now = base::Time::Now(); |
| 80 std::string text = "http://example.com/"; |
| 81 test_clipboard_->WriteText(text.data(), text.length()); |
| 82 test_clipboard_->SetClipboardLastModifiedTime( |
| 83 now - base::TimeDelta::FromSeconds(10)); |
| 84 GURL url; |
| 85 EXPECT_TRUE(recent_content.GetRecentURLFromClipboard(&url)); |
| 86 // If the last modified time is days ago, the URL shouldn't be suggested. |
| 87 test_clipboard_->SetClipboardLastModifiedTime(now - |
| 88 base::TimeDelta::FromDays(2)); |
| 89 EXPECT_FALSE(recent_content.GetRecentURLFromClipboard(&url)); |
| 90 } |
| 91 |
| 92 TEST_F(ClipboardRecentContentGenericTest, GetClipboardContentAge) { |
| 93 ClipboardRecentContentGeneric recent_content; |
| 94 base::Time now = base::Time::Now(); |
| 95 std::string text = " whether URL or not should not matter here."; |
| 96 test_clipboard_->WriteText(text.data(), text.length()); |
| 97 test_clipboard_->SetClipboardLastModifiedTime( |
| 98 now - base::TimeDelta::FromSeconds(32)); |
| 99 base::TimeDelta age = recent_content.GetClipboardContentAge(); |
| 100 // It's possible the GetClipboardContentAge() took some time, so allow a |
| 101 // little slop (5 seconds) in this comparison; don't check for equality. |
| 102 EXPECT_LT(age - base::TimeDelta::FromSeconds(32), |
| 103 base::TimeDelta::FromSeconds(5)); |
| 104 } |
| 105 |
| 106 TEST_F(ClipboardRecentContentGenericTest, SuppressClipboardContent) { |
| 107 // Make sure the URL is suggested. |
| 108 ClipboardRecentContentGeneric recent_content; |
| 109 base::Time now = base::Time::Now(); |
| 110 std::string text = "http://example.com/"; |
| 111 test_clipboard_->WriteText(text.data(), text.length()); |
| 112 test_clipboard_->SetClipboardLastModifiedTime( |
| 113 now - base::TimeDelta::FromSeconds(10)); |
| 114 GURL url; |
| 115 EXPECT_TRUE(recent_content.GetRecentURLFromClipboard(&url)); |
| 116 |
| 117 // After suppressing it, it shouldn't be suggested. |
| 118 recent_content.SuppressClipboardContent(); |
| 119 EXPECT_FALSE(recent_content.GetRecentURLFromClipboard(&url)); |
| 120 |
| 121 // If the clipboard changes, even if to the same thing again, the content |
| 122 // should be suggested again. |
| 123 test_clipboard_->WriteText(text.data(), text.length()); |
| 124 test_clipboard_->SetClipboardLastModifiedTime(now); |
| 125 EXPECT_TRUE(recent_content.GetRecentURLFromClipboard(&url)); |
| 126 } |
OLD | NEW |