OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/ui/cocoa/autofill/autofill_suggestion_container.h" | 5 #import "chrome/browser/ui/cocoa/autofill/autofill_suggestion_container.h" |
6 | 6 |
7 #import "chrome/browser/ui/cocoa/autofill/autofill_textfield.h" | 7 #import "chrome/browser/ui/cocoa/autofill/autofill_textfield.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #import "ui/base/test/ui_cocoa_test_helper.h" | 9 #import "ui/base/test/ui_cocoa_test_helper.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 class AutofillSuggestionContainerTest : public ui::CocoaTest { | 13 class AutofillSuggestionContainerTest : public ui::CocoaTest { |
14 public: | 14 public: |
15 virtual void SetUp() { | 15 virtual void SetUp() { |
16 CocoaTest::SetUp(); | 16 CocoaTest::SetUp(); |
17 container_.reset([[AutofillSuggestionContainer alloc] init]); | 17 container_.reset([[AutofillSuggestionContainer alloc] init]); |
18 [[test_window() contentView] addSubview:[container_ view]]; | 18 [[test_window() contentView] addSubview:[container_ view]]; |
| 19 view_ = [container_ view]; |
19 } | 20 } |
20 | 21 |
21 protected: | 22 protected: |
22 base::scoped_nsobject<AutofillSuggestionContainer> container_; | 23 base::scoped_nsobject<AutofillSuggestionContainer> container_; |
| 24 NSView* view_; |
23 }; | 25 }; |
24 | 26 |
25 } // namespace | 27 } // namespace |
26 | 28 |
27 TEST_VIEW(AutofillSuggestionContainerTest, [container_ view]) | 29 TEST_VIEW(AutofillSuggestionContainerTest, [container_ view]) |
28 | 30 |
29 TEST_F(AutofillSuggestionContainerTest, HasSubviews) { | 31 TEST_F(AutofillSuggestionContainerTest, HasSubviews) { |
30 ASSERT_EQ(3U, [[[container_ view] subviews] count]); | 32 ASSERT_EQ(3U, [[[container_ view] subviews] count]); |
31 | 33 |
32 bool has_text_view = false; | 34 bool has_text_view = false; |
33 bool has_edit_field = false; | 35 bool has_edit_field = false; |
34 | 36 |
35 // Ignore |spacer_| NSBox in this test. | 37 // Ignore |spacer_| NSBox in this test. |
36 for (id view in [[container_ view] subviews]) { | 38 for (id view in [[container_ view] subviews]) { |
37 if ([view isKindOfClass:[AutofillTextField class]]) { | 39 if ([view isKindOfClass:[AutofillTextField class]]) { |
38 has_edit_field = true; | 40 has_edit_field = true; |
39 } else if ([view isKindOfClass:[NSTextView class]]) { | 41 } else if ([view isKindOfClass:[NSTextView class]]) { |
40 has_text_view = true; | 42 has_text_view = true; |
41 } | 43 } |
42 } | 44 } |
43 | 45 |
44 EXPECT_TRUE(has_text_view); | 46 EXPECT_TRUE(has_text_view); |
45 EXPECT_TRUE(has_edit_field); | 47 EXPECT_TRUE(has_edit_field); |
46 } | 48 } |
| 49 |
| 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]; |
| 61 |
| 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; |
| 66 |
| 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]); |
| 71 |
| 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 } |
| 84 |
| 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]]]); |
| 89 |
| 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); |
| 97 } |
OLD | NEW |