⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsunittestmanager.js

📁 sourcode about java basic
💻 JS
📖 第 1 页 / 共 2 页
字号:
function jsUnitTestManager() {
    this._windowForAllProblemMessages = null;

    this.container = top.frames.testContainer
    this.documentLoader = top.frames.documentLoader;
    this.mainFrame = top.frames.mainFrame;

    this.containerController = this.container.frames.testContainerController;
    this.containerTestFrame = this.container.frames.testFrame;

    var mainData = this.mainFrame.frames.mainData;

    // form elements on mainData frame
    this.testFileName = mainData.document.testRunnerForm.testFileName;
    this.runButton = mainData.document.testRunnerForm.runButton;
    this.traceLevel = mainData.document.testRunnerForm.traceLevel;
    this.closeTraceWindowOnNewRun = mainData.document.testRunnerForm.closeTraceWindowOnNewRun;
    this.timeout = mainData.document.testRunnerForm.timeout;
    this.setUpPageTimeout = mainData.document.testRunnerForm.setUpPageTimeout;

    // image output
    this.progressBar = this.mainFrame.frames.mainProgress.document.progress;

    this.problemsListField = this.mainFrame.frames.mainErrors.document.testRunnerForm.problemsList;
    this.testCaseResultsField = this.mainFrame.frames.mainResults.document.resultsForm.testCases;
    this.resultsTimeField = this.mainFrame.frames.mainResults.document.resultsForm.time;

    // 'layer' output frames
    this.uiFrames = new Object();
    this.uiFrames.mainStatus = this.mainFrame.frames.mainStatus;

    var mainCounts = this.mainFrame.frames.mainCounts;

    this.uiFrames.mainCountsErrors = mainCounts.frames.mainCountsErrors;
    this.uiFrames.mainCountsFailures = mainCounts.frames.mainCountsFailures;
    this.uiFrames.mainCountsRuns = mainCounts.frames.mainCountsRuns;
    this._baseURL = "";

    this.setup();
}

// seconds to wait for each test page to load
jsUnitTestManager.TESTPAGE_WAIT_SEC = 120;
jsUnitTestManager.TIMEOUT_LENGTH = 20;

// seconds to wait for setUpPage to complete
jsUnitTestManager.SETUPPAGE_TIMEOUT = 120;

// milliseconds to wait between polls on setUpPages
jsUnitTestManager.SETUPPAGE_INTERVAL = 100;

jsUnitTestManager.RESTORED_HTML_DIV_ID = "jsUnitRestoredHTML";

jsUnitTestManager.prototype.setup = function () {
    this.totalCount = 0;
    this.errorCount = 0;
    this.failureCount = 0;
    this._suiteStack = Array();

    var initialSuite = new top.jsUnitTestSuite();
    push(this._suiteStack, initialSuite);
}

jsUnitTestManager.prototype.start = function () {
    this._baseURL = this.resolveUserEnteredTestFileName();
    var firstQuery = this._baseURL.indexOf("?");
    if (firstQuery >= 0) {
        this._baseURL = this._baseURL.substring(0, firstQuery);
    }
    var lastSlash = this._baseURL.lastIndexOf("/");
    var lastRevSlash = this._baseURL.lastIndexOf("\\");
    if (lastRevSlash > lastSlash) {
        lastSlash = lastRevSlash;
    }
    if (lastSlash > 0) {
        this._baseURL = this._baseURL.substring(0, lastSlash + 1);
    }

    this._timeRunStarted = new Date();
    this.initialize();
    setTimeout('top.testManager._nextPage();', jsUnitTestManager.TIMEOUT_LENGTH);
}

jsUnitTestManager.prototype.getBaseURL = function () {
    return this._baseURL;
}

jsUnitTestManager.prototype.doneLoadingPage = function (pageName) {
    //this.containerTestFrame.setTracer(top.tracer);
    this._testFileName = pageName;
    if (this.isTestPageSuite())
        this._handleNewSuite();
    else
    {
        this._testIndex = 0;
        this._testsInPage = this.getTestFunctionNames();
        this._numberOfTestsInPage = this._testsInPage.length;
        this._runTest();
    }
}

jsUnitTestManager.prototype._handleNewSuite = function () {
    var allegedSuite = this.containerTestFrame.suite();
    if (allegedSuite.isjsUnitTestSuite) {
        var newSuite = allegedSuite.clone();
        if (newSuite.containsTestPages())
            push(this._suiteStack, newSuite);
        this._nextPage();
    }
    else {
        this.fatalError('Invalid test suite in file ' + this._testFileName);
        this.abort();
    }
}

jsUnitTestManager.prototype._runTest = function () {
    if (this._testIndex + 1 > this._numberOfTestsInPage)
    {
        // execute tearDownPage *synchronously*
        // (unlike setUpPage which is asynchronous)
        if (typeof this.containerTestFrame.tearDownPage == 'function') {
            this.containerTestFrame.tearDownPage();
        }

        this._nextPage();
        return;
    }

    if (this._testIndex == 0) {
        this.storeRestoredHTML();
        if (typeof(this.containerTestFrame.setUpPage) == 'function') {
            // first test for this page and a setUpPage is defined
            if (typeof(this.containerTestFrame.setUpPageStatus) == 'undefined') {
                // setUpPage() not called yet, so call it
                this.containerTestFrame.setUpPageStatus = false;
                this.containerTestFrame.startTime = new Date();
                this.containerTestFrame.setUpPage();
                // try test again later
                setTimeout('top.testManager._runTest()', jsUnitTestManager.SETUPPAGE_INTERVAL);
                return;
            }

            if (this.containerTestFrame.setUpPageStatus != 'complete') {
                top.status = 'setUpPage not completed... ' + this.containerTestFrame.setUpPageStatus + ' ' + (new Date());
                if ((new Date() - this.containerTestFrame.startTime) / 1000 > this.getsetUpPageTimeout()) {
                    this.fatalError('setUpPage timed out without completing.');
                    if (!this.userConfirm('Retry Test Run?')) {
                        this.abort();
                        return;
                    }
                    this.containerTestFrame.startTime = (new Date());
                }
                // try test again later
                setTimeout('top.testManager._runTest()', jsUnitTestManager.SETUPPAGE_INTERVAL);
                return;
            }
        }
    }

    top.status = '';
    // either not first test, or no setUpPage defined, or setUpPage completed
    this.executeTestFunction(this._testsInPage[this._testIndex]);
    this.totalCount++;
    this.updateProgressIndicators();
    this._testIndex++;
    setTimeout('top.testManager._runTest()', jsUnitTestManager.TIMEOUT_LENGTH);
}

jsUnitTestManager.prototype._done = function () {
    var secondsSinceRunBegan = (new Date() - this._timeRunStarted) / 1000;
    this.setStatus('Done (' + secondsSinceRunBegan + ' seconds)');
    this._cleanUp();
    if (top.shouldSubmitResults()) {
        this.resultsTimeField.value = secondsSinceRunBegan;
        top.submitResults();
    }
}

jsUnitTestManager.prototype._nextPage = function () {
    this._restoredHTML = null;
    if (this._currentSuite().hasMorePages()) {
        this.loadPage(this._currentSuite().nextPage());
    }
    else {
        pop(this._suiteStack);
        if (this._currentSuite() == null)
            this._done();
        else
            this._nextPage();
    }
}

jsUnitTestManager.prototype._currentSuite = function () {
    var suite = null;

    if (this._suiteStack && this._suiteStack.length > 0)
        suite = this._suiteStack[this._suiteStack.length - 1];

    return suite;
}

jsUnitTestManager.prototype.calculateProgressBarProportion = function () {
    if (this.totalCount == 0)
        return 0;
    var currentDivisor = 1;
    var result = 0;

    for (var i = 0; i < this._suiteStack.length; i++) {
        var aSuite = this._suiteStack[i];
        currentDivisor *= aSuite.testPages.length;
        result += (aSuite.pageIndex - 1) / currentDivisor;
    }
    result += (this._testIndex + 1) / (this._numberOfTestsInPage * currentDivisor);
    return result;
}

jsUnitTestManager.prototype._cleanUp = function () {
    this.containerController.setTestPage('./app/emptyPage.html');
    this.finalize();
    top.tracer.finalize();
}

jsUnitTestManager.prototype.abort = function () {
    this.setStatus('Aborted');
    this._cleanUp();
}

jsUnitTestManager.prototype.getTimeout = function () {
    var result = jsUnitTestManager.TESTPAGE_WAIT_SEC;
    try {
        result = eval(this.timeout.value);
    }
    catch (e) {
    }
    return result;
}

jsUnitTestManager.prototype.getsetUpPageTimeout = function () {
    var result = jsUnitTestManager.SETUPPAGE_TIMEOUT;
    try {
        result = eval(this.setUpPageTimeout.value);
    }
    catch (e) {
    }
    return result;
}

jsUnitTestManager.prototype.isTestPageSuite = function () {
    var result = false;
    if (typeof(this.containerTestFrame.suite) == 'function')
    {
        result = true;
    }
    return result;
}

jsUnitTestManager.prototype.getTestFunctionNames = function () {
    var testFrame = this.containerTestFrame;
    var testFunctionNames = new Array();
    var i;

    if (testFrame && typeof(testFrame.exposeTestFunctionNames) == 'function')
        return testFrame.exposeTestFunctionNames();

    if (testFrame &&
        testFrame.document &&
        typeof(testFrame.document.scripts) != 'undefined' &&
        testFrame.document.scripts.length > 0) { // IE5 and up
        var scriptsInTestFrame = testFrame.document.scripts;

        for (i = 0; i < scriptsInTestFrame.length; i++) {
            var someNames = this._extractTestFunctionNamesFromScript(scriptsInTestFrame[i]);
            if (someNames)
                testFunctionNames = testFunctionNames.concat(someNames);
        }
    }
    else {
        for (i in testFrame) {
            if (i.substring(0, 4) == 'test' && typeof(testFrame[i]) == 'function')
                push(testFunctionNames, i);
        }
    }
    return testFunctionNames;
}

jsUnitTestManager.prototype._extractTestFunctionNamesFromScript = function (aScript) {
    var result;
    var remainingScriptToInspect = aScript.text;
    var currentIndex = this._indexOfTestFunctionIn(remainingScriptToInspect);
    while (currentIndex != -1) {
        if (!result)
            result = new Array();

        var fragment = remainingScriptToInspect.substring(currentIndex, remainingScriptToInspect.length);
        result = result.concat(fragment.substring('function '.length, fragment.indexOf('(')));
        remainingScriptToInspect = remainingScriptToInspect.substring(currentIndex + 12, remainingScriptToInspect.length);
        currentIndex = this._indexOfTestFunctionIn(remainingScriptToInspect);
    }
    return result;
}

jsUnitTestManager.prototype._indexOfTestFunctionIn = function (string) {
    return string.indexOf('function test');
}

jsUnitTestManager.prototype.loadPage = function (testFileName) {
    this._testFileName = testFileName;
    this._loadAttemptStartTime = new Date();
    this.setStatus('Opening Test Page "' + this._testFileName + '"');
    this.containerController.setTestPage(this._testFileName);
    this._callBackWhenPageIsLoaded();
}

jsUnitTestManager.prototype._callBackWhenPageIsLoaded = function () {
    if ((new Date() - this._loadAttemptStartTime) / 1000 > this.getTimeout()) {
        this.fatalError('Reading Test Page ' + this._testFileName + ' timed out.\nMake sure that the file exists and is a Test Page.');
        if (this.userConfirm('Retry Test Run?')) {
            this.loadPage(this._testFileName);
            return;
        } else {
            this.abort();
            return;
        }
    }
    if (!this._isTestFrameLoaded()) {
        setTimeout('top.testManager._callBackWhenPageIsLoaded();', jsUnitTestManager.TIMEOUT_LENGTH);
        return;
    }
    this.doneLoadingPage(this._testFileName);
}

jsUnitTestManager.prototype._isTestFrameLoaded = function () {
    try {
        return this.containerController.isPageLoaded();
    }
    catch (e) {
    }
    return false;
}

jsUnitTestManager.prototype.executeTestFunction = function (functionName) {
    this._testFunctionName = functionName;
    this.setStatus('Running test "' + this._testFunctionName + '"');
    var excep = null;
    var timeBefore = new Date();
    try {
        if (this._restoredHTML)
            top.testContainer.testFrame.document.getElementById(jsUnitTestManager.RESTORED_HTML_DIV_ID).innerHTML = this._restoredHTML;
        if (this.containerTestFrame.setUp !== JSUNIT_UNDEFINED_VALUE)
            this.containerTestFrame.setUp();
        this.containerTestFrame[this._testFunctionName]();
    }
    catch (e1) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -