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/test/widget_test.h"
7 #include <Cocoa/Cocoa.h>
9 #import "base/mac/scoped_nsobject.h"
10 #import "base/mac/scoped_objc_class_swizzler.h"
11 #import "ui/views/cocoa/bridged_native_widget.h"
12 #include "ui/views/widget/root_view.h"
14 @interface IsKeyWindowDonor : NSObject
17 @implementation IsKeyWindowDonor
28 class FakeActivationMac : public WidgetTest::FakeActivation {
31 : swizzler_([NSWindow class],
32 [IsKeyWindowDonor class],
33 @selector(isKeyWindow)) {}
36 base::mac::ScopedObjCClassSwizzler swizzler_;
38 DISALLOW_COPY_AND_ASSIGN(FakeActivationMac);
41 // The NSWindow last activated by SimulateNativeActivate(). It will have a
42 // simulated deactivate on a subsequent call.
43 NSWindow* g_simulated_active_window_ = nil;
48 void WidgetTest::SimulateNativeDestroy(Widget* widget) {
49 // Retain the window while closing it, otherwise the window may lose its last
50 // owner before -[NSWindow close] completes (this offends AppKit). Usually
51 // this reference will exist on an event delivered to the runloop.
52 base::scoped_nsobject<NSWindow> window([widget->GetNativeWindow() retain]);
57 void WidgetTest::SimulateNativeActivate(Widget* widget) {
58 NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
59 if (g_simulated_active_window_) {
60 [center postNotificationName:NSWindowDidResignKeyNotification
61 object:g_simulated_active_window_];
64 g_simulated_active_window_ = widget->GetNativeWindow();
65 DCHECK(g_simulated_active_window_);
67 // For now, don't simulate main status or windows that can't activate.
68 DCHECK([g_simulated_active_window_ canBecomeKeyWindow]);
69 [center postNotificationName:NSWindowDidBecomeKeyNotification
70 object:g_simulated_active_window_];
74 bool WidgetTest::IsNativeWindowVisible(gfx::NativeWindow window) {
75 return [window isVisible];
79 bool WidgetTest::IsWindowStackedAbove(Widget* above, Widget* below) {
80 EXPECT_TRUE(above->IsVisible());
81 EXPECT_TRUE(below->IsVisible());
83 // -[NSApplication orderedWindows] are ordered front-to-back.
84 NSWindow* first = above->GetNativeWindow();
85 NSWindow* second = below->GetNativeWindow();
87 for (NSWindow* window in [NSApp orderedWindows]) {
98 gfx::Size WidgetTest::GetNativeWidgetMinimumContentSize(Widget* widget) {
99 return gfx::Size([widget->GetNativeWindow() contentMinSize]);
103 ui::EventProcessor* WidgetTest::GetEventProcessor(Widget* widget) {
104 return static_cast<internal::RootView*>(widget->GetRootView());
108 scoped_ptr<WidgetTest::FakeActivation> WidgetTest::FakeWidgetIsActiveAlways() {
109 return make_scoped_ptr(new FakeActivationMac);
113 ui::internal::InputMethodDelegate* WidgetTest::GetInputMethodDelegateForWidget(
115 return NativeWidgetMac::GetBridgeForNativeWindow(widget->GetNativeWindow());