1 /*
2  * Copyright (C) 2010 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 com.android.camera.unittest;
18 
19 import android.graphics.Matrix;
20 
21 import androidx.test.filters.SmallTest;
22 
23 import com.android.camera.Util;
24 
25 import junit.framework.TestCase;
26 
27 @SmallTest
28 public class CameraTest extends TestCase {
testRoundOrientation()29     public void testRoundOrientation() {
30         int h = Util.ORIENTATION_HYSTERESIS;
31         assertEquals(0, Util.roundOrientation(0, 0));
32         assertEquals(0, Util.roundOrientation(359, 0));
33         assertEquals(0, Util.roundOrientation(0 + 44 + h, 0));
34         assertEquals(90, Util.roundOrientation(0 + 45 + h, 0));
35         assertEquals(0, Util.roundOrientation(360 - 44 - h, 0));
36         assertEquals(270, Util.roundOrientation(360 - 45 - h, 0));
37 
38         assertEquals(90, Util.roundOrientation(90, 90));
39         assertEquals(90, Util.roundOrientation(90 + 44 + h, 90));
40         assertEquals(180, Util.roundOrientation(90 + 45 + h, 90));
41         assertEquals(90, Util.roundOrientation(90 - 44 - h, 90));
42         assertEquals(0, Util.roundOrientation(90 - 45 - h, 90));
43 
44         assertEquals(180, Util.roundOrientation(180, 180));
45         assertEquals(180, Util.roundOrientation(180 + 44 + h, 180));
46         assertEquals(270, Util.roundOrientation(180 + 45 + h, 180));
47         assertEquals(180, Util.roundOrientation(180 - 44 - h, 180));
48         assertEquals(90, Util.roundOrientation(180 - 45 - h, 180));
49 
50         assertEquals(270, Util.roundOrientation(270, 270));
51         assertEquals(270, Util.roundOrientation(270 + 44 + h, 270));
52         assertEquals(0, Util.roundOrientation(270 + 45 + h, 270));
53         assertEquals(270, Util.roundOrientation(270 - 44 - h, 270));
54         assertEquals(180, Util.roundOrientation(270 - 45 - h, 270));
55 
56         assertEquals(90, Util.roundOrientation(90, 0));
57         assertEquals(180, Util.roundOrientation(180, 0));
58         assertEquals(270, Util.roundOrientation(270, 0));
59 
60         assertEquals(0, Util.roundOrientation(0, 90));
61         assertEquals(180, Util.roundOrientation(180, 90));
62         assertEquals(270, Util.roundOrientation(270, 90));
63 
64         assertEquals(0, Util.roundOrientation(0, 180));
65         assertEquals(90, Util.roundOrientation(90, 180));
66         assertEquals(270, Util.roundOrientation(270, 180));
67 
68         assertEquals(0, Util.roundOrientation(0, 270));
69         assertEquals(90, Util.roundOrientation(90, 270));
70         assertEquals(180, Util.roundOrientation(180, 270));
71     }
72 
testPrepareMatrix()73     public void testPrepareMatrix() {
74         Matrix matrix = new Matrix();
75         float[] points;
76         int[] expected;
77 
78         Util.prepareMatrix(matrix, false, 0, 800, 480);
79         points = new float[] {-1000, -1000, 0, 0, 1000, 1000, 0, 1000, -750, 250};
80         expected = new int[] {0, 0, 400, 240, 800, 480, 400, 480, 100, 300};
81         matrix.mapPoints(points);
82         assertEquals(expected, points);
83 
84         Util.prepareMatrix(matrix, false, 90, 800, 480);
85         points = new float[] {-1000, -1000,   0,   0, 1000, 1000, 0, 1000, -750, 250};
86         expected = new int[] {800, 0, 400, 240, 0, 480, 0, 240, 300, 60};
87         matrix.mapPoints(points);
88         assertEquals(expected, points);
89 
90         Util.prepareMatrix(matrix, false, 180, 800, 480);
91         points = new float[] {-1000, -1000, 0, 0, 1000, 1000, 0, 1000, -750, 250};
92         expected = new int[] {800, 480, 400, 240, 0, 0, 400, 0, 700, 180};
93         matrix.mapPoints(points);
94         assertEquals(expected, points);
95 
96         Util.prepareMatrix(matrix, true, 180, 800, 480);
97         points = new float[] {-1000, -1000, 0, 0, 1000, 1000, 0, 1000, -750, 250};
98         expected = new int[] {0, 480, 400, 240, 800, 0, 400, 0, 100, 180};
99         matrix.mapPoints(points);
100         assertEquals(expected, points);
101     }
102 
assertEquals(int expected[], float[] actual)103     private void assertEquals(int expected[], float[] actual) {
104         for (int i = 0; i < expected.length; i++) {
105             assertEquals("Array index " + i + " mismatch", expected[i], Math.round(actual[i]));
106         }
107     }
108 }
109