Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / ui / views / test / widget_test_mac.mm
blob628655f58de6c4764d1d754517f3695a9d359fc8
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
15 @end
17 @implementation IsKeyWindowDonor
18 - (BOOL)isKeyWindow {
19   return YES;
21 @end
23 namespace views {
24 namespace test {
26 namespace {
28 class FakeActivationMac : public WidgetTest::FakeActivation {
29  public:
30   FakeActivationMac()
31       : swizzler_([NSWindow class],
32                   [IsKeyWindowDonor class],
33                   @selector(isKeyWindow)) {}
35  private:
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;
45 }  // namespace
47 // static
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]);
53   [window close];
56 // static
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_];
62   }
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_];
73 // static
74 bool WidgetTest::IsNativeWindowVisible(gfx::NativeWindow window) {
75   return [window isVisible];
78 // static
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]) {
88     if (window == second)
89       return !first;
91     if (window == first)
92       first = nil;
93   }
94   return false;
97 // static
98 gfx::Size WidgetTest::GetNativeWidgetMinimumContentSize(Widget* widget) {
99   return gfx::Size([widget->GetNativeWindow() contentMinSize]);
102 // static
103 ui::EventProcessor* WidgetTest::GetEventProcessor(Widget* widget) {
104   return static_cast<internal::RootView*>(widget->GetRootView());
107 // static
108 scoped_ptr<WidgetTest::FakeActivation> WidgetTest::FakeWidgetIsActiveAlways() {
109   return make_scoped_ptr(new FakeActivationMac);
112 // static
113 ui::internal::InputMethodDelegate* WidgetTest::GetInputMethodDelegateForWidget(
114     Widget* widget) {
115   return NativeWidgetMac::GetBridgeForNativeWindow(widget->GetNativeWindow());
118 }  // namespace test
119 }  // namespace views