Skip to content

Commit 3012e0c

Browse files
author
jossonsmith
committed
Setup environment for asynchronous SWT unit tests. The unit tests are for both native
SWT application and Java2Script application. For more details about how to write asynchronous SWT unit tests, please read net.sf.j2s.test.swt.ajunit.SWTSampleTest
1 parent 969fe64 commit 3012e0c

20 files changed

Lines changed: 1213 additions & 75 deletions

tests/net.sf.j2s.test.swt/.classpath

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
55
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
6+
<classpathentry combineaccessrules="false" kind="src" path="/net.sf.j2s.ajax"/>
7+
<classpathentry kind="src" path="ajaxjunit"/>
68
<classpathentry kind="output" path="bin"/>
79
</classpath>

tests/net.sf.j2s.test.swt/.j2s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Java2Script Configuration
2-
#Fri Jul 14 02:50:02 CST 2006
3-
j2s.resources.list=../../plugins/net.sf.j2s.lib_1.0.0/j2slib/j2slib.z.js
4-
j2s.output.path=bin
2+
#Tue Aug 01 23:08:59 CST 2006
3+
j2s.resources.list=../../plugins/net.sf.j2s.lib_1.0.0/j2slib/j2slib.z.js,bin/org/eclipse/swt/snippets/NewComposite2.js,bin/org/eclipse/swt/snippets/NewComposite1.js,bin/org/eclipse/swt/snippets/NewComposite.js,bin/net/sf/j2s/test/swt/widgets/j2stest.js,bin/net/sf/j2s/test/swt/widgets/Widgets.js,bin/net/sf/j2s/test/swt/widgets/TestWidgetHookEventType.js,bin/net/sf/j2s/test/swt/widgets/TestWidgetData.js,bin/net/sf/j2s/test/swt/widgets/TestTextVerifyText.js,bin/net/sf/j2s/test/swt/widgets/TestText.js,bin/net/sf/j2s/test/swt/widgets/TestTabFolder.js,bin/net/sf/j2s/test/swt/widgets/TestShell.js,bin/net/sf/j2s/test/swt/widgets/TestLink.js,bin/net/sf/j2s/test/swt/widgets/TestLabelHookEventType.js,bin/net/sf/j2s/test/swt/widgets/TestLabel.js,bin/net/sf/j2s/test/swt/widgets/TestGroup.js,bin/net/sf/j2s/test/swt/widgets/TestDeferredLayout.js,bin/net/sf/j2s/test/swt/widgets/TestComposite.js,bin/net/sf/j2s/test/swt/widgets/TestButton.js,bin/net/sf/j2s/test/swt/widgets/TestBrowser.js,bin/net/sf/j2s/test/swt/widgets/Snippet24.js,bin/net/sf/j2s/test/swt/widgets/Snippet109.js,bin/net/sf/j2s/test/swt/extra/TestHTML.js,bin/net/sf/j2s/test/swt/extra/TemplateUtil.js,bin/net/sf/j2s/test/swt/extra/TabCognize.js,bin/net/sf/j2s/test/swt/extra/Range.js,bin/net/sf/j2s/test/swt/TestSWTPlugin.js,bin/net/sf/j2s/test/swt/widgets/ButtonTest.js,bin/net/sf/j2s/test/swt/widgets/AsyncButtonTest.js,bin/net/sf/j2s/ajax/junit/ResultPrinterBridge.js,bin/net/sf/j2s/ajax/junit/AsyncTestSuite.js,bin/net/sf/j2s/ajax/junit/AsyncTestRunner.js,bin/net/sf/j2s/ajax/junit/AsyncTestRunnable.js,bin/net/sf/j2s/ajax/junit/AsyncTestCase.js,bin/net/sf/j2s/ajax/junit/AsyncSWT.js,bin/net/sf/j2s/test/swt/ajunit/AsyncButtonTest.js,bin/net/sf/j2s/test/swt/ajunit/ButtonTest.js,bin/net/sf/j2s/test/swt/ajunit/SWTEmptyTest.js,bin/net/sf/j2s/test/swt/ajunit/SWTSampleTest.js
54
j2s.abandoned.resources.list=
5+
j2s.output.path=bin
66
j2s.compiler.status=enable

tests/net.sf.j2s.test.swt/META-INF/MANIFEST.MF

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ Bundle-SymbolicName: net.sf.j2s.test.swt
55
Bundle-Version: 1.0.0
66
Bundle-Activator: net.sf.j2s.test.swt.TestSWTPlugin
77
Bundle-Localization: plugin
8-
Require-Bundle: org.eclipse.ui,
9-
org.eclipse.core.runtime
8+
Require-Bundle: org.junit,
9+
org.eclipse.swt
1010
Eclipse-AutoStart: true
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*******************************************************************************
2+
* Java2Script Pacemaker (http://j2s.sourceforge.net)
3+
*
4+
* Copyright (c) 2006 ognize.com and others.
5+
* All rights reserved. This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v1.0
7+
* which accompanies this distribution, and is available at
8+
* http://www.eclipse.org/legal/epl-v10.html
9+
*
10+
* Contributors:
11+
* ognize.com - initial API and implementation
12+
*******************************************************************************/
13+
14+
package net.sf.j2s.ajax.junit;
15+
16+
import junit.framework.AssertionFailedError;
17+
import org.eclipse.swt.widgets.Shell;
18+
19+
/**
20+
* @author josson smith
21+
*
22+
* 2006-7-31
23+
*/
24+
public class AsyncSWT {
25+
26+
/**
27+
* @param shell
28+
* @param runnable
29+
*
30+
* @j2sNative
31+
var oThis = arguments[2];
32+
var closeCleanUp = arguments[3];
33+
var f = function () {
34+
if (runnable != null) {
35+
try {
36+
runnable.run ();
37+
} catch (e) {
38+
if (Clazz.instanceOf (runnable,
39+
net.sf.j2s.ajax.junit.AsyncTestRunnable)) {
40+
if (Clazz.instanceOf (e, junit.framework.AssertionFailedError)) {
41+
runnable.test.testResult.addFailure (runnable.test, e);
42+
} else if (Clazz.instanceOf (e, ThreadDeath)) {
43+
throw e;
44+
} else if (Clazz.instanceOf (e, Throwable)) {
45+
runnable.test.testResult.addError (runnable.test, e);
46+
} else {
47+
throw e;
48+
}
49+
}
50+
} finally {
51+
shell.close ();
52+
//if (f.next != null) {
53+
// f.next ();
54+
//}
55+
}
56+
} else {
57+
shell.close ();
58+
//if (f.next != null) {
59+
// f.next ();
60+
//}
61+
}
62+
};
63+
var key = "j2s.swt.shell.finish.layout";
64+
//if (window[key] != null) {
65+
// f.next = window[key];
66+
//}
67+
window[key] = f;
68+
var closeFun = closeCleanUp;
69+
if (Clazz.instanceOf (runnable, net.sf.j2s.ajax.junit.AsyncTestRunnable)) {
70+
closeFun = function () {
71+
closeCleanUp ();
72+
runnable.callback ();
73+
};
74+
}
75+
Sync2Async.block (shell, oThis, closeFun);
76+
*/
77+
public static void waitLayout(Shell shell, Runnable runnable) {
78+
if (runnable != null) {
79+
try {
80+
runnable.run();
81+
} catch (Throwable e) {
82+
//throw (Error) e;
83+
if (runnable instanceof AsyncTestRunnable) {
84+
AsyncTestRunnable aTest = (AsyncTestRunnable) runnable;
85+
if (e instanceof AssertionFailedError) {
86+
aTest.test.getTestResult()
87+
.addFailure (aTest.test, (AssertionFailedError) e);
88+
} else if (e instanceof ThreadDeath) {
89+
throw (ThreadDeath) e;
90+
} else if (e instanceof Throwable) {
91+
aTest.test.getTestResult()
92+
.addError (aTest.test, e);
93+
} else {
94+
throw (Error) e;
95+
}
96+
}
97+
} finally {
98+
shell.close();
99+
}
100+
} else {
101+
shell.close();
102+
}
103+
if (runnable instanceof AsyncTestRunnable) {
104+
final AsyncTestRunnable aTest = (AsyncTestRunnable) runnable;
105+
//aTest.callback();
106+
new Thread(new Runnable() {
107+
public void run() {
108+
try {
109+
Thread.sleep(100);
110+
} catch (InterruptedException e) {
111+
e.printStackTrace();
112+
}
113+
aTest.callback();
114+
}
115+
}).start();
116+
}
117+
}
118+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*******************************************************************************
2+
* Java2Script Pacemaker (http://j2s.sourceforge.net)
3+
*
4+
* Copyright (c) 2006 ognize.com and others.
5+
* All rights reserved. This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v1.0
7+
* which accompanies this distribution, and is available at
8+
* http://www.eclipse.org/legal/epl-v10.html
9+
*
10+
* Contributors:
11+
* ognize.com - initial API and implementation
12+
*******************************************************************************/
13+
14+
package net.sf.j2s.ajax.junit;
15+
16+
import junit.framework.TestCase;
17+
import junit.framework.TestResult;
18+
19+
/**
20+
* @author josson smith
21+
*
22+
* 2006-8-1
23+
*/
24+
public class AsyncTestCase extends TestCase {
25+
26+
Runnable callback;
27+
28+
TestResult testResult;
29+
30+
public AsyncTestCase() {
31+
super();
32+
}
33+
34+
public AsyncTestCase(String name) {
35+
super(name);
36+
}
37+
38+
public Runnable getCallback() {
39+
return callback;
40+
}
41+
42+
public void setCallback(Runnable callback) {
43+
this.callback = callback;
44+
}
45+
46+
public TestResult getTestResult() {
47+
return testResult;
48+
}
49+
50+
public void setTestResult(TestResult testRestult) {
51+
this.testResult = testRestult;
52+
}
53+
54+
55+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*******************************************************************************
2+
* Java2Script Pacemaker (http://j2s.sourceforge.net)
3+
*
4+
* Copyright (c) 2006 ognize.com and others.
5+
* All rights reserved. This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v1.0
7+
* which accompanies this distribution, and is available at
8+
* http://www.eclipse.org/legal/epl-v10.html
9+
*
10+
* Contributors:
11+
* ognize.com - initial API and implementation
12+
*******************************************************************************/
13+
14+
package net.sf.j2s.ajax.junit;
15+
16+
/**
17+
* @author josson smith
18+
*
19+
* 2006-7-31
20+
*/
21+
public abstract class AsyncTestRunnable implements Runnable {
22+
public AsyncTestCase test;
23+
24+
25+
public AsyncTestRunnable(AsyncTestCase test) {
26+
super();
27+
this.test = test;
28+
}
29+
30+
public void callback() {
31+
if (test.callback != null) {
32+
test.callback.run();
33+
}
34+
}
35+
36+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*******************************************************************************
2+
* Java2Script Pacemaker (http://j2s.sourceforge.net)
3+
*
4+
* Copyright (c) 2006 ognize.com and others.
5+
* All rights reserved. This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v1.0
7+
* which accompanies this distribution, and is available at
8+
* http://www.eclipse.org/legal/epl-v10.html
9+
*
10+
* Contributors:
11+
* ognize.com - initial API and implementation
12+
*******************************************************************************/
13+
14+
package net.sf.j2s.ajax.junit;
15+
16+
import java.io.PrintStream;
17+
import junit.framework.Test;
18+
import junit.framework.TestResult;
19+
import junit.textui.ResultPrinter;
20+
import junit.textui.TestRunner;
21+
22+
/**
23+
* @author josson smith
24+
*
25+
* 2006-7-31
26+
*/
27+
public class AsyncTestRunner extends TestRunner{
28+
private ResultPrinterBridge f2Printer;
29+
30+
public AsyncTestRunner() {
31+
this(System.out);
32+
}
33+
34+
public AsyncTestRunner(PrintStream writer) {
35+
this(new ResultPrinterBridge(writer));
36+
}
37+
38+
public AsyncTestRunner(ResultPrinter printer) {
39+
super(printer);
40+
this.f2Printer = (ResultPrinterBridge) printer;
41+
}
42+
43+
public TestResult doRun(Test suite, final boolean wait) {
44+
final TestResult result= createTestResult();
45+
result.addListener(f2Printer);
46+
final long startTime= System.currentTimeMillis();
47+
((AsyncTestSuite) suite).run(result, new Runnable() {
48+
public void run() {
49+
long endTime= System.currentTimeMillis();
50+
long runTime= endTime-startTime;
51+
52+
f2Printer.printResult(result, runTime);
53+
54+
pause(wait);
55+
}
56+
});
57+
return result;
58+
}
59+
60+
61+
/**
62+
* Runs a suite extracted from a TestCase subclass.
63+
*/
64+
static public void asyncRun(Class testClass) {
65+
asyncRun(new AsyncTestSuite(testClass));
66+
}
67+
68+
/**
69+
* Runs a single test and collects its results.
70+
* This method can be used to start a test run
71+
* from your program.
72+
* <pre>
73+
* public static void main (String[] args) {
74+
* test.textui.TestRunner.run(suite());
75+
* }
76+
* </pre>
77+
*/
78+
static public TestResult asyncRun(Test test) {
79+
AsyncTestRunner runner= new AsyncTestRunner();
80+
return runner.doRun(test);
81+
}
82+
83+
/**
84+
* Runs a single test and waits until the user
85+
* types RETURN.
86+
*/
87+
static public void asyncRunAndWait(Test suite) {
88+
AsyncTestRunner aTestRunner= new AsyncTestRunner();
89+
aTestRunner.doRun(suite, true);
90+
}
91+
}

0 commit comments

Comments
 (0)