/core/externals/google-toolbox-for-mac/Foundation/GTMNSAppleEventDescriptor+FoundationTest.m

http://macfuse.googlecode.com/ · Objective C · 643 lines · 529 code · 63 blank · 51 comment · 6 complexity · 5738d6537e792434212b44e99bfdba4f MD5 · raw file

  1. //
  2. // GTMNSAppleEventDescriptor+FoundationTest.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. #import "GTMSenTestCase.h"
  19. #import <Carbon/Carbon.h>
  20. #import "GTMNSAppleEventDescriptor+Foundation.h"
  21. #import "GTMFourCharCode.h"
  22. #import "GTMUnitTestDevLog.h"
  23. @interface GTMNSAppleEventDescriptor_TestObject : NSObject
  24. @end
  25. @implementation GTMNSAppleEventDescriptor_TestObject
  26. - (NSAppleEventDescriptor*)gtm_appleEventDescriptor {
  27. return nil;
  28. }
  29. @end
  30. @interface GTMNSAppleEventDescriptor_FoundationTest : GTMTestCase {
  31. BOOL gotEvent_;
  32. }
  33. - (void)handleEvent:(NSAppleEventDescriptor*)event
  34. withReply:(NSAppleEventDescriptor*)reply;
  35. - (void)handleEvent:(NSAppleEventDescriptor*)event
  36. withError:(NSAppleEventDescriptor*)reply;
  37. @end
  38. @implementation GTMNSAppleEventDescriptor_FoundationTest
  39. - (void)testRegisterSelectorForTypesCount {
  40. // Weird edge casey stuff.
  41. // + (void)registerSelector:(SEL)selector
  42. // forTypes:(DescType*)types count:(int)count
  43. // is tested heavily by the other NSAppleEventDescriptor+foo categories.
  44. DescType type;
  45. [NSAppleEventDescriptor gtm_registerSelector:nil
  46. forTypes:&type count:1];
  47. [NSAppleEventDescriptor gtm_registerSelector:@selector(initListDescriptor)
  48. forTypes:nil count:1];
  49. [NSAppleEventDescriptor gtm_registerSelector:@selector(initListDescriptor)
  50. forTypes:&type count:0];
  51. // Test the duplicate case
  52. [NSAppleEventDescriptor gtm_registerSelector:@selector(initListDescriptor)
  53. forTypes:&type count:1];
  54. [GTMUnitTestDevLog expectPattern:@"initListDescriptor being replaced with "
  55. "initListDescriptor exists for type: [0-9]+"];
  56. [NSAppleEventDescriptor gtm_registerSelector:@selector(initListDescriptor)
  57. forTypes:&type count:1];
  58. }
  59. - (void)testObjectValue {
  60. // - (void)testObjectValue is tested heavily by the other
  61. // NSAppleEventDescriptor+foo categories.
  62. long data = 1;
  63. // v@#f is just a bogus descriptor type that we don't recognize.
  64. NSAppleEventDescriptor *desc
  65. = [NSAppleEventDescriptor descriptorWithDescriptorType:'v@#f'
  66. bytes:&data
  67. length:sizeof(data)];
  68. id value = [desc gtm_objectValue];
  69. STAssertNil(value, nil);
  70. }
  71. - (void)testAppleEventDescriptor {
  72. // - (NSAppleEventDescriptor*)appleEventDescriptor is tested heavily by the
  73. // other NSAppleEventDescriptor+foo categories.
  74. NSAppleEventDescriptor *desc = [self gtm_appleEventDescriptor];
  75. STAssertNotNil(desc, nil);
  76. STAssertEquals([desc descriptorType], (DescType)typeUnicodeText, nil);
  77. }
  78. - (void)testDescriptorWithArrayAndArrayValue {
  79. // Test empty array
  80. NSAppleEventDescriptor *desc = [[NSArray array] gtm_appleEventDescriptor];
  81. STAssertNotNil(desc, nil);
  82. STAssertEquals([desc numberOfItems], (NSInteger)0, nil);
  83. // Complex array
  84. NSArray *array = [NSArray arrayWithObjects:
  85. [NSNumber numberWithInt:4],
  86. @"foo",
  87. [NSNumber numberWithInt:2],
  88. @"bar",
  89. [NSArray arrayWithObjects:
  90. @"bam",
  91. [NSArray arrayWithObject:[NSNumber numberWithFloat:4.2f]],
  92. nil],
  93. nil];
  94. STAssertNotNil(array, nil);
  95. desc = [array gtm_appleEventDescriptor];
  96. STAssertNotNil(desc, nil);
  97. NSArray *array2 = [desc gtm_objectValue];
  98. STAssertNotNil(array2, nil);
  99. NSArray *array3 = [desc gtm_arrayValue];
  100. STAssertNotNil(array3, nil);
  101. STAssertTrue([array isEqualToArray:array2],
  102. @"array: %@\narray2: %@\ndesc: %@",
  103. [array description], [array2 description], [desc description]);
  104. STAssertTrue([array2 isEqualToArray:array3],
  105. @"array: %@\narray2: %@\ndesc: %@",
  106. [array description], [array2 description], [desc description]);
  107. // Test a single object
  108. array = [NSArray arrayWithObject:@"foo"];
  109. desc = [NSAppleEventDescriptor descriptorWithString:@"foo"];
  110. STAssertNotNil(desc, nil);
  111. array2 = [desc gtm_arrayValue];
  112. STAssertTrue([array isEqualToArray:array2],
  113. @"array: %@\narray2: %@\ndesc: %@",
  114. [array description], [array2 description], [desc description]);
  115. // Something that doesn't know how to register itself.
  116. GTMNSAppleEventDescriptor_TestObject *obj
  117. = [[[GTMNSAppleEventDescriptor_TestObject alloc] init] autorelease];
  118. [GTMUnitTestDevLog expectPattern:@"Unable to create Apple Event Descriptor for .*"];
  119. desc = [[NSArray arrayWithObject:obj] gtm_appleEventDescriptor];
  120. STAssertNil(desc, @"Should be nil");
  121. // A list containing something we don't know how to deal with
  122. desc = [NSAppleEventDescriptor listDescriptor];
  123. NSAppleEventDescriptor *desc2
  124. = [NSAppleEventDescriptor descriptorWithDescriptorType:'@!@#'
  125. bytes:&desc
  126. length:sizeof(desc)];
  127. [GTMUnitTestDevLog expectPattern:@"Unknown type of descriptor "
  128. "<NSAppleEventDescriptor: '@!@#'\\(\\$[0-9A-F]*\\$\\)>"];
  129. [desc insertDescriptor:desc2 atIndex:0];
  130. array = [desc gtm_objectValue];
  131. STAssertEquals([array count], (NSUInteger)0, @"Should have 0 items");
  132. }
  133. - (void)testDescriptorWithDictionaryAndDictionaryValue {
  134. // Test empty dictionary
  135. NSAppleEventDescriptor *desc
  136. = [[NSDictionary dictionary] gtm_appleEventDescriptor];
  137. STAssertNotNil(desc, nil);
  138. STAssertEquals([desc numberOfItems], (NSInteger)0, nil);
  139. // Complex dictionary
  140. NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  141. @"fooobject",
  142. @"fookey",
  143. @"barobject",
  144. @"barkey",
  145. [NSDictionary dictionaryWithObjectsAndKeys:
  146. @"january",
  147. [GTMFourCharCode fourCharCodeWithFourCharCode:cJanuary],
  148. @"february",
  149. [GTMFourCharCode fourCharCodeWithFourCharCode:cFebruary],
  150. nil],
  151. @"dictkey",
  152. nil];
  153. STAssertNotNil(dictionary, nil);
  154. desc = [dictionary gtm_appleEventDescriptor];
  155. STAssertNotNil(desc, nil);
  156. NSDictionary *dictionary2 = [desc gtm_objectValue];
  157. STAssertNotNil(dictionary2, nil);
  158. NSDictionary *dictionary3 = [desc gtm_dictionaryValue];
  159. STAssertNotNil(dictionary3, nil);
  160. STAssertEqualObjects(dictionary, dictionary2,
  161. @"desc: %@", [desc description]);
  162. STAssertEqualObjects(dictionary2, dictionary3,
  163. @"desc: %@", [desc description]);
  164. // Something that doesn't know how to register itself.
  165. GTMNSAppleEventDescriptor_TestObject *obj
  166. = [[[GTMNSAppleEventDescriptor_TestObject alloc] init] autorelease];
  167. [GTMUnitTestDevLog expectPattern:@"Unable to create Apple Event Descriptor for .*"];
  168. desc = [[NSDictionary dictionaryWithObject:obj
  169. forKey:@"foo"] gtm_appleEventDescriptor];
  170. STAssertNil(desc, @"Should be nil");
  171. GTMFourCharCode *fcc = [GTMFourCharCode fourCharCodeWithFourCharCode:cJanuary];
  172. desc = [[NSDictionary dictionaryWithObject:obj
  173. forKey:fcc] gtm_appleEventDescriptor];
  174. STAssertNil(desc, @"Should be nil");
  175. // A list containing something we don't know how to deal with
  176. desc = [NSAppleEventDescriptor recordDescriptor];
  177. NSAppleEventDescriptor *desc2
  178. = [NSAppleEventDescriptor descriptorWithDescriptorType:'@!@#'
  179. bytes:&desc
  180. length:sizeof(desc)];
  181. [desc setDescriptor:desc2 forKeyword:cJanuary];
  182. [GTMUnitTestDevLog expectPattern:@"Unknown type of descriptor "
  183. "<NSAppleEventDescriptor: '@!@#'\\(\\$[0-9A-F]+\\$\\)>"];
  184. dictionary = [desc gtm_objectValue];
  185. STAssertEquals([dictionary count], (NSUInteger)0, @"Should have 0 items");
  186. // A bad dictionary
  187. dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  188. @"foo",
  189. [GTMFourCharCode fourCharCodeWithFourCharCode:'APPL'],
  190. @"bam",
  191. @"bar",
  192. nil];
  193. STAssertNotNil(dictionary, nil);
  194. // I cannot use expectString here to the exact string because interestingly
  195. // dictionaries in 64 bit enumerate in a different order from dictionaries
  196. // on 32 bit. This is the closest pattern I can match.
  197. [GTMUnitTestDevLog expectPattern:@"Keys must be homogenous .*"];
  198. desc = [dictionary gtm_appleEventDescriptor];
  199. STAssertNil(desc, nil);
  200. // Another bad dictionary
  201. dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  202. @"foo",
  203. [NSNumber numberWithInt:4],
  204. @"bam",
  205. @"bar",
  206. nil];
  207. STAssertNotNil(dictionary, nil);
  208. // I cannot use expectString here to the exact string because interestingly
  209. // dictionaries in 64 bit enumerate in a different order from dictionaries
  210. // on 32 bit. This is the closest pattern I can match.
  211. [GTMUnitTestDevLog expectPattern:@"Keys must be .*"];
  212. desc = [dictionary gtm_appleEventDescriptor];
  213. STAssertNil(desc, nil);
  214. // A bad descriptor
  215. desc = [NSAppleEventDescriptor recordDescriptor];
  216. STAssertNotNil(desc, @"");
  217. NSArray *array = [NSArray arrayWithObjects:@"foo", @"bar", @"bam", nil];
  218. STAssertNotNil(array, @"");
  219. NSAppleEventDescriptor *userRecord = [array gtm_appleEventDescriptor];
  220. STAssertNotNil(userRecord, @"");
  221. [desc setDescriptor:userRecord forKeyword:keyASUserRecordFields];
  222. [GTMUnitTestDevLog expectPattern:@"Got a key bam with no value in \\(.*"];
  223. dictionary = [desc gtm_objectValue];
  224. STAssertNil(dictionary, @"Should be nil");
  225. }
  226. - (void)testDescriptorWithNull {
  227. // Test Null
  228. NSNull *null = [NSNull null];
  229. NSAppleEventDescriptor *desc = [null gtm_appleEventDescriptor];
  230. STAssertNotNil(desc, nil);
  231. NSNull *null2 = [desc gtm_objectValue];
  232. STAssertNotNil(null2, nil);
  233. NSNull *null3 = [desc gtm_nullValue];
  234. STAssertNotNil(null2, nil);
  235. STAssertEqualObjects(null, null2,
  236. @"null: %@\null2: %@\ndesc: %@",
  237. [null description], [null2 description],
  238. [desc description]);
  239. STAssertEqualObjects(null, null3,
  240. @"null: %@\null3: %@\ndesc: %@",
  241. [null description], [null3 description],
  242. [desc description]);
  243. }
  244. - (void)testDescriptorWithString {
  245. // Test empty String
  246. NSAppleEventDescriptor *desc = [[NSString string] gtm_appleEventDescriptor];
  247. STAssertNotNil(desc, nil);
  248. // Test String
  249. NSString *string = @"Ratatouille!";
  250. desc = [string gtm_appleEventDescriptor];
  251. STAssertNotNil(desc, nil);
  252. NSString *string2 = [desc gtm_objectValue];
  253. STAssertNotNil(string2, nil);
  254. STAssertEqualObjects(string, string2,
  255. @"string: %@\nstring: %@\ndesc: %@",
  256. [string description], [string2 description], [desc description]);
  257. }
  258. - (void)testDescriptorWithNumberAndNumberValue {
  259. // There's really no good way to make this into a loop sadly due
  260. // to me having to pass a pointer of bytes to NSInvocation as an argument.
  261. // I want the compiler to convert my int to the appropriate type.
  262. NSNumber *original = [NSNumber numberWithBool:YES];
  263. STAssertNotNil(original, @"Value: YES");
  264. NSAppleEventDescriptor *desc = [original gtm_appleEventDescriptor];
  265. STAssertNotNil(desc, @"Value: YES");
  266. id returned = [desc gtm_objectValue];
  267. STAssertNotNil(returned, @"Value: YES");
  268. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: YES");
  269. STAssertEqualObjects(original, returned, @"Value: YES");
  270. desc = [desc coerceToDescriptorType:typeBoolean];
  271. NSNumber *number = [desc gtm_numberValue];
  272. STAssertEqualObjects(number, original, @"Value: YES");
  273. original = [NSNumber numberWithBool:NO];
  274. STAssertNotNil(original, @"Value: NO");
  275. desc = [original gtm_appleEventDescriptor];
  276. STAssertNotNil(desc, @"Value: NO");
  277. returned = [desc gtm_objectValue];
  278. STAssertNotNil(returned, @"Value: NO");
  279. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: NO");
  280. STAssertEqualObjects(original, returned, @"Value: NO");
  281. sranddev();
  282. double value = rand();
  283. original = [NSNumber numberWithChar:value];
  284. STAssertNotNil(original, @"Value: %g", value);
  285. desc = [original gtm_appleEventDescriptor];
  286. STAssertNotNil(desc, @"Value: %g", value);
  287. returned = [desc gtm_objectValue];
  288. STAssertNotNil(returned, @"Value: %g", value);
  289. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  290. STAssertEqualObjects(original, returned, @"Value: %g", value);
  291. value = rand();
  292. original = [NSNumber numberWithUnsignedChar:value];
  293. STAssertNotNil(original, @"Value: %g", value);
  294. desc = [original gtm_appleEventDescriptor];
  295. STAssertNotNil(desc, @"Value: %g", value);
  296. returned = [desc gtm_objectValue];
  297. STAssertNotNil(returned, @"Value: %g", value);
  298. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  299. STAssertEqualObjects(original, returned, @"Value: %g", value);
  300. value = rand();
  301. original = [NSNumber numberWithShort:value];
  302. STAssertNotNil(original, @"Value: %g", value);
  303. desc = [original gtm_appleEventDescriptor];
  304. STAssertNotNil(desc, @"Value: %g", value);
  305. returned = [desc gtm_objectValue];
  306. STAssertNotNil(returned, @"Value: %g", value);
  307. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  308. STAssertEqualObjects(original, returned, @"Value: %g", value);
  309. value = rand();
  310. original = [NSNumber numberWithUnsignedShort:value];
  311. STAssertNotNil(original, @"Value: %g", value);
  312. desc = [original gtm_appleEventDescriptor];
  313. STAssertNotNil(desc, @"Value: %g", value);
  314. returned = [desc gtm_objectValue];
  315. STAssertNotNil(returned, @"Value: %g", value);
  316. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  317. STAssertEqualObjects(original, returned, @"Value: %g", value);
  318. value = rand();
  319. original = [NSNumber numberWithInt:(int)value];
  320. STAssertNotNil(original, @"Value: %g", value);
  321. desc = [original gtm_appleEventDescriptor];
  322. STAssertNotNil(desc, @"Value: %g", value);
  323. returned = [desc gtm_objectValue];
  324. STAssertNotNil(returned, @"Value: %g", value);
  325. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  326. STAssertEqualObjects(original, returned, @"Value: %g", value);
  327. value = rand();
  328. original = [NSNumber numberWithUnsignedInt:(unsigned int)value];
  329. STAssertNotNil(original, @"Value: %g", value);
  330. desc = [original gtm_appleEventDescriptor];
  331. STAssertNotNil(desc, @"Value: %g", value);
  332. returned = [desc gtm_objectValue];
  333. STAssertNotNil(returned, @"Value: %g", value);
  334. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  335. STAssertEqualObjects(original, returned, @"Value: %g", value);
  336. value = rand();
  337. original = [NSNumber numberWithLong:value];
  338. STAssertNotNil(original, @"Value: %g", value);
  339. desc = [original gtm_appleEventDescriptor];
  340. STAssertNotNil(desc, @"Value: %g", value);
  341. returned = [desc gtm_objectValue];
  342. STAssertNotNil(returned, @"Value: %g", value);
  343. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  344. STAssertEqualObjects(original, returned, @"Value: %g", value);
  345. value = rand();
  346. original = [NSNumber numberWithUnsignedLong:value];
  347. STAssertNotNil(original, @"Value: %g", value);
  348. desc = [original gtm_appleEventDescriptor];
  349. STAssertNotNil(desc, @"Value: %g", value);
  350. returned = [desc gtm_objectValue];
  351. STAssertNotNil(returned, @"Value: %g", value);
  352. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  353. STAssertEqualObjects(original, returned, @"Value: %g", value);
  354. value = rand();
  355. original = [NSNumber numberWithLongLong:value];
  356. STAssertNotNil(original, @"Value: %g", value);
  357. desc = [original gtm_appleEventDescriptor];
  358. STAssertNotNil(desc, @"Value: %g", value);
  359. returned = [desc gtm_objectValue];
  360. STAssertNotNil(returned, @"Value: %g", value);
  361. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  362. STAssertEqualObjects(original, returned, @"Value: %g", value);
  363. value = rand();
  364. original = [NSNumber numberWithUnsignedLongLong:value];
  365. STAssertNotNil(original, @"Value: %g", value);
  366. desc = [original gtm_appleEventDescriptor];
  367. STAssertNotNil(desc, @"Value: %g", value);
  368. returned = [desc gtm_objectValue];
  369. STAssertNotNil(returned, @"Value: %g", value);
  370. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  371. STAssertEqualObjects(original, returned, @"Value: %g", value);
  372. float floatA = rand();
  373. float floatB = rand();
  374. value = floatA / floatB;
  375. original = [NSNumber numberWithFloat:(float)value];
  376. STAssertNotNil(original, @"Value: %g", value);
  377. desc = [original gtm_appleEventDescriptor];
  378. STAssertNotNil(desc, @"Value: %g", value);
  379. returned = [desc gtm_objectValue];
  380. STAssertNotNil(returned, @"Value: %g", value);
  381. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  382. STAssertEqualObjects(original, returned, @"Value: %g", value);
  383. double doubleA = rand();
  384. double doubleB = rand();
  385. value = doubleA / doubleB;
  386. original = [NSNumber numberWithDouble:value];
  387. STAssertNotNil(original, @"Value: %g", value);
  388. desc = [original gtm_appleEventDescriptor];
  389. STAssertNotNil(desc, @"Value: %g", value);
  390. returned = [desc gtm_objectValue];
  391. STAssertNotNil(returned, @"Value: %g", value);
  392. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  393. STAssertEqualObjects(original, returned, @"Value: %g", value);
  394. value = rand();
  395. original = [NSNumber numberWithBool:value];
  396. STAssertNotNil(original, @"Value: %g", value);
  397. desc = [original gtm_appleEventDescriptor];
  398. STAssertNotNil(desc, @"Value: %g", value);
  399. returned = [desc gtm_objectValue];
  400. STAssertNotNil(returned, @"Value: %g", value);
  401. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  402. STAssertEqualObjects(original, returned, @"Value: %g", value);
  403. value = NAN;
  404. original = [NSNumber numberWithDouble:value];
  405. STAssertNotNil(original, @"Value: %g", value);
  406. desc = [original gtm_appleEventDescriptor];
  407. STAssertNotNil(desc, @"Value: %g", value);
  408. returned = [desc gtm_objectValue];
  409. STAssertNotNil(returned, @"Value: %g", value);
  410. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  411. STAssertEqualObjects(original, returned, @"Value: %g", value);
  412. value = INFINITY;
  413. original = [NSNumber numberWithDouble:value];
  414. STAssertNotNil(original, @"Value: %g", value);
  415. desc = [original gtm_appleEventDescriptor];
  416. STAssertNotNil(desc, @"Value: %g", value);
  417. returned = [desc gtm_objectValue];
  418. STAssertNotNil(returned, @"Value: %g", value);
  419. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  420. STAssertEqualObjects(original, returned, @"Value: %g", value);
  421. value = -0.0;
  422. original = [NSNumber numberWithDouble:value];
  423. STAssertNotNil(original, @"Value: %g", value);
  424. desc = [original gtm_appleEventDescriptor];
  425. STAssertNotNil(desc, @"Value: %g", value);
  426. returned = [desc gtm_objectValue];
  427. STAssertNotNil(returned, @"Value: %g", value);
  428. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  429. STAssertEqualObjects(original, returned, @"Value: %g", value);
  430. value = -INFINITY;
  431. original = [NSNumber numberWithDouble:value];
  432. STAssertNotNil(original, @"Value: %g", value);
  433. desc = [original gtm_appleEventDescriptor];
  434. STAssertNotNil(desc, @"Value: %g", value);
  435. returned = [desc gtm_objectValue];
  436. STAssertNotNil(returned, @"Value: %g", value);
  437. STAssertTrue([returned isKindOfClass:[NSNumber class]], @"Value: %g", value);
  438. STAssertEqualObjects(original, returned, @"Value: %g", value);
  439. }
  440. - (void)testDescriptorWithDoubleAndDoubleValue {
  441. sranddev();
  442. for (int i = 0; i < 1000; ++i) {
  443. double value1 = rand();
  444. double value2 = rand();
  445. double value = value1 / value2;
  446. NSAppleEventDescriptor *desc
  447. = [NSAppleEventDescriptor gtm_descriptorWithDouble:value];
  448. STAssertNotNil(desc, @"Value: %g", value);
  449. double returnedValue = [desc gtm_doubleValue];
  450. STAssertEquals(value, returnedValue, @"Value: %g", value);
  451. }
  452. double specialCases[] = { 0.0f, __DBL_MIN__, __DBL_EPSILON__, INFINITY, NAN };
  453. for (size_t i = 0; i < sizeof(specialCases) / sizeof(double); ++i) {
  454. double value = specialCases[i];
  455. NSAppleEventDescriptor *desc
  456. = [NSAppleEventDescriptor gtm_descriptorWithDouble:value];
  457. STAssertNotNil(desc, @"Value: %g", value);
  458. double returnedValue = [desc gtm_doubleValue];
  459. STAssertEquals(value, returnedValue, @"Value: %g", value);
  460. }
  461. }
  462. - (void)testDescriptorWithFloatAndFloatValue {
  463. sranddev();
  464. for (int i = 0; i < 1000; ++i) {
  465. float value1 = rand();
  466. float value2 = rand();
  467. float value = value1 / value2;
  468. NSAppleEventDescriptor *desc
  469. = [NSAppleEventDescriptor gtm_descriptorWithFloat:value];
  470. STAssertNotNil(desc, @"Value: %f", value);
  471. float returnedValue = [desc gtm_floatValue];
  472. STAssertEquals(value, returnedValue, @"Value: %f", value);
  473. }
  474. float specialCases[] = { 0.0f, FLT_MIN, FLT_MAX, FLT_EPSILON, INFINITY, NAN };
  475. for (size_t i = 0; i < sizeof(specialCases) / sizeof(float); ++i) {
  476. float value = specialCases[i];
  477. NSAppleEventDescriptor *desc
  478. = [NSAppleEventDescriptor gtm_descriptorWithFloat:value];
  479. STAssertNotNil(desc, @"Value: %f", value);
  480. float returnedValue = [desc gtm_floatValue];
  481. STAssertEquals(value, returnedValue, @"Value: %f", value);
  482. }
  483. }
  484. - (void)testDescriptorWithCGFloatAndCGFloatValue {
  485. sranddev();
  486. for (int i = 0; i < 1000; ++i) {
  487. CGFloat value1 = rand();
  488. CGFloat value2 = rand();
  489. CGFloat value = value1 / value2;
  490. NSAppleEventDescriptor *desc
  491. = [NSAppleEventDescriptor gtm_descriptorWithCGFloat:value];
  492. STAssertNotNil(desc, @"Value: %g", (double)value);
  493. CGFloat returnedValue = [desc gtm_cgFloatValue];
  494. STAssertEquals(value, returnedValue, @"Value: %g", (double)value);
  495. }
  496. CGFloat specialCases[] = { 0.0f, CGFLOAT_MIN, CGFLOAT_MAX, NAN };
  497. for (size_t i = 0; i < sizeof(specialCases) / sizeof(CGFloat); ++i) {
  498. CGFloat value = specialCases[i];
  499. NSAppleEventDescriptor *desc
  500. = [NSAppleEventDescriptor gtm_descriptorWithCGFloat:value];
  501. STAssertNotNil(desc, @"Value: %g", (double)value);
  502. CGFloat returnedValue = [desc gtm_cgFloatValue];
  503. STAssertEquals(value, returnedValue, @"Value: %g", (double)value);
  504. }
  505. }
  506. - (void)testDescriptorWithGTMFourCharCode {
  507. GTMFourCharCode *fcc = [GTMFourCharCode fourCharCodeWithFourCharCode:'APPL'];
  508. STAssertNotNil(fcc, nil);
  509. NSAppleEventDescriptor *desc = [fcc gtm_appleEventDescriptor];
  510. STAssertNotNil(desc, nil);
  511. GTMFourCharCode *fcc2 = [desc gtm_objectValue];
  512. STAssertNotNil(fcc2, nil);
  513. STAssertEqualObjects(fcc, fcc2, nil);
  514. STAssertEquals([desc descriptorType], (DescType)typeType, nil);
  515. desc = [fcc gtm_appleEventDescriptorOfType:typeKeyword];
  516. STAssertNotNil(desc, nil);
  517. fcc2 = [desc gtm_objectValue];
  518. STAssertNotNil(fcc2, nil);
  519. STAssertEqualObjects(fcc, fcc2, nil);
  520. STAssertEquals([desc descriptorType], (DescType)typeKeyword, nil);
  521. }
  522. - (void)testDescriptorWithDescriptor {
  523. NSAppleEventDescriptor *desc
  524. = [NSAppleEventDescriptor descriptorWithString:@"foo"];
  525. NSAppleEventDescriptor *desc2 = [desc gtm_appleEventDescriptor];
  526. STAssertEqualObjects(desc, desc2, nil);
  527. }
  528. - (void)handleEvent:(NSAppleEventDescriptor*)event
  529. withReply:(NSAppleEventDescriptor*)reply {
  530. gotEvent_ = YES;
  531. NSAppleEventDescriptor *answer = [NSAppleEventDescriptor descriptorWithInt32:1];
  532. [reply setDescriptor:answer forKeyword:keyDirectObject];
  533. }
  534. - (void)handleEvent:(NSAppleEventDescriptor*)event
  535. withError:(NSAppleEventDescriptor*)error {
  536. gotEvent_ = YES;
  537. NSAppleEventDescriptor *answer = [NSAppleEventDescriptor descriptorWithInt32:1];
  538. [error setDescriptor:answer forKeyword:keyErrorNumber];
  539. }
  540. - (void)testSend {
  541. const AEEventClass eventClass = 'Fooz';
  542. const AEEventID eventID = 'Ball';
  543. NSAppleEventManager *mgr = [NSAppleEventManager sharedAppleEventManager];
  544. [mgr setEventHandler:self
  545. andSelector:@selector(handleEvent:withReply:)
  546. forEventClass:eventClass
  547. andEventID:'Ball'];
  548. NSAppleEventDescriptor *currentProcess
  549. = [[NSProcessInfo processInfo] gtm_appleEventDescriptor];
  550. NSAppleEventDescriptor *event
  551. = [NSAppleEventDescriptor appleEventWithEventClass:eventClass
  552. eventID:eventID
  553. targetDescriptor:currentProcess
  554. returnID:kAutoGenerateReturnID
  555. transactionID:kAnyTransactionID];
  556. gotEvent_ = NO;
  557. NSAppleEventDescriptor *reply;
  558. BOOL goodEvent = [event gtm_sendEventWithMode:kAEWaitReply timeOut:60 reply:&reply];
  559. [mgr removeEventHandlerForEventClass:eventClass andEventID:eventID];
  560. STAssertTrue(goodEvent, @"bad event?");
  561. STAssertTrue(gotEvent_, @"Handler not called");
  562. NSAppleEventDescriptor *value = [reply descriptorForKeyword:keyDirectObject];
  563. STAssertEquals([value int32Value], (SInt32)1, @"didn't get reply");
  564. gotEvent_ = NO;
  565. [GTMUnitTestDevLog expectString:@"Unable to send message: "
  566. "<NSAppleEventDescriptor: 'Fooz'\\'Ball'{ }> -1708"];
  567. goodEvent = [event gtm_sendEventWithMode:kAEWaitReply timeOut:60 reply:&reply];
  568. STAssertFalse(goodEvent, @"good event?");
  569. STAssertFalse(gotEvent_, @"Handler called?");
  570. [mgr setEventHandler:self
  571. andSelector:@selector(handleEvent:withError:)
  572. forEventClass:eventClass
  573. andEventID:eventID];
  574. gotEvent_ = NO;
  575. goodEvent = [event gtm_sendEventWithMode:kAEWaitReply timeOut:60 reply:&reply];
  576. STAssertFalse(goodEvent, @"good event?");
  577. STAssertTrue(gotEvent_, @"Handler not called?");
  578. [mgr removeEventHandlerForEventClass:eventClass andEventID:eventID];
  579. }
  580. @end