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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h" 6 7 #include "base/strings/utf_string_conversions.h" 8 #include "chrome/browser/bookmarks/bookmark_model.h" 9 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/test/base/browser_with_test_window_test.h" 12 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/ui_test_utils.h" 14 #include "ui/views/controls/menu/menu_runner.h" 15 16 class BookmarkMenuDelegateTest : public BrowserWithTestWindowTest { 17 public: 18 BookmarkMenuDelegateTest() : model_(NULL) { 19 } 20 21 virtual void SetUp() OVERRIDE { 22 BrowserWithTestWindowTest::SetUp(); 23 24 profile()->CreateBookmarkModel(true); 25 26 model_ = BookmarkModelFactory::GetForProfile(profile()); 27 ui_test_utils::WaitForBookmarkModelToLoad(model_); 28 29 AddTestData(); 30 31 bookmark_menu_delegate_.reset( 32 new BookmarkMenuDelegate(browser(), NULL, NULL, 0)); 33 } 34 35 virtual void TearDown() OVERRIDE { 36 // Since we never show the menu we need to pass the MenuItemView to 37 // MenuRunner so that the MenuItemView is destroyed. 38 views::MenuRunner menu_runner(bookmark_menu_delegate_->menu()); 39 bookmark_menu_delegate_.reset(); 40 BrowserWithTestWindowTest::TearDown(); 41 } 42 43 protected: 44 BookmarkModel* model_; 45 46 scoped_ptr<BookmarkMenuDelegate> bookmark_menu_delegate_; 47 48 private: 49 std::string base_path() const { return "file:///c:/tmp/"; } 50 51 // Creates the following structure: 52 // bookmark bar node 53 // a 54 // F1 55 // f1a 56 // F11 57 // f11a 58 // F2 59 // other node 60 // oa 61 // OF1 62 // of1a 63 void AddTestData() { 64 const BookmarkNode* bb_node = model_->bookmark_bar_node(); 65 std::string test_base = base_path(); 66 model_->AddURL(bb_node, 0, ASCIIToUTF16("a"), GURL(test_base + "a")); 67 const BookmarkNode* f1 = model_->AddFolder(bb_node, 1, ASCIIToUTF16("F1")); 68 model_->AddURL(f1, 0, ASCIIToUTF16("f1a"), GURL(test_base + "f1a")); 69 const BookmarkNode* f11 = model_->AddFolder(f1, 1, ASCIIToUTF16("F11")); 70 model_->AddURL(f11, 0, ASCIIToUTF16("f11a"), GURL(test_base + "f11a")); 71 model_->AddFolder(bb_node, 2, ASCIIToUTF16("F2")); 72 73 // Children of the other node. 74 model_->AddURL(model_->other_node(), 0, ASCIIToUTF16("oa"), 75 GURL(test_base + "oa")); 76 const BookmarkNode* of1 = 77 model_->AddFolder(model_->other_node(), 1, ASCIIToUTF16("OF1")); 78 model_->AddURL(of1, 0, ASCIIToUTF16("of1a"), GURL(test_base + "of1a")); 79 } 80 81 DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegateTest); 82 }; 83 84 // Verifies WillRemoveBookmarks() doesn't attempt to access MenuItemViews that 85 // have since been deleted. 86 TEST_F(BookmarkMenuDelegateTest, RemoveBookmarks) { 87 views::MenuDelegate test_delegate; 88 const BookmarkNode* node = model_->bookmark_bar_node()->GetChild(1); 89 bookmark_menu_delegate_->Init(&test_delegate, NULL, node, 0, 90 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, 91 bookmark_utils::LAUNCH_NONE); 92 std::vector<const BookmarkNode*> nodes_to_remove; 93 nodes_to_remove.push_back(node->GetChild(1)); 94 bookmark_menu_delegate_->WillRemoveBookmarks(nodes_to_remove); 95 nodes_to_remove.clear(); 96 bookmark_menu_delegate_->DidRemoveBookmarks(); 97 } 98