OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 #import <AppKit/AppKit.h> |
| 6 #include "chrome/app/chrome_dll_resource.h" |
| 7 #include "chrome/browser/browser.h" |
| 8 #include "chrome/browser/cocoa/bookmark_menu_bridge.h" |
| 9 #include "chrome/browser/cocoa/browser_test_helper.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 // TODO(jrg): see refactor comment in bookmark_bar_state_controller_unittest.mm |
| 13 class BookmarkMenuBridgeTest : public testing::Test { |
| 14 public: |
| 15 |
| 16 // We are a friend of BookmarkMenuBridge (and have access to |
| 17 // protected methods), but none of the classes generated by TEST_F() |
| 18 // are. This (and AddNodeToMenu()) are simple wrappers to let |
| 19 // derived test classes have access to protected methods. |
| 20 void ClearBookmarkMenu(BookmarkMenuBridge* bridge, NSMenu* menu) { |
| 21 bridge->ClearBookmarkMenu(menu); |
| 22 } |
| 23 |
| 24 void AddNodeToMenu(BookmarkMenuBridge* bridge, BookmarkNode* root, |
| 25 NSMenu* menu) { |
| 26 bridge->AddNodeToMenu(root, menu); |
| 27 } |
| 28 |
| 29 NSMenuItem* AddItemToMenu(NSMenu *menu, NSString *title, NSInteger tag) { |
| 30 NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle:title action:NULL |
| 31 keyEquivalent:@""] autorelease]; |
| 32 [item setTag:tag]; |
| 33 [menu addItem:item]; |
| 34 return item; |
| 35 } |
| 36 |
| 37 BrowserTestHelper browser_test_helper_; |
| 38 }; |
| 39 |
| 40 |
| 41 // Test that ClearBookmarkMenu() removes all bookmark menus. |
| 42 TEST_F(BookmarkMenuBridgeTest, TestClearBookmarkMenu) { |
| 43 Browser* browser = browser_test_helper_.GetBrowser(); |
| 44 BookmarkMenuBridge* bridge = new BookmarkMenuBridge(browser); |
| 45 EXPECT_TRUE(bridge); |
| 46 |
| 47 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"foo"] autorelease]; |
| 48 |
| 49 AddItemToMenu(menu, @"hi mom", IDC_BOOKMARK_MENUITEM_BASE); |
| 50 AddItemToMenu(menu, @"not", 0); |
| 51 NSMenuItem* item = AddItemToMenu(menu, @"hi mom", 0); |
| 52 [item setSubmenu:[[[NSMenu alloc] initWithTitle:@"bar"] autorelease]]; |
| 53 AddItemToMenu(menu, @"not", 0); |
| 54 |
| 55 ClearBookmarkMenu(bridge, menu); |
| 56 |
| 57 // Make sure all IDC_BOOKMARK items are removed, and all items with |
| 58 // submenus removed. |
| 59 EXPECT_EQ(2, [menu numberOfItems]); |
| 60 for (NSMenuItem *item in [menu itemArray]) { |
| 61 EXPECT_TRUE([[item title] isEqual:@"not"]); |
| 62 } |
| 63 } |
| 64 |
| 65 // Test that AddNodeToMenu() properly adds bookmark nodes as menus, |
| 66 // including the recursive case. |
| 67 TEST_F(BookmarkMenuBridgeTest, TestAddNodeToMenu) { |
| 68 Browser* browser = browser_test_helper_.GetBrowser(); |
| 69 Profile* profile = browser_test_helper_.GetProfile(); |
| 70 |
| 71 BookmarkMenuBridge *bridge = new BookmarkMenuBridge(browser); |
| 72 EXPECT_TRUE(bridge); |
| 73 |
| 74 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"foo"] autorelease]; |
| 75 |
| 76 BookmarkModel* model = new BookmarkModel(profile); |
| 77 BookmarkNode* root = new BookmarkNode(model, GURL()); |
| 78 EXPECT_TRUE(model && root); |
| 79 |
| 80 // 3 nodes; middle one has a child |
| 81 BookmarkNode* node = NULL; |
| 82 for (int x = 0; x < 3; x++) { |
| 83 node = new BookmarkNode(model, (x==1 ? GURL() : GURL("http://foo"))); |
| 84 root->Add(x, node); |
| 85 } |
| 86 node = new BookmarkNode(model, GURL("http://sub")); |
| 87 root->GetChild(1)->Add(0, node); |
| 88 |
| 89 // Add to the NSMenu, then confirm it looks good |
| 90 AddNodeToMenu(bridge, root, menu); |
| 91 |
| 92 EXPECT_EQ(3, [menu numberOfItems]); |
| 93 for (int x=0; x < 3; x++) { |
| 94 NSMenuItem* item = [menu itemAtIndex:x]; |
| 95 NSInteger tag = [item tag]; |
| 96 EXPECT_TRUE((tag >= IDC_BOOKMARK_MENUITEM_BASE) && |
| 97 (tag < IDC_BOOKMARK_MENUITEM_MAX)); |
| 98 } |
| 99 EXPECT_EQ(NO, [[menu itemAtIndex:0] hasSubmenu]); |
| 100 EXPECT_EQ(NO, [[menu itemAtIndex:2] hasSubmenu]); |
| 101 NSMenuItem* middle = [menu itemAtIndex:1]; |
| 102 EXPECT_NE(NO, [middle hasSubmenu]); |
| 103 EXPECT_EQ(1, [[middle submenu] numberOfItems]); |
| 104 |
| 105 delete root; // deletes all its kids |
| 106 delete model; |
| 107 } |
OLD | NEW |