Skip to content

Commit 8b148b2

Browse files
authored
Merge pull request #87 from ITArray/someRefactoringsAfterToleranceAdding
Some refactorings after tolerance adding
2 parents 4c5f623 + 9c3c297 commit 8b148b2

39 files changed

+692
-239
lines changed

src/main/java/net/itarray/automotion/internal/ResponsiveUIChunkValidatorBase.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.itarray.automotion.internal;
1+
package net.itarray.automotion.internal;
22

33
import net.itarray.automotion.internal.geometry.ConnectedIntervals;
44
import net.itarray.automotion.internal.geometry.Interval;
@@ -281,9 +281,7 @@ private void validateElementsAreNotOverlapped(List<UIElement> elements) {
281281
UIElement first = elements.get(firstIndex);
282282
for (int secondIndex = firstIndex+1; secondIndex < elements.size(); secondIndex++) {
283283
UIElement second = elements.get(secondIndex);
284-
if (!first.notOverlaps(second, context)) {
285-
context.add("Elements are overlapped");
286-
context.draw(first);
284+
if (!first.validateNotOverlappingWithElement(second, context)) {
287285
break;
288286
}
289287
}

src/main/java/net/itarray/automotion/internal/UIElement.java

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import net.itarray.automotion.internal.geometry.Rectangle;
88
import net.itarray.automotion.internal.geometry.Scalar;
99
import net.itarray.automotion.internal.geometry.Vector;
10-
import net.itarray.automotion.internal.properties.ConstantExpression;
1110
import net.itarray.automotion.internal.properties.Context;
11+
import net.itarray.automotion.internal.properties.SuccessorConditionedExpressionDescription;
1212
import net.itarray.automotion.tools.general.SystemHelper;
1313
import net.itarray.automotion.tools.helpers.TextFinder;
1414
import net.itarray.automotion.validation.properties.Condition;
@@ -165,44 +165,50 @@ public Scalar getBottom() {
165165
return getCorner().getY();
166166
}
167167

168-
public boolean overlaps(UIElement other, Context context) {
169-
return Condition.lessThan(other.getRight()).isSatisfiedOn(getLeft(), context, RIGHT) &&
170-
Condition.lessThan(getRight()).isSatisfiedOn(other.getLeft(), context, RIGHT) &&
171-
Condition.lessThan(other.getBottom()).isSatisfiedOn(getTop(), context, DOWN) &&
172-
Condition.lessThan(getBottom()).isSatisfiedOn(other.getTop(), context, DOWN);
168+
public Expression<Boolean> overlaps(UIElement other) {
169+
return Expression.and(
170+
Expression.and(
171+
Condition.lessThan(other.end(RIGHT)).applyTo(end(LEFT)),
172+
Condition.lessThan(end(RIGHT)).applyTo(other.end(LEFT))),
173+
Expression.and(
174+
Condition.lessThan(other.end(DOWN)).applyTo(end(UP)),
175+
Condition.lessThan(end(DOWN)).applyTo(other.end(UP)))
176+
);
173177
}
174178

175-
public boolean notOverlaps(UIElement other, Context context) {
176-
return Condition.greaterOrEqualTo(other.getRight()).isSatisfiedOn(getLeft(), context, RIGHT) ||
177-
Condition.greaterOrEqualTo(getRight()).isSatisfiedOn(other.getLeft(), context, RIGHT) ||
178-
Condition.greaterOrEqualTo(other.getBottom()).isSatisfiedOn(getTop(), context, DOWN) ||
179-
Condition.greaterOrEqualTo(getBottom()).isSatisfiedOn(other.getTop(), context, DOWN);
179+
public Expression<Boolean> notOverlaps(UIElement other) {
180+
return Expression.or(
181+
Expression.or(
182+
Condition.greaterOrEqualTo(other.end(RIGHT)).applyTo(end(LEFT)),
183+
Condition.greaterOrEqualTo(end(RIGHT)).applyTo(other.end(LEFT))),
184+
Expression.or(
185+
Condition.greaterOrEqualTo(other.end(DOWN)).applyTo(end(UP)),
186+
Condition.greaterOrEqualTo(end(DOWN)).applyTo(other.end(UP)))
187+
);
180188
}
181189

182-
private Scalar getOffset(Direction direction, UIElement page) {
183-
return direction.signedDistance(getEnd(direction), page.getEnd(direction));
184-
}
185-
186-
private boolean hasEqualOppositeOffsets(Direction direction, UIElement page, Context context) {
187-
return equalTo(
188-
new ConstantExpression<>(getOffset(direction, page)),
189-
new ConstantExpression<>(getOffset(direction.opposite(), page))).evaluateIn(context, direction);
190+
private <V extends MetricSpace<V>> Expression<V> offset(UIElement page, ExtendGiving<V> direction) {
191+
return Expression.signedDistance(end(direction), page.end(direction), direction);
190192
}
191193

194+
@Deprecated
192195
private boolean hasSuccessor(Direction direction, UIElement possibleSuccessor) {
193196
return signedDistanceToSuccessor(direction, possibleSuccessor).isGreaterOrEqualTo(scalar(0));
194197
}
195198

199+
@Deprecated
196200
private Scalar signedDistanceToSuccessor(Direction direction, UIElement successor) {
197201
return direction.signedDistance(direction.end(rectangle), direction.begin(successor.rectangle));
198202
}
199203

200204
// todo: used only in PageValidator - no tolerance yet
205+
@Deprecated
201206
public boolean hasRightElement(UIElement rightElement) {
202207
return hasSuccessor(RIGHT, rightElement);
203208
}
204209

205210
// todo: used only in PageValidator - no tolerance yet
211+
@Deprecated
206212
public boolean hasBelowElement(UIElement bottomElement) {
207213
return hasSuccessor(DOWN, bottomElement);
208214
}
@@ -230,11 +236,11 @@ private static String getShortenedText(String text) {
230236

231237
public boolean contains(UIElement other, Context context) {
232238
return
233-
Condition.lessOrEqualTo(other.getLeft()).isSatisfiedOn(getLeft(), context, RIGHT) &&
234-
Condition.lessOrEqualTo(getRight()).isSatisfiedOn(other.getRight(), context, RIGHT) &&
235-
Condition.lessOrEqualTo(other.getTop()).isSatisfiedOn(getTop(), context, DOWN) &&
236-
Condition.lessOrEqualTo(getBottom()).isSatisfiedOn(other.getBottom(), context, DOWN);
237-
}
239+
Condition.lessOrEqualTo(other.end(LEFT)).applyTo(end(LEFT)).evaluateIn(context, RIGHT) &&
240+
Condition.lessOrEqualTo(end(RIGHT)).applyTo(other.end(RIGHT)).evaluateIn(context, RIGHT) &&
241+
Condition.lessOrEqualTo(other.end(UP)).applyTo(end(UP)).evaluateIn(context, DOWN) &&
242+
Condition.lessOrEqualTo(end(DOWN)).applyTo(other.end(DOWN)).evaluateIn(context, DOWN);
243+
}
238244

239245
public void validateLeftAlignedWith(UIElement element, Context context) {
240246
validateEqualEnd(LEFT, element, context);
@@ -270,6 +276,10 @@ public <V extends MetricSpace<V>> Expression<V> end(ExtendGiving<V> direction) {
270276
return ElementPropertyExpression.end(direction, this);
271277
}
272278

279+
public <V extends MetricSpace<V>> Expression<V> begin(ExtendGiving<V> direction) {
280+
return ElementPropertyExpression.begin(direction, this);
281+
}
282+
273283
public void validateSameSize(UIElement element, Context context) {
274284
validateSameExtend(ORIGIN_CORNER, element, context);
275285
}
@@ -333,33 +343,34 @@ public void validateIsAbove(UIElement element, Condition<Scalar> condition, Cont
333343
validateSuccessor(DOWN, element, condition, context);
334344
}
335345

336-
public void validateSuccessor(Direction direction, UIElement toBeValidatedSuccessor, Condition<Scalar> condition, Context context) {
337-
Scalar signedDistance = signedDistanceToSuccessor(direction, toBeValidatedSuccessor);
338-
if (!signedDistance.satisfies(condition, context, direction)) {
339-
context.add(String.format("%s element aligned not properly. Expected margin should be %s. Actual margin is %s",
340-
direction.afterName(),
341-
condition.getDescription(context, direction),
342-
signedDistance.toStringWithUnits(PIXELS)));
346+
public <V extends MetricSpace<V>> void validateSuccessor(ExtendGiving<V> direction, UIElement toBeValidatedSuccessor, Condition<V> condition, Context context) {
347+
Expression<V> signedDistance = Expression.signedDistance(end(direction), toBeValidatedSuccessor.begin(direction), direction);
348+
Expression<Boolean> assertion = condition.applyTo(signedDistance, new SuccessorConditionedExpressionDescription<>(signedDistance, condition, direction));
349+
if (!assertion.evaluateIn(context, direction)) {
350+
context.add(assertion.getDescription(context, direction));
343351
context.draw(toBeValidatedSuccessor);
344352
}
345353
}
346354

355+
347356
public void validateOverlappingWithElement(UIElement element, Context context) {
348-
if (!overlaps(element, context)) {
357+
if (!overlaps(element).evaluateIn(context, DOWN)) {
349358
context.add(String.format("Element %s is not overlapped with element %s but should be",
350359
getQuotedName(),
351360
element.getQuotedName()));
352361
context.draw(element);
353362
}
354363
}
355364

356-
public void validateNotOverlappingWithElement(UIElement element, Context context) {
357-
if (!notOverlaps(element, context)) {
365+
public boolean validateNotOverlappingWithElement(UIElement element, Context context) {
366+
if (!notOverlaps(element).evaluateIn(context, DOWN)) {
358367
context.add(String.format("Element %s is overlapped with element %s but should not",
359368
getQuotedName(),
360369
element.getQuotedName()));
361370
context.draw(element);
371+
return false;
362372
}
373+
return true;
363374
}
364375

365376
public void validateLeftOffset(Condition condition, UIElement page, Context context) {
@@ -379,14 +390,15 @@ public void validateBottomOffset(Condition condition, UIElement page, Context co
379390
}
380391

381392
public void validateOffset(Direction direction, Condition condition, UIElement page, Context context) {
382-
if (!getOffset(direction, page).satisfies(condition, context, direction)) {
393+
Expression<Scalar> offset = offset(page, direction);
394+
if (!condition.isSatisfiedOn(offset, context, direction)) {
383395
context.add(
384396
String.format("Expected %s offset of element %s to be %s. Actual %s offset is: %s",
385397
direction.endName(),
386398
getQuotedName(),
387399
condition.getDescription(context, direction),
388400
direction.endName(),
389-
getOffset(direction, page).toStringWithUnits(PIXELS)));
401+
offset.evaluateIn(context, direction).toStringWithUnits(PIXELS)));
390402
}
391403
}
392404

@@ -400,15 +412,18 @@ public void validateCenteredOnHorizontally(UIElement page, Context context) {
400412

401413
private void validateCentered(Direction direction, UIElement page, Context context) {
402414
Direction opposite = direction.opposite();
403-
if (!hasEqualOppositeOffsets(direction, page, context)) {
415+
Expression<Scalar> offset = offset(page, direction);
416+
Expression<Scalar> oppositeOffset = offset(page, opposite);
417+
Expression<Boolean> expression = equalTo(offset, oppositeOffset);
418+
if (!expression.evaluateIn(context, direction)) {
404419
context.add(String.format("Element %s has not equal %s and %s offset. %s offset is %s, %s is %s",
405420
getQuotedName(),
406421
opposite.endName(),
407422
direction.endName(),
408423
capitalize(opposite.endName()),
409-
getOffset(opposite, page).toStringWithUnits(PIXELS),
424+
oppositeOffset.evaluateIn(context, opposite).toStringWithUnits(PIXELS),
410425
direction.endName(),
411-
getOffset(direction, page).toStringWithUnits(PIXELS)));
426+
offset.evaluateIn(context, direction).toStringWithUnits(PIXELS)));
412427
context.draw(this);
413428
}
414429
}

src/main/java/net/itarray/automotion/internal/geometry/Direction.java

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package net.itarray.automotion.internal.geometry;
22

3-
public enum Direction implements ExtendGiving<Scalar> {
4-
DOWN{
5-
@Override
6-
public boolean beforeOrEqual(Scalar p1, Scalar p2) {
7-
return p1.isLessOrEqualTo(p2);
8-
}
3+
import java.util.function.Function;
94

5+
import static net.itarray.automotion.internal.geometry.Scalar.scalar;
6+
7+
public enum Direction implements ExtendGiving<Scalar> {
8+
DOWN(Vector::getY, 1) {
109
public String beforeName() {
1110
return "Above";
1211
}
@@ -31,12 +30,7 @@ public String extendName() {
3130
return "height";
3231
}
3332
},
34-
UP {
35-
@Override
36-
public boolean beforeOrEqual(Scalar p1, Scalar p2) {
37-
return p2.isLessOrEqualTo(p1);
38-
}
39-
33+
UP(Vector::getY, -1) {
4034
public String beforeName() {
4135
return "Below";
4236
}
@@ -60,13 +54,9 @@ public String beginName() {
6054
public String extendName() {
6155
return "height";
6256
}
63-
},
64-
RIGHT {
65-
@Override
66-
public boolean beforeOrEqual(Scalar p1, Scalar p2) {
67-
return p1.isLessOrEqualTo(p2);
68-
}
6957

58+
},
59+
RIGHT(Vector::getX, 1) {
7060
public String beforeName() {
7161
return "Left";
7262
}
@@ -91,12 +81,7 @@ public String extendName() {
9181
return "width";
9282
}
9383
},
94-
LEFT {
95-
@Override
96-
public boolean beforeOrEqual(Scalar p1, Scalar p2) {
97-
return p2.isLessOrEqualTo(p1);
98-
}
99-
84+
LEFT(Vector::getX, -1) {
10085
public String beforeName() {
10186
return "Right";
10287
}
@@ -120,8 +105,12 @@ public String beginName() {
120105
public String extendName() {
121106
return "width";
122107
}
108+
123109
};
124110

111+
private final Function<Vector, Scalar> projection;
112+
private final Scalar u;
113+
125114
public abstract Direction opposite();
126115

127116
public abstract Scalar begin(Rectangle rectangle);
@@ -146,13 +135,18 @@ public String afterName() {
146135

147136
public abstract String extendName();
148137

149-
public abstract boolean beforeOrEqual(Scalar p1, Scalar p2);
150-
138+
@Override
151139
public Scalar signedDistance(Scalar p1, Scalar p2) {
152-
return beforeOrEqual(p1, p2) ? distance(p1, p2) : distance(p1, p2).negated();
140+
return p2.minus(p1).times(u);
141+
}
142+
143+
@Override
144+
public Function<Vector, Scalar> transform() {
145+
return v -> projection.apply(v).times(u);
153146
}
154147

155-
public Scalar distance(Scalar p1, Scalar p2) {
156-
return p2.minus(p1).abs();
148+
Direction(Function<Vector, Scalar> projection, int u) {
149+
this.projection = projection;
150+
this.u = scalar(u);
157151
}
158152
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package net.itarray.automotion.internal.geometry;
22

3+
import java.util.function.Function;
4+
35
public interface ExtendGiving<V extends MetricSpace<V>> {
46
String beginName();
57
String endName();
8+
String beforeName();
9+
String afterName();
610
String extendName();
711

812
V begin(Rectangle rectangle);
913
V end(Rectangle rectangle);
1014
default V extend(Rectangle rectangle) {
11-
return end(rectangle).minus(begin(rectangle));
15+
return transform().apply(rectangle.getCorner().minus(rectangle.getOrigin()));
1216
}
17+
V signedDistance(V p1, V p2);
18+
19+
Function<Vector, V> transform();
1320
}

src/main/java/net/itarray/automotion/internal/geometry/Rectangle.java

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,14 @@
44
import org.openqa.selenium.Point;
55
import org.openqa.selenium.WebElement;
66

7+
import static net.itarray.automotion.internal.geometry.Direction.DOWN;
8+
import static net.itarray.automotion.internal.geometry.Direction.RIGHT;
9+
710
public class Rectangle {
811
private final Vector origin;
912
private final Vector corner;
1013

11-
public static final ExtendGiving<Vector> ORIGIN_CORNER = new ExtendGiving<Vector>() {
12-
@Override
13-
public String extendName() {
14-
return "size";
15-
}
16-
17-
@Override
18-
public String beginName() {
19-
return "top left";
20-
}
21-
22-
@Override
23-
public String endName() {
24-
return "bottom right";
25-
}
26-
27-
@Override
28-
public Vector begin(Rectangle rectangle) {
29-
return rectangle.getOrigin();
30-
}
31-
32-
@Override
33-
public Vector end(Rectangle rectangle) {
34-
return rectangle.getCorner();
35-
}
36-
};
37-
38-
14+
public static final ExtendGiving<Vector> ORIGIN_CORNER = new VectorExtendGiving(RIGHT, DOWN);
3915

4016
public static Rectangle rectangle(WebElement webElement) {
4117
Point location = webElement.getLocation();
@@ -70,7 +46,7 @@ public boolean intersects(Direction direction, Rectangle other) {
7046
}
7147

7248
public boolean intersects(Rectangle other) {
73-
return intersects(Direction.RIGHT, other) && intersects(Direction.DOWN, other);
49+
return intersects(RIGHT, other) && intersects(DOWN, other);
7450
}
7551

7652
public boolean contains(Direction direction, Rectangle other) {
@@ -79,12 +55,13 @@ public boolean contains(Direction direction, Rectangle other) {
7955
}
8056

8157
public boolean contains(Rectangle other) {
82-
return contains(Direction.RIGHT, other) && contains(Direction.DOWN, other);
58+
return contains(RIGHT, other) && contains(DOWN, other);
8359
}
8460

8561
@Override
8662
public String toString() {
8763
Vector extend = corner.minus(origin);
8864
return String.format("[(%s,%s) - %sx%s]", origin.getX(), origin.getY(), extend.getX(), extend.getY());
8965
}
66+
9067
}

0 commit comments

Comments
 (0)