summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/history_menu_bridge_unittest.mm
blob: a0e45e604a8f8f4f566c805f22e21c4c040e81e1 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// 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 <Cocoa/Cocoa.h>
#include "base/sys_string_conversions.h"
#include "chrome/app/chrome_dll_resource.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/cocoa/history_menu_bridge.h"
#include "chrome/browser/cocoa/browser_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

class HistoryMenuBridgeTest : public PlatformTest {
 public:

  // We are a friend of HistoryMenuBridge (and have access to
  // protected methods), but none of the classes generated by TEST_F()
  // are. Wraps common commands.
  void ClearMenuSection(HistoryMenuBridge* bridge,
                        NSMenu* menu,
                        NSInteger tag,
                        unsigned int count) {
    bridge->ClearMenuSection(menu, tag, count);
  }

  void AddItemToBridgeMenu(HistoryMenuBridge* bridge,
                           HistoryMenuBridge::HistoryItem& item,
                           NSMenu* menu,
                           NSInteger tag,
                           NSInteger index) {
    bridge->AddItemToMenu(item, menu, tag, index);
  }

  NSMenuItem* AddItemToMenu(NSMenu* menu,
                            NSString* title,
                            SEL selector,
                            int tag) {
    NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:title action:NULL
                                            keyEquivalent:@""] autorelease];
    [item setTag:tag];
    if (selector)
      [item setAction:selector];
    [menu addItem:item];
    return item;
  }

  HistoryMenuBridge::HistoryItem CreateItem(const string16& title) {
    HistoryMenuBridge::HistoryItem item;
    item.title = title;
    item.url = GURL("http://google.com");
    return item;
  }

  BrowserTestHelper browser_test_helper_;
};

// Edge case test for clearing until the end of a menu.
TEST_F(HistoryMenuBridgeTest, TestClearHistoryMenuUntilEnd) {
  scoped_ptr<HistoryMenuBridge> bridge(
      new HistoryMenuBridge(browser_test_helper_.profile()));
  EXPECT_TRUE(bridge.get());

  NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
  NSInteger section_tag = 9990;
  AddItemToMenu(menu, @"HEADER", NULL, section_tag);
  NSInteger tag = section_tag;

  AddItemToMenu(menu, @"alpha", @selector(openHistoryMenuItem:), ++tag);
  AddItemToMenu(menu, @"bravo", @selector(openHistoryMenuItem:), ++tag);
  AddItemToMenu(menu, @"charlie", @selector(openHistoryMenuItem:), ++tag);
  AddItemToMenu(menu, @"delta", @selector(openHistoryMenuItem:), ++tag);

  ClearMenuSection(bridge.get(), menu, section_tag, 4);

  EXPECT_EQ(1, [menu numberOfItems]);
  EXPECT_EQ(@"HEADER", [[menu itemWithTag:section_tag] title]);
}

// Skip menu items that are not hooked up to |-openHistoryMenuItem:|.
TEST_F(HistoryMenuBridgeTest, TestClearHistoryMenuSkipping) {
  scoped_ptr<HistoryMenuBridge> bridge(
      new HistoryMenuBridge(browser_test_helper_.profile()));
  EXPECT_TRUE(bridge.get());

  NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
  NSInteger section_tag = 9990;
  AddItemToMenu(menu, @"HEADER", NULL, section_tag);
  NSInteger tag = section_tag;

  AddItemToMenu(menu, @"alpha", @selector(openHistoryMenuItem:), ++tag);
  AddItemToMenu(menu, @"bravo", @selector(openHistoryMenuItem:), ++tag);
  AddItemToMenu(menu, @"unhooked", NULL, ++tag);
  AddItemToMenu(menu, @"charlie", @selector(openHistoryMenuItem:), ++tag);

  ClearMenuSection(bridge.get(), menu, section_tag, 4);

  EXPECT_EQ(2, [menu numberOfItems]);
  EXPECT_EQ(@"HEADER", [[menu itemWithTag:section_tag] title]);
  EXPECT_EQ(@"unhooked", [[menu itemAtIndex:1] title]);
}

// Edge case test for clearing an empty menu.
TEST_F(HistoryMenuBridgeTest, TestClearHistoryMenuEmpty) {
  scoped_ptr<HistoryMenuBridge> bridge(
      new HistoryMenuBridge(browser_test_helper_.profile()));
  EXPECT_TRUE(bridge.get());

  NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
  NSInteger section_tag = 9990;
  AddItemToMenu(menu, @"HEADER", NULL, section_tag);

  ClearMenuSection(bridge.get(), menu, section_tag, 1);

  EXPECT_EQ(1, [menu numberOfItems]);
  EXPECT_EQ(@"HEADER", [[menu itemWithTag:section_tag] title]);
}

// Test that AddItemToMenu() properly adds HistoryItem objects as menus.
TEST_F(HistoryMenuBridgeTest, TestAddItemToMenu) {
  scoped_ptr<HistoryMenuBridge> bridge(
      new HistoryMenuBridge(browser_test_helper_.profile()));
  EXPECT_TRUE(bridge.get());

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

  string16 short_url = ASCIIToUTF16("http://foo/");
  string16 long_url = ASCIIToUTF16("http://super-duper-long-url--."
    "that.cannot.possibly.fit.even-in-80-columns"
    "or.be.reasonably-displayed-in-a-menu"
    "without.looking-ridiculous.com/"); // 140 chars total

  HistoryMenuBridge::HistoryItem item1;
  item1.title = short_url;
  item1.url = GURL(short_url);
  AddItemToBridgeMenu(bridge.get(), item1, menu, 100, 0);

  HistoryMenuBridge::HistoryItem item2;
  item2.title = long_url;
  item2.url = GURL(long_url);
  AddItemToBridgeMenu(bridge.get(), item2, menu, 101, 1);

  EXPECT_EQ(2, [menu numberOfItems]);

  EXPECT_EQ(@selector(openHistoryMenuItem:), [[menu itemAtIndex:0] action]);
  EXPECT_EQ(@selector(openHistoryMenuItem:), [[menu itemAtIndex:1] action]);

  EXPECT_EQ(100, [[menu itemAtIndex:0] tag]);
  EXPECT_EQ(101, [[menu itemAtIndex:1] tag]);

  // Make sure a short title looks fine
  NSString* s = [[menu itemAtIndex:0] title];
  EXPECT_EQ(base::SysNSStringToUTF16(s), short_url);

  // Make sure a super-long title gets trimmed
  s = [[menu itemAtIndex:0] title];
  EXPECT_TRUE([s length] < long_url.length());

  // Confirm tooltips and confirm they are not trimmed (like the item
  // name might be).  Add tolerance for URL fixer-upping;
  // e.g. http://foo becomes http://foo/)
  EXPECT_GE([[[menu itemAtIndex:0] toolTip] length], (2*short_url.length()-5));
  EXPECT_GE([[[menu itemAtIndex:1] toolTip] length], (2*long_url.length()-5));
}