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/memory/scoped_nsobject.h" 6#include "chrome/app/chrome_command_ids.h" 7#include "chrome/browser/ui/cocoa/browser_test_helper.h" 8#import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 9#import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" 10#import "chrome/browser/ui/cocoa/view_resizer_pong.h" 11#import "chrome/browser/ui/cocoa/wrench_menu/wrench_menu_controller.h" 12#include "chrome/browser/ui/toolbar/wrench_menu_model.h" 13#include "testing/gmock/include/gmock/gmock.h" 14#include "testing/gtest/include/gtest/gtest.h" 15#include "testing/platform_test.h" 16 17// Override to avoid dealing with run loops in the testing environment. 18@implementation WrenchMenuController (UnitTesting) 19- (void)dispatchCommandInternal:(NSInteger)tag { 20 [self wrenchMenuModel]->ExecuteCommand(tag); 21} 22@end 23 24 25namespace { 26 27class MockWrenchMenuModel : public WrenchMenuModel { 28 public: 29 MockWrenchMenuModel() : WrenchMenuModel() {} 30 ~MockWrenchMenuModel() { 31 // This dirty, ugly hack gets around a bug in the test. In 32 // ~WrenchMenuModel(), there's a call to TabstripModel::RemoveObserver(this) 33 // which mysteriously leads to this crash: http://crbug.com/49206 . It 34 // seems that the vector of observers is getting hosed somewhere between 35 // |-[ToolbarController dealloc]| and ~MockWrenchMenuModel(). This line 36 // short-circuits the parent destructor to avoid this crash. 37 tabstrip_model_ = NULL; 38 } 39 MOCK_METHOD1(ExecuteCommand, void(int command_id)); 40}; 41 42class WrenchMenuControllerTest : public CocoaTest { 43 public: 44 void SetUp() { 45 Browser* browser = helper_.browser(); 46 resize_delegate_.reset([[ViewResizerPong alloc] init]); 47 toolbar_controller_.reset( 48 [[ToolbarController alloc] initWithModel:browser->toolbar_model() 49 commands:browser->command_updater() 50 profile:helper_.profile() 51 browser:browser 52 resizeDelegate:resize_delegate_.get()]); 53 EXPECT_TRUE([toolbar_controller_ view]); 54 NSView* parent = [test_window() contentView]; 55 [parent addSubview:[toolbar_controller_ view]]; 56 } 57 58 WrenchMenuController* controller() { 59 return [toolbar_controller_ wrenchMenuController]; 60 } 61 62 BrowserTestHelper helper_; 63 scoped_nsobject<ViewResizerPong> resize_delegate_; 64 MockWrenchMenuModel fake_model_; 65 scoped_nsobject<ToolbarController> toolbar_controller_; 66}; 67 68TEST_F(WrenchMenuControllerTest, Initialized) { 69 EXPECT_TRUE([controller() menu]); 70 EXPECT_GE([[controller() menu] numberOfItems], 5); 71} 72 73TEST_F(WrenchMenuControllerTest, DispatchSimple) { 74 scoped_nsobject<NSButton> button([[NSButton alloc] init]); 75 [button setTag:IDC_ZOOM_PLUS]; 76 77 // Set fake model to test dispatching. 78 EXPECT_CALL(fake_model_, ExecuteCommand(IDC_ZOOM_PLUS)); 79 [controller() setModel:&fake_model_]; 80 81 [controller() dispatchWrenchMenuCommand:button.get()]; 82} 83 84} // namespace 85