Componentize HistoryURLProvider/ScoredHistoryMatch.
[chromium-blink-merge.git] / chrome / browser / bookmarks / bookmark_html_writer_unittest.cc
blob362b098956d3dab69a8028d9b8b93accbce63200
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/run_loop.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
15 #include "chrome/browser/favicon/favicon_service_factory.h"
16 #include "chrome/browser/history/history_service_factory.h"
17 #include "chrome/common/importer/imported_bookmark_entry.h"
18 #include "chrome/common/importer/importer_data_types.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "chrome/utility/importer/bookmark_html_reader.h"
21 #include "components/bookmarks/browser/bookmark_model.h"
22 #include "components/bookmarks/test/bookmark_test_helpers.h"
23 #include "components/favicon/core/favicon_service.h"
24 #include "components/favicon_base/favicon_usage_data.h"
25 #include "components/history/core/browser/history_service.h"
26 #include "content/public/test/test_browser_thread_bundle.h"
27 #include "grit/components_strings.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 using bookmarks::BookmarkModel;
34 using bookmarks::BookmarkNode;
36 namespace {
38 const int kIconWidth = 16;
39 const int kIconHeight = 16;
41 void MakeTestSkBitmap(int w, int h, SkBitmap* bmp) {
42 bmp->allocN32Pixels(w, h);
44 uint32_t* src_data = bmp->getAddr32(0, 0);
45 for (int i = 0; i < w * h; i++) {
46 src_data[i] = SkPreMultiplyARGB(i % 255, i % 250, i % 245, i % 240);
50 } // namespace
52 class BookmarkHTMLWriterTest : public testing::Test {
53 protected:
54 void SetUp() override {
55 testing::Test::SetUp();
56 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
57 path_ = temp_dir_.path().AppendASCII("bookmarks.html");
60 // Converts an ImportedBookmarkEntry to a string suitable for assertion
61 // testing.
62 base::string16 BookmarkEntryToString(const ImportedBookmarkEntry& entry) {
63 base::string16 result;
64 result.append(base::ASCIIToUTF16("on_toolbar="));
65 if (entry.in_toolbar)
66 result.append(base::ASCIIToUTF16("true"));
67 else
68 result.append(base::ASCIIToUTF16("false"));
70 result.append(base::ASCIIToUTF16(" url=") +
71 base::UTF8ToUTF16(entry.url.spec()));
73 result.append(base::ASCIIToUTF16(" path="));
74 for (size_t i = 0; i < entry.path.size(); ++i) {
75 if (i != 0)
76 result.append(base::ASCIIToUTF16("/"));
77 result.append(entry.path[i]);
80 result.append(base::ASCIIToUTF16(" title="));
81 result.append(entry.title);
83 result.append(base::ASCIIToUTF16(" time="));
84 result.append(base::TimeFormatFriendlyDateAndTime(entry.creation_time));
85 return result;
88 // Creates a set of bookmark values to a string for assertion testing.
89 base::string16 BookmarkValuesToString(bool on_toolbar,
90 const GURL& url,
91 const base::string16& title,
92 base::Time creation_time,
93 const base::string16& f1,
94 const base::string16& f2,
95 const base::string16& f3) {
96 ImportedBookmarkEntry entry;
97 entry.in_toolbar = on_toolbar;
98 entry.url = url;
99 if (!f1.empty()) {
100 entry.path.push_back(f1);
101 if (!f2.empty()) {
102 entry.path.push_back(f2);
103 if (!f3.empty())
104 entry.path.push_back(f3);
107 entry.title = title;
108 entry.creation_time = creation_time;
109 return BookmarkEntryToString(entry);
112 void AssertBookmarkEntryEquals(const ImportedBookmarkEntry& entry,
113 bool on_toolbar,
114 const GURL& url,
115 const base::string16& title,
116 base::Time creation_time,
117 const base::string16& f1,
118 const base::string16& f2,
119 const base::string16& f3) {
120 EXPECT_EQ(BookmarkValuesToString(on_toolbar, url, title, creation_time,
121 f1, f2, f3),
122 BookmarkEntryToString(entry));
125 base::ScopedTempDir temp_dir_;
126 base::FilePath path_;
129 // Class that will notify message loop when file is written.
130 class BookmarksObserver : public BookmarksExportObserver {
131 public:
132 explicit BookmarksObserver(base::RunLoop* loop) : loop_(loop) {
133 DCHECK(loop);
136 void OnExportFinished() override { 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 bookmarks::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->AddURLWithCreationTimeAndMetaInfo(f1, 0, url1_title, url1, t1, NULL);
203 HistoryServiceFactory::GetForProfile(&profile,
204 ServiceAccessType::EXPLICIT_ACCESS)
205 ->AddPage(url1, base::Time::Now(), history::SOURCE_BROWSED);
206 FaviconServiceFactory::GetForProfile(&profile,
207 ServiceAccessType::EXPLICIT_ACCESS)
208 ->SetFavicons(url1, url1_favicon, favicon_base::FAVICON,
209 gfx::Image::CreateFrom1xBitmap(bitmap));
210 const BookmarkNode* f2 = model->AddFolder(f1, 1, f2_title);
211 model->AddURLWithCreationTimeAndMetaInfo(f2, 0, url2_title, url2, t2, NULL);
212 model->AddURLWithCreationTimeAndMetaInfo(
213 model->bookmark_bar_node(), 1, url3_title, url3, t3, NULL);
215 model->AddURLWithCreationTimeAndMetaInfo(
216 model->other_node(), 0, url1_title, url1, t1, NULL);
217 model->AddURLWithCreationTimeAndMetaInfo(
218 model->other_node(), 1, url2_title, url2, t2, NULL);
219 const BookmarkNode* f3 = model->AddFolder(model->other_node(), 2, f3_title);
220 const BookmarkNode* f4 = model->AddFolder(f3, 0, f4_title);
221 model->AddURLWithCreationTimeAndMetaInfo(f4, 0, url1_title, url1, t1, NULL);
222 model->AddURLWithCreationTimeAndMetaInfo(
223 model->bookmark_bar_node(), 2, url4_title, url4, t4, NULL);
224 model->AddURLWithCreationTimeAndMetaInfo(
225 model->mobile_node(), 0, url1_title, url1, t1, NULL);
226 model->AddURLWithCreationTimeAndMetaInfo(model->mobile_node(),
228 unnamed_bookmark_title,
229 unnamed_bookmark_url,
231 NULL);
233 base::RunLoop run_loop;
235 // Write to a temp file.
236 BookmarksObserver observer(&run_loop);
237 bookmark_html_writer::WriteBookmarks(&profile, path_, &observer);
238 run_loop.Run();
240 // Clear favicon so that it would be read from file.
241 FaviconServiceFactory::GetForProfile(&profile,
242 ServiceAccessType::EXPLICIT_ACCESS)
243 ->SetFavicons(url1, url1_favicon, favicon_base::FAVICON, gfx::Image());
245 // Read the bookmarks back in.
246 std::vector<ImportedBookmarkEntry> parsed_bookmarks;
247 std::vector<importer::SearchEngineInfo> parsed_search_engines;
248 favicon_base::FaviconUsageDataList favicons;
249 bookmark_html_reader::ImportBookmarksFile(base::Callback<bool(void)>(),
250 base::Callback<bool(const GURL&)>(),
251 path_,
252 &parsed_bookmarks,
253 &parsed_search_engines,
254 &favicons);
256 // Check loaded favicon (url1 is represented by 4 separate bookmarks).
257 EXPECT_EQ(4U, favicons.size());
258 for (size_t i = 0; i < favicons.size(); i++) {
259 if (url1_favicon == favicons[i].favicon_url) {
260 EXPECT_EQ(1U, favicons[i].urls.size());
261 std::set<GURL>::const_iterator iter = favicons[i].urls.find(url1);
262 ASSERT_TRUE(iter != favicons[i].urls.end());
263 ASSERT_TRUE(*iter == url1);
264 ASSERT_TRUE(favicons[i].png_data == icon_data);
268 // Since we did not populate the BookmarkModel with any entry which can be
269 // imported as search engine, verify that we got back no search engines.
270 ASSERT_EQ(0U, parsed_search_engines.size());
272 // Verify we got back what we wrote.
273 ASSERT_EQ(9U, parsed_bookmarks.size());
274 // Windows and ChromeOS builds use Sentence case.
275 base::string16 bookmark_folder_name =
276 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_FOLDER_NAME);
277 AssertBookmarkEntryEquals(parsed_bookmarks[0], true, url1, url1_title, t1,
278 bookmark_folder_name, f1_title, base::string16());
279 AssertBookmarkEntryEquals(parsed_bookmarks[1], true, url2, url2_title, t2,
280 bookmark_folder_name, f1_title, f2_title);
281 AssertBookmarkEntryEquals(parsed_bookmarks[2], true, url3, url3_title, t3,
282 bookmark_folder_name, base::string16(),
283 base::string16());
284 AssertBookmarkEntryEquals(parsed_bookmarks[3], true, url4, url4_title, t4,
285 bookmark_folder_name, base::string16(),
286 base::string16());
287 AssertBookmarkEntryEquals(parsed_bookmarks[4], false, url1, url1_title, t1,
288 base::string16(), base::string16(),
289 base::string16());
290 AssertBookmarkEntryEquals(parsed_bookmarks[5], false, url2, url2_title, t2,
291 base::string16(), base::string16(),
292 base::string16());
293 AssertBookmarkEntryEquals(parsed_bookmarks[6], false, url1, url1_title, t1,
294 f3_title, f4_title, base::string16());
295 AssertBookmarkEntryEquals(parsed_bookmarks[7], false, url1, url1_title, t1,
296 base::string16(), base::string16(),
297 base::string16());
298 AssertBookmarkEntryEquals(parsed_bookmarks[8], false, unnamed_bookmark_url,
299 unnamed_bookmark_title, t2,
300 base::string16(), base::string16(),
301 base::string16());