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 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 // TODO(shess): Make a sensible distinction between | |
15 // AutocompleteTextField unittesting and AutocompleteTextFieldCell | |
16 // unittesting - or, since AutocompleteTextFieldCell doesn't really | |
17 // have independent existence, just put tests for both in | |
18 // autocomplete_text_field_unittest.mm. | |
19 | |
20 class AutocompleteTextFieldCellTest : public testing::Test { | 14 class AutocompleteTextFieldCellTest : public testing::Test { |
21 public: | 15 public: |
22 AutocompleteTextFieldCellTest() { | 16 AutocompleteTextFieldCellTest() { |
23 NSRect frame = NSMakeRect(0, 0, 50, 30); | 17 // Make sure this is wide enough to play games with the cell |
| 18 // decorations. |
| 19 NSRect frame = NSMakeRect(0, 0, 300, 30); |
24 view_.reset([[NSTextField alloc] initWithFrame:frame]); | 20 view_.reset([[NSTextField alloc] initWithFrame:frame]); |
25 scoped_nsobject<AutocompleteTextFieldCell> cell( | 21 scoped_nsobject<AutocompleteTextFieldCell> cell( |
26 [[AutocompleteTextFieldCell alloc] initTextCell:@"Testing"]); | 22 [[AutocompleteTextFieldCell alloc] initTextCell:@"Testing"]); |
27 [view_ setCell:cell.get()]; | 23 [view_ setCell:cell.get()]; |
28 [cocoa_helper_.contentView() addSubview:view_.get()]; | 24 [cocoa_helper_.contentView() addSubview:view_.get()]; |
29 } | 25 } |
30 | 26 |
31 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... | 27 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... |
32 scoped_nsobject<NSTextField> view_; | 28 scoped_nsobject<NSTextField> view_; |
33 }; | 29 }; |
34 | 30 |
35 // Test adding/removing from the view hierarchy, mostly to ensure nothing | 31 // Test adding/removing from the view hierarchy, mostly to ensure nothing |
36 // leaks or crashes. | 32 // leaks or crashes. |
37 TEST_F(AutocompleteTextFieldCellTest, AddRemove) { | 33 TEST_F(AutocompleteTextFieldCellTest, AddRemove) { |
38 EXPECT_EQ(cocoa_helper_.contentView(), [view_ superview]); | 34 EXPECT_EQ(cocoa_helper_.contentView(), [view_ superview]); |
39 [view_.get() removeFromSuperview]; | 35 [view_.get() removeFromSuperview]; |
40 EXPECT_FALSE([view_ superview]); | 36 EXPECT_FALSE([view_ superview]); |
41 } | 37 } |
42 | 38 |
43 // Test drawing, mostly to ensure nothing leaks or crashes. | 39 // Test drawing, mostly to ensure nothing leaks or crashes. |
44 TEST_F(AutocompleteTextFieldCellTest, Display) { | 40 TEST_F(AutocompleteTextFieldCellTest, Display) { |
45 [view_ display]; | 41 [view_ display]; |
| 42 |
| 43 // Test display of various cell configurations. |
| 44 AutocompleteTextFieldCell* cell = |
| 45 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 46 |
| 47 [cell setSearchHintString:@"Type to search"]; |
| 48 [view_ display]; |
| 49 |
| 50 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; |
| 51 [cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix"]; |
| 52 [view_ display]; |
| 53 |
| 54 [cell setKeywordString:@"Search Engine:"]; |
| 55 [view_ display]; |
| 56 } |
| 57 |
| 58 TEST_F(AutocompleteTextFieldCellTest, SearchHint) { |
| 59 AutocompleteTextFieldCell* cell = |
| 60 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 61 |
| 62 // At default settings, everything is already good to go. |
| 63 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 64 |
| 65 // Setting a search hint will need a reset. |
| 66 [cell setSearchHintString:@"Type to search"]; |
| 67 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 68 [cell setFieldEditorNeedsReset:NO]; |
| 69 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 70 |
| 71 // Changing the search hint needs a reset. |
| 72 [cell setSearchHintString:@"Type to find"]; |
| 73 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 74 [cell setFieldEditorNeedsReset:NO]; |
| 75 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 76 |
| 77 // Changing to an identical string doesn't need a reset. |
| 78 [cell setSearchHintString:@"Type to find"]; |
| 79 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 80 } |
| 81 |
| 82 TEST_F(AutocompleteTextFieldCellTest, KeywordHint) { |
| 83 AutocompleteTextFieldCell* cell = |
| 84 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 85 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; |
| 86 |
| 87 // At default settings, everything is already good to go. |
| 88 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 89 |
| 90 // Setting a keyword hint will need a reset. |
| 91 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine"]; |
| 92 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 93 [cell setFieldEditorNeedsReset:NO]; |
| 94 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 95 |
| 96 // Changing the keyword hint needs a reset. |
| 97 [cell setKeywordHintPrefix:@"Type " image:image suffix:@" to search Engine"]; |
| 98 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 99 [cell setFieldEditorNeedsReset:NO]; |
| 100 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 101 |
| 102 // Changing to identical strings doesn't need a reset. |
| 103 [cell setKeywordHintPrefix:@"Type " image:image suffix:@" to search Engine"]; |
| 104 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 105 } |
| 106 |
| 107 TEST_F(AutocompleteTextFieldCellTest, KeywordString) { |
| 108 AutocompleteTextFieldCell* cell = |
| 109 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 110 |
| 111 // At default settings, everything is already good to go. |
| 112 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 113 |
| 114 // Setting a keyword string will need a reset. |
| 115 [cell setKeywordString:@"Search Engine:"]; |
| 116 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 117 [cell setFieldEditorNeedsReset:NO]; |
| 118 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 119 |
| 120 // Changing the keyword string needs a reset. |
| 121 [cell setKeywordString:@"Search on Engine:"]; |
| 122 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 123 [cell setFieldEditorNeedsReset:NO]; |
| 124 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 125 |
| 126 // Changing to an identical string doesn't need a reset. |
| 127 [cell setKeywordString:@"Search on Engine:"]; |
| 128 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 129 } |
| 130 |
| 131 // Test that transitions between various modes set the reset flag. |
| 132 TEST_F(AutocompleteTextFieldCellTest, Transitions) { |
| 133 AutocompleteTextFieldCell* cell = |
| 134 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 135 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; |
| 136 |
| 137 // Transitions from hint to keyword string, keyword hint, and |
| 138 // cleared. |
| 139 [cell setSearchHintString:@"Type to search"]; |
| 140 [cell setFieldEditorNeedsReset:NO]; |
| 141 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 142 [cell setKeywordString:@"Search Engine:"]; |
| 143 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 144 |
| 145 [cell setSearchHintString:@"Type to search"]; |
| 146 [cell setFieldEditorNeedsReset:NO]; |
| 147 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 148 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine"]; |
| 149 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 150 |
| 151 [cell setSearchHintString:@"Type to search"]; |
| 152 [cell setFieldEditorNeedsReset:NO]; |
| 153 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 154 [cell clearKeywordAndHint]; |
| 155 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 156 |
| 157 // Transitions from keyword hint to keyword string, simple hint, and |
| 158 // cleared. |
| 159 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine"]; |
| 160 [cell setFieldEditorNeedsReset:NO]; |
| 161 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 162 [cell setSearchHintString:@"Type to search"]; |
| 163 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 164 |
| 165 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine"]; |
| 166 [cell setFieldEditorNeedsReset:NO]; |
| 167 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 168 [cell setKeywordString:@"Search Engine:"]; |
| 169 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 170 |
| 171 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine"]; |
| 172 [cell setFieldEditorNeedsReset:NO]; |
| 173 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 174 [cell clearKeywordAndHint]; |
| 175 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 176 |
| 177 // Transitions from keyword string to either type of hint, or |
| 178 // cleared. |
| 179 [cell setKeywordString:@"Search on Engine:"]; |
| 180 [cell setFieldEditorNeedsReset:NO]; |
| 181 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 182 [cell setSearchHintString:@"Type to search"]; |
| 183 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 184 |
| 185 [cell setKeywordString:@"Search on Engine:"]; |
| 186 [cell setFieldEditorNeedsReset:NO]; |
| 187 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 188 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine"]; |
| 189 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 190 |
| 191 [cell setKeywordString:@"Search on Engine:"]; |
| 192 [cell setFieldEditorNeedsReset:NO]; |
| 193 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
| 194 [cell clearKeywordAndHint]; |
| 195 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
| 196 } |
| 197 |
| 198 TEST_F(AutocompleteTextFieldCellTest, TextFrame) { |
| 199 AutocompleteTextFieldCell* cell = |
| 200 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 201 const NSRect bounds([view_ bounds]); |
| 202 NSRect textFrame; |
| 203 |
| 204 // At default settings, everything goes to the text area. |
| 205 textFrame = [cell textFrameForFrame:bounds]; |
| 206 EXPECT_FALSE(NSIsEmptyRect(textFrame)); |
| 207 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); |
| 208 EXPECT_EQ(NSMinX(bounds), NSMinX(textFrame)); |
| 209 EXPECT_EQ(NSMaxX(bounds), NSMaxX(textFrame)); |
| 210 |
| 211 // Small search hint leaves text frame to left. |
| 212 [cell setSearchHintString:@"Search hint"]; |
| 213 textFrame = [cell textFrameForFrame:bounds]; |
| 214 EXPECT_FALSE(NSIsEmptyRect(textFrame)); |
| 215 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); |
| 216 EXPECT_LT(NSMaxX(textFrame), NSMaxX(bounds)); |
| 217 |
| 218 // Save search-hint's frame for future reference. |
| 219 const CGFloat searchHintMaxX(NSMaxX(textFrame)); |
| 220 |
| 221 // Keyword hint also leaves text to left. Arrange for it to be |
| 222 // larger than the search hint just to make sure that things have |
| 223 // changed (keyword hint overrode search hint), and that they aren't |
| 224 // handled identically w/out reference to the actual button cell. |
| 225 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; |
| 226 [cell setKeywordHintPrefix:@"Keyword " image:image suffix:@" hint"]; |
| 227 textFrame = [cell textFrameForFrame:bounds]; |
| 228 EXPECT_FALSE(NSIsEmptyRect(textFrame)); |
| 229 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); |
| 230 EXPECT_LT(NSMaxX(textFrame), NSMaxX(bounds)); |
| 231 EXPECT_LT(NSMaxX(textFrame), searchHintMaxX); |
| 232 |
| 233 // Keyword search leaves text area to right. |
| 234 [cell setKeywordString:@"Search Engine:"]; |
| 235 textFrame = [cell textFrameForFrame:bounds]; |
| 236 EXPECT_FALSE(NSIsEmptyRect(textFrame)); |
| 237 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); |
| 238 EXPECT_GT(NSMinX(textFrame), NSMinX(bounds)); |
| 239 EXPECT_LT(NSMinX(textFrame), searchHintMaxX); |
| 240 EXPECT_GT(NSMaxX(textFrame), searchHintMaxX); |
| 241 |
| 242 // Text frame should take everything over again on reset. |
| 243 [cell clearKeywordAndHint]; |
| 244 textFrame = [cell textFrameForFrame:bounds]; |
| 245 EXPECT_FALSE(NSIsEmptyRect(textFrame)); |
| 246 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); |
| 247 EXPECT_EQ(NSMinX(bounds), NSMinX(textFrame)); |
| 248 EXPECT_EQ(NSMaxX(bounds), NSMaxX(textFrame)); |
46 } | 249 } |
47 | 250 |
48 } // namespace | 251 } // namespace |
OLD | NEW |