Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d62f4c1e3664d46556702df04236ef26f5622de6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
 *
 */
package org.eclipse.scout.rt.ui.swing;

import static org.junit.Assert.assertEquals;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.verify;
import static org.easymock.EasyMock.replay;

import java.util.Arrays;
import java.util.Collections;

import org.easymock.EasyMock;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.scout.rt.client.IClientSession;
import org.eclipse.scout.rt.ui.swing.extension.ISwingApplicationExtension;
import org.junit.Test;

/**
 * @author awe
 *
 */
public class ExtensibleSwingApplicationTest {

	ExtensibleSwingApplication app;

	@Test
	public void testStop() {
		ISwingApplicationExtension ext = EasyMock.createMock(ISwingApplicationExtension.class);
		IClientSession cs  = EasyMock.createMock(IClientSession.class);
		expect(ext.getClientSession()).andReturn(cs);
		cs.stopSession();
		EasyMock.expectLastCall();
		replay(ext, cs);
		app = new ExtensibleSwingApplication(Collections.singletonList(ext));

		app.stop();
		verify(ext, cs);
	}

	/**
	 * When one of the extensions returns anything other than null, start() is aborted.
	 * @throws Exception
	 */
	@Test
	public void testStart_ExtensionAbort() throws Exception {
		assertStart(IApplication.EXIT_OK, null, IApplication.EXIT_OK);
		assertStart(IApplication.EXIT_RELAUNCH, IApplication.EXIT_RELAUNCH, IApplication.EXIT_OK);
		// assertStart(?, null, null) causes the super.start() method to be called
		// which results in a call to startSubject. These cases are testet in the testStartInSubject* methods
	}

	private void assertStart(Object expected, Object firstReturnValue, Object secondReturnValue) throws Exception {
		ISwingApplicationExtension ext1 = EasyMock.createMock(ISwingApplicationExtension.class);
		ISwingApplicationExtension ext2 = EasyMock.createMock(ISwingApplicationExtension.class);
		expect(ext1.execStart(null, null)).andReturn(firstReturnValue);
		expect(ext2.execStart(null, null)).andReturn(secondReturnValue);
		replay(ext1, ext2);
		app = new ExtensibleSwingApplication(Arrays.asList(ext1, ext2));
		assertEquals(expected, app.start(null));
	}

	@Test
	public void testStartInSubject_Abort() throws Exception {
		ISwingApplicationExtension ext = EasyMock.createMock(ISwingApplicationExtension.class);
		expect(ext.execStartInSubject(null, null)).andReturn(IApplication.EXIT_OK);
		replay(ext);
		app = new ExtensibleSwingApplication(Collections.singletonList(ext));
		assertEquals(IApplication.EXIT_OK, app.startInSubject(null));
	}

	/*

	Cannot test this because showLoadError() opens a Swing message box which blocks the application.

	@Test
	public void testStartInSubject_CheckClientSession_Abort() throws Exception {
		ISwingApplicationExtension ext = EasyMock.createMock(ISwingApplicationExtension.class);
		IClientSession cs = EasyMock.createMock(IClientSession.class);
		expect(ext.execStartInSubject(null, null)).andReturn(null);
		expect(ext.getClientSession()).andReturn(cs);
		expect(cs.isActive()).andReturn(false);
		expect(cs.getLoadError()).andReturn(new IOException());
		replay(ext, cs);
		app = new ExtensibleSwingApplication(Collections.singletonList(ext));
		assertEquals(IApplication.EXIT_OK, app.startInSubject(null));
		verify(ext, cs);
	}
	*/

	@Test
	public void testStartGUI() throws Exception {
		ISwingApplicationExtension ext = EasyMock.createMock(ISwingApplicationExtension.class);
		ISwingEnvironment env = EasyMock.createMock(ISwingEnvironment.class);
		IClientSession cs = EasyMock.createMock(IClientSession.class);
		expect(ext.getEnvironment()).andReturn(env);
		expect(ext.getClientSession()).andReturn(cs);
		env.showGUI(cs);
		EasyMock.expectLastCall();
		replay(ext, env, cs);
		app = new ExtensibleSwingApplication(Collections.singletonList(ext));
		app.startGUI();
		verify(ext, env, cs);
	}

	@Test
	public void testRunWhileActive_NoExtensions() throws Exception {
		app = new ExtensibleSwingApplication(Collections.<ISwingApplicationExtension>emptyList());
		assertEquals(IApplication.EXIT_OK, Integer.valueOf(app.runWhileActive()));
	}

	/**
	 * This test requires multiple threads. First we lookup for an active client session.
	 * Than the main thread waits on the lock object of this client session. We need a second thread
	 * to call the notifyAll() method on that lock object a bit later. After that call the session is
	 * not active anymore (the 3rd call for isActive() does return false). So the application should
	 * terminate with the exit code of the last session that was terminated.
	 * @throws Exception
	 */
	@Test
	public void testRunWhileActive_OneExtension() throws Exception {
		final Object stateLock = new Object();
		ISwingApplicationExtension ext = EasyMock.createMock(ISwingApplicationExtension.class);
		IClientSession cs = EasyMock.createMock(IClientSession.class);
		expect(ext.getClientSession()).andReturn(cs).anyTimes();
		expect(cs.isActive()).andReturn(true).times(2);
		expect(cs.isActive()).andReturn(false);
		expect(cs.getStateLock()).andReturn(stateLock).anyTimes();
		expect(cs.getExitCode()).andReturn(IApplication.EXIT_RELAUNCH);
		replay(ext, cs);

		Thread t = new Thread() {
			@Override
			public void run() {
				try {
					Thread.sleep(250);
				} catch (InterruptedException e) {
				}
				synchronized (stateLock) {
					stateLock.notifyAll();
				}
			}
		};
		t.start();

		app = new ExtensibleSwingApplication(Collections.singletonList(ext));
		assertEquals(IApplication.EXIT_RELAUNCH, Integer.valueOf(app.runWhileActive()));
	}

	@Test
	public void testInitializeSwing() throws Exception {
		ISwingApplicationExtension ext = EasyMock.createMock(ISwingApplicationExtension.class);
		ext.initializeSwing();
		EasyMock.expectLastCall();
		replay(ext);
		app = new ExtensibleSwingApplication(Collections.singletonList(ext));
		app.initializeSwing();
		verify(ext);
	}

}

Back to the top