1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.print.pdf.cts; 18 19 import static android.print.test.Utils.assertException; 20 21 import static org.junit.Assert.assertEquals; 22 23 import android.content.Context; 24 import android.graphics.Rect; 25 import android.graphics.pdf.PdfDocument; 26 import android.platform.test.annotations.Presubmit; 27 import android.print.PrintAttributes; 28 import android.print.pdf.PrintedPdfDocument; 29 30 import androidx.test.InstrumentationRegistry; 31 import androidx.test.runner.AndroidJUnit4; 32 33 import org.junit.BeforeClass; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 /** 38 * Tests {@link PrintedPdfDocument}. This class is a subclass of {@link PdfDocument}, hence only the 39 * overridden methods are tested. 40 */ 41 @Presubmit 42 @RunWith(AndroidJUnit4.class) 43 public class PrintedPdfDocumentTest { 44 private static final PrintAttributes.Margins ZERO_MARGINS = new PrintAttributes.Margins(0, 0, 0, 45 0); 46 private static Context sContext; 47 48 @BeforeClass setUp()49 public static void setUp() { 50 sContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 51 } 52 53 @Test createWithNullAttributes()54 public void createWithNullAttributes() throws Throwable { 55 assertException(() -> new PrintedPdfDocument(sContext, null), NullPointerException.class); 56 } 57 58 @Test createWithNullMediaSize()59 public void createWithNullMediaSize() throws Throwable { 60 PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS).build(); 61 assertException(() -> new PrintedPdfDocument(sContext, attr), NullPointerException.class); 62 } 63 64 @Test createWithNullMargins()65 public void createWithNullMargins() throws Throwable { 66 PrintAttributes attr = new PrintAttributes.Builder() 67 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build(); 68 assertException(() -> new PrintedPdfDocument(sContext, attr), 69 NullPointerException.class); 70 } 71 72 @Test createWithNullContext()73 public void createWithNullContext() throws Exception { 74 PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS) 75 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build(); 76 77 // Legacy: Context is not used and not checked for null-ness 78 PrintedPdfDocument doc = new PrintedPdfDocument(null, attr); 79 doc.close(); 80 } 81 82 @Test startPage()83 public void startPage() throws Exception { 84 PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS) 85 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build(); 86 87 PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr); 88 PdfDocument.Page page = doc.startPage(0); 89 doc.finishPage(page); 90 doc.close(); 91 } 92 93 @Test oneMilPageSize()94 public void oneMilPageSize() throws Throwable { 95 PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS) 96 .setMediaSize(new PrintAttributes.MediaSize("oneMil", "oneMil", 1, 1)).build(); 97 98 PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr); 99 100 // We get an illegal argument exception here as a single mil of page size is converted to 0 101 // pts. 102 assertEquals(0, milsToPts(attr.getMediaSize().getHeightMils())); 103 assertException(() -> doc.startPage(0), IllegalArgumentException.class); 104 105 doc.close(); 106 } 107 108 /** 109 * Converts mils (1000th of an inch) to postscript points (72th of an inch). 110 * 111 * @param mils The distance in mils 112 * 113 * @return The distance in Postscript points 114 */ milsToPts(int mils)115 private int milsToPts(int mils) { 116 return (int) (((float) mils / 1000) * 72); 117 } 118 119 @Test getPageWidth()120 public void getPageWidth() throws Exception { 121 PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS) 122 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build(); 123 124 PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr); 125 assertEquals(milsToPts(attr.getMediaSize().getWidthMils()), doc.getPageWidth()); 126 doc.close(); 127 } 128 129 @Test getPageHeight()130 public void getPageHeight() throws Exception { 131 PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS) 132 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build(); 133 134 PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr); 135 assertEquals(milsToPts(attr.getMediaSize().getHeightMils()), doc.getPageHeight()); 136 doc.close(); 137 } 138 139 @Test getContentRect()140 public void getContentRect() throws Exception { 141 PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS) 142 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build(); 143 144 PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr); 145 assertEquals(new Rect(0, 0, milsToPts(attr.getMediaSize().getWidthMils()), 146 milsToPts(attr.getMediaSize().getHeightMils())), doc.getPageContentRect()); 147 doc.close(); 148 } 149 150 @Test getContentRectBigMargins()151 public void getContentRectBigMargins() throws Exception { 152 PrintAttributes.Margins margins = new PrintAttributes.Margins(50, 60, 70, 80); 153 PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(margins) 154 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build(); 155 156 PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr); 157 assertEquals(new Rect(milsToPts(margins.getLeftMils()), milsToPts(margins.getTopMils()), 158 milsToPts(attr.getMediaSize().getWidthMils()) - milsToPts(margins.getRightMils()), 159 milsToPts(attr.getMediaSize().getHeightMils()) - 160 milsToPts(margins.getBottomMils())), doc.getPageContentRect()); 161 doc.close(); 162 } 163 164 @Test getPageHeightAfterClose()165 public void getPageHeightAfterClose() throws Exception { 166 PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS) 167 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build(); 168 169 PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr); 170 doc.close(); 171 assertEquals(milsToPts(attr.getMediaSize().getHeightMils()), doc.getPageHeight()); 172 } 173 174 @Test getPageWidthAfterClose()175 public void getPageWidthAfterClose() throws Exception { 176 PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS) 177 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build(); 178 179 PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr); 180 doc.close(); 181 assertEquals(milsToPts(attr.getMediaSize().getWidthMils()), doc.getPageWidth()); 182 } 183 184 @Test getContentRectAfterClose()185 public void getContentRectAfterClose() throws Exception { 186 PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS) 187 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build(); 188 189 PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr); 190 doc.close(); 191 assertEquals(new Rect(0, 0, milsToPts(attr.getMediaSize().getWidthMils()), 192 milsToPts(attr.getMediaSize().getHeightMils())), doc.getPageContentRect()); 193 } 194 } 195