[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 1 | // 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] | 584cbad | 2009-10-28 19:14:53 | [diff] [blame^] | 7 | #include "app/l10n_util.h" |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 8 | #include "base/file_util.h" |
| 9 | #include "base/path_service.h" |
[email protected] | 35793b4 | 2009-09-25 20:00:08 | [diff] [blame] | 10 | #include "base/string_util.h" |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 11 | #include "base/time.h" |
[email protected] | fb895c6 | 2009-10-09 18:20:30 | [diff] [blame] | 12 | #include "base/i18n/time_formatting.h" |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 13 | #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] | 584cbad | 2009-10-28 19:14:53 | [diff] [blame^] | 16 | #include "grit/generated_resources.h" |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 17 | |
| 18 | class BookmarkHTMLWriterTest : public testing::Test { |
| 19 | protected: |
| 20 | virtual void SetUp() { |
| 21 | ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_)); |
[email protected] | b647e0b | 2009-05-04 17:50:31 | [diff] [blame] | 22 | path_ = path_.AppendASCII("bookmarks.html"); |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 23 | file_util::Delete(path_, true); |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | virtual void TearDown() { |
| 27 | if (!path_.empty()) |
| 28 | file_util::Delete(path_, true); |
| 29 | } |
| 30 | |
[email protected] | 35793b4 | 2009-09-25 20:00:08 | [diff] [blame] | 31 | // 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] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 85 | 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] | 35793b4 | 2009-09-25 20:00:08 | [diff] [blame] | 93 | EXPECT_EQ(BookmarkValuesToString(on_toolbar, url, title, creation_time, |
| 94 | f1, f2, f3), |
| 95 | BookmarkEntryToString(entry)); |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | b647e0b | 2009-05-04 17:50:31 | [diff] [blame] | 98 | FilePath path_; |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 99 | }; |
| 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] | 584cbad | 2009-10-28 19:14:53 | [diff] [blame^] | 103 | TEST_F(BookmarkHTMLWriterTest, Test) { |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 104 | // Populate the BookmarkModel. This creates the following bookmark structure: |
[email protected] | 8acb6458 | 2009-03-25 00:57:18 | [diff] [blame] | 105 | // Bookmarks bar |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 106 | // F1 |
| 107 | // url1 |
| 108 | // F2 |
| 109 | // url2 |
| 110 | // url3 |
[email protected] | 6eac1b3 | 2009-06-30 21:00:27 | [diff] [blame] | 111 | // url4 |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 112 | // 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] | 6eac1b3 | 2009-06-30 21:00:27 | [diff] [blame] | 125 | std::wstring url4_title = L"url\"&;"; |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 126 | GURL url1("http://url1"); |
| 127 | GURL url2("http://url2"); |
| 128 | GURL url3("http://url3"); |
[email protected] | 6eac1b3 | 2009-06-30 21:00:27 | [diff] [blame] | 129 | GURL url4("http://\"&;\""); |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 130 | 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] | 6eac1b3 | 2009-06-30 21:00:27 | [diff] [blame] | 134 | base::Time t4(t1 + base::TimeDelta::FromHours(1)); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 135 | const BookmarkNode* f1 = model.AddGroup( |
| 136 | model.GetBookmarkBarNode(), 0, f1_title); |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 137 | model.AddURLWithCreationTime(f1, 0, url1_title, url1, t1); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 138 | const BookmarkNode* f2 = model.AddGroup(f1, 1, f2_title); |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 139 | 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] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 145 | const BookmarkNode* f3 = model.AddGroup(model.other_node(), 2, f3_title); |
| 146 | const BookmarkNode* f4 = model.AddGroup(f3, 0, f4_title); |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 147 | model.AddURLWithCreationTime(f4, 0, url1_title, url1, t1); |
[email protected] | 6eac1b3 | 2009-06-30 21:00:27 | [diff] [blame] | 148 | model.AddURLWithCreationTime(model.GetBookmarkBarNode(), 2, url4_title, |
| 149 | url4, t4); |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 150 | |
| 151 | // Write to a temp file. |
[email protected] | 304ec81a | 2009-09-25 15:15:04 | [diff] [blame] | 152 | bookmark_html_writer::WriteBookmarks(NULL, &model, path_); |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 153 | |
| 154 | // Read the bookmarks back in. |
| 155 | std::vector<ProfileWriter::BookmarkEntry> parsed_bookmarks; |
[email protected] | b647e0b | 2009-05-04 17:50:31 | [diff] [blame] | 156 | Firefox2Importer::ImportBookmarksFile(path_.ToWStringHack(), std::set<GURL>(), |
| 157 | false, L"x", NULL, &parsed_bookmarks, |
| 158 | NULL, NULL); |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 159 | |
| 160 | // Verify we got back what we wrote. |
[email protected] | 6eac1b3 | 2009-06-30 21:00:27 | [diff] [blame] | 161 | ASSERT_EQ(7U, parsed_bookmarks.size()); |
[email protected] | 1a2640f9 | 2009-10-19 18:48:12 | [diff] [blame] | 162 | // Windows and ChromeOS builds use Sentence case. |
[email protected] | 584cbad | 2009-10-28 19:14:53 | [diff] [blame^] | 163 | std::wstring bookmark_folder_name = |
| 164 | l10n_util::GetString(IDS_BOOMARK_BAR_FOLDER_NAME); |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 165 | AssertBookmarkEntryEquals(parsed_bookmarks[0], false, url1, url1_title, t1, |
[email protected] | 584cbad | 2009-10-28 19:14:53 | [diff] [blame^] | 166 | bookmark_folder_name, f1_title, std::wstring()); |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 167 | AssertBookmarkEntryEquals(parsed_bookmarks[1], false, url2, url2_title, t2, |
[email protected] | 584cbad | 2009-10-28 19:14:53 | [diff] [blame^] | 168 | bookmark_folder_name, f1_title, f2_title); |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 169 | AssertBookmarkEntryEquals(parsed_bookmarks[2], false, url3, url3_title, t3, |
[email protected] | 584cbad | 2009-10-28 19:14:53 | [diff] [blame^] | 170 | bookmark_folder_name, std::wstring(), |
[email protected] | 8acb6458 | 2009-03-25 00:57:18 | [diff] [blame] | 171 | std::wstring()); |
[email protected] | 6eac1b3 | 2009-06-30 21:00:27 | [diff] [blame] | 172 | AssertBookmarkEntryEquals(parsed_bookmarks[3], false, url4, url4_title, t4, |
[email protected] | 584cbad | 2009-10-28 19:14:53 | [diff] [blame^] | 173 | bookmark_folder_name, std::wstring(), |
[email protected] | 6eac1b3 | 2009-06-30 21:00:27 | [diff] [blame] | 174 | std::wstring()); |
| 175 | AssertBookmarkEntryEquals(parsed_bookmarks[4], false, url1, url1_title, t1, |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 176 | std::wstring(), std::wstring(), std::wstring()); |
[email protected] | 6eac1b3 | 2009-06-30 21:00:27 | [diff] [blame] | 177 | AssertBookmarkEntryEquals(parsed_bookmarks[5], false, url2, url2_title, t2, |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 178 | std::wstring(), std::wstring(), std::wstring()); |
[email protected] | 6eac1b3 | 2009-06-30 21:00:27 | [diff] [blame] | 179 | AssertBookmarkEntryEquals(parsed_bookmarks[6], false, url1, url1_title, t1, |
[email protected] | b504899 | 2008-11-07 04:31:35 | [diff] [blame] | 180 | f3_title, f4_title, std::wstring()); |
| 181 | } |