1package com.xtremelabs.robolectric.shadows;
2
3import android.graphics.Path;
4import com.xtremelabs.robolectric.internal.Implementation;
5import com.xtremelabs.robolectric.internal.Implements;
6
7import java.util.ArrayList;
8import java.util.List;
9
10import static com.xtremelabs.robolectric.shadows.ShadowPath.Point.Type.LINE_TO;
11import static com.xtremelabs.robolectric.shadows.ShadowPath.Point.Type.MOVE_TO;
12
13/**
14 * Shadow of {@code Path} that contains a simplified implementation of the original class that only supports
15 * straight-line {@code Path}s.
16 */
17@SuppressWarnings({"UnusedDeclaration"})
18@Implements(Path.class)
19public class ShadowPath {
20    private List<Point> points = new ArrayList<Point>();
21    private List<Point> pointsMovedTo = new ArrayList<Point>();
22    private List<Point> pointsLinedTo = new ArrayList<Point>();
23    private Point wasMovedTo;
24    private String quadDescription = "";
25
26    @Implementation
27    public void moveTo(float x, float y) {
28        Point p = new Point(x, y, MOVE_TO);
29        points.add(p);
30        wasMovedTo = p;
31    }
32
33    @Implementation
34    public void lineTo(float x, float y) {
35        Point point = new Point(x, y, LINE_TO);
36        points.add(point);
37    }
38
39    @Implementation
40    public void quadTo(float x1, float y1, float x2, float y2) {
41    	quadDescription = "Add a quadratic bezier from last point, approaching (" + x1 + "," + y1 + "), " +
42    			"ending at (" +x2+","+ y2 + ")";
43    }
44
45    public String getQuadDescription() {
46    	return quadDescription;
47    }
48
49    /**
50     * Non-Android accessor.
51     *
52     * @return all the points that have been added to the {@code Path}
53     */
54    public List<Point> getPoints() {
55        return points;
56    }
57
58    /**
59     * Non-Android accessor.
60     *
61     * @return whether the {@link #moveTo(float, float)} method was called
62     */
63    public Point getWasMovedTo() {
64        return wasMovedTo;
65    }
66
67    public static class Point {
68        float x, y;
69        private Type type;
70
71        public enum Type {
72            MOVE_TO,
73            LINE_TO
74        }
75
76        public Point(float x, float y, Type type) {
77            this.x = x;
78            this.y = y;
79            this.type = type;
80        }
81
82        @Override
83        public boolean equals(Object o) {
84            if (this == o) return true;
85            if (!(o instanceof Point)) return false;
86
87            Point point = (Point) o;
88
89            if (Float.compare(point.x, x) != 0) return false;
90            if (Float.compare(point.y, y) != 0) return false;
91            if (type != point.type) return false;
92
93            return true;
94        }
95
96        @Override
97        public int hashCode() {
98            int result = (x != +0.0f ? Float.floatToIntBits(x) : 0);
99            result = 31 * result + (y != +0.0f ? Float.floatToIntBits(y) : 0);
100            result = 31 * result + (type != null ? type.hashCode() : 0);
101            return result;
102        }
103
104        @Override
105        public String toString() {
106            return "Point(" + x + "," + y + "," + type + ")";
107        }
108
109        public float getX() {
110        	return x;
111        }
112
113        public float getY() {
114        	return y;
115        }
116
117        public Type getType() {
118            return type;
119        }
120    }
121}
122