Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright 2018 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 
     17 package androidx.core.graphics;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertNotEquals;
     21 import static org.junit.Assert.fail;
     22 
     23 import android.graphics.Path;
     24 import android.graphics.PointF;
     25 import android.support.test.filters.SdkSuppress;
     26 import android.support.test.filters.SmallTest;
     27 
     28 import org.junit.Test;
     29 
     30 @SmallTest
     31 public final class PathUtilsTest {
     32     @SdkSuppress(minSdkVersion = 26)
     33     @Test public void flattenEmptyPath() {
     34         for (PathSegment segment : PathUtils.flatten(new Path())) {
     35             fail("An empty path should not have segments: " + segment);
     36         }
     37     }
     38 
     39     @SdkSuppress(minSdkVersion = 26)
     40     @Test public void flatten() {
     41         Path p = new Path();
     42 
     43         // Single line
     44         p.lineTo(10.0f, 10.0f);
     45         assertEquals(
     46                 new PathSegment(new PointF(), 0.0f, new PointF(10.0f, 10.0f), 1.0f),
     47                 PathUtils.flatten(p).iterator().next());
     48 
     49         // Only moves
     50         p.reset();
     51         p.moveTo(10.0f, 10.0f);
     52         p.moveTo(20.0f, 20.0f);
     53         for (PathSegment segment : PathUtils.flatten(p)) {
     54             fail("A path with only moves should not have segments: " + segment);
     55         }
     56 
     57         // Mix of moves/lines
     58         p.reset();
     59         p.moveTo(10.0f, 10.0f);
     60         p.lineTo(20.0f, 20.0f);
     61         p.lineTo(60.0f, 20.0f);
     62 
     63         int count = 0;
     64         for (PathSegment segment : PathUtils.flatten(p)) {
     65             count++;
     66             assertNotEquals(segment.getStartFraction(), segment.getEndFraction());
     67         }
     68         assertEquals(2, count);
     69 
     70         // Mix of moves/lines, starts with moves, ends with moves
     71         p.reset();
     72         // Start with several moves
     73         p.moveTo(5.0f, 5.0f);
     74         p.moveTo(10.0f, 10.0f);
     75         p.lineTo(20.0f, 20.0f);
     76         p.lineTo(30.0f, 10.0f);
     77         // Several moves in the middle
     78         p.moveTo(40.0f, 10.0f);
     79         p.moveTo(50.0f, 10.0f);
     80         p.lineTo(60.0f, 20.0f);
     81         // End with several moves
     82         p.moveTo(10.0f, 10.0f);
     83         p.moveTo(30.0f, 30.0f);
     84 
     85         count = 0;
     86         for (PathSegment segment : PathUtils.flatten(p)) {
     87             count++;
     88             assertNotEquals(segment.getStartFraction(), segment.getEndFraction());
     89         }
     90         assertEquals(3, count);
     91     }
     92 }
     93 
     94