blob: 526b5d31887bac4d4ce75d6cc28326cfe9dcae2e [file] [log] [blame]
[email protected]b5048992008-11-07 04:31:351// Copyright (c) 2006-2008 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 "testing/gtest/include/gtest/gtest.h"
6
[email protected]584cbad2009-10-28 19:14:537#include "app/l10n_util.h"
[email protected]b5048992008-11-07 04:31:358#include "base/file_util.h"
9#include "base/path_service.h"
[email protected]35793b42009-09-25 20:00:0810#include "base/string_util.h"
[email protected]b5048992008-11-07 04:31:3511#include "base/time.h"
[email protected]fb895c62009-10-09 18:20:3012#include "base/i18n/time_formatting.h"
[email protected]b5048992008-11-07 04:31:3513#include "chrome/browser/bookmarks/bookmark_html_writer.h"
14#include "chrome/browser/bookmarks/bookmark_model.h"
15#include "chrome/browser/importer/firefox2_importer.h"
[email protected]584cbad2009-10-28 19:14:5316#include "grit/generated_resources.h"
[email protected]b5048992008-11-07 04:31:3517
18class BookmarkHTMLWriterTest : public testing::Test {
19 protected:
20 virtual void SetUp() {
21 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_));
[email protected]b647e0b2009-05-04 17:50:3122 path_ = path_.AppendASCII("bookmarks.html");
[email protected]b5048992008-11-07 04:31:3523 file_util::Delete(path_, true);
[email protected]b5048992008-11-07 04:31:3524 }
25
26 virtual void TearDown() {
27 if (!path_.empty())
28 file_util::Delete(path_, true);
29 }
30
[email protected]35793b42009-09-25 20:00:0831 // Converts a BookmarkEntry to a string suitable for assertion testing.
32 std::wstring BookmarkEntryToString(
33 const ProfileWriter::BookmarkEntry& entry) {
34 std::wstring result;
35 result.append(L"on_toolbar=");
36 if (entry.in_toolbar)
37 result.append(L"false");
38 else
39 result.append(L"true");
40
41 result.append(L" url=" + UTF8ToWide(entry.url.spec()));
42
43 result.append(L" path=");
44 for (size_t i = 0; i < entry.path.size(); ++i) {
45 if (i != 0)
46 result.append(L"/");
47 result.append(entry.path[i]);
48 }
49
50 result.append(L" title=");
51 result.append(entry.title);
52
53 result.append(L" time=");
54 result.append(base::TimeFormatFriendlyDateAndTime(entry.creation_time));
55 return result;
56 }
57
58 // Creates a set of bookmark values to a string for assertion testing.
59 std::wstring BookmarkValuesToString(bool on_toolbar,
60 const GURL& url,
61 const std::wstring& title,
62 base::Time creation_time,
63 const std::wstring& f1,
64 const std::wstring& f2,
65 const std::wstring& f3) {
66 ProfileWriter::BookmarkEntry entry;
67 entry.in_toolbar = on_toolbar;
68 entry.url = url;
69 // The first path element should always be 'x', as that is what we passed
70 // to the importer.
71 entry.path.push_back(L"x");
72 if (!f1.empty()) {
73 entry.path.push_back(f1);
74 if (!f2.empty()) {
75 entry.path.push_back(f2);
76 if (!f3.empty())
77 entry.path.push_back(f3);
78 }
79 }
80 entry.title = title;
81 entry.creation_time = creation_time;
82 return BookmarkEntryToString(entry);
83 }
84
[email protected]b5048992008-11-07 04:31:3585 void AssertBookmarkEntryEquals(const ProfileWriter::BookmarkEntry& entry,
86 bool on_toolbar,
87 const GURL& url,
88 const std::wstring& title,
89 base::Time creation_time,
90 const std::wstring& f1,
91 const std::wstring& f2,
92 const std::wstring& f3) {
[email protected]35793b42009-09-25 20:00:0893 EXPECT_EQ(BookmarkValuesToString(on_toolbar, url, title, creation_time,
94 f1, f2, f3),
95 BookmarkEntryToString(entry));
[email protected]b5048992008-11-07 04:31:3596 }
97
[email protected]b647e0b2009-05-04 17:50:3198 FilePath path_;
[email protected]b5048992008-11-07 04:31:3599};
100
101// Tests bookmark_html_writer by populating a BookmarkModel, writing it out by
102// way of bookmark_html_writer, then using the importer to read it back in.
[email protected]584cbad2009-10-28 19:14:53103TEST_F(BookmarkHTMLWriterTest, Test) {
[email protected]b5048992008-11-07 04:31:35104 // Populate the BookmarkModel. This creates the following bookmark structure:
[email protected]8acb64582009-03-25 00:57:18105 // Bookmarks bar
[email protected]b5048992008-11-07 04:31:35106 // F1
107 // url1
108 // F2
109 // url2
110 // url3
[email protected]6eac1b32009-06-30 21:00:27111 // url4
[email protected]b5048992008-11-07 04:31:35112 // Other
113 // url1
114 // url2
115 // F3
116 // F4
117 // url1
118 std::wstring f1_title = L"F\"&;<1\"";
119 std::wstring f2_title = L"F2";
120 std::wstring f3_title = L"F 3";
121 std::wstring f4_title = L"F4";
122 std::wstring url1_title = L"url 1";
123 std::wstring url2_title = L"url&2";
124 std::wstring url3_title = L"url\"3";
[email protected]6eac1b32009-06-30 21:00:27125 std::wstring url4_title = L"url\"&;";
[email protected]b5048992008-11-07 04:31:35126 GURL url1("http://url1");
127 GURL url2("http://url2");
128 GURL url3("http://url3");
[email protected]6eac1b32009-06-30 21:00:27129 GURL url4("http://\"&;\"");
[email protected]b5048992008-11-07 04:31:35130 BookmarkModel model(NULL);
131 base::Time t1(base::Time::Now());
132 base::Time t2(t1 + base::TimeDelta::FromHours(1));
133 base::Time t3(t1 + base::TimeDelta::FromHours(1));
[email protected]6eac1b32009-06-30 21:00:27134 base::Time t4(t1 + base::TimeDelta::FromHours(1));
[email protected]b3c33d462009-06-26 22:29:20135 const BookmarkNode* f1 = model.AddGroup(
136 model.GetBookmarkBarNode(), 0, f1_title);
[email protected]b5048992008-11-07 04:31:35137 model.AddURLWithCreationTime(f1, 0, url1_title, url1, t1);
[email protected]b3c33d462009-06-26 22:29:20138 const BookmarkNode* f2 = model.AddGroup(f1, 1, f2_title);
[email protected]b5048992008-11-07 04:31:35139 model.AddURLWithCreationTime(f2, 0, url2_title, url2, t2);
140 model.AddURLWithCreationTime(model.GetBookmarkBarNode(), 1, url3_title, url3,
141 t3);
142
143 model.AddURLWithCreationTime(model.other_node(), 0, url1_title, url1, t1);
144 model.AddURLWithCreationTime(model.other_node(), 1, url2_title, url2, t2);
[email protected]b3c33d462009-06-26 22:29:20145 const BookmarkNode* f3 = model.AddGroup(model.other_node(), 2, f3_title);
146 const BookmarkNode* f4 = model.AddGroup(f3, 0, f4_title);
[email protected]b5048992008-11-07 04:31:35147 model.AddURLWithCreationTime(f4, 0, url1_title, url1, t1);
[email protected]6eac1b32009-06-30 21:00:27148 model.AddURLWithCreationTime(model.GetBookmarkBarNode(), 2, url4_title,
149 url4, t4);
[email protected]b5048992008-11-07 04:31:35150
151 // Write to a temp file.
[email protected]304ec81a2009-09-25 15:15:04152 bookmark_html_writer::WriteBookmarks(NULL, &model, path_);
[email protected]b5048992008-11-07 04:31:35153
154 // Read the bookmarks back in.
155 std::vector<ProfileWriter::BookmarkEntry> parsed_bookmarks;
[email protected]b647e0b2009-05-04 17:50:31156 Firefox2Importer::ImportBookmarksFile(path_.ToWStringHack(), std::set<GURL>(),
157 false, L"x", NULL, &parsed_bookmarks,
158 NULL, NULL);
[email protected]b5048992008-11-07 04:31:35159
160 // Verify we got back what we wrote.
[email protected]6eac1b32009-06-30 21:00:27161 ASSERT_EQ(7U, parsed_bookmarks.size());
[email protected]1a2640f92009-10-19 18:48:12162 // Windows and ChromeOS builds use Sentence case.
[email protected]584cbad2009-10-28 19:14:53163 std::wstring bookmark_folder_name =
164 l10n_util::GetString(IDS_BOOMARK_BAR_FOLDER_NAME);
[email protected]b5048992008-11-07 04:31:35165 AssertBookmarkEntryEquals(parsed_bookmarks[0], false, url1, url1_title, t1,
[email protected]584cbad2009-10-28 19:14:53166 bookmark_folder_name, f1_title, std::wstring());
[email protected]b5048992008-11-07 04:31:35167 AssertBookmarkEntryEquals(parsed_bookmarks[1], false, url2, url2_title, t2,
[email protected]584cbad2009-10-28 19:14:53168 bookmark_folder_name, f1_title, f2_title);
[email protected]b5048992008-11-07 04:31:35169 AssertBookmarkEntryEquals(parsed_bookmarks[2], false, url3, url3_title, t3,
[email protected]584cbad2009-10-28 19:14:53170 bookmark_folder_name, std::wstring(),
[email protected]8acb64582009-03-25 00:57:18171 std::wstring());
[email protected]6eac1b32009-06-30 21:00:27172 AssertBookmarkEntryEquals(parsed_bookmarks[3], false, url4, url4_title, t4,
[email protected]584cbad2009-10-28 19:14:53173 bookmark_folder_name, std::wstring(),
[email protected]6eac1b32009-06-30 21:00:27174 std::wstring());
175 AssertBookmarkEntryEquals(parsed_bookmarks[4], false, url1, url1_title, t1,
[email protected]b5048992008-11-07 04:31:35176 std::wstring(), std::wstring(), std::wstring());
[email protected]6eac1b32009-06-30 21:00:27177 AssertBookmarkEntryEquals(parsed_bookmarks[5], false, url2, url2_title, t2,
[email protected]b5048992008-11-07 04:31:35178 std::wstring(), std::wstring(), std::wstring());
[email protected]6eac1b32009-06-30 21:00:27179 AssertBookmarkEntryEquals(parsed_bookmarks[6], false, url1, url1_title, t1,
[email protected]b5048992008-11-07 04:31:35180 f3_title, f4_title, std::wstring());
181}