PageRenderTime 106ms CodeModel.GetById 18ms app.highlight 84ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/AppKit/GTMCarbonEventTest.m

http://macfuse.googlecode.com/
Objective C | 384 lines | 291 code | 62 blank | 31 comment | 11 complexity | b2fcc2195e317b06d0cb42d8f2102709 MD5 | raw file
  1//
  2//  GTMCarbonEventTest.m
  3//
  4//  Copyright 2006-2008 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9//
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11//
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import "GTMSenTestCase.h"
 20#import "GTMCarbonEvent.h"
 21#import "GTMAppKitUnitTestingUtilities.h"
 22#import "GTMUnitTestDevLog.h"
 23
 24@interface GTMCarbonEventTest : GTMTestCase {
 25 @private
 26  GTMCarbonEvent *event_;
 27}
 28@end
 29
 30@interface GTMCarbonEventHandlerTest : GTMTestCase {
 31 @private
 32  GTMCarbonEventHandler *handler_;
 33}
 34@end
 35
 36@interface GTMCarbonEventMonitorHandlerTest : GTMTestCase
 37@end
 38
 39@interface GTMCarbonEventApplicationEventHandlerTest : GTMTestCase
 40@end
 41
 42@interface GTMCarbonEventDispatcherHandlerTest : GTMTestCase {
 43 @private
 44  GTMUnitTestingBooleanRunLoopContext *hotKeyHit_;
 45}
 46@end
 47
 48static const UInt32 kTestClass = 'foo ';
 49static const UInt32 kTestKind = 'bar ';
 50static const UInt32 kTestParameterName = 'baz ';
 51static const UInt32 kTestBadParameterName = 'bom ';
 52static const UInt32 kTestParameterValue = 'bam ';
 53
 54@implementation GTMCarbonEventTest
 55- (void)setUp {
 56  event_ = [[GTMCarbonEvent eventWithClass:kTestClass kind:kTestKind] retain];
 57}
 58
 59- (void)tearDown {
 60  [event_ release];
 61}
 62
 63- (void)testCopy {
 64  GTMCarbonEvent *event2 = [[event_ copy] autorelease];
 65  STAssertNotNil(event2, nil);
 66}
 67
 68- (void)testEventWithClassAndKind {
 69  STAssertEquals([event_ eventClass], kTestClass, nil);
 70  STAssertEquals([event_ eventKind], kTestKind, nil);
 71}
 72
 73- (void)testEventWithEvent {
 74  GTMCarbonEvent *event2 = [GTMCarbonEvent eventWithEvent:[event_ event]];
 75  STAssertEquals([event2 event], [event_ event], nil);
 76}
 77
 78- (void)testCurrentEvent {
 79  EventRef eventRef = GetCurrentEvent();
 80  GTMCarbonEvent *event = [GTMCarbonEvent currentEvent];
 81  STAssertEquals([event event], eventRef, nil);
 82}
 83
 84- (void)testEventClass {
 85  [self testEventWithClassAndKind];
 86}
 87
 88- (void)testEventKind {
 89  [self testEventWithClassAndKind];
 90}
 91
 92- (void)testSetTime {
 93  EventTime eventTime = [event_ time];
 94  STAssertNotEquals(eventTime, kEventDurationNoWait, nil);
 95  STAssertNotEquals(eventTime, kEventDurationForever, nil);
 96  [event_ setTime:kEventDurationForever];
 97  EventTime testTime = [event_ time];
 98  STAssertEquals(testTime, kEventDurationForever, nil);
 99  [event_ setTime:eventTime];
100  STAssertEquals([event_ time], eventTime, nil);
101}
102
103- (void)testTime {
104  [self testSetTime];
105}
106
107- (void)testEvent {
108  [self testEventWithEvent];
109}
110
111- (void)testSetParameterNamed {
112  UInt32 theData = kTestParameterValue;
113  [event_ setUInt32ParameterNamed:kTestParameterName data:&theData];
114  theData = 0;
115  STAssertEquals([event_ sizeOfParameterNamed:kTestParameterName
116                                         type:typeUInt32],
117                 sizeof(UInt32), nil);
118  STAssertTrue([event_ getUInt32ParameterNamed:kTestParameterName
119                                          data:&theData], nil);
120  STAssertEquals(theData, kTestParameterValue, nil);
121}
122
123- (void)testGetParameterNamed {
124  [self testSetParameterNamed];
125  UInt32 theData = kTestParameterValue;
126  STAssertFalse([event_ getUInt32ParameterNamed:kTestBadParameterName
127                                           data:&theData], nil);
128  STAssertFalse([event_ getUInt32ParameterNamed:kTestBadParameterName
129                                           data:NULL], nil);
130
131}
132
133- (void)testSizeOfParameterNamed {
134  [self testSetParameterNamed];
135}
136
137- (void)testHasParameterNamed {
138  [self testSetParameterNamed];
139}
140
141- (OSStatus)gtm_eventHandler:(GTMCarbonEventHandler *)sender
142               receivedEvent:(GTMCarbonEvent *)event
143                     handler:(EventHandlerCallRef)handler {
144  OSStatus status = eventNotHandledErr;
145  if ([event eventClass] == kTestClass && [event eventKind] == kTestKind) {
146    status = noErr;
147  }
148  return status;
149}
150
151- (void)testSendToTarget {
152  EventTypeSpec types = { kTestClass, kTestKind };
153  GTMCarbonEventDispatcherHandler *handler
154    = [[GTMCarbonEventDispatcherHandler sharedEventDispatcherHandler]
155       autorelease];
156  [handler registerForEvents:&types count:1];
157  OSStatus status = [event_ sendToTarget:handler options:0];
158  STAssertErr(status, eventNotHandledErr, @"status: %ld", (long)status);
159  [handler setDelegate:self];
160  status = [event_ sendToTarget:handler options:0];
161  STAssertNoErr(status, @"status: %ld", (long)status);
162  [handler unregisterForEvents:&types count:1];
163}
164
165- (void)testPostToQueue {
166  EventQueueRef eventQueue = GetMainEventQueue();
167  [event_ postToMainQueue];
168  OSStatus status = [event_ postToQueue:eventQueue
169                               priority:kEventPriorityStandard];
170  STAssertErr(status, eventAlreadyPostedErr, @"status: %ld", (long)status);
171  EventTypeSpec types = { kTestClass, kTestKind };
172  status = FlushEventsMatchingListFromQueue(eventQueue, 1, &types);
173  STAssertNoErr(status, @"status: %ld", (long)status);
174
175  eventQueue = GetCurrentEventQueue();
176  [event_ postToCurrentQueue];
177  status = [event_ postToQueue:eventQueue priority:kEventPriorityStandard];
178  STAssertErr(status, eventAlreadyPostedErr, @"status: %ld", (long)status);
179  status = FlushEventsMatchingListFromQueue(eventQueue, 1, &types);
180  STAssertNoErr(status, @"status: %ld", status);
181}
182
183- (void)testPostToMainQueue {
184  [self testPostToQueue];
185}
186
187- (void)testPostToCurrentQueue {
188  STAssertEquals(GetCurrentEventQueue(), GetMainEventQueue(), nil);
189  [self testPostToMainQueue];
190}
191
192- (void)testDescription {
193  NSString *descString
194    = [NSString stringWithFormat:@"GTMCarbonEvent 'foo ' %lu",
195       (unsigned long)kTestKind];
196  STAssertEqualObjects([event_ description], descString, nil);
197}
198@end
199
200@implementation GTMCarbonEventHandlerTest
201
202- (void)setUp {
203  handler_ = [[GTMCarbonEventHandler alloc] init];
204}
205
206- (void)tearDown {
207  [handler_ release];
208}
209
210- (void)testEventTarget {
211  STAssertNULL([handler_ eventTarget], nil);
212}
213
214- (void)testEventHandler {
215  [GTMUnitTestDevLogDebug expectPattern:
216   @"DebugAssert: GoogleToolboxForMac: event CantUseParams .*"];
217  STAssertErr([handler_ handleEvent:nil handler:nil],
218              (long)eventNotHandledErr, nil);
219}
220
221- (void)testDelegate {
222  [handler_ setDelegate:self];
223  STAssertEqualObjects([handler_ delegate], self, nil);
224  [handler_ setDelegate:nil];
225  STAssertNil([handler_ delegate], nil);
226}
227
228
229- (void)testSetDelegate {
230  [self testDelegate];
231}
232
233@end
234
235@implementation GTMCarbonEventMonitorHandlerTest
236
237- (void)testEventHandler {
238  GTMCarbonEventMonitorHandler *monitor
239    = [GTMCarbonEventMonitorHandler sharedEventMonitorHandler];
240  STAssertEquals([monitor eventTarget], GetEventMonitorTarget(), nil);
241}
242
243@end
244
245#if (MAC_OS_X_VERSION_MAX_ALLOWED == MAC_OS_X_VERSION_10_5)
246// Accidentally marked as !LP64 in the 10.5sdk, it's back in the 10.6 sdk.
247// If you remove this decl, please remove it from GTMCarbonEvent.m as well.
248extern EventTargetRef GetApplicationEventTarget(void);
249#endif  // (MAC_OS_X_VERSION_MAX_ALLOWED == MAC_OS_X_VERSION_10_5)
250
251@implementation GTMCarbonEventApplicationEventHandlerTest
252
253- (void)testEventHandler {
254  GTMCarbonEventApplicationEventHandler *handler
255    = [GTMCarbonEventApplicationEventHandler sharedApplicationEventHandler];
256  STAssertEquals([handler eventTarget], GetApplicationEventTarget(), nil);
257}
258
259@end
260
261@implementation GTMCarbonEventDispatcherHandlerTest
262
263- (void)setUp {
264  hotKeyHit_ = [[GTMUnitTestingBooleanRunLoopContext alloc] init];
265}
266
267- (void)tearDown {
268  [hotKeyHit_ release];
269  hotKeyHit_ = nil;
270}
271
272- (void)testEventHandler {
273  GTMCarbonEventDispatcherHandler *dispatcher
274    = [GTMCarbonEventDispatcherHandler sharedEventDispatcherHandler];
275  STAssertEquals([dispatcher eventTarget], GetEventDispatcherTarget(), nil);
276}
277
278- (void)hitHotKey:(GTMCarbonHotKey *)key {
279  STAssertEqualObjects([key userInfo], self, nil);
280  [hotKeyHit_ setShouldStop:YES];
281}
282
283- (void)hitExceptionalHotKey:(GTMCarbonHotKey *)key {
284  STAssertEqualObjects([key userInfo], self, nil);
285  [hotKeyHit_ setShouldStop:YES];
286  [NSException raise:@"foo" format:@"bar"];
287}
288
289- (void)testRegisterHotKeyModifiersTargetActionWhenPressed {
290
291  // This test can't be run if the screen saver is active because the security
292  // agent blocks us from sending events via remote operations
293  if (![GTMAppKitUnitTestingUtilities isScreenSaverActive]) {
294    GTMCarbonEventDispatcherHandler *dispatcher
295      = [GTMCarbonEventDispatcherHandler sharedEventDispatcherHandler];
296    STAssertNotNil(dispatcher, @"Unable to acquire singleton");
297    UInt32 keyMods = (NSShiftKeyMask | NSControlKeyMask
298                      | NSAlternateKeyMask | NSCommandKeyMask);
299    [GTMUnitTestDevLogDebug expectPattern:@"DebugAssert: GoogleToolboxForMac: "
300     @"newKey CantCreateKey .*"];
301    STAssertNil([dispatcher registerHotKey:0x5
302                                 modifiers:keyMods
303                                    target:nil
304                                    action:nil
305                                  userInfo:nil
306                               whenPressed:YES],
307                 @"Shouldn't have created hotkey");
308    GTMCarbonHotKey *hotKey = [dispatcher registerHotKey:0x5
309                                               modifiers:keyMods
310                                                  target:self
311                                                  action:@selector(hitHotKey:)
312                                                userInfo:self
313                                             whenPressed:YES];
314    STAssertNotNil(hotKey, @"Unable to create hotkey");
315
316    // Post the hotkey combo to the event queue. If everything is working
317    // correctly hitHotKey: should get called, and hotKeyHit_ will be set for
318    // us.  We run the event loop for a set amount of time waiting for this to
319    // happen.
320    [GTMAppKitUnitTestingUtilities postTypeCharacterEvent:'g' modifiers:keyMods];
321    STAssertTrue([NSApp gtm_runUpToSixtySecondsWithContext:hotKeyHit_], nil);
322    [dispatcher unregisterHotKey:hotKey];
323  }
324}
325
326- (void)testRegisterHotKeyModifiersTargetActionWhenPressedException {
327
328  // This test can't be run if the screen saver is active because the security
329  // agent blocks us from sending events via remote operations
330  if (![GTMAppKitUnitTestingUtilities isScreenSaverActive]) {
331    GTMCarbonEventDispatcherHandler *dispatcher
332      = [GTMCarbonEventDispatcherHandler sharedEventDispatcherHandler];
333    STAssertNotNil(dispatcher, @"Unable to acquire singleton");
334    UInt32 keyMods = (NSShiftKeyMask | NSControlKeyMask
335                      | NSAlternateKeyMask | NSCommandKeyMask);
336    GTMCarbonHotKey *hotKey
337      = [dispatcher registerHotKey:0x5
338                         modifiers:keyMods
339                            target:self
340                            action:@selector(hitExceptionalHotKey:)
341                          userInfo:self
342                       whenPressed:YES];
343    STAssertNotNil(hotKey, @"Unable to create hotkey");
344
345    // Post the hotkey combo to the event queue. If everything is working
346    // correctly hitHotKey: should get called, and hotKeyHit_ will be set for
347    // us.  We run the event loop for a set amount of time waiting for this to
348    // happen.
349    [GTMAppKitUnitTestingUtilities postTypeCharacterEvent:'g' modifiers:keyMods];
350    [GTMUnitTestDevLog expectString:@"Exception fired in hotkey: foo (bar)"];
351    STAssertTrue([NSApp gtm_runUpToSixtySecondsWithContext:hotKeyHit_], nil);
352    [dispatcher unregisterHotKey:hotKey];
353  }
354}
355
356- (void)testKeyModifiers {
357  struct {
358    NSUInteger cocoaKey_;
359    UInt32 carbonKey_;
360  } keyMap[] = {
361    { NSAlphaShiftKeyMask, alphaLock},
362    { NSShiftKeyMask, shiftKey},
363    { NSControlKeyMask, controlKey},
364    { NSAlternateKeyMask, optionKey},
365    { NSCommandKeyMask, cmdKey},
366  };
367  size_t combos = pow(2, sizeof(keyMap) / sizeof(keyMap[0]));
368  for (size_t i = 0; i < combos; i++) {
369    NSUInteger cocoaMods = 0;
370    UInt32 carbonMods = 0;
371    for (size_t j = 0; j < 32 && j < sizeof(keyMap) / sizeof(keyMap[0]); j++) {
372      if (i & 1 << j) {
373        cocoaMods |= keyMap[j].cocoaKey_;
374        carbonMods |= keyMap[j].carbonKey_;
375      }
376    }
377    STAssertEquals(GTMCocoaToCarbonKeyModifiers(cocoaMods), carbonMods, nil);
378    STAssertEquals(GTMCarbonToCocoaKeyModifiers(carbonMods), cocoaMods, nil);
379  }
380}
381
382
383@end
384