Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2011 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 "base/mac/cocoa_protocols.h"
      6 #include "base/memory/scoped_nsobject.h"
      7 #import "chrome/browser/ui/cocoa/clickhold_button_cell.h"
      8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
      9 #import "chrome/browser/ui/cocoa/menu_button.h"
     10 
     11 @interface MenuButtonTestDelegate : NSObject<NSMenuDelegate> {
     12  @private
     13   scoped_nsobject<NSMenu> menu_;
     14   BOOL open_;
     15   BOOL didOpen_;
     16 }
     17 - (BOOL)isOpen;
     18 - (BOOL)didOpen;
     19 @end
     20 
     21 @implementation MenuButtonTestDelegate
     22 - (id)initWithMenu:(NSMenu*)menu {
     23   if ((self = [super init])) {
     24     menu_.reset([menu retain]);
     25   }
     26   return self;
     27 }
     28 
     29 - (BOOL)isOpen {
     30   return open_;
     31 }
     32 
     33 - (BOOL)didOpen {
     34   return didOpen_;
     35 }
     36 
     37 - (void)menuWillOpen:(NSMenu*)menu {
     38   EXPECT_EQ(menu_.get(), menu);
     39   EXPECT_FALSE(open_);
     40   open_ = YES;
     41   didOpen_ = YES;
     42   NSArray* modes = [NSArray arrayWithObjects:NSEventTrackingRunLoopMode,
     43                                              NSDefaultRunLoopMode,
     44                                              nil];
     45   [menu performSelector:@selector(cancelTracking)
     46              withObject:nil
     47              afterDelay:1.5
     48                 inModes:modes];
     49 }
     50 
     51 - (void)menuDidClose:(NSMenu*)menu {
     52   EXPECT_EQ(menu_.get(), menu);
     53   EXPECT_TRUE(open_);
     54   open_ = NO;
     55 }
     56 @end
     57 
     58 namespace {
     59 
     60 class MenuButtonTest : public CocoaTest {
     61  public:
     62   MenuButtonTest() {
     63     NSRect frame = NSMakeRect(0, 0, 50, 30);
     64     scoped_nsobject<MenuButton> button(
     65         [[MenuButton alloc] initWithFrame:frame]);
     66     button_ = button.get();
     67     scoped_nsobject<ClickHoldButtonCell> cell(
     68         [[ClickHoldButtonCell alloc] initTextCell:@"Testing"]);
     69     [button_ setCell:cell.get()];
     70     [[test_window() contentView] addSubview:button_];
     71   }
     72 
     73   NSMenu* CreateMenu() {
     74     NSMenu* menu = [[NSMenu alloc] initWithTitle:@""];
     75     [menu insertItemWithTitle:@"" action:nil keyEquivalent:@"" atIndex:0];
     76     [menu insertItemWithTitle:@"foo" action:nil keyEquivalent:@"" atIndex:1];
     77     [menu insertItemWithTitle:@"bar" action:nil keyEquivalent:@"" atIndex:2];
     78     [menu insertItemWithTitle:@"baz" action:nil keyEquivalent:@"" atIndex:3];
     79     return menu;
     80   }
     81 
     82   MenuButton* button_;
     83 };
     84 
     85 TEST_VIEW(MenuButtonTest, button_);
     86 
     87 // Test assigning a menu, again mostly to ensure nothing leaks or crashes.
     88 TEST_F(MenuButtonTest, MenuAssign) {
     89   scoped_nsobject<NSMenu> menu(CreateMenu());
     90   ASSERT_TRUE(menu.get());
     91 
     92   [button_ setAttachedMenu:menu];
     93   EXPECT_TRUE([button_ attachedMenu]);
     94 }
     95 
     96 TEST_F(MenuButtonTest, OpenOnClick) {
     97   scoped_nsobject<NSMenu> menu(CreateMenu());
     98   ASSERT_TRUE(menu.get());
     99 
    100   scoped_nsobject<MenuButtonTestDelegate> delegate(
    101       [[MenuButtonTestDelegate alloc] initWithMenu:menu.get()]);
    102   ASSERT_TRUE(delegate.get());
    103 
    104   [menu setDelegate:delegate.get()];
    105   [button_ setAttachedMenu:menu];
    106   [button_ setOpenMenuOnClick:YES];
    107 
    108   EXPECT_FALSE([delegate isOpen]);
    109   EXPECT_FALSE([delegate didOpen]);
    110 
    111   // Should open the menu.
    112   [button_ performClick:nil];
    113 
    114   EXPECT_TRUE([delegate didOpen]);
    115   EXPECT_FALSE([delegate isOpen]);
    116 }
    117 
    118 }  // namespace
    119