/** @deprecated to suppress deprecation warnings */
  public void testCancellation() {
    TestProgressMonitor root = new TestProgressMonitor();
    root.beginTask("", 1000);

    SubProgressMonitor spm = new SubProgressMonitor(root, 1000);

    // Test that changes at the root propogate to the child
    root.setCanceled(true);
    Assert.assertTrue(spm.isCanceled());
    root.setCanceled(false);
    Assert.assertFalse(spm.isCanceled());

    // Test that changes to the child propogate to the root
    spm.setCanceled(true);
    Assert.assertTrue(root.isCanceled());
    spm.setCanceled(false);
    Assert.assertFalse(root.isCanceled());

    // Test a chain of depth 2
    spm.beginTask("", 1000);
    SubProgressMonitor spm2 = new SubProgressMonitor(spm, 1000);

    // Test that changes at the root propogate to the child
    root.setCanceled(true);
    Assert.assertTrue(spm2.isCanceled());
    root.setCanceled(false);
    Assert.assertFalse(spm2.isCanceled());

    // Test that changes to the child propogate to the root
    spm2.setCanceled(true);
    Assert.assertTrue(root.isCanceled());
    spm2.setCanceled(false);
    Assert.assertFalse(root.isCanceled());
  }
 private String[] runChildTest(
     int depth, TestProgressMonitor root, IProgressMonitor child, int ticks) {
   ArrayList results = new ArrayList();
   child.beginTask("beginTask" + depth, ticks);
   results.add(root.getTaskName());
   child.subTask("subTask" + depth);
   results.add(root.getSubTaskName());
   child.setTaskName("setTaskName" + depth);
   results.add(root.getTaskName());
   return (String[]) results.toArray(new String[results.size()]);
 }
  /** Test SubProgressMonitor's created with negative a work value. */
  public void testNegativeWorkValues() {
    TestProgressMonitor top = new TestProgressMonitor();
    top.beginTask("", 10);

    SubProgressMonitor childMonitor = new SubProgressMonitor(top, IProgressMonitor.UNKNOWN); // -1
    childMonitor.beginTask("", 10);
    childMonitor.worked(5);
    Assert.assertEquals(0.0, top.getTotalWork(), 0.01d);
    childMonitor.done();
    Assert.assertEquals(0.0, top.getTotalWork(), 0.01d);

    top.done();
  }
  /**
   * Tests creating progress monitors under a custom progress monitor parent. This is the same as
   * the performance test as the same name, but it verifies correctness rather than performance.
   */
  public void testCreateChildrenUnderCustomParent() {
    TestProgressMonitor monitor = new TestProgressMonitor();
    createChildrenUnderParent(monitor, SubProgressTest.PROGRESS_SIZE);

    // We don't actually expect the progress to be optimal in this case since the progress monitor
    // wouldn't
    // know what it was rooted under and would have had to report more progress than necessary...
    // but we
    // should be able to check that there was no redundancy.

    Assert.assertTrue(monitor.getRedundantWorkCalls() == 0);
    Assert.assertTrue(monitor.getWorkCalls() >= 100);
  }
  /**
   * Test behaviors that subclasses of SubProgressMonitor will expect from their base class.
   *
   * @deprecated to suppress deprecation warnings
   */
  public void testCustomSubclass() {
    TestProgressMonitor top = new TestProgressMonitor();
    top.beginTask("", 1000);

    SubProgressSubclass customSubclass = new SubProgressSubclass(top, 1000);
    customSubclass.beginTask("", 10000);

    for (int count = 0; count < 10000; count++) customSubclass.worked(1);

    Assert.assertEquals(
        "If there is a custom subclass of SubProgressMonitor, all calls to worked() should delegate to internalWorked",
        10000,
        customSubclass.internalWorkedCalls);
    customSubclass.done();
    top.done();
  }
  /**
   * Tests the automatic cleanup when progress monitors are created via their constructor
   *
   * @deprecated to suppress deprecation warnings
   */
  public void testParallelChildren() {
    TestProgressMonitor top = new TestProgressMonitor();
    top.beginTask("", 1000);
    SubProgressMonitor mon = new SubProgressMonitor(top, 1000);
    mon.beginTask("", 1000);

    SubProgressMonitor monitor1 = new SubProgressMonitor(mon, 200);
    SubProgressMonitor monitor2 = new SubProgressMonitor(mon, 200);

    Assert.assertEquals("Ensure no work has been reported yet", 0.0, top.getTotalWork(), 0.01d);
    monitor1.beginTask("", 1000);
    Assert.assertEquals("Ensure no work has been reported yet", 0.0, top.getTotalWork(), 0.01d);
    monitor2.beginTask("", 1000);
    Assert.assertEquals("Should not have cleaned up monitor 1", 0.0, top.getTotalWork(), 0.01d);
    monitor1.done();

    Assert.assertEquals("Should have cleaned up monitor 1", 200.0, top.getTotalWork(), 0.01d);
    monitor1.worked(1000);
    Assert.assertEquals(
        "Monitor1 shouldn't report work once it's complete", 200.0, top.getTotalWork(), 0.01d);
    monitor2.worked(500);
    Assert.assertEquals(300.0, top.getTotalWork(), 0.01d);

    // Create a monitor that will leak - monitors won't be auto-completed until their done methods
    // are
    // called
    SubProgressMonitor monitor3 = new SubProgressMonitor(mon, 300);
    Assert.assertEquals(
        "Monitor2 should not have been cleaned up yet", 300.0, top.getTotalWork(), 0.01d);
    SubProgressMonitor monitor4 = new SubProgressMonitor(mon, 300);
    monitor4.beginTask("", 100);
    mon.done();
    Assert.assertNotNull(monitor3);

    Assert.assertEquals(
        "All leaked work should have been collected", 1000.0, top.getTotalWork(), 0.01d);
  }
  /**
   * Tests SubProgressMonitor nesting when using the default constructor. (Tests parents in floating
   * point mode)
   *
   * @deprecated to suppress deprecation warnings
   */
  public void testConstructorNestingFP() {
    TestProgressMonitor top = new TestProgressMonitor();
    top.beginTask("", 2000);

    // Create an SPM, put it in floating-point mode, and consume half its work
    SubProgressMonitor fpMonitor = new SubProgressMonitor(top, 1000);
    fpMonitor.beginTask("", 100);
    fpMonitor.internalWorked(50.0);
    fpMonitor.internalWorked(-10.0); // should have no effect

    Assert.assertEquals(500.0, top.getTotalWork(), 0.01d);

    // Create a child monitor, and ensure that it grabs the correct amount of work
    // from the parent.
    SubProgressMonitor childMonitor = new SubProgressMonitor(fpMonitor, 20);
    childMonitor.beginTask("", 100);
    childMonitor.worked(100);
    childMonitor.done();

    Assert.assertEquals(700.0, top.getTotalWork(), 0.01d);

    // Create a child monitor, and ensure that it grabs the correct amount of work
    // from the parent.
    SubProgressMonitor childMonitor2 = new SubProgressMonitor(fpMonitor, 30);
    childMonitor2.beginTask("", 100);
    childMonitor2.worked(100);
    childMonitor2.done();

    Assert.assertEquals(1000.0, top.getTotalWork(), 0.01d);

    // Ensure that creating another child will have no effect
    SubProgressMonitor childMonitor3 = new SubProgressMonitor(fpMonitor, 10);
    childMonitor3.beginTask("", 100);
    childMonitor3.worked(100);
    childMonitor3.done();

    Assert.assertEquals(1000.0, top.getTotalWork(), 0.01d);
    fpMonitor.worked(100);
    Assert.assertEquals(1000.0, top.getTotalWork(), 0.01d);
    fpMonitor.done();
    Assert.assertEquals(1000.0, top.getTotalWork(), 0.01d);
  }
  /**
   * Tests the style bits in SubProgressMonitor
   *
   * @deprecated to suppress deprecation warnings
   */
  public void testStyles() {

    int[] styles =
        new int[] {
          0,
          SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK,
          SubProgressMonitor.SUPPRESS_SUBTASK_LABEL,
          SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK
              | SubProgressMonitor.SUPPRESS_SUBTASK_LABEL
        };

    HashMap expected = new HashMap();
    expected.put("style 0 below style 2", new String[] {"setTaskName0", "", "setTaskName1"});
    expected.put(
        "style 2 below style 0", new String[] {"setTaskName1", "beginTask1 ", "setTaskName1"});
    expected.put(
        "style 6 below style 0", new String[] {"setTaskName1", "beginTask1 ", "setTaskName1"});
    expected.put(
        "style 2 below style 4",
        new String[] {"setTaskName1", "beginTask0 beginTask1 ", "setTaskName1"});
    expected.put(
        "style 0 below style 0", new String[] {"setTaskName0", "subTask1", "setTaskName1"});
    expected.put("style 6 as top-level monitor", new String[] {"", "", "setTaskName0"});
    expected.put("style 6 below style 2", new String[] {"setTaskName1", "", "setTaskName1"});
    expected.put("style 6 below style 6", new String[] {"setTaskName1", "", "setTaskName1"});
    expected.put("style 0 below style 6", new String[] {"setTaskName0", "", "setTaskName1"});
    expected.put("style 4 below style 2", new String[] {"setTaskName1", "", "setTaskName1"});
    expected.put("style 0 as top-level monitor", new String[] {"", "subTask0", "setTaskName0"});
    expected.put(
        "style 0 below style 4",
        new String[] {"setTaskName0", "beginTask0 subTask1", "setTaskName1"});
    expected.put(
        "style 4 below style 0",
        new String[] {"setTaskName1", "beginTask1 subTask1", "setTaskName1"});
    expected.put(
        "style 4 as top-level monitor", new String[] {"", "beginTask0 subTask0", "setTaskName0"});
    expected.put("style 2 below style 6", new String[] {"setTaskName1", "", "setTaskName1"});
    expected.put("style 4 below style 6", new String[] {"setTaskName1", "", "setTaskName1"});
    expected.put("style 2 below style 2", new String[] {"setTaskName1", "", "setTaskName1"});
    expected.put("style 2 as top-level monitor", new String[] {"", "", "setTaskName0"});
    expected.put(
        "style 6 below style 4",
        new String[] {"setTaskName1", "beginTask0 beginTask1 ", "setTaskName1"});
    expected.put(
        "style 4 below style 4",
        new String[] {"setTaskName1", "beginTask0 beginTask1 subTask1", "setTaskName1"});
    HashMap results = new HashMap();

    for (int i = 0; i < styles.length; i++) {
      int style = styles[i];
      TestProgressMonitor top = new TestProgressMonitor();
      top.beginTask("", 100);
      SubProgressMonitor child = new SubProgressMonitor(top, 100, style);

      String testName = "style " + style + " as top-level monitor";
      results.put(testName, runChildTest(0, top, child, 100 * styles.length));

      for (int j = 0; j < styles.length; j++) {
        int innerStyle = styles[j];
        SubProgressMonitor innerChild = new SubProgressMonitor(child, 100, innerStyle);
        testName = "style " + innerStyle + " below style " + style;
        results.put(testName, runChildTest(1, top, innerChild, 100));
        innerChild.done();
      }
      child.done();
    }

    String failure = null;
    // Output the code for the observed results, in case one of them has changed intentionally
    for (Iterator iter = results.entrySet().iterator(); iter.hasNext(); ) {
      Map.Entry next = (Map.Entry) iter.next();
      String[] expectedResult = (String[]) expected.get(next.getKey());
      String[] value = (String[]) next.getValue();
      if (compareArray(value, expectedResult)) continue;

      System.out.print("expected.put(\"" + next.getKey() + "\", new String[] {");
      failure = (String) next.getKey();
      String list = concatArray(value);
      System.out.println(list + "});");
    }

    if (failure != null) // Now actually throw an assertation if one of the results failed
    Assert.assertEquals(
          failure,
          concatArray((String[]) expected.get(failure)),
          concatArray((String[]) results.get(failure)));
  }