OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_con
troller.h" |
| 6 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| 7 #include "chrome/browser/ui/cocoa/run_loop_testing.h" |
| 8 #import "testing/gtest_mac.h" |
| 9 |
| 10 class ConstrainedWindowSheetControllerTest : public CocoaTest { |
| 11 protected: |
| 12 ConstrainedWindowSheetControllerTest() { |
| 13 } |
| 14 |
| 15 ~ConstrainedWindowSheetControllerTest() { |
| 16 } |
| 17 |
| 18 virtual void SetUp() OVERRIDE { |
| 19 CocoaTest::SetUp(); |
| 20 |
| 21 // Create two dummy tabs and make the first one active. |
| 22 NSRect dummyRect = NSMakeRect(0, 0, 50, 50); |
| 23 tab_views_.reset([[NSMutableArray alloc] init]); |
| 24 for (int i = 0; i < 2; ++i) { |
| 25 scoped_nsobject<NSView> view([[NSView alloc] initWithFrame:dummyRect]); |
| 26 [tab_views_ addObject:view]; |
| 27 } |
| 28 ActivateTabView([tab_views_ objectAtIndex:0]); |
| 29 |
| 30 // Create a test sheet. |
| 31 sheet_.reset([[NSWindow alloc] initWithContentRect:dummyRect |
| 32 styleMask:NSTitledWindowMask |
| 33 backing:NSBackingStoreBuffered |
| 34 defer:NO]); |
| 35 [sheet_ setReleasedWhenClosed:NO]; |
| 36 |
| 37 EXPECT_FALSE([ConstrainedWindowSheetController controllerForSheet:sheet_]); |
| 38 } |
| 39 |
| 40 virtual void TearDown() OVERRIDE { |
| 41 sheet_.reset(); |
| 42 CocoaTest::TearDown(); |
| 43 } |
| 44 |
| 45 void ActivateTabView(NSView* tab_view) { |
| 46 for (NSView* view in tab_views_.get()) { |
| 47 [view removeFromSuperview]; |
| 48 } |
| 49 [[test_window() contentView] addSubview:tab_view]; |
| 50 active_tab_view_.reset([tab_view retain]); |
| 51 |
| 52 ConstrainedWindowSheetController* controller = |
| 53 [ConstrainedWindowSheetController |
| 54 controllerForParentWindow:test_window()]; |
| 55 EXPECT_TRUE(controller); |
| 56 [controller parentViewDidBecomeActive:active_tab_view_]; |
| 57 } |
| 58 |
| 59 scoped_nsobject<NSWindow> sheet_; |
| 60 scoped_nsobject<NSMutableArray> tab_views_; |
| 61 scoped_nsobject<NSView> active_tab_view_; |
| 62 }; |
| 63 |
| 64 // Test showing then hiding the sheet. |
| 65 TEST_F(ConstrainedWindowSheetControllerTest, ShowHide) { |
| 66 ConstrainedWindowSheetController* controller = |
| 67 [ConstrainedWindowSheetController |
| 68 controllerForParentWindow:test_window()]; |
| 69 EXPECT_TRUE(controller); |
| 70 |
| 71 EXPECT_FALSE([sheet_ isVisible]); |
| 72 [controller showSheet:sheet_ forParentView:active_tab_view_]; |
| 73 EXPECT_TRUE([ConstrainedWindowSheetController controllerForSheet:sheet_]); |
| 74 EXPECT_TRUE([sheet_ isVisible]); |
| 75 |
| 76 [controller closeSheet:sheet_]; |
| 77 [controller endAnimationForSheet:sheet_]; |
| 78 chrome::testing::NSRunLoopRunAllPending(); |
| 79 EXPECT_FALSE([ConstrainedWindowSheetController controllerForSheet:sheet_]); |
| 80 EXPECT_FALSE([sheet_ isVisible]); |
| 81 } |
| 82 |
| 83 // Test that switching tabs correctly hides the inactive tab's sheet. |
| 84 TEST_F(ConstrainedWindowSheetControllerTest, SwitchTabs) { |
| 85 ConstrainedWindowSheetController* controller = |
| 86 [ConstrainedWindowSheetController |
| 87 controllerForParentWindow:test_window()]; |
| 88 [controller showSheet:sheet_ forParentView:active_tab_view_]; |
| 89 |
| 90 EXPECT_TRUE([sheet_ isVisible]); |
| 91 EXPECT_EQ(1.0, [sheet_ alphaValue]); |
| 92 ActivateTabView([tab_views_ objectAtIndex:1]); |
| 93 EXPECT_TRUE([sheet_ isVisible]); |
| 94 EXPECT_EQ(0.0, [sheet_ alphaValue]); |
| 95 ActivateTabView([tab_views_ objectAtIndex:0]); |
| 96 EXPECT_TRUE([sheet_ isVisible]); |
| 97 EXPECT_EQ(1.0, [sheet_ alphaValue]); |
| 98 } |
| 99 |
| 100 // Test that two parent windows with two sheet controllers don't conflict. |
| 101 TEST_F(ConstrainedWindowSheetControllerTest, TwoParentWindows) { |
| 102 ConstrainedWindowSheetController* controller1 = |
| 103 [ConstrainedWindowSheetController |
| 104 controllerForParentWindow:test_window()]; |
| 105 EXPECT_TRUE(controller1); |
| 106 |
| 107 scoped_nsobject<NSWindow> parent_window2([[NSWindow alloc] |
| 108 initWithContentRect:NSMakeRect(0, 0, 30, 30) |
| 109 styleMask:NSTitledWindowMask |
| 110 backing:NSBackingStoreBuffered |
| 111 defer:NO]); |
| 112 [parent_window2 setReleasedWhenClosed:NO]; |
| 113 |
| 114 ConstrainedWindowSheetController* controller2 = |
| 115 [ConstrainedWindowSheetController |
| 116 controllerForParentWindow:parent_window2]; |
| 117 EXPECT_TRUE(controller2); |
| 118 EXPECT_NSNE(controller1, controller2); |
| 119 |
| 120 [controller2 showSheet:sheet_ forParentView:[parent_window2 contentView]]; |
| 121 EXPECT_NSEQ(controller2, |
| 122 [ConstrainedWindowSheetController controllerForSheet:sheet_]); |
| 123 |
| 124 [parent_window2 close]; |
| 125 } |
OLD | NEW |