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 virtual 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(clipboard
,
78 ui::CLIPBOARD_TYPE_COPY_PASTE
);
79 clipboard_writer
.WriteText(kPlainText
);
81 EXPECT_EQ(kPlainText
, OmniboxView::GetClipboardText());
83 // Can we pull a string consists of white-space?
84 const base::string16
kSpace6(ASCIIToUTF16(" "));
85 const base::string16
kSpace1(ASCIIToUTF16(" "));
87 ui::ScopedClipboardWriter
clipboard_writer(clipboard
,
88 ui::CLIPBOARD_TYPE_COPY_PASTE
);
89 clipboard_writer
.WriteText(kSpace6
);
91 EXPECT_EQ(kSpace1
, OmniboxView::GetClipboardText());
93 // Does an empty clipboard get empty text?
94 clipboard
->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE
);
95 EXPECT_EQ(base::string16(), OmniboxView::GetClipboardText());
97 // Bookmark clipboard apparently not supported on Linux.
98 // See TODO on ClipboardText.BookmarkTest.
99 #if !defined(OS_POSIX) || defined(OS_MACOSX)
100 const base::string16
kTitle(ASCIIToUTF16("The Example Company"));
101 // Can we pull a bookmark off the clipboard?
103 ui::ScopedClipboardWriter
clipboard_writer(clipboard
,
104 ui::CLIPBOARD_TYPE_COPY_PASTE
);
105 clipboard_writer
.WriteBookmark(kTitle
, kURL
);
107 EXPECT_EQ(ASCIIToUTF16(kURL
), OmniboxView::GetClipboardText());
109 // Do we pull text in preference to a bookmark?
111 ui::ScopedClipboardWriter
clipboard_writer(clipboard
,
112 ui::CLIPBOARD_TYPE_COPY_PASTE
);
113 clipboard_writer
.WriteText(kPlainText
);
114 clipboard_writer
.WriteBookmark(kTitle
, kURL
);
116 EXPECT_EQ(kPlainText
, OmniboxView::GetClipboardText());
119 // Do we get nothing if there is neither text nor a bookmark?
121 const base::string16
kMarkup(ASCIIToUTF16("<strong>Hi!</string>"));
122 ui::ScopedClipboardWriter
clipboard_writer(clipboard
,
123 ui::CLIPBOARD_TYPE_COPY_PASTE
);
124 clipboard_writer
.WriteHTML(kMarkup
, kURL
);
126 EXPECT_TRUE(OmniboxView::GetClipboardText().empty());