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.graphics.pdf.cts;
18 
19 import static android.graphics.pdf.cts.Utils.A4_HEIGHT_PTS;
20 import static android.graphics.pdf.cts.Utils.A4_PORTRAIT;
21 import static android.graphics.pdf.cts.Utils.A4_WIDTH_PTS;
22 import static android.graphics.pdf.cts.Utils.renderAndCompare;
23 
24 import android.content.Context;
25 import android.graphics.Matrix;
26 import android.graphics.Rect;
27 import android.graphics.pdf.PdfRenderer;
28 import android.graphics.pdf.PdfRenderer.Page;
29 
30 import androidx.annotation.Nullable;
31 import androidx.annotation.RawRes;
32 import androidx.test.InstrumentationRegistry;
33 import androidx.test.filters.SmallTest;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.junit.runners.Parameterized;
39 
40 import java.util.ArrayList;
41 import java.util.Collection;
42 
43 /**
44  * Test for the {@link PdfRenderer}
45  */
46 @RunWith(Parameterized.class)
47 public class PdfRendererTransformTest {
48     private Context mContext;
49     private int mWidth;
50     private int mHeight;
51     private int mDocRes;
52     private @Nullable Rect mClipping;
53     private @Nullable Matrix mTransformation;
54     private int mRenderMode;
55 
PdfRendererTransformTest(int width, int height, @RawRes int docRes, @Nullable Rect clipping, @Nullable Matrix transformation, int renderMode)56     public PdfRendererTransformTest(int width, int height, @RawRes int docRes,
57             @Nullable Rect clipping, @Nullable Matrix transformation, int renderMode) {
58         mWidth = width;
59         mHeight = height;
60         mDocRes = docRes;
61         mClipping = clipping;
62         mTransformation = transformation;
63         mRenderMode = renderMode;
64     }
65 
66     @Parameterized.Parameters
getParameters()67     public static Collection<Object[]> getParameters() {
68         int[] widths = new int[] { A4_WIDTH_PTS * 3 / 4, A4_WIDTH_PTS, A4_WIDTH_PTS * 4 / 3
69         };
70         int[] heights = new int[] { A4_HEIGHT_PTS * 3 / 4, A4_HEIGHT_PTS, A4_HEIGHT_PTS * 4 / 3
71         };
72         int[] rotations = new int[] { 0, 15, 90, 180 };
73         int[] translations = new int[] { -A4_HEIGHT_PTS / 2, 0, A4_HEIGHT_PTS / 2 };
74         float[] scales = { -0.5f, 0, 1, 1.5f };
75 
76         Collection<Object[]> params = new ArrayList<>();
77 
78         for (int rotation : rotations) {
79             for (float scaleX : scales) {
80                 for (float scaleY : scales) {
81                     for (int translateX : translations) {
82                         for (int translateY : translations) {
83                             Matrix transformation = new Matrix();
84                             if (rotation != 0 || translateX != 0 || translateY != 0
85                                     || scaleX != 0 || scaleY != 0) {
86                                 if (rotation != 0) {
87                                     transformation.postRotate(rotation);
88                                 }
89 
90                                 if (scaleX != 0 || scaleY != 0) {
91                                     transformation.postScale(scaleX, scaleY);
92                                 }
93 
94                                 if (translateX != 0 || translateY != 0) {
95                                     transformation.postTranslate(translateX,
96                                             translateY);
97                                 }
98                             }
99 
100                             for (int width : widths) {
101                                 for (int height : heights) {
102                                     params.add(
103                                             new Object[] { width, height, A4_PORTRAIT, null,
104                                                     transformation, Page.RENDER_MODE_FOR_DISPLAY
105                                             });
106                                 }
107                             }
108                         }
109                     }
110                 }
111             }
112         }
113 
114         return params;
115     }
116 
117     @Before
setup()118     public void setup() {
119         mContext = InstrumentationRegistry.getTargetContext();
120     }
121 
122     // Note that the size annotation refers to the "size" of each individual parameterized run,
123     // and not the "full" run.
124     @SmallTest
125     @Test
test()126     public void test() throws Exception {
127         renderAndCompare(mWidth, mHeight, mDocRes, mClipping, mTransformation, mRenderMode,
128                 mContext);
129     }
130 }
131