Commit d8262e98 authored by jif's avatar jif Committed by Commit bot

Add unittest to Open from Clipboard component.

BUG=None.

Review URL: https://codereview.chromium.org/961673004

Cr-Commit-Position: refs/heads/master@{#319042}
parent 95a1ab38
...@@ -536,6 +536,7 @@ ...@@ -536,6 +536,7 @@
], ],
}, { # 'OS == "ios"' }, { # 'OS == "ios"'
'sources': [ 'sources': [
'open_from_clipboard/clipboard_recent_content_ios_unittest.mm',
'webp_transcode/webp_decoder_unittest.mm', 'webp_transcode/webp_decoder_unittest.mm',
], ],
'sources/': [ 'sources/': [
...@@ -625,6 +626,7 @@ ...@@ -625,6 +626,7 @@
'../ios/ios_tests.gyp:test_support_ios', '../ios/ios_tests.gyp:test_support_ios',
'../ios/web/ios_web.gyp:test_support_ios_web', '../ios/web/ios_web.gyp:test_support_ios_web',
'../third_party/ocmock/ocmock.gyp:ocmock', '../third_party/ocmock/ocmock.gyp:ocmock',
'components.gyp:open_from_clipboard',
'components.gyp:sessions_ios', 'components.gyp:sessions_ios',
'components.gyp:signin_ios_browser', 'components.gyp:signin_ios_browser',
'components.gyp:translate_ios_browser', 'components.gyp:translate_ios_browser',
......
...@@ -12,6 +12,10 @@ ...@@ -12,6 +12,10 @@
@class NSDate; @class NSDate;
@class PasteboardNotificationListenerBridge; @class PasteboardNotificationListenerBridge;
namespace test {
class ClipboardRecentContentIOSTestHelper;
}
template <typename T> template <typename T>
struct DefaultSingletonTraits; struct DefaultSingletonTraits;
...@@ -25,11 +29,15 @@ class ClipboardRecentContentIOS : public ClipboardRecentContent { ...@@ -25,11 +29,15 @@ class ClipboardRecentContentIOS : public ClipboardRecentContent {
// ClipboardRecentContent implementation. // ClipboardRecentContent implementation.
bool GetRecentURLFromClipboard(GURL* url) const override; bool GetRecentURLFromClipboard(GURL* url) const override;
protected:
// Protected for testing.
ClipboardRecentContentIOS();
~ClipboardRecentContentIOS() override;
private: private:
friend struct DefaultSingletonTraits<ClipboardRecentContentIOS>; friend struct DefaultSingletonTraits<ClipboardRecentContentIOS>;
friend class test::ClipboardRecentContentIOSTestHelper;
ClipboardRecentContentIOS();
~ClipboardRecentContentIOS() override;
// Loads information from the user defaults about the latest pasteboard entry. // Loads information from the user defaults about the latest pasteboard entry.
void LoadFromUserDefaults(); void LoadFromUserDefaults();
// Saves information to the user defaults about the latest pasteboard entry. // Saves information to the user defaults about the latest pasteboard entry.
......
...@@ -100,17 +100,14 @@ bool ClipboardRecentContentIOS::GetRecentURLFromClipboard(GURL* url) const { ...@@ -100,17 +100,14 @@ bool ClipboardRecentContentIOS::GetRecentURLFromClipboard(GURL* url) const {
} }
void ClipboardRecentContentIOS::PasteboardChanged() { void ClipboardRecentContentIOS::PasteboardChanged() {
if ([UIPasteboard generalPasteboard].changeCount != urlFromPasteboardCache_ = URLFromPasteboard();
lastPasteboardChangeCount_) { if (!urlFromPasteboardCache_.is_empty()) {
urlFromPasteboardCache_ = URLFromPasteboard(); base::RecordAction(
if (!urlFromPasteboardCache_.is_empty()) { base::UserMetricsAction("MobileOmniboxClipboardChanged"));
base::RecordAction(
base::UserMetricsAction("MobileOmniboxClipboardChanged"));
}
lastPasteboardChangeDate_.reset([[NSDate date] retain]);
lastPasteboardChangeCount_ = [UIPasteboard generalPasteboard].changeCount;
SaveToUserDefaults();
} }
lastPasteboardChangeDate_.reset([[NSDate date] retain]);
lastPasteboardChangeCount_ = [UIPasteboard generalPasteboard].changeCount;
SaveToUserDefaults();
} }
ClipboardRecentContentIOS::ClipboardRecentContentIOS() ClipboardRecentContentIOS::ClipboardRecentContentIOS()
...@@ -124,7 +121,7 @@ ClipboardRecentContentIOS::ClipboardRecentContentIOS() ...@@ -124,7 +121,7 @@ ClipboardRecentContentIOS::ClipboardRecentContentIOS()
NSInteger changeCount = [UIPasteboard generalPasteboard].changeCount; NSInteger changeCount = [UIPasteboard generalPasteboard].changeCount;
if (changeCount != lastPasteboardChangeCount_ || if (changeCount != lastPasteboardChangeCount_ ||
DeviceRestartedSincePasteboardChanged()) { DeviceRestartedSincePasteboardChanged()) {
PasteboardChanged(); PasteboardChanged();
} }
notificationBridge_.reset( notificationBridge_.reset(
[[PasteboardNotificationListenerBridge alloc] initWithDelegate:this]); [[PasteboardNotificationListenerBridge alloc] initWithDelegate:this]);
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// 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 <UIKit/UIKit.h>
#include "base/memory/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace test {
class ClipboardRecentContentIOSTestHelper : public ClipboardRecentContentIOS {
public:
ClipboardRecentContentIOSTestHelper() {}
~ClipboardRecentContentIOSTestHelper() override {}
void SetStoredPasteboardChangeDate(NSDate* changeDate) {
lastPasteboardChangeDate_.reset([changeDate copy]);
SaveToUserDefaults();
}
};
} // namespace test
namespace {
void SetPasteboardContent(const char* data) {
[[UIPasteboard generalPasteboard]
setValue:[NSString stringWithUTF8String:data]
forPasteboardType:@"public.plain-text"];
}
const char* kUnrecognizedURL = "ftp://foo/";
const char* kRecognizedURL = "http://bar/";
const char* kAppSpecificURL = "test://qux/";
const char* kAppSpecificScheme = "test";
NSTimeInterval kSevenHours = 60 * 60 * 7;
} // namespace
class ClipboardRecentContentIOSTest : public ::testing::Test {
protected:
void SetUp() override {
clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper());
}
void TearDown() override {}
protected:
scoped_ptr<test::ClipboardRecentContentIOSTestHelper> clipboard_content_;
};
TEST_F(ClipboardRecentContentIOSTest, SchemeFiltering) {
GURL gurl;
// Test unrecognized URL.
SetPasteboardContent(kUnrecognizedURL);
EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
// Test recognized URL.
SetPasteboardContent(kRecognizedURL);
EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str());
// Test URL with app specific scheme, before and after configuration.
SetPasteboardContent(kAppSpecificURL);
EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
clipboard_content_->set_application_scheme(kAppSpecificScheme);
SetPasteboardContent(kAppSpecificURL);
EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
EXPECT_STREQ(kAppSpecificURL, gurl.spec().c_str());
}
TEST_F(ClipboardRecentContentIOSTest, PasteboardURLObsolescence) {
GURL gurl;
SetPasteboardContent(kRecognizedURL);
// Test that recent pasteboard data is provided.
EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str());
// Test that old pasteboard data is not provided.
clipboard_content_->SetStoredPasteboardChangeDate(
[NSDate dateWithTimeIntervalSinceNow:-kSevenHours]);
EXPECT_FALSE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
// Test that pasteboard data is treated as new if the last recorded clipboard
// change dates from before the machine booted.
clipboard_content_->SetStoredPasteboardChangeDate(
[NSDate dateWithTimeIntervalSince1970:0]);
clipboard_content_.reset(new test::ClipboardRecentContentIOSTestHelper());
EXPECT_TRUE(clipboard_content_->GetRecentURLFromClipboard(&gurl));
EXPECT_STREQ(kRecognizedURL, gurl.spec().c_str());
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment