1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/autocomplete/autocomplete_popup_view_mac.h"
#include "base/scoped_ptr.h"
#include "base/sys_string_conversions.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/autocomplete/autocomplete.h"
#include "testing/platform_test.h"
#include "ui/base/text/text_elider.h"
namespace {
const float kLargeWidth = 10000;
class AutocompletePopupViewMacTest : public PlatformTest {
public:
AutocompletePopupViewMacTest() {}
virtual void SetUp() {
PlatformTest::SetUp();
// These are here because there is no autorelease pool for the
// constructor.
color_ = [NSColor blackColor];
dimColor_ = [NSColor darkGrayColor];
font_ = gfx::Font(
base::SysNSStringToUTF16([[NSFont userFontOfSize:12] fontName]), 12);
}
// Returns the length of the run starting at |location| for which
// |attributeName| remains the same.
static NSUInteger RunLengthForAttribute(NSAttributedString* string,
NSUInteger location,
NSString* attributeName) {
const NSRange fullRange = NSMakeRange(0, [string length]);
NSRange range;
[string attribute:attributeName
atIndex:location longestEffectiveRange:&range inRange:fullRange];
// In order to signal when the run doesn't start exactly at
// location, return a weirdo length. This causes the incorrect
// expectation to manifest at the calling location, which is more
// useful than an EXPECT_EQ() would be here.
if (range.location != location) {
return -1;
}
return range.length;
}
// Return true if the run starting at |location| has |color| for
// attribute NSForegroundColorAttributeName.
static bool RunHasColor(NSAttributedString* string,
NSUInteger location, NSColor* color) {
const NSRange fullRange = NSMakeRange(0, [string length]);
NSRange range;
NSColor* runColor = [string attribute:NSForegroundColorAttributeName
atIndex:location
longestEffectiveRange:&range inRange:fullRange];
// According to one "Ali Ozer", you can compare objects within the
// same color space using -isEqual:. Converting color spaces
// seems too heavyweight for these tests.
// http://lists.apple.com/archives/cocoa-dev/2005/May/msg00186.html
return [runColor isEqual:color] ? true : false;
}
// Return true if the run starting at |location| has the font
// trait(s) in |mask| font in NSFontAttributeName.
static bool RunHasFontTrait(NSAttributedString* string, NSUInteger location,
NSFontTraitMask mask) {
const NSRange fullRange = NSMakeRange(0, [string length]);
NSRange range;
NSFont* runFont = [string attribute:NSFontAttributeName
atIndex:location
longestEffectiveRange:&range inRange:fullRange];
NSFontManager* fontManager = [NSFontManager sharedFontManager];
if (runFont && (mask == ([fontManager traitsOfFont:runFont]&mask))) {
return true;
}
return false;
}
// AutocompleteMatch doesn't really have the right constructor for our
// needs. Fake one for us to use.
static AutocompleteMatch MakeMatch(const string16 &contents,
const string16 &description) {
AutocompleteMatch m(NULL, 1, true, AutocompleteMatch::URL_WHAT_YOU_TYPED);
m.contents = contents;
m.description = description;
return m;
}
NSColor* color_; // weak
NSColor* dimColor_; // weak
gfx::Font font_;
};
// Simple inputs with no matches should result in styled output who's
// text matches the input string, with the passed-in color, and
// nothing bolded.
TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringNoMatch) {
NSString* const string = @"This is a test";
AutocompleteMatch::ACMatchClassifications classifications;
NSAttributedString* decorated =
AutocompletePopupViewMac::DecorateMatchedString(
base::SysNSStringToUTF16(string), classifications,
color_, dimColor_, font_);
// Result has same characters as the input.
EXPECT_EQ([decorated length], [string length]);
EXPECT_TRUE([[decorated string] isEqualToString:string]);
// Our passed-in color for the entire string.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSForegroundColorAttributeName),
[string length]);
EXPECT_TRUE(RunHasColor(decorated, 0U, color_));
// An unbolded font for the entire string.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSFontAttributeName), [string length]);
EXPECT_FALSE(RunHasFontTrait(decorated, 0U, NSBoldFontMask));
}
// Identical to DecorateMatchedStringNoMatch, except test that URL
// style gets a different color than we passed in.
TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringURLNoMatch) {
NSString* const string = @"This is a test";
AutocompleteMatch::ACMatchClassifications classifications;
classifications.push_back(
ACMatchClassification(0, ACMatchClassification::URL));
NSAttributedString* decorated =
AutocompletePopupViewMac::DecorateMatchedString(
base::SysNSStringToUTF16(string), classifications,
color_, dimColor_, font_);
// Result has same characters as the input.
EXPECT_EQ([decorated length], [string length]);
EXPECT_TRUE([[decorated string] isEqualToString:string]);
// One color for the entire string, and it's not the one we passed in.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSForegroundColorAttributeName),
[string length]);
EXPECT_FALSE(RunHasColor(decorated, 0U, color_));
// An unbolded font for the entire string.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSFontAttributeName), [string length]);
EXPECT_FALSE(RunHasFontTrait(decorated, 0U, NSBoldFontMask));
}
// Test that DIM works as expected.
TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringDimNoMatch) {
NSString* const string = @"This is a test";
// Dim "is".
const NSUInteger runLength1 = 5, runLength2 = 2, runLength3 = 7;
// Make sure nobody messed up the inputs.
EXPECT_EQ(runLength1 + runLength2 + runLength3, [string length]);
// Push each run onto classifications.
AutocompleteMatch::ACMatchClassifications classifications;
classifications.push_back(
ACMatchClassification(0, ACMatchClassification::NONE));
classifications.push_back(
ACMatchClassification(runLength1, ACMatchClassification::DIM));
classifications.push_back(
ACMatchClassification(runLength1 + runLength2,
ACMatchClassification::NONE));
NSAttributedString* decorated =
AutocompletePopupViewMac::DecorateMatchedString(
base::SysNSStringToUTF16(string), classifications,
color_, dimColor_, font_);
// Result has same characters as the input.
EXPECT_EQ([decorated length], [string length]);
EXPECT_TRUE([[decorated string] isEqualToString:string]);
// Should have three font runs, normal, dim, normal.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSForegroundColorAttributeName),
runLength1);
EXPECT_TRUE(RunHasColor(decorated, 0U, color_));
EXPECT_EQ(RunLengthForAttribute(decorated, runLength1,
NSForegroundColorAttributeName),
runLength2);
EXPECT_TRUE(RunHasColor(decorated, runLength1, dimColor_));
EXPECT_EQ(RunLengthForAttribute(decorated, runLength1 + runLength2,
NSForegroundColorAttributeName),
runLength3);
EXPECT_TRUE(RunHasColor(decorated, runLength1 + runLength2, color_));
// An unbolded font for the entire string.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSFontAttributeName), [string length]);
EXPECT_FALSE(RunHasFontTrait(decorated, 0U, NSBoldFontMask));
}
// Test that the matched run gets bold-faced, but keeps the same
// color.
TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringMatch) {
NSString* const string = @"This is a test";
// Match "is".
const NSUInteger runLength1 = 5, runLength2 = 2, runLength3 = 7;
// Make sure nobody messed up the inputs.
EXPECT_EQ(runLength1 + runLength2 + runLength3, [string length]);
// Push each run onto classifications.
AutocompleteMatch::ACMatchClassifications classifications;
classifications.push_back(
ACMatchClassification(0, ACMatchClassification::NONE));
classifications.push_back(
ACMatchClassification(runLength1, ACMatchClassification::MATCH));
classifications.push_back(
ACMatchClassification(runLength1 + runLength2,
ACMatchClassification::NONE));
NSAttributedString* decorated =
AutocompletePopupViewMac::DecorateMatchedString(
base::SysNSStringToUTF16(string), classifications,
color_, dimColor_, font_);
// Result has same characters as the input.
EXPECT_EQ([decorated length], [string length]);
EXPECT_TRUE([[decorated string] isEqualToString:string]);
// Our passed-in color for the entire string.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSForegroundColorAttributeName),
[string length]);
EXPECT_TRUE(RunHasColor(decorated, 0U, color_));
// Should have three font runs, not bold, bold, then not bold again.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSFontAttributeName), runLength1);
EXPECT_FALSE(RunHasFontTrait(decorated, 0U, NSBoldFontMask));
EXPECT_EQ(RunLengthForAttribute(decorated, runLength1,
NSFontAttributeName), runLength2);
EXPECT_TRUE(RunHasFontTrait(decorated, runLength1, NSBoldFontMask));
EXPECT_EQ(RunLengthForAttribute(decorated, runLength1 + runLength2,
NSFontAttributeName), runLength3);
EXPECT_FALSE(RunHasFontTrait(decorated, runLength1 + runLength2,
NSBoldFontMask));
}
// Just like DecorateMatchedStringURLMatch, this time with URL style.
TEST_F(AutocompletePopupViewMacTest, DecorateMatchedStringURLMatch) {
NSString* const string = @"http://hello.world/";
// Match "hello".
const NSUInteger runLength1 = 7, runLength2 = 5, runLength3 = 7;
// Make sure nobody messed up the inputs.
EXPECT_EQ(runLength1 + runLength2 + runLength3, [string length]);
// Push each run onto classifications.
AutocompleteMatch::ACMatchClassifications classifications;
classifications.push_back(
ACMatchClassification(0, ACMatchClassification::URL));
const int kURLMatch = ACMatchClassification::URL|ACMatchClassification::MATCH;
classifications.push_back(ACMatchClassification(runLength1, kURLMatch));
classifications.push_back(
ACMatchClassification(runLength1 + runLength2,
ACMatchClassification::URL));
NSAttributedString* decorated =
AutocompletePopupViewMac::DecorateMatchedString(
base::SysNSStringToUTF16(string), classifications,
color_, dimColor_, font_);
// Result has same characters as the input.
EXPECT_EQ([decorated length], [string length]);
EXPECT_TRUE([[decorated string] isEqualToString:string]);
// One color for the entire string, and it's not the one we passed in.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSForegroundColorAttributeName),
[string length]);
EXPECT_FALSE(RunHasColor(decorated, 0U, color_));
// Should have three font runs, not bold, bold, then not bold again.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSFontAttributeName), runLength1);
EXPECT_FALSE(RunHasFontTrait(decorated, 0U, NSBoldFontMask));
EXPECT_EQ(RunLengthForAttribute(decorated, runLength1,
NSFontAttributeName), runLength2);
EXPECT_TRUE(RunHasFontTrait(decorated, runLength1, NSBoldFontMask));
EXPECT_EQ(RunLengthForAttribute(decorated, runLength1 + runLength2,
NSFontAttributeName), runLength3);
EXPECT_FALSE(RunHasFontTrait(decorated, runLength1 + runLength2,
NSBoldFontMask));
}
// Check that matches with both contents and description come back
// with contents at the beginning, description at the end, and
// something separating them. Not being specific about the separator
// on purpose, in case it changes.
TEST_F(AutocompletePopupViewMacTest, MatchText) {
NSString* const contents = @"contents";
NSString* const description = @"description";
AutocompleteMatch m = MakeMatch(base::SysNSStringToUTF16(contents),
base::SysNSStringToUTF16(description));
NSAttributedString* decorated =
AutocompletePopupViewMac::MatchText(m, font_, kLargeWidth);
// Result contains the characters of the input in the right places.
EXPECT_GT([decorated length], [contents length] + [description length]);
EXPECT_TRUE([[decorated string] hasPrefix:contents]);
EXPECT_TRUE([[decorated string] hasSuffix:description]);
// Check that the description is a different color from the
// contents.
const NSUInteger descriptionLocation =
[decorated length] - [description length];
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSForegroundColorAttributeName),
descriptionLocation);
EXPECT_EQ(RunLengthForAttribute(decorated, descriptionLocation,
NSForegroundColorAttributeName),
[description length]);
// Same font all the way through, nothing bold.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSFontAttributeName), [decorated length]);
EXPECT_FALSE(RunHasFontTrait(decorated, 0, NSBoldFontMask));
}
// Check that MatchText() styles content matches as expected.
TEST_F(AutocompletePopupViewMacTest, MatchTextContentsMatch) {
NSString* const contents = @"This is a test";
// Match "is".
const NSUInteger runLength1 = 5, runLength2 = 2, runLength3 = 7;
// Make sure nobody messed up the inputs.
EXPECT_EQ(runLength1 + runLength2 + runLength3, [contents length]);
AutocompleteMatch m = MakeMatch(base::SysNSStringToUTF16(contents),
string16());
// Push each run onto contents classifications.
m.contents_class.push_back(
ACMatchClassification(0, ACMatchClassification::NONE));
m.contents_class.push_back(
ACMatchClassification(runLength1, ACMatchClassification::MATCH));
m.contents_class.push_back(
ACMatchClassification(runLength1 + runLength2,
ACMatchClassification::NONE));
NSAttributedString* decorated =
AutocompletePopupViewMac::MatchText(m, font_, kLargeWidth);
// Result has same characters as the input.
EXPECT_EQ([decorated length], [contents length]);
EXPECT_TRUE([[decorated string] isEqualToString:contents]);
// Result is all one color.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSForegroundColorAttributeName),
[contents length]);
// Should have three font runs, not bold, bold, then not bold again.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSFontAttributeName), runLength1);
EXPECT_FALSE(RunHasFontTrait(decorated, 0U, NSBoldFontMask));
EXPECT_EQ(RunLengthForAttribute(decorated, runLength1,
NSFontAttributeName), runLength2);
EXPECT_TRUE(RunHasFontTrait(decorated, runLength1, NSBoldFontMask));
EXPECT_EQ(RunLengthForAttribute(decorated, runLength1 + runLength2,
NSFontAttributeName), runLength3);
EXPECT_FALSE(RunHasFontTrait(decorated, runLength1 + runLength2,
NSBoldFontMask));
}
// Check that MatchText() styles description matches as expected.
TEST_F(AutocompletePopupViewMacTest, MatchTextDescriptionMatch) {
NSString* const contents = @"This is a test";
NSString* const description = @"That was a test";
// Match "That was".
const NSUInteger runLength1 = 8, runLength2 = 7;
// Make sure nobody messed up the inputs.
EXPECT_EQ(runLength1 + runLength2, [description length]);
AutocompleteMatch m = MakeMatch(base::SysNSStringToUTF16(contents),
base::SysNSStringToUTF16(description));
// Push each run onto contents classifications.
m.description_class.push_back(
ACMatchClassification(0, ACMatchClassification::MATCH));
m.description_class.push_back(
ACMatchClassification(runLength1, ACMatchClassification::NONE));
NSAttributedString* decorated =
AutocompletePopupViewMac::MatchText(m, font_, kLargeWidth);
// Result contains the characters of the input.
EXPECT_GT([decorated length], [contents length] + [description length]);
EXPECT_TRUE([[decorated string] hasPrefix:contents]);
EXPECT_TRUE([[decorated string] hasSuffix:description]);
// Check that the description is a different color from the
// contents.
const NSUInteger descriptionLocation =
[decorated length] - [description length];
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSForegroundColorAttributeName),
descriptionLocation);
EXPECT_EQ(RunLengthForAttribute(decorated, descriptionLocation,
NSForegroundColorAttributeName),
[description length]);
// Should have three font runs, not bold, bold, then not bold again.
// The first run is the contents and the separator, the second run
// is the first run of the description.
EXPECT_EQ(RunLengthForAttribute(decorated, 0U,
NSFontAttributeName), descriptionLocation);
EXPECT_FALSE(RunHasFontTrait(decorated, 0U, NSBoldFontMask));
EXPECT_EQ(RunLengthForAttribute(decorated, descriptionLocation,
NSFontAttributeName), runLength1);
EXPECT_TRUE(RunHasFontTrait(decorated, descriptionLocation, NSBoldFontMask));
EXPECT_EQ(RunLengthForAttribute(decorated, descriptionLocation + runLength1,
NSFontAttributeName), runLength2);
EXPECT_FALSE(RunHasFontTrait(decorated, descriptionLocation + runLength1,
NSBoldFontMask));
}
TEST_F(AutocompletePopupViewMacTest, ElideString) {
NSString* const contents = @"This is a test with long contents";
const string16 contents16(base::SysNSStringToUTF16(contents));
const float kWide = 1000.0;
const float kNarrow = 20.0;
NSDictionary* attributes =
[NSDictionary dictionaryWithObject:font_.GetNativeFont()
forKey:NSFontAttributeName];
scoped_nsobject<NSMutableAttributedString> as(
[[NSMutableAttributedString alloc] initWithString:contents
attributes:attributes]);
// Nothing happens if the space is really wide.
NSMutableAttributedString* ret =
AutocompletePopupViewMac::ElideString(as, contents16, font_, kWide);
EXPECT_TRUE(ret == as);
EXPECT_TRUE([[as string] isEqualToString:contents]);
// When elided, result is the same as ElideText().
ret = AutocompletePopupViewMac::ElideString(as, contents16, font_, kNarrow);
string16 elided = ui::ElideText(contents16, font_, kNarrow, false);
EXPECT_TRUE(ret == as);
EXPECT_FALSE([[as string] isEqualToString:contents]);
EXPECT_TRUE([[as string] isEqualToString:base::SysUTF16ToNSString(elided)]);
// When elided, result is the same as ElideText().
ret = AutocompletePopupViewMac::ElideString(as, contents16, font_, 0.0);
elided = ui::ElideText(contents16, font_, 0.0, false);
EXPECT_TRUE(ret == as);
EXPECT_FALSE([[as string] isEqualToString:contents]);
EXPECT_TRUE([[as string] isEqualToString:base::SysUTF16ToNSString(elided)]);
}
TEST_F(AutocompletePopupViewMacTest, MatchTextElide) {
NSString* const contents = @"This is a test with long contents";
NSString* const description = @"That was a test";
// Match "long".
const NSUInteger runLength1 = 20, runLength2 = 4, runLength3 = 9;
// Make sure nobody messed up the inputs.
EXPECT_EQ(runLength1 + runLength2 + runLength3, [contents length]);
AutocompleteMatch m = MakeMatch(base::SysNSStringToUTF16(contents),
base::SysNSStringToUTF16(description));
// Push each run onto contents classifications.
m.contents_class.push_back(
ACMatchClassification(0, ACMatchClassification::NONE));
m.contents_class.push_back(
ACMatchClassification(runLength1, ACMatchClassification::MATCH));
m.contents_class.push_back(
ACMatchClassification(runLength1 + runLength2,
ACMatchClassification::URL));
// Figure out the width of the contents.
NSDictionary* attributes =
[NSDictionary dictionaryWithObject:font_.GetNativeFont()
forKey:NSFontAttributeName];
const float contentsWidth = [contents sizeWithAttributes:attributes].width;
// After accounting for the width of the image, this will force us
// to elide the contents.
float cellWidth = ceil(contentsWidth / 0.7);
NSAttributedString* decorated =
AutocompletePopupViewMac::MatchText(m, font_, cellWidth);
// Results contain a prefix of the contents and all of description.
NSString* commonPrefix =
[[decorated string] commonPrefixWithString:contents options:0];
EXPECT_GT([commonPrefix length], 0U);
EXPECT_LT([commonPrefix length], [contents length]);
EXPECT_TRUE([[decorated string] hasSuffix:description]);
// At one point the code had a bug where elided text was being
// marked up using pre-elided offsets, resulting in out-of-range
// values being passed to NSAttributedString. Push the ellipsis
// through part of each run to verify that we don't continue to see
// such things.
while([commonPrefix length] > runLength1 - 3) {
EXPECT_GT(cellWidth, 0.0);
cellWidth -= 1.0;
decorated = AutocompletePopupViewMac::MatchText(m, font_, cellWidth);
commonPrefix =
[[decorated string] commonPrefixWithString:contents options:0];
ASSERT_GT([commonPrefix length], 0U);
}
}
// TODO(shess): Test that
// AutocompletePopupViewMac::UpdatePopupAppearance() creates/destroys
// the popup according to result contents. Test that the matrix gets
// the right number of results. Test the contents of the cells for
// the right strings. Icons? Maybe, that seems harder to test.
// Styling seems almost impossible.
// TODO(shess): Test that AutocompletePopupViewMac::PaintUpdatesNow()
// updates the matrix selection.
// TODO(shess): Test that AutocompletePopupViewMac::AcceptInput()
// updates the model's selection from the matrix before returning.
// Could possibly test that via -select:.
// TODO(shess): Test that AutocompleteButtonCell returns the right
// background colors for on, highlighted, and neither.
// TODO(shess): Test that AutocompleteMatrixTarget can be initialized
// and then sends -select: to the view.
} // namespace
|