Skip to content

Commit 643d9d1

Browse files
yvsvarmapujaganiVietND96
authored
[java] Enhance PageSize class to support for predefined and custom Paper Sizes (#15052)
Co-authored-by: Puja Jagani <[email protected]> Co-authored-by: Viet Nguyen Duc <[email protected]>
1 parent 535f96f commit 643d9d1

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

Diff for: java/src/org/openqa/selenium/print/PageSize.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@
2323

2424
@NullMarked
2525
public class PageSize {
26-
2726
private final double height;
2827
private final double width;
2928

29+
// Reference for predefined page size constants:
30+
// https://www.agooddaytoprint.com/page/paper-size-chart-faq
31+
public static final PageSize ISO_A4 = new PageSize(29.7, 21.0); // ISO_A4 size in cm
32+
public static final PageSize US_LEGAL = new PageSize(35.56, 21.59); // US_LEGAL size in cm
33+
public static final PageSize ANSI_TABLOID = new PageSize(43.18, 27.94); // ANSI_TABLOID size in cm
34+
public static final PageSize US_LETTER = new PageSize(27.94, 21.59); // US_LETTER size in cm
35+
3036
public PageSize() {
31-
// Initialize with defaults. A4 paper size defaults in cms.
32-
this.height = 27.94;
33-
this.width = 21.59;
37+
// Initialize with defaults. ISO_A4 paper size defaults in cms.
38+
this(ISO_A4.getHeight(), ISO_A4.getWidth());
3439
}
3540

3641
public PageSize(double height, double width) {
@@ -46,11 +51,23 @@ public double getWidth() {
4651
return width;
4752
}
4853

54+
public static PageSize setPageSize(PageSize pageSize) {
55+
if (pageSize == null) {
56+
throw new IllegalArgumentException("Page size cannot be null");
57+
}
58+
return new PageSize(pageSize.getHeight(), pageSize.getWidth());
59+
}
60+
4961
public Map<String, Object> toMap() {
5062
final Map<String, Object> options = new HashMap<>(7);
5163
options.put("height", getHeight());
5264
options.put("width", getWidth());
5365

5466
return options;
5567
}
68+
69+
@Override
70+
public String toString() {
71+
return "PageSize[width=" + this.getWidth() + ", height=" + this.getHeight() + "]";
72+
}
5673
}

Diff for: java/test/org/openqa/selenium/print/PageSizeTest.java

+37-5
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,53 @@
1919

2020
import static org.assertj.core.api.Assertions.assertThat;
2121

22+
import org.junit.jupiter.api.BeforeEach;
2223
import org.junit.jupiter.api.Tag;
2324
import org.junit.jupiter.api.Test;
2425

2526
@Tag("UnitTests")
2627
class PageSizeTest {
2728

28-
// Defaults assertion
29-
private static final double HEIGHT = 27.94;
30-
private static final double WIDTH = 21.59;
29+
private PrintOptions printOptions;
30+
31+
@BeforeEach
32+
void setUp() {
33+
printOptions = new PrintOptions();
34+
}
3135

3236
@Test
3337
void setsDefaultHeightWidth() {
3438
PageSize pageSize = new PageSize();
39+
assertThat(pageSize.getHeight()).isEqualTo(29.7);
40+
assertThat(pageSize.getWidth()).isEqualTo(21.0);
41+
}
42+
43+
@Test
44+
void verifiesPageSizeA4() {
45+
46+
printOptions.setPageSize(PageSize.ISO_A4);
47+
assertThat(printOptions.getPageSize().getHeight()).isEqualTo(29.7);
48+
assertThat(printOptions.getPageSize().getWidth()).isEqualTo(21.0);
49+
}
3550

36-
assertThat(pageSize.getHeight()).isEqualTo(HEIGHT);
37-
assertThat(pageSize.getWidth()).isEqualTo(WIDTH);
51+
@Test
52+
void verifiesPageSizeLegal() {
53+
printOptions.setPageSize(PageSize.US_LEGAL);
54+
assertThat(printOptions.getPageSize().getHeight()).isEqualTo(35.56);
55+
assertThat(printOptions.getPageSize().getWidth()).isEqualTo(21.59);
56+
}
57+
58+
@Test
59+
void verifiesPageSizeLetter() {
60+
printOptions.setPageSize(PageSize.US_LETTER);
61+
assertThat(printOptions.getPageSize().getHeight()).isEqualTo(27.94);
62+
assertThat(printOptions.getPageSize().getWidth()).isEqualTo(21.59);
63+
}
64+
65+
@Test
66+
void verifiesPageSizeTabloid() {
67+
printOptions.setPageSize(PageSize.ANSI_TABLOID);
68+
assertThat(printOptions.getPageSize().getHeight()).isEqualTo(43.18);
69+
assertThat(printOptions.getPageSize().getWidth()).isEqualTo(27.94);
3870
}
3971
}

0 commit comments

Comments
 (0)