1ecbcdd384c07402204064243981a432f5b0aad36sergeyv/*
2ecbcdd384c07402204064243981a432f5b0aad36sergeyv * Copyright (C) 2008 The Android Open Source Project
3ecbcdd384c07402204064243981a432f5b0aad36sergeyv *
4ecbcdd384c07402204064243981a432f5b0aad36sergeyv * Licensed under the Apache License, Version 2.0 (the "License");
5ecbcdd384c07402204064243981a432f5b0aad36sergeyv * you may not use this file except in compliance with the License.
6ecbcdd384c07402204064243981a432f5b0aad36sergeyv * You may obtain a copy of the License at
7ecbcdd384c07402204064243981a432f5b0aad36sergeyv *
8ecbcdd384c07402204064243981a432f5b0aad36sergeyv *      http://www.apache.org/licenses/LICENSE-2.0
9ecbcdd384c07402204064243981a432f5b0aad36sergeyv *
10ecbcdd384c07402204064243981a432f5b0aad36sergeyv * Unless required by applicable law or agreed to in writing, software
11ecbcdd384c07402204064243981a432f5b0aad36sergeyv * distributed under the License is distributed on an "AS IS" BASIS,
12ecbcdd384c07402204064243981a432f5b0aad36sergeyv * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ecbcdd384c07402204064243981a432f5b0aad36sergeyv * See the License for the specific language governing permissions and
14ecbcdd384c07402204064243981a432f5b0aad36sergeyv * limitations under the License.
15ecbcdd384c07402204064243981a432f5b0aad36sergeyv */
16ecbcdd384c07402204064243981a432f5b0aad36sergeyv
17ecbcdd384c07402204064243981a432f5b0aad36sergeyvpackage android.graphics;
18ecbcdd384c07402204064243981a432f5b0aad36sergeyv
19ecbcdd384c07402204064243981a432f5b0aad36sergeyv
20ecbcdd384c07402204064243981a432f5b0aad36sergeyvimport android.graphics.Bitmap.Config;
21ecbcdd384c07402204064243981a432f5b0aad36sergeyvimport android.graphics.Path.Direction;
22ecbcdd384c07402204064243981a432f5b0aad36sergeyvimport android.support.test.filters.SmallTest;
23ecbcdd384c07402204064243981a432f5b0aad36sergeyvimport android.support.test.runner.AndroidJUnit4;
24ecbcdd384c07402204064243981a432f5b0aad36sergeyv
25ecbcdd384c07402204064243981a432f5b0aad36sergeyvimport static org.junit.Assert.assertTrue;
26ecbcdd384c07402204064243981a432f5b0aad36sergeyv
27ecbcdd384c07402204064243981a432f5b0aad36sergeyvimport org.junit.Test;
28ecbcdd384c07402204064243981a432f5b0aad36sergeyvimport org.junit.runner.RunWith;
29ecbcdd384c07402204064243981a432f5b0aad36sergeyv
30ecbcdd384c07402204064243981a432f5b0aad36sergeyv@RunWith(AndroidJUnit4.class)
31ecbcdd384c07402204064243981a432f5b0aad36sergeyvpublic class PathOffsetTest {
32ecbcdd384c07402204064243981a432f5b0aad36sergeyv
33ecbcdd384c07402204064243981a432f5b0aad36sergeyv    private static final int SQUARE = 10;
34ecbcdd384c07402204064243981a432f5b0aad36sergeyv    private static final int WIDTH = 100;
35ecbcdd384c07402204064243981a432f5b0aad36sergeyv    private static final int HEIGHT = 100;
36ecbcdd384c07402204064243981a432f5b0aad36sergeyv    private static final int START_X = 10;
37ecbcdd384c07402204064243981a432f5b0aad36sergeyv    private static final int START_Y = 20;
38ecbcdd384c07402204064243981a432f5b0aad36sergeyv    private static final int OFFSET_X = 30;
39ecbcdd384c07402204064243981a432f5b0aad36sergeyv    private static final int OFFSET_Y = 40;
40ecbcdd384c07402204064243981a432f5b0aad36sergeyv
41ecbcdd384c07402204064243981a432f5b0aad36sergeyv    @Test
42ecbcdd384c07402204064243981a432f5b0aad36sergeyv    @SmallTest
43ecbcdd384c07402204064243981a432f5b0aad36sergeyv    public void testPathOffset() {
44ecbcdd384c07402204064243981a432f5b0aad36sergeyv        Path actualPath = new Path();
45ecbcdd384c07402204064243981a432f5b0aad36sergeyv        actualPath.addRect(START_X, START_Y, START_X + SQUARE, START_Y + SQUARE, Direction.CW);
46ecbcdd384c07402204064243981a432f5b0aad36sergeyv        assertTrue(actualPath.isSimplePath);
47ecbcdd384c07402204064243981a432f5b0aad36sergeyv        actualPath.offset(OFFSET_X, OFFSET_Y);
48ecbcdd384c07402204064243981a432f5b0aad36sergeyv        assertTrue(actualPath.isSimplePath);
49ecbcdd384c07402204064243981a432f5b0aad36sergeyv
50ecbcdd384c07402204064243981a432f5b0aad36sergeyv        Path expectedPath = new Path();
51ecbcdd384c07402204064243981a432f5b0aad36sergeyv        expectedPath.addRect(START_X + OFFSET_X, START_Y + OFFSET_Y, START_X + OFFSET_X + SQUARE,
52ecbcdd384c07402204064243981a432f5b0aad36sergeyv                START_Y + OFFSET_Y + SQUARE, Direction.CW);
53ecbcdd384c07402204064243981a432f5b0aad36sergeyv
54ecbcdd384c07402204064243981a432f5b0aad36sergeyv        assertPaths(actualPath, expectedPath);
55ecbcdd384c07402204064243981a432f5b0aad36sergeyv    }
56ecbcdd384c07402204064243981a432f5b0aad36sergeyv
57ecbcdd384c07402204064243981a432f5b0aad36sergeyv    @Test
58ecbcdd384c07402204064243981a432f5b0aad36sergeyv    @SmallTest
59ecbcdd384c07402204064243981a432f5b0aad36sergeyv    public void testPathOffsetWithDestination() {
60ecbcdd384c07402204064243981a432f5b0aad36sergeyv        Path initialPath = new Path();
61ecbcdd384c07402204064243981a432f5b0aad36sergeyv        initialPath.addRect(START_X, START_Y, START_X + SQUARE, START_Y + SQUARE, Direction.CW);
62ecbcdd384c07402204064243981a432f5b0aad36sergeyv        Path actualPath = new Path();
63ecbcdd384c07402204064243981a432f5b0aad36sergeyv        assertTrue(initialPath.isSimplePath);
64ecbcdd384c07402204064243981a432f5b0aad36sergeyv        assertTrue(actualPath.isSimplePath);
65ecbcdd384c07402204064243981a432f5b0aad36sergeyv        initialPath.offset(OFFSET_X, OFFSET_Y, actualPath);
66ecbcdd384c07402204064243981a432f5b0aad36sergeyv        assertTrue(actualPath.isSimplePath);
67ecbcdd384c07402204064243981a432f5b0aad36sergeyv
68ecbcdd384c07402204064243981a432f5b0aad36sergeyv        Path expectedPath = new Path();
69ecbcdd384c07402204064243981a432f5b0aad36sergeyv        expectedPath.addRect(START_X + OFFSET_X, START_Y + OFFSET_Y, START_X + OFFSET_X + SQUARE,
70ecbcdd384c07402204064243981a432f5b0aad36sergeyv                START_Y + OFFSET_Y + SQUARE, Direction.CW);
71ecbcdd384c07402204064243981a432f5b0aad36sergeyv
72ecbcdd384c07402204064243981a432f5b0aad36sergeyv        assertPaths(actualPath, expectedPath);
73ecbcdd384c07402204064243981a432f5b0aad36sergeyv    }
74ecbcdd384c07402204064243981a432f5b0aad36sergeyv
75ecbcdd384c07402204064243981a432f5b0aad36sergeyv    private static void assertPaths(Path actual, Path expected) {
76ecbcdd384c07402204064243981a432f5b0aad36sergeyv        Bitmap actualBitmap = drawAndGetBitmap(actual);
77ecbcdd384c07402204064243981a432f5b0aad36sergeyv        Bitmap expectedBitmap = drawAndGetBitmap(expected);
78ecbcdd384c07402204064243981a432f5b0aad36sergeyv        assertTrue(actualBitmap.sameAs(expectedBitmap));
79ecbcdd384c07402204064243981a432f5b0aad36sergeyv    }
80ecbcdd384c07402204064243981a432f5b0aad36sergeyv
81ecbcdd384c07402204064243981a432f5b0aad36sergeyv    private static Bitmap drawAndGetBitmap(Path path) {
82ecbcdd384c07402204064243981a432f5b0aad36sergeyv        Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_8888);
83ecbcdd384c07402204064243981a432f5b0aad36sergeyv        bitmap.eraseColor(Color.BLACK);
84ecbcdd384c07402204064243981a432f5b0aad36sergeyv        Paint paint = new Paint();
85ecbcdd384c07402204064243981a432f5b0aad36sergeyv        paint.setColor(Color.RED);
86ecbcdd384c07402204064243981a432f5b0aad36sergeyv        Canvas canvas = new Canvas(bitmap);
87ecbcdd384c07402204064243981a432f5b0aad36sergeyv        canvas.drawPath(path, paint);
88ecbcdd384c07402204064243981a432f5b0aad36sergeyv        return bitmap;
89ecbcdd384c07402204064243981a432f5b0aad36sergeyv    }
90ecbcdd384c07402204064243981a432f5b0aad36sergeyv
91ecbcdd384c07402204064243981a432f5b0aad36sergeyv}
92