Home | History | Annotate | Download | only in location_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 "base/memory/scoped_nsobject.h"
      6 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
      7 #import "chrome/browser/ui/cocoa/location_bar/omnibox_popup_view.h"
      8 
      9 namespace {
     10 
     11 class OmniboxPopupViewTest : public CocoaTest {
     12  public:
     13   OmniboxPopupViewTest() {
     14     NSRect content_frame = [[test_window() contentView] frame];
     15     scoped_nsobject<OmniboxPopupView> view(
     16         [[OmniboxPopupView alloc] initWithFrame:content_frame]);
     17     view_ = view.get();
     18     [[test_window() contentView] addSubview:view_];
     19   }
     20 
     21   OmniboxPopupView* view_;  // Weak. Owned by the view hierarchy.
     22 };
     23 
     24 // Tests display, add/remove.
     25 TEST_VIEW(OmniboxPopupViewTest, view_);
     26 
     27 // A single subview should completely fill the popup view.
     28 TEST_F(OmniboxPopupViewTest, ResizeWithOneSubview) {
     29   scoped_nsobject<NSView> subview1([[NSView alloc] initWithFrame:NSZeroRect]);
     30 
     31   // Adding the subview should not change its frame.
     32   [view_ addSubview:subview1];
     33   EXPECT_TRUE(NSEqualRects(NSZeroRect, [subview1 frame]));
     34 
     35   // Resizing the popup view should also resize the subview.
     36   [view_ setFrame:NSMakeRect(0, 0, 100, 100)];
     37   EXPECT_TRUE(NSEqualRects([view_ bounds], [subview1 frame]));
     38 }
     39 
     40 TEST_F(OmniboxPopupViewTest, ResizeWithTwoSubviews) {
     41   const CGFloat height = 50;
     42   NSRect initial = NSMakeRect(0, 0, 100, height);
     43 
     44   scoped_nsobject<NSView> subview1([[NSView alloc] initWithFrame:NSZeroRect]);
     45   scoped_nsobject<NSView> subview2([[NSView alloc] initWithFrame:initial]);
     46   [view_ addSubview:subview1];
     47   [view_ addSubview:subview2];
     48 
     49   // Resize the popup view to be much larger than height.  |subview2|'s height
     50   // should stay the same, and |subview1| should resize to fill all available
     51   // space.
     52   [view_ setFrame:NSMakeRect(0, 0, 300, 4 * height)];
     53   EXPECT_EQ(NSWidth([view_ frame]), NSWidth([subview1 frame]));
     54   EXPECT_EQ(NSWidth([view_ frame]), NSWidth([subview2 frame]));
     55   EXPECT_EQ(height, NSHeight([subview2 frame]));
     56   EXPECT_EQ(NSHeight([view_ frame]),
     57             NSHeight([subview1 frame]) + NSHeight([subview2 frame]));
     58 
     59   // Now resize the popup view to be smaller than height.  |subview2|'s height
     60   // should stay the same, and |subview1|'s height should be zero, not negative.
     61   [view_ setFrame:NSMakeRect(0, 0, 300, height - 10)];
     62   EXPECT_EQ(NSWidth([view_ frame]), NSWidth([subview1 frame]));
     63   EXPECT_EQ(NSWidth([view_ frame]), NSWidth([subview2 frame]));
     64   EXPECT_EQ(0, NSHeight([subview1 frame]));
     65   EXPECT_EQ(height, NSHeight([subview2 frame]));
     66 }
     67 
     68 }  // namespace
     69