OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/open_from_clipboard/clipboard_recent_content_ios.h" |
| 6 |
| 7 #import <UIKit/UIKit.h> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace test { |
| 13 class ClipboardRecentContentIOSTestHelper : public ClipboardRecentContentIOS { |
| 14 public: |
| 15 ClipboardRecentContentIOSTestHelper() {} |
| 16 ~ClipboardRecentContentIOSTestHelper() override {} |
| 17 void SetStoredPasteboardChangeDate(NSDate* changeDate) { |
| 18 lastPasteboardChangeDate_.reset([changeDate copy]); |
| 19 SaveToUserDefaults(); |
| 20 } |
| 21 }; |
| 22 } // namespace test |
| 23 |
| 24 namespace { |
| 25 void SetPasteboardContent(const char* data) { |
| 26 [[UIPasteboard generalPasteboard] |
| 27 setValue:[NSString stringWithUTF8String:data] |
| 28 forPasteboardType:@"public.plain-text"]; |
| 29 } |
| 30 const char* kUnrecognizedURL = "ftp://foo/"; |
| 31 const char* kRecognizedURL = "http://bar/"; |
| 32 const char* kAppSpecificURL = "test://qux/"; |
| 33 const char* kAppSpecificScheme = "test"; |
| 34 NSTimeInterval kSevenHours = 60 * 60 * 7; |
| 35 } // namespace |
| 36 |
| 37 class ClipboardRecentContentIOSTest : public ::testing::Test { |
| 38 protected: |
| 39 void SetUp() override { |
| 40 clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper()); |
| 41 } |
| 42 void TearDown() override {} |
| 43 |
| 44 protected: |
| 45 scoped_ptr<test::ClipboardRecentContentIOSTestHelper> clipboard_content_; |
| 46 }; |
| 47 |
| 48 TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) { |
| 49 GURL gurl; |
| 50 |
| 51 // Test unrecognized URL. |
| 52 SetPasteboardContent(kUnrecognizedURL); |
| 53 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 54 |
| 55 // Test recognized URL. |
| 56 SetPasteboardContent(kRecognizedURL); |
| 57 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 58 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str()); |
| 59 |
| 60 // Test URL with app specific scheme, before and after configuration. |
| 61 SetPasteboardContent(kAppSpecificURL); |
| 62 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 63 clipboard_content_->set_application_scheme(kAppSpecificScheme); |
| 64 SetPasteboardContent(kAppSpecificURL); |
| 65 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 66 EXPECT_STREQ(kAppSpecificURL, gurl.spec().c_str()); |
| 67 } |
| 68 |
| 69 TEST_F(ClipboardRecentContentIOSTest, PasteboardURLObsolescence) { |
| 70 GURL gurl; |
| 71 SetPasteboardContent(kRecognizedURL); |
| 72 |
| 73 // Test that recent pasteboard data is provided. |
| 74 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 75 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str()); |
| 76 |
| 77 // Test that old pasteboard data is not provided. |
| 78 clipboard_content_->SetStoredPasteboardChangeDate( |
| 79 [NSDate dateWithTimeIntervalSinceNow:-kSevenHours]); |
| 80 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 81 |
| 82 // Test that pasteboard data is treated as new if the last recorded clipboard |
| 83 // change dates from before the machine booted. |
| 84 clipboard_content_->SetStoredPasteboardChangeDate( |
| 85 [NSDate dateWithTimeIntervalSince1970:0]); |
| 86 clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper()); |
| 87 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 88 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str()); |
| 89 } |
OLD | NEW |