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.
5 #import <Cocoa/Cocoa.h>
7 #include "base/mac/scoped_nsobject.h"
8 #import "chrome/browser/ui/cocoa/clickhold_button_cell.h"
9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/gtest_mac.h"
12 #include "testing/platform_test.h"
14 @interface ClickHoldButtonCellTestTarget : NSObject {
22 @implementation ClickHoldButtonCellTestTarget
23 - (instancetype)init {
24 if ((self = [super init]))
41 class ClickHoldButtonCellTest : public CocoaTest {
43 ClickHoldButtonCellTest() {
44 NSRect frame = NSMakeRect(0, 0, 50, 30);
45 base::scoped_nsobject<NSButton> view(
46 [[NSButton alloc] initWithFrame:frame]);
48 base::scoped_nsobject<ClickHoldButtonCell> cell(
49 [[ClickHoldButtonCell alloc] initTextCell:@"Testing"]);
50 [view_ setCell:cell.get()];
51 [[test_window() contentView] addSubview:view_];
57 TEST_VIEW(ClickHoldButtonCellTest, view_)
59 // Test default values; make sure they are what they should be.
60 TEST_F(ClickHoldButtonCellTest, Defaults) {
61 ClickHoldButtonCell* cell = static_cast<ClickHoldButtonCell*>([view_ cell]);
62 ASSERT_TRUE([cell isKindOfClass:[ClickHoldButtonCell class]]);
64 EXPECT_FALSE([cell enableClickHold]);
66 NSTimeInterval clickHoldTimeout = [cell clickHoldTimeout];
67 EXPECT_GE(clickHoldTimeout, 0.15); // Check for a "Cocoa-ish" value.
68 EXPECT_LE(clickHoldTimeout, 0.35);
70 EXPECT_FALSE([cell trackOnlyInRect]);
71 EXPECT_TRUE([cell activateOnDrag]);
74 // TODO(viettrungluu): (1) Enable click-hold and figure out how to test the
75 // tracking loop (i.e., |-trackMouse:...|), which is the nontrivial part.
76 // (2) Test various initialization code paths (in particular, loading from nib).
78 TEST_F(ClickHoldButtonCellTest, AccessibilityShowMenu) {
79 ClickHoldButtonCell* cell = static_cast<ClickHoldButtonCell*>([view_ cell]);
80 ASSERT_TRUE([cell isKindOfClass:[ClickHoldButtonCell class]]);
82 ClickHoldButtonCellTestTarget* cellTarget =
83 [[[ClickHoldButtonCellTestTarget alloc] init] autorelease];
84 ASSERT_NSNE(nil, cellTarget);
85 ASSERT_TRUE([cellTarget respondsToSelector:@selector(open)]);
86 ASSERT_FALSE([cellTarget didOpen]);
88 [cell setEnableClickHold:NO];
90 NSArray* actionNames = [cell accessibilityActionNames];
91 EXPECT_FALSE([actionNames containsObject:NSAccessibilityShowMenuAction]);
93 [cell setEnableClickHold:YES];
95 // There is no action set.
96 actionNames = [cell accessibilityActionNames];
97 EXPECT_FALSE([actionNames containsObject:NSAccessibilityShowMenuAction]);
99 // clickHoldAction should be independent from accessibilityShowMenuAction
100 // since different operations, e.g. releasing vs. not releasing a mouse
101 // button, may need to be performed for each occasion.
102 [cell setClickHoldAction:@selector(open)];
103 [cell setClickHoldTarget:cellTarget];
105 actionNames = [cell accessibilityActionNames];
106 EXPECT_FALSE([actionNames containsObject:NSAccessibilityShowMenuAction]);
107 EXPECT_FALSE([cellTarget didOpen]);
109 // Even when accessibilityShowMenuAction is set, no action should be available
110 // if neither enableClickHold nor enableRightClickHold are true.
111 [cell setEnableClickHold:NO];
112 [cell setAccessibilityShowMenuAction:@selector(open)];
113 [cell setAccessibilityShowMenuTarget:cellTarget];
115 actionNames = [cell accessibilityActionNames];
116 EXPECT_FALSE([actionNames containsObject:NSAccessibilityShowMenuAction]);
118 // Now the action should be available.
119 [cell setEnableClickHold:YES];
121 actionNames = [cell accessibilityActionNames];
122 EXPECT_TRUE([actionNames containsObject:NSAccessibilityShowMenuAction]);
124 [cell setEnableClickHold:NO];
125 [cell setEnableRightClick:YES];
127 actionNames = [cell accessibilityActionNames];
128 EXPECT_TRUE([actionNames containsObject:NSAccessibilityShowMenuAction]);
130 [cell accessibilityPerformAction:NSAccessibilityShowMenuAction];
131 EXPECT_TRUE([cellTarget didOpen]);