commit 4e1d102fe9f1c370ccaa98d7bb29b2b69823c9cc
parent dc0a5c3422c9b3b99248b118930dfa2c8fd4b022
Author: Andreas Pehrson <apehrson@mozilla.com>
Date: Thu, 23 Oct 2025 14:11:18 +0000
Bug 1771789 - Break out some helpers from MST-resizeMode.https.html. r=jib
Differential Revision: https://phabricator.services.mozilla.com/D266397
Diffstat:
3 files changed, 72 insertions(+), 68 deletions(-)
diff --git a/testing/web-platform/mozilla/tests/mediacapture-streams/MediaStreamTrack-resizeMode.https.html b/testing/web-platform/mozilla/tests/mediacapture-streams/MediaStreamTrack-resizeMode.https.html
@@ -6,77 +6,11 @@
<script src=/resources/testharnessreport.js></script>
<script src=/resources/testdriver.js></script>
<script src=/resources/testdriver-vendor.js></script>
+<script src=settings-helper.js></script>
+<script src=video-test-helper.js></script>
<script>
"use strict"
- async function test_framerate_between_exclusive(t, track, lower, upper) {
- const video = document.createElement("video");
- document.body.appendChild(video);
- t.add_cleanup(async () => document.body.removeChild(video));
-
- video.srcObject = new MediaStream([track]);
- await video.play();
-
- const numSeconds = 2;
- await new Promise(r => setTimeout(r, numSeconds * 1000));
- const totalVideoFrames = video.mozPaintedFrames;
- assert_between_exclusive(totalVideoFrames / numSeconds, lower, upper, "totalVideoFrames");
- }
-
- function createSettingsDicts(width, height, step = 1) {
- const settingsDicts = [], aspect = width / height;
- do {
- settingsDicts.push({ width, height });
- if (width > height) {
- height = Math.round((width - step) / aspect);
- width -= step;
- } else {
- width = Math.round((height - step) * aspect);
- height -= step;
- }
- } while (width > 2 && height > 2);
- return settingsDicts;
- }
-
- function integerFitness(actual, ideal) {
- if (actual == ideal) {
- return 0;
- }
- return Math.abs(actual - ideal) / Math.max(Math.abs(actual), Math.abs(ideal));
- }
-
- function findFittestResolutionSetting(width, height, constraints) {
- const widthIsNumber = typeof constraints.width == "number";
- const heightIsNumber = typeof constraints.height == "number";
- const c = {
- width: {
- ideal: widthIsNumber ? constraints.width : constraints?.width?.ideal,
- max: constraints?.width?.max ?? 1000000,
- },
- height: {
- ideal: heightIsNumber ? constraints.height : constraints?.height?.ideal,
- max: constraints?.height?.max ?? 1000000,
- },
- };
- const dicts = createSettingsDicts(width, height)
- .filter(s => s.width <= c.width.max && s.height <= c.height.max);
- for (const dict of dicts) {
- dict.distance =
- integerFitness(dict.width, c.width.ideal) +
- integerFitness(dict.height, c.height.ideal);
- }
-
- const filteredDicts = dicts.filter(s => {
- return (!c.width.ideal || s.width <= c.width.ideal) &&
- (!c.height.ideal || s.height <= c.height.ideal);
- });
-
- return filteredDicts.reduce(
- (a, b) => (a.distance < b.distance ? a : b),
- filteredDicts[0],
- );
- }
-
// Native capabilities supported by the fake camera.
const nativeLow = {width: 640, height: 480, frameRate: 30, resizeMode: "none"};
const nativeHigh = {width: 1280, height: 720, frameRate: 10, resizeMode: "none"};
diff --git a/testing/web-platform/mozilla/tests/mediacapture-streams/settings-helper.js b/testing/web-platform/mozilla/tests/mediacapture-streams/settings-helper.js
@@ -0,0 +1,54 @@
+
+function createSettingsDicts(width, height, step = 1) {
+ const settingsDicts = [], aspect = width / height;
+ do {
+ settingsDicts.push({ width, height });
+ if (width > height) {
+ height = Math.round((width - step) / aspect);
+ width -= step;
+ } else {
+ width = Math.round((height - step) * aspect);
+ height -= step;
+ }
+ } while (width > 2 && height > 2);
+ return settingsDicts;
+}
+
+function integerFitness(actual, ideal) {
+ if (actual == ideal) {
+ return 0;
+ }
+ return Math.abs(actual - ideal) / Math.max(Math.abs(actual), Math.abs(ideal));
+}
+
+function findFittestResolutionSetting(width, height, constraints) {
+ const widthIsNumber = typeof constraints.width == "number";
+ const heightIsNumber = typeof constraints.height == "number";
+ const c = {
+ width: {
+ ideal: widthIsNumber ? constraints.width : constraints?.width?.ideal,
+ max: constraints?.width?.max ?? 1000000,
+ },
+ height: {
+ ideal: heightIsNumber ? constraints.height : constraints?.height?.ideal,
+ max: constraints?.height?.max ?? 1000000,
+ },
+ };
+ const dicts = createSettingsDicts(width, height)
+ .filter(s => s.width <= c.width.max && s.height <= c.height.max);
+ for (const dict of dicts) {
+ dict.distance =
+ integerFitness(dict.width, c.width.ideal) +
+ integerFitness(dict.height, c.height.ideal);
+ }
+
+ const filteredDicts = dicts.filter(s => {
+ return (!c.width.ideal || s.width <= c.width.ideal) &&
+ (!c.height.ideal || s.height <= c.height.ideal);
+ });
+
+ return filteredDicts.reduce(
+ (a, b) => (a.distance < b.distance ? a : b),
+ filteredDicts[0],
+ );
+}
diff --git a/testing/web-platform/mozilla/tests/mediacapture-streams/video-test-helper.js b/testing/web-platform/mozilla/tests/mediacapture-streams/video-test-helper.js
@@ -0,0 +1,16 @@
+// Helper functions checking video flow using HTMLVideoElement.
+
+async function test_framerate_between_exclusive(t, track, lower, upper) {
+ const video = document.createElement("video");
+ document.body.appendChild(video);
+ video.style = "width:320px;height:240px;"
+ t.add_cleanup(async () => document.body.removeChild(video));
+
+ video.srcObject = new MediaStream([track]);
+ await video.play();
+
+ const numSeconds = 2;
+ await new Promise(r => setTimeout(r, numSeconds * 1000));
+ const totalVideoFrames = video.mozPaintedFrames;
+ assert_between_exclusive(totalVideoFrames / numSeconds, lower, upper, "totalVideoFrames");
+}