Skip to content

Commit a44487d

Browse files
authored
Merge pull request #74 from ITArray/GridAlignment
tests and correction for grid
2 parents a8ae908 + fb57310 commit a44487d

File tree

5 files changed

+150
-98
lines changed

5 files changed

+150
-98
lines changed

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

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

3+
import com.google.common.collect.Maps;
4+
import com.google.common.collect.Sets;
35
import net.itarray.automotion.internal.geometry.Scalar;
46
import net.itarray.automotion.validation.ChunkUIElementValidator;
57
import net.itarray.automotion.validation.UISnapshot;
@@ -10,6 +12,7 @@
1012

1113
import java.util.List;
1214
import java.util.Map;
15+
import java.util.Set;
1316
import java.util.SortedMap;
1417
import java.util.TreeMap;
1518
import java.util.concurrent.ConcurrentSkipListMap;
@@ -254,13 +257,22 @@ private void validateElementsAreNotOverlapped(List<UIElement> elements) {
254257
}
255258

256259
private void validateGridAlignment(List<UIElement> elements, int columns, int rows) {
257-
SortedMap<Scalar, Integer> map = new TreeMap<>();
260+
SortedMap<Scalar, Integer> countByYCoordinate = new TreeMap<>();
258261
for (UIElement element : elements) {
259-
int oldCount = map.getOrDefault(element.getY(), 0);
260-
map.put(element.getY(), oldCount + 1);
262+
int oldCount = countByYCoordinate.getOrDefault(element.getY(), 0);
263+
countByYCoordinate.put(element.getY(), oldCount + 1);
261264
}
262265

263-
int mapSize = map.size();
266+
Set<Scalar> xCoordinates = Sets.newHashSet();
267+
for (UIElement element : elements) {
268+
xCoordinates.add(element.getX());
269+
}
270+
271+
if (xCoordinates.size() > columns) {
272+
addError(String.format("Elements in a grid are not aligned properly. Looks like grid has wrong amount of columns. Expected is %d. Actual is %d", columns, xCoordinates.size()));
273+
}
274+
275+
int mapSize = countByYCoordinate.size();
264276
if (rows > 0) {
265277
if (mapSize != rows) {
266278
addError(String.format("Elements in a grid are not aligned properly. Looks like grid has wrong amount of rows. Expected is %d. Actual is %d", rows, mapSize));
@@ -270,7 +282,7 @@ private void validateGridAlignment(List<UIElement> elements, int columns, int ro
270282
if (columns > 0) {
271283
int errorLastLine = 0;
272284
int rowCount = 1;
273-
for (Map.Entry<Scalar, Integer> entry : map.entrySet()) {
285+
for (Map.Entry<Scalar, Integer> entry : countByYCoordinate.entrySet()) {
274286
if (rowCount <= mapSize) {
275287
int actualInARow = entry.getValue();
276288
if (actualInARow != columns) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package net.itarray.automotion.tests.grid;
2+
3+
import net.itarray.automotion.internal.ResponsiveUIValidatorBase;
4+
import net.itarray.automotion.validation.ChunkUIElementValidator;
5+
import net.itarray.automotion.validation.ResponsiveUIValidator;
6+
import net.itarray.automotion.validation.UISnapshot;
7+
import org.junit.Before;
8+
import org.openqa.selenium.Dimension;
9+
import org.openqa.selenium.WebElement;
10+
import rectangles.DummyDriverFacade;
11+
12+
import java.util.List;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
16+
public abstract class GridTest {
17+
protected ChunkUIElementValidator chunkValidator;
18+
19+
@Before
20+
public void setUp() {
21+
DummyDriverFacade driverFacade = new DummyDriverFacade();
22+
driverFacade.setPageSize(new Dimension(2000, 1000));
23+
ResponsiveUIValidator uiValidator = new ResponsiveUIValidator(driverFacade);
24+
UISnapshot snapshot = uiValidator.snapshot();
25+
List<WebElement> elements = createElemements();
26+
chunkValidator = snapshot.findElements(elements);
27+
}
28+
29+
30+
public void assertValid() {
31+
ResponsiveUIValidatorBase base = (ResponsiveUIValidatorBase) chunkValidator;
32+
String lastMessage = base.getErrors().getLastMessage();
33+
assertThat(lastMessage).isNull();
34+
}
35+
36+
public void assertInvalid() {
37+
ResponsiveUIValidatorBase base = (ResponsiveUIValidatorBase) chunkValidator;
38+
String lastMessage = base.getErrors().getLastMessage();
39+
assertThat(lastMessage).isNotNull();
40+
}
41+
42+
public abstract List<WebElement> createElemements();
43+
}

src/test/java/net/itarray/automotion/tests/grid/SevenElementsInThreeRowsWithDifferentSizesAndGuttersTest.java

Lines changed: 10 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -16,79 +16,22 @@
1616
import static org.assertj.core.api.Assertions.assertThat;
1717
import static rectangles.DummyWebElement.createElement;
1818

19-
public class SevenElementsInThreeRowsWithDifferentSizesAndGuttersTest {
19+
public class SevenElementsInThreeRowsWithDifferentSizesAndGuttersTest extends GridTest {
2020

21-
private ChunkUIElementValidator chunkValidator;
22-
23-
@Before
24-
public void setUp() {
25-
DummyDriverFacade driverFacade = new DummyDriverFacade();
26-
driverFacade.setPageSize(new Dimension(2000, 1000));
27-
ResponsiveUIValidator uiValidator = new ResponsiveUIValidator(driverFacade);
28-
UISnapshot snapshot = uiValidator.snapshot();
29-
List<WebElement> elements = newArrayList(
30-
createElement(100, 50, 300, 60),
31-
createElement(400, 50, 700, 70),
32-
createElement(900, 50, 1200, 80),
33-
createElement(100, 150, 300, 160),
34-
createElement(400, 150, 700, 170),
35-
createElement(900, 150, 1200, 180),
36-
createElement(100, 250, 300, 160)
37-
);
38-
chunkValidator = snapshot.findElements(elements);
39-
}
40-
41-
@Test
42-
public void areNotAlignedInTwoColumns() {
43-
chunkValidator.alignedAsGrid(2);
44-
assertInvalid();
45-
}
46-
47-
@Test
48-
public void areAlignedInThreeColumns() {
49-
chunkValidator.alignedAsGrid(3);
50-
assertValid();
51-
}
52-
53-
@Test
54-
public void areAlignedInThreeColumnsAndThreeRows() {
55-
chunkValidator.alignedAsGrid(3, 3);
56-
assertValid();
21+
public List<WebElement> createElemements() {
22+
return newArrayList(
23+
createElement(100, 50, 300, 60),
24+
createElement(900, 50, 1200, 80),
25+
createElement(1300, 50, 1800, 80),
26+
createElement(100, 150, 300, 160),
27+
createElement(400, 150, 700, 170),
28+
createElement(1300, 150, 1800, 180)
29+
);
5730
}
5831

5932
@Test
6033
public void areNotAlignedInThreeColumnsAndTwoRows() {
6134
chunkValidator.alignedAsGrid(3, 2);
6235
assertInvalid();
6336
}
64-
65-
@Test
66-
public void areNotAlignedInThreeColumnsAndTwoFour() {
67-
chunkValidator.alignedAsGrid(3, 4);
68-
assertInvalid();
69-
}
70-
71-
@Test
72-
public void areNotAlignedInFourColumns() {
73-
chunkValidator.alignedAsGrid(4);
74-
assertInvalid();
75-
}
76-
77-
@Test
78-
public void areNotAlignedInFourColumnsAndOneRow() {
79-
chunkValidator.alignedAsGrid(4, 1);
80-
assertInvalid();
81-
}
82-
83-
public void assertValid() {
84-
ResponsiveUIValidatorBase base = (ResponsiveUIValidatorBase) chunkValidator;
85-
String lastMessage = base.getErrors().getLastMessage();
86-
assertThat(lastMessage).isNull();
87-
}
88-
89-
public void assertInvalid() {
90-
ResponsiveUIValidatorBase base = (ResponsiveUIValidatorBase) chunkValidator;
91-
String lastMessage = base.getErrors().getLastMessage();
92-
assertThat(lastMessage).isNotNull();
93-
}
9437
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package net.itarray.automotion.tests.grid;
2+
3+
import net.itarray.automotion.internal.ResponsiveUIValidatorBase;
4+
import net.itarray.automotion.validation.ChunkUIElementValidator;
5+
import net.itarray.automotion.validation.ResponsiveUIValidator;
6+
import net.itarray.automotion.validation.UISnapshot;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.openqa.selenium.Dimension;
10+
import org.openqa.selenium.WebElement;
11+
import rectangles.DummyDriverFacade;
12+
13+
import java.util.List;
14+
15+
import static com.google.common.collect.Lists.newArrayList;
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
import static rectangles.DummyWebElement.createElement;
18+
19+
public class SixElementsInTwoRowsAndFourXCoordinatesTest extends GridTest {
20+
21+
public List<WebElement> createElemements() {
22+
return newArrayList(
23+
createElement(100, 50, 300, 60),
24+
createElement(400, 50, 700, 70),
25+
createElement(900, 50, 1200, 80),
26+
createElement(100, 150, 300, 160),
27+
createElement(400, 150, 700, 170),
28+
createElement(900, 150, 1200, 180),
29+
createElement(100, 250, 300, 160)
30+
);
31+
}
32+
33+
@Test
34+
public void areNotAlignedInTwoColumns() {
35+
chunkValidator.alignedAsGrid(2);
36+
assertInvalid();
37+
}
38+
39+
@Test
40+
public void areAlignedInThreeColumns() {
41+
chunkValidator.alignedAsGrid(3);
42+
assertValid();
43+
}
44+
45+
@Test
46+
public void areAlignedInThreeColumnsAndThreeRows() {
47+
chunkValidator.alignedAsGrid(3, 3);
48+
assertValid();
49+
}
50+
51+
@Test
52+
public void areNotAlignedInThreeColumnsAndTwoRows() {
53+
chunkValidator.alignedAsGrid(3, 2);
54+
assertInvalid();
55+
}
56+
57+
@Test
58+
public void areNotAlignedInThreeColumnsAndTwoFour() {
59+
chunkValidator.alignedAsGrid(3, 4);
60+
assertInvalid();
61+
}
62+
63+
@Test
64+
public void areNotAlignedInFourColumns() {
65+
chunkValidator.alignedAsGrid(4);
66+
assertInvalid();
67+
}
68+
69+
@Test
70+
public void areNotAlignedInFourColumnsAndOneRow() {
71+
chunkValidator.alignedAsGrid(4, 1);
72+
assertInvalid();
73+
}
74+
}

src/test/java/net/itarray/automotion/tests/grid/ThreeElementsInARowWithDifferentSizesAndGuttersTest.java

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,13 @@
1717
import static org.assertj.core.api.Assertions.assertThat;
1818
import static rectangles.DummyWebElement.createElement;
1919

20-
public class ThreeElementsInARowWithDifferentSizesAndGuttersTest {
20+
public class ThreeElementsInARowWithDifferentSizesAndGuttersTest extends GridTest {
2121

22-
private ChunkUIElementValidator chunkValidator;
23-
24-
@Before
25-
public void setUp() {
26-
DummyDriverFacade driverFacade = new DummyDriverFacade();
27-
driverFacade.setPageSize(new Dimension(2000, 1000));
28-
ResponsiveUIValidator uiValidator = new ResponsiveUIValidator(driverFacade);
29-
UISnapshot snapshot = uiValidator.snapshot();
30-
List<WebElement> elements = newArrayList(
31-
createElement(100, 50, 300, 60),
32-
createElement(400, 50, 700, 60),
33-
createElement(900, 50, 1200, 60));
34-
chunkValidator = snapshot.findElements(elements);
22+
public List<WebElement> createElemements() {
23+
return newArrayList(
24+
createElement(100, 50, 300, 60),
25+
createElement(400, 50, 700, 60),
26+
createElement(900, 50, 1200, 60));
3527
}
3628

3729
@Test
@@ -75,16 +67,4 @@ public void areNotAlignedInFourColumnsAndTwoRows() {
7567
chunkValidator.alignedAsGrid(4, 2);
7668
assertInvalid();
7769
}
78-
79-
public void assertValid() {
80-
ResponsiveUIValidatorBase base = (ResponsiveUIValidatorBase) chunkValidator;
81-
String lastMessage = base.getErrors().getLastMessage();
82-
assertThat(lastMessage).isNull();
83-
}
84-
85-
public void assertInvalid() {
86-
ResponsiveUIValidatorBase base = (ResponsiveUIValidatorBase) chunkValidator;
87-
String lastMessage = base.getErrors().getLastMessage();
88-
assertThat(lastMessage).isNotNull();
89-
}
9070
}

0 commit comments

Comments
 (0)