Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/utility/importer/bookmark_html_reader.h" | 5 #include "chrome/utility/importer/bookmark_html_reader.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 | 127 |
| 128 // Epiphany format. | 128 // Epiphany format. |
| 129 result = internal::ParseMinimumBookmarkFromLine( | 129 result = internal::ParseMinimumBookmarkFromLine( |
| 130 "<dt><a href=\"http://www.google.com/\">Google</a></dt>", | 130 "<dt><a href=\"http://www.google.com/\">Google</a></dt>", |
| 131 charset, &title, &url); | 131 charset, &title, &url); |
| 132 EXPECT_TRUE(result); | 132 EXPECT_TRUE(result); |
| 133 EXPECT_EQ(ASCIIToUTF16("Google"), title); | 133 EXPECT_EQ(ASCIIToUTF16("Google"), title); |
| 134 EXPECT_EQ("http://www.google.com/", url.spec()); | 134 EXPECT_EQ("http://www.google.com/", url.spec()); |
| 135 } | 135 } |
| 136 | 136 |
| 137 namespace { | 137 class BookmarkHTMLReaderTestWithData : public testing::Test { |
|
gab
2013/08/12 14:17:41
Put the fixture in the unnamed namespace.
zhchbin
2013/08/12 14:42:09
Done.
| |
| 138 public: | |
| 139 BookmarkHTMLReaderTestWithData() {} | |
|
gab
2013/08/12 14:17:41
Don't need this empty constructor.
zhchbin
2013/08/12 14:42:09
Done.
| |
| 138 | 140 |
| 139 void ExpectFirstFirefox2Bookmark(const ImportedBookmarkEntry& entry) { | 141 virtual void SetUp() OVERRIDE; |
| 142 | |
| 143 protected: | |
| 144 class CancelAfterFifteenCalls { | |
| 145 int count; | |
| 146 public: | |
| 147 CancelAfterFifteenCalls() : count(0) { } | |
| 148 bool ShouldCancel() { | |
| 149 return ++count > 16; | |
| 150 } | |
| 151 }; | |
| 152 | |
| 153 void ExpectFirstFirefox2Bookmark(const ImportedBookmarkEntry& entry); | |
| 154 void ExpectSecondFirefox2Bookmark(const ImportedBookmarkEntry& entry); | |
| 155 void ExpectThirdFirefox2Bookmark(const ImportedBookmarkEntry& entry); | |
| 156 void ExpectFirstEpiphanyBookmark(const ImportedBookmarkEntry& entry); | |
| 157 void ExpectSecondEpiphanyBookmark(const ImportedBookmarkEntry& entry); | |
| 158 void ExpectFirstFirefox23Bookmark(const ImportedBookmarkEntry& entry); | |
| 159 void ExpectSecondFirefox23Bookmark(const ImportedBookmarkEntry& entry); | |
| 160 void ExpectThirdFirefox23Bookmark(const ImportedBookmarkEntry& entry); | |
| 161 | |
| 162 static bool IsURLValid(const GURL& url); | |
| 163 | |
| 164 base::FilePath test_data_path_; | |
| 165 }; | |
| 166 | |
| 167 void BookmarkHTMLReaderTestWithData::SetUp() { | |
| 168 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_path_)); | |
| 169 test_data_path_ = test_data_path_.AppendASCII("bookmark_html_reader"); | |
| 170 } | |
| 171 | |
| 172 void BookmarkHTMLReaderTestWithData::ExpectFirstFirefox2Bookmark( | |
| 173 const ImportedBookmarkEntry& entry) { | |
| 140 EXPECT_EQ(ASCIIToUTF16("Empty"), entry.title); | 174 EXPECT_EQ(ASCIIToUTF16("Empty"), entry.title); |
| 141 EXPECT_TRUE(entry.is_folder); | 175 EXPECT_TRUE(entry.is_folder); |
| 142 EXPECT_EQ(base::Time::FromTimeT(1295938143), entry.creation_time); | 176 EXPECT_EQ(base::Time::FromTimeT(1295938143), entry.creation_time); |
| 143 EXPECT_EQ(1U, entry.path.size()); | 177 EXPECT_EQ(1U, entry.path.size()); |
| 144 if (entry.path.size() == 1) | 178 if (entry.path.size() == 1) |
| 145 EXPECT_EQ(ASCIIToUTF16("Empty's Parent"), entry.path.front()); | 179 EXPECT_EQ(ASCIIToUTF16("Empty's Parent"), entry.path.front()); |
| 146 } | 180 } |
| 147 | 181 |
| 148 void ExpectSecondFirefox2Bookmark(const ImportedBookmarkEntry& entry) { | 182 void BookmarkHTMLReaderTestWithData::ExpectSecondFirefox2Bookmark( |
| 183 const ImportedBookmarkEntry& entry) { | |
| 149 EXPECT_EQ(ASCIIToUTF16("[Tamura Yukari.com]"), entry.title); | 184 EXPECT_EQ(ASCIIToUTF16("[Tamura Yukari.com]"), entry.title); |
| 150 EXPECT_FALSE(entry.is_folder); | 185 EXPECT_FALSE(entry.is_folder); |
| 151 EXPECT_EQ(base::Time::FromTimeT(1234567890), entry.creation_time); | 186 EXPECT_EQ(base::Time::FromTimeT(1234567890), entry.creation_time); |
| 152 EXPECT_EQ(1U, entry.path.size()); | 187 EXPECT_EQ(1U, entry.path.size()); |
| 153 if (entry.path.size() == 1) | 188 if (entry.path.size() == 1) |
| 154 EXPECT_EQ(ASCIIToUTF16("Not Empty"), entry.path.front()); | 189 EXPECT_EQ(ASCIIToUTF16("Not Empty"), entry.path.front()); |
| 155 EXPECT_EQ("http://www.tamurayukari.com/", entry.url.spec()); | 190 EXPECT_EQ("http://www.tamurayukari.com/", entry.url.spec()); |
| 156 } | 191 } |
| 157 | 192 |
| 158 void ExpectThirdFirefox2Bookmark(const ImportedBookmarkEntry& entry) { | 193 void BookmarkHTMLReaderTestWithData::ExpectThirdFirefox2Bookmark( |
| 194 const ImportedBookmarkEntry& entry) { | |
| 159 EXPECT_EQ(ASCIIToUTF16("Google"), entry.title); | 195 EXPECT_EQ(ASCIIToUTF16("Google"), entry.title); |
| 160 EXPECT_FALSE(entry.is_folder); | 196 EXPECT_FALSE(entry.is_folder); |
| 161 EXPECT_EQ(base::Time::FromTimeT(0000000000), entry.creation_time); | 197 EXPECT_EQ(base::Time::FromTimeT(0000000000), entry.creation_time); |
| 162 EXPECT_EQ(1U, entry.path.size()); | 198 EXPECT_EQ(1U, entry.path.size()); |
| 163 if (entry.path.size() == 1) | 199 if (entry.path.size() == 1) |
| 164 EXPECT_EQ(ASCIIToUTF16("Not Empty But Default"), entry.path.front()); | 200 EXPECT_EQ(ASCIIToUTF16("Not Empty But Default"), entry.path.front()); |
| 165 EXPECT_EQ("http://www.google.com/", entry.url.spec()); | 201 EXPECT_EQ("http://www.google.com/", entry.url.spec()); |
| 166 } | 202 } |
| 167 | 203 |
| 168 void ExpectFirstEpiphanyBookmark(const ImportedBookmarkEntry& entry) { | 204 void BookmarkHTMLReaderTestWithData::ExpectFirstEpiphanyBookmark( |
| 205 const ImportedBookmarkEntry& entry) { | |
| 169 EXPECT_EQ(ASCIIToUTF16("[Tamura Yukari.com]"), entry.title); | 206 EXPECT_EQ(ASCIIToUTF16("[Tamura Yukari.com]"), entry.title); |
| 170 EXPECT_EQ("http://www.tamurayukari.com/", entry.url.spec()); | 207 EXPECT_EQ("http://www.tamurayukari.com/", entry.url.spec()); |
| 171 EXPECT_EQ(0U, entry.path.size()); | 208 EXPECT_EQ(0U, entry.path.size()); |
| 172 } | 209 } |
| 173 | 210 |
| 174 void ExpectSecondEpiphanyBookmark(const ImportedBookmarkEntry& entry) { | 211 void BookmarkHTMLReaderTestWithData::ExpectSecondEpiphanyBookmark( |
| 212 const ImportedBookmarkEntry& entry) { | |
| 175 EXPECT_EQ(ASCIIToUTF16("Google"), entry.title); | 213 EXPECT_EQ(ASCIIToUTF16("Google"), entry.title); |
| 176 EXPECT_EQ("http://www.google.com/", entry.url.spec()); | 214 EXPECT_EQ("http://www.google.com/", entry.url.spec()); |
| 177 EXPECT_EQ(0U, entry.path.size()); | 215 EXPECT_EQ(0U, entry.path.size()); |
| 178 } | 216 } |
| 179 | 217 |
| 180 } // namespace | 218 void BookmarkHTMLReaderTestWithData::ExpectFirstFirefox23Bookmark( |
| 219 const ImportedBookmarkEntry& entry) { | |
| 220 EXPECT_EQ(ASCIIToUTF16("Google"), entry.title); | |
| 221 EXPECT_FALSE(entry.is_folder); | |
| 222 EXPECT_EQ(base::Time::FromTimeT(1376102167), entry.creation_time); | |
| 223 EXPECT_EQ(0U, entry.path.size()); | |
| 224 EXPECT_EQ("https://www.google.com/", entry.url.spec()); | |
| 225 } | |
| 181 | 226 |
| 182 TEST(BookmarkHTMLReaderTest, Firefox2BookmarkFileImport) { | 227 void BookmarkHTMLReaderTestWithData::ExpectSecondFirefox23Bookmark( |
| 183 base::FilePath path; | 228 const ImportedBookmarkEntry& entry) { |
| 184 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | 229 EXPECT_EQ(ASCIIToUTF16("Issues"), entry.title); |
| 185 path = path.AppendASCII("bookmark_html_reader"); | 230 EXPECT_FALSE(entry.is_folder); |
| 186 path = path.AppendASCII("firefox2.html"); | 231 EXPECT_EQ(base::Time::FromTimeT(1376102304), entry.creation_time); |
| 232 EXPECT_EQ(1U, entry.path.size()); | |
| 233 EXPECT_EQ(ASCIIToUTF16("Chromium"), entry.path.front()); | |
| 234 EXPECT_EQ("https://code.google.com/p/chromium/issues/list", entry.url.spec()); | |
| 235 } | |
| 236 | |
| 237 void BookmarkHTMLReaderTestWithData::ExpectThirdFirefox23Bookmark( | |
| 238 const ImportedBookmarkEntry& entry) { | |
| 239 EXPECT_EQ(ASCIIToUTF16("CodeSearch"), entry.title); | |
| 240 EXPECT_FALSE(entry.is_folder); | |
| 241 EXPECT_EQ(base::Time::FromTimeT(1376102224), entry.creation_time); | |
| 242 EXPECT_EQ(1U, entry.path.size()); | |
| 243 EXPECT_EQ(ASCIIToUTF16("Chromium"), entry.path.front()); | |
| 244 EXPECT_EQ("http://code.google.com/p/chromium/codesearch", entry.url.spec()); | |
| 245 } | |
| 246 | |
| 247 // static | |
| 248 bool BookmarkHTMLReaderTestWithData::IsURLValid(const GURL& url) { | |
| 249 // No offense to whomever owns this domain... | |
| 250 return !url.DomainIs("tamurayukari.com"); | |
| 251 } | |
| 252 | |
| 253 TEST_F(BookmarkHTMLReaderTestWithData, Firefox2BookmarkFileImport) { | |
| 254 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); | |
| 187 | 255 |
| 188 std::vector<ImportedBookmarkEntry> bookmarks; | 256 std::vector<ImportedBookmarkEntry> bookmarks; |
| 189 ImportBookmarksFile(base::Callback<bool(void)>(), | 257 ImportBookmarksFile(base::Callback<bool(void)>(), |
| 190 base::Callback<bool(const GURL&)>(), | 258 base::Callback<bool(const GURL&)>(), |
| 191 path, &bookmarks, NULL); | 259 path, &bookmarks, NULL); |
| 192 | 260 |
| 193 ASSERT_EQ(3U, bookmarks.size()); | 261 ASSERT_EQ(3U, bookmarks.size()); |
| 194 ExpectFirstFirefox2Bookmark(bookmarks[0]); | 262 ExpectFirstFirefox2Bookmark(bookmarks[0]); |
| 195 ExpectSecondFirefox2Bookmark(bookmarks[1]); | 263 ExpectSecondFirefox2Bookmark(bookmarks[1]); |
| 196 ExpectThirdFirefox2Bookmark(bookmarks[2]); | 264 ExpectThirdFirefox2Bookmark(bookmarks[2]); |
| 197 } | 265 } |
| 198 | 266 |
| 199 TEST(BookmarkHTMLReaderTest, EpiphanyBookmarkFileImport) { | 267 TEST_F(BookmarkHTMLReaderTestWithData, BookmarkFileWithHrTagImport) { |
| 200 base::FilePath path; | 268 base::FilePath path = test_data_path_.AppendASCII("firefox23.html"); |
| 201 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | 269 |
| 202 path = path.AppendASCII("bookmark_html_reader"); | 270 std::vector<ImportedBookmarkEntry> bookmarks; |
| 203 path = path.AppendASCII("epiphany.html"); | 271 ImportBookmarksFile(base::Callback<bool(void)>(), |
| 272 base::Callback<bool(const GURL&)>(), | |
| 273 path, &bookmarks, NULL); | |
| 274 | |
| 275 ASSERT_EQ(3U, bookmarks.size()); | |
| 276 ExpectFirstFirefox23Bookmark(bookmarks[0]); | |
| 277 ExpectSecondFirefox23Bookmark(bookmarks[1]); | |
| 278 ExpectThirdFirefox23Bookmark(bookmarks[2]); | |
| 279 } | |
| 280 | |
| 281 TEST_F(BookmarkHTMLReaderTestWithData, EpiphanyBookmarkFileImport) { | |
| 282 base::FilePath path = test_data_path_.AppendASCII("epiphany.html"); | |
| 204 | 283 |
| 205 std::vector<ImportedBookmarkEntry> bookmarks; | 284 std::vector<ImportedBookmarkEntry> bookmarks; |
| 206 ImportBookmarksFile(base::Callback<bool(void)>(), | 285 ImportBookmarksFile(base::Callback<bool(void)>(), |
| 207 base::Callback<bool(const GURL&)>(), | 286 base::Callback<bool(const GURL&)>(), |
| 208 path, &bookmarks, NULL); | 287 path, &bookmarks, NULL); |
| 209 | 288 |
| 210 ASSERT_EQ(2U, bookmarks.size()); | 289 ASSERT_EQ(2U, bookmarks.size()); |
| 211 ExpectFirstEpiphanyBookmark(bookmarks[0]); | 290 ExpectFirstEpiphanyBookmark(bookmarks[0]); |
| 212 ExpectSecondEpiphanyBookmark(bookmarks[1]); | 291 ExpectSecondEpiphanyBookmark(bookmarks[1]); |
| 213 } | 292 } |
| 214 | 293 |
| 215 namespace { | 294 TEST_F(BookmarkHTMLReaderTestWithData, CancellationCallback) { |
| 216 | |
| 217 class CancelAfterFifteenCalls { | |
|
gab
2013/08/12 14:17:41
I like to have this beside the test that uses it..
zhchbin
2013/08/12 14:42:09
Done.
| |
| 218 int count; | |
| 219 public: | |
| 220 CancelAfterFifteenCalls() : count(0) { } | |
| 221 bool ShouldCancel() { | |
| 222 return ++count > 16; | |
| 223 } | |
| 224 }; | |
| 225 | |
| 226 } // namespace | |
| 227 | |
| 228 TEST(BookmarkHTMLReaderTest, CancellationCallback) { | |
| 229 base::FilePath path; | |
| 230 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | |
| 231 path = path.AppendASCII("bookmark_html_reader"); | |
| 232 // Use a file for testing that has multiple bookmarks. | 295 // Use a file for testing that has multiple bookmarks. |
| 233 path = path.AppendASCII("firefox2.html"); | 296 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); |
| 234 | 297 |
| 235 std::vector<ImportedBookmarkEntry> bookmarks; | 298 std::vector<ImportedBookmarkEntry> bookmarks; |
| 236 CancelAfterFifteenCalls cancel_fifteen; | 299 CancelAfterFifteenCalls cancel_fifteen; |
| 237 ImportBookmarksFile(base::Bind(&CancelAfterFifteenCalls::ShouldCancel, | 300 ImportBookmarksFile(base::Bind(&CancelAfterFifteenCalls::ShouldCancel, |
| 238 base::Unretained(&cancel_fifteen)), | 301 base::Unretained(&cancel_fifteen)), |
| 239 base::Callback<bool(const GURL&)>(), | 302 base::Callback<bool(const GURL&)>(), |
| 240 path, &bookmarks, NULL); | 303 path, &bookmarks, NULL); |
| 241 | 304 |
| 242 // The cancellation callback is checked before each line is read, so fifteen | 305 // The cancellation callback is checked before each line is read, so fifteen |
| 243 // lines are imported. The first fifteen lines of firefox2.html include only | 306 // lines are imported. The first fifteen lines of firefox2.html include only |
| 244 // one bookmark. | 307 // one bookmark. |
| 245 ASSERT_EQ(1U, bookmarks.size()); | 308 ASSERT_EQ(1U, bookmarks.size()); |
| 246 ExpectFirstFirefox2Bookmark(bookmarks[0]); | 309 ExpectFirstFirefox2Bookmark(bookmarks[0]); |
| 247 } | 310 } |
| 248 | 311 |
| 249 namespace { | 312 TEST_F(BookmarkHTMLReaderTestWithData, ValidURLCallback) { |
| 250 | |
| 251 bool IsURLValid(const GURL& url) { | |
|
gab
2013/08/12 14:17:41
I like to have this beside the test that uses it..
zhchbin
2013/08/12 14:42:09
Done.
| |
| 252 // No offense to whomever owns this domain... | |
| 253 return !url.DomainIs("tamurayukari.com"); | |
| 254 } | |
| 255 | |
| 256 } // namespace | |
| 257 | |
| 258 TEST(BookmarkHTMLReaderTest, ValidURLCallback) { | |
| 259 base::FilePath path; | |
| 260 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | |
| 261 path = path.AppendASCII("bookmark_html_reader"); | |
| 262 // Use a file for testing that has multiple bookmarks. | 313 // Use a file for testing that has multiple bookmarks. |
| 263 path = path.AppendASCII("firefox2.html"); | 314 base::FilePath path = test_data_path_.AppendASCII("firefox2.html"); |
| 264 | 315 |
| 265 std::vector<ImportedBookmarkEntry> bookmarks; | 316 std::vector<ImportedBookmarkEntry> bookmarks; |
| 266 ImportBookmarksFile(base::Callback<bool(void)>(), | 317 ImportBookmarksFile(base::Callback<bool(void)>(), |
| 267 base::Bind(&IsURLValid), | 318 base::Bind(&IsURLValid), |
| 268 path, &bookmarks, NULL); | 319 path, &bookmarks, NULL); |
| 269 | 320 |
| 270 ASSERT_EQ(2U, bookmarks.size()); | 321 ASSERT_EQ(2U, bookmarks.size()); |
| 271 ExpectFirstFirefox2Bookmark(bookmarks[0]); | 322 ExpectFirstFirefox2Bookmark(bookmarks[0]); |
| 272 ExpectThirdFirefox2Bookmark(bookmarks[1]); | 323 ExpectThirdFirefox2Bookmark(bookmarks[1]); |
| 273 } | 324 } |
| 274 | 325 |
| 275 } // namespace bookmark_html_reader | 326 } // namespace bookmark_html_reader |
| OLD | NEW |