1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/open_from_clipboard/clipboard_recent_content_ios.h"
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKit.h>
#include <memory>
#include "base/functional/bind.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#import "base/test/ios/wait_util.h"
#include "base/test/task_environment.h"
#import "components/open_from_clipboard/clipboard_recent_content_impl_ios.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
using base::test::ios::WaitUntilConditionOrTimeout;
using base::test::ios::kWaitForCookiesTimeout;
using base::test::ios::kWaitForActionTimeout;
namespace {
UIImage* TestUIImage(UIColor* color = [UIColor redColor]) {
CGRect frame = CGRectMake(0, 0, 1.0, 1.0);
UIGraphicsBeginImageContext(frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, frame);
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
void SetPasteboardImage(UIImage* image) {
[[UIPasteboard generalPasteboard] setImage:image];
}
void SetPasteboardContent(const char* data) {
[[UIPasteboard generalPasteboard]
setValue:[NSString stringWithUTF8String:data]
forPasteboardType:@"public.plain-text"];
}
const char kUnrecognizedURL[] = "bad://foo/";
const char kRecognizedURL[] = "good://bar/";
const char kRecognizedURL2[] = "good://bar/2";
const char kAppSpecificURL[] = "test://qux/";
const char kAppSpecificScheme[] = "test";
const char kRecognizedScheme[] = "good";
NSTimeInterval kLongerThanMaxAge = 60 * 60 * 7;
NSTimeInterval kMaxAge = 60 * 60 * 1;
} // namespace
@interface ClipboardRecentContentImplIOSWithFakeUptime
: ClipboardRecentContentImplIOS
@property(nonatomic) NSTimeInterval fakeUptime;
- (instancetype)initWithMaxAge:(NSTimeInterval)maxAge
authorizedSchemes:(NSArray*)authorizedSchemes
userDefaults:(NSUserDefaults*)groupUserDefaults
onlyUseClipboardAsync:(BOOL)onlyUseClipboardAsync
uptime:(NSTimeInterval)uptime;
@end
@implementation ClipboardRecentContentImplIOSWithFakeUptime
@synthesize fakeUptime = _fakeUptime;
- (instancetype)initWithMaxAge:(NSTimeInterval)maxAge
authorizedSchemes:(NSSet*)authorizedSchemes
userDefaults:(NSUserDefaults*)groupUserDefaults
onlyUseClipboardAsync:(BOOL)onlyUseClipboardAsync
uptime:(NSTimeInterval)uptime {
self = [super initWithMaxAge:maxAge
authorizedSchemes:authorizedSchemes
userDefaults:groupUserDefaults
onlyUseClipboardAsync:onlyUseClipboardAsync
delegate:nil];
if (self) {
_fakeUptime = uptime;
}
return self;
}
- (NSTimeInterval)uptime {
return self.fakeUptime;
}
@end
class ClipboardRecentContentIOSWithFakeUptime
: public ClipboardRecentContentIOS {
public:
ClipboardRecentContentIOSWithFakeUptime(
ClipboardRecentContentImplIOS* implementation)
: ClipboardRecentContentIOS(implementation) {}
};
class ClipboardRecentContentIOSTest : public ::testing::Test {
protected:
ClipboardRecentContentIOSTest() {
// By default, set that the device booted 10 days ago.
ResetClipboardRecentContent(kAppSpecificScheme, base::Days(10));
}
void SimulateDeviceRestart() {
ResetClipboardRecentContent(kAppSpecificScheme, base::Seconds(0));
}
void ResetClipboardRecentContent(const std::string& application_scheme,
base::TimeDelta time_delta) {
ClipboardRecentContentImplIOSWithFakeUptime*
clipboard_content_implementation =
[[ClipboardRecentContentImplIOSWithFakeUptime alloc]
initWithMaxAge:kMaxAge
authorizedSchemes:@[
base::SysUTF8ToNSString(kRecognizedScheme),
base::SysUTF8ToNSString(application_scheme)
]
userDefaults:[NSUserDefaults standardUserDefaults]
onlyUseClipboardAsync:NO
uptime:time_delta.InSecondsF()];
clipboard_content_ =
std::make_unique<ClipboardRecentContentIOSWithFakeUptime>(
clipboard_content_implementation);
// Keep a weak pointer to the ClipboardRecentContentImplIOS to allow
// updating the fake pasteboard change date.
clipboard_content_implementation_ = clipboard_content_implementation;
}
void SetStoredPasteboardChangeDate(NSDate* change_date) {
clipboard_content_implementation_.lastPasteboardChangeDate = change_date;
[clipboard_content_implementation_ saveToUserDefaults];
}
protected:
std::unique_ptr<ClipboardRecentContentIOSWithFakeUptime> clipboard_content_;
__weak ClipboardRecentContentImplIOSWithFakeUptime*
clipboard_content_implementation_;
base::test::TaskEnvironment task_environment_;
void VerifyClipboardTypeExists(ClipboardContentType type, bool exists) {
__block BOOL callback_called = NO;
__block BOOL type_exists = NO;
std::set<ClipboardContentType> types;
types.insert(type);
clipboard_content_->HasRecentContentFromClipboard(
types, base::BindOnce(^(std::set<ClipboardContentType> found_types) {
callback_called = YES;
type_exists = found_types.find(type) != found_types.end();
}));
EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForCookiesTimeout, ^bool {
base::RunLoop().RunUntilIdle();
return callback_called;
}));
EXPECT_EQ(exists, type_exists);
}
void VerifyClipboardURLExists(const char* expected_url) {
VerifyClipboardTypeExists(ClipboardContentType::URL, true);
__block BOOL callback_called = NO;
__block std::optional<GURL> optional_gurl;
clipboard_content_->GetRecentURLFromClipboard(
base::BindOnce(^(std::optional<GURL> copied_url) {
optional_gurl = copied_url;
callback_called = YES;
}));
EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForCookiesTimeout, ^bool {
base::RunLoop().RunUntilIdle();
return callback_called;
}));
ASSERT_TRUE(optional_gurl.has_value());
EXPECT_STREQ(expected_url, optional_gurl.value().spec().c_str());
}
bool VerifyCacheClipboardContentTypeExists(ClipboardContentType type) {
std::optional<std::set<ClipboardContentType>> cached_content_types =
clipboard_content_->GetCachedClipboardContentTypes();
if (cached_content_types.has_value()) {
return cached_content_types.value().find(type) !=
cached_content_types.value().end();
} else {
return false;
}
}
void VerifiyClipboardURLIsInvalid() {
VerifyClipboardTypeExists(ClipboardContentType::URL, true);
__block BOOL callback_called = NO;
__block std::optional<GURL> optional_gurl;
clipboard_content_->GetRecentURLFromClipboard(
base::BindOnce(^(std::optional<GURL> copied_url) {
optional_gurl = copied_url;
callback_called = YES;
}));
EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForCookiesTimeout, ^bool {
base::RunLoop().RunUntilIdle();
return callback_called;
}));
EXPECT_FALSE(optional_gurl.has_value());
}
bool WaitForClipboardContentTypesRefresh() {
bool success = WaitUntilConditionOrTimeout(kWaitForActionTimeout, ^bool() {
return clipboard_content_->GetCachedClipboardContentTypes().has_value();
});
return success;
}
};
TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) {
// Test unrecognized URL.
SetPasteboardContent(kUnrecognizedURL);
VerifiyClipboardURLIsInvalid();
// Test recognized URL.
SetPasteboardContent(kRecognizedURL);
VerifyClipboardURLExists(kRecognizedURL);
// Test URL with app specific scheme.
SetPasteboardContent(kAppSpecificURL);
VerifyClipboardURLExists(kAppSpecificURL);
// Test URL without app specific scheme.
ResetClipboardRecentContent(std::string(), base::Days(10));
SetPasteboardContent(kAppSpecificURL);
VerifiyClipboardURLIsInvalid();
}
TEST_F(ClipboardRecentContentIOSTest, PasteboardURLObsolescence) {
SetPasteboardContent(kRecognizedURL);
// Test that recent pasteboard data is provided.
VerifyClipboardURLExists(kRecognizedURL);
// Test that old pasteboard data is not provided.
SetStoredPasteboardChangeDate(
[NSDate dateWithTimeIntervalSinceNow:-kLongerThanMaxAge]);
VerifyClipboardTypeExists(ClipboardContentType::URL, false);
VerifyClipboardTypeExists(ClipboardContentType::Text, false);
// Tests that if chrome is relaunched, old pasteboard data is still
// not provided.
ResetClipboardRecentContent(kAppSpecificScheme, base::Days(10));
VerifyClipboardTypeExists(ClipboardContentType::URL, false);
VerifyClipboardTypeExists(ClipboardContentType::Text, false);
SimulateDeviceRestart();
// Tests that if the device is restarted, old pasteboard data is still
// not provided.
VerifyClipboardTypeExists(ClipboardContentType::URL, false);
VerifyClipboardTypeExists(ClipboardContentType::Text, false);
}
TEST_F(ClipboardRecentContentIOSTest,
CacheClipboardContentTypesUpdatesForCopiedURL) {
SetPasteboardContent(kRecognizedURL);
ASSERT_TRUE(WaitForClipboardContentTypesRefresh());
EXPECT_TRUE(VerifyCacheClipboardContentTypeExists(ClipboardContentType::URL));
EXPECT_FALSE(
VerifyCacheClipboardContentTypeExists(ClipboardContentType::Image));
EXPECT_FALSE(
VerifyCacheClipboardContentTypeExists(ClipboardContentType::Text));
}
TEST_F(ClipboardRecentContentIOSTest,
CacheClipboardContentTypesUpdatesForCopiedImage) {
SetPasteboardImage(TestUIImage());
ASSERT_TRUE(WaitForClipboardContentTypesRefresh());
EXPECT_TRUE(
VerifyCacheClipboardContentTypeExists(ClipboardContentType::Image));
EXPECT_FALSE(
VerifyCacheClipboardContentTypeExists(ClipboardContentType::URL));
EXPECT_FALSE(
VerifyCacheClipboardContentTypeExists(ClipboardContentType::Text));
}
TEST_F(ClipboardRecentContentIOSTest,
CacheClipboardContentTypesUpdatesForCopiedText) {
SetPasteboardContent("foobar");
ASSERT_TRUE(WaitForClipboardContentTypesRefresh());
EXPECT_TRUE(
VerifyCacheClipboardContentTypeExists(ClipboardContentType::Text));
EXPECT_FALSE(
VerifyCacheClipboardContentTypeExists(ClipboardContentType::Image));
EXPECT_FALSE(
VerifyCacheClipboardContentTypeExists(ClipboardContentType::URL));
}
// Checks that if the pasteboard is marked as having confidential data, it is
// not returned.
TEST_F(ClipboardRecentContentIOSTest, ConfidentialPasteboardText) {
[[UIPasteboard generalPasteboard]
setItems:@[ @{
@"public.plain-text" : @"hunter2",
@"org.nspasteboard.ConcealedType" : @"hunter2"
} ]
options:@{}];
VerifyClipboardTypeExists(ClipboardContentType::Text, false);
}
// Checks that if the user suppresses content, no text will be returned,
// and if the text changes, the new text will be returned again.
TEST_F(ClipboardRecentContentIOSTest, SuppressedPasteboardContent) {
SetPasteboardContent(kRecognizedURL);
// Test that recent pasteboard data is provided.
VerifyClipboardURLExists(kRecognizedURL);
// Suppress the content of the pasteboard.
clipboard_content_->SuppressClipboardContent();
// Check that the pasteboard content is suppressed.
VerifyClipboardTypeExists(ClipboardContentType::URL, false);
// Create a new clipboard content to test persistence.
ResetClipboardRecentContent(kAppSpecificScheme, base::Days(10));
// Check that the pasteboard content is still suppressed.
VerifyClipboardTypeExists(ClipboardContentType::URL, false);
// Check that even if the device is restarted, pasteboard content is
// still suppressed.
SimulateDeviceRestart();
VerifyClipboardTypeExists(ClipboardContentType::URL, false);
// Check that if the pasteboard changes, the new content is not
// suppressed anymore.
SetPasteboardContent(kRecognizedURL2);
VerifyClipboardURLExists(kRecognizedURL2);
}
// TODO(crbug.com/40275048): This test is flaky.
// Checks that if the user suppresses content, no image will be returned,
// and if the image changes, the new image will be returned again.
TEST_F(ClipboardRecentContentIOSTest, DISABLED_SuppressedPasteboardImage) {
SetPasteboardImage(TestUIImage());
// Test that recent pasteboard data is provided.
VerifyClipboardTypeExists(ClipboardContentType::Image, true);
// Suppress the content of the pasteboard.
clipboard_content_->SuppressClipboardContent();
// Check that the pasteboard content is suppressed.
VerifyClipboardTypeExists(ClipboardContentType::Image, false);
// Create a new clipboard content to test persistence.
ResetClipboardRecentContent(kAppSpecificScheme, base::Days(10));
// Check that the pasteboard content is still suppressed.
VerifyClipboardTypeExists(ClipboardContentType::Image, false);
// Check that even if the device is restarted, pasteboard content is
// still suppressed.
SimulateDeviceRestart();
VerifyClipboardTypeExists(ClipboardContentType::Image, false);
// Check that if the pasteboard changes, the new content is not
// suppressed anymore.
SetPasteboardImage(TestUIImage([UIColor greenColor]));
VerifyClipboardTypeExists(ClipboardContentType::Image, true);
}
// TODO(crbug.com/40275048): This test is flaky.
// Checks that if user copies something other than a string we don't cache the
// string in pasteboard.
TEST_F(ClipboardRecentContentIOSTest,
DISABLED_AddingNonStringRemovesCachedString) {
SetPasteboardContent(kRecognizedURL);
// Test that recent pasteboard data is provided as url.
VerifyClipboardURLExists(kRecognizedURL);
VerifyClipboardTypeExists(ClipboardContentType::Text, false);
// Image pasteboard should be empty.
VerifyClipboardTypeExists(ClipboardContentType::Image, false);
// Overwrite pasteboard with an image.
SetPasteboardImage(TestUIImage());
// Url and text pasteboard should appear empty.
VerifyClipboardTypeExists(ClipboardContentType::URL, false);
VerifyClipboardTypeExists(ClipboardContentType::Text, false);
// Image pasteboard should be full
VerifyClipboardTypeExists(ClipboardContentType::Image, true);
// Tests that if URL is added again, pasteboard provides it normally.
SetPasteboardContent(kRecognizedURL);
VerifyClipboardURLExists(kRecognizedURL);
VerifyClipboardTypeExists(ClipboardContentType::Text, false);
// Image pasteboard should be empty.
VerifyClipboardTypeExists(ClipboardContentType::Image, false);
}
|