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.print.PrintAttributes;
27 import android.print.pdf.PrintedPdfDocument;
28 
29 import androidx.test.InstrumentationRegistry;
30 import androidx.test.runner.AndroidJUnit4;
31 
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 /**
37  * Tests {@link PrintedPdfDocument}. This class is a subclass of {@link PdfDocument}, hence only the
38  * overridden methods are tested.
39  */
40 @RunWith(AndroidJUnit4.class)
41 public class PrintedPdfDocumentTest {
42     private static final PrintAttributes.Margins ZERO_MARGINS = new PrintAttributes.Margins(0, 0, 0,
43             0);
44     private static Context sContext;
45 
46     @BeforeClass
setUp()47     public static void setUp() {
48         sContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
49     }
50 
51     @Test
createWithNullAttributes()52     public void createWithNullAttributes() throws Throwable {
53         assertException(() -> new PrintedPdfDocument(sContext, null), NullPointerException.class);
54     }
55 
56     @Test
createWithNullMediaSize()57     public void createWithNullMediaSize() throws Throwable {
58         PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS).build();
59         assertException(() -> new PrintedPdfDocument(sContext, attr), NullPointerException.class);
60     }
61 
62     @Test
createWithNullMargins()63     public void createWithNullMargins() throws Throwable {
64         PrintAttributes attr = new PrintAttributes.Builder()
65                 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build();
66         assertException(() -> new PrintedPdfDocument(sContext, attr),
67                 NullPointerException.class);
68     }
69 
70     @Test
createWithNullContext()71     public void createWithNullContext() throws Exception {
72         PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS)
73                 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build();
74 
75         // Legacy: Context is not used and not checked for null-ness
76         PrintedPdfDocument doc = new PrintedPdfDocument(null, attr);
77         doc.close();
78     }
79 
80     @Test
startPage()81     public void startPage() throws Exception {
82         PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS)
83                 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build();
84 
85         PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr);
86         PdfDocument.Page page = doc.startPage(0);
87         doc.finishPage(page);
88         doc.close();
89     }
90 
91     @Test
oneMilPageSize()92     public void oneMilPageSize() throws Throwable {
93         PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS)
94                 .setMediaSize(new PrintAttributes.MediaSize("oneMil", "oneMil", 1, 1)).build();
95 
96         PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr);
97 
98         // We get an illegal argument exception here as a single mil of page size is converted to 0
99         // pts.
100         assertEquals(0, milsToPts(attr.getMediaSize().getHeightMils()));
101         assertException(() -> doc.startPage(0), IllegalArgumentException.class);
102 
103         doc.close();
104     }
105 
106     /**
107      * Converts mils (1000th of an inch) to postscript points (72th of an inch).
108      *
109      * @param mils The distance in mils
110      *
111      * @return The distance in Postscript points
112      */
milsToPts(int mils)113     private int milsToPts(int mils) {
114         return (int) (((float) mils / 1000) * 72);
115     }
116 
117     @Test
getPageWidth()118     public void getPageWidth() throws Exception {
119         PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS)
120                 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build();
121 
122         PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr);
123         assertEquals(milsToPts(attr.getMediaSize().getWidthMils()), doc.getPageWidth());
124         doc.close();
125     }
126 
127     @Test
getPageHeight()128     public void getPageHeight() throws Exception {
129         PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS)
130                 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build();
131 
132         PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr);
133         assertEquals(milsToPts(attr.getMediaSize().getHeightMils()), doc.getPageHeight());
134         doc.close();
135     }
136 
137     @Test
getContentRect()138     public void getContentRect() throws Exception {
139         PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS)
140                 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build();
141 
142         PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr);
143         assertEquals(new Rect(0, 0, milsToPts(attr.getMediaSize().getWidthMils()),
144                 milsToPts(attr.getMediaSize().getHeightMils())), doc.getPageContentRect());
145         doc.close();
146     }
147 
148     @Test
getContentRectBigMargins()149     public void getContentRectBigMargins() throws Exception {
150         PrintAttributes.Margins margins = new PrintAttributes.Margins(50, 60, 70, 80);
151         PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(margins)
152                 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build();
153 
154         PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr);
155         assertEquals(new Rect(milsToPts(margins.getLeftMils()), milsToPts(margins.getTopMils()),
156                 milsToPts(attr.getMediaSize().getWidthMils()) - milsToPts(margins.getRightMils()),
157                 milsToPts(attr.getMediaSize().getHeightMils()) -
158                         milsToPts(margins.getBottomMils())), doc.getPageContentRect());
159         doc.close();
160     }
161 
162     @Test
getPageHeightAfterClose()163     public void getPageHeightAfterClose() throws Exception {
164         PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS)
165                 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build();
166 
167         PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr);
168         doc.close();
169         assertEquals(milsToPts(attr.getMediaSize().getHeightMils()), doc.getPageHeight());
170     }
171 
172     @Test
getPageWidthAfterClose()173     public void getPageWidthAfterClose() throws Exception {
174         PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS)
175                 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build();
176 
177         PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr);
178         doc.close();
179         assertEquals(milsToPts(attr.getMediaSize().getWidthMils()), doc.getPageWidth());
180     }
181 
182     @Test
getContentRectAfterClose()183     public void getContentRectAfterClose() throws Exception {
184         PrintAttributes attr = new PrintAttributes.Builder().setMinMargins(ZERO_MARGINS)
185                 .setMediaSize(PrintAttributes.MediaSize.ISO_A4).build();
186 
187         PrintedPdfDocument doc = new PrintedPdfDocument(sContext, attr);
188         doc.close();
189         assertEquals(new Rect(0, 0, milsToPts(attr.getMediaSize().getWidthMils()),
190                 milsToPts(attr.getMediaSize().getHeightMils())), doc.getPageContentRect());
191     }
192 }
193