Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2016 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.transition.cts;
     17 
     18 import android.graphics.Path;
     19 import android.graphics.PathMeasure;
     20 import android.transition.ArcMotion;
     21 
     22 import junit.framework.TestCase;
     23 
     24 public class ArcMotionTest extends PathMotionTest {
     25 
     26     public void test90Quadrants() throws Throwable {
     27         ArcMotion arcMotion = new ArcMotion();
     28         arcMotion.setMaximumAngle(90);
     29 
     30         Path expected = arcWithPoint(0, 100, 100, 0, 100, 100);
     31         Path path = arcMotion.getPath(0, 100, 100, 0);
     32         assertPathMatches(expected, path);
     33 
     34         expected = arcWithPoint(100, 0, 0, -100, 100, -100);
     35         path = arcMotion.getPath(100, 0, 0, -100);
     36         assertPathMatches(expected, path);
     37 
     38         expected = arcWithPoint(0, -100, -100, 0, -100, -100);
     39         path = arcMotion.getPath(0, -100, -100, 0);
     40         assertPathMatches(expected, path);
     41 
     42         expected = arcWithPoint(-100, 0, 0, 100, -100, 100);
     43         path = arcMotion.getPath(-100, 0, 0, 100);
     44         assertPathMatches(expected, path);
     45     }
     46 
     47     public void test345Triangles() throws Throwable {
     48         // 3-4-5 triangles are easy to calculate the control points
     49         ArcMotion arcMotion = new ArcMotion();
     50         arcMotion.setMaximumAngle(90);
     51         Path expected;
     52         Path path;
     53 
     54         expected = arcWithPoint(0, 120, 160, 0, 125, 120);
     55         path = arcMotion.getPath(0, 120, 160, 0);
     56         assertPathMatches(expected, path);
     57 
     58         expected = arcWithPoint(0, 160, 120, 0, 120, 125);
     59         path = arcMotion.getPath(0, 160, 120, 0);
     60         assertPathMatches(expected, path);
     61 
     62         expected = arcWithPoint(-120, 0, 0, 160, -120, 125);
     63         path = arcMotion.getPath(-120, 0, 0, 160);
     64         assertPathMatches(expected, path);
     65 
     66         expected = arcWithPoint(-160, 0, 0, 120, -125, 120);
     67         path = arcMotion.getPath(-160, 0, 0, 120);
     68         assertPathMatches(expected, path);
     69 
     70         expected = arcWithPoint(0, -120, -160, 0, -125, -120);
     71         path = arcMotion.getPath(0, -120, -160, 0);
     72         assertPathMatches(expected, path);
     73 
     74         expected = arcWithPoint(0, -160, -120, 0, -120, -125);
     75         path = arcMotion.getPath(0, -160, -120, 0);
     76         assertPathMatches(expected, path);
     77 
     78         expected = arcWithPoint(120, 0, 0, -160, 120, -125);
     79         path = arcMotion.getPath(120, 0, 0, -160);
     80         assertPathMatches(expected, path);
     81 
     82         expected = arcWithPoint(160, 0, 0, -120, 125, -120);
     83         path = arcMotion.getPath(160, 0, 0, -120);
     84         assertPathMatches(expected, path);
     85     }
     86 
     87     private Path arcWithPoint(float startX, float startY, float endX, float endY,
     88             float eX, float eY) {
     89         float c1x = (eX + startX)/2;
     90         float c1y = (eY + startY)/2;
     91         float c2x = (eX + endX)/2;
     92         float c2y = (eY + endY)/2;
     93         Path path = new Path();
     94         path.moveTo(startX, startY);
     95         path.cubicTo(c1x, c1y, c2x, c2y, endX, endY);
     96         return path;
     97     }
     98 
     99     public void testMaximumAngle() throws Throwable {
    100         ArcMotion arcMotion = new ArcMotion();
    101         arcMotion.setMaximumAngle(45f);
    102         assertEquals(45f, arcMotion.getMaximumAngle());
    103 
    104         float ratio = (float) Math.tan(Math.PI/8);
    105         float ex = 50 + (50 * ratio);
    106         float ey = ex;
    107 
    108         Path expected = arcWithPoint(0, 100, 100, 0, ex, ey);
    109         Path path = arcMotion.getPath(0, 100, 100, 0);
    110         assertPathMatches(expected, path);
    111     }
    112 
    113     public void testMinimumHorizontalAngle() throws Throwable {
    114         ArcMotion arcMotion = new ArcMotion();
    115         arcMotion.setMinimumHorizontalAngle(45);
    116         assertEquals(45f, arcMotion.getMinimumHorizontalAngle());
    117 
    118         float ey = (float)(Math.tan(Math.PI/8) * 50);
    119         float ex = 50;
    120         Path expected = arcWithPoint(0, 0, 100, 0, ex, ey);
    121         Path path = arcMotion.getPath(0, 0, 100, 0);
    122         assertPathMatches(expected, path);
    123 
    124         // Pretty much the same, but follows a different path.
    125         expected = arcWithPoint(0, 0, 100.001f, 0, ex, ey);
    126         path = arcMotion.getPath(0, 0, 100.001f, 0);
    127         assertPathMatches(expected, path);
    128     }
    129 
    130     public void testMinimumVerticalAngle() throws Throwable {
    131         ArcMotion arcMotion = new ArcMotion();
    132         arcMotion.setMinimumVerticalAngle(45);
    133         assertEquals(45f, arcMotion.getMinimumVerticalAngle());
    134 
    135         float ex = (float)(Math.tan(Math.PI/8) * 50);
    136         float ey = 50;
    137         Path expected = arcWithPoint(0, 0, 0, 100, ex, ey);
    138         Path path = arcMotion.getPath(0, 0, 0, 100);
    139         assertPathMatches(expected, path);
    140 
    141         // Pretty much the same, but follows a different path.
    142         expected = arcWithPoint(0, 0, 0, 100.001f, ex, ey);
    143         path = arcMotion.getPath(0, 0, 0, 100.001f);
    144         assertPathMatches(expected, path);
    145     }
    146 }
    147 
    148