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