Skip to content

Commit a0d57be

Browse files
committed
Addressed review comments: Added prefixed constants to PageSize and updated tests to validate predefined sizes.
1 parent 520b8e1 commit a0d57be

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

java/src/org/openqa/selenium/print/PageSize.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ public class PageSize {
2525
private final double width;
2626

2727
// Reference for predefined page size constants: https://www.agooddaytoprint.com/page/paper-size-chart-faq
28-
public static final PageSize A4 = new PageSize(29.7, 21.0); // A4 size in cm
29-
public static final PageSize LEGAL = new PageSize(35.56, 21.59); // Legal size in cm
30-
public static final PageSize TABLOID = new PageSize(43.18, 27.94); // Tabloid size in cm
31-
public static final PageSize LETTER = new PageSize(27.94, 21.59); // Letter size in cm
28+
public static final PageSize ISO_A4 = new PageSize(29.7, 21.0); // ISO_A4 size in cm
29+
public static final PageSize US_LEGAL = new PageSize(35.56, 21.59); // US_LEGAL size in cm
30+
public static final PageSize ANSI_TABLOID = new PageSize(43.18, 27.94); // ANSI_TABLOID size in cm
31+
public static final PageSize US_LETTER = new PageSize(27.94, 21.59); // US_LETTER size in cm
3232

3333
public PageSize() {
34-
// Initialize with defaults. A4 paper size defaults in cms.
35-
this.height = 27.94;
36-
this.width = 21.59;
34+
// Initialize with defaults. ISO_A4 paper size defaults in cms.
35+
this(ISO_A4.getHeight(), ISO_A4.getWidth());
3736
}
3837

3938
public PageSize(double height, double width) {
@@ -49,6 +48,13 @@ public double getWidth() {
4948
return width;
5049
}
5150

51+
public static PageSize setPageSize(PageSize pageSize) {
52+
if (pageSize == null) {
53+
throw new IllegalArgumentException("Page size cannot be null");
54+
}
55+
return new PageSize(pageSize.getHeight(), pageSize.getWidth());
56+
}
57+
5258
public Map<String, Object> toMap() {
5359
final Map<String, Object> options = new HashMap<>(7);
5460
options.put("height", getHeight());
@@ -60,6 +66,6 @@ public Map<String, Object> toMap() {
6066
@Override
6167
public String toString() {
6268
return "PageSize[width=" + this.getWidth() + ", height=" + this.getHeight() + "]";
63-
}
69+
}
6470

6571
}

java/test/org/openqa/selenium/print/PageSizeTest.java

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,38 @@
2525
@Tag("UnitTests")
2626
class PageSizeTest {
2727

28-
// Defaults assertion
29-
private static final double HEIGHT = 27.94;
30-
private static final double WIDTH = 21.59;
31-
32-
@Test
33-
void setsDefaultHeightWidth() {
28+
@Test
29+
void setsDefaultHeightWidth() {
3430
PageSize pageSize = new PageSize();
35-
36-
assertThat(pageSize.getHeight()).isEqualTo(HEIGHT);
37-
assertThat(pageSize.getWidth()).isEqualTo(WIDTH);
31+
assertThat(pageSize.getHeight()).isEqualTo(29.7);
32+
assertThat(pageSize.getWidth()).isEqualTo(21.0);
3833
}
3934

40-
@Test
35+
@Test
4136
void verifiesPageSizeA4() {
42-
PageSize pageSize = PageSize.A4;
43-
assertThat(pageSize.getHeight()).isEqualTo(HEIGHT);
44-
assertThat(pageSize.getWidth()).isEqualTo(WIDTH);
37+
PageSize pageSize = PageSize.setPageSize(PageSize.ISO_A4);
38+
assertThat(pageSize.getHeight()).isEqualTo(29.7);
39+
assertThat(pageSize.getWidth()).isEqualTo(21.0);
4540
}
4641

47-
@Test
42+
@Test
4843
void verifiesPageSizeLegal() {
49-
PageSize pageSize = PageSize.LEGAL;
50-
assertThat(pageSize.getHeight()).isEqualTo(HEIGHT);
51-
assertThat(pageSize.getWidth()).isEqualTo(WIDTH);
44+
PageSize pageSize = PageSize.setPageSize(PageSize.US_LEGAL);
45+
assertThat(pageSize.getHeight()).isEqualTo(35.56);
46+
assertThat(pageSize.getWidth()).isEqualTo(21.59);
5247
}
5348

54-
@Test
49+
@Test
5550
void verifiesPageSizeLetter() {
56-
PageSize pageSize = PageSize.LETTER;
57-
assertThat(pageSize.getHeight()).isEqualTo(HEIGHT);
58-
assertThat(pageSize.getWidth()).isEqualTo(WIDTH);
51+
PageSize pageSize = PageSize.setPageSize(PageSize.US_LETTER);
52+
assertThat(pageSize.getHeight()).isEqualTo(27.94);
53+
assertThat(pageSize.getWidth()).isEqualTo(21.59);
5954
}
6055

6156
@Test
6257
void verifiesPageSizeTabloid() {
63-
PageSize pageSize = PageSize.TABLOID;
64-
assertThat(pageSize.getHeight()).isEqualTo(HEIGHT);
65-
assertThat(pageSize.getWidth()).isEqualTo(WIDTH);
58+
PageSize pageSize = PageSize.setPageSize(PageSize.ANSI_TABLOID);
59+
assertThat(pageSize.getHeight()).isEqualTo(43.18);
60+
assertThat(pageSize.getWidth()).isEqualTo(27.94);
6661
}
67-
6862
}

0 commit comments

Comments
 (0)