Example usage for org.eclipse.jface.operation ProgressMonitorUtil createAccumulatingProgressMonitor

List of usage examples for org.eclipse.jface.operation ProgressMonitorUtil createAccumulatingProgressMonitor

Introduction

In this page you can find the example usage for org.eclipse.jface.operation ProgressMonitorUtil createAccumulatingProgressMonitor.

Prototype

public static IProgressMonitorWithBlocking createAccumulatingProgressMonitor(IProgressMonitor monitor,
        Display display) 

Source Link

Document

Wraps an IProgressMonitor associated with the UI thread, producing a new IProgressMonitorWithBlocking which can be used from any one thread.

Usage

From source file:org.eclipse.ui.tests.progress.AccumulatingProgressMonitorTest.java

License:Open Source License

/**
 * Call all applicable methods in a separate thread. Since the test runs in
 * the UI thread, we must fire a new thread to do the initial calls. While
 * the thread is running, we will also run the event loop until either the
 * thread dies or a maximum duration has passed. Then, assert that no
 * assertion exceptions were thrown during the calls to
 * UIThreadAsserterMonitor's methods, and assert that each method was in
 * fact called./*from w  ww. ja  v  a2 s. c o  m*/
 */
@Test
public void testAccumulatingMonitorInUIThread() throws Exception {
    Semaphore uiSemaphore = new Semaphore(0);
    Semaphore backgroundSemaphore = new Semaphore(0);

    final Throwable[] death = new Throwable[1];
    final UIThreadAsserterMonitor[] mon2 = new UIThreadAsserterMonitor[1];

    Thread t = new Thread("Test Accumulating Monitor") {
        @Override
        public void run() {
            int uiReleaseCount = 0;
            try {
                UIThreadAsserterMonitor tm = new UIThreadAsserterMonitor();
                mon2[0] = tm;
                IProgressMonitorWithBlocking wrapper = ProgressMonitorUtil.createAccumulatingProgressMonitor(tm,
                        Display.getDefault());

                wrapper.beginTask("Some Task", 100);
                wrapper.setTaskName("Task Name");
                wrapper.subTask("Subtask");
                // call work, but internalWorked will be called
                // on our monitor instead
                wrapper.worked(10);
                wrapper.isCanceled();
                wrapper.setCanceled(false);
                wrapper.setBlocked(new Status(IStatus.ERROR, "org.eclipse.ui.tests", "Some Error"));

                uiSemaphore.release();
                uiReleaseCount++;
                backgroundSemaphore.acquire();
                wrapper.clearBlocked();
                wrapper.done();
                uiSemaphore.release();
                uiReleaseCount++;
            } catch (Throwable t) {
                death[0] = t;
                if (uiReleaseCount == 0) {
                    // we never released UI, test is frozen
                    uiReleaseCount++;
                    uiSemaphore.release();
                    try {
                        backgroundSemaphore.acquire();
                    } catch (InterruptedException ie) {
                    }
                }
                // We only released UI once, it is still waiting for 2nd
                // release
                if (uiReleaseCount == 1) {
                    uiSemaphore.release();
                }
            }
        }
    };
    t.start();
    uiSemaphore.acquire();
    runEventLoopUntilEmpty();
    backgroundSemaphore.release();
    uiSemaphore.acquire();
    runEventLoopUntilEmpty();

    assertNull("Wrapped monitor not executed in UI thread", death[0]);
    assertNotNull(mon2[0]);
    assertTrue(mon2[0].beginTaskCalled);
    assertTrue(mon2[0].setTaskNameCalled);
    assertTrue(mon2[0].subTaskCalled);

    // AccumulatingProgressMonitor calls internalWorked instead
    assertFalse(mon2[0].workedCalled);
    assertTrue(mon2[0].internalWorkedCalled);

    assertTrue(mon2[0].isCanceledCalled);
    assertTrue(mon2[0].setCanceledCalled);

    assertTrue(mon2[0].setBlockedCalled);
    assertTrue(mon2[0].clearBlockedCalled);

    assertTrue(mon2[0].doneCalled);
}

From source file:org.eclipse.ui.tests.progress.AccumulatingProgressMonitorTest.java

License:Open Source License

/**
 * Call setTaskName a large number of times and verify a sufficiently
 * efficient lower number were actually propagated to our monitor. This
 * verifies that the UI thread is not firing all events, and the
 * AccumulatingProgressMonitor's collector is functioning properly.
 */// w ww. j  av  a2s  .  c o m
@Test
public void testCollector() {
    final CollectorAsserterMonitor mon = new CollectorAsserterMonitor();
    final int[] numLoops = new int[] { 10000 };
    IProgressMonitorWithBlocking wrapper = ProgressMonitorUtil.createAccumulatingProgressMonitor(mon,
            Display.getDefault());
    wrapper.beginTask("Some Task", 100);
    for (int i = 0; i < numLoops[0]; i++) {
        wrapper.setTaskName("Task Name " + i);
    }
    wrapper.done();

    ArrayList<String> tasks = mon.getTaskNames();
    int size = tasks.size();
    assertEquals(0, size); // No events should have been processed yet

    runEventLoopUntilEmpty();
    tasks = mon.getTaskNames();
    size = tasks.size();
    assertEquals(1, size);
    String expected = "Task Name " + (numLoops[0] - 1);
    assertEquals(expected, tasks.get(0));
}