diff --git a/java/src/org/openqa/selenium/print/PageSize.java b/java/src/org/openqa/selenium/print/PageSize.java index 664a336ff1edc..a673fda24ec45 100644 --- a/java/src/org/openqa/selenium/print/PageSize.java +++ b/java/src/org/openqa/selenium/print/PageSize.java @@ -23,14 +23,19 @@ @NullMarked public class PageSize { - private final double height; private final double width; + // Reference for predefined page size constants: + // https://www.agooddaytoprint.com/page/paper-size-chart-faq + public static final PageSize ISO_A4 = new PageSize(29.7, 21.0); // ISO_A4 size in cm + public static final PageSize US_LEGAL = new PageSize(35.56, 21.59); // US_LEGAL size in cm + public static final PageSize ANSI_TABLOID = new PageSize(43.18, 27.94); // ANSI_TABLOID size in cm + public static final PageSize US_LETTER = new PageSize(27.94, 21.59); // US_LETTER size in cm + public PageSize() { - // Initialize with defaults. A4 paper size defaults in cms. - this.height = 27.94; - this.width = 21.59; + // Initialize with defaults. ISO_A4 paper size defaults in cms. + this(ISO_A4.getHeight(), ISO_A4.getWidth()); } public PageSize(double height, double width) { @@ -46,6 +51,13 @@ public double getWidth() { return width; } + public static PageSize setPageSize(PageSize pageSize) { + if (pageSize == null) { + throw new IllegalArgumentException("Page size cannot be null"); + } + return new PageSize(pageSize.getHeight(), pageSize.getWidth()); + } + public Map toMap() { final Map options = new HashMap<>(7); options.put("height", getHeight()); @@ -53,4 +65,9 @@ public Map toMap() { return options; } + + @Override + public String toString() { + return "PageSize[width=" + this.getWidth() + ", height=" + this.getHeight() + "]"; + } } diff --git a/java/test/org/openqa/selenium/print/PageSizeTest.java b/java/test/org/openqa/selenium/print/PageSizeTest.java index 07a85dc60301e..ec8240f73c309 100644 --- a/java/test/org/openqa/selenium/print/PageSizeTest.java +++ b/java/test/org/openqa/selenium/print/PageSizeTest.java @@ -19,21 +19,53 @@ import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @Tag("UnitTests") class PageSizeTest { - // Defaults assertion - private static final double HEIGHT = 27.94; - private static final double WIDTH = 21.59; + private PrintOptions printOptions; + + @BeforeEach + void setUp() { + printOptions = new PrintOptions(); + } @Test void setsDefaultHeightWidth() { PageSize pageSize = new PageSize(); + assertThat(pageSize.getHeight()).isEqualTo(29.7); + assertThat(pageSize.getWidth()).isEqualTo(21.0); + } + + @Test + void verifiesPageSizeA4() { + + printOptions.setPageSize(PageSize.ISO_A4); + assertThat(printOptions.getPageSize().getHeight()).isEqualTo(29.7); + assertThat(printOptions.getPageSize().getWidth()).isEqualTo(21.0); + } - assertThat(pageSize.getHeight()).isEqualTo(HEIGHT); - assertThat(pageSize.getWidth()).isEqualTo(WIDTH); + @Test + void verifiesPageSizeLegal() { + printOptions.setPageSize(PageSize.US_LEGAL); + assertThat(printOptions.getPageSize().getHeight()).isEqualTo(35.56); + assertThat(printOptions.getPageSize().getWidth()).isEqualTo(21.59); + } + + @Test + void verifiesPageSizeLetter() { + printOptions.setPageSize(PageSize.US_LETTER); + assertThat(printOptions.getPageSize().getHeight()).isEqualTo(27.94); + assertThat(printOptions.getPageSize().getWidth()).isEqualTo(21.59); + } + + @Test + void verifiesPageSizeTabloid() { + printOptions.setPageSize(PageSize.ANSI_TABLOID); + assertThat(printOptions.getPageSize().getHeight()).isEqualTo(43.18); + assertThat(printOptions.getPageSize().getWidth()).isEqualTo(27.94); } }