1 // Copyright 2013 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 "ui/base/cocoa/controls/hyperlink_button_cell.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/mac/foundation_util.h"
10 #include "base/mac/scoped_nsobject.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #import "testing/gtest_mac.h"
13 #include "testing/platform_test.h"
14 #import "ui/gfx/test/ui_cocoa_test_helper.h"
20 class HyperlinkButtonCellTest : public CocoaTest {
22 HyperlinkButtonCellTest() {
23 NSRect frame = NSMakeRect(0, 0, 50, 30);
24 base::scoped_nsobject<NSButton> view(
25 [[NSButton alloc] initWithFrame:frame]);
27 base::scoped_nsobject<HyperlinkButtonCell> cell(
28 [[HyperlinkButtonCell alloc] initTextCell:@"Testing"]);
30 [view_ setCell:cell_];
31 [[test_window() contentView] addSubview:view_];
34 void TestCellCustomization(HyperlinkButtonCell* cell) {
35 EXPECT_FALSE([cell isBordered]);
36 EXPECT_EQ(NSNoCellMask, [cell_ highlightsBy]);
37 EXPECT_TRUE([cell showsBorderOnlyWhileMouseInside]);
38 EXPECT_TRUE([cell textColor]);
42 bool HasUnderlineAttribute(NSDictionary* attributes) {
43 NSNumber* number = base::mac::ObjCCastStrict<NSNumber>(
44 [attributes objectForKey:NSUnderlineStyleAttributeName]);
45 return [number unsignedIntegerValue] != 0;
49 HyperlinkButtonCell* cell_;
52 TEST_VIEW(HyperlinkButtonCellTest, view_)
54 // Tests the three designated intializers.
55 TEST_F(HyperlinkButtonCellTest, Initializers) {
56 TestCellCustomization(cell_); // |-initTextFrame:|
57 base::scoped_nsobject<HyperlinkButtonCell> cell(
58 [[HyperlinkButtonCell alloc] init]);
59 TestCellCustomization(cell.get());
61 // Need to create a dummy archiver to test |-initWithCoder:|.
62 NSData* emptyData = [NSKeyedArchiver archivedDataWithRootObject:@""];
64 [[[NSKeyedUnarchiver alloc] initForReadingWithData:emptyData] autorelease];
65 cell.reset([[HyperlinkButtonCell alloc] initWithCoder:coder]);
66 TestCellCustomization(cell);
70 TEST_F(HyperlinkButtonCellTest, SetTextColor) {
71 NSColor* textColor = [NSColor redColor];
72 EXPECT_NE(textColor, [cell_ textColor]);
73 [cell_ setTextColor:textColor];
74 EXPECT_EQ(textColor, [cell_ textColor]);
78 // TODO(rsesek): See if we can synthesize mouse events to more accurately
80 TEST_F(HyperlinkButtonCellTest, MouseHover) {
81 [[NSCursor disappearingItemCursor] push]; // Set a known state.
82 [cell_ mouseEntered:nil];
83 EXPECT_EQ([NSCursor pointingHandCursor], [NSCursor currentCursor]);
84 [cell_ mouseExited:nil];
85 EXPECT_EQ([NSCursor disappearingItemCursor], [NSCursor currentCursor]);
89 // Test mouse events when button is disabled. {
90 TEST_F(HyperlinkButtonCellTest, MouseHoverWhenDisabled) {
91 [cell_ setEnabled:NO];
93 [[NSCursor disappearingItemCursor] push]; // Set a known state.
94 [cell_ mouseEntered:nil];
95 EXPECT_EQ([NSCursor disappearingItemCursor], [NSCursor currentCursor]);
97 [cell_ mouseExited:nil];
98 EXPECT_EQ([NSCursor disappearingItemCursor], [NSCursor currentCursor]);
103 // Test underline on hover.
104 TEST_F(HyperlinkButtonCellTest, UnderlineOnHover) {
105 EXPECT_TRUE(HasUnderlineAttribute([cell_ linkAttributes]));
106 [cell_ mouseEntered:nil];
107 EXPECT_TRUE(HasUnderlineAttribute([cell_ linkAttributes]));
108 [cell_ mouseExited:nil];
109 EXPECT_TRUE(HasUnderlineAttribute([cell_ linkAttributes]));
111 [cell_ setUnderlineOnHover:YES];
112 EXPECT_FALSE(HasUnderlineAttribute([cell_ linkAttributes]));
113 [cell_ mouseEntered:nil];
114 EXPECT_TRUE(HasUnderlineAttribute([cell_ linkAttributes]));
115 [cell_ mouseExited:nil];
116 EXPECT_FALSE(HasUnderlineAttribute([cell_ linkAttributes]));
119 TEST_F(HyperlinkButtonCellTest, Copy) {
120 base::scoped_nsobject<HyperlinkButtonCell> cell1([[HyperlinkButtonCell alloc]
121 initTextCell:@"Cell"]);
122 [cell1 setTextColor:[NSColor redColor]];
124 base::scoped_nsobject<HyperlinkButtonCell> cell2([cell1 copy]);
125 EXPECT_NSEQ([cell1 textColor], [cell2 textColor]);
126 [cell1 setTextColor:[NSColor purpleColor]];
127 [cell2 setTextColor:[NSColor greenColor]];