Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: components/open_from_clipboard/clipboard_recent_content_ios_unittest.mm

Issue 2782823003: Rewrite implementation of ClipboardRecentContent in Objective C. (Closed)
Patch Set: sdefresne's comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/open_from_clipboard/clipboard_recent_content_ios.h" 5 #include "components/open_from_clipboard/clipboard_recent_content_ios.h"
6 6
7 #import <CoreGraphics/CoreGraphics.h> 7 #import <CoreGraphics/CoreGraphics.h>
8 #import <UIKit/UIKit.h> 8 #import <UIKit/UIKit.h>
9 9
10 #include <memory> 10 #include <memory>
11 11
12 #include "base/strings/sys_string_conversions.h"
13 #import "components/open_from_clipboard/clipboard_recent_content_impl_ios.h"
12 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/platform_test.h" 15 #include "testing/platform_test.h"
14 16
15 namespace { 17 namespace {
16 18
17 UIImage* TestUIImage() { 19 UIImage* TestUIImage() {
18 CGRect frame = CGRectMake(0, 0, 1.0, 1.0); 20 CGRect frame = CGRectMake(0, 0, 1.0, 1.0);
19 UIGraphicsBeginImageContext(frame.size); 21 UIGraphicsBeginImageContext(frame.size);
20 22
21 CGContextRef context = UIGraphicsGetCurrentContext(); 23 CGContextRef context = UIGraphicsGetCurrentContext();
22 CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 24 CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
23 CGContextFillRect(context, frame); 25 CGContextFillRect(context, frame);
24 26
25 UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); 27 UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
26 UIGraphicsEndImageContext(); 28 UIGraphicsEndImageContext();
27 29
28 return image; 30 return image;
29 } 31 }
30 32
31 void SetPasteboardImage(UIImage* image) { 33 void SetPasteboardImage(UIImage* image) {
32 [[UIPasteboard generalPasteboard] setImage:image]; 34 [[UIPasteboard generalPasteboard] setImage:image];
33 } 35 }
34 36
35 void SetPasteboardContent(const char* data) { 37 void SetPasteboardContent(const char* data) {
36 [[UIPasteboard generalPasteboard] 38 [[UIPasteboard generalPasteboard]
37 setValue:[NSString stringWithUTF8String:data] 39 setValue:[NSString stringWithUTF8String:data]
38 forPasteboardType:@"public.plain-text"]; 40 forPasteboardType:@"public.plain-text"];
39 } 41 }
40 const char kUnrecognizedURL[] = "ftp://foo/"; 42 const char kUnrecognizedURL[] = "bad://foo/";
41 const char kRecognizedURL[] = "http://bar/"; 43 const char kRecognizedURL[] = "good://bar/";
42 const char kRecognizedURL2[] = "http://bar/2"; 44 const char kRecognizedURL2[] = "good://bar/2";
43 const char kAppSpecificURL[] = "test://qux/"; 45 const char kAppSpecificURL[] = "test://qux/";
44 const char kAppSpecificScheme[] = "test"; 46 const char kAppSpecificScheme[] = "test";
47 const char kRecognizedScheme[] = "good";
45 NSTimeInterval kSevenHours = 60 * 60 * 7; 48 NSTimeInterval kSevenHours = 60 * 60 * 7;
46 } // namespace 49 } // namespace
47 50
51 @interface ClipboardRecentContentImplIOSWithFakeUptime
52 : ClipboardRecentContentImplIOS
53 @property(nonatomic) NSTimeInterval fakeUptime;
54
55 - (instancetype)initWithDelegate:(id<ClipboardRecentContentDelegate>)delegate
56 authorizedSchemes:(NSArray*)authorizedSchemes
57 userDefaults:(NSUserDefaults*)groupUserDefaults
58 uptime:(NSTimeInterval)uptime;
59
60 @end
61
62 @implementation ClipboardRecentContentImplIOSWithFakeUptime
63
64 @synthesize fakeUptime = _fakeUptime;
65
66 - (instancetype)initWithDelegate:(id<ClipboardRecentContentDelegate>)delegate
67 authorizedSchemes:(NSSet*)authorizedSchemes
68 userDefaults:(NSUserDefaults*)groupUserDefaults
69 uptime:(NSTimeInterval)uptime {
70 self = [super initWithAuthorizedSchemes:authorizedSchemes
71 userDefaults:groupUserDefaults
72 delegate:delegate];
73 if (self) {
74 _fakeUptime = uptime;
75 }
76 return self;
77 }
78
79 - (NSTimeInterval)uptime {
80 return self.fakeUptime;
81 }
82
83 @end
84
48 class ClipboardRecentContentIOSWithFakeUptime 85 class ClipboardRecentContentIOSWithFakeUptime
49 : public ClipboardRecentContentIOS { 86 : public ClipboardRecentContentIOS {
50 public: 87 public:
51 ClipboardRecentContentIOSWithFakeUptime(const std::string& application_scheme, 88 ClipboardRecentContentIOSWithFakeUptime(
52 NSUserDefaults* group_user_defaults) 89 ClipboardRecentContentImplIOS* implementation)
53 : ClipboardRecentContentIOS(application_scheme, group_user_defaults) {} 90 : ClipboardRecentContentIOS(implementation) {}
54 // Sets the uptime.
55 void SetUptime(base::TimeDelta uptime) { uptime_ = uptime; }
56
57 protected:
58 base::TimeDelta Uptime() const override { return uptime_; }
59
60 private:
61 base::TimeDelta uptime_;
62 }; 91 };
63 92
64 class ClipboardRecentContentIOSTest : public ::testing::Test { 93 class ClipboardRecentContentIOSTest : public ::testing::Test {
65 protected: 94 protected:
66 ClipboardRecentContentIOSTest() { 95 ClipboardRecentContentIOSTest() {
67 // By default, set that the device booted 10 days ago. 96 // By default, set that the device booted 10 days ago.
68 ResetClipboardRecentContent(kAppSpecificScheme, 97 ResetClipboardRecentContent(kAppSpecificScheme,
69 base::TimeDelta::FromDays(10)); 98 base::TimeDelta::FromDays(10));
70 } 99 }
71 100
72 void SimulateDeviceRestart() { 101 void SimulateDeviceRestart() {
73 ResetClipboardRecentContent(kAppSpecificScheme, 102 ResetClipboardRecentContent(kAppSpecificScheme,
74 base::TimeDelta::FromSeconds(0)); 103 base::TimeDelta::FromSeconds(0));
75 } 104 }
76 105
77 void ResetClipboardRecentContent(const std::string& application_scheme, 106 void ResetClipboardRecentContent(const std::string& application_scheme,
78 base::TimeDelta time_delta) { 107 base::TimeDelta time_delta) {
79 clipboard_content_.reset(new ClipboardRecentContentIOSWithFakeUptime( 108 clipboard_content_impl_ =
80 application_scheme, [NSUserDefaults standardUserDefaults])); 109 [[ClipboardRecentContentImplIOSWithFakeUptime alloc]
81 clipboard_content_->SetUptime(time_delta); 110 initWithDelegate:nil
111 authorizedSchemes:@[
112 base::SysUTF8ToNSString(kRecognizedScheme),
113 base::SysUTF8ToNSString(application_scheme)
114 ]
115 userDefaults:[NSUserDefaults standardUserDefaults]
116 uptime:time_delta.InSecondsF()];
117
118 clipboard_content_.reset(
119 new ClipboardRecentContentIOSWithFakeUptime(clipboard_content_impl_));
82 } 120 }
83 121
84 void SetStoredPasteboardChangeDate(NSDate* changeDate) { 122 void SetStoredPasteboardChangeDate(NSDate* change_date) {
85 clipboard_content_->last_pasteboard_change_date_.reset([changeDate copy]); 123 clipboard_content_impl_.lastPasteboardChangeDate = [change_date copy];
86 clipboard_content_->SaveToUserDefaults(); 124 [clipboard_content_impl_ saveToUserDefaults];
87 }
88
89 void SetStoredPasteboardChangeCount(NSInteger newChangeCount) {
90 clipboard_content_->last_pasteboard_change_count_ = newChangeCount;
91 clipboard_content_->SaveToUserDefaults();
92 } 125 }
93 126
94 protected: 127 protected:
95 std::unique_ptr<ClipboardRecentContentIOSWithFakeUptime> clipboard_content_; 128 std::unique_ptr<ClipboardRecentContentIOSWithFakeUptime> clipboard_content_;
129 ClipboardRecentContentImplIOSWithFakeUptime* clipboard_content_impl_;
96 }; 130 };
97 131
98 TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) { 132 TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) {
99 GURL gurl; 133 GURL gurl;
100 134
101 // Test unrecognized URL. 135 // Test unrecognized URL.
102 SetPasteboardContent(kUnrecognizedURL); 136 SetPasteboardContent(kUnrecognizedURL);
103 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 137 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
104 138
105 // Test recognized URL. 139 // Test recognized URL.
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 // Check that the pasteboard content is suppressed. 191 // Check that the pasteboard content is suppressed.
158 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 192 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
159 193
160 // Create a new clipboard content to test persistence. 194 // Create a new clipboard content to test persistence.
161 ResetClipboardRecentContent(kAppSpecificScheme, 195 ResetClipboardRecentContent(kAppSpecificScheme,
162 base::TimeDelta::FromDays(10)); 196 base::TimeDelta::FromDays(10));
163 197
164 // Check that the pasteboard content is still suppressed. 198 // Check that the pasteboard content is still suppressed.
165 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 199 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
166 200
167 // Check that the even if the device is restarted, pasteboard content is 201 // Check that even if the device is restarted, pasteboard content is
168 // still suppressed. 202 // still suppressed.
169 SimulateDeviceRestart(); 203 SimulateDeviceRestart();
170 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 204 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
171 205
172 // Check that if the pasteboard changes, the new content is not 206 // Check that if the pasteboard changes, the new content is not
173 // supressed anymore. 207 // supressed anymore.
174 SetPasteboardContent(kRecognizedURL2); 208 SetPasteboardContent(kRecognizedURL2);
175 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 209 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
176 } 210 }
177 211
(...skipping 12 matching lines...) Expand all
190 SetPasteboardImage(TestUIImage()); 224 SetPasteboardImage(TestUIImage());
191 225
192 // Pasteboard should appear empty. 226 // Pasteboard should appear empty.
193 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 227 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
194 228
195 // Tests that if URL is added again, pasteboard provides it normally. 229 // Tests that if URL is added again, pasteboard provides it normally.
196 SetPasteboardContent(kRecognizedURL); 230 SetPasteboardContent(kRecognizedURL);
197 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 231 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
198 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str()); 232 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str());
199 } 233 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698