Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: chrome/browser/cocoa/location_bar/autocomplete_text_field_cell_unittest.mm

Issue 2805070: [Mac] First part of Omnibox decoration refactor. Enable ev bubble. (Closed) Base URL: git://codf21.jail/chromium.git
Patch Set: comment clarification Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import <Cocoa/Cocoa.h> 5 #import <Cocoa/Cocoa.h>
6 6
7 #include "app/resource_bundle.h" 7 #include "app/resource_bundle.h"
8 #include "base/scoped_nsobject.h" 8 #include "base/scoped_nsobject.h"
9 #import "chrome/browser/cocoa/cocoa_test_helper.h" 9 #import "chrome/browser/cocoa/cocoa_test_helper.h"
10 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h" 10 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h"
11 #import "chrome/browser/cocoa/location_bar/ev_bubble_decoration.h"
12 #import "chrome/browser/cocoa/location_bar/location_bar_decoration.h"
13 #import "chrome/browser/cocoa/location_bar/location_icon_decoration.h"
14 #import "chrome/browser/cocoa/location_bar/selected_keyword_decoration.h"
11 #include "grit/theme_resources.h" 15 #include "grit/theme_resources.h"
16 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/platform_test.h" 18 #include "testing/platform_test.h"
14 19
20 using ::testing::StrictMock;
21 using ::testing::_;
22
15 namespace { 23 namespace {
16 24
17 // Width of the field so that we don't have to ask |field_| for it all 25 // Width of the field so that we don't have to ask |field_| for it all
18 // the time. 26 // the time.
19 const CGFloat kWidth(300.0); 27 const CGFloat kWidth(300.0);
20 28
21 // A narrow width for tests which test things that don't fit. 29 // A narrow width for tests which test things that don't fit.
22 const CGFloat kNarrowWidth(5.0); 30 const CGFloat kNarrowWidth(5.0);
23 31
32 class MockDecoration : public LocationBarDecoration {
33 public:
34 virtual CGFloat GetWidthForSpace(CGFloat width) { return 20.0; }
35 virtual bool IsVisible() const { return visible_; }
36 void SetVisible(bool visible) { visible_ = visible; }
37
38 MOCK_METHOD2(DrawInFrame, void(NSRect frame, NSView* control_view));
39
40 bool visible_;
41 };
42
24 // A testing subclass that doesn't bother hooking up the Page Action itself. 43 // A testing subclass that doesn't bother hooking up the Page Action itself.
25 class TestPageActionImageView : public LocationBarViewMac::PageActionImageView { 44 class TestPageActionImageView : public LocationBarViewMac::PageActionImageView {
26 public: 45 public:
27 TestPageActionImageView() {} 46 TestPageActionImageView() {}
28 ~TestPageActionImageView() {} 47 ~TestPageActionImageView() {}
29 }; 48 };
30 49
31 class TestPageActionViewList : public LocationBarViewMac::PageActionViewList { 50 class TestPageActionViewList : public LocationBarViewMac::PageActionViewList {
32 public: 51 public:
33 TestPageActionViewList() 52 TestPageActionViewList()
34 : LocationBarViewMac::PageActionViewList(NULL, NULL, NULL) {} 53 : LocationBarViewMac::PageActionViewList(NULL, NULL, NULL) {}
35 ~TestPageActionViewList() { 54 ~TestPageActionViewList() {
36 // |~PageActionViewList()| calls delete on the contents of 55 // |~PageActionViewList()| calls delete on the contents of
37 // |views_|, which here are refs to stack objects. 56 // |views_|, which here are refs to stack objects.
38 views_.clear(); 57 views_.clear();
39 } 58 }
40 59
41 void Add(LocationBarViewMac::PageActionImageView* view) { 60 void Add(LocationBarViewMac::PageActionImageView* view) {
42 views_.push_back(view); 61 views_.push_back(view);
43 } 62 }
44 }; 63 };
45 64
46 class AutocompleteTextFieldCellTest : public CocoaTest { 65 class AutocompleteTextFieldCellTest : public CocoaTest {
47 public: 66 public:
48 AutocompleteTextFieldCellTest() : location_icon_view_(NULL), 67 AutocompleteTextFieldCellTest() : page_action_views_() {
49 page_action_views_() {
50 // Make sure this is wide enough to play games with the cell 68 // Make sure this is wide enough to play games with the cell
51 // decorations. 69 // decorations.
52 const NSRect frame = NSMakeRect(0, 0, kWidth, 30); 70 const NSRect frame = NSMakeRect(0, 0, kWidth, 30);
53 71
54 scoped_nsobject<NSTextField> view( 72 scoped_nsobject<NSTextField> view(
55 [[NSTextField alloc] initWithFrame:frame]); 73 [[NSTextField alloc] initWithFrame:frame]);
56 view_ = view.get(); 74 view_ = view.get();
57 75
58 scoped_nsobject<AutocompleteTextFieldCell> cell( 76 scoped_nsobject<AutocompleteTextFieldCell> cell(
59 [[AutocompleteTextFieldCell alloc] initTextCell:@"Testing"]); 77 [[AutocompleteTextFieldCell alloc] initTextCell:@"Testing"]);
60 [cell setEditable:YES]; 78 [cell setEditable:YES];
61 [cell setBordered:YES]; 79 [cell setBordered:YES];
62 [cell setLocationIconView:&location_icon_view_];
63 [cell setSecurityLabelView:&security_label_view_];
64 [cell setPageActionViewList:&page_action_views_]; 80 [cell setPageActionViewList:&page_action_views_];
81
82 [cell clearDecorations];
83 mock_left_decoration_.SetVisible(false);
84 [cell addLeftDecoration:&mock_left_decoration_];
85
65 [view_ setCell:cell.get()]; 86 [view_ setCell:cell.get()];
66 87
67 [[test_window() contentView] addSubview:view_]; 88 [[test_window() contentView] addSubview:view_];
68 } 89 }
69 90
70 NSTextField* view_; 91 NSTextField* view_;
71 LocationBarViewMac::LocationIconView location_icon_view_; 92 MockDecoration mock_left_decoration_;
72 LocationBarViewMac::LocationBarImageView security_label_view_;
73 TestPageActionViewList page_action_views_; 93 TestPageActionViewList page_action_views_;
74 }; 94 };
75 95
76 // Basic view tests (AddRemove, Display). 96 // Basic view tests (AddRemove, Display).
77 TEST_VIEW(AutocompleteTextFieldCellTest, view_); 97 TEST_VIEW(AutocompleteTextFieldCellTest, view_);
78 98
79 // Test drawing, mostly to ensure nothing leaks or crashes. 99 // Test drawing, mostly to ensure nothing leaks or crashes.
80 TEST_F(AutocompleteTextFieldCellTest, FocusedDisplay) { 100 TEST_F(AutocompleteTextFieldCellTest, FocusedDisplay) {
81 [view_ display]; 101 [view_ display];
82 102
83 // Test focused drawing. 103 // Test focused drawing.
84 [test_window() makePretendKeyWindowAndSetFirstResponder:view_]; 104 [test_window() makePretendKeyWindowAndSetFirstResponder:view_];
85 [view_ display]; 105 [view_ display];
86 [test_window() clearPretendKeyWindowAndFirstResponder]; 106 [test_window() clearPretendKeyWindowAndFirstResponder];
87 107
88 // Test display of various cell configurations. 108 // Test display of various cell configurations.
89 AutocompleteTextFieldCell* cell = 109 AutocompleteTextFieldCell* cell =
90 static_cast<AutocompleteTextFieldCell*>([view_ cell]); 110 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
91 111
92 [cell setSearchHintString:@"Type to search" availableWidth:kWidth]; 112 [cell setSearchHintString:@"Type to search" availableWidth:kWidth];
93 [view_ display]; 113 [view_ display];
94 114
95 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; 115 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
96 [cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix" 116 [cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix"
97 availableWidth:kWidth]; 117 availableWidth:kWidth];
98 [view_ display]; 118 [view_ display];
99 119
100 [cell setKeywordString:@"Search Engine:" 120 // Load available decorations and try drawing.
101 partialString:@"Search Eng:" 121 SelectedKeywordDecoration selected_keyword_decoration([view_ font]);
102 availableWidth:kWidth]; 122 selected_keyword_decoration.SetVisible(true);
123 selected_keyword_decoration.SetKeyword(std::wstring(L"Google"), false);
124 [cell addLeftDecoration:&selected_keyword_decoration];
125
126 // TODO(shess): This really wants a |LocationBarViewMac|, but only a
127 // few methods reference it, so this works well enough. But
128 // something better would be nice.
129 LocationIconDecoration location_icon_decoration(NULL);
130 location_icon_decoration.SetVisible(true);
131 location_icon_decoration.SetImage([NSImage imageNamed:@"NSApplicationIcon"]);
132 [cell addLeftDecoration:&location_icon_decoration];
133
134 EVBubbleDecoration ev_bubble_decoration(&location_icon_decoration,
135 [view_ font]);
136 ev_bubble_decoration.SetVisible(true);
137 ev_bubble_decoration.SetImage([NSImage imageNamed:@"NSApplicationIcon"]);
138 ev_bubble_decoration.SetLabel(@"Application");
139 [cell addLeftDecoration:&ev_bubble_decoration];
140
141 // Make sure we're actually calling |DrawInFrame()|.
142 StrictMock<MockDecoration> mock_decoration;
143 mock_decoration.SetVisible(true);
144 [cell addLeftDecoration:&mock_decoration];
145 EXPECT_CALL(mock_decoration, DrawInFrame(_, _));
146
103 [view_ display]; 147 [view_ display];
148
149 [cell clearDecorations];
104 } 150 }
105 151
106 // Verify that transitions between states clear other states. 152 TEST_F(AutocompleteTextFieldCellTest, ClearKeywordAndHint) {
107 TEST_F(AutocompleteTextFieldCellTest, StateTransitionsResetOthers) {
108 AutocompleteTextFieldCell* cell = 153 AutocompleteTextFieldCell* cell =
109 static_cast<AutocompleteTextFieldCell*>([view_ cell]); 154 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
110 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; 155 EXPECT_FALSE([cell hintString]);
111 156
112 // Setting hint leaves keyword empty. 157 // Check that the search hint can be cleared.
113 [cell setSearchHintString:@"Type to search" availableWidth:kWidth]; 158 [cell setSearchHintString:@"Type to search" availableWidth:kWidth];
114 EXPECT_TRUE([cell hintString]); 159 EXPECT_TRUE([cell hintString]);
115 EXPECT_FALSE([cell keywordString]); 160 [cell clearHint];
161 EXPECT_FALSE([cell hintString]);
116 162
117 // Setting keyword clears hint. 163 // Check that the keyword hint can be cleared.
118 [cell setKeywordString:@"Search Engine:" 164 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
119 partialString:@"Search Eng:"
120 availableWidth:kWidth];
121 EXPECT_FALSE([cell hintString]);
122 EXPECT_TRUE([cell keywordString]);
123
124 // Setting hint clears keyword.
125 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine" 165 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine"
126 availableWidth:kWidth]; 166 availableWidth:kWidth];
127 EXPECT_TRUE([cell hintString]); 167 EXPECT_TRUE([cell hintString]);
128 EXPECT_FALSE([cell keywordString]); 168 [cell clearHint];
129
130 // Clear clears keyword.
131 [cell clearKeywordAndHint];
132 EXPECT_FALSE([cell hintString]); 169 EXPECT_FALSE([cell hintString]);
133 EXPECT_FALSE([cell keywordString]);
134
135 // Clear clears hint.
136 [cell setSearchHintString:@"Type to search" availableWidth:kWidth];
137 EXPECT_TRUE([cell hintString]);
138 [cell clearKeywordAndHint];
139 EXPECT_FALSE([cell hintString]);
140 EXPECT_FALSE([cell keywordString]);
141 } 170 }
142 171
143 TEST_F(AutocompleteTextFieldCellTest, TextFrame) { 172 TEST_F(AutocompleteTextFieldCellTest, TextFrame) {
144 AutocompleteTextFieldCell* cell = 173 AutocompleteTextFieldCell* cell =
145 static_cast<AutocompleteTextFieldCell*>([view_ cell]); 174 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
146 const NSRect bounds([view_ bounds]); 175 const NSRect bounds([view_ bounds]);
147 NSRect textFrame; 176 NSRect textFrame;
148 177
149 // The cursor frame should stay the same throughout. 178 // The cursor frame should stay the same throughout.
150 const NSRect cursorFrame([cell textCursorFrameForFrame:bounds]); 179 const NSRect cursorFrame([cell textCursorFrameForFrame:bounds]);
(...skipping 24 matching lines...) Expand all
175 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; 204 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
176 [cell setKeywordHintPrefix:@"Keyword " image:image suffix:@" hint" 205 [cell setKeywordHintPrefix:@"Keyword " image:image suffix:@" hint"
177 availableWidth:kWidth]; 206 availableWidth:kWidth];
178 textFrame = [cell textFrameForFrame:bounds]; 207 textFrame = [cell textFrameForFrame:bounds];
179 EXPECT_FALSE(NSIsEmptyRect(textFrame)); 208 EXPECT_FALSE(NSIsEmptyRect(textFrame));
180 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); 209 EXPECT_TRUE(NSContainsRect(bounds, textFrame));
181 EXPECT_LT(NSMaxX(textFrame), NSMaxX(bounds)); 210 EXPECT_LT(NSMaxX(textFrame), NSMaxX(bounds));
182 EXPECT_LT(NSMaxX(textFrame), searchHintMaxX); 211 EXPECT_LT(NSMaxX(textFrame), searchHintMaxX);
183 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame)); 212 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame));
184 213
185 // Keyword search leaves text area to right. 214 // Text frame should take everything over again on reset.
186 [cell setKeywordString:@"Search Engine:" 215 [cell clearHint];
187 partialString:@"Search Eng:"
188 availableWidth:kWidth];
189 textFrame = [cell textFrameForFrame:bounds]; 216 textFrame = [cell textFrameForFrame:bounds];
190 EXPECT_FALSE(NSIsEmptyRect(textFrame)); 217 EXPECT_FALSE(NSIsEmptyRect(textFrame));
191 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); 218 EXPECT_TRUE(NSContainsRect(bounds, textFrame));
192 EXPECT_GT(NSMinX(textFrame), NSMinX(bounds));
193 EXPECT_LT(NSMinX(textFrame), searchHintMaxX);
194 EXPECT_GT(NSMaxX(textFrame), searchHintMaxX);
195 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame));
196
197 // Text frame should take everything over again on reset.
198 [cell clearKeywordAndHint];
199 textFrame = [cell textFrameForFrame:bounds];
200 EXPECT_FALSE(NSIsEmptyRect(textFrame));
201 EXPECT_TRUE(NSContainsRect(bounds, textFrame));
202 EXPECT_EQ(NSMinX(bounds), NSMinX(textFrame)); 219 EXPECT_EQ(NSMinX(bounds), NSMinX(textFrame));
203 EXPECT_EQ(NSMaxX(bounds), NSMaxX(textFrame)); 220 EXPECT_EQ(NSMaxX(bounds), NSMaxX(textFrame));
204 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame)); 221 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame));
205 222
206 // Location icon takes up space on the left
207 location_icon_view_.SetImage(
208 ResourceBundle::GetSharedInstance().GetNSImageNamed(
209 IDR_OMNIBOX_HTTPS_VALID));
210 location_icon_view_.SetVisible(true);
211
212 textFrame = [cell textFrameForFrame:bounds];
213 EXPECT_FALSE(NSIsEmptyRect(textFrame));
214 EXPECT_TRUE(NSContainsRect(bounds, textFrame));
215 EXPECT_GT(NSMinX(textFrame), NSMinX(bounds));
216 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame));
217
218 // Search hint text takes precedence over the hint icon; the text frame 223 // Search hint text takes precedence over the hint icon; the text frame
219 // should be smaller in order to accomodate the text that is wider than 224 // should be smaller in order to accomodate the text that is wider than
220 // the icon. 225 // the icon.
221 [cell setSearchHintString:@"Search hint" availableWidth:kWidth]; 226 [cell setSearchHintString:@"Search hint" availableWidth:kWidth];
222 NSRect textFrameWithHintText = [cell textFrameForFrame:bounds]; 227 NSRect textFrameWithHintText = [cell textFrameForFrame:bounds];
223 EXPECT_TRUE(NSContainsRect(textFrame, textFrameWithHintText)); 228 EXPECT_TRUE(NSContainsRect(textFrame, textFrameWithHintText));
224 EXPECT_LT(NSWidth(textFrameWithHintText), NSWidth(textFrame)); 229 EXPECT_LT(NSWidth(textFrameWithHintText), NSWidth(textFrame));
230
231 // Decoration on the left takes up space.
232 mock_left_decoration_.SetVisible(true);
233 textFrame = [cell textFrameForFrame:bounds];
234 EXPECT_FALSE(NSIsEmptyRect(textFrame));
235 EXPECT_TRUE(NSContainsRect(bounds, textFrame));
236 EXPECT_GT(NSMinX(textFrame), NSMinX(bounds));
237 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame));
225 } 238 }
226 239
227 // The editor frame should be slightly inset from the text frame. 240 // The editor frame should be slightly inset from the text frame.
228 TEST_F(AutocompleteTextFieldCellTest, DrawingRectForBounds) { 241 TEST_F(AutocompleteTextFieldCellTest, DrawingRectForBounds) {
229 AutocompleteTextFieldCell* cell = 242 AutocompleteTextFieldCell* cell =
230 static_cast<AutocompleteTextFieldCell*>([view_ cell]); 243 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
231 const NSRect bounds([view_ bounds]); 244 const NSRect bounds([view_ bounds]);
232 NSRect textFrame, drawingRect; 245 NSRect textFrame, drawingRect;
233 246
234 textFrame = [cell textFrameForFrame:bounds]; 247 textFrame = [cell textFrameForFrame:bounds];
(...skipping 13 matching lines...) Expand all
248 EXPECT_TRUE(NSContainsRect(textFrame, NSInsetRect(drawingRect, 1, 1))); 261 EXPECT_TRUE(NSContainsRect(textFrame, NSInsetRect(drawingRect, 1, 1)));
249 262
250 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; 263 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
251 [cell setKeywordHintPrefix:@"Keyword " image:image suffix:@" hint" 264 [cell setKeywordHintPrefix:@"Keyword " image:image suffix:@" hint"
252 availableWidth:kWidth]; 265 availableWidth:kWidth];
253 textFrame = [cell textFrameForFrame:bounds]; 266 textFrame = [cell textFrameForFrame:bounds];
254 drawingRect = [cell drawingRectForBounds:bounds]; 267 drawingRect = [cell drawingRectForBounds:bounds];
255 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); 268 EXPECT_FALSE(NSIsEmptyRect(drawingRect));
256 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); 269 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect));
257 270
258 [cell setKeywordString:@"Search Engine:" 271 [cell clearHint];
259 partialString:@"Search Eng:"
260 availableWidth:kWidth];
261 textFrame = [cell textFrameForFrame:bounds];
262 drawingRect = [cell drawingRectForBounds:bounds];
263 EXPECT_FALSE(NSIsEmptyRect(drawingRect));
264 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect));
265
266 [cell clearKeywordAndHint];
267 textFrame = [cell textFrameForFrame:bounds]; 272 textFrame = [cell textFrameForFrame:bounds];
268 drawingRect = [cell drawingRectForBounds:bounds]; 273 drawingRect = [cell drawingRectForBounds:bounds];
269 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); 274 EXPECT_FALSE(NSIsEmptyRect(drawingRect));
270 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); 275 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect));
271 EXPECT_TRUE(NSEqualRects(drawingRect, originalDrawingRect)); 276 EXPECT_TRUE(NSEqualRects(drawingRect, originalDrawingRect));
272 277
273 location_icon_view_.SetImage( 278 mock_left_decoration_.SetVisible(true);
274 ResourceBundle::GetSharedInstance().GetNSImageNamed(
275 IDR_OMNIBOX_HTTPS_VALID));
276 location_icon_view_.SetVisible(true);
277
278 textFrame = [cell textFrameForFrame:bounds]; 279 textFrame = [cell textFrameForFrame:bounds];
279 drawingRect = [cell drawingRectForBounds:bounds]; 280 drawingRect = [cell drawingRectForBounds:bounds];
280 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); 281 EXPECT_FALSE(NSIsEmptyRect(drawingRect));
281 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); 282 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect));
282 } 283 }
283 284
284 // Test that the location icon is at the right side of the cell. 285 // Test that left decorations are at the correct edge of the cell.
285 TEST_F(AutocompleteTextFieldCellTest, LocationIconFrame) { 286 TEST_F(AutocompleteTextFieldCellTest, LeftDecorationFrame) {
286 AutocompleteTextFieldCell* cell = 287 AutocompleteTextFieldCell* cell =
287 static_cast<AutocompleteTextFieldCell*>([view_ cell]); 288 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
288 const NSRect bounds([view_ bounds]); 289 const NSRect bounds = [view_ bounds];
289 location_icon_view_.SetImage(
290 ResourceBundle::GetSharedInstance().GetNSImageNamed(
291 IDR_OMNIBOX_HTTPS_VALID));
292 290
293 location_icon_view_.SetVisible(true); 291 mock_left_decoration_.SetVisible(true);
294 const NSRect iconRect = [cell locationIconFrameForFrame:bounds]; 292 const NSRect decorationRect =
295 EXPECT_FALSE(NSIsEmptyRect(iconRect)); 293 [cell frameForDecoration:&mock_left_decoration_ inFrame:bounds];
296 EXPECT_TRUE(NSContainsRect(bounds, iconRect)); 294 EXPECT_FALSE(NSIsEmptyRect(decorationRect));
295 EXPECT_TRUE(NSContainsRect(bounds, decorationRect));
297 296
298 // Location icon should be left of |drawingRect|. 297 // Decoration should be left of |drawingRect|.
299 const NSRect drawingRect = [cell drawingRectForBounds:bounds]; 298 const NSRect drawingRect = [cell drawingRectForBounds:bounds];
300 EXPECT_GT(NSMinX(drawingRect), NSMinX(iconRect)); 299 EXPECT_GT(NSMinX(drawingRect), NSMinX(decorationRect));
301 300
302 // Location icon should be left of |textFrame|. 301 // Decoration should be left of |textFrame|.
303 const NSRect textFrame = [cell textFrameForFrame:bounds]; 302 const NSRect textFrame = [cell textFrameForFrame:bounds];
304 EXPECT_GT(NSMinX(textFrame), NSMinX(iconRect)); 303 EXPECT_GT(NSMinX(textFrame), NSMinX(decorationRect));
305 }
306
307 // Test that security label takes space to the right.
308 TEST_F(AutocompleteTextFieldCellTest, SecurityLabelFrame) {
309 AutocompleteTextFieldCell* cell =
310 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
311 const NSRect bounds([view_ bounds]);
312
313 // No label shows nothing, regardless of visibility setting.
314 security_label_view_.SetVisible(false);
315 const NSRect baseTextFrame = [cell textFrameForFrame:bounds];
316 security_label_view_.SetVisible(true);
317 EXPECT_TRUE(NSEqualRects(baseTextFrame, [cell textFrameForFrame:bounds]));
318
319 // Still not visible even with a label.
320 NSFont* font = [NSFont controlContentFontOfSize:12.0];
321 NSColor* color = [NSColor blackColor];
322 security_label_view_.SetLabel(@"Label", font, color);
323 security_label_view_.SetVisible(false);
324 EXPECT_TRUE(NSEqualRects(baseTextFrame, [cell textFrameForFrame:bounds]));
325
326 // Visible with a label is strictly narrower than without.
327 security_label_view_.SetVisible(true);
328 NSRect textFrame = [cell textFrameForFrame:bounds];
329 const CGFloat labelWidth = [security_label_view_.GetLabel() size].width;
330 EXPECT_TRUE(NSContainsRect(baseTextFrame, textFrame));
331 EXPECT_LT(NSWidth(textFrame), NSWidth(baseTextFrame) - labelWidth);
332
333 NSString* longLabel =
334 @"Really super-long labels will not show up if there's not enough room.";
335 security_label_view_.SetLabel(longLabel, font, color);
336 textFrame = [cell textFrameForFrame:bounds];
337 EXPECT_TRUE(NSEqualRects(baseTextFrame, [cell textFrameForFrame:bounds]));
338 } 304 }
339 305
340 // Test Page Action counts. 306 // Test Page Action counts.
341 TEST_F(AutocompleteTextFieldCellTest, PageActionCount) { 307 TEST_F(AutocompleteTextFieldCellTest, PageActionCount) {
342 AutocompleteTextFieldCell* cell = 308 AutocompleteTextFieldCell* cell =
343 static_cast<AutocompleteTextFieldCell*>([view_ cell]); 309 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
344 310
345 TestPageActionImageView page_action_view; 311 TestPageActionImageView page_action_view;
346 TestPageActionImageView page_action_view2; 312 TestPageActionImageView page_action_view2;
347 313
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 EXPECT_TRUE(NSContainsRect(bounds, iconRect1)); 388 EXPECT_TRUE(NSContainsRect(bounds, iconRect1));
423 EXPECT_FALSE(NSIsEmptyRect(labelRect)); 389 EXPECT_FALSE(NSIsEmptyRect(labelRect));
424 EXPECT_TRUE(NSContainsRect(bounds, labelRect)); 390 EXPECT_TRUE(NSContainsRect(bounds, labelRect));
425 391
426 EXPECT_LE(NSMaxX(drawingRect), NSMinX(iconRect1)); 392 EXPECT_LE(NSMaxX(drawingRect), NSMinX(iconRect1));
427 EXPECT_LE(NSMaxX(textFrame), NSMinX(iconRect1)); 393 EXPECT_LE(NSMaxX(textFrame), NSMinX(iconRect1));
428 EXPECT_LE(NSMaxX(iconRect1), NSMinX(iconRect0)); 394 EXPECT_LE(NSMaxX(iconRect1), NSMinX(iconRect0));
429 EXPECT_LE(NSMaxX(labelRect), NSMinX(iconRect0)); 395 EXPECT_LE(NSMaxX(labelRect), NSMinX(iconRect0));
430 } 396 }
431 397
432 // Test that the cell correctly chooses the partial keyword if there's
433 // not enough room.
434 TEST_F(AutocompleteTextFieldCellTest, UsesPartialKeywordIfNarrow) {
435 AutocompleteTextFieldCell* cell =
436 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
437
438 NSString* const kFullString = @"Search Engine:";
439 NSString* const kPartialString = @"Search Eng:";
440
441 // Wide width chooses the full string, including an image on the
442 // left.
443 [cell setKeywordString:kFullString
444 partialString:kPartialString
445 availableWidth:kWidth];
446 EXPECT_TRUE([cell keywordString]);
447 EXPECT_TRUE([[[cell keywordString] string] hasSuffix:kFullString]);
448 EXPECT_TRUE([[cell keywordString] containsAttachments]);
449
450 // If not enough space to include the image, uses exactly the full
451 // string.
452 CGFloat allWidth = [[cell keywordString] size].width;
453 [cell setKeywordString:kFullString
454 partialString:kPartialString
455 availableWidth:allWidth - 5.0];
456 EXPECT_TRUE([cell keywordString]);
457 EXPECT_TRUE([[[cell keywordString] string] isEqualToString:kFullString]);
458
459 // Narrow width chooses the partial string.
460 [cell setKeywordString:kFullString
461 partialString:kPartialString
462 availableWidth:kNarrowWidth];
463 EXPECT_TRUE([cell keywordString]);
464 EXPECT_TRUE([[[cell keywordString] string] isEqualToString:kPartialString]);
465
466 // But not if there isn't one!
467 [cell setKeywordString:kFullString
468 partialString:nil
469 availableWidth:kNarrowWidth];
470 EXPECT_TRUE([cell keywordString]);
471 EXPECT_TRUE([[[cell keywordString] string] isEqualToString:kFullString]);
472 }
473
474 // Test that the cell drops the search hint if there is no room for 398 // Test that the cell drops the search hint if there is no room for
475 // it. 399 // it.
476 TEST_F(AutocompleteTextFieldCellTest, OmitsSearchHintIfNarrow) { 400 TEST_F(AutocompleteTextFieldCellTest, OmitsSearchHintIfNarrow) {
477 AutocompleteTextFieldCell* cell = 401 AutocompleteTextFieldCell* cell =
478 static_cast<AutocompleteTextFieldCell*>([view_ cell]); 402 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
479 403
480 NSString* const kSearchHint = @"Type to search"; 404 NSString* const kSearchHint = @"Type to search";
481 405
482 // Wide width chooses the full string. 406 // Wide width chooses the full string.
483 [cell setSearchHintString:kSearchHint availableWidth:kWidth]; 407 [cell setSearchHintString:kSearchHint availableWidth:kWidth];
(...skipping 23 matching lines...) Expand all
507 EXPECT_TRUE([[[cell hintString] string] hasPrefix:kHintPrefix]); 431 EXPECT_TRUE([[[cell hintString] string] hasPrefix:kHintPrefix]);
508 EXPECT_TRUE([[[cell hintString] string] hasSuffix:kHintSuffix]); 432 EXPECT_TRUE([[[cell hintString] string] hasSuffix:kHintSuffix]);
509 433
510 // Narrow width suppresses everything but the image. 434 // Narrow width suppresses everything but the image.
511 [cell setKeywordHintPrefix:kHintPrefix image:image suffix:kHintSuffix 435 [cell setKeywordHintPrefix:kHintPrefix image:image suffix:kHintSuffix
512 availableWidth:kNarrowWidth]; 436 availableWidth:kNarrowWidth];
513 EXPECT_EQ([[cell hintString] length], 1U); 437 EXPECT_EQ([[cell hintString] length], 1U);
514 } 438 }
515 439
516 } // namespace 440 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698