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 "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" | 5 #import "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" |
6 | 6 |
7 #include "app/text_elider.h" | 7 #include "app/text_elider.h" |
8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
10 #include "chrome/browser/autocomplete/autocomplete.h" | 10 #include "chrome/browser/autocomplete/autocomplete.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 } | 93 } |
94 | 94 |
95 NSColor* color_; // weak | 95 NSColor* color_; // weak |
96 gfx::Font font_; | 96 gfx::Font font_; |
97 }; | 97 }; |
98 | 98 |
99 // Simple inputs with no matches should result in styled output who's | 99 // Simple inputs with no matches should result in styled output who's |
100 // text matches the input string, with the passed-in color, and | 100 // text matches the input string, with the passed-in color, and |
101 // nothing bolded. | 101 // nothing bolded. |
102 TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringNoMatch) { | 102 TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringNoMatch) { |
103 const NSString* string = @"This is a test"; | 103 NSString* const string = @"This is a test"; |
104 AutocompleteMatch::ACMatchClassifications classifications; | 104 AutocompleteMatch::ACMatchClassifications classifications; |
105 | 105 |
106 NSAttributedString* decorated = | 106 NSAttributedString* decorated = |
107 AutocompletePopupViewMac::DecorateMatchedString( | 107 AutocompletePopupViewMac::DecorateMatchedString( |
108 base::SysNSStringToWide(string), classifications, | 108 base::SysNSStringToWide(string), classifications, |
109 color_, font_); | 109 color_, font_); |
110 | 110 |
111 // Result has same characters as the input. | 111 // Result has same characters as the input. |
112 EXPECT_EQ([decorated length], [string length]); | 112 EXPECT_EQ([decorated length], [string length]); |
113 EXPECT_TRUE([[decorated string] isEqualToString:string]); | 113 EXPECT_TRUE([[decorated string] isEqualToString:string]); |
114 | 114 |
115 // Our passed-in color for the entire string. | 115 // Our passed-in color for the entire string. |
116 EXPECT_EQ(RunLengthForAttribute(decorated, 0U, | 116 EXPECT_EQ(RunLengthForAttribute(decorated, 0U, |
117 NSForegroundColorAttributeName), | 117 NSForegroundColorAttributeName), |
118 [string length]); | 118 [string length]); |
119 EXPECT_TRUE(RunHasColor(decorated, 0U, color_)); | 119 EXPECT_TRUE(RunHasColor(decorated, 0U, color_)); |
120 | 120 |
121 // An unbolded font for the entire string. | 121 // An unbolded font for the entire string. |
122 EXPECT_EQ(RunLengthForAttribute(decorated, 0U, | 122 EXPECT_EQ(RunLengthForAttribute(decorated, 0U, |
123 NSFontAttributeName), [string length]); | 123 NSFontAttributeName), [string length]); |
124 EXPECT_FALSE(RunHasFontTrait(decorated, 0U, NSBoldFontMask)); | 124 EXPECT_FALSE(RunHasFontTrait(decorated, 0U, NSBoldFontMask)); |
125 } | 125 } |
126 | 126 |
127 // Identical to DecorateMatchedStringNoMatch, except test that URL | 127 // Identical to DecorateMatchedStringNoMatch, except test that URL |
128 // style gets a different color than we passed in. | 128 // style gets a different color than we passed in. |
129 TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringURLNoMatch) { | 129 TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringURLNoMatch) { |
130 const NSString* string = @"This is a test"; | 130 NSString* const string = @"This is a test"; |
131 AutocompleteMatch::ACMatchClassifications classifications; | 131 AutocompleteMatch::ACMatchClassifications classifications; |
132 | 132 |
133 classifications.push_back( | 133 classifications.push_back( |
134 ACMatchClassification(0, ACMatchClassification::URL)); | 134 ACMatchClassification(0, ACMatchClassification::URL)); |
135 | 135 |
136 NSAttributedString* decorated = | 136 NSAttributedString* decorated = |
137 AutocompletePopupViewMac::DecorateMatchedString( | 137 AutocompletePopupViewMac::DecorateMatchedString( |
138 base::SysNSStringToWide(string), classifications, | 138 base::SysNSStringToWide(string), classifications, |
139 color_, font_); | 139 color_, font_); |
140 | 140 |
141 // Result has same characters as the input. | 141 // Result has same characters as the input. |
142 EXPECT_EQ([decorated length], [string length]); | 142 EXPECT_EQ([decorated length], [string length]); |
143 EXPECT_TRUE([[decorated string] isEqualToString:string]); | 143 EXPECT_TRUE([[decorated string] isEqualToString:string]); |
144 | 144 |
145 // One color for the entire string, and it's not the one we passed in. | 145 // One color for the entire string, and it's not the one we passed in. |
146 EXPECT_EQ(RunLengthForAttribute(decorated, 0U, | 146 EXPECT_EQ(RunLengthForAttribute(decorated, 0U, |
147 NSForegroundColorAttributeName), | 147 NSForegroundColorAttributeName), |
148 [string length]); | 148 [string length]); |
149 EXPECT_FALSE(RunHasColor(decorated, 0U, color_)); | 149 EXPECT_FALSE(RunHasColor(decorated, 0U, color_)); |
150 | 150 |
151 // An unbolded font for the entire string. | 151 // An unbolded font for the entire string. |
152 EXPECT_EQ(RunLengthForAttribute(decorated, 0U, | 152 EXPECT_EQ(RunLengthForAttribute(decorated, 0U, |
153 NSFontAttributeName), [string length]); | 153 NSFontAttributeName), [string length]); |
154 EXPECT_FALSE(RunHasFontTrait(decorated, 0U, NSBoldFontMask)); | 154 EXPECT_FALSE(RunHasFontTrait(decorated, 0U, NSBoldFontMask)); |
155 } | 155 } |
156 | 156 |
157 // Test that DIM doesn't have any impact - true at this time. | 157 // Test that DIM doesn't have any impact - true at this time. |
158 TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringDimNoMatch) { | 158 TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringDimNoMatch) { |
159 const NSString* string = @"This is a test"; | 159 NSString* const string = @"This is a test"; |
160 | 160 |
161 // Switch to DIM halfway through. | 161 // Switch to DIM halfway through. |
162 AutocompleteMatch::ACMatchClassifications classifications; | 162 AutocompleteMatch::ACMatchClassifications classifications; |
163 classifications.push_back( | 163 classifications.push_back( |
164 ACMatchClassification(0, ACMatchClassification::NONE)); | 164 ACMatchClassification(0, ACMatchClassification::NONE)); |
165 classifications.push_back( | 165 classifications.push_back( |
166 ACMatchClassification([string length] / 2, ACMatchClassification::DIM)); | 166 ACMatchClassification([string length] / 2, ACMatchClassification::DIM)); |
167 | 167 |
168 NSAttributedString* decorated = | 168 NSAttributedString* decorated = |
169 AutocompletePopupViewMac::DecorateMatchedString( | 169 AutocompletePopupViewMac::DecorateMatchedString( |
(...skipping 12 matching lines...) Expand all Loading... |
182 | 182 |
183 // An unbolded font for the entire string. | 183 // An unbolded font for the entire string. |
184 EXPECT_EQ(RunLengthForAttribute(decorated, 0U, | 184 EXPECT_EQ(RunLengthForAttribute(decorated, 0U, |
185 NSFontAttributeName), [string length]); | 185 NSFontAttributeName), [string length]); |
186 EXPECT_FALSE(RunHasFontTrait(decorated, 0U, NSBoldFontMask)); | 186 EXPECT_FALSE(RunHasFontTrait(decorated, 0U, NSBoldFontMask)); |
187 } | 187 } |
188 | 188 |
189 // Test that the matched run gets bold-faced, but keeps the same | 189 // Test that the matched run gets bold-faced, but keeps the same |
190 // color. | 190 // color. |
191 TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringMatch) { | 191 TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringMatch) { |
192 const NSString* string = @"This is a test"; | 192 NSString* const string = @"This is a test"; |
193 // Match "is". | 193 // Match "is". |
194 const NSUInteger runLength1 = 5, runLength2 = 2, runLength3 = 7; | 194 const NSUInteger runLength1 = 5, runLength2 = 2, runLength3 = 7; |
195 // Make sure nobody messed up the inputs. | 195 // Make sure nobody messed up the inputs. |
196 EXPECT_EQ(runLength1 + runLength2 + runLength3, [string length]); | 196 EXPECT_EQ(runLength1 + runLength2 + runLength3, [string length]); |
197 | 197 |
198 // Push each run onto classifications. | 198 // Push each run onto classifications. |
199 AutocompleteMatch::ACMatchClassifications classifications; | 199 AutocompleteMatch::ACMatchClassifications classifications; |
200 classifications.push_back( | 200 classifications.push_back( |
201 ACMatchClassification(0, ACMatchClassification::NONE)); | 201 ACMatchClassification(0, ACMatchClassification::NONE)); |
202 classifications.push_back( | 202 classifications.push_back( |
(...skipping 27 matching lines...) Expand all Loading... |
230 EXPECT_TRUE(RunHasFontTrait(decorated, runLength1, NSBoldFontMask)); | 230 EXPECT_TRUE(RunHasFontTrait(decorated, runLength1, NSBoldFontMask)); |
231 | 231 |
232 EXPECT_EQ(RunLengthForAttribute(decorated, runLength1 + runLength2, | 232 EXPECT_EQ(RunLengthForAttribute(decorated, runLength1 + runLength2, |
233 NSFontAttributeName), runLength3); | 233 NSFontAttributeName), runLength3); |
234 EXPECT_FALSE(RunHasFontTrait(decorated, runLength1 + runLength2, | 234 EXPECT_FALSE(RunHasFontTrait(decorated, runLength1 + runLength2, |
235 NSBoldFontMask)); | 235 NSBoldFontMask)); |
236 } | 236 } |
237 | 237 |
238 // Just like DecorateMatchedStringURLMatch, this time with URL style. | 238 // Just like DecorateMatchedStringURLMatch, this time with URL style. |
239 TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringURLMatch) { | 239 TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringURLMatch) { |
240 const NSString* string = @"http://hello.world/"; | 240 NSString* const string = @"http://hello.world/"; |
241 // Match "hello". | 241 // Match "hello". |
242 const NSUInteger runLength1 = 7, runLength2 = 5, runLength3 = 7; | 242 const NSUInteger runLength1 = 7, runLength2 = 5, runLength3 = 7; |
243 // Make sure nobody messed up the inputs. | 243 // Make sure nobody messed up the inputs. |
244 EXPECT_EQ(runLength1 + runLength2 + runLength3, [string length]); | 244 EXPECT_EQ(runLength1 + runLength2 + runLength3, [string length]); |
245 | 245 |
246 // Push each run onto classifications. | 246 // Push each run onto classifications. |
247 AutocompleteMatch::ACMatchClassifications classifications; | 247 AutocompleteMatch::ACMatchClassifications classifications; |
248 classifications.push_back( | 248 classifications.push_back( |
249 ACMatchClassification(0, ACMatchClassification::URL)); | 249 ACMatchClassification(0, ACMatchClassification::URL)); |
250 const int kURLMatch = ACMatchClassification::URL|ACMatchClassification::MATCH; | 250 const int kURLMatch = ACMatchClassification::URL|ACMatchClassification::MATCH; |
(...skipping 30 matching lines...) Expand all Loading... |
281 NSFontAttributeName), runLength3); | 281 NSFontAttributeName), runLength3); |
282 EXPECT_FALSE(RunHasFontTrait(decorated, runLength1 + runLength2, | 282 EXPECT_FALSE(RunHasFontTrait(decorated, runLength1 + runLength2, |
283 NSBoldFontMask)); | 283 NSBoldFontMask)); |
284 } | 284 } |
285 | 285 |
286 // Check that matches with both contents and description come back | 286 // Check that matches with both contents and description come back |
287 // with contents at the beginning, description at the end, and | 287 // with contents at the beginning, description at the end, and |
288 // something separating them. Not being specific about the separator | 288 // something separating them. Not being specific about the separator |
289 // on purpose, in case it changes. | 289 // on purpose, in case it changes. |
290 TEST_F(AutocompletePopupViewMacTest, MatchText) { | 290 TEST_F(AutocompletePopupViewMacTest, MatchText) { |
291 const NSString* contents = @"contents"; | 291 NSString* const contents = @"contents"; |
292 const NSString* description = @"description"; | 292 NSString* const description = @"description"; |
293 AutocompleteMatch m = MakeMatch(base::SysNSStringToWide(contents), | 293 AutocompleteMatch m = MakeMatch(base::SysNSStringToWide(contents), |
294 base::SysNSStringToWide(description)); | 294 base::SysNSStringToWide(description)); |
295 | 295 |
296 NSAttributedString* decorated = | 296 NSAttributedString* decorated = |
297 AutocompletePopupViewMac::MatchText(m, font_, kLargeWidth); | 297 AutocompletePopupViewMac::MatchText(m, font_, kLargeWidth); |
298 | 298 |
299 // Result contains the characters of the input in the right places. | 299 // Result contains the characters of the input in the right places. |
300 EXPECT_GT([decorated length], [contents length] + [description length]); | 300 EXPECT_GT([decorated length], [contents length] + [description length]); |
301 EXPECT_TRUE([[decorated string] hasPrefix:contents]); | 301 EXPECT_TRUE([[decorated string] hasPrefix:contents]); |
302 EXPECT_TRUE([[decorated string] hasSuffix:description]); | 302 EXPECT_TRUE([[decorated string] hasSuffix:description]); |
(...skipping 10 matching lines...) Expand all Loading... |
313 [description length]); | 313 [description length]); |
314 | 314 |
315 // Same font all the way through, nothing bold. | 315 // Same font all the way through, nothing bold. |
316 EXPECT_EQ(RunLengthForAttribute(decorated, 0U, | 316 EXPECT_EQ(RunLengthForAttribute(decorated, 0U, |
317 NSFontAttributeName), [decorated length]); | 317 NSFontAttributeName), [decorated length]); |
318 EXPECT_FALSE(RunHasFontTrait(decorated, 0, NSBoldFontMask)); | 318 EXPECT_FALSE(RunHasFontTrait(decorated, 0, NSBoldFontMask)); |
319 } | 319 } |
320 | 320 |
321 // Check that MatchText() styles content matches as expected. | 321 // Check that MatchText() styles content matches as expected. |
322 TEST_F(AutocompletePopupViewMacTest, MatchTextContentsMatch) { | 322 TEST_F(AutocompletePopupViewMacTest, MatchTextContentsMatch) { |
323 const NSString* contents = @"This is a test"; | 323 NSString* const contents = @"This is a test"; |
324 // Match "is". | 324 // Match "is". |
325 const NSUInteger runLength1 = 5, runLength2 = 2, runLength3 = 7; | 325 const NSUInteger runLength1 = 5, runLength2 = 2, runLength3 = 7; |
326 // Make sure nobody messed up the inputs. | 326 // Make sure nobody messed up the inputs. |
327 EXPECT_EQ(runLength1 + runLength2 + runLength3, [contents length]); | 327 EXPECT_EQ(runLength1 + runLength2 + runLength3, [contents length]); |
328 | 328 |
329 AutocompleteMatch m = MakeMatch(base::SysNSStringToWide(contents), | 329 AutocompleteMatch m = MakeMatch(base::SysNSStringToWide(contents), |
330 std::wstring()); | 330 std::wstring()); |
331 | 331 |
332 // Push each run onto contents classifications. | 332 // Push each run onto contents classifications. |
333 m.contents_class.push_back( | 333 m.contents_class.push_back( |
(...skipping 26 matching lines...) Expand all Loading... |
360 EXPECT_TRUE(RunHasFontTrait(decorated, runLength1, NSBoldFontMask)); | 360 EXPECT_TRUE(RunHasFontTrait(decorated, runLength1, NSBoldFontMask)); |
361 | 361 |
362 EXPECT_EQ(RunLengthForAttribute(decorated, runLength1 + runLength2, | 362 EXPECT_EQ(RunLengthForAttribute(decorated, runLength1 + runLength2, |
363 NSFontAttributeName), runLength3); | 363 NSFontAttributeName), runLength3); |
364 EXPECT_FALSE(RunHasFontTrait(decorated, runLength1 + runLength2, | 364 EXPECT_FALSE(RunHasFontTrait(decorated, runLength1 + runLength2, |
365 NSBoldFontMask)); | 365 NSBoldFontMask)); |
366 } | 366 } |
367 | 367 |
368 // Check that MatchText() styles description matches as expected. | 368 // Check that MatchText() styles description matches as expected. |
369 TEST_F(AutocompletePopupViewMacTest, MatchTextDescriptionMatch) { | 369 TEST_F(AutocompletePopupViewMacTest, MatchTextDescriptionMatch) { |
370 const NSString* contents = @"This is a test"; | 370 NSString* const contents = @"This is a test"; |
371 const NSString* description = @"That was a test"; | 371 NSString* const description = @"That was a test"; |
372 // Match "That was". | 372 // Match "That was". |
373 const NSUInteger runLength1 = 8, runLength2 = 7; | 373 const NSUInteger runLength1 = 8, runLength2 = 7; |
374 // Make sure nobody messed up the inputs. | 374 // Make sure nobody messed up the inputs. |
375 EXPECT_EQ(runLength1 + runLength2, [description length]); | 375 EXPECT_EQ(runLength1 + runLength2, [description length]); |
376 | 376 |
377 AutocompleteMatch m = MakeMatch(base::SysNSStringToWide(contents), | 377 AutocompleteMatch m = MakeMatch(base::SysNSStringToWide(contents), |
378 base::SysNSStringToWide(description)); | 378 base::SysNSStringToWide(description)); |
379 | 379 |
380 // Push each run onto contents classifications. | 380 // Push each run onto contents classifications. |
381 m.description_class.push_back( | 381 m.description_class.push_back( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 NSFontAttributeName), runLength1); | 413 NSFontAttributeName), runLength1); |
414 EXPECT_TRUE(RunHasFontTrait(decorated, descriptionLocation, NSBoldFontMask)); | 414 EXPECT_TRUE(RunHasFontTrait(decorated, descriptionLocation, NSBoldFontMask)); |
415 | 415 |
416 EXPECT_EQ(RunLengthForAttribute(decorated, descriptionLocation + runLength1, | 416 EXPECT_EQ(RunLengthForAttribute(decorated, descriptionLocation + runLength1, |
417 NSFontAttributeName), runLength2); | 417 NSFontAttributeName), runLength2); |
418 EXPECT_FALSE(RunHasFontTrait(decorated, descriptionLocation + runLength1, | 418 EXPECT_FALSE(RunHasFontTrait(decorated, descriptionLocation + runLength1, |
419 NSBoldFontMask)); | 419 NSBoldFontMask)); |
420 } | 420 } |
421 | 421 |
422 TEST_F(AutocompletePopupViewMacTest, ElideString) { | 422 TEST_F(AutocompletePopupViewMacTest, ElideString) { |
423 const NSString* contents = @"This is a test with long contents"; | 423 NSString* const contents = @"This is a test with long contents"; |
424 const std::wstring wideContents(base::SysNSStringToWide(contents)); | 424 const std::wstring wideContents(base::SysNSStringToWide(contents)); |
425 | 425 |
426 const float kWide = 1000.0; | 426 const float kWide = 1000.0; |
427 const float kNarrow = 20.0; | 427 const float kNarrow = 20.0; |
428 | 428 |
429 NSDictionary* attributes = | 429 NSDictionary* attributes = |
430 [NSDictionary dictionaryWithObject:font_.nativeFont() | 430 [NSDictionary dictionaryWithObject:font_.nativeFont() |
431 forKey:NSFontAttributeName]; | 431 forKey:NSFontAttributeName]; |
432 scoped_nsobject<NSMutableAttributedString> as( | 432 scoped_nsobject<NSMutableAttributedString> as( |
433 [[NSMutableAttributedString alloc] initWithString:contents | 433 [[NSMutableAttributedString alloc] initWithString:contents |
(...skipping 14 matching lines...) Expand all Loading... |
448 | 448 |
449 // When elided, result is the same as ElideText(). | 449 // When elided, result is the same as ElideText(). |
450 ret = AutocompletePopupViewMac::ElideString(as, wideContents, font_, 0.0); | 450 ret = AutocompletePopupViewMac::ElideString(as, wideContents, font_, 0.0); |
451 elided = ElideText(wideContents, font_, 0.0, false); | 451 elided = ElideText(wideContents, font_, 0.0, false); |
452 EXPECT_TRUE(ret == as); | 452 EXPECT_TRUE(ret == as); |
453 EXPECT_FALSE([[as string] isEqualToString:contents]); | 453 EXPECT_FALSE([[as string] isEqualToString:contents]); |
454 EXPECT_TRUE([[as string] isEqualToString:base::SysWideToNSString(elided)]); | 454 EXPECT_TRUE([[as string] isEqualToString:base::SysWideToNSString(elided)]); |
455 } | 455 } |
456 | 456 |
457 TEST_F(AutocompletePopupViewMacTest, MatchTextElide) { | 457 TEST_F(AutocompletePopupViewMacTest, MatchTextElide) { |
458 const NSString* contents = @"This is a test with long contents"; | 458 NSString* const contents = @"This is a test with long contents"; |
459 const NSString* description = @"That was a test"; | 459 NSString* const description = @"That was a test"; |
460 // Match "long". | 460 // Match "long". |
461 const NSUInteger runLength1 = 20, runLength2 = 4, runLength3 = 9; | 461 const NSUInteger runLength1 = 20, runLength2 = 4, runLength3 = 9; |
462 // Make sure nobody messed up the inputs. | 462 // Make sure nobody messed up the inputs. |
463 EXPECT_EQ(runLength1 + runLength2 + runLength3, [contents length]); | 463 EXPECT_EQ(runLength1 + runLength2 + runLength3, [contents length]); |
464 | 464 |
465 AutocompleteMatch m = MakeMatch(base::SysNSStringToWide(contents), | 465 AutocompleteMatch m = MakeMatch(base::SysNSStringToWide(contents), |
466 base::SysNSStringToWide(description)); | 466 base::SysNSStringToWide(description)); |
467 | 467 |
468 // Push each run onto contents classifications. | 468 // Push each run onto contents classifications. |
469 m.contents_class.push_back( | 469 m.contents_class.push_back( |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 // updates the model's selection from the matrix before returning. | 523 // updates the model's selection from the matrix before returning. |
524 // Could possibly test that via -select:. | 524 // Could possibly test that via -select:. |
525 | 525 |
526 // TODO(shess): Test that AutocompleteButtonCell returns the right | 526 // TODO(shess): Test that AutocompleteButtonCell returns the right |
527 // background colors for on, highlighted, and neither. | 527 // background colors for on, highlighted, and neither. |
528 | 528 |
529 // TODO(shess): Test that AutocompleteMatrixTarget can be initialized | 529 // TODO(shess): Test that AutocompleteMatrixTarget can be initialized |
530 // and then sends -select: to the view. | 530 // and then sends -select: to the view. |
531 | 531 |
532 } // namespace | 532 } // namespace |
OLD | NEW |