| 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 <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
| 8 | 8 |
| 9 #include "base/ios/ios_util.h" | 9 #include "base/ios/ios_util.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "testing/platform_test.h" | 12 #include "testing/platform_test.h" |
| 13 | 13 |
| 14 namespace test { | |
| 15 class ClipboardRecentContentIOSTestHelper : public ClipboardRecentContentIOS { | |
| 16 public: | |
| 17 // By default, set that the device booted 10 days ago. | |
| 18 ClipboardRecentContentIOSTestHelper() | |
| 19 : ClipboardRecentContentIOS(base::TimeDelta::FromDays(10)) {} | |
| 20 ClipboardRecentContentIOSTestHelper(base::TimeDelta t) | |
| 21 : ClipboardRecentContentIOS(t) {} | |
| 22 | |
| 23 ~ClipboardRecentContentIOSTestHelper() override {} | |
| 24 void SetStoredPasteboardChangeDate(NSDate* changeDate) { | |
| 25 lastPasteboardChangeDate_.reset([changeDate copy]); | |
| 26 SaveToUserDefaults(); | |
| 27 } | |
| 28 void SetStoredPasteboardChangeCount(NSInteger newChangeCount) { | |
| 29 lastPasteboardChangeCount_ = newChangeCount; | |
| 30 SaveToUserDefaults(); | |
| 31 } | |
| 32 }; | |
| 33 } // namespace test | |
| 34 | |
| 35 namespace { | 14 namespace { |
| 36 void SetPasteboardContent(const char* data) { | 15 void SetPasteboardContent(const char* data) { |
| 37 [[UIPasteboard generalPasteboard] | 16 [[UIPasteboard generalPasteboard] |
| 38 setValue:[NSString stringWithUTF8String:data] | 17 setValue:[NSString stringWithUTF8String:data] |
| 39 forPasteboardType:@"public.plain-text"]; | 18 forPasteboardType:@"public.plain-text"]; |
| 40 } | 19 } |
| 41 const char* kUnrecognizedURL = "ftp://foo/"; | 20 const char kUnrecognizedURL[] = "ftp://foo/"; |
| 42 const char* kRecognizedURL = "http://bar/"; | 21 const char kRecognizedURL[] = "http://bar/"; |
| 43 const char* kAppSpecificURL = "test://qux/"; | 22 const char kAppSpecificURL[] = "test://qux/"; |
| 44 const char* kAppSpecificScheme = "test"; | 23 const char kAppSpecificScheme[] = "test"; |
| 45 NSTimeInterval kSevenHours = 60 * 60 * 7; | 24 NSTimeInterval kSevenHours = 60 * 60 * 7; |
| 46 } // namespace | 25 } // namespace |
| 47 | 26 |
| 48 class ClipboardRecentContentIOSTest : public ::testing::Test { | 27 class ClipboardRecentContentIOSTest : public ::testing::Test { |
| 49 protected: | 28 protected: |
| 50 void SetUp() override { | 29 ClipboardRecentContentIOSTest() { |
| 51 clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper()); | 30 // By default, set that the device booted 10 days ago. |
| 31 ResetClipboardRecentContent(kAppSpecificScheme, |
| 32 base::TimeDelta::FromDays(10)); |
| 52 } | 33 } |
| 53 void TearDown() override {} | |
| 54 | 34 |
| 55 void SimulateDeviceRestart() { | 35 void SimulateDeviceRestart() { |
| 56 // TODO(jif): Simulates the fact that on iOS7, the pasteboard's changeCount | 36 // TODO(jif): Simulates the fact that on iOS7, the pasteboard's changeCount |
| 57 // is reset. http://crbug.com/503609 | 37 // is reset. http://crbug.com/503609 |
| 58 clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper( | 38 ResetClipboardRecentContent(kAppSpecificScheme, |
| 59 base::TimeDelta::FromSeconds(0))); | 39 base::TimeDelta::FromSeconds(0)); |
| 40 } |
| 41 |
| 42 void ResetClipboardRecentContent(const std::string& application_scheme, |
| 43 base::TimeDelta time_delta) { |
| 44 clipboard_content_.reset( |
| 45 new ClipboardRecentContentIOS(application_scheme, time_delta)); |
| 46 } |
| 47 |
| 48 void SetStoredPasteboardChangeDate(NSDate* changeDate) { |
| 49 clipboard_content_->lastPasteboardChangeDate_.reset([changeDate copy]); |
| 50 clipboard_content_->SaveToUserDefaults(); |
| 51 } |
| 52 |
| 53 void SetStoredPasteboardChangeCount(NSInteger newChangeCount) { |
| 54 clipboard_content_->lastPasteboardChangeCount_ = newChangeCount; |
| 55 clipboard_content_->SaveToUserDefaults(); |
| 60 } | 56 } |
| 61 | 57 |
| 62 protected: | 58 protected: |
| 63 scoped_ptr<test::ClipboardRecentContentIOSTestHelper> clipboard_content_; | 59 scoped_ptr<ClipboardRecentContentIOS> clipboard_content_; |
| 64 }; | 60 }; |
| 65 | 61 |
| 66 TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) { | 62 TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) { |
| 67 GURL gurl; | 63 GURL gurl; |
| 68 | 64 |
| 69 // Test unrecognized URL. | 65 // Test unrecognized URL. |
| 70 SetPasteboardContent(kUnrecognizedURL); | 66 SetPasteboardContent(kUnrecognizedURL); |
| 71 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 67 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 72 | 68 |
| 73 // Test recognized URL. | 69 // Test recognized URL. |
| 74 SetPasteboardContent(kRecognizedURL); | 70 SetPasteboardContent(kRecognizedURL); |
| 75 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 71 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 76 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str()); | 72 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str()); |
| 77 | 73 |
| 78 // Test URL with app specific scheme, before and after configuration. | 74 // Test URL with app specific scheme. |
| 79 SetPasteboardContent(kAppSpecificURL); | |
| 80 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | |
| 81 clipboard_content_->set_application_scheme(kAppSpecificScheme); | |
| 82 SetPasteboardContent(kAppSpecificURL); | 75 SetPasteboardContent(kAppSpecificURL); |
| 83 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 76 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 84 EXPECT_STREQ(kAppSpecificURL, gurl.spec().c_str()); | 77 EXPECT_STREQ(kAppSpecificURL, gurl.spec().c_str()); |
| 78 |
| 79 // Test URL without app specific scheme. |
| 80 ResetClipboardRecentContent(std::string(), base::TimeDelta::FromDays(10)); |
| 81 |
| 82 SetPasteboardContent(kAppSpecificURL); |
| 83 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 85 } | 84 } |
| 86 | 85 |
| 87 TEST_F(ClipboardRecentContentIOSTest, PasteboardURLObsolescence) { | 86 TEST_F(ClipboardRecentContentIOSTest, PasteboardURLObsolescence) { |
| 88 GURL gurl; | 87 GURL gurl; |
| 89 SetPasteboardContent(kRecognizedURL); | 88 SetPasteboardContent(kRecognizedURL); |
| 90 | 89 |
| 91 // Test that recent pasteboard data is provided. | 90 // Test that recent pasteboard data is provided. |
| 92 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 91 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 93 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str()); | 92 EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str()); |
| 94 | 93 |
| 95 // Test that old pasteboard data is not provided. | 94 // Test that old pasteboard data is not provided. |
| 96 clipboard_content_->SetStoredPasteboardChangeDate( | 95 SetStoredPasteboardChangeDate( |
| 97 [NSDate dateWithTimeIntervalSinceNow:-kSevenHours]); | 96 [NSDate dateWithTimeIntervalSinceNow:-kSevenHours]); |
| 98 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 97 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 99 | 98 |
| 100 // Tests that if chrome is relaunched, old pasteboard data is still | 99 // Tests that if chrome is relaunched, old pasteboard data is still |
| 101 // not provided. | 100 // not provided. |
| 102 clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper()); | 101 ResetClipboardRecentContent(kAppSpecificScheme, |
| 102 base::TimeDelta::FromDays(10)); |
| 103 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 103 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 104 | 104 |
| 105 | |
| 106 SimulateDeviceRestart(); | 105 SimulateDeviceRestart(); |
| 107 if (base::ios::IsRunningOnIOS8OrLater()) { | 106 if (base::ios::IsRunningOnIOS8OrLater()) { |
| 108 // Tests that if the device is restarted, old pasteboard data is still | 107 // Tests that if the device is restarted, old pasteboard data is still |
| 109 // not provided. | 108 // not provided. |
| 110 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 109 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 111 } else { | 110 } else { |
| 112 // TODO(jif): Simulates the fact that on iOS7, the pasteboard's changeCount | 111 // TODO(jif): Simulates the fact that on iOS7, the pasteboard's changeCount |
| 113 // is reset. http://crbug.com/503609 | 112 // is reset. http://crbug.com/503609 |
| 114 } | 113 } |
| 115 | 114 |
| 116 } | 115 } |
| 117 | 116 |
| 118 TEST_F(ClipboardRecentContentIOSTest, SupressedPasteboard) { | 117 TEST_F(ClipboardRecentContentIOSTest, SupressedPasteboard) { |
| 119 GURL gurl; | 118 GURL gurl; |
| 120 SetPasteboardContent(kRecognizedURL); | 119 SetPasteboardContent(kRecognizedURL); |
| 121 | 120 |
| 122 // Test that recent pasteboard data is provided. | 121 // Test that recent pasteboard data is provided. |
| 123 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 122 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 124 | 123 |
| 125 // Suppress the content of the pasteboard. | 124 // Suppress the content of the pasteboard. |
| 126 clipboard_content_->SuppressClipboardContent(); | 125 clipboard_content_->SuppressClipboardContent(); |
| 127 | 126 |
| 128 // Check that the pasteboard content is suppressed. | 127 // Check that the pasteboard content is suppressed. |
| 129 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 128 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 130 | 129 |
| 131 // Create a new clipboard content to test persistence. | 130 // Create a new clipboard content to test persistence. |
| 132 clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper()); | 131 ResetClipboardRecentContent(kAppSpecificScheme, |
| 132 base::TimeDelta::FromDays(10)); |
| 133 | 133 |
| 134 // Check that the pasteboard content is still suppressed. | 134 // Check that the pasteboard content is still suppressed. |
| 135 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 135 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 136 | 136 |
| 137 // Check that the even if the device is restarted, pasteboard content is | 137 // Check that the even if the device is restarted, pasteboard content is |
| 138 // still suppressed. | 138 // still suppressed. |
| 139 SimulateDeviceRestart(); | 139 SimulateDeviceRestart(); |
| 140 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 140 EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 141 | 141 |
| 142 // Check that if the pasteboard changes, the new content is not | 142 // Check that if the pasteboard changes, the new content is not |
| 143 // supressed anymore. | 143 // supressed anymore. |
| 144 SetPasteboardContent(kRecognizedURL); | 144 SetPasteboardContent(kRecognizedURL); |
| 145 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); | 145 EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl)); |
| 146 } | 146 } |
| OLD | NEW |