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