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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
// Copyright (c) 2009 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 <Cocoa/Cocoa.h>
#import "base/cocoa_protocols_mac.h"
#include "base/scoped_nsobject.h"
#import "chrome/browser/cocoa/autocomplete_text_field.h"
#import "chrome/browser/cocoa/autocomplete_text_field_cell.h"
#import "chrome/browser/cocoa/autocomplete_text_field_editor.h"
#import "chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
using ::testing::InSequence;
// OCMock wants to mock a concrete class or protocol. This should
// provide a correct protocol for newer versions of the SDK, while
// providing something mockable for older versions.
@protocol MockTextEditingDelegate<NSControlTextEditingDelegate>
- (void)controlTextDidBeginEditing:(NSNotification*)aNotification;
- (BOOL)control:(NSControl*)control textShouldEndEditing:(NSText*)fieldEditor;
@end
namespace {
// Mock a SecurityImageView.
class MockSecurityImageView : public LocationBarViewMac::SecurityImageView {
public:
MockSecurityImageView(Profile* profile, ToolbarModel* model)
: LocationBarViewMac::SecurityImageView(profile, model) {}
MOCK_METHOD0(OnMousePressed, bool());
};
// Mock up an incrementing event number.
NSUInteger eventNumber = 0;
// Create an event of the indicated |type| at |point| within |view|.
// TODO(shess): Would be nice to have a MockApplication which provided
// nifty accessors to create these things and inject them. It could
// even provide functions for "Click and drag mouse from point A to
// point B".
NSEvent* Event(NSView* view, const NSPoint point, const NSEventType type,
const NSUInteger clickCount) {
NSWindow* window([view window]);
const NSPoint locationInWindow([view convertPoint:point toView:nil]);
const NSPoint location([window convertBaseToScreen:locationInWindow]);
return [NSEvent mouseEventWithType:type
location:location
modifierFlags:0
timestamp:0
windowNumber:[window windowNumber]
context:nil
eventNumber:eventNumber++
clickCount:clickCount
pressure:0.0];
}
NSEvent* Event(NSView* view, const NSPoint point, const NSEventType type) {
return Event(view, point, type, 1);
}
// Width of the field so that we don't have to ask |field_| for it all
// the time.
static const CGFloat kWidth(300.0);
class AutocompleteTextFieldTest : public CocoaTest {
public:
AutocompleteTextFieldTest() {
// Make sure this is wide enough to play games with the cell
// decorations.
NSRect frame = NSMakeRect(0, 0, kWidth, 30);
scoped_nsobject<AutocompleteTextField> field(
[[AutocompleteTextField alloc] initWithFrame:frame]);
field_ = field.get();
[field_ setStringValue:@"Test test"];
[field_ setObserver:&field_observer_];
[[test_window() contentView] addSubview:field_];
window_delegate_.reset(
[[AutocompleteTextFieldWindowTestDelegate alloc] init]);
[test_window() setDelegate:window_delegate_.get()];
}
NSEvent* KeyDownEventWithFlags(NSUInteger flags) {
return [NSEvent keyEventWithType:NSKeyDown
location:NSZeroPoint
modifierFlags:flags
timestamp:0.0
windowNumber:[test_window() windowNumber]
context:nil
characters:@"a"
charactersIgnoringModifiers:@"a"
isARepeat:NO
keyCode:'a'];
}
// Helper to return the field-editor frame being used w/in |field_|.
NSRect EditorFrame() {
EXPECT_TRUE([field_ currentEditor]);
EXPECT_EQ([[field_ subviews] count], 1U);
if ([[field_ subviews] count] > 0) {
return [[[field_ subviews] objectAtIndex:0] frame];
} else {
// Return something which won't work so the caller can soldier
// on.
return NSZeroRect;
}
}
AutocompleteTextField* field_;
MockAutocompleteTextFieldObserver field_observer_;
scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_;
};
TEST_VIEW(AutocompleteTextFieldTest, field_);
// Test that we have the right cell class.
TEST_F(AutocompleteTextFieldTest, CellClass) {
EXPECT_TRUE([[field_ cell] isKindOfClass:[AutocompleteTextFieldCell class]]);
}
// Test that we get the same cell from -cell and
// -autocompleteTextFieldCell.
TEST_F(AutocompleteTextFieldTest, Cell) {
AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
EXPECT_EQ(cell, [field_ cell]);
EXPECT_TRUE(cell != nil);
}
// Test that becoming first responder sets things up correctly.
TEST_F(AutocompleteTextFieldTest, FirstResponder) {
EXPECT_EQ(nil, [field_ currentEditor]);
EXPECT_EQ([[field_ subviews] count], 0U);
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
EXPECT_FALSE(nil == [field_ currentEditor]);
EXPECT_EQ([[field_ subviews] count], 1U);
EXPECT_TRUE([[field_ currentEditor] isDescendantOf:field_]);
// Check that the window delegate is providing the right editor.
Class c = [AutocompleteTextFieldEditor class];
EXPECT_TRUE([[field_ currentEditor] isKindOfClass:c]);
}
TEST_F(AutocompleteTextFieldTest, AvailableDecorationWidth) {
// A fudge factor to account for how much space the border takes up.
// The test shouldn't be too dependent on the field's internals, but
// it also shouldn't let deranged cases fall through the cracks
// (like nothing available with no text, or everything available
// with some text).
const CGFloat kBorderWidth = 20.0;
// With no contents, almost the entire width is available for
// decorations.
[field_ setStringValue:@""];
CGFloat availableWidth = [field_ availableDecorationWidth];
EXPECT_LE(availableWidth, kWidth);
EXPECT_GT(availableWidth, kWidth - kBorderWidth);
// With minor contents, most of the remaining width is available for
// decorations.
NSDictionary* attributes =
[NSDictionary dictionaryWithObject:[field_ font]
forKey:NSFontAttributeName];
NSString* string = @"Hello world";
const NSSize size([string sizeWithAttributes:attributes]);
[field_ setStringValue:string];
availableWidth = [field_ availableDecorationWidth];
EXPECT_LE(availableWidth, kWidth - size.width);
EXPECT_GT(availableWidth, kWidth - size.width - kBorderWidth);
// With huge contents, nothing at all is left for decorations.
string = @"A long string which is surely wider than field_ can hold.";
[field_ setStringValue:string];
availableWidth = [field_ availableDecorationWidth];
EXPECT_LT(availableWidth, 0.0);
}
// Test drawing, mostly to ensure nothing leaks or crashes.
TEST_F(AutocompleteTextFieldTest, Display) {
[field_ display];
// Test focussed drawing.
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
[field_ display];
[test_window() clearPretendKeyWindowAndFirstResponder];
// Test display of various cell configurations.
AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
[cell setSearchHintString:@"Type to search" availableWidth:kWidth];
[field_ display];
NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
[cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix"
availableWidth:kWidth];
[field_ display];
[cell setKeywordString:@"Search Engine:"
partialString:@"Search Eng:"
availableWidth:kWidth];
[field_ display];
[cell clearKeywordAndHint];
[field_ display];
}
TEST_F(AutocompleteTextFieldTest, FlagsChanged) {
InSequence dummy; // Call mock in exactly the order specified.
// Test without Control key down, but some other modifier down.
EXPECT_CALL(field_observer_, OnControlKeyChanged(false));
[field_ flagsChanged:KeyDownEventWithFlags(NSShiftKeyMask)];
// Test with Control key down.
EXPECT_CALL(field_observer_, OnControlKeyChanged(true));
[field_ flagsChanged:KeyDownEventWithFlags(NSControlKeyMask)];
}
// This test is here rather than in the editor's tests because the
// field catches -flagsChanged: because it's on the responder chain,
// the field editor doesn't implement it.
TEST_F(AutocompleteTextFieldTest, FieldEditorFlagsChanged) {
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
NSResponder* firstResponder = [[field_ window] firstResponder];
EXPECT_EQ(firstResponder, [field_ currentEditor]);
InSequence dummy; // Call mock in exactly the order specified.
// Test without Control key down, but some other modifier down.
EXPECT_CALL(field_observer_, OnControlKeyChanged(false));
[firstResponder flagsChanged:KeyDownEventWithFlags(NSShiftKeyMask)];
// Test with Control key down.
EXPECT_CALL(field_observer_, OnControlKeyChanged(true));
[firstResponder flagsChanged:KeyDownEventWithFlags(NSControlKeyMask)];
}
// Frame size changes are propagated to |observer_|.
TEST_F(AutocompleteTextFieldTest, FrameChanged) {
EXPECT_CALL(field_observer_, OnFrameChanged());
NSRect frame = [field_ frame];
frame.size.width += 10.0;
[field_ setFrame:frame];
}
// Test that the field editor gets the same bounds when focus is
// delivered by the standard focusing machinery, or by
// -resetFieldEditorFrameIfNeeded.
TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBase) {
AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
// Capture the editor frame resulting from the standard focus
// machinery.
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
const NSRect baseEditorFrame(EditorFrame());
// Setting a hint should result in a strictly smaller editor frame.
EXPECT_FALSE([cell hintString]);
[cell setSearchHintString:@"search hint" availableWidth:kWidth];
EXPECT_TRUE([cell hintString]);
[field_ resetFieldEditorFrameIfNeeded];
EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame()));
EXPECT_TRUE(NSContainsRect(baseEditorFrame, EditorFrame()));
// Clearing hint string and using -resetFieldEditorFrameIfNeeded
// should result in the same frame as the standard focus machinery.
[cell clearKeywordAndHint];
EXPECT_FALSE([cell hintString]);
[field_ resetFieldEditorFrameIfNeeded];
EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame()));
}
// Test that the field editor gets the same bounds when focus is
// delivered by the standard focusing machinery, or by
// -resetFieldEditorFrameIfNeeded.
TEST_F(AutocompleteTextFieldTest, ResetFieldEditorSearchHint) {
AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
const NSString* kHintString(@"Type to search");
// Capture the editor frame resulting from the standard focus
// machinery.
[cell setSearchHintString:kHintString availableWidth:kWidth];
EXPECT_TRUE([cell hintString]);
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
const NSRect baseEditorFrame(EditorFrame());
// Clearing the hint should result in a strictly larger editor
// frame.
[cell clearKeywordAndHint];
EXPECT_FALSE([cell hintString]);
[field_ resetFieldEditorFrameIfNeeded];
EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame()));
EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame));
// Setting the same hint string and using
// -resetFieldEditorFrameIfNeeded should result in the same frame as
// the standard focus machinery.
[cell setSearchHintString:kHintString availableWidth:kWidth];
EXPECT_TRUE([cell hintString]);
[field_ resetFieldEditorFrameIfNeeded];
EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame()));
}
// Test that the field editor gets the same bounds when focus is
// delivered by the standard focusing machinery, or by
// -resetFieldEditorFrameIfNeeded.
TEST_F(AutocompleteTextFieldTest, ResetFieldEditorKeywordHint) {
AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
const NSString* kFullString(@"Search Engine:");
const NSString* kPartialString(@"Search Eng:");
// Capture the editor frame resulting from the standard focus
// machinery.
[cell setKeywordString:kFullString
partialString:kPartialString
availableWidth:kWidth];
EXPECT_TRUE([cell keywordString]);
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
const NSRect baseEditorFrame(EditorFrame());
// Clearing the hint should result in a strictly larger editor
// frame.
[cell clearKeywordAndHint];
EXPECT_FALSE([cell keywordString]);
[field_ resetFieldEditorFrameIfNeeded];
EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame()));
EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame));
// Setting the same hint string and using
// -resetFieldEditorFrameIfNeeded should result in the same frame as
// the standard focus machinery.
[cell setKeywordString:kFullString
partialString:kPartialString
availableWidth:kWidth];
EXPECT_TRUE([cell keywordString]);
[field_ resetFieldEditorFrameIfNeeded];
EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame()));
}
// Test that resetting the field editor bounds does not cause untoward
// messages to the field's delegate.
TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBlocksEndEditing) {
// First, test that -makeFirstResponder: sends
// -controlTextDidBeginEditing: and -control:textShouldEndEditing at
// the expected times.
{
id mockDelegate =
[OCMockObject mockForProtocol:@protocol(MockTextEditingDelegate)];
[field_ setDelegate:mockDelegate];
// Becoming first responder doesn't begin editing.
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]);
EXPECT_TRUE(nil != editor);
[mockDelegate verify];
// This should begin editing.
[[mockDelegate expect] controlTextDidBeginEditing:OCMOCK_ANY];
[editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""];
[mockDelegate verify];
// Changing first responder ends editing.
BOOL yes = YES;
[[[mockDelegate expect] andReturnValue:OCMOCK_VALUE(yes)]
control:OCMOCK_ANY textShouldEndEditing:OCMOCK_ANY];
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
[mockDelegate verify];
[field_ setDelegate:nil];
}
// Test that -resetFieldEditorFrameIfNeeded manages to rearrange the
// editor without ending editing.
{
id mockDelegate =
[OCMockObject mockForProtocol:@protocol(MockTextEditingDelegate)];
[field_ setDelegate:mockDelegate];
// Start editing.
[[mockDelegate expect] controlTextDidBeginEditing:OCMOCK_ANY];
NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]);
[editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""];
[mockDelegate verify];
// No more messages to mockDelegate.
AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
[cell setSearchHintString:@"Type to search" availableWidth:kWidth];
[field_ resetFieldEditorFrameIfNeeded];
[mockDelegate verify];
[field_ setDelegate:nil];
}
}
// Clicking in the search hint should put the caret in the rightmost
// position.
TEST_F(AutocompleteTextFieldTest, ClickSearchHintPutsCaretRightmost) {
// Set the decoration before becoming responder.
EXPECT_FALSE([field_ currentEditor]);
AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
[cell setSearchHintString:@"Type to search" availableWidth:kWidth];
// Can't rely on the window machinery to make us first responder,
// here.
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
EXPECT_TRUE([field_ currentEditor]);
const NSPoint point(NSMakePoint(300.0 - 20.0, 5.0));
NSEvent* downEvent(Event(field_, point, NSLeftMouseDown));
NSEvent* upEvent(Event(field_, point, NSLeftMouseUp));
[NSApp postEvent:upEvent atStart:YES];
[field_ mouseDown:downEvent];
const NSRange selectedRange([[field_ currentEditor] selectedRange]);
EXPECT_EQ(selectedRange.location, [[field_ stringValue] length]);
EXPECT_EQ(selectedRange.length, 0U);
}
// Clicking in the keyword-search should put the caret in the
// leftmost position.
TEST_F(AutocompleteTextFieldTest, ClickKeywordPutsCaretLeftmost) {
// Set the decoration before becoming responder.
EXPECT_FALSE([field_ currentEditor]);
AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
[cell setKeywordString:@"Search Engine:"
partialString:@"Search:"
availableWidth:kWidth];
// Can't rely on the window machinery to make us first responder,
// here.
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
EXPECT_TRUE([field_ currentEditor]);
const NSPoint point(NSMakePoint(20.0, 5.0));
NSEvent* downEvent(Event(field_, point, NSLeftMouseDown));
NSEvent* upEvent(Event(field_, point, NSLeftMouseUp));
[NSApp postEvent:upEvent atStart:YES];
[field_ mouseDown:downEvent];
const NSRange selectedRange([[field_ currentEditor] selectedRange]);
EXPECT_EQ(selectedRange.location, 0U);
EXPECT_EQ(selectedRange.length, 0U);
}
// Clicks not in the text area or the cell's decorations fall through
// to the editor.
TEST_F(AutocompleteTextFieldTest, ClickBorderSelectsAll) {
// Can't rely on the window machinery to make us first responder,
// here.
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
EXPECT_TRUE([field_ currentEditor]);
const NSPoint point(NSMakePoint(20.0, 1.0));
NSEvent* downEvent(Event(field_, point, NSLeftMouseDown));
NSEvent* upEvent(Event(field_, point, NSLeftMouseUp));
[NSApp postEvent:upEvent atStart:YES];
[field_ mouseDown:downEvent];
// Clicking in the narrow border area around a Cocoa NSTextField
// does a select-all. Regardless of whether this is a good call, it
// works as a test that things get passed down to the editor.
const NSRange selectedRange([[field_ currentEditor] selectedRange]);
EXPECT_EQ(selectedRange.location, 0U);
EXPECT_EQ(selectedRange.length, [[field_ stringValue] length]);
}
// Single-click with no drag should setup a field editor and
// select all.
TEST_F(AutocompleteTextFieldTest, ClickSelectsAll) {
EXPECT_FALSE([field_ currentEditor]);
const NSPoint point(NSMakePoint(20.0, 5.0));
NSEvent* downEvent(Event(field_, point, NSLeftMouseDown));
NSEvent* upEvent(Event(field_, point, NSLeftMouseUp));
[NSApp postEvent:upEvent atStart:YES];
[field_ mouseDown:downEvent];
EXPECT_TRUE([field_ currentEditor]);
const NSRange selectedRange([[field_ currentEditor] selectedRange]);
EXPECT_EQ(selectedRange.location, 0U);
EXPECT_EQ(selectedRange.length, [[field_ stringValue] length]);
}
// Click-drag selects text, not select all.
TEST_F(AutocompleteTextFieldTest, ClickDragSelectsText) {
EXPECT_FALSE([field_ currentEditor]);
NSEvent* downEvent(Event(field_, NSMakePoint(20.0, 5.0), NSLeftMouseDown));
NSEvent* upEvent(Event(field_, NSMakePoint(0.0, 5.0), NSLeftMouseUp));
[NSApp postEvent:upEvent atStart:YES];
[field_ mouseDown:downEvent];
EXPECT_TRUE([field_ currentEditor]);
// Expect this to have selected a prefix of the content. Mostly
// just don't want the select-all behavior.
const NSRange selectedRange([[field_ currentEditor] selectedRange]);
EXPECT_EQ(selectedRange.location, 0U);
EXPECT_LT(selectedRange.length, [[field_ stringValue] length]);
}
// TODO(shess): Test that click/pause/click allows cursor placement.
// In this case the first click goes to the field, but the second
// click goes to the field editor, so the current testing pattern
// can't work. What really needs to happen is to push through the
// NSWindow event machinery so that we can say "two independent clicks
// at the same location have the right effect". Once that is done, it
// might make sense to revise the other tests to use the same
// machinery.
// Double-click selects word, not select all.
TEST_F(AutocompleteTextFieldTest, DoubleClickSelectsWord) {
EXPECT_FALSE([field_ currentEditor]);
const NSPoint point(NSMakePoint(20.0, 5.0));
NSEvent* downEvent(Event(field_, point, NSLeftMouseDown, 1));
NSEvent* upEvent(Event(field_, point, NSLeftMouseUp, 1));
NSEvent* downEvent2(Event(field_, point, NSLeftMouseDown, 2));
NSEvent* upEvent2(Event(field_, point, NSLeftMouseUp, 2));
[NSApp postEvent:upEvent atStart:YES];
[field_ mouseDown:downEvent];
[NSApp postEvent:upEvent2 atStart:YES];
[field_ mouseDown:downEvent2];
EXPECT_TRUE([field_ currentEditor]);
// Selected the first word.
const NSRange selectedRange([[field_ currentEditor] selectedRange]);
const NSRange spaceRange([[field_ stringValue] rangeOfString:@" "]);
EXPECT_GT(spaceRange.location, 0U);
EXPECT_LT(spaceRange.length, [[field_ stringValue] length]);
EXPECT_EQ(selectedRange.location, 0U);
EXPECT_EQ(selectedRange.length, spaceRange.location);
}
TEST_F(AutocompleteTextFieldTest, TripleClickSelectsAll) {
EXPECT_FALSE([field_ currentEditor]);
const NSPoint point(NSMakePoint(20.0, 5.0));
NSEvent* downEvent(Event(field_, point, NSLeftMouseDown, 1));
NSEvent* upEvent(Event(field_, point, NSLeftMouseUp, 1));
NSEvent* downEvent2(Event(field_, point, NSLeftMouseDown, 2));
NSEvent* upEvent2(Event(field_, point, NSLeftMouseUp, 2));
NSEvent* downEvent3(Event(field_, point, NSLeftMouseDown, 3));
NSEvent* upEvent3(Event(field_, point, NSLeftMouseUp, 3));
[NSApp postEvent:upEvent atStart:YES];
[field_ mouseDown:downEvent];
[NSApp postEvent:upEvent2 atStart:YES];
[field_ mouseDown:downEvent2];
[NSApp postEvent:upEvent3 atStart:YES];
[field_ mouseDown:downEvent3];
EXPECT_TRUE([field_ currentEditor]);
// Selected the first word.
const NSRange selectedRange([[field_ currentEditor] selectedRange]);
EXPECT_EQ(selectedRange.location, 0U);
EXPECT_EQ(selectedRange.length, [[field_ stringValue] length]);
}
TEST_F(AutocompleteTextFieldTest, SecurityIconMouseDown) {
AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
MockSecurityImageView security_image_view(NULL, NULL);
[cell setSecurityImageView:&security_image_view];
security_image_view.SetImageShown(
LocationBarViewMac::SecurityImageView::LOCK);
security_image_view.SetVisible(true);
NSRect iconFrame([cell securityImageFrameForFrame:[field_ bounds]]);
NSPoint location(NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)));
NSEvent* event(Event(field_, location, NSLeftMouseDown, 1));
EXPECT_CALL(security_image_view, OnMousePressed());
[field_ mouseDown:event];
}
// Verify that -setAttributedStringValue: works as expected when
// focussed or when not focussed. Our code mostly depends on about
// whether -stringValue works right.
TEST_F(AutocompleteTextFieldTest, SetAttributedStringBaseline) {
EXPECT_EQ(nil, [field_ currentEditor]);
// So that we can set rich text.
[field_ setAllowsEditingTextAttributes:YES];
// Set an attribute different from the field's default so we can
// tell we got the same string out as we put in.
NSFont* font = [NSFont fontWithDescriptor:[[field_ font] fontDescriptor]
size:[[field_ font] pointSize] + 2];
NSDictionary* attributes =
[NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
static const NSString* kString = @"This is a test";
scoped_nsobject<NSAttributedString> attributedString(
[[NSAttributedString alloc] initWithString:kString
attributes:attributes]);
// Check that what we get back looks like what we put in.
EXPECT_FALSE([[field_ stringValue] isEqualToString:kString]);
[field_ setAttributedStringValue:attributedString];
EXPECT_TRUE([[field_ attributedStringValue]
isEqualToAttributedString:attributedString]);
EXPECT_TRUE([[field_ stringValue] isEqualToString:kString]);
// Try that again with focus.
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
EXPECT_TRUE([field_ currentEditor]);
// Check that what we get back looks like what we put in.
[field_ setStringValue:@""];
EXPECT_FALSE([[field_ stringValue] isEqualToString:kString]);
[field_ setAttributedStringValue:attributedString];
EXPECT_TRUE([[field_ attributedStringValue]
isEqualToAttributedString:attributedString]);
EXPECT_TRUE([[field_ stringValue] isEqualToString:kString]);
}
// -setAttributedStringValue: shouldn't reset the undo state if things
// are being editted.
TEST_F(AutocompleteTextFieldTest, SetAttributedStringUndo) {
NSColor* redColor = [NSColor redColor];
NSDictionary* attributes =
[NSDictionary dictionaryWithObject:redColor
forKey:NSForegroundColorAttributeName];
static const NSString* kString = @"This is a test";
scoped_nsobject<NSAttributedString> attributedString(
[[NSAttributedString alloc] initWithString:kString
attributes:attributes]);
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
EXPECT_TRUE([field_ currentEditor]);
NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]);
NSUndoManager* undoManager = [editor undoManager];
EXPECT_TRUE(undoManager);
// Nothing to undo, yet.
EXPECT_FALSE([undoManager canUndo]);
// Starting an editing action creates an undoable item.
[editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""];
[editor didChangeText];
EXPECT_TRUE([undoManager canUndo]);
// -setStringValue: resets the editor's undo chain.
[field_ setStringValue:kString];
EXPECT_FALSE([undoManager canUndo]);
// Verify that -setAttributedStringValue: does not reset the
// editor's undo chain.
[field_ setStringValue:@""];
[editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""];
[editor didChangeText];
EXPECT_TRUE([undoManager canUndo]);
[field_ setAttributedStringValue:attributedString];
EXPECT_TRUE([undoManager canUndo]);
// Verify that calling -clearUndoChain clears the undo chain.
[field_ clearUndoChain];
EXPECT_FALSE([undoManager canUndo]);
}
TEST_F(AutocompleteTextFieldTest, EditorGetsCorrectUndoManager) {
[test_window() makePretendKeyWindowAndSetFirstResponder:field_];
NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]);
EXPECT_TRUE(editor);
EXPECT_EQ([field_ undoManagerForTextView:editor], [editor undoManager]);
}
} // namespace
|