1 /*
2  * Copyright (C) 2009 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 package android.gesture.cts;
17 
18 import android.gesture.GesturePoint;
19 import android.gesture.GestureStroke;
20 import android.graphics.Path;
21 import android.graphics.RectF;
22 
23 import java.util.ArrayList;
24 
25 import junit.framework.Assert;
26 
27 /**
28  * Helper class that generates a simple line GestureStroke, and provides common methods
29  * to validate values calculated from it.
30  */
31 class LineGestureStrokeHelper {
32 
33     // point constants for 45 degree line gesture
34     // points have identical x and y coordinates
35     final static float LINE_START_POINT = 0;
36     final static int START_TIMESTAMP = 0;
37     final static float LINE_END_POINT = 20;
38     final static float LINE_MIDWAY_POINT = LINE_END_POINT/2;
39     final static float LINE_QUARTER_POINT = LINE_MIDWAY_POINT/2;
40     final static int END_TIMESTAMP = 1;
41     final static float LINE_ANGLE = 45;
42 
43     /**
44      * Creates a standard straight line gesture starting from the {@link LINE_START_POINT} and
45      * {@link LINE_END_POINT}
46      */
createLineGesture()47     GestureStroke createLineGesture() {
48         return createGestureStroke(
49                 new GesturePoint(LINE_START_POINT, LINE_START_POINT, START_TIMESTAMP),
50                 new GesturePoint(LINE_END_POINT, LINE_END_POINT, END_TIMESTAMP));
51     }
52 
53     /**
54      * Helper method to assert expected values for a path derived from createLineGesture
55      *
56      * @throws {@link junit.framework.AssertionFailedError} if linePath attributes differs from
57      * expected values
58      */
assertLineBoundingBox(Path linePath)59     void assertLineBoundingBox(Path linePath) {
60         Assert.assertFalse(linePath.isEmpty());
61         RectF bounds = new RectF();
62         linePath.computeBounds(bounds, true);
63         // TODO: is it a compatability requirement that these numbers be consistent?
64         // expect a square bounding box, starting at LINE_START_POINT and bounded by
65         // LINE_MIDWAY_POINT
66         Assert.assertEquals(LINE_MIDWAY_POINT, bounds.bottom);
67         Assert.assertEquals(LINE_START_POINT, bounds.left);
68         Assert.assertEquals(LINE_MIDWAY_POINT, bounds.right);
69         Assert.assertEquals(LINE_START_POINT, bounds.top);
70     }
71 
72     /**
73      * Helper method to assert expected values for a bounds derived from createLineGesture
74      *
75      * @throws AssertionFailedError if linePath attributes differs from expected values
76      */
assertLineBoundingBox(RectF bounds)77     void assertLineBoundingBox(RectF bounds) {
78         // expect a square bounding box, starting at LINE_START_POINT and bounded by
79         // LINE_END_POINT
80         Assert.assertEquals(LINE_END_POINT, bounds.bottom);
81         Assert.assertEquals(LINE_START_POINT, bounds.left);
82         Assert.assertEquals(LINE_END_POINT, bounds.right);
83         Assert.assertEquals(LINE_START_POINT, bounds.top);
84     }
85 
86     /**
87      * Helper method for creating a gesture stroke
88      */
createGestureStroke(GesturePoint... points)89     GestureStroke createGestureStroke(GesturePoint... points) {
90         ArrayList<GesturePoint> list = new ArrayList<GesturePoint>(points.length);
91         for (GesturePoint point : points) {
92             list.add(point);
93         }
94         return new GestureStroke(list);
95     }
96 }
97