Revision: 741
Author: allain.lalonde
Date: Mon Oct 19 07:28:52 2009
Log: Adding tests for PFrame and PApplet as well as structuring shadow  
testing.
http://code.google.com/p/piccolo2d/source/detail?r=741

Added:
   
/piccolo2d.java/trunk/extras/src/test/java/edu/umd/cs/piccolox/PAppletTest.java
   
/piccolo2d.java/trunk/extras/src/test/java/edu/umd/cs/piccolox/PFrameTest.java
Modified:
   
/piccolo2d.java/trunk/extras/src/main/java/edu/umd/cs/piccolox/util/ShadowUtils.java
   
/piccolo2d.java/trunk/extras/src/test/java/edu/umd/cs/piccolox/nodes/PShadowTest.java
   
/piccolo2d.java/trunk/extras/src/test/java/edu/umd/cs/piccolox/util/ShadowUtilsTest.java

=======================================
--- /dev/null
+++  
/piccolo2d.java/trunk/extras/src/test/java/edu/umd/cs/piccolox/PAppletTest.java 
 
Mon Oct 19 07:28:52 2009
@@ -0,0 +1,26 @@
+package edu.umd.cs.piccolox;
+
+import junit.framework.TestCase;
+import edu.umd.cs.piccolo.PCanvas;
+
+public class PAppletTest extends TestCase {
+    private PApplet applet;
+
+    public void setUp() {
+        applet = new PApplet();
+        applet.init();
+        applet.setVisible(false);
+    }
+
+    public void tearDown() {
+        applet.setVisible(false);
+    }
+
+    public void testCanvasIsValidWithDefaultConstructor() {
+        PCanvas canvas = applet.getCanvas();
+        assertNotNull(canvas);
+        assertNotNull(canvas.getLayer());
+        assertNotNull(canvas.getCamera());
+        assertSame(canvas.getLayer(), canvas.getCamera().getLayer(0));
+    }
+}
=======================================
--- /dev/null
+++  
/piccolo2d.java/trunk/extras/src/test/java/edu/umd/cs/piccolox/PFrameTest.java  
 
Mon Oct 19 07:28:52 2009
@@ -0,0 +1,46 @@
+package edu.umd.cs.piccolox;
+
+import java.awt.event.KeyListener;
+
+import junit.framework.TestCase;
+import edu.umd.cs.piccolo.PCanvas;
+
+public class PFrameTest extends TestCase {
+    private PFrame frame;
+
+    public void testCanvasIsValidWithDefaultConstructor() {
+        PFrame frame = new PFrame() {
+            public void setVisible(boolean visible) {
+                // why oh why is PFrame visible by default
+            }
+        };
+        PCanvas canvas = frame.getCanvas();
+        assertNotNull(canvas);
+        assertNotNull(canvas.getLayer());
+        assertNotNull(canvas.getCamera());
+        assertSame(canvas.getLayer(), canvas.getCamera().getLayer(0));
+    }
+
+    public void testDefaultsToWindowed() {
+        PFrame frame = new PFrame() {
+            public void setVisible(boolean visible) {
+                // why oh why is PFrame visible by default
+            }
+        };
+        assertFalse(frame.isFullScreenMode());
+    }
+
+    public void testFullScreenModeInstallsEscapeListeners() {
+        PFrame frame = new PFrame();
+        frame.setFullScreenMode(true);
+
+
+        KeyListener[] listeners = frame.getCanvas().getKeyListeners();
+        assertEquals(1, listeners.length);
+
+        KeyListener listener = listeners[0];
+        assertNotNull(listener);
+        frame.setVisible(false);
+        frame.setFullScreenMode(false);
+    }
+}
=======================================
---  
/piccolo2d.java/trunk/extras/src/main/java/edu/umd/cs/piccolox/util/ShadowUtils.java
     
Wed Oct 14 08:47:49 2009
+++  
/piccolo2d.java/trunk/extras/src/main/java/edu/umd/cs/piccolox/util/ShadowUtils.java
     
Mon Oct 19 07:28:52 2009
@@ -50,21 +50,22 @@
      private ShadowUtils() {
          // empty
      }
-

      /**
-     * Create and return a new buffered image containing a shadow of the  
specified source image
-     * using the specifed shadow paint and gaussian blur radius.  The  
dimensions of the returned image will be
-     * <code>src.getWidth() + 4 * blurRadius</code> x  
<code>src.getHeight() + 4 * blurRadius</code>
-     * to account for blurring beyond the bounds of the source image.   
Thus the source image
-     * will appear to be be offset by (<code>2 * blurRadius</code>,  
<code>2 * blurRadius</code>)
-     * in the returned image.
-     *
+     * Create and return a new buffered image containing a shadow of the
+     * specified source image using the specifed shadow paint and gaussian  
blur
+     * radius. The dimensions of the returned image will be
+     * <code>src.getWidth() + 4 * blurRadius</code> x
+     * <code>src.getHeight() + 4 * blurRadius</code> to account for  
blurring
+     * beyond the bounds of the source image. Thus the source image will  
appear
+     * to be be offset by (<code>2 * blurRadius</code>,
+     * <code>2 * blurRadius</code>) in the returned image.
+     *
       * @param src source image, must not be null
       * @param shadowPaint shadow paint
       * @param blurRadius gaussian blur radius, must be <code>&gt; 0</code>
-     * @return a new buffered image containing a shadow of the specified  
source image
-     *   using the specifed shadow paint and gaussian blur radius
+     * @return a new buffered image containing a shadow of the specified  
source
+     *         image using the specifed shadow paint and gaussian blur  
radius
       */
      public static BufferedImage createShadow(final Image src, final Paint  
shadowPaint, final int blurRadius) {
          if (src == null) {
@@ -100,19 +101,20 @@

          /**
           * Create a new gaussian kernel with the specified blur radius.
-         *
+         *
           * @param blurRadius blur radius
           */
          GaussianKernel(final int blurRadius) {
              super((2 * blurRadius) + 1, (2 * blurRadius) + 1,  
createKernel(blurRadius));
          }
-

          /**
-         * Create an array of floats representing a gaussian kernel with  
the specified radius.
-         *
+         * Create an array of floats representing a gaussian kernel with  
the
+         * specified radius.
+         *
           * @param r radius
-         * @return an array of floats representing a gaussian kernel with  
the specified radius
+         * @return an array of floats representing a gaussian kernel with  
the
+         *         specified radius
           */
          private static float[] createKernel(final int r) {
              int w = (2 * r) + 1;
@@ -121,16 +123,17 @@
              double n = Math.PI * m;

              double sum = 0.0d;
-            for (int i = 0; i < w; i++) {
-                for (int j = 0; j < w; j++) {
-                    kernel[i * w + j] = (float) (Math.pow(Math.E, -((j -  
r) * (j - r) + (i - r) * (i - r)) / m) / n);
-                    sum += kernel[i * w + j];
+            for (int x = 0; x < w; x++) {
+                int xr2 = (x-r) * (x-r);
+                for (int y = 0; y < w; y++) {
+                    int yr2 = (y-r) * (y-r);
+                    kernel[x * w + y] = (float) (Math.pow(Math.E, -(yr2 +  
xr2) / m) / n);
+                    sum += kernel[x * w + y];
                  }
              }
-            for (int i = 0; i < w; i++) {
-                for (int j = 0; j < w; j++) {
-                    kernel[i * w + j] /= sum;
-                }
+
+            for (int i = kernel.length - 1; i >= 0; i--) {
+                kernel[i] /= sum;
              }
              return kernel;
          }
=======================================
---  
/piccolo2d.java/trunk/extras/src/test/java/edu/umd/cs/piccolox/nodes/PShadowTest.java
    
Thu Aug 20 14:06:00 2009
+++  
/piccolo2d.java/trunk/extras/src/test/java/edu/umd/cs/piccolox/nodes/PShadowTest.java
    
Mon Oct 19 07:28:52 2009
@@ -40,43 +40,26 @@
   * Unit test for PShadow.
   */
  public final class PShadowTest extends TestCase {
-
-    public void testConstructor() {
-        BufferedImage src = new BufferedImage(100, 100,  
BufferedImage.TYPE_INT_ARGB);
+    private static final int TEST_IMAGE_WIDTH = 25;
+    private static final int TEST_IMAGE_HEIGHT = 10;
+    private BufferedImage src;
+    private Color shadowPaint;
+
+    public void setUp() {
+        shadowPaint = new Color(20, 20, 20, 200);
+        src = new BufferedImage(TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT,  
BufferedImage.TYPE_INT_ARGB);
          Paint srcPaint = new Color(255, 0, 0, 200);
-        Paint shadowPaint = new Color(20, 20, 20, 200);
+
          Graphics2D g = src.createGraphics();
          g.setPaint(srcPaint);
          g.drawRect(25, 25, 50, 50);
          g.dispose();
-
-        for (int blurRadius = 1; blurRadius < 33; blurRadius += 4) {
-            PShadow shadowNode = new PShadow(src, shadowPaint, blurRadius);
-            assertNotNull(shadowNode);
-            assertEquals(src.getWidth() + 4 * blurRadius,  
shadowNode.getWidth(), 0.001d);
-            assertEquals(src.getHeight() + 4 * blurRadius,  
shadowNode.getHeight(), 0.001d);
-        }
-
-        try {
-            new PShadow(null, shadowPaint, 4);
-            fail("ctr(null, ...) expected IllegalArgumentException");
-        }
-        catch (IllegalArgumentException e) {
-            // expected
-        }
-        try {
-            new PShadow(src, shadowPaint, 0);
-            fail("ctr(..., -1) expected IllegalArgumentException");
-        }
-        catch (IllegalArgumentException e) {
-            // expected
-        }
-        try {
-            new PShadow(src, shadowPaint, -1);
-            fail("ctr(..., -1) expected IllegalArgumentException");
-        }
-        catch (IllegalArgumentException e) {
-            // expected
-        }
+    }
+
+    public void testShadowCreatesCorrectImageSize() {
+        PShadow shadowNode = new PShadow(src, shadowPaint, 4);
+        assertNotNull(shadowNode);
+        assertEquals(TEST_IMAGE_WIDTH + 16, shadowNode.getWidth(), 0.001d);
+        assertEquals(TEST_IMAGE_HEIGHT + 16, shadowNode.getHeight(),  
0.001d);
      }
  }
=======================================
---  
/piccolo2d.java/trunk/extras/src/test/java/edu/umd/cs/piccolox/util/ShadowUtilsTest.java
         
Thu Aug 20 14:06:00 2009
+++  
/piccolo2d.java/trunk/extras/src/test/java/edu/umd/cs/piccolox/util/ShadowUtilsTest.java
         
Mon Oct 19 07:28:52 2009
@@ -31,52 +31,69 @@
  import java.awt.Color;
  import java.awt.Graphics2D;
  import java.awt.Paint;
-
  import java.awt.image.BufferedImage;

+import edu.umd.cs.piccolox.nodes.PShadow;
+
  import junit.framework.TestCase;

  /**
   * Unit test for ShadowUtils.
   */
  public final class ShadowUtilsTest extends TestCase {
-
-    public void testCreateShadow() {
-        BufferedImage src = new BufferedImage(100, 100,  
BufferedImage.TYPE_INT_ARGB);
+    private static final int TEST_IMAGE_SIZE = 25;
+    private static final Paint shadowPaint = new Color(20, 20, 20, 200);
+    private BufferedImage src;
+
+    public void setUp() {
+        src = new BufferedImage(TEST_IMAGE_SIZE, TEST_IMAGE_SIZE,  
BufferedImage.TYPE_INT_ARGB);
          Paint srcPaint = new Color(255, 0, 0, 200);
-        Paint shadowPaint = new Color(20, 20, 20, 200);
          Graphics2D g = src.createGraphics();
          g.setPaint(srcPaint);
          g.drawRect(25, 25, 50, 50);
          g.dispose();
-
-        for (int blurRadius = 1; blurRadius < 33; blurRadius += 4) {
-            BufferedImage dest = ShadowUtils.createShadow(src,  
shadowPaint, blurRadius);
-            assertNotNull(dest);
-            assertEquals(src.getWidth() + 4 * blurRadius, dest.getWidth());
-            assertEquals(src.getHeight() + 4 * blurRadius,  
dest.getHeight());
-        }
-
+    }
+
+    public void testCreateShadowAcceptsTinyShadow() {
+        BufferedImage dest = ShadowUtils.createShadow(src, shadowPaint, 1);
+        assertNotNull(dest);
+        assertEquals(TEST_IMAGE_SIZE + 4, dest.getWidth());
+        assertEquals(TEST_IMAGE_SIZE + 4, dest.getHeight());
+    }
+
+    public void testCreateShadowAcceptsHugeShadow() {
+        BufferedImage dest = ShadowUtils.createShadow(src, shadowPaint,  
25);
+        assertNotNull(dest);
+        assertEquals(TEST_IMAGE_SIZE + 100, dest.getWidth());
+        assertEquals(TEST_IMAGE_SIZE + 100, dest.getHeight());
+    }
+
+    public void testNonPositiveBlurRadiusFails() {
          try {
-            ShadowUtils.createShadow(null, shadowPaint, 4);
-            fail("createShadow(null, ...) expected  
IllegalArgumentException");
+            ShadowUtils.createShadow(src, shadowPaint, 0);
+            fail("Non positive blur radius should fail");
          }
          catch (IllegalArgumentException e) {
              // expected
          }
+
          try {
-            ShadowUtils.createShadow(src, shadowPaint, 0);
-            fail("createShadow(..., 0) expected IllegalArgumentException");
+            ShadowUtils.createShadow(src, shadowPaint, -1);
+            fail("Non positive blur radius should fail");
          }
          catch (IllegalArgumentException e) {
              // expected
          }
+    }
+
+    public void testConstructorDoesNotAcceptNullSrc() {
          try {
-            ShadowUtils.createShadow(src, shadowPaint, -1);
-            fail("createShadow(..., -1) expected  
IllegalArgumentException");
+            ShadowUtils.createShadow(null, Color.BLACK, 4);
+            fail("ctr(null, ...) expected IllegalArgumentException");
          }
          catch (IllegalArgumentException e) {
              // expected
          }
      }
-}
+
+}

--~--~---------~--~----~------------~-------~--~----~
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to