Example #1
0
  private boolean pruneTestCase(TestCase testCase) {
    returnToInitialState();
    executeTestCase(testCase);

    WidgetHierarchy widgetHierarchy = new WidgetHierarchy();
    widgetHierarchy.getWidgetHierarchy(true);
    widgetHierarchy.populateValues();

    if (widgetStates.contains(widgetHierarchy)) {
      return true;
    } else {
      for (WidgetHierarchy existingState : widgetStates) {
        logger.debug(CompareHierarchies.compare(existingState, widgetHierarchy));
      }
    }
    widgetStates.add(widgetHierarchy);
    return false;
  }
Example #2
0
  private void generate() {
    synchronized (lock) {
      widgetStates.clear();
      uniqueTestCases.clear();
      processedTestCases.clear();
      currentTestCase = null;
      throwable = null;
    }

    try {
      final DisplayProxy defaultDisplay = DisplayProxy.Factory.getDefault();
      defaultDisplay.asyncExec(
          new Runnable() {
            public void run() {
              defaultDisplay.addFilter(SWTProxy.KeyDown, keyListener);
            }
          });

      returnToInitialState();

      // Add initial SUT state.
      WidgetHierarchy widgetHierarchy = new WidgetHierarchy();
      widgetHierarchy.getWidgetHierarchy(true);
      widgetHierarchy.populateValues();
      widgetStates.add(widgetHierarchy);

      boolean newUniqueTestCasesFound;
      do {
        newUniqueTestCasesFound = false;

        candidateTestCases = new LinkedHashSet<TestCase>();
        boolean firstStep = uniqueTestCases.size() == 0;
        if (firstStep) {
          List<TestStep> nextSteps = generateNextSteps();
          for (TestStep testStep : nextSteps) {
            TestCase newTestCase = new TestCase();
            newTestCase.add(testStep);
            candidateTestCases.add(newTestCase);
          }
        } else {
          for (TestCase testCase : uniqueTestCases) {
            if (processedTestCases.contains(testCase)) {
              continue;
            }

            returnToInitialState();

            if (isStopped()) {
              break;
            }

            currentTestCase = testCase;
            executeTestCase(testCase);

            List<TestStep> nextSteps = generateNextSteps();
            for (TestStep nextStep : nextSteps) {
              try {
                TestCase newTestCase = (TestCase) testCase.clone();
                newTestCase.add(nextStep);
                newTestCase.setParent(testCase);
                candidateTestCases.add(newTestCase);
              } catch (CloneNotSupportedException ignore) {
                // Ignore.
              }
            }

            processedTestCases.add(testCase);
          }
        }

        for (TestCase newTestCase : candidateTestCases) {
          if (isStopped()) {
            break;
          }

          if (!pruneTestCase(newTestCase)) {
            if (uniqueTestCases.add(newTestCase)) {
              uniqueTestCases.remove(newTestCase.getParent());
              logger.debug("Adding test case, newTestCase=" + newTestCase);
              newUniqueTestCasesFound = true;
            }
          } else {
            logger.debug("Test case will be pruned, newTestCase=" + newTestCase);
          }
        }

        logger.debug(
            "Current test case count="
                + uniqueTestCases.size()
                + ", new unique test cases found="
                + newUniqueTestCasesFound);
      } while (newUniqueTestCasesFound && !isStopped());

      returnToInitialState();
    } catch (Throwable throwable) {
      logger.error("An exception occurred during test case generation.", throwable);
      this.throwable = throwable;
    }

    final DisplayProxy defaultDisplay = DisplayProxy.Factory.getDefault();
    defaultDisplay.asyncExec(
        new Runnable() {
          public void run() {
            defaultDisplay.removeFilter(SWTProxy.KeyDown, keyListener);
          }
        });

    synchronized (lock) {
      executionState = ExecutionState.STOPPED;
      lock.notifyAll();
    }
  }