forked from moredip/UISpec
/
UIQuery.m
767 lines (666 loc) · 22.5 KB
/
UIQuery.m
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#import "UIQuery.h"
#import "objc/runtime.h"
#import "UIDescendants.h"
#import "UIChildren.h"
#import "UIParents.h"
#import "WaitUntilIdle.h"
#import "UIRedoer.h"
#import "UIQueryTableViewCell.h"
#import "UIQueryTableView.h"
#import "UIQueryScrollView.h"
#import "UIQuerySearchBar.h"
#import "UIQueryTabBar.h"
#import "UIQuerySegmentedControl.h"
#import "UIQueryWebView.h"
#import "UIQueryAll.h"
#import "UIFilter.h"
#import "UIBug.h"
#import "UIQueryExpectation.h"
#import "VisibleTouch.h"
@implementation UIQuery
@synthesize with;
@synthesize should;
@synthesize parent, child, descendant, find;
@synthesize touch, flash, show, path, inspect;
@synthesize timeout;
@synthesize views;
@synthesize className;
@synthesize redoer;
@synthesize first, last, all, redo;
@synthesize exists;
+(id)withApplication {
return [self withViews:[NSMutableArray arrayWithObject:[UIApplication sharedApplication]] className:NSStringFromClass([UIApplication class])];
}
-(UIQuery *)find {
return [self descendant];
}
-(UIQuery *)descendant {
[self wait:.25];
return [UIQuery withViews:[[UIDescendants withTraversal] collect:views] className:className filter:YES];
}
-(UIQuery *)child {
[self wait:.25];
return [UIQuery withViews:[[UIChildren withTraversal] collect:views] className:className filter:YES];
}
-(UIQuery *)parent {
[self wait:.25];
return [UIQuery withViews:[[UIParents withTraversal] collect:views] className:className filter:YES];
}
-(UIQueryExpectation *)should {
return [UIQueryExpectation withQuery:self];
}
-(UIFilter *)with {
return [UIFilter withQuery:self];
}
+(id)withViews:(NSMutableArray *)views className:(NSString *)className {
return [UIRedoer withTarget:[[[self alloc] initWithViews:views className:className filter:NO] autorelease]];
}
+(id)withViews:(NSMutableArray *)views className:(NSString *)className filter:(BOOL)filter {
return [UIRedoer withTarget:[[[self alloc] initWithViews:views className:className filter:filter] autorelease]];
}
-(id)initWithViews:(NSMutableArray *)_views className:(NSString *)_className filter:(BOOL)_filter {
if (self = [super init]) {
self.timeout = 10;
self.views = _views;
self.className = _className;
filter = _filter;
}
return self;
}
-(NSArray *)collect:(NSArray *)views {
return [[[[UIDescendants alloc] init] autorelease] collect:views];
}
-(UIQuery *)target {
return self;
}
-(NSArray *)targetViews {
return (views.count == 0) ? [NSArray array] : [NSArray arrayWithObject:[views objectAtIndex:0]];
}
-(UIQuery *)timeout:(int)seconds {
UIQuery *copy = [UIQuery withViews:views className:className];
copy.timeout = seconds;
return copy;
}
-(id)templateFilter {
NSString *viewName = NSStringFromSelector(_cmd);
return [self view:[NSString stringWithFormat:@"UI%@", [viewName stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[viewName substringWithRange:NSMakeRange(0,1)] uppercaseString]]]];
}
-(UIQuery *)index:(int)index {
if (index >= views.count) {
NSLog(@"UISPEC WARNING: %@ doesn't exist at index %i", className, index);
}
NSArray *resultViews = (index >= views.count) ? [NSArray array] : [NSArray arrayWithObject:[views objectAtIndex:index]];
return [UIQuery withViews:resultViews className:className];
}
-(UIQuery *)first {
return [self index:0];
}
-(UIQuery *)last {
return [self index:views.count - 1];
}
-(UIQuery *)all {
return [UIQueryAll withViews:views className:className];
}
-(UIQuery *)view:(NSString *)className {
NSMutableArray *array = [NSMutableArray array];
NSDate *start = [NSDate date];
while ([start timeIntervalSinceNow] > (0 - timeout)) {
NSArray *views = filter ? self.views : self.descendant.views;
Class class = NSClassFromString(className);
for (UIView * v in views) {
if ([v isKindOfClass:class]) {
[array addObject:v];
}
}
if (array.count > 0) {
break;
}
self.redo;
}
if ([className isEqualToString:@"UITableViewCell"]) {
return [UIQueryTableViewCell withViews:array className:className];
} else if ([className isEqualToString:@"UITableView"]) {
return [UIQueryTableView withViews:array className:className];
}
else if ([className isEqualToString:@"UIScrollView"]) {
return [UIQueryScrollView withViews:array className:className];
}
else if ([className isEqualToString:@"UISearchBar"]) {
return [UIQuerySearchBar withViews:array className:className];
}
else if ([className isEqualToString:@"UITabBar"]) {
return [UIQueryTabBar withViews:array className:className];
}
else if ([className isEqualToString:@"UISegmentedControl"]) {
return [UIQuerySegmentedControl withViews:array className:className];
}
else if ([className isEqualToString:@"UIWebView"]) {
return [UIQueryWebView withViews:array className:className];
}
else {
return [UIQuery withViews:array className:className];
}
}
-(UIQuery *)marked:(NSString *)mark {
return [self.with accessibilityLabel:mark];
}
-(UIQuery *)wait:(double)seconds {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, seconds, false);
return [UIQuery withViews:views className:className];
}
-(id)redo {
//NSLog(@"UIQuery redo");
if (redoer != nil) {
//NSLog(@"UIQuery redo redoer = %@", redoer);
UIRedoer *redone = [redoer redo];
redoer.target = redone.target;
self.views = [[redoer play] views];
}
//return is provided by uiredoer
}
-(BOOL)exists {
return [self targetViews].count > 0;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
if ([UIQuery instancesRespondToSelector:aSelector]) {
return [super methodSignatureForSelector:aSelector];
}
[[UIQueryExpectation withQuery:self] exist:[NSString stringWithFormat:@"before you can call %@", NSStringFromSelector(aSelector)]];
NSString *selector = NSStringFromSelector(aSelector);
for (UIView *target in [self targetViews]) {
if ([target respondsToSelector:aSelector]) {
return [target methodSignatureForSelector:aSelector];
}
}
//Check if any view responds as a property match
NSArray *selectors = [selector componentsSeparatedByString:@":"];
if (selectors.count > 1) {
return [self.with methodSignatureForSelector:aSelector];
}
return [super methodSignatureForSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
BOOL isDirect = NO;
for (UIView *target in [self targetViews]) {
if ([target respondsToSelector:[anInvocation selector]]) {
[anInvocation invokeWithTarget:target];
isDirect = YES;
}
}
if (!isDirect) {
[[[UIRedoer withTarget:self] with] forwardInvocation:anInvocation];
}
}
- (BOOL)respondsToSelector:(SEL)aSelector {
if ([UIQuery instancesRespondToSelector:aSelector]) {
return YES;
}
for (UIView *target in [self targetViews]) {
if ([target respondsToSelector:aSelector]) {
return YES;
}
}
return [super respondsToSelector:aSelector];
}
-(UIQuery *)flash {
[[UIQueryExpectation withQuery:self] exist:@"before you can flash it"];
for (UIView *view in [self targetViews]) {
UIColor *tempColor = [view.backgroundColor retain];
for (int i=0; i<5; i++) {
view.backgroundColor = [UIColor yellowColor];
CFRunLoopRunInMode(kCFRunLoopDefaultMode, .05, false);
view.backgroundColor = [UIColor blueColor];
CFRunLoopRunInMode(kCFRunLoopDefaultMode, .05, false);
}
view.backgroundColor = tempColor;
[tempColor release];
}
return [UIQuery withViews:views className:className];
}
-(UIQuery *)show {
//NSLog(@"calling show");
[[UIQueryExpectation withQuery:self] exist:@"before you can show it"];
[UIQuery show:[self targetViews]];
return [UIQuery withViews:views className:className];
}
-(UIQuery *)path {
[[UIQueryExpectation withQuery:self] exist:@"before you can show its path"];
for (UIView *view in [self targetViews]) {
NSMutableString *path = [NSMutableString stringWithString:@"\n"];
NSArray *parentViews = [[UIParents withTraversal] collect:[NSArray arrayWithObject:view]];
for (int i = parentViews.count-1; i>=0; i--) {
[path appendFormat:@"%@ -> ", [[parentViews objectAtIndex:i] class]];
}
[path appendFormat:@"%@", [view class]];
NSLog(path);
}
return [UIQuery withViews:views className:className];
}
-(UIQuery *)inspect {
[UIBug openInspectorWithView:[views objectAtIndex:0]];
return [UIQuery withViews:views className:className];
}
+(void)show:(NSArray *)views {
for (UIView *view in views) {
NSMutableDictionary *dict = [self describe:view];
if ([dict allKeys].count > 0) {
NSLog(@"\nClass: %@ \n%@", [view class], [dict description]);
}
}
}
+(NSDictionary *)describe:(id)object {
//NSLog(@"describing object %@", object);
NSMutableDictionary *properties = [NSMutableDictionary dictionary];
Class clazz = [object class];
do {
//NSLog(@"describing class %@", NSStringFromClass(clazz));
unsigned i;
id objValue;
int intValue;
long longValue;
char *charPtrValue;
char charValue;
short shortValue;
float floatValue;
double doubleValue;
int propertyCount = 0;
objc_property_t *propertyList = class_copyPropertyList(clazz, &propertyCount);
//NSLog(@"property count = %d", propertyCount);
for (i=0; i<propertyCount; i++) {
objc_property_t *thisProperty = propertyList + i;
const char* propertyName = property_getName(*thisProperty);
const char* propertyAttributes = property_getAttributes(*thisProperty);
NSString *key = [NSString stringWithFormat:@"%s", propertyName];
NSString *keyAttributes = [NSString stringWithFormat:@"%s", propertyAttributes];
//NSLog(@"key = %@", key);
//NSLog(@"keyAttributes = %@", keyAttributes);
NSArray *attributes = [keyAttributes componentsSeparatedByString:@","];
if ([[[attributes lastObject] substringToIndex:1] isEqualToString:@"G"]) {
key = [[attributes lastObject] substringFromIndex:1];
}
SEL selector = NSSelectorFromString(key);
if ([object respondsToSelector:selector]) {
NSMethodSignature *sig = [object methodSignatureForSelector:selector];
//NSLog(@"sig = %@", sig);
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setSelector:selector];
//NSLog(@"invocation selector = %@", NSStringFromSelector([invocation selector]));
[invocation setTarget:object];
@try {
[invocation invoke];
}
@catch (NSException *exception) {
NSLog(@"UIQuery.describe caught %@: %@", [exception name], [exception reason]);
continue;
}
const char* type = [[invocation methodSignature] methodReturnType];
NSString *returnType = [NSString stringWithFormat:@"%s", type];
const char* trimmedType = [[returnType substringToIndex:1] cStringUsingEncoding:NSASCIIStringEncoding];
//NSLog(@"return type = %@", returnType);
switch(*trimmedType) {
case '@':
[invocation getReturnValue:(void **)&objValue];
if (objValue == nil) {
[properties setObject:[NSString stringWithFormat:@"%@", objValue] forKey:key];
} else {
[properties setObject:objValue forKey:key];
}
break;
case 'i':
[invocation getReturnValue:(void **)&intValue];
[properties setObject:[NSString stringWithFormat:@"%i", intValue] forKey:key];
break;
case 's':
[invocation getReturnValue:(void **)&shortValue];
[properties setObject:[NSString stringWithFormat:@"%ud", shortValue] forKey:key];
break;
case 'd':
[invocation getReturnValue:(void **)&doubleValue];
[properties setObject:[NSString stringWithFormat:@"%lf", doubleValue] forKey:key];
break;
case 'f':
[invocation getReturnValue:(void **)&floatValue];
[properties setObject:[NSString stringWithFormat:@"%f", floatValue] forKey:key];
break;
case 'l':
[invocation getReturnValue:(void **)&longValue];
[properties setObject:[NSString stringWithFormat:@"%ld", longValue] forKey:key];
break;
case '*':
[invocation getReturnValue:(void **)&charPtrValue];
[properties setObject:[NSString stringWithFormat:@"%s", charPtrValue] forKey:key];
break;
case 'c':
[invocation getReturnValue:(void **)&charValue];
[properties setObject:[NSString stringWithFormat:@"%d", charValue] forKey:key];
break;
case '{': {
unsigned int length = [[invocation methodSignature] methodReturnLength];
void *buffer = (void *)malloc(length);
[invocation getReturnValue:buffer];
NSValue *value = [[[NSValue alloc] initWithBytes:buffer objCType:type] autorelease];
[properties setObject:value forKey:key];
break;
}
}
}
}
} while ((clazz = class_getSuperclass(clazz)) != nil);
return properties;
}
- (UIQuery *)touch {
[[UIQueryExpectation withQuery:self] exist:@"before you can touch it"];
for (UIView *view in [self targetViews]) {
UITouch *touch = [[UITouch alloc] initInView:view];
UIEvent *eventDown = [[NSClassFromString(@"UITouchesEvent") alloc] initWithTouch:touch];
NSSet *touches = [[NSMutableSet alloc] initWithObjects:&touch count:1];
[touch.view touchesBegan:touches withEvent:eventDown];
UIEvent *eventUp = [[NSClassFromString(@"UITouchesEvent") alloc] initWithTouch:touch];
[touch setPhase:UITouchPhaseEnded];
[touch.view touchesEnded:touches withEvent:eventDown];
[eventDown release];
[eventUp release];
[touches release];
[touch release];
[self wait:.5];
}
return [UIQuery withViews:views className:className];
}
- (UIQuery *)touchxy:(NSNumber *)x ycoord:(NSNumber *)y
{
return [self touchx:x y:y]; // call new function name but allow for choice
}
- (UIQuery *)touchx:(NSNumber *)x y:(NSNumber *)y {
//NSLog(@"UIQuery - (UIQuery *)touchxy:(int)x ycoord:(int)y = %@, %@", x, y);
[[UIQueryExpectation withQuery:self] exist:@"before you can touch it"];
for (UIView *aView in [self targetViews]) {
UITouch *aTouch = [[UITouch alloc] initInView:aView xcoord:[x intValue] ycoord:[y intValue]];
// Create a view to display a visible touch on the screen with a center of the touch
CGPoint thePoint = CGPointMake([x floatValue], [y floatValue]);
UIView *visibleTouch = [[VisibleTouch alloc] initWithCenter:thePoint];
[[aView window] addSubview:visibleTouch];
[[aView window] bringSubviewToFront:visibleTouch];
UIEvent *eventDown = [[NSClassFromString(@"UITouchesEvent") alloc] initWithTouch:aTouch];
NSSet *touches = [[NSMutableSet alloc] initWithObjects:&aTouch count:1];
[aTouch.view touchesBegan:touches withEvent:eventDown];
// Send event to the gesture recognizers
for (UIGestureRecognizer *recognizer in [aView gestureRecognizers])
{
[recognizer touchesBegan:touches withEvent:eventDown];
}
[self wait:.25]; // Pause so touch can be seen
UIEvent *eventUp = [[NSClassFromString(@"UITouchesEvent") alloc] initWithTouch:aTouch];
[aTouch setPhase:UITouchPhaseEnded];
[aTouch.view touchesEnded:touches withEvent:eventDown];
for (UIGestureRecognizer *recognizer in [aView gestureRecognizers])
{
[recognizer touchesEnded:touches withEvent:eventDown];
}
[visibleTouch removeFromSuperview];
[visibleTouch release];
[eventDown release];
[eventUp release];
[touches release];
[aTouch release];
[self wait:.5];
}
return [UIQuery withViews:views className:className];
}
-(NSString *)description {
return [NSString stringWithFormat:@"UIQuery: %@", [views description]];
}
-(void)logRange:(NSString *)prefix range:(NSRange)range {
NSLog(@"%@ location = %d, length = %d", prefix, range.location, range.length);
}
-(void)dealloc {
self.views = nil;
self.className = nil;
self.redoer = nil;
[super dealloc];
}
@end
UIQuery * $(NSMutableString *script, ...) {
va_list args;
va_start(args, script);
script = [[[NSMutableString alloc] initWithFormat:script arguments:args] autorelease];
va_end(args);
UIQuery *result = [UIQuery withApplication];
//NSLog(@"script = %@, length = %d", script, script.length);
NSMutableArray *strings = [NSMutableArray array];
NSArray *stringParams = [script componentsSeparatedByString:@"'"];
//NSLog(@"stringParams = %@", stringParams);
if (stringParams.count > 1) {
for (int i=1; i<stringParams.count; i=i+2) {
[strings addObject:[stringParams objectAtIndex:i]];
[script replaceOccurrencesOfString:[NSString stringWithFormat:@"'%@'", [stringParams objectAtIndex:i]] withString:@"BIGFATGUYWITHPIGFEET" options:NSLiteralSearch range:NSMakeRange(0, [script length])];
}
} else {
stringParams = [script componentsSeparatedByString:@"\""];
//NSLog(@"stringParams = %@", stringParams);
if (stringParams.count > 1) {
for (int i=1; i<stringParams.count; i=i+2) {
[strings addObject:[stringParams objectAtIndex:i]];
[script replaceOccurrencesOfString:[NSString stringWithFormat:@"\"%@\"", [stringParams objectAtIndex:i]] withString:@"BIGFATGUYWITHPIGFEET" options:NSLiteralSearch range:NSMakeRange(0, [script length])];
}
}
}
//NSLog(@"script = %@", script);
//NSLog(@"strings = %@", strings);
NSArray *commands = [script componentsSeparatedByString:@" "];
//NSLog(@"commands = %@", commands);
int stringCount = 0;
for (NSString *command in commands) {
NSArray *commandAndParam = [command componentsSeparatedByString:@":"];
NSString *commandValue = [commandAndParam objectAtIndex:0];
//NSLog(@"commandValue = %@", commandValue);
NSString *param = nil;
if (commandAndParam.count > 1) {
param = [commandAndParam objectAtIndex:1];
}
if (param != nil) {
id paramValue = nil;
if ([param isEqualToString:@"BIGFATGUYWITHPIGFEET"]) {
paramValue = [strings objectAtIndex:stringCount];
stringCount++;
//NSLog(@"paramValue = %@", paramValue);
} else if ([param isEqualToString:@"YES"] || [param isEqualToString:@"NO"]) {
paramValue = [param isEqualToString:@"YES"];
//NSLog(@"paramValue = %d", paramValue);
} else {
paramValue = [param intValue];
//NSLog(@"paramValue = %d", paramValue);
}
result = [result performSelector:NSSelectorFromString([NSString stringWithFormat:@"%@:", commandValue]) withObject:paramValue];
} else {
result = [result performSelector:NSSelectorFromString(commandValue)];
}
//NSLog(@"result = %@", result);
}
return result;
}
//
// TouchSynthesis.m
// SelfTesting
//
// Created by Matt Gallagher on 23/11/08.
// Copyright 2008 Matt Gallagher. All rights reserved.
//
// Permission is given to use this source code file, free of charge, in any
// project, commercial or otherwise, entirely at your risk, with the condition
// that any redistribution (in part or whole) of source code must retain
// this copyright and permission notice. Attribution in compiled projects is
// appreciated but not required.
//
@implementation UITouch (Synthesize)
//
// initInView:phase:
//
// Creats a UITouch, centered on the specified view, in the view's window.
// Sets the phase as specified.
//
- (id)initInView:(UIView *)view
{
self = [super init];
if (self != nil)
{
CGRect frameInWindow;
if ([view isKindOfClass:[UIWindow class]])
{
frameInWindow = view.frame;
}
else
{
frameInWindow =
[view.window convertRect:view.frame fromView:view.superview];
}
_tapCount = 1;
_locationInWindow =
CGPointMake(
frameInWindow.origin.x + 0.5 * frameInWindow.size.width,
frameInWindow.origin.y + 0.5 * frameInWindow.size.height);
_previousLocationInWindow = _locationInWindow;
UIView *target = [view.window hitTest:_locationInWindow withEvent:nil];
_window = [view.window retain];
_view = [target retain];
_phase = UITouchPhaseBegan;
_touchFlags._firstTouchForView = 1;
_touchFlags._isTap = 1;
_timestamp = [NSDate timeIntervalSinceReferenceDate];
}
return self;
}
- (id)initInView:(UIView *)view xcoord:(int)x ycoord:(int)y
{
self = [super init];
if (self != nil)
{
CGRect frameInWindow;
if ([view isKindOfClass:[UIWindow class]])
{
frameInWindow = view.frame;
}
else
{
frameInWindow =
[view.window convertRect:view.frame fromView:view.superview];
}
_tapCount = 1;
_locationInWindow =
CGPointMake(
frameInWindow.origin.x + x,
frameInWindow.origin.y + y);
_previousLocationInWindow = _locationInWindow;
UIView *target = [view.window hitTest:_locationInWindow withEvent:nil];
_window = [view.window retain];
_view = [target retain];
_phase = UITouchPhaseBegan;
_touchFlags._firstTouchForView = 1;
_touchFlags._isTap = 1;
_timestamp = [NSDate timeIntervalSinceReferenceDate];
}
return self;
}
//
// setPhase:
//
// Setter to allow access to the _phase member.
//
- (void)setPhase:(UITouchPhase)phase
{
_phase = phase;
_timestamp = [NSDate timeIntervalSinceReferenceDate];
}
//
// setPhase:
//
// Setter to allow access to the _locationInWindow member.
//
- (void)setLocationInWindow:(CGPoint)location
{
_previousLocationInWindow = _locationInWindow;
_locationInWindow = location;
_timestamp = [NSDate timeIntervalSinceReferenceDate];
}
@end
//
// GSEvent is an undeclared object. We don't need to use it ourselves but some
// Apple APIs (UIScrollView in particular) require the x and y fields to be present.
//
@interface GSEventProxy : NSObject
{
@public
unsigned int flags;
unsigned int type;
unsigned int ignored1;
float x1;
float y1;
float x2;
float y2;
unsigned int ignored2[10];
unsigned int ignored3[7];
float sizeX;
float sizeY;
float x3;
float y3;
unsigned int ignored4[3];
}
@end
@implementation GSEventProxy
@end
//
// PublicEvent
//
// A dummy class used to gain access to UIEvent's private member variables.
// If UIEvent changes at all, this will break.
//
@interface PublicEvent : NSObject
{
@public
GSEventProxy *_event;
NSTimeInterval _timestamp;
NSMutableSet *_touches;
CFMutableDictionaryRef _keyedTouches;
}
@end
@implementation PublicEvent
@end
@interface UIEvent (Creation)
- (id)_initWithEvent:(GSEventProxy *)fp8 touches:(id)fp12;
@end
//
// UIEvent (Synthesize)
//
// A category to allow creation of a touch event.
//
@implementation UIEvent (Synthesize)
- (id)initWithTouch:(UITouch *)touch
{
CGPoint location = [touch locationInView:touch.window];
GSEventProxy *gsEventProxy = [[GSEventProxy alloc] init];
gsEventProxy->x1 = location.x;
gsEventProxy->y1 = location.y;
gsEventProxy->x2 = location.x;
gsEventProxy->y2 = location.y;
gsEventProxy->x3 = location.x;
gsEventProxy->y3 = location.y;
gsEventProxy->sizeX = 1.0;
gsEventProxy->sizeY = 1.0;
gsEventProxy->flags = ([touch phase] == UITouchPhaseEnded) ? 0x1010180 : 0x3010180;
gsEventProxy->type = 3001;
//
// On SDK versions 3.0 and greater, we need to reallocate as a
// UITouchesEvent.
//
Class touchesEventClass = objc_getClass("UITouchesEvent");
if (touchesEventClass && ![[self class] isEqual:touchesEventClass])
{
[self release];
self = [touchesEventClass alloc];
}
self = [self _initWithEvent:gsEventProxy touches:[NSSet setWithObject:touch]];
if (self != nil)
{
}
return self;
}
@end