|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 package com.nokia.openlcdui.mt.game; |
|
18 |
|
19 import junit.framework.*; |
|
20 |
|
21 import javax.microedition.lcdui.*; |
|
22 import javax.microedition.lcdui.game.GameCanvas; |
|
23 import javax.microedition.lcdui.game.Sprite; |
|
24 |
|
25 import com.nokia.openlcdui.mt.SWTTestCase; |
|
26 |
|
27 /** |
|
28 * GameCanvas non-interactive test. |
|
29 */ |
|
30 public class GameCanvasTest extends SWTTestCase { |
|
31 |
|
32 Sprite iSprite = null; |
|
33 int [] iRGB = |
|
34 { |
|
35 0xFFFFFFFF, 0xFF000000, 0xFF000000, 0xFFFFFFFF, |
|
36 0xFF000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFF000000, |
|
37 0xFF000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFF000000, |
|
38 0xFFFFFFFF, 0xFF000000, 0xFF000000, 0xFFFFFFFF |
|
39 }; |
|
40 |
|
41 /** |
|
42 * Constructor. |
|
43 */ |
|
44 public GameCanvasTest() { |
|
45 } |
|
46 |
|
47 /** |
|
48 * Constructor. |
|
49 * |
|
50 * @param sTestName Test name. |
|
51 * @param rTestMethod Test method. |
|
52 */ |
|
53 public GameCanvasTest(String sTestName) { |
|
54 super(sTestName); |
|
55 } |
|
56 |
|
57 protected void setUp() throws Exception { |
|
58 super.setUp(); |
|
59 iSprite = new Sprite(Image.createRGBImage(iRGB, 4, 4, true)); |
|
60 } |
|
61 |
|
62 /** |
|
63 * To create the test suite. You need to add a new aSuite.addTest entry for |
|
64 * any new test methods. |
|
65 * |
|
66 * @return new testsuite. |
|
67 */ |
|
68 public static Test suite() { |
|
69 TestSuite suite = new TestSuite(); |
|
70 |
|
71 java.util.Vector methodNames; |
|
72 java.util.Enumeration e; |
|
73 |
|
74 // Add widget tests |
|
75 methodNames = GameCanvasTest.methodNames(); |
|
76 e = methodNames.elements(); |
|
77 while (e.hasMoreElements()) { |
|
78 suite.addTest(new GameCanvasTest((String)e.nextElement())); |
|
79 } |
|
80 |
|
81 return suite; |
|
82 } |
|
83 |
|
84 public static java.util.Vector methodNames() { |
|
85 java.util.Vector methodNames = new java.util.Vector(); |
|
86 methodNames.addElement("testGraphics"); |
|
87 methodNames.addElement("testPaint"); |
|
88 return methodNames; |
|
89 } |
|
90 |
|
91 protected void runTest() throws Throwable { |
|
92 if (getName().equals("testGraphics")) testGraphics(); |
|
93 else if (getName().equals("testPaint")) testPaint(); |
|
94 else super.runTest(); |
|
95 } |
|
96 |
|
97 /** |
|
98 * Tests following methods:<br> |
|
99 * {@link javax.microedition.lcdui.game.GameCanvas#getGraphics}<br> |
|
100 * {@link javax.microedition.lcdui.game.GameCanvas#flushGraphics()}<br> |
|
101 * {@link javax.microedition.lcdui.game.GameCanvas#flushGraphics(int,int,int,int)}<br> |
|
102 */ |
|
103 public void testGraphics() { |
|
104 GameCanvasWithoutKeys canvas = new GameCanvasWithoutKeys(); |
|
105 Graphics g = canvas.getBufferGraphics(); |
|
106 |
|
107 assertEquals("clipX should be 0", 0, g.getClipX()); |
|
108 assertEquals("clipX should be 0", 0, g.getClipY()); |
|
109 assertEquals("clipX should = width", canvas.getWidth(), g.getClipWidth()); |
|
110 assertEquals("clipX should = height", canvas.getHeight(), g.getClipHeight()); |
|
111 |
|
112 assertEquals("Current color should be black", 0, g.getColor()); |
|
113 assertTrue("Font should be Font.getDefaultFont", (g.getFont()) |
|
114 .equals(Font.getDefaultFont())); |
|
115 |
|
116 assertEquals("Stroke should be SOLID", Graphics.SOLID, g.getStrokeStyle()); |
|
117 |
|
118 assertEquals("translateX should be 0", 0, g.getTranslateX()); |
|
119 assertEquals("translateY should be 0", 0, g.getTranslateY()); |
|
120 } |
|
121 |
|
122 /** |
|
123 * Tests following methods:<br> |
|
124 * {@link javax.microedition.lcdui.game.GameCanvas#paint(javax.microedition.lcdui.Graphics)}<br> |
|
125 */ |
|
126 public void testPaint() { |
|
127 |
|
128 // This test hangs (hang probably caused by GameCanvas command buffering), backlog item has been created. |
|
129 DISABLE_TEST(); |
|
130 |
|
131 // how it works: |
|
132 // - GameCanvas has a buffer |
|
133 // - getGraphics gives you the buffer |
|
134 // - you paint into this graphics (that is to the buffer) |
|
135 // - GameCanvas.paint(Graphics) gets called by the sys or by you to |
|
136 // paint the buffer into supplied graphics |
|
137 |
|
138 // testing paint - using external mutable image as screen |
|
139 GameCanvasWithoutKeys canvas = new GameCanvasWithoutKeys(); |
|
140 Image image = Image.createImage(canvas.getWidth(), canvas.getHeight()); |
|
141 Graphics imageGraphics = image.getGraphics(); |
|
142 int[] compare = iRGB; |
|
143 testPaint("paint without clip and translation", canvas, image, imageGraphics, compare); |
|
144 |
|
145 // testing paint - using external mutable image as screen |
|
146 canvas = new GameCanvasWithoutKeys(); |
|
147 image = Image.createImage(canvas.getWidth(), canvas.getHeight()); |
|
148 imageGraphics = image.getGraphics(); |
|
149 // clip |
|
150 imageGraphics.setClip(0, 0, 1, 4); // first column is in, rest is out |
|
151 int[] compare2 = { |
|
152 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, |
|
153 0xFF000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, |
|
154 0xFF000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, |
|
155 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}; |
|
156 testPaint("paint with clip", canvas, image, imageGraphics, compare2); |
|
157 |
|
158 // testing paint - using external mutable image as screen |
|
159 canvas = new GameCanvasWithoutKeys(); |
|
160 image = Image.createImage(canvas.getWidth(), canvas.getHeight()); |
|
161 imageGraphics = image.getGraphics(); |
|
162 // clip |
|
163 imageGraphics.translate(0, -1); |
|
164 int[] compare3 = { |
|
165 0xFF000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFF000000, |
|
166 0xFF000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFF000000, |
|
167 0xFFFFFFFF, 0xFF000000, 0xFF000000, 0xFFFFFFFF, |
|
168 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,}; |
|
169 testPaint("paint with translation", canvas, image, imageGraphics, compare3); |
|
170 |
|
171 // testing paint - using external mutable image as screen |
|
172 canvas = new GameCanvasWithoutKeys(); |
|
173 image = Image.createImage(canvas.getWidth(), canvas.getHeight()); |
|
174 imageGraphics = image.getGraphics(); |
|
175 // clip |
|
176 imageGraphics.translate(0, -1); |
|
177 imageGraphics.setClip(0, 0, 2, 4); // first two columns are in, rest is out |
|
178 int[] compare4 = { |
|
179 0xFF000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, |
|
180 0xFF000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, |
|
181 0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF, 0xFFFFFFFF, |
|
182 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,}; |
|
183 testPaint("paint with translation and clip", |
|
184 canvas, image, imageGraphics, compare4); |
|
185 } |
|
186 |
|
187 /** |
|
188 * Checks the painted image against provided rgb array. |
|
189 * |
|
190 * The area of interest is four by four pixels located at 0,0. |
|
191 * |
|
192 * Note that the buffer background in the area of interest will be initialized |
|
193 * to white before painting {@link #iSprite} into it at 0,0. This should be |
|
194 * taken into account when generating the array for comparison. |
|
195 * |
|
196 * @param screenGraphics The graphics object to paint the GameCanvas to. This |
|
197 * graphics may have clip and translation set |
|
198 * @param screen The image object that aScreenGraphics paints to. |
|
199 * @param checkRGB An array of integers with expected values for the |
|
200 * area of interest. |
|
201 */ |
|
202 private void testPaint(String msg, GameCanvasWithoutKeys canvas, |
|
203 Image screen, Graphics screenGraphics, int[] expectedRGB) { |
|
204 |
|
205 Graphics bufferGraphics = canvas.getBufferGraphics(); |
|
206 |
|
207 // paint into gameCanvas buffer |
|
208 int color = bufferGraphics.getColor(); |
|
209 bufferGraphics.setColor(0x00FFFFFF); |
|
210 bufferGraphics.fillRect(0, 0, 4, 4); |
|
211 bufferGraphics.setColor(color); |
|
212 |
|
213 iSprite.paint(bufferGraphics); |
|
214 canvas.flushGraphics(); |
|
215 canvas.paint(screenGraphics); |
|
216 |
|
217 // getRGB will sync graphics |
|
218 int[] actualRGB = new int[16]; |
|
219 screen.getRGB(actualRGB, 0, 4, 0, 0, 4, 4); |
|
220 |
|
221 // compare to the sprite image |
|
222 for (int i = 0; i < 16; i++) { |
|
223 if (actualRGB[i] != expectedRGB[i]) { |
|
224 printRGB(actualRGB, 4, 4); |
|
225 printRGB(expectedRGB, 4, 4); |
|
226 fail(msg); |
|
227 } |
|
228 } |
|
229 } |
|
230 |
|
231 /** |
|
232 * Prints out the image to standard output. If an image contains unexpected |
|
233 * colours, i.e. not those used to form images, this method will throw an |
|
234 * exception. This behaviour provides protection against hiding colour |
|
235 * discretization problem and reporting test error. |
|
236 */ |
|
237 private void printRGB(int[] data, int w, int h) { |
|
238 int odd = 0; |
|
239 for (int i = 0; i < h; i++) { |
|
240 for (int j = 0; j < w; j++) { |
|
241 int ind = i * w + j; |
|
242 String cha = " "; |
|
243 switch (data[ind]) { |
|
244 case 0xFFFFFFFF: |
|
245 cha = "W"; |
|
246 break; |
|
247 case 0x00FFFFFF: |
|
248 cha = "w"; |
|
249 break; |
|
250 case 0xFF000000: |
|
251 cha = "B"; |
|
252 break; |
|
253 case 0x00000000: |
|
254 cha = "b"; |
|
255 break; |
|
256 case 0xFF888888: |
|
257 cha = "."; |
|
258 break; |
|
259 case 0x00888888: |
|
260 cha = ","; |
|
261 break; |
|
262 default: { |
|
263 cha = "U"; |
|
264 odd = data[ind]; |
|
265 } |
|
266 } |
|
267 System.out.print(cha); |
|
268 } |
|
269 System.out.println(""); |
|
270 } |
|
271 System.out.println(""); |
|
272 if (odd != 0) { |
|
273 fail("Unexpected colour in test image : 0x" + Integer.toHexString(odd)); |
|
274 } |
|
275 } |
|
276 |
|
277 class GameCanvasWithKeys extends GameCanvas { |
|
278 |
|
279 public GameCanvasWithKeys() { |
|
280 super(false); |
|
281 } |
|
282 |
|
283 public Graphics getTheGraphics() { |
|
284 return getGraphics(); |
|
285 } |
|
286 } |
|
287 |
|
288 class GameCanvasWithoutKeys extends GameCanvas { |
|
289 |
|
290 public GameCanvasWithoutKeys() { |
|
291 super(true); |
|
292 } |
|
293 |
|
294 public Graphics getBufferGraphics() { |
|
295 return getGraphics(); |
|
296 } |
|
297 } |
|
298 |
|
299 } |