Chromium Code Reviews| Index: components/open_from_clipboard/clipboard_recent_content_generic_unittest.cc |
| diff --git a/components/open_from_clipboard/clipboard_recent_content_generic_unittest.cc b/components/open_from_clipboard/clipboard_recent_content_generic_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..28b2aac29d7df9786800b3732f617b238cccbefd |
| --- /dev/null |
| +++ b/components/open_from_clipboard/clipboard_recent_content_generic_unittest.cc |
| @@ -0,0 +1,210 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/open_from_clipboard/clipboard_recent_content_generic.h" |
| + |
| +#include <memory> |
| +#include <utility> |
| + |
| +#include "base/strings/string16.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/time/time.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| +#include "ui/base/clipboard/clipboard.h" |
| +#include "url/gurl.h" |
| + |
| +using ui::ClipboardType; |
| + |
| +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
|
| + public: |
| + MockClipboard(); |
| + |
| + void ReadText(ClipboardType type, base::string16* result) const override; |
| + void ReadAsciiText(ClipboardType type, std::string* result) const override; |
| + base::Time GetClipboardLastModifiedTime() const override; |
| + |
| + void SetText(const std::string& string); |
| + void SetClipboardLastModifiedTime(const base::Time& time); |
| + |
| + // Empty required implementations of Clipboard. |
| + void OnPreShutdown() override {} |
| + uint64_t GetSequenceNumber(ClipboardType type) const override { return 0; } |
| + bool IsFormatAvailable(const FormatType& format, |
| + ClipboardType type) const override { |
| + return true; |
| + } |
| + void Clear(ClipboardType type) override {} |
| + void ReadAvailableTypes(ClipboardType type, |
| + std::vector<base::string16>* types, |
| + bool* contains_filenames) const override {} |
| + void ReadHTML(ClipboardType type, |
| + base::string16* markup, |
| + std::string* src_url, |
| + uint32_t* fragment_start, |
| + uint32_t* fragment_end) const override {} |
| + void ReadRTF(ClipboardType type, std::string* result) const override {} |
| + SkBitmap ReadImage(ClipboardType type) const override { return SkBitmap(); } |
| + void ReadCustomData(ClipboardType clipboard_type, |
| + const base::string16& type, |
| + base::string16* result) const override {} |
| + void ReadBookmark(base::string16* title, std::string* url) const override {} |
| + void ReadData(const FormatType& format, std::string* result) const override {} |
| + |
| + protected: |
| + // Empty required implementations of Clipboard. |
| + void WriteText(const char* text_data, size_t text_len) override {} |
| + void WriteObjects(ClipboardType type, const ObjectMap& objects) override {} |
| + void WriteHTML(const char* markup_data, |
| + size_t markup_len, |
| + const char* url_data, |
| + size_t url_len) override {} |
| + void WriteRTF(const char* rtf_data, size_t data_len) override {} |
| + void WriteBookmark(const char* title_data, |
| + size_t title_len, |
| + const char* url_data, |
| + size_t url_len) override {} |
| + void WriteWebSmartPaste() override {} |
| + void WriteBitmap(const SkBitmap& bitmap) override {} |
| + void WriteData(const FormatType& format, |
| + const char* data_data, |
| + size_t data_len) override {} |
| + |
| + private: |
| + std::string contents_; |
| + base::Time last_modified_time_; |
| +}; |
| + |
| +MockClipboard::MockClipboard() {} |
| + |
| +void MockClipboard::ReadText(ClipboardType type, base::string16* result) const { |
| + (*result) = base::UTF8ToUTF16(contents_); |
| +} |
| + |
| +void MockClipboard::ReadAsciiText(ClipboardType type, |
| + std::string* result) const { |
| + (*result) = contents_; |
| +} |
| + |
| +base::Time MockClipboard::GetClipboardLastModifiedTime() const { |
| + return last_modified_time_; |
| +} |
| + |
| +void MockClipboard::SetText(const std::string& string) { |
| + contents_ = string; |
| +} |
| + |
| +void MockClipboard::SetClipboardLastModifiedTime(const base::Time& time) { |
| + last_modified_time_ = time; |
| +} |
| + |
| +class ClipboardRecentContentGenericTest : public testing::Test { |
| + protected: |
| + void SetUp() override { |
| + mock_clipboard_ = new MockClipboard; |
| + std::unique_ptr<ui::Clipboard> clipboard(mock_clipboard_); |
| + ui::Clipboard::SetClipboardForCurrentThread(std::move(clipboard)); |
| + } |
| + |
| + void TearDown() override { |
| + ui::Clipboard::DestroyClipboardForCurrentThread(); |
| + } |
| + |
| + MockClipboard* mock_clipboard_; |
| +}; |
| + |
| +TEST_F(ClipboardRecentContentGenericTest, RecognizesURLs) { |
| + struct { |
| + std::string clipboard; |
| + const bool expected_get_recent_url_value; |
| + } test_data[] = { |
| + {"www", false}, |
| + {"query string", false}, |
| + {"www.example.com", false}, |
| + {"http://www.example.com/", true}, |
| + // The missing trailing slash shouldn't matter. |
| + {"http://www.example.com", true}, |
| + {"https://another-example.com/", true}, |
| + {"http://example.com/with-path/", true}, |
| + {"about:version", true}, |
| + {"data:,Hello%2C%20World!", true}, |
| + // Certain schemes are not eligible to be suggested. |
| + {"ftp://example.com/", false}, |
| + // Leading and trailing spaces are okay, other spaces not. |
| + {" http://leading.com", true}, |
| + {" http://both.com/trailing ", true}, |
| + {"http://malformed url", false}, |
| + {"http://another.com/malformed url", false}, |
| + // Internationalized domain names should work. |
| + {"http://xn--c1yn36f", true}, |
| + {" http://xn--c1yn36f/path ", true}, |
| + {"http://xn--c1yn36f extra ", false}, |
| + {"http://點看", true}, |
| + {"http://點看/path", true}, |
| + {" http://點看/path ", true}, |
| + {" http://點看/path extra word", false}, |
| + }; |
| + |
| + ClipboardRecentContentGeneric recent_content; |
| + base::Time now = base::Time::Now(); |
| + for (size_t i = 0; i < arraysize(test_data); ++i) { |
| + mock_clipboard_->SetText(test_data[i].clipboard); |
| + mock_clipboard_->SetClipboardLastModifiedTime( |
| + now - base::TimeDelta::FromSeconds(10)); |
| + GURL url; |
| + EXPECT_EQ(test_data[i].expected_get_recent_url_value, |
| + recent_content.GetRecentURLFromClipboard(&url)) |
| + << "for input " << test_data[i].clipboard << url.spec() |
| + << url.IsStandard(); |
| + } |
| +} |
| + |
| +TEST_F(ClipboardRecentContentGenericTest, OlderURLsNotSuggested) { |
| + ClipboardRecentContentGeneric recent_content; |
| + base::Time now = base::Time::Now(); |
| + mock_clipboard_->SetText("http://example.com/"); |
| + mock_clipboard_->SetClipboardLastModifiedTime( |
| + now - base::TimeDelta::FromSeconds(10)); |
| + GURL url; |
| + EXPECT_TRUE(recent_content.GetRecentURLFromClipboard(&url)); |
| + // If the last modified time is days ago, the URL shouldn't be suggested. |
| + mock_clipboard_->SetClipboardLastModifiedTime(now - |
| + base::TimeDelta::FromDays(2)); |
| + EXPECT_FALSE(recent_content.GetRecentURLFromClipboard(&url)); |
| +} |
| + |
| +TEST_F(ClipboardRecentContentGenericTest, GetClipboardContentAge) { |
| + ClipboardRecentContentGeneric recent_content; |
| + base::Time now = base::Time::Now(); |
| + mock_clipboard_->SetText(" whether URL or not should not matter here."); |
| + mock_clipboard_->SetClipboardLastModifiedTime( |
| + now - base::TimeDelta::FromSeconds(32)); |
| + base::TimeDelta age = recent_content.GetClipboardContentAge(); |
| + // It's possible the GetClipboardContentAge() took some time, so allow a |
| + // little slop (5 seconds) in this comparison; don't check for equality. |
| + EXPECT_LT(age - base::TimeDelta::FromSeconds(32), |
| + base::TimeDelta::FromSeconds(5)); |
| +} |
| + |
| +TEST_F(ClipboardRecentContentGenericTest, SuppressClipboardContent) { |
| + // Make sure the URL is suggested. |
| + ClipboardRecentContentGeneric recent_content; |
| + base::Time now = base::Time::Now(); |
| + mock_clipboard_->SetText("http://example.com/"); |
| + mock_clipboard_->SetClipboardLastModifiedTime( |
| + now - base::TimeDelta::FromSeconds(10)); |
| + GURL url; |
| + EXPECT_TRUE(recent_content.GetRecentURLFromClipboard(&url)); |
| + |
| + // After suppressing it, it shouldn't be suggested. |
| + recent_content.SuppressClipboardContent(); |
| + EXPECT_FALSE(recent_content.GetRecentURLFromClipboard(&url)); |
| + |
| + // If the clipboard changes, even if to the same thing again, the content |
| + // should be suggested again. |
| + mock_clipboard_->SetText("http://example.com/"); |
| + mock_clipboard_->SetClipboardLastModifiedTime( |
| + now - base::TimeDelta::FromSeconds(5)); |
| + EXPECT_TRUE(recent_content.GetRecentURLFromClipboard(&url)); |
| +} |