1 // Copyright 2013 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 "chrome/browser/ui/cocoa/apps/native_app_window_cocoa.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/mac/mac_util.h"
10 #include "base/mac/sdk_forward_declarations.h"
11 #include "chrome/browser/apps/app_browsertest_util.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/extensions/application_launch.h"
14 #include "content/public/browser/notification_service.h"
15 #include "content/public/test/test_utils.h"
16 #include "extensions/browser/app_window/app_window_registry.h"
18 using extensions::PlatformAppBrowserTest;
22 class NativeAppWindowCocoaBrowserTest : public PlatformAppBrowserTest {
24 NativeAppWindowCocoaBrowserTest() {}
26 void SetUpAppWithWindows(int num_windows) {
27 app_ = InstallExtension(
28 test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal"), 1);
31 for (int i = 0; i < num_windows; ++i) {
32 content::WindowedNotificationObserver app_loaded_observer(
33 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
34 content::NotificationService::AllSources());
36 AppLaunchParams(profile(),
38 extensions::LAUNCH_CONTAINER_NONE,
40 app_loaded_observer.Wait();
44 const extensions::Extension* app_;
47 DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoaBrowserTest);
52 // Test interaction of Hide/Show() with Hide/ShowWithApp().
53 IN_PROC_BROWSER_TEST_F(NativeAppWindowCocoaBrowserTest, HideShowWithApp) {
54 SetUpAppWithWindows(2);
55 extensions::AppWindowRegistry::AppWindowList windows =
56 extensions::AppWindowRegistry::Get(profile())->app_windows();
58 extensions::AppWindow* app_window = windows.front();
59 extensions::NativeAppWindow* native_window = app_window->GetBaseWindow();
60 NSWindow* ns_window = native_window->GetNativeWindow();
62 extensions::AppWindow* other_app_window = windows.back();
63 extensions::NativeAppWindow* other_native_window =
64 other_app_window->GetBaseWindow();
65 NSWindow* other_ns_window = other_native_window->GetNativeWindow();
69 EXPECT_FALSE([ns_window isVisible]);
70 app_window->Show(extensions::AppWindow::SHOW_ACTIVE);
71 EXPECT_TRUE([ns_window isVisible]);
73 // Normal Hide/ShowWithApp.
74 native_window->HideWithApp();
75 EXPECT_FALSE([ns_window isVisible]);
76 native_window->ShowWithApp();
77 EXPECT_TRUE([ns_window isVisible]);
79 // HideWithApp, Hide, ShowWithApp does not show.
80 native_window->HideWithApp();
82 native_window->ShowWithApp();
83 EXPECT_FALSE([ns_window isVisible]);
85 // Hide, HideWithApp, ShowWithApp does not show.
86 native_window->HideWithApp();
87 native_window->ShowWithApp();
88 EXPECT_FALSE([ns_window isVisible]);
90 // Return to shown state.
91 app_window->Show(extensions::AppWindow::SHOW_ACTIVE);
92 EXPECT_TRUE([ns_window isVisible]);
94 // HideWithApp the other window.
95 EXPECT_TRUE([other_ns_window isVisible]);
96 other_native_window->HideWithApp();
97 EXPECT_FALSE([other_ns_window isVisible]);
99 // HideWithApp, Show shows all windows for this app.
100 native_window->HideWithApp();
101 EXPECT_FALSE([ns_window isVisible]);
102 app_window->Show(extensions::AppWindow::SHOW_ACTIVE);
103 EXPECT_TRUE([ns_window isVisible]);
104 EXPECT_TRUE([other_ns_window isVisible]);
106 // Hide the other window.
107 other_app_window->Hide();
108 EXPECT_FALSE([other_ns_window isVisible]);
110 // HideWithApp, ShowWithApp does not show the other window.
111 native_window->HideWithApp();
112 EXPECT_FALSE([ns_window isVisible]);
113 native_window->ShowWithApp();
114 EXPECT_TRUE([ns_window isVisible]);
115 EXPECT_FALSE([other_ns_window isVisible]);
118 // Only test fullscreen for 10.7 and above.
119 // Replicate specific 10.7 SDK declarations for building with prior SDKs.
120 #if !defined(MAC_OS_X_VERSION_10_7) || \
121 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
123 NSString* const NSWindowDidEnterFullScreenNotification =
124 @"NSWindowDidEnterFullScreenNotification";
125 NSString* const NSWindowDidExitFullScreenNotification =
126 @"NSWindowDidExitFullScreenNotification";
128 #endif // MAC_OS_X_VERSION_10_7
130 @interface ScopedNotificationWatcher : NSObject {
134 - (id)initWithNotification:(NSString*)notification
135 andObject:(NSObject*)object;
136 - (void)onNotification:(NSString*)notification;
137 - (void)waitForNotification;
140 @implementation ScopedNotificationWatcher
142 - (id)initWithNotification:(NSString*)notification
143 andObject:(NSObject*)object {
144 if ((self = [super init])) {
145 [[NSNotificationCenter defaultCenter]
147 selector:@selector(onNotification:)
154 - (void)onNotification:(NSString*)notification {
156 [[NSNotificationCenter defaultCenter] removeObserver:self];
159 - (void)waitForNotification {
161 content::RunAllPendingInMessageLoop();
166 // Test that NativeAppWindow and AppWindow fullscreen state is updated when
167 // the window is fullscreened natively.
168 IN_PROC_BROWSER_TEST_F(NativeAppWindowCocoaBrowserTest, Fullscreen) {
169 if (!base::mac::IsOSLionOrLater())
172 SetUpAppWithWindows(1);
173 extensions::AppWindow* app_window = GetFirstAppWindow();
174 extensions::NativeAppWindow* window = app_window->GetBaseWindow();
175 NSWindow* ns_window = app_window->GetNativeWindow();
176 base::scoped_nsobject<ScopedNotificationWatcher> watcher;
178 EXPECT_EQ(extensions::AppWindow::FULLSCREEN_TYPE_NONE,
179 app_window->fullscreen_types_for_test());
180 EXPECT_FALSE(window->IsFullscreen());
181 EXPECT_FALSE([ns_window styleMask] & NSFullScreenWindowMask);
183 watcher.reset([[ScopedNotificationWatcher alloc]
184 initWithNotification:NSWindowDidEnterFullScreenNotification
185 andObject:ns_window]);
186 [ns_window toggleFullScreen:nil];
187 [watcher waitForNotification];
188 EXPECT_TRUE(app_window->fullscreen_types_for_test() &
189 extensions::AppWindow::FULLSCREEN_TYPE_OS);
190 EXPECT_TRUE(window->IsFullscreen());
191 EXPECT_TRUE([ns_window styleMask] & NSFullScreenWindowMask);
193 watcher.reset([[ScopedNotificationWatcher alloc]
194 initWithNotification:NSWindowDidExitFullScreenNotification
195 andObject:ns_window]);
196 app_window->Restore();
197 EXPECT_FALSE(window->IsFullscreenOrPending());
198 [watcher waitForNotification];
199 EXPECT_EQ(extensions::AppWindow::FULLSCREEN_TYPE_NONE,
200 app_window->fullscreen_types_for_test());
201 EXPECT_FALSE(window->IsFullscreen());
202 EXPECT_FALSE([ns_window styleMask] & NSFullScreenWindowMask);
204 watcher.reset([[ScopedNotificationWatcher alloc]
205 initWithNotification:NSWindowDidEnterFullScreenNotification
206 andObject:ns_window]);
207 app_window->Fullscreen();
208 EXPECT_TRUE(window->IsFullscreenOrPending());
209 [watcher waitForNotification];
210 EXPECT_TRUE(app_window->fullscreen_types_for_test() &
211 extensions::AppWindow::FULLSCREEN_TYPE_WINDOW_API);
212 EXPECT_TRUE(window->IsFullscreen());
213 EXPECT_TRUE([ns_window styleMask] & NSFullScreenWindowMask);
215 watcher.reset([[ScopedNotificationWatcher alloc]
216 initWithNotification:NSWindowDidExitFullScreenNotification
217 andObject:ns_window]);
218 [ns_window toggleFullScreen:nil];
219 [watcher waitForNotification];
220 EXPECT_EQ(extensions::AppWindow::FULLSCREEN_TYPE_NONE,
221 app_window->fullscreen_types_for_test());
222 EXPECT_FALSE(window->IsFullscreen());
223 EXPECT_FALSE([ns_window styleMask] & NSFullScreenWindowMask);