Search in sources :

Example 11 with RunAndTrack

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

the class EclipseContextTest method testRunAndTrackMultipleValues.

@Test
public void testRunAndTrackMultipleValues() {
    IEclipseContext parent = EclipseContextFactory.create("ParentContext");
    final IEclipseContext child = parent.createChild("ChildContext");
    parent.set("parentValue", "x");
    child.set("childValue", "x");
    RunAndTrack runnable = new RunAndTrack() {

        @Override
        public boolean changed(IEclipseContext context) {
            runCounter++;
            if (runCounter < 2) {
                child.get("childValue");
                return true;
            }
            if (runCounter < 3) {
                child.get("parentValue");
                return true;
            }
            return false;
        }
    };
    child.runAndTrack(runnable);
    assertEquals(1, runCounter);
    child.set("childValue", "z");
    assertEquals(2, runCounter);
    parent.set("parentValue", "z");
    assertEquals(3, runCounter);
}
Also used : RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 12 with RunAndTrack

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

the class ActivationTest method testGetActiveRATNumberOfCalls.

@Test
public void testGetActiveRATNumberOfCalls() {
    IEclipseContext root = EclipseContextFactory.create("root");
    IEclipseContext child1 = root.createChild("child1");
    IEclipseContext child11 = child1.createChild("child11");
    IEclipseContext child12 = child1.createChild("child12");
    IEclipseContext child2 = root.createChild("child2");
    IEclipseContext child21 = child2.createChild("child21");
    IEclipseContext child22 = child2.createChild("child22");
    child11.set("var", "1");
    child12.set("var", "1");
    child1.set("var", "3");
    child21.set("var", "4");
    child22.set("var", "4");
    child2.set("var", "6");
    root.set("var", "7");
    final String[] result = new String[1];
    final int[] called = new int[1];
    called[0] = 0;
    child1.runAndTrack(new RunAndTrack() {

        @Override
        public boolean changed(IEclipseContext context) {
            result[0] = (String) context.getActive("var");
            called[0]++;
            return true;
        }
    });
    // nothing is active - we get value from the node
    assertEquals("3", result[0]);
    assertEquals(1, called[0]);
    child11.activateBranch();
    assertEquals("1", result[0]);
    assertEquals(2, called[0]);
    child12.activateBranch();
    assertEquals("1", result[0]);
    assertEquals(3, called[0]);
    child22.activateBranch();
    assertEquals("1", result[0]);
    assertEquals(3, called[0]);
    child21.activateBranch();
    assertEquals("1", result[0]);
    assertEquals(3, called[0]);
}
Also used : RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 13 with RunAndTrack

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

the class ReparentingTest method testRunAndTrackParentBecomeNull.

/**
 * Tests a child switching from a non-null parent to a null parent.
 */
@Test
public void testRunAndTrackParentBecomeNull() {
    final String[] value = new String[1];
    IEclipseContext parent = EclipseContextFactory.create();
    final IEclipseContext child = parent.createChild();
    parent.set("x", "oldParent");
    child.runAndTrack(new RunAndTrack() {

        @Override
        public boolean changed(IEclipseContext context) {
            value[0] = (String) child.get("x");
            return true;
        }
    });
    assertEquals("oldParent", value[0]);
    child.setParent(null);
    assertNull(value[0]);
}
Also used : RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 14 with RunAndTrack

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

the class ReparentingTest method testRunAndTrackNullBecomesParent.

/**
 * Tests a child switching from a null parent to a non-null parent.
 */
@Test
public void testRunAndTrackNullBecomesParent() {
    final String[] value = new String[1];
    final IEclipseContext child = EclipseContextFactory.create();
    child.runAndTrack(new RunAndTrack() {

        @Override
        public boolean changed(IEclipseContext context) {
            value[0] = (String) child.get("x");
            return true;
        }
    });
    assertEquals(null, value[0]);
    IEclipseContext parent = EclipseContextFactory.create();
    parent.set("x", "newParent");
    child.setParent(parent);
    assertEquals("newParent", value[0]);
}
Also used : RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 15 with RunAndTrack

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

the class EclipseContextTest method testRegisterRunAndTrackTwice.

/**
 * Tests registering a single run and track instance multiple times with the same context.
 */
@Test
public void testRegisterRunAndTrackTwice() {
    final Object[] value = new Object[1];
    RunAndTrack runnable = new RunAndTrack() {

        @Override
        public boolean changed(IEclipseContext context) {
            runCounter++;
            value[0] = context.get("foo");
            return true;
        }
    };
    context.runAndTrack(runnable);
    assertEquals(1, runCounter);
    context.runAndTrack(runnable);
    assertEquals(2, runCounter);
    assertEquals(null, value[0]);
    context.set("foo", "bar");
    assertEquals(3, runCounter);
    assertEquals("bar", value[0]);
    context.remove("foo");
    assertEquals(4, runCounter);
}
Also used : RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Aggregations

RunAndTrack (org.eclipse.e4.core.contexts.RunAndTrack)18 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)17 Test (org.junit.Test)15 ContextFunction (org.eclipse.e4.core.contexts.ContextFunction)2 PerformanceTestRunner (org.eclipse.core.tests.harness.PerformanceTestRunner)1 Active (org.eclipse.e4.core.contexts.Active)1 CanExecute (org.eclipse.e4.core.di.annotations.CanExecute)1 BundleContext (org.osgi.framework.BundleContext)1