| 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/browser/importer/safari_importer.h" | 5 #include "chrome/browser/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/string16.h" | 12 #include "base/string16.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/history/history_types.h" | 16 #include "chrome/browser/bookmarks/imported_bookmark_entry.h" |
| 17 #include "chrome/browser/favicon/imported_favicon_usage.h" |
| 17 #include "chrome/browser/importer/importer_bridge.h" | 18 #include "chrome/browser/importer/importer_bridge.h" |
| 18 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
| 19 #include "sql/connection.h" | 20 #include "sql/connection.h" |
| 20 #include "testing/platform_test.h" | 21 #include "testing/platform_test.h" |
| 21 | 22 |
| 22 // In order to test the Safari import functionality effectively, we store a | 23 // In order to test the Safari import functionality effectively, we store a |
| 23 // simulated Library directory containing dummy data files in the same | 24 // simulated Library directory containing dummy data files in the same |
| 24 // structure as ~/Library in the Chrome test data directory. | 25 // structure as ~/Library in the Chrome test data directory. |
| 25 // This function returns the path to that directory. | 26 // This function returns the path to that directory. |
| 26 base::FilePath GetTestSafariLibraryPath() { | 27 base::FilePath GetTestSafariLibraryPath() { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 }, | 121 }, |
| 121 { | 122 { |
| 122 false, | 123 false, |
| 123 GURL("http://www.webkit.org/blog/"), | 124 GURL("http://www.webkit.org/blog/"), |
| 124 string16(), | 125 string16(), |
| 125 ASCIIToUTF16("Surfin' Safari - The WebKit Blog") | 126 ASCIIToUTF16("Surfin' Safari - The WebKit Blog") |
| 126 }, | 127 }, |
| 127 }; | 128 }; |
| 128 | 129 |
| 129 scoped_refptr<SafariImporter> importer(GetSafariImporter()); | 130 scoped_refptr<SafariImporter> importer(GetSafariImporter()); |
| 130 std::vector<ProfileWriter::BookmarkEntry> bookmarks; | 131 std::vector<ImportedBookmarkEntry> bookmarks; |
| 131 importer->ParseBookmarks(ASCIIToUTF16("Toolbar"), &bookmarks); | 132 importer->ParseBookmarks(ASCIIToUTF16("Toolbar"), &bookmarks); |
| 132 size_t num_bookmarks = bookmarks.size(); | 133 size_t num_bookmarks = bookmarks.size(); |
| 133 ASSERT_EQ(ARRAYSIZE_UNSAFE(kImportedBookmarksData), num_bookmarks); | 134 ASSERT_EQ(ARRAYSIZE_UNSAFE(kImportedBookmarksData), num_bookmarks); |
| 134 | 135 |
| 135 for (size_t i = 0; i < num_bookmarks; ++i) { | 136 for (size_t i = 0; i < num_bookmarks; ++i) { |
| 136 ProfileWriter::BookmarkEntry& entry = bookmarks[i]; | 137 ImportedBookmarkEntry& entry = bookmarks[i]; |
| 137 EXPECT_EQ(kImportedBookmarksData[i].in_toolbar, entry.in_toolbar); | 138 EXPECT_EQ(kImportedBookmarksData[i].in_toolbar, entry.in_toolbar); |
| 138 EXPECT_EQ(kImportedBookmarksData[i].url, entry.url); | 139 EXPECT_EQ(kImportedBookmarksData[i].url, entry.url); |
| 139 | 140 |
| 140 std::vector<string16> path; | 141 std::vector<string16> path; |
| 141 Tokenize(kImportedBookmarksData[i].path, ASCIIToUTF16("/"), &path); | 142 Tokenize(kImportedBookmarksData[i].path, ASCIIToUTF16("/"), &path); |
| 142 ASSERT_EQ(path.size(), entry.path.size()); | 143 ASSERT_EQ(path.size(), entry.path.size()); |
| 143 for (size_t j = 0; j < path.size(); ++j) { | 144 for (size_t j = 0; j < path.size(); ++j) { |
| 144 EXPECT_EQ(path[j], entry.path[j]); | 145 EXPECT_EQ(path[j], entry.path[j]); |
| 145 } | 146 } |
| 146 | 147 |
| 147 EXPECT_EQ(kImportedBookmarksData[i].title, entry.title); | 148 EXPECT_EQ(kImportedBookmarksData[i].title, entry.title); |
| 148 } | 149 } |
| 149 } | 150 } |
| 150 | 151 |
| 151 TEST_F(SafariImporterTest, FaviconImport) { | 152 TEST_F(SafariImporterTest, FaviconImport) { |
| 152 scoped_refptr<SafariImporter> importer(GetSafariImporter()); | 153 scoped_refptr<SafariImporter> importer(GetSafariImporter()); |
| 153 sql::Connection db; | 154 sql::Connection db; |
| 154 ASSERT_TRUE(importer->OpenDatabase(&db)); | 155 ASSERT_TRUE(importer->OpenDatabase(&db)); |
| 155 | 156 |
| 156 SafariImporter::FaviconMap favicon_map; | 157 SafariImporter::FaviconMap favicon_map; |
| 157 importer->ImportFaviconURLs(&db, &favicon_map); | 158 importer->ImportFaviconURLs(&db, &favicon_map); |
| 158 | 159 |
| 159 std::vector<history::ImportedFaviconUsage> favicons; | 160 std::vector<ImportedFaviconUsage> favicons; |
| 160 importer->LoadFaviconData(&db, favicon_map, &favicons); | 161 importer->LoadFaviconData(&db, favicon_map, &favicons); |
| 161 | 162 |
| 162 size_t num_favicons = favicons.size(); | 163 size_t num_favicons = favicons.size(); |
| 163 ASSERT_EQ(num_favicons, 2U); | 164 ASSERT_EQ(num_favicons, 2U); |
| 164 | 165 |
| 165 history::ImportedFaviconUsage &fav0 = favicons[0]; | 166 ImportedFaviconUsage &fav0 = favicons[0]; |
| 166 EXPECT_EQ("http://s.ytimg.com/yt/favicon-vfl86270.ico", | 167 EXPECT_EQ("http://s.ytimg.com/yt/favicon-vfl86270.ico", |
| 167 fav0.favicon_url.spec()); | 168 fav0.favicon_url.spec()); |
| 168 EXPECT_GT(fav0.png_data.size(), 0U); | 169 EXPECT_GT(fav0.png_data.size(), 0U); |
| 169 EXPECT_EQ(fav0.urls.size(), 1U); | 170 EXPECT_EQ(fav0.urls.size(), 1U); |
| 170 EXPECT_TRUE(fav0.urls.find(GURL("http://www.youtube.com/")) | 171 EXPECT_TRUE(fav0.urls.find(GURL("http://www.youtube.com/")) |
| 171 != fav0.urls.end()); | 172 != fav0.urls.end()); |
| 172 | 173 |
| 173 history::ImportedFaviconUsage &fav1 = favicons[1]; | 174 ImportedFaviconUsage &fav1 = favicons[1]; |
| 174 EXPECT_EQ("http://www.opensearch.org/favicon.ico", | 175 EXPECT_EQ("http://www.opensearch.org/favicon.ico", |
| 175 fav1.favicon_url.spec()); | 176 fav1.favicon_url.spec()); |
| 176 EXPECT_GT(fav1.png_data.size(), 0U); | 177 EXPECT_GT(fav1.png_data.size(), 0U); |
| 177 EXPECT_EQ(fav1.urls.size(), 2U); | 178 EXPECT_EQ(fav1.urls.size(), 2U); |
| 178 EXPECT_TRUE(fav1.urls.find(GURL("http://www.opensearch.org/Home")) | 179 EXPECT_TRUE(fav1.urls.find(GURL("http://www.opensearch.org/Home")) |
| 179 != fav1.urls.end()); | 180 != fav1.urls.end()); |
| 180 | 181 |
| 181 EXPECT_TRUE(fav1.urls.find( | 182 EXPECT_TRUE(fav1.urls.find( |
| 182 GURL("http://www.opensearch.org/Special:Search?search=lalala&go=Search")) | 183 GURL("http://www.opensearch.org/Special:Search?search=lalala&go=Search")) |
| 183 != fav1.urls.end()); | 184 != fav1.urls.end()); |
| 184 } | 185 } |
| 185 | 186 |
| 186 TEST_F(SafariImporterTest, CanImport) { | 187 TEST_F(SafariImporterTest, CanImport) { |
| 187 uint16 items = importer::NONE; | 188 uint16 items = importer::NONE; |
| 188 EXPECT_TRUE(SafariImporter::CanImport(GetTestSafariLibraryPath(), &items)); | 189 EXPECT_TRUE(SafariImporter::CanImport(GetTestSafariLibraryPath(), &items)); |
| 189 EXPECT_EQ(items, importer::HISTORY | importer::FAVORITES); | 190 EXPECT_EQ(items, importer::HISTORY | importer::FAVORITES); |
| 190 EXPECT_EQ(items & importer::COOKIES, importer::NONE); | 191 EXPECT_EQ(items & importer::COOKIES, importer::NONE); |
| 191 EXPECT_EQ(items & importer::PASSWORDS, importer::NONE); | 192 EXPECT_EQ(items & importer::PASSWORDS, importer::NONE); |
| 192 EXPECT_EQ(items & importer::SEARCH_ENGINES, importer::NONE); | 193 EXPECT_EQ(items & importer::SEARCH_ENGINES, importer::NONE); |
| 193 EXPECT_EQ(items & importer::HOME_PAGE, importer::NONE); | 194 EXPECT_EQ(items & importer::HOME_PAGE, importer::NONE); |
| 194 | 195 |
| 195 // Check that we don't import anything from a bogus library directory. | 196 // Check that we don't import anything from a bogus library directory. |
| 196 base::ScopedTempDir fake_library_dir; | 197 base::ScopedTempDir fake_library_dir; |
| 197 ASSERT_TRUE(fake_library_dir.CreateUniqueTempDir()); | 198 ASSERT_TRUE(fake_library_dir.CreateUniqueTempDir()); |
| 198 EXPECT_FALSE(SafariImporter::CanImport(fake_library_dir.path(), &items)); | 199 EXPECT_FALSE(SafariImporter::CanImport(fake_library_dir.path(), &items)); |
| 199 } | 200 } |
| OLD | NEW |