forked from rpetrich/fonemonkey4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIScrollView+FMReady.m
305 lines (266 loc) · 11.8 KB
/
UIScrollView+FMReady.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
/* This file is part of FoneMonkey.
FoneMonkey is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FoneMonkey is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FoneMonkey. If not, see <http://www.gnu.org/licenses/>. */
//
// UIScrollView+FMReady.m
// FoneMonkey
//
// Created by Stuart Stern on 10/29/09.
// Copyright 2009 Gorilla Logic, Inc.. All rights reserved.
//
#import "UIScrollView+FMReady.h"
#import "FMUtils.h"
#import <objc/runtime.h>
#import "FoneMonkey.h"
#import "FMCommandEvent.h"
#import "UIView+FMReady.h"
#pragma mark UIScrollView delegate methods
@interface FMReadyUIScrollViewDelegate : NSObject <UIScrollViewDelegate> {
}
@end
@implementation FMReadyUIScrollViewDelegate
- (void)scrollViewWillBeginDragging_defaultImp:(UIScrollView *)scrollView {}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self scrollViewWillBeginDragging_defaultImp:scrollView];
}
- (void)scrollViewDidEndDragging_defaultImp:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[self scrollViewDidEndDragging_defaultImp:scrollView willDecelerate:decelerate];
}
@end
#pragma mark -
@implementation UIScrollView (FMReady)
- (BOOL) isFMEnabled {
return YES;
}
+ (void)load {
if (self == [UIScrollView class]) {
//[self interceptMethod:@selector(setDelegate:) withClass:self types:"v@:@"];
Method originalMethod = class_getInstanceMethod(self, @selector(setContentOffset:));
Method replacedMethod = class_getInstanceMethod(self, @selector(fmSetContentOffset:));
method_exchangeImplementations(originalMethod, replacedMethod);
// originalMethod = class_getInstanceMethod(self, @selector(setDelegate:));
// replacedMethod = class_getInstanceMethod(self, @selector(fmSetDelegate:));
// method_exchangeImplementations(originalMethod, replacedMethod);
}
}
//- (void) fm_setDelegate:(id <UIScrollViewDelegate>) del {
// [self orig_setDelegate:del];
//}
-(void) assureDelegate {
if (self.delegate==nil) {
FMReadyUIScrollViewDelegate* del= [[FMReadyUIScrollViewDelegate alloc]init];
self.delegate=del;
}
}
- (void)fmSetContentOffset:(CGPoint)offset {
//[self assureDelegate];
if (!self.dragging || (offset.x == self.contentOffset.x && offset.y == self.contentOffset.y)) {
[self fmSetContentOffset:offset];
return;
}
// offsets are apparently stored as whole-valued floats. We round so that direction determination (up, down, left, right) works
offset.x = round(offset.x);
offset.y = round(offset.y);
// NSLog(@"new:%d,%d prev:%d,%d",offset.x,offset.y,self.contentOffset.x, self.contentOffset.y);
// Bouncing screws up delta checking so we're disabling and we'll see if anybody cares
if (self.bounces) {
self.bounces = NO;
}
// Since it's unclear exactly how to do this in a subclass (override a swapped method), we do it here instead (sorry)
if ([self isKindOfClass:[UITableView class]]) {
[self fmSetContentOffset:offset];
UITableView* table = (UITableView*) self;
NSArray* cells = [table visibleCells];
if ([cells count]) {
NSIndexPath* indexPath = [table indexPathForCell:[cells objectAtIndex:0]];
[[FoneMonkey sharedMonkey] postCommandFrom:self
command:FMCommandVScroll
args:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%d", indexPath.row],
indexPath.section == 0 ? nil : [NSString stringWithFormat:@"%d", indexPath.section], nil]];
return;
}
}
// if (offset.x == self.contentOffset.x) {
// if (offset.y > self.contentOffset.y) {
// cmd = FMCommandScrollDown;
// } else {
// cmd = FMCommandScrollUp;
// }
// [[FoneMonkey sharedMonkey] postCommandFrom:self
// command:cmd
// args:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%1.0f", offset.y], nil]];
// } else if (offset.y == self.contentOffset.y) {
// if (offset.x > self.contentOffset.x) {
// cmd = FMCommandScrollRight;
// } else {
// cmd = FMCommandScrollLeft;
// }
// [[FoneMonkey sharedMonkey] postCommandFrom:self
// command:cmd
// args:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%1.0f", offset.x], nil]];
// } else {
UIView* currentItem = [self subviewAtContentOffset:offset];
NSString* elementMonkeyID = nil;
if (currentItem!=nil) {
elementMonkeyID = currentItem.accessibilityLabel;
if (elementMonkeyID==nil || elementMonkeyID.length==0) {
elementMonkeyID = currentItem.monkeyID;
}
}
[[FoneMonkey sharedMonkey] postCommandFrom:self
command:FMCommandScroll
args:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%1.0f", offset.x],
[NSString stringWithFormat:@"%1.0f", offset.y],
nil]];
// }
[self fmSetContentOffset:offset];
}
- (void) playbackMonkeyEvent:(FMCommandEvent*)event {
CGPoint offset = [self contentOffset];
if ([event.command isEqual:FMCommandScroll]) {
if ([event.args count] < 2) {
event.lastResult = @"Requires 2 arguments, but has %d", [event.args count];
}
offset.x = [[[event args] objectAtIndex:0] floatValue];
offset.y = [[[event args] objectAtIndex:1] floatValue];
// } else if ([event.command isEqual:FMCommandScrollDown] || [event.command isEqual:FMCommandScrollUp]) {
// offset.y = [[[event args] objectAtIndex:0] floatValue];
//
// } else if ([event.command isEqual:FMCommandScrollDown] || [event.command isEqual:FMCommandScrollUp]) {
// offset.x = [[[event args] objectAtIndex:0] floatValue];
} else {
[super playbackMonkeyEvent:event];
return;
}
[self setContentOffset:offset animated:YES];
}
+ (NSString*) uiAutomationCommand:(FMCommandEvent*)command {
NSMutableString* string = [[NSMutableString alloc] init];
if ([command.command isEqualToString:FMCommandScroll]) {
NSString* x = [command.args count] < 1 ? @"0" : [command.args objectAtIndex:0];
NSString* y = [command.args count] < 2 ? @"0" : [command.args objectAtIndex:1];
//NSString* hitTestViewName = [command.args count] < 3 ? @"" : [command.args objectAtIndex:2];
// handle this with drag...
[string appendFormat:@" // Scroll \"%@\" to (%d,%d) - FoneMonkey cannot yet generate the corresponding UIAutomation command. You must manually create the necessary scrolling command.",
[FMUtils stringByJsEscapingQuotesAndNewlines:command.monkeyID],
[FMUtils stringByJsEscapingQuotesAndNewlines:x],
[FMUtils stringByJsEscapingQuotesAndNewlines:y]
//,[FMUtils stringByJsEscapingQuotesAndNewlines:hitTestViewName]
];
} else {
[string appendString:[super uiAutomationCommand:command]];
}
return string;
}
- (BOOL) shouldRecordMonkeyTouch:(UITouch*)touch {
return NO;
}
- (UIView*) subviewAtContentOffset:(CGPoint) offset {
return [self subviewAtContentOffset:offset inView:self];
}
- (UIView*) subviewAtContentOffset:(CGPoint)offset inView:(UIView*)view {
/*
UIView* rez=nil;
NSArray* kids=view.subviews;
NSLog(@"testing content offset at %f, %f", offset.x, offset.y);
NSLog(@"found %d kids", kids.count);
int i=0;
for (UIView * kid in kids) {
i++;
NSLog(@"kid %d is a %@ with frame x=%f y=%f w=%f h=%f", i, NSStringFromClass([kid class]), kid.frame.origin.x, kid.frame.origin.y, kid.frame.size.width, kid.frame.size.height );
if (!kid.hidden && [kid pointInside:offset withEvent:nil]) {
rez=kid;
CGPoint newOffset;
newOffset.x = offset.x - rez.frame.origin.x;
newOffset.y = offset.y - rez.frame.origin.y;
UIView* nestedRez = [self subviewAtContentOffset:newOffset inView:rez];
if (nestedRez != nil) {
rez=nestedRez;
}
}
}
return rez;
*/
UIView* rez = [view hitTest:offset withEvent:nil];;
// NSLog(@"testing content offset at %f, %f", offset.x, offset.y);
if (rez!=nil) {
// NSLog(@"it's a %@ with frame x=%f y=%f w=%f h=%f",NSStringFromClass([rez class]), rez.frame.origin.x, rez.frame.origin.y, rez.frame.size.width, rez.frame.size.height );
} else {
// NSLog(@"it's nil");
}
return rez;
}
- (id <UIScrollViewDelegate>)fmReadyZapDelegate:(id <UIScrollViewDelegate>)del {
Class clazz = [del class];
SEL targetSelector = @selector(scrollViewWillBeginDragging:);
Method replacementMethod = class_getInstanceMethod([self class],@selector(fmScrollViewWillBeginDragging:));
SEL saveOriginalAs = @selector(fmOrigScrollViewWillBeginDragging:);
Method defaultMethod = class_getInstanceMethod([FMReadyUIScrollViewDelegate class], @selector(scrollViewWillBeginDragging_defaultImp:));
[self zapInstanceMethodForClass:clazz
targetSelector:targetSelector
withReplacement:replacementMethod
saveOriginalAs:saveOriginalAs
defaultIfNotFound:defaultMethod];
targetSelector = @selector(scrollViewDidEndDragging:willDecelerate:);
replacementMethod = class_getInstanceMethod([self class],@selector(fmScrollViewDidEndDragging:willDecelerate:));
saveOriginalAs = @selector(fmOrigScrollViewDidEndDragging:willDecelerate:);
defaultMethod = class_getInstanceMethod([FMReadyUIScrollViewDelegate class], @selector(scrollViewDidEndDragging_defaultImp:willDecelerate:));
[self zapInstanceMethodForClass:clazz
targetSelector:targetSelector
withReplacement:replacementMethod
saveOriginalAs:saveOriginalAs
defaultIfNotFound:defaultMethod];
return del;
}
- (void)zapInstanceMethodForClass:(Class)clazz targetSelector:(SEL)targetSelector withReplacement:(Method)replacedMethod
saveOriginalAs:(SEL)saveAsSelector defaultIfNotFound:(Method)defaultMethod {
Method saveAsMethod = class_getInstanceMethod(clazz, saveAsSelector);
if (!saveAsMethod) {
IMP replImp = method_getImplementation(replacedMethod);
Method originalMethod = class_getInstanceMethod(clazz,targetSelector);
if (originalMethod) {
// NSLog(@"-- --- -- - ------ -- - zapping method in class %@", NSStringFromClass(clazz));
const char* typeEncoding = method_getTypeEncoding(originalMethod);
IMP origImp = method_getImplementation(originalMethod);
if (origImp != replImp) {
method_setImplementation(originalMethod, replImp);
class_addMethod(clazz, saveAsSelector, origImp, typeEncoding);
}
} else {
// NSLog(@"-- --- -- - ------ -- - original method not found in class %@", NSStringFromClass(clazz));
if (defaultMethod) {
// NSLog(@"-- --- -- - ------ -- - using default method");
IMP defaultImp = method_getImplementation(defaultMethod);
const char* typeEncoding = method_getTypeEncoding(originalMethod);
class_addMethod(clazz, targetSelector, replImp, typeEncoding);
class_addMethod(clazz, saveAsSelector, defaultImp, typeEncoding);
}
}
} else {
// NSLog(@"-- --- -- - ------ -- - had already zapped class %@", NSStringFromClass(clazz));
}
}
- (void)fmScrollViewWillBeginDragging:(UIScrollView *)scrollView {
// NSLog(@" ### AAA ### ### ### ### ### ### ### ### UIScrollView+FMReady::fmScrollViewWillBeginDragging called");
// [FMUtils setShouldRecordMonkeyTouch:YES forView:scrollView];
[self fmOrigScrollViewWillBeginDragging:scrollView];
}
- (void)fmScrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
// NSLog(@" ### BBB ### ### ### ### ### ### ### ### UIScrollView+FMReady::fmScrollViewDidEndDragging called");
// [FMUtils setShouldRecordMonkeyTouch:NO forView:self];
[self fmOrigScrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}
- (void) fmSetDelegate:(id <UIScrollViewDelegate>) del {
[self fmReadyZapDelegate:del];
[self fmSetDelegate:del];
}
@end