1 /*
2  * Copyright (C) 2014 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.uirendering.cts.testclasses;
18 
19 import android.graphics.Canvas;
20 import android.graphics.Color;
21 import android.graphics.Paint;
22 import android.graphics.PorterDuff;
23 import android.graphics.Rect;
24 import android.graphics.drawable.NinePatchDrawable;
25 import android.test.suitebuilder.annotation.SmallTest;
26 import android.uirendering.cts.bitmapcomparers.BitmapComparer;
27 import android.uirendering.cts.bitmapcomparers.ExactComparer;
28 import android.uirendering.cts.bitmapverifiers.BitmapVerifier;
29 import android.uirendering.cts.bitmapverifiers.RectVerifier;
30 import android.uirendering.cts.testinfrastructure.ActivityTestBase;
31 import android.uirendering.cts.testinfrastructure.CanvasClient;
32 import com.android.cts.uirendering.R;
33 
34 public class ExactCanvasTests extends ActivityTestBase {
35     private final BitmapComparer mExactComparer = new ExactComparer();
36 
37     @SmallTest
testBlueRect()38     public void testBlueRect() {
39         final Rect rect = new Rect(10, 10, 80, 80);
40         createTest()
41                 .addCanvasClient(new CanvasClient() {
42                     @Override
43                     public void draw(Canvas canvas, int width, int height) {
44                         Paint p = new Paint();
45                         p.setAntiAlias(false);
46                         p.setColor(Color.BLUE);
47                         canvas.drawRect(rect, p);
48                     }
49                 })
50                 .runWithVerifier(new RectVerifier(Color.WHITE, Color.BLUE, rect));
51     }
52 
53     @SmallTest
testPoints()54     public void testPoints() {
55         createTest()
56                 .addCanvasClient(new CanvasClient() {
57                     @Override
58                     public void draw(Canvas canvas, int width, int height) {
59                         Paint p = new Paint();
60                         p.setAntiAlias(false);
61                         p.setStrokeWidth(1f);
62                         p.setColor(Color.BLACK);
63                         for (int i = 0; i < 10; i++) {
64                             canvas.drawPoint(i * 10, i * 10, p);
65                         }
66                     }
67                 })
68                 .runWithComparer(mExactComparer);
69     }
70 
71     @SmallTest
testBlackRectWithStroke()72     public void testBlackRectWithStroke() {
73         createTest()
74                 .addCanvasClient(new CanvasClient() {
75                     @Override
76                     public void draw(Canvas canvas, int width, int height) {
77                         Paint p = new Paint();
78                         p.setColor(Color.RED);
79                         canvas.drawRect(0, 0, ActivityTestBase.TEST_WIDTH,
80                                 ActivityTestBase.TEST_HEIGHT, p);
81                         p.setColor(Color.BLACK);
82                         p.setStrokeWidth(5);
83                         canvas.drawRect(10, 10, 80, 80, p);
84                     }
85                 })
86                 .runWithComparer(mExactComparer);
87     }
88 
89     @SmallTest
testBlackLineOnGreenBack()90     public void testBlackLineOnGreenBack() {
91         createTest()
92                 .addCanvasClient(new CanvasClient() {
93                     @Override
94                     public void draw(Canvas canvas, int width, int height) {
95                         canvas.drawColor(Color.GREEN);
96                         Paint p = new Paint();
97                         p.setColor(Color.BLACK);
98                         p.setStrokeWidth(10);
99                         canvas.drawLine(0, 0, 50, 0, p);
100                     }
101                 })
102                 .runWithComparer(mExactComparer);
103     }
104 
105     @SmallTest
testDrawRedRectOnBlueBack()106     public void testDrawRedRectOnBlueBack() {
107         createTest()
108                 .addCanvasClient(new CanvasClient() {
109                     @Override
110                     public void draw(Canvas canvas, int width, int height) {
111                         canvas.drawColor(Color.BLUE);
112                         Paint p = new Paint();
113                         p.setColor(Color.RED);
114                         canvas.drawRect(10, 10, 40, 40, p);
115                     }
116                 })
117                 .runWithComparer(mExactComparer);
118     }
119 
120     @SmallTest
testDrawLine()121     public void testDrawLine() {
122         createTest()
123                 .addCanvasClient(new CanvasClient() {
124                     @Override
125                     public void draw(Canvas canvas, int width, int height) {
126                         Paint p = new Paint();
127                         canvas.drawColor(Color.WHITE);
128                         p.setColor(Color.BLACK);
129                         float[] pts = {
130                                 0, 0, 80, 80, 80, 0, 0, 80, 40, 50, 60, 50
131                         };
132                         canvas.drawLines(pts, p);
133                     }
134                 })
135                 .runWithComparer(mExactComparer);
136     }
137 
138     @SmallTest
testDrawWhiteScreen()139     public void testDrawWhiteScreen() {
140         createTest()
141                 .addCanvasClient(new CanvasClient() {
142                     @Override
143                     public void draw(Canvas canvas, int width, int height) {
144                         canvas.drawColor(Color.WHITE);
145                     }
146                 })
147                 .runWithComparer(mExactComparer);
148     }
149 
150     @SmallTest
testBasicText()151     public void testBasicText() {
152         final String testString = "THIS IS A TEST";
153         createTest()
154                 .addCanvasClient(new CanvasClient() {
155                     @Override
156                     public void draw(Canvas canvas, int width, int height) {
157                         Paint p = new Paint();
158                         canvas.drawColor(Color.BLACK);
159                         p.setColor(Color.WHITE);
160                         p.setStrokeWidth(5);
161                         canvas.drawText(testString, 30, 50, p);
162                     }
163                 })
164                 .runWithComparer(mExactComparer);
165     }
166 
167     @SmallTest
testBasicColorXfermode()168     public void testBasicColorXfermode() {
169         createTest()
170                 .addCanvasClient(new CanvasClient() {
171                     @Override
172                     public void draw(Canvas canvas, int width, int height) {
173                         canvas.drawColor(Color.GRAY);
174                         canvas.drawColor(Color.BLUE, PorterDuff.Mode.MULTIPLY);
175                     }
176                 })
177                 .runWithComparer(mExactComparer);
178     }
179 
180     @SmallTest
testBluePaddedSquare()181     public void testBluePaddedSquare() {
182         final NinePatchDrawable ninePatchDrawable = (NinePatchDrawable)
183             getActivity().getResources().getDrawable(R.drawable.blue_padded_square);
184         ninePatchDrawable.setBounds(0, 0, 90, 90);
185 
186         BitmapVerifier verifier = new RectVerifier(Color.WHITE, Color.BLUE,
187                 new Rect(10, 10, 80, 80));
188 
189         createTest()
190                 .addCanvasClient(new CanvasClient() {
191                     @Override
192                     public void draw(Canvas canvas, int width, int height) {
193                         canvas.drawColor(Color.WHITE);
194                         Paint p = new Paint();
195                         p.setColor(Color.BLUE);
196                         canvas.drawRect(10, 10, 80, 80, p);
197                     }
198                 })
199                 .addCanvasClient(new CanvasClient() {
200                     @Override
201                     public void draw(Canvas canvas, int width, int height) {
202                         ninePatchDrawable.draw(canvas);
203                     }
204                 })
205                 .addLayout(R.layout.blue_padded_square, null)
206                 .runWithVerifier(verifier);
207     }
208 }
209