| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/string_util.h" |
| 9 #include "base/time.h" | 10 #include "base/time.h" |
| 11 #include "base/time_format.h" |
| 10 #include "chrome/browser/bookmarks/bookmark_html_writer.h" | 12 #include "chrome/browser/bookmarks/bookmark_html_writer.h" |
| 11 #include "chrome/browser/bookmarks/bookmark_model.h" | 13 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 12 #include "chrome/browser/importer/firefox2_importer.h" | 14 #include "chrome/browser/importer/firefox2_importer.h" |
| 13 | 15 |
| 14 class BookmarkHTMLWriterTest : public testing::Test { | 16 class BookmarkHTMLWriterTest : public testing::Test { |
| 15 protected: | 17 protected: |
| 16 virtual void SetUp() { | 18 virtual void SetUp() { |
| 17 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_)); | 19 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_)); |
| 18 path_ = path_.AppendASCII("bookmarks.html"); | 20 path_ = path_.AppendASCII("bookmarks.html"); |
| 19 file_util::Delete(path_, true); | 21 file_util::Delete(path_, true); |
| 20 } | 22 } |
| 21 | 23 |
| 22 virtual void TearDown() { | 24 virtual void TearDown() { |
| 23 if (!path_.empty()) | 25 if (!path_.empty()) |
| 24 file_util::Delete(path_, true); | 26 file_util::Delete(path_, true); |
| 25 } | 27 } |
| 26 | 28 |
| 29 // Converts a BookmarkEntry to a string suitable for assertion testing. |
| 30 std::wstring BookmarkEntryToString( |
| 31 const ProfileWriter::BookmarkEntry& entry) { |
| 32 std::wstring result; |
| 33 result.append(L"on_toolbar="); |
| 34 if (entry.in_toolbar) |
| 35 result.append(L"false"); |
| 36 else |
| 37 result.append(L"true"); |
| 38 |
| 39 result.append(L" url=" + UTF8ToWide(entry.url.spec())); |
| 40 |
| 41 result.append(L" path="); |
| 42 for (size_t i = 0; i < entry.path.size(); ++i) { |
| 43 if (i != 0) |
| 44 result.append(L"/"); |
| 45 result.append(entry.path[i]); |
| 46 } |
| 47 |
| 48 result.append(L" title="); |
| 49 result.append(entry.title); |
| 50 |
| 51 result.append(L" time="); |
| 52 result.append(base::TimeFormatFriendlyDateAndTime(entry.creation_time)); |
| 53 return result; |
| 54 } |
| 55 |
| 56 // Creates a set of bookmark values to a string for assertion testing. |
| 57 std::wstring BookmarkValuesToString(bool on_toolbar, |
| 58 const GURL& url, |
| 59 const std::wstring& title, |
| 60 base::Time creation_time, |
| 61 const std::wstring& f1, |
| 62 const std::wstring& f2, |
| 63 const std::wstring& f3) { |
| 64 ProfileWriter::BookmarkEntry entry; |
| 65 entry.in_toolbar = on_toolbar; |
| 66 entry.url = url; |
| 67 // The first path element should always be 'x', as that is what we passed |
| 68 // to the importer. |
| 69 entry.path.push_back(L"x"); |
| 70 if (!f1.empty()) { |
| 71 entry.path.push_back(f1); |
| 72 if (!f2.empty()) { |
| 73 entry.path.push_back(f2); |
| 74 if (!f3.empty()) |
| 75 entry.path.push_back(f3); |
| 76 } |
| 77 } |
| 78 entry.title = title; |
| 79 entry.creation_time = creation_time; |
| 80 return BookmarkEntryToString(entry); |
| 81 } |
| 82 |
| 27 void AssertBookmarkEntryEquals(const ProfileWriter::BookmarkEntry& entry, | 83 void AssertBookmarkEntryEquals(const ProfileWriter::BookmarkEntry& entry, |
| 28 bool on_toolbar, | 84 bool on_toolbar, |
| 29 const GURL& url, | 85 const GURL& url, |
| 30 const std::wstring& title, | 86 const std::wstring& title, |
| 31 base::Time creation_time, | 87 base::Time creation_time, |
| 32 const std::wstring& f1, | 88 const std::wstring& f1, |
| 33 const std::wstring& f2, | 89 const std::wstring& f2, |
| 34 const std::wstring& f3) { | 90 const std::wstring& f3) { |
| 35 EXPECT_EQ(on_toolbar, entry.in_toolbar); | 91 EXPECT_EQ(BookmarkValuesToString(on_toolbar, url, title, creation_time, |
| 36 EXPECT_TRUE(url == entry.url); | 92 f1, f2, f3), |
| 37 EXPECT_EQ(title, entry.title); | 93 BookmarkEntryToString(entry)); |
| 38 EXPECT_TRUE(creation_time.ToTimeT() == entry.creation_time.ToTimeT()); | |
| 39 size_t path_count = 0; | |
| 40 if (!f3.empty()) | |
| 41 path_count = 3; | |
| 42 else if (!f2.empty()) | |
| 43 path_count = 2; | |
| 44 else if (!f1.empty()) | |
| 45 path_count = 1; | |
| 46 // The first path element should always be 'x', as that is what we passed | |
| 47 // to the importer. | |
| 48 ASSERT_EQ(path_count + 1, entry.path.size()); | |
| 49 EXPECT_EQ(L"x", entry.path[0]); | |
| 50 EXPECT_TRUE(path_count < 1 || entry.path[1] == f1); | |
| 51 EXPECT_TRUE(path_count < 2 || entry.path[2] == f2); | |
| 52 EXPECT_TRUE(path_count < 3 || entry.path[3] == f3); | |
| 53 } | 94 } |
| 54 | 95 |
| 55 FilePath path_; | 96 FilePath path_; |
| 56 }; | 97 }; |
| 57 | 98 |
| 58 // Tests bookmark_html_writer by populating a BookmarkModel, writing it out by | 99 // Tests bookmark_html_writer by populating a BookmarkModel, writing it out by |
| 59 // way of bookmark_html_writer, then using the importer to read it back in. | 100 // way of bookmark_html_writer, then using the importer to read it back in. |
| 60 TEST_F(BookmarkHTMLWriterTest, DISABLED_Test) { | 101 TEST_F(BookmarkHTMLWriterTest, Test) { |
| 61 // Populate the BookmarkModel. This creates the following bookmark structure: | 102 // Populate the BookmarkModel. This creates the following bookmark structure: |
| 62 // Bookmarks bar | 103 // Bookmarks bar |
| 63 // F1 | 104 // F1 |
| 64 // url1 | 105 // url1 |
| 65 // F2 | 106 // F2 |
| 66 // url2 | 107 // url2 |
| 67 // url3 | 108 // url3 |
| 68 // url4 | 109 // url4 |
| 69 // Other | 110 // Other |
| 70 // url1 | 111 // url1 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 AssertBookmarkEntryEquals(parsed_bookmarks[3], false, url4, url4_title, t4, | 170 AssertBookmarkEntryEquals(parsed_bookmarks[3], false, url4, url4_title, t4, |
| 130 kBookmarkBarFolderName, std::wstring(), | 171 kBookmarkBarFolderName, std::wstring(), |
| 131 std::wstring()); | 172 std::wstring()); |
| 132 AssertBookmarkEntryEquals(parsed_bookmarks[4], false, url1, url1_title, t1, | 173 AssertBookmarkEntryEquals(parsed_bookmarks[4], false, url1, url1_title, t1, |
| 133 std::wstring(), std::wstring(), std::wstring()); | 174 std::wstring(), std::wstring(), std::wstring()); |
| 134 AssertBookmarkEntryEquals(parsed_bookmarks[5], false, url2, url2_title, t2, | 175 AssertBookmarkEntryEquals(parsed_bookmarks[5], false, url2, url2_title, t2, |
| 135 std::wstring(), std::wstring(), std::wstring()); | 176 std::wstring(), std::wstring(), std::wstring()); |
| 136 AssertBookmarkEntryEquals(parsed_bookmarks[6], false, url1, url1_title, t1, | 177 AssertBookmarkEntryEquals(parsed_bookmarks[6], false, url1, url1_title, t1, |
| 137 f3_title, f4_title, std::wstring()); | 178 f3_title, f4_title, std::wstring()); |
| 138 } | 179 } |
| OLD | NEW |