Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / cocoa / controls / hyperlink_button_cell_unittest.mm
blob3e7c611011089da8cdf90a6c73ee07ee6d12a010
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"
16 namespace ui {
18 namespace {
20 class 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   }
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   }
41  protected:
42   bool HasUnderlineAttribute(NSDictionary* attributes) {
43     NSNumber* number = base::mac::ObjCCastStrict<NSNumber>(
44         [attributes objectForKey:NSUnderlineStyleAttributeName]);
45     return [number unsignedIntegerValue] != 0;
46   }
48   NSButton* view_;
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:@""];
63   NSCoder* coder =
64     [[[NSKeyedUnarchiver alloc] initForReadingWithData:emptyData] autorelease];
65   cell.reset([[HyperlinkButtonCell alloc] initWithCoder:coder]);
66   TestCellCustomization(cell);
69 // Test set color.
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]);
77 // Test mouse events.
78 // TODO(rsesek): See if we can synthesize mouse events to more accurately
79 // test this.
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]);
86   [NSCursor pop];
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]);
99   [NSCursor pop];
100   [NSCursor pop];
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]];
130 }  // namespace
132 }  // namespace ui