public void testSourceProvider() throws Exception {
    IWorkbenchWindow window = openTestWindow();
    IEvaluationService service = (IEvaluationService) window.getService(IEvaluationService.class);
    assertNotNull(service);

    MyEval listener = new MyEval();
    UserExpression expression = new UserExpression("Paul");
    IEvaluationReference ref =
        service.addEvaluationListener(expression, listener, IEvaluationService.RESULT);
    assertEquals(ISources.ACTIVE_CONTEXT << 1, ref.getSourcePriority());
    assertFalse(listener.currentValue);
    assertEquals(1, listener.count);

    ISourceProviderService sps =
        (ISourceProviderService) window.getService(ISourceProviderService.class);
    ActiveUserSourceProvider userProvider =
        (ActiveUserSourceProvider) sps.getSourceProvider("username");

    userProvider.setUsername("John");
    assertFalse(listener.currentValue);
    assertEquals(1, listener.count);

    userProvider.setUsername("Paul");
    assertTrue(listener.currentValue);
    assertEquals(2, listener.count);

    userProvider.setUsername("guest");
    assertFalse(listener.currentValue);
    assertEquals(3, listener.count);
  }
  public void testTwoEvaluations() throws Exception {
    IWorkbenchWindow window = openTestWindow();
    IEvaluationService service = (IEvaluationService) window.getService(IEvaluationService.class);
    assertNotNull(service);

    MyEval listener1 = new MyEval();
    MyEval listener2 = new MyEval();
    IContextActivation context1 = null;
    IEvaluationReference evalRef1 = null;
    IEvaluationReference evalRef2 = null;
    IContextService contextService = null;
    try {
      evalRef1 =
          service.addEvaluationListener(
              new ActiveContextExpression(CONTEXT_ID1, new String[] {ISources.ACTIVE_CONTEXT_NAME}),
              listener1,
              IEvaluationService.RESULT);
      assertEquals(1, listener1.count);
      assertFalse(listener1.currentValue);

      evalRef2 =
          service.addEvaluationListener(
              new ActiveContextExpression(CONTEXT_ID1, new String[] {ISources.ACTIVE_CONTEXT_NAME}),
              listener2,
              IEvaluationService.RESULT);
      assertEquals(1, listener2.count);
      assertFalse(listener2.currentValue);
      evalRef2.setResult(true);

      contextService = (IContextService) window.getService(IContextService.class);
      context1 = contextService.activateContext(CONTEXT_ID1);
      assertEquals(2, listener1.count);
      assertTrue(listener1.currentValue);
      // we already set this guy to true, he should skip
      assertEquals(1, listener2.count);
      assertFalse(listener2.currentValue);

      evalRef1.setResult(false);
      contextService.deactivateContext(context1);
      context1 = null;
      assertEquals(2, listener2.count);
      assertFalse(listener2.currentValue);

      // we already set this guy to false, so he should be the old
      // values
      assertEquals(2, listener1.count);
      assertTrue(listener1.currentValue);

    } finally {
      if (context1 != null) {
        contextService.deactivateContext(context1);
      }
      if (evalRef1 != null) {
        service.removeEvaluationListener(evalRef1);
      }
      if (evalRef2 != null) {
        service.removeEvaluationListener(evalRef2);
      }
    }
  }