Roll src/third_party/WebKit 605a979:06cb9e9 (svn 202556:202558)
[chromium-blink-merge.git] / components / open_from_clipboard / clipboard_recent_content_ios_unittest.mm
blob7626449fa4533649ccd9be98d1815e3dfe2b0044
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"
14 namespace {
15 void SetPasteboardContent(const char* data) {
16   [[UIPasteboard generalPasteboard]
17                setValue:[NSString stringWithUTF8String:data]
18       forPasteboardType:@"public.plain-text"];
20 const char kUnrecognizedURL[] = "ftp://foo/";
21 const char kRecognizedURL[] = "http://bar/";
22 const char kAppSpecificURL[] = "test://qux/";
23 const char kAppSpecificScheme[] = "test";
24 NSTimeInterval kSevenHours = 60 * 60 * 7;
25 }  // namespace
27 class ClipboardRecentContentIOSTest : public ::testing::Test {
28  protected:
29   ClipboardRecentContentIOSTest() {
30     // By default, set that the device booted 10 days ago.
31     ResetClipboardRecentContent(kAppSpecificScheme,
32                                 base::TimeDelta::FromDays(10));
33   }
35   void SimulateDeviceRestart() {
36     // TODO(jif): Simulates the fact that on iOS7, the pasteboard's changeCount
37     // is reset. http://crbug.com/503609
38     ResetClipboardRecentContent(kAppSpecificScheme,
39                                 base::TimeDelta::FromSeconds(0));
40   }
42   void ResetClipboardRecentContent(const std::string& application_scheme,
43                                    base::TimeDelta time_delta) {
44     clipboard_content_.reset(
45         new ClipboardRecentContentIOS(application_scheme, time_delta));
46   }
48   void SetStoredPasteboardChangeDate(NSDate* changeDate) {
49     clipboard_content_->last_pasteboard_change_date_.reset([changeDate copy]);
50     clipboard_content_->SaveToUserDefaults();
51   }
53   void SetStoredPasteboardChangeCount(NSInteger newChangeCount) {
54     clipboard_content_->last_pasteboard_change_count_ = newChangeCount;
55     clipboard_content_->SaveToUserDefaults();
56   }
58  protected:
59   scoped_ptr<ClipboardRecentContentIOS> clipboard_content_;
62 TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) {
63   GURL gurl;
65   // Test unrecognized URL.
66   SetPasteboardContent(kUnrecognizedURL);
67   EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
69   // Test recognized URL.
70   SetPasteboardContent(kRecognizedURL);
71   EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
72   EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str());
74   // Test URL with app specific scheme.
75   SetPasteboardContent(kAppSpecificURL);
76   EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
77   EXPECT_STREQ(kAppSpecificURL, gurl.spec().c_str());
79   // Test URL without app specific scheme.
80   ResetClipboardRecentContent(std::string(), base::TimeDelta::FromDays(10));
82   SetPasteboardContent(kAppSpecificURL);
83   EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
86 TEST_F(ClipboardRecentContentIOSTest, PasteboardURLObsolescence) {
87   GURL gurl;
88   SetPasteboardContent(kRecognizedURL);
90   // Test that recent pasteboard data is provided.
91   EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
92   EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str());
94   // Test that old pasteboard data is not provided.
95   SetStoredPasteboardChangeDate(
96       [NSDate dateWithTimeIntervalSinceNow:-kSevenHours]);
97   EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
99   // Tests that if chrome is relaunched, old pasteboard data is still
100   // not provided.
101   ResetClipboardRecentContent(kAppSpecificScheme,
102                               base::TimeDelta::FromDays(10));
103   EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
105   SimulateDeviceRestart();
106   if (base::ios::IsRunningOnIOS8OrLater()) {
107     // Tests that if the device is restarted, old pasteboard data is still
108     // not provided.
109     EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
110   } else {
111     // TODO(jif): Simulates the fact that on iOS7, the pasteboard's changeCount
112     // is reset. http://crbug.com/503609
113   }
117 TEST_F(ClipboardRecentContentIOSTest, SupressedPasteboard) {
118   GURL gurl;
119   SetPasteboardContent(kRecognizedURL);
121   // Test that recent pasteboard data is provided.
122   EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
124   // Suppress the content of the pasteboard.
125   clipboard_content_->SuppressClipboardContent();
127   // Check that the pasteboard content is suppressed.
128   EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
130   // Create a new clipboard content to test persistence.
131   ResetClipboardRecentContent(kAppSpecificScheme,
132                               base::TimeDelta::FromDays(10));
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
138   // still suppressed.
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));