Retool loop through actions

This commit is contained in:
Isaac Murchie
2014-04-01 14:35:23 -07:00
parent 63ff8fb2b4
commit 0e11389bb7
2 changed files with 74 additions and 61 deletions

View File

@@ -1874,44 +1874,45 @@ iOSController.parseTouch = function (gestures, cb) {
gestures.pop();
}
var completed = 0;
var touchStateObjects = [];
var error = null;
var action = function () {
var finishParsing = function () {
var prevPos = null;
if (completed === gestures.length) {
// we need to change the time (which is now an offset)
// and the position (which may be an offset)
var time = 0;
_.each(touchStateObjects, function (state, index) {
if (state.touch[0] === false) {
// if we have no position (this happens with `wait`) we need the previous one
state.touch[0] = prevPos;
} else if (state.touch[0].offset) {
// the current position is an offset
state.touch[0].x += prevPos.x;
state.touch[0].y += prevPos.y;
}
delete state.touch[0].offset;
prevPos = state.touch[0];
var timeOffset = state.timeOffset;
time += timeOffset;
state.time = time;
// we need to change the time (which is now an offset)
// and the position (which may be an offset)
var time = 0;
_.each(touchStateObjects, function (state, index) {
if (state.touch[0] === false) {
// if we have no position (this happens with `wait`) we need the previous one
state.touch[0] = prevPos;
} else if (state.touch[0].offset && prevPos) {
// the current position is an offset
state.touch[0].x += prevPos.x;
state.touch[0].y += prevPos.y;
}
delete state.touch[0].offset;
prevPos = state.touch[0];
delete state.timeOffset;
});
var timeOffset = state.timeOffset;
time += timeOffset;
state.time = time;
cb(error, touchStateObjects);
}
delete state.timeOffset;
});
cb(null, touchStateObjects);
}.bind(this);
var needsPoint = function (action) {
return _.contains(['press', 'moveTo', 'tap', 'longPress'], action);
};
_.each(gestures, function (gesture, index) {
if (error) return;
// _.each(gestures, function (gesture, index) {
var cycleThroughGestures = function () {
var gesture = gestures.shift();
if (typeof gesture === "undefined") {
return finishParsing();
}
var tapPoint = false;
if (needsPoint(gesture.action)) { // press, longPress, moveTo and tap all need a position
@@ -1919,11 +1920,8 @@ iOSController.parseTouch = function (gestures, cb) {
if (elementId) {
var command = ["au.getElement('", elementId, "').rect()"].join('');
this.proxy(command, function (err, res) {
if (error) return;
if (err) {
error = err;
return;
}
if (err) return cb(err); // short circuit and quit
var rect = res.value;
var pos = {x: rect.origin.x, y: rect.origin.y};
var size = {w: rect.size.width, h: rect.size.height};
@@ -1948,9 +1946,8 @@ iOSController.parseTouch = function (gestures, cb) {
tapPoint
]
};
touchStateObjects[index] = touchStateObject;
completed++;
action();
touchStateObjects.push(touchStateObject);
cycleThroughGestures();
}.bind(this));
} else {
// iOS expects absolute coordinates, so we need to save these as offsets
@@ -1966,9 +1963,8 @@ iOSController.parseTouch = function (gestures, cb) {
tapPoint
]
};
touchStateObjects[index] = touchStateObject;
completed++;
action();
touchStateObjects.push(touchStateObject);
cycleThroughGestures();
}
} else {
// in this case we need the previous entry's tap point
@@ -1985,11 +1981,12 @@ iOSController.parseTouch = function (gestures, cb) {
tapPoint
]
};
touchStateObjects[index] = touchStateObject;
completed++;
action();
touchStateObjects.push(touchStateObject);
cycleThroughGestures();
}
}.bind(this));
}.bind(this);
cycleThroughGestures();
};
var mergeStates = function (states) {
@@ -2030,30 +2027,21 @@ var mergeStates = function (states) {
iOSController.performMultiAction = function (actions, cb) {
var states = [];
var completed = 0;
var error = null;
var action = function () {
if (completed === actions.length) {
if (error) return cb(error);
var cycleThroughActions = function () {
var action = actions.shift();
if (typeof action === "undefined") {
var mergedStates = mergeStates(states);
this.proxy("target.touch(" + JSON.stringify(mergedStates) + ")", cb);
return this.proxy("target.touch(" + JSON.stringify(mergedStates) + ")", cb);
}
}.bind(this);
_.each(actions, function (gestures, index) {
if (error) return;
this.parseTouch(action, function (err, val) {
if (err) return cb(err); // short-circuit the loop and send the error up
states.push(val);
this.parseTouch(gestures, function (err, touchStateObjects) {
if (error) return;
if (err !== null) {
error = err;
return;
}
states[index] = touchStateObjects;
completed++;
action();
cycleThroughActions();
}.bind(this));
}.bind(this));
}.bind(this);
cycleThroughActions();
};
module.exports = iOSController;

View File

@@ -112,5 +112,30 @@ describe('touch actions', function () {
.sleep(15000)
.nodeify(done);
});
it('should do more involved pinching in and out', function (done) {
driver
.elementsByTagName('button').then(function (buttons) {
var el = buttons[3];
var action = new TouchAction(el);
return action.tap().perform();
})
.sleep(500).then(function () { okIfAlert(driver); })
.sleep(500)
.elementByXPath('//window[1]/UIAMapView[1]')
.then(function (el) {
var a1 = new TouchAction(el);
a1.press().moveTo({ x: -100, y: 0 }).wait(3000).moveTo({ x: 100, y: 0 }).release();
var a2 = new TouchAction(el);
a2.press().moveTo({ x: 100, y: 0 }).wait({ ms: 3000 }).moveTo({ x: -100, y: 0 }).release();
var ma = new MultiAction(el);
ma.add(a1, a2);
ma.perform();
})
.sleep(15000)
.nodeify(done);
});
});
});