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

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

Issue 295036: [Mac] Simplify field editor code which tracks whether editor rect changed. (Closed)
Patch Set: Pinks comments. Created 11 years, 2 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) 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"
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 [cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix" 66 [cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix"
67 availableWidth:kWidth]; 67 availableWidth:kWidth];
68 [view_ display]; 68 [view_ display];
69 69
70 [cell setKeywordString:@"Search Engine:" 70 [cell setKeywordString:@"Search Engine:"
71 partialString:@"Search Eng:" 71 partialString:@"Search Eng:"
72 availableWidth:kWidth]; 72 availableWidth:kWidth];
73 [view_ display]; 73 [view_ display];
74 } 74 }
75 75
76 TEST_F(AutocompleteTextFieldCellTest, SearchHint) { 76 // Verify that transitions between states clear other states.
77 AutocompleteTextFieldCell* cell = 77 TEST_F(AutocompleteTextFieldCellTest, StateTransitionsResetOthers) {
78 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
79
80 // At default settings, everything is already good to go.
81 EXPECT_FALSE([cell fieldEditorNeedsReset]);
82
83 // Setting a search hint will need a reset.
84 [cell setSearchHintString:@"Type to search" availableWidth:kWidth];
85 EXPECT_TRUE([cell fieldEditorNeedsReset]);
86 [cell setFieldEditorNeedsReset:NO];
87 EXPECT_FALSE([cell fieldEditorNeedsReset]);
88
89 // Changing the search hint needs a reset.
90 [cell setSearchHintString:@"Type to find" availableWidth:kWidth];
91 EXPECT_TRUE([cell fieldEditorNeedsReset]);
92 [cell setFieldEditorNeedsReset:NO];
93 EXPECT_FALSE([cell fieldEditorNeedsReset]);
94
95 // Changing to an identical string doesn't need a reset.
96 [cell setSearchHintString:@"Type to find" availableWidth:kWidth];
97 EXPECT_FALSE([cell fieldEditorNeedsReset]);
98 }
99
100 TEST_F(AutocompleteTextFieldCellTest, KeywordHint) {
101 AutocompleteTextFieldCell* cell = 78 AutocompleteTextFieldCell* cell =
102 static_cast<AutocompleteTextFieldCell*>([view_ cell]); 79 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
103 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; 80 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
104 81
105 // At default settings, everything is already good to go. 82 // Setting hint leaves keyword empty.
106 EXPECT_FALSE([cell fieldEditorNeedsReset]); 83 [cell setSearchHintString:@"Type to search" availableWidth:kWidth];
84 EXPECT_TRUE([cell hintString]);
85 EXPECT_FALSE([cell keywordString]);
107 86
108 // Setting a keyword hint will need a reset. 87 // Setting keyword clears hint.
109 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine"
110 availableWidth:kWidth];
111 EXPECT_TRUE([cell fieldEditorNeedsReset]);
112 [cell setFieldEditorNeedsReset:NO];
113 EXPECT_FALSE([cell fieldEditorNeedsReset]);
114
115 // Changing the keyword hint needs a reset.
116 [cell setKeywordHintPrefix:@"Type " image:image suffix:@" to search Engine"
117 availableWidth:kWidth];
118 EXPECT_TRUE([cell fieldEditorNeedsReset]);
119 [cell setFieldEditorNeedsReset:NO];
120 EXPECT_FALSE([cell fieldEditorNeedsReset]);
121
122 // Changing to identical strings doesn't need a reset.
123 [cell setKeywordHintPrefix:@"Type " image:image suffix:@" to search Engine"
124 availableWidth:kWidth];
125 EXPECT_FALSE([cell fieldEditorNeedsReset]);
126 }
127
128 TEST_F(AutocompleteTextFieldCellTest, KeywordString) {
129 AutocompleteTextFieldCell* cell =
130 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
131
132 // At default settings, everything is already good to go.
133 EXPECT_FALSE([cell fieldEditorNeedsReset]);
134
135 // Setting a keyword string will need a reset.
136 [cell setKeywordString:@"Search Engine:" 88 [cell setKeywordString:@"Search Engine:"
137 partialString:@"Search Eng:" 89 partialString:@"Search Eng:"
138 availableWidth:kWidth]; 90 availableWidth:kWidth];
139 EXPECT_TRUE([cell fieldEditorNeedsReset]); 91 EXPECT_FALSE([cell hintString]);
140 [cell setFieldEditorNeedsReset:NO]; 92 EXPECT_TRUE([cell keywordString]);
141 EXPECT_FALSE([cell fieldEditorNeedsReset]);
142 93
143 // Changing the keyword string needs a reset. 94 // Setting hint clears keyword.
144 [cell setKeywordString:@"Search on Engine:"
145 partialString:@"Search Eng:"
146 availableWidth:kWidth];
147 EXPECT_TRUE([cell fieldEditorNeedsReset]);
148 [cell setFieldEditorNeedsReset:NO];
149 EXPECT_FALSE([cell fieldEditorNeedsReset]);
150
151 // Changing to an identical string doesn't need a reset.
152 [cell setKeywordString:@"Search on Engine:"
153 partialString:@"Search Eng:"
154 availableWidth:kWidth];
155 EXPECT_FALSE([cell fieldEditorNeedsReset]);
156 }
157
158 TEST_F(AutocompleteTextFieldCellTest, SecurityIcon) {
159 AutocompleteTextFieldCell* cell =
160 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
161
162 EXPECT_FALSE([cell fieldEditorNeedsReset]);
163
164 NSImage* image1 = [NSImage imageNamed:@"NSApplicationIcon"];
165 // Setting a security icon will need a reset.
166 [cell setHintIcon:image1 label:nil color:nil];
167 EXPECT_TRUE([cell fieldEditorNeedsReset]);
168 [cell setFieldEditorNeedsReset:NO];
169 EXPECT_FALSE([cell fieldEditorNeedsReset]);
170
171 // Changing the security icon needs a reset.
172 NSImage* image2 = [NSImage imageNamed:@"NSComputer"];
173 [cell setHintIcon:image2 label:nil color:nil];
174 EXPECT_TRUE([cell fieldEditorNeedsReset]);
175 [cell setFieldEditorNeedsReset:NO];
176 EXPECT_FALSE([cell fieldEditorNeedsReset]);
177
178 // Changing to an identical security icon doesn't need a reset.
179 [cell setHintIcon:image2 label:nil color:nil];
180 EXPECT_FALSE([cell fieldEditorNeedsReset]);
181
182 // Adding a label without changing the icon needs a reset.
183 NSColor *color = [NSColor blackColor];
184 [cell setHintIcon:image2 label:@"New Label" color:color];
185 EXPECT_TRUE([cell fieldEditorNeedsReset]);
186
187 // Removing the label without changing the icon needs a reset.
188 [cell setHintIcon:image2 label:nil color:nil];
189 EXPECT_TRUE([cell fieldEditorNeedsReset]);
190 }
191
192 TEST_F(AutocompleteTextFieldCellTest, SecurityIconLabel) {
193 AutocompleteTextFieldCell* cell =
194 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
195 NSColor *color = [NSColor blackColor];
196
197 EXPECT_FALSE([cell fieldEditorNeedsReset]);
198
199 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
200 // Setting a security icon will need a reset.
201 [cell setHintIcon:image label:@"Hello, world" color:color];
202 EXPECT_TRUE([cell fieldEditorNeedsReset]);
203 [cell setFieldEditorNeedsReset:NO];
204 EXPECT_FALSE([cell fieldEditorNeedsReset]);
205
206 // Changing the label needs a reset.
207 [cell setHintIcon:image label:@"Hello, you" color:color];
208 EXPECT_TRUE([cell fieldEditorNeedsReset]);
209 [cell setFieldEditorNeedsReset:NO];
210 EXPECT_FALSE([cell fieldEditorNeedsReset]);
211
212 // Changing to an identical label doesn't need a reset
213 [cell setHintIcon:image label:@"Hello, you" color:color];
214 EXPECT_FALSE([cell fieldEditorNeedsReset]);
215 }
216
217 // Test that transitions between various modes set the reset flag.
218 TEST_F(AutocompleteTextFieldCellTest, Transitions) {
219 AutocompleteTextFieldCell* cell =
220 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
221 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
222
223 // Transitions from hint to keyword string, keyword hint, and
224 // cleared.
225 [cell setSearchHintString:@"Type to search" availableWidth:kWidth];
226 [cell setFieldEditorNeedsReset:NO];
227 EXPECT_FALSE([cell fieldEditorNeedsReset]);
228 [cell setKeywordString:@"Search Engine:"
229 partialString:@"Search Eng:"
230 availableWidth:kWidth];
231 EXPECT_TRUE([cell fieldEditorNeedsReset]);
232
233 [cell setSearchHintString:@"Type to search" availableWidth:kWidth];
234 [cell setFieldEditorNeedsReset:NO];
235 EXPECT_FALSE([cell fieldEditorNeedsReset]);
236 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine" 95 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine"
237 availableWidth:kWidth]; 96 availableWidth:kWidth];
238 EXPECT_TRUE([cell fieldEditorNeedsReset]); 97 EXPECT_TRUE([cell hintString]);
98 EXPECT_FALSE([cell keywordString]);
239 99
100 // Clear clears keyword.
101 [cell clearKeywordAndHint];
102 EXPECT_FALSE([cell hintString]);
103 EXPECT_FALSE([cell keywordString]);
104
105 // Clear clears hint.
240 [cell setSearchHintString:@"Type to search" availableWidth:kWidth]; 106 [cell setSearchHintString:@"Type to search" availableWidth:kWidth];
241 [cell setFieldEditorNeedsReset:NO]; 107 EXPECT_TRUE([cell hintString]);
242 EXPECT_FALSE([cell fieldEditorNeedsReset]);
243 [cell clearKeywordAndHint]; 108 [cell clearKeywordAndHint];
244 EXPECT_TRUE([cell fieldEditorNeedsReset]); 109 EXPECT_FALSE([cell hintString]);
245 110 EXPECT_FALSE([cell keywordString]);
246 // Transitions from keyword hint to keyword string, simple hint, and
247 // cleared.
248 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine"
249 availableWidth:kWidth];
250 [cell setFieldEditorNeedsReset:NO];
251 EXPECT_FALSE([cell fieldEditorNeedsReset]);
252 [cell setSearchHintString:@"Type to search" availableWidth:kWidth];
253 EXPECT_TRUE([cell fieldEditorNeedsReset]);
254
255 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine"
256 availableWidth:kWidth];
257 [cell setFieldEditorNeedsReset:NO];
258 EXPECT_FALSE([cell fieldEditorNeedsReset]);
259 [cell setKeywordString:@"Search Engine:"
260 partialString:@"Search Eng:"
261 availableWidth:kWidth];
262 EXPECT_TRUE([cell fieldEditorNeedsReset]);
263
264 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine"
265 availableWidth:kWidth];
266 [cell setFieldEditorNeedsReset:NO];
267 EXPECT_FALSE([cell fieldEditorNeedsReset]);
268 [cell clearKeywordAndHint];
269 EXPECT_TRUE([cell fieldEditorNeedsReset]);
270
271 // Transitions from keyword string to either type of hint, or
272 // cleared.
273 [cell setKeywordString:@"Search on Engine:"
274 partialString:@"Search Eng:"
275 availableWidth:kWidth];
276 [cell setFieldEditorNeedsReset:NO];
277 EXPECT_FALSE([cell fieldEditorNeedsReset]);
278 [cell setSearchHintString:@"Type to search" availableWidth:kWidth];
279 EXPECT_TRUE([cell fieldEditorNeedsReset]);
280
281 [cell setKeywordString:@"Search on Engine:"
282 partialString:@"Search Eng:"
283 availableWidth:kWidth];
284 [cell setFieldEditorNeedsReset:NO];
285 EXPECT_FALSE([cell fieldEditorNeedsReset]);
286 [cell setKeywordHintPrefix:@"Press " image:image suffix:@" to search Engine"
287 availableWidth:kWidth];
288 EXPECT_TRUE([cell fieldEditorNeedsReset]);
289
290 [cell setKeywordString:@"Search on Engine:"
291 partialString:@"Search Eng:"
292 availableWidth:kWidth];
293 [cell setFieldEditorNeedsReset:NO];
294 EXPECT_FALSE([cell fieldEditorNeedsReset]);
295 [cell clearKeywordAndHint];
296 EXPECT_TRUE([cell fieldEditorNeedsReset]);
297 } 111 }
298 112
299 TEST_F(AutocompleteTextFieldCellTest, TextFrame) { 113 TEST_F(AutocompleteTextFieldCellTest, TextFrame) {
300 AutocompleteTextFieldCell* cell = 114 AutocompleteTextFieldCell* cell =
301 static_cast<AutocompleteTextFieldCell*>([view_ cell]); 115 static_cast<AutocompleteTextFieldCell*>([view_ cell]);
302 const NSRect bounds([view_ bounds]); 116 const NSRect bounds([view_ bounds]);
303 NSRect textFrame; 117 NSRect textFrame;
304 118
305 // The cursor frame should stay the same throughout. 119 // The cursor frame should stay the same throughout.
306 const NSRect cursorFrame([cell textCursorFrameForFrame:bounds]); 120 const NSRect cursorFrame([cell textCursorFrameForFrame:bounds]);
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 EXPECT_TRUE([[[cell hintString] string] hasPrefix:kHintPrefix]); 344 EXPECT_TRUE([[[cell hintString] string] hasPrefix:kHintPrefix]);
531 EXPECT_TRUE([[[cell hintString] string] hasSuffix:kHintSuffix]); 345 EXPECT_TRUE([[[cell hintString] string] hasSuffix:kHintSuffix]);
532 346
533 // Narrow width suppresses everything but the image. 347 // Narrow width suppresses everything but the image.
534 [cell setKeywordHintPrefix:kHintPrefix image:image suffix:kHintSuffix 348 [cell setKeywordHintPrefix:kHintPrefix image:image suffix:kHintSuffix
535 availableWidth:kNarrowWidth]; 349 availableWidth:kNarrowWidth];
536 EXPECT_EQ([[cell hintString] length], 1U); 350 EXPECT_EQ([[cell hintString] length], 1U);
537 } 351 }
538 352
539 } // namespace 353 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/autocomplete_text_field_cell.mm ('k') | chrome/browser/cocoa/autocomplete_text_field_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698