summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/bookmark_menu_bridge_unittest.mm
blob: d00f75fbce77e5cd1e1e2fb6aa280cd0dae81098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// 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 <AppKit/AppKit.h>
#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, NSInteger tag) {
    NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle:title action:NULL
                                            keyEquivalent:@""] autorelease];
    [item setTag:tag];
    [menu addItem:item];
    return item;
  }

  BrowserTestHelper browser_test_helper_;
};


// Test that ClearBookmarkMenu() removes all bookmark menus.
TEST_F(BookmarkMenuBridgeTest, TestClearBookmarkMenu) {
  Browser* browser = browser_test_helper_.GetBrowser();
  BookmarkMenuBridge* bridge = new BookmarkMenuBridge(browser);
  EXPECT_TRUE(bridge);

  NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"foo"] autorelease];

  AddItemToMenu(menu, @"hi mom", IDC_BOOKMARK_MENUITEM_BASE);
  AddItemToMenu(menu, @"not", 0);
  NSMenuItem* item = AddItemToMenu(menu, @"hi mom", 0);
  [item setSubmenu:[[[NSMenu alloc] initWithTitle:@"bar"] autorelease]];
  AddItemToMenu(menu, @"not", 0);

  ClearBookmarkMenu(bridge, menu);

  // Make sure all IDC_BOOKMARK items are removed, and all items with
  // submenus removed.
  EXPECT_EQ(2, [menu numberOfItems]);
  for (NSMenuItem *item in [menu itemArray]) {
    EXPECT_TRUE([[item title] isEqual:@"not"]);
  }
}

// Test that AddNodeToMenu() properly adds bookmark nodes as menus,
// including the recursive case.
TEST_F(BookmarkMenuBridgeTest, TestAddNodeToMenu) {
  Browser* browser = browser_test_helper_.GetBrowser();
  Profile* profile = browser_test_helper_.GetProfile();

  BookmarkMenuBridge *bridge = new BookmarkMenuBridge(browser);
  EXPECT_TRUE(bridge);

  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, root, menu);

  EXPECT_EQ(3, [menu numberOfItems]);
  for (int x=0; x < 3; x++) {
    NSMenuItem* item = [menu itemAtIndex:x];
    NSInteger tag = [item tag];
    EXPECT_TRUE((tag >= IDC_BOOKMARK_MENUITEM_BASE) &&
                (tag < IDC_BOOKMARK_MENUITEM_MAX));
  }
  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;
}