Search in sources :

Example 1 with RunAndTrack

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

the class ContextPerformanceTest method testSetValueRunAndTrack.

/**
 * Tests setting a value in a context that a RAT is listening to. This test mimics what occurs
 * when handlers change in e4. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=305038
 */
public void testSetValueRunAndTrack() {
    context.set("somefunction", new ContextFunction() {

        @Override
        public Object compute(IEclipseContext context, String contextKey) {
            // make sure this function has a large number of dependencies
            for (int i = 0; i < 1000; i++) {
                context.get("NonExistentValue-" + i);
            }
            return context.get("something");
        }
    });
    context.runAndTrack(new RunAndTrack() {

        @Override
        public boolean changed(IEclipseContext context) {
            context.get("somefunction");
            return true;
        }
    });
    new PerformanceTestRunner() {

        int i = 0;

        @Override
        protected void test() {
            context.set("something", "value-" + i++);
        }
    }.run(this, 10, 400);
}
Also used : ContextFunction(org.eclipse.e4.core.contexts.ContextFunction) RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack) PerformanceTestRunner(org.eclipse.core.tests.harness.PerformanceTestRunner) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext)

Example 2 with RunAndTrack

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

the class ContextExample method price.

public void price() {
    final IEclipseContext context = EclipseContextFactory.create();
    context.set("price", 19.99);
    context.set("tax", 0.05);
    context.runAndTrack(new RunAndTrack() {

        @Override
        public boolean changed(IEclipseContext context) {
            total = (Double) context.get("price") * (1.0 + (Double) context.get("tax"));
            return true;
        }

        @Override
        public String toString() {
            return "calculator";
        }
    });
    print(total);
    context.set("tax", 0.07);
    print(total);
}
Also used : RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext)

Example 3 with RunAndTrack

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

the class InvokeInRATTest method testStaticInvoke.

@Test
public void testStaticInvoke() {
    IEclipseContext context = EclipseContextFactory.create();
    final int[] count = new int[1];
    context.runAndTrack(new RunAndTrack() {

        @Override
        public boolean changed(IEclipseContext context) {
            TestHandler handler = (TestHandler) context.get("handlerA");
            if (handler != null) {
                ContextInjectionFactory.invoke(handler, CanExecute.class, context);
                count[0]++;
            }
            // continue to be notified
            return true;
        }
    });
    // check that updates are propagated
    context.set("active", Integer.valueOf(123));
    context.set("selected", "abc");
    TestHandler handler = new TestHandler();
    context.set("handlerA", handler);
    assertEquals(Integer.valueOf(123), handler.active);
    assertEquals("abc", handler.selected);
    // check handler replacement
    count[0] = 0;
    TestHandler newHandler = new TestHandler();
    context.set("handlerA", newHandler);
    assertEquals(1, count[0]);
    assertEquals(Integer.valueOf(123), newHandler.active);
    assertEquals("abc", newHandler.selected);
    // change values in the context; values should not be propagated to handlers
    context.set("active", Integer.valueOf(456));
    context.set("selected", "xyz");
    assertEquals(Integer.valueOf(123), handler.active);
    assertEquals("abc", handler.selected);
    assertEquals(Integer.valueOf(123), newHandler.active);
    assertEquals("abc", newHandler.selected);
}
Also used : RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) CanExecute(org.eclipse.e4.core.di.annotations.CanExecute) Test(org.junit.Test)

Example 4 with RunAndTrack

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

the class ContextObjectSupplier method get.

@Override
public void get(IObjectDescriptor[] descriptors, Object[] actualArgs, final IRequestor requestor, boolean initial, boolean track, boolean group) {
    final String[] keys = new String[descriptors.length];
    final boolean[] active = new boolean[descriptors.length];
    for (int i = 0; i < descriptors.length; i++) {
        String key = getKey(descriptors[i]);
        if ((actualArgs[i] == IInjector.NOT_A_VALUE))
            keys[i] = key;
        else if (// allow provider to override IEclipseContext
        ECLIPSE_CONTEXT_NAME.equals(key))
            keys[i] = ECLIPSE_CONTEXT_NAME;
        else
            keys[i] = null;
        if (descriptors[i] == null)
            active[i] = false;
        else
            active[i] = (descriptors[i].hasQualifier(Active.class));
    }
    if (requestor != null && track) {
        // only track if requested
        if (initial) {
            RunAndTrack trackable = new ContextInjectionListener(context, actualArgs, keys, active, requestor, group);
            context.runAndTrack(trackable);
        } else {
            // we do track if this is done inside a computation, but don't create another runnable
            fillArgs(actualArgs, keys, active);
        }
    } else {
        if (descriptors.length > 0) {
            pauseRecording();
            try {
                fillArgs(actualArgs, keys, active);
            } finally {
                resumeRecording();
            }
        }
    }
}
Also used : Active(org.eclipse.e4.core.contexts.Active) RunAndTrack(org.eclipse.e4.core.contexts.RunAndTrack)

Example 5 with RunAndTrack

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

the class ActivationTest method testGetActiveRATNumberOfCalls2.

/**
 * A variation of {@link #testGetActiveRATNumberOfCalls()} that
 * uses distinct values in the leaf contexts.
 */
@Test
public void testGetActiveRATNumberOfCalls2() {
    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", "11");
    child12.set("var", "12");
    child1.set("var", "3");
    child21.set("var", "21");
    child22.set("var", "22");
    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("11", result[0]);
    assertEquals(2, called[0]);
    child12.activateBranch();
    assertEquals("12", result[0]);
    assertEquals(3, called[0]);
    child22.activateBranch();
    assertEquals("12", result[0]);
    assertEquals(3, called[0]);
    child21.activateBranch();
    assertEquals("12", 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)

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