7
7
import net .itarray .automotion .internal .geometry .Rectangle ;
8
8
import net .itarray .automotion .internal .geometry .Scalar ;
9
9
import net .itarray .automotion .internal .geometry .Vector ;
10
- import net .itarray .automotion .internal .properties .ConstantExpression ;
11
10
import net .itarray .automotion .internal .properties .Context ;
11
+ import net .itarray .automotion .internal .properties .SuccessorConditionedExpressionDescription ;
12
12
import net .itarray .automotion .tools .general .SystemHelper ;
13
13
import net .itarray .automotion .tools .helpers .TextFinder ;
14
14
import net .itarray .automotion .validation .properties .Condition ;
@@ -165,44 +165,50 @@ public Scalar getBottom() {
165
165
return getCorner ().getY ();
166
166
}
167
167
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
+ );
173
177
}
174
178
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
+ );
180
188
}
181
189
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 );
190
192
}
191
193
194
+ @ Deprecated
192
195
private boolean hasSuccessor (Direction direction , UIElement possibleSuccessor ) {
193
196
return signedDistanceToSuccessor (direction , possibleSuccessor ).isGreaterOrEqualTo (scalar (0 ));
194
197
}
195
198
199
+ @ Deprecated
196
200
private Scalar signedDistanceToSuccessor (Direction direction , UIElement successor ) {
197
201
return direction .signedDistance (direction .end (rectangle ), direction .begin (successor .rectangle ));
198
202
}
199
203
200
204
// todo: used only in PageValidator - no tolerance yet
205
+ @ Deprecated
201
206
public boolean hasRightElement (UIElement rightElement ) {
202
207
return hasSuccessor (RIGHT , rightElement );
203
208
}
204
209
205
210
// todo: used only in PageValidator - no tolerance yet
211
+ @ Deprecated
206
212
public boolean hasBelowElement (UIElement bottomElement ) {
207
213
return hasSuccessor (DOWN , bottomElement );
208
214
}
@@ -230,11 +236,11 @@ private static String getShortenedText(String text) {
230
236
231
237
public boolean contains (UIElement other , Context context ) {
232
238
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
+ }
238
244
239
245
public void validateLeftAlignedWith (UIElement element , Context context ) {
240
246
validateEqualEnd (LEFT , element , context );
@@ -270,6 +276,10 @@ public <V extends MetricSpace<V>> Expression<V> end(ExtendGiving<V> direction) {
270
276
return ElementPropertyExpression .end (direction , this );
271
277
}
272
278
279
+ public <V extends MetricSpace <V >> Expression <V > begin (ExtendGiving <V > direction ) {
280
+ return ElementPropertyExpression .begin (direction , this );
281
+ }
282
+
273
283
public void validateSameSize (UIElement element , Context context ) {
274
284
validateSameExtend (ORIGIN_CORNER , element , context );
275
285
}
@@ -333,33 +343,34 @@ public void validateIsAbove(UIElement element, Condition<Scalar> condition, Cont
333
343
validateSuccessor (DOWN , element , condition , context );
334
344
}
335
345
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 ));
343
351
context .draw (toBeValidatedSuccessor );
344
352
}
345
353
}
346
354
355
+
347
356
public void validateOverlappingWithElement (UIElement element , Context context ) {
348
- if (!overlaps (element , context )) {
357
+ if (!overlaps (element ). evaluateIn ( context , DOWN )) {
349
358
context .add (String .format ("Element %s is not overlapped with element %s but should be" ,
350
359
getQuotedName (),
351
360
element .getQuotedName ()));
352
361
context .draw (element );
353
362
}
354
363
}
355
364
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 )) {
358
367
context .add (String .format ("Element %s is overlapped with element %s but should not" ,
359
368
getQuotedName (),
360
369
element .getQuotedName ()));
361
370
context .draw (element );
371
+ return false ;
362
372
}
373
+ return true ;
363
374
}
364
375
365
376
public void validateLeftOffset (Condition condition , UIElement page , Context context ) {
@@ -379,14 +390,15 @@ public void validateBottomOffset(Condition condition, UIElement page, Context co
379
390
}
380
391
381
392
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 )) {
383
395
context .add (
384
396
String .format ("Expected %s offset of element %s to be %s. Actual %s offset is: %s" ,
385
397
direction .endName (),
386
398
getQuotedName (),
387
399
condition .getDescription (context , direction ),
388
400
direction .endName (),
389
- getOffset ( direction , page ).toStringWithUnits (PIXELS )));
401
+ offset . evaluateIn ( context , direction ).toStringWithUnits (PIXELS )));
390
402
}
391
403
}
392
404
@@ -400,15 +412,18 @@ public void validateCenteredOnHorizontally(UIElement page, Context context) {
400
412
401
413
private void validateCentered (Direction direction , UIElement page , Context context ) {
402
414
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 )) {
404
419
context .add (String .format ("Element %s has not equal %s and %s offset. %s offset is %s, %s is %s" ,
405
420
getQuotedName (),
406
421
opposite .endName (),
407
422
direction .endName (),
408
423
capitalize (opposite .endName ()),
409
- getOffset ( opposite , page ).toStringWithUnits (PIXELS ),
424
+ oppositeOffset . evaluateIn ( context , opposite ).toStringWithUnits (PIXELS ),
410
425
direction .endName (),
411
- getOffset ( direction , page ).toStringWithUnits (PIXELS )));
426
+ offset . evaluateIn ( context , direction ).toStringWithUnits (PIXELS )));
412
427
context .draw (this );
413
428
}
414
429
}
0 commit comments