Search in sources :

Example 6 with ContextFunction

use of org.eclipse.e4.core.contexts.ContextFunction in project eclipse.platform.runtime by eclipse.

the class ContextFunctionDynamicsTest method testChangeICF.

/**
 * Changing context function should update injected values
 */
@Test
public void testChangeICF() {
    IEclipseContext context1 = EclipseContextFactory.create("context1");
    IEclipseContext context2 = context1.createChild("context2");
    context1.set(SELECTION, new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context, String contextKey) {
            return "func1";
        }
    });
    InjectTarget target = new InjectTarget();
    ContextInjectionFactory.inject(target, context2);
    assertEquals("func1", target.input);
    context1.set(SELECTION, new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context, String contextKey) {
            return "func2";
        }
    });
    assertEquals("func2", target.input);
}
Also used : ContextFunction(org.eclipse.e4.core.contexts.ContextFunction) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 7 with ContextFunction

use of org.eclipse.e4.core.contexts.ContextFunction in project eclipse.platform.runtime by eclipse.

the class ContextDynamicTest method testReplaceFunctionWithStaticValue.

@Test
public void testReplaceFunctionWithStaticValue() {
    IEclipseContext parent = EclipseContextFactory.create();
    IEclipseContext context = parent.createChild();
    assertNull(context.getLocal("bar"));
    context.set("bar", "baz1");
    context.set("bar", new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context, String contextKey) {
            return "baz1";
        }
    });
    parent.set("bar", "baz2");
    assertEquals("baz1", context.get("bar"));
    context.set("bar", "baz3");
    assertEquals("baz3", context.get("bar"));
}
Also used : ContextFunction(org.eclipse.e4.core.contexts.ContextFunction) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 8 with ContextFunction

use of org.eclipse.e4.core.contexts.ContextFunction in project eclipse.platform.runtime by eclipse.

the class EclipseContextTest method testRunAndTrack.

@Test
public void testRunAndTrack() {
    final Object[] value = new Object[1];
    context.runAndTrack(new RunAndTrack() {

        @Override
        public boolean changed(IEclipseContext context) {
            runCounter++;
            value[0] = context.get("foo");
            return true;
        }
    });
    assertEquals(1, runCounter);
    assertEquals(null, value[0]);
    context.set("foo", "bar");
    assertEquals(2, runCounter);
    assertEquals("bar", value[0]);
    context.remove("foo");
    assertEquals(3, runCounter);
    assertEquals(null, value[0]);
    context.set("foo", new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context, String contextKey) {
            return context.get("bar");
        }
    });
    assertEquals(4, runCounter);
    assertEquals(null, value[0]);
    context.set("bar", "baz");
    assertEquals(5, runCounter);
    assertEquals("baz", value[0]);
    context.set("bar", "baf");
    assertEquals(6, runCounter);
    assertEquals("baf", value[0]);
    context.remove("bar");
    assertEquals(7, runCounter);
    assertEquals(null, value[0]);
    parentContext.set("bar", "bam");
    assertEquals(8, runCounter);
    assertEquals("bam", value[0]);
}
Also used : ContextFunction(org.eclipse.e4.core.contexts.ContextFunction) RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 9 with ContextFunction

use of org.eclipse.e4.core.contexts.ContextFunction in project eclipse.platform.runtime by eclipse.

the class ReparentingTest method testBug468048_injection.

@Test
public void testBug468048_injection() {
    IEclipseContext p1 = EclipseContextFactory.create("parent1");
    p1.set("sample", new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context) {
            return Integer.valueOf(1);
        }
    });
    IEclipseContext p2 = EclipseContextFactory.create("parent2");
    p2.set("sample", new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context) {
            return Integer.valueOf(2);
        }
    });
    final IEclipseContext intermed = p1.createChild("intermed");
    final IEclipseContext leaf = intermed.createChild("leaf");
    Bug468048 b = ContextInjectionFactory.make(Bug468048.class, leaf);
    assertEquals(Integer.valueOf(1), b.sample);
    intermed.setParent(p2);
    assertEquals(Integer.valueOf(2), b.sample);
}
Also used : ContextFunction(org.eclipse.e4.core.contexts.ContextFunction) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 10 with ContextFunction

use of org.eclipse.e4.core.contexts.ContextFunction in project eclipse.platform.runtime by eclipse.

the class ReparentingTest method testBug468048_contextFunction.

@Test
public void testBug468048_contextFunction() {
    IEclipseContext p1 = EclipseContextFactory.create("parent1");
    p1.set("sample", new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context) {
            return Integer.valueOf(1);
        }
    });
    IEclipseContext p2 = EclipseContextFactory.create("parent2");
    p2.set("sample", new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context) {
            return Integer.valueOf(2);
        }
    });
    final IEclipseContext intermed = p1.createChild("intermed");
    final IEclipseContext leaf = intermed.createChild("leaf");
    assertEquals(Integer.valueOf(1), leaf.get("sample"));
    intermed.setParent(p2);
    assertEquals(Integer.valueOf(2), leaf.get("sample"));
}
Also used : ContextFunction(org.eclipse.e4.core.contexts.ContextFunction) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Aggregations

ContextFunction (org.eclipse.e4.core.contexts.ContextFunction)17 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)17 Test (org.junit.Test)14 PerformanceTestRunner (org.eclipse.core.tests.harness.PerformanceTestRunner)3 RunAndTrack (org.eclipse.e4.core.contexts.RunAndTrack)2