| OLD | NEW |
| (Empty) |
| 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/importer/safari_importer.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/files/scoped_temp_dir.h" | |
| 11 #include "base/path_service.h" | |
| 12 #include "base/strings/string16.h" | |
| 13 #include "base/strings/string_util.h" | |
| 14 #include "base/strings/sys_string_conversions.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | |
| 16 #include "chrome/browser/importer/importer_bridge.h" | |
| 17 #include "chrome/common/chrome_paths.h" | |
| 18 #include "chrome/common/importer/imported_bookmark_entry.h" | |
| 19 #include "chrome/common/importer/imported_favicon_usage.h" | |
| 20 #include "sql/connection.h" | |
| 21 #include "testing/platform_test.h" | |
| 22 | |
| 23 // In order to test the Safari import functionality effectively, we store a | |
| 24 // simulated Library directory containing dummy data files in the same | |
| 25 // structure as ~/Library in the Chrome test data directory. | |
| 26 // This function returns the path to that directory. | |
| 27 base::FilePath GetTestSafariLibraryPath() { | |
| 28 base::FilePath test_dir; | |
| 29 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); | |
| 30 | |
| 31 // Our simulated ~/Library directory | |
| 32 test_dir = test_dir.AppendASCII("safari_import"); | |
| 33 return test_dir; | |
| 34 } | |
| 35 | |
| 36 class SafariImporterTest : public PlatformTest { | |
| 37 public: | |
| 38 SafariImporter* GetSafariImporter() { | |
| 39 base::FilePath test_library_dir = GetTestSafariLibraryPath(); | |
| 40 CHECK(base::PathExists(test_library_dir)) << | |
| 41 "Missing test data directory"; | |
| 42 | |
| 43 return new SafariImporter(test_library_dir); | |
| 44 } | |
| 45 }; | |
| 46 | |
| 47 TEST_F(SafariImporterTest, HistoryImport) { | |
| 48 scoped_refptr<SafariImporter> importer(GetSafariImporter()); | |
| 49 | |
| 50 std::vector<ImporterURLRow> history_items; | |
| 51 importer->ParseHistoryItems(&history_items); | |
| 52 | |
| 53 // Should be 2 history items. | |
| 54 ASSERT_EQ(history_items.size(), 2U); | |
| 55 | |
| 56 ImporterURLRow& it1 = history_items[0]; | |
| 57 EXPECT_EQ(it1.url, GURL("http://www.firsthistoryitem.com/")); | |
| 58 EXPECT_EQ(it1.title, UTF8ToUTF16("First History Item Title")); | |
| 59 EXPECT_EQ(it1.visit_count, 1); | |
| 60 EXPECT_EQ(it1.hidden, 0); | |
| 61 EXPECT_EQ(it1.typed_count, 0); | |
| 62 EXPECT_EQ(it1.last_visit.ToDoubleT(), | |
| 63 importer->HistoryTimeToEpochTime(@"270598264.4")); | |
| 64 | |
| 65 ImporterURLRow& it2 = history_items[1]; | |
| 66 std::string second_item_title("http://www.secondhistoryitem.com/"); | |
| 67 EXPECT_EQ(it2.url, GURL(second_item_title)); | |
| 68 // The second item lacks a title so we expect the URL to be substituted. | |
| 69 EXPECT_EQ(UTF16ToUTF8(it2.title), second_item_title.c_str()); | |
| 70 EXPECT_EQ(it2.visit_count, 55); | |
| 71 EXPECT_EQ(it2.hidden, 0); | |
| 72 EXPECT_EQ(it2.typed_count, 0); | |
| 73 EXPECT_EQ(it2.last_visit.ToDoubleT(), | |
| 74 importer->HistoryTimeToEpochTime(@"270598231.4")); | |
| 75 } | |
| 76 | |
| 77 TEST_F(SafariImporterTest, BookmarkImport) { | |
| 78 // Expected results | |
| 79 const struct { | |
| 80 bool in_toolbar; | |
| 81 GURL url; | |
| 82 // We store the path with levels of nesting delimited by forward slashes. | |
| 83 string16 path; | |
| 84 string16 title; | |
| 85 } kImportedBookmarksData[] = { | |
| 86 { | |
| 87 true, | |
| 88 GURL("http://www.apple.com/"), | |
| 89 ASCIIToUTF16("Toolbar/"), | |
| 90 ASCIIToUTF16("Apple") | |
| 91 }, | |
| 92 { | |
| 93 true, | |
| 94 GURL("http://www.yahoo.com/"), | |
| 95 ASCIIToUTF16("Toolbar/"), | |
| 96 ASCIIToUTF16("Yahoo!") | |
| 97 }, | |
| 98 { | |
| 99 true, | |
| 100 GURL("http://www.cnn.com/"), | |
| 101 ASCIIToUTF16("Toolbar/News"), | |
| 102 ASCIIToUTF16("CNN") | |
| 103 }, | |
| 104 { | |
| 105 true, | |
| 106 GURL("http://www.nytimes.com/"), | |
| 107 ASCIIToUTF16("Toolbar/News"), | |
| 108 ASCIIToUTF16("The New York Times") | |
| 109 }, | |
| 110 { | |
| 111 false, | |
| 112 GURL("http://www.reddit.com/"), | |
| 113 string16(), | |
| 114 ASCIIToUTF16("reddit.com: what's new online!") | |
| 115 }, | |
| 116 { | |
| 117 false, | |
| 118 GURL(), | |
| 119 string16(), | |
| 120 ASCIIToUTF16("Empty Folder") | |
| 121 }, | |
| 122 { | |
| 123 false, | |
| 124 GURL("http://www.webkit.org/blog/"), | |
| 125 string16(), | |
| 126 ASCIIToUTF16("Surfin' Safari - The WebKit Blog") | |
| 127 }, | |
| 128 }; | |
| 129 | |
| 130 scoped_refptr<SafariImporter> importer(GetSafariImporter()); | |
| 131 std::vector<ImportedBookmarkEntry> bookmarks; | |
| 132 importer->ParseBookmarks(ASCIIToUTF16("Toolbar"), &bookmarks); | |
| 133 size_t num_bookmarks = bookmarks.size(); | |
| 134 ASSERT_EQ(ARRAYSIZE_UNSAFE(kImportedBookmarksData), num_bookmarks); | |
| 135 | |
| 136 for (size_t i = 0; i < num_bookmarks; ++i) { | |
| 137 ImportedBookmarkEntry& entry = bookmarks[i]; | |
| 138 EXPECT_EQ(kImportedBookmarksData[i].in_toolbar, entry.in_toolbar); | |
| 139 EXPECT_EQ(kImportedBookmarksData[i].url, entry.url); | |
| 140 | |
| 141 std::vector<string16> path; | |
| 142 Tokenize(kImportedBookmarksData[i].path, ASCIIToUTF16("/"), &path); | |
| 143 ASSERT_EQ(path.size(), entry.path.size()); | |
| 144 for (size_t j = 0; j < path.size(); ++j) { | |
| 145 EXPECT_EQ(path[j], entry.path[j]); | |
| 146 } | |
| 147 | |
| 148 EXPECT_EQ(kImportedBookmarksData[i].title, entry.title); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 TEST_F(SafariImporterTest, FaviconImport) { | |
| 153 scoped_refptr<SafariImporter> importer(GetSafariImporter()); | |
| 154 sql::Connection db; | |
| 155 ASSERT_TRUE(importer->OpenDatabase(&db)); | |
| 156 | |
| 157 SafariImporter::FaviconMap favicon_map; | |
| 158 importer->ImportFaviconURLs(&db, &favicon_map); | |
| 159 | |
| 160 std::vector<ImportedFaviconUsage> favicons; | |
| 161 importer->LoadFaviconData(&db, favicon_map, &favicons); | |
| 162 | |
| 163 size_t num_favicons = favicons.size(); | |
| 164 ASSERT_EQ(num_favicons, 2U); | |
| 165 | |
| 166 ImportedFaviconUsage &fav0 = favicons[0]; | |
| 167 EXPECT_EQ("http://s.ytimg.com/yt/favicon-vfl86270.ico", | |
| 168 fav0.favicon_url.spec()); | |
| 169 EXPECT_GT(fav0.png_data.size(), 0U); | |
| 170 EXPECT_EQ(fav0.urls.size(), 1U); | |
| 171 EXPECT_TRUE(fav0.urls.find(GURL("http://www.youtube.com/")) | |
| 172 != fav0.urls.end()); | |
| 173 | |
| 174 ImportedFaviconUsage &fav1 = favicons[1]; | |
| 175 EXPECT_EQ("http://www.opensearch.org/favicon.ico", | |
| 176 fav1.favicon_url.spec()); | |
| 177 EXPECT_GT(fav1.png_data.size(), 0U); | |
| 178 EXPECT_EQ(fav1.urls.size(), 2U); | |
| 179 EXPECT_TRUE(fav1.urls.find(GURL("http://www.opensearch.org/Home")) | |
| 180 != fav1.urls.end()); | |
| 181 | |
| 182 EXPECT_TRUE(fav1.urls.find( | |
| 183 GURL("http://www.opensearch.org/Special:Search?search=lalala&go=Search")) | |
| 184 != fav1.urls.end()); | |
| 185 } | |
| 186 | |
| 187 TEST_F(SafariImporterTest, CanImport) { | |
| 188 uint16 items = importer::NONE; | |
| 189 EXPECT_TRUE(SafariImporter::CanImport(GetTestSafariLibraryPath(), &items)); | |
| 190 EXPECT_EQ(items, importer::HISTORY | importer::FAVORITES); | |
| 191 EXPECT_EQ(items & importer::COOKIES, importer::NONE); | |
| 192 EXPECT_EQ(items & importer::PASSWORDS, importer::NONE); | |
| 193 EXPECT_EQ(items & importer::SEARCH_ENGINES, importer::NONE); | |
| 194 EXPECT_EQ(items & importer::HOME_PAGE, importer::NONE); | |
| 195 | |
| 196 // Check that we don't import anything from a bogus library directory. | |
| 197 base::ScopedTempDir fake_library_dir; | |
| 198 ASSERT_TRUE(fake_library_dir.CreateUniqueTempDir()); | |
| 199 EXPECT_FALSE(SafariImporter::CanImport(fake_library_dir.path(), &items)); | |
| 200 } | |
| OLD | NEW |