- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / bookmarks / bookmark_html_writer_unittest.cc
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.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"
32
33 namespace {
34
35 const int kIconWidth = 16;
36 const int kIconHeight = 16;
37
38 void 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
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");
56   }
57
58   // Converts an ImportedBookmarkEntry to a string suitable for assertion
59   // testing.
60   string16 BookmarkEntryToString(const ImportedBookmarkEntry& entry) {
61     string16 result;
62     result.append(ASCIIToUTF16("on_toolbar="));
63     if (entry.in_toolbar)
64       result.append(ASCIIToUTF16("true"));
65     else
66       result.append(ASCIIToUTF16("false"));
67
68     result.append(ASCIIToUTF16(" url=") + UTF8ToUTF16(entry.url.spec()));
69
70     result.append(ASCIIToUTF16(" path="));
71     for (size_t i = 0; i < entry.path.size(); ++i) {
72       if (i != 0)
73         result.append(ASCIIToUTF16("/"));
74       result.append(entry.path[i]);
75     }
76
77     result.append(ASCIIToUTF16(" title="));
78     result.append(entry.title);
79
80     result.append(ASCIIToUTF16(" time="));
81     result.append(base::TimeFormatFriendlyDateAndTime(entry.creation_time));
82     return result;
83   }
84
85   // Creates a set of bookmark values to a string for assertion testing.
86   string16 BookmarkValuesToString(bool on_toolbar,
87                                   const GURL& url,
88                                   const string16& title,
89                                   base::Time creation_time,
90                                   const string16& f1,
91                                   const string16& f2,
92                                   const string16& f3) {
93     ImportedBookmarkEntry entry;
94     entry.in_toolbar = on_toolbar;
95     entry.url = url;
96     if (!f1.empty()) {
97       entry.path.push_back(f1);
98       if (!f2.empty()) {
99         entry.path.push_back(f2);
100         if (!f3.empty())
101           entry.path.push_back(f3);
102       }
103     }
104     entry.title = title;
105     entry.creation_time = creation_time;
106     return BookmarkEntryToString(entry);
107   }
108
109   void AssertBookmarkEntryEquals(const ImportedBookmarkEntry& entry,
110                                  bool on_toolbar,
111                                  const GURL& url,
112                                  const string16& title,
113                                  base::Time creation_time,
114                                  const string16& f1,
115                                  const string16& f2,
116                                  const string16& f3) {
117     EXPECT_EQ(BookmarkValuesToString(on_toolbar, url, title, creation_time,
118                                      f1, f2, f3),
119               BookmarkEntryToString(entry));
120   }
121
122   base::ScopedTempDir temp_dir_;
123   base::FilePath path_;
124 };
125
126 // Class that will notify message loop when file is written.
127 class BookmarksObserver : public BookmarksExportObserver {
128  public:
129   explicit BookmarksObserver(base::RunLoop* loop) : loop_(loop) {
130     DCHECK(loop);
131   }
132
133   virtual void OnExportFinished() OVERRIDE {
134     loop_->Quit();
135   }
136
137  private:
138   base::RunLoop* loop_;
139
140   DISALLOW_COPY_AND_ASSIGN(BookmarksObserver);
141 };
142
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;
147
148   TestingProfile profile;
149   ASSERT_TRUE(profile.CreateHistoryService(true, false));
150   profile.BlockUntilHistoryProcessesPendingRequests();
151   profile.CreateFaviconService();
152   profile.CreateBookmarkModel(true);
153
154   BookmarkModel* model = BookmarkModelFactory::GetForProfile(&profile);
155   test::WaitForBookmarkModelToLoad(model);
156
157   // Create test PNG representing favicon for url1.
158   SkBitmap bitmap;
159   MakeTestSkBitmap(kIconWidth, kIconHeight, &bitmap);
160   std::vector<unsigned char> icon_data;
161   gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &icon_data);
162
163   // Populate the BookmarkModel. This creates the following bookmark structure:
164   // Bookmarks bar
165   //   F1
166   //     url1
167   //     F2
168   //       url2
169   //   url3
170   //   url4
171   // Other
172   //   url1
173   //   url2
174   //   F3
175   //     F4
176   //       url1
177   // Mobile
178   //   url1
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);
212
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,
219                                 url4, t4);
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);
223
224   base::RunLoop run_loop;
225
226   // Write to a temp file.
227   BookmarksObserver observer(&run_loop);
228   bookmark_html_writer::WriteBookmarks(&profile, path_, &observer);
229   run_loop.Run();
230
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());
235
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&)>(),
241                                             path_,
242                                             &parsed_bookmarks,
243                                             &favicons);
244
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);
254     }
255   }
256
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());
281 }