OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 <Cocoa/Cocoa.h> | |
6 | |
7 #include "base/mac/foundation_util.h" | |
8 #include "base/mac/scoped_nsobject.h" | |
9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | |
10 #import "chrome/browser/ui/cocoa/hyperlink_button_cell.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 #include "testing/platform_test.h" | |
13 | |
14 class HyperlinkButtonCellTest : public CocoaTest { | |
15 public: | |
16 HyperlinkButtonCellTest() { | |
17 NSRect frame = NSMakeRect(0, 0, 50, 30); | |
18 base::scoped_nsobject<NSButton> view( | |
19 [[NSButton alloc] initWithFrame:frame]); | |
20 view_ = view.get(); | |
21 base::scoped_nsobject<HyperlinkButtonCell> cell( | |
22 [[HyperlinkButtonCell alloc] initTextCell:@"Testing"]); | |
23 cell_ = cell.get(); | |
24 [view_ setCell:cell_]; | |
25 [[test_window() contentView] addSubview:view_]; | |
26 } | |
27 | |
28 void TestCellCustomization(HyperlinkButtonCell* cell) { | |
29 EXPECT_FALSE([cell isBordered]); | |
30 EXPECT_EQ(NSNoCellMask, [cell_ highlightsBy]); | |
31 EXPECT_TRUE([cell showsBorderOnlyWhileMouseInside]); | |
32 EXPECT_TRUE([cell textColor]); | |
33 } | |
34 | |
35 protected: | |
36 bool HasUnderlineAttribute(NSDictionary* attributes) { | |
37 NSNumber* number = base::mac::ObjCCastStrict<NSNumber>( | |
38 [attributes objectForKey:NSUnderlineStyleAttributeName]); | |
39 return [number unsignedIntegerValue] != 0; | |
40 } | |
41 | |
42 NSButton* view_; | |
43 HyperlinkButtonCell* cell_; | |
44 }; | |
45 | |
46 TEST_VIEW(HyperlinkButtonCellTest, view_) | |
47 | |
48 // Tests the three designated intializers. | |
49 TEST_F(HyperlinkButtonCellTest, Initializers) { | |
50 TestCellCustomization(cell_); // |-initTextFrame:| | |
51 base::scoped_nsobject<HyperlinkButtonCell> cell( | |
52 [[HyperlinkButtonCell alloc] init]); | |
53 TestCellCustomization(cell.get()); | |
54 | |
55 // Need to create a dummy archiver to test |-initWithCoder:|. | |
56 NSData* emptyData = [NSKeyedArchiver archivedDataWithRootObject:@""]; | |
57 NSCoder* coder = | |
58 [[[NSKeyedUnarchiver alloc] initForReadingWithData:emptyData] autorelease]; | |
59 cell.reset([[HyperlinkButtonCell alloc] initWithCoder:coder]); | |
60 TestCellCustomization(cell); | |
61 } | |
62 | |
63 // Test set color. | |
64 TEST_F(HyperlinkButtonCellTest, SetTextColor) { | |
65 NSColor* textColor = [NSColor redColor]; | |
66 EXPECT_NE(textColor, [cell_ textColor]); | |
67 [cell_ setTextColor:textColor]; | |
68 EXPECT_EQ(textColor, [cell_ textColor]); | |
69 } | |
70 | |
71 // Test mouse events. | |
72 // TODO(rsesek): See if we can synthesize mouse events to more accurately | |
73 // test this. | |
74 TEST_F(HyperlinkButtonCellTest, MouseHover) { | |
75 [[NSCursor disappearingItemCursor] push]; // Set a known state. | |
76 [cell_ mouseEntered:nil]; | |
77 EXPECT_EQ([NSCursor pointingHandCursor], [NSCursor currentCursor]); | |
78 [cell_ mouseExited:nil]; | |
79 EXPECT_EQ([NSCursor disappearingItemCursor], [NSCursor currentCursor]); | |
80 [NSCursor pop]; | |
81 } | |
82 | |
83 // Test mouse events when button is disabled. { | |
84 TEST_F(HyperlinkButtonCellTest, MouseHoverWhenDisabled) { | |
85 [cell_ setEnabled:NO]; | |
86 | |
87 [[NSCursor disappearingItemCursor] push]; // Set a known state. | |
88 [cell_ mouseEntered:nil]; | |
89 EXPECT_EQ([NSCursor disappearingItemCursor], [NSCursor currentCursor]); | |
90 | |
91 [cell_ mouseExited:nil]; | |
92 EXPECT_EQ([NSCursor disappearingItemCursor], [NSCursor currentCursor]); | |
93 [NSCursor pop]; | |
94 [NSCursor pop]; | |
95 } | |
96 | |
97 // Test underline on hover. | |
98 TEST_F(HyperlinkButtonCellTest, UnderlineOnHover) { | |
99 EXPECT_TRUE(HasUnderlineAttribute([cell_ linkAttributes])); | |
100 [cell_ mouseEntered:nil]; | |
101 EXPECT_TRUE(HasUnderlineAttribute([cell_ linkAttributes])); | |
102 [cell_ mouseExited:nil]; | |
103 EXPECT_TRUE(HasUnderlineAttribute([cell_ linkAttributes])); | |
104 | |
105 [cell_ setUnderlineOnHover:YES]; | |
106 EXPECT_FALSE(HasUnderlineAttribute([cell_ linkAttributes])); | |
107 [cell_ mouseEntered:nil]; | |
108 EXPECT_TRUE(HasUnderlineAttribute([cell_ linkAttributes])); | |
109 [cell_ mouseExited:nil]; | |
110 EXPECT_FALSE(HasUnderlineAttribute([cell_ linkAttributes])); | |
111 } | |
OLD | NEW |