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. 4 5#import "ui/base/cocoa/controls/hyperlink_button_cell.h" 6 7#import <Cocoa/Cocoa.h> 8 9#include "base/mac/foundation_util.h" 10#include "base/mac/scoped_nsobject.h" 11#import "testing/gtest_mac.h" 12#include "testing/gtest/include/gtest/gtest.h" 13#include "testing/platform_test.h" 14#import "ui/base/test/ui_cocoa_test_helper.h" 15 16namespace ui { 17 18namespace { 19 20class HyperlinkButtonCellTest : public CocoaTest { 21 public: 22 HyperlinkButtonCellTest() { 23 NSRect frame = NSMakeRect(0, 0, 50, 30); 24 base::scoped_nsobject<NSButton> view( 25 [[NSButton alloc] initWithFrame:frame]); 26 view_ = view.get(); 27 base::scoped_nsobject<HyperlinkButtonCell> cell( 28 [[HyperlinkButtonCell alloc] initTextCell:@"Testing"]); 29 cell_ = cell.get(); 30 [view_ setCell:cell_]; 31 [[test_window() contentView] addSubview:view_]; 32 } 33 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]); 39 } 40 41 protected: 42 bool HasUnderlineAttribute(NSDictionary* attributes) { 43 NSNumber* number = base::mac::ObjCCastStrict<NSNumber>( 44 [attributes objectForKey:NSUnderlineStyleAttributeName]); 45 return [number unsignedIntegerValue] != 0; 46 } 47 48 NSButton* view_; 49 HyperlinkButtonCell* cell_; 50}; 51 52TEST_VIEW(HyperlinkButtonCellTest, view_) 53 54// Tests the three designated intializers. 55TEST_F(HyperlinkButtonCellTest, Initializers) { 56 TestCellCustomization(cell_); // |-initTextFrame:| 57 base::scoped_nsobject<HyperlinkButtonCell> cell( 58 [[HyperlinkButtonCell alloc] init]); 59 TestCellCustomization(cell.get()); 60 61 // Need to create a dummy archiver to test |-initWithCoder:|. 62 NSData* emptyData = [NSKeyedArchiver archivedDataWithRootObject:@""]; 63 NSCoder* coder = 64 [[[NSKeyedUnarchiver alloc] initForReadingWithData:emptyData] autorelease]; 65 cell.reset([[HyperlinkButtonCell alloc] initWithCoder:coder]); 66 TestCellCustomization(cell); 67} 68 69// Test set color. 70TEST_F(HyperlinkButtonCellTest, SetTextColor) { 71 NSColor* textColor = [NSColor redColor]; 72 EXPECT_NE(textColor, [cell_ textColor]); 73 [cell_ setTextColor:textColor]; 74 EXPECT_EQ(textColor, [cell_ textColor]); 75} 76 77// Test mouse events. 78// TODO(rsesek): See if we can synthesize mouse events to more accurately 79// test this. 80TEST_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]); 86 [NSCursor pop]; 87} 88 89// Test mouse events when button is disabled. { 90TEST_F(HyperlinkButtonCellTest, MouseHoverWhenDisabled) { 91 [cell_ setEnabled:NO]; 92 93 [[NSCursor disappearingItemCursor] push]; // Set a known state. 94 [cell_ mouseEntered:nil]; 95 EXPECT_EQ([NSCursor disappearingItemCursor], [NSCursor currentCursor]); 96 97 [cell_ mouseExited:nil]; 98 EXPECT_EQ([NSCursor disappearingItemCursor], [NSCursor currentCursor]); 99 [NSCursor pop]; 100 [NSCursor pop]; 101} 102 103// Test underline on hover. 104TEST_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])); 110 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])); 117} 118 119TEST_F(HyperlinkButtonCellTest, Copy) { 120 base::scoped_nsobject<HyperlinkButtonCell> cell1([[HyperlinkButtonCell alloc] 121 initTextCell:@"Cell"]); 122 [cell1 setTextColor:[NSColor redColor]]; 123 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]]; 128} 129 130} // namespace 131 132} // namespace ui 133