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