summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/autofill/autofill_textfield_unittest.mm
blob: 8fb537a9d02932d5ac7d8f016cab4c116e14fb6d (plain)
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
// Copyright (c) 2013 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/ui/cocoa/autofill/autofill_textfield.h"

#import "base/mac/scoped_nsobject.h"
#include "base/macros.h"
#import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "testing/platform_test.h"
#include "ui/events/test/cocoa_test_event_utils.h"

class AutofillTextFieldTest : public CocoaTest {
 public:
  AutofillTextFieldTest() {
    NSRect frame = NSMakeRect(0, 0, 50, 30);
    textfield_.reset([[AutofillTextField alloc] initWithFrame:frame]);
    [textfield_ setStringValue:@"Abcdefg"];
    [textfield_ sizeToFit];
    [[test_window() contentView] addSubview:textfield_];
  }

 protected:
  base::scoped_nsobject<AutofillTextField> textfield_;

  DISALLOW_COPY_AND_ASSIGN(AutofillTextFieldTest);
};

TEST_VIEW(AutofillTextFieldTest, textfield_)

// Test invalid, mostly to ensure nothing leaks or crashes.
TEST_F(AutofillTextFieldTest, DisplayWithInvalid) {
  [[textfield_ cell] setInvalid:YES];
  [textfield_ display];
  [[textfield_ cell] setInvalid:NO];
  [textfield_ display];
}

// Test with icon, mostly to ensure nothing leaks or crashes.
TEST_F(AutofillTextFieldTest, DisplayWithIcon) {
  NSImage* image = [NSImage imageNamed:NSImageNameStatusAvailable];
  [[textfield_ cell] setIcon:image];
  [textfield_ sizeToFit];
  [textfield_ display];
  [[textfield_ cell] setIcon:nil];
  [textfield_ sizeToFit];
  [textfield_ display];
}

// Test multiline behavior.
TEST_F(AutofillTextFieldTest, Multiline) {
  [[textfield_ window] makeFirstResponder:textfield_];

  // First, test with multiline disabled (default state).
  ASSERT_EQ(NO, [textfield_ isMultiline]);

  // All input interactions must happen via the field editor - AutofillTextField
  // is based on NSTextField.
  [[textfield_ currentEditor] insertText:@"foo"];

  // Insert a newline. Must do this via simulated key event to trigger
  // -control:textView:doCommandBySelector:.
  [[textfield_ currentEditor]
      keyDown:cocoa_test_event_utils::KeyEventWithCharacter('\n')];
  [[textfield_ currentEditor] insertText:@"bar"];
  EXPECT_NSEQ(@"bar", [textfield_ stringValue]);

  // Now test with multiline mode enabled.
  [textfield_ setIsMultiline:YES];
  [textfield_ setStringValue:@""];
  [[textfield_ currentEditor] insertText:@"foo"];
  [[textfield_ currentEditor]
      keyDown:cocoa_test_event_utils::KeyEventWithCharacter('\n')];
  [[textfield_ currentEditor] insertText:@"bar"];
  EXPECT_NSEQ(@"foo\nbar", [textfield_ stringValue]);
}