Home | History | Annotate | Download | only in find_bar
      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.
      4 
      5 #import <Cocoa/Cocoa.h>
      6 
      7 #include "base/memory/scoped_nsobject.h"
      8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
      9 #import "chrome/browser/ui/cocoa/find_bar/find_bar_view.h"
     10 #include "chrome/browser/ui/cocoa/test_event_utils.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 #include "testing/platform_test.h"
     13 
     14 @interface MouseDownViewPong : NSView {
     15   BOOL pong_;
     16 }
     17 @property (nonatomic, assign) BOOL pong;
     18 @end
     19 
     20 @implementation MouseDownViewPong
     21 @synthesize pong = pong_;
     22 - (void)mouseDown:(NSEvent*)event {
     23   pong_ = YES;
     24 }
     25 @end
     26 
     27 
     28 namespace {
     29 
     30 class FindBarViewTest : public CocoaTest {
     31  public:
     32   FindBarViewTest() {
     33     NSRect frame = NSMakeRect(0, 0, 100, 30);
     34     scoped_nsobject<FindBarView> view(
     35         [[FindBarView alloc] initWithFrame:frame]);
     36     view_ = view.get();
     37     [[test_window() contentView] addSubview:view_];
     38   }
     39 
     40   FindBarView* view_;
     41 };
     42 
     43 TEST_VIEW(FindBarViewTest, view_)
     44 
     45 TEST_F(FindBarViewTest, FindBarEatsMouseClicksInBackgroundArea) {
     46   scoped_nsobject<MouseDownViewPong> pongView(
     47       [[MouseDownViewPong alloc] initWithFrame:NSMakeRect(0, 0, 200, 200)]);
     48 
     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
     51   // background area.
     52   [view_ setSubviews:[NSArray array]];
     53   [view_ setFrame:NSMakeRect(0, 0, 200, 200)];
     54 
     55   // Add the pong view as a sibling of the findbar.
     56   [[test_window() contentView] addSubview:pongView.get()
     57                                positioned:NSWindowBelow
     58                                relativeTo:view_];
     59 
     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()
     65       sendEvent:test_event_utils::LeftMouseDownAtPoint(pointInCenterOfFindBar)];
     66   // Click gets eaten by findbar, not passed through to underlying view.
     67   EXPECT_FALSE([pongView pong]);
     68 }
     69 
     70 TEST_F(FindBarViewTest, FindBarPassesThroughClicksInTransparentArea) {
     71   scoped_nsobject<MouseDownViewPong> pongView(
     72       [[MouseDownViewPong alloc] initWithFrame:NSMakeRect(0, 0, 200, 200)]);
     73   [view_ setFrame:NSMakeRect(0, 0, 200, 200)];
     74 
     75   // Add the pong view as a sibling of the findbar.
     76   [[test_window() contentView] addSubview:pongView.get()
     77                                positioned:NSWindowBelow
     78                                relativeTo:view_];
     79 
     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()
     86       sendEvent:test_event_utils::LeftMouseDownAtPoint(pointInTransparentArea)];
     87   // Click is ignored by findbar, passed through to underlying view.
     88   EXPECT_TRUE([pongView pong]);
     89 }
     90 }  // namespace
     91