| 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 // A testing subclass that doesn't bother hooking up the Page Action itself. |
| 23 class TestPageActionImageView : public LocationBarViewMac::PageActionImageView { |
| 24 public: |
| 25 TestPageActionImageView() {} |
| 26 ~TestPageActionImageView() {} |
| 27 }; |
| 28 |
| 29 class TestPageActionViewList : public LocationBarViewMac::PageActionViewList { |
| 30 public: |
| 31 TestPageActionViewList() |
| 32 : LocationBarViewMac::PageActionViewList(NULL, NULL, NULL) {} |
| 33 |
| 34 void Add(LocationBarViewMac::PageActionImageView* view) { |
| 35 views_.push_back(view); |
| 36 } |
| 37 }; |
| 38 |
| 22 class AutocompleteTextFieldCellTest : public CocoaTest { | 39 class AutocompleteTextFieldCellTest : public CocoaTest { |
| 23 public: | 40 public: |
| 24 AutocompleteTextFieldCellTest() : security_image_view_(NULL, NULL), | 41 AutocompleteTextFieldCellTest() : security_image_view_(NULL, NULL), |
| 25 page_action_views_(NULL, NULL, NULL) { | 42 page_action_views_() { |
| 26 // Make sure this is wide enough to play games with the cell | 43 // Make sure this is wide enough to play games with the cell |
| 27 // decorations. | 44 // decorations. |
| 28 const NSRect frame = NSMakeRect(0, 0, kWidth, 30); | 45 const NSRect frame = NSMakeRect(0, 0, kWidth, 30); |
| 29 | 46 |
| 30 scoped_nsobject<NSTextField> view( | 47 scoped_nsobject<NSTextField> view( |
| 31 [[NSTextField alloc] initWithFrame:frame]); | 48 [[NSTextField alloc] initWithFrame:frame]); |
| 32 view_ = view.get(); | 49 view_ = view.get(); |
| 33 | 50 |
| 34 scoped_nsobject<AutocompleteTextFieldCell> cell( | 51 scoped_nsobject<AutocompleteTextFieldCell> cell( |
| 35 [[AutocompleteTextFieldCell alloc] initTextCell:@"Testing"]); | 52 [[AutocompleteTextFieldCell alloc] initTextCell:@"Testing"]); |
| 36 [cell setEditable:YES]; | 53 [cell setEditable:YES]; |
| 37 [cell setBordered:YES]; | 54 [cell setBordered:YES]; |
| 38 [cell setSecurityImageView:&security_image_view_]; | 55 [cell setSecurityImageView:&security_image_view_]; |
| 39 [cell setPageActionViewList:&page_action_views_]; | 56 [cell setPageActionViewList:&page_action_views_]; |
| 40 [view_ setCell:cell.get()]; | 57 [view_ setCell:cell.get()]; |
| 41 | 58 |
| 42 [[test_window() contentView] addSubview:view_]; | 59 [[test_window() contentView] addSubview:view_]; |
| 43 } | 60 } |
| 44 | 61 |
| 45 NSTextField* view_; | 62 NSTextField* view_; |
| 46 LocationBarViewMac::SecurityImageView security_image_view_; | 63 LocationBarViewMac::SecurityImageView security_image_view_; |
| 47 LocationBarViewMac::PageActionViewList page_action_views_; | 64 TestPageActionViewList page_action_views_; |
| 48 }; | 65 }; |
| 49 | 66 |
| 50 // Basic view tests (AddRemove, Display). | 67 // Basic view tests (AddRemove, Display). |
| 51 TEST_VIEW(AutocompleteTextFieldCellTest, view_); | 68 TEST_VIEW(AutocompleteTextFieldCellTest, view_); |
| 52 | 69 |
| 53 // Test drawing, mostly to ensure nothing leaks or crashes. | 70 // Test drawing, mostly to ensure nothing leaks or crashes. |
| 54 TEST_F(AutocompleteTextFieldCellTest, FocusedDisplay) { | 71 TEST_F(AutocompleteTextFieldCellTest, FocusedDisplay) { |
| 55 [view_ display]; | 72 [view_ display]; |
| 56 | 73 |
| 57 // Test focused drawing. | 74 // Test focused drawing. |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 LocationBarViewMac::SecurityImageView::LOCK); | 264 LocationBarViewMac::SecurityImageView::LOCK); |
| 248 security_image_view_.SetVisible(true); | 265 security_image_view_.SetVisible(true); |
| 249 | 266 |
| 250 textFrame = [cell textFrameForFrame:bounds]; | 267 textFrame = [cell textFrameForFrame:bounds]; |
| 251 drawingRect = [cell drawingRectForBounds:bounds]; | 268 drawingRect = [cell drawingRectForBounds:bounds]; |
| 252 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); | 269 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); |
| 253 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); | 270 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); |
| 254 } | 271 } |
| 255 | 272 |
| 256 // Test that the security icon is at the right side of the cell. | 273 // Test that the security icon is at the right side of the cell. |
| 257 TEST_F(AutocompleteTextFieldCellTest, HintImageFrame) { | 274 TEST_F(AutocompleteTextFieldCellTest, SecurityImageFrame) { |
| 258 AutocompleteTextFieldCell* cell = | 275 AutocompleteTextFieldCell* cell = |
| 259 static_cast<AutocompleteTextFieldCell*>([view_ cell]); | 276 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 260 const NSRect bounds([view_ bounds]); | 277 const NSRect bounds([view_ bounds]); |
| 261 security_image_view_.SetImageShown( | 278 security_image_view_.SetImageShown( |
| 262 LocationBarViewMac::SecurityImageView::LOCK); | 279 LocationBarViewMac::SecurityImageView::LOCK); |
| 263 | 280 |
| 264 security_image_view_.SetVisible(false); | 281 security_image_view_.SetVisible(false); |
| 265 NSRect iconRect = [cell securityImageFrameForFrame:bounds]; | 282 NSRect iconRect = [cell securityImageFrameForFrame:bounds]; |
| 266 EXPECT_TRUE(NSIsEmptyRect(iconRect)); | 283 EXPECT_TRUE(NSIsEmptyRect(iconRect)); |
| 267 | 284 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 textFrame = [cell textFrameForFrame:bounds]; | 316 textFrame = [cell textFrameForFrame:bounds]; |
| 300 EXPECT_LE(NSMaxX(textFrame), NSMinX(iconRect)); | 317 EXPECT_LE(NSMaxX(textFrame), NSMinX(iconRect)); |
| 301 | 318 |
| 302 // Make sure we clear correctly. | 319 // Make sure we clear correctly. |
| 303 security_image_view_.SetVisible(false); | 320 security_image_view_.SetVisible(false); |
| 304 iconRect = [cell securityImageFrameForFrame:bounds]; | 321 iconRect = [cell securityImageFrameForFrame:bounds]; |
| 305 EXPECT_TRUE(NSEqualRects(iconRect, originalIconRect)); | 322 EXPECT_TRUE(NSEqualRects(iconRect, originalIconRect)); |
| 306 EXPECT_TRUE(NSIsEmptyRect(iconRect)); | 323 EXPECT_TRUE(NSIsEmptyRect(iconRect)); |
| 307 } | 324 } |
| 308 | 325 |
| 326 // Test Page Action counts. |
| 327 TEST_F(AutocompleteTextFieldCellTest, PageActionCount) { |
| 328 AutocompleteTextFieldCell* cell = |
| 329 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 330 |
| 331 TestPageActionImageView page_action_view; |
| 332 TestPageActionImageView page_action_view2; |
| 333 |
| 334 TestPageActionViewList list; |
| 335 [cell setPageActionViewList:&list]; |
| 336 |
| 337 EXPECT_EQ(0u, [cell pageActionCount]); |
| 338 list.Add(&page_action_view); |
| 339 EXPECT_EQ(1u, [cell pageActionCount]); |
| 340 list.Add(&page_action_view2); |
| 341 EXPECT_EQ(2u, [cell pageActionCount]); |
| 342 } |
| 343 |
| 344 // Test that Page Action icons are properly placed. |
| 345 TEST_F(AutocompleteTextFieldCellTest, PageActionImageFrame) { |
| 346 AutocompleteTextFieldCell* cell = |
| 347 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 348 const NSRect bounds([view_ bounds]); |
| 349 security_image_view_.SetImageShown( |
| 350 LocationBarViewMac::SecurityImageView::LOCK); |
| 351 |
| 352 TestPageActionImageView page_action_view; |
| 353 // We'll assume that the extensions code enforces icons smaller than the |
| 354 // location bar. |
| 355 NSImage* image = [[[NSImage alloc] |
| 356 initWithSize:NSMakeSize(NSHeight(bounds) - 4, NSHeight(bounds) - 4)] |
| 357 autorelease]; |
| 358 page_action_view.SetImage(image); |
| 359 |
| 360 TestPageActionImageView page_action_view2; |
| 361 page_action_view2.SetImage(image); |
| 362 |
| 363 TestPageActionViewList list; |
| 364 list.Add(&page_action_view); |
| 365 list.Add(&page_action_view2); |
| 366 [cell setPageActionViewList:&list]; |
| 367 |
| 368 security_image_view_.SetVisible(false); |
| 369 page_action_view.SetVisible(false); |
| 370 page_action_view2.SetVisible(false); |
| 371 EXPECT_TRUE(NSIsEmptyRect([cell pageActionFrameForIndex:0 inFrame:bounds])); |
| 372 EXPECT_TRUE(NSIsEmptyRect([cell pageActionFrameForIndex:1 inFrame:bounds])); |
| 373 |
| 374 // One page action, no security icon. |
| 375 page_action_view.SetVisible(true); |
| 376 NSRect iconRect0 = [cell pageActionFrameForIndex:0 inFrame:bounds]; |
| 377 |
| 378 EXPECT_FALSE(NSIsEmptyRect(iconRect0)); |
| 379 EXPECT_TRUE(NSContainsRect(bounds, iconRect0)); |
| 380 |
| 381 // Make sure we are right of the |drawingRect|. |
| 382 NSRect drawingRect = [cell drawingRectForBounds:bounds]; |
| 383 EXPECT_LE(NSMaxX(drawingRect), NSMinX(iconRect0)); |
| 384 |
| 385 // Make sure we're right of the |textFrame|. |
| 386 NSRect textFrame = [cell textFrameForFrame:bounds]; |
| 387 EXPECT_LE(NSMaxX(textFrame), NSMinX(iconRect0)); |
| 388 |
| 389 // Two page actions plus a security icon. |
| 390 page_action_view2.SetVisible(true); |
| 391 security_image_view_.SetVisible(true); |
| 392 iconRect0 = [cell pageActionFrameForIndex:0 inFrame:bounds]; |
| 393 NSRect iconRect1 = [cell pageActionFrameForIndex:1 inFrame:bounds]; |
| 394 NSRect lockRect = [cell securityImageFrameForFrame:bounds]; |
| 395 |
| 396 // Make sure they're all in the expected order, and right of the |drawingRect| |
| 397 // and |textFrame|. |
| 398 drawingRect = [cell drawingRectForBounds:bounds]; |
| 399 textFrame = [cell textFrameForFrame:bounds]; |
| 400 |
| 401 EXPECT_FALSE(NSIsEmptyRect(iconRect0)); |
| 402 EXPECT_TRUE(NSContainsRect(bounds, iconRect0)); |
| 403 EXPECT_FALSE(NSIsEmptyRect(iconRect1)); |
| 404 EXPECT_TRUE(NSContainsRect(bounds, iconRect1)); |
| 405 EXPECT_FALSE(NSIsEmptyRect(lockRect)); |
| 406 EXPECT_TRUE(NSContainsRect(bounds, lockRect)); |
| 407 |
| 408 EXPECT_LE(NSMaxX(drawingRect), NSMinX(iconRect1)); |
| 409 EXPECT_LE(NSMaxX(textFrame), NSMinX(iconRect1)); |
| 410 EXPECT_LE(NSMaxX(iconRect1), NSMinX(iconRect0)); |
| 411 EXPECT_LE(NSMaxX(iconRect0), NSMinX(lockRect)); |
| 412 } |
| 413 |
| 309 // Test that the cell correctly chooses the partial keyword if there's | 414 // Test that the cell correctly chooses the partial keyword if there's |
| 310 // no enough room. | 415 // not enough room. |
| 311 TEST_F(AutocompleteTextFieldCellTest, UsesPartialKeywordIfNarrow) { | 416 TEST_F(AutocompleteTextFieldCellTest, UsesPartialKeywordIfNarrow) { |
| 312 AutocompleteTextFieldCell* cell = | 417 AutocompleteTextFieldCell* cell = |
| 313 static_cast<AutocompleteTextFieldCell*>([view_ cell]); | 418 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 314 | 419 |
| 315 const NSString* kFullString = @"Search Engine:"; | 420 const NSString* kFullString = @"Search Engine:"; |
| 316 const NSString* kPartialString = @"Search Eng:"; | 421 const NSString* kPartialString = @"Search Eng:"; |
| 317 | 422 |
| 318 // Wide width chooses the full string. | 423 // Wide width chooses the full string. |
| 319 [cell setKeywordString:kFullString | 424 [cell setKeywordString:kFullString |
| 320 partialString:kPartialString | 425 partialString:kPartialString |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 EXPECT_TRUE([[[cell hintString] string] hasPrefix:kHintPrefix]); | 478 EXPECT_TRUE([[[cell hintString] string] hasPrefix:kHintPrefix]); |
| 374 EXPECT_TRUE([[[cell hintString] string] hasSuffix:kHintSuffix]); | 479 EXPECT_TRUE([[[cell hintString] string] hasSuffix:kHintSuffix]); |
| 375 | 480 |
| 376 // Narrow width suppresses everything but the image. | 481 // Narrow width suppresses everything but the image. |
| 377 [cell setKeywordHintPrefix:kHintPrefix image:image suffix:kHintSuffix | 482 [cell setKeywordHintPrefix:kHintPrefix image:image suffix:kHintSuffix |
| 378 availableWidth:kNarrowWidth]; | 483 availableWidth:kNarrowWidth]; |
| 379 EXPECT_EQ([[cell hintString] length], 1U); | 484 EXPECT_EQ([[cell hintString] length], 1U); |
| 380 } | 485 } |
| 381 | 486 |
| 382 } // namespace | 487 } // namespace |
| OLD | NEW |