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.
5 #include "chrome/browser/bookmarks/bookmark_html_writer.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/i18n/time_formatting.h"
9 #include "base/path_service.h"
10 #include "base/run_loop.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/bookmarks/bookmark_model.h"
16 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
17 #include "chrome/browser/bookmarks/bookmark_test_helpers.h"
18 #include "chrome/browser/favicon/favicon_service.h"
19 #include "chrome/browser/favicon/favicon_service_factory.h"
20 #include "chrome/browser/history/history_service.h"
21 #include "chrome/browser/history/history_service_factory.h"
22 #include "chrome/common/importer/imported_bookmark_entry.h"
23 #include "chrome/common/importer/imported_favicon_usage.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "chrome/utility/importer/bookmark_html_reader.h"
26 #include "content/public/test/test_browser_thread_bundle.h"
27 #include "grit/generated_resources.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "third_party/skia/include/core/SkBitmap.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/gfx/codec/png_codec.h"
35 const int kIconWidth = 16;
36 const int kIconHeight = 16;
38 void MakeTestSkBitmap(int w, int h, SkBitmap* bmp) {
39 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
42 uint32_t* src_data = bmp->getAddr32(0, 0);
43 for (int i = 0; i < w * h; i++) {
44 src_data[i] = SkPreMultiplyARGB(i % 255, i % 250, i % 245, i % 240);
50 class BookmarkHTMLWriterTest : public testing::Test {
52 virtual void SetUp() {
53 testing::Test::SetUp();
54 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
55 path_ = temp_dir_.path().AppendASCII("bookmarks.html");
58 // Converts an ImportedBookmarkEntry to a string suitable for assertion
60 string16 BookmarkEntryToString(const ImportedBookmarkEntry& entry) {
62 result.append(ASCIIToUTF16("on_toolbar="));
64 result.append(ASCIIToUTF16("true"));
66 result.append(ASCIIToUTF16("false"));
68 result.append(ASCIIToUTF16(" url=") + UTF8ToUTF16(entry.url.spec()));
70 result.append(ASCIIToUTF16(" path="));
71 for (size_t i = 0; i < entry.path.size(); ++i) {
73 result.append(ASCIIToUTF16("/"));
74 result.append(entry.path[i]);
77 result.append(ASCIIToUTF16(" title="));
78 result.append(entry.title);
80 result.append(ASCIIToUTF16(" time="));
81 result.append(base::TimeFormatFriendlyDateAndTime(entry.creation_time));
85 // Creates a set of bookmark values to a string for assertion testing.
86 string16 BookmarkValuesToString(bool on_toolbar,
88 const string16& title,
89 base::Time creation_time,
93 ImportedBookmarkEntry entry;
94 entry.in_toolbar = on_toolbar;
97 entry.path.push_back(f1);
99 entry.path.push_back(f2);
101 entry.path.push_back(f3);
105 entry.creation_time = creation_time;
106 return BookmarkEntryToString(entry);
109 void AssertBookmarkEntryEquals(const ImportedBookmarkEntry& entry,
112 const string16& title,
113 base::Time creation_time,
116 const string16& f3) {
117 EXPECT_EQ(BookmarkValuesToString(on_toolbar, url, title, creation_time,
119 BookmarkEntryToString(entry));
122 base::ScopedTempDir temp_dir_;
123 base::FilePath path_;
126 // Class that will notify message loop when file is written.
127 class BookmarksObserver : public BookmarksExportObserver {
129 explicit BookmarksObserver(base::RunLoop* loop) : loop_(loop) {
133 virtual void OnExportFinished() OVERRIDE {
138 base::RunLoop* loop_;
140 DISALLOW_COPY_AND_ASSIGN(BookmarksObserver);
143 // Tests bookmark_html_writer by populating a BookmarkModel, writing it out by
144 // way of bookmark_html_writer, then using the importer to read it back in.
145 TEST_F(BookmarkHTMLWriterTest, Test) {
146 content::TestBrowserThreadBundle thread_bundle;
148 TestingProfile profile;
149 ASSERT_TRUE(profile.CreateHistoryService(true, false));
150 profile.BlockUntilHistoryProcessesPendingRequests();
151 profile.CreateFaviconService();
152 profile.CreateBookmarkModel(true);
154 BookmarkModel* model = BookmarkModelFactory::GetForProfile(&profile);
155 test::WaitForBookmarkModelToLoad(model);
157 // Create test PNG representing favicon for url1.
159 MakeTestSkBitmap(kIconWidth, kIconHeight, &bitmap);
160 std::vector<unsigned char> icon_data;
161 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &icon_data);
163 // Populate the BookmarkModel. This creates the following bookmark structure:
179 // <bookmark without a title.>
180 string16 f1_title = ASCIIToUTF16("F\"&;<1\"");
181 string16 f2_title = ASCIIToUTF16("F2");
182 string16 f3_title = ASCIIToUTF16("F 3");
183 string16 f4_title = ASCIIToUTF16("F4");
184 string16 url1_title = ASCIIToUTF16("url 1");
185 string16 url2_title = ASCIIToUTF16("url&2");
186 string16 url3_title = ASCIIToUTF16("url\"3");
187 string16 url4_title = ASCIIToUTF16("url\"&;");
188 string16 unnamed_bookmark_title = ASCIIToUTF16("");
189 GURL url1("http://url1");
190 GURL url1_favicon("http://url1/icon.ico");
191 GURL url2("http://url2");
192 GURL url3("http://url3");
193 GURL url4("javascript:alert(\"Hello!\");");
194 GURL unnamed_bookmark_url("about:blank");
195 base::Time t1(base::Time::Now());
196 base::Time t2(t1 + base::TimeDelta::FromHours(1));
197 base::Time t3(t1 + base::TimeDelta::FromHours(1));
198 base::Time t4(t1 + base::TimeDelta::FromHours(1));
199 const BookmarkNode* f1 = model->AddFolder(
200 model->bookmark_bar_node(), 0, f1_title);
201 model->AddURLWithCreationTime(f1, 0, url1_title, url1, t1);
202 HistoryServiceFactory::GetForProfile(&profile, Profile::EXPLICIT_ACCESS)->
203 AddPage(url1, base::Time::Now(), history::SOURCE_BROWSED);
204 FaviconServiceFactory::GetForProfile(
205 &profile, Profile::EXPLICIT_ACCESS)->SetFavicons(
206 url1, url1_favicon, chrome::FAVICON,
207 gfx::Image::CreateFrom1xBitmap(bitmap));
208 const BookmarkNode* f2 = model->AddFolder(f1, 1, f2_title);
209 model->AddURLWithCreationTime(f2, 0, url2_title, url2, t2);
210 model->AddURLWithCreationTime(model->bookmark_bar_node(),
211 1, url3_title, url3, t3);
213 model->AddURLWithCreationTime(model->other_node(), 0, url1_title, url1, t1);
214 model->AddURLWithCreationTime(model->other_node(), 1, url2_title, url2, t2);
215 const BookmarkNode* f3 = model->AddFolder(model->other_node(), 2, f3_title);
216 const BookmarkNode* f4 = model->AddFolder(f3, 0, f4_title);
217 model->AddURLWithCreationTime(f4, 0, url1_title, url1, t1);
218 model->AddURLWithCreationTime(model->bookmark_bar_node(), 2, url4_title,
220 model->AddURLWithCreationTime(model->mobile_node(), 0, url1_title, url1, t1);
221 model->AddURLWithCreationTime(model->mobile_node(), 1, unnamed_bookmark_title,
222 unnamed_bookmark_url, t2);
224 base::RunLoop run_loop;
226 // Write to a temp file.
227 BookmarksObserver observer(&run_loop);
228 bookmark_html_writer::WriteBookmarks(&profile, path_, &observer);
231 // Clear favicon so that it would be read from file.
232 FaviconServiceFactory::GetForProfile(
233 &profile, Profile::EXPLICIT_ACCESS)->SetFavicons(
234 url1, url1_favicon, chrome::FAVICON, gfx::Image());
236 // Read the bookmarks back in.
237 std::vector<ImportedBookmarkEntry> parsed_bookmarks;
238 std::vector<ImportedFaviconUsage> favicons;
239 bookmark_html_reader::ImportBookmarksFile(base::Callback<bool(void)>(),
240 base::Callback<bool(const GURL&)>(),
245 // Check loaded favicon (url1 is represented by 4 separate bookmarks).
246 EXPECT_EQ(4U, favicons.size());
247 for (size_t i = 0; i < favicons.size(); i++) {
248 if (url1_favicon == favicons[i].favicon_url) {
249 EXPECT_EQ(1U, favicons[i].urls.size());
250 std::set<GURL>::const_iterator iter = favicons[i].urls.find(url1);
251 ASSERT_TRUE(iter != favicons[i].urls.end());
252 ASSERT_TRUE(*iter == url1);
253 ASSERT_TRUE(favicons[i].png_data == icon_data);
257 // Verify we got back what we wrote.
258 ASSERT_EQ(9U, parsed_bookmarks.size());
259 // Windows and ChromeOS builds use Sentence case.
260 string16 bookmark_folder_name =
261 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_FOLDER_NAME);
262 AssertBookmarkEntryEquals(parsed_bookmarks[0], true, url1, url1_title, t1,
263 bookmark_folder_name, f1_title, string16());
264 AssertBookmarkEntryEquals(parsed_bookmarks[1], true, url2, url2_title, t2,
265 bookmark_folder_name, f1_title, f2_title);
266 AssertBookmarkEntryEquals(parsed_bookmarks[2], true, url3, url3_title, t3,
267 bookmark_folder_name, string16(), string16());
268 AssertBookmarkEntryEquals(parsed_bookmarks[3], true, url4, url4_title, t4,
269 bookmark_folder_name, string16(), string16());
270 AssertBookmarkEntryEquals(parsed_bookmarks[4], false, url1, url1_title, t1,
271 string16(), string16(), string16());
272 AssertBookmarkEntryEquals(parsed_bookmarks[5], false, url2, url2_title, t2,
273 string16(), string16(), string16());
274 AssertBookmarkEntryEquals(parsed_bookmarks[6], false, url1, url1_title, t1,
275 f3_title, f4_title, string16());
276 AssertBookmarkEntryEquals(parsed_bookmarks[7], false, url1, url1_title, t1,
277 string16(), string16(), string16());
278 AssertBookmarkEntryEquals(parsed_bookmarks[8], false, unnamed_bookmark_url,
279 unnamed_bookmark_title, t2,
280 string16(), string16(), string16());