gecko-dev/dom/media/test/test_imagecapture.html
Bryce Van Dyk 898975f341 Bug 1436523 - Update dom/media/test/ tests to better handle loopback + gUM device config. r=achronop
This changeset adds a gUM_support.js to dom/media/test/. This file provides
functions to setup prefs for loopback or fake device selection before gUM calls
are made. This is useful for configuring tests and providing an explicit point
of reference for settings, rather than the implicit ones provided by the
harness.

Updates tests so that the new helper functions are called before gUM. This
will result in loopback prefs being set if loopback device names are detected,
if not then fake devices will be used. This also removes the use of the fake
constraint in gUM calls.

Update touched tests to use some more modern JS. No behavioural changes were
made (except in minor cases, but functionality should be the same). These
changes are largely as follows:
- var -> let
- async is used in places where I felt it improved readability
- semicolons added to various event handler assignments

MozReview-Commit-ID: 1HuE8thBA6w

--HG--
extra : rebase_source : b866056b2821436cf34ea683421c200b4bb4e55f
2018-02-21 09:11:29 -05:00

149 lines
4.0 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1041393
-->
<head>
<meta charset="utf-8">
<title>ImageCapture tests</title>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="gUM_support.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1041393">ImageCapture tests</a>
<script type="application/javascript">
let repeat = 100;
let count;
// Check if the callback returns even no JS reference on it.
function gcTest(track) {
return new Promise(function(resolve, reject) {
count = 0;
let i;
let imageCapture;
for(i = 0; i < repeat; i++) {
imageCapture = new ImageCapture(track);
imageCapture.onphoto = function(blob) {
count++;
if (count == repeat) {
ok(true, "pass gc testing");
resolve(track);
}
};
imageCapture.onerror = function(error) {
ok(false, "takePhoto failure in gc testing");
reject();
};
imageCapture.takePhoto();
}
info("Call gc ");
SpecialPowers.gc();
});
}
// Continue calling takePhoto() in rapid succession.
function rapidTest(track) {
return new Promise(function(resolve, reject) {
let imageCapture = new ImageCapture(track);
imageCapture.onphoto = function(blob) {
count++;
if (count == repeat) {
ok(true, "pass raipd takePhoto() testing");
resolve(track);
}
};
imageCapture.onerror = function(error) {
ok(false, "takePhoto() failure in rapid testing");
reject();
};
count = 0;
let i;
for(i = 0; i < repeat; i++) {
imageCapture.takePhoto();
}
});
}
// Check if the blob is decodable.
function blobTest(track) {
return new Promise(function(resolve, reject) {
let imageCapture = new ImageCapture(track);
imageCapture.onphoto = function(blob) {
let img = new Image();
img.onerror = function() {
ok(false, "fail to decode blob");
reject();
};
img.onload = function() {
ok(true, "decode blob success");
resolve(track);
};
img.src = URL.createObjectURL(blob.data);
};
imageCapture.onerror = function(error) {
ok(false, "fail to capture image");
};
imageCapture.takePhoto();
});
}
// It should return an error event after disabling video track.
function trackTest(track) {
return new Promise(function(resolve, reject) {
let imageCapture = new ImageCapture(track);
imageCapture.onphoto = function(blob) {
ok(false, "expect error when video track is disable");
reject();
};
imageCapture.onerror = function(error) {
ok(error.imageCaptureError.code == error.imageCaptureError.PHOTO_ERROR, "error code is PHOTO_ERROR")
track.enabled = true;
resolve(track);
};
track.enabled = false;
imageCapture.takePhoto();
});
}
async function init() {
// use loopback camera if available, otherwise fake, MediaStreamGraph will be the backend of ImageCapture.
await setupGetUserMediaTestPrefs();
let stream = await navigator.mediaDevices.getUserMedia({video: true});
return stream.getVideoTracks()[0];
}
async function start() {
try {
let track = await init();
info("ImageCapture track disable test.");
track = await trackTest(track);
info("ImageCapture blob test.");
track = await blobTest(track);
info("ImageCapture rapid takePhoto() test.");
track = await rapidTest(track);
info("ImageCapture multiple instances test.");
await gcTest(track);
} catch (e) {
ok(false, "Unexpected error during test: " + e);
} finally {
SimpleTest.finish();
}
}
SimpleTest.requestCompleteLog();
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [["dom.imagecapture.enabled", true],
["media.navigator.permission.disabled", true]
]}, start);
</script>
</pre>
</body>
</html>