1 // Copyright (c) 2011 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 <Cocoa/Cocoa.h>
7 #include "base/mac/scoped_nsobject.h"
8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
9 #import "chrome/browser/ui/cocoa/find_bar/find_bar_view_cocoa.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h"
12 #include "ui/events/test/cocoa_test_event_utils.h"
14 @interface MouseDownViewPong : NSView {
17 @property (nonatomic, assign) BOOL pong;
20 @implementation MouseDownViewPong
21 @synthesize pong = pong_;
22 - (void)mouseDown:(NSEvent*)event {
30 class FindBarViewTest : public CocoaTest {
33 NSRect frame = NSMakeRect(0, 0, 100, 30);
34 base::scoped_nsobject<FindBarView> view(
35 [[FindBarView alloc] initWithFrame:frame]);
37 [[test_window() contentView] addSubview:view_];
43 TEST_VIEW(FindBarViewTest, view_)
45 TEST_F(FindBarViewTest, FindBarEatsMouseClicksInBackgroundArea) {
46 base::scoped_nsobject<MouseDownViewPong> pongView(
47 [[MouseDownViewPong alloc] initWithFrame:NSMakeRect(0, 0, 200, 200)]);
49 // Remove all of the subviews of the findbar, to make sure we don't
50 // accidentally hit a subview when trying to simulate a click in the
52 [view_ setSubviews:[NSArray array]];
53 [view_ setFrame:NSMakeRect(0, 0, 200, 200)];
55 // Add the pong view as a sibling of the findbar.
56 [[test_window() contentView] addSubview:pongView.get()
57 positioned:NSWindowBelow
60 // Synthesize a mousedown event and send it to the window. The event is
61 // placed in the center of the find bar.
62 NSPoint pointInCenterOfFindBar = NSMakePoint(100, 100);
63 [pongView setPong:NO];
64 [test_window() sendEvent:
65 cocoa_test_event_utils::LeftMouseDownAtPoint(pointInCenterOfFindBar)];
66 // Click gets eaten by findbar, not passed through to underlying view.
67 EXPECT_FALSE([pongView pong]);
70 TEST_F(FindBarViewTest, FindBarPassesThroughClicksInTransparentArea) {
71 base::scoped_nsobject<MouseDownViewPong> pongView(
72 [[MouseDownViewPong alloc] initWithFrame:NSMakeRect(0, 0, 200, 200)]);
73 [view_ setFrame:NSMakeRect(0, 0, 200, 200)];
75 // Add the pong view as a sibling of the findbar.
76 [[test_window() contentView] addSubview:pongView.get()
77 positioned:NSWindowBelow
80 // Synthesize a mousedown event and send it to the window. The event is inset
81 // a few pixels from the lower left corner of the window, which places it in
82 // the transparent area surrounding the findbar.
83 NSPoint pointInTransparentArea = NSMakePoint(2, 2);
84 [pongView setPong:NO];
85 [test_window() sendEvent:
86 cocoa_test_event_utils::LeftMouseDownAtPoint(pointInTransparentArea)];
87 // Click is ignored by findbar, passed through to underlying view.
88 EXPECT_TRUE([pongView pong]);