Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: JUnit and Testcases



I've submitted bunches of UI-related tests which use Abbot (abbot.sf.net) to demonstrate failures. Abbot provides extensions to JUnit to facilitate poking and prodding your UI at a significantly higher level than java.awt.Robot.

It's kind of a pain to have to strip things back down to java.awt.Robot when submitting the test.

Here's an example of a UI test using the Abbot framework:

public class MyUITest extends ComponentTestFixture {
boolean flag;
public void testClickButton() {
JButton button = new JButton("buggy");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { flag = true; }
});
showFrame(button);
ComponentTester tester = new ComponentTester();
tester.actionClick(button);
assertTrue("Button was never pressed", flag);
}
}

On Mar 31, 2004, at 2:32 PM, Michael McDougall wrote:

A year or so back the java team started a concerted push on java-dev for getting testcases of the form:


static void main(String args) {
...
doSomeCoolTest()
...
}

You folks on java-dev have responded by giving us a lot of great support -- sending in short, reproducible testcases with your bug reports.

These are perfect for isolating particular problems.

At the same time, sometimes tests of this form are a little tricky to transform into tests that watch for regressions.

Here on the java team we're getting even further into doing automated regression -- so we think it's appropriate that we let java-dev know that if a developer puts forth the extra effort of making a JUnit test which flags a problem without human intervention, the possibility that we here inside Apple will run it in our test harness against every single nightly build we do goes up dramatically. If we can get to the point where your issues can easily be automatically tested inside Apple as changes are made in our sources, we all benefit. The bottom line? We encourage anyone and everyone to submit JUnit style tests with their bug reports.

Please note -- if your testcase can't easily be expressed as a JUnit test, we still want it! If you simply don't have time to compose a JUnit test, we still want your ready-to-run test code! Nothing clarifies the issues around a defect report like having access to running code that displays the problem.

I welcome questions, comments, or other feedback (to the list, please).



Here's a real example of a JUnit test we run. To run this yourself, try the following:

1) Download JUnit and put it in your classpath.
(See www.junit.org or junit.sourceforge.net)
(I just stick junit.jar in /Library/Java/Extensions/ because I use it so much)
2) Dut and paste the file below into someplace convenient like /tmp/InterpolationTest.java
3) cd /tmp/
4) javac InterpolationTest.java
5) java InterpolationTest

I welcome questions, comments, or other feedback (to the list, please).


Mikey McDougall
Java Team
Apple Computer


/**
* File: InterpolationTest
*
* Spot check the behavior of Interpolation RenderingHints
*/

import java.awt.*;
import java.awt.image.*;
import junit.framework.*;

/**
* Adapting simple tests for the test harness.
*
*/

public class InterpolationTest extends TestCase {
static final int srcWidth = 5;
static final int srcHeight = 3;
static final Rectangle srcRect = new Rectangle(0,0,srcWidth,srcHeight);

static final int dstWidth = srcWidth;
static final int dstHeight = srcHeight * 3;
static final Rectangle dstRect = new Rectangle(0,0,dstWidth,dstHeight);

protected GraphicsEnvironment ge;
protected GraphicsDevice gd;
protected GraphicsConfiguration gc;
protected BufferedImage src;
protected BufferedImage dst;


protected void setUp() {
// Get the default (or compatible) buffered image
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = ge.getDefaultScreenDevice();
gc = gd.getDefaultConfiguration();

// create source and destination images
src = gc.createCompatibleImage( srcWidth,srcHeight );
dst = gc.createCompatibleImage( dstWidth,dstHeight );

}

public void testExpandNN() {
assertNotNull( src );
assertNotNull( dst );

// Draw some bits into the source image
Graphics2D sg = (Graphics2D) src.createGraphics();
sg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
sg.setColor(Color.black);
sg.fillRect(0, 0, srcWidth,srcHeight );

// draw a single horizonal blue line
sg.setColor(Color.blue);
sg.drawLine(0, 1, srcWidth-1, 1);


// Now stretch it using VALUE_INTERPOLATION_NEAREST_NEIGHBOR
Graphics2D dg = (Graphics2D) dst.createGraphics();
dg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
dg.drawImage(src, 0, 0, dstWidth, dstHeight, null);
dg.dispose();

// spot check the values
int blues[] = new int[9];
for (int i = 0; i < 9; i++) {
blues[i] = dst.getRGB(3,i) & 0x000000FF;
}

assertEquals("top part, pixel 0 should be clear", 0x00, blues[0]);
assertEquals("top part, pixel 1 should be clear", 0x00, blues[1]);
assertEquals("top part, pixel 2 should be clear", 0x00, blues[2]);

assertEquals("middle part, pixel 3 should be blue", 0xFF, blues[3]);
assertEquals("middle part, pixel 4 should be blue", 0xFF, blues[4]);
assertEquals("middle part, pixel 5 should be blue", 0xFF, blues[5]);

assertEquals("bottom part, pixel 6 should be clear", 0x00, blues[6]);
assertEquals("bottom part, pixel 7 should be clear", 0x00, blues[7]);
assertEquals("bottom part, pixel 8 should be clear", 0x00, blues[8]);

sg.dispose();
}

public void testExpandBIL() {
assertNotNull( src );
assertNotNull( dst );

// Draw some bits into the source image
Graphics2D sg = (Graphics2D) src.createGraphics();
sg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
sg.setColor(Color.black);
sg.fillRect(0, 0, srcWidth,srcHeight );

sg.setColor(Color.blue);
sg.drawLine(0, 1, srcWidth-1, 1);


// Now stretch it using VALUE_INTERPOLATION_BILINEAR
Graphics2D dg = (Graphics2D) dst.createGraphics();
dg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
dg.drawImage(src, 0, 0, dstWidth, dstHeight, null);
dg.dispose();

// spot check the values
int blues[] = new int[9];
for (int i = 0; i < 9; i++) {
blues[i] = dst.getRGB(3,i) & 0x000000FF;
}

assertEquals("top part, pixel 0 should be clear", 0x00, blues[0]);
assertEquals("top part, pixel 1 should be clear", 0x00, blues[1]);

assertTrue("top middle part should fade up to blue", blues[1] < blues[2]);
assertTrue("top middle part should fade up to blue", blues[2] < blues[3]);
assertTrue("top middle part should fade up to blue", blues[3] < blues[4]);


assertEquals("middle part, pixel 4 should be blue", 0xFF, blues[4]);

assertTrue("bottom middle part should fade down to white", blues[4] > blues[5]);
assertTrue("bottom middle part should fade down to white", blues[5] > blues[6]);
assertTrue("bottom middle part should fade down to white", blues[6] > blues[7]);

assertEquals("bottom part, pixel 7 should be clear", 0x00, blues[7]);
assertEquals("bottom part, pixel 8 should be clear", 0x00, blues[8]);

assertTrue("symetrical about middle", blues[2] == blues[6]);
assertTrue("symetrical about middle", blues[3] == blues[5]);

sg.dispose();
}


// boilerplate below

public static Test suite() {
return new TestSuite(InterpolationTest.class);
}

public static void main (String[] args) {
junit.textui.TestRunner.run(suite());
}
}
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >JUnit and Testcases (From: Michael McDougall <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.