1 // Copyright 2015 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.
5 #include "components/open_from_clipboard/clipboard_recent_content_ios.h"
7 #import <UIKit/UIKit.h>
9 #include "base/memory/scoped_ptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
13 class ClipboardRecentContentIOSTestHelper : public ClipboardRecentContentIOS {
15 ClipboardRecentContentIOSTestHelper() {}
16 ~ClipboardRecentContentIOSTestHelper() override {}
17 void SetStoredPasteboardChangeDate(NSDate* changeDate) {
18 lastPasteboardChangeDate_.reset([changeDate copy]);
25 void SetPasteboardContent(const char* data) {
26 [[UIPasteboard generalPasteboard]
27 setValue:[NSString stringWithUTF8String:data]
28 forPasteboardType:@"public.plain-text"];
30 const char* kUnrecognizedURL = "ftp://foo/";
31 const char* kRecognizedURL = "http://bar/";
32 const char* kAppSpecificURL = "test://qux/";
33 const char* kAppSpecificScheme = "test";
34 NSTimeInterval kSevenHours = 60 * 60 * 7;
37 class ClipboardRecentContentIOSTest : public ::testing::Test {
39 void SetUp() override {
40 clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper());
42 void TearDown() override {}
45 scoped_ptr<test::ClipboardRecentContentIOSTestHelper> clipboard_content_;
48 TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) {
51 // Test unrecognized URL.
52 SetPasteboardContent(kUnrecognizedURL);
53 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
55 // Test recognized URL.
56 SetPasteboardContent(kRecognizedURL);
57 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
58 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str());
60 // Test URL with app specific scheme, before and after configuration.
61 SetPasteboardContent(kAppSpecificURL);
62 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
63 clipboard_content_->set_application_scheme(kAppSpecificScheme);
64 SetPasteboardContent(kAppSpecificURL);
65 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
66 EXPECT_STREQ(kAppSpecificURL, gurl.spec().c_str());
69 TEST_F(ClipboardRecentContentIOSTest, PasteboardURLObsolescence) {
71 SetPasteboardContent(kRecognizedURL);
73 // Test that recent pasteboard data is provided.
74 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
75 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str());
77 // Test that old pasteboard data is not provided.
78 clipboard_content_->SetStoredPasteboardChangeDate(
79 [NSDate dateWithTimeIntervalSinceNow:-kSevenHours]);
80 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
82 // Test that pasteboard data is treated as new if the last recorded clipboard
83 // change dates from before the machine booted.
84 clipboard_content_->SetStoredPasteboardChangeDate(
85 [NSDate dateWithTimeIntervalSince1970:0]);
86 clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper());
87 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
88 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str());