aboutsummaryrefslogtreecommitdiff
path: root/AppKit/GTMCarbonEventTest.m
blob: f7b4d54f609bd7cb3f7dad09c20a876cc3806bd6 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
//  GTMCarbonEventTest.m
//
//  Copyright 2006-2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
// 
//  http://www.apache.org/licenses/LICENSE-2.0
// 
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMSenTestCase.h"
#import "GTMCarbonEvent.h"
#import "GTMUnitTestingUtilities.h"
#import "GTMUnitTestDevLog.h"

@interface GTMCarbonEventTest : GTMTestCase {
 @private
  GTMCarbonEvent *event_;
}
@end

@interface GTMCarbonEventHandlerTest : GTMTestCase {
 @private
  GTMCarbonEventHandler *handler_;
}
@end

@interface GTMCarbonEventMonitorHandlerTest : GTMTestCase 
@end

@interface GTMCarbonEventDispatcherHandlerTest : GTMTestCase {
 @private
  BOOL hotKeyHit_;
}
@end

static const UInt32 kTestClass = 'foo ';
static const UInt32 kTestKind = 'bar ';
static const UInt32 kTestParameterName = 'baz ';
static const UInt32 kTestBadParameterName = 'bom ';
static const UInt32 kTestParameterValue = 'bam ';

@implementation GTMCarbonEventTest
- (void)setUp {
  event_ = [[GTMCarbonEvent eventWithClass:kTestClass kind:kTestKind] retain];
}

- (void)tearDown {
  [event_ release];
}

- (void)testCopy { 
  GTMCarbonEvent *event2 = [[event_ copy] autorelease];
  STAssertNotNil(event2, nil);
}

- (void)testEventWithClassAndKind {
  STAssertEquals([event_ eventClass], kTestClass, nil);
  STAssertEquals([event_ eventKind], kTestKind, nil);
}

- (void)testEventWithEvent {
  GTMCarbonEvent *event2 = [GTMCarbonEvent eventWithEvent:[event_ event]];
  STAssertEquals([event2 event], [event_ event], nil);
}

- (void)testCurrentEvent {
  EventRef eventRef = GetCurrentEvent();
  GTMCarbonEvent *event = [GTMCarbonEvent currentEvent];
  STAssertEquals([event event], eventRef, nil);
}

- (void)testEventClass {
  [self testEventWithClassAndKind];
}  

- (void)testEventKind {
  [self testEventWithClassAndKind];
}

- (void)testSetTime {
  EventTime eventTime = [event_ time];
  STAssertNotEquals(eventTime, kEventDurationNoWait, nil);
  STAssertNotEquals(eventTime, kEventDurationForever, nil);
  [event_ setTime:kEventDurationForever];
  EventTime testTime = [event_ time];
  STAssertEquals(testTime, kEventDurationForever, nil);
  [event_ setTime:eventTime];
  STAssertEquals([event_ time], eventTime, nil);
}

- (void)testTime {
  [self testSetTime];
}

- (void)testEvent {
  [self testEventWithEvent];
}

- (void)testSetParameterNamed {
  UInt32 theData = kTestParameterValue;
  [event_ setUInt32ParameterNamed:kTestParameterName data:&theData];
  theData = 0;
  STAssertEquals([event_ sizeOfParameterNamed:kTestParameterName 
                                         type:typeUInt32], 
                 sizeof(UInt32), nil);
  STAssertTrue([event_ getUInt32ParameterNamed:kTestParameterName 
                                          data:&theData], nil);
  STAssertEquals(theData, kTestParameterValue, nil);
}

- (void)testGetParameterNamed {
  [self testSetParameterNamed];
  UInt32 theData = kTestParameterValue;
  STAssertFalse([event_ getUInt32ParameterNamed:kTestBadParameterName 
                                           data:&theData], nil);
  STAssertFalse([event_ getUInt32ParameterNamed:kTestBadParameterName 
                                           data:NULL], nil);
  
}

- (void)testSizeOfParameterNamed {
  [self testSetParameterNamed];
}

- (void)testHasParameterNamed {
  [self testSetParameterNamed];
}

- (OSStatus)gtm_eventHandler:(GTMCarbonEventHandler *)sender 
               receivedEvent:(GTMCarbonEvent *)event 
                     handler:(EventHandlerCallRef)handler {
  OSStatus status = eventNotHandledErr;
  if ([event eventClass] == kTestClass && [event eventKind] == kTestKind) {
    status = noErr;
  }
  return status;
}

- (void)testSendToTarget {
  EventTypeSpec types = { kTestClass, kTestKind };
  GTMCarbonEventDispatcherHandler *handler 
    = [[GTMCarbonEventDispatcherHandler sharedEventDispatcherHandler] 
       autorelease];
  [handler registerForEvents:&types count:1];
  OSStatus status = [event_ sendToTarget:handler options:0];
  STAssertErr(status, eventNotHandledErr, @"status: %ld", status);
  [handler setDelegate:self];
  status = [event_ sendToTarget:handler options:0];
  STAssertNoErr(status, @"status: %ld", status);
  [handler unregisterForEvents:&types count:1];
}

- (void)testPostToQueue {
  EventQueueRef eventQueue = GetMainEventQueue();
  [event_ postToMainQueue];
  OSStatus status = [event_ postToQueue:eventQueue 
                               priority:kEventPriorityStandard];
  STAssertErr(status, eventAlreadyPostedErr, @"status: %ld", status);
  EventTypeSpec types = { kTestClass, kTestKind };
  status = FlushEventsMatchingListFromQueue(eventQueue, 1, &types);
  STAssertNoErr(status, @"status: %ld", status);
  
  eventQueue = GetCurrentEventQueue();
  [event_ postToCurrentQueue];
  status = [event_ postToQueue:eventQueue priority:kEventPriorityStandard];
  STAssertErr(status, eventAlreadyPostedErr, @"status: %ld", status);
  status = FlushEventsMatchingListFromQueue(eventQueue, 1, &types);
  STAssertNoErr(status, @"status: %ld", status);
}

- (void)testPostToMainQueue {
  [self testPostToQueue];
}

- (void)testPostToCurrentQueue {
  STAssertEquals(GetCurrentEventQueue(), GetMainEventQueue(), nil);
  [self testPostToMainQueue];
}

- (void)testDescription {
  NSString *descString 
    = [NSString stringWithFormat:@"GTMCarbonEvent 'foo ' %d", kTestKind];
  STAssertEqualObjects([event_ description], descString, nil); 
}
@end

@implementation GTMCarbonEventHandlerTest

- (void)setUp {
  handler_ = [[GTMCarbonEventHandler alloc] init];
}

- (void)tearDown {
  [handler_ release];
}

- (void)testEventTarget {
  STAssertNULL([handler_ eventTarget], nil);
}

- (void)testEventHandler {
  [GTMUnitTestDevLogDebug expectPattern:
   @"DebugAssert: GoogleToolboxForMac: event CantUseParams .*"];
  STAssertErr([handler_ handleEvent:nil handler:nil], eventNotHandledErr, nil);
}

- (void)testDelegate {
  [handler_ setDelegate:self];
  STAssertEqualObjects([handler_ delegate], self, nil);
  [handler_ setDelegate:nil];
  STAssertNil([handler_ delegate], nil);
}


- (void)testSetDelegate { 
  [self testDelegate];
}

@end

@implementation GTMCarbonEventMonitorHandlerTest

- (void)testEventHandler {
  GTMCarbonEventMonitorHandler *monitor 
    = [GTMCarbonEventMonitorHandler sharedEventMonitorHandler];
  STAssertEquals([monitor eventTarget], GetEventMonitorTarget(), nil);
}

@end

@implementation GTMCarbonEventDispatcherHandlerTest

- (void)testEventHandler {
  GTMCarbonEventDispatcherHandler *dispatcher 
    = [GTMCarbonEventDispatcherHandler sharedEventDispatcherHandler];
  STAssertEquals([dispatcher eventTarget], GetEventDispatcherTarget(), nil);
}

- (void)hitHotKey:(id)sender {
  hotKeyHit_ = YES;
  [NSApp stop:self];
}

- (void)hitExceptionalHotKey:(id)sender {
  [NSException raise:@"foo" format:@"bar"];
}

- (void)testRegisterHotKeyModifiersTargetActionWhenPressed {
  
  // This test can't be run if the screen saver is active because the security
  // agent blocks us from sending events via remote operations
  if (![GTMUnitTestingUtilities isScreenSaverActive]) {
    GTMCarbonEventDispatcherHandler *dispatcher 
      = [GTMCarbonEventDispatcherHandler sharedEventDispatcherHandler];
    STAssertNotNil(dispatcher, @"Unable to acquire singleton");
    UInt32 keyMods = (NSShiftKeyMask | NSControlKeyMask 
                      | NSAlternateKeyMask | NSCommandKeyMask);
    EventHotKeyRef hotKey;
    [GTMUnitTestDevLogDebug expectPattern:@"DebugAssert: GoogleToolboxForMac: "
     @"newKey CantCreateKey .*"];
    STAssertNULL([dispatcher registerHotKey:0x5 
                                  modifiers:keyMods
                                     target:nil 
                                     action:nil
                                whenPressed:YES], 
                 @"Shouldn't have created hotkey");
#if DEBUG
    // This tests debug selector validation, so we only can do it in debug.
    [GTMUnitTestDevLogDebug expectPattern:@"RecordedNSAssert in "
      @"GTMAssertSelectorNilOrImplementedWithReturnTypeAndArguments - "
      @"\"GTMCarbonEventDispatcherHandlerTest\" selector \"badSelector:\" is "
      @"unimplemented or misnamed \\(.*/GTMDebugSelectorValidation.h:[0-9]*\\)"];
    STAssertThrowsSpecificNamed([dispatcher registerHotKey:0x5 
                                                 modifiers:keyMods
                                                    target:self 
                                                    action:@selector(badSelector:) 
                                               whenPressed:YES],
                                NSException, NSInternalInconsistencyException,
                                @"Shouldn't have created hotkey");
#endif
    hotKey = [dispatcher registerHotKey:0x5 
                              modifiers:keyMods
                                 target:self 
                                 action:@selector(hitHotKey:) 
                            whenPressed:YES];
    STAssertNotNULL(hotKey, @"Unable to create hotkey");
    
    hotKeyHit_ = NO;
    
    // Post the hotkey combo to the event queue. If everything is working
    // correctly hitHotKey: should get called, and hotKeyHit_ will be set for
    // us.  We run the event loop for a set amount of time waiting for this to
    // happen.
    [GTMUnitTestingUtilities postTypeCharacterEvent:'g' modifiers:keyMods];
    NSDate* future = [NSDate dateWithTimeIntervalSinceNow:1.0f];
    [GTMUnitTestingUtilities runUntilDate:future];
    STAssertTrue(hotKeyHit_, @"Hot key never got fired.");
    [dispatcher unregisterHotKey:hotKey];    
  }
}

- (void)testRegisterHotKeyModifiersTargetActionWhenPressedException {
  
  // This test can't be run if the screen saver is active because the security
  // agent blocks us from sending events via remote operations
  if (![GTMUnitTestingUtilities isScreenSaverActive]) {
    GTMCarbonEventDispatcherHandler *dispatcher 
      = [GTMCarbonEventDispatcherHandler sharedEventDispatcherHandler];
    STAssertNotNil(dispatcher, @"Unable to acquire singleton");
    UInt32 keyMods = (NSShiftKeyMask | NSControlKeyMask 
                      | NSAlternateKeyMask | NSCommandKeyMask);
    EventHotKeyRef hotKey = [dispatcher registerHotKey:0x5 
                                             modifiers:keyMods 
                                                target:self 
                                                action:@selector(hitExceptionalHotKey:) 
                                           whenPressed:YES];
    STAssertTrue(hotKey != nil, @"Unable to create hotkey");
        
    // Post the hotkey combo to the event queue. If everything is working correctly
    // hitHotKey: should get called, and hotKeyHit_ will be set for us.
    // We run the event loop for a set amount of time waiting for this to happen.
    [GTMUnitTestingUtilities postTypeCharacterEvent:'g' modifiers:keyMods];
    NSDate* future = [NSDate dateWithTimeIntervalSinceNow:1.0f];
    [GTMUnitTestDevLog expectString:@"Exception fired in hotkey: foo (bar)"];
    [GTMUnitTestingUtilities runUntilDate:future];
    [dispatcher unregisterHotKey:hotKey];    
  }
}

- (void)testKeyModifiers {
  struct {
    NSUInteger cocoaKey_;
    UInt32 carbonKey_;
  } keyMap[] = {
    { NSAlphaShiftKeyMask, alphaLock},
    { NSShiftKeyMask, shiftKey},
    { NSControlKeyMask, controlKey},
    { NSAlternateKeyMask, optionKey},
    { NSCommandKeyMask, cmdKey},
  };
  size_t combos = pow(2, sizeof(keyMap) / sizeof(keyMap[0]));
  for (size_t i = 0; i < combos; i++) {
    NSUInteger cocoaMods = 0;
    UInt32 carbonMods = 0;
    for (size_t j = 0; j < 32 && j < sizeof(keyMap) / sizeof(keyMap[0]); j++) {
      if (i & 1 << j) {
        cocoaMods |= keyMap[j].cocoaKey_;
        carbonMods |= keyMap[j].carbonKey_;
      }
    }
    STAssertEquals(GTMCocoaToCarbonKeyModifiers(cocoaMods), carbonMods, nil);
    STAssertEquals(GTMCarbonToCocoaKeyModifiers(carbonMods), cocoaMods, nil);
  }
}
    

@end