1 // Copyright 2014 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 #include "ui/views/widget/native_widget_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #include "ui/views/test/widget_test.h"
10 #include "ui/views/test/test_widget_observer.h"
15 // Tests for NativeWidgetMac that rely on global window manager state, and can
16 // not be parallelized.
17 class NativeWidgetMacInteractiveUITest
19 public ::testing::WithParamInterface<bool> {
23 NativeWidgetMacInteractiveUITest()
24 : activationCount_(0), deactivationCount_(0) {}
26 Widget* MakeWidget() {
27 return GetParam() ? CreateTopLevelFramelessPlatformWidget()
28 : CreateTopLevelPlatformWidget();
32 scoped_ptr<Observer> observer_;
34 int deactivationCount_;
37 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMacInteractiveUITest);
40 class NativeWidgetMacInteractiveUITest::Observer : public TestWidgetObserver {
42 Observer(NativeWidgetMacInteractiveUITest* parent, Widget* widget)
43 : TestWidgetObserver(widget), parent_(parent) {}
45 void OnWidgetActivationChanged(Widget* widget, bool active) override {
47 parent_->activationCount_++;
49 parent_->deactivationCount_++;
53 NativeWidgetMacInteractiveUITest* parent_;
55 DISALLOW_COPY_AND_ASSIGN(Observer);
58 // Test that showing a window causes it to attain global keyWindow status.
59 TEST_P(NativeWidgetMacInteractiveUITest, ShowAttainsKeyStatus) {
60 Widget* widget = MakeWidget();
61 observer_.reset(new Observer(this, widget));
63 EXPECT_FALSE(widget->IsActive());
64 EXPECT_EQ(0, activationCount_);
66 EXPECT_TRUE(widget->IsActive());
68 EXPECT_TRUE([widget->GetNativeWindow() isKeyWindow]);
69 EXPECT_EQ(1, activationCount_);
70 EXPECT_EQ(0, deactivationCount_);
72 // Now check that losing and gaining key status due events outside of Widget
74 Widget* widget2 = MakeWidget(); // Note: not observed.
75 EXPECT_EQ(0, deactivationCount_);
77 EXPECT_EQ(1, deactivationCount_);
80 EXPECT_FALSE(widget->IsActive());
81 EXPECT_EQ(1, deactivationCount_);
82 EXPECT_EQ(1, activationCount_);
84 [widget->GetNativeWindow() makeKeyAndOrderFront:nil];
86 EXPECT_TRUE(widget->IsActive());
87 EXPECT_EQ(1, deactivationCount_);
88 EXPECT_EQ(2, activationCount_);
93 EXPECT_EQ(1, deactivationCount_);
94 EXPECT_EQ(2, activationCount_);
97 // Test that ShowInactive does not take keyWindow status from an active window.
98 TEST_P(NativeWidgetMacInteractiveUITest, ShowInactiveIgnoresKeyStatus) {
99 Widget* widget = MakeWidget();
101 // In an application with only a single window, that window is always "active"
102 // for the application unless that window is not visible. However, it will not
104 EXPECT_FALSE(widget->IsActive());
105 widget->ShowInactive();
106 EXPECT_TRUE(widget->IsActive());
107 RunPendingMessages();
108 EXPECT_FALSE([widget->GetNativeWindow() isKeyWindow]);
110 // Creating a second widget should now keep that widget active.
111 Widget* widget2 = MakeWidget();
113 widget->ShowInactive();
115 EXPECT_FALSE(widget->IsActive());
116 EXPECT_TRUE(widget2->IsActive());
117 RunPendingMessages();
118 EXPECT_FALSE([widget->GetNativeWindow() isKeyWindow]);
119 EXPECT_TRUE([widget2->GetNativeWindow() isKeyWindow]);
121 // And finally activating the inactive widget should activate it and make it
124 EXPECT_TRUE(widget->IsActive());
125 RunPendingMessages();
126 EXPECT_TRUE([widget->GetNativeWindow() isKeyWindow]);
132 INSTANTIATE_TEST_CASE_P(NativeWidgetMacInteractiveUITestInstance,
133 NativeWidgetMacInteractiveUITest,