blob: b78680dba23dec94efc4996da2d114eccf24eafe (
plain)
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
|
// Copyright 2015 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.
#import "ui/base/test/scoped_fake_nswindow_focus.h"
#import <Cocoa/Cocoa.h>
#import "base/mac/foundation_util.h"
#import "base/mac/scoped_objc_class_swizzler.h"
using base::mac::ScopedObjCClassSwizzler;
namespace {
NSWindow* g_fake_focused_window = nil;
void SetFocus(NSWindow* window) {
g_fake_focused_window = window;
[[NSNotificationCenter defaultCenter]
postNotificationName:NSWindowDidBecomeMainNotification
object:g_fake_focused_window];
[[NSNotificationCenter defaultCenter]
postNotificationName:NSWindowDidBecomeKeyNotification
object:g_fake_focused_window];
}
void ClearFocus() {
NSWindow* window = g_fake_focused_window;
g_fake_focused_window = nil;
[[NSNotificationCenter defaultCenter]
postNotificationName:NSWindowDidResignKeyNotification
object:window];
[[NSNotificationCenter defaultCenter]
postNotificationName:NSWindowDidResignMainNotification
object:window];
}
} // namespace
// Donates testing implementations of NSWindow methods.
@interface FakeNSWindowFocusDonor : NSObject
@end
@implementation FakeNSWindowFocusDonor
- (BOOL)isKeyWindow {
NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self);
return selfAsWindow == g_fake_focused_window;
}
- (BOOL)isMainWindow {
NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self);
return selfAsWindow == g_fake_focused_window;
}
- (void)makeKeyWindow {
NSWindow* selfAsWindow = base::mac::ObjCCastStrict<NSWindow>(self);
if (selfAsWindow == g_fake_focused_window ||
![selfAsWindow canBecomeKeyWindow])
return;
ClearFocus();
SetFocus(selfAsWindow);
}
- (void)makeMainWindow {
[self makeKeyWindow];
}
@end
namespace ui {
namespace test {
ScopedFakeNSWindowFocus::ScopedFakeNSWindowFocus()
: is_main_swizzler_(
new ScopedObjCClassSwizzler([NSWindow class],
[FakeNSWindowFocusDonor class],
@selector(isMainWindow))),
make_main_swizzler_(
new ScopedObjCClassSwizzler([NSWindow class],
[FakeNSWindowFocusDonor class],
@selector(makeMainWindow))),
is_key_swizzler_(
new ScopedObjCClassSwizzler([NSWindow class],
[FakeNSWindowFocusDonor class],
@selector(isKeyWindow))),
make_key_swizzler_(
new ScopedObjCClassSwizzler([NSWindow class],
[FakeNSWindowFocusDonor class],
@selector(makeKeyWindow))) {}
ScopedFakeNSWindowFocus::~ScopedFakeNSWindowFocus() {
ClearFocus();
}
} // namespace test
} // namespace ui
|