Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / autofill / autofill_textfield_unittest.mm
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.
4
5 #import "chrome/browser/ui/cocoa/autofill/autofill_textfield.h"
6
7 #import "base/mac/scoped_nsobject.h"
8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
9 #include "testing/gtest_mac.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h"
12 #include "ui/events/test/cocoa_test_event_utils.h"
13
14 class AutofillTextFieldTest : public CocoaTest {
15  public:
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_];
22   }
23
24  protected:
25   base::scoped_nsobject<AutofillTextField> textfield_;
26
27   DISALLOW_COPY_AND_ASSIGN(AutofillTextFieldTest);
28 };
29
30 TEST_VIEW(AutofillTextFieldTest, textfield_)
31
32 // Test invalid, mostly to ensure nothing leaks or crashes.
33 TEST_F(AutofillTextFieldTest, DisplayWithInvalid) {
34   [[textfield_ cell] setInvalid:YES];
35   [textfield_ display];
36   [[textfield_ cell] setInvalid:NO];
37   [textfield_ display];
38 }
39
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];
45   [textfield_ display];
46   [[textfield_ cell] setIcon:nil];
47   [textfield_ sizeToFit];
48   [textfield_ display];
49 }
50
51 // Test multiline behavior.
52 TEST_F(AutofillTextFieldTest, Multiline) {
53   [[textfield_ window] makeFirstResponder:textfield_];
54
55   // First, test with multiline disabled (default state).
56   ASSERT_EQ(NO, [textfield_ isMultiline]);
57
58   // All input interactions must happen via the field editor - AutofillTextField
59   // is based on NSTextField.
60   [[textfield_ currentEditor] insertText:@"foo"];
61
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]);
68
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]);
77 }