Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mebjas committed Aug 30, 2020
1 parent 8aa06c8 commit 00da52e
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 219 deletions.
192 changes: 96 additions & 96 deletions html5-qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,106 +316,11 @@ class Html5Qrcode {
}
});
}

//#region private method to create correct camera selection filter.
const createVideoConstraints = cameraIdOrConfig => {
if (typeof cameraIdOrConfig == "string") {
// If it's a string it should be camera device Id.
return { deviceId: { exact: cameraIdOrConfig } };
} else if (typeof cameraIdOrConfig == "object") {
const facingModeKey = "facingMode";
const deviceIdKey = "deviceId";
const allowedFacingModeValues
= { "user" : true, "environment" : true};
const exactKey = "exact";
const isValidFacingModeValue = value => {
if (value in allowedFacingModeValues) {
// Valid config
return true;
} else {
// Invalid config
throw "config has invalid 'facingMode' value = "
+ `'${value}'`;
}
};

const keys = Object.keys(cameraIdOrConfig);
if (keys.length != 1) {
throw "'cameraIdOrConfig' object should have exactly 1 key,"
+ ` if passed as an object, found ${keys.length} keys`;
}

const key = Object.keys(cameraIdOrConfig)[0];
if (key != facingModeKey && key != deviceIdKey) {
throw `Only '${facingModeKey}' and '${deviceIdKey}' `
+ " are supported for 'cameraIdOrConfig'";
}

if (key == facingModeKey) {
/**
* Supported scenarios:
* - { facingMode: "user" }
* - { facingMode: "environment" }
* - { facingMode: { exact: "environment" } }
* - { facingMode: { exact: "user" } }
*/
const facingMode = cameraIdOrConfig[key];
if (typeof facingMode == "string") {
if (isValidFacingModeValue(facingMode)) {
return { facingMode: facingMode };
}
} else if (typeof facingMode == "object") {
if (exactKey in facingMode) {
if (isValidFacingModeValue(facingMode[exactKey])) {
return {
facingMode: {
exact: facingMode[exactKey]
}
};
}
} else {
throw "'facingMode' should be string or object with"
+ ` ${exactKey} as key.`;
}
} else {
const type = (typeof facingMode);
throw `Invalid type of 'facingMode' = ${type}`;
}
} else {
/**
* key == deviceIdKey; Supported scenarios:
* - { deviceId: { exact: "a76afe74e95e3.....38627b3bde" }
* - { deviceId: "a76afe74e95e3....065c9cd89438627b3bde" }
*/
const deviceId = cameraIdOrConfig[key];
if (typeof deviceId == "string") {
return { deviceId: deviceId };
} else if (typeof deviceId == "object") {
if (exactKey in deviceId) {
return {
deviceId : { exact: deviceId[exactKey] }
};
} else {
throw "'deviceId' should be string or object with"
+ ` ${exactKey} as key.`;
}
} else {
const type = (typeof deviceId);
throw `Invalid type of 'deviceId' = ${type}`;
}
}
} else {
// invalid type
const type = (typeof cameraIdOrConfig);
throw `Invalid type of 'cameraIdOrConfig' = ${type}`;
}
}
//#endregion
//#endregion

return new Promise((resolve, reject) => {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
const videoConstraints = createVideoConstraints(
const videoConstraints = $this._createVideoConstraints(
cameraIdOrConfig);
navigator.mediaDevices.getUserMedia(
{
Expand Down Expand Up @@ -963,6 +868,101 @@ class Html5Qrcode {
}
}

//#region private method to create correct camera selection filter.
_createVideoConstraints(cameraIdOrConfig) {
if (typeof cameraIdOrConfig == "string") {
// If it's a string it should be camera device Id.
return { deviceId: { exact: cameraIdOrConfig } };
} else if (typeof cameraIdOrConfig == "object") {
const facingModeKey = "facingMode";
const deviceIdKey = "deviceId";
const allowedFacingModeValues
= { "user" : true, "environment" : true};
const exactKey = "exact";
const isValidFacingModeValue = value => {
if (value in allowedFacingModeValues) {
// Valid config
return true;
} else {
// Invalid config
throw "config has invalid 'facingMode' value = "
+ `'${value}'`;
}
};

const keys = Object.keys(cameraIdOrConfig);
if (keys.length != 1) {
throw "'cameraIdOrConfig' object should have exactly 1 key,"
+ ` if passed as an object, found ${keys.length} keys`;
}

const key = Object.keys(cameraIdOrConfig)[0];
if (key != facingModeKey && key != deviceIdKey) {
throw `Only '${facingModeKey}' and '${deviceIdKey}' `
+ " are supported for 'cameraIdOrConfig'";
}

if (key == facingModeKey) {
/**
* Supported scenarios:
* - { facingMode: "user" }
* - { facingMode: "environment" }
* - { facingMode: { exact: "environment" } }
* - { facingMode: { exact: "user" } }
*/
const facingMode = cameraIdOrConfig[key];
if (typeof facingMode == "string") {
if (isValidFacingModeValue(facingMode)) {
return { facingMode: facingMode };
}
} else if (typeof facingMode == "object") {
if (exactKey in facingMode) {
if (isValidFacingModeValue(facingMode[exactKey])) {
return {
facingMode: {
exact: facingMode[exactKey]
}
};
}
} else {
throw "'facingMode' should be string or object with"
+ ` ${exactKey} as key.`;
}
} else {
const type = (typeof facingMode);
throw `Invalid type of 'facingMode' = ${type}`;
}
} else {
/**
* key == deviceIdKey; Supported scenarios:
* - { deviceId: { exact: "a76afe74e95e3.....38627b3bde" }
* - { deviceId: "a76afe74e95e3....065c9cd89438627b3bde" }
*/
const deviceId = cameraIdOrConfig[key];
if (typeof deviceId == "string") {
return { deviceId: deviceId };
} else if (typeof deviceId == "object") {
if (exactKey in deviceId) {
return {
deviceId : { exact: deviceId[exactKey] }
};
} else {
throw "'deviceId' should be string or object with"
+ ` ${exactKey} as key.`;
}
} else {
const type = (typeof deviceId);
throw `Invalid type of 'deviceId' = ${type}`;
}
}
} else {
// invalid type
const type = (typeof cameraIdOrConfig);
throw `Invalid type of 'cameraIdOrConfig' = ${type}`;
}
}
//#endregion

static _getTimeoutFps(fps) {
return 1000 / fps;
}
Expand Down
2 changes: 1 addition & 1 deletion minified/html5-qrcode.min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
"@babel/plugin-proposal-class-properties": "^7.10.4",
"@babel/preset-env": "^7.11.0",
"babel-minify": "^0.5.1",
"chai": "^4.2.0",
"mocha": "^7.2.0",
"mocha-phantomjs": "^4.1.0",
"phantomjs": "^2.1.7",
"promise-polyfill": "^8.1.3",
"rewire": "^5.0.0"
},
"files": [
Expand Down
13 changes: 9 additions & 4 deletions test/test.html5-qrcode.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8" />
<title>Mocha Tests</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>
Expand All @@ -13,16 +13,21 @@
<!-- TODO(mebjas): Can this be hidden? -->
<div id="qr"></div>

<!-- Polyfills -->
<script src="../node_modules/promise-polyfill/dist/polyfill.min.js"></script>

<!-- actual library code -->
<script src="../third_party/qrcode.min.js"></script>
<script src="../transpiled/html5-qrcode.js"></script>

<!-- test dependencies -->
<script src="https://unpkg.com/chai/chai.js"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>
<script class="mocha-init">
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/chai/chai.js"></script>
<script>
mocha.setup('bdd');
mocha.reporter('html');
mocha.checkLeaks();
const expect = chai.expect;
const assert = chai.assert;
</script>

Expand Down
Loading

0 comments on commit 00da52e

Please sign in to comment.