// Copyright (c) 2009 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #import #include "chrome/app/chrome_dll_resource.h" #include "chrome/browser/browser.h" #include "chrome/browser/cocoa/bookmark_menu_bridge.h" #include "chrome/browser/cocoa/browser_test_helper.h" #include "testing/gtest/include/gtest/gtest.h" // TODO(jrg): see refactor comment in bookmark_bar_state_controller_unittest.mm class BookmarkMenuBridgeTest : public testing::Test { public: // We are a friend of BookmarkMenuBridge (and have access to // protected methods), but none of the classes generated by TEST_F() // are. This (and AddNodeToMenu()) are simple wrappers to let // derived test classes have access to protected methods. void ClearBookmarkMenu(BookmarkMenuBridge* bridge, NSMenu* menu) { bridge->ClearBookmarkMenu(menu); } void AddNodeToMenu(BookmarkMenuBridge* bridge, BookmarkNode* root, NSMenu* menu) { bridge->AddNodeToMenu(root, menu); } NSMenuItem* AddItemToMenu(NSMenu *menu, NSString *title, SEL selector) { NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle:title action:NULL keyEquivalent:@""] autorelease]; if (selector) [item setAction:selector]; [menu addItem:item]; return item; } BrowserTestHelper browser_test_helper_; }; // Test that ClearBookmarkMenu() removes all bookmark menus. TEST_F(BookmarkMenuBridgeTest, TestClearBookmarkMenu) { scoped_ptr bridge(new BookmarkMenuBridge()); EXPECT_TRUE(bridge.get()); NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"foo"] autorelease]; AddItemToMenu(menu, @"hi mom", nil); AddItemToMenu(menu, @"not", @selector(openBookmarkMenuItem:)); NSMenuItem* item = AddItemToMenu(menu, @"hi mom", nil); [item setSubmenu:[[[NSMenu alloc] initWithTitle:@"bar"] autorelease]]; AddItemToMenu(menu, @"not", @selector(openBookmarkMenuItem:)); AddItemToMenu(menu, @"zippy", @selector(length)); ClearBookmarkMenu(bridge.get(), menu); // Make sure all bookmark items are removed, and all items with // submenus removed. EXPECT_EQ(2, [menu numberOfItems]); for (NSMenuItem *item in [menu itemArray]) { EXPECT_FALSE([[item title] isEqual:@"not"]); } } // Test that AddNodeToMenu() properly adds bookmark nodes as menus, // including the recursive case. TEST_F(BookmarkMenuBridgeTest, TestAddNodeToMenu) { Profile* profile = browser_test_helper_.GetProfile(); scoped_ptr bridge(new BookmarkMenuBridge()); EXPECT_TRUE(bridge.get()); NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"foo"] autorelease]; BookmarkModel* model = new BookmarkModel(profile); BookmarkNode* root = new BookmarkNode(model, GURL()); EXPECT_TRUE(model && root); // 3 nodes; middle one has a child BookmarkNode* node = NULL; for (int x = 0; x < 3; x++) { node = new BookmarkNode(model, (x==1 ? GURL() : GURL("http://foo"))); root->Add(x, node); } node = new BookmarkNode(model, GURL("http://sub")); root->GetChild(1)->Add(0, node); // Add to the NSMenu, then confirm it looks good AddNodeToMenu(bridge.get(), root, menu); EXPECT_EQ(3, [menu numberOfItems]); for (int x=0; x < 3; x++) { NSMenuItem* item = [menu itemAtIndex:x]; EXPECT_EQ(@selector(openBookmarkMenuItem:), [item action]); } EXPECT_EQ(NO, [[menu itemAtIndex:0] hasSubmenu]); EXPECT_EQ(NO, [[menu itemAtIndex:2] hasSubmenu]); NSMenuItem* middle = [menu itemAtIndex:1]; EXPECT_NE(NO, [middle hasSubmenu]); EXPECT_EQ(1, [[middle submenu] numberOfItems]); delete root; // deletes all its kids delete model; }