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 
19 import android.gesture.GesturePoint;
20 import android.gesture.GestureStroke;
21 import android.gesture.OrientedBoundingBox;
22 import android.graphics.Path;
23 import android.graphics.RectF;
24 
25 import junit.framework.TestCase;
26 
27 /**
28  * Simple compatibility unit tests for {@link GestureStroke}
29  */
30 public class GestureStrokeTest extends TestCase {
31 
32     private LineGestureStrokeHelper mHelper;
33 
34     @Override
setUp()35     protected void setUp() throws Exception {
36         mHelper = new LineGestureStrokeHelper();
37     }
38 
39     /**
40      * Test {@link GestureStroke#getPath()} when set of empty points are provided.
41      */
testGetPath_empty()42     public void testGetPath_empty() {
43         GestureStroke emptyStroke = mHelper.createGestureStroke();
44         // null path currently returned
45         assertNull(emptyStroke.getPath());
46     }
47 
48     /**
49      * Test {@link GestureStroke#getPath()} when a single point is provided.
50      */
testGetPath_singlePoint()51     public void testGetPath_singlePoint() {
52         GestureStroke emptyStroke = mHelper.createGestureStroke(new GesturePoint(0, 0, 0));
53         Path emptyPath = emptyStroke.getPath();
54         // a single point is considered a tap and is not empty
55         assertFalse(emptyPath.isEmpty());
56     }
57 
58     /**
59      * Test {@link GestureStroke#getPath()} when a line is provided.
60      */
testGetPath_line()61     public void testGetPath_line() {
62         GestureStroke lineStroke = mHelper.createLineGesture();
63         Path linePath = lineStroke.getPath();
64         mHelper.assertLineBoundingBox(linePath);
65     }
66 
67     /**
68      * Test {@link android.gesture.GestureStroke#toPath(float, float, int)} returns expected results
69      * for a single line, where the given toPath bounds exceeds the expected path.
70      */
testToPath_line()71     public void testToPath_line() {
72         // use the same
73         GestureStroke lineStroke = mHelper.createLineGesture();
74         // bound the path by the endpoint
75         Path linePath = lineStroke.toPath(LineGestureStrokeHelper.LINE_END_POINT,
76                 LineGestureStrokeHelper.LINE_END_POINT, 2);
77 
78         // expect the same results generated by getPath
79         mHelper.assertLineBoundingBox(linePath);
80     }
81 
82     /**
83      * Test for {@link android.gesture.GestureStroke#toPath(float, float, int)}
84      *
85      * Verifies method returns expected results for a single line, where the given toPath bounds
86      * truncates the expected path.
87      */
testToPath_boundedLine()88     public void testToPath_boundedLine() {
89         // use the same
90         GestureStroke lineStroke = mHelper.createLineGesture();
91         // bound the path by the midpoint
92         Path linePath = lineStroke.toPath(LineGestureStrokeHelper.LINE_MIDWAY_POINT,
93                 LineGestureStrokeHelper.LINE_MIDWAY_POINT, 2);
94         RectF bounds = new RectF();
95         linePath.computeBounds(bounds, true);
96         // expect a square bounding box, starting at LINE_START_POINT and bounded by
97         // LINE_QUARTER_POINT
98         assertEquals(LineGestureStrokeHelper.LINE_QUARTER_POINT, bounds.bottom);
99         assertEquals(LineGestureStrokeHelper.LINE_START_POINT, bounds.left);
100         assertEquals(LineGestureStrokeHelper.LINE_QUARTER_POINT, bounds.right);
101         assertEquals(LineGestureStrokeHelper.LINE_START_POINT, bounds.top);
102     }
103 
104     /**
105      * Test method for {@link android.gesture.GestureStroke#computeOrientedBoundingBox()}.
106      *
107      * Verifies method returns expected results for a single line.
108      */
testComputeOrientedBoundingBox()109     public void testComputeOrientedBoundingBox() {
110         GestureStroke line = mHelper.createLineGesture();
111         OrientedBoundingBox box = line.computeOrientedBoundingBox();
112         // expect a center of LINE_MIDWAY_POINT, LINE_MIDWAY_POINT, with a 45-degree orientation
113         assertEquals(LineGestureStrokeHelper.LINE_MIDWAY_POINT, box.centerX);
114         assertEquals(LineGestureStrokeHelper.LINE_MIDWAY_POINT, box.centerY);
115         assertEquals(LineGestureStrokeHelper.LINE_ANGLE, box.orientation);
116     }
117 
118     /**
119      * Verifies that {@link android.gesture.GestureStroke#boundingBox} has expected values for
120      * a simple straight line gesture
121      */
testBoundingBox_line()122     public void testBoundingBox_line() {
123         GestureStroke line = mHelper.createLineGesture();
124         mHelper.assertLineBoundingBox(line.boundingBox);
125     }
126 }
127