Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/utility/importer/safari_importer.h" | 5 #include "chrome/utility/importer/safari_importer.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
| 17 #include "chrome/common/importer/imported_bookmark_entry.h" | 17 #include "chrome/common/importer/imported_bookmark_entry.h" |
| 18 #include "chrome/common/importer/imported_favicon_usage.h" | 18 #include "chrome/common/importer/imported_favicon_usage.h" |
| 19 #include "chrome/common/importer/importer_bridge.h" | 19 #include "chrome/common/importer/importer_bridge.h" |
| 20 #include "chrome/common/importer/safari_importer_utils.h" | 20 #include "chrome/common/importer/safari_importer_utils.h" |
| 21 #include "chrome/utility/importer/safari_importer.h" | 21 #include "chrome/utility/importer/safari_importer.h" |
| 22 #include "sql/connection.h" | 22 #include "sql/connection.h" |
| 23 #include "testing/platform_test.h" | 23 #include "testing/platform_test.h" |
| 24 | 24 |
| 25 // In order to test the Safari import functionality effectively, we store a | 25 // In order to test the Safari import functionality effectively, we store a |
| 26 // simulated Library directory containing dummy data files in the same | 26 // simulated Library directory containing dummy data files in the same |
| 27 // structure as ~/Library in the Chrome test data directory. | 27 // structure as ~/Library in the Chrome test data directory. |
| 28 // This function returns the path to that directory. | 28 // This function returns the path to that directory. |
| 29 base::FilePath GetTestSafariLibraryPath() { | 29 base::FilePath GetTestSafariLibraryPath(const std::string& suffix) { |
| 30 base::FilePath test_dir; | 30 base::FilePath test_dir; |
| 31 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); | 31 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); |
| 32 | 32 |
| 33 // Our simulated ~/Library directory | 33 // Our simulated ~/Library directory |
| 34 test_dir = test_dir.AppendASCII("safari_import"); | 34 return |
| 35 return test_dir; | 35 test_dir.AppendASCII("import").AppendASCII("safari").AppendASCII(suffix); |
| 36 } | 36 } |
| 37 | 37 |
| 38 class SafariImporterTest : public PlatformTest { | 38 class SafariImporterTest : public PlatformTest { |
| 39 public: | 39 public: |
| 40 SafariImporter* GetSafariImporter() { | 40 SafariImporter* GetSafariImporter() { |
| 41 base::FilePath test_library_dir = GetTestSafariLibraryPath(); | 41 return GetSafariImporterWithPathSuffix("default"); |
| 42 CHECK(base::PathExists(test_library_dir)) << | 42 } |
| 43 "Missing test data directory"; | |
| 44 | 43 |
| 44 SafariImporter* GetSafariImporterWithPathSuffix(const std::string& suffix) { | |
| 45 base::FilePath test_library_dir = GetTestSafariLibraryPath(suffix); | |
| 46 CHECK(base::PathExists(test_library_dir)); | |
| 45 return new SafariImporter(test_library_dir); | 47 return new SafariImporter(test_library_dir); |
| 46 } | 48 } |
| 47 }; | 49 }; |
| 48 | 50 |
| 49 TEST_F(SafariImporterTest, HistoryImport) { | 51 TEST_F(SafariImporterTest, HistoryImport) { |
| 50 scoped_refptr<SafariImporter> importer(GetSafariImporter()); | 52 scoped_refptr<SafariImporter> importer(GetSafariImporter()); |
| 51 | 53 |
| 52 std::vector<ImporterURLRow> history_items; | 54 std::vector<ImporterURLRow> history_items; |
| 53 importer->ParseHistoryItems(&history_items); | 55 importer->ParseHistoryItems(&history_items); |
| 54 | 56 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 Tokenize(kImportedBookmarksData[i].path, ASCIIToUTF16("/"), &path); | 146 Tokenize(kImportedBookmarksData[i].path, ASCIIToUTF16("/"), &path); |
| 145 ASSERT_EQ(path.size(), entry.path.size()); | 147 ASSERT_EQ(path.size(), entry.path.size()); |
| 146 for (size_t j = 0; j < path.size(); ++j) { | 148 for (size_t j = 0; j < path.size(); ++j) { |
| 147 EXPECT_EQ(path[j], entry.path[j]); | 149 EXPECT_EQ(path[j], entry.path[j]); |
| 148 } | 150 } |
| 149 | 151 |
| 150 EXPECT_EQ(kImportedBookmarksData[i].title, entry.title); | 152 EXPECT_EQ(kImportedBookmarksData[i].title, entry.title); |
| 151 } | 153 } |
| 152 } | 154 } |
| 153 | 155 |
| 156 TEST_F(SafariImporterTest, BookmarkImportWithEmptyBookmarksMenu) { | |
| 157 // Expected results | |
|
jeremy
2013/12/22 13:14:22
nit: results.
Ilya Sherman
2014/01/08 05:26:37
Done.
| |
| 158 const struct { | |
| 159 bool in_toolbar; | |
| 160 GURL url; | |
| 161 // We store the path with levels of nesting delimited by forward slashes. | |
| 162 string16 path; | |
| 163 string16 title; | |
| 164 } kImportedBookmarksData[] = { | |
| 165 { | |
| 166 true, | |
| 167 GURL("http://www.apple.com/"), | |
| 168 ASCIIToUTF16("Toolbar/"), | |
| 169 ASCIIToUTF16("Apple") | |
| 170 }, | |
| 171 { | |
| 172 true, | |
| 173 GURL("http://www.yahoo.com/"), | |
| 174 ASCIIToUTF16("Toolbar/"), | |
| 175 ASCIIToUTF16("Yahoo!") | |
| 176 }, | |
| 177 { | |
| 178 true, | |
| 179 GURL("http://www.cnn.com/"), | |
| 180 ASCIIToUTF16("Toolbar/News"), | |
| 181 ASCIIToUTF16("CNN") | |
| 182 }, | |
| 183 { | |
| 184 true, | |
| 185 GURL("http://www.nytimes.com/"), | |
| 186 ASCIIToUTF16("Toolbar/News"), | |
| 187 ASCIIToUTF16("The New York Times") | |
| 188 }, | |
| 189 { | |
| 190 false, | |
| 191 GURL("http://www.webkit.org/blog/"), | |
| 192 string16(), | |
| 193 ASCIIToUTF16("Surfin' Safari - The WebKit Blog") | |
| 194 }, | |
| 195 }; | |
| 196 | |
| 197 scoped_refptr<SafariImporter> importer( | |
| 198 GetSafariImporterWithPathSuffix("empty_bookmarks_menu")); | |
| 199 std::vector<ImportedBookmarkEntry> bookmarks; | |
| 200 importer->ParseBookmarks(ASCIIToUTF16("Toolbar"), &bookmarks); | |
| 201 size_t num_bookmarks = bookmarks.size(); | |
| 202 ASSERT_EQ(ARRAYSIZE_UNSAFE(kImportedBookmarksData), num_bookmarks); | |
| 203 | |
| 204 for (size_t i = 0; i < num_bookmarks; ++i) { | |
| 205 ImportedBookmarkEntry& entry = bookmarks[i]; | |
| 206 EXPECT_EQ(kImportedBookmarksData[i].in_toolbar, entry.in_toolbar); | |
| 207 EXPECT_EQ(kImportedBookmarksData[i].url, entry.url); | |
| 208 | |
| 209 std::vector<string16> path; | |
| 210 Tokenize(kImportedBookmarksData[i].path, ASCIIToUTF16("/"), &path); | |
| 211 ASSERT_EQ(path.size(), entry.path.size()); | |
| 212 for (size_t j = 0; j < path.size(); ++j) { | |
| 213 EXPECT_EQ(path[j], entry.path[j]); | |
| 214 } | |
| 215 | |
| 216 EXPECT_EQ(kImportedBookmarksData[i].title, entry.title); | |
| 217 } | |
| 218 } | |
| 219 | |
| 154 TEST_F(SafariImporterTest, FaviconImport) { | 220 TEST_F(SafariImporterTest, FaviconImport) { |
| 155 scoped_refptr<SafariImporter> importer(GetSafariImporter()); | 221 scoped_refptr<SafariImporter> importer(GetSafariImporter()); |
| 156 sql::Connection db; | 222 sql::Connection db; |
| 157 ASSERT_TRUE(importer->OpenDatabase(&db)); | 223 ASSERT_TRUE(importer->OpenDatabase(&db)); |
| 158 | 224 |
| 159 SafariImporter::FaviconMap favicon_map; | 225 SafariImporter::FaviconMap favicon_map; |
| 160 importer->ImportFaviconURLs(&db, &favicon_map); | 226 importer->ImportFaviconURLs(&db, &favicon_map); |
| 161 | 227 |
| 162 std::vector<ImportedFaviconUsage> favicons; | 228 std::vector<ImportedFaviconUsage> favicons; |
| 163 importer->LoadFaviconData(&db, favicon_map, &favicons); | 229 importer->LoadFaviconData(&db, favicon_map, &favicons); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 181 EXPECT_TRUE(fav1.urls.find(GURL("http://www.opensearch.org/Home")) | 247 EXPECT_TRUE(fav1.urls.find(GURL("http://www.opensearch.org/Home")) |
| 182 != fav1.urls.end()); | 248 != fav1.urls.end()); |
| 183 | 249 |
| 184 EXPECT_TRUE(fav1.urls.find( | 250 EXPECT_TRUE(fav1.urls.find( |
| 185 GURL("http://www.opensearch.org/Special:Search?search=lalala&go=Search")) | 251 GURL("http://www.opensearch.org/Special:Search?search=lalala&go=Search")) |
| 186 != fav1.urls.end()); | 252 != fav1.urls.end()); |
| 187 } | 253 } |
| 188 | 254 |
| 189 TEST_F(SafariImporterTest, CanImport) { | 255 TEST_F(SafariImporterTest, CanImport) { |
| 190 uint16 items = importer::NONE; | 256 uint16 items = importer::NONE; |
| 191 EXPECT_TRUE(SafariImporterCanImport(GetTestSafariLibraryPath(), &items)); | 257 EXPECT_TRUE(SafariImporterCanImport( |
| 258 GetTestSafariLibraryPath("default"), &items)); | |
| 192 EXPECT_EQ(items, importer::HISTORY | importer::FAVORITES); | 259 EXPECT_EQ(items, importer::HISTORY | importer::FAVORITES); |
| 193 EXPECT_EQ(items & importer::COOKIES, importer::NONE); | 260 EXPECT_EQ(items & importer::COOKIES, importer::NONE); |
| 194 EXPECT_EQ(items & importer::PASSWORDS, importer::NONE); | 261 EXPECT_EQ(items & importer::PASSWORDS, importer::NONE); |
| 195 EXPECT_EQ(items & importer::SEARCH_ENGINES, importer::NONE); | 262 EXPECT_EQ(items & importer::SEARCH_ENGINES, importer::NONE); |
| 196 EXPECT_EQ(items & importer::HOME_PAGE, importer::NONE); | 263 EXPECT_EQ(items & importer::HOME_PAGE, importer::NONE); |
| 197 | 264 |
| 198 // Check that we don't import anything from a bogus library directory. | 265 // Check that we don't import anything from a bogus library directory. |
| 199 base::ScopedTempDir fake_library_dir; | 266 base::ScopedTempDir fake_library_dir; |
| 200 ASSERT_TRUE(fake_library_dir.CreateUniqueTempDir()); | 267 ASSERT_TRUE(fake_library_dir.CreateUniqueTempDir()); |
| 201 EXPECT_FALSE(SafariImporterCanImport(fake_library_dir.path(), &items)); | 268 EXPECT_FALSE(SafariImporterCanImport(fake_library_dir.path(), &items)); |
| 202 } | 269 } |
| OLD | NEW |