bookmark_html_writer_unittest.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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. 4 5#include "chrome/browser/bookmarks/bookmark_html_writer.h" 6 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_factory.h" 16#include "chrome/browser/favicon/favicon_service.h" 17#include "chrome/browser/favicon/favicon_service_factory.h" 18#include "chrome/browser/history/history_service.h" 19#include "chrome/browser/history/history_service_factory.h" 20#include "chrome/common/importer/imported_bookmark_entry.h" 21#include "chrome/common/importer/imported_favicon_usage.h" 22#include "chrome/test/base/testing_profile.h" 23#include "chrome/utility/importer/bookmark_html_reader.h" 24#include "components/bookmarks/browser/bookmark_model.h" 25#include "components/bookmarks/test/bookmark_test_helpers.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" 32 33namespace { 34 35const int kIconWidth = 16; 36const int kIconHeight = 16; 37 38void MakeTestSkBitmap(int w, int h, SkBitmap* bmp) { 39 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); 40 bmp->allocPixels(); 41 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); 45 } 46} 47 48} // namespace 49 50class 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"); 56 } 57 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")); 67 68 result.append(base::ASCIIToUTF16(" url=") + 69 base::UTF8ToUTF16(entry.url.spec())); 70 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]); 76 } 77 78 result.append(base::ASCIIToUTF16(" title=")); 79 result.append(entry.title); 80 81 result.append(base::ASCIIToUTF16(" time=")); 82 result.append(base::TimeFormatFriendlyDateAndTime(entry.creation_time)); 83 return result; 84 } 85 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); 103 } 104 } 105 entry.title = title; 106 entry.creation_time = creation_time; 107 return BookmarkEntryToString(entry); 108 } 109 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)); 121 } 122 123 base::ScopedTempDir temp_dir_; 124 base::FilePath path_; 125}; 126 127// Class that will notify message loop when file is written. 128class BookmarksObserver : public BookmarksExportObserver { 129 public: 130 explicit BookmarksObserver(base::RunLoop* loop) : loop_(loop) { 131 DCHECK(loop); 132 } 133 134 virtual void OnExportFinished() OVERRIDE { 135 loop_->Quit(); 136 } 137 138 private: 139 base::RunLoop* loop_; 140 141 DISALLOW_COPY_AND_ASSIGN(BookmarksObserver); 142}; 143 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. 146TEST_F(BookmarkHTMLWriterTest, Test) { 147 content::TestBrowserThreadBundle thread_bundle; 148 149 TestingProfile profile; 150 ASSERT_TRUE(profile.CreateHistoryService(true, false)); 151 profile.BlockUntilHistoryProcessesPendingRequests(); 152 profile.CreateFaviconService(); 153 profile.CreateBookmarkModel(true); 154 155 BookmarkModel* model = BookmarkModelFactory::GetForProfile(&profile); 156 test::WaitForBookmarkModelToLoad(model); 157 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); 163 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, Profile::EXPLICIT_ACCESS)-> 204 AddPage(url1, base::Time::Now(), history::SOURCE_BROWSED); 205 FaviconServiceFactory::GetForProfile(&profile, Profile::EXPLICIT_ACCESS) 206 ->SetFavicons(url1, 207 url1_favicon, 208 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); 214 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(), 227 1, 228 unnamed_bookmark_title, 229 unnamed_bookmark_url, 230 t2, 231 NULL); 232 233 base::RunLoop run_loop; 234 235 // Write to a temp file. 236 BookmarksObserver observer(&run_loop); 237 bookmark_html_writer::WriteBookmarks(&profile, path_, &observer); 238 run_loop.Run(); 239 240 // Clear favicon so that it would be read from file. 241 FaviconServiceFactory::GetForProfile(&profile, Profile::EXPLICIT_ACCESS) 242 ->SetFavicons(url1, url1_favicon, favicon_base::FAVICON, gfx::Image()); 243 244 // Read the bookmarks back in. 245 std::vector<ImportedBookmarkEntry> parsed_bookmarks; 246 std::vector<ImportedFaviconUsage> favicons; 247 bookmark_html_reader::ImportBookmarksFile(base::Callback<bool(void)>(), 248 base::Callback<bool(const GURL&)>(), 249 path_, 250 &parsed_bookmarks, 251 &favicons); 252 253 // Check loaded favicon (url1 is represented by 4 separate bookmarks). 254 EXPECT_EQ(4U, favicons.size()); 255 for (size_t i = 0; i < favicons.size(); i++) { 256 if (url1_favicon == favicons[i].favicon_url) { 257 EXPECT_EQ(1U, favicons[i].urls.size()); 258 std::set<GURL>::const_iterator iter = favicons[i].urls.find(url1); 259 ASSERT_TRUE(iter != favicons[i].urls.end()); 260 ASSERT_TRUE(*iter == url1); 261 ASSERT_TRUE(favicons[i].png_data == icon_data); 262 } 263 } 264 265 // Verify we got back what we wrote. 266 ASSERT_EQ(9U, parsed_bookmarks.size()); 267 // Windows and ChromeOS builds use Sentence case. 268 base::string16 bookmark_folder_name = 269 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_FOLDER_NAME); 270 AssertBookmarkEntryEquals(parsed_bookmarks[0], true, url1, url1_title, t1, 271 bookmark_folder_name, f1_title, base::string16()); 272 AssertBookmarkEntryEquals(parsed_bookmarks[1], true, url2, url2_title, t2, 273 bookmark_folder_name, f1_title, f2_title); 274 AssertBookmarkEntryEquals(parsed_bookmarks[2], true, url3, url3_title, t3, 275 bookmark_folder_name, base::string16(), 276 base::string16()); 277 AssertBookmarkEntryEquals(parsed_bookmarks[3], true, url4, url4_title, t4, 278 bookmark_folder_name, base::string16(), 279 base::string16()); 280 AssertBookmarkEntryEquals(parsed_bookmarks[4], false, url1, url1_title, t1, 281 base::string16(), base::string16(), 282 base::string16()); 283 AssertBookmarkEntryEquals(parsed_bookmarks[5], false, url2, url2_title, t2, 284 base::string16(), base::string16(), 285 base::string16()); 286 AssertBookmarkEntryEquals(parsed_bookmarks[6], false, url1, url1_title, t1, 287 f3_title, f4_title, base::string16()); 288 AssertBookmarkEntryEquals(parsed_bookmarks[7], false, url1, url1_title, t1, 289 base::string16(), base::string16(), 290 base::string16()); 291 AssertBookmarkEntryEquals(parsed_bookmarks[8], false, unnamed_bookmark_url, 292 unnamed_bookmark_title, t2, 293 base::string16(), base::string16(), 294 base::string16()); 295} 296