1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/app_window/native_app_window.h"
#import <Cocoa/Cocoa.h>
#import "base/mac/foundation_util.h"
#import "base/mac/mac_util.h"
#import "base/mac/scoped_cftyperef.h"
#import "base/mac/scoped_nsobject.h"
#import "base/mac/sdk_forward_declarations.h"
#include "base/macros.h"
#include "chrome/browser/apps/app_browsertest_util.h"
#include "chrome/browser/apps/app_shim/extension_app_shim_handler_mac.h"
#include "chrome/browser/apps/app_shim/test/app_shim_host_manager_test_api_mac.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/extensions/app_launch_params.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/common/constants.h"
#include "skia/ext/skia_utils_mac.h"
#include "testing/gmock/include/gmock/gmock.h"
#import "testing/gtest_mac.h"
#import "ui/base/test/nswindow_fullscreen_notification_waiter.h"
#import "ui/base/test/scoped_fake_nswindow_focus.h"
#import "ui/base/test/scoped_fake_nswindow_fullscreen.h"
#import "ui/base/test/windowed_nsnotification_observer.h"
#import "ui/gfx/mac/nswindow_frame_controls.h"
using extensions::AppWindow;
using extensions::PlatformAppBrowserTest;
using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
namespace {
// The param selects whether to use ChromeNativeAppWindowViewsMac, otherwise it
// will use NativeAppWindowCocoa.
class NativeAppWindowCocoaBrowserTest
: public testing::WithParamInterface<bool>,
public PlatformAppBrowserTest {
protected:
NativeAppWindowCocoaBrowserTest() {}
void SetUpCommandLine(base::CommandLine* command_line) override {
PlatformAppBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(
GetParam() ? switches::kEnableMacViewsNativeAppWindows
: switches::kDisableMacViewsNativeAppWindows);
}
void SetUpAppWithWindows(int num_windows) {
app_ = InstallExtension(
test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal"), 1);
EXPECT_TRUE(app_);
for (int i = 0; i < num_windows; ++i) {
content::WindowedNotificationObserver app_loaded_observer(
content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
content::NotificationService::AllSources());
OpenApplication(AppLaunchParams(
profile(), app_, extensions::LAUNCH_CONTAINER_NONE,
WindowOpenDisposition::NEW_WINDOW, extensions::SOURCE_TEST));
app_loaded_observer.Wait();
}
}
const extensions::Extension* app_;
private:
DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoaBrowserTest);
};
} // namespace
// Test interaction of Hide/Show() with Hide/ShowWithApp().
IN_PROC_BROWSER_TEST_P(NativeAppWindowCocoaBrowserTest, HideShowWithApp) {
SetUpAppWithWindows(2);
extensions::AppWindowRegistry::AppWindowList windows =
extensions::AppWindowRegistry::Get(profile())->app_windows();
AppWindow* app_window = windows.front();
extensions::NativeAppWindow* native_window = app_window->GetBaseWindow();
NSWindow* ns_window = native_window->GetNativeWindow();
AppWindow* other_app_window = windows.back();
extensions::NativeAppWindow* other_native_window =
other_app_window->GetBaseWindow();
NSWindow* other_ns_window = other_native_window->GetNativeWindow();
// Normal Hide/Show.
app_window->Hide();
EXPECT_FALSE([ns_window isVisible]);
app_window->Show(AppWindow::SHOW_ACTIVE);
EXPECT_TRUE([ns_window isVisible]);
// Normal Hide/ShowWithApp.
native_window->HideWithApp();
EXPECT_FALSE([ns_window isVisible]);
native_window->ShowWithApp();
EXPECT_TRUE([ns_window isVisible]);
// HideWithApp, Hide, ShowWithApp does not show.
native_window->HideWithApp();
app_window->Hide();
native_window->ShowWithApp();
EXPECT_FALSE([ns_window isVisible]);
// Hide, HideWithApp, ShowWithApp does not show.
native_window->HideWithApp();
native_window->ShowWithApp();
EXPECT_FALSE([ns_window isVisible]);
// Return to shown state.
app_window->Show(AppWindow::SHOW_ACTIVE);
EXPECT_TRUE([ns_window isVisible]);
// HideWithApp the other window.
EXPECT_TRUE([other_ns_window isVisible]);
other_native_window->HideWithApp();
EXPECT_FALSE([other_ns_window isVisible]);
// HideWithApp, Show shows just one window since there's no shim.
native_window->HideWithApp();
EXPECT_FALSE([ns_window isVisible]);
app_window->Show(AppWindow::SHOW_ACTIVE);
EXPECT_TRUE([ns_window isVisible]);
EXPECT_FALSE([other_ns_window isVisible]);
// Hide the other window.
other_app_window->Hide();
EXPECT_FALSE([other_ns_window isVisible]);
// HideWithApp, ShowWithApp does not show the other window.
native_window->HideWithApp();
EXPECT_FALSE([ns_window isVisible]);
native_window->ShowWithApp();
EXPECT_TRUE([ns_window isVisible]);
EXPECT_FALSE([other_ns_window isVisible]);
}
namespace {
class MockAppShimHost : public apps::AppShimHandler::Host {
public:
MockAppShimHost() {}
~MockAppShimHost() override {}
MOCK_METHOD1(OnAppLaunchComplete, void(apps::AppShimLaunchResult));
MOCK_METHOD0(OnAppClosed, void());
MOCK_METHOD0(OnAppHide, void());
MOCK_METHOD0(OnAppUnhideWithoutActivation, void());
MOCK_METHOD1(OnAppRequestUserAttention, void(apps::AppShimAttentionType));
MOCK_CONST_METHOD0(GetProfilePath, base::FilePath());
MOCK_CONST_METHOD0(GetAppId, std::string());
};
class MockExtensionAppShimHandler : public apps::ExtensionAppShimHandler {
public:
MockExtensionAppShimHandler() {
ON_CALL(*this, FindHost(_, _))
.WillByDefault(Invoke(this, &apps::ExtensionAppShimHandler::FindHost));
}
~MockExtensionAppShimHandler() override {}
MOCK_METHOD2(FindHost, AppShimHandler::Host*(Profile*, const std::string&));
};
} // namespace
// Test Hide/Show and Hide/ShowWithApp() behavior when shims are enabled.
IN_PROC_BROWSER_TEST_P(NativeAppWindowCocoaBrowserTest,
HideShowWithAppWithShim) {
test::AppShimHostManagerTestApi test_api(
g_browser_process->platform_part()->app_shim_host_manager());
MockExtensionAppShimHandler* mock = new MockExtensionAppShimHandler();
test_api.SetExtensionAppShimHandler(
std::unique_ptr<apps::ExtensionAppShimHandler>(
mock)); // Takes ownership.
MockAppShimHost mock_host;
SetUpAppWithWindows(1);
extensions::AppWindowRegistry::AppWindowList windows =
extensions::AppWindowRegistry::Get(profile())->app_windows();
extensions::AppWindow* app_window = windows.front();
extensions::NativeAppWindow* native_window = app_window->GetBaseWindow();
NSWindow* ns_window = native_window->GetNativeWindow();
// HideWithApp.
native_window->HideWithApp();
EXPECT_FALSE([ns_window isVisible]);
// Show notifies the shim to unhide.
EXPECT_CALL(mock_host, OnAppUnhideWithoutActivation());
EXPECT_CALL(*mock, FindHost(_, _)).WillOnce(Return(&mock_host));
app_window->Show(extensions::AppWindow::SHOW_ACTIVE);
EXPECT_TRUE([ns_window isVisible]);
testing::Mock::VerifyAndClearExpectations(mock);
testing::Mock::VerifyAndClearExpectations(&mock_host);
// HideWithApp
native_window->HideWithApp();
EXPECT_FALSE([ns_window isVisible]);
// Activate does the same.
EXPECT_CALL(mock_host, OnAppUnhideWithoutActivation());
EXPECT_CALL(*mock, FindHost(_, _)).WillOnce(Return(&mock_host));
native_window->Activate();
EXPECT_TRUE([ns_window isVisible]);
testing::Mock::VerifyAndClearExpectations(mock);
testing::Mock::VerifyAndClearExpectations(&mock_host);
}
// Test that NativeAppWindow and AppWindow fullscreen state is updated when
// the window is fullscreened natively.
IN_PROC_BROWSER_TEST_P(NativeAppWindowCocoaBrowserTest, Fullscreen) {
ui::test::ScopedFakeNSWindowFullscreen fake_fullscreen;
extensions::AppWindow* app_window =
CreateTestAppWindow("{\"alwaysOnTop\": true }");
extensions::NativeAppWindow* window = app_window->GetBaseWindow();
NSWindow* ns_window = app_window->GetNativeWindow();
base::scoped_nsobject<NSWindowFullscreenNotificationWaiter> waiter(
[[NSWindowFullscreenNotificationWaiter alloc] initWithWindow:ns_window]);
EXPECT_EQ(AppWindow::FULLSCREEN_TYPE_NONE,
app_window->fullscreen_types_for_test());
EXPECT_FALSE(window->IsFullscreen());
EXPECT_FALSE([ns_window styleMask] & NSFullScreenWindowMask);
EXPECT_TRUE(gfx::IsNSWindowAlwaysOnTop(ns_window));
[ns_window toggleFullScreen:nil];
[waiter waitForEnterCount:1 exitCount:0];
EXPECT_TRUE(app_window->fullscreen_types_for_test() &
AppWindow::FULLSCREEN_TYPE_OS);
EXPECT_TRUE(window->IsFullscreen());
EXPECT_TRUE([ns_window styleMask] & NSFullScreenWindowMask);
EXPECT_FALSE(gfx::IsNSWindowAlwaysOnTop(ns_window));
app_window->Restore();
EXPECT_FALSE(window->IsFullscreenOrPending());
[waiter waitForEnterCount:1 exitCount:1];
EXPECT_EQ(AppWindow::FULLSCREEN_TYPE_NONE,
app_window->fullscreen_types_for_test());
EXPECT_FALSE(window->IsFullscreen());
EXPECT_FALSE([ns_window styleMask] & NSFullScreenWindowMask);
EXPECT_TRUE(gfx::IsNSWindowAlwaysOnTop(ns_window));
app_window->Fullscreen();
EXPECT_TRUE(window->IsFullscreenOrPending());
[waiter waitForEnterCount:2 exitCount:1];
EXPECT_TRUE(app_window->fullscreen_types_for_test() &
AppWindow::FULLSCREEN_TYPE_WINDOW_API);
EXPECT_TRUE(window->IsFullscreen());
EXPECT_TRUE([ns_window styleMask] & NSFullScreenWindowMask);
EXPECT_FALSE(gfx::IsNSWindowAlwaysOnTop(ns_window));
[ns_window toggleFullScreen:nil];
[waiter waitForEnterCount:2 exitCount:2];
EXPECT_EQ(AppWindow::FULLSCREEN_TYPE_NONE,
app_window->fullscreen_types_for_test());
EXPECT_FALSE(window->IsFullscreen());
EXPECT_FALSE([ns_window styleMask] & NSFullScreenWindowMask);
EXPECT_TRUE(gfx::IsNSWindowAlwaysOnTop(ns_window));
}
// Test Minimize, Restore combinations with their native equivalents.
IN_PROC_BROWSER_TEST_P(NativeAppWindowCocoaBrowserTest, Minimize) {
if (base::mac::IsOS10_10())
return; // Fails when swarmed. http://crbug.com/660582
SetUpAppWithWindows(1);
AppWindow* app_window = GetFirstAppWindow();
extensions::NativeAppWindow* window = app_window->GetBaseWindow();
NSWindow* ns_window = app_window->GetNativeWindow();
NSRect initial_frame = [ns_window frame];
EXPECT_FALSE(window->IsMinimized());
EXPECT_FALSE([ns_window isMiniaturized]);
// Native minimize, Restore.
[ns_window miniaturize:nil];
EXPECT_NSEQ(initial_frame, [ns_window frame]);
EXPECT_TRUE(window->IsMinimized());
EXPECT_TRUE([ns_window isMiniaturized]);
app_window->Restore();
EXPECT_NSEQ(initial_frame, [ns_window frame]);
EXPECT_FALSE(window->IsMinimized());
EXPECT_FALSE([ns_window isMiniaturized]);
// Minimize, native restore.
app_window->Minimize();
EXPECT_NSEQ(initial_frame, [ns_window frame]);
EXPECT_TRUE(window->IsMinimized());
EXPECT_TRUE([ns_window isMiniaturized]);
[ns_window deminiaturize:nil];
EXPECT_NSEQ(initial_frame, [ns_window frame]);
EXPECT_FALSE(window->IsMinimized());
EXPECT_FALSE([ns_window isMiniaturized]);
}
// Test Maximize, Restore combinations with their native equivalents.
IN_PROC_BROWSER_TEST_P(NativeAppWindowCocoaBrowserTest, Maximize) {
SetUpAppWithWindows(1);
AppWindow* app_window = GetFirstAppWindow();
extensions::NativeAppWindow* window = app_window->GetBaseWindow();
NSWindow* ns_window = app_window->GetNativeWindow();
base::scoped_nsobject<WindowedNSNotificationObserver> watcher;
gfx::Rect initial_restored_bounds = window->GetRestoredBounds();
NSRect initial_frame = [ns_window frame];
NSRect maximized_frame = [[ns_window screen] visibleFrame];
EXPECT_FALSE(window->IsMaximized());
// Native maximize, Restore.
watcher.reset([[WindowedNSNotificationObserver alloc]
initForNotification:NSWindowDidResizeNotification
object:ns_window]);
[ns_window zoom:nil];
[watcher wait];
EXPECT_EQ(initial_restored_bounds, window->GetRestoredBounds());
EXPECT_NSEQ(maximized_frame, [ns_window frame]);
EXPECT_TRUE(window->IsMaximized());
watcher.reset([[WindowedNSNotificationObserver alloc]
initForNotification:NSWindowDidResizeNotification
object:ns_window]);
app_window->Restore();
[watcher wait];
EXPECT_EQ(initial_restored_bounds, window->GetRestoredBounds());
EXPECT_NSEQ(initial_frame, [ns_window frame]);
EXPECT_FALSE(window->IsMaximized());
// Maximize, native restore.
watcher.reset([[WindowedNSNotificationObserver alloc]
initForNotification:NSWindowDidResizeNotification
object:ns_window]);
app_window->Maximize();
[watcher wait];
EXPECT_EQ(initial_restored_bounds, window->GetRestoredBounds());
EXPECT_NSEQ(maximized_frame, [ns_window frame]);
EXPECT_TRUE(window->IsMaximized());
watcher.reset([[WindowedNSNotificationObserver alloc]
initForNotification:NSWindowDidResizeNotification
object:ns_window]);
[ns_window zoom:nil];
[watcher wait];
EXPECT_EQ(initial_restored_bounds, window->GetRestoredBounds());
EXPECT_NSEQ(initial_frame, [ns_window frame]);
EXPECT_FALSE(window->IsMaximized());
}
// Test Maximize when the window has a maximum size. The maximum size means that
// the window is not user-maximizable. However, calling Maximize() via the
// javascript API should still maximize and since the zoom button is removed,
// the codepath changes.
IN_PROC_BROWSER_TEST_P(NativeAppWindowCocoaBrowserTest, MaximizeConstrained) {
AppWindow* app_window = CreateTestAppWindow(
"{\"outerBounds\": {\"maxWidth\":200, \"maxHeight\":300}}");
extensions::NativeAppWindow* window = app_window->GetBaseWindow();
NSWindow* ns_window = app_window->GetNativeWindow();
base::scoped_nsobject<WindowedNSNotificationObserver> watcher;
gfx::Rect initial_restored_bounds = window->GetRestoredBounds();
NSRect initial_frame = [ns_window frame];
NSRect maximized_frame = [[ns_window screen] visibleFrame];
EXPECT_FALSE(window->IsMaximized());
// Maximize, Restore.
watcher.reset([[WindowedNSNotificationObserver alloc]
initForNotification:NSWindowDidResizeNotification
object:ns_window]);
app_window->Maximize();
[watcher wait];
EXPECT_EQ(initial_restored_bounds, window->GetRestoredBounds());
EXPECT_NSEQ(maximized_frame, [ns_window frame]);
EXPECT_TRUE(window->IsMaximized());
watcher.reset([[WindowedNSNotificationObserver alloc]
initForNotification:NSWindowDidResizeNotification
object:ns_window]);
app_window->Restore();
[watcher wait];
EXPECT_EQ(initial_restored_bounds, window->GetRestoredBounds());
EXPECT_NSEQ(initial_frame, [ns_window frame]);
EXPECT_FALSE(window->IsMaximized());
}
// Test Minimize, Maximize, Restore combinations with their native equivalents.
IN_PROC_BROWSER_TEST_P(NativeAppWindowCocoaBrowserTest, MinimizeMaximize) {
if (base::mac::IsOS10_10())
return; // Fails when swarmed. http://crbug.com/660582
SetUpAppWithWindows(1);
AppWindow* app_window = GetFirstAppWindow();
extensions::NativeAppWindow* window = app_window->GetBaseWindow();
NSWindow* ns_window = app_window->GetNativeWindow();
base::scoped_nsobject<WindowedNSNotificationObserver> watcher;
NSRect initial_frame = [ns_window frame];
NSRect maximized_frame = [[ns_window screen] visibleFrame];
EXPECT_FALSE(window->IsMaximized());
EXPECT_FALSE(window->IsMinimized());
EXPECT_FALSE([ns_window isMiniaturized]);
// Maximize, Minimize, Restore.
watcher.reset([[WindowedNSNotificationObserver alloc]
initForNotification:NSWindowDidResizeNotification
object:ns_window]);
app_window->Maximize();
[watcher wait];
EXPECT_NSEQ(maximized_frame, [ns_window frame]);
EXPECT_TRUE(window->IsMaximized());
app_window->Minimize();
EXPECT_NSEQ(maximized_frame, [ns_window frame]);
EXPECT_FALSE(window->IsMaximized());
EXPECT_TRUE(window->IsMinimized());
EXPECT_TRUE([ns_window isMiniaturized]);
app_window->Restore();
EXPECT_NSEQ(initial_frame, [ns_window frame]);
EXPECT_FALSE(window->IsMaximized());
EXPECT_FALSE(window->IsMinimized());
EXPECT_FALSE([ns_window isMiniaturized]);
// Minimize, Maximize.
app_window->Minimize();
EXPECT_NSEQ(initial_frame, [ns_window frame]);
EXPECT_TRUE(window->IsMinimized());
EXPECT_TRUE([ns_window isMiniaturized]);
app_window->Maximize();
EXPECT_TRUE([ns_window isVisible]);
EXPECT_NSEQ(maximized_frame, [ns_window frame]);
EXPECT_TRUE(window->IsMaximized());
EXPECT_FALSE(window->IsMinimized());
EXPECT_FALSE([ns_window isMiniaturized]);
}
// Test Maximize, Fullscreen, Restore combinations.
IN_PROC_BROWSER_TEST_P(NativeAppWindowCocoaBrowserTest, MaximizeFullscreen) {
ui::test::ScopedFakeNSWindowFullscreen fake_fullscreen;
SetUpAppWithWindows(1);
AppWindow* app_window = GetFirstAppWindow();
extensions::NativeAppWindow* window = app_window->GetBaseWindow();
NSWindow* ns_window = app_window->GetNativeWindow();
base::scoped_nsobject<WindowedNSNotificationObserver> watcher;
base::scoped_nsobject<NSWindowFullscreenNotificationWaiter> waiter(
[[NSWindowFullscreenNotificationWaiter alloc] initWithWindow:ns_window]);
NSRect initial_frame = [ns_window frame];
NSRect maximized_frame = [[ns_window screen] visibleFrame];
EXPECT_FALSE(window->IsMaximized());
EXPECT_FALSE(window->IsFullscreen());
// Maximize, Fullscreen, Restore, Restore.
watcher.reset([[WindowedNSNotificationObserver alloc]
initForNotification:NSWindowDidResizeNotification
object:ns_window]);
app_window->Maximize();
[watcher wait];
EXPECT_NSEQ(maximized_frame, [ns_window frame]);
EXPECT_TRUE(window->IsMaximized());
EXPECT_EQ(0, [waiter enterCount]);
app_window->Fullscreen();
[waiter waitForEnterCount:1 exitCount:0];
EXPECT_FALSE(window->IsMaximized());
EXPECT_TRUE(window->IsFullscreen());
app_window->Restore();
[waiter waitForEnterCount:1 exitCount:1];
EXPECT_NSEQ(maximized_frame, [ns_window frame]);
EXPECT_TRUE(window->IsMaximized());
EXPECT_FALSE(window->IsFullscreen());
app_window->Restore();
EXPECT_NSEQ(initial_frame, [ns_window frame]);
EXPECT_FALSE(window->IsMaximized());
// Fullscreen, Maximize, Restore.
app_window->Fullscreen();
[waiter waitForEnterCount:2 exitCount:1];
EXPECT_FALSE(window->IsMaximized());
EXPECT_TRUE(window->IsFullscreen());
app_window->Maximize();
EXPECT_FALSE(window->IsMaximized());
EXPECT_TRUE(window->IsFullscreen());
app_window->Restore();
[waiter waitForEnterCount:2 exitCount:2];
EXPECT_NSEQ(initial_frame, [ns_window frame]);
EXPECT_FALSE(window->IsMaximized());
EXPECT_FALSE(window->IsFullscreen());
}
// Test that, in frameless windows, the web contents has the same size as the
// window.
IN_PROC_BROWSER_TEST_P(NativeAppWindowCocoaBrowserTest, Frameless) {
AppWindow* app_window = CreateTestAppWindow("{\"frame\": \"none\"}");
NSWindow* ns_window = app_window->GetNativeWindow();
NSView* web_contents = app_window->web_contents()->GetNativeView();
EXPECT_TRUE(NSEqualSizes(NSMakeSize(512, 384), [web_contents frame].size));
// Move and resize the window.
NSRect new_frame = NSMakeRect(50, 50, 200, 200);
[ns_window setFrame:new_frame display:YES];
EXPECT_TRUE(NSEqualSizes(new_frame.size, [web_contents frame].size));
// Windows created with NSBorderlessWindowMask by default don't have shadow,
// but packaged apps should always have one.
EXPECT_TRUE([ns_window hasShadow]);
// Since the window has no constraints, it should have all of the following
// style mask bits.
NSUInteger style_mask = NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask | NSResizableWindowMask |
NSTexturedBackgroundWindowMask;
EXPECT_EQ(style_mask, [ns_window styleMask]);
CloseAppWindow(app_window);
}
namespace {
// Test that resize and fullscreen controls are correctly enabled/disabled.
void TestControls(AppWindow* app_window) {
NSWindow* ns_window = app_window->GetNativeWindow();
// The window is resizable.
EXPECT_TRUE([ns_window styleMask] & NSResizableWindowMask);
// Due to this bug: http://crbug.com/362039, which manifests on the Cocoa
// implementation but not the views one, frameless windows should have
// fullscreen controls disabled.
BOOL can_fullscreen =
![NSStringFromClass([ns_window class]) isEqualTo:@"AppFramelessNSWindow"];
// The window can fullscreen and maximize.
EXPECT_EQ(can_fullscreen, !!([ns_window collectionBehavior] &
NSWindowCollectionBehaviorFullScreenPrimary));
// In OSX 10.10+, the zoom button performs the zoom action rather than the
// fullscreen action. The above check that collectionBehavior does not include
// NSWindowCollectionBehaviorFullScreenPrimary is sufficient to determine that
// the window can't be fullscreened.
if (base::mac::IsOS10_9()) {
EXPECT_EQ(can_fullscreen,
[[ns_window standardWindowButton:NSWindowZoomButton] isEnabled]);
}
// Set a maximum size.
app_window->SetContentSizeConstraints(gfx::Size(), gfx::Size(200, 201));
EXPECT_EQ(200, [ns_window contentMaxSize].width);
EXPECT_EQ(201, [ns_window contentMaxSize].height);
NSView* web_contents = app_window->web_contents()->GetNativeView();
EXPECT_EQ(200, [web_contents frame].size.width);
EXPECT_EQ(201, [web_contents frame].size.height);
// Still resizable.
EXPECT_TRUE([ns_window styleMask] & NSResizableWindowMask);
// Fullscreen and maximize are disabled.
EXPECT_FALSE([ns_window collectionBehavior] &
NSWindowCollectionBehaviorFullScreenPrimary);
EXPECT_FALSE([[ns_window standardWindowButton:NSWindowZoomButton] isEnabled]);
// Set a minimum size equal to the maximum size.
app_window->SetContentSizeConstraints(gfx::Size(200, 201),
gfx::Size(200, 201));
EXPECT_EQ(200, [ns_window contentMinSize].width);
EXPECT_EQ(201, [ns_window contentMinSize].height);
// No longer resizable.
EXPECT_FALSE([ns_window styleMask] & NSResizableWindowMask);
// If a window is made fullscreen by the API, fullscreen should be enabled so
// the user can exit fullscreen.
ui::test::ScopedFakeNSWindowFullscreen fake_fullscreen;
base::scoped_nsobject<NSWindowFullscreenNotificationWaiter> waiter(
[[NSWindowFullscreenNotificationWaiter alloc] initWithWindow:ns_window]);
app_window->SetFullscreen(AppWindow::FULLSCREEN_TYPE_WINDOW_API, true);
[waiter waitForEnterCount:1 exitCount:0];
EXPECT_TRUE([ns_window collectionBehavior] &
NSWindowCollectionBehaviorFullScreenPrimary);
EXPECT_EQ(NSWidth([[ns_window contentView] frame]),
NSWidth([ns_window frame]));
// Once it leaves fullscreen, it is disabled again.
app_window->SetFullscreen(AppWindow::FULLSCREEN_TYPE_WINDOW_API, false);
[waiter waitForEnterCount:1 exitCount:1];
EXPECT_FALSE([ns_window collectionBehavior] &
NSWindowCollectionBehaviorFullScreenPrimary);
}
} // namespace
IN_PROC_BROWSER_TEST_P(NativeAppWindowCocoaBrowserTest, Controls) {
TestControls(CreateTestAppWindow("{}"));
}
IN_PROC_BROWSER_TEST_P(NativeAppWindowCocoaBrowserTest, ControlsFrameless) {
TestControls(CreateTestAppWindow("{\"frame\": \"none\"}"));
}
namespace {
// Convert a color constant to an NSColor that can be compared with |bitmap|.
NSColor* ColorInBitmapColorSpace(SkColor color, NSBitmapImageRep* bitmap) {
return [skia::SkColorToSRGBNSColor(color)
colorUsingColorSpace:[bitmap colorSpace]];
}
// Take a screenshot of the window, including its native frame.
NSBitmapImageRep* ScreenshotNSWindow(NSWindow* window) {
// When building with 10.10 SDK and running on 10.9, -[NSView
// cacheDisplayInRect] does not seem to capture subviews. This seems related
// to the frame view having a layer with 10.10 SDK, but is probably a bug
// since it doesn't manifest on 10.10. See http://crbug.com/508722.
// In this case, take a screenshot using the CGWindowList API instead. The
// bitmap is now in the display's color space, so expected colors need to be
// converted.
// TODO(jackhou): Update this if it is fixed in AppKit, or if other
// platform/SDK combinations need it.
// NOTE: This doesn't work with Views, but the regular test does, so use that.
bool mac_views = base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableMacViewsNativeAppWindows);
if (base::mac::IsOS10_9() && !mac_views) {
// -[NSView setNeedsDisplay:YES] doesn't synchronously display the view, it
// gets drawn by another event in the queue, so let that run first.
content::RunAllPendingInMessageLoop();
base::ScopedCFTypeRef<CGImageRef> cg_image(CGWindowListCreateImage(
CGRectNull, kCGWindowListOptionIncludingWindow, [window windowNumber],
kCGWindowImageBoundsIgnoreFraming));
return [[[NSBitmapImageRep alloc] initWithCGImage:cg_image] autorelease];
}
NSView* frame_view = [[window contentView] superview];
NSRect bounds = [frame_view bounds];
NSBitmapImageRep* bitmap =
[frame_view bitmapImageRepForCachingDisplayInRect:bounds];
[frame_view cacheDisplayInRect:bounds toBitmapImageRep:bitmap];
return bitmap;
}
} // namespace
// Test that the colored frames have the correct color when active and inactive.
IN_PROC_BROWSER_TEST_P(NativeAppWindowCocoaBrowserTest, FrameColor) {
// The hex values indicate an RGB color. When we get the NSColor later, the
// components are CGFloats in the range [0, 1].
extensions::AppWindow* app_window = CreateTestAppWindow(
"{\"frame\": {\"color\": \"#FF0000\", \"inactiveColor\": \"#0000FF\"}}");
NSWindow* ns_window = app_window->GetNativeWindow();
// No color correction in the default case.
[ns_window setColorSpace:[NSColorSpace sRGBColorSpace]];
int half_width = NSWidth([ns_window frame]) / 2;
NSBitmapImageRep* bitmap = ScreenshotNSWindow(ns_window);
// The window is currently inactive so it should be blue (#0000FF).
NSColor* expected_color = ColorInBitmapColorSpace(0xFF0000FF, bitmap);
NSColor* color = [bitmap colorAtX:half_width y:5];
CGFloat expected_components[4], color_components[4];
[expected_color getComponents:expected_components];
[color getComponents:color_components];
EXPECT_NEAR(expected_components[0], color_components[0], 0.01);
EXPECT_NEAR(expected_components[1], color_components[1], 0.01);
EXPECT_NEAR(expected_components[2], color_components[2], 0.01);
ui::test::ScopedFakeNSWindowFocus fake_focus;
[ns_window makeMainWindow];
bitmap = ScreenshotNSWindow(ns_window);
// The window is now active so it should be red (#FF0000).
expected_color = ColorInBitmapColorSpace(0xFFFF0000, bitmap);
color = [bitmap colorAtX:half_width y:5];
[expected_color getComponents:expected_components];
[color getComponents:color_components];
EXPECT_NEAR(expected_components[0], color_components[0], 0.01);
EXPECT_NEAR(expected_components[1], color_components[1], 0.01);
EXPECT_NEAR(expected_components[2], color_components[2], 0.01);
}
INSTANTIATE_TEST_CASE_P(NativeAppWindowCocoaBrowserTestInstance,
NativeAppWindowCocoaBrowserTest,
::testing::Bool());
|