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