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"
17 class OmniboxViewTest
: public PlatformTest
{
19 virtual void TearDown() OVERRIDE
{
20 ui::Clipboard::DestroyClipboardForCurrentThread();
24 // Windows requires a message loop for clipboard access.
25 base::MessageLoopForUI message_loop_
;
28 TEST_F(OmniboxViewTest
, TestStripSchemasUnsafeForPaste
) {
29 const char* urls
[] = {
30 "http://www.google.com?q=javascript:alert(0)", // Safe URL.
31 "javAscript:alert(0)", // Unsafe JS URL.
32 "jaVascript:\njavaScript: alert(0)" // Single strip unsafe.
35 const char* expecteds
[] = {
36 "http://www.google.com?q=javascript:alert(0)", // Safe URL.
37 "alert(0)", // Unsafe JS URL.
38 "alert(0)" // Single strip unsafe.
41 for (size_t i
= 0; i
< arraysize(urls
); i
++) {
42 EXPECT_EQ(ASCIIToUTF16(expecteds
[i
]),
43 OmniboxView::StripJavascriptSchemas(ASCIIToUTF16(urls
[i
])));
47 TEST_F(OmniboxViewTest
, SanitizeTextForPaste
) {
48 // Broken URL has newlines stripped.
49 const string16
kWrappedURL(ASCIIToUTF16(
50 "http://www.chromium.org/developers/testing/chromium-\n"
51 "build-infrastructure/tour-of-the-chromium-buildbot"));
53 const string16
kFixedURL(ASCIIToUTF16(
54 "http://www.chromium.org/developers/testing/chromium-"
55 "build-infrastructure/tour-of-the-chromium-buildbot"));
56 EXPECT_EQ(kFixedURL
, OmniboxView::SanitizeTextForPaste(kWrappedURL
));
58 // Multi-line address is converted to a single-line address.
59 const string16
kWrappedAddress(ASCIIToUTF16(
60 "1600 Amphitheatre Parkway\nMountain View, CA"));
62 const string16
kFixedAddress(ASCIIToUTF16(
63 "1600 Amphitheatre Parkway Mountain View, CA"));
64 EXPECT_EQ(kFixedAddress
, OmniboxView::SanitizeTextForPaste(kWrappedAddress
));
67 TEST_F(OmniboxViewTest
, GetClipboardText
) {
68 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
70 const string16
kPlainText(ASCIIToUTF16("test text"));
71 const std::string
kURL("http://www.example.com/");
73 // Can we pull straight text off the clipboard?
75 ui::ScopedClipboardWriter
clipboard_writer(clipboard
,
76 ui::Clipboard::BUFFER_STANDARD
);
77 clipboard_writer
.WriteText(kPlainText
);
79 EXPECT_EQ(kPlainText
, OmniboxView::GetClipboardText());
81 // Can we pull a string consists of white-space?
82 const string16
kSpace6(ASCIIToUTF16(" "));
83 const string16
kSpace1(ASCIIToUTF16(" "));
85 ui::ScopedClipboardWriter
clipboard_writer(clipboard
,
86 ui::Clipboard::BUFFER_STANDARD
);
87 clipboard_writer
.WriteText(kSpace6
);
89 EXPECT_EQ(kSpace1
, OmniboxView::GetClipboardText());
91 // TODO(shess): ChromeOS hits a DCHECK() at CommitToClipboard() if
92 // ObjectMap is empty. http://crbug.com/133848
93 #if !defined(OS_CHROMEOS)
94 // Does an empty clipboard get empty text?
95 clipboard
->WriteObjects(ui::Clipboard::BUFFER_STANDARD
,
96 ui::Clipboard::ObjectMap());
97 EXPECT_EQ(string16(), OmniboxView::GetClipboardText());
100 // Bookmark clipboard apparently not supported on Linux.
101 // See TODO on ClipboardText.BookmarkTest.
102 #if !defined(OS_POSIX) || defined(OS_MACOSX)
103 const string16
kTitle(ASCIIToUTF16("The Example Company"));
104 // Can we pull a bookmark off the clipboard?
106 ui::ScopedClipboardWriter
clipboard_writer(clipboard
,
107 ui::Clipboard::BUFFER_STANDARD
);
108 clipboard_writer
.WriteBookmark(kTitle
, kURL
);
110 EXPECT_EQ(ASCIIToUTF16(kURL
), OmniboxView::GetClipboardText());
112 // Do we pull text in preference to a bookmark?
114 ui::ScopedClipboardWriter
clipboard_writer(clipboard
,
115 ui::Clipboard::BUFFER_STANDARD
);
116 clipboard_writer
.WriteText(kPlainText
);
117 clipboard_writer
.WriteBookmark(kTitle
, kURL
);
119 EXPECT_EQ(kPlainText
, OmniboxView::GetClipboardText());
122 // Do we get nothing if there is neither text nor a bookmark?
124 const string16
kMarkup(ASCIIToUTF16("<strong>Hi!</string>"));
125 ui::ScopedClipboardWriter
clipboard_writer(clipboard
,
126 ui::Clipboard::BUFFER_STANDARD
);
127 clipboard_writer
.WriteHTML(kMarkup
, kURL
);
129 EXPECT_TRUE(OmniboxView::GetClipboardText().empty());