• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
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"
14 
15 using base::ASCIIToUTF16;
16 
17 namespace {
18 
19 class OmniboxViewTest : public PlatformTest {
20  public:
TearDown()21   virtual void TearDown() OVERRIDE {
22     ui::Clipboard::DestroyClipboardForCurrentThread();
23   }
24 
25  private:
26   // Windows requires a message loop for clipboard access.
27   base::MessageLoopForUI message_loop_;
28 };
29 
TEST_F(OmniboxViewTest,TestStripSchemasUnsafeForPaste)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.
35   };
36 
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.
41   };
42 
43   for (size_t i = 0; i < arraysize(urls); i++) {
44     EXPECT_EQ(ASCIIToUTF16(expecteds[i]),
45               OmniboxView::StripJavascriptSchemas(ASCIIToUTF16(urls[i])));
46   }
47 }
48 
TEST_F(OmniboxViewTest,SanitizeTextForPaste)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"));
54 
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));
59 
60   // Multi-line address is converted to a single-line address.
61   const base::string16 kWrappedAddress(ASCIIToUTF16(
62       "1600 Amphitheatre Parkway\nMountain View, CA"));
63 
64   const base::string16 kFixedAddress(ASCIIToUTF16(
65       "1600 Amphitheatre Parkway Mountain View, CA"));
66   EXPECT_EQ(kFixedAddress, OmniboxView::SanitizeTextForPaste(kWrappedAddress));
67 }
68 
TEST_F(OmniboxViewTest,GetClipboardText)69 TEST_F(OmniboxViewTest, GetClipboardText) {
70   ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
71 
72   const base::string16 kPlainText(ASCIIToUTF16("test text"));
73   const std::string kURL("http://www.example.com/");
74 
75   // Can we pull straight text off the clipboard?
76   {
77     ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE);
78     clipboard_writer.WriteText(kPlainText);
79   }
80   EXPECT_EQ(kPlainText, OmniboxView::GetClipboardText());
81 
82   // Can we pull a string consists of white-space?
83   const base::string16 kSpace6(ASCIIToUTF16("      "));
84   const base::string16 kSpace1(ASCIIToUTF16(" "));
85   {
86     ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE);
87     clipboard_writer.WriteText(kSpace6);
88   }
89   EXPECT_EQ(kSpace1, OmniboxView::GetClipboardText());
90 
91   // Does an empty clipboard get empty text?
92   clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
93   EXPECT_EQ(base::string16(), OmniboxView::GetClipboardText());
94 
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?
100   {
101     ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE);
102     clipboard_writer.WriteBookmark(kTitle, kURL);
103   }
104   EXPECT_EQ(ASCIIToUTF16(kURL), OmniboxView::GetClipboardText());
105 
106   // Do we pull text in preference to a bookmark?
107   {
108     ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE);
109     clipboard_writer.WriteText(kPlainText);
110     clipboard_writer.WriteBookmark(kTitle, kURL);
111   }
112   EXPECT_EQ(kPlainText, OmniboxView::GetClipboardText());
113 #endif
114 
115   // Do we get nothing if there is neither text nor a bookmark?
116   {
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);
120   }
121   EXPECT_TRUE(OmniboxView::GetClipboardText().empty());
122 }
123 
124 }  // namespace
125