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