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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_helpers.h" 6 7 #include "base/memory/scoped_ptr.h" 8 #include "base/strings/utf_string_conversions.h" 9 #include "base/values.h" 10 #include "chrome/browser/bookmarks/bookmark_model.h" 11 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_constants.h" 12 #include "chrome/common/extensions/api/bookmarks.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace extensions { 16 17 namespace keys = bookmark_api_constants; 18 using api::bookmarks::BookmarkTreeNode; 19 20 namespace bookmark_api_helpers { 21 22 class ExtensionBookmarksTest : public testing::Test { 23 public: 24 virtual void SetUp() OVERRIDE { 25 model_.reset(new BookmarkModel(NULL)); 26 model_->AddURL(model_->other_node(), 0, ASCIIToUTF16("Digg"), 27 GURL("http://www.reddit.com")); 28 model_->AddURL(model_->other_node(), 0, ASCIIToUTF16("News"), 29 GURL("http://www.foxnews.com")); 30 folder_ = model_->AddFolder( 31 model_->other_node(), 0, ASCIIToUTF16("outer folder")); 32 model_->AddFolder(folder_, 0, ASCIIToUTF16("inner folder 1")); 33 model_->AddFolder(folder_, 0, ASCIIToUTF16("inner folder 2")); 34 model_->AddURL(folder_, 0, ASCIIToUTF16("Digg"), GURL("http://reddit.com")); 35 model_->AddURL(folder_, 0, ASCIIToUTF16("CNet"), GURL("http://cnet.com")); 36 } 37 38 scoped_ptr<BookmarkModel> model_; 39 const BookmarkNode* folder_; 40 }; 41 TEST_F(ExtensionBookmarksTest, GetFullTreeFromRoot) { 42 scoped_ptr<BookmarkTreeNode> tree( 43 GetBookmarkTreeNode(model_->other_node(), 44 true, // Recurse. 45 false)); // Not only folders. 46 ASSERT_EQ(3U, tree->children->size()); 47 } 48 49 TEST_F(ExtensionBookmarksTest, GetFoldersOnlyFromRoot) { 50 scoped_ptr<BookmarkTreeNode> tree( 51 GetBookmarkTreeNode(model_->other_node(), 52 true, // Recurse. 53 true)); // Only folders. 54 ASSERT_EQ(1U, tree->children->size()); 55 } 56 57 TEST_F(ExtensionBookmarksTest, GetSubtree) { 58 scoped_ptr<BookmarkTreeNode> tree( 59 GetBookmarkTreeNode(folder_, 60 true, // Recurse. 61 false)); // Not only folders. 62 ASSERT_EQ(4U, tree->children->size()); 63 linked_ptr<BookmarkTreeNode> digg = tree->children->at(1); 64 ASSERT_TRUE(digg.get()); 65 ASSERT_EQ("Digg", digg->title); 66 } 67 68 TEST_F(ExtensionBookmarksTest, GetSubtreeFoldersOnly) { 69 scoped_ptr<BookmarkTreeNode> tree( 70 GetBookmarkTreeNode(folder_, 71 true, // Recurse. 72 true)); // Only folders. 73 ASSERT_EQ(2U, tree->children->size()); 74 linked_ptr<BookmarkTreeNode> inner_folder = tree->children->at(1); 75 ASSERT_TRUE(inner_folder.get()); 76 ASSERT_EQ("inner folder 1", inner_folder->title); 77 } 78 79 } // namespace bookmark_api_helpers 80 } // namespace extensions 81