OLD | NEW |
(Empty) | |
| 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 |
| 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" |
| 9 #include "base/time.h" |
| 10 #include "chrome/browser/bookmarks/bookmark_html_writer.h" |
| 11 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 12 #include "chrome/browser/importer/firefox2_importer.h" |
| 13 |
| 14 class BookmarkHTMLWriterTest : public testing::Test { |
| 15 protected: |
| 16 virtual void SetUp() { |
| 17 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_)); |
| 18 file_util::AppendToPath(&path_, L"bookmarks.html"); |
| 19 file_util::Delete(path_, true); |
| 20 |
| 21 } |
| 22 |
| 23 virtual void TearDown() { |
| 24 if (!path_.empty()) |
| 25 file_util::Delete(path_, true); |
| 26 } |
| 27 |
| 28 void AssertBookmarkEntryEquals(const ProfileWriter::BookmarkEntry& entry, |
| 29 bool on_toolbar, |
| 30 const GURL& url, |
| 31 const std::wstring& title, |
| 32 base::Time creation_time, |
| 33 const std::wstring& f1, |
| 34 const std::wstring& f2, |
| 35 const std::wstring& f3) { |
| 36 EXPECT_EQ(on_toolbar, entry.in_toolbar); |
| 37 EXPECT_TRUE(url == entry.url); |
| 38 EXPECT_EQ(title, entry.title); |
| 39 EXPECT_TRUE(creation_time.ToTimeT() == entry.creation_time.ToTimeT()); |
| 40 size_t path_count = 0; |
| 41 if (!f3.empty()) |
| 42 path_count = 3; |
| 43 else if (!f2.empty()) |
| 44 path_count = 2; |
| 45 else if (!f1.empty()) |
| 46 path_count = 1; |
| 47 // The first path element should always be 'x', as that is what we passed |
| 48 // to the importer. |
| 49 ASSERT_EQ(path_count + 1, entry.path.size()); |
| 50 EXPECT_EQ(L"x", entry.path[0]); |
| 51 EXPECT_TRUE(path_count < 1 || entry.path[1] == f1); |
| 52 EXPECT_TRUE(path_count < 2 || entry.path[2] == f2); |
| 53 EXPECT_TRUE(path_count < 3 || entry.path[3] == f3); |
| 54 } |
| 55 |
| 56 std::wstring path_; |
| 57 }; |
| 58 |
| 59 // Tests bookmark_html_writer by populating a BookmarkModel, writing it out by |
| 60 // way of bookmark_html_writer, then using the importer to read it back in. |
| 61 TEST_F(BookmarkHTMLWriterTest, Test) { |
| 62 // Populate the BookmarkModel. This creates the following bookmark structure: |
| 63 // Bookmark Bar |
| 64 // F1 |
| 65 // url1 |
| 66 // F2 |
| 67 // url2 |
| 68 // url3 |
| 69 // Other |
| 70 // url1 |
| 71 // url2 |
| 72 // F3 |
| 73 // F4 |
| 74 // url1 |
| 75 std::wstring f1_title = L"F\"&;<1\""; |
| 76 std::wstring f2_title = L"F2"; |
| 77 std::wstring f3_title = L"F 3"; |
| 78 std::wstring f4_title = L"F4"; |
| 79 std::wstring url1_title = L"url 1"; |
| 80 std::wstring url2_title = L"url&2"; |
| 81 std::wstring url3_title = L"url\"3"; |
| 82 GURL url1("http://url1"); |
| 83 GURL url2("http://url2"); |
| 84 GURL url3("http://url3"); |
| 85 BookmarkModel model(NULL); |
| 86 base::Time t1(base::Time::Now()); |
| 87 base::Time t2(t1 + base::TimeDelta::FromHours(1)); |
| 88 base::Time t3(t1 + base::TimeDelta::FromHours(1)); |
| 89 BookmarkNode* f1 = model.AddGroup(model.GetBookmarkBarNode(), 0, f1_title); |
| 90 model.AddURLWithCreationTime(f1, 0, url1_title, url1, t1); |
| 91 BookmarkNode* f2 = model.AddGroup(f1, 1, f2_title); |
| 92 model.AddURLWithCreationTime(f2, 0, url2_title, url2, t2); |
| 93 model.AddURLWithCreationTime(model.GetBookmarkBarNode(), 1, url3_title, url3, |
| 94 t3); |
| 95 |
| 96 model.AddURLWithCreationTime(model.other_node(), 0, url1_title, url1, t1); |
| 97 model.AddURLWithCreationTime(model.other_node(), 1, url2_title, url2, t2); |
| 98 BookmarkNode* f3 = model.AddGroup(model.other_node(), 2, f3_title); |
| 99 BookmarkNode* f4 = model.AddGroup(f3, 0, f4_title); |
| 100 model.AddURLWithCreationTime(f4, 0, url1_title, url1, t1); |
| 101 |
| 102 // Write to a temp file. |
| 103 bookmark_html_writer::WriteBookmarks(NULL, &model, path_); |
| 104 |
| 105 // Read the bookmarks back in. |
| 106 std::vector<ProfileWriter::BookmarkEntry> parsed_bookmarks; |
| 107 Firefox2Importer::ImportBookmarksFile(path_, std::set<GURL>(), false, |
| 108 L"x", NULL, &parsed_bookmarks, NULL, |
| 109 NULL); |
| 110 |
| 111 // Verify we got back what we wrote. |
| 112 ASSERT_EQ(6, parsed_bookmarks.size()); |
| 113 AssertBookmarkEntryEquals(parsed_bookmarks[0], false, url1, url1_title, t1, |
| 114 L"Bookmark Bar", f1_title, std::wstring()); |
| 115 AssertBookmarkEntryEquals(parsed_bookmarks[1], false, url2, url2_title, t2, |
| 116 L"Bookmark Bar", f1_title, f2_title); |
| 117 AssertBookmarkEntryEquals(parsed_bookmarks[2], false, url3, url3_title, t3, |
| 118 L"Bookmark Bar", std::wstring(), std::wstring()); |
| 119 AssertBookmarkEntryEquals(parsed_bookmarks[3], false, url1, url1_title, t1, |
| 120 std::wstring(), std::wstring(), std::wstring()); |
| 121 AssertBookmarkEntryEquals(parsed_bookmarks[4], false, url2, url2_title, t2, |
| 122 std::wstring(), std::wstring(), std::wstring()); |
| 123 AssertBookmarkEntryEquals(parsed_bookmarks[5], false, url1, url1_title, t1, |
| 124 f3_title, f4_title, std::wstring()); |
| 125 } |
OLD | NEW |