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
|
// Copyright 2014 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 "ui/views/widget/native_widget_mac.h"
#import <Cocoa/Cocoa.h>
#include "ui/views/test/widget_test.h"
#include "ui/views/test/test_widget_observer.h"
namespace views {
namespace test {
// Tests for NativeWidgetMac that rely on global window manager state, and can
// not be parallelized.
class NativeWidgetMacInteractiveUITest
: public WidgetTest,
public ::testing::WithParamInterface<bool> {
public:
class Observer;
NativeWidgetMacInteractiveUITest()
: activationCount_(0), deactivationCount_(0) {}
Widget* MakeWidget() {
return GetParam() ? CreateTopLevelFramelessPlatformWidget()
: CreateTopLevelPlatformWidget();
}
protected:
scoped_ptr<Observer> observer_;
int activationCount_;
int deactivationCount_;
private:
DISALLOW_COPY_AND_ASSIGN(NativeWidgetMacInteractiveUITest);
};
class NativeWidgetMacInteractiveUITest::Observer : public TestWidgetObserver {
public:
Observer(NativeWidgetMacInteractiveUITest* parent, Widget* widget)
: TestWidgetObserver(widget), parent_(parent) {}
virtual void OnWidgetActivationChanged(Widget* widget, bool active) override {
if (active)
parent_->activationCount_++;
else
parent_->deactivationCount_++;
}
private:
NativeWidgetMacInteractiveUITest* parent_;
DISALLOW_COPY_AND_ASSIGN(Observer);
};
// Test that showing a window causes it to attain global keyWindow status.
TEST_P(NativeWidgetMacInteractiveUITest, ShowAttainsKeyStatus) {
Widget* widget = MakeWidget();
observer_.reset(new Observer(this, widget));
EXPECT_FALSE(widget->IsActive());
EXPECT_EQ(0, activationCount_);
widget->Show();
EXPECT_TRUE(widget->IsActive());
RunPendingMessages();
EXPECT_TRUE([widget->GetNativeWindow() isKeyWindow]);
EXPECT_EQ(1, activationCount_);
EXPECT_EQ(0, deactivationCount_);
// Now check that losing and gaining key status due events outside of Widget
// works correctly.
Widget* widget2 = MakeWidget(); // Note: not observed.
EXPECT_EQ(0, deactivationCount_);
widget2->Show();
EXPECT_EQ(1, deactivationCount_);
RunPendingMessages();
EXPECT_FALSE(widget->IsActive());
EXPECT_EQ(1, deactivationCount_);
EXPECT_EQ(1, activationCount_);
[widget->GetNativeWindow() makeKeyAndOrderFront:nil];
RunPendingMessages();
EXPECT_TRUE(widget->IsActive());
EXPECT_EQ(1, deactivationCount_);
EXPECT_EQ(2, activationCount_);
widget2->CloseNow();
widget->CloseNow();
EXPECT_EQ(1, deactivationCount_);
EXPECT_EQ(2, activationCount_);
}
// Test that ShowInactive does not take keyWindow status from an active window.
TEST_P(NativeWidgetMacInteractiveUITest, ShowInactiveIgnoresKeyStatus) {
Widget* widget = MakeWidget();
// In an application with only a single window, that window is always "active"
// for the application unless that window is not visible. However, it will not
// be key.
EXPECT_FALSE(widget->IsActive());
widget->ShowInactive();
EXPECT_TRUE(widget->IsActive());
RunPendingMessages();
EXPECT_FALSE([widget->GetNativeWindow() isKeyWindow]);
// Creating a second widget should now keep that widget active.
Widget* widget2 = MakeWidget();
widget2->Show();
widget->ShowInactive();
EXPECT_FALSE(widget->IsActive());
EXPECT_TRUE(widget2->IsActive());
RunPendingMessages();
EXPECT_FALSE([widget->GetNativeWindow() isKeyWindow]);
EXPECT_TRUE([widget2->GetNativeWindow() isKeyWindow]);
// And finally activating the inactive widget should activate it and make it
// key.
widget->Activate();
EXPECT_TRUE(widget->IsActive());
RunPendingMessages();
EXPECT_TRUE([widget->GetNativeWindow() isKeyWindow]);
widget2->CloseNow();
widget->CloseNow();
}
INSTANTIATE_TEST_CASE_P(NativeWidgetMacInteractiveUITestInstance,
NativeWidgetMacInteractiveUITest,
::testing::Bool());
} // namespace test
} // namespace views
|