Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / bookmarks / bookmark_html_writer_unittest.cc
blobc2331be919dad6a152da5160b396194c0dc3661a
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"
33 namespace {
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);
40 bmp->allocPixels();
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);
48 } // namespace
50 class BookmarkHTMLWriterTest : public testing::Test {
51 protected:
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
59 // testing.
60 base::string16 BookmarkEntryToString(const ImportedBookmarkEntry& entry) {
61 base::string16 result;
62 result.append(base::ASCIIToUTF16("on_toolbar="));
63 if (entry.in_toolbar)
64 result.append(base::ASCIIToUTF16("true"));
65 else
66 result.append(base::ASCIIToUTF16("false"));
68 result.append(base::ASCIIToUTF16(" url=") +
69 base::UTF8ToUTF16(entry.url.spec()));
71 result.append(base::ASCIIToUTF16(" path="));
72 for (size_t i = 0; i < entry.path.size(); ++i) {
73 if (i != 0)
74 result.append(base::ASCIIToUTF16("/"));
75 result.append(entry.path[i]);
78 result.append(base::ASCIIToUTF16(" title="));
79 result.append(entry.title);
81 result.append(base::ASCIIToUTF16(" time="));
82 result.append(base::TimeFormatFriendlyDateAndTime(entry.creation_time));
83 return result;
86 // Creates a set of bookmark values to a string for assertion testing.
87 base::string16 BookmarkValuesToString(bool on_toolbar,
88 const GURL& url,
89 const base::string16& title,
90 base::Time creation_time,
91 const base::string16& f1,
92 const base::string16& f2,
93 const base::string16& f3) {
94 ImportedBookmarkEntry entry;
95 entry.in_toolbar = on_toolbar;
96 entry.url = url;
97 if (!f1.empty()) {
98 entry.path.push_back(f1);
99 if (!f2.empty()) {
100 entry.path.push_back(f2);
101 if (!f3.empty())
102 entry.path.push_back(f3);
105 entry.title = title;
106 entry.creation_time = creation_time;
107 return BookmarkEntryToString(entry);
110 void AssertBookmarkEntryEquals(const ImportedBookmarkEntry& entry,
111 bool on_toolbar,
112 const GURL& url,
113 const base::string16& title,
114 base::Time creation_time,
115 const base::string16& f1,
116 const base::string16& f2,
117 const base::string16& f3) {
118 EXPECT_EQ(BookmarkValuesToString(on_toolbar, url, title, creation_time,
119 f1, f2, f3),
120 BookmarkEntryToString(entry));
123 base::ScopedTempDir temp_dir_;
124 base::FilePath path_;
127 // Class that will notify message loop when file is written.
128 class BookmarksObserver : public BookmarksExportObserver {
129 public:
130 explicit BookmarksObserver(base::RunLoop* loop) : loop_(loop) {
131 DCHECK(loop);
134 virtual void OnExportFinished() OVERRIDE {
135 loop_->Quit();
138 private:
139 base::RunLoop* loop_;
141 DISALLOW_COPY_AND_ASSIGN(BookmarksObserver);
144 // Tests bookmark_html_writer by populating a BookmarkModel, writing it out by
145 // way of bookmark_html_writer, then using the importer to read it back in.
146 TEST_F(BookmarkHTMLWriterTest, Test) {
147 content::TestBrowserThreadBundle thread_bundle;
149 TestingProfile profile;
150 ASSERT_TRUE(profile.CreateHistoryService(true, false));
151 profile.BlockUntilHistoryProcessesPendingRequests();
152 profile.CreateFaviconService();
153 profile.CreateBookmarkModel(true);
155 BookmarkModel* model = BookmarkModelFactory::GetForProfile(&profile);
156 test::WaitForBookmarkModelToLoad(model);
158 // Create test PNG representing favicon for url1.
159 SkBitmap bitmap;
160 MakeTestSkBitmap(kIconWidth, kIconHeight, &bitmap);
161 std::vector<unsigned char> icon_data;
162 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &icon_data);
164 // Populate the BookmarkModel. This creates the following bookmark structure:
165 // Bookmarks bar
166 // F1
167 // url1
168 // F2
169 // url2
170 // url3
171 // url4
172 // Other
173 // url1
174 // url2
175 // F3
176 // F4
177 // url1
178 // Mobile
179 // url1
180 // <bookmark without a title.>
181 base::string16 f1_title = base::ASCIIToUTF16("F\"&;<1\"");
182 base::string16 f2_title = base::ASCIIToUTF16("F2");
183 base::string16 f3_title = base::ASCIIToUTF16("F 3");
184 base::string16 f4_title = base::ASCIIToUTF16("F4");
185 base::string16 url1_title = base::ASCIIToUTF16("url 1");
186 base::string16 url2_title = base::ASCIIToUTF16("url&2");
187 base::string16 url3_title = base::ASCIIToUTF16("url\"3");
188 base::string16 url4_title = base::ASCIIToUTF16("url\"&;");
189 base::string16 unnamed_bookmark_title = base::ASCIIToUTF16("");
190 GURL url1("http://url1");
191 GURL url1_favicon("http://url1/icon.ico");
192 GURL url2("http://url2");
193 GURL url3("http://url3");
194 GURL url4("javascript:alert(\"Hello!\");");
195 GURL unnamed_bookmark_url("about:blank");
196 base::Time t1(base::Time::Now());
197 base::Time t2(t1 + base::TimeDelta::FromHours(1));
198 base::Time t3(t1 + base::TimeDelta::FromHours(1));
199 base::Time t4(t1 + base::TimeDelta::FromHours(1));
200 const BookmarkNode* f1 = model->AddFolder(
201 model->bookmark_bar_node(), 0, f1_title);
202 model->AddURLWithCreationTime(f1, 0, url1_title, url1, t1);
203 HistoryServiceFactory::GetForProfile(&profile, Profile::EXPLICIT_ACCESS)->
204 AddPage(url1, base::Time::Now(), history::SOURCE_BROWSED);
205 FaviconServiceFactory::GetForProfile(
206 &profile, Profile::EXPLICIT_ACCESS)->SetFavicons(
207 url1, url1_favicon, chrome::FAVICON,
208 gfx::Image::CreateFrom1xBitmap(bitmap));
209 const BookmarkNode* f2 = model->AddFolder(f1, 1, f2_title);
210 model->AddURLWithCreationTime(f2, 0, url2_title, url2, t2);
211 model->AddURLWithCreationTime(model->bookmark_bar_node(),
212 1, url3_title, url3, t3);
214 model->AddURLWithCreationTime(model->other_node(), 0, url1_title, url1, t1);
215 model->AddURLWithCreationTime(model->other_node(), 1, url2_title, url2, t2);
216 const BookmarkNode* f3 = model->AddFolder(model->other_node(), 2, f3_title);
217 const BookmarkNode* f4 = model->AddFolder(f3, 0, f4_title);
218 model->AddURLWithCreationTime(f4, 0, url1_title, url1, t1);
219 model->AddURLWithCreationTime(model->bookmark_bar_node(), 2, url4_title,
220 url4, t4);
221 model->AddURLWithCreationTime(model->mobile_node(), 0, url1_title, url1, t1);
222 model->AddURLWithCreationTime(model->mobile_node(), 1, unnamed_bookmark_title,
223 unnamed_bookmark_url, t2);
225 base::RunLoop run_loop;
227 // Write to a temp file.
228 BookmarksObserver observer(&run_loop);
229 bookmark_html_writer::WriteBookmarks(&profile, path_, &observer);
230 run_loop.Run();
232 // Clear favicon so that it would be read from file.
233 FaviconServiceFactory::GetForProfile(
234 &profile, Profile::EXPLICIT_ACCESS)->SetFavicons(
235 url1, url1_favicon, chrome::FAVICON, gfx::Image());
237 // Read the bookmarks back in.
238 std::vector<ImportedBookmarkEntry> parsed_bookmarks;
239 std::vector<ImportedFaviconUsage> favicons;
240 bookmark_html_reader::ImportBookmarksFile(base::Callback<bool(void)>(),
241 base::Callback<bool(const GURL&)>(),
242 path_,
243 &parsed_bookmarks,
244 &favicons);
246 // Check loaded favicon (url1 is represented by 4 separate bookmarks).
247 EXPECT_EQ(4U, favicons.size());
248 for (size_t i = 0; i < favicons.size(); i++) {
249 if (url1_favicon == favicons[i].favicon_url) {
250 EXPECT_EQ(1U, favicons[i].urls.size());
251 std::set<GURL>::const_iterator iter = favicons[i].urls.find(url1);
252 ASSERT_TRUE(iter != favicons[i].urls.end());
253 ASSERT_TRUE(*iter == url1);
254 ASSERT_TRUE(favicons[i].png_data == icon_data);
258 // Verify we got back what we wrote.
259 ASSERT_EQ(9U, parsed_bookmarks.size());
260 // Windows and ChromeOS builds use Sentence case.
261 base::string16 bookmark_folder_name =
262 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_FOLDER_NAME);
263 AssertBookmarkEntryEquals(parsed_bookmarks[0], true, url1, url1_title, t1,
264 bookmark_folder_name, f1_title, base::string16());
265 AssertBookmarkEntryEquals(parsed_bookmarks[1], true, url2, url2_title, t2,
266 bookmark_folder_name, f1_title, f2_title);
267 AssertBookmarkEntryEquals(parsed_bookmarks[2], true, url3, url3_title, t3,
268 bookmark_folder_name, base::string16(),
269 base::string16());
270 AssertBookmarkEntryEquals(parsed_bookmarks[3], true, url4, url4_title, t4,
271 bookmark_folder_name, base::string16(),
272 base::string16());
273 AssertBookmarkEntryEquals(parsed_bookmarks[4], false, url1, url1_title, t1,
274 base::string16(), base::string16(),
275 base::string16());
276 AssertBookmarkEntryEquals(parsed_bookmarks[5], false, url2, url2_title, t2,
277 base::string16(), base::string16(),
278 base::string16());
279 AssertBookmarkEntryEquals(parsed_bookmarks[6], false, url1, url1_title, t1,
280 f3_title, f4_title, base::string16());
281 AssertBookmarkEntryEquals(parsed_bookmarks[7], false, url1, url1_title, t1,
282 base::string16(), base::string16(),
283 base::string16());
284 AssertBookmarkEntryEquals(parsed_bookmarks[8], false, unnamed_bookmark_url,
285 unnamed_bookmark_title, t2,
286 base::string16(), base::string16(),
287 base::string16());