/manual
test sends
events to System.out/System.err
so that the user can determine
whether the test behaved properly. How do I write my test if I can't see these
output streams?The test code should be written to determine whether a test has passed or
failed based on events generated during a given time-frame. Use the
/manual=done
option of the applet
action to set the
time frame. If the user has not generated the expected event before the
done
button has been pressed, the test should detect this in the
destroy
method and throw an exception.
While this approach takes potentially more time to implement, it avoids user error which may occur in checking the event. This scheme also avoids string comparison of events. (A much safer way to determine whether the expected event has been received is to check the event type, coordinates, modifiers, etc.)
Warning! The AWT event thread does not propagate exceptions! It is
recommended that all exceptions indicating failure of the test be thrown from
one of the methods called by the harness. (i.e. init()
,
start()
, stop()
, destroy()
)
The following simple applet
test illustrates the recommended
behavior.
Basic .html
test description file.
<html> <body> <!-- @test @bug 2997924 @summary Sample test that verifies an event @run applet/manual=done SampleTest.html --> <applet code=SampleTest width=200 height=50></applet> Select the "pick me" check box. </body> </html>
The sample test code.
import java.applet.Applet; import java.awt.Checkbox; import java.awt.FlowLayout; import java.awt.Panel; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; // WARNING! The AWT event thread does not propagate exceptions! // It is recommended that all exceptions indicating failure // of the test be thrown from one of the methods called by the harness. // (i.e. init(), start(), stop(), destroy()) public class SampleTest extends Applet { public void init() { setLayout(new FlowLayout()); add(new TestPanel(this)); } public void destroy() { if (myEvent == null) throw new RuntimeException("no events"); else { Checkbox cb = (Checkbox)(myEvent.getItemSelectable()); if (!(cb.getLabel().equals("pick me!") && cb.getState())) throw new RuntimeException("unexpected last event"); } } class TestPanel extends Panel { Checkbox pickMe, notMe; Listener listener = new Listener(); Applet applet; public TestPanel(Applet myApplet) { applet = myApplet; pickMe = new Checkbox("pick me!"); notMe = new Checkbox("not me"); pickMe.addItemListener(listener); notMe.addItemListener(listener); add(pickMe); add(notMe); } class Listener implements ItemListener { // Don't throw an exception here. The awt event thread // will NOT propagate your exception! public void itemStateChanged(ItemEvent event) { System.out.println("event: " + event); myEvent = event; } } } private ItemEvent myEvent; }