Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2004, 2005 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.ui.tests.rcp; |
12 |
|
13 |
import java.io.File; |
14 |
import java.io.FileInputStream; |
15 |
import java.io.FileNotFoundException; |
16 |
import java.io.FileOutputStream; |
17 |
import java.io.IOException; |
18 |
import java.io.InputStreamReader; |
19 |
import java.io.OutputStreamWriter; |
20 |
import java.io.UnsupportedEncodingException; |
21 |
|
22 |
import junit.framework.TestCase; |
23 |
|
24 |
import org.eclipse.core.runtime.IPath; |
25 |
import org.eclipse.core.runtime.IStatus; |
26 |
import org.eclipse.swt.widgets.Display; |
27 |
import org.eclipse.ui.IMemento; |
28 |
import org.eclipse.ui.IWorkbenchWindow; |
29 |
import org.eclipse.ui.PlatformUI; |
30 |
import org.eclipse.ui.WorkbenchException; |
31 |
import org.eclipse.ui.XMLMemento; |
32 |
import org.eclipse.ui.application.ActionBarAdvisor; |
33 |
import org.eclipse.ui.application.IActionBarConfigurer; |
34 |
import org.eclipse.ui.application.IWorkbenchConfigurer; |
35 |
import org.eclipse.ui.application.IWorkbenchWindowConfigurer; |
36 |
import org.eclipse.ui.application.WorkbenchWindowAdvisor; |
37 |
import org.eclipse.ui.internal.UIPlugin; |
38 |
import org.eclipse.ui.tests.rcp.util.WorkbenchAdvisorObserver; |
39 |
|
40 |
public class WorkbenchSaveRestoreStateTest extends TestCase { |
41 |
|
42 |
private static final String ADVISOR_STATE_KEY = "advisorStateKey"; |
43 |
private static final String WINDOW_ADVISOR_STATE_KEY = "windowAdvisorStateKey"; |
44 |
private static final String ACTIONBAR_ADVISOR_STATE_KEY = "actionBarAdvisorStateKey"; |
45 |
|
46 |
public WorkbenchSaveRestoreStateTest(String name) { |
47 |
super(name); |
48 |
} |
49 |
|
50 |
private Display display = null; |
51 |
|
52 |
protected void setUp() throws Exception { |
53 |
super.setUp(); |
54 |
|
55 |
assertNull(display); |
56 |
display = PlatformUI.createDisplay(); |
57 |
assertNotNull(display); |
58 |
} |
59 |
|
60 |
protected void tearDown() throws Exception { |
61 |
assertNotNull(display); |
62 |
display.dispose(); |
63 |
assertTrue(display.isDisposed()); |
64 |
|
65 |
super.tearDown(); |
66 |
} |
67 |
|
68 |
/** |
69 |
* Test save/restore state lifecycle API for WorkbenchAdvisor |
70 |
*/ |
71 |
public void testSaveRestoreAdvisorState() { |
72 |
final String advisorStateData = Long.toString(System.currentTimeMillis()); |
73 |
|
74 |
// launch workbench and save some state data |
75 |
WorkbenchAdvisorObserver wa = new WorkbenchAdvisorObserver(1) { |
76 |
IWorkbenchWindow window = null; |
77 |
|
78 |
public IStatus saveState (IMemento memento) { |
79 |
assertNotNull(memento); |
80 |
memento.putString(ADVISOR_STATE_KEY, advisorStateData); |
81 |
return super.saveState(memento); |
82 |
} |
83 |
|
84 |
public void initialize(IWorkbenchConfigurer c) { |
85 |
super.initialize(c); |
86 |
c.setSaveAndRestore(true); |
87 |
} |
88 |
|
89 |
public void eventLoopIdle(Display d) { |
90 |
workbenchConfig.getWorkbench().restart(); |
91 |
} |
92 |
}; |
93 |
|
94 |
int code = PlatformUI.createAndRunWorkbench(display, wa); |
95 |
assertEquals(PlatformUI.RETURN_RESTART, code); |
96 |
assertFalse(display.isDisposed()); |
97 |
display.dispose(); |
98 |
assertTrue(display.isDisposed()); |
99 |
|
100 |
// restore the workbench and check for state data |
101 |
display = PlatformUI.createDisplay(); |
102 |
WorkbenchAdvisorObserver wa2 = new WorkbenchAdvisorObserver(1) { |
103 |
|
104 |
public IStatus restoreState(IMemento memento) { |
105 |
assertNotNull(memento); |
106 |
String stateData = memento.getString(ADVISOR_STATE_KEY); |
107 |
assertNotNull(stateData); |
108 |
assertTrue(advisorStateData.equals(stateData)); |
109 |
return super.restoreState(memento); |
110 |
} |
111 |
|
112 |
public void initialize(IWorkbenchConfigurer c) { |
113 |
super.initialize(c); |
114 |
c.setSaveAndRestore(true); |
115 |
} |
116 |
}; |
117 |
|
118 |
int code2 = PlatformUI.createAndRunWorkbench(display, wa2); |
119 |
assertEquals(PlatformUI.RETURN_OK, code2); |
120 |
} |
121 |
|
122 |
/** |
123 |
* Test save/restore state lifecycle API for WorkbenchWindowAdvisor |
124 |
*/ |
125 |
public void testSaveRestoreWindowState() { |
126 |
final String advisorStateData = Long.toString(System.currentTimeMillis()); |
127 |
|
128 |
// launch workbench and save some state data |
129 |
WorkbenchAdvisorObserver wa = new WorkbenchAdvisorObserver(1) { |
130 |
IWorkbenchWindow window = null; |
131 |
|
132 |
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { |
133 |
return new WorkbenchWindowAdvisor(configurer) { |
134 |
public IStatus saveState(IMemento memento) { |
135 |
assertNotNull(memento); |
136 |
memento.putString(WINDOW_ADVISOR_STATE_KEY, advisorStateData); |
137 |
return super.saveState(memento); |
138 |
} |
139 |
}; |
140 |
} |
141 |
|
142 |
public void initialize(IWorkbenchConfigurer c) { |
143 |
super.initialize(c); |
144 |
c.setSaveAndRestore(true); |
145 |
} |
146 |
|
147 |
public void eventLoopIdle(Display d) { |
148 |
workbenchConfig.getWorkbench().restart(); |
149 |
} |
150 |
}; |
151 |
|
152 |
int code = PlatformUI.createAndRunWorkbench(display, wa); |
153 |
assertEquals(PlatformUI.RETURN_RESTART, code); |
154 |
assertFalse(display.isDisposed()); |
155 |
display.dispose(); |
156 |
assertTrue(display.isDisposed()); |
157 |
|
158 |
// restore the workbench and check for state data |
159 |
display = PlatformUI.createDisplay(); |
160 |
WorkbenchAdvisorObserver wa2 = new WorkbenchAdvisorObserver(1) { |
161 |
|
162 |
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { |
163 |
return new WorkbenchWindowAdvisor(configurer) { |
164 |
public IStatus restoreState(IMemento memento) { |
165 |
assertNotNull(memento); |
166 |
String stateData = memento.getString(WINDOW_ADVISOR_STATE_KEY); |
167 |
assertNotNull(stateData); |
168 |
assertTrue(advisorStateData.equals(stateData)); |
169 |
return super.restoreState(memento); |
170 |
} |
171 |
}; |
172 |
} |
173 |
|
174 |
public void initialize(IWorkbenchConfigurer c) { |
175 |
super.initialize(c); |
176 |
c.setSaveAndRestore(true); |
177 |
} |
178 |
}; |
179 |
|
180 |
int code2 = PlatformUI.createAndRunWorkbench(display, wa2); |
181 |
assertEquals(PlatformUI.RETURN_OK, code2); |
182 |
} |
183 |
|
184 |
/** |
185 |
* Test save/restore state lifecycle API for WorkbenchWindowAdvisor |
186 |
*/ |
187 |
public void testSaveRestoreActionBarState() { |
188 |
final String advisorStateData = Long.toString(System.currentTimeMillis()); |
189 |
|
190 |
// launch workbench and save some state data |
191 |
WorkbenchAdvisorObserver wa = new WorkbenchAdvisorObserver(1) { |
192 |
IWorkbenchWindow window = null; |
193 |
|
194 |
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { |
195 |
return new WorkbenchWindowAdvisor(configurer) { |
196 |
public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer1) { |
197 |
return new ActionBarAdvisor(configurer1) { |
198 |
public IStatus saveState(IMemento memento) { |
199 |
assertNotNull(memento); |
200 |
memento.putString(ACTIONBAR_ADVISOR_STATE_KEY, advisorStateData); |
201 |
return super.saveState(memento); |
202 |
} |
203 |
}; |
204 |
} |
205 |
}; |
206 |
} |
207 |
|
208 |
public void initialize(IWorkbenchConfigurer c) { |
209 |
super.initialize(c); |
210 |
c.setSaveAndRestore(true); |
211 |
} |
212 |
|
213 |
public void eventLoopIdle(Display d) { |
214 |
workbenchConfig.getWorkbench().restart(); |
215 |
} |
216 |
}; |
217 |
|
218 |
int code = PlatformUI.createAndRunWorkbench(display, wa); |
219 |
assertEquals(PlatformUI.RETURN_RESTART, code); |
220 |
assertFalse(display.isDisposed()); |
221 |
display.dispose(); |
222 |
assertTrue(display.isDisposed()); |
223 |
|
224 |
// restore the workbench and check for state data |
225 |
display = PlatformUI.createDisplay(); |
226 |
WorkbenchAdvisorObserver wa2 = new WorkbenchAdvisorObserver(1) { |
227 |
|
228 |
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { |
229 |
return new WorkbenchWindowAdvisor(configurer) { |
230 |
public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer1) { |
231 |
return new ActionBarAdvisor(configurer1) { |
232 |
public IStatus restoreState(IMemento memento) { |
233 |
assertNotNull(memento); |
234 |
String stateData = memento.getString(ACTIONBAR_ADVISOR_STATE_KEY); |
235 |
assertNotNull(stateData); |
236 |
assertTrue(advisorStateData.equals(stateData)); |
237 |
return super.restoreState(memento); |
238 |
} |
239 |
}; |
240 |
} |
241 |
}; |
242 |
} |
243 |
|
244 |
public void initialize(IWorkbenchConfigurer c) { |
245 |
super.initialize(c); |
246 |
c.setSaveAndRestore(true); |
247 |
} |
248 |
}; |
249 |
|
250 |
int code2 = PlatformUI.createAndRunWorkbench(display, wa2); |
251 |
assertEquals(PlatformUI.RETURN_OK, code2); |
252 |
} |
253 |
|
254 |
/** |
255 |
* Test on-demand save/restore state API |
256 |
*/ |
257 |
public void testOnDemandSaveRestoreState() { |
258 |
|
259 |
// save some window state on demand |
260 |
WorkbenchAdvisorObserver wa = new WorkbenchAdvisorObserver(1) { |
261 |
IWorkbenchWindow window = null; |
262 |
|
263 |
public void initialize(IWorkbenchConfigurer c) { |
264 |
super.initialize(c); |
265 |
c.setSaveAndRestore(true); |
266 |
} |
267 |
|
268 |
public void eventLoopIdle(Display d) { |
269 |
workbenchConfig.getWorkbench().restart(); |
270 |
} |
271 |
|
272 |
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { |
273 |
return new WorkbenchWindowAdvisor(configurer) { |
274 |
public void postWindowOpen() { |
275 |
File stateLocation = getStateFileLocation(); |
276 |
ensureDirectoryExists(stateLocation); |
277 |
String stateFileName = stateLocation.getPath() + File.separator + "testOnDemandSaveRestoreState.xml"; |
278 |
|
279 |
OutputStreamWriter writer = null; |
280 |
try { |
281 |
writer = new OutputStreamWriter(new FileOutputStream(stateFileName),"UTF-8"); |
282 |
|
283 |
} catch (UnsupportedEncodingException e1) { |
284 |
// not possible, UTF-8 is required to be implemented by all JVMs |
285 |
} catch (FileNotFoundException e1) { |
286 |
// creating a new file, won't happen unless the path eclipse |
287 |
// specifies is totally wrong, or its read-only |
288 |
} |
289 |
|
290 |
XMLMemento xmlm = XMLMemento.createWriteRoot("TestState"); |
291 |
saveState(xmlm); |
292 |
|
293 |
try { |
294 |
xmlm.save(writer); |
295 |
writer.close(); |
296 |
} catch (IOException e) { |
297 |
e.printStackTrace(); |
298 |
} |
299 |
} |
300 |
}; |
301 |
} |
302 |
}; |
303 |
|
304 |
int code = PlatformUI.createAndRunWorkbench(display, wa); |
305 |
assertEquals(PlatformUI.RETURN_RESTART, code); |
306 |
assertFalse(display.isDisposed()); |
307 |
display.dispose(); |
308 |
assertTrue(display.isDisposed()); |
309 |
|
310 |
// restore the workbench and restore a window |
311 |
// with state data on demand |
312 |
display = PlatformUI.createDisplay(); |
313 |
WorkbenchAdvisorObserver wa2 = new WorkbenchAdvisorObserver(1) { |
314 |
|
315 |
public void initialize(IWorkbenchConfigurer c) { |
316 |
super.initialize(c); |
317 |
c.setSaveAndRestore(true); |
318 |
} |
319 |
|
320 |
public boolean openWindows() { |
321 |
File stateLocation = getStateFileLocation(); |
322 |
String stateFileName = "testOnDemandSaveRestoreState.xml"; |
323 |
File stateFile = new File(stateLocation.getPath() + File.separator + stateFileName); |
324 |
assertTrue(stateFile.exists()); |
325 |
|
326 |
IMemento memento = null; |
327 |
try { |
328 |
memento = XMLMemento.createReadRoot( new InputStreamReader ( |
329 |
new FileInputStream(stateFile),"UTF-8")); |
330 |
} catch (WorkbenchException e) { |
331 |
e.printStackTrace(); |
332 |
} catch (FileNotFoundException e) { |
333 |
// won't happen because we already checked it exists |
334 |
} catch (UnsupportedEncodingException e) { |
335 |
// not possible - UTF8 is required |
336 |
} |
337 |
|
338 |
assertNotNull(memento); |
339 |
IWorkbenchWindowConfigurer window = null; |
340 |
try { |
341 |
window = getWorkbenchConfigurer().restoreWorkbenchWindow(memento); |
342 |
} catch (WorkbenchException e) { |
343 |
e.printStackTrace(); |
344 |
} |
345 |
assertNotNull(window); |
346 |
return true; |
347 |
} |
348 |
|
349 |
public void postWindowRestore(IWorkbenchWindowConfigurer configurer) throws WorkbenchException { |
350 |
// TODO Auto-generated method stub |
351 |
super.postWindowRestore(configurer); |
352 |
} |
353 |
}; |
354 |
|
355 |
int code2 = PlatformUI.createAndRunWorkbench(display, wa2); |
356 |
assertEquals(PlatformUI.RETURN_OK, code2); |
357 |
} |
358 |
|
359 |
private File getStateFileLocation() { |
360 |
IPath path = UIPlugin.getDefault().getStateLocation(); |
361 |
StringBuffer fileName = new StringBuffer(); |
362 |
fileName.append(File.separator); |
363 |
fileName.append("TestWorkbenchState"); |
364 |
fileName.append(File.separator); |
365 |
|
366 |
File stateLocation = path.append(fileName.toString()).toFile(); |
367 |
ensureDirectoryExists(stateLocation); |
368 |
|
369 |
return stateLocation; |
370 |
} |
371 |
|
372 |
private void ensureDirectoryExists(File directory) { |
373 |
directory.mkdirs(); |
374 |
} |
375 |
|
376 |
} |