1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16package com.android.launcher3;
17
18import android.graphics.PointF;
19import android.test.AndroidTestCase;
20import android.test.suitebuilder.annotation.SmallTest;
21import android.util.Log;
22
23import com.android.launcher3.DeviceProfile;
24import com.android.launcher3.InvariantDeviceProfile;
25import com.android.launcher3.util.FocusLogic;
26
27import java.util.ArrayList;
28
29/**
30 * Tests the {@link DeviceProfile} and {@link InvariantDeviceProfile}.
31 */
32@SmallTest
33public class InvariantDeviceProfileTest extends AndroidTestCase {
34
35    private static final String TAG = "DeviceProfileTest";
36    private static final boolean DEBUG = false;
37
38    private InvariantDeviceProfile mInvariantProfile;
39    private ArrayList<InvariantDeviceProfile> mPredefinedDeviceProfiles;
40
41    @Override
42    protected void setUp() throws Exception {
43        super.setUp();
44        mInvariantProfile = new InvariantDeviceProfile(getContext());
45        mPredefinedDeviceProfiles = mInvariantProfile.getPredefinedDeviceProfiles();
46    }
47
48    @Override
49    protected void tearDown() throws Exception {
50        // Nothing to tear down as this class only tests static methods.
51    }
52
53    public void testFindClosestDeviceProfile2() {
54        for (InvariantDeviceProfile idf: mPredefinedDeviceProfiles) {
55            ArrayList<InvariantDeviceProfile> closestProfiles =
56                    mInvariantProfile.findClosestDeviceProfiles(
57                            idf.minWidthDps, idf.minHeightDps, mPredefinedDeviceProfiles);
58            assertTrue(closestProfiles.get(0).equals(idf));
59        }
60    }
61
62    /**
63     * Used to print out how the invDistWeightedInterpolate works between device profiles to
64     * tweak the two constants that control how the interpolation curve is shaped.
65     */
66    public void testInvInterpolation() {
67
68        InvariantDeviceProfile p1 = mPredefinedDeviceProfiles.get(7); // e.g., Large Phone
69        InvariantDeviceProfile p2 = mPredefinedDeviceProfiles.get(8); // e.g., Nexus 7
70
71        ArrayList<PointF> pts = createInterpolatedPoints(
72                new PointF(p1.minWidthDps, p1.minHeightDps),
73                new PointF(p2.minWidthDps, p2.minHeightDps),
74                20f);
75
76        for (int i = 0; i < pts.size(); i++) {
77            ArrayList<InvariantDeviceProfile> closestProfiles =
78                    mInvariantProfile.findClosestDeviceProfiles(
79                            pts.get(i).x, pts.get(i).y, mPredefinedDeviceProfiles);
80            InvariantDeviceProfile result =
81                    mInvariantProfile.invDistWeightedInterpolate(
82                            pts.get(i).x, pts.get(i).y, closestProfiles);
83            if (DEBUG) {
84                Log.d(TAG, String.format("width x height = (%f, %f)] iconSize = %f",
85                        pts.get(i).x, pts.get(i).y, result.iconSize));
86            }
87        }
88    }
89
90    private ArrayList<PointF> createInterpolatedPoints(PointF a, PointF b, float numPts) {
91        ArrayList<PointF> result = new ArrayList<PointF>();
92        result.add(a);
93        for (float i = 1; i < numPts; i = i + 1.0f) {
94            result.add(new PointF((b.x * i +  a.x * (numPts - i)) / numPts,
95                    (b.y * i + a.y * (numPts - i)) / numPts));
96        }
97        result.add(b);
98        return result;
99    }
100
101    /**
102     * Ensures that system calls (e.g., WindowManager, DisplayMetrics) that require contexts are
103     * properly working to generate minimum width and height of the display.
104     */
105    public void test_hammerhead() {
106        if (!android.os.Build.DEVICE.equals("hammerhead")) {
107            return;
108        }
109        assertEquals(mInvariantProfile.numRows, 4);
110        assertEquals(mInvariantProfile.numColumns, 4);
111        assertEquals((int) mInvariantProfile.numHotseatIcons, 5);
112
113        DeviceProfile landscapeProfile = mInvariantProfile.landscapeProfile;
114        DeviceProfile portraitProfile = mInvariantProfile.portraitProfile;
115
116        assertEquals(portraitProfile.allAppsNumCols, 3);
117        assertEquals(landscapeProfile.allAppsNumCols, 5); // not used
118    }
119
120    // Add more tests for other devices, however, running them once on a single device is enough
121    // for verifying that for a platform version, the WindowManager and DisplayMetrics is
122    // working as intended.
123}
124