Skip to content

Commit

Permalink
[INTERNAL] Test Starter: derive testsuite name from HTML page
Browse files Browse the repository at this point in the history
- ui5loader now exposes its guessResourceName function as a private API
- createSuite uses this API to determine a default testsuite name, based
  on the URL of the testsuite's HTML page
- as this makes the data-sap-ui-testsuite attribute obsolete in most
  testsuites, TestRunner and find.html need to recognize such testsuite
  pages by another criterion (use of createSuite script)
- adopt some testsuites in sap.ui.core

Change-Id: I728435162b2d60c0c2260e74ac5520b0d8610038
  • Loading branch information
codeworrior committed Jun 3, 2021
1 parent 3dfc51d commit b8569e7
Show file tree
Hide file tree
Showing 24 changed files with 187 additions and 92 deletions.
6 changes: 6 additions & 0 deletions src/sap.ui.core/src/sap/ui/test/starter/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ sap.ui.define([
return tag ? tag.getAttribute(name) : null;
}

function getDefaultSuiteName() {
var sName = sap.ui.loader._.guessResourceName(location.href);
return sName ? sName.replace(/\.html$/, "") : null;
}

/**
* Execute the given callback once the DOM is ready (which might already be the case).
*
Expand Down Expand Up @@ -275,6 +280,7 @@ sap.ui.define([
addStylesheet: addStylesheet,
encode: encode,
getAttribute: getAttribute,
getDefaultSuiteName: getDefaultSuiteName,
getSuiteConfig: getSuiteConfig,
whenDOMReady: whenDOMReady
};
Expand Down
2 changes: 1 addition & 1 deletion src/sap.ui.core/src/sap/ui/test/starter/createSuite.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@

}

var sSuiteName = utils.getAttribute("data-sap-ui-testsuite");
var sSuiteName = utils.getAttribute("data-sap-ui-testsuite") || utils.getDefaultSuiteName();
var whenLoaded = utils.getSuiteConfig(sSuiteName);


Expand Down
23 changes: 19 additions & 4 deletions src/sap.ui.core/src/ui5loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,21 @@
return syncCallBehavior;
}

function guessResourceName(sURL) {
/**
* Try to find a resource name that would be mapped to the given URL.
*
* If multiple path mappings would create a match, the returned name is not necessarily
* the best (longest) match. The first match which is found, will be returned.
*
* When <code>bLoadedResourcesOnly</code> is set, only those resources will be taken
* into account for which content has been loaded already.
*
* @param {string} sURL URL to guess the resource name for
* @param {boolean} [bLoadedResourcesOnly=false] Whether the guess should be limited to already loaded resources
* @returns {string} Resource name or undefined if no matching name could be found
* @private
*/
function guessResourceName(sURL, bLoadedResourcesOnly) {
var sNamePrefix,
sUrlPrefix,
sResourceName;
Expand All @@ -563,7 +577,7 @@
// the prefix check here has to be done without the slash
sUrlPrefix = mUrlPrefixes[sNamePrefix].absoluteUrl.slice(0, -1);

if ( sURL.indexOf(sUrlPrefix) === 0 ) {
if ( sURL.lastIndexOf(sUrlPrefix, 0) === 0 ) {

// calc resource name
sResourceName = sNamePrefix + sURL.slice(sUrlPrefix.length);
Expand All @@ -572,7 +586,7 @@
sResourceName = sResourceName.slice(1);
}

if ( mModules[sResourceName] && mModules[sResourceName].data != undefined ) {
if ( !bLoadedResourcesOnly || mModules[sResourceName] && mModules[sResourceName].data != undefined ) {
return sResourceName;
}
}
Expand Down Expand Up @@ -2176,7 +2190,7 @@
if ( name ) {
name = getMappedName(name);
} else {
name = guessResourceName(url);
name = guessResourceName(url, true);
}
var oModule = name && mModules[name];
if ( oModule ) {
Expand Down Expand Up @@ -2395,6 +2409,7 @@
getUrlPrefixes: getUrlPrefixes,
loadJSResourceAsync: loadJSResourceAsync,
resolveURL: resolveURL,
guessResourceName: guessResourceName,
toUrl: toUrl,
unloadResources: unloadResources
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<meta charset="utf-8">
<base href="../../../../../../">
<title>TestSuite for sap.ui.core: GTP testcase CORE/APP</title>
<script src="resources/sap/ui/test/starter/createSuite.js"
data-sap-ui-testsuite="test-resources/sap/ui/core/qunit/app/testsuite.app.qunit"></script>
<script src="resources/sap/ui/test/starter/createSuite.js"></script>
</head>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<meta charset="utf-8">
<base href="../../../../../../">
<title>TestSuite for sap.ui.core: GTP testcase CORE/BOOTSTRAP</title>
<script src="resources/sap/ui/test/starter/createSuite.js"
data-sap-ui-testsuite="test-resources/sap/ui/core/qunit/bootstrap/testsuite.bootstrap.qunit"></script>
<script src="resources/sap/ui/test/starter/createSuite.js"></script>
</head>
<body>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<meta charset="utf-8">
<base href="../../../../../../">
<title>TestSuite for Topic: Component</title>
<script src="resources/sap/ui/test/starter/createSuite.js"
data-sap-ui-testsuite="test-resources/sap/ui/core/qunit/component/testsuite.component.qunit"></script>
<script src="resources/sap/ui/test/starter/createSuite.js"></script>
</head>
<body>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<meta charset="utf-8">
<base href="../../../../../../">
<title>TestSuite for sap.ui.core: Content Security Policy Checks</title>
<script src="resources/sap/ui/test/starter/createSuite.js"
data-sap-ui-testsuite="test-resources/sap/ui/core/qunit/csp/testsuite.csp.qunit"></script>
<script src="resources/sap/ui/test/starter/createSuite.js"></script>
</head>
<body>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<base href="../../../../../../">
<title>TestSuite for sap.ui.core: GTP testcase CORE/DESIGNTIME</title>
<script
src="resources/sap/ui/test/starter/createSuite.js"
data-sap-ui-testsuite="test-resources/sap/ui/core/qunit/designtime/testsuite.designtime.qunit"></script>
src="resources/sap/ui/test/starter/createSuite.js"></script>
</head>
<body>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<meta charset="utf-8">
<base href="../../../../../../">
<title>TestSuite for sap.ui.core: GTP testcase CORE/DND</title>
<script src="resources/sap/ui/test/starter/createSuite.js"
data-sap-ui-testsuite="test-resources/sap/ui/core/qunit/dnd/testsuite.dnd.qunit"></script>
<script src="resources/sap/ui/test/starter/createSuite.js"></script>
</head>
<body>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<meta charset="utf-8">
<base href="../../../../../../">
<title>TestSuite for sap.ui.core: GTP testcase CORE/GENERIC</title>
<script src="resources/sap/ui/test/starter/createSuite.js"
data-sap-ui-testsuite="test-resources/sap/ui/core/qunit/generic/testsuite.generic.qunit"></script>
<script src="resources/sap/ui/test/starter/createSuite.js"></script>
</head>
<body>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<meta charset="utf-8">
<base href="../../../../../../">
<title>TestSuite for sap.ui.core: NOT-YET-GTP testcase CORE/GHERKIN</title>
<script src="resources/sap/ui/test/starter/createSuite.js"
data-sap-ui-testsuite="test-resources/sap/ui/core/qunit/gherkin/testsuite.gherkin.qunit"></script>
<script src="resources/sap/ui/test/starter/createSuite.js"></script>
</head>
<body>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<meta charset="utf-8">
<base href="../../../../../../">
<title>TestSuite for sap.ui.core: GTP testcase CORE/I18N</title>
<script src="resources/sap/ui/test/starter/createSuite.js"
data-sap-ui-testsuite="test-resources/sap/ui/core/qunit/i18n/testsuite.i18n.qunit"></script>
<script src="resources/sap/ui/test/starter/createSuite.js"></script>
</head>
<body>
</body>
Expand Down
54 changes: 0 additions & 54 deletions src/sap.ui.core/test/sap/ui/core/qunit/loader/asyncMode.qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,60 +216,6 @@



// ========================================================================================
// Non-JS Resources
// ========================================================================================

QUnit.module("Resource Preload", {
beforeEach: function() {
this.EXPECTED_VIEW_CONTENT = '<mvc:View xmlns:mvc="sap.ui.core.mvc"></mvc:View>';
sap.ui.require.preload({
'fixture/resource-preload/Main.view.xml': this.EXPECTED_VIEW_CONTENT,
'fixture/resource-preload/i18n.properties': ""
});
}
});

QUnit.test("Simple access to a resource", function(assert) {
assert.strictEqual(
sap.ui.loader._.getModuleContent('fixture/resource-preload/Main.view.xml'),
this.EXPECTED_VIEW_CONTENT,
"reading a preloaded non-JS resource should return the expected text result");
});

QUnit.test("Access via a denormalized name", function(assert) {
assert.strictEqual(
sap.ui.loader._.getModuleContent('fixture/resource-preload/dummy/../Main.view.xml'),
this.EXPECTED_VIEW_CONTENT,
"reading a preloaded non-JS resource by a denormalized name should return the expected text result");
});

QUnit.test("Access via a mapped name", function(assert) {
sap.ui.loader.config({
map: {
'resource-preload-alias': 'fixture/resource-preload',
'resource-preload-alias-Main.view': 'fixture/resource-preload/Main.view'
// Note: mapping doesn't handle subtypes like '.view', to be compliant with the AMD spec!
}
});
assert.strictEqual(
sap.ui.loader._.getModuleContent('resource-preload-alias/Main.view.xml'),
this.EXPECTED_VIEW_CONTENT,
"reading a preloaded non-JS resource by a prefixed-mapped module ID should return the expected text result");
assert.strictEqual(
sap.ui.loader._.getModuleContent('resource-preload-alias-Main.view.xml'),
this.EXPECTED_VIEW_CONTENT,
"reading a preloaded non-JS resource by a name-mapped module ID should return the expected text result");
});

QUnit.test("Access empty resource via url", function(assert) {
assert.strictEqual(
sap.ui.loader._.getModuleContent(undefined, sap.ui.require.toUrl('fixture/resource-preload/i18n.properties')),
"",
"reading a preloaded empty resource via url should return the expected text result");
});


// ========================================================================================
// Error Handling for sync and async APIs
// ========================================================================================
Expand Down
135 changes: 135 additions & 0 deletions src/sap.ui.core/test/sap/ui/core/qunit/loader/privateAPIs.qunit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*global QUnit */
sap.ui.define(function() {
"use strict";

// ========================================================================================
// Non-JS Resources (_getModuleContent)
// ========================================================================================

QUnit.module("getModuleContent", {
beforeEach: function() {
this.EXPECTED_VIEW_CONTENT = '<mvc:View xmlns:mvc="sap.ui.core.mvc"></mvc:View>';
sap.ui.require.preload({
'fixture/resource-preload/Main.view.xml': this.EXPECTED_VIEW_CONTENT,
'fixture/resource-preload/i18n.properties': ""
});
}
});

QUnit.test("Simple access to a resource", function(assert) {
assert.strictEqual(
sap.ui.loader._.getModuleContent('fixture/resource-preload/Main.view.xml'),
this.EXPECTED_VIEW_CONTENT,
"reading a preloaded non-JS resource should return the expected text result");
});

QUnit.test("Access via a denormalized name", function(assert) {
assert.strictEqual(
sap.ui.loader._.getModuleContent('fixture/resource-preload/dummy/../Main.view.xml'),
this.EXPECTED_VIEW_CONTENT,
"reading a preloaded non-JS resource by a denormalized name should return the expected text result");
});

QUnit.test("Access via a mapped name", function(assert) {
sap.ui.loader.config({
map: {
'resource-preload-alias': 'fixture/resource-preload',
'resource-preload-alias-Main.view': 'fixture/resource-preload/Main.view'
// Note: mapping doesn't handle subtypes like '.view', to be compliant with the AMD spec!
}
});
assert.strictEqual(
sap.ui.loader._.getModuleContent('resource-preload-alias/Main.view.xml'),
this.EXPECTED_VIEW_CONTENT,
"reading a preloaded non-JS resource by a prefixed-mapped module ID should return the expected text result");
assert.strictEqual(
sap.ui.loader._.getModuleContent('resource-preload-alias-Main.view.xml'),
this.EXPECTED_VIEW_CONTENT,
"reading a preloaded non-JS resource by a name-mapped module ID should return the expected text result");
});

QUnit.test("Access empty resource via url", function(assert) {
assert.strictEqual(
sap.ui.loader._.getModuleContent(undefined, sap.ui.require.toUrl('fixture/resource-preload/i18n.properties')),
"",
"reading a preloaded empty resource via url should return the expected text result");
});



// ========================================================================================
// Non-JS Resources (_getModuleContent)
// ========================================================================================

QUnit.module("guessResourceName", {
before: function() {
this.sExistingResourceName = 'fixture/guessResourceName/Existing.view.xml';
this.sNonExistingResourceName = 'fixture/guessResourceName/NonExisting.view.xml';
sap.ui.require.preload({
'fixture/guessResourceName/Existing.view.xml': '<mvc:View xmlns:mvc="sap.ui.core.mvc"></mvc:View>'
});
}
});

QUnit.test("preloaded resource with bLoadedResourcesOnly:true", function(assert) {
var sResource = this.sExistingResourceName;
var sUrl = sap.ui.require.toUrl(sResource);

// act
var sGuess = sap.ui.loader._.guessResourceName(sUrl, true);

// assert
assert.strictEqual(sGuess, sResource, "guess should return the expected name");
});

QUnit.test("preloaded resource with bLoadedResourcesOnly:false", function(assert) {
var sResource = this.sExistingResourceName;
var sUrl = sap.ui.require.toUrl(sResource);

// act
var sGuess = sap.ui.loader._.guessResourceName(sUrl, false);

// assert
assert.strictEqual(sGuess, sResource, "guess should return the expected name");
});

QUnit.test("preloaded resource with bLoadedResourcesOnly:true", function(assert) {
var sResource = this.sNonExistingResourceName;
var sUrl = sap.ui.require.toUrl(sResource);

// act
var sGuess = sap.ui.loader._.guessResourceName(sUrl, true);

// assert
assert.strictEqual(sGuess, undefined, "guess should return undefined");
});

QUnit.test("preloaded resource with bLoadedResourcesOnly:false", function(assert) {
var sResource = this.sNonExistingResourceName;
var sUrl = sap.ui.require.toUrl(sResource);

// act
var sGuess = sap.ui.loader._.guessResourceName(sUrl, false);

// assert
assert.strictEqual(sGuess, sResource, "guess should return the expected name");
});

QUnit.test("multiple matching mappings", function(assert) {
sap.ui.loader.config({
paths: {
"fixture/alternative1": "./",
"fixture/alternative2": "./foo/"
}
});
var sUrl = "./foo/Main.view.xml";

// act
var sGuess = sap.ui.loader._.guessResourceName(sUrl, false);

// assert
assert.ok(sGuess === "fixture/alternative1/foo/Main.view.xml" || sGuess === "fixture/alternative2/Main.view.xml",
"guess should return one of the expected names (returned: " + sGuess + ")");
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<meta charset="utf-8">
<base href="../../../../../../">
<title>TestSuite for sap.ui.core: GTP testcase CORE/LOADER</title>
<script src="resources/sap/ui/test/starter/createSuite.js"
data-sap-ui-testsuite="test-resources/sap/ui/core/qunit/loader/testsuite.loader.qunit"></script>
<script src="resources/sap/ui/test/starter/createSuite.js"></script>
</head>
<body>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ sap.ui.define(function() {
page: "test-resources/sap/ui/core/qunit/loader/exposeAsAMDLoader.qunit.html?sap-ui-debug=true",
title: "Test Page for ui5loader config option 'amd' with activated debug mode"
},
privateAPIs: {
qunit: {
seed: Math.random()
},
bootCore: false
},
/*
* syncMode.qunit.html is still an HTML page of its own as it tests sync loading.
* The generic starter Test.qunit.html only supports async loading.
Expand Down
Loading

0 comments on commit b8569e7

Please sign in to comment.