1/* 2 * Copyright (C) 2008 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 17package android.graphics; 18 19 20import android.graphics.Bitmap.Config; 21import android.graphics.Path.Direction; 22import android.support.test.filters.SmallTest; 23import android.support.test.runner.AndroidJUnit4; 24 25import static org.junit.Assert.assertTrue; 26 27import org.junit.Test; 28import org.junit.runner.RunWith; 29 30@RunWith(AndroidJUnit4.class) 31public class PathOffsetTest { 32 33 private static final int SQUARE = 10; 34 private static final int WIDTH = 100; 35 private static final int HEIGHT = 100; 36 private static final int START_X = 10; 37 private static final int START_Y = 20; 38 private static final int OFFSET_X = 30; 39 private static final int OFFSET_Y = 40; 40 41 @Test 42 @SmallTest 43 public void testPathOffset() { 44 Path actualPath = new Path(); 45 actualPath.addRect(START_X, START_Y, START_X + SQUARE, START_Y + SQUARE, Direction.CW); 46 assertTrue(actualPath.isSimplePath); 47 actualPath.offset(OFFSET_X, OFFSET_Y); 48 assertTrue(actualPath.isSimplePath); 49 50 Path expectedPath = new Path(); 51 expectedPath.addRect(START_X + OFFSET_X, START_Y + OFFSET_Y, START_X + OFFSET_X + SQUARE, 52 START_Y + OFFSET_Y + SQUARE, Direction.CW); 53 54 assertPaths(actualPath, expectedPath); 55 } 56 57 @Test 58 @SmallTest 59 public void testPathOffsetWithDestination() { 60 Path initialPath = new Path(); 61 initialPath.addRect(START_X, START_Y, START_X + SQUARE, START_Y + SQUARE, Direction.CW); 62 Path actualPath = new Path(); 63 assertTrue(initialPath.isSimplePath); 64 assertTrue(actualPath.isSimplePath); 65 initialPath.offset(OFFSET_X, OFFSET_Y, actualPath); 66 assertTrue(actualPath.isSimplePath); 67 68 Path expectedPath = new Path(); 69 expectedPath.addRect(START_X + OFFSET_X, START_Y + OFFSET_Y, START_X + OFFSET_X + SQUARE, 70 START_Y + OFFSET_Y + SQUARE, Direction.CW); 71 72 assertPaths(actualPath, expectedPath); 73 } 74 75 private static void assertPaths(Path actual, Path expected) { 76 Bitmap actualBitmap = drawAndGetBitmap(actual); 77 Bitmap expectedBitmap = drawAndGetBitmap(expected); 78 assertTrue(actualBitmap.sameAs(expectedBitmap)); 79 } 80 81 private static Bitmap drawAndGetBitmap(Path path) { 82 Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_8888); 83 bitmap.eraseColor(Color.BLACK); 84 Paint paint = new Paint(); 85 paint.setColor(Color.RED); 86 Canvas canvas = new Canvas(bitmap); 87 canvas.drawPath(path, paint); 88 return bitmap; 89 } 90 91} 92