blob: ebcb4d49603ea5db3ccad08280aeb70cae1351e4 (
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
|
// Copyright (c) 2009 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.
#ifndef CHROME_BROWSER_COCOA_COCOA_TEST_HELPER_H_
#define CHROME_BROWSER_COCOA_COCOA_TEST_HELPER_H_
#import <Cocoa/Cocoa.h>
#include "base/debug_util.h"
#include "base/file_path.h"
#include "base/mac_util.h"
#include "base/path_service.h"
#import "base/scoped_nsobject.h"
#include "chrome/common/mac_app_names.h"
// Background windows normally will not display things such as focus
// rings. This class allows -isKeyWindow to be manipulated to test
// such things.
@interface CocoaTestHelperWindow : NSWindow {
@private
BOOL pretendIsKeyWindow_;
}
// Init a borderless non-deferred window with a backing store.
- (id)initWithContentRect:(NSRect)contentRect;
// Init with a default frame.
- (id)init;
// Set value to return for -isKeyWindow.
- (void)setPretendIsKeyWindow:(BOOL)isKeyWindow;
- (BOOL)isKeyWindow;
@end
// A class that initializes Cocoa and sets up resources for many of our
// Cocoa controller unit tests. It does several key things:
// - Creates and displays an empty Cocoa window for views to live in
// - Loads the appropriate bundle so nib loading works. When loading the
// nib in the class being tested, your must use |mac_util::MainAppBundle()|
// as the bundle. If you do not specify a bundle, your test will likely
// fail.
// It currently does not create an autorelease pool, though that can easily be
// added. If your test wants one, it can derive from PlatformTest instead of
// testing::Test.
class CocoaTestHelper {
public:
CocoaTestHelper() {
// Look in the framework bundle for resources.
FilePath path;
PathService::Get(base::DIR_EXE, &path);
path = path.AppendASCII(MAC_BROWSER_APP_NAME);
path = path.AppendASCII("Contents");
path = path.AppendASCII("Frameworks");
path = path.AppendASCII(MAC_FRAMEWORK_NAME);
mac_util::SetOverrideAppBundlePath(path);
// Bootstrap Cocoa. It's very unhappy without this.
[NSApplication sharedApplication];
// Create a window.
window_.reset([[CocoaTestHelperWindow alloc] init]);
if (DebugUtil::BeingDebugged()) {
[window_ orderFront:nil];
} else {
[window_ orderBack:nil];
}
// Set the duration of AppKit-evaluated animations (such as frame changes)
// to zero for testing purposes. That way they take effect immediately.
[[NSAnimationContext currentContext] setDuration:0.0];
}
// Access the Cocoa window created for the test.
NSWindow* window() const { return window_.get(); }
NSView* contentView() const { return [window_ contentView]; }
// Set |window_| to pretend to be key and make |aView| its
// firstResponder.
void makeFirstResponder(NSView* aView) {
[window_ setPretendIsKeyWindow:YES];
[window_ makeFirstResponder:aView];
}
// Clear |window_| firstResponder and stop pretending to be key.
void clearFirstResponder() {
[window_ makeFirstResponder:nil];
[window_ setPretendIsKeyWindow:NO];
}
private:
scoped_nsobject<CocoaTestHelperWindow> window_;
};
#endif // CHROME_BROWSER_COCOA_COCOA_TEST_HELPER_H_
|