|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009 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.e4.ui.tests.application; |
| 12 |
|
| 13 |
import java.util.Collection; |
| 14 |
|
| 15 |
import junit.framework.TestCase; |
| 16 |
|
| 17 |
import org.eclipse.e4.core.services.IContributionFactory; |
| 18 |
import org.eclipse.e4.core.services.IDisposable; |
| 19 |
import org.eclipse.e4.core.services.context.IEclipseContext; |
| 20 |
import org.eclipse.e4.ui.model.application.MApplication; |
| 21 |
import org.eclipse.e4.ui.model.application.MApplicationFactory; |
| 22 |
import org.eclipse.e4.ui.model.application.MElementContainer; |
| 23 |
import org.eclipse.e4.ui.model.application.MPart; |
| 24 |
import org.eclipse.e4.ui.model.application.MPartStack; |
| 25 |
import org.eclipse.e4.ui.model.application.MWindow; |
| 26 |
import org.eclipse.e4.ui.workbench.swt.internal.E4Application; |
| 27 |
import org.eclipse.e4.workbench.modeling.EPartService; |
| 28 |
import org.eclipse.e4.workbench.ui.IPresentationEngine; |
| 29 |
|
| 30 |
public class EPartServiceTest extends TestCase { |
| 31 |
|
| 32 |
private IEclipseContext applicationContext; |
| 33 |
|
| 34 |
private IPresentationEngine engine; |
| 35 |
|
| 36 |
@Override |
| 37 |
protected void setUp() throws Exception { |
| 38 |
applicationContext = E4Application.createDefaultContext(); |
| 39 |
|
| 40 |
IContributionFactory contributionFactory = (IContributionFactory) applicationContext |
| 41 |
.get(IContributionFactory.class.getName()); |
| 42 |
Object newEngine = contributionFactory.create(getEngineURI(), |
| 43 |
applicationContext); |
| 44 |
assertTrue(newEngine instanceof IPresentationEngine); |
| 45 |
applicationContext.set(IPresentationEngine.class.getName(), newEngine); |
| 46 |
|
| 47 |
engine = (IPresentationEngine) newEngine; |
| 48 |
|
| 49 |
super.setUp(); |
| 50 |
} |
| 51 |
|
| 52 |
protected String getEngineURI() { |
| 53 |
return "platform:/plugin/org.eclipse.e4.ui.tests/org.eclipse.e4.ui.tests.application.HeadlessContextPresentationEngine"; //$NON-NLS-1$ |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
protected void tearDown() throws Exception { |
| 58 |
super.tearDown(); |
| 59 |
if (applicationContext instanceof IDisposable) { |
| 60 |
((IDisposable) applicationContext).dispose(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
public void testFindPart_PartInWindow() { |
| 65 |
MApplication application = createApplication("partId"); |
| 66 |
|
| 67 |
MWindow window = application.getChildren().get(0); |
| 68 |
engine.createGui(window); |
| 69 |
|
| 70 |
EPartService partService = (EPartService) window.getContext().get( |
| 71 |
EPartService.class.getName()); |
| 72 |
MPart part = partService.findPart("partId"); |
| 73 |
assertNotNull(part); |
| 74 |
|
| 75 |
MPartStack partStack = (MPartStack) window.getChildren().get(0); |
| 76 |
assertEquals(partStack.getChildren().get(0), part); |
| 77 |
|
| 78 |
part = partService.findPart("invalidPartId"); |
| 79 |
assertNull(part); |
| 80 |
} |
| 81 |
|
| 82 |
public void testFindPart_PartNotInWindow() { |
| 83 |
MApplication application = createApplication("partId"); |
| 84 |
|
| 85 |
MWindow window = application.getChildren().get(0); |
| 86 |
engine.createGui(window); |
| 87 |
|
| 88 |
EPartService partService = (EPartService) window.getContext().get( |
| 89 |
EPartService.class.getName()); |
| 90 |
MPart part = partService.findPart("invalidPartId"); |
| 91 |
assertNull(part); |
| 92 |
} |
| 93 |
|
| 94 |
public void testFindPart_PartInAnotherWindow() { |
| 95 |
MApplication application = createApplication( |
| 96 |
new String[] { "partInWindow1" }, |
| 97 |
new String[] { "partInWindow2" }); |
| 98 |
|
| 99 |
MWindow window1 = application.getChildren().get(0); |
| 100 |
MWindow window2 = application.getChildren().get(1); |
| 101 |
|
| 102 |
engine.createGui(window1); |
| 103 |
engine.createGui(window2); |
| 104 |
|
| 105 |
EPartService partService = (EPartService) window1.getContext().get( |
| 106 |
EPartService.class.getName()); |
| 107 |
MPart part = partService.findPart("partInWindow2"); |
| 108 |
assertNull(part); |
| 109 |
part = partService.findPart("partInWindow1"); |
| 110 |
assertNotNull(part); |
| 111 |
|
| 112 |
MPartStack partStack = (MPartStack) window1.getChildren().get(0); |
| 113 |
assertEquals(partStack.getChildren().get(0), part); |
| 114 |
|
| 115 |
partService = (EPartService) window2.getContext().get( |
| 116 |
EPartService.class.getName()); |
| 117 |
part = partService.findPart("partInWindow1"); |
| 118 |
assertNull(part); |
| 119 |
part = partService.findPart("partInWindow2"); |
| 120 |
assertNotNull(part); |
| 121 |
|
| 122 |
partStack = (MPartStack) window2.getChildren().get(0); |
| 123 |
assertEquals(partStack.getChildren().get(0), part); |
| 124 |
} |
| 125 |
|
| 126 |
public void testBringToTop_PartOnTop() { |
| 127 |
MApplication application = createApplication("partFront", "partBack"); |
| 128 |
|
| 129 |
MWindow window = application.getChildren().get(0); |
| 130 |
MPartStack partStack = (MPartStack) window.getChildren().get(0); |
| 131 |
MPart partFront = partStack.getChildren().get(0); |
| 132 |
partStack.setActiveChild(partFront); |
| 133 |
|
| 134 |
engine.createGui(window); |
| 135 |
|
| 136 |
EPartService partService = (EPartService) window.getContext().get( |
| 137 |
EPartService.class.getName()); |
| 138 |
|
| 139 |
partService.bringToTop(partFront); |
| 140 |
assertEquals(partStack.getActiveChild(), partFront); |
| 141 |
} |
| 142 |
|
| 143 |
public void testBringToTop_PartNotOnTop() { |
| 144 |
MApplication application = createApplication("partFront", "partBack"); |
| 145 |
|
| 146 |
MWindow window = application.getChildren().get(0); |
| 147 |
MPartStack partStack = (MPartStack) window.getChildren().get(0); |
| 148 |
MPart partFront = partStack.getChildren().get(0); |
| 149 |
MPart partBack = partStack.getChildren().get(1); |
| 150 |
partStack.setActiveChild(partFront); |
| 151 |
|
| 152 |
engine.createGui(window); |
| 153 |
|
| 154 |
EPartService partService = (EPartService) window.getContext().get( |
| 155 |
EPartService.class.getName()); |
| 156 |
|
| 157 |
partService.bringToTop(partBack); |
| 158 |
assertEquals(partStack.getActiveChild(), partBack); |
| 159 |
} |
| 160 |
|
| 161 |
public void testBringToTop_PartInAnotherWindow() { |
| 162 |
MApplication application = createApplication(new String[] { |
| 163 |
"partFrontA", "partBackA" }, new String[] { "partFrontB", |
| 164 |
"partBackB" }); |
| 165 |
|
| 166 |
MWindow windowA = application.getChildren().get(0); |
| 167 |
MPartStack partStackA = (MPartStack) windowA.getChildren().get(0); |
| 168 |
MPart partFrontA = partStackA.getChildren().get(0); |
| 169 |
MPart partBackA = partStackA.getChildren().get(1); |
| 170 |
partStackA.setActiveChild(partFrontA); |
| 171 |
|
| 172 |
MWindow windowB = application.getChildren().get(1); |
| 173 |
MPartStack partStackB = (MPartStack) windowB.getChildren().get(0); |
| 174 |
MPart partFrontB = partStackB.getChildren().get(0); |
| 175 |
MPart partBackB = partStackB.getChildren().get(1); |
| 176 |
partStackB.setActiveChild(partFrontB); |
| 177 |
|
| 178 |
engine.createGui(windowA); |
| 179 |
engine.createGui(windowB); |
| 180 |
|
| 181 |
EPartService partServiceA = (EPartService) windowA.getContext().get( |
| 182 |
EPartService.class.getName()); |
| 183 |
EPartService partServiceB = (EPartService) windowB.getContext().get( |
| 184 |
EPartService.class.getName()); |
| 185 |
|
| 186 |
partServiceA.bringToTop(partBackB); |
| 187 |
assertEquals(partStackA.getActiveChild(), partFrontA); |
| 188 |
assertEquals(partStackB.getActiveChild(), partFrontB); |
| 189 |
|
| 190 |
partServiceB.bringToTop(partBackA); |
| 191 |
assertEquals(partStackA.getActiveChild(), partFrontA); |
| 192 |
assertEquals(partStackB.getActiveChild(), partFrontB); |
| 193 |
|
| 194 |
partServiceA.bringToTop(partBackA); |
| 195 |
assertEquals(partStackA.getActiveChild(), partBackA); |
| 196 |
assertEquals(partStackB.getActiveChild(), partFrontB); |
| 197 |
|
| 198 |
partServiceB.bringToTop(partBackB); |
| 199 |
assertEquals(partStackA.getActiveChild(), partBackA); |
| 200 |
assertEquals(partStackB.getActiveChild(), partBackB); |
| 201 |
} |
| 202 |
|
| 203 |
public void testGetParts_Empty() { |
| 204 |
MApplication application = createApplication(1, new String[1][0]); |
| 205 |
MWindow window = application.getChildren().get(0); |
| 206 |
|
| 207 |
engine.createGui(window); |
| 208 |
|
| 209 |
EPartService partService = (EPartService) window.getContext().get( |
| 210 |
EPartService.class.getName()); |
| 211 |
Collection<MPart> parts = partService.getParts(); |
| 212 |
assertNotNull(parts); |
| 213 |
assertEquals(0, parts.size()); |
| 214 |
} |
| 215 |
|
| 216 |
public void testGetParts_OneWindow() { |
| 217 |
MApplication application = createApplication("partId", "partId2"); |
| 218 |
MWindow window = application.getChildren().get(0); |
| 219 |
MPartStack partStack = (MPartStack) window.getChildren().get(0); |
| 220 |
|
| 221 |
engine.createGui(window); |
| 222 |
|
| 223 |
EPartService partService = (EPartService) window.getContext().get( |
| 224 |
EPartService.class.getName()); |
| 225 |
Collection<MPart> parts = partService.getParts(); |
| 226 |
assertNotNull(parts); |
| 227 |
assertEquals(2, parts.size()); |
| 228 |
assertTrue(parts.containsAll(partStack.getChildren())); |
| 229 |
} |
| 230 |
|
| 231 |
public void testGetParts_TwoWindows() { |
| 232 |
MApplication application = createApplication(new String[] { "partId", |
| 233 |
"partId2" }, new String[] { "partIA", "partIdB", "partIdC" }); |
| 234 |
|
| 235 |
MWindow windowA = application.getChildren().get(0); |
| 236 |
MWindow windowB = application.getChildren().get(1); |
| 237 |
|
| 238 |
engine.createGui(windowA); |
| 239 |
engine.createGui(windowB); |
| 240 |
|
| 241 |
EPartService partServiceA = (EPartService) windowA.getContext().get( |
| 242 |
EPartService.class.getName()); |
| 243 |
EPartService partServiceB = (EPartService) windowB.getContext().get( |
| 244 |
EPartService.class.getName()); |
| 245 |
|
| 246 |
MPartStack partStackA = (MPartStack) windowA.getChildren().get(0); |
| 247 |
MPartStack partStackB = (MPartStack) windowB.getChildren().get(0); |
| 248 |
|
| 249 |
Collection<MPart> partsA = partServiceA.getParts(); |
| 250 |
Collection<MPart> partsB = partServiceB.getParts(); |
| 251 |
|
| 252 |
assertNotNull(partsA); |
| 253 |
assertEquals(2, partsA.size()); |
| 254 |
assertTrue(partsA.containsAll(partStackA.getChildren())); |
| 255 |
|
| 256 |
assertNotNull(partsB); |
| 257 |
assertEquals(3, partsB.size()); |
| 258 |
assertTrue(partsB.containsAll(partStackB.getChildren())); |
| 259 |
|
| 260 |
for (MPart partA : partsA) { |
| 261 |
assertFalse(partsB.contains(partA)); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
public void testIsVisible_ViewVisible() { |
| 266 |
MApplication application = createApplication("partId"); |
| 267 |
|
| 268 |
MWindow window = application.getChildren().get(0); |
| 269 |
MPartStack partStack = (MPartStack) window.getChildren().get(0); |
| 270 |
MPart part = partStack.getChildren().get(0); |
| 271 |
partStack.setActiveChild(part); |
| 272 |
|
| 273 |
engine.createGui(window); |
| 274 |
|
| 275 |
EPartService partService = (EPartService) window.getContext().get( |
| 276 |
EPartService.class.getName()); |
| 277 |
assertTrue(partService.isPartVisible(part)); |
| 278 |
} |
| 279 |
|
| 280 |
public void testIsVisible_ViewNotVisible() { |
| 281 |
MApplication application = createApplication("partId", "partId2"); |
| 282 |
|
| 283 |
MWindow window = application.getChildren().get(0); |
| 284 |
MPartStack partStack = (MPartStack) window.getChildren().get(0); |
| 285 |
partStack.setActiveChild(partStack.getChildren().get(0)); |
| 286 |
|
| 287 |
engine.createGui(window); |
| 288 |
|
| 289 |
MPart part = partStack.getChildren().get(1); |
| 290 |
|
| 291 |
EPartService partService = (EPartService) window.getContext().get( |
| 292 |
EPartService.class.getName()); |
| 293 |
assertFalse(partService.isPartVisible(part)); |
| 294 |
} |
| 295 |
|
| 296 |
public void testIsVisible_ViewInAnotherWindow() { |
| 297 |
MApplication application = createApplication(new String[] { |
| 298 |
"partFrontA", "partBackA" }, new String[] { "partFrontB", |
| 299 |
"partBackB" }); |
| 300 |
|
| 301 |
MWindow windowA = application.getChildren().get(0); |
| 302 |
MPartStack partStackA = (MPartStack) windowA.getChildren().get(0); |
| 303 |
MPart partFrontA = partStackA.getChildren().get(0); |
| 304 |
MPart partBackA = partStackA.getChildren().get(1); |
| 305 |
partStackA.setActiveChild(partFrontA); |
| 306 |
|
| 307 |
MWindow windowB = application.getChildren().get(1); |
| 308 |
MPartStack partStackB = (MPartStack) windowB.getChildren().get(0); |
| 309 |
MPart partFrontB = partStackB.getChildren().get(0); |
| 310 |
MPart partBackB = partStackB.getChildren().get(1); |
| 311 |
partStackB.setActiveChild(partFrontB); |
| 312 |
|
| 313 |
engine.createGui(windowA); |
| 314 |
engine.createGui(windowB); |
| 315 |
|
| 316 |
EPartService partServiceA = (EPartService) windowA.getContext().get( |
| 317 |
EPartService.class.getName()); |
| 318 |
EPartService partServiceB = (EPartService) windowB.getContext().get( |
| 319 |
EPartService.class.getName()); |
| 320 |
|
| 321 |
assertTrue(partServiceA.isPartVisible(partFrontA)); |
| 322 |
assertFalse(partServiceA.isPartVisible(partBackA)); |
| 323 |
assertFalse(partServiceA.isPartVisible(partFrontB)); |
| 324 |
assertFalse(partServiceA.isPartVisible(partBackB)); |
| 325 |
|
| 326 |
assertFalse(partServiceB.isPartVisible(partFrontA)); |
| 327 |
assertFalse(partServiceB.isPartVisible(partBackA)); |
| 328 |
assertTrue(partServiceB.isPartVisible(partFrontB)); |
| 329 |
assertFalse(partServiceB.isPartVisible(partBackB)); |
| 330 |
} |
| 331 |
|
| 332 |
private MApplication createApplication(String partId) { |
| 333 |
return createApplication(new String[] { partId }); |
| 334 |
} |
| 335 |
|
| 336 |
private MApplication createApplication(String... partIds) { |
| 337 |
return createApplication(new String[][] { partIds }); |
| 338 |
} |
| 339 |
|
| 340 |
private MApplication createApplication(String[]... partIds) { |
| 341 |
return createApplication(partIds.length, partIds); |
| 342 |
} |
| 343 |
|
| 344 |
private MApplication createApplication(int windows, String[][] partIds) { |
| 345 |
MApplication application = MApplicationFactory.eINSTANCE |
| 346 |
.createApplication(); |
| 347 |
|
| 348 |
for (int i = 0; i < windows; i++) { |
| 349 |
MWindow window = MApplicationFactory.eINSTANCE.createWindow(); |
| 350 |
application.getChildren().add(window); |
| 351 |
|
| 352 |
MPartStack partStack = MApplicationFactory.eINSTANCE |
| 353 |
.createPartStack(); |
| 354 |
window.getChildren().add(partStack); |
| 355 |
|
| 356 |
for (int j = 0; j < partIds[i].length; j++) { |
| 357 |
MPart part = MApplicationFactory.eINSTANCE.createPart(); |
| 358 |
part.setId(partIds[i][j]); |
| 359 |
partStack.getChildren().add(part); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
applicationContext.set(MApplication.class.getName(), application); |
| 364 |
applicationContext.set(MElementContainer.class.getName(), application); |
| 365 |
application.setContext(applicationContext); |
| 366 |
|
| 367 |
return application; |
| 368 |
} |
| 369 |
} |