NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / omnibox / omnibox_view_unittest.cc
blobbb2cdb5c7b358e91ec2bb40bb53a2d580073c83f
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;
17 namespace {
19 class OmniboxViewTest : public PlatformTest {
20 public:
21 virtual void TearDown() OVERRIDE {
22 ui::Clipboard::DestroyClipboardForCurrentThread();
25 private:
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 // TODO(shess): ChromeOS hits a DCHECK() at CommitToClipboard() if
94 // ObjectMap is empty. http://crbug.com/133848
95 #if !defined(OS_CHROMEOS)
96 // Does an empty clipboard get empty text?
97 clipboard->WriteObjects(ui::CLIPBOARD_TYPE_COPY_PASTE,
98 ui::Clipboard::ObjectMap());
99 EXPECT_EQ(base::string16(), OmniboxView::GetClipboardText());
100 #endif
102 // Bookmark clipboard apparently not supported on Linux.
103 // See TODO on ClipboardText.BookmarkTest.
104 #if !defined(OS_POSIX) || defined(OS_MACOSX)
105 const base::string16 kTitle(ASCIIToUTF16("The Example Company"));
106 // Can we pull a bookmark off the clipboard?
108 ui::ScopedClipboardWriter clipboard_writer(clipboard,
109 ui::CLIPBOARD_TYPE_COPY_PASTE);
110 clipboard_writer.WriteBookmark(kTitle, kURL);
112 EXPECT_EQ(ASCIIToUTF16(kURL), OmniboxView::GetClipboardText());
114 // Do we pull text in preference to a bookmark?
116 ui::ScopedClipboardWriter clipboard_writer(clipboard,
117 ui::CLIPBOARD_TYPE_COPY_PASTE);
118 clipboard_writer.WriteText(kPlainText);
119 clipboard_writer.WriteBookmark(kTitle, kURL);
121 EXPECT_EQ(kPlainText, OmniboxView::GetClipboardText());
122 #endif
124 // Do we get nothing if there is neither text nor a bookmark?
126 const base::string16 kMarkup(ASCIIToUTF16("<strong>Hi!</string>"));
127 ui::ScopedClipboardWriter clipboard_writer(clipboard,
128 ui::CLIPBOARD_TYPE_COPY_PASTE);
129 clipboard_writer.WriteHTML(kMarkup, kURL);
131 EXPECT_TRUE(OmniboxView::GetClipboardText().empty());
134 } // namespace