Home | History | Annotate | Download | only in cocoa
      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/debug/debugger.h"
      8 #include "base/memory/scoped_nsobject.h"
      9 #include "chrome/app/chrome_command_ids.h"
     10 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
     11 #import "chrome/browser/ui/cocoa/browser_frame_view.h"
     12 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
     13 #import "chrome/browser/ui/cocoa/framed_browser_window.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 #import "testing/gtest_mac.h"
     16 #include "testing/platform_test.h"
     17 #import "third_party/ocmock/OCMock/OCMock.h"
     18 
     19 class FramedBrowserWindowTest : public CocoaTest {
     20  public:
     21   virtual void SetUp() {
     22     CocoaTest::SetUp();
     23     // Create a window.
     24     const NSUInteger mask = NSTitledWindowMask | NSClosableWindowMask |
     25         NSMiniaturizableWindowMask | NSResizableWindowMask;
     26     window_ = [[FramedBrowserWindow alloc]
     27                initWithContentRect:NSMakeRect(0, 0, 800, 600)
     28                          styleMask:mask
     29                            backing:NSBackingStoreBuffered
     30                              defer:NO];
     31     if (base::debug::BeingDebugged()) {
     32       [window_ orderFront:nil];
     33     } else {
     34       [window_ orderBack:nil];
     35     }
     36   }
     37 
     38   virtual void TearDown() {
     39     [window_ close];
     40     CocoaTest::TearDown();
     41   }
     42 
     43   // Returns a canonical snapshot of the window.
     44   NSData* WindowContentsAsTIFF() {
     45     [window_ display];
     46 
     47     NSView* frameView = [window_ contentView];
     48     while ([frameView superview]) {
     49       frameView = [frameView superview];
     50     }
     51     const NSRect bounds = [frameView bounds];
     52 
     53     [frameView lockFocus];
     54     scoped_nsobject<NSBitmapImageRep> bitmap(
     55         [[NSBitmapImageRep alloc] initWithFocusedViewRect:bounds]);
     56     [frameView unlockFocus];
     57 
     58     return [bitmap TIFFRepresentation];
     59   }
     60 
     61   FramedBrowserWindow* window_;
     62 };
     63 
     64 // Baseline test that the window creates, displays, closes, and
     65 // releases.
     66 TEST_F(FramedBrowserWindowTest, ShowAndClose) {
     67   [window_ display];
     68 }
     69 
     70 // Test that undocumented title-hiding API we're using does the job.
     71 TEST_F(FramedBrowserWindowTest, DoesHideTitle) {
     72   // The -display calls are not strictly necessary, but they do
     73   // make it easier to see what's happening when debugging (without
     74   // them the changes are never flushed to the screen).
     75 
     76   [window_ setTitle:@""];
     77   [window_ display];
     78   NSData* emptyTitleData = WindowContentsAsTIFF();
     79 
     80   [window_ setTitle:@"This is a title"];
     81   [window_ display];
     82   NSData* thisTitleData = WindowContentsAsTIFF();
     83 
     84   // The default window with a title should look different from the
     85   // window with an empty title.
     86   EXPECT_FALSE([emptyTitleData isEqualToData:thisTitleData]);
     87 
     88   [window_ setShouldHideTitle:YES];
     89   [window_ setTitle:@""];
     90   [window_ display];
     91   [window_ setTitle:@"This is a title"];
     92   [window_ display];
     93   NSData* hiddenTitleData = WindowContentsAsTIFF();
     94 
     95   // With our magic setting, the window with a title should look the
     96   // same as the window with an empty title.
     97   EXPECT_TRUE([window_ _isTitleHidden]);
     98   EXPECT_TRUE([emptyTitleData isEqualToData:hiddenTitleData]);
     99 }
    100 
    101 // Test to make sure that our window widgets are in the right place.
    102 TEST_F(FramedBrowserWindowTest, WindowWidgetLocation) {
    103   // First without tabstrip.
    104   NSView* closeBoxControl = [window_ standardWindowButton:NSWindowCloseButton];
    105   EXPECT_TRUE(closeBoxControl);
    106   NSRect closeBoxFrame = [closeBoxControl frame];
    107   NSRect windowBounds = [window_ frame];
    108   windowBounds = [[window_ contentView] convertRect:windowBounds fromView:nil];
    109   windowBounds.origin = NSZeroPoint;
    110   EXPECT_EQ(NSMaxY(closeBoxFrame),
    111             NSMaxY(windowBounds) -
    112                 kFramedWindowButtonsWithoutTabStripOffsetFromTop);
    113   EXPECT_EQ(NSMinX(closeBoxFrame),
    114             kFramedWindowButtonsWithoutTabStripOffsetFromLeft);
    115 
    116   NSView* miniaturizeControl =
    117       [window_ standardWindowButton:NSWindowMiniaturizeButton];
    118   EXPECT_TRUE(miniaturizeControl);
    119   NSRect miniaturizeFrame = [miniaturizeControl frame];
    120   EXPECT_EQ(NSMaxY(miniaturizeFrame),
    121             NSMaxY(windowBounds) -
    122                 kFramedWindowButtonsWithoutTabStripOffsetFromTop);
    123   EXPECT_EQ(NSMinX(miniaturizeFrame),
    124             NSMaxX(closeBoxFrame) + kFramedWindowButtonsInterButtonSpacing);
    125 
    126   // Then with a tabstrip.
    127   id controller = [OCMockObject mockForClass:[BrowserWindowController class]];
    128   BOOL yes = YES;
    129   BOOL no = NO;
    130   [[[controller stub] andReturnValue:OCMOCK_VALUE(yes)]
    131    isKindOfClass:[BrowserWindowController class]];
    132   [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] hasTabStrip];
    133   [[[controller expect] andReturnValue:OCMOCK_VALUE(no)] hasTitleBar];
    134   [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] isNormalWindow];
    135   [window_ setWindowController:controller];
    136 
    137   closeBoxControl = [window_ standardWindowButton:NSWindowCloseButton];
    138   EXPECT_TRUE(closeBoxControl);
    139   closeBoxFrame = [closeBoxControl frame];
    140   windowBounds = [window_ frame];
    141   windowBounds = [[window_ contentView] convertRect:windowBounds fromView:nil];
    142   windowBounds.origin = NSZeroPoint;
    143   EXPECT_EQ(NSMaxY(closeBoxFrame),
    144             NSMaxY(windowBounds) -
    145                 kFramedWindowButtonsWithTabStripOffsetFromTop);
    146   EXPECT_EQ(NSMinX(closeBoxFrame),
    147             kFramedWindowButtonsWithTabStripOffsetFromLeft);
    148 
    149   miniaturizeControl = [window_ standardWindowButton:NSWindowMiniaturizeButton];
    150   EXPECT_TRUE(miniaturizeControl);
    151   miniaturizeFrame = [miniaturizeControl frame];
    152   EXPECT_EQ(NSMaxY(miniaturizeFrame),
    153             NSMaxY(windowBounds) -
    154                 kFramedWindowButtonsWithTabStripOffsetFromTop);
    155   EXPECT_EQ(NSMinX(miniaturizeFrame),
    156             NSMaxX(closeBoxFrame) + kFramedWindowButtonsInterButtonSpacing);
    157   [window_ setWindowController:nil];
    158 }
    159 
    160