| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/bookmarks/bookmark_html_writer.h" | 5 #include "chrome/browser/bookmarks/bookmark_html_writer.h" |
| 6 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/i18n/time_formatting.h" | 8 #include "base/i18n/time_formatting.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 class BookmarkHTMLWriterTest : public testing::Test { | 50 class BookmarkHTMLWriterTest : public testing::Test { |
| 51 protected: | 51 protected: |
| 52 virtual void SetUp() { | 52 virtual void SetUp() { |
| 53 testing::Test::SetUp(); | 53 testing::Test::SetUp(); |
| 54 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 54 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 55 path_ = temp_dir_.path().AppendASCII("bookmarks.html"); | 55 path_ = temp_dir_.path().AppendASCII("bookmarks.html"); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Converts an ImportedBookmarkEntry to a string suitable for assertion | 58 // Converts an ImportedBookmarkEntry to a string suitable for assertion |
| 59 // testing. | 59 // testing. |
| 60 string16 BookmarkEntryToString(const ImportedBookmarkEntry& entry) { | 60 base::string16 BookmarkEntryToString(const ImportedBookmarkEntry& entry) { |
| 61 string16 result; | 61 base::string16 result; |
| 62 result.append(ASCIIToUTF16("on_toolbar=")); | 62 result.append(ASCIIToUTF16("on_toolbar=")); |
| 63 if (entry.in_toolbar) | 63 if (entry.in_toolbar) |
| 64 result.append(ASCIIToUTF16("true")); | 64 result.append(ASCIIToUTF16("true")); |
| 65 else | 65 else |
| 66 result.append(ASCIIToUTF16("false")); | 66 result.append(ASCIIToUTF16("false")); |
| 67 | 67 |
| 68 result.append(ASCIIToUTF16(" url=") + UTF8ToUTF16(entry.url.spec())); | 68 result.append(ASCIIToUTF16(" url=") + UTF8ToUTF16(entry.url.spec())); |
| 69 | 69 |
| 70 result.append(ASCIIToUTF16(" path=")); | 70 result.append(ASCIIToUTF16(" path=")); |
| 71 for (size_t i = 0; i < entry.path.size(); ++i) { | 71 for (size_t i = 0; i < entry.path.size(); ++i) { |
| 72 if (i != 0) | 72 if (i != 0) |
| 73 result.append(ASCIIToUTF16("/")); | 73 result.append(ASCIIToUTF16("/")); |
| 74 result.append(entry.path[i]); | 74 result.append(entry.path[i]); |
| 75 } | 75 } |
| 76 | 76 |
| 77 result.append(ASCIIToUTF16(" title=")); | 77 result.append(ASCIIToUTF16(" title=")); |
| 78 result.append(entry.title); | 78 result.append(entry.title); |
| 79 | 79 |
| 80 result.append(ASCIIToUTF16(" time=")); | 80 result.append(ASCIIToUTF16(" time=")); |
| 81 result.append(base::TimeFormatFriendlyDateAndTime(entry.creation_time)); | 81 result.append(base::TimeFormatFriendlyDateAndTime(entry.creation_time)); |
| 82 return result; | 82 return result; |
| 83 } | 83 } |
| 84 | 84 |
| 85 // Creates a set of bookmark values to a string for assertion testing. | 85 // Creates a set of bookmark values to a string for assertion testing. |
| 86 string16 BookmarkValuesToString(bool on_toolbar, | 86 base::string16 BookmarkValuesToString(bool on_toolbar, |
| 87 const GURL& url, | 87 const GURL& url, |
| 88 const string16& title, | 88 const base::string16& title, |
| 89 base::Time creation_time, | 89 base::Time creation_time, |
| 90 const string16& f1, | 90 const base::string16& f1, |
| 91 const string16& f2, | 91 const base::string16& f2, |
| 92 const string16& f3) { | 92 const base::string16& f3) { |
| 93 ImportedBookmarkEntry entry; | 93 ImportedBookmarkEntry entry; |
| 94 entry.in_toolbar = on_toolbar; | 94 entry.in_toolbar = on_toolbar; |
| 95 entry.url = url; | 95 entry.url = url; |
| 96 if (!f1.empty()) { | 96 if (!f1.empty()) { |
| 97 entry.path.push_back(f1); | 97 entry.path.push_back(f1); |
| 98 if (!f2.empty()) { | 98 if (!f2.empty()) { |
| 99 entry.path.push_back(f2); | 99 entry.path.push_back(f2); |
| 100 if (!f3.empty()) | 100 if (!f3.empty()) |
| 101 entry.path.push_back(f3); | 101 entry.path.push_back(f3); |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 entry.title = title; | 104 entry.title = title; |
| 105 entry.creation_time = creation_time; | 105 entry.creation_time = creation_time; |
| 106 return BookmarkEntryToString(entry); | 106 return BookmarkEntryToString(entry); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void AssertBookmarkEntryEquals(const ImportedBookmarkEntry& entry, | 109 void AssertBookmarkEntryEquals(const ImportedBookmarkEntry& entry, |
| 110 bool on_toolbar, | 110 bool on_toolbar, |
| 111 const GURL& url, | 111 const GURL& url, |
| 112 const string16& title, | 112 const base::string16& title, |
| 113 base::Time creation_time, | 113 base::Time creation_time, |
| 114 const string16& f1, | 114 const base::string16& f1, |
| 115 const string16& f2, | 115 const base::string16& f2, |
| 116 const string16& f3) { | 116 const base::string16& f3) { |
| 117 EXPECT_EQ(BookmarkValuesToString(on_toolbar, url, title, creation_time, | 117 EXPECT_EQ(BookmarkValuesToString(on_toolbar, url, title, creation_time, |
| 118 f1, f2, f3), | 118 f1, f2, f3), |
| 119 BookmarkEntryToString(entry)); | 119 BookmarkEntryToString(entry)); |
| 120 } | 120 } |
| 121 | 121 |
| 122 base::ScopedTempDir temp_dir_; | 122 base::ScopedTempDir temp_dir_; |
| 123 base::FilePath path_; | 123 base::FilePath path_; |
| 124 }; | 124 }; |
| 125 | 125 |
| 126 // Class that will notify message loop when file is written. | 126 // Class that will notify message loop when file is written. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 // url4 | 170 // url4 |
| 171 // Other | 171 // Other |
| 172 // url1 | 172 // url1 |
| 173 // url2 | 173 // url2 |
| 174 // F3 | 174 // F3 |
| 175 // F4 | 175 // F4 |
| 176 // url1 | 176 // url1 |
| 177 // Mobile | 177 // Mobile |
| 178 // url1 | 178 // url1 |
| 179 // <bookmark without a title.> | 179 // <bookmark without a title.> |
| 180 string16 f1_title = ASCIIToUTF16("F\"&;<1\""); | 180 base::string16 f1_title = ASCIIToUTF16("F\"&;<1\""); |
| 181 string16 f2_title = ASCIIToUTF16("F2"); | 181 base::string16 f2_title = ASCIIToUTF16("F2"); |
| 182 string16 f3_title = ASCIIToUTF16("F 3"); | 182 base::string16 f3_title = ASCIIToUTF16("F 3"); |
| 183 string16 f4_title = ASCIIToUTF16("F4"); | 183 base::string16 f4_title = ASCIIToUTF16("F4"); |
| 184 string16 url1_title = ASCIIToUTF16("url 1"); | 184 base::string16 url1_title = ASCIIToUTF16("url 1"); |
| 185 string16 url2_title = ASCIIToUTF16("url&2"); | 185 base::string16 url2_title = ASCIIToUTF16("url&2"); |
| 186 string16 url3_title = ASCIIToUTF16("url\"3"); | 186 base::string16 url3_title = ASCIIToUTF16("url\"3"); |
| 187 string16 url4_title = ASCIIToUTF16("url\"&;"); | 187 base::string16 url4_title = ASCIIToUTF16("url\"&;"); |
| 188 string16 unnamed_bookmark_title = ASCIIToUTF16(""); | 188 base::string16 unnamed_bookmark_title = ASCIIToUTF16(""); |
| 189 GURL url1("http://url1"); | 189 GURL url1("http://url1"); |
| 190 GURL url1_favicon("http://url1/icon.ico"); | 190 GURL url1_favicon("http://url1/icon.ico"); |
| 191 GURL url2("http://url2"); | 191 GURL url2("http://url2"); |
| 192 GURL url3("http://url3"); | 192 GURL url3("http://url3"); |
| 193 GURL url4("javascript:alert(\"Hello!\");"); | 193 GURL url4("javascript:alert(\"Hello!\");"); |
| 194 GURL unnamed_bookmark_url("about:blank"); | 194 GURL unnamed_bookmark_url("about:blank"); |
| 195 base::Time t1(base::Time::Now()); | 195 base::Time t1(base::Time::Now()); |
| 196 base::Time t2(t1 + base::TimeDelta::FromHours(1)); | 196 base::Time t2(t1 + base::TimeDelta::FromHours(1)); |
| 197 base::Time t3(t1 + base::TimeDelta::FromHours(1)); | 197 base::Time t3(t1 + base::TimeDelta::FromHours(1)); |
| 198 base::Time t4(t1 + base::TimeDelta::FromHours(1)); | 198 base::Time t4(t1 + base::TimeDelta::FromHours(1)); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 std::set<GURL>::const_iterator iter = favicons[i].urls.find(url1); | 250 std::set<GURL>::const_iterator iter = favicons[i].urls.find(url1); |
| 251 ASSERT_TRUE(iter != favicons[i].urls.end()); | 251 ASSERT_TRUE(iter != favicons[i].urls.end()); |
| 252 ASSERT_TRUE(*iter == url1); | 252 ASSERT_TRUE(*iter == url1); |
| 253 ASSERT_TRUE(favicons[i].png_data == icon_data); | 253 ASSERT_TRUE(favicons[i].png_data == icon_data); |
| 254 } | 254 } |
| 255 } | 255 } |
| 256 | 256 |
| 257 // Verify we got back what we wrote. | 257 // Verify we got back what we wrote. |
| 258 ASSERT_EQ(9U, parsed_bookmarks.size()); | 258 ASSERT_EQ(9U, parsed_bookmarks.size()); |
| 259 // Windows and ChromeOS builds use Sentence case. | 259 // Windows and ChromeOS builds use Sentence case. |
| 260 string16 bookmark_folder_name = | 260 base::string16 bookmark_folder_name = |
| 261 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_FOLDER_NAME); | 261 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_FOLDER_NAME); |
| 262 AssertBookmarkEntryEquals(parsed_bookmarks[0], true, url1, url1_title, t1, | 262 AssertBookmarkEntryEquals(parsed_bookmarks[0], true, url1, url1_title, t1, |
| 263 bookmark_folder_name, f1_title, string16()); | 263 bookmark_folder_name, f1_title, base::string16()); |
| 264 AssertBookmarkEntryEquals(parsed_bookmarks[1], true, url2, url2_title, t2, | 264 AssertBookmarkEntryEquals(parsed_bookmarks[1], true, url2, url2_title, t2, |
| 265 bookmark_folder_name, f1_title, f2_title); | 265 bookmark_folder_name, f1_title, f2_title); |
| 266 AssertBookmarkEntryEquals(parsed_bookmarks[2], true, url3, url3_title, t3, | 266 AssertBookmarkEntryEquals(parsed_bookmarks[2], true, url3, url3_title, t3, |
| 267 bookmark_folder_name, string16(), string16()); | 267 bookmark_folder_name, base::string16(), |
| 268 base::string16()); |
| 268 AssertBookmarkEntryEquals(parsed_bookmarks[3], true, url4, url4_title, t4, | 269 AssertBookmarkEntryEquals(parsed_bookmarks[3], true, url4, url4_title, t4, |
| 269 bookmark_folder_name, string16(), string16()); | 270 bookmark_folder_name, base::string16(), |
| 271 base::string16()); |
| 270 AssertBookmarkEntryEquals(parsed_bookmarks[4], false, url1, url1_title, t1, | 272 AssertBookmarkEntryEquals(parsed_bookmarks[4], false, url1, url1_title, t1, |
| 271 string16(), string16(), string16()); | 273 base::string16(), base::string16(), |
| 274 base::string16()); |
| 272 AssertBookmarkEntryEquals(parsed_bookmarks[5], false, url2, url2_title, t2, | 275 AssertBookmarkEntryEquals(parsed_bookmarks[5], false, url2, url2_title, t2, |
| 273 string16(), string16(), string16()); | 276 base::string16(), base::string16(), |
| 277 base::string16()); |
| 274 AssertBookmarkEntryEquals(parsed_bookmarks[6], false, url1, url1_title, t1, | 278 AssertBookmarkEntryEquals(parsed_bookmarks[6], false, url1, url1_title, t1, |
| 275 f3_title, f4_title, string16()); | 279 f3_title, f4_title, base::string16()); |
| 276 AssertBookmarkEntryEquals(parsed_bookmarks[7], false, url1, url1_title, t1, | 280 AssertBookmarkEntryEquals(parsed_bookmarks[7], false, url1, url1_title, t1, |
| 277 string16(), string16(), string16()); | 281 base::string16(), base::string16(), |
| 282 base::string16()); |
| 278 AssertBookmarkEntryEquals(parsed_bookmarks[8], false, unnamed_bookmark_url, | 283 AssertBookmarkEntryEquals(parsed_bookmarks[8], false, unnamed_bookmark_url, |
| 279 unnamed_bookmark_title, t2, | 284 unnamed_bookmark_title, t2, |
| 280 string16(), string16(), string16()); | 285 base::string16(), base::string16(), |
| 286 base::string16()); |
| 281 } | 287 } |
| OLD | NEW |