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