| 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/bookmarks/bookmark_html_writer.h" | 5 #include "chrome/browser/bookmarks/bookmark_html_writer.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/i18n/time_formatting.h" | 9 #include "base/i18n/time_formatting.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.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/time.h" | 14 #include "base/time.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/bookmarks/bookmark_html_reader.h" |
| 16 #include "chrome/browser/bookmarks/bookmark_model.h" | 17 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 17 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 18 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 19 #include "chrome/browser/bookmarks/imported_bookmark_entry.h" |
| 18 #include "chrome/browser/favicon/favicon_service.h" | 20 #include "chrome/browser/favicon/favicon_service.h" |
| 19 #include "chrome/browser/favicon/favicon_service_factory.h" | 21 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 20 #include "chrome/browser/history/history_service.h" | 22 #include "chrome/browser/history/history_service.h" |
| 21 #include "chrome/browser/history/history_service_factory.h" | 23 #include "chrome/browser/history/history_service_factory.h" |
| 22 #include "chrome/browser/importer/firefox2_importer.h" | |
| 23 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
| 24 #include "chrome/test/base/ui_test_utils.h" | 25 #include "chrome/test/base/ui_test_utils.h" |
| 25 #include "content/public/test/test_browser_thread.h" | 26 #include "content/public/test/test_browser_thread.h" |
| 26 #include "grit/generated_resources.h" | 27 #include "grit/generated_resources.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 28 #include "third_party/skia/include/core/SkBitmap.h" | 29 #include "third_party/skia/include/core/SkBitmap.h" |
| 29 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
| 30 #include "ui/gfx/codec/png_codec.h" | 31 #include "ui/gfx/codec/png_codec.h" |
| 31 | 32 |
| 32 using content::BrowserThread; | 33 using content::BrowserThread; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 49 } // namespace | 50 } // namespace |
| 50 | 51 |
| 51 class BookmarkHTMLWriterTest : public testing::Test { | 52 class BookmarkHTMLWriterTest : public testing::Test { |
| 52 protected: | 53 protected: |
| 53 virtual void SetUp() { | 54 virtual void SetUp() { |
| 54 testing::Test::SetUp(); | 55 testing::Test::SetUp(); |
| 55 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 56 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 56 path_ = temp_dir_.path().AppendASCII("bookmarks.html"); | 57 path_ = temp_dir_.path().AppendASCII("bookmarks.html"); |
| 57 } | 58 } |
| 58 | 59 |
| 59 // Converts a BookmarkEntry to a string suitable for assertion testing. | 60 // Converts an ImportedBookmarkEntry to a string suitable for assertion |
| 60 string16 BookmarkEntryToString(const ProfileWriter::BookmarkEntry& entry) { | 61 // testing. |
| 62 string16 BookmarkEntryToString(const ImportedBookmarkEntry& entry) { |
| 61 string16 result; | 63 string16 result; |
| 62 result.append(ASCIIToUTF16("on_toolbar=")); | 64 result.append(ASCIIToUTF16("on_toolbar=")); |
| 63 if (entry.in_toolbar) | 65 if (entry.in_toolbar) |
| 64 result.append(ASCIIToUTF16("true")); | 66 result.append(ASCIIToUTF16("true")); |
| 65 else | 67 else |
| 66 result.append(ASCIIToUTF16("false")); | 68 result.append(ASCIIToUTF16("false")); |
| 67 | 69 |
| 68 result.append(ASCIIToUTF16(" url=") + UTF8ToUTF16(entry.url.spec())); | 70 result.append(ASCIIToUTF16(" url=") + UTF8ToUTF16(entry.url.spec())); |
| 69 | 71 |
| 70 result.append(ASCIIToUTF16(" path=")); | 72 result.append(ASCIIToUTF16(" path=")); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 83 } | 85 } |
| 84 | 86 |
| 85 // Creates a set of bookmark values to a string for assertion testing. | 87 // Creates a set of bookmark values to a string for assertion testing. |
| 86 string16 BookmarkValuesToString(bool on_toolbar, | 88 string16 BookmarkValuesToString(bool on_toolbar, |
| 87 const GURL& url, | 89 const GURL& url, |
| 88 const string16& title, | 90 const string16& title, |
| 89 base::Time creation_time, | 91 base::Time creation_time, |
| 90 const string16& f1, | 92 const string16& f1, |
| 91 const string16& f2, | 93 const string16& f2, |
| 92 const string16& f3) { | 94 const string16& f3) { |
| 93 ProfileWriter::BookmarkEntry entry; | 95 ImportedBookmarkEntry entry; |
| 94 entry.in_toolbar = on_toolbar; | 96 entry.in_toolbar = on_toolbar; |
| 95 entry.url = url; | 97 entry.url = url; |
| 96 if (!f1.empty()) { | 98 if (!f1.empty()) { |
| 97 entry.path.push_back(f1); | 99 entry.path.push_back(f1); |
| 98 if (!f2.empty()) { | 100 if (!f2.empty()) { |
| 99 entry.path.push_back(f2); | 101 entry.path.push_back(f2); |
| 100 if (!f3.empty()) | 102 if (!f3.empty()) |
| 101 entry.path.push_back(f3); | 103 entry.path.push_back(f3); |
| 102 } | 104 } |
| 103 } | 105 } |
| 104 entry.title = title; | 106 entry.title = title; |
| 105 entry.creation_time = creation_time; | 107 entry.creation_time = creation_time; |
| 106 return BookmarkEntryToString(entry); | 108 return BookmarkEntryToString(entry); |
| 107 } | 109 } |
| 108 | 110 |
| 109 void AssertBookmarkEntryEquals(const ProfileWriter::BookmarkEntry& entry, | 111 void AssertBookmarkEntryEquals(const ImportedBookmarkEntry& entry, |
| 110 bool on_toolbar, | 112 bool on_toolbar, |
| 111 const GURL& url, | 113 const GURL& url, |
| 112 const string16& title, | 114 const string16& title, |
| 113 base::Time creation_time, | 115 base::Time creation_time, |
| 114 const string16& f1, | 116 const string16& f1, |
| 115 const string16& f2, | 117 const string16& f2, |
| 116 const string16& f3) { | 118 const string16& f3) { |
| 117 EXPECT_EQ(BookmarkValuesToString(on_toolbar, url, title, creation_time, | 119 EXPECT_EQ(BookmarkValuesToString(on_toolbar, url, title, creation_time, |
| 118 f1, f2, f3), | 120 f1, f2, f3), |
| 119 BookmarkEntryToString(entry)); | 121 BookmarkEntryToString(entry)); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 // url3 | 173 // url3 |
| 172 // url4 | 174 // url4 |
| 173 // Other | 175 // Other |
| 174 // url1 | 176 // url1 |
| 175 // url2 | 177 // url2 |
| 176 // F3 | 178 // F3 |
| 177 // F4 | 179 // F4 |
| 178 // url1 | 180 // url1 |
| 179 // Mobile | 181 // Mobile |
| 180 // url1 | 182 // url1 |
| 181 // <bookmark without a title. Not supported by Firefox 2 import> | 183 // <bookmark without a title.> |
| 182 string16 f1_title = ASCIIToUTF16("F\"&;<1\""); | 184 string16 f1_title = ASCIIToUTF16("F\"&;<1\""); |
| 183 string16 f2_title = ASCIIToUTF16("F2"); | 185 string16 f2_title = ASCIIToUTF16("F2"); |
| 184 string16 f3_title = ASCIIToUTF16("F 3"); | 186 string16 f3_title = ASCIIToUTF16("F 3"); |
| 185 string16 f4_title = ASCIIToUTF16("F4"); | 187 string16 f4_title = ASCIIToUTF16("F4"); |
| 186 string16 url1_title = ASCIIToUTF16("url 1"); | 188 string16 url1_title = ASCIIToUTF16("url 1"); |
| 187 string16 url2_title = ASCIIToUTF16("url&2"); | 189 string16 url2_title = ASCIIToUTF16("url&2"); |
| 188 string16 url3_title = ASCIIToUTF16("url\"3"); | 190 string16 url3_title = ASCIIToUTF16("url\"3"); |
| 189 string16 url4_title = ASCIIToUTF16("url\"&;"); | 191 string16 url4_title = ASCIIToUTF16("url\"&;"); |
| 190 string16 unnamed_bookmark_title = ASCIIToUTF16(""); | 192 string16 unnamed_bookmark_title = ASCIIToUTF16(""); |
| 191 GURL url1("http://url1"); | 193 GURL url1("http://url1"); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 bookmark_html_writer::WriteBookmarks(&profile, path_, &observer); | 231 bookmark_html_writer::WriteBookmarks(&profile, path_, &observer); |
| 230 message_loop.Run(); | 232 message_loop.Run(); |
| 231 | 233 |
| 232 // Clear favicon so that it would be read from file. | 234 // Clear favicon so that it would be read from file. |
| 233 FaviconServiceFactory::GetForProfile( | 235 FaviconServiceFactory::GetForProfile( |
| 234 &profile, Profile::EXPLICIT_ACCESS)->SetFavicons( | 236 &profile, Profile::EXPLICIT_ACCESS)->SetFavicons( |
| 235 url1, url1_favicon, history::FAVICON, gfx::Image()); | 237 url1, url1_favicon, history::FAVICON, gfx::Image()); |
| 236 message_loop.RunUntilIdle(); | 238 message_loop.RunUntilIdle(); |
| 237 | 239 |
| 238 // Read the bookmarks back in. | 240 // Read the bookmarks back in. |
| 239 std::vector<ProfileWriter::BookmarkEntry> parsed_bookmarks; | 241 std::vector<ImportedBookmarkEntry> parsed_bookmarks; |
| 240 std::vector<history::ImportedFaviconUsage> favicons; | 242 std::vector<history::ImportedFaviconUsage> favicons; |
| 241 Firefox2Importer::ImportBookmarksFile(path_, | 243 bookmark_html_reader::ImportBookmarksFile(NULL, |
| 242 std::set<GURL>(), | 244 NULL, |
| 243 NULL, | 245 path_, |
| 244 &parsed_bookmarks, | 246 &parsed_bookmarks, |
| 245 NULL, | 247 &favicons); |
| 246 &favicons); | |
| 247 | 248 |
| 248 // Check loaded favicon (url1 is represented by 4 separate bookmarks). | 249 // Check loaded favicon (url1 is represented by 4 separate bookmarks). |
| 249 EXPECT_EQ(4U, favicons.size()); | 250 EXPECT_EQ(4U, favicons.size()); |
| 250 for (size_t i = 0; i < favicons.size(); i++) { | 251 for (size_t i = 0; i < favicons.size(); i++) { |
| 251 if (url1_favicon == favicons[i].favicon_url) { | 252 if (url1_favicon == favicons[i].favicon_url) { |
| 252 EXPECT_EQ(1U, favicons[i].urls.size()); | 253 EXPECT_EQ(1U, favicons[i].urls.size()); |
| 253 std::set<GURL>::const_iterator iter = favicons[i].urls.find(url1); | 254 std::set<GURL>::const_iterator iter = favicons[i].urls.find(url1); |
| 254 ASSERT_TRUE(iter != favicons[i].urls.end()); | 255 ASSERT_TRUE(iter != favicons[i].urls.end()); |
| 255 ASSERT_TRUE(*iter == url1); | 256 ASSERT_TRUE(*iter == url1); |
| 256 ASSERT_TRUE(favicons[i].png_data == icon_data); | 257 ASSERT_TRUE(favicons[i].png_data == icon_data); |
| 257 } | 258 } |
| 258 } | 259 } |
| 259 | 260 |
| 260 // Verify we got back what we wrote. | 261 // Verify we got back what we wrote. |
| 261 ASSERT_EQ(8U, parsed_bookmarks.size()); | 262 ASSERT_EQ(9U, parsed_bookmarks.size()); |
| 262 // Windows and ChromeOS builds use Sentence case. | 263 // Windows and ChromeOS builds use Sentence case. |
| 263 string16 bookmark_folder_name = | 264 string16 bookmark_folder_name = |
| 264 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_FOLDER_NAME); | 265 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_FOLDER_NAME); |
| 265 AssertBookmarkEntryEquals(parsed_bookmarks[0], true, url1, url1_title, t1, | 266 AssertBookmarkEntryEquals(parsed_bookmarks[0], true, url1, url1_title, t1, |
| 266 bookmark_folder_name, f1_title, string16()); | 267 bookmark_folder_name, f1_title, string16()); |
| 267 AssertBookmarkEntryEquals(parsed_bookmarks[1], true, url2, url2_title, t2, | 268 AssertBookmarkEntryEquals(parsed_bookmarks[1], true, url2, url2_title, t2, |
| 268 bookmark_folder_name, f1_title, f2_title); | 269 bookmark_folder_name, f1_title, f2_title); |
| 269 AssertBookmarkEntryEquals(parsed_bookmarks[2], true, url3, url3_title, t3, | 270 AssertBookmarkEntryEquals(parsed_bookmarks[2], true, url3, url3_title, t3, |
| 270 bookmark_folder_name, string16(), string16()); | 271 bookmark_folder_name, string16(), string16()); |
| 271 AssertBookmarkEntryEquals(parsed_bookmarks[3], true, url4, url4_title, t4, | 272 AssertBookmarkEntryEquals(parsed_bookmarks[3], true, url4, url4_title, t4, |
| 272 bookmark_folder_name, string16(), string16()); | 273 bookmark_folder_name, string16(), string16()); |
| 273 AssertBookmarkEntryEquals(parsed_bookmarks[4], false, url1, url1_title, t1, | 274 AssertBookmarkEntryEquals(parsed_bookmarks[4], false, url1, url1_title, t1, |
| 274 string16(), string16(), string16()); | 275 string16(), string16(), string16()); |
| 275 AssertBookmarkEntryEquals(parsed_bookmarks[5], false, url2, url2_title, t2, | 276 AssertBookmarkEntryEquals(parsed_bookmarks[5], false, url2, url2_title, t2, |
| 276 string16(), string16(), string16()); | 277 string16(), string16(), string16()); |
| 277 AssertBookmarkEntryEquals(parsed_bookmarks[6], false, url1, url1_title, t1, | 278 AssertBookmarkEntryEquals(parsed_bookmarks[6], false, url1, url1_title, t1, |
| 278 f3_title, f4_title, string16()); | 279 f3_title, f4_title, string16()); |
| 279 AssertBookmarkEntryEquals(parsed_bookmarks[7], false, url1, url1_title, t1, | 280 AssertBookmarkEntryEquals(parsed_bookmarks[7], false, url1, url1_title, t1, |
| 280 string16(), string16(), string16()); | 281 string16(), string16(), string16()); |
| 282 AssertBookmarkEntryEquals(parsed_bookmarks[8], false, unnamed_bookmark_url, |
| 283 unnamed_bookmark_title, t2, |
| 284 string16(), string16(), string16()); |
| 281 } | 285 } |
| OLD | NEW |