Android: Get rid of extra dup()s on launching child processes
[chromium-blink-merge.git] / ui / views / widget / native_widget_mac_interactive_uitest.mm
blobcfaa47431d61a11b01cb8df40aca99b7e97ffd10
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"
12 namespace views {
13 namespace test {
15 // Tests for NativeWidgetMac that rely on global window manager state, and can
16 // not be parallelized.
17 class NativeWidgetMacInteractiveUITest
18     : public WidgetTest,
19       public ::testing::WithParamInterface<bool> {
20  public:
21   class Observer;
23   NativeWidgetMacInteractiveUITest()
24       : activationCount_(0), deactivationCount_(0) {}
26   Widget* MakeWidget() {
27     return GetParam() ? CreateTopLevelFramelessPlatformWidget()
28                       : CreateTopLevelPlatformWidget();
29   }
31  protected:
32   scoped_ptr<Observer> observer_;
33   int activationCount_;
34   int deactivationCount_;
36  private:
37   DISALLOW_COPY_AND_ASSIGN(NativeWidgetMacInteractiveUITest);
40 class NativeWidgetMacInteractiveUITest::Observer : public TestWidgetObserver {
41  public:
42   Observer(NativeWidgetMacInteractiveUITest* parent, Widget* widget)
43       : TestWidgetObserver(widget), parent_(parent) {}
45   void OnWidgetActivationChanged(Widget* widget, bool active) override {
46     if (active)
47       parent_->activationCount_++;
48     else
49       parent_->deactivationCount_++;
50   }
52  private:
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_);
65   widget->Show();
66   EXPECT_TRUE(widget->IsActive());
67   RunPendingMessages();
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
73   // works correctly.
74   Widget* widget2 = MakeWidget();  // Note: not observed.
75   EXPECT_EQ(0, deactivationCount_);
76   widget2->Show();
77   EXPECT_EQ(1, deactivationCount_);
79   RunPendingMessages();
80   EXPECT_FALSE(widget->IsActive());
81   EXPECT_EQ(1, deactivationCount_);
82   EXPECT_EQ(1, activationCount_);
84   [widget->GetNativeWindow() makeKeyAndOrderFront:nil];
85   RunPendingMessages();
86   EXPECT_TRUE(widget->IsActive());
87   EXPECT_EQ(1, deactivationCount_);
88   EXPECT_EQ(2, activationCount_);
90   widget2->CloseNow();
91   widget->CloseNow();
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
103   // be key.
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();
112   widget2->Show();
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
122   // key.
123   widget->Activate();
124   EXPECT_TRUE(widget->IsActive());
125   RunPendingMessages();
126   EXPECT_TRUE([widget->GetNativeWindow() isKeyWindow]);
128   widget2->CloseNow();
129   widget->CloseNow();
132 INSTANTIATE_TEST_CASE_P(NativeWidgetMacInteractiveUITestInstance,
133                         NativeWidgetMacInteractiveUITest,
134                         ::testing::Bool());
136 }  // namespace test
137 }  // namespace views