Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: chrome/browser/bookmarks/bookmark_html_writer_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698