Skip to content

Commit

Permalink
Merge pull request #243 from wix/generation/add-content-edge
Browse files Browse the repository at this point in the history
Add support for GreyAction contentEdge to generated code
  • Loading branch information
rotemmiz authored Sep 7, 2017
2 parents 24cd288 + d5c2ee7 commit 551719e
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 12 deletions.
71 changes: 71 additions & 0 deletions detox/src/ios/earlgreyapi/GREYActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ function sanitize_greyDirection(action) {
}
}

function sanitize_greyContentEdge(action) {
switch (action) {
case "left":
return 0;
case "right":
return 1;
case "top":
return 2;
case "bottom":
return 3;

default:
throw new Error(`GREYAction.GREYContentEdge must be a 'left'/'right'/'top'/'bottom', got ${action}`);
}
}



class GREYActions {
/*@return A GREYAction that performs multiple taps of a specified @c count.
Expand Down Expand Up @@ -148,6 +165,60 @@ starting from the given start points.
};
}

/*@return A GREYAction that scrolls to the given content @c edge of a scroll view.
*/static actionForScrollToContentEdge(edge) {
if (!["left", "right", "top", "bottom"].some(option => option === edge)) throw new Error("edge should be one of [left, right, top, bottom], but got " + edge);
return {
target: {
type: "Class",
value: "GREYActions"
},
method: "actionForScrollToContentEdge:",
args: [{
type: "NSInteger",
value: sanitize_greyContentEdge(edge)
}]
};
}

/*A GREYAction that scrolls to the given content @c edge of a scroll view with the scroll action
starting from the given start point specified as percentages. @c xOriginStartPercentage is the x
start position as a percentage of the total width of the scrollable visible area,
@c yOriginStartPercentage is the y start position as a percentage of the total height of the
scrollable visible area. @c xOriginStartPercentage and @c yOriginStartPercentage must be between
0 and 1, exclusive.
@param edge The edge towards which the scrolling is to take place.
@param xOriginStartPercentage X coordinate of the start point specified as a percentage (0, 1)
exclusive, of the total width of the scrollable visible area.
@param yOriginStartPercentage Y coordinate of the start point specified as a percentage (0, 1)
exclusive, of the total height of the scrollable visible area.
@return A GREYAction that scrolls to the given content @c edge of a scroll view with the scroll
action starting from the given start point.
*/static actionForScrollToContentEdgeXOriginStartPercentageYOriginStartPercentage(edge, xOriginStartPercentage, yOriginStartPercentage) {
if (!["left", "right", "top", "bottom"].some(option => option === edge)) throw new Error("edge should be one of [left, right, top, bottom], but got " + edge);
if (typeof xOriginStartPercentage !== "number") throw new Error("xOriginStartPercentage should be a number, but got " + (xOriginStartPercentage + (" (" + (typeof xOriginStartPercentage + ")"))));
if (typeof yOriginStartPercentage !== "number") throw new Error("yOriginStartPercentage should be a number, but got " + (yOriginStartPercentage + (" (" + (typeof yOriginStartPercentage + ")"))));
return {
target: {
type: "Class",
value: "GREYActions"
},
method: "actionForScrollToContentEdge:xOriginStartPercentage:yOriginStartPercentage:",
args: [{
type: "NSInteger",
value: sanitize_greyContentEdge(edge)
}, {
type: "CGFloat",
value: xOriginStartPercentage
}, {
type: "CGFloat",
value: yOriginStartPercentage
}]
};
}

/*Returns an action that fast swipes through the view. The start point of the swipe is chosen to
achieve the maximum the swipe possible to the other edge.
Expand Down
11 changes: 2 additions & 9 deletions detox/src/ios/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,8 @@ class ScrollAmountAction extends Action {
class ScrollEdgeAction extends Action {
constructor(edge) {
super();
if (typeof edge !== 'string') throw new Error(`ScrollEdgeAction ctor 1st argument must be a string, got ${typeof edge}`);
switch (edge) {
case 'left': edge = 0; break;
case 'right': edge = 1; break;
case 'top': edge = 2; break;
case 'bottom': edge = 3; break;
default: throw new Error(`ScrollEdgeAction edge must be a 'left'/'right'/'top'/'bottom', got ${edge}`);
}
this._call = invoke.call(invoke.IOS.Class('GREYActions'), 'actionForScrollToContentEdge:', invoke.IOS.NSInteger(edge));

this._call = invoke.callDirectly(GreyActions.actionForScrollToContentEdge(edge));
}
}

Expand Down
2 changes: 2 additions & 0 deletions generation/__tests__/__snapshots__/global-functions.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`globals sanitize_greyContentEdge should fail with unknown value 1`] = `"GREYAction.GREYContentEdge must be a 'left'/'right'/'top'/'bottom', got kittens"`;

exports[`globals sanitize_greyDirection should fail with unknown value 1`] = `"GREYAction.GREYDirection must be a 'left'/'right'/'up'/'down', got kittens"`;
7 changes: 7 additions & 0 deletions generation/__tests__/earl-grey.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ describe("earl-grey generation", () => {
expect(result.args[0].value).toBe(4);
expect(result).toMatchSnapshot();
});

it("should sanitize the content edge", () => {
const result = ExampleClass.actionForScrollToContentEdge("bottom");

expect(result.args[0].type).toBe("NSInteger");
expect(result.args[0].value).toBe(3);
});
});

describe("filter functions with unknown arguments", () => {
Expand Down
15 changes: 15 additions & 0 deletions generation/__tests__/global-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,19 @@ describe("globals", () => {
}).toThrowErrorMatchingSnapshot();
});
});

describe("sanitize_greyContentEdge", () => {
it("should return numbers for strings", () => {
expect(globals.sanitize_greyContentEdge("left")).toBe(0);
expect(globals.sanitize_greyContentEdge("right")).toBe(1);
expect(globals.sanitize_greyContentEdge("top")).toBe(2);
expect(globals.sanitize_greyContentEdge("bottom")).toBe(3);
});

it("should fail with unknown value", () => {
expect(() => {
globals.sanitize_greyContentEdge("kittens");
}).toThrowErrorMatchingSnapshot();
});
});
});
18 changes: 18 additions & 0 deletions generation/earl-grey/global-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ function sanitize_greyDirection(action) {
}
}

function sanitize_greyContentEdge(action) {
switch (action) {
case "left":
return 0;
case "right":
return 1;
case "top":
return 2;
case "bottom":
return 3;

default:
throw new Error(`GREYAction.GREYContentEdge must be a 'left'/'right'/'top'/'bottom', got ${action}`);
}
}


module.exports = {
sanitize_greyDirection,
sanitize_greyContentEdge,
};
11 changes: 8 additions & 3 deletions generation/earl-grey/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ const isOneOf = generateIsOneOfCheck;
const SUPPORTED_TYPES = [
"CGFloat",
"CGPoint",
"GREYContentEdge",
"GREYDirection",
"NSInteger",
"NSString *",
"NSString",
"NSUInteger"
"NSUInteger",
];

/**
Expand Down Expand Up @@ -148,9 +149,13 @@ const callGlobal = sanitizerName => argIdentifier =>
t.callExpression(t.identifier(sanitizerName), [t.identifier(argIdentifier)]);
const supportedContentSanitizersMap = {
GREYDirection: {
type: 'NSInteger',
value: callGlobal("sanitize_greyDirection"),
type: "NSInteger",
value: callGlobal("sanitize_greyDirection")
},
GREYContentEdge: {
type: "NSInteger",
value: callGlobal("sanitize_greyContentEdge")
}
};
function addArgumentContentSanitizerCall(json) {
if (supportedContentSanitizersMap[json.type]) {
Expand Down
5 changes: 5 additions & 0 deletions generation/fixtures/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@
*/
+ (id<GREYAction>)actionForTypeText:(NSString *)text;

/**
* @return A GREYAction that scrolls to the given content @c edge of a scroll view.
*/
+ (id<GREYAction>)actionForScrollToContentEdge:(GREYContentEdge)edge;

+ (id<GREYAction>)actionWithUnknownType:(WTFType *)wat;
+ (id<GREYAction>)actionWithKnown:(NSUInteger)iknowdis andUnknownType:(WTFTypalike *)wat;

0 comments on commit 551719e

Please sign in to comment.