OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/omnibox/omnibox_popup_cell.h" | |
5 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.h" | 6 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.h" |
6 | 7 |
7 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
8 #import "ui/events/test/cocoa_test_event_utils.h" | 9 #import "ui/events/test/cocoa_test_event_utils.h" |
9 | 10 |
11 @interface OmniboxPopupTableController () | |
12 // Setup the information used by the NSTableView data source. | |
13 - (id)initWithArray:(NSArray*)array; | |
14 @end | |
Scott Hess - ex-Googler
2015/05/21 20:40:27
Same as my other comment, test-only API in the hea
dschuyler
2015/05/26 18:40:21
I made the change for instancetype.
| |
15 | |
10 namespace { | 16 namespace { |
11 | 17 |
12 NSEvent* MouseEventInRow(NSMatrix* matrix, NSEventType type, NSInteger row) { | 18 NSEvent* MouseEventInRow(OmniboxPopupMatrix* matrix, |
13 NSRect cell_rect = [matrix cellFrameAtRow:row column:0]; | 19 NSEventType type, |
20 NSInteger row) { | |
21 NSRect cell_rect = [matrix rectOfRow:row]; | |
14 NSPoint point_in_view = NSMakePoint(NSMidX(cell_rect), NSMidY(cell_rect)); | 22 NSPoint point_in_view = NSMakePoint(NSMidX(cell_rect), NSMidY(cell_rect)); |
15 NSPoint point_in_window = [matrix convertPoint:point_in_view toView:nil]; | 23 NSPoint point_in_window = [matrix convertPoint:point_in_view toView:nil]; |
16 return cocoa_test_event_utils::MouseEventAtPoint( | 24 return cocoa_test_event_utils::MouseEventAtPoint( |
17 point_in_window, type, 0); | 25 point_in_window, type, 0); |
18 } | 26 } |
19 | 27 |
20 class OmniboxPopupMatrixTest : public CocoaTest, | 28 class OmniboxPopupMatrixTest : public CocoaTest, |
21 public OmniboxPopupMatrixObserver { | 29 public OmniboxPopupMatrixObserver { |
22 public: | 30 public: |
23 OmniboxPopupMatrixTest() | 31 OmniboxPopupMatrixTest() |
24 : selected_row_(0), | 32 : selected_row_(0), clicked_row_(0), middle_clicked_row_(0) {} |
25 clicked_row_(0), | |
26 middle_clicked_row_(0) { | |
27 } | |
28 | 33 |
29 void SetUp() override { | 34 void SetUp() override { |
30 CocoaTest::SetUp(); | 35 CocoaTest::SetUp(); |
31 matrix_.reset([[OmniboxPopupMatrix alloc] initWithObserver:this]); | 36 matrix_.reset([[OmniboxPopupMatrix alloc] initWithObserver:this]); |
32 [[test_window() contentView] addSubview:matrix_]; | 37 [[test_window() contentView] addSubview:matrix_]; |
38 | |
39 base::scoped_nsobject<NSTableColumn> column( | |
40 [[NSTableColumn alloc] initWithIdentifier:@"MainCell"]); | |
41 [column setDataCell:[[[OmniboxPopupCell alloc] init] autorelease]]; | |
42 [matrix_ addTableColumn:column]; | |
groby-ooo-7-16
2015/05/21 03:01:48
Why add another table column? Didn't -initWithObse
dschuyler
2015/05/26 18:40:21
Done.
| |
43 | |
44 NSMutableArray* array = [NSMutableArray array]; | |
45 for (size_t ii = 0; ii < 3; ++ii) { | |
groby-ooo-7-16
2015/05/21 03:01:48
Please, please, please: No ii :)
dschuyler
2015/05/26 18:40:21
Done.
| |
46 base::scoped_nsobject<OmniboxPopupCellData> cellData( | |
47 [[OmniboxPopupCellData alloc] init]); | |
groby-ooo-7-16
2015/05/21 03:01:48
chain methods :)
Scott Hess - ex-Googler
2015/05/21 20:40:27
For tests, I'm more lenient about -autorelease, as
dschuyler
2015/05/26 18:40:21
I'd like to keep the reference counting correct.
I
groby-ooo-7-16
2015/05/26 21:23:22
It's a minimal cost, so I'm not too worried. I lea
dschuyler
2015/05/28 20:34:22
I also vote for the easy-to-read, but it sounds li
Scott Hess - ex-Googler
2015/05/29 18:00:26
I actually gave up on CPP vs OBJC long ago, and mo
dschuyler
2015/06/09 01:30:46
Done.
| |
48 [array addObject:cellData]; | |
49 } | |
50 [matrix_ setController:[[OmniboxPopupTableController alloc] | |
51 initWithArray:array]]; | |
Scott Hess - ex-Googler
2015/05/21 20:40:27
I believe the only refs to OPTController after thi
dschuyler
2015/05/26 18:40:21
Thanks. I totally missed that.
| |
52 | |
53 NSRect table_frame = NSZeroRect; | |
groby-ooo-7-16
2015/05/21 03:01:48
NSMakeRect(0, 0, testWidth, NSHeight([matrix_ fram
Scott Hess - ex-Googler
2015/05/21 20:40:27
I'd not bother defining testWidth unless you're us
dschuyler
2015/05/26 18:40:21
Done.
dschuyler
2015/05/26 18:40:21
Not needed any longer.
| |
54 CGFloat testWidth = 400; | |
55 table_frame.size.width = testWidth; | |
56 table_frame.size.height = NSHeight([matrix_ frame]); | |
57 [matrix_ setFrame:table_frame]; | |
33 }; | 58 }; |
34 | 59 |
35 void OnMatrixRowSelected(OmniboxPopupMatrix* matrix, size_t row) override { | 60 void OnMatrixRowSelected(OmniboxPopupMatrix* matrix, size_t row) override { |
36 selected_row_ = row; | 61 selected_row_ = row; |
37 [matrix_ selectCellAtRow:row column:0]; | |
38 } | 62 } |
39 | 63 |
40 void OnMatrixRowClicked(OmniboxPopupMatrix* matrix, size_t row) override { | 64 void OnMatrixRowClicked(OmniboxPopupMatrix* matrix, size_t row) override { |
41 clicked_row_ = row; | 65 clicked_row_ = row; |
42 } | 66 } |
43 | 67 |
44 void OnMatrixRowMiddleClicked(OmniboxPopupMatrix* matrix, | 68 void OnMatrixRowMiddleClicked(OmniboxPopupMatrix* matrix, |
45 size_t row) override { | 69 size_t row) override { |
46 middle_clicked_row_ = row; | 70 middle_clicked_row_ = row; |
47 } | 71 } |
48 | 72 |
49 protected: | 73 protected: |
50 base::scoped_nsobject<OmniboxPopupMatrix> matrix_; | 74 base::scoped_nsobject<OmniboxPopupMatrix> matrix_; |
51 size_t selected_row_; | 75 size_t selected_row_; |
52 size_t clicked_row_; | 76 size_t clicked_row_; |
53 size_t middle_clicked_row_; | 77 size_t middle_clicked_row_; |
54 | 78 |
55 private: | 79 private: |
56 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupMatrixTest); | 80 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupMatrixTest); |
57 }; | 81 }; |
58 | 82 |
59 TEST_VIEW(OmniboxPopupMatrixTest, matrix_); | 83 TEST_VIEW(OmniboxPopupMatrixTest, matrix_); |
60 | 84 |
61 TEST_F(OmniboxPopupMatrixTest, HighlightedRow) { | 85 TEST_F(OmniboxPopupMatrixTest, HighlightedRow) { |
62 [matrix_ renewRows:3 columns:1]; | |
63 EXPECT_EQ(-1, [matrix_ highlightedRow]); | 86 EXPECT_EQ(-1, [matrix_ highlightedRow]); |
64 | 87 |
65 [matrix_ mouseEntered:MouseEventInRow(matrix_, NSMouseMoved, 0)]; | 88 [matrix_ mouseEntered:MouseEventInRow(matrix_, NSMouseMoved, 0)]; |
66 EXPECT_EQ(0, [matrix_ highlightedRow]); | 89 EXPECT_EQ(0, [matrix_ highlightedRow]); |
67 [matrix_ mouseEntered:MouseEventInRow(matrix_, NSMouseMoved, 2)]; | 90 [matrix_ mouseEntered:MouseEventInRow(matrix_, NSMouseMoved, 2)]; |
68 EXPECT_EQ(2, [matrix_ highlightedRow]); | 91 EXPECT_EQ(2, [matrix_ highlightedRow]); |
69 | 92 |
70 [matrix_ mouseExited:MouseEventInRow(matrix_, NSMouseMoved, 2)]; | 93 [matrix_ mouseExited:MouseEventInRow(matrix_, NSMouseMoved, 2)]; |
71 EXPECT_EQ(-1, [matrix_ highlightedRow]); | 94 EXPECT_EQ(-1, [matrix_ highlightedRow]); |
72 } | 95 } |
73 | 96 |
74 TEST_F(OmniboxPopupMatrixTest, SelectedRow) { | 97 TEST_F(OmniboxPopupMatrixTest, SelectedRow) { |
75 [matrix_ renewRows:3 columns:1]; | |
76 | |
77 [NSApp postEvent:MouseEventInRow(matrix_, NSLeftMouseUp, 2) atStart:YES]; | 98 [NSApp postEvent:MouseEventInRow(matrix_, NSLeftMouseUp, 2) atStart:YES]; |
78 [matrix_ mouseDown:MouseEventInRow(matrix_, NSLeftMouseDown, 2)]; | 99 [matrix_ mouseDown:MouseEventInRow(matrix_, NSLeftMouseDown, 2)]; |
79 | 100 |
80 EXPECT_EQ(2u, selected_row_); | 101 EXPECT_EQ(2u, selected_row_); |
81 EXPECT_EQ(2u, clicked_row_); | 102 EXPECT_EQ(2u, clicked_row_); |
82 EXPECT_EQ(0u, middle_clicked_row_); | 103 EXPECT_EQ(0u, middle_clicked_row_); |
83 } | 104 } |
84 | 105 |
85 } // namespace | 106 } // namespace |
OLD | NEW |