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 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
7 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h"
8 #include "testing/gtest_mac.h"
12 class HyperlinkTextViewTest : public CocoaTest {
14 HyperlinkTextViewTest() {
15 NSRect frame = NSMakeRect(0, 0, 50, 50);
16 base::scoped_nsobject<HyperlinkTextView> view(
17 [[HyperlinkTextView alloc] initWithFrame:frame]);
19 [[test_window() contentView] addSubview:view_];
22 NSFont* GetDefaultFont() {
23 return [NSFont labelFontOfSize:
24 [NSFont systemFontSizeForControlSize:NSRegularControlSize]];
27 NSDictionary* GetDefaultTextAttributes() {
28 const float kTextBaselineShift = -1.0;
30 NSForegroundColorAttributeName : [NSColor blackColor],
31 NSCursorAttributeName : [NSCursor arrowCursor],
32 NSFontAttributeName : GetDefaultFont(),
33 NSBaselineOffsetAttributeName : @(kTextBaselineShift)
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 : @""}];
49 return [NSMutableDictionary dictionaryWithDictionary:linkAttributes_];
52 HyperlinkTextView* view_;
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, LinkInsertion) {
68 // Test that setMessage:withLink:... inserts the link text.
69 [view_ setMessageAndLink:@"This is a short text message"
70 withLink:@"alarmingly "
73 messageColor:[NSColor blackColor]
74 linkColor:[NSColor blueColor]];
75 EXPECT_NSEQ(@"This is a alarmingly short text message",
76 [[view_ textStorage] string]);
78 // Test insertion at end - most common use case.
79 NSString* message=@"This is another test message ";
80 [view_ setMessageAndLink:message
82 atOffset:[message length]
84 messageColor:[NSColor blackColor]
85 linkColor:[NSColor blueColor]];
86 EXPECT_NSEQ(@"This is another test message with link",
87 [[view_ textStorage] string]);
90 TEST_F(HyperlinkTextViewTest, AttributesForMessageWithLink) {
91 // Verifies text attributes are set as expected for setMessageWithLink:...
92 [view_ setMessageAndLink:@"aaabbbbb"
96 messageColor:[NSColor blackColor]
97 linkColor:[NSColor blueColor]];
99 NSDictionary* attributes;
100 NSRange rangeLimit = NSMakeRange(0, 12);
102 attributes = [[view_ textStorage] attributesAtIndex:0
103 longestEffectiveRange:&range
105 EXPECT_EQ(0U, range.location);
106 EXPECT_EQ(3U, range.length);
107 EXPECT_NSEQ(GetDefaultTextAttributes(), attributes);
109 attributes = [[view_ textStorage] attributesAtIndex:3
110 longestEffectiveRange:&range
112 EXPECT_EQ(3U, range.location);
113 EXPECT_EQ(4U, range.length);
114 EXPECT_NSEQ(GetDefaultLinkAttributes(), attributes);
116 attributes = [[view_ textStorage] attributesAtIndex:7
117 longestEffectiveRange:&range
119 EXPECT_EQ(7U, range.location);
120 EXPECT_EQ(5U, range.length);
121 EXPECT_NSEQ(GetDefaultTextAttributes(), attributes);
125 TEST_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]);
133 NSDictionary* attributes;
134 NSRange rangeLimit = NSMakeRange(0, [message length]);
136 attributes = [[view_ textStorage] attributesAtIndex:0
137 longestEffectiveRange:&range
139 EXPECT_EQ(0U, range.location);
140 EXPECT_EQ([message length], range.length);
141 EXPECT_NSEQ(GetDefaultTextAttributes(), attributes);
144 TEST_F(HyperlinkTextViewTest, TestAddLinkRange) {
145 NSString* message = @"One Two Three Four";
146 [view_ setMessage:message
147 withFont:GetDefaultFont()
148 messageColor:[NSColor blackColor]];
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];
154 NSDictionary* attributes;
155 NSRange rangeLimit = NSMakeRange(0, [message length]);
157 attributes = [[view_ textStorage] attributesAtIndex:0
158 longestEffectiveRange:&range
160 EXPECT_EQ(0U, range.location);
161 EXPECT_EQ(4U, range.length);
162 EXPECT_NSEQ(GetDefaultTextAttributes(), attributes);
164 NSMutableDictionary* linkAttributes = GetDefaultLinkAttributes();
165 [linkAttributes setObject:@"Name:Two" forKey:NSLinkAttributeName];
166 attributes = [[view_ textStorage] attributesAtIndex:4
167 longestEffectiveRange:&range
169 EXPECT_EQ(4U, range.location);
170 EXPECT_EQ(3U, range.length);
171 EXPECT_NSEQ(linkAttributes, attributes);
173 attributes = [[view_ textStorage] attributesAtIndex:7
174 longestEffectiveRange:&range
176 EXPECT_EQ(7U, range.location);
177 EXPECT_EQ(7U, range.length);
178 EXPECT_NSEQ(GetDefaultTextAttributes(), attributes);
180 [linkAttributes setObject:@"Name:Four" forKey:NSLinkAttributeName];
181 attributes = [[view_ textStorage] attributesAtIndex:14
182 longestEffectiveRange:&range
184 EXPECT_EQ(14U, range.location);
185 EXPECT_EQ(4U, range.length);
186 EXPECT_NSEQ(linkAttributes, attributes);