@@ -36,6 +36,9 @@ public class ResponsiveUIValidator implements Validator {
3636 private static boolean withReport = false ;
3737 private static long startTime ;
3838 private static String scenarioName = "Default" ;
39+ private static Color rootColor = Color .RED ;
40+ private static Color highlightedElementsColor = Color .MAGENTA ;
41+ private static Color linesColor = Color .ORANGE ;
3942 private WebDriver driver ;
4043 private String rootElementReadableName = "Root Element" ;
4144 private WebElement rootElement ;
@@ -63,6 +66,18 @@ public ResponsiveUIValidator(WebDriver driver) {
6366 errorMessage = new JSONArray ();
6467 }
6568
69+ public void setColorForRootElement (Color color ) {
70+ rootColor = color ;
71+ }
72+
73+ public void setColorForHighlightedElements (Color color ) {
74+ highlightedElementsColor = color ;
75+ }
76+
77+ public void setLinesColor (Color color ) {
78+ linesColor = color ;
79+ }
80+
6681 @ Override
6782 public ResponsiveUIValidator init () {
6883 return new ResponsiveUIValidator (driver );
@@ -496,7 +511,7 @@ public void generateReport(String name) {
496511 private void drawScreenshot () {
497512 g = img .createGraphics ();
498513
499- drawRoot (Color . RED );
514+ drawRoot (rootColor );
500515
501516 for (Object obj : errorMessage ) {
502517 JSONObject det = (JSONObject ) obj ;
@@ -509,9 +524,9 @@ private void drawScreenshot() {
509524 float width = (float ) numE .get (WIDTH );
510525 float height = (float ) numE .get (HEIGHT );
511526
512- g .setColor (Color . MAGENTA );
527+ g .setColor (highlightedElementsColor );
513528 g .setStroke (new BasicStroke (2 ));
514- if (SystemHelper .isRetinaDisplay (g ) && isChrome ()) {
529+ if (SystemHelper .isRetinaDisplay () && isChrome ()) {
515530 g .drawRect (2 * (int ) x , 2 * (int ) y , 2 * (int ) width , 2 * (int ) height );
516531 } else {
517532 g .drawRect ((int ) x , (int ) y , (int ) width , (int ) height );
@@ -710,8 +725,8 @@ private void validateSameSize(List<WebElement> elements) {
710725 int h2 = elements .get (i + 1 ).getSize ().getHeight ();
711726 int w2 = elements .get (i + 1 ).getSize ().getWidth ();
712727 if (h1 != h2 || w1 != w2 ) {
713- putJsonDetailsWithElement ("Elements in a grid have different size." , elements .get (i ));
714- putJsonDetailsWithElement ("Elements in a grid have different size." , elements .get (i + 1 ));
728+ putJsonDetailsWithElement ("Element #" + i + " has different size in a grid ." , elements .get (i ));
729+ putJsonDetailsWithElement ("Element #" + ( i + 1 ) + " has different size in a grid ." , elements .get (i + 1 ));
715730 }
716731 }
717732 }
@@ -812,17 +827,41 @@ private void validateLeftElement(WebElement leftElement) {
812827 }
813828 }
814829
830+ private boolean elementsAreOverlappedOnBorder (WebElement rootElement , WebElement elementOverlapWith ) {
831+ Point elLoc = elementOverlapWith .getLocation ();
832+ Dimension elSize = elementOverlapWith .getSize ();
833+ int xRoot = rootElement .getLocation ().x ;
834+ int yRoot = rootElement .getLocation ().y ;
835+ int widthRoot = rootElement .getSize ().width ;
836+ int heightRoot = rootElement .getSize ().height ;
837+
838+ int sqRootElement = (xRoot + widthRoot ) * (yRoot + heightRoot );
839+ int sqElement = (elLoc .x + elSize .width ) * (elLoc .y + elSize .height );
840+
841+ int sqCommon = 0 ;
842+ if ((xRoot < elLoc .x && yRoot == elLoc .y ) || (yRoot < elLoc .y && xRoot == elLoc .x )) {
843+ sqCommon = (xRoot + widthRoot + elSize .width ) + (yRoot + heightRoot + elSize .height );
844+ } else if ((elLoc .x < xRoot && yRoot == elLoc .y ) || (elLoc .y < yRoot && xRoot == elLoc .x )) {
845+ sqCommon = (elLoc .x + elSize .width + widthRoot ) * (elLoc .y + elSize .height + heightRoot );
846+ }
847+
848+ return sqCommon - sqElement >= sqRootElement ;
849+ }
850+
815851 private boolean elementsAreOverlapped (WebElement elementOverlapWith ) {
816852 Point elLoc = elementOverlapWith .getLocation ();
817853 Dimension elSize = elementOverlapWith .getSize ();
818- return ((xRoot > elLoc .x && yRoot > elLoc .y && xRoot < elLoc .x + elSize .width && yRoot < elLoc .y + elSize .height )
854+ return ((xRoot >= elLoc .x && yRoot > elLoc .y && xRoot < elLoc .x + elSize .width && yRoot < elLoc .y + elSize .height )
819855 || (xRoot + widthRoot > elLoc .x && yRoot > elLoc .y && xRoot + widthRoot < elLoc .x + elSize .width && yRoot < elLoc .y + elSize .height )
820856 || (xRoot > elLoc .x && yRoot + heightRoot > elLoc .y && xRoot < elLoc .x + elSize .width && yRoot + heightRoot < elLoc .y + elSize .height )
821- || (xRoot + widthRoot > elLoc .x && yRoot + heightRoot > elLoc .y && xRoot + widthRoot < elLoc .x + elSize .width && yRoot + widthRoot < elLoc .y + elSize .height )) ||
822- ((elLoc .x > xRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot )
823- || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot )
824- || (elLoc .x > xRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot + heightRoot )
825- || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot + widthRoot ));
857+ || (xRoot + widthRoot > elLoc .x && yRoot + heightRoot > elLoc .y && xRoot + widthRoot < elLoc .x + elSize .width && yRoot + widthRoot < elLoc .y + elSize .height ))
858+
859+ || ((elLoc .x > xRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot )
860+ || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot )
861+ || (elLoc .x > xRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot + heightRoot )
862+ || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot + widthRoot ))
863+
864+ || elementsAreOverlappedOnBorder (rootElement , elementOverlapWith );
826865 }
827866
828867 private boolean elementsAreOverlapped (WebElement rootElement , WebElement elementOverlapWith ) {
@@ -836,11 +875,14 @@ private boolean elementsAreOverlapped(WebElement rootElement, WebElement element
836875 return ((xRoot > elLoc .x && yRoot > elLoc .y && xRoot < elLoc .x + elSize .width && yRoot < elLoc .y + elSize .height )
837876 || (xRoot + widthRoot > elLoc .x && yRoot > elLoc .y && xRoot + widthRoot < elLoc .x + elSize .width && yRoot < elLoc .y + elSize .height )
838877 || (xRoot > elLoc .x && yRoot + heightRoot > elLoc .y && xRoot < elLoc .x + elSize .width && yRoot + heightRoot < elLoc .y + elSize .height )
839- || (xRoot + widthRoot > elLoc .x && yRoot + heightRoot > elLoc .y && xRoot + widthRoot < elLoc .x + elSize .width && yRoot + widthRoot < elLoc .y + elSize .height )) ||
840- ((elLoc .x > xRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot )
841- || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot )
842- || (elLoc .x > xRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot + heightRoot )
843- || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot + widthRoot ));
878+ || (xRoot + widthRoot > elLoc .x && yRoot + heightRoot > elLoc .y && xRoot + widthRoot < elLoc .x + elSize .width && yRoot + widthRoot < elLoc .y + elSize .height ))
879+
880+ || ((elLoc .x > xRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot )
881+ || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot )
882+ || (elLoc .x > xRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot + heightRoot )
883+ || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot + widthRoot ))
884+
885+ || elementsAreOverlappedOnBorder (rootElement , elementOverlapWith );
844886 }
845887
846888 private boolean elementsHasEqualLeftRightOffset (boolean isLeft , WebElement elementToCompare ) {
@@ -868,38 +910,38 @@ private boolean elementsHasEqualTopBottomOffset(boolean isTop, WebElement elemen
868910 private void drawRoot (Color color ) {
869911 g .setColor (color );
870912 g .setStroke (new BasicStroke (2 ));
871- if (SystemHelper .isRetinaDisplay (g ) && isChrome ()) {
913+ if (SystemHelper .isRetinaDisplay () && isChrome ()) {
872914 g .drawRect (2 * xRoot , 2 * yRoot , 2 * widthRoot , 2 * heightRoot );
873915 } else {
874916 g .drawRect (xRoot , yRoot , widthRoot , heightRoot );
875917 }
876918
877919 Stroke dashed = new BasicStroke (1 , BasicStroke .CAP_BUTT , BasicStroke .JOIN_BEVEL , 0 , new float []{9 }, 0 );
878920 g .setStroke (dashed );
879- g .setColor (Color . ORANGE );
921+ g .setColor (linesColor );
880922 if (drawLeftOffsetLine ) {
881- if (SystemHelper .isRetinaDisplay (g ) && isChrome ()) {
923+ if (SystemHelper .isRetinaDisplay () && isChrome ()) {
882924 g .drawLine (2 * xRoot , 0 , 2 * xRoot , 2 * img .getHeight ());
883925 } else {
884926 g .drawLine (xRoot , 0 , xRoot , img .getHeight ());
885927 }
886928 }
887929 if (drawRightOffsetLine ) {
888- if (SystemHelper .isRetinaDisplay (g ) && isChrome ()) {
930+ if (SystemHelper .isRetinaDisplay () && isChrome ()) {
889931 g .drawLine (2 * (xRoot + widthRoot ), 0 , 2 * (xRoot + widthRoot ), 2 * img .getHeight ());
890932 } else {
891933 g .drawLine (xRoot + widthRoot , 0 , xRoot + widthRoot , img .getHeight ());
892934 }
893935 }
894936 if (drawTopOffsetLine ) {
895- if (SystemHelper .isRetinaDisplay (g ) && isChrome ()) {
937+ if (SystemHelper .isRetinaDisplay () && isChrome ()) {
896938 g .drawLine (0 , 2 * yRoot , 2 * img .getWidth (), 2 * yRoot );
897939 } else {
898940 g .drawLine (0 , yRoot , img .getWidth (), yRoot );
899941 }
900942 }
901943 if (drawBottomOffsetLine ) {
902- if (SystemHelper .isRetinaDisplay (g ) && isChrome ()) {
944+ if (SystemHelper .isRetinaDisplay () && isChrome ()) {
903945 g .drawLine (0 , 2 * (yRoot + heightRoot ), 2 * img .getWidth (), 2 * (yRoot + heightRoot ));
904946 } else {
905947 g .drawLine (0 , yRoot + heightRoot , img .getWidth (), yRoot + heightRoot );
0 commit comments