Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / autofill / autofill_suggestion_container_unittest.mm
blobcd3b3e8deb3a0ab6b4692e73ec4bbd79c1b5a7ae
1 // Copyright 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_suggestion_container.h"
7 #import "chrome/browser/ui/cocoa/autofill/autofill_textfield.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #import "ui/base/test/ui_cocoa_test_helper.h"
11 namespace {
13 class AutofillSuggestionContainerTest : public ui::CocoaTest {
14  public:
15   virtual void SetUp() {
16     CocoaTest::SetUp();
17     container_.reset([[AutofillSuggestionContainer alloc] init]);
18     [[test_window() contentView] addSubview:[container_ view]];
19     view_ = [container_ view];
20   }
22  protected:
23   base::scoped_nsobject<AutofillSuggestionContainer> container_;
24   NSView* view_;
27 }  // namespace
29 TEST_VIEW(AutofillSuggestionContainerTest, [container_ view])
31 TEST_F(AutofillSuggestionContainerTest, HasSubviews) {
32   ASSERT_EQ(3U, [[[container_ view] subviews] count]);
34   bool has_text_view = false;
35   bool has_edit_field = false;
37   // Ignore |spacer_| NSBox in this test.
38   for (id view in [[container_ view] subviews]) {
39     if ([view isKindOfClass:[AutofillTextField class]]) {
40       has_edit_field = true;
41     } else if ([view isKindOfClass:[NSTextView class]]) {
42       has_text_view = true;
43     }
44   }
46   EXPECT_TRUE(has_text_view);
47   EXPECT_TRUE(has_edit_field);
50 // Test that mouse events outside the input field are ignored.
51 TEST_F(AutofillSuggestionContainerTest, HitTestInputField) {
52  base::scoped_nsobject<NSImage> icon(
53       [[NSImage alloc] initWithSize:NSMakeSize(16, 16)]);
54   [container_ setSuggestionWithVerticallyCompactText:@"suggest"
55                              horizontallyCompactText:@"suggest"
56                                                 icon:nil
57                                             maxWidth:200];
58   [container_ showInputField:@"input" withIcon:icon];
59   [view_ setFrameSize:[container_ preferredSize]];
60   [container_ performLayout];
62   // Point not touching any subviews, in |view_|'s coordinate system.
63   NSPoint point_outside_subviews =
64       NSMakePoint(NSMinX([view_ bounds]), NSMaxY([view_ bounds]) - 1);
65   NSPoint point_inside_text_view = NSZeroPoint;
67   // hitTests on all inputs should be false, except for the inputField.
68   for (id field_view in [view_ subviews]) {
69     // Ensure |point_outside_subviews| really is outside subviews.
70     ASSERT_FALSE([field_view hitTest:point_outside_subviews]);
72     // Compute center of |field_view| in |view_|'s parent coordinate system.
73     NSPoint point =
74         [view_ convertPoint:NSMakePoint(NSMidX([field_view frame]),
75                                         NSMidY([field_view frame]))
76                      toView:[view_ superview]];
77     if (field_view == [container_ inputField]) {
78       point_inside_text_view = point;
79       EXPECT_TRUE([view_ hitTest:point]);
80     } else {
81       EXPECT_FALSE([view_ hitTest:point]);
82     }
83   }
85   // Mouse events directly on the main view should be ignored.
86   EXPECT_FALSE([view_ hitTest:
87       [view_ convertPoint:point_outside_subviews
88                    toView:[view_ superview]]]);
90   // Mouse events on the text view's editor should propagate.
91   // Asserts ensure the path covering children of |inputField| is taken.
92   [[view_ window] makeFirstResponder:[container_ inputField]];
93   NSView* editorView = [view_ hitTest:point_inside_text_view];
94   ASSERT_NE(editorView, [container_ inputField]);
95   ASSERT_TRUE([editorView isDescendantOf:[container_ inputField]]);
96   EXPECT_TRUE(editorView);