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.gallery3d.common;
18
19import android.test.AndroidTestCase;
20import android.test.suitebuilder.annotation.SmallTest;
21import android.util.Log;
22
23public class UtilsTest extends AndroidTestCase {
24    private static final String TAG = "UtilsTest";
25
26    private static final int [] testData = new int [] {
27        /* outWidth, outHeight, minSideLength, maxNumOfPixels, sample size */
28        1, 1, BitmapUtils.UNCONSTRAINED, BitmapUtils.UNCONSTRAINED, 1,
29        1, 1, 1, 1, 1,
30        100, 100, 100, 10000, 1,
31        100, 100, 100, 2500, 2,
32        99, 66, 33, 10000, 2,
33        66, 99, 33, 10000, 2,
34        99, 66, 34, 10000, 1,
35        99, 66, 22, 10000, 4,
36        99, 66, 16, 10000, 4,
37
38        10000, 10000, 20000, 1000000, 16,
39
40        100, 100, 100, 10000, 1, // 1
41        100, 100, 50, 10000, 2,  // 2
42        100, 100, 30, 10000, 4,  // 3->4
43        100, 100, 22, 10000, 4,  // 4
44        100, 100, 20, 10000, 8,  // 5->8
45        100, 100, 11, 10000, 16, // 9->16
46        100, 100, 5,  10000, 24, // 20->24
47        100, 100, 2,  10000, 56, // 50->56
48
49        100, 100, 100, 10000 - 1, 2,                  // a bit less than 1
50        100, 100, 100, 10000 / (2 * 2) - 1, 4,        // a bit less than 2
51        100, 100, 100, 10000 / (3 * 3) - 1, 4,        // a bit less than 3
52        100, 100, 100, 10000 / (4 * 4) - 1, 8,        // a bit less than 4
53        100, 100, 100, 10000 / (8 * 8) - 1, 16,       // a bit less than 8
54        100, 100, 100, 10000 / (16 * 16) - 1, 24,     // a bit less than 16
55        100, 100, 100, 10000 / (24 * 24) - 1, 32,     // a bit less than 24
56        100, 100, 100, 10000 / (32 * 32) - 1, 40,     // a bit less than 32
57
58        640, 480, 480, BitmapUtils.UNCONSTRAINED, 1,  // 1
59        640, 480, 240, BitmapUtils.UNCONSTRAINED, 2,  // 2
60        640, 480, 160, BitmapUtils.UNCONSTRAINED, 4,  // 3->4
61        640, 480, 120, BitmapUtils.UNCONSTRAINED, 4,  // 4
62        640, 480, 96, BitmapUtils.UNCONSTRAINED,  8,  // 5->8
63        640, 480, 80, BitmapUtils.UNCONSTRAINED,  8,  // 6->8
64        640, 480, 60, BitmapUtils.UNCONSTRAINED,  8,  // 8
65        640, 480, 48, BitmapUtils.UNCONSTRAINED, 16,  // 10->16
66        640, 480, 40, BitmapUtils.UNCONSTRAINED, 16,  // 12->16
67        640, 480, 30, BitmapUtils.UNCONSTRAINED, 16,  // 16
68        640, 480, 24, BitmapUtils.UNCONSTRAINED, 24,  // 20->24
69        640, 480, 20, BitmapUtils.UNCONSTRAINED, 24,  // 24
70        640, 480, 16, BitmapUtils.UNCONSTRAINED, 32,  // 30->32
71        640, 480, 12, BitmapUtils.UNCONSTRAINED, 40,  // 40
72        640, 480, 10, BitmapUtils.UNCONSTRAINED, 48,  // 48
73        640, 480, 8, BitmapUtils.UNCONSTRAINED,  64,  // 60->64
74        640, 480, 6, BitmapUtils.UNCONSTRAINED,  80,  // 80
75        640, 480, 4, BitmapUtils.UNCONSTRAINED, 120,  // 120
76        640, 480, 3, BitmapUtils.UNCONSTRAINED, 160,  // 160
77        640, 480, 2, BitmapUtils.UNCONSTRAINED, 240,  // 240
78        640, 480, 1, BitmapUtils.UNCONSTRAINED, 480,  // 480
79
80        640, 480, BitmapUtils.UNCONSTRAINED, BitmapUtils.UNCONSTRAINED, 1,
81        640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480, 1,                  // 1
82        640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 - 1, 2,              // a bit less than 1
83        640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 4, 2,              // 2
84        640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 4 - 1, 4,          // a bit less than 2
85        640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 9, 4,              // 3
86        640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 9 - 1, 4,          // a bit less than 3
87        640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 16, 4,             // 4
88        640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 16 - 1, 8,         // a bit less than 4
89        640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 64, 8,             // 8
90        640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 64 - 1, 16,        // a bit less than 8
91        640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 256, 16,           // 16
92        640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 256 - 1, 24,       // a bit less than 16
93        640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / (24 * 24) - 1, 32, // a bit less than 24
94    };
95
96    @SmallTest
97    public void testComputeSampleSize() {
98
99        for (int i = 0; i < testData.length; i += 5) {
100            int w = testData[i];
101            int h = testData[i + 1];
102            int minSide = testData[i + 2];
103            int maxPixels = testData[i + 3];
104            int sampleSize = testData[i + 4];
105            int result = BitmapUtils.computeSampleSize(w, h, minSide, maxPixels);
106            if (result != sampleSize) {
107                Log.v(TAG, w + "x" + h + ", minSide = " + minSide + ", maxPixels = "
108                        + maxPixels + ", sampleSize = " + sampleSize + ", result = "
109                        + result);
110            }
111            assertTrue(sampleSize == result);
112        }
113    }
114
115    public void testAssert() {
116        // This should not throw an exception.
117        Utils.assertTrue(true);
118
119        // This should throw an exception.
120        try {
121            Utils.assertTrue(false);
122            fail();
123        } catch (AssertionError ex) {
124            // expected.
125        }
126    }
127
128    public void testCheckNotNull() {
129        // These should not throw an expection.
130        Utils.checkNotNull(new Object());
131        Utils.checkNotNull(0);
132        Utils.checkNotNull("");
133
134        // This should throw an expection.
135        try {
136            Utils.checkNotNull(null);
137            fail();
138        } catch (NullPointerException ex) {
139            // expected.
140        }
141    }
142
143    public void testEquals() {
144        Object a = new Object();
145        Object b = new Object();
146
147        assertTrue(Utils.equals(null, null));
148        assertTrue(Utils.equals(a, a));
149        assertFalse(Utils.equals(null, a));
150        assertFalse(Utils.equals(a, null));
151        assertFalse(Utils.equals(a, b));
152    }
153
154    public void testNextPowerOf2() {
155        int[] q = new int[] {1, 2, 3, 4, 5, 6, 10, 65535, (1 << 30) - 1, (1 << 30)};
156        int[] a = new int[] {1, 2, 4, 4, 8, 8, 16, 65536, (1 << 30)    , (1 << 30)};
157
158        for (int i = 0; i < q.length; i++) {
159            assertEquals(a[i], Utils.nextPowerOf2(q[i]));
160        }
161
162        int[] e = new int[] {0, -1, -2, -4, -65536, (1 << 30) + 1, Integer.MAX_VALUE};
163
164        for (int v : e) {
165            try {
166                Utils.nextPowerOf2(v);
167                fail();
168            } catch (IllegalArgumentException ex) {
169                // expected.
170            }
171        }
172    }
173
174    public void testClamp() {
175        assertEquals(1000, Utils.clamp(300, 1000, 2000));
176        assertEquals(1300, Utils.clamp(1300, 1000, 2000));
177        assertEquals(2000, Utils.clamp(2300, 1000, 2000));
178
179        assertEquals(0.125f, Utils.clamp(0.1f, 0.125f, 0.5f));
180        assertEquals(0.25f, Utils.clamp(0.25f, 0.125f, 0.5f));
181        assertEquals(0.5f, Utils.clamp(0.9f, 0.125f, 0.5f));
182    }
183
184    public void testIsOpaque() {
185        assertTrue(Utils.isOpaque(0xFF000000));
186        assertTrue(Utils.isOpaque(0xFFFFFFFF));
187        assertTrue(Utils.isOpaque(0xFF123456));
188
189        assertFalse(Utils.isOpaque(0xFEFFFFFF));
190        assertFalse(Utils.isOpaque(0x8FFFFFFF));
191        assertFalse(Utils.isOpaque(0x00FF0000));
192        assertFalse(Utils.isOpaque(0x5500FF00));
193        assertFalse(Utils.isOpaque(0xAA0000FF));
194    }
195
196    public static void assertFloatEq(float expected, float actual) {
197        if (Math.abs(actual - expected) > 1e-6) {
198            Log.v(TAG, "expected: " + expected + ", actual: " + actual);
199            fail();
200        }
201    }
202}
203