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_matrix.h"
7 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
8 #import "ui/events/test/cocoa_test_event_utils.h"
12 NSEvent* MouseEventInRow(NSMatrix* matrix, NSEventType type, NSInteger row) {
13 NSRect cell_rect = [matrix cellFrameAtRow:row column:0];
14 NSPoint point_in_view = NSMakePoint(NSMidX(cell_rect), NSMidY(cell_rect));
15 NSPoint point_in_window = [matrix convertPoint:point_in_view toView:nil];
16 return cocoa_test_event_utils::MouseEventAtPoint(
17 point_in_window, type, 0);
20 class OmniboxPopupMatrixTest : public CocoaTest,
21 public OmniboxPopupMatrixDelegate {
23 OmniboxPopupMatrixTest()
26 middle_clicked_row_(0) {
29 void SetUp() override {
31 matrix_.reset([[OmniboxPopupMatrix alloc] initWithDelegate:this]);
32 [[test_window() contentView] addSubview:matrix_];
35 void OnMatrixRowSelected(OmniboxPopupMatrix* matrix, size_t row) override {
37 [matrix_ selectCellAtRow:row column:0];
40 void OnMatrixRowClicked(OmniboxPopupMatrix* matrix, size_t row) override {
44 void OnMatrixRowMiddleClicked(OmniboxPopupMatrix* matrix,
45 size_t row) override {
46 middle_clicked_row_ = row;
50 base::scoped_nsobject<OmniboxPopupMatrix> matrix_;
53 size_t middle_clicked_row_;
56 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupMatrixTest);
59 TEST_VIEW(OmniboxPopupMatrixTest, matrix_);
61 TEST_F(OmniboxPopupMatrixTest, HighlightedRow) {
62 [matrix_ renewRows:3 columns:1];
63 EXPECT_EQ(-1, [matrix_ highlightedRow]);
65 [matrix_ mouseEntered:MouseEventInRow(matrix_, NSMouseMoved, 0)];
66 EXPECT_EQ(0, [matrix_ highlightedRow]);
67 [matrix_ mouseEntered:MouseEventInRow(matrix_, NSMouseMoved, 2)];
68 EXPECT_EQ(2, [matrix_ highlightedRow]);
70 [matrix_ mouseExited:MouseEventInRow(matrix_, NSMouseMoved, 2)];
71 EXPECT_EQ(-1, [matrix_ highlightedRow]);
74 TEST_F(OmniboxPopupMatrixTest, SelectedRow) {
75 [matrix_ renewRows:3 columns:1];
77 [NSApp postEvent:MouseEventInRow(matrix_, NSLeftMouseUp, 2) atStart:YES];
78 [matrix_ mouseDown:MouseEventInRow(matrix_, NSLeftMouseDown, 2)];
80 EXPECT_EQ(2u, selected_row_);
81 EXPECT_EQ(2u, clicked_row_);
82 EXPECT_EQ(0u, middle_clicked_row_);