Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / cocoa / controls / hyperlink_text_view_unittest.mm
blob1aca1a36812388fa8607cd9ac9c62a8696d75863
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 #include "base/mac/scoped_nsobject.h"
6 #include "testing/gtest_mac.h"
7 #import "ui/base/cocoa/controls/hyperlink_text_view.h"
8 #import "ui/gfx/test/ui_cocoa_test_helper.h"
10 namespace {
12 class HyperlinkTextViewTest : public ui::CocoaTest {
13  public:
14   HyperlinkTextViewTest() {
15     NSRect frame = NSMakeRect(0, 0, 50, 50);
16     base::scoped_nsobject<HyperlinkTextView> view(
17         [[HyperlinkTextView alloc] initWithFrame:frame]);
18     view_ = view.get();
19     [[test_window() contentView] addSubview:view_];
20   }
22   NSFont* GetDefaultFont() {
23     return [NSFont labelFontOfSize:
24          [NSFont systemFontSizeForControlSize:NSRegularControlSize]];
25   }
27   NSDictionary* GetDefaultTextAttributes() {
28     const float kTextBaselineShift = -1.0;
29     return @{
30       NSForegroundColorAttributeName : [NSColor blackColor],
31       NSCursorAttributeName : [NSCursor arrowCursor],
32       NSFontAttributeName : GetDefaultFont(),
33       NSBaselineOffsetAttributeName : @(kTextBaselineShift)
34     };
35   }
37   NSMutableDictionary* GetDefaultLinkAttributes() {
38     if (!linkAttributes_.get()) {
39       linkAttributes_.reset(
40           [[NSMutableDictionary dictionaryWithDictionary:
41               GetDefaultTextAttributes()] retain]);
42       [linkAttributes_ addEntriesFromDictionary:@{
43           NSForegroundColorAttributeName : [NSColor blueColor],
44           NSUnderlineStyleAttributeName : @(YES),
45           NSCursorAttributeName : [NSCursor pointingHandCursor],
46           NSUnderlineStyleAttributeName : @(NSSingleUnderlineStyle),
47           NSLinkAttributeName : @""}];
48     }
49     return [NSMutableDictionary dictionaryWithDictionary:linkAttributes_];
50   }
52   HyperlinkTextView* view_;
54  private:
55   base::scoped_nsobject<NSMutableDictionary> linkAttributes_;
58 TEST_VIEW(HyperlinkTextViewTest, view_);
60 TEST_F(HyperlinkTextViewTest, TestViewConfiguration) {
61   EXPECT_FALSE([view_ isEditable]);
62   EXPECT_FALSE([view_ drawsBackground]);
63   EXPECT_FALSE([view_ isHorizontallyResizable]);
64   EXPECT_FALSE([view_ isVerticallyResizable]);
67 TEST_F(HyperlinkTextViewTest, TestSetMessage) {
68   // Verifies setMessage sets text and attributes properly.
69   NSString* message = @"Test message";
70   [view_ setMessage:message
71            withFont:GetDefaultFont()
72        messageColor:[NSColor blackColor]];
73   EXPECT_NSEQ(@"Test message", [[view_ textStorage] string]);
75   NSDictionary* attributes;
76   NSRange rangeLimit = NSMakeRange(0, [message length]);
77   NSRange range;
78   attributes = [[view_ textStorage] attributesAtIndex:0
79                                 longestEffectiveRange:&range
80                                               inRange:rangeLimit];
81   EXPECT_EQ(0U, range.location);
82   EXPECT_EQ([message length], range.length);
83   EXPECT_NSEQ(GetDefaultTextAttributes(), attributes);
86 TEST_F(HyperlinkTextViewTest, TestAddLinkRange) {
87   NSString* message = @"One Two Three Four";
88   [view_ setMessage:message
89            withFont:GetDefaultFont()
90        messageColor:[NSColor blackColor]];
92   NSColor* blue = [NSColor blueColor];
93   [view_ addLinkRange:NSMakeRange(4,3) withName:@"Name:Two" linkColor:blue];
94   [view_ addLinkRange:NSMakeRange(14,4) withName:@"Name:Four" linkColor:blue];
96   NSDictionary* attributes;
97   NSRange rangeLimit = NSMakeRange(0, [message length]);
98   NSRange range;
99   attributes = [[view_ textStorage] attributesAtIndex:0
100                                 longestEffectiveRange:&range
101                                               inRange:rangeLimit];
102   EXPECT_EQ(0U, range.location);
103   EXPECT_EQ(4U, range.length);
104   EXPECT_NSEQ(GetDefaultTextAttributes(), attributes);
106   NSMutableDictionary* linkAttributes = GetDefaultLinkAttributes();
107   [linkAttributes setObject:@"Name:Two" forKey:NSLinkAttributeName];
108   attributes = [[view_ textStorage] attributesAtIndex:4
109                                 longestEffectiveRange:&range
110                                               inRange:rangeLimit];
111   EXPECT_EQ(4U, range.location);
112   EXPECT_EQ(3U, range.length);
113   EXPECT_NSEQ(linkAttributes, attributes);
115   attributes = [[view_ textStorage] attributesAtIndex:7
116                                 longestEffectiveRange:&range
117                                               inRange:rangeLimit];
118   EXPECT_EQ(7U, range.location);
119   EXPECT_EQ(7U, range.length);
120   EXPECT_NSEQ(GetDefaultTextAttributes(), attributes);
122   [linkAttributes setObject:@"Name:Four" forKey:NSLinkAttributeName];
123   attributes = [[view_ textStorage] attributesAtIndex:14
124                                 longestEffectiveRange:&range
125                                               inRange:rangeLimit];
126   EXPECT_EQ(14U, range.location);
127   EXPECT_EQ(4U, range.length);
128   EXPECT_NSEQ(linkAttributes, attributes);
131 TEST_F(HyperlinkTextViewTest, FirstResponderBehavior) {
132   // By default, accept.
133   EXPECT_TRUE([view_ acceptsFirstResponder]);
135   [view_ setRefusesFirstResponder:YES];
136   EXPECT_FALSE([view_ acceptsFirstResponder]);
139 }  // namespace