blob: 817cf4c9dd7c3947e5f735589065dd50c49192ac (
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
101
102
103
104
105
106
107
108
109
110
111
|
// 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.
#import <Cocoa/Cocoa.h>
#include "base/scoped_nsobject.h"
#include "chrome/app/chrome_dll_resource.h"
#import "chrome/browser/cocoa/chrome_event_processing_window.h"
#import "chrome/browser/cocoa/browser_window_controller.h"
#import "chrome/browser/cocoa/browser_frame_view.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
namespace {
NSEvent* KeyEvent(const NSUInteger flags, const NSUInteger keyCode) {
return [NSEvent keyEventWithType:NSKeyDown
location:NSZeroPoint
modifierFlags:flags
timestamp:0.0
windowNumber:0
context:nil
characters:@""
charactersIgnoringModifiers:@""
isARepeat:NO
keyCode:keyCode];
}
class ChromeEventProcessingWindowTest : public PlatformTest {
public:
ChromeEventProcessingWindowTest() {
// Create a window.
const NSUInteger mask = NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask | NSResizableWindowMask;
window_.reset([[ChromeEventProcessingWindow alloc]
initWithContentRect:NSMakeRect(0, 0, 800, 600)
styleMask:mask
backing:NSBackingStoreBuffered
defer:NO]);
if (DebugUtil::BeingDebugged()) {
[window_ orderFront:nil];
} else {
[window_ orderBack:nil];
}
}
// Returns a canonical snapshot of the window.
NSData* WindowContentsAsTIFF() {
NSRect frame([window_ frame]);
frame.origin = [window_ convertScreenToBase:frame.origin];
NSData* pdfData = [window_ dataWithPDFInsideRect:frame];
// |pdfData| can differ for windows which look the same, so make it
// canonical.
NSImage* image = [[[NSImage alloc] initWithData:pdfData] autorelease];
return [image TIFFRepresentation];
}
CocoaNoWindowTestHelper cocoa_helper_;
scoped_nsobject<ChromeEventProcessingWindow> window_;
};
// Verify that the window intercepts a particular key event and
// forwards it to [delegate executeCommand:]. Assume that other
// CommandForKeyboardShortcut() will work the same for the rest.
TEST_F(ChromeEventProcessingWindowTest,
PerformKeyEquivalentForwardToExecuteCommand) {
NSEvent* event = KeyEvent(NSCommandKeyMask, kVK_ANSI_1);
id delegate = [OCMockObject mockForClass:[BrowserWindowController class]];
// -stub to satisfy the DCHECK.
BOOL yes = YES;
[[[delegate stub] andReturnValue:OCMOCK_VALUE(yes)]
isKindOfClass:[BrowserWindowController class]];
[[delegate expect] executeCommand:IDC_SELECT_TAB_0];
[window_ setDelegate:delegate];
[window_ performKeyEquivalent:event];
// Don't wish to mock all the way down...
[window_ setDelegate:nil];
[delegate verify];
}
// Verify that an unhandled shortcut does not get forwarded via
// -executeCommand:.
// TODO(shess) Think of a way to test that it is sent to the
// superclass.
TEST_F(ChromeEventProcessingWindowTest, PerformKeyEquivalentNoForward) {
NSEvent* event = KeyEvent(0, 0);
id delegate = [OCMockObject mockForClass:[BrowserWindowController class]];
// -stub to satisfy the DCHECK.
BOOL yes = YES;
[[[delegate stub] andReturnValue:OCMOCK_VALUE(yes)]
isKindOfClass:[BrowserWindowController class]];
[window_ setDelegate:delegate];
[window_ performKeyEquivalent:event];
// Don't wish to mock all the way down...
[window_ setDelegate:nil];
[delegate verify];
}
} // namespace
|