|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Eclipse Platform Contributors and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Eclipse Platform Contributors - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.swt.tests.junit; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | + |
| 24 | +import org.eclipse.swt.graphics.GC; |
| 25 | +import org.eclipse.swt.printing.PDFDocument; |
| 26 | +import org.eclipse.swt.widgets.Display; |
| 27 | +import org.junit.jupiter.api.AfterEach; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.io.TempDir; |
| 31 | + |
| 32 | +/** |
| 33 | + * Automated Test Suite for class org.eclipse.swt.printing.PDFDocument |
| 34 | + * |
| 35 | + * @see org.eclipse.swt.printing.PDFDocument |
| 36 | + */ |
| 37 | +public class Test_org_eclipse_swt_printing_PDFDocument { |
| 38 | + |
| 39 | + Display display; |
| 40 | + PDFDocument pdfDocument; |
| 41 | + GC gc; |
| 42 | + File tempFile; |
| 43 | + |
| 44 | + @TempDir |
| 45 | + Path tempDir; |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + public void setUp() { |
| 49 | + display = Display.getDefault(); |
| 50 | + } |
| 51 | + |
| 52 | + @AfterEach |
| 53 | + public void tearDown() { |
| 54 | + if (gc != null && !gc.isDisposed()) { |
| 55 | + gc.dispose(); |
| 56 | + } |
| 57 | + if (pdfDocument != null && !pdfDocument.isDisposed()) { |
| 58 | + pdfDocument.dispose(); |
| 59 | + } |
| 60 | + if (tempFile != null && tempFile.exists()) { |
| 61 | + tempFile.delete(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void test_createPDFDocumentWithHelloWorld() throws IOException { |
| 67 | + // Create a temporary file for the PDF |
| 68 | + tempFile = Files.createTempFile(tempDir, "test", ".pdf").toFile(); |
| 69 | + String filename = tempFile.getAbsolutePath(); |
| 70 | + |
| 71 | + // Create PDF document with standard letter size (612 x 792 points) |
| 72 | + pdfDocument = new PDFDocument(filename, 612, 792); |
| 73 | + assertNotNull(pdfDocument, "PDFDocument should be created"); |
| 74 | + |
| 75 | + // Create a GC on the PDF document |
| 76 | + gc = new GC(pdfDocument); |
| 77 | + assertNotNull(gc, "GC should be created on PDFDocument"); |
| 78 | + |
| 79 | + // Draw "hello world" text |
| 80 | + gc.drawText("hello world", 100, 100); |
| 81 | + |
| 82 | + // Dispose of resources to finalize the PDF |
| 83 | + gc.dispose(); |
| 84 | + gc = null; |
| 85 | + pdfDocument.dispose(); |
| 86 | + pdfDocument = null; |
| 87 | + |
| 88 | + // Verify the PDF file was created and is not empty |
| 89 | + assertTrue(tempFile.exists(), "PDF file should exist"); |
| 90 | + assertTrue(tempFile.length() > 0, "PDF file should not be empty"); |
| 91 | + |
| 92 | + // Verify PDF magic bytes and content |
| 93 | + byte[] fileContent = Files.readAllBytes(tempFile.toPath()); |
| 94 | + assertTrue(fileContent.length >= 5, "PDF file should have at least 5 bytes for header"); |
| 95 | + |
| 96 | + // Check for PDF magic bytes: %PDF- |
| 97 | + String headerString = new String(fileContent, 0, Math.min(5, fileContent.length)); |
| 98 | + assertTrue(headerString.startsWith("%PDF-"), |
| 99 | + "PDF file should start with %PDF- magic bytes, but got: " + headerString); |
| 100 | + |
| 101 | + // Check if "hello world" appears in the PDF content |
| 102 | + // Convert only as much as needed to search for the text |
| 103 | + String content = new String(fileContent, 0, Math.min(fileContent.length, 10000)); |
| 104 | + assertTrue(content.contains("hello world"), |
| 105 | + "PDF content should contain 'hello world' text"); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void test_createPDFDocumentMultiplePages() throws IOException { |
| 110 | + // Create a temporary file for the PDF |
| 111 | + tempFile = Files.createTempFile(tempDir, "test_multipage", ".pdf").toFile(); |
| 112 | + String filename = tempFile.getAbsolutePath(); |
| 113 | + |
| 114 | + // Create PDF document |
| 115 | + pdfDocument = new PDFDocument(filename, 612, 792); |
| 116 | + gc = new GC(pdfDocument); |
| 117 | + |
| 118 | + // Draw on first page |
| 119 | + gc.drawText("Page 1", 100, 100); |
| 120 | + |
| 121 | + // Create a new page |
| 122 | + pdfDocument.newPage(); |
| 123 | + |
| 124 | + // Draw on second page |
| 125 | + gc.drawText("Page 2", 100, 100); |
| 126 | + |
| 127 | + // Dispose resources |
| 128 | + gc.dispose(); |
| 129 | + gc = null; |
| 130 | + pdfDocument.dispose(); |
| 131 | + pdfDocument = null; |
| 132 | + |
| 133 | + // Verify the PDF file was created |
| 134 | + assertTrue(tempFile.exists(), "PDF file should exist"); |
| 135 | + assertTrue(tempFile.length() > 0, "PDF file should not be empty"); |
| 136 | + |
| 137 | + // Verify PDF magic bytes and content |
| 138 | + byte[] fileContent = Files.readAllBytes(tempFile.toPath()); |
| 139 | + assertTrue(fileContent.length >= 5, "PDF file should have at least 5 bytes for header"); |
| 140 | + String headerString = new String(fileContent, 0, Math.min(5, fileContent.length)); |
| 141 | + assertTrue(headerString.startsWith("%PDF-"), |
| 142 | + "PDF file should start with %PDF- magic bytes"); |
| 143 | + |
| 144 | + // Verify both page texts are present in the PDF |
| 145 | + String content = new String(fileContent, 0, Math.min(fileContent.length, 10000)); |
| 146 | + assertTrue(content.contains("Page 1"), |
| 147 | + "PDF content should contain 'Page 1' text"); |
| 148 | + assertTrue(content.contains("Page 2"), |
| 149 | + "PDF content should contain 'Page 2' text"); |
| 150 | + } |
| 151 | +} |
0 commit comments