| 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> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 [[UIPasteboard generalPasteboard] setImage:image]; | 32 [[UIPasteboard generalPasteboard] setImage:image]; |
| 33 } | 33 } |
| 34 | 34 |
| 35 void SetPasteboardContent(const char* data) { | 35 void SetPasteboardContent(const char* data) { |
| 36 [[UIPasteboard generalPasteboard] | 36 [[UIPasteboard generalPasteboard] |
| 37 setValue:[NSString stringWithUTF8String:data] | 37 setValue:[NSString stringWithUTF8String:data] |
| 38 forPasteboardType:@"public.plain-text"]; | 38 forPasteboardType:@"public.plain-text"]; |
| 39 } | 39 } |
| 40 const char kUnrecognizedURL[] = "ftp://foo/"; | 40 const char kUnrecognizedURL[] = "ftp://foo/"; |
| 41 const char kRecognizedURL[] = "http://bar/"; | 41 const char kRecognizedURL[] = "http://bar/"; |
| 42 const char kRecognizedURL2[] = "http://bar/2"; |
| 42 const char kAppSpecificURL[] = "test://qux/"; | 43 const char kAppSpecificURL[] = "test://qux/"; |
| 43 const char kAppSpecificScheme[] = "test"; | 44 const char kAppSpecificScheme[] = "test"; |
| 44 NSTimeInterval kSevenHours = 60 * 60 * 7; | 45 NSTimeInterval kSevenHours = 60 * 60 * 7; |
| 45 } // namespace | 46 } // namespace |
| 46 | 47 |
| 48 class ClipboardRecentContentIOSWithFakeUptime |
| 49 : public ClipboardRecentContentIOS { |
| 50 public: |
| 51 ClipboardRecentContentIOSWithFakeUptime(const std::string& application_scheme, |
| 52 NSUserDefaults* group_user_defaults) |
| 53 : 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 }; |
| 63 |
| 47 class ClipboardRecentContentIOSTest : public ::testing::Test { | 64 class ClipboardRecentContentIOSTest : public ::testing::Test { |
| 48 protected: | 65 protected: |
| 49 ClipboardRecentContentIOSTest() { | 66 ClipboardRecentContentIOSTest() { |
| 50 // By default, set that the device booted 10 days ago. | 67 // By default, set that the device booted 10 days ago. |
| 51 ResetClipboardRecentContent(kAppSpecificScheme, | 68 ResetClipboardRecentContent(kAppSpecificScheme, |
| 52 base::TimeDelta::FromDays(10)); | 69 base::TimeDelta::FromDays(10)); |
| 53 } | 70 } |
| 54 | 71 |
| 55 void SimulateDeviceRestart() { | 72 void SimulateDeviceRestart() { |
| 56 ResetClipboardRecentContent(kAppSpecificScheme, | 73 ResetClipboardRecentContent(kAppSpecificScheme, |
| 57 base::TimeDelta::FromSeconds(0)); | 74 base::TimeDelta::FromSeconds(0)); |
| 58 } | 75 } |
| 59 | 76 |
| 60 void ResetClipboardRecentContent(const std::string& application_scheme, | 77 void ResetClipboardRecentContent(const std::string& application_scheme, |
| 61 base::TimeDelta time_delta) { | 78 base::TimeDelta time_delta) { |
| 62 clipboard_content_.reset( | 79 clipboard_content_.reset(new ClipboardRecentContentIOSWithFakeUptime( |
| 63 new ClipboardRecentContentIOS(application_scheme, time_delta)); | 80 application_scheme, [NSUserDefaults standardUserDefaults])); |
| 81 clipboard_content_->SetUptime(time_delta); |
| 64 } | 82 } |
| 65 | 83 |
| 66 void SetStoredPasteboardChangeDate(NSDate* changeDate) { | 84 void SetStoredPasteboardChangeDate(NSDate* changeDate) { |
| 67 clipboard_content_->last_pasteboard_change_date_.reset([changeDate copy]); | 85 clipboard_content_->last_pasteboard_change_date_.reset([changeDate copy]); |
| 68 clipboard_content_->SaveToUserDefaults(); | 86 clipboard_content_->SaveToUserDefaults(); |
| 69 } | 87 } |
| 70 | 88 |
| 71 void SetStoredPasteboardChangeCount(NSInteger newChangeCount) { | 89 void SetStoredPasteboardChangeCount(NSInteger newChangeCount) { |
| 72 clipboard_content_->last_pasteboard_change_count_ = newChangeCount; | 90 clipboard_content_->last_pasteboard_change_count_ = newChangeCount; |
| 73 clipboard_content_->SaveToUserDefaults(); | 91 clipboard_content_->SaveToUserDefaults(); |
| 74 } | 92 } |
| 75 | 93 |
| 76 protected: | 94 protected: |
| 77 std::unique_ptr<ClipboardRecentContentIOS> clipboard_content_; | 95 std::unique_ptr<ClipboardRecentContentIOSWithFakeUptime> clipboard_content_; |
| 78 }; | 96 }; |
| 79 | 97 |
| 80 TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) { | 98 TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) { |
| 81 GURL gurl; | 99 GURL gurl; |
| 82 | 100 |
| 83 // Test unrecognized URL. | 101 // Test unrecognized URL. |
| 84 SetPasteboardContent(kUnrecognizedURL); | 102 SetPasteboardContent(kUnrecognizedURL); |
| 85 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 103 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 86 | 104 |
| 87 // Test recognized URL. | 105 // Test recognized URL. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 // Check that the pasteboard content is still suppressed. | 164 // Check that the pasteboard content is still suppressed. |
| 147 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 165 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 148 | 166 |
| 149 // Check that the even if the device is restarted, pasteboard content is | 167 // Check that the even if the device is restarted, pasteboard content is |
| 150 // still suppressed. | 168 // still suppressed. |
| 151 SimulateDeviceRestart(); | 169 SimulateDeviceRestart(); |
| 152 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 170 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 153 | 171 |
| 154 // Check that if the pasteboard changes, the new content is not | 172 // Check that if the pasteboard changes, the new content is not |
| 155 // supressed anymore. | 173 // supressed anymore. |
| 156 SetPasteboardContent(kRecognizedURL); | 174 SetPasteboardContent(kRecognizedURL2); |
| 157 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 175 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 158 } | 176 } |
| 159 | 177 |
| 160 // Checks that if user copies something other than a string we don't cache the | 178 // Checks that if user copies something other than a string we don't cache the |
| 161 // string in pasteboard. | 179 // string in pasteboard. |
| 162 TEST_F(ClipboardRecentContentIOSTest, AddingNonStringRemovesCachedString) { | 180 TEST_F(ClipboardRecentContentIOSTest, AddingNonStringRemovesCachedString) { |
| 163 GURL gurl; | 181 GURL gurl; |
| 164 SetPasteboardContent(kRecognizedURL); | 182 SetPasteboardContent(kRecognizedURL); |
| 165 | 183 |
| 166 // Test that recent pasteboard data is provided. | 184 // Test that recent pasteboard data is provided. |
| 167 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 185 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 168 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str()); | 186 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str()); |
| 169 | 187 |
| 170 // Overwrite pasteboard with an image. | 188 // Overwrite pasteboard with an image. |
| 171 base::scoped_nsobject<UIImage> image([[UIImage alloc] init]); | 189 base::scoped_nsobject<UIImage> image([[UIImage alloc] init]); |
| 172 SetPasteboardImage(TestUIImage()); | 190 SetPasteboardImage(TestUIImage()); |
| 173 | 191 |
| 174 // Pasteboard should appear empty. | 192 // Pasteboard should appear empty. |
| 175 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 193 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 176 | 194 |
| 177 // Tests that if URL is added again, pasteboard provides it normally. | 195 // Tests that if URL is added again, pasteboard provides it normally. |
| 178 SetPasteboardContent(kRecognizedURL); | 196 SetPasteboardContent(kRecognizedURL); |
| 179 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 197 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 180 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str()); | 198 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str()); |
| 181 } | 199 } |
| OLD | NEW |