A friend of mine wanted to get a look at the visual verification I’m using to unit test my rendering code, so I figured I might as well give it a spit-shine and share it with anyone that might find it useful. If you are working with pixel art a lot, especially if you are working on a tile-based game, this might be useful. It won’t work well in most other situations, since it expects pixel-perfect matches.

/*
 Copyright (c) 2009 Andrew Traviss

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/
package com.andrewtraviss.flexunit
{
   import flash.display.BitmapData;

   import flexunit.framework.TestCase;

   public class VisualTestCase extends TestCase
   {
      public function VisualTestCase(methodName:String=null)
      {
         super(methodName);
      }
      protected function assertBitmapsMatch(in_expected:BitmapData, in_actual:BitmapData):void
      {
         oneAssertionHasBeenMade();
         if(!areIdentical(in_expected, in_actual))
         {
            failBitmapsMatch();
         }
      }
      protected function assertBitmapsDoNotMatch(in_notExpected:BitmapData, in_actual:BitmapData):void
      {
         oneAssertionHasBeenMade();
         if(areIdentical(in_notExpected, in_actual))
         {
            failBitmapsDoNotMatch();
         }
      }
      private function areIdentical(in_bitmapDataA:BitmapData, in_bitmapDataB:BitmapData):Boolean
      {
         var result:* = in_bitmapDataA.compare(in_bitmapDataB);
         if(result is BitmapData)
         {
            return false;
         }
         else
         {
            return (result == IDENTICAL);
         }
      }
      private function failBitmapsMatch():void
      {
         fail("Expected identical bitmaps, actual bitmaps are different.");
      }
      private function failBitmapsDoNotMatch():void
      {
         fail("Expected different bitmaps, actual bitmaps are identical.");
      }
      private const IDENTICAL:int = 0;
   }
}

Here’s a quick usage example from the unit tests for my CellRenderer class. Note that this code has not been spit-shined, so it’s a bit rougher. I’ve included just enough of the class to communicate the gist.

override public function setUp():void
{
   super.setUp();
   surface = createSurface();
   cellRenderer = new CellRenderer();
   cell = MapCellFactory.createCell(0,0);
   terrain = createTestTerrain();
   testSheet = createTestSheet();
   
   cellRenderer.useSurface(surface);
   cell.terrain = terrain;
}
public function testCellTerrain():void
{
   target = new BitmapData(TILE_SIZE, TILE_SIZE, false, 0xff0000);
   
   assertBitmapsDoNotMatch(target, surface);
   cellRenderer.renderCellFromSheet(cell, testSheet);
   assertBitmapsMatch(target, surface);
}
public function testCellWithActor():void
{
   target = new BitmapData(TILE_SIZE, TILE_SIZE, false, 0xff0000);
   target.fillRect(new Rectangle((TILE_SIZE-2)/2,(TILE_SIZE-2)/2,2,2), 0xffffff);
   
   var actorSprite:BitmapData = new BitmapData(TILE_SIZE, TILE_SIZE, true, 0x00000000);
   actorSprite.fillRect(new Rectangle((TILE_SIZE-2)/2,(TILE_SIZE-2)/2,2,2), 0xffffffff);
   
   var actor:Actor = new Actor();
   actor.sprite = actorSprite;
   cell.addActor(actor);
   
   assertBitmapsDoNotMatch(target, surface);
   cellRenderer.renderCellFromSheet(cell, testSheet);
   assertBitmapsMatch(target, surface);
}