1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/autofill/autofill_textfield.h"
7 #import "base/mac/scoped_nsobject.h"
8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/gtest_mac.h"
11 #include "testing/platform_test.h"
12 #include "ui/events/test/cocoa_test_event_utils.h"
14 class AutofillTextFieldTest : public CocoaTest {
16 AutofillTextFieldTest() {
17 NSRect frame = NSMakeRect(0, 0, 50, 30);
18 textfield_.reset([[AutofillTextField alloc] initWithFrame:frame]);
19 [textfield_ setStringValue:@"Abcdefg"];
20 [textfield_ sizeToFit];
21 [[test_window() contentView] addSubview:textfield_];
25 base::scoped_nsobject<AutofillTextField> textfield_;
27 DISALLOW_COPY_AND_ASSIGN(AutofillTextFieldTest);
30 TEST_VIEW(AutofillTextFieldTest, textfield_)
32 // Test invalid, mostly to ensure nothing leaks or crashes.
33 TEST_F(AutofillTextFieldTest, DisplayWithInvalid) {
34 [[textfield_ cell] setInvalid:YES];
36 [[textfield_ cell] setInvalid:NO];
40 // Test with icon, mostly to ensure nothing leaks or crashes.
41 TEST_F(AutofillTextFieldTest, DisplayWithIcon) {
42 NSImage* image = [NSImage imageNamed:NSImageNameStatusAvailable];
43 [[textfield_ cell] setIcon:image];
44 [textfield_ sizeToFit];
46 [[textfield_ cell] setIcon:nil];
47 [textfield_ sizeToFit];
51 // Test multiline behavior.
52 TEST_F(AutofillTextFieldTest, Multiline) {
53 [[textfield_ window] makeFirstResponder:textfield_];
55 // First, test with multiline disabled (default state).
56 ASSERT_EQ(NO, [textfield_ isMultiline]);
58 // All input interactions must happen via the field editor - AutofillTextField
59 // is based on NSTextField.
60 [[textfield_ currentEditor] insertText:@"foo"];
62 // Insert a newline. Must do this via simulated key event to trigger
63 // -control:textView:doCommandBySelector:.
64 [[textfield_ currentEditor]
65 keyDown:cocoa_test_event_utils::KeyEventWithCharacter('\n')];
66 [[textfield_ currentEditor] insertText:@"bar"];
67 EXPECT_NSEQ(@"bar", [textfield_ stringValue]);
69 // Now test with multiline mode enabled.
70 [textfield_ setIsMultiline:YES];
71 [textfield_ setStringValue:@""];
72 [[textfield_ currentEditor] insertText:@"foo"];
73 [[textfield_ currentEditor]
74 keyDown:cocoa_test_event_utils::KeyEventWithCharacter('\n')];
75 [[textfield_ currentEditor] insertText:@"bar"];
76 EXPECT_NSEQ(@"foo\nbar", [textfield_ stringValue]);