PageRenderTime 145ms CodeModel.GetById 36ms app.highlight 104ms RepoModel.GetById 2ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMNSAppleScript+HandlerTest.m

http://macfuse.googlecode.com/
Objective C | 504 lines | 410 code | 47 blank | 47 comment | 3 complexity | 97245ab7590cf15a0374a6a9e901c564 MD5 | raw file
  1//
  2//  GTMNSAppleScript+HandlerTest.m
  3//
  4//  Copyright 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 <Carbon/Carbon.h>
 21#import "GTMNSAppleScript+Handler.h"
 22#import "GTMNSAppleEventDescriptor+Foundation.h"
 23#import "GTMUnitTestDevLog.h"
 24#import "GTMSystemVersion.h"
 25#import "GTMFourCharCode.h"
 26
 27@protocol ScriptInterface
 28- (id)test;
 29- (id)testReturnParam:(id)param;
 30- (id)testAddParams:(id)param1 :(id)param2;
 31@end
 32
 33@interface GTMNSAppleScript_HandlerTest : GTMTestCase {
 34  NSAppleScript *script_; 
 35}
 36@end
 37
 38@implementation GTMNSAppleScript_HandlerTest
 39
 40- (void)setUp {
 41  NSBundle *bundle
 42    = [NSBundle bundleForClass:[GTMNSAppleScript_HandlerTest class]];
 43  STAssertNotNil(bundle, nil);
 44  NSString *path = [bundle pathForResource:@"GTMNSAppleEvent+HandlerTest" 
 45                                    ofType:@"scpt"
 46                               inDirectory:@"Scripts"];
 47  STAssertNotNil(path, [bundle description]);
 48  NSDictionary *error = nil;
 49  script_ 
 50    = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]
 51                                             error:&error];
 52  STAssertNotNil(script_, [error description]);
 53  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
 54}
 55
 56- (void)tearDown {
 57  [script_ release];
 58  script_ = nil;
 59}
 60
 61- (void)testExecuteAppleEvent {
 62  NSString *source = @"on test()\nreturn 1\nend test";
 63  NSAppleScript *script 
 64    = [[[NSAppleScript alloc] initWithSource:source] autorelease];
 65  STAssertNotNil(script, nil);
 66  NSDictionary *error = nil;
 67  NSAppleEventDescriptor *desc = [script gtm_executePositionalHandler:@"test" 
 68                                                           parameters:nil 
 69                                                                error:&error];
 70  STAssertNotNil(desc, [error description]);
 71  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
 72  STAssertEquals([desc gtm_objectValue], [NSNumber numberWithInt:1], nil);
 73  
 74  // bogus script
 75  source = @"adf872345ba asdf asdf gr";
 76  script = [[[NSAppleScript alloc] initWithSource:source] autorelease];
 77  STAssertNotNil(script, nil);
 78  desc = [script gtm_executePositionalHandler:@"test" 
 79                                   parameters:nil 
 80                                        error:&error];
 81  STAssertNil(desc, nil);
 82  STAssertNotNil(error, @"Error should not be nil");
 83}
 84
 85- (void)testHandlerNoParamsNoReturn {
 86  NSDictionary *error = nil;
 87  NSAppleEventDescriptor *desc = [script_ gtm_executePositionalHandler:@"test" 
 88                                                            parameters:nil 
 89                                                                 error:&error];
 90  STAssertNotNil(desc, [error description]);
 91  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
 92  STAssertEquals([desc descriptorType], (DescType)typeNull, nil);
 93  desc = [script_ gtm_executePositionalHandler:@"test" 
 94                                    parameters:[NSArray array] 
 95                                         error:&error];
 96  STAssertNotNil(desc, [error description]);
 97  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
 98  STAssertEquals([desc descriptorType], (DescType)typeNull, nil);
 99  
100  //Applescript doesn't appear to get upset about extra params
101  desc = [script_ gtm_executePositionalHandler:@"test" 
102                                    parameters:[NSArray arrayWithObject:@"foo"] 
103                                         error:&error];
104  STAssertNotNil(desc, [error description]);
105  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
106  STAssertEquals([desc descriptorType], (DescType)typeNull, nil);
107}
108  
109- (void)testHandlerNoParamsWithReturn {
110  NSDictionary *error = nil;
111  NSAppleEventDescriptor *desc 
112    = [script_ gtm_executePositionalHandler:@"testReturnOne" 
113                                 parameters:nil 
114                                      error:&error];
115  STAssertNotNil(desc, [error description]);
116  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
117  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
118  STAssertEquals([desc int32Value], (SInt32)1, nil);
119  desc = [script_ gtm_executePositionalHandler:@"testReturnOne" 
120                                    parameters:[NSArray array] 
121                                         error:&error];
122  STAssertNotNil(desc, [error description]);
123  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
124  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
125  STAssertEquals([desc int32Value], (SInt32)1, nil);
126  
127  //Applescript doesn't appear to get upset about extra params
128  desc = [script_ gtm_executePositionalHandler:@"testReturnOne" 
129                                    parameters:[NSArray arrayWithObject:@"foo"] 
130                                         error:&error];
131  STAssertNotNil(desc, [error description]);
132  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
133  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
134  STAssertEquals([desc int32Value], (SInt32)1, nil);
135}
136
137- (void)testHandlerOneParamWithReturn {
138  NSDictionary *error = nil;
139  // Note case change in executeHandler call
140  NSAppleEventDescriptor *desc 
141    = [script_ gtm_executePositionalHandler:@"testreturnParam" 
142                                 parameters:nil 
143                                      error:&error];
144  STAssertNil(desc, @"Desc should by nil %@", desc);
145  STAssertNotNil(error, nil);
146  error = nil;
147  
148  desc = [script_ gtm_executePositionalHandler:@"testReturnParam" 
149                                    parameters:[NSArray array] 
150                                         error:&error];
151  STAssertNil(desc, @"Desc should by nil %@", desc);
152  
153  // Verify that our error handling is working correctly.
154  STAssertEquals([[error allKeys] count], (NSUInteger)6, @"%@", error);
155  STAssertNotNil([error objectForKey:GTMNSAppleScriptErrorOffendingObject], 
156                 @"%@", error);
157  STAssertNotNil([error objectForKey:GTMNSAppleScriptErrorPartialResult], 
158                 @"%@", error);
159  
160  error = nil;
161  
162  desc = [script_ gtm_executePositionalHandler:@"testReturnParam" 
163                                    parameters:[NSArray arrayWithObject:@"foo"]
164                                         error:&error];
165  STAssertNotNil(desc, [error description]);
166  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
167  STAssertEquals([desc descriptorType], (DescType)typeUnicodeText, nil);
168  STAssertEqualObjects([desc gtm_objectValue], @"foo", nil);
169}
170
171- (void)testHandlerTwoParamsWithReturn {
172  NSDictionary *error = nil;
173  // Note case change in executeHandler call
174  // Test case and empty params
175  NSAppleEventDescriptor *desc 
176    = [script_ gtm_executePositionalHandler:@"testADDPArams" 
177                                 parameters:nil 
178                                      error:&error];
179  STAssertNil(desc, @"Desc should by nil %@", desc);
180  STAssertNotNil(error, nil);
181  
182  // Test empty params
183  error = nil;
184  desc = [script_ gtm_executePositionalHandler:@"testAddParams" 
185                                    parameters:[NSArray array] 
186                                         error:&error];
187  STAssertNil(desc, @"Desc should by nil %@", desc);
188  STAssertNotNil(error, nil);
189  
190  error = nil;
191  NSArray *args = [NSArray arrayWithObjects:
192    [NSNumber numberWithInt:1],
193    [NSNumber numberWithInt:2],
194    nil];
195  desc = [script_ gtm_executePositionalHandler:@"testAddParams" 
196                                    parameters:args 
197                                         error:&error];
198  STAssertNotNil(desc, [error description]);
199  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
200  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
201  STAssertEquals([desc int32Value], (SInt32)3, nil);
202
203  // Test bad params
204  error = nil;
205  args = [NSArray arrayWithObjects:
206    @"foo",
207    @"bar",
208    nil];
209  desc = [script_ gtm_executePositionalHandler:@"testAddParams" 
210                                    parameters:args 
211                                         error:&error];
212  STAssertNil(desc, @"Desc should by nil %@", desc);
213  STAssertNotNil(error, nil);
214
215  // Test too many params. Currently Applescript allows this so it should pass
216  error = nil;
217  args = [NSArray arrayWithObjects:
218    [NSNumber numberWithInt:1],
219    [NSNumber numberWithInt:2],
220    [NSNumber numberWithInt:3],
221    nil];
222  desc = [script_ gtm_executePositionalHandler:@"testAddParams" 
223                                    parameters:args 
224                                         error:&error];
225  STAssertNotNil(desc, [error description]);
226  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
227  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
228  STAssertEquals([desc int32Value], (SInt32)3, nil);}
229
230- (void)testLabeledHandler {
231  NSDictionary *error = nil;
232  AEKeyword labels[] = { keyDirectObject, 
233                         keyASPrepositionOnto, 
234                         keyASPrepositionGiven };
235  id params[3];
236  params[0] = [NSNumber numberWithInt:1];
237  params[1] = [NSNumber numberWithInt:3];
238  params[2] = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:4] 
239                                          forKey:@"othervalue"];
240  
241  NSAppleEventDescriptor *desc 
242    = [script_ gtm_executeLabeledHandler:@"testAdd" 
243                                  labels:labels
244                              parameters:params
245                                   count:sizeof(params) / sizeof(id)
246                                                              error:&error];
247  STAssertNotNil(desc, [error description]);
248  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
249  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
250  STAssertEquals([desc int32Value], (SInt32)8, nil);
251  
252  // Test too many params. Currently Applescript allows this so it should pass
253  AEKeyword labels2[] = { keyDirectObject, 
254                         keyASPrepositionOnto, 
255                         keyASPrepositionBetween,
256                         keyASPrepositionGiven };
257  id params2[4];
258  params2[0] = [NSNumber numberWithInt:1];
259  params2[1] = [NSNumber numberWithInt:3];
260  params2[2] = [NSNumber numberWithInt:5];
261  params2[3] = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:4] 
262                                            forKey:@"othervalue"];
263
264  error = nil;
265  desc = [script_ gtm_executeLabeledHandler:@"testAdd" 
266                                     labels:labels2
267                                 parameters:params2
268                                      count:sizeof(params2) / sizeof(id)
269                                      error:&error];
270  STAssertNotNil(desc, [error description]);
271  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
272  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
273  STAssertEquals([desc int32Value], (SInt32)8, nil);}
274
275- (void)testHandlers {
276  NSSet *handlers = [script_ gtm_handlers];
277  NSSet *expected = [NSSet setWithObjects:
278                     @"aevtpdoc",
279                     @"test",
280                     @"testreturnone",
281                     @"testreturnparam",
282                     @"testaddparams",
283                     @"testadd",
284                     @"testgetscript",
285                     nil];
286  if ([GTMSystemVersion isSnowLeopardOrGreater]) {
287    // Workaround for bug in SnowLeopard
288    // rdar://66688601 OSAGetHandlersNames returns names in camelcase instead
289    // of smallcaps.
290    handlers = [handlers valueForKey:@"lowercaseString"];
291  }
292  STAssertEqualObjects(handlers, expected, @"Unexpected handlers?");
293}
294
295- (void)testInheritedHandlers {
296  NSDictionary *error = nil;
297  NSAppleEventDescriptor *desc 
298    = [script_ gtm_executePositionalHandler:@"testGetScript" 
299                                 parameters:nil 
300                                      error:&error];
301  STAssertNil(error, nil);
302  STAssertNotNil(desc, nil);
303  NSAppleScript *script = [desc gtm_objectValue];
304  STAssertTrue([script isKindOfClass:[NSAppleScript class]], nil);
305  error = nil;
306  desc = [script gtm_executePositionalHandler:@"parentTestScriptFunc"
307                                   parameters:nil error:&error];
308  STAssertNil(error, nil);
309  STAssertNotNil(desc, nil);
310  NSString *value = [desc gtm_objectValue];
311  STAssertEqualObjects(value, @"parent", nil);
312}
313
314- (void)testProperties {
315  NSDictionary *error = nil;
316  NSAppleEventDescriptor *desc 
317    = [script_ gtm_executePositionalHandler:@"testGetScript" 
318                                 parameters:nil 
319                                      error:&error];
320  STAssertNil(error, nil);
321  STAssertNotNil(desc, nil);
322  NSAppleScript *script = [desc gtm_objectValue];
323  STAssertTrue([script isKindOfClass:[NSAppleScript class]], nil);
324  
325  NSSet *properties = [script gtm_properties];
326  NSSet *expected 
327    = [NSSet setWithObjects:
328       @"testscriptproperty",
329       @"parenttestscriptproperty",
330       @"foo",
331       @"testscript",
332       @"parenttestscript",
333       @"asdscriptuniqueidentifier",
334       [GTMFourCharCode fourCharCodeWithFourCharCode:pVersion],
335       [GTMFourCharCode fourCharCodeWithFourCharCode:pASPrintDepth],
336       [GTMFourCharCode fourCharCodeWithFourCharCode:pASTopLevelScript],
337       [GTMFourCharCode fourCharCodeWithFourCharCode:pASResult],
338       [GTMFourCharCode fourCharCodeWithFourCharCode:pASMinutes],
339       [GTMFourCharCode fourCharCodeWithFourCharCode:pASDays],
340       // No constant for linefeed in the 10.5 sdk
341       // Radar 6132775 Need a constant for the Applescript Property 'lnfd'
342       [GTMFourCharCode fourCharCodeWithFourCharCode:'lnfd'],
343       [GTMFourCharCode fourCharCodeWithFourCharCode:pASPi],
344       [GTMFourCharCode fourCharCodeWithFourCharCode:pASReturn],
345       [GTMFourCharCode fourCharCodeWithFourCharCode:pASSpace],
346       [GTMFourCharCode fourCharCodeWithFourCharCode:pASPrintLength],
347       [GTMFourCharCode fourCharCodeWithFourCharCode:pASQuote],
348       [GTMFourCharCode fourCharCodeWithFourCharCode:pASWeeks],
349       [GTMFourCharCode fourCharCodeWithFourCharCode:pTextItemDelimiters],
350       // Applescript properties should be pASSeconds, but
351       // on 10.5.4/10.5.5 it is actually using cSeconds.
352       // Radar 6132696 Applescript root level property is cSeconds 
353       // instead of pASSeconds
354       [GTMFourCharCode fourCharCodeWithFourCharCode:cSeconds],
355       [GTMFourCharCode fourCharCodeWithFourCharCode:pASHours],
356       [GTMFourCharCode fourCharCodeWithFourCharCode:pASTab],
357       nil];
358  if ([GTMSystemVersion isSnowLeopardOrGreater]) {
359    // Workaround for bug in SnowLeopard
360    // rdar://6289077 OSAGetPropertyNames returns names in camelcase instead
361    // of lowercase.
362    id obj;
363    NSMutableSet *properties2 = [NSMutableSet set];
364    GTM_FOREACH_OBJECT(obj, properties) {
365      if ([obj isKindOfClass:[NSString class]]) {
366        obj = [obj lowercaseString];
367      }
368      [properties2 addObject:obj];
369    }
370    properties = properties2;
371  }
372  STAssertEqualObjects(properties, expected, @"Unexpected properties?");
373  id value = [script gtm_valueForProperty:@"testScriptProperty"];
374  STAssertEqualObjects(value, [NSNumber numberWithInt:5], @"bad property?");
375  BOOL goodSet = [script gtm_setValue:@"bar" 
376                          forProperty:@"foo" 
377                      addingDefinition:NO];
378  STAssertTrue(goodSet, @"Couldn't set property");
379  
380  // Test local set
381  value = [script gtm_valueForProperty:@"foo"];
382  STAssertEqualObjects(value, @"bar", @"bad property?");
383
384  // Test inherited set
385  value = [script_ gtm_valueForProperty:@"foo"];
386  STAssertEqualObjects(value, @"bar", @"bad property?");
387
388  [GTMUnitTestDevLog expectPattern:@"Unable to setValue:bar forProperty:"
389   "\\(null\\) from <NSAppleScript: 0x[0-9a-f]+> \\(-50\\)"];
390  goodSet = [script gtm_setValue:@"bar" 
391                     forProperty:nil
392                 addingDefinition:NO];
393  STAssertFalse(goodSet, @"Set property?");
394
395  [GTMUnitTestDevLog expectPattern:@"Unable to setValue:bar forProperty:3"
396   " from <NSAppleScript: 0x[0-9a-f]+> \\(-50\\)"];
397  goodSet = [script gtm_setValue:@"bar"
398                     forProperty:[NSNumber numberWithInt:3]
399                 addingDefinition:YES];
400  STAssertFalse(goodSet, @"Set property?");
401  
402  
403  [GTMUnitTestDevLog expectPattern:@"Unable to get valueForProperty:gargle "
404   "from <NSAppleScript: 0x[0-9a-f]+> \\(-1753\\)"];
405  value = [script gtm_valueForProperty:@"gargle"];
406  STAssertNil(value, @"Property named gargle?");
407  
408  goodSet = [script_ gtm_setValue:@"wow"
409                      forProperty:@"addedProperty" 
410                  addingDefinition:YES];
411  STAssertTrue(goodSet, @"Unable to addProperty");
412  
413  value = [script gtm_valueForProperty:@"addedProperty"];
414  STAssertNotNil(value, nil);
415  STAssertEqualObjects(value, @"wow", nil);
416  
417  // http://www.straightdope.com/classics/a3_341.html
418  NSNumber *newPI = [NSNumber numberWithInt:3];
419  goodSet = [script gtm_setValue:newPI
420                  forPropertyEnum:pASPi
421                 addingDefinition:NO];
422  STAssertTrue(goodSet, @"Unable to set property");
423  value = [script_ gtm_valueForPropertyEnum:pASPi];
424  STAssertNotNil(value, nil);
425  STAssertEqualObjects(value, newPI, @"bad property");
426}
427
428- (void)testFailures {
429  NSDictionary *error = nil;
430  NSAppleEventDescriptor *desc 
431    = [script_ gtm_executePositionalHandler:@"noSuchTest" 
432                                 parameters:nil 
433                                      error:&error];
434  STAssertNil(desc, nil);
435  STAssertNotNil(error, nil);
436
437  // Test with empty handler name
438  error = nil;
439  desc = [script_ gtm_executePositionalHandler:@"" 
440                                    parameters:[NSArray array] 
441                                         error:&error];
442  STAssertNil(desc, nil);
443  STAssertNotNil(error, nil);
444  
445  // Test with nil handler
446  error = nil;
447  desc = [script_ gtm_executePositionalHandler:nil
448                                    parameters:[NSArray array] 
449                                         error:&error];
450  STAssertNil(desc, nil);
451  STAssertNotNil(error, nil);
452  
453  // Test with nil handler and nil error
454  desc = [script_ gtm_executePositionalHandler:nil
455                                    parameters:nil 
456                                         error:nil];
457  STAssertNil(desc, nil);
458  
459  // Test with a bad script
460  NSAppleScript *script 
461    = [[[NSAppleScript alloc] initWithSource:@"david hasselhoff"] autorelease];
462  [GTMUnitTestDevLog expectPattern:@"Unable to compile script: .*"];
463  [GTMUnitTestDevLog expectString:@"Unable to coerce script -2147450879"];
464  NSSet *handlers = [script gtm_handlers];
465  STAssertEquals([handlers count], (NSUInteger)0, @"Should have no handlers");
466  [GTMUnitTestDevLog expectPattern:@"Unable to compile script: .*"];
467  [GTMUnitTestDevLog expectString:@"Unable to coerce script -2147450879"];
468  NSSet *properties = [script gtm_properties];
469  STAssertEquals([properties count], 
470                 (NSUInteger)0, 
471                 @"Should have no properties");
472  [GTMUnitTestDevLog expectPattern:@"Unable to compile script: .*"];
473  [GTMUnitTestDevLog expectString:@"Unable to get script info about "
474   @"open handler -2147450879"];
475  STAssertFalse([script gtm_hasOpenDocumentsHandler],
476                @"Has an opendoc handler?");
477}
478
479- (void)testScriptDescriptors {
480  NSAppleEventDescriptor *desc = [script_ gtm_appleEventDescriptor];
481  STAssertNotNil(desc, @"Couldn't make a script desc");
482  NSAppleScript *script = [desc gtm_objectValue];
483  STAssertNotNil(script, @"Couldn't get a script back");
484  NSSet *handlers = [script gtm_handlers];
485  STAssertNotNil(handlers, @"Couldn't get handlers");
486}
487
488- (void)testOpenHandler {
489  STAssertFalse([script_ gtm_hasOpenDocumentsHandler], nil);
490  id script = [script_ gtm_valueForProperty:@"testscript"];
491  STAssertNotNil(script, nil);
492  STAssertTrue([script gtm_hasOpenDocumentsHandler], nil);
493}
494
495- (void)testForwarding {
496  id<ScriptInterface> foo = (id<ScriptInterface>)script_;
497  [foo test];
498  NSNumber *val = [foo testReturnParam:[NSNumber numberWithInt:2]];
499  STAssertEquals([val intValue], 2, @"should be 2");
500  val = [foo testAddParams:[NSNumber numberWithInt:2] 
501                          :[NSNumber numberWithInt:3]];
502  STAssertEquals([val intValue], 5, @"should be 5");
503}
504@end