• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4
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"
9
10namespace {
11
12class 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  }
21
22  NSFont* GetDefaultFont() {
23    return [NSFont labelFontOfSize:
24         [NSFont systemFontSizeForControlSize:NSRegularControlSize]];
25  }
26
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  }
36
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  }
51
52  HyperlinkTextView* view_;
53
54 private:
55  base::scoped_nsobject<NSMutableDictionary> linkAttributes_;
56};
57
58TEST_VIEW(HyperlinkTextViewTest, view_);
59
60TEST_F(HyperlinkTextViewTest, TestViewConfiguration) {
61  EXPECT_FALSE([view_ isEditable]);
62  EXPECT_FALSE([view_ drawsBackground]);
63  EXPECT_FALSE([view_ isHorizontallyResizable]);
64  EXPECT_FALSE([view_ isVerticallyResizable]);
65}
66
67TEST_F(HyperlinkTextViewTest, LinkInsertion) {
68  // Test that setMessage:withLink:... inserts the link text.
69  [view_ setMessageAndLink:@"This is a short text message"
70                  withLink:@"alarmingly "
71                  atOffset:10
72                      font:GetDefaultFont()
73              messageColor:[NSColor blackColor]
74                 linkColor:[NSColor blueColor]];
75  EXPECT_NSEQ(@"This is a alarmingly short text message",
76              [[view_ textStorage] string]);
77
78  // Test insertion at end - most common use case.
79  NSString* message=@"This is another test message ";
80  [view_ setMessageAndLink:message
81                  withLink:@"with link"
82                  atOffset:[message length]
83                      font:GetDefaultFont()
84              messageColor:[NSColor blackColor]
85                 linkColor:[NSColor blueColor]];
86  EXPECT_NSEQ(@"This is another test message with link",
87              [[view_ textStorage] string]);
88}
89
90TEST_F(HyperlinkTextViewTest, AttributesForMessageWithLink) {
91  // Verifies text attributes are set as expected for setMessageWithLink:...
92  [view_ setMessageAndLink:@"aaabbbbb"
93                  withLink:@"xxxx"
94                  atOffset:3
95                      font:GetDefaultFont()
96              messageColor:[NSColor blackColor]
97                 linkColor:[NSColor blueColor]];
98
99  NSDictionary* attributes;
100  NSRange rangeLimit = NSMakeRange(0, 12);
101  NSRange range;
102  attributes = [[view_ textStorage] attributesAtIndex:0
103                                longestEffectiveRange:&range
104                                              inRange:rangeLimit];
105  EXPECT_EQ(0U, range.location);
106  EXPECT_EQ(3U, range.length);
107  EXPECT_NSEQ(GetDefaultTextAttributes(), attributes);
108
109  attributes = [[view_ textStorage] attributesAtIndex:3
110                                longestEffectiveRange:&range
111                                              inRange:rangeLimit];
112  EXPECT_EQ(3U, range.location);
113  EXPECT_EQ(4U, range.length);
114  EXPECT_NSEQ(GetDefaultLinkAttributes(), attributes);
115
116  attributes = [[view_ textStorage] attributesAtIndex:7
117                                longestEffectiveRange:&range
118                                              inRange:rangeLimit];
119  EXPECT_EQ(7U, range.location);
120  EXPECT_EQ(5U, range.length);
121  EXPECT_NSEQ(GetDefaultTextAttributes(), attributes);
122
123}
124
125TEST_F(HyperlinkTextViewTest, TestSetMessage) {
126  // Verifies setMessage sets text and attributes properly.
127  NSString* message = @"Test message";
128  [view_ setMessage:message
129           withFont:GetDefaultFont()
130       messageColor:[NSColor blackColor]];
131  EXPECT_NSEQ(@"Test message", [[view_ textStorage] string]);
132
133  NSDictionary* attributes;
134  NSRange rangeLimit = NSMakeRange(0, [message length]);
135  NSRange range;
136  attributes = [[view_ textStorage] attributesAtIndex:0
137                                longestEffectiveRange:&range
138                                              inRange:rangeLimit];
139  EXPECT_EQ(0U, range.location);
140  EXPECT_EQ([message length], range.length);
141  EXPECT_NSEQ(GetDefaultTextAttributes(), attributes);
142}
143
144TEST_F(HyperlinkTextViewTest, TestAddLinkRange) {
145  NSString* message = @"One Two Three Four";
146  [view_ setMessage:message
147           withFont:GetDefaultFont()
148       messageColor:[NSColor blackColor]];
149
150  NSColor* blue = [NSColor blueColor];
151  [view_ addLinkRange:NSMakeRange(4,3) withName:@"Name:Two" linkColor:blue];
152  [view_ addLinkRange:NSMakeRange(14,4) withName:@"Name:Four" linkColor:blue];
153
154  NSDictionary* attributes;
155  NSRange rangeLimit = NSMakeRange(0, [message length]);
156  NSRange range;
157  attributes = [[view_ textStorage] attributesAtIndex:0
158                                longestEffectiveRange:&range
159                                              inRange:rangeLimit];
160  EXPECT_EQ(0U, range.location);
161  EXPECT_EQ(4U, range.length);
162  EXPECT_NSEQ(GetDefaultTextAttributes(), attributes);
163
164  NSMutableDictionary* linkAttributes = GetDefaultLinkAttributes();
165  [linkAttributes setObject:@"Name:Two" forKey:NSLinkAttributeName];
166  attributes = [[view_ textStorage] attributesAtIndex:4
167                                longestEffectiveRange:&range
168                                              inRange:rangeLimit];
169  EXPECT_EQ(4U, range.location);
170  EXPECT_EQ(3U, range.length);
171  EXPECT_NSEQ(linkAttributes, attributes);
172
173  attributes = [[view_ textStorage] attributesAtIndex:7
174                                longestEffectiveRange:&range
175                                              inRange:rangeLimit];
176  EXPECT_EQ(7U, range.location);
177  EXPECT_EQ(7U, range.length);
178  EXPECT_NSEQ(GetDefaultTextAttributes(), attributes);
179
180  [linkAttributes setObject:@"Name:Four" forKey:NSLinkAttributeName];
181  attributes = [[view_ textStorage] attributesAtIndex:14
182                                longestEffectiveRange:&range
183                                              inRange:rangeLimit];
184  EXPECT_EQ(14U, range.location);
185  EXPECT_EQ(4U, range.length);
186  EXPECT_NSEQ(linkAttributes, attributes);
187}
188
189TEST_F(HyperlinkTextViewTest, FirstResponderBehavior) {
190  // By default, accept.
191  EXPECT_TRUE([view_ acceptsFirstResponder]);
192
193  [view_ setRefusesFirstResponder:YES];
194  EXPECT_FALSE([view_ acceptsFirstResponder]);
195}
196
197}  // namespace
198