1 // Copyright 2015 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 "ui/base/test/scoped_fake_nswindow_fullscreen.h"
7 #import "base/mac/mac_util.h"
8 #import "base/mac/scoped_nsobject.h"
9 #import "base/mac/sdk_forward_declarations.h"
10 #include "base/message_loop/message_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #import "ui/base/test/windowed_nsnotification_observer.h"
13 #import "ui/gfx/mac/nswindow_frame_controls.h"
15 @interface TestNSWindowDelegate : NSObject<NSWindowDelegate> {
19 - (id)initWithFullScreenContentSize:(NSSize)targetSize;
22 @implementation TestNSWindowDelegate
24 - (id)initWithFullScreenContentSize:(NSSize)targetSize {
25 if ((self = [super init])) {
26 targetSize_ = targetSize;
31 - (NSSize)window:(NSWindow*)window
32 willUseFullScreenContentSize:(NSSize)proposedSize {
41 // Test the order of notifications sent when faking fullscreen transitions.
42 TEST(ScopedFakeNSWindowFullscreenTest, TestOrdering) {
43 if (base::mac::IsOSSnowLeopard())
46 base::MessageLoopForUI message_loop;
48 NSUInteger style_mask = NSTexturedBackgroundWindowMask | NSTitledWindowMask |
49 NSClosableWindowMask | NSMiniaturizableWindowMask |
50 NSResizableWindowMask;
51 base::scoped_nsobject<NSWindow> window(
52 [[NSWindow alloc] initWithContentRect:NSMakeRect(50, 60, 130, 170)
54 backing:NSBackingStoreBuffered
56 [window setReleasedWhenClosed:NO];
57 [window orderFront:nil];
58 NSRect initial_frame = [window frame];
60 NSSize fullscreen_content_size = NSMakeSize(500, 400);
61 base::scoped_nsobject<TestNSWindowDelegate> delegate(
62 [[TestNSWindowDelegate alloc]
63 initWithFullScreenContentSize:fullscreen_content_size]);
64 [window setDelegate:delegate];
66 base::scoped_nsobject<WindowedNSNotificationObserver> will_enter(
67 [[WindowedNSNotificationObserver alloc]
68 initForNotification:NSWindowWillEnterFullScreenNotification
70 base::scoped_nsobject<WindowedNSNotificationObserver> did_enter(
71 [[WindowedNSNotificationObserver alloc]
72 initForNotification:NSWindowDidEnterFullScreenNotification
74 base::scoped_nsobject<WindowedNSNotificationObserver> will_exit(
75 [[WindowedNSNotificationObserver alloc]
76 initForNotification:NSWindowWillExitFullScreenNotification
78 base::scoped_nsobject<WindowedNSNotificationObserver> did_exit(
79 [[WindowedNSNotificationObserver alloc]
80 initForNotification:NSWindowDidExitFullScreenNotification
83 test::ScopedFakeNSWindowFullscreen fake_fullscreen;
85 // Nothing happens if the window cannot go fullscreen.
86 gfx::SetNSWindowCanFullscreen(window, false);
87 [window toggleFullScreen:nil];
88 EXPECT_EQ(0, [will_enter notificationCount]);
90 gfx::SetNSWindowCanFullscreen(window, true);
92 // WillEnter is immediate.
93 [window toggleFullScreen:nil];
94 EXPECT_EQ(1, [will_enter notificationCount]);
95 EXPECT_EQ(0, [did_enter notificationCount]);
96 EXPECT_TRUE(NSEqualRects(initial_frame, [window frame]));
97 EXPECT_FALSE([window styleMask] & NSFullScreenWindowMask);
99 // Changes and DidEnter happen asynchronously.
100 EXPECT_TRUE([did_enter wait]);
101 EXPECT_EQ(fullscreen_content_size.width, [window frame].size.width);
102 EXPECT_EQ(fullscreen_content_size.height, [window frame].size.height);
103 EXPECT_TRUE([window styleMask] & NSFullScreenWindowMask);
105 // WillExit is immediate.
106 [window toggleFullScreen:nil];
107 EXPECT_EQ(1, [will_exit notificationCount]);
108 EXPECT_EQ(0, [did_exit notificationCount]);
109 EXPECT_EQ(fullscreen_content_size.width, [window frame].size.width);
110 EXPECT_EQ(fullscreen_content_size.height, [window frame].size.height);
111 EXPECT_TRUE([window styleMask] & NSFullScreenWindowMask);
113 // Changes and DidExit happen asynchronously.
114 EXPECT_TRUE([did_exit wait]);
115 EXPECT_TRUE(NSEqualRects(initial_frame, [window frame]));
116 EXPECT_FALSE([window styleMask] & NSFullScreenWindowMask);
118 // Go back into fullscreen.
119 [window toggleFullScreen:nil];
120 EXPECT_TRUE([did_enter waitForCount:2]);
122 // On the way out, call -[NSWindow setFrame:]. It should stay at those bounds.
123 [window toggleFullScreen:nil];
124 NSRect new_frame = NSMakeRect(90, 90, 90, 90);
125 [window setFrame:new_frame display:YES animate:NO];
126 EXPECT_TRUE([did_exit waitForCount:2]);
127 NSRect frame_outside_fullscreen = [window frameRectForContentRect:new_frame];
128 EXPECT_TRUE(NSEqualRects(frame_outside_fullscreen, [window frame]));