|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2010 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.lang.reflect.InvocationTargetException; |
| 14 |
|
| 15 |
import javax.inject.Inject; |
| 16 |
import javax.inject.Named; |
| 17 |
|
| 18 |
import junit.framework.TestCase; |
| 19 |
|
| 20 |
import org.eclipse.e4.core.contexts.ContextChangeEvent; |
| 21 |
import org.eclipse.e4.core.contexts.ContextFunction; |
| 22 |
import org.eclipse.e4.core.contexts.ContextInjectionFactory; |
| 23 |
import org.eclipse.e4.core.contexts.EclipseContextFactory; |
| 24 |
import org.eclipse.e4.core.contexts.IContextConstants; |
| 25 |
import org.eclipse.e4.core.contexts.IEclipseContext; |
| 26 |
import org.eclipse.e4.core.contexts.IRunAndTrack; |
| 27 |
import org.eclipse.e4.core.di.annotations.Optional; |
| 28 |
import org.eclipse.e4.ui.services.IServiceConstants; |
| 29 |
|
| 30 |
public class Bug308220Test extends TestCase { |
| 31 |
|
| 32 |
static class WindowService { |
| 33 |
|
| 34 |
@Inject |
| 35 |
@Named(IServiceConstants.ACTIVE_PART) |
| 36 |
@Optional |
| 37 |
Object activePart; |
| 38 |
} |
| 39 |
|
| 40 |
public void testBug308220() throws Exception { |
| 41 |
IEclipseContext app = EclipseContextFactory.create(); |
| 42 |
app.set(WindowService.class.getName(), new ContextFunction() { |
| 43 |
@Override |
| 44 |
public Object compute(IEclipseContext context, Object[] arguments) { |
| 45 |
try { |
| 46 |
return ContextInjectionFactory.make(WindowService.class, |
| 47 |
context); |
| 48 |
} catch (InvocationTargetException e) { |
| 49 |
throw new RuntimeException(e); |
| 50 |
} catch (InstantiationException e) { |
| 51 |
throw new RuntimeException(e); |
| 52 |
} |
| 53 |
} |
| 54 |
}); |
| 55 |
// lookup function that goes down the context's active child chain |
| 56 |
app.set(IServiceConstants.ACTIVE_PART, new ContextFunction() { |
| 57 |
@Override |
| 58 |
public Object compute(IEclipseContext context, Object[] arguments) { |
| 59 |
IEclipseContext childContext = (IEclipseContext) context |
| 60 |
.getLocal(IContextConstants.ACTIVE_CHILD); |
| 61 |
if (childContext == null) { |
| 62 |
return null; |
| 63 |
} |
| 64 |
|
| 65 |
while (childContext != null) { |
| 66 |
context = childContext; |
| 67 |
childContext = (IEclipseContext) context |
| 68 |
.getLocal(IContextConstants.ACTIVE_CHILD); |
| 69 |
} |
| 70 |
return context.getLocal(Object.class.getName()); |
| 71 |
} |
| 72 |
}); |
| 73 |
|
| 74 |
app.runAndTrack(new IRunAndTrack() { |
| 75 |
public boolean notify(ContextChangeEvent event) { |
| 76 |
// remove this line to pass the test |
| 77 |
event.getContext().get(IServiceConstants.ACTIVE_PART); |
| 78 |
return true; |
| 79 |
} |
| 80 |
}, null); |
| 81 |
|
| 82 |
// create two contexts |
| 83 |
IEclipseContext windowA = EclipseContextFactory.create(app, null); |
| 84 |
IEclipseContext windowB = EclipseContextFactory.create(app, null); |
| 85 |
|
| 86 |
Object o1 = new Object(); |
| 87 |
Object o2 = new Object(); |
| 88 |
|
| 89 |
IEclipseContext part = EclipseContextFactory.create(windowA, null); |
| 90 |
// set the active part as some object |
| 91 |
part.set(Object.class.getName(), o1); |
| 92 |
// construct the active chain |
| 93 |
windowA.set(IContextConstants.ACTIVE_CHILD, part); |
| 94 |
app.set(IContextConstants.ACTIVE_CHILD, windowA); |
| 95 |
|
| 96 |
WindowService windowServiceA = (WindowService) windowA |
| 97 |
.get(WindowService.class.getName()); |
| 98 |
WindowService windowServiceB = (WindowService) windowB |
| 99 |
.get(WindowService.class.getName()); |
| 100 |
|
| 101 |
// windowA should have an active part, it was set earlier |
| 102 |
assertEquals(o1, windowServiceA.activePart); |
| 103 |
// windowB has no child contexts, this should be null |
| 104 |
assertNull(windowServiceB.activePart); |
| 105 |
|
| 106 |
// change the active part |
| 107 |
part.set(Object.class.getName(), o2); |
| 108 |
|
| 109 |
// windowA's active part should have changed |
| 110 |
assertEquals(o2, windowServiceA.activePart); |
| 111 |
// windowB should still have no active part |
| 112 |
assertNull(windowServiceB.activePart); |
| 113 |
} |
| 114 |
} |