1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ui/omnibox/omnibox_view.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/strings/string16.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h"
12 #include "ui/base/clipboard/clipboard.h"
13 #include "ui/base/clipboard/scoped_clipboard_writer.h"
15 using base::ASCIIToUTF16
;
19 class OmniboxViewTest
: public PlatformTest
{
21 void TearDown() override
{
22 ui::Clipboard::DestroyClipboardForCurrentThread();
26 // Windows requires a message loop for clipboard access.
27 base::MessageLoopForUI message_loop_
;
30 TEST_F(OmniboxViewTest
, TestStripSchemasUnsafeForPaste
) {
31 const char* urls
[] = {
32 "http://www.google.com?q=javascript:alert(0)", // Safe URL.
33 "javAscript:alert(0)", // Unsafe JS URL.
34 "jaVascript:\njavaScript: alert(0)" // Single strip unsafe.
37 const char* expecteds
[] = {
38 "http://www.google.com?q=javascript:alert(0)", // Safe URL.
39 "alert(0)", // Unsafe JS URL.
40 "alert(0)" // Single strip unsafe.
43 for (size_t i
= 0; i
< arraysize(urls
); i
++) {
44 EXPECT_EQ(ASCIIToUTF16(expecteds
[i
]),
45 OmniboxView::StripJavascriptSchemas(ASCIIToUTF16(urls
[i
])));
49 TEST_F(OmniboxViewTest
, SanitizeTextForPaste
) {
50 // Broken URL has newlines stripped.
51 const base::string16
kWrappedURL(ASCIIToUTF16(
52 "http://www.chromium.org/developers/testing/chromium-\n"
53 "build-infrastructure/tour-of-the-chromium-buildbot"));
55 const base::string16
kFixedURL(ASCIIToUTF16(
56 "http://www.chromium.org/developers/testing/chromium-"
57 "build-infrastructure/tour-of-the-chromium-buildbot"));
58 EXPECT_EQ(kFixedURL
, OmniboxView::SanitizeTextForPaste(kWrappedURL
));
60 // Multi-line address is converted to a single-line address.
61 const base::string16
kWrappedAddress(ASCIIToUTF16(
62 "1600 Amphitheatre Parkway\nMountain View, CA"));
64 const base::string16
kFixedAddress(ASCIIToUTF16(
65 "1600 Amphitheatre Parkway Mountain View, CA"));
66 EXPECT_EQ(kFixedAddress
, OmniboxView::SanitizeTextForPaste(kWrappedAddress
));
69 TEST_F(OmniboxViewTest
, GetClipboardText
) {
70 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
72 const base::string16
kPlainText(ASCIIToUTF16("test text"));
73 const std::string
kURL("http://www.example.com/");
75 // Can we pull straight text off the clipboard?
77 ui::ScopedClipboardWriter
clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE
);
78 clipboard_writer
.WriteText(kPlainText
);
80 EXPECT_EQ(kPlainText
, OmniboxView::GetClipboardText());
82 // Can we pull a string consists of white-space?
83 const base::string16
kSpace6(ASCIIToUTF16(" "));
84 const base::string16
kSpace1(ASCIIToUTF16(" "));
86 ui::ScopedClipboardWriter
clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE
);
87 clipboard_writer
.WriteText(kSpace6
);
89 EXPECT_EQ(kSpace1
, OmniboxView::GetClipboardText());
91 // Does an empty clipboard get empty text?
92 clipboard
->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE
);
93 EXPECT_EQ(base::string16(), OmniboxView::GetClipboardText());
95 // Bookmark clipboard apparently not supported on Linux.
96 // See TODO on ClipboardText.BookmarkTest.
97 #if !defined(OS_POSIX) || defined(OS_MACOSX)
98 const base::string16
kTitle(ASCIIToUTF16("The Example Company"));
99 // Can we pull a bookmark off the clipboard?
101 ui::ScopedClipboardWriter
clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE
);
102 clipboard_writer
.WriteBookmark(kTitle
, kURL
);
104 EXPECT_EQ(ASCIIToUTF16(kURL
), OmniboxView::GetClipboardText());
106 // Do we pull text in preference to a bookmark?
108 ui::ScopedClipboardWriter
clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE
);
109 clipboard_writer
.WriteText(kPlainText
);
110 clipboard_writer
.WriteBookmark(kTitle
, kURL
);
112 EXPECT_EQ(kPlainText
, OmniboxView::GetClipboardText());
115 // Do we get nothing if there is neither text nor a bookmark?
117 const base::string16
kMarkup(ASCIIToUTF16("<strong>Hi!</string>"));
118 ui::ScopedClipboardWriter
clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE
);
119 clipboard_writer
.WriteHTML(kMarkup
, kURL
);
121 EXPECT_TRUE(OmniboxView::GetClipboardText().empty());