77import net .itarray .automotion .internal .geometry .Rectangle ;
88import net .itarray .automotion .internal .geometry .Scalar ;
99import net .itarray .automotion .internal .geometry .Vector ;
10- import net .itarray .automotion .internal .properties .ConstantExpression ;
1110import net .itarray .automotion .internal .properties .Context ;
11+ import net .itarray .automotion .internal .properties .SuccessorConditionedExpressionDescription ;
1212import net .itarray .automotion .tools .general .SystemHelper ;
1313import net .itarray .automotion .tools .helpers .TextFinder ;
1414import 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 }
0 commit comments