001: package abbot.script;
002:
003: import java.awt.*;
004: import java.applet.*;
005: import java.util.*;
006:
007: import junit.extensions.abbot.*;
008: import abbot.finder.matchers.*;
009:
010: public class AppletviewerTest extends ResolverFixture {
011:
012: private static boolean manual;
013: private TestAppletviewer viewer;
014:
015: public static class TestApplet extends Applet {
016: public static volatile Applet instance = null;
017:
018: public void init() {
019: setBackground(java.awt.Color.green);
020: instance = this ;
021: }
022:
023: public void destroy() {
024: instance = null;
025: }
026: }
027:
028: // Until applet disposal is worked out, only run these tests manually
029: public void runBare() throws Throwable {
030: if (manual) {
031: super .runBare();
032: }
033: }
034:
035: protected void tearDown() throws Exception {
036: // ensure appletviewer instances get disposed of properly
037: if (viewer != null) {
038: viewer.dispose();
039: }
040: viewer = null;
041: }
042:
043: public void testLaunch() throws Throwable {
044: viewer = new TestAppletviewer(new HashMap());
045: viewer.run();
046: // Step should not return until the applet is visible
047: assertTrue("Test applet should be instantiated after step run",
048: TestApplet.instance != null);
049: assertTrue("Test applet should be visible", TestApplet.instance
050: .isShowing());
051: }
052:
053: public void testGeneratedHTML() {
054: HashMap map = new HashMap();
055: map.put("name", "value");
056: viewer = new TestAppletviewer(map);
057:
058: String html = viewer.generateHTML();
059: assertTrue("Missing html tag: " + html, html
060: .startsWith("<html>"));
061: assertTrue("Missing applet tag: " + html, html
062: .indexOf("<applet") != -1);
063: assertTrue("Code param missing: " + html, html
064: .indexOf("code=\"" + TestApplet.class.getName()) != -1);
065: assertTrue("Params missing: " + html, html
066: .indexOf("><param name=\"name\" value=\"value\"") != -1);
067: }
068:
069: public void testClassLoader() throws Throwable {
070: viewer = new TestAppletviewer(new HashMap());
071: viewer.run();
072: Component c = getFinder().find(
073: new ClassMatcher(Applet.class, true));
074: assertEquals(
075: "Appletviewer should provide the Applet's class loader",
076: c.getClass().getClassLoader(), viewer
077: .getContextClassLoader());
078: }
079:
080: public void testDocumentBase() throws Throwable {
081: viewer = new TestAppletviewer(new HashMap());
082: viewer.run();
083: Applet applet = (Applet) getFinder().find(
084: new ClassMatcher(Applet.class, true));
085:
086: assertNotNull("Document base should not be null", applet
087: .getDocumentBase());
088: }
089:
090: public void testThreadsDisposed() throws Throwable {
091: viewer = new TestAppletviewer(new HashMap());
092: viewer.run();
093: viewer.run();
094: viewer.run();
095: Iterator iter = getHierarchy().getRoots().iterator();
096: while (iter.hasNext()) {
097: getHierarchy().dispose((Window) iter.next());
098: }
099:
100: ThreadGroup group = Thread.currentThread().getThreadGroup();
101: while (group.getParent() != null) {
102: group = group.getParent();
103: }
104: int count = group.activeCount();
105: Thread[] list = new Thread[count];
106: count = group.enumerate(list);
107:
108: for (int i = 0; i < list.length; i++) {
109: Thread thread = list[i];
110: if (thread != null) {
111: String name = thread.getName();
112: if (name != null && name.indexOf("EventQueue") != -1
113: && name.indexOf("(abbot)") != -1) {
114: //Log.debug("Thread " + thread + " still running");
115: assertFalse("Event dispatch thread " + thread
116: + " should not still be running", thread
117: .isAlive());
118: }
119: }
120: }
121: }
122:
123: private class TestAppletviewer extends Appletviewer {
124: public TestAppletviewer(Map map) {
125: this (map, "CODEBASE", "ARCHIVE");
126: }
127:
128: public TestAppletviewer(Map map, String codebase, String archive) {
129: super (AppletviewerTest.this .getResolver(), getName(),
130: TestApplet.class.getName(), map, codebase, archive,
131: null);
132: }
133:
134: public void runStep() throws Throwable {
135: super .runStep();
136: getFrame().setName(getName());
137: getFrame().setTitle(getName());
138: }
139:
140: public String generateHTML() {
141: return super .generateHTML();
142: }
143:
144: protected boolean removeSMOnExit() {
145: return true;
146: }
147:
148: public void dispose() {
149: terminate();
150: }
151: }
152:
153: public static void main(String[] args) {
154: manual = true;
155: TestHelper.runTests(args, AppletviewerTest.class);
156: }
157: }
|