[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / apps / native_app_window_cocoa_browsertest.mm
blobcc9d793999e93844c5b7e4651f1ac6e1d29b8b3c
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 "apps/app_window_registry.h"
10 #include "base/mac/mac_util.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"
17 using extensions::PlatformAppBrowserTest;
19 namespace {
21 class NativeAppWindowCocoaBrowserTest : public PlatformAppBrowserTest {
22  protected:
23   NativeAppWindowCocoaBrowserTest() {}
25   void SetUpAppWithWindows(int num_windows) {
26     app_ = InstallExtension(
27         test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal"), 1);
28     EXPECT_TRUE(app_);
30     for (int i = 0; i < num_windows; ++i) {
31       content::WindowedNotificationObserver app_loaded_observer(
32           content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
33           content::NotificationService::AllSources());
34       OpenApplication(
35           AppLaunchParams(profile(),
36                           app_,
37                           extensions::LAUNCH_CONTAINER_NONE,
38                           NEW_WINDOW));
39       app_loaded_observer.Wait();
40     }
41   }
43   const extensions::Extension* app_;
45  private:
46   DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoaBrowserTest);
49 }  // namespace
51 // Test interaction of Hide/Show() with Hide/ShowWithApp().
52 IN_PROC_BROWSER_TEST_F(NativeAppWindowCocoaBrowserTest, HideShowWithApp) {
53   SetUpAppWithWindows(2);
54   apps::AppWindowRegistry::AppWindowList windows =
55       apps::AppWindowRegistry::Get(profile())->app_windows();
57   apps::AppWindow* app_window = windows.front();
58   apps::NativeAppWindow* native_window = app_window->GetBaseWindow();
59   NSWindow* ns_window = native_window->GetNativeWindow();
61   apps::AppWindow* other_app_window = windows.back();
62   apps::NativeAppWindow* other_native_window =
63       other_app_window->GetBaseWindow();
64   NSWindow* other_ns_window = other_native_window->GetNativeWindow();
66   // Normal Hide/Show.
67   app_window->Hide();
68   EXPECT_FALSE([ns_window isVisible]);
69   app_window->Show(apps::AppWindow::SHOW_ACTIVE);
70   EXPECT_TRUE([ns_window isVisible]);
72   // Normal Hide/ShowWithApp.
73   native_window->HideWithApp();
74   EXPECT_FALSE([ns_window isVisible]);
75   native_window->ShowWithApp();
76   EXPECT_TRUE([ns_window isVisible]);
78   // HideWithApp, Hide, ShowWithApp does not show.
79   native_window->HideWithApp();
80   app_window->Hide();
81   native_window->ShowWithApp();
82   EXPECT_FALSE([ns_window isVisible]);
84   // Hide, HideWithApp, ShowWithApp does not show.
85   native_window->HideWithApp();
86   native_window->ShowWithApp();
87   EXPECT_FALSE([ns_window isVisible]);
89   // Return to shown state.
90   app_window->Show(apps::AppWindow::SHOW_ACTIVE);
91   EXPECT_TRUE([ns_window isVisible]);
93   // HideWithApp the other window.
94   EXPECT_TRUE([other_ns_window isVisible]);
95   other_native_window->HideWithApp();
96   EXPECT_FALSE([other_ns_window isVisible]);
98   // HideWithApp, Show shows all windows for this app.
99   native_window->HideWithApp();
100   EXPECT_FALSE([ns_window isVisible]);
101   app_window->Show(apps::AppWindow::SHOW_ACTIVE);
102   EXPECT_TRUE([ns_window isVisible]);
103   EXPECT_TRUE([other_ns_window isVisible]);
105   // Hide the other window.
106   other_app_window->Hide();
107   EXPECT_FALSE([other_ns_window isVisible]);
109   // HideWithApp, ShowWithApp does not show the other window.
110   native_window->HideWithApp();
111   EXPECT_FALSE([ns_window isVisible]);
112   native_window->ShowWithApp();
113   EXPECT_TRUE([ns_window isVisible]);
114   EXPECT_FALSE([other_ns_window isVisible]);
117 // Only test fullscreen for 10.7 and above.
118 // Replicate specific 10.7 SDK declarations for building with prior SDKs.
119 #if !defined(MAC_OS_X_VERSION_10_7) || \
120     MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
122 @interface NSWindow (LionSDKDeclarations)
123 - (void)toggleFullScreen:(id)sender;
124 @end
126 enum {
127   NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7,
128   NSFullScreenWindowMask = 1 << 14
131 NSString* const NSWindowDidEnterFullScreenNotification =
132     @"NSWindowDidEnterFullScreenNotification";
133 NSString* const NSWindowDidExitFullScreenNotification =
134     @"NSWindowDidExitFullScreenNotification";
136 #endif  // MAC_OS_X_VERSION_10_7
138 @interface ScopedNotificationWatcher : NSObject {
139  @private
140   BOOL received_;
142 - (id)initWithNotification:(NSString*)notification
143                  andObject:(NSObject*)object;
144 - (void)onNotification:(NSString*)notification;
145 - (void)waitForNotification;
146 @end
148 @implementation ScopedNotificationWatcher
150 - (id)initWithNotification:(NSString*)notification
151                  andObject:(NSObject*)object {
152   if ((self = [super init])) {
153     [[NSNotificationCenter defaultCenter]
154         addObserver:self
155            selector:@selector(onNotification:)
156                name:notification
157              object:object];
158   }
159   return self;
162 - (void)onNotification:(NSString*)notification {
163   received_ = YES;
164   [[NSNotificationCenter defaultCenter] removeObserver:self];
167 - (void)waitForNotification {
168   while (!received_)
169     content::RunAllPendingInMessageLoop();
172 @end
174 // Test that NativeAppWindow and AppWindow fullscreen state is updated when
175 // the window is fullscreened natively.
176 IN_PROC_BROWSER_TEST_F(NativeAppWindowCocoaBrowserTest, Fullscreen) {
177   if (!base::mac::IsOSLionOrLater())
178     return;
180   SetUpAppWithWindows(1);
181   apps::AppWindow* app_window = GetFirstAppWindow();
182   apps::NativeAppWindow* window = app_window->GetBaseWindow();
183   NSWindow* ns_window = app_window->GetNativeWindow();
184   base::scoped_nsobject<ScopedNotificationWatcher> watcher;
186   EXPECT_EQ(apps::AppWindow::FULLSCREEN_TYPE_NONE,
187             app_window->fullscreen_types_for_test());
188   EXPECT_FALSE(window->IsFullscreen());
189   EXPECT_FALSE([ns_window styleMask] & NSFullScreenWindowMask);
191   watcher.reset([[ScopedNotificationWatcher alloc]
192       initWithNotification:NSWindowDidEnterFullScreenNotification
193                  andObject:ns_window]);
194   [ns_window toggleFullScreen:nil];
195   [watcher waitForNotification];
196   EXPECT_TRUE(app_window->fullscreen_types_for_test() &
197       apps::AppWindow::FULLSCREEN_TYPE_OS);
198   EXPECT_TRUE(window->IsFullscreen());
199   EXPECT_TRUE([ns_window styleMask] & NSFullScreenWindowMask);
201   watcher.reset([[ScopedNotificationWatcher alloc]
202       initWithNotification:NSWindowDidExitFullScreenNotification
203                  andObject:ns_window]);
204   app_window->Restore();
205   EXPECT_FALSE(window->IsFullscreenOrPending());
206   [watcher waitForNotification];
207   EXPECT_EQ(apps::AppWindow::FULLSCREEN_TYPE_NONE,
208             app_window->fullscreen_types_for_test());
209   EXPECT_FALSE(window->IsFullscreen());
210   EXPECT_FALSE([ns_window styleMask] & NSFullScreenWindowMask);
212   watcher.reset([[ScopedNotificationWatcher alloc]
213       initWithNotification:NSWindowDidEnterFullScreenNotification
214                  andObject:ns_window]);
215   app_window->Fullscreen();
216   EXPECT_TRUE(window->IsFullscreenOrPending());
217   [watcher waitForNotification];
218   EXPECT_TRUE(app_window->fullscreen_types_for_test() &
219       apps::AppWindow::FULLSCREEN_TYPE_WINDOW_API);
220   EXPECT_TRUE(window->IsFullscreen());
221   EXPECT_TRUE([ns_window styleMask] & NSFullScreenWindowMask);
223   watcher.reset([[ScopedNotificationWatcher alloc]
224       initWithNotification:NSWindowDidExitFullScreenNotification
225                  andObject:ns_window]);
226   [ns_window toggleFullScreen:nil];
227   [watcher waitForNotification];
228   EXPECT_EQ(apps::AppWindow::FULLSCREEN_TYPE_NONE,
229             app_window->fullscreen_types_for_test());
230   EXPECT_FALSE(window->IsFullscreen());
231   EXPECT_FALSE([ns_window styleMask] & NSFullScreenWindowMask);