Home | History | Annotate | Download | only in cts
      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 
     17 package android.graphics.drawable.shapes.cts;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertNotNull;
     22 import static org.junit.Assert.assertTrue;
     23 
     24 import android.graphics.Bitmap;
     25 import android.graphics.Bitmap.Config;
     26 import android.graphics.Canvas;
     27 import android.graphics.Outline;
     28 import android.graphics.Paint;
     29 import android.graphics.Paint.Style;
     30 import android.graphics.Rect;
     31 import android.graphics.drawable.shapes.OvalShape;
     32 
     33 import androidx.test.filters.SmallTest;
     34 import androidx.test.runner.AndroidJUnit4;
     35 
     36 import org.junit.Test;
     37 import org.junit.runner.RunWith;
     38 
     39 @SmallTest
     40 @RunWith(AndroidJUnit4.class)
     41 public class OvalShapeTest {
     42     private static final int TEST_WIDTH  = 100;
     43     private static final int TEST_HEIGHT = 200;
     44 
     45     private static final int TEST_COLOR_1 = 0xFF00FF00;
     46     private static final int TEST_COLOR_2 = 0xFFFF0000;
     47 
     48     private static final int TOLERANCE = 4; // tolerance in pixels
     49 
     50     @Test
     51     public void testConstructor() {
     52         new OvalShape();
     53     }
     54 
     55     @Test
     56     public void testDraw() {
     57         OvalShape ovalShape = new OvalShape();
     58         Bitmap bitmap = Bitmap.createBitmap(TEST_WIDTH, TEST_HEIGHT, Config.ARGB_8888);
     59         Canvas canvas = new Canvas(bitmap);
     60         Paint paint = new Paint();
     61         paint.setStyle(Style.FILL);
     62         paint.setColor(TEST_COLOR_1);
     63         ovalShape.resize(TEST_WIDTH, TEST_HEIGHT);
     64 
     65         ovalShape.draw(canvas, paint);
     66         // check the color at the center of bitmap
     67         assertEquals(TEST_COLOR_1, bitmap.getPixel(TEST_WIDTH / 2, TEST_HEIGHT / 2));
     68 
     69         final int SQUARE = Math.min(TEST_WIDTH, TEST_HEIGHT);
     70         paint.setColor(TEST_COLOR_2);
     71         ovalShape.resize(SQUARE, SQUARE); // circle
     72         ovalShape.draw(canvas, paint);
     73         // count number of pixels with TEST_COLOR_2 along diagonal
     74         int count = 0;
     75         for (int i = 0; i < SQUARE; i++) {
     76             if (bitmap.getPixel(i, i) == TEST_COLOR_2) {
     77                 count += 1;
     78             }
     79         }
     80         assertEquals((double)SQUARE / Math.sqrt(2), count, TOLERANCE);
     81     }
     82 
     83     @Test
     84     public void testGetOutline() {
     85         Outline outline = new Outline();
     86         Rect rect = new Rect();
     87         OvalShape shape;
     88 
     89         // Zero-sized oval yields an empty outline.
     90         shape = new OvalShape();
     91         shape.getOutline(outline);
     92         assertTrue(outline.isEmpty());
     93         assertTrue(outline.getRadius() < 0);
     94         assertFalse(outline.getRect(rect));
     95 
     96         // Non-zero oval yields a rounded rect.
     97         shape.resize(100, 100);
     98         shape.getOutline(outline);
     99         assertFalse(outline.isEmpty());
    100         assertEquals(50.0f, outline.getRadius(), 0.01f);
    101         assertTrue(outline.getRect(rect));
    102         assertEquals(0, rect.left);
    103         assertEquals(0, rect.top);
    104         assertEquals(100, rect.right);
    105         assertEquals(100, rect.bottom);
    106 
    107         // Non-circular oval yields a path.
    108         shape.resize(100, 200);
    109         shape.getOutline(outline);
    110         assertFalse(outline.isEmpty());
    111         assertTrue(outline.getRadius() < 0);
    112         assertFalse(outline.getRect(rect));
    113     }
    114 
    115     @Test
    116     public void testClone() throws Exception {
    117         OvalShape shape = new OvalShape();
    118         shape.resize(100, 100);
    119 
    120         OvalShape clone = shape.clone();
    121         assertNotNull(clone);
    122         assertEquals(shape.getWidth(), clone.getWidth(), 0.0f);
    123         assertEquals(shape.getHeight(), clone.getHeight(), 0.0f);
    124     }
    125 }
    126