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/ios/ios_util.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/platform_test.h"
15 class ClipboardRecentContentIOSTestHelper : public ClipboardRecentContentIOS {
17 // By default, set that the device booted 10 days ago.
18 ClipboardRecentContentIOSTestHelper()
19 : ClipboardRecentContentIOS(base::TimeDelta::FromDays(10)) {}
20 ClipboardRecentContentIOSTestHelper(base::TimeDelta t)
21 : ClipboardRecentContentIOS(t) {}
23 ~ClipboardRecentContentIOSTestHelper() override {}
24 void SetStoredPasteboardChangeDate(NSDate* changeDate) {
25 lastPasteboardChangeDate_.reset([changeDate copy]);
28 void SetStoredPasteboardChangeCount(NSInteger newChangeCount) {
29 lastPasteboardChangeCount_ = newChangeCount;
36 void SetPasteboardContent(const char* data) {
37 [[UIPasteboard generalPasteboard]
38 setValue:[NSString stringWithUTF8String:data]
39 forPasteboardType:@"public.plain-text"];
41 const char* kUnrecognizedURL = "ftp://foo/";
42 const char* kRecognizedURL = "http://bar/";
43 const char* kAppSpecificURL = "test://qux/";
44 const char* kAppSpecificScheme = "test";
45 NSTimeInterval kSevenHours = 60 * 60 * 7;
48 class ClipboardRecentContentIOSTest : public ::testing::Test {
50 void SetUp() override {
51 clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper());
53 void TearDown() override {}
55 void SimulateDeviceRestart() {
56 // TODO(jif): Simulates the fact that on iOS7, the pasteboard's changeCount
57 // is reset. http://crbug.com/503609
58 clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper(
59 base::TimeDelta::FromSeconds(0)));
63 scoped_ptr<test::ClipboardRecentContentIOSTestHelper> clipboard_content_;
66 TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) {
69 // Test unrecognized URL.
70 SetPasteboardContent(kUnrecognizedURL);
71 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
73 // Test recognized URL.
74 SetPasteboardContent(kRecognizedURL);
75 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
76 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str());
78 // Test URL with app specific scheme, before and after configuration.
79 SetPasteboardContent(kAppSpecificURL);
80 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
81 clipboard_content_->set_application_scheme(kAppSpecificScheme);
82 SetPasteboardContent(kAppSpecificURL);
83 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
84 EXPECT_STREQ(kAppSpecificURL, gurl.spec().c_str());
87 TEST_F(ClipboardRecentContentIOSTest, PasteboardURLObsolescence) {
89 SetPasteboardContent(kRecognizedURL);
91 // Test that recent pasteboard data is provided.
92 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
93 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str());
95 // Test that old pasteboard data is not provided.
96 clipboard_content_->SetStoredPasteboardChangeDate(
97 [NSDate dateWithTimeIntervalSinceNow:-kSevenHours]);
98 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
100 // Tests that if chrome is relaunched, old pasteboard data is still
102 clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper());
103 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
106 SimulateDeviceRestart();
107 if (base::ios::IsRunningOnIOS8OrLater()) {
108 // Tests that if the device is restarted, old pasteboard data is still
110 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
112 // TODO(jif): Simulates the fact that on iOS7, the pasteboard's changeCount
113 // is reset. http://crbug.com/503609
118 TEST_F(ClipboardRecentContentIOSTest, SupressedPasteboard) {
120 SetPasteboardContent(kRecognizedURL);
122 // Test that recent pasteboard data is provided.
123 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
125 // Suppress the content of the pasteboard.
126 clipboard_content_->SuppressClipboardContent();
128 // Check that the pasteboard content is suppressed.
129 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
131 // Create a new clipboard content to test persistence.
132 clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper());
134 // Check that the pasteboard content is still suppressed.
135 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
137 // Check that the even if the device is restarted, pasteboard content is
139 SimulateDeviceRestart();
140 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
142 // Check that if the pasteboard changes, the new content is not
143 // supressed anymore.
144 SetPasteboardContent(kRecognizedURL);
145 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));