1 // Copyright (c) 2011 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 "testing/gtest/include/gtest/gtest.h" 6 7 #include "base/message_loop.h" 8 #include "base/string_util.h" 9 #include "base/utf_string_conversions.h" 10 #include "chrome/browser/bookmarks/bookmark_model.h" 11 #include "chrome/browser/bookmarks/bookmark_utils.h" 12 #include "chrome/test/testing_browser_process_test.h" 13 #include "ui/base/clipboard/scoped_clipboard_writer.h" 14 15 #if !defined(OS_MACOSX) 16 #include "chrome/browser/browser_process.h" 17 #endif 18 19 typedef TestingBrowserProcessTest BookmarkUtilsTest; 20 21 TEST_F(BookmarkUtilsTest, GetBookmarksContainingText) { 22 BookmarkModel model(NULL); 23 const BookmarkNode* n1 = 24 model.AddURL(model.other_node(), 0, ASCIIToUTF16("foo bar"), 25 GURL("http://www.google.com")); 26 const BookmarkNode* n2 = 27 model.AddURL(model.other_node(), 0, ASCIIToUTF16("baz buz"), 28 GURL("http://www.cnn.com")); 29 30 model.AddFolder(model.other_node(), 0, ASCIIToUTF16("foo")); 31 32 std::vector<const BookmarkNode*> nodes; 33 bookmark_utils::GetBookmarksContainingText( 34 &model, ASCIIToUTF16("foo"), 100, std::string(), &nodes); 35 ASSERT_EQ(1U, nodes.size()); 36 EXPECT_TRUE(nodes[0] == n1); 37 EXPECT_TRUE(bookmark_utils::DoesBookmarkContainText( 38 n1, ASCIIToUTF16("foo"), std::string())); 39 nodes.clear(); 40 41 bookmark_utils::GetBookmarksContainingText( 42 &model, ASCIIToUTF16("cnn"), 100, std::string(), &nodes); 43 ASSERT_EQ(1U, nodes.size()); 44 EXPECT_TRUE(nodes[0] == n2); 45 EXPECT_TRUE(bookmark_utils::DoesBookmarkContainText( 46 n2, ASCIIToUTF16("cnn"), std::string())); 47 nodes.clear(); 48 49 bookmark_utils::GetBookmarksContainingText( 50 &model, ASCIIToUTF16("foo bar"), 100, std::string(), &nodes); 51 ASSERT_EQ(1U, nodes.size()); 52 EXPECT_TRUE(nodes[0] == n1); 53 EXPECT_TRUE(bookmark_utils::DoesBookmarkContainText( 54 n1, ASCIIToUTF16("foo bar"), std::string())); 55 nodes.clear(); 56 } 57 58 TEST_F(BookmarkUtilsTest, DoesBookmarkContainText) { 59 BookmarkModel model(NULL); 60 const BookmarkNode* node = model.AddURL(model.other_node(), 0, 61 ASCIIToUTF16("foo bar"), 62 GURL("http://www.google.com")); 63 // Matches to the title. 64 ASSERT_TRUE(bookmark_utils::DoesBookmarkContainText( 65 node, ASCIIToUTF16("ar"), std::string())); 66 // Matches to the URL 67 ASSERT_TRUE(bookmark_utils::DoesBookmarkContainText( 68 node, ASCIIToUTF16("www"), std::string())); 69 // No match. 70 ASSERT_FALSE(bookmark_utils::DoesBookmarkContainText( 71 node, ASCIIToUTF16("cnn"), std::string())); 72 73 // Tests for a Japanese IDN. 74 const string16 kDecodedIdn = WideToUTF16(L"\x30B0\x30FC\x30B0\x30EB"); 75 node = model.AddURL(model.other_node(), 0, ASCIIToUTF16("foo bar"), 76 GURL("http://xn--qcka1pmc.jp")); 77 // Unicode query doesn't match if languages have no "ja". 78 ASSERT_FALSE(bookmark_utils::DoesBookmarkContainText( 79 node, kDecodedIdn, "en")); 80 // Unicode query matches if languages have "ja". 81 ASSERT_TRUE(bookmark_utils::DoesBookmarkContainText( 82 node, kDecodedIdn, "ja")); 83 // Punycode query also matches as ever. 84 ASSERT_TRUE(bookmark_utils::DoesBookmarkContainText( 85 node, ASCIIToUTF16("qcka1pmc"), "ja")); 86 87 // Tests with various lower/upper case characters. 88 node = model.AddURL(model.other_node(), 0, ASCIIToUTF16("FOO bar"), 89 GURL("http://www.google.com/search?q=ABC")); 90 ASSERT_TRUE(bookmark_utils::DoesBookmarkContainText( 91 node, ASCIIToUTF16("foo"), std::string())); 92 ASSERT_TRUE(bookmark_utils::DoesBookmarkContainText( 93 node, ASCIIToUTF16("Foo"), std::string())); 94 ASSERT_TRUE(bookmark_utils::DoesBookmarkContainText( 95 node, ASCIIToUTF16("FOO"), std::string())); 96 ASSERT_TRUE(bookmark_utils::DoesBookmarkContainText( 97 node, ASCIIToUTF16("google abc"), std::string())); 98 ASSERT_TRUE(bookmark_utils::DoesBookmarkContainText( 99 node, ASCIIToUTF16("google ABC"), std::string())); 100 ASSERT_TRUE(bookmark_utils::DoesBookmarkContainText( 101 node, ASCIIToUTF16("http://www.google.com/search?q=A"), std::string())); 102 } 103 104 #if !defined(OS_MACOSX) 105 TEST_F(BookmarkUtilsTest, CopyPaste) { 106 // Clipboard requires a message loop. 107 MessageLoopForUI loop; 108 109 BookmarkModel model(NULL); 110 const BookmarkNode* node = model.AddURL(model.other_node(), 0, 111 ASCIIToUTF16("foo bar"), 112 GURL("http://www.google.com")); 113 114 // Copy a node to the clipboard. 115 std::vector<const BookmarkNode*> nodes; 116 nodes.push_back(node); 117 bookmark_utils::CopyToClipboard(&model, nodes, false); 118 119 // And make sure we can paste a bookmark from the clipboard. 120 EXPECT_TRUE( 121 bookmark_utils::CanPasteFromClipboard(model.GetBookmarkBarNode())); 122 123 // Write some text to the clipboard. 124 { 125 ui::ScopedClipboardWriter clipboard_writer(g_browser_process->clipboard()); 126 clipboard_writer.WriteText(ASCIIToUTF16("foo")); 127 } 128 129 // Now we shouldn't be able to paste from the clipboard. 130 EXPECT_FALSE( 131 bookmark_utils::CanPasteFromClipboard(model.GetBookmarkBarNode())); 132 } 133 #endif 134