Simulating UITouch in xCode 4.5 programmatically (

2019-05-19 03:51发布

问题:

I'm using UISpec for automated testing in my project. Everything was fine until Apple released xCode 4.5 with iOS 6 SDK. Now UISpec project can't be compiled because a list of errors in category of UITouch class. Here is the code:

//
//  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;
}

//
// 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

compiler gives errors on lines like "_tapCount = 1;" error is "Uknown type name "_tapCount"; did you mean ...?"

what i did:

  1. get a list of instance variable for UITouch class, using run time function "class_copyIvarList" and checked if these variables still exist. they do.
  2. tried to change needed values at runtime, like

    uint uPhase = UITouchPhaseBegin; object_setInstanceVariable(self, "_phase", &uPhase);

or

Ivar var = class_getInstanceVariable([self class], "_phase");
object_setIvar(self, var, [NSNumber numberWithInt:UITouchPhaseBegin]);

in both cases invalid value was written to "_phase" member. i checked that by printing description of the UITouch instance to console. it was written "phase: Uknown".

then i decided to try key-value coding and implemented it like:

[self setValue:[NSNumber numberWithInt:1] forKey:@"tapCount"];
[self setValue:[NSNumber numberWithInt:UITouchPhaseBegin] forKey:@"phase"];

now it gave some results. compile errors are eliminated, member values of UITouch instance are changed but still nothing happens. i set breakpoints in "touchesBegan:..." and "touchesEnded:..." methods of appropriate UI object (it was UIButton in my case) and debugged it. these methods are called, but nothing happens - button handler is not called.

also due to KVC i had to comment methods "setPhase" and "setLocationInWindow" of UITouch category, otherwise endless recursion happens. setter of the property calls itself.

now i'm out of ideas. this category is "Created by Matt Gallagher on 23/11/08.", that means it is a third party code for UISpec. so i hope it is used somewhere else except the UISpec and somebody knows a work around.

thanks.

P.S. i know that this is a private API. it is not included to the release configuration of the project. i need it only for automated testing

回答1:

You do not have to change anything in UIQuery.m file. Please add following lines of code to UIQuery.h file :

#ifdef __IPHONE_6_0
@interface UITouch () {
    NSTimeInterval _timestamp;
    UITouchPhase _phase;
    UITouchPhase _savedPhase;
    NSUInteger _tapCount;

    UIWindow *_window;
    UIView *_view;
    UIView *_gestureView;
    UIView *_warpedIntoView;
    NSMutableArray *_gestureRecognizers;
    NSMutableArray *_forwardingRecord;

    CGPoint _locationInWindow;
    CGPoint _previousLocationInWindow;
    UInt8 _pathIndex;
    UInt8 _pathIdentity;
    float _pathMajorRadius;
    struct {
        unsigned int _firstTouchForView:1;
        unsigned int _isTap:1;
        unsigned int _isDelayed:1;
        unsigned int _sentTouchesEnded:1;
        unsigned int _abandonForwardingRecord:1;
    } _touchFlags;
}
@end
#endif

Thanks.