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

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: address 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 #import "components/open_from_clipboard/clipboard_recent_content_ios_impl.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/platform_test.h" 14 #include "testing/platform_test.h"
14 15
15 namespace { 16 namespace {
16 17
17 UIImage* TestUIImage() { 18 UIImage* TestUIImage() {
18 CGRect frame = CGRectMake(0, 0, 1.0, 1.0); 19 CGRect frame = CGRectMake(0, 0, 1.0, 1.0);
19 UIGraphicsBeginImageContext(frame.size); 20 UIGraphicsBeginImageContext(frame.size);
20 21
21 CGContextRef context = UIGraphicsGetCurrentContext(); 22 CGContextRef context = UIGraphicsGetCurrentContext();
(...skipping 16 matching lines...) Expand all
38 forPasteboardType:@"public.plain-text"]; 39 forPasteboardType:@"public.plain-text"];
39 } 40 }
40 const char kUnrecognizedURL[] = "ftp://foo/"; 41 const char kUnrecognizedURL[] = "ftp://foo/";
41 const char kRecognizedURL[] = "http://bar/"; 42 const char kRecognizedURL[] = "http://bar/";
42 const char kRecognizedURL2[] = "http://bar/2"; 43 const char kRecognizedURL2[] = "http://bar/2";
43 const char kAppSpecificURL[] = "test://qux/"; 44 const char kAppSpecificURL[] = "test://qux/";
44 const char kAppSpecificScheme[] = "test"; 45 const char kAppSpecificScheme[] = "test";
45 NSTimeInterval kSevenHours = 60 * 60 * 7; 46 NSTimeInterval kSevenHours = 60 * 60 * 7;
46 } // namespace 47 } // namespace
47 48
49 @interface ClipboardRecentContentIOSImplWithFakeUptime
50 : ClipboardRecentContentIOSImpl
51 @property(nonatomic) NSTimeInterval fakeUptime;
52
53 - (instancetype)initWithDelegate:
54 (id<ClipboardRecentContentMetricsDelegate>)delegate
55 authorizedSchemes:(NSArray*)authorizedSchemes
56 userDefaults:(NSUserDefaults*)groupUserDefaults
57 uptime:(NSTimeInterval)uptime;
58
59 @end
60
61 @implementation ClipboardRecentContentIOSImplWithFakeUptime
62
63 @synthesize fakeUptime = _fakeUptime;
64
65 - (instancetype)initWithDelegate:
66 (id<ClipboardRecentContentMetricsDelegate>)delegate
67 authorizedSchemes:(NSArray*)authorizedSchemes
68 userDefaults:(NSUserDefaults*)groupUserDefaults
69 uptime:(NSTimeInterval)uptime {
70 self = [super initWithDelegate:delegate
71 authorizedSchemes:authorizedSchemes
72 userDefaults:groupUserDefaults];
73 if (self) {
74 _fakeUptime = uptime;
75 }
76 return self;
77 }
78
79 - (NSTimeInterval)uptime {
sdefresne 2017/04/03 09:27:17 I would change this to a public property instead (
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(const std::string& application_scheme,
52 NSUserDefaults* group_user_defaults) 89 NSUserDefaults* group_user_defaults)
53 : ClipboardRecentContentIOS(application_scheme, group_user_defaults) {} 90 : ClipboardRecentContentIOS(application_scheme, group_user_defaults) {}
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_.reset(new ClipboardRecentContentIOSWithFakeUptime(
80 application_scheme, [NSUserDefaults standardUserDefaults])); 109 application_scheme, [NSUserDefaults standardUserDefaults]));
81 clipboard_content_->SetUptime(time_delta); 110
111 clipboard_content_->implementation_.reset(
sdefresne 2017/04/03 09:27:17 This is exactly how you should not write test. Ins
sdefresne 2017/04/03 09:37:32 Sorry, this comment tone is too negative. I should
lody 2017/04/04 13:42:19 Done.
112 [[ClipboardRecentContentIOSImplWithFakeUptime alloc]
113 initWithDelegate:nil
114 authorizedSchemes:clipboard_content_->GetAuthorizedSchemeList(
sdefresne 2017/04/03 09:27:17 Just inject a know list/set of schemes here, no ne
lody 2017/04/04 13:42:19 Done.
115 application_scheme)
116 userDefaults:[NSUserDefaults standardUserDefaults]
117 uptime:time_delta.InSecondsF()]);
82 } 118 }
83 119
84 void SetStoredPasteboardChangeDate(NSDate* changeDate) { 120 void SetStoredPasteboardChangeDate(NSDate* change_date) {
85 clipboard_content_->last_pasteboard_change_date_.reset([changeDate copy]); 121 clipboard_content_->SetLastPasteboardChangeDate([change_date copy]);
sdefresne 2017/04/03 09:27:17 Can't you simulate this by instead manipulating th
lody 2017/04/04 13:42:19 I don't think so? There's two things, one is the u
86 clipboard_content_->SaveToUserDefaults(); 122 clipboard_content_->SaveToUserDefaults();
87 } 123 }
88 124
89 void SetStoredPasteboardChangeCount(NSInteger newChangeCount) {
90 clipboard_content_->last_pasteboard_change_count_ = newChangeCount;
91 clipboard_content_->SaveToUserDefaults();
92 }
93
94 protected: 125 protected:
95 std::unique_ptr<ClipboardRecentContentIOSWithFakeUptime> clipboard_content_; 126 std::unique_ptr<ClipboardRecentContentIOSWithFakeUptime> clipboard_content_;
96 }; 127 };
97 128
98 TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) { 129 TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) {
99 GURL gurl; 130 GURL gurl;
100 131
101 // Test unrecognized URL. 132 // Test unrecognized URL.
102 SetPasteboardContent(kUnrecognizedURL); 133 SetPasteboardContent(kUnrecognizedURL);
103 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 134 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 // Check that the pasteboard content is suppressed. 188 // Check that the pasteboard content is suppressed.
158 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 189 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
159 190
160 // Create a new clipboard content to test persistence. 191 // Create a new clipboard content to test persistence.
161 ResetClipboardRecentContent(kAppSpecificScheme, 192 ResetClipboardRecentContent(kAppSpecificScheme,
162 base::TimeDelta::FromDays(10)); 193 base::TimeDelta::FromDays(10));
163 194
164 // Check that the pasteboard content is still suppressed. 195 // Check that the pasteboard content is still suppressed.
165 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 196 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
166 197
167 // Check that the even if the device is restarted, pasteboard content is 198 // Check that even if the device is restarted, pasteboard content is
168 // still suppressed. 199 // still suppressed.
169 SimulateDeviceRestart(); 200 SimulateDeviceRestart();
170 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 201 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
171 202
172 // Check that if the pasteboard changes, the new content is not 203 // Check that if the pasteboard changes, the new content is not
173 // supressed anymore. 204 // supressed anymore.
174 SetPasteboardContent(kRecognizedURL2); 205 SetPasteboardContent(kRecognizedURL2);
175 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 206 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
176 } 207 }
177 208
(...skipping 12 matching lines...) Expand all
190 SetPasteboardImage(TestUIImage()); 221 SetPasteboardImage(TestUIImage());
191 222
192 // Pasteboard should appear empty. 223 // Pasteboard should appear empty.
193 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 224 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
194 225
195 // Tests that if URL is added again, pasteboard provides it normally. 226 // Tests that if URL is added again, pasteboard provides it normally.
196 SetPasteboardContent(kRecognizedURL); 227 SetPasteboardContent(kRecognizedURL);
197 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); 228 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
198 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str()); 229 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str());
199 } 230 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698