mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-22 04:34:44 +00:00
Improve tests when running outside of our test runner
We often verify the correctness of tests by loading them in an external browser, but some tests just don't run the same/correctly. For example, we used to hard- code the http://127.0.0.1:9582/ origin, but that would cause tests to fail if running from a different origin. This commit _begins_ the work of improving this. It introduces a testing.ORIGIN, testing.BASE_URL and testing.HOST which will work correctly in both our runner and an external browser. It also introduces `testing.IS_TEST_RUNNER` boolean flag so that tests which have no chance of working in an external browser (e.g. screen.width) can be skipped. The goal is to reduce/remove tests which fail in external browsers so that such failures aren't quickly written off as "just how it is".
This commit is contained in:
@@ -12,7 +12,7 @@
|
|||||||
testing.expectEqual(10, document.childNodes[0].nodeType);
|
testing.expectEqual(10, document.childNodes[0].nodeType);
|
||||||
testing.expectEqual(null, document.parentNode);
|
testing.expectEqual(null, document.parentNode);
|
||||||
testing.expectEqual(undefined, document.getCurrentScript);
|
testing.expectEqual(undefined, document.getCurrentScript);
|
||||||
testing.expectEqual("http://127.0.0.1:9582/src/browser/tests/document/document.html", document.URL);
|
testing.expectEqual(testing.BASE_URL + 'document/document.html', document.URL);
|
||||||
testing.expectEqual(window, document.defaultView);
|
testing.expectEqual(window, document.defaultView);
|
||||||
testing.expectEqual(false, document.hidden);
|
testing.expectEqual(false, document.hidden);
|
||||||
testing.expectEqual("visible", document.visibilityState);
|
testing.expectEqual("visible", document.visibilityState);
|
||||||
@@ -57,7 +57,7 @@
|
|||||||
testing.expectEqual('CSS1Compat', document.compatMode);
|
testing.expectEqual('CSS1Compat', document.compatMode);
|
||||||
testing.expectEqual(document.URL, document.documentURI);
|
testing.expectEqual(document.URL, document.documentURI);
|
||||||
testing.expectEqual('', document.referrer);
|
testing.expectEqual('', document.referrer);
|
||||||
testing.expectEqual('127.0.0.1', document.domain);
|
testing.expectEqual(testing.HOST, document.domain);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script id=programmatic_document_metadata>
|
<script id=programmatic_document_metadata>
|
||||||
@@ -70,7 +70,7 @@
|
|||||||
testing.expectEqual('CSS1Compat', doc.compatMode);
|
testing.expectEqual('CSS1Compat', doc.compatMode);
|
||||||
testing.expectEqual('', doc.referrer);
|
testing.expectEqual('', doc.referrer);
|
||||||
// Programmatic document should have empty domain (no URL/origin)
|
// Programmatic document should have empty domain (no URL/origin)
|
||||||
testing.expectEqual('127.0.0.1', doc.domain);
|
testing.expectEqual(testing.HOST, doc.domain);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Test anchors and links -->
|
<!-- Test anchors and links -->
|
||||||
|
|||||||
@@ -11,11 +11,11 @@
|
|||||||
<script id=empty_href>
|
<script id=empty_href>
|
||||||
testing.expectEqual('', $('#a0').href);
|
testing.expectEqual('', $('#a0').href);
|
||||||
|
|
||||||
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/element/anchor1.html', $('#a1').href);
|
testing.expectEqual(testing.BASE_URL + 'element/anchor1.html', $('#a1').href);
|
||||||
testing.expectEqual('http://127.0.0.1:9582/hello/world/anchor2.html', $('#a2').href);
|
testing.expectEqual(testing.ORIGIN + 'hello/world/anchor2.html', $('#a2').href);
|
||||||
testing.expectEqual('https://www.openmymind.net/Elixirs-With-Statement/', $('#a3').href);
|
testing.expectEqual('https://www.openmymind.net/Elixirs-With-Statement/', $('#a3').href);
|
||||||
|
|
||||||
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/element/html/foo', $('#link').href);
|
testing.expectEqual(testing.BASE_URL + 'element/html/foo', $('#link').href);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script id=dynamic_anchor_defaults>
|
<script id=dynamic_anchor_defaults>
|
||||||
@@ -129,7 +129,7 @@
|
|||||||
testing.expectEqual('https://foo.bar/?q=bar#frag', link.href);
|
testing.expectEqual('https://foo.bar/?q=bar#frag', link.href);
|
||||||
|
|
||||||
link.href = 'foo';
|
link.href = 'foo';
|
||||||
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/element/html/foo', link.href);
|
testing.expectEqual(testing.BASE_URL + 'element/html/foo', link.href);
|
||||||
|
|
||||||
testing.expectEqual('', link.type);
|
testing.expectEqual('', link.type);
|
||||||
link.type = 'text/html';
|
link.type = 'text/html';
|
||||||
|
|||||||
@@ -32,12 +32,12 @@
|
|||||||
|
|
||||||
img.src = 'test.png';
|
img.src = 'test.png';
|
||||||
// src property returns resolved absolute URL
|
// src property returns resolved absolute URL
|
||||||
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/element/html/test.png', img.src);
|
testing.expectEqual(testing.BASE_URL + 'element/html/test.png', img.src);
|
||||||
// getAttribute returns the raw attribute value
|
// getAttribute returns the raw attribute value
|
||||||
testing.expectEqual('test.png', img.getAttribute('src'));
|
testing.expectEqual('test.png', img.getAttribute('src'));
|
||||||
|
|
||||||
img.src = '/absolute/path.png';
|
img.src = '/absolute/path.png';
|
||||||
testing.expectEqual('http://127.0.0.1:9582/absolute/path.png', img.src);
|
testing.expectEqual(testing.ORIGIN + 'absolute/path.png', img.src);
|
||||||
testing.expectEqual('/absolute/path.png', img.getAttribute('src'));
|
testing.expectEqual('/absolute/path.png', img.getAttribute('src'));
|
||||||
|
|
||||||
img.src = 'https://example.com/image.png';
|
img.src = 'https://example.com/image.png';
|
||||||
|
|||||||
@@ -57,9 +57,9 @@
|
|||||||
|
|
||||||
testing.expectEqual('', input.src);
|
testing.expectEqual('', input.src);
|
||||||
input.src = 'foo'
|
input.src = 'foo'
|
||||||
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/element/html/foo', input.src);
|
testing.expectEqual(testing.BASE_URL + 'element/html/foo', input.src);
|
||||||
input.src = '-3'
|
input.src = '-3'
|
||||||
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/element/html/-3', input.src);
|
testing.expectEqual(testing.BASE_URL + 'element/html/-3', input.src);
|
||||||
input.src = ''
|
input.src = ''
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
testing.expectEqual('https://lightpanda.io/opensource-browser/15', l2.href);
|
testing.expectEqual('https://lightpanda.io/opensource-browser/15', l2.href);
|
||||||
|
|
||||||
l2.href = '/over/9000';
|
l2.href = '/over/9000';
|
||||||
testing.expectEqual('http://127.0.0.1:9582/over/9000', l2.href);
|
testing.expectEqual(testing.ORIGIN + 'over/9000', l2.href);
|
||||||
|
|
||||||
l2.crossOrigin = 'nope';
|
l2.crossOrigin = 'nope';
|
||||||
testing.expectEqual('anonymous', l2.crossOrigin);
|
testing.expectEqual('anonymous', l2.crossOrigin);
|
||||||
|
|||||||
@@ -238,7 +238,7 @@
|
|||||||
testing.expectEqual('', audio.src);
|
testing.expectEqual('', audio.src);
|
||||||
|
|
||||||
audio.src = 'test.mp3';
|
audio.src = 'test.mp3';
|
||||||
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/element/html/test.mp3', audio.src);
|
testing.expectEqual(testing.BASE_URL + 'element/html/test.mp3', audio.src);
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -248,7 +248,7 @@
|
|||||||
testing.expectEqual('', video.poster);
|
testing.expectEqual('', video.poster);
|
||||||
|
|
||||||
video.poster = 'poster.jpg';
|
video.poster = 'poster.jpg';
|
||||||
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/element/html/poster.jpg', video.poster);
|
testing.expectEqual(testing.BASE_URL + 'element/html/poster.jpg', video.poster);
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
let s = document.createElement('script');
|
let s = document.createElement('script');
|
||||||
testing.expectEqual('', s.src);
|
testing.expectEqual('', s.src);
|
||||||
|
|
||||||
s.src = '/over.9000.js';
|
s.src = 'over.9000.js';
|
||||||
testing.expectEqual('http://127.0.0.1:9582/over.9000.js', s.src);
|
testing.expectEqual(testing.BASE_URL + 'element/html/script/over.9000.js', s.src);
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -45,7 +45,6 @@
|
|||||||
testing.expectEqual('hi', $('#link').innerText);
|
testing.expectEqual('hi', $('#link').innerText);
|
||||||
|
|
||||||
d1.innerHTML = '';
|
d1.innerHTML = '';
|
||||||
testing.todo(null, $('#link'));
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script id=attributeSerialization>
|
<script id=attributeSerialization>
|
||||||
|
|||||||
@@ -111,7 +111,7 @@
|
|||||||
<script id=legacy>
|
<script id=legacy>
|
||||||
{
|
{
|
||||||
let request = new Request("flower.png");
|
let request = new Request("flower.png");
|
||||||
testing.expectEqual("http://127.0.0.1:9582/src/browser/tests/net/flower.png", request.url);
|
testing.expectEqual(testing.BASE_URL + 'net/flower.png', request.url);
|
||||||
testing.expectEqual("GET", request.method);
|
testing.expectEqual("GET", request.method);
|
||||||
|
|
||||||
let request2 = new Request("https://google.com", {
|
let request2 = new Request("https://google.com", {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<a href="foo" id="foo">foo</a>
|
<a href="foo" id="foo">foo</a>
|
||||||
|
|
||||||
<script id=baseURI>
|
<script id=baseURI>
|
||||||
testing.expectEqual("http://127.0.0.1:9582/src/browser/tests/node/base_uri.html", document.URL);
|
testing.expectEqual(testing.BASE_URL + 'node/base_uri.html', document.URL);
|
||||||
testing.expectEqual("https://example.com/", document.baseURI);
|
testing.expectEqual("https://example.com/", document.baseURI);
|
||||||
|
|
||||||
const link = $('#foo');
|
const link = $('#foo');
|
||||||
|
|||||||
@@ -99,6 +99,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// our test runner sets this to true
|
||||||
|
const IS_TEST_RUNNER = window._lightpanda_skip_auto_assert === true;
|
||||||
|
|
||||||
window.testing = {
|
window.testing = {
|
||||||
fail: fail,
|
fail: fail,
|
||||||
async: async,
|
async: async,
|
||||||
@@ -109,9 +112,24 @@
|
|||||||
expectError: expectError,
|
expectError: expectError,
|
||||||
withError: withError,
|
withError: withError,
|
||||||
eventually: eventually,
|
eventually: eventually,
|
||||||
todo: function(){},
|
IS_TEST_RUNNER: IS_TEST_RUNNER,
|
||||||
|
HOST: '127.0.0.1',
|
||||||
|
ORIGIN: 'http://127.0.0.1:9582/',
|
||||||
|
BASE_URL: 'http://127.0.0.1:9582/src/browser/tests/',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (!IS_TEST_RUNNER) {
|
||||||
|
// The page is running in a different browser. Probably a developer making sure
|
||||||
|
// a test is correct. There are a few tweaks we need to do to make this a
|
||||||
|
// seemless, namely around adapting paths/urls.
|
||||||
|
console.warn(`The page is not being executed in the test runner, certain behavior has been adjusted`);
|
||||||
|
window.testing.HOST = location.hostname;
|
||||||
|
window.testing.ORIGIN = location.origin + '/';
|
||||||
|
window.testing.BASE_URL = location.origin + '/src/browser/tests/';
|
||||||
|
window.addEventListener('load', testing.assertOk);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
window.$ = function(sel) {
|
window.$ = function(sel) {
|
||||||
return document.querySelector(sel);
|
return document.querySelector(sel);
|
||||||
}
|
}
|
||||||
@@ -220,8 +238,4 @@
|
|||||||
return val;
|
return val;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (window._lightpanda_skip_auto_assert !== true) {
|
|
||||||
window.addEventListener('load', testing.assertOk);
|
|
||||||
}
|
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -2,24 +2,24 @@
|
|||||||
<script src="../testing.js"></script>
|
<script src="../testing.js"></script>
|
||||||
|
|
||||||
<script id=location>
|
<script id=location>
|
||||||
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/window/location.html', window.location.href);
|
testing.expectEqual(testing.BASE_URL + 'window/location.html', window.location.href);
|
||||||
testing.expectEqual(document.location, window.location);
|
testing.expectEqual(document.location, window.location);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script id=location_hash>
|
<script id=location_hash>
|
||||||
location.hash = "";
|
location.hash = "";
|
||||||
testing.expectEqual("", location.hash);
|
testing.expectEqual("", location.hash);
|
||||||
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/window/location.html', location.href);
|
testing.expectEqual(testing.BASE_URL + 'window/location.html', location.href);
|
||||||
|
|
||||||
location.hash = "#abcdef";
|
location.hash = "#abcdef";
|
||||||
testing.expectEqual("#abcdef", location.hash);
|
testing.expectEqual("#abcdef", location.hash);
|
||||||
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/window/location.html#abcdef', location.href);
|
testing.expectEqual(testing.BASE_URL + 'window/location.html#abcdef', location.href);
|
||||||
|
|
||||||
location.hash = "xyzxyz";
|
location.hash = "xyzxyz";
|
||||||
testing.expectEqual("#xyzxyz", location.hash);
|
testing.expectEqual("#xyzxyz", location.hash);
|
||||||
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/window/location.html#xyzxyz', location.href);
|
testing.expectEqual(testing.BASE_URL + 'window/location.html#xyzxyz', location.href);
|
||||||
|
|
||||||
location.hash = "";
|
location.hash = "";
|
||||||
testing.expectEqual("", location.hash);
|
testing.expectEqual("", location.hash);
|
||||||
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/window/location.html', location.href);
|
testing.expectEqual(testing.BASE_URL + 'window/location.html', location.href);
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -3,8 +3,10 @@
|
|||||||
|
|
||||||
<script id=screen>
|
<script id=screen>
|
||||||
let screen = window.screen;
|
let screen = window.screen;
|
||||||
testing.expectEqual(1920, screen.width);
|
if (testing.IS_TEST_RUNNER) {
|
||||||
testing.expectEqual(1080, screen.height);
|
testing.expectEqual(1920, screen.width);
|
||||||
|
testing.expectEqual(1080, screen.height);
|
||||||
|
}
|
||||||
|
|
||||||
let orientation = screen.orientation;
|
let orientation = screen.orientation;
|
||||||
testing.expectEqual(0, orientation.angle);
|
testing.expectEqual(0, orientation.angle);
|
||||||
|
|||||||
Reference in New Issue
Block a user