Commit 3c7390c2 authored by Elly Fong-Jones's avatar Elly Fong-Jones Committed by Commit Bot

views: fix ShowInactive tests on 10.15

As of 10.15, the system seems to try to activate every app shortly
after the app starts up. This causes windows the app creates early
in its lifecycle to be given key status by appkit, even if they were
created without being given key status. In order to avoid that, tests
that depend on this behavior need to wait for the system activation
to happen before creating their windows. This change adds a mechanism
for those tests to do so, and adds appropriate uses of it.

Bug: 988868
Change-Id: I219c926a8f716ad938e0f5279c04b9f94dba1c24
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1853107Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#705600}
parent f66b268f
...@@ -112,6 +112,12 @@ class WidgetTest : public ViewsTestBase { ...@@ -112,6 +112,12 @@ class WidgetTest : public ViewsTestBase {
// Returns the set of all Widgets that currently have a NativeWindow. // Returns the set of all Widgets that currently have a NativeWindow.
static Widget::Widgets GetAllWidgets(); static Widget::Widgets GetAllWidgets();
// Waits for system app activation events, if any, to have happened. This is
// necessary on macOS 10.15+, where the system will attempt to find and
// activate a window owned by the app shortly after app startup, if there is
// one. See https://crbug.com/998868 for details.
static void WaitForSystemAppActivation();
private: private:
DISALLOW_COPY_AND_ASSIGN(WidgetTest); DISALLOW_COPY_AND_ASSIGN(WidgetTest);
}; };
......
...@@ -175,5 +175,8 @@ Widget::Widgets WidgetTest::GetAllWidgets() { ...@@ -175,5 +175,8 @@ Widget::Widgets WidgetTest::GetAllWidgets() {
return all_widgets; return all_widgets;
} }
// static
void WidgetTest::WaitForSystemAppActivation() {}
} // namespace test } // namespace test
} // namespace views } // namespace views
...@@ -6,10 +6,12 @@ ...@@ -6,10 +6,12 @@
#include <Cocoa/Cocoa.h> #include <Cocoa/Cocoa.h>
#include "base/mac/mac_util.h"
#import "base/mac/scoped_nsobject.h" #import "base/mac/scoped_nsobject.h"
#import "base/mac/scoped_objc_class_swizzler.h" #import "base/mac/scoped_objc_class_swizzler.h"
#include "base/macros.h" #include "base/macros.h"
#import "components/remote_cocoa/app_shim/native_widget_ns_window_bridge.h" #import "components/remote_cocoa/app_shim/native_widget_ns_window_bridge.h"
#import "ui/base/test/windowed_nsnotification_observer.h"
#include "ui/views/cocoa/native_widget_mac_ns_window_host.h" #include "ui/views/cocoa/native_widget_mac_ns_window_host.h"
#include "ui/views/widget/native_widget_mac.h" #include "ui/views/widget/native_widget_mac.h"
#include "ui/views/widget/root_view.h" #include "ui/views/widget/root_view.h"
...@@ -107,5 +109,22 @@ Widget::Widgets WidgetTest::GetAllWidgets() { ...@@ -107,5 +109,22 @@ Widget::Widgets WidgetTest::GetAllWidgets() {
return all_widgets; return all_widgets;
} }
// static
void WidgetTest::WaitForSystemAppActivation() {
if (base::mac::IsAtMostOS10_14())
return;
// This seems to be only necessary on 10.15+ but it's obscure why. Shortly
// after launching an app, the system sends ApplicationDidFinishLaunching
// (which is normal), which causes AppKit on 10.15 to try to find a window to
// activate. If it finds one it will makeKeyAndOrderFront: it, which breaks
// tests that are deliberately creating inactive windows.
base::scoped_nsobject<WindowedNSNotificationObserver> observer(
[[WindowedNSNotificationObserver alloc]
initForNotification:NSApplicationDidFinishLaunchingNotification
object:NSApp]);
[observer wait];
}
} // namespace test } // namespace test
} // namespace views } // namespace views
...@@ -116,6 +116,7 @@ TEST_P(NativeWidgetMacInteractiveUITest, ShowAttainsKeyStatus) { ...@@ -116,6 +116,7 @@ TEST_P(NativeWidgetMacInteractiveUITest, ShowAttainsKeyStatus) {
// Test that ShowInactive does not take keyWindow status. // Test that ShowInactive does not take keyWindow status.
TEST_P(NativeWidgetMacInteractiveUITest, ShowInactiveIgnoresKeyStatus) { TEST_P(NativeWidgetMacInteractiveUITest, ShowInactiveIgnoresKeyStatus) {
WidgetTest::WaitForSystemAppActivation();
Widget* widget = MakeWidget(); Widget* widget = MakeWidget();
NSWindow* widget_window = widget->GetNativeWindow().GetNativeNSWindow(); NSWindow* widget_window = widget->GetNativeWindow().GetNativeNSWindow();
......
...@@ -1260,6 +1260,7 @@ TEST_F(WidgetTestInteractive, ShowCreatesActiveWindow) { ...@@ -1260,6 +1260,7 @@ TEST_F(WidgetTestInteractive, ShowCreatesActiveWindow) {
} }
TEST_F(WidgetTestInteractive, ShowInactive) { TEST_F(WidgetTestInteractive, ShowInactive) {
WidgetTest::WaitForSystemAppActivation();
Widget* widget = CreateTopLevelPlatformWidget(); Widget* widget = CreateTopLevelPlatformWidget();
ShowInactiveSync(widget); ShowInactiveSync(widget);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment