OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/scoped_nsobject.h" | 7 #include "base/scoped_nsobject.h" |
8 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h" | 8 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h" |
9 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 9 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "testing/platform_test.h" | 11 #include "testing/platform_test.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Width of the field so that we don't have to ask |field_| for it all | 15 // Width of the field so that we don't have to ask |field_| for it all |
16 // the time. | 16 // the time. |
17 const CGFloat kWidth(300.0); | 17 const CGFloat kWidth(300.0); |
18 | 18 |
19 // A narrow width for tests which test things that don't fit. | 19 // A narrow width for tests which test things that don't fit. |
20 const CGFloat kNarrowWidth(5.0); | 20 const CGFloat kNarrowWidth(5.0); |
21 | 21 |
22 class AutocompleteTextFieldCellTest : public PlatformTest { | 22 class AutocompleteTextFieldCellTest : public PlatformTest { |
23 public: | 23 public: |
24 AutocompleteTextFieldCellTest() { | 24 AutocompleteTextFieldCellTest() : security_image_view_(NULL, NULL) { |
25 // Make sure this is wide enough to play games with the cell | 25 // Make sure this is wide enough to play games with the cell |
26 // decorations. | 26 // decorations. |
27 const NSRect frame = NSMakeRect(0, 0, kWidth, 30); | 27 const NSRect frame = NSMakeRect(0, 0, kWidth, 30); |
28 view_.reset([[NSTextField alloc] initWithFrame:frame]); | 28 view_.reset([[NSTextField alloc] initWithFrame:frame]); |
29 scoped_nsobject<AutocompleteTextFieldCell> cell( | 29 scoped_nsobject<AutocompleteTextFieldCell> cell( |
30 [[AutocompleteTextFieldCell alloc] initTextCell:@"Testing"]); | 30 [[AutocompleteTextFieldCell alloc] initTextCell:@"Testing"]); |
31 [cell setEditable:YES]; | 31 [cell setEditable:YES]; |
32 [cell setBordered:YES]; | 32 [cell setBordered:YES]; |
33 [view_ setCell:cell.get()]; | 33 [view_ setCell:cell.get()]; |
| 34 [cell setSecurityImageView:&security_image_view_]; |
34 [cocoa_helper_.contentView() addSubview:view_.get()]; | 35 [cocoa_helper_.contentView() addSubview:view_.get()]; |
35 } | 36 } |
36 | 37 |
37 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... | 38 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... |
38 scoped_nsobject<NSTextField> view_; | 39 scoped_nsobject<NSTextField> view_; |
| 40 LocationBarViewMac::SecurityImageView security_image_view_; |
39 }; | 41 }; |
40 | 42 |
41 // Test adding/removing from the view hierarchy, mostly to ensure nothing | 43 // Test adding/removing from the view hierarchy, mostly to ensure nothing |
42 // leaks or crashes. | 44 // leaks or crashes. |
43 TEST_F(AutocompleteTextFieldCellTest, AddRemove) { | 45 TEST_F(AutocompleteTextFieldCellTest, AddRemove) { |
44 EXPECT_EQ(cocoa_helper_.contentView(), [view_ superview]); | 46 EXPECT_EQ(cocoa_helper_.contentView(), [view_ superview]); |
45 [view_.get() removeFromSuperview]; | 47 [view_.get() removeFromSuperview]; |
46 EXPECT_FALSE([view_ superview]); | 48 EXPECT_FALSE([view_ superview]); |
47 } | 49 } |
48 | 50 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 168 |
167 // Text frame should take everything over again on reset. | 169 // Text frame should take everything over again on reset. |
168 [cell clearKeywordAndHint]; | 170 [cell clearKeywordAndHint]; |
169 textFrame = [cell textFrameForFrame:bounds]; | 171 textFrame = [cell textFrameForFrame:bounds]; |
170 EXPECT_FALSE(NSIsEmptyRect(textFrame)); | 172 EXPECT_FALSE(NSIsEmptyRect(textFrame)); |
171 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); | 173 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); |
172 EXPECT_EQ(NSMinX(bounds), NSMinX(textFrame)); | 174 EXPECT_EQ(NSMinX(bounds), NSMinX(textFrame)); |
173 EXPECT_EQ(NSMaxX(bounds), NSMaxX(textFrame)); | 175 EXPECT_EQ(NSMaxX(bounds), NSMaxX(textFrame)); |
174 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame)); | 176 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame)); |
175 | 177 |
176 // Hint icon takes up space on the right | 178 // Security icon takes up space on the right |
177 [cell setHintIcon:[NSImage imageNamed:@"NSComputer"] label:nil color:nil]; | 179 security_image_view_.SetImageShown( |
| 180 LocationBarViewMac::SecurityImageView::LOCK); |
| 181 security_image_view_.SetVisible(true); |
| 182 |
178 textFrame = [cell textFrameForFrame:bounds]; | 183 textFrame = [cell textFrameForFrame:bounds]; |
179 EXPECT_FALSE(NSIsEmptyRect(textFrame)); | 184 EXPECT_FALSE(NSIsEmptyRect(textFrame)); |
180 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); | 185 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); |
181 EXPECT_LT(NSMaxX(textFrame), NSMaxX(bounds)); | 186 EXPECT_LT(NSMaxX(textFrame), NSMaxX(bounds)); |
182 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame)); | 187 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame)); |
183 | 188 |
184 // Search hint text takes precedence over the hint icon; the text frame | 189 // Search hint text takes precedence over the hint icon; the text frame |
185 // should be smaller in order to accomodate the text that is wider than | 190 // should be smaller in order to accomodate the text that is wider than |
186 // the icon. | 191 // the icon. |
187 [cell setSearchHintString:@"Search hint" availableWidth:kWidth]; | 192 [cell setSearchHintString:@"Search hint" availableWidth:kWidth]; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); | 234 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); |
230 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); | 235 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); |
231 | 236 |
232 [cell clearKeywordAndHint]; | 237 [cell clearKeywordAndHint]; |
233 textFrame = [cell textFrameForFrame:bounds]; | 238 textFrame = [cell textFrameForFrame:bounds]; |
234 drawingRect = [cell drawingRectForBounds:bounds]; | 239 drawingRect = [cell drawingRectForBounds:bounds]; |
235 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); | 240 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); |
236 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); | 241 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); |
237 EXPECT_TRUE(NSEqualRects(drawingRect, originalDrawingRect)); | 242 EXPECT_TRUE(NSEqualRects(drawingRect, originalDrawingRect)); |
238 | 243 |
239 [cell setHintIcon:[NSImage imageNamed:@"NSComputer"] label:nil color:nil]; | 244 security_image_view_.SetImageShown( |
| 245 LocationBarViewMac::SecurityImageView::LOCK); |
| 246 security_image_view_.SetVisible(true); |
| 247 |
240 textFrame = [cell textFrameForFrame:bounds]; | 248 textFrame = [cell textFrameForFrame:bounds]; |
241 drawingRect = [cell drawingRectForBounds:bounds]; | 249 drawingRect = [cell drawingRectForBounds:bounds]; |
242 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); | 250 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); |
243 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); | 251 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); |
244 } | 252 } |
245 | 253 |
246 // Test that the security icon is at the right side of the cell. | 254 // Test that the security icon is at the right side of the cell. |
247 TEST_F(AutocompleteTextFieldCellTest, HintImageFrame) { | 255 TEST_F(AutocompleteTextFieldCellTest, HintImageFrame) { |
248 AutocompleteTextFieldCell* cell = | 256 AutocompleteTextFieldCell* cell = |
249 static_cast<AutocompleteTextFieldCell*>([view_ cell]); | 257 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
250 const NSRect bounds([view_ bounds]); | 258 const NSRect bounds([view_ bounds]); |
251 scoped_nsobject<NSImage> hintIcon( | 259 security_image_view_.SetImageShown( |
252 [[NSImage alloc] initWithSize:NSMakeSize(20, 20)]); | 260 LocationBarViewMac::SecurityImageView::LOCK); |
253 | 261 |
254 NSRect iconRect = [cell hintImageFrameForFrame:bounds]; | 262 security_image_view_.SetVisible(false); |
| 263 NSRect iconRect = [cell securityImageFrameForFrame:bounds]; |
255 EXPECT_TRUE(NSIsEmptyRect(iconRect)); | 264 EXPECT_TRUE(NSIsEmptyRect(iconRect)); |
256 | 265 |
257 // Save the starting frame for after clear. | 266 // Save the starting frame for after clear. |
258 const NSRect originalIconRect(iconRect); | 267 const NSRect originalIconRect(iconRect); |
259 | 268 |
260 [cell setHintIcon:hintIcon label:nil color:nil]; | 269 security_image_view_.SetVisible(true); |
261 iconRect = [cell hintImageFrameForFrame:bounds]; | 270 iconRect = [cell securityImageFrameForFrame:bounds]; |
| 271 |
262 EXPECT_FALSE(NSIsEmptyRect(iconRect)); | 272 EXPECT_FALSE(NSIsEmptyRect(iconRect)); |
263 EXPECT_TRUE(NSContainsRect(bounds, iconRect)); | 273 EXPECT_TRUE(NSContainsRect(bounds, iconRect)); |
264 | 274 |
265 // Make sure we are right of the |drawingRect|. | 275 // Make sure we are right of the |drawingRect|. |
266 NSRect drawingRect = [cell drawingRectForBounds:bounds]; | 276 NSRect drawingRect = [cell drawingRectForBounds:bounds]; |
267 EXPECT_LE(NSMaxX(drawingRect), NSMinX(iconRect)); | 277 EXPECT_LE(NSMaxX(drawingRect), NSMinX(iconRect)); |
268 | 278 |
269 // Make sure we're right of the |textFrame|. | 279 // Make sure we're right of the |textFrame|. |
270 NSRect textFrame = [cell textFrameForFrame:bounds]; | 280 NSRect textFrame = [cell textFrameForFrame:bounds]; |
271 EXPECT_LE(NSMaxX(textFrame), NSMinX(iconRect)); | 281 EXPECT_LE(NSMaxX(textFrame), NSMinX(iconRect)); |
272 | 282 |
| 283 // Now add a label. |
| 284 NSFont* font = [NSFont controlContentFontOfSize:12.0]; |
| 285 NSColor* color = [NSColor blackColor]; |
| 286 security_image_view_.SetLabel(@"Label", font, color); |
| 287 iconRect = [cell securityImageFrameForFrame:bounds]; |
| 288 |
| 289 EXPECT_FALSE(NSIsEmptyRect(iconRect)); |
| 290 EXPECT_TRUE(NSContainsRect(bounds, iconRect)); |
| 291 |
| 292 // Make sure we are right of the |drawingRect|. |
| 293 drawingRect = [cell drawingRectForBounds:bounds]; |
| 294 EXPECT_LE(NSMaxX(drawingRect), NSMinX(iconRect)); |
| 295 |
| 296 // Make sure we're right of the |textFrame|. |
| 297 textFrame = [cell textFrameForFrame:bounds]; |
| 298 EXPECT_LE(NSMaxX(textFrame), NSMinX(iconRect)); |
| 299 |
273 // Make sure we clear correctly. | 300 // Make sure we clear correctly. |
274 [cell setHintIcon:nil label:nil color:nil]; | 301 security_image_view_.SetVisible(false); |
275 iconRect = [cell hintImageFrameForFrame:bounds]; | 302 iconRect = [cell securityImageFrameForFrame:bounds]; |
276 EXPECT_TRUE(NSEqualRects(iconRect, originalIconRect)); | 303 EXPECT_TRUE(NSEqualRects(iconRect, originalIconRect)); |
277 EXPECT_TRUE(NSIsEmptyRect(iconRect)); | 304 EXPECT_TRUE(NSIsEmptyRect(iconRect)); |
278 } | 305 } |
279 | 306 |
280 // Test that the cell correctly chooses the partial keyword if there's | 307 // Test that the cell correctly chooses the partial keyword if there's |
281 // no enough room. | 308 // no enough room. |
282 TEST_F(AutocompleteTextFieldCellTest, UsesPartialKeywordIfNarrow) { | 309 TEST_F(AutocompleteTextFieldCellTest, UsesPartialKeywordIfNarrow) { |
283 AutocompleteTextFieldCell* cell = | 310 AutocompleteTextFieldCell* cell = |
284 static_cast<AutocompleteTextFieldCell*>([view_ cell]); | 311 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
285 | 312 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 EXPECT_TRUE([[[cell hintString] string] hasPrefix:kHintPrefix]); | 371 EXPECT_TRUE([[[cell hintString] string] hasPrefix:kHintPrefix]); |
345 EXPECT_TRUE([[[cell hintString] string] hasSuffix:kHintSuffix]); | 372 EXPECT_TRUE([[[cell hintString] string] hasSuffix:kHintSuffix]); |
346 | 373 |
347 // Narrow width suppresses everything but the image. | 374 // Narrow width suppresses everything but the image. |
348 [cell setKeywordHintPrefix:kHintPrefix image:image suffix:kHintSuffix | 375 [cell setKeywordHintPrefix:kHintPrefix image:image suffix:kHintSuffix |
349 availableWidth:kNarrowWidth]; | 376 availableWidth:kNarrowWidth]; |
350 EXPECT_EQ([[cell hintString] length], 1U); | 377 EXPECT_EQ([[cell hintString] length], 1U); |
351 } | 378 } |
352 | 379 |
353 } // namespace | 380 } // namespace |
OLD | NEW |