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.
5 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h"
6 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.h"
8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
9 #import "ui/events/test/cocoa_test_event_utils.h"
13 NSEvent* MouseEventInRow(OmniboxPopupMatrix* matrix,
16 NSRect cell_rect = [matrix rectOfRow:row];
17 NSPoint point_in_view = NSMakePoint(NSMidX(cell_rect), NSMidY(cell_rect));
18 NSPoint point_in_window = [matrix convertPoint:point_in_view toView:nil];
19 return cocoa_test_event_utils::MouseEventAtPoint(
20 point_in_window, type, 0);
23 class OmniboxPopupMatrixTest : public CocoaTest,
24 public OmniboxPopupMatrixObserver {
26 OmniboxPopupMatrixTest()
27 : selected_row_(0), clicked_row_(0), middle_clicked_row_(0) {}
29 void SetUp() override {
31 matrix_.reset([[OmniboxPopupMatrix alloc] initWithObserver:this]);
32 [[test_window() contentView] addSubview:matrix_];
34 NSMutableArray* array = [NSMutableArray array];
35 for (size_t i = 0; i < 3; ++i)
36 [array addObject:[[[OmniboxPopupCellData alloc] init] autorelease]];
38 matrixController_.reset(
39 [[OmniboxPopupTableController alloc] initWithArray:array]);
40 [matrix_ setController:matrixController_];
43 void OnMatrixRowSelected(OmniboxPopupMatrix* matrix, size_t row) override {
47 void OnMatrixRowClicked(OmniboxPopupMatrix* matrix, size_t row) override {
51 void OnMatrixRowMiddleClicked(OmniboxPopupMatrix* matrix,
52 size_t row) override {
53 middle_clicked_row_ = row;
57 base::scoped_nsobject<OmniboxPopupMatrix> matrix_;
58 base::scoped_nsobject<OmniboxPopupTableController> matrixController_;
61 size_t middle_clicked_row_;
64 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupMatrixTest);
67 TEST_VIEW(OmniboxPopupMatrixTest, matrix_);
69 TEST_F(OmniboxPopupMatrixTest, HighlightedRow) {
70 EXPECT_EQ(-1, [matrix_ highlightedRow]);
72 [matrix_ mouseMoved:MouseEventInRow(matrix_, NSMouseMoved, 0)];
73 EXPECT_EQ(0, [matrix_ highlightedRow]);
74 [matrix_ mouseMoved:MouseEventInRow(matrix_, NSMouseMoved, 2)];
75 EXPECT_EQ(2, [matrix_ highlightedRow]);
77 [matrix_ mouseExited:cocoa_test_event_utils::MouseEventAtPoint(
78 NSZeroPoint, NSMouseMoved, 0)];
79 EXPECT_EQ(-1, [matrix_ highlightedRow]);
82 TEST_F(OmniboxPopupMatrixTest, SelectedRow) {
83 [NSApp postEvent:MouseEventInRow(matrix_, NSLeftMouseUp, 2) atStart:YES];
84 [matrix_ mouseDown:MouseEventInRow(matrix_, NSLeftMouseDown, 2)];
86 EXPECT_EQ(2u, selected_row_);
87 EXPECT_EQ(2u, clicked_row_);
88 EXPECT_EQ(0u, middle_clicked_row_);