| OLD | NEW |
| 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" | 11 #import "chrome/browser/cocoa/location_bar/ev_bubble_decoration.h" |
| 12 #import "chrome/browser/cocoa/location_bar/keyword_hint_decoration.h" |
| 12 #import "chrome/browser/cocoa/location_bar/location_bar_decoration.h" | 13 #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/location_icon_decoration.h" |
| 14 #import "chrome/browser/cocoa/location_bar/selected_keyword_decoration.h" | 15 #import "chrome/browser/cocoa/location_bar/selected_keyword_decoration.h" |
| 15 #import "chrome/browser/cocoa/location_bar/star_decoration.h" | 16 #import "chrome/browser/cocoa/location_bar/star_decoration.h" |
| 16 #include "grit/theme_resources.h" | 17 #include "grit/theme_resources.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "testing/platform_test.h" | 20 #include "testing/platform_test.h" |
| 20 | 21 |
| 21 using ::testing::StrictMock; | 22 using ::testing::StrictMock; |
| 22 using ::testing::_; | 23 using ::testing::_; |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 // Width of the field so that we don't have to ask |field_| for it all | 27 // Width of the field so that we don't have to ask |field_| for it all |
| 27 // the time. | 28 // the time. |
| 28 const CGFloat kWidth(300.0); | 29 const CGFloat kWidth(300.0); |
| 29 | 30 |
| 30 // A narrow width for tests which test things that don't fit. | 31 // A narrow width for tests which test things that don't fit. |
| 31 const CGFloat kNarrowWidth(5.0); | 32 const CGFloat kNarrowWidth(5.0); |
| 32 | 33 |
| 33 class MockDecoration : public LocationBarDecoration { | 34 class MockDecoration : public LocationBarDecoration { |
| 34 public: | 35 public: |
| 35 virtual CGFloat GetWidthForSpace(CGFloat width) { return 20.0; } | 36 virtual CGFloat GetWidthForSpace(CGFloat width) { return 20.0; } |
| 36 virtual bool IsVisible() const { return visible_; } | |
| 37 void SetVisible(bool visible) { visible_ = visible; } | |
| 38 | 37 |
| 39 MOCK_METHOD2(DrawInFrame, void(NSRect frame, NSView* control_view)); | 38 MOCK_METHOD2(DrawInFrame, void(NSRect frame, NSView* control_view)); |
| 40 | |
| 41 bool visible_; | |
| 42 }; | 39 }; |
| 43 | 40 |
| 44 class AutocompleteTextFieldCellTest : public CocoaTest { | 41 class AutocompleteTextFieldCellTest : public CocoaTest { |
| 45 public: | 42 public: |
| 46 AutocompleteTextFieldCellTest() { | 43 AutocompleteTextFieldCellTest() { |
| 47 // Make sure this is wide enough to play games with the cell | 44 // Make sure this is wide enough to play games with the cell |
| 48 // decorations. | 45 // decorations. |
| 49 const NSRect frame = NSMakeRect(0, 0, kWidth, 30); | 46 const NSRect frame = NSMakeRect(0, 0, kWidth, 30); |
| 50 | 47 |
| 51 scoped_nsobject<NSTextField> view( | 48 scoped_nsobject<NSTextField> view( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 | 82 |
| 86 // Test focused drawing. | 83 // Test focused drawing. |
| 87 [test_window() makePretendKeyWindowAndSetFirstResponder:view_]; | 84 [test_window() makePretendKeyWindowAndSetFirstResponder:view_]; |
| 88 [view_ display]; | 85 [view_ display]; |
| 89 [test_window() clearPretendKeyWindowAndFirstResponder]; | 86 [test_window() clearPretendKeyWindowAndFirstResponder]; |
| 90 | 87 |
| 91 // Test display of various cell configurations. | 88 // Test display of various cell configurations. |
| 92 AutocompleteTextFieldCell* cell = | 89 AutocompleteTextFieldCell* cell = |
| 93 static_cast<AutocompleteTextFieldCell*>([view_ cell]); | 90 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 94 | 91 |
| 95 [cell setSearchHintString:@"Type to search" availableWidth:kWidth]; | |
| 96 [view_ display]; | |
| 97 | |
| 98 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; | |
| 99 [cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix" | |
| 100 availableWidth:kWidth]; | |
| 101 [view_ display]; | |
| 102 | |
| 103 // Load available decorations and try drawing. To make sure that | 92 // Load available decorations and try drawing. To make sure that |
| 104 // they are actually drawn, check that |GetWidthForSpace()| doesn't | 93 // they are actually drawn, check that |GetWidthForSpace()| doesn't |
| 105 // indicate that they should be omitted. | 94 // indicate that they should be omitted. |
| 106 const CGFloat kVeryWide = 1000.0; | 95 const CGFloat kVeryWide = 1000.0; |
| 107 | 96 |
| 108 SelectedKeywordDecoration selected_keyword_decoration([view_ font]); | 97 SelectedKeywordDecoration selected_keyword_decoration([view_ font]); |
| 109 selected_keyword_decoration.SetVisible(true); | 98 selected_keyword_decoration.SetVisible(true); |
| 110 selected_keyword_decoration.SetKeyword(std::wstring(L"Google"), false); | 99 selected_keyword_decoration.SetKeyword(std::wstring(L"Google"), false); |
| 111 [cell addLeftDecoration:&selected_keyword_decoration]; | 100 [cell addLeftDecoration:&selected_keyword_decoration]; |
| 112 EXPECT_NE(selected_keyword_decoration.GetWidthForSpace(kVeryWide), | 101 EXPECT_NE(selected_keyword_decoration.GetWidthForSpace(kVeryWide), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 130 [cell addLeftDecoration:&ev_bubble_decoration]; | 119 [cell addLeftDecoration:&ev_bubble_decoration]; |
| 131 EXPECT_NE(ev_bubble_decoration.GetWidthForSpace(kVeryWide), | 120 EXPECT_NE(ev_bubble_decoration.GetWidthForSpace(kVeryWide), |
| 132 LocationBarDecoration::kOmittedWidth); | 121 LocationBarDecoration::kOmittedWidth); |
| 133 | 122 |
| 134 StarDecoration star_decoration(NULL); | 123 StarDecoration star_decoration(NULL); |
| 135 star_decoration.SetVisible(true); | 124 star_decoration.SetVisible(true); |
| 136 [cell addRightDecoration:&star_decoration]; | 125 [cell addRightDecoration:&star_decoration]; |
| 137 EXPECT_NE(star_decoration.GetWidthForSpace(kVeryWide), | 126 EXPECT_NE(star_decoration.GetWidthForSpace(kVeryWide), |
| 138 LocationBarDecoration::kOmittedWidth); | 127 LocationBarDecoration::kOmittedWidth); |
| 139 | 128 |
| 129 KeywordHintDecoration keyword_hint_decoration([view_ font]); |
| 130 keyword_hint_decoration.SetVisible(true); |
| 131 keyword_hint_decoration.SetKeyword(std::wstring(L"google"), false); |
| 132 [cell addRightDecoration:&keyword_hint_decoration]; |
| 133 EXPECT_NE(keyword_hint_decoration.GetWidthForSpace(kVeryWide), |
| 134 LocationBarDecoration::kOmittedWidth); |
| 135 |
| 140 // Make sure we're actually calling |DrawInFrame()|. | 136 // Make sure we're actually calling |DrawInFrame()|. |
| 141 StrictMock<MockDecoration> mock_decoration; | 137 StrictMock<MockDecoration> mock_decoration; |
| 142 mock_decoration.SetVisible(true); | 138 mock_decoration.SetVisible(true); |
| 143 [cell addLeftDecoration:&mock_decoration]; | 139 [cell addLeftDecoration:&mock_decoration]; |
| 144 EXPECT_CALL(mock_decoration, DrawInFrame(_, _)); | 140 EXPECT_CALL(mock_decoration, DrawInFrame(_, _)); |
| 145 EXPECT_NE(mock_decoration.GetWidthForSpace(kVeryWide), | 141 EXPECT_NE(mock_decoration.GetWidthForSpace(kVeryWide), |
| 146 LocationBarDecoration::kOmittedWidth); | 142 LocationBarDecoration::kOmittedWidth); |
| 147 | 143 |
| 148 [view_ display]; | 144 [view_ display]; |
| 149 | 145 |
| 150 [cell clearDecorations]; | 146 [cell clearDecorations]; |
| 151 } | 147 } |
| 152 | 148 |
| 153 TEST_F(AutocompleteTextFieldCellTest, ClearKeywordAndHint) { | |
| 154 AutocompleteTextFieldCell* cell = | |
| 155 static_cast<AutocompleteTextFieldCell*>([view_ cell]); | |
| 156 EXPECT_FALSE([cell hintString]); | |
| 157 | |
| 158 // Check that the search hint can be cleared. | |
| 159 [cell setSearchHintString:@"Type to search" availableWidth:kWidth]; | |
| 160 EXPECT_TRUE([cell hintString]); | |
| 161 [cell clearHint]; | |
| 162 EXPECT_FALSE([cell hintString]); | |
| 163 | |
| 164 // Check that the keyword hint can be cleared. | |
| 165 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; | |
| 166 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine" | |
| 167 availableWidth:kWidth]; | |
| 168 EXPECT_TRUE([cell hintString]); | |
| 169 [cell clearHint]; | |
| 170 EXPECT_FALSE([cell hintString]); | |
| 171 } | |
| 172 | |
| 173 TEST_F(AutocompleteTextFieldCellTest, TextFrame) { | 149 TEST_F(AutocompleteTextFieldCellTest, TextFrame) { |
| 174 AutocompleteTextFieldCell* cell = | 150 AutocompleteTextFieldCell* cell = |
| 175 static_cast<AutocompleteTextFieldCell*>([view_ cell]); | 151 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 176 const NSRect bounds([view_ bounds]); | 152 const NSRect bounds([view_ bounds]); |
| 177 NSRect textFrame; | 153 NSRect textFrame; |
| 178 | 154 |
| 179 // The cursor frame should stay the same throughout. | 155 // The cursor frame should stay the same throughout. |
| 180 const NSRect cursorFrame([cell textCursorFrameForFrame:bounds]); | 156 const NSRect cursorFrame([cell textCursorFrameForFrame:bounds]); |
| 181 EXPECT_TRUE(NSEqualRects(cursorFrame, bounds)); | 157 EXPECT_TRUE(NSEqualRects(cursorFrame, bounds)); |
| 182 | 158 |
| 183 // At default settings, everything goes to the text area. | 159 // At default settings, everything goes to the text area. |
| 184 textFrame = [cell textFrameForFrame:bounds]; | 160 textFrame = [cell textFrameForFrame:bounds]; |
| 185 EXPECT_FALSE(NSIsEmptyRect(textFrame)); | 161 EXPECT_FALSE(NSIsEmptyRect(textFrame)); |
| 186 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); | 162 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); |
| 187 EXPECT_EQ(NSMinX(bounds), NSMinX(textFrame)); | 163 EXPECT_EQ(NSMinX(bounds), NSMinX(textFrame)); |
| 188 EXPECT_EQ(NSMaxX(bounds), NSMaxX(textFrame)); | 164 EXPECT_EQ(NSMaxX(bounds), NSMaxX(textFrame)); |
| 189 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame)); | 165 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame)); |
| 190 | 166 |
| 191 // Small search hint leaves text frame to left. | |
| 192 [cell setSearchHintString:@"Search hint" availableWidth:kWidth]; | |
| 193 textFrame = [cell textFrameForFrame:bounds]; | |
| 194 EXPECT_FALSE(NSIsEmptyRect(textFrame)); | |
| 195 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); | |
| 196 EXPECT_LT(NSMaxX(textFrame), NSMaxX(bounds)); | |
| 197 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame)); | |
| 198 | |
| 199 // Save search-hint's frame for future reference. | |
| 200 const CGFloat searchHintMaxX(NSMaxX(textFrame)); | |
| 201 | |
| 202 // Keyword hint also leaves text to left. Arrange for it to be | |
| 203 // larger than the search hint just to make sure that things have | |
| 204 // changed (keyword hint overrode search hint), and that they aren't | |
| 205 // handled identically w/out reference to the actual button cell. | |
| 206 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; | |
| 207 [cell setKeywordHintPrefix:@"Keyword " image:image suffix:@" hint" | |
| 208 availableWidth:kWidth]; | |
| 209 textFrame = [cell textFrameForFrame:bounds]; | |
| 210 EXPECT_FALSE(NSIsEmptyRect(textFrame)); | |
| 211 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); | |
| 212 EXPECT_LT(NSMaxX(textFrame), NSMaxX(bounds)); | |
| 213 EXPECT_LT(NSMaxX(textFrame), searchHintMaxX); | |
| 214 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame)); | |
| 215 | |
| 216 | |
| 217 // Text frame should take everything over again on reset. | |
| 218 [cell clearHint]; | |
| 219 textFrame = [cell textFrameForFrame:bounds]; | |
| 220 EXPECT_FALSE(NSIsEmptyRect(textFrame)); | |
| 221 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); | |
| 222 EXPECT_EQ(NSMinX(bounds), NSMinX(textFrame)); | |
| 223 EXPECT_EQ(NSMaxX(bounds), NSMaxX(textFrame)); | |
| 224 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame)); | |
| 225 | |
| 226 // Search hint text takes precedence over the hint icon; the text frame | |
| 227 // should be smaller in order to accomodate the text that is wider than | |
| 228 // the icon. | |
| 229 [cell setSearchHintString:@"Search hint" availableWidth:kWidth]; | |
| 230 NSRect textFrameWithHintText = [cell textFrameForFrame:bounds]; | |
| 231 EXPECT_TRUE(NSContainsRect(textFrame, textFrameWithHintText)); | |
| 232 EXPECT_LT(NSWidth(textFrameWithHintText), NSWidth(textFrame)); | |
| 233 | |
| 234 // Decoration on the left takes up space. | 167 // Decoration on the left takes up space. |
| 235 mock_left_decoration_.SetVisible(true); | 168 mock_left_decoration_.SetVisible(true); |
| 236 textFrame = [cell textFrameForFrame:bounds]; | 169 textFrame = [cell textFrameForFrame:bounds]; |
| 237 EXPECT_FALSE(NSIsEmptyRect(textFrame)); | 170 EXPECT_FALSE(NSIsEmptyRect(textFrame)); |
| 238 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); | 171 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); |
| 239 EXPECT_GT(NSMinX(textFrame), NSMinX(bounds)); | 172 EXPECT_GT(NSMinX(textFrame), NSMinX(bounds)); |
| 240 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame)); | 173 EXPECT_TRUE(NSContainsRect(cursorFrame, textFrame)); |
| 241 } | 174 } |
| 242 | 175 |
| 243 // The editor frame should be slightly inset from the text frame. | 176 // The editor frame should be slightly inset from the text frame. |
| 244 TEST_F(AutocompleteTextFieldCellTest, DrawingRectForBounds) { | 177 TEST_F(AutocompleteTextFieldCellTest, DrawingRectForBounds) { |
| 245 AutocompleteTextFieldCell* cell = | 178 AutocompleteTextFieldCell* cell = |
| 246 static_cast<AutocompleteTextFieldCell*>([view_ cell]); | 179 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 247 const NSRect bounds([view_ bounds]); | 180 const NSRect bounds([view_ bounds]); |
| 248 NSRect textFrame, drawingRect; | 181 NSRect textFrame, drawingRect; |
| 249 | 182 |
| 250 textFrame = [cell textFrameForFrame:bounds]; | 183 textFrame = [cell textFrameForFrame:bounds]; |
| 251 drawingRect = [cell drawingRectForBounds:bounds]; | 184 drawingRect = [cell drawingRectForBounds:bounds]; |
| 252 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); | 185 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); |
| 253 EXPECT_TRUE(NSContainsRect(textFrame, NSInsetRect(drawingRect, 1, 1))); | 186 EXPECT_TRUE(NSContainsRect(textFrame, NSInsetRect(drawingRect, 1, 1))); |
| 254 | 187 |
| 255 // Save the starting frame for after clear. | 188 // Save the starting frame for after clear. |
| 256 const NSRect originalDrawingRect(drawingRect); | 189 const NSRect originalDrawingRect = drawingRect; |
| 257 | |
| 258 // TODO(shess): Do we really need to test every combination? | |
| 259 | |
| 260 [cell setSearchHintString:@"Search hint" availableWidth:kWidth]; | |
| 261 textFrame = [cell textFrameForFrame:bounds]; | |
| 262 drawingRect = [cell drawingRectForBounds:bounds]; | |
| 263 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); | |
| 264 EXPECT_TRUE(NSContainsRect(textFrame, NSInsetRect(drawingRect, 1, 1))); | |
| 265 | |
| 266 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; | |
| 267 [cell setKeywordHintPrefix:@"Keyword " image:image suffix:@" hint" | |
| 268 availableWidth:kWidth]; | |
| 269 textFrame = [cell textFrameForFrame:bounds]; | |
| 270 drawingRect = [cell drawingRectForBounds:bounds]; | |
| 271 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); | |
| 272 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); | |
| 273 | |
| 274 [cell clearHint]; | |
| 275 | |
| 276 textFrame = [cell textFrameForFrame:bounds]; | |
| 277 drawingRect = [cell drawingRectForBounds:bounds]; | |
| 278 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); | |
| 279 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); | |
| 280 EXPECT_TRUE(NSEqualRects(drawingRect, originalDrawingRect)); | |
| 281 | 190 |
| 282 mock_left_decoration_.SetVisible(true); | 191 mock_left_decoration_.SetVisible(true); |
| 283 textFrame = [cell textFrameForFrame:bounds]; | 192 textFrame = [cell textFrameForFrame:bounds]; |
| 284 drawingRect = [cell drawingRectForBounds:bounds]; | 193 drawingRect = [cell drawingRectForBounds:bounds]; |
| 285 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); | 194 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); |
| 286 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); | 195 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); |
| 287 | 196 |
| 288 mock_right_decoration0_.SetVisible(true); | 197 mock_right_decoration0_.SetVisible(true); |
| 289 textFrame = [cell textFrameForFrame:bounds]; | 198 textFrame = [cell textFrameForFrame:bounds]; |
| 290 drawingRect = [cell drawingRectForBounds:bounds]; | 199 drawingRect = [cell drawingRectForBounds:bounds]; |
| 291 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); | 200 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); |
| 292 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); | 201 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); |
| 202 |
| 203 mock_left_decoration_.SetVisible(false); |
| 204 mock_right_decoration0_.SetVisible(false); |
| 205 drawingRect = [cell drawingRectForBounds:bounds]; |
| 206 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); |
| 207 EXPECT_TRUE(NSEqualRects(drawingRect, originalDrawingRect)); |
| 293 } | 208 } |
| 294 | 209 |
| 295 // Test that left decorations are at the correct edge of the cell. | 210 // Test that left decorations are at the correct edge of the cell. |
| 296 TEST_F(AutocompleteTextFieldCellTest, LeftDecorationFrame) { | 211 TEST_F(AutocompleteTextFieldCellTest, LeftDecorationFrame) { |
| 297 AutocompleteTextFieldCell* cell = | 212 AutocompleteTextFieldCell* cell = |
| 298 static_cast<AutocompleteTextFieldCell*>([view_ cell]); | 213 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 299 const NSRect bounds = [view_ bounds]; | 214 const NSRect bounds = [view_ bounds]; |
| 300 | 215 |
| 301 mock_left_decoration_.SetVisible(true); | 216 mock_left_decoration_.SetVisible(true); |
| 302 const NSRect decorationRect = | 217 const NSRect decorationRect = |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 | 252 |
| 338 // Decoration should be right of |drawingRect|. | 253 // Decoration should be right of |drawingRect|. |
| 339 const NSRect drawingRect = [cell drawingRectForBounds:bounds]; | 254 const NSRect drawingRect = [cell drawingRectForBounds:bounds]; |
| 340 EXPECT_LT(NSMinX(drawingRect), NSMinX(decoration1Rect)); | 255 EXPECT_LT(NSMinX(drawingRect), NSMinX(decoration1Rect)); |
| 341 | 256 |
| 342 // Decoration should be right of |textFrame|. | 257 // Decoration should be right of |textFrame|. |
| 343 const NSRect textFrame = [cell textFrameForFrame:bounds]; | 258 const NSRect textFrame = [cell textFrameForFrame:bounds]; |
| 344 EXPECT_LT(NSMinX(textFrame), NSMinX(decoration1Rect)); | 259 EXPECT_LT(NSMinX(textFrame), NSMinX(decoration1Rect)); |
| 345 } | 260 } |
| 346 | 261 |
| 347 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; | |
| 348 | |
| 349 // Test that the cell drops the search hint if there is no room for | |
| 350 // it. | |
| 351 TEST_F(AutocompleteTextFieldCellTest, OmitsSearchHintIfNarrow) { | |
| 352 AutocompleteTextFieldCell* cell = | |
| 353 static_cast<AutocompleteTextFieldCell*>([view_ cell]); | |
| 354 | |
| 355 NSString* const kSearchHint = @"Type to search"; | |
| 356 | |
| 357 // Wide width chooses the full string. | |
| 358 [cell setSearchHintString:kSearchHint availableWidth:kWidth]; | |
| 359 EXPECT_TRUE([cell hintString]); | |
| 360 EXPECT_TRUE([[[cell hintString] string] isEqualToString:kSearchHint]); | |
| 361 | |
| 362 // Narrow width suppresses entirely. | |
| 363 [cell setSearchHintString:kSearchHint availableWidth:kNarrowWidth]; | |
| 364 EXPECT_FALSE([cell hintString]); | |
| 365 } | |
| 366 | |
| 367 // Test that the cell drops all but the image if there is no room for | |
| 368 // the entire keyword hint. | |
| 369 TEST_F(AutocompleteTextFieldCellTest, TrimsKeywordHintIfNarrow) { | |
| 370 AutocompleteTextFieldCell* cell = | |
| 371 static_cast<AutocompleteTextFieldCell*>([view_ cell]); | |
| 372 scoped_nsobject<NSImage> image( | |
| 373 [[NSImage alloc] initWithSize:NSMakeSize(20, 20)]); | |
| 374 | |
| 375 NSString* const kHintPrefix = @"Press "; | |
| 376 NSString* const kHintSuffix = @" to search Engine"; | |
| 377 | |
| 378 // Wide width chooses the full string. | |
| 379 [cell setKeywordHintPrefix:kHintPrefix image:image suffix:kHintSuffix | |
| 380 availableWidth:kWidth]; | |
| 381 EXPECT_TRUE([cell hintString]); | |
| 382 EXPECT_TRUE([[[cell hintString] string] hasPrefix:kHintPrefix]); | |
| 383 EXPECT_TRUE([[[cell hintString] string] hasSuffix:kHintSuffix]); | |
| 384 | |
| 385 // Narrow width suppresses everything but the image. | |
| 386 [cell setKeywordHintPrefix:kHintPrefix image:image suffix:kHintSuffix | |
| 387 availableWidth:kNarrowWidth]; | |
| 388 EXPECT_EQ([[cell hintString] length], 1U); | |
| 389 } | |
| 390 | |
| 391 } // namespace | 262 } // namespace |
| OLD | NEW |