From 1d06ea31976d0def9c0c3e6fc6824e9dd5da9e03 Mon Sep 17 00:00:00 2001 From: sel-en-ium Date: Fri, 2 Aug 2019 07:35:45 -0600 Subject: [PATCH 001/166] Update cast library so that it doesn't conflict with the androidx libraries. Fixes issue#26 https://github.com/jellyfin/cordova-plugin-chromecast/issues/26 --- plugin.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin.xml b/plugin.xml index 4d0054c..64fe1b6 100644 --- a/plugin.xml +++ b/plugin.xml @@ -34,8 +34,8 @@ - - + + From 455c96870a4228e663efcef83b06f8b5dd49e1fd Mon Sep 17 00:00:00 2001 From: sel-en-ium Date: Fri, 2 Aug 2019 07:35:45 -0600 Subject: [PATCH 002/166] Update cast library so that it doesn't conflict with the androidx libraries. Fixes issue#26 https://github.com/jellyfin/cordova-plugin-chromecast/issues/26 --- README.md | 6 ------ plugin.xml | 4 ++-- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8d7a960..e59972e 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,6 @@ Add the plugin with the command below in your cordova project directory. cordova plugin add https://github.com/jellyfin/cordova-plugin-chromecast.git ``` -If you have NodeJS installed, the dependencies should be automatically copied. Otrherwise, you will need to import the following projects as Library Projects in order for this plugin to work. - -- `adt-bundle\sdk\extras\google\google_play_services\libproject\google-play-services_lib` -- `adt-bundle\sdk\extras\android\support\v7\appcompat` -- `adt-bundle\sdk\extras\android\support\v7\mediarouter` - ## Usage This project attempts to implement the official Google Cast SDK for Chrome within Cordova. We've made a lot of progress in making this possible, so check out the [offical documentation](https://developers.google.com/cast/docs/chrome_sender) for examples. diff --git a/plugin.xml b/plugin.xml index 4d0054c..64fe1b6 100644 --- a/plugin.xml +++ b/plugin.xml @@ -34,8 +34,8 @@ - - + + From 1e5ec66890eee80bfc738269d9bef8085be34aef Mon Sep 17 00:00:00 2001 From: sel-en-ium Date: Tue, 30 Jul 2019 23:11:48 -0600 Subject: [PATCH 003/166] Move js files to www to follow the standard-ish plugin format --- plugin.xml | 4 ++-- EventEmitter.js => www/EventEmitter.js | 0 chrome.cast.js => www/chrome.cast.js | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename EventEmitter.js => www/EventEmitter.js (100%) rename chrome.cast.js => www/chrome.cast.js (100%) diff --git a/plugin.xml b/plugin.xml index 64fe1b6..e87d36d 100644 --- a/plugin.xml +++ b/plugin.xml @@ -9,11 +9,11 @@ Cordova ChromeCast - + - + diff --git a/EventEmitter.js b/www/EventEmitter.js similarity index 100% rename from EventEmitter.js rename to www/EventEmitter.js diff --git a/chrome.cast.js b/www/chrome.cast.js similarity index 100% rename from chrome.cast.js rename to www/chrome.cast.js From 4f2d19db5487afa4fe695db72d20f1e600335769 Mon Sep 17 00:00:00 2001 From: sel-en-ium Date: Tue, 30 Jul 2019 23:26:16 -0600 Subject: [PATCH 004/166] Add eslint test (so that the code can have a unified [standard-ish] style) (Will satisfy the eslint test in another commit.) --- .eslintrc.yml | 10 + .gitignore | 1 + package-lock.json | 1510 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 15 +- 4 files changed, 1533 insertions(+), 3 deletions(-) create mode 100644 .eslintrc.yml create mode 100644 package-lock.json diff --git a/.eslintrc.yml b/.eslintrc.yml new file mode 100644 index 0000000..0cccb8c --- /dev/null +++ b/.eslintrc.yml @@ -0,0 +1,10 @@ +root: true +extends: semistandard +rules: + indent: + - error + - 4 + camelcase: off + padded-blocks: off + operator-linebreak: off + no-throw-literal: off \ No newline at end of file diff --git a/.gitignore b/.gitignore index ea84723..7589473 100644 --- a/.gitignore +++ b/.gitignore @@ -136,3 +136,4 @@ Temporary Items # Created by .ignore support plugin (hsz.mobi) node_modules + diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..4b2032e --- /dev/null +++ b/package-lock.json @@ -0,0 +1,1510 @@ +{ + "name": "cordova-plugin-chromecast", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "acorn": { + "version": "5.7.3", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-5.7.3.tgz", + "integrity": "sha512-T/zvzYRfbVojPWahDsE5evJdHb3oJoQfFbsrKM7w5Zcs++Tr257tia3BmMP8XYVjp1S9RZXQMh7gao96BlqZOw==", + "dev": true + }, + "acorn-jsx": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-3.0.1.tgz", + "integrity": "sha1-r9+UiPsezvyDSPb7IvRk4ypYs2s=", + "dev": true, + "requires": { + "acorn": "^3.0.4" + }, + "dependencies": { + "acorn": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-3.3.0.tgz", + "integrity": "sha1-ReN/s56No/JbruP/U2niu18iAXo=", + "dev": true + } + } + }, + "ajv": { + "version": "4.11.8", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-4.11.8.tgz", + "integrity": "sha1-gv+wKynmYq5TvcIK8VlHcGc5xTY=", + "dev": true, + "requires": { + "co": "^4.6.0", + "json-stable-stringify": "^1.0.1" + } + }, + "ajv-keywords": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-1.5.1.tgz", + "integrity": "sha1-MU3QpLM2j609/NxU7eYXG4htrzw=", + "dev": true + }, + "ansi-escapes": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-1.4.0.tgz", + "integrity": "sha1-06ioOzGapneTZisT52HHkRQiMG4=", + "dev": true + }, + "ansi-regex": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", + "dev": true + }, + "ansi-styles": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", + "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=", + "dev": true + }, + "argparse": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", + "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", + "dev": true, + "requires": { + "sprintf-js": "~1.0.2" + } + }, + "array-includes": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/array-includes/-/array-includes-3.0.3.tgz", + "integrity": "sha1-GEtI9i2S10UrsxsyMWXH+L0CJm0=", + "dev": true, + "requires": { + "define-properties": "^1.1.2", + "es-abstract": "^1.7.0" + } + }, + "babel-code-frame": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", + "integrity": "sha1-Y/1D99weO7fONZR9uP42mj9Yx0s=", + "dev": true, + "requires": { + "chalk": "^1.1.3", + "esutils": "^2.0.2", + "js-tokens": "^3.0.2" + } + }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "buffer-from": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", + "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==", + "dev": true + }, + "caller-path": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/caller-path/-/caller-path-0.1.0.tgz", + "integrity": "sha1-lAhe9jWB7NPaqSREqP6U6CV3dR8=", + "dev": true, + "requires": { + "callsites": "^0.2.0" + } + }, + "callsites": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/callsites/-/callsites-0.2.0.tgz", + "integrity": "sha1-r6uWJikQp/M8GaV3WCXGnzTjUMo=", + "dev": true + }, + "chalk": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", + "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", + "dev": true, + "requires": { + "ansi-styles": "^2.2.1", + "escape-string-regexp": "^1.0.2", + "has-ansi": "^2.0.0", + "strip-ansi": "^3.0.0", + "supports-color": "^2.0.0" + } + }, + "circular-json": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/circular-json/-/circular-json-0.3.3.tgz", + "integrity": "sha512-UZK3NBx2Mca+b5LsG7bY183pHWt5Y1xts4P3Pz7ENTwGVnJOUWbRb3ocjvX7hx9tq/yTAdclXm9sZ38gNuem4A==", + "dev": true + }, + "cli-cursor": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-1.0.2.tgz", + "integrity": "sha1-ZNo/fValRBLll5S9Ytw1KV6PKYc=", + "dev": true, + "requires": { + "restore-cursor": "^1.0.1" + } + }, + "cli-width": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/cli-width/-/cli-width-2.2.0.tgz", + "integrity": "sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk=", + "dev": true + }, + "co": { + "version": "4.6.0", + "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", + "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=", + "dev": true + }, + "code-point-at": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz", + "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=", + "dev": true + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true + }, + "concat-stream": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", + "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", + "dev": true, + "requires": { + "buffer-from": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^2.2.2", + "typedarray": "^0.0.6" + } + }, + "contains-path": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/contains-path/-/contains-path-0.1.0.tgz", + "integrity": "sha1-/ozxhP9mcLa67wGp1IYaXL7EEgo=", + "dev": true + }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", + "dev": true + }, + "d": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/d/-/d-1.0.1.tgz", + "integrity": "sha512-m62ShEObQ39CfralilEQRjH6oAMtNCV1xJyEx5LpRYUVN+EviphDgUc/F3hnYbADmkiNs67Y+3ylmlG7Lnu+FA==", + "dev": true, + "requires": { + "es5-ext": "^0.10.50", + "type": "^1.0.1" + } + }, + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "dev": true, + "requires": { + "ms": "2.0.0" + } + }, + "deep-is": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz", + "integrity": "sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ=", + "dev": true + }, + "define-properties": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz", + "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==", + "dev": true, + "requires": { + "object-keys": "^1.0.12" + } + }, + "doctrine": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-2.1.0.tgz", + "integrity": "sha512-35mSku4ZXK0vfCuHEDAwt55dg2jNajHZ1odvF+8SSr82EsZY4QmXfuWso8oEd8zRhVObSN18aM0CjSdoBX7zIw==", + "dev": true, + "requires": { + "esutils": "^2.0.2" + } + }, + "error-ex": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", + "integrity": "sha512-7dFHNmqeFSEt2ZBsCriorKnn3Z2pj+fd9kmI6QoWw4//DL+icEBfc0U7qJCisqrTsKTjw4fNFy2pW9OqStD84g==", + "dev": true, + "requires": { + "is-arrayish": "^0.2.1" + } + }, + "es-abstract": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.13.0.tgz", + "integrity": "sha512-vDZfg/ykNxQVwup/8E1BZhVzFfBxs9NqMzGcvIJrqg5k2/5Za2bWo40dK2J1pgLngZ7c+Shh8lwYtLGyrwPutg==", + "dev": true, + "requires": { + "es-to-primitive": "^1.2.0", + "function-bind": "^1.1.1", + "has": "^1.0.3", + "is-callable": "^1.1.4", + "is-regex": "^1.0.4", + "object-keys": "^1.0.12" + } + }, + "es-to-primitive": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.0.tgz", + "integrity": "sha512-qZryBOJjV//LaxLTV6UC//WewneB3LcXOL9NP++ozKVXsIIIpm/2c13UDiD9Jp2eThsecw9m3jPqDwTyobcdbg==", + "dev": true, + "requires": { + "is-callable": "^1.1.4", + "is-date-object": "^1.0.1", + "is-symbol": "^1.0.2" + } + }, + "es5-ext": { + "version": "0.10.50", + "resolved": "https://registry.npmjs.org/es5-ext/-/es5-ext-0.10.50.tgz", + "integrity": "sha512-KMzZTPBkeQV/JcSQhI5/z6d9VWJ3EnQ194USTUwIYZ2ZbpN8+SGXQKt1h68EX44+qt+Fzr8DO17vnxrw7c3agw==", + "dev": true, + "requires": { + "es6-iterator": "~2.0.3", + "es6-symbol": "~3.1.1", + "next-tick": "^1.0.0" + } + }, + "es6-iterator": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/es6-iterator/-/es6-iterator-2.0.3.tgz", + "integrity": "sha1-p96IkUGgWpSwhUQDstCg+/qY87c=", + "dev": true, + "requires": { + "d": "1", + "es5-ext": "^0.10.35", + "es6-symbol": "^3.1.1" + } + }, + "es6-map": { + "version": "0.1.5", + "resolved": "https://registry.npmjs.org/es6-map/-/es6-map-0.1.5.tgz", + "integrity": "sha1-kTbgUD3MBqMBaQ8LsU/042TpSfA=", + "dev": true, + "requires": { + "d": "1", + "es5-ext": "~0.10.14", + "es6-iterator": "~2.0.1", + "es6-set": "~0.1.5", + "es6-symbol": "~3.1.1", + "event-emitter": "~0.3.5" + } + }, + "es6-set": { + "version": "0.1.5", + "resolved": "https://registry.npmjs.org/es6-set/-/es6-set-0.1.5.tgz", + "integrity": "sha1-0rPsXU2ADO2BjbU40ol02wpzzLE=", + "dev": true, + "requires": { + "d": "1", + "es5-ext": "~0.10.14", + "es6-iterator": "~2.0.1", + "es6-symbol": "3.1.1", + "event-emitter": "~0.3.5" + } + }, + "es6-symbol": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/es6-symbol/-/es6-symbol-3.1.1.tgz", + "integrity": "sha1-vwDvT9q2uhtG7Le2KbTH7VcVzHc=", + "dev": true, + "requires": { + "d": "1", + "es5-ext": "~0.10.14" + } + }, + "es6-weak-map": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/es6-weak-map/-/es6-weak-map-2.0.3.tgz", + "integrity": "sha512-p5um32HOTO1kP+w7PRnB+5lQ43Z6muuMuIMffvDN8ZB4GcnjLBV6zGStpbASIMk4DCAvEaamhe2zhyCb/QXXsA==", + "dev": true, + "requires": { + "d": "1", + "es5-ext": "^0.10.46", + "es6-iterator": "^2.0.3", + "es6-symbol": "^3.1.1" + } + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true + }, + "escope": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/escope/-/escope-3.6.0.tgz", + "integrity": "sha1-4Bl16BJ4GhY6ba392AOY3GTIicM=", + "dev": true, + "requires": { + "es6-map": "^0.1.3", + "es6-weak-map": "^2.0.1", + "esrecurse": "^4.1.0", + "estraverse": "^4.1.1" + } + }, + "eslint": { + "version": "3.19.0", + "resolved": "https://registry.npmjs.org/eslint/-/eslint-3.19.0.tgz", + "integrity": "sha1-yPxiAcf0DdCJQbh8CFdnOGpnmsw=", + "dev": true, + "requires": { + "babel-code-frame": "^6.16.0", + "chalk": "^1.1.3", + "concat-stream": "^1.5.2", + "debug": "^2.1.1", + "doctrine": "^2.0.0", + "escope": "^3.6.0", + "espree": "^3.4.0", + "esquery": "^1.0.0", + "estraverse": "^4.2.0", + "esutils": "^2.0.2", + "file-entry-cache": "^2.0.0", + "glob": "^7.0.3", + "globals": "^9.14.0", + "ignore": "^3.2.0", + "imurmurhash": "^0.1.4", + "inquirer": "^0.12.0", + "is-my-json-valid": "^2.10.0", + "is-resolvable": "^1.0.0", + "js-yaml": "^3.5.1", + "json-stable-stringify": "^1.0.0", + "levn": "^0.3.0", + "lodash": "^4.0.0", + "mkdirp": "^0.5.0", + "natural-compare": "^1.4.0", + "optionator": "^0.8.2", + "path-is-inside": "^1.0.1", + "pluralize": "^1.2.1", + "progress": "^1.1.8", + "require-uncached": "^1.0.2", + "shelljs": "^0.7.5", + "strip-bom": "^3.0.0", + "strip-json-comments": "~2.0.1", + "table": "^3.7.8", + "text-table": "~0.2.0", + "user-home": "^2.0.0" + } + }, + "eslint-config-semistandard": { + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/eslint-config-semistandard/-/eslint-config-semistandard-11.0.0.tgz", + "integrity": "sha1-RO73z9/Uchnjp7gbkbVA6IC7JhU=", + "dev": true + }, + "eslint-config-standard": { + "version": "10.2.1", + "resolved": "https://registry.npmjs.org/eslint-config-standard/-/eslint-config-standard-10.2.1.tgz", + "integrity": "sha1-wGHk0GbzedwXzVYsZOgZtN1FRZE=", + "dev": true + }, + "eslint-import-resolver-node": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.2.tgz", + "integrity": "sha512-sfmTqJfPSizWu4aymbPr4Iidp5yKm8yDkHp+Ir3YiTHiiDfxh69mOUsmiqW6RZ9zRXFaF64GtYmN7e+8GHBv6Q==", + "dev": true, + "requires": { + "debug": "^2.6.9", + "resolve": "^1.5.0" + } + }, + "eslint-module-utils": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/eslint-module-utils/-/eslint-module-utils-2.4.1.tgz", + "integrity": "sha512-H6DOj+ejw7Tesdgbfs4jeS4YMFrT8uI8xwd1gtQqXssaR0EQ26L+2O/w6wkYFy2MymON0fTwHmXBvvfLNZVZEw==", + "dev": true, + "requires": { + "debug": "^2.6.8", + "pkg-dir": "^2.0.0" + } + }, + "eslint-plugin-import": { + "version": "2.18.2", + "resolved": "https://registry.npmjs.org/eslint-plugin-import/-/eslint-plugin-import-2.18.2.tgz", + "integrity": "sha512-5ohpsHAiUBRNaBWAF08izwUGlbrJoJJ+W9/TBwsGoR1MnlgfwMIKrFeSjWbt6moabiXW9xNvtFz+97KHRfI4HQ==", + "dev": true, + "requires": { + "array-includes": "^3.0.3", + "contains-path": "^0.1.0", + "debug": "^2.6.9", + "doctrine": "1.5.0", + "eslint-import-resolver-node": "^0.3.2", + "eslint-module-utils": "^2.4.0", + "has": "^1.0.3", + "minimatch": "^3.0.4", + "object.values": "^1.1.0", + "read-pkg-up": "^2.0.0", + "resolve": "^1.11.0" + }, + "dependencies": { + "doctrine": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-1.5.0.tgz", + "integrity": "sha1-N53Ocw9hZvds76TmcHoVmwLFpvo=", + "dev": true, + "requires": { + "esutils": "^2.0.2", + "isarray": "^1.0.0" + } + } + } + }, + "eslint-plugin-node": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/eslint-plugin-node/-/eslint-plugin-node-5.2.1.tgz", + "integrity": "sha512-xhPXrh0Vl/b7870uEbaumb2Q+LxaEcOQ3kS1jtIXanBAwpMre1l5q/l2l/hESYJGEFKuI78bp6Uw50hlpr7B+g==", + "dev": true, + "requires": { + "ignore": "^3.3.6", + "minimatch": "^3.0.4", + "resolve": "^1.3.3", + "semver": "5.3.0" + }, + "dependencies": { + "semver": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.3.0.tgz", + "integrity": "sha1-myzl094C0XxgEq0yaqa00M9U+U8=", + "dev": true + } + } + }, + "eslint-plugin-promise": { + "version": "3.8.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-3.8.0.tgz", + "integrity": "sha512-JiFL9UFR15NKpHyGii1ZcvmtIqa3UTwiDAGb8atSffe43qJ3+1czVGN6UtkklpcJ2DVnqvTMzEKRaJdBkAL2aQ==", + "dev": true + }, + "eslint-plugin-standard": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-standard/-/eslint-plugin-standard-3.1.0.tgz", + "integrity": "sha512-fVcdyuKRr0EZ4fjWl3c+gp1BANFJD1+RaWa2UPYfMZ6jCtp5RG00kSaXnK/dE5sYzt4kaWJ9qdxqUfc0d9kX0w==", + "dev": true + }, + "espree": { + "version": "3.5.4", + "resolved": "https://registry.npmjs.org/espree/-/espree-3.5.4.tgz", + "integrity": "sha512-yAcIQxtmMiB/jL32dzEp2enBeidsB7xWPLNiw3IIkpVds1P+h7qF9YwJq1yUNzp2OKXgAprs4F61ih66UsoD1A==", + "dev": true, + "requires": { + "acorn": "^5.5.0", + "acorn-jsx": "^3.0.0" + } + }, + "esprima": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", + "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==", + "dev": true + }, + "esquery": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/esquery/-/esquery-1.0.1.tgz", + "integrity": "sha512-SmiyZ5zIWH9VM+SRUReLS5Q8a7GxtRdxEBVZpm98rJM7Sb+A9DVCndXfkeFUd3byderg+EbDkfnevfCwynWaNA==", + "dev": true, + "requires": { + "estraverse": "^4.0.0" + } + }, + "esrecurse": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/esrecurse/-/esrecurse-4.2.1.tgz", + "integrity": "sha512-64RBB++fIOAXPw3P9cy89qfMlvZEXZkqqJkjqqXIvzP5ezRZjW+lPWjw35UX/3EhUPFYbg5ER4JYgDw4007/DQ==", + "dev": true, + "requires": { + "estraverse": "^4.1.0" + } + }, + "estraverse": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/estraverse/-/estraverse-4.2.0.tgz", + "integrity": "sha1-De4/7TH81GlhjOc0IJn8GvoL2xM=", + "dev": true + }, + "esutils": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz", + "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==", + "dev": true + }, + "event-emitter": { + "version": "0.3.5", + "resolved": "https://registry.npmjs.org/event-emitter/-/event-emitter-0.3.5.tgz", + "integrity": "sha1-34xp7vFkeSPHFXuc6DhAYQsCzDk=", + "dev": true, + "requires": { + "d": "1", + "es5-ext": "~0.10.14" + } + }, + "exit-hook": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/exit-hook/-/exit-hook-1.1.1.tgz", + "integrity": "sha1-8FyiM7SMBdVP/wd2XfhQfpXAL/g=", + "dev": true + }, + "fast-levenshtein": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz", + "integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=", + "dev": true + }, + "figures": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/figures/-/figures-1.7.0.tgz", + "integrity": "sha1-y+Hjr/zxzUS4DK3+0o3Hk6lwHS4=", + "dev": true, + "requires": { + "escape-string-regexp": "^1.0.5", + "object-assign": "^4.1.0" + } + }, + "file-entry-cache": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-2.0.0.tgz", + "integrity": "sha1-w5KZDD5oR4PYOLjISkXYoEhFg2E=", + "dev": true, + "requires": { + "flat-cache": "^1.2.1", + "object-assign": "^4.0.1" + } + }, + "find-up": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-2.1.0.tgz", + "integrity": "sha1-RdG35QbHF93UgndaK3eSCjwMV6c=", + "dev": true, + "requires": { + "locate-path": "^2.0.0" + } + }, + "flat-cache": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-1.3.4.tgz", + "integrity": "sha512-VwyB3Lkgacfik2vhqR4uv2rvebqmDvFu4jlN/C1RzWoJEo8I7z4Q404oiqYCkq41mni8EzQnm95emU9seckwtg==", + "dev": true, + "requires": { + "circular-json": "^0.3.1", + "graceful-fs": "^4.1.2", + "rimraf": "~2.6.2", + "write": "^0.2.1" + } + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true + }, + "function-bind": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", + "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", + "dev": true + }, + "generate-function": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz", + "integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==", + "dev": true, + "requires": { + "is-property": "^1.0.2" + } + }, + "generate-object-property": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/generate-object-property/-/generate-object-property-1.2.0.tgz", + "integrity": "sha1-nA4cQDCM6AT0eDYYuTf6iPmdUNA=", + "dev": true, + "requires": { + "is-property": "^1.0.0" + } + }, + "glob": { + "version": "7.1.4", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.4.tgz", + "integrity": "sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A==", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, + "globals": { + "version": "9.18.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-9.18.0.tgz", + "integrity": "sha512-S0nG3CLEQiY/ILxqtztTWH/3iRRdyBLw6KMDxnKMchrtbj2OFmehVh0WUCfW3DUrIgx/qFrJPICrq4Z4sTR9UQ==", + "dev": true + }, + "graceful-fs": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.0.tgz", + "integrity": "sha512-jpSvDPV4Cq/bgtpndIWbI5hmYxhQGHPC4d4cqBPb4DLniCfhJokdXhwhaDuLBGLQdvvRum/UiX6ECVIPvDXqdg==", + "dev": true + }, + "has": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", + "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", + "dev": true, + "requires": { + "function-bind": "^1.1.1" + } + }, + "has-ansi": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", + "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=", + "dev": true, + "requires": { + "ansi-regex": "^2.0.0" + } + }, + "has-symbols": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz", + "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=", + "dev": true + }, + "hosted-git-info": { + "version": "2.7.1", + "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.7.1.tgz", + "integrity": "sha512-7T/BxH19zbcCTa8XkMlbK5lTo1WtgkFi3GvdWEyNuc4Vex7/9Dqbnpsf4JMydcfj9HCg4zUWFTL3Za6lapg5/w==", + "dev": true + }, + "ignore": { + "version": "3.3.10", + "resolved": "https://registry.npmjs.org/ignore/-/ignore-3.3.10.tgz", + "integrity": "sha512-Pgs951kaMm5GXP7MOvxERINe3gsaVjUWFm+UZPSq9xYriQAksyhg0csnS0KXSNRD5NmNdapXEpjxG49+AKh/ug==", + "dev": true + }, + "imurmurhash": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", + "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=", + "dev": true + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true + }, + "inquirer": { + "version": "0.12.0", + "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-0.12.0.tgz", + "integrity": "sha1-HvK/1jUE3wvHV4X/+MLEHfEvB34=", + "dev": true, + "requires": { + "ansi-escapes": "^1.1.0", + "ansi-regex": "^2.0.0", + "chalk": "^1.0.0", + "cli-cursor": "^1.0.1", + "cli-width": "^2.0.0", + "figures": "^1.3.5", + "lodash": "^4.3.0", + "readline2": "^1.0.1", + "run-async": "^0.1.0", + "rx-lite": "^3.1.2", + "string-width": "^1.0.1", + "strip-ansi": "^3.0.0", + "through": "^2.3.6" + } + }, + "interpret": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.2.0.tgz", + "integrity": "sha512-mT34yGKMNceBQUoVn7iCDKDntA7SC6gycMAWzGx1z/CMCTV7b2AAtXlo3nRyHZ1FelRkQbQjprHSYGwzLtkVbw==", + "dev": true + }, + "is-arrayish": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz", + "integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=", + "dev": true + }, + "is-callable": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz", + "integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA==", + "dev": true + }, + "is-date-object": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz", + "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", + "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", + "dev": true, + "requires": { + "number-is-nan": "^1.0.0" + } + }, + "is-my-ip-valid": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-my-ip-valid/-/is-my-ip-valid-1.0.0.tgz", + "integrity": "sha512-gmh/eWXROncUzRnIa1Ubrt5b8ep/MGSnfAUI3aRp+sqTCs1tv1Isl8d8F6JmkN3dXKc3ehZMrtiPN9eL03NuaQ==", + "dev": true + }, + "is-my-json-valid": { + "version": "2.20.0", + "resolved": "https://registry.npmjs.org/is-my-json-valid/-/is-my-json-valid-2.20.0.tgz", + "integrity": "sha512-XTHBZSIIxNsIsZXg7XB5l8z/OBFosl1Wao4tXLpeC7eKU4Vm/kdop2azkPqULwnfGQjmeDIyey9g7afMMtdWAA==", + "dev": true, + "requires": { + "generate-function": "^2.0.0", + "generate-object-property": "^1.1.0", + "is-my-ip-valid": "^1.0.0", + "jsonpointer": "^4.0.0", + "xtend": "^4.0.0" + } + }, + "is-property": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", + "integrity": "sha1-V/4cTkhHTt1lsJkR8msc1Ald2oQ=", + "dev": true + }, + "is-regex": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", + "integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=", + "dev": true, + "requires": { + "has": "^1.0.1" + } + }, + "is-resolvable": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/is-resolvable/-/is-resolvable-1.1.0.tgz", + "integrity": "sha512-qgDYXFSR5WvEfuS5dMj6oTMEbrrSaM0CrFk2Yiq/gXnBvD9pMa2jGXxyhGLfvhZpuMZe18CJpFxAt3CRs42NMg==", + "dev": true + }, + "is-symbol": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.2.tgz", + "integrity": "sha512-HS8bZ9ox60yCJLH9snBpIwv9pYUAkcuLhSA1oero1UB5y9aiQpRA8y2ex945AOtCZL1lJDeIk3G5LthswI46Lw==", + "dev": true, + "requires": { + "has-symbols": "^1.0.0" + } + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", + "dev": true + }, + "js-tokens": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz", + "integrity": "sha1-mGbfOVECEw449/mWvOtlRDIJwls=", + "dev": true + }, + "js-yaml": { + "version": "3.13.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", + "integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==", + "dev": true, + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + }, + "json-stable-stringify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/json-stable-stringify/-/json-stable-stringify-1.0.1.tgz", + "integrity": "sha1-mnWdOcXy/1A/1TAGRu1EX4jE+a8=", + "dev": true, + "requires": { + "jsonify": "~0.0.0" + } + }, + "jsonify": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", + "integrity": "sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=", + "dev": true + }, + "jsonpointer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/jsonpointer/-/jsonpointer-4.0.1.tgz", + "integrity": "sha1-T9kss04OnbPInIYi7PUfm5eMbLk=", + "dev": true + }, + "levn": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/levn/-/levn-0.3.0.tgz", + "integrity": "sha1-OwmSTt+fCDwEkP3UwLxEIeBHZO4=", + "dev": true, + "requires": { + "prelude-ls": "~1.1.2", + "type-check": "~0.3.2" + } + }, + "load-json-file": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-2.0.0.tgz", + "integrity": "sha1-eUfkIUmvgNaWy/eXvKq8/h/inKg=", + "dev": true, + "requires": { + "graceful-fs": "^4.1.2", + "parse-json": "^2.2.0", + "pify": "^2.0.0", + "strip-bom": "^3.0.0" + } + }, + "locate-path": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-2.0.0.tgz", + "integrity": "sha1-K1aLJl7slExtnA3pw9u7ygNUzY4=", + "dev": true, + "requires": { + "p-locate": "^2.0.0", + "path-exists": "^3.0.0" + } + }, + "lodash": { + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", + "dev": true + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, + "requires": { + "minimist": "0.0.8" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + }, + "mute-stream": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.5.tgz", + "integrity": "sha1-j7+rsKmKJT0xhDMfno3rc3L6xsA=", + "dev": true + }, + "natural-compare": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", + "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", + "dev": true + }, + "next-tick": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.0.0.tgz", + "integrity": "sha1-yobR/ogoFpsBICCOPchCS524NCw=", + "dev": true + }, + "normalize-package-data": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.5.0.tgz", + "integrity": "sha512-/5CMN3T0R4XTj4DcGaexo+roZSdSFW/0AOOTROrjxzCG1wrWXEsGbRKevjlIL+ZDE4sZlJr5ED4YW0yqmkK+eA==", + "dev": true, + "requires": { + "hosted-git-info": "^2.1.4", + "resolve": "^1.10.0", + "semver": "2 || 3 || 4 || 5", + "validate-npm-package-license": "^3.0.1" + } + }, + "number-is-nan": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", + "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=", + "dev": true + }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=", + "dev": true + }, + "object-keys": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", + "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==", + "dev": true + }, + "object.values": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/object.values/-/object.values-1.1.0.tgz", + "integrity": "sha512-8mf0nKLAoFX6VlNVdhGj31SVYpaNFtUnuoOXWyFEstsWRgU837AK+JYM0iAxwkSzGRbwn8cbFmgbyxj1j4VbXg==", + "dev": true, + "requires": { + "define-properties": "^1.1.3", + "es-abstract": "^1.12.0", + "function-bind": "^1.1.1", + "has": "^1.0.3" + } + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, + "requires": { + "wrappy": "1" + } + }, + "onetime": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz", + "integrity": "sha1-ofeDj4MUxRbwXs78vEzP4EtO14k=", + "dev": true + }, + "optionator": { + "version": "0.8.2", + "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.8.2.tgz", + "integrity": "sha1-NkxeQJ0/TWMB1sC0wFu6UBgK62Q=", + "dev": true, + "requires": { + "deep-is": "~0.1.3", + "fast-levenshtein": "~2.0.4", + "levn": "~0.3.0", + "prelude-ls": "~1.1.2", + "type-check": "~0.3.2", + "wordwrap": "~1.0.0" + } + }, + "os-homedir": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz", + "integrity": "sha1-/7xJiDNuDoM94MFox+8VISGqf7M=", + "dev": true + }, + "p-limit": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-1.3.0.tgz", + "integrity": "sha512-vvcXsLAJ9Dr5rQOPk7toZQZJApBl2K4J6dANSsEuh6QI41JYcsS/qhTGa9ErIUUgK3WNQoJYvylxvjqmiqEA9Q==", + "dev": true, + "requires": { + "p-try": "^1.0.0" + } + }, + "p-locate": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-2.0.0.tgz", + "integrity": "sha1-IKAQOyIqcMj9OcwuWAaA893l7EM=", + "dev": true, + "requires": { + "p-limit": "^1.1.0" + } + }, + "p-try": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/p-try/-/p-try-1.0.0.tgz", + "integrity": "sha1-y8ec26+P1CKOE/Yh8rGiN8GyB7M=", + "dev": true + }, + "parse-json": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-2.2.0.tgz", + "integrity": "sha1-9ID0BDTvgHQfhGkJn43qGPVaTck=", + "dev": true, + "requires": { + "error-ex": "^1.2.0" + } + }, + "path-exists": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", + "dev": true + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true + }, + "path-is-inside": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/path-is-inside/-/path-is-inside-1.0.2.tgz", + "integrity": "sha1-NlQX3t5EQw0cEa9hAn+s8HS9/FM=", + "dev": true + }, + "path-parse": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", + "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", + "dev": true + }, + "path-type": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/path-type/-/path-type-2.0.0.tgz", + "integrity": "sha1-8BLMuEFbcJb8LaoQVMPXI4lZTHM=", + "dev": true, + "requires": { + "pify": "^2.0.0" + } + }, + "pify": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", + "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=", + "dev": true + }, + "pkg-dir": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-2.0.0.tgz", + "integrity": "sha1-9tXREJ4Z1j7fQo4L1X4Sd3YVM0s=", + "dev": true, + "requires": { + "find-up": "^2.1.0" + } + }, + "pluralize": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-1.2.1.tgz", + "integrity": "sha1-0aIUg/0iu0HlihL6NCGCMUCJfEU=", + "dev": true + }, + "prelude-ls": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz", + "integrity": "sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=", + "dev": true + }, + "process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", + "dev": true + }, + "progress": { + "version": "1.1.8", + "resolved": "https://registry.npmjs.org/progress/-/progress-1.1.8.tgz", + "integrity": "sha1-4mDHj2Fhzdmw5WzD4Khd4Xx6V74=", + "dev": true + }, + "read-pkg": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-2.0.0.tgz", + "integrity": "sha1-jvHAYjxqbbDcZxPEv6xGMysjaPg=", + "dev": true, + "requires": { + "load-json-file": "^2.0.0", + "normalize-package-data": "^2.3.2", + "path-type": "^2.0.0" + } + }, + "read-pkg-up": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/read-pkg-up/-/read-pkg-up-2.0.0.tgz", + "integrity": "sha1-a3KoBImE4MQeeVEP1en6mbO1Sb4=", + "dev": true, + "requires": { + "find-up": "^2.0.0", + "read-pkg": "^2.0.0" + } + }, + "readable-stream": { + "version": "2.3.6", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", + "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", + "dev": true, + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "readline2": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/readline2/-/readline2-1.0.1.tgz", + "integrity": "sha1-QQWWCP/BVHV7cV2ZidGZ/783LjU=", + "dev": true, + "requires": { + "code-point-at": "^1.0.0", + "is-fullwidth-code-point": "^1.0.0", + "mute-stream": "0.0.5" + } + }, + "rechoir": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", + "integrity": "sha1-hSBLVNuoLVdC4oyWdW70OvUOM4Q=", + "dev": true, + "requires": { + "resolve": "^1.1.6" + } + }, + "require-uncached": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/require-uncached/-/require-uncached-1.0.3.tgz", + "integrity": "sha1-Tg1W1slmL9MeQwEcS5WqSZVUIdM=", + "dev": true, + "requires": { + "caller-path": "^0.1.0", + "resolve-from": "^1.0.0" + } + }, + "resolve": { + "version": "1.11.1", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.11.1.tgz", + "integrity": "sha512-vIpgF6wfuJOZI7KKKSP+HmiKggadPQAdsp5HiC1mvqnfp0gF1vdwgBWZIdrVft9pgqoMFQN+R7BSWZiBxx+BBw==", + "dev": true, + "requires": { + "path-parse": "^1.0.6" + } + }, + "resolve-from": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-1.0.1.tgz", + "integrity": "sha1-Jsv+k10a7uq7Kbw/5a6wHpPUQiY=", + "dev": true + }, + "restore-cursor": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-1.0.1.tgz", + "integrity": "sha1-NGYfRohjJ/7SmRR5FSJS35LapUE=", + "dev": true, + "requires": { + "exit-hook": "^1.0.0", + "onetime": "^1.0.0" + } + }, + "rimraf": { + "version": "2.6.3", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.3.tgz", + "integrity": "sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA==", + "dev": true, + "requires": { + "glob": "^7.1.3" + } + }, + "run-async": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/run-async/-/run-async-0.1.0.tgz", + "integrity": "sha1-yK1KXhEGYeQCp9IbUw4AnyX444k=", + "dev": true, + "requires": { + "once": "^1.3.0" + } + }, + "rx-lite": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/rx-lite/-/rx-lite-3.1.2.tgz", + "integrity": "sha1-Gc5QLKVyZl87ZHsQk5+X/RYV8QI=", + "dev": true + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "dev": true + }, + "semver": { + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", + "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==", + "dev": true + }, + "shelljs": { + "version": "0.7.8", + "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.7.8.tgz", + "integrity": "sha1-3svPh0sNHl+3LhSxZKloMEjprLM=", + "dev": true, + "requires": { + "glob": "^7.0.0", + "interpret": "^1.0.0", + "rechoir": "^0.6.2" + } + }, + "slice-ansi": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-0.0.4.tgz", + "integrity": "sha1-7b+JA/ZvfOL46v1s7tZeJkyDGzU=", + "dev": true + }, + "spdx-correct": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.1.0.tgz", + "integrity": "sha512-lr2EZCctC2BNR7j7WzJ2FpDznxky1sjfxvvYEyzxNyb6lZXHODmEoJeFu4JupYlkfha1KZpJyoqiJ7pgA1qq8Q==", + "dev": true, + "requires": { + "spdx-expression-parse": "^3.0.0", + "spdx-license-ids": "^3.0.0" + } + }, + "spdx-exceptions": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/spdx-exceptions/-/spdx-exceptions-2.2.0.tgz", + "integrity": "sha512-2XQACfElKi9SlVb1CYadKDXvoajPgBVPn/gOQLrTvHdElaVhr7ZEbqJaRnJLVNeaI4cMEAgVCeBMKF6MWRDCRA==", + "dev": true + }, + "spdx-expression-parse": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/spdx-expression-parse/-/spdx-expression-parse-3.0.0.tgz", + "integrity": "sha512-Yg6D3XpRD4kkOmTpdgbUiEJFKghJH03fiC1OPll5h/0sO6neh2jqRDVHOQ4o/LMea0tgCkbMgea5ip/e+MkWyg==", + "dev": true, + "requires": { + "spdx-exceptions": "^2.1.0", + "spdx-license-ids": "^3.0.0" + } + }, + "spdx-license-ids": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-3.0.5.tgz", + "integrity": "sha512-J+FWzZoynJEXGphVIS+XEh3kFSjZX/1i9gFBaWQcB+/tmpe2qUsSBABpcxqxnAxFdiUFEgAX1bjYGQvIZmoz9Q==", + "dev": true + }, + "sprintf-js": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", + "dev": true + }, + "string-width": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", + "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", + "dev": true, + "requires": { + "code-point-at": "^1.0.0", + "is-fullwidth-code-point": "^1.0.0", + "strip-ansi": "^3.0.0" + } + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "dev": true, + "requires": { + "safe-buffer": "~5.1.0" + } + }, + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", + "dev": true, + "requires": { + "ansi-regex": "^2.0.0" + } + }, + "strip-bom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", + "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", + "dev": true + }, + "strip-json-comments": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", + "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=", + "dev": true + }, + "supports-color": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", + "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=", + "dev": true + }, + "table": { + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/table/-/table-3.8.3.tgz", + "integrity": "sha1-K7xULw/amGGnVdOUf+/Ys/UThV8=", + "dev": true, + "requires": { + "ajv": "^4.7.0", + "ajv-keywords": "^1.0.0", + "chalk": "^1.1.1", + "lodash": "^4.0.0", + "slice-ansi": "0.0.4", + "string-width": "^2.0.0" + }, + "dependencies": { + "ansi-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", + "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", + "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=", + "dev": true + }, + "string-width": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", + "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", + "dev": true, + "requires": { + "is-fullwidth-code-point": "^2.0.0", + "strip-ansi": "^4.0.0" + } + }, + "strip-ansi": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", + "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=", + "dev": true, + "requires": { + "ansi-regex": "^3.0.0" + } + } + } + }, + "text-table": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz", + "integrity": "sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=", + "dev": true + }, + "through": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", + "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=", + "dev": true + }, + "type": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/type/-/type-1.0.1.tgz", + "integrity": "sha512-MAM5dBMJCJNKs9E7JXo4CXRAansRfG0nlJxW7Wf6GZzSOvH31zClSaHdIMWLehe/EGMBkqeC55rrkaOr5Oo7Nw==", + "dev": true + }, + "type-check": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.3.2.tgz", + "integrity": "sha1-WITKtRLPHTVeP7eE8wgEsrUg23I=", + "dev": true, + "requires": { + "prelude-ls": "~1.1.2" + } + }, + "typedarray": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", + "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=", + "dev": true + }, + "user-home": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/user-home/-/user-home-2.0.0.tgz", + "integrity": "sha1-nHC/2Babwdy/SGBODwS4tJzenp8=", + "dev": true, + "requires": { + "os-homedir": "^1.0.0" + } + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", + "dev": true + }, + "validate-npm-package-license": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz", + "integrity": "sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew==", + "dev": true, + "requires": { + "spdx-correct": "^3.0.0", + "spdx-expression-parse": "^3.0.0" + } + }, + "wordwrap": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz", + "integrity": "sha1-J1hIEIkUVqQXHI0CJkQa3pDLyus=", + "dev": true + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true + }, + "write": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/write/-/write-0.2.1.tgz", + "integrity": "sha1-X8A4KOJkzqP+kUVUdvejxWbLB1c=", + "dev": true, + "requires": { + "mkdirp": "^0.5.1" + } + }, + "xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", + "dev": true + } + } +} diff --git a/package.json b/package.json index a1a4fb8..e6888a6 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,21 @@ { "name": "cordova-plugin-chromecast", "version": "1.0.0", - "main": "chrome.cast.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "npm run eslint", + "eslint": "node node_modules/eslint/bin/eslint www && node node_modules/eslint/bin/eslint src && node node_modules/eslint/bin/eslint tests" }, "author": "", "license": "GPL-2.0-only", "readme": "README.md", - "description": "README.md" + "description": "README.md", + "devDependencies": { + "eslint": "~3.19.0", + "eslint-config-semistandard": "~11.0.0", + "eslint-config-standard": "~10.2.1", + "eslint-plugin-import": "~2.3.0", + "eslint-plugin-node": "~5.0.0", + "eslint-plugin-promise": "~3.5.0", + "eslint-plugin-standard": "~3.0.1" + } } From 08a0ec00895b0c4a1a8038eed7b303ac69cb9f88 Mon Sep 17 00:00:00 2001 From: sel-en-ium Date: Tue, 30 Jul 2019 23:54:44 -0600 Subject: [PATCH 005/166] Satisfy ESLint. (To [commit history], I'm sorry.) # Conflicts: # tests/tests.js # www/chrome.cast.js --- README.md | 11 + tests/tests.js | 489 +++++----- www/EventEmitter.js | 63 +- www/chrome.cast.js | 2098 +++++++++++++++++++++---------------------- 4 files changed, 1331 insertions(+), 1330 deletions(-) diff --git a/README.md b/README.md index e59972e..6d41773 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,14 @@ When you call `chrome.cast.requestSession()` a popup will be displayed to select ## Status The project is now pretty much feature complete - the only things that will possibly break are missing parameters. We haven't done any checking for optional paramaters. When using the plugin make sure your constructors and function calls have every parameter you can find in the method declarations. + +# Development + +## Formatting + +* Run `npm run test` (from the plugin directory) + * You may need to run `npm install` if you don't have eslint installed already + +If it finds errors you can try and automatically fix them with: + +`node node_modules/eslint/bin/eslint --fix` \ No newline at end of file diff --git a/tests/tests.js b/tests/tests.js index fbf1def..41bb90a 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1,284 +1,281 @@ -exports.init = function() { - eval(require('org.apache.cordova.test-framework.test').injectJasmineInterface(this, 'this')); - jasmine.DEFAULT_TIMEOUT_INTERVAL = 25000; +exports.init = function () { + /* eslint-disable no-undef */ - // var cc = require('acidhax.cordova.chromecast.Chromecast'); + eval(require('org.apache.cordova.test-framework.test').injectJasmineInterface(this, 'this')); // eslint-disable-line no-eval + jasmine.DEFAULT_TIMEOUT_INTERVAL = 25000; - var applicationID = 'CC1AD845'; - var videoUrl = 'http://s3.nwgat.net/flvplayers3/bbb.mp4'; + // var cc = require('acidhax.cordova.chromecast.Chromecast'); + var applicationID = 'CC1AD845'; + var videoUrl = 'http://s3.nwgat.net/flvplayers3/bbb.mp4'; - describe('chrome.cast', function() { + describe('chrome.cast', function () { - var _session = null; - var _receiverAvailability = null; - var _sessionUpdatedFired = false; - var _mediaUpdatedFired = false; + var _session = null; + var _receiverAvailability = null; + var _sessionUpdatedFired = false; + var _mediaUpdatedFired = false; // var _currentMedia = null; - it('should contain definitions', function(done) { - setTimeout(function() { - expect(chrome.cast.VERSION).toBeDefined(); - expect(chrome.cast.ReceiverAvailability).toBeDefined(); - expect(chrome.cast.ReceiverType).toBeDefined(); - expect(chrome.cast.SenderPlatform).toBeDefined(); - expect(chrome.cast.AutoJoinPolicy).toBeDefined(); - expect(chrome.cast.Capability).toBeDefined(); - expect(chrome.cast.DefaultActionPolicy).toBeDefined(); - expect(chrome.cast.ErrorCode).toBeDefined(); - expect(chrome.cast.timeout).toBeDefined(); - expect(chrome.cast.isAvailable).toBeDefined(); - expect(chrome.cast.ApiConfig).toBeDefined(); - expect(chrome.cast.Receiver).toBeDefined(); - expect(chrome.cast.DialRequest).toBeDefined(); - expect(chrome.cast.SessionRequest).toBeDefined(); - expect(chrome.cast.Error).toBeDefined(); - expect(chrome.cast.Image).toBeDefined(); - expect(chrome.cast.SenderApplication).toBeDefined(); - expect(chrome.cast.Volume).toBeDefined(); - expect(chrome.cast.media).toBeDefined(); - expect(chrome.cast.initialize).toBeDefined(); - expect(chrome.cast.requestSession).toBeDefined(); - expect(chrome.cast.setCustomReceivers).toBeDefined(); - expect(chrome.cast.Session).toBeDefined(); - expect(chrome.cast.media.PlayerState).toBeDefined(); - expect(chrome.cast.media.ResumeState).toBeDefined(); - expect(chrome.cast.media.MediaCommand).toBeDefined(); - expect(chrome.cast.media.MetadataType).toBeDefined(); - expect(chrome.cast.media.StreamType).toBeDefined(); - expect(chrome.cast.media.timeout).toBeDefined(); - expect(chrome.cast.media.LoadRequest).toBeDefined(); - expect(chrome.cast.media.PlayRequest).toBeDefined(); - expect(chrome.cast.media.SeekRequest).toBeDefined(); - expect(chrome.cast.media.VolumeRequest).toBeDefined(); - expect(chrome.cast.media.StopRequest).toBeDefined(); - expect(chrome.cast.media.PauseRequest).toBeDefined(); - expect(chrome.cast.media.GenericMediaMetadata).toBeDefined(); - expect(chrome.cast.media.MovieMediaMetadata).toBeDefined(); - expect(chrome.cast.media.MusicTrackMediaMetadata).toBeDefined(); - expect(chrome.cast.media.PhotoMediaMetadata).toBeDefined(); - expect(chrome.cast.media.TvShowMediaMetadata).toBeDefined(); - expect(chrome.cast.media.MediaInfo).toBeDefined(); - expect(chrome.cast.media.Media).toBeDefined(); - expect(chrome.cast.Session.prototype.setReceiverVolumeLevel).toBeDefined(); - expect(chrome.cast.Session.prototype.setReceiverMuted).toBeDefined(); - expect(chrome.cast.Session.prototype.stop).toBeDefined(); - expect(chrome.cast.Session.prototype.sendMessage).toBeDefined(); - expect(chrome.cast.Session.prototype.addUpdateListener).toBeDefined(); - expect(chrome.cast.Session.prototype.removeUpdateListener).toBeDefined(); - expect(chrome.cast.Session.prototype.addMessageListener).toBeDefined(); - expect(chrome.cast.Session.prototype.removeMessageListener).toBeDefined(); - expect(chrome.cast.Session.prototype.addMediaListener).toBeDefined(); - expect(chrome.cast.Session.prototype.removeMediaListener).toBeDefined(); - expect(chrome.cast.Session.prototype.loadMedia).toBeDefined(); - expect(chrome.cast.media.Media.prototype.play).toBeDefined(); - expect(chrome.cast.media.Media.prototype.pause).toBeDefined(); - expect(chrome.cast.media.Media.prototype.seek).toBeDefined(); - expect(chrome.cast.media.Media.prototype.stop).toBeDefined(); - expect(chrome.cast.media.Media.prototype.setVolume).toBeDefined(); - expect(chrome.cast.media.Media.prototype.supportsCommand).toBeDefined(); - expect(chrome.cast.media.Media.prototype.getEstimatedTime).toBeDefined(); - expect(chrome.cast.media.Media.prototype.addUpdateListener).toBeDefined(); - expect(chrome.cast.media.Media.prototype.removeUpdateListener).toBeDefined(); - done(); - }, 1000); - }); - - it('api should be available', function(done) { - setTimeout(function() { - console.log('api should be available'); - expect(chrome.cast.isAvailable).toEqual(true); - done(); - }, 4000) - }); + it('should contain definitions', function (done) { + setTimeout(function () { + expect(chrome.cast.VERSION).toBeDefined(); + expect(chrome.cast.ReceiverAvailability).toBeDefined(); + expect(chrome.cast.ReceiverType).toBeDefined(); + expect(chrome.cast.SenderPlatform).toBeDefined(); + expect(chrome.cast.AutoJoinPolicy).toBeDefined(); + expect(chrome.cast.Capability).toBeDefined(); + expect(chrome.cast.DefaultActionPolicy).toBeDefined(); + expect(chrome.cast.ErrorCode).toBeDefined(); + expect(chrome.cast.timeout).toBeDefined(); + expect(chrome.cast.isAvailable).toBeDefined(); + expect(chrome.cast.ApiConfig).toBeDefined(); + expect(chrome.cast.Receiver).toBeDefined(); + expect(chrome.cast.DialRequest).toBeDefined(); + expect(chrome.cast.SessionRequest).toBeDefined(); + expect(chrome.cast.Error).toBeDefined(); + expect(chrome.cast.Image).toBeDefined(); + expect(chrome.cast.SenderApplication).toBeDefined(); + expect(chrome.cast.Volume).toBeDefined(); + expect(chrome.cast.media).toBeDefined(); + expect(chrome.cast.initialize).toBeDefined(); + expect(chrome.cast.requestSession).toBeDefined(); + expect(chrome.cast.setCustomReceivers).toBeDefined(); + expect(chrome.cast.Session).toBeDefined(); + expect(chrome.cast.media.PlayerState).toBeDefined(); + expect(chrome.cast.media.ResumeState).toBeDefined(); + expect(chrome.cast.media.MediaCommand).toBeDefined(); + expect(chrome.cast.media.MetadataType).toBeDefined(); + expect(chrome.cast.media.StreamType).toBeDefined(); + expect(chrome.cast.media.timeout).toBeDefined(); + expect(chrome.cast.media.LoadRequest).toBeDefined(); + expect(chrome.cast.media.PlayRequest).toBeDefined(); + expect(chrome.cast.media.SeekRequest).toBeDefined(); + expect(chrome.cast.media.VolumeRequest).toBeDefined(); + expect(chrome.cast.media.StopRequest).toBeDefined(); + expect(chrome.cast.media.PauseRequest).toBeDefined(); + expect(chrome.cast.media.GenericMediaMetadata).toBeDefined(); + expect(chrome.cast.media.MovieMediaMetadata).toBeDefined(); + expect(chrome.cast.media.MusicTrackMediaMetadata).toBeDefined(); + expect(chrome.cast.media.PhotoMediaMetadata).toBeDefined(); + expect(chrome.cast.media.TvShowMediaMetadata).toBeDefined(); + expect(chrome.cast.media.MediaInfo).toBeDefined(); + expect(chrome.cast.media.Media).toBeDefined(); + expect(chrome.cast.Session.prototype.setReceiverVolumeLevel).toBeDefined(); + expect(chrome.cast.Session.prototype.setReceiverMuted).toBeDefined(); + expect(chrome.cast.Session.prototype.stop).toBeDefined(); + expect(chrome.cast.Session.prototype.sendMessage).toBeDefined(); + expect(chrome.cast.Session.prototype.addUpdateListener).toBeDefined(); + expect(chrome.cast.Session.prototype.removeUpdateListener).toBeDefined(); + expect(chrome.cast.Session.prototype.addMessageListener).toBeDefined(); + expect(chrome.cast.Session.prototype.removeMessageListener).toBeDefined(); + expect(chrome.cast.Session.prototype.addMediaListener).toBeDefined(); + expect(chrome.cast.Session.prototype.removeMediaListener).toBeDefined(); + expect(chrome.cast.Session.prototype.loadMedia).toBeDefined(); + expect(chrome.cast.media.Media.prototype.play).toBeDefined(); + expect(chrome.cast.media.Media.prototype.pause).toBeDefined(); + expect(chrome.cast.media.Media.prototype.seek).toBeDefined(); + expect(chrome.cast.media.Media.prototype.stop).toBeDefined(); + expect(chrome.cast.media.Media.prototype.setVolume).toBeDefined(); + expect(chrome.cast.media.Media.prototype.supportsCommand).toBeDefined(); + expect(chrome.cast.media.Media.prototype.getEstimatedTime).toBeDefined(); + expect(chrome.cast.media.Media.prototype.addUpdateListener).toBeDefined(); + expect(chrome.cast.media.Media.prototype.removeUpdateListener).toBeDefined(); + done(); + }, 1000); + }); - it('initialize should succeed', function(done) { - console.log('initialize should succeed'); - var sessionRequest = new chrome.cast.SessionRequest(applicationID); - var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function(session) { - console.log('sessionCallback'); - _session = session; - }, function(available) { - console.log('receiverCallback') - _receiverAvailability = available; - }); - - chrome.cast.initialize(apiConfig, function() { - console.log('initialize done'); - done(); - }, function(err) { - console.log('initialize error', err); - expect(err).toBe(null); - done(); - }); - }); + it('api should be available', function (done) { + setTimeout(function () { + console.log('api should be available'); + expect(chrome.cast.isAvailable).toEqual(true); + done(); + }, 4000); + }); - it('receiver available', function(done) { - setTimeout(function() { - console.log('receiver available', _receiverAvailability); - expect(_receiverAvailability).toEqual(chrome.cast.ReceiverAvailability.AVAILABLE); - done(); - }, 2000); - }); + it('initialize should succeed', function (done) { + console.log('initialize should succeed'); + var sessionRequest = new chrome.cast.SessionRequest(applicationID); + var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) { + console.log('sessionCallback'); + _session = session; + }, function (available) { + console.log('receiverCallback'); + _receiverAvailability = available; + }); + + chrome.cast.initialize(apiConfig, function () { + console.log('initialize done'); + done(); + }, function (err) { + console.log('initialize error', err); + expect(err).toBe(null); + done(); + }); + }); + it('receiver available', function (done) { + setTimeout(function () { + console.log('receiver available', _receiverAvailability); + expect(_receiverAvailability).toEqual(chrome.cast.ReceiverAvailability.AVAILABLE); + done(); + }, 2000); + }); - it('requestSession should succeed', function(done) { - chrome.cast.requestSession(function(session) { - console.log('request session success'); - _session = session; - expect(session).toBeDefined(); - expect(session.appId).toBeDefined(); - expect(session.displayName).toBeDefined(); - expect(session.receiver).toBeDefined(); - expect(session.receiver.friendlyName).toBeDefined(); - expect(session.addUpdateListener).toBeDefined(); - expect(session.removeUpdateListener).toBeDefined(); - - var updateListener = function(isAlive) { - _sessionUpdatedFired = true; - session.removeUpdateListener(updateListener); - }; - - session.addUpdateListener(updateListener); - done(); - }, function(err) { - console.log('request session error'); - expect(err).toBe(null); - done(); - }) - }); + it('requestSession should succeed', function (done) { + chrome.cast.requestSession(function (session) { + console.log('request session success'); + _session = session; + expect(session).toBeDefined(); + expect(session.appId).toBeDefined(); + expect(session.displayName).toBeDefined(); + expect(session.receiver).toBeDefined(); + expect(session.receiver.friendlyName).toBeDefined(); + expect(session.addUpdateListener).toBeDefined(); + expect(session.removeUpdateListener).toBeDefined(); + + var updateListener = function (isAlive) { + _sessionUpdatedFired = true; + session.removeUpdateListener(updateListener); + }; + + session.addUpdateListener(updateListener); + done(); + }, function (err) { + console.log('request session error'); + expect(err).toBe(null); + done(); + }); + }); - it('loadRequest should work', function(done) { - var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); - var request = new chrome.cast.media.LoadRequest(mediaInfo); - expect(_session).not.toBeNull() - _session.loadMedia(request, function(media) { - console.log('loadRequest success', media); - _currentMedia = media; + it('loadRequest should work', function (done) { + var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + var request = new chrome.cast.media.LoadRequest(mediaInfo); + expect(_session).not.toBeNull(); + _session.loadMedia(request, function (media) { + console.log('loadRequest success', media); + _currentMedia = media; // expect(_currentMedia instanceof chrome.cast.media.Media).toBe(true); - - expect(_currentMedia.sessionId).toEqual(_session.sessionId); - expect(_currentMedia.addUpdateListener).toBeDefined(); - expect(_currentMedia.removeUpdateListener).toBeDefined(); - var updateListener = function() { - _mediaUpdatedFired = true; - _currentMedia.removeUpdateListener(updateListener); - }; + expect(_currentMedia.sessionId).toEqual(_session.sessionId); + expect(_currentMedia.addUpdateListener).toBeDefined(); + expect(_currentMedia.removeUpdateListener).toBeDefined(); - _currentMedia.addUpdateListener(updateListener); + var updateListener = function () { + _mediaUpdatedFired = true; + _currentMedia.removeUpdateListener(updateListener); + }; - done(); - }, function(err) { - console.log('loadRequest error', err); - expect(err).toBeNull(); - done(); - }); + _currentMedia.addUpdateListener(updateListener); - }); + done(); + }, function (err) { + console.log('loadRequest error', err); + expect(err).toBeNull(); + done(); + }); - it('pause media should succeed', function(done) { - setTimeout(function() { - _currentMedia.pause(null, function() { - console.log('pause success'); - done(); - }, function(err) { - console.log('pause error', err); - expect(err).toBeNull(); - done(); }); - }, 5000); - }); - it('play media should succeed', function(done) { - setTimeout(function() { - _currentMedia.play(null, function() { - console.log('play success'); - done(); - }, function(err) { - console.log('play error', err); - expect(err).toBeNull(); - done(); + it('pause media should succeed', function (done) { + setTimeout(function () { + _currentMedia.pause(null, function () { + console.log('pause success'); + done(); + }, function (err) { + console.log('pause error', err); + expect(err).toBeNull(); + done(); + }); + }, 5000); + }); + + it('play media should succeed', function (done) { + setTimeout(function () { + _currentMedia.play(null, function () { + console.log('play success'); + done(); + }, function (err) { + console.log('play error', err); + expect(err).toBeNull(); + done(); + }); + }, 1000); }); - }, 1000); - }); - it('seek media should succeed', function(done) { - setTimeout(function() { - var request = new chrome.cast.media.SeekRequest(); - request.currentTime = 10; + it('seek media should succeed', function (done) { + setTimeout(function () { + var request = new chrome.cast.media.SeekRequest(); + request.currentTime = 10; + + _currentMedia.seek(request, function () { + done(); + }, function (err) { + expect(err).toBeNull(); + done(); + }); + }, 1000); + }); - _currentMedia.seek(request, function() { - done(); - }, function(err) { - expect(err).toBeNull(); - done(); + it('session updateListener', function (done) { + expect(_sessionUpdatedFired).toEqual(true); + done(); }); - }, 1000); - }); - it('session updateListener', function(done) { - expect(_sessionUpdatedFired).toEqual(true); - done(); - }); + it('media updateListener', function (done) { + expect(_mediaUpdatedFired).toEqual(true); + done(); + }); - it('media updateListener', function(done) { - expect(_mediaUpdatedFired).toEqual(true); - done(); - }); + it('volume and muting', function (done) { + var volume = new chrome.cast.Volume(); + volume.level = 0.5; - it('volume and muting', function(done) { - var volume = new chrome.cast.Volume(); - volume.level = 0.5; - - var request = new chrome.cast.media.VolumeRequest(); - request.volume = volume; + var request = new chrome.cast.media.VolumeRequest(); + request.volume = volume; - _currentMedia.setVolume(request, function() { - - var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, true)); - _currentMedia.setVolume(request, function() { + _currentMedia.setVolume(request, function () { + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, true)); + _currentMedia.setVolume(request, function () { - var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, false)); - _currentMedia.setVolume(request, function() { - done(); - }, function(err) { - expect(err).toBeNull(); - done(); - }); + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, false)); + _currentMedia.setVolume(request, function () { + done(); + }, function (err) { + expect(err).toBeNull(); + done(); + }); - }, function(err) { - expect(err).toBeNull(); - done(); - }); + }, function (err) { + expect(err).toBeNull(); + done(); + }); - }, function(err) { - expect(err).toBeNull(); - done(); - }); + }, function (err) { + expect(err).toBeNull(); + done(); + }); - }); + }); + it('stopping the video', function (done) { + _currentMedia.stop(null, function () { + setTimeout(done, 1000); + }, function (err) { + expect(err).toBeNull(); + done(); + }); + }); - it('stopping the video', function(done) { - _currentMedia.stop(null, function() { - setTimeout(done, 1000); - }, function(err) { - expect(err).toBeNull(); - done(); - }); - }); + it('unloading the session', function (done) { + _session.stop(function () { + done(); + }, function (err) { + expect(err).toBeNull(); + done(); + }); + }); - it('unloading the session', function(done) { - _session.stop(function() { - done(); - }, function(err) { - expect(err).toBeNull(); - done(); - }); }); - - }); }; - diff --git a/www/EventEmitter.js b/www/EventEmitter.js index 426cb8a..d2281fa 100644 --- a/www/EventEmitter.js +++ b/www/EventEmitter.js @@ -4,7 +4,7 @@ * Oliver Caldwell - http://oli.me.uk/ * @preserve */ - +(function () { 'use strict'; /** @@ -13,7 +13,7 @@ * * @class EventEmitter Manages event registering and emitting. */ - function EventEmitter() {} + function EventEmitter () {} // Shortcuts to improve speed and size var proto = EventEmitter.prototype; @@ -26,7 +26,7 @@ * @return {Number} Index of the specified listener, -1 if not found * @api private */ - function indexOfListener(listeners, listener) { + function indexOfListener (listeners, listener) { var i = listeners.length; while (i--) { if (listeners[i].listener === listener) { @@ -44,8 +44,8 @@ * @return {Function} The aliased method * @api private */ - function alias(name) { - return function aliasClosure() { + function alias (name) { + return function aliasClosure () { return this[name].apply(this, arguments); }; } @@ -59,7 +59,7 @@ * @param {String|RegExp} evt Name of the event to return the listeners from. * @return {Function[]|Object} All listener functions for the event. */ - proto.getListeners = function getListeners(evt) { + proto.getListeners = function getListeners (evt) { var events = this._getEvents(); var response; var key; @@ -73,8 +73,7 @@ response[key] = events[key]; } } - } - else { + } else { response = events[evt] || (events[evt] = []); } @@ -87,7 +86,7 @@ * @param {Object[]} listeners Raw listener objects. * @return {Function[]} Just the listener functions. */ - proto.flattenListeners = function flattenListeners(listeners) { + proto.flattenListeners = function flattenListeners (listeners) { var flatListeners = []; var i; @@ -104,7 +103,7 @@ * @param {String|RegExp} evt Name of the event to return the listeners from. * @return {Object} All listener functions for an event in an object. */ - proto.getListenersAsObject = function getListenersAsObject(evt) { + proto.getListenersAsObject = function getListenersAsObject (evt) { var listeners = this.getListeners(evt); var response; @@ -126,7 +125,7 @@ * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. * @return {Object} Current instance of EventEmitter for chaining. */ - proto.addListener = function addListener(evt, listener) { + proto.addListener = function addListener (evt, listener) { var listeners = this.getListenersAsObject(evt); var listenerIsWrapped = typeof listener === 'object'; var key; @@ -156,7 +155,7 @@ * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. * @return {Object} Current instance of EventEmitter for chaining. */ - proto.addOnceListener = function addOnceListener(evt, listener) { + proto.addOnceListener = function addOnceListener (evt, listener) { return this.addListener(evt, { listener: listener, once: true @@ -175,7 +174,7 @@ * @param {String} evt Name of the event to create. * @return {Object} Current instance of EventEmitter for chaining. */ - proto.defineEvent = function defineEvent(evt) { + proto.defineEvent = function defineEvent (evt) { this.getListeners(evt); return this; }; @@ -186,7 +185,7 @@ * @param {String[]} evts An array of event names to define. * @return {Object} Current instance of EventEmitter for chaining. */ - proto.defineEvents = function defineEvents(evts) { + proto.defineEvents = function defineEvents (evts) { for (var i = 0; i < evts.length; i += 1) { this.defineEvent(evts[i]); } @@ -201,7 +200,7 @@ * @param {Function} listener Method to remove from the event. * @return {Object} Current instance of EventEmitter for chaining. */ - proto.removeListener = function removeListener(evt, listener) { + proto.removeListener = function removeListener (evt, listener) { var listeners = this.getListenersAsObject(evt); var index; var key; @@ -234,7 +233,7 @@ * @param {Function[]} [listeners] An optional array of listener functions to add. * @return {Object} Current instance of EventEmitter for chaining. */ - proto.addListeners = function addListeners(evt, listeners) { + proto.addListeners = function addListeners (evt, listeners) { // Pass through to manipulateListeners return this.manipulateListeners(false, evt, listeners); }; @@ -249,7 +248,7 @@ * @param {Function[]} [listeners] An optional array of listener functions to remove. * @return {Object} Current instance of EventEmitter for chaining. */ - proto.removeListeners = function removeListeners(evt, listeners) { + proto.removeListeners = function removeListeners (evt, listeners) { // Pass through to manipulateListeners return this.manipulateListeners(true, evt, listeners); }; @@ -266,7 +265,7 @@ * @param {Function[]} [listeners] An optional array of listener functions to add/remove. * @return {Object} Current instance of EventEmitter for chaining. */ - proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) { + proto.manipulateListeners = function manipulateListeners (remove, evt, listeners) { var i; var value; var single = remove ? this.removeListener : this.addListener; @@ -279,15 +278,13 @@ // Pass the single listener straight through to the singular method if (typeof value === 'function') { single.call(this, i, value); - } - else { + } else { // Otherwise pass back to the multiple function multiple.call(this, i, value); } } } - } - else { + } else { // So evt must be a string // And listeners must be an array of listeners // Loop over it and pass each one to the multiple method @@ -309,7 +306,7 @@ * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed. * @return {Object} Current instance of EventEmitter for chaining. */ - proto.removeEvent = function removeEvent(evt) { + proto.removeEvent = function removeEvent (evt) { var type = typeof evt; var events = this._getEvents(); var key; @@ -318,16 +315,14 @@ if (type === 'string') { // Remove all listeners for the specified event delete events[evt]; - } - else if (evt instanceof RegExp) { + } else if (evt instanceof RegExp) { // Remove all events matching the regex. for (key in events) { if (events.hasOwnProperty(key) && evt.test(key)) { delete events[key]; } } - } - else { + } else { // Remove all listeners in all events delete this._events; } @@ -354,7 +349,7 @@ * @param {Array} [args] Optional array of arguments to be passed to each listener. * @return {Object} Current instance of EventEmitter for chaining. */ - proto.emitEvent = function emitEvent(evt, args) { + proto.emitEvent = function emitEvent (evt, args) { var listeners = this.getListenersAsObject(evt); var listener; var i; @@ -399,7 +394,7 @@ * @param {...*} Optional additional arguments to be passed to each listener. * @return {Object} Current instance of EventEmitter for chaining. */ - proto.emit = function emit(evt) { + proto.emit = function emit (evt) { var args = Array.prototype.slice.call(arguments, 1); return this.emitEvent(evt, args); }; @@ -412,7 +407,7 @@ * @param {*} value The new value to check for when executing listeners. * @return {Object} Current instance of EventEmitter for chaining. */ - proto.setOnceReturnValue = function setOnceReturnValue(value) { + proto.setOnceReturnValue = function setOnceReturnValue (value) { this._onceReturnValue = value; return this; }; @@ -425,11 +420,10 @@ * @return {*|Boolean} The current value to check for or the default, true. * @api private */ - proto._getOnceReturnValue = function _getOnceReturnValue() { + proto._getOnceReturnValue = function _getOnceReturnValue () { if (this.hasOwnProperty('_onceReturnValue')) { return this._onceReturnValue; - } - else { + } else { return true; } }; @@ -440,8 +434,9 @@ * @return {Object} The events storage object. * @api private */ - proto._getEvents = function _getEvents() { + proto._getEvents = function _getEvents () { return this._events || (this._events = {}); }; module.exports = EventEmitter; +}()); diff --git a/www/chrome.cast.js b/www/chrome.cast.js index e3d90cf..0cd8a11 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -1,6 +1,6 @@ /** - * Portions of this page are modifications based on work created and shared by - * Google and used according to terms described in the Creative Commons 3.0 + * Portions of this page are modifications based on work created and shared by + * Google and used according to terms described in the Creative Commons 3.0 * Attribution License. */ var EventEmitter = require('cordova-plugin-chromecast.EventEmitter'); @@ -9,517 +9,517 @@ var chrome = {}; chrome.cast = { - /** - * The API version. - * @type {Array} - */ - VERSION: [1, 1], - - /** - * Describes availability of a Cast receiver. - * AVAILABLE: At least one receiver is available that is compatible with the session request. - * UNAVAILABLE: No receivers are available. - * @type {Object} - */ - ReceiverAvailability: { AVAILABLE: "available", UNAVAILABLE: "unavailable" }, - - /** - * TODO: Update when the official API docs are finished - * https://developers.google.com/cast/docs/reference/chrome/chrome.cast.ReceiverType - * CAST: - * DIAL: - * CUSTOM: - * @type {Object} - */ - ReceiverType: { CAST: "cast", DIAL: "dial", CUSTOM: "custom" }, - - /** - * Describes a sender application platform. - * CHROME: - * IOS: - * ANDROID: - * @type {Object} - */ - SenderPlatform: { CHROME: "chrome", IOS: "ios", ANDROID: "android" }, - - /** - * Auto-join policy determines when the SDK will automatically connect a sender application to an existing session after API initialization. - * ORIGIN_SCOPED: Automatically connects when the session was started with the same appId and the same page origin (regardless of tab). - * PAGE_SCOPED: No automatic connection. - * TAB_AND_ORIGIN_SCOPED: Automatically connects when the session was started with the same appId, in the same tab and page origin. - * @type {Object} - */ - AutoJoinPolicy: { TAB_AND_ORIGIN_SCOPED: "tab_and_origin_scoped", ORIGIN_SCOPED: "origin_scoped", PAGE_SCOPED: "page_scoped" }, - - /** - * Capabilities that are supported by the receiver device. - * AUDIO_IN: The receiver supports audio input (microphone). - * AUDIO_OUT: The receiver supports audio output. - * VIDEO_IN: The receiver supports video input (camera). - * VIDEO_OUT: The receiver supports video output. - * @type {Object} - */ - Capability: { VIDEO_OUT: "video_out", AUDIO_OUT: "audio_out", VIDEO_IN: "video_in", AUDIO_IN: "audio_in" }, - - /** - * Default action policy determines when the SDK will automatically create a session after initializing the API. This also controls the default action for the tab in the extension popup. - * CAST_THIS_TAB: No automatic launch is done after initializing the API, even if the tab is being cast. - * CREATE_SESSION: If the tab containing the app is being casted when the API initializes, the SDK stops tab casting and automatically launches the app. - * @type {Object} - */ - DefaultActionPolicy: { CREATE_SESSION: "create_session", CAST_THIS_TAB: "cast_this_tab" }, - - /** - * Errors that may be returned by the SDK. - * API_NOT_INITIALIZED: The API is not initialized. - * CANCEL: The operation was canceled by the user. - * CHANNEL_ERROR: A channel to the receiver is not available. - * EXTENSION_MISSING: The Cast extension is not available. - * EXTENSION_NOT_COMPATIBLE: The API script is not compatible with the installed Cast extension. - * INVALID_PARAMETER: The parameters to the operation were not valid. - * LOAD_MEDIA_FAILED: Load media failed. - * RECEIVER_UNAVAILABLE: No receiver was compatible with the session request. - * SESSION_ERROR: A session could not be created, or a session was invalid. - * TIMEOUT: The operation timed out. - * @type {Object} - */ - ErrorCode: { - API_NOT_INITIALIZED: "api_not_initialized", - CANCEL: "cancel", - CHANNEL_ERROR: "channel_error", - EXTENSION_MISSING: "extension_missing", - EXTENSION_NOT_COMPATIBLE: "extension_not_compatible", - INVALID_PARAMETER: "invalid_parameter", - LOAD_MEDIA_FAILED: "load_media_failed", - RECEIVER_UNAVAILABLE: "receiver_unavailable", - SESSION_ERROR: "session_error", - TIMEOUT: "timeout", - UNKNOWN: "unknown", - NOT_IMPLEMENTED: "not_implemented" - }, - - /** - * TODO: Update when the official API docs are finished - * https://developers.google.com/cast/docs/reference/chrome/chrome.cast.timeout - * @type {Object} - */ - timeout: { - requestSession: 10000, - sendCustomMessage: 3000, - setReceiverVolume: 3000, - stopSession: 3000 - }, - - /** - * Flag for clients to check whether the API is loaded. - * @type {Boolean} - */ - isAvailable: false, - - /** - * [ApiConfig description] - * @param {chrome.cast.SessionRequest} sessionRequest Describes the session to launch or the session to connect. - * @param {function} sessionListener Listener invoked when a session is created or connected by the SDK. - * @param {function} receiverListener Function invoked when the availability of a Cast receiver that supports the application in sessionRequest is known or changes. - * @param {chrome.cast.AutoJoinPolicy} autoJoinPolicy Determines whether the SDK will automatically connect to a running session after initialization. - * @param {chrome.cast.DefaultActionPolicy} defaultActionPolicy Requests whether the application should be launched on API initialization when the tab is already being cast. - */ - ApiConfig: function (sessionRequest, sessionListener, receiverListener, autoJoinPolicy, defaultActionPolicy) { - this.sessionRequest = sessionRequest; - this.sessionListener = sessionListener; - this.receiverListener = receiverListener; - this.autoJoinPolicy = autoJoinPolicy || chrome.cast.AutoJoinPolicy.TAB_AND_ORIGIN_SCOPED; - this.defaultActionPolicy = defaultActionPolicy || chrome.cast.DefaultActionPolicy.CREATE_SESSION; - }, - - /** - * Describes the receiver running an application. Normally, these objects should not be created by the client. - * @param {string} label An identifier for the receiver that is unique to the browser profile and the origin of the API client. - * @param {string} friendlyName The user given name for the receiver. - * @param {chrome.cast.Capability[]} capabilities The capabilities of the receiver, for example audio and video. - * @param {chrome.cast.Volume} volume The current volume of the receiver. - */ - Receiver: function (label, friendlyName, capabilities, volume) { - this.label = label; - this.friendlyName = friendlyName; - this.capabilities = capabilities || []; - this.volume = volume || null; - this.receiverType = chrome.cast.ReceiverType.CAST; - this.isActiveInput = null; - }, - - /** - * TODO: Update when the official API docs are finished - * https://developers.google.com/cast/docs/reference/chrome/chrome.cast.DialRequest - * @param {[type]} appName [description] - * @param {[type]} launchParameter [description] - */ - DialRequest: function (appName, launchParameter) { - this.appName = appName; - this.launchParameter = launchParameter; - }, - - /** - * A request to start or connect to a session. - * @param {string} appId The receiver application id. - * @param {chrome.cast.Capability[]} capabilities Capabilities required of the receiver device. - * @property {chrome.cast.DialRequest} dialRequest If given, the SDK will also discover DIAL devices that support the DIAL application given in the dialRequest. - */ - SessionRequest: function (appId, capabilities) { - this.appId = appId; - this.capabilities = capabilities || [chrome.cast.Capability.VIDEO_OUT, chrome.cast.Capability.AUDIO_OUT]; - this.dialRequest = null; - }, - - /** - * Describes an error returned by the API. Normally, these objects should not be created by the client. - * @param {chrome.cast.ErrorCode} code The error code. - * @param {string} description Human readable description of the error. - * @param {Object} details Details specific to the error. - */ - Error: function (code, description, details) { - this.code = code; - this.description = description || null; - this.details = details || null; - }, - - /** - * An image that describes a receiver application or media item. This could be an application icon, cover art, or a thumbnail. - * @param {string} url The URL to the image. - * @property {number} height The height of the image - * @property {number} width The width of the image - */ - Image: function (url) { - this.url = url; - this.width = this.height = null; - }, - - /** - * Describes a sender application. Normally, these objects should not be created by the client. - * @param {chrome.cast.SenderPlatform} platform The supported platform. - * @property {string} packageId The identifier or URL for the application in the respective platform's app store. - * @property {string} url URL or intent to launch the application. - */ - SenderApplication: function (platform) { - this.platform = platform; - this.packageId = this.url = null; - }, - - /** - * The volume of a device or media stream. - * @param {number} level The current volume level as a value between 0.0 and 1.0. - * @param {boolean} muted Whether the receiver is muted, independent of the volume level. - */ - Volume: function (level, muted) { - this.level = level || null; - this.muted = null; - if (muted === true || muted === false) { - this.muted = muted; - } - }, - - // media package - media: { - /** - * The default receiver app. - */ - DEFAULT_MEDIA_RECEIVER_APP_ID: 'CC1AD845', - - /** - * Possible states of the media player. - * BUFFERING: Player is in PLAY mode but not actively playing content. currentTime will not change. - * IDLE: No media is loaded into the player. - * PAUSED: The media is not playing. - * PLAYING: The media is playing. - * @type {Object} - */ - PlayerState: { IDLE: "IDLE", PLAYING: "PLAYING", PAUSED: "PAUSED", BUFFERING: "BUFFERING" }, - - /** - * States of the media player after resuming. - * PLAYBACK_PAUSE: Force media to pause. - * PLAYBACK_START: Force media to start. - * @type {Object} - */ - ResumeState: { PLAYBACK_START: "PLAYBACK_START", PLAYBACK_PAUSE: "PLAYBACK_PAUSE" }, - - /** - * Possible media commands supported by the receiver application. - * @type {Object} - */ - MediaCommand: { PAUSE: "pause", SEEK: "seek", STREAM_VOLUME: "stream_volume", STREAM_MUTE: "stream_mute" }, - - /** - * Possible types of media metadata. - * GENERIC: Generic template suitable for most media types. Used by chrome.cast.media.GenericMediaMetadata. - * MOVIE: A full length movie. Used by chrome.cast.media.MovieMediaMetadata. - * MUSIC_TRACK: A music track. Used by chrome.cast.media.MusicTrackMediaMetadata. - * PHOTO: Photo. Used by chrome.cast.media.PhotoMediaMetadata. - * TV_SHOW: An episode of a TV series. Used by chrome.cast.media.TvShowMediaMetadata. - * @type {Object} - */ - MetadataType: { GENERIC: 0, TV_SHOW: 1, MOVIE: 2, MUSIC_TRACK: 3, PHOTO: 4 }, - - /** - * Possible media stream types. - * BUFFERED: Stored media streamed from an existing data store. - * LIVE: Live media generated on the fly. - * OTHER: None of the above. - * @type {Object} - */ - StreamType: { BUFFERED: 'buffered', LIVE: 'live', OTHER: 'other' }, - - /** - * TODO: Update when the official API docs are finished - * https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.timeout - * @type {Object} - */ - timeout: { - load: 0, - ob: 0, - pause: 0, - play: 0, - seek: 0, - setVolume: 0, - stop: 0 - }, - - /** - * A request to load new media into the player. - * @param {chrome.cast.media.MediaInfo} media Media description. - * @property {boolean} autoplay Whether the media will automatically play. - * @property {number} currentTime Seconds from the beginning of the media to start playback. - * @property {Object} customData Custom data for the receiver application. - */ - LoadRequest: function (media) { - this.type = 'LOAD'; - this.sessionId = this.requestId = this.customData = this.currentTime = null; - this.media = media; - this.autoplay = !0; - }, - - /** - * A request to play the currently paused media. - * @property {Object} customData Custom data for the receiver application. - */ - PlayRequest: function () { - this.customData = null; - }, - - /** - * A request to seek the current media. - * @property {number} currentTime The new current time for the media, in seconds after the start of the media. - * @property {chrome.cast.media.ResumeState} resumeState The desired media player state after the seek is complete. - * @property {Object} customData Custom data for the receiver application. - */ - SeekRequest: function () { - this.customData = this.resumeState = this.currentTime = null; - }, - - /** - * A request to set the stream volume of the playing media. - * @param {chrome.cast.Volume} volume The new volume of the stream. - * @property {Object} customData Custom data for the receiver application. - */ - VolumeRequest: function (volume) { - this.volume = volume; - this.customData = null; - }, - - /** - * A request to stop the media player. - * @property {Object} customData Custom data for the receiver application. - */ - StopRequest: function () { - this.customData = null; - }, - - /** - * A request to pause the currently playing media. - * @property {Object} customData Custom data for the receiver application. - */ - PauseRequest: function () { - this.customData = null; - }, - - /** - * A generic media description. - * @property {chrome.cast.Image[]} images Content images. - * @property {string} releaseDate ISO 8601 date and/or time when the content was released, e.g. - * @property {number} releaseYear Integer year when the content was released. - * @property {string} subtitle Content subtitle. - * @property {string} title Content title. - * @property {chrome.cast.media.MetadataType} type The type of metadata. - */ - GenericMediaMetadata: function () { - this.metadataType = this.type = chrome.cast.media.MetadataType.GENERIC; - this.releaseDate = this.releaseYear = this.images = this.subtitle = this.title = null; - }, - - /** - * A movie media description. - * @property {chrome.cast.Image[]} images Content images. - * @property {string} releaseDate ISO 8601 date and/or time when the content was released, e.g. - * @property {number} releaseYear Integer year when the content was released. - * @property {string} studio Movie studio - * @property {string} subtitle Content subtitle. - * @property {string} title Content title. - * @property {chrome.cast.media.MetadataType} type The type of metadata. - */ - MovieMediaMetadata: function () { - this.metadataType = this.type = chrome.cast.media.MetadataType.MOVIE; - this.releaseDate = this.releaseYear = this.images = this.subtitle = this.studio = this.title = null; - }, - - /** - * A music track media description. - * @property {string} albumArtist Album artist name. - * @property {string} albumName Album name. - * @property {string} artist Track artist name. - * @property {string} artistName Track artist name. - * @property {string} composer Track composer name. - * @property {number} discNumber Disc number. - * @property {chrome.cast.Image[]} images Content images. - * @property {string} releaseDate ISO 8601 date when the track was released, e.g. - * @property {number} releaseYear Integer year when the album was released. - * @property {string} songName Track name. - * @property {string} title Track title. - * @property {number} trackNumber Track number in album. - * @property {chrome.cast.media.MetadataType} type The type of metadata. - */ - MusicTrackMediaMetadata: function () { - this.metadataType = this.type = chrome.cast.media.MetadataType.MUSIC_TRACK; - this.releaseDate = this.releaseYear = this.images = this.discNumber = this.trackNumber = this.artistName = this.songName = this.composer = this.artist = this.albumArtist = this.title = this.albumName = null; - }, - - /** - * A photo media description. - * @property {string} artist Name of the photographer. - * @property {string} creationDateTime ISO 8601 date and time the photo was taken, e.g. - * @property {number} height Photo height, in pixels. - * @property {chrome.cast.Image[]} images Images associated with the content. - * @property {number} latitude Latitude. - * @property {string} location Location where the photo was taken. - * @property {number} longitude Longitude. - * @property {string} title Photo title. - * @property {chrome.cast.media.MetadataType} type The type of metadata. - * @property {number} width Photo width, in pixels. - */ - PhotoMediaMetadata: function () { - this.metadataType = this.type = chrome.cast.media.MetadataType.PHOTO; - this.creationDateTime = this.height = this.width = this.longitude = this.latitude = this.images = this.location = this.artist = this.title = null; - }, - - /** - * [TvShowMediaMetadata description] - * @property {number} episode TV episode number. - * @property {number} episodeNumber TV episode number. - * @property {string} episodeTitle TV episode title. - * @property {chrome.cast.Image[]} images Content images. - * @property {string} originalAirdate ISO 8601 date when the episode originally aired, e.g. - * @property {number} releaseYear Integer year when the content was released. - * @property {number} season TV episode season. - * @property {number} seasonNumber TV episode season. - * @property {string} seriesTitle TV series title. - * @property {string} title TV episode title. - * @property {chrome.cast.media.MetadataType} type The type of metadata. - */ - TvShowMediaMetadata: function () { - this.metadataType = this.type = chrome.cast.media.MetadataType.TV_SHOW; - this.originalAirdate = this.releaseYear = this.images = this.episode = this.episodeNumber = this.season = this.seasonNumber = this.episodeTitle = this.title = this.seriesTitle = null; - }, - - /** - * Describes a media item. - * @param {string} contentId Identifies the content. - * @param {string} contentType MIME content type of the media. - * @property {Object} customData Custom data set by the receiver application. - * @property {number} duration Duration of the content, in seconds. - * @property {any type} metadata Describes the media content. - * @property {chrome.cast.media.StreamType} streamType The type of media stream. - */ - MediaInfo: function (contentId, contentType) { - this.contentId = contentId; - this.streamType = chrome.cast.media.StreamType.BUFFERED; - this.contentType = contentType; - this.customData = this.duration = this.metadata = null; - }, - - /** - * Possible media track types. - */ - TrackType: {TEXT: "TEXT", AUDIO: "AUDIO", VIDEO: "VIDEO"}, - - /** - * Possible text track types. - */ - TextTrackType: {SUBTITLES: "SUBTITLES", CAPTIONS: "CAPTIONS", DESCRIPTIONS: "DESCRIPTIONS", CHAPTERS: "CHAPTERS", METADATA: "METADATA"}, - - /** - * Describes track metadata information - * @param {number} trackId Unique identifier of the track within the context of a chrome.cast.media.MediaInfo objects - * @param {chrome.cast.media.TrackType} trackType The type of track. Value must not be null. - */ - Track: function (trackId, trackType) { - this.trackId = trackId; - this.type = trackType; - this.customData = this.language = this.name = this.subtype = this.trackContentId = this.trackContentType = null; - }, - - /** - * Possible text track edge types. - */ - TextTrackEdgeType: {NONE: "NONE", OUTLINE: "OUTLINE", DROP_SHADOW: "DROP_SHADOW", RAISED: "RAISED", DEPRESSED: "DEPRESSED"}, - - /** - * Possible text track font generic family. - */ - TextTrackFontGenericFamily: { - CURSIVE: "CURSIVE", - MONOSPACED_SANS_SERIF: "MONOSPACED_SANS_SERIF", - MONOSPACED_SERIF: "MONOSPACED_SERIF", - SANS_SERIF: "SANS_SERIF", - SERIF: "SERIF", - SMALL_CAPITALS: "SMALL_CAPITALS" - }, - - /** - * Possible text track font style. - */ - TextTrackFontStyle: {NORMAL: "NORMAL", BOLD: "BOLD", BOLD_ITALIC: "BOLD_ITALIC", ITALIC: "ITALIC"}, - - /** - * Possible text track window types. - */ - TextTrackWindowType: {NONE: "NONE", NORMAL: "NORMAL", ROUNDED_CORNERS: "ROUNDED_CORNERS"}, - - /** - * Describes style information for a text track. - * - * Colors are represented as strings "#RRGGBBAA" where XX are the two hexadecimal symbols that represent - * the 0-255 value for the specific channel/color. It follows CSS 8-digit hex color notation (See - * http://dev.w3.org/csswg/css-color/#hex-notation). - */ - TextTrackStyle: function () { - this.backgroundColor = this.customData = this.edgeColor = this.edgeType = - this.fontFamily = this.fontGenericFamily = this.fontScale = this.fontStyle = - this.foregroundColor = this.windowColor = this.windowRoundedCornerRadius = - this.windowType = null; - }, - - /** - * A request to modify the text tracks style or change the tracks status. If a trackId does not match - * the existing trackIds the whole request will fail and no status will change. It is acceptable to - * change the text track style even if no text track is currently active. - * @param {number[]} opt_activeTrackIds Optional. - * @param {chrome.cast.media.TextTrackStyle} opt_textTrackSytle Optional. - **/ - EditTracksInfoRequest: function (opt_activeTrackIds, opt_textTrackSytle) { - this.activeTrackIds = opt_activeTrackIds; - this.textTrackSytle = opt_textTrackSytle; - this.requestId = null; - } - } + /** + * The API version. + * @type {Array} + */ + VERSION: [1, 1], + + /** + * Describes availability of a Cast receiver. + * AVAILABLE: At least one receiver is available that is compatible with the session request. + * UNAVAILABLE: No receivers are available. + * @type {Object} + */ + ReceiverAvailability: { AVAILABLE: 'available', UNAVAILABLE: 'unavailable' }, + + /** + * TODO: Update when the official API docs are finished + * https://developers.google.com/cast/docs/reference/chrome/chrome.cast.ReceiverType + * CAST: + * DIAL: + * CUSTOM: + * @type {Object} + */ + ReceiverType: { CAST: 'cast', DIAL: 'dial', CUSTOM: 'custom' }, + + /** + * Describes a sender application platform. + * CHROME: + * IOS: + * ANDROID: + * @type {Object} + */ + SenderPlatform: { CHROME: 'chrome', IOS: 'ios', ANDROID: 'android' }, + + /** + * Auto-join policy determines when the SDK will automatically connect a sender application to an existing session after API initialization. + * ORIGIN_SCOPED: Automatically connects when the session was started with the same appId and the same page origin (regardless of tab). + * PAGE_SCOPED: No automatic connection. + * TAB_AND_ORIGIN_SCOPED: Automatically connects when the session was started with the same appId, in the same tab and page origin. + * @type {Object} + */ + AutoJoinPolicy: { TAB_AND_ORIGIN_SCOPED: 'tab_and_origin_scoped', ORIGIN_SCOPED: 'origin_scoped', PAGE_SCOPED: 'page_scoped' }, + + /** + * Capabilities that are supported by the receiver device. + * AUDIO_IN: The receiver supports audio input (microphone). + * AUDIO_OUT: The receiver supports audio output. + * VIDEO_IN: The receiver supports video input (camera). + * VIDEO_OUT: The receiver supports video output. + * @type {Object} + */ + Capability: { VIDEO_OUT: 'video_out', AUDIO_OUT: 'audio_out', VIDEO_IN: 'video_in', AUDIO_IN: 'audio_in' }, + + /** + * Default action policy determines when the SDK will automatically create a session after initializing the API. This also controls the default action for the tab in the extension popup. + * CAST_THIS_TAB: No automatic launch is done after initializing the API, even if the tab is being cast. + * CREATE_SESSION: If the tab containing the app is being casted when the API initializes, the SDK stops tab casting and automatically launches the app. + * @type {Object} + */ + DefaultActionPolicy: { CREATE_SESSION: 'create_session', CAST_THIS_TAB: 'cast_this_tab' }, + + /** + * Errors that may be returned by the SDK. + * API_NOT_INITIALIZED: The API is not initialized. + * CANCEL: The operation was canceled by the user. + * CHANNEL_ERROR: A channel to the receiver is not available. + * EXTENSION_MISSING: The Cast extension is not available. + * EXTENSION_NOT_COMPATIBLE: The API script is not compatible with the installed Cast extension. + * INVALID_PARAMETER: The parameters to the operation were not valid. + * LOAD_MEDIA_FAILED: Load media failed. + * RECEIVER_UNAVAILABLE: No receiver was compatible with the session request. + * SESSION_ERROR: A session could not be created, or a session was invalid. + * TIMEOUT: The operation timed out. + * @type {Object} + */ + ErrorCode: { + API_NOT_INITIALIZED: 'api_not_initialized', + CANCEL: 'cancel', + CHANNEL_ERROR: 'channel_error', + EXTENSION_MISSING: 'extension_missing', + EXTENSION_NOT_COMPATIBLE: 'extension_not_compatible', + INVALID_PARAMETER: 'invalid_parameter', + LOAD_MEDIA_FAILED: 'load_media_failed', + RECEIVER_UNAVAILABLE: 'receiver_unavailable', + SESSION_ERROR: 'session_error', + TIMEOUT: 'timeout', + UNKNOWN: 'unknown', + NOT_IMPLEMENTED: 'not_implemented' + }, + + /** + * TODO: Update when the official API docs are finished + * https://developers.google.com/cast/docs/reference/chrome/chrome.cast.timeout + * @type {Object} + */ + timeout: { + requestSession: 10000, + sendCustomMessage: 3000, + setReceiverVolume: 3000, + stopSession: 3000 + }, + + /** + * Flag for clients to check whether the API is loaded. + * @type {Boolean} + */ + isAvailable: false, + + /** + * [ApiConfig description] + * @param {chrome.cast.SessionRequest} sessionRequest Describes the session to launch or the session to connect. + * @param {function} sessionListener Listener invoked when a session is created or connected by the SDK. + * @param {function} receiverListener Function invoked when the availability of a Cast receiver that supports the application in sessionRequest is known or changes. + * @param {chrome.cast.AutoJoinPolicy} autoJoinPolicy Determines whether the SDK will automatically connect to a running session after initialization. + * @param {chrome.cast.DefaultActionPolicy} defaultActionPolicy Requests whether the application should be launched on API initialization when the tab is already being cast. + */ + ApiConfig: function (sessionRequest, sessionListener, receiverListener, autoJoinPolicy, defaultActionPolicy) { + this.sessionRequest = sessionRequest; + this.sessionListener = sessionListener; + this.receiverListener = receiverListener; + this.autoJoinPolicy = autoJoinPolicy || chrome.cast.AutoJoinPolicy.TAB_AND_ORIGIN_SCOPED; + this.defaultActionPolicy = defaultActionPolicy || chrome.cast.DefaultActionPolicy.CREATE_SESSION; + }, + + /** + * Describes the receiver running an application. Normally, these objects should not be created by the client. + * @param {string} label An identifier for the receiver that is unique to the browser profile and the origin of the API client. + * @param {string} friendlyName The user given name for the receiver. + * @param {chrome.cast.Capability[]} capabilities The capabilities of the receiver, for example audio and video. + * @param {chrome.cast.Volume} volume The current volume of the receiver. + */ + Receiver: function (label, friendlyName, capabilities, volume) { + this.label = label; + this.friendlyName = friendlyName; + this.capabilities = capabilities || []; + this.volume = volume || null; + this.receiverType = chrome.cast.ReceiverType.CAST; + this.isActiveInput = null; + }, + + /** + * TODO: Update when the official API docs are finished + * https://developers.google.com/cast/docs/reference/chrome/chrome.cast.DialRequest + * @param {[type]} appName [description] + * @param {[type]} launchParameter [description] + */ + DialRequest: function (appName, launchParameter) { + this.appName = appName; + this.launchParameter = launchParameter; + }, + + /** + * A request to start or connect to a session. + * @param {string} appId The receiver application id. + * @param {chrome.cast.Capability[]} capabilities Capabilities required of the receiver device. + * @property {chrome.cast.DialRequest} dialRequest If given, the SDK will also discover DIAL devices that support the DIAL application given in the dialRequest. + */ + SessionRequest: function (appId, capabilities) { + this.appId = appId; + this.capabilities = capabilities || [chrome.cast.Capability.VIDEO_OUT, chrome.cast.Capability.AUDIO_OUT]; + this.dialRequest = null; + }, + + /** + * Describes an error returned by the API. Normally, these objects should not be created by the client. + * @param {chrome.cast.ErrorCode} code The error code. + * @param {string} description Human readable description of the error. + * @param {Object} details Details specific to the error. + */ + Error: function (code, description, details) { + this.code = code; + this.description = description || null; + this.details = details || null; + }, + + /** + * An image that describes a receiver application or media item. This could be an application icon, cover art, or a thumbnail. + * @param {string} url The URL to the image. + * @property {number} height The height of the image + * @property {number} width The width of the image + */ + Image: function (url) { + this.url = url; + this.width = this.height = null; + }, + + /** + * Describes a sender application. Normally, these objects should not be created by the client. + * @param {chrome.cast.SenderPlatform} platform The supported platform. + * @property {string} packageId The identifier or URL for the application in the respective platform's app store. + * @property {string} url URL or intent to launch the application. + */ + SenderApplication: function (platform) { + this.platform = platform; + this.packageId = this.url = null; + }, + + /** + * The volume of a device or media stream. + * @param {number} level The current volume level as a value between 0.0 and 1.0. + * @param {boolean} muted Whether the receiver is muted, independent of the volume level. + */ + Volume: function (level, muted) { + this.level = level || null; + this.muted = null; + if (muted === true || muted === false) { + this.muted = muted; + } + }, + + // media package + media: { + /** + * The default receiver app. + */ + DEFAULT_MEDIA_RECEIVER_APP_ID: 'CC1AD845', + + /** + * Possible states of the media player. + * BUFFERING: Player is in PLAY mode but not actively playing content. currentTime will not change. + * IDLE: No media is loaded into the player. + * PAUSED: The media is not playing. + * PLAYING: The media is playing. + * @type {Object} + */ + PlayerState: { IDLE: 'IDLE', PLAYING: 'PLAYING', PAUSED: 'PAUSED', BUFFERING: 'BUFFERING' }, + + /** + * States of the media player after resuming. + * PLAYBACK_PAUSE: Force media to pause. + * PLAYBACK_START: Force media to start. + * @type {Object} + */ + ResumeState: { PLAYBACK_START: 'PLAYBACK_START', PLAYBACK_PAUSE: 'PLAYBACK_PAUSE' }, + + /** + * Possible media commands supported by the receiver application. + * @type {Object} + */ + MediaCommand: { PAUSE: 'pause', SEEK: 'seek', STREAM_VOLUME: 'stream_volume', STREAM_MUTE: 'stream_mute' }, + + /** + * Possible types of media metadata. + * GENERIC: Generic template suitable for most media types. Used by chrome.cast.media.GenericMediaMetadata. + * MOVIE: A full length movie. Used by chrome.cast.media.MovieMediaMetadata. + * MUSIC_TRACK: A music track. Used by chrome.cast.media.MusicTrackMediaMetadata. + * PHOTO: Photo. Used by chrome.cast.media.PhotoMediaMetadata. + * TV_SHOW: An episode of a TV series. Used by chrome.cast.media.TvShowMediaMetadata. + * @type {Object} + */ + MetadataType: { GENERIC: 0, TV_SHOW: 1, MOVIE: 2, MUSIC_TRACK: 3, PHOTO: 4 }, + + /** + * Possible media stream types. + * BUFFERED: Stored media streamed from an existing data store. + * LIVE: Live media generated on the fly. + * OTHER: None of the above. + * @type {Object} + */ + StreamType: { BUFFERED: 'buffered', LIVE: 'live', OTHER: 'other' }, + + /** + * TODO: Update when the official API docs are finished + * https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.timeout + * @type {Object} + */ + timeout: { + load: 0, + ob: 0, + pause: 0, + play: 0, + seek: 0, + setVolume: 0, + stop: 0 + }, + + /** + * A request to load new media into the player. + * @param {chrome.cast.media.MediaInfo} media Media description. + * @property {boolean} autoplay Whether the media will automatically play. + * @property {number} currentTime Seconds from the beginning of the media to start playback. + * @property {Object} customData Custom data for the receiver application. + */ + LoadRequest: function (media) { + this.type = 'LOAD'; + this.sessionId = this.requestId = this.customData = this.currentTime = null; + this.media = media; + this.autoplay = !0; + }, + + /** + * A request to play the currently paused media. + * @property {Object} customData Custom data for the receiver application. + */ + PlayRequest: function () { + this.customData = null; + }, + + /** + * A request to seek the current media. + * @property {number} currentTime The new current time for the media, in seconds after the start of the media. + * @property {chrome.cast.media.ResumeState} resumeState The desired media player state after the seek is complete. + * @property {Object} customData Custom data for the receiver application. + */ + SeekRequest: function () { + this.customData = this.resumeState = this.currentTime = null; + }, + + /** + * A request to set the stream volume of the playing media. + * @param {chrome.cast.Volume} volume The new volume of the stream. + * @property {Object} customData Custom data for the receiver application. + */ + VolumeRequest: function (volume) { + this.volume = volume; + this.customData = null; + }, + + /** + * A request to stop the media player. + * @property {Object} customData Custom data for the receiver application. + */ + StopRequest: function () { + this.customData = null; + }, + + /** + * A request to pause the currently playing media. + * @property {Object} customData Custom data for the receiver application. + */ + PauseRequest: function () { + this.customData = null; + }, + + /** + * A generic media description. + * @property {chrome.cast.Image[]} images Content images. + * @property {string} releaseDate ISO 8601 date and/or time when the content was released, e.g. + * @property {number} releaseYear Integer year when the content was released. + * @property {string} subtitle Content subtitle. + * @property {string} title Content title. + * @property {chrome.cast.media.MetadataType} type The type of metadata. + */ + GenericMediaMetadata: function () { + this.metadataType = this.type = chrome.cast.media.MetadataType.GENERIC; + this.releaseDate = this.releaseYear = this.images = this.subtitle = this.title = null; + }, + + /** + * A movie media description. + * @property {chrome.cast.Image[]} images Content images. + * @property {string} releaseDate ISO 8601 date and/or time when the content was released, e.g. + * @property {number} releaseYear Integer year when the content was released. + * @property {string} studio Movie studio + * @property {string} subtitle Content subtitle. + * @property {string} title Content title. + * @property {chrome.cast.media.MetadataType} type The type of metadata. + */ + MovieMediaMetadata: function () { + this.metadataType = this.type = chrome.cast.media.MetadataType.MOVIE; + this.releaseDate = this.releaseYear = this.images = this.subtitle = this.studio = this.title = null; + }, + + /** + * A music track media description. + * @property {string} albumArtist Album artist name. + * @property {string} albumName Album name. + * @property {string} artist Track artist name. + * @property {string} artistName Track artist name. + * @property {string} composer Track composer name. + * @property {number} discNumber Disc number. + * @property {chrome.cast.Image[]} images Content images. + * @property {string} releaseDate ISO 8601 date when the track was released, e.g. + * @property {number} releaseYear Integer year when the album was released. + * @property {string} songName Track name. + * @property {string} title Track title. + * @property {number} trackNumber Track number in album. + * @property {chrome.cast.media.MetadataType} type The type of metadata. + */ + MusicTrackMediaMetadata: function () { + this.metadataType = this.type = chrome.cast.media.MetadataType.MUSIC_TRACK; + this.releaseDate = this.releaseYear = this.images = this.discNumber = this.trackNumber = this.artistName = this.songName = this.composer = this.artist = this.albumArtist = this.title = this.albumName = null; + }, + + /** + * A photo media description. + * @property {string} artist Name of the photographer. + * @property {string} creationDateTime ISO 8601 date and time the photo was taken, e.g. + * @property {number} height Photo height, in pixels. + * @property {chrome.cast.Image[]} images Images associated with the content. + * @property {number} latitude Latitude. + * @property {string} location Location where the photo was taken. + * @property {number} longitude Longitude. + * @property {string} title Photo title. + * @property {chrome.cast.media.MetadataType} type The type of metadata. + * @property {number} width Photo width, in pixels. + */ + PhotoMediaMetadata: function () { + this.metadataType = this.type = chrome.cast.media.MetadataType.PHOTO; + this.creationDateTime = this.height = this.width = this.longitude = this.latitude = this.images = this.location = this.artist = this.title = null; + }, + + /** + * [TvShowMediaMetadata description] + * @property {number} episode TV episode number. + * @property {number} episodeNumber TV episode number. + * @property {string} episodeTitle TV episode title. + * @property {chrome.cast.Image[]} images Content images. + * @property {string} originalAirdate ISO 8601 date when the episode originally aired, e.g. + * @property {number} releaseYear Integer year when the content was released. + * @property {number} season TV episode season. + * @property {number} seasonNumber TV episode season. + * @property {string} seriesTitle TV series title. + * @property {string} title TV episode title. + * @property {chrome.cast.media.MetadataType} type The type of metadata. + */ + TvShowMediaMetadata: function () { + this.metadataType = this.type = chrome.cast.media.MetadataType.TV_SHOW; + this.originalAirdate = this.releaseYear = this.images = this.episode = this.episodeNumber = this.season = this.seasonNumber = this.episodeTitle = this.title = this.seriesTitle = null; + }, + + /** + * Describes a media item. + * @param {string} contentId Identifies the content. + * @param {string} contentType MIME content type of the media. + * @property {Object} customData Custom data set by the receiver application. + * @property {number} duration Duration of the content, in seconds. + * @property {any type} metadata Describes the media content. + * @property {chrome.cast.media.StreamType} streamType The type of media stream. + */ + MediaInfo: function (contentId, contentType) { + this.contentId = contentId; + this.streamType = chrome.cast.media.StreamType.BUFFERED; + this.contentType = contentType; + this.customData = this.duration = this.metadata = null; + }, + + /** + * Possible media track types. + */ + TrackType: {TEXT: 'TEXT', AUDIO: 'AUDIO', VIDEO: 'VIDEO'}, + + /** + * Possible text track types. + */ + TextTrackType: {SUBTITLES: 'SUBTITLES', CAPTIONS: 'CAPTIONS', DESCRIPTIONS: 'DESCRIPTIONS', CHAPTERS: 'CHAPTERS', METADATA: 'METADATA'}, + + /** + * Describes track metadata information + * @param {number} trackId Unique identifier of the track within the context of a chrome.cast.media.MediaInfo objects + * @param {chrome.cast.media.TrackType} trackType The type of track. Value must not be null. + */ + Track: function (trackId, trackType) { + this.trackId = trackId; + this.type = trackType; + this.customData = this.language = this.name = this.subtype = this.trackContentId = this.trackContentType = null; + }, + + /** + * Possible text track edge types. + */ + TextTrackEdgeType: {NONE: 'NONE', OUTLINE: 'OUTLINE', DROP_SHADOW: 'DROP_SHADOW', RAISED: 'RAISED', DEPRESSED: 'DEPRESSED'}, + + /** + * Possible text track font generic family. + */ + TextTrackFontGenericFamily: { + CURSIVE: 'CURSIVE', + MONOSPACED_SANS_SERIF: 'MONOSPACED_SANS_SERIF', + MONOSPACED_SERIF: 'MONOSPACED_SERIF', + SANS_SERIF: 'SANS_SERIF', + SERIF: 'SERIF', + SMALL_CAPITALS: 'SMALL_CAPITALS' + }, + + /** + * Possible text track font style. + */ + TextTrackFontStyle: {NORMAL: 'NORMAL', BOLD: 'BOLD', BOLD_ITALIC: 'BOLD_ITALIC', ITALIC: 'ITALIC'}, + + /** + * Possible text track window types. + */ + TextTrackWindowType: {NONE: 'NONE', NORMAL: 'NORMAL', ROUNDED_CORNERS: 'ROUNDED_CORNERS'}, + + /** + * Describes style information for a text track. + * + * Colors are represented as strings "#RRGGBBAA" where XX are the two hexadecimal symbols that represent + * the 0-255 value for the specific channel/color. It follows CSS 8-digit hex color notation (See + * http://dev.w3.org/csswg/css-color/#hex-notation). + */ + TextTrackStyle: function () { + this.backgroundColor = this.customData = this.edgeColor = this.edgeType = + this.fontFamily = this.fontGenericFamily = this.fontScale = this.fontStyle = + this.foregroundColor = this.windowColor = this.windowRoundedCornerRadius = + this.windowType = null; + }, + + /** + * A request to modify the text tracks style or change the tracks status. If a trackId does not match + * the existing trackIds the whole request will fail and no status will change. It is acceptable to + * change the text track style even if no text track is currently active. + * @param {number[]} opt_activeTrackIds Optional. + * @param {chrome.cast.media.TextTrackStyle} opt_textTrackSytle Optional. + **/ + EditTracksInfoRequest: function (opt_activeTrackIds, opt_textTrackSytle) { + this.activeTrackIds = opt_activeTrackIds; + this.textTrackSytle = opt_textTrackSytle; + this.requestId = null; + } + } }; var _sessionRequest = null; @@ -545,66 +545,65 @@ var _receiverAvailable = false; * @param {function} errorCallback */ chrome.cast.initialize = function (apiConfig, successCallback, errorCallback) { - if (!chrome.cast.isAvailable) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - - _sessionListener = apiConfig.sessionListener; - _autoJoinPolicy = apiConfig.autoJoinPolicy; - _defaultActionPolicy = apiConfig.defaultActionPolicy; - _receiverListener = apiConfig.receiverListener; - _sessionRequest = apiConfig.sessionRequest; - - execute('initialize', _sessionRequest.appId, _autoJoinPolicy, _defaultActionPolicy, function(err) { - if (!err) { - successCallback(); - - clearInterval(_routeRefreshInterval); - _routeRefreshInterval = setInterval(function() { - execute('emitAllRoutes'); - }, 15000); - - setTimeout(function() { execute('emitAllRoutes'); }, 2000); - } else { - handleError(err, errorCallback); - } - }); + if (!chrome.cast.isAvailable) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + + _sessionListener = apiConfig.sessionListener; + _autoJoinPolicy = apiConfig.autoJoinPolicy; + _defaultActionPolicy = apiConfig.defaultActionPolicy; + _receiverListener = apiConfig.receiverListener; + _sessionRequest = apiConfig.sessionRequest; + + execute('initialize', _sessionRequest.appId, _autoJoinPolicy, _defaultActionPolicy, function (err) { + if (!err) { + successCallback(); + + clearInterval(_routeRefreshInterval); + _routeRefreshInterval = setInterval(function () { + execute('emitAllRoutes'); + }, 15000); + + setTimeout(function () { execute('emitAllRoutes'); }, 2000); + } else { + handleError(err, errorCallback); + } + }); }; /** * Requests that a receiver application session be created or joined. * By default, the SessionRequest passed to the API at initialization time is used; - * this may be overridden by passing a different session request in opt_sessionRequest. - * @param {function} successCallback - * @param {function} errorCallback The possible errors are TIMEOUT, INVALID_PARAMETER, API_NOT_INITIALIZED, CANCEL, CHANNEL_ERROR, SESSION_ERROR, RECEIVER_UNAVAILABLE, and EXTENSION_MISSING. Note that the timeout timer starts after users select a receiver. Selecting a receiver requires user's action, which has no timeout. + * this may be overridden by passing a different session request in opt_sessionRequest. + * @param {function} successCallback + * @param {function} errorCallback The possible errors are TIMEOUT, INVALID_PARAMETER, API_NOT_INITIALIZED, CANCEL, CHANNEL_ERROR, SESSION_ERROR, RECEIVER_UNAVAILABLE, and EXTENSION_MISSING. Note that the timeout timer starts after users select a receiver. Selecting a receiver requires user's action, which has no timeout. * @param {chrome.cast.SessionRequest} opt_sessionRequest */ chrome.cast.requestSession = function (successCallback, errorCallback, opt_sessionRequest) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - if (_receiverAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.RECEIVER_UNAVAILABLE, 'No receiver was compatible with the session request.', {})); - return; - } - - execute('requestSession', function(err, obj) { - if (!err) { - if (obj === 'cancel') { - return - } - var sessionId = obj.sessionId; - var appId = obj.appId; - var displayName = obj.displayName; - var appImages = obj.appImages || []; - var receiver = new chrome.cast.Receiver(obj.receiver.label, obj.receiver.friendlyName, obj.receiver.capabilities || [], obj.receiver.volume || null); - - var session = _sessions[sessionId] = new chrome.cast.Session(sessionId, appId, displayName, appImages, receiver); - - if (obj.media && obj.media.sessionId) - { + if (chrome.cast.isAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + if (_receiverAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.RECEIVER_UNAVAILABLE, 'No receiver was compatible with the session request.', {})); + return; + } + + execute('requestSession', function (err, obj) { + if (!err) { + if (obj === 'cancel') { + return; + } + var sessionId = obj.sessionId; + var appId = obj.appId; + var displayName = obj.displayName; + var appImages = obj.appImages || []; + var receiver = new chrome.cast.Receiver(obj.receiver.label, obj.receiver.friendlyName, obj.receiver.capabilities || [], obj.receiver.volume || null); + + var session = _sessions[sessionId] = new chrome.cast.Session(sessionId, appId, displayName, appImages, receiver); + + if (obj.media && obj.media.sessionId) { _currentMedia = new chrome.cast.media.Media(sessionId, obj.media.mediaSessionId); _currentMedia.currentTime = obj.media.currentTime; _currentMedia.playerState = obj.media.playerState; @@ -612,90 +611,90 @@ chrome.cast.requestSession = function (successCallback, errorCallback, opt_sessi session.media[0] = _currentMedia; } - successCallback(session); - _sessionListener(session); /*Fix - Already has a sessionListener*/ - } else { - handleError(err, errorCallback); - } - }); + successCallback(session); + _sessionListener(session); /* Fix - Already has a sessionListener */ + } else { + handleError(err, errorCallback); + } + }); }; /** * Sets custom receiver list - * @param {chrome.cast.Receiver[]} receivers The new list. Must not be null. - * @param {function} successCallback - * @param {function} errorCallback + * @param {chrome.cast.Receiver[]} receivers The new list. Must not be null. + * @param {function} successCallback + * @param {function} errorCallback */ chrome.cast.setCustomReceivers = function (receivers, successCallback, errorCallback) { - // TODO: Implement + // TODO: Implement }; /** * Describes the state of a currently running Cast application. Normally, these objects should not be created by the client. - * @param {string} sessionId Uniquely identifies this instance of the receiver application. - * @param {string} appId The identifer of the Cast application. - * @param {string} displayName The human-readable name of the Cast application, for example, "YouTube". - * @param {chrome.cast.Image[]} appImages Array of images available describing the application. - * @param {chrome.cast.Receiver} receiver The receiver that is running the application. + * @param {string} sessionId Uniquely identifies this instance of the receiver application. + * @param {string} appId The identifer of the Cast application. + * @param {string} displayName The human-readable name of the Cast application, for example, "YouTube". + * @param {chrome.cast.Image[]} appImages Array of images available describing the application. + * @param {chrome.cast.Receiver} receiver The receiver that is running the application. * - * @property {Object} customData Custom data set by the receiver application. - * @property {chrome.cast.media.Media} media The media that belong to this Cast session, including those loaded by other senders. - * @property {Object[]} namespaces A list of the namespaces supported by the receiver application. - * @property {chrome.cast.SenderApplication} senderApps The sender applications supported by the receiver application. - * @property {string} statusText Descriptive text for the current application content, for example “My Wedding Slideshow”. + * @property {Object} customData Custom data set by the receiver application. + * @property {chrome.cast.media.Media} media The media that belong to this Cast session, including those loaded by other senders. + * @property {Object[]} namespaces A list of the namespaces supported by the receiver application. + * @property {chrome.cast.SenderApplication} senderApps The sender applications supported by the receiver application. + * @property {string} statusText Descriptive text for the current application content, for example “My Wedding Slideshow”. */ -chrome.cast.Session = function(sessionId, appId, displayName, appImages, receiver) { - EventEmitter.call(this); - this.sessionId = sessionId; - this.appId = appId; - this.displayName = displayName; - this.appImages = appImages || []; - this.receiver = receiver; - this.media = []; +chrome.cast.Session = function (sessionId, appId, displayName, appImages, receiver) { + EventEmitter.call(this); + this.sessionId = sessionId; + this.appId = appId; + this.displayName = displayName; + this.appImages = appImages || []; + this.receiver = receiver; + this.media = []; }; chrome.cast.Session.prototype = Object.create(EventEmitter.prototype); /** * Sets the receiver volume. - * @param {number} newLevel The new volume level between 0.0 and 1.0. - * @param {function} successCallback - * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, and EXTENSION_MISSING. + * @param {number} newLevel The new volume level between 0.0 and 1.0. + * @param {function} successCallback + * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.setReceiverVolumeLevel = function (newLevel, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - - execute('setReceiverVolumeLevel', newLevel, function(err) { - if (!err) { - successCallback && successCallback(); - } else { - handleError(err, errorCallback); - } - }); + if (chrome.cast.isAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + + execute('setReceiverVolumeLevel', newLevel, function (err) { + if (!err) { + successCallback && successCallback(); + } else { + handleError(err, errorCallback); + } + }); }; /** * Sets the receiver volume. - * @param {boolean} muted The new muted status. + * @param {boolean} muted The new muted status. * @param {function} successCallback - * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, and EXTENSION_MISSING. + * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.setReceiverMuted = function (muted, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - - execute('setReceiverMuted', muted, function(err) { - if (!err) { - successCallback && successCallback(); - } else { - handleError(err, errorCallback); - } - }); + if (chrome.cast.isAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + + execute('setReceiverMuted', muted, function (err) { + if (!err) { + successCallback && successCallback(); + } else { + handleError(err, errorCallback); + } + }); }; /** @@ -704,18 +703,18 @@ chrome.cast.Session.prototype.setReceiverMuted = function (muted, successCallbac * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, CHANNEL_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.stop = function (successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - - execute('sessionStop', function(err) { - if (!err) { - successCallback && successCallback(); - } else { - handleError(err, errorCallback); - } - }); + if (chrome.cast.isAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + + execute('sessionStop', function (err) { + if (!err) { + successCallback && successCallback(); + } else { + handleError(err, errorCallback); + } + }); }; /** @@ -724,45 +723,45 @@ chrome.cast.Session.prototype.stop = function (successCallback, errorCallback) { * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, CHANNEL_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.leave = function (successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - - execute('sessionLeave', function(err) { - if (!err) { - successCallback && successCallback(); - } else { - handleError(err, errorCallback); - } - }); + if (chrome.cast.isAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + + execute('sessionLeave', function (err) { + if (!err) { + successCallback && successCallback(); + } else { + handleError(err, errorCallback); + } + }); }; /** * Sends a message to the receiver application on the given namespace. * The successCallback is invoked when the message has been submitted to the messaging channel. * Delivery to the receiver application is best effort and not guaranteed. - * @param {string} namespace - * @param {Object or string} message Must not be null - * @param {[type]} successCallback Invoked when the message has been sent. Must not be null. - * @param {[type]} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING + * @param {string} namespace + * @param {Object or string} message Must not be null + * @param {[type]} successCallback Invoked when the message has been sent. Must not be null. + * @param {[type]} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING */ chrome.cast.Session.prototype.sendMessage = function (namespace, message, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - - if (typeof message === 'object') { - message = JSON.stringify(message); - } - execute('sendMessage', namespace, message, function(err) { - if (!err) { - successCallback && successCallback(); - } else { - handleError(err, errorCallback); - } - }); + if (chrome.cast.isAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + + if (typeof message === 'object') { + message = JSON.stringify(message); + } + execute('sendMessage', namespace, message, function (err) { + if (!err) { + successCallback && successCallback(); + } else { + handleError(err, errorCallback); + } + }); }; /** @@ -772,50 +771,50 @@ chrome.cast.Session.prototype.sendMessage = function (namespace, message, succes * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.loadMedia = function (loadRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - - var self = this; - - var mediaInfo = loadRequest.media; - execute('loadMedia', mediaInfo.contentId, mediaInfo.customData || {}, mediaInfo.contentType, mediaInfo.duration || 0.0, mediaInfo.streamType, loadRequest.autoplay || false, loadRequest.currentTime || 0, mediaInfo.metadata || {}, mediaInfo.textTrackSytle || {}, function(err, obj) { - if (!err) { - _currentMedia = new chrome.cast.media.Media(self.sessionId, obj.mediaSessionId); - _currentMedia.activeTrackIds = obj.activeTrackIds; - _currentMedia.currentItemId = obj.currentItemId; - _currentMedia.idleReason = obj.idleReason; - _currentMedia.loadingItemId = obj.loadingItemId; - _currentMedia.media = mediaInfo; - _currentMedia.media.duration = obj.media.duration; - _currentMedia.media.tracks = obj.media.tracks; - _currentMedia.media.customData = obj.media.customData || null; - _currentMedia.currentTime = obj.currentTime; - _currentMedia.playbackRate = obj.playbackRate; - _currentMedia.preloadedItemId = obj.preloadedItemId; - _currentMedia.volume = new chrome.cast.Volume(obj.volume.level, obj.volume.muted); - - _currentMedia.media.tracks = []; - - obj.media.tracks.forEach((track) => { - let newTrack = new chrome.cast.media.Track(track.trackId, track.type); - newTrack.customData = track.customData || null; - newTrack.language = track.language || null; - newTrack.name = track.name || null; - newTrack.subtype = track.subtype || null; - newTrack.trackContentId = track.trackContentId || null; - newTrack.trackContentType = track.trackContentType || null; - - _currentMedia.media.tracks.push(newTrack); - }) - - successCallback(_currentMedia); - - } else { - handleError(err, errorCallback); - } - }); + if (chrome.cast.isAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + + var self = this; + + var mediaInfo = loadRequest.media; + execute('loadMedia', mediaInfo.contentId, mediaInfo.customData || {}, mediaInfo.contentType, mediaInfo.duration || 0.0, mediaInfo.streamType, loadRequest.autoplay || false, loadRequest.currentTime || 0, mediaInfo.metadata || {}, mediaInfo.textTrackSytle || {}, function (err, obj) { + if (!err) { + _currentMedia = new chrome.cast.media.Media(self.sessionId, obj.mediaSessionId); + _currentMedia.activeTrackIds = obj.activeTrackIds; + _currentMedia.currentItemId = obj.currentItemId; + _currentMedia.idleReason = obj.idleReason; + _currentMedia.loadingItemId = obj.loadingItemId; + _currentMedia.media = mediaInfo; + _currentMedia.media.duration = obj.media.duration; + _currentMedia.media.tracks = obj.media.tracks; + _currentMedia.media.customData = obj.media.customData || null; + _currentMedia.currentTime = obj.currentTime; + _currentMedia.playbackRate = obj.playbackRate; + _currentMedia.preloadedItemId = obj.preloadedItemId; + _currentMedia.volume = new chrome.cast.Volume(obj.volume.level, obj.volume.muted); + + _currentMedia.media.tracks = []; + + obj.media.tracks.forEach((track) => { + let newTrack = new chrome.cast.media.Track(track.trackId, track.type); + newTrack.customData = track.customData || null; + newTrack.language = track.language || null; + newTrack.name = track.name || null; + newTrack.subtype = track.subtype || null; + newTrack.trackContentId = track.trackContentId || null; + newTrack.trackContentType = track.trackContentType || null; + + _currentMedia.media.tracks.push(newTrack); + }); + + successCallback(_currentMedia); + + } else { + handleError(err, errorCallback); + } + }); }; /** @@ -826,7 +825,7 @@ chrome.cast.Session.prototype.loadMedia = function (loadRequest, successCallback * @param {function} listener The listener to add. */ chrome.cast.Session.prototype.addUpdateListener = function (listener) { - this.on('_sessionUpdated', listener); + this.on('_sessionUpdated', listener); }; /** @@ -834,7 +833,7 @@ chrome.cast.Session.prototype.addUpdateListener = function (listener) { * @param {function} listener The listener to remove. */ chrome.cast.Session.prototype.removeUpdateListener = function (listener) { - this.removeListener('_sessionUpdated', listener); + this.removeListener('_sessionUpdated', listener); }; /** @@ -844,8 +843,8 @@ chrome.cast.Session.prototype.removeUpdateListener = function (listener) { * @param {function} listener The listener to add. */ chrome.cast.Session.prototype.addMessageListener = function (namespace, listener) { - execute('addMessageListener', namespace); - this.on('message:' + namespace, listener); + execute('addMessageListener', namespace); + this.on('message:' + namespace, listener); }; /** @@ -854,7 +853,7 @@ chrome.cast.Session.prototype.addMessageListener = function (namespace, listener * @param {function} listener The listener to remove. */ chrome.cast.Session.prototype.removeMessageListener = function (namespace, listener) { - this.removeListener('message:' + namespace, listener); + this.removeListener('message:' + namespace, listener); }; /** @@ -862,7 +861,7 @@ chrome.cast.Session.prototype.removeMessageListener = function (namespace, liste * @param {function} listener The listener to add. */ chrome.cast.Session.prototype.addMediaListener = function (listener) { - this.on('_mediaListener', listener); + this.on('_mediaListener', listener); }; /** @@ -870,27 +869,27 @@ chrome.cast.Session.prototype.addMediaListener = function (listener) { * @param {function} listener The listener to remove. */ chrome.cast.Session.prototype.removeMediaListener = function (listener) { - this.removeListener('_mediaListener', listener); + this.removeListener('_mediaListener', listener); }; -chrome.cast.Session.prototype._update = function(isAlive, obj) { - this.appId = obj.appId; - this.appImages = obj.appImages; - this.displayName = obj.displayName; +chrome.cast.Session.prototype._update = function (isAlive, obj) { + this.appId = obj.appId; + this.appImages = obj.appImages; + this.displayName = obj.displayName; - if (obj.receiver) { - if (!this.receiver) { - this.receiver = new chrome.cast.Receiver(null, null, null, null); - } - this.receiver.friendlyName = obj.receiver.friendlyName; - this.receiver.label = obj.receiver.label; + if (obj.receiver) { + if (!this.receiver) { + this.receiver = new chrome.cast.Receiver(null, null, null, null); + } + this.receiver.friendlyName = obj.receiver.friendlyName; + this.receiver.label = obj.receiver.label; - if (obj.receiver.volume) { - this.receiver.volume = new chrome.cast.Volume(obj.receiver.volume.level, obj.receiver.volume.muted); - } - } + if (obj.receiver.volume) { + this.receiver.volume = new chrome.cast.Volume(obj.receiver.volume.level, obj.receiver.volume.muted); + } + } - this.emit('_sessionUpdated', isAlive); + this.emit('_sessionUpdated', isAlive); }; /** @@ -898,120 +897,120 @@ chrome.cast.Session.prototype._update = function(isAlive, obj) { * @param {string} sessionId Identifies the session that is hosting the media. * @param {number} mediaSessionId Identifies the media item. * - * @property {Object} customData Custom data set by the receiver application. - * @property {number} currentTime The current playback position in seconds since the start of the media. - * @property {chrome.cast.media.MediaInfo} media Media description. - * @property {number} playbackRate The playback rate. - * @property {chrome.cast.media.PlayerState} playerState The player state. + * @property {Object} customData Custom data set by the receiver application. + * @property {number} currentTime The current playback position in seconds since the start of the media. + * @property {chrome.cast.media.MediaInfo} media Media description. + * @property {number} playbackRate The playback rate. + * @property {chrome.cast.media.PlayerState} playerState The player state. * @property {chrome.cast.media.MediaCommand[]} supportedMediaCommands The media commands supported by the media player. - * @property {chrome.cast.Volume} volume The media stream volume. - * @property {string} idleReason Reason for idling + * @property {chrome.cast.Volume} volume The media stream volume. + * @property {string} idleReason Reason for idling */ -chrome.cast.media.Media = function(sessionId, mediaSessionId) { - EventEmitter.call(this); - this.sessionId = sessionId; - this.mediaSessionId = mediaSessionId; - this.currentTime = 0; - this.playbackRate = 1; - this.playerState = chrome.cast.media.PlayerState.BUFFERING; - this.supportedMediaCommands = [ - chrome.cast.media.MediaCommand.PAUSE, - chrome.cast.media.MediaCommand.SEEK, - chrome.cast.media.MediaCommand.STREAM_VOLUME, - chrome.cast.media.MediaCommand.STREAM_MUTE - ]; - this.volume = new chrome.cast.Volume(1, false); - this._lastUpdatedTime = Date.now(); - this.media = {}; +chrome.cast.media.Media = function (sessionId, mediaSessionId) { + EventEmitter.call(this); + this.sessionId = sessionId; + this.mediaSessionId = mediaSessionId; + this.currentTime = 0; + this.playbackRate = 1; + this.playerState = chrome.cast.media.PlayerState.BUFFERING; + this.supportedMediaCommands = [ + chrome.cast.media.MediaCommand.PAUSE, + chrome.cast.media.MediaCommand.SEEK, + chrome.cast.media.MediaCommand.STREAM_VOLUME, + chrome.cast.media.MediaCommand.STREAM_MUTE + ]; + this.volume = new chrome.cast.Volume(1, false); + this._lastUpdatedTime = Date.now(); + this.media = {}; }; chrome.cast.media.Media.prototype = Object.create(EventEmitter.prototype); /** * Plays the media item. - * @param {chrome.cast.media.PlayRequest} playRequest The optional media play request. - * @param {function} successCallback Invoked on success. - * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. + * @param {chrome.cast.media.PlayRequest} playRequest The optional media play request. + * @param {function} successCallback Invoked on success. + * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.play = function (playRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - - execute('mediaPlay', function(err) { - if (!err) { - successCallback && successCallback(); - } else { - handleError(err, errorCallback); - } - }); + if (chrome.cast.isAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + + execute('mediaPlay', function (err) { + if (!err) { + successCallback && successCallback(); + } else { + handleError(err, errorCallback); + } + }); }; /** * Pauses the media item. * @param {chrome.cast.media.PauseRequest} pauseRequest The optional media pause request. - * @param {function} successCallback Invoked on success. - * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. + * @param {function} successCallback Invoked on success. + * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.pause = function (pauseRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - - execute('mediaPause', function(err) { - if (!err) { - successCallback && successCallback(); - } else { - handleError(err, errorCallback); - } - }); + if (chrome.cast.isAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + + execute('mediaPause', function (err) { + if (!err) { + successCallback && successCallback(); + } else { + handleError(err, errorCallback); + } + }); }; /** * Seeks the media item. - * @param {chrome.cast.media.SeekRequest} seekRequest The media seek request. Must not be null. - * @param {function} successCallback Invoked on success. - * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. + * @param {chrome.cast.media.SeekRequest} seekRequest The media seek request. Must not be null. + * @param {function} successCallback Invoked on success. + * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.seek = function (seekRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - - const currentTime = Math.round(seekRequest.currentTime); - const resumeState = seekRequest.resumeState || ""; - - execute('mediaSeek', currentTime, resumeState, function(err) { - if (!err) { - successCallback && successCallback(); - } else { - handleError(err, errorCallback); - } - }) + if (chrome.cast.isAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + + const currentTime = Math.round(seekRequest.currentTime); + const resumeState = seekRequest.resumeState || ''; + + execute('mediaSeek', currentTime, resumeState, function (err) { + if (!err) { + successCallback && successCallback(); + } else { + handleError(err, errorCallback); + } + }); }; /** * Stops the media player. - * @param {chrome.cast.media.StopRequest} stopRequest The media stop request. - * @param {function} successCallback Invoked on success. - * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. + * @param {chrome.cast.media.StopRequest} stopRequest The media stop request. + * @param {function} successCallback Invoked on success. + * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.stop = function (stopRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - - execute('mediaStop', function(err) { - if (!err) { - successCallback && successCallback(); - } else { - handleError(err, errorCallback); - } - }) + if (chrome.cast.isAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + + execute('mediaStop', function (err) { + if (!err) { + successCallback && successCallback(); + } else { + handleError(err, errorCallback); + } + }); }; /** @@ -1021,40 +1020,40 @@ chrome.cast.media.Media.prototype.stop = function (stopRequest, successCallback, * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.setVolume = function (volumeRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - var argsMuted = []; - var argsVolume = []; - - if (volumeRequest.volume.muted !== null) { - argsMuted.push('setMediaMuted'); - argsMuted.push(volumeRequest.volume.muted); - } - - if (volumeRequest.volume.level) { - argsVolume.push('setMediaVolume'); - argsVolume.push(volumeRequest.volume.level); - } - - if (argsMuted.length < 2 && argsVolume.length < 2) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.INVALID_PARAMETER), 'Invalid request.', {}); - } else { - var callback = (function(err) { - if (!err) { - successCallback && successCallback(); - } else { - handleError(err, errorCallback); - } - }); - - argsMuted.push(callback); - argsVolume.push(callback); - - execute.apply(null, argsMuted); - execute.apply(null, argsVolume); - } + if (chrome.cast.isAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + var argsMuted = []; + var argsVolume = []; + + if (volumeRequest.volume.muted !== null) { + argsMuted.push('setMediaMuted'); + argsMuted.push(volumeRequest.volume.muted); + } + + if (volumeRequest.volume.level) { + argsVolume.push('setMediaVolume'); + argsVolume.push(volumeRequest.volume.level); + } + + if (argsMuted.length < 2 && argsVolume.length < 2) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.INVALID_PARAMETER), 'Invalid request.', {}); + } else { + var callback = function (err) { + if (!err) { + successCallback && successCallback(); + } else { + handleError(err, errorCallback); + } + }; + + argsMuted.push(callback); + argsVolume.push(callback); + + execute.apply(null, argsMuted); + execute.apply(null, argsVolume); + } }; /** @@ -1063,7 +1062,7 @@ chrome.cast.media.Media.prototype.setVolume = function (volumeRequest, successCa * @returns {boolean} True if the player supports the command. */ chrome.cast.media.Media.prototype.supportsCommand = function (command) { - return this.supportsCommands.indexOf(command) > -1; + return this.supportsCommands.indexOf(command) > -1; }; /** @@ -1071,41 +1070,41 @@ chrome.cast.media.Media.prototype.supportsCommand = function (command) { * @returns {number} number An estimate of the current playback position in seconds since the start of the media. */ chrome.cast.media.Media.prototype.getEstimatedTime = function () { - if (this.playerState === chrome.cast.media.PlayerState.PLAYING) { - var elapsed = (Date.now() - this._lastUpdatedTime) / 1000; - var estimatedTime = this.currentTime + elapsed; - - return estimatedTime; - } else { - return this.currentTime; - } + if (this.playerState === chrome.cast.media.PlayerState.PLAYING) { + var elapsed = (Date.now() - this._lastUpdatedTime) / 1000; + var estimatedTime = this.currentTime + elapsed; + + return estimatedTime; + } else { + return this.currentTime; + } }; /** * Modifies the text tracks style or change the tracks status. If a trackId does not match * the existing trackIds the whole request will fail and no status will change. - * @param {chrome.cast.media.EditTracksInfoRequest} editTracksInfoRequest Value must not be null. - * @param {function()} successCallback Invoked on success. - * @param {function(not-null chrome.cast.Error)} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. + * @param {chrome.cast.media.EditTracksInfoRequest} editTracksInfoRequest Value must not be null. + * @param {function()} successCallback Invoked on success. + * @param {function(not-null chrome.cast.Error)} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. **/ - chrome.cast.media.Media.prototype.editTracksInfo = function (editTracksInfoRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - - var activeTracks = editTracksInfoRequest.activeTrackIds; - var textTrackSytle = editTracksInfoRequest.textTrackSytle; - - execute('mediaEditTracksInfo', activeTracks, textTrackSytle || {}, function (err) { - if (!err) { - successCallback && successCallback(); - } else { - handleError(err, errorCallback); - } - }) +chrome.cast.media.Media.prototype.editTracksInfo = function (editTracksInfoRequest, successCallback, errorCallback) { + if (chrome.cast.isAvailable === false) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return; + } + + var activeTracks = editTracksInfoRequest.activeTrackIds; + var textTrackSytle = editTracksInfoRequest.textTrackSytle; + + execute('mediaEditTracksInfo', activeTracks, textTrackSytle || {}, function (err) { + if (!err) { + successCallback && successCallback(); + } else { + handleError(err, errorCallback); + } + }); - } +}; /** * Adds a listener that is invoked when the status of the media has changed. @@ -1113,7 +1112,7 @@ chrome.cast.media.Media.prototype.getEstimatedTime = function () { * @param {function} listener The listener to add. The parameter indicates whether the Media object is still alive. */ chrome.cast.media.Media.prototype.addUpdateListener = function (listener) { - this.on('_mediaUpdated', listener); + this.on('_mediaUpdated', listener); }; /** @@ -1121,154 +1120,153 @@ chrome.cast.media.Media.prototype.addUpdateListener = function (listener) { * @param {function} listener The listener to remove. */ chrome.cast.media.Media.prototype.removeUpdateListener = function (listener) { - this.removeListener('_mediaUpdated', listener); + this.removeListener('_mediaUpdated', listener); }; -chrome.cast.media.Media.prototype._update = function(isAlive, obj) { - this.currentTime = obj.currentTime || this.currentTime; - this.idleReason = obj.idleReason || this.idleReason; - this.sessionId = obj.sessionId || this.sessionId; - this.mediaSessionId = obj.mediaSessionId || this.mediaSessionId; - this.playbackRate = obj.playbackRate || this.playbackRate; - this.playerState = obj.playerState || this.playerState; +chrome.cast.media.Media.prototype._update = function (isAlive, obj) { + this.currentTime = obj.currentTime || this.currentTime; + this.idleReason = obj.idleReason || this.idleReason; + this.sessionId = obj.sessionId || this.sessionId; + this.mediaSessionId = obj.mediaSessionId || this.mediaSessionId; + this.playbackRate = obj.playbackRate || this.playbackRate; + this.playerState = obj.playerState || this.playerState; - if (obj.media && obj.media.duration) { - this.media.duration = obj.media.duration || this.media.duration; - this.media.streamType = obj.media.streamType || this.media.streamType; - } + if (obj.media && obj.media.duration) { + this.media.duration = obj.media.duration || this.media.duration; + this.media.streamType = obj.media.streamType || this.media.streamType; + } - this.volume.level = obj.volume.level; - this.volume.muted = obj.volume.muted; + this.volume.level = obj.volume.level; + this.volume.muted = obj.volume.muted; - this._lastUpdatedTime = Date.now(); + this._lastUpdatedTime = Date.now(); - this.emit('_mediaUpdated', isAlive); + this.emit('_mediaUpdated', isAlive); }; -function createRouteElement(route) { - var el = document.createElement('li'); - el.classList.add('route'); - el.addEventListener('touchstart', onRouteClick); - el.textContent = route.name; - el.setAttribute('data-routeid', route.id); - return el; +function createRouteElement (route) { + var el = document.createElement('li'); + el.classList.add('route'); + el.addEventListener('touchstart', onRouteClick); + el.textContent = route.name; + el.setAttribute('data-routeid', route.id); + return el; } -function onRouteClick() { - var id = this.getAttribute('data-routeid'); +function onRouteClick () { + var id = this.getAttribute('data-routeid'); - if (id) { - try { - chrome.cast._emitConnecting(); - } catch(e) { - console.error('Error in connectingListener', e); - } + if (id) { + try { + chrome.cast._emitConnecting(); + } catch (e) { + console.error('Error in connectingListener', e); + } - execute('selectRoute', id, function(err, obj) { - var sessionId = obj.sessionId; - var appId = obj.appId; - var displayName = obj.displayName; - var appImages = obj.appImages || []; - var receiver = new chrome.cast.Receiver(obj.receiver.label, obj.receiver.friendlyName, obj.receiver.capabilities || [], obj.receiver.volume || null); + execute('selectRoute', id, function (err, obj) { + if (err) { + // TODO + } + var sessionId = obj.sessionId; + var appId = obj.appId; + var displayName = obj.displayName; + var appImages = obj.appImages || []; + var receiver = new chrome.cast.Receiver(obj.receiver.label, obj.receiver.friendlyName, obj.receiver.capabilities || [], obj.receiver.volume || null); - var session = _sessions[sessionId] = new chrome.cast.Session(sessionId, appId, displayName, appImages, receiver); + var session = _sessions[sessionId] = new chrome.cast.Session(sessionId, appId, displayName, appImages, receiver); - _sessionListener && _sessionListener(session); - }); - } + _sessionListener && _sessionListener(session); + }); + } } -chrome.cast.getRouteListElement = function() { - return _routeListEl; +chrome.cast.getRouteListElement = function () { + return _routeListEl; }; var _connectingListeners = []; -chrome.cast.addConnectingListener = function(cb) { - _connectingListeners.push(cb); +chrome.cast.addConnectingListener = function (cb) { + _connectingListeners.push(cb); }; -chrome.cast.removeConnectingListener = function(cb) { - if (_connectingListeners.indexOf(cb) > -1) { - _connectingListeners.splice(_connectingListeners.indexOf(cb), 1); - } +chrome.cast.removeConnectingListener = function (cb) { + if (_connectingListeners.indexOf(cb) > -1) { + _connectingListeners.splice(_connectingListeners.indexOf(cb), 1); + } }; -chrome.cast._emitConnecting = function() { - for (var n = 0; n < _connectingListeners.length; n++) { - _connectingListeners[n](); - } +chrome.cast._emitConnecting = function () { + for (var n = 0; n < _connectingListeners.length; n++) { + _connectingListeners[n](); + } }; chrome.cast._ = { - receiverUnavailable: function() { - _receiverListener(chrome.cast.ReceiverAvailability.UNAVAILABLE); - _receiverAvailable = false; - }, - receiverAvailable: function() { - _receiverListener(chrome.cast.ReceiverAvailability.AVAILABLE); - _receiverAvailable = true; - }, - routeAdded: function(route) { - if (!_routeList[route.id]) { - route.el = createRouteElement(route); - _routeList[route.id] = route; - - _routeListEl.appendChild(route.el); - } - }, - routeRemoved: function(route) { - if (_routeList[route.id]) { - _routeList[route.id].el.remove(); - delete _routeList[route.id]; - } - }, - sessionUpdated: function(isAlive, session) { - if (session && session.sessionId && _sessions[session.sessionId]) { - _sessions[session.sessionId]._update(isAlive, session); - } - }, - mediaUpdated: function(isAlive, media) { - - if (media && media.mediaSessionId !== undefined) - { - if (_currentMedia) { - _currentMedia._update(isAlive, media); - } else { - _currentMedia = new chrome.cast.media.Media(media.sessionId, media.mediaSessionId); - _currentMedia.currentTime = media.currentTime; - _currentMedia.playerState = media.playerState; - _currentMedia.media = media.media; - - _sessions[media.sessionId].media[0] = _currentMedia; - _sessionListener && _sessionListener(_sessions[media.sessionId]); - } - } - }, - mediaLoaded: function(isAlive, media) { - if (_sessions[media.sessionId]) { - - if (!_currentMedia) - { + receiverUnavailable: function () { + _receiverListener(chrome.cast.ReceiverAvailability.UNAVAILABLE); + _receiverAvailable = false; + }, + receiverAvailable: function () { + _receiverListener(chrome.cast.ReceiverAvailability.AVAILABLE); + _receiverAvailable = true; + }, + routeAdded: function (route) { + if (!_routeList[route.id]) { + route.el = createRouteElement(route); + _routeList[route.id] = route; + + _routeListEl.appendChild(route.el); + } + }, + routeRemoved: function (route) { + if (_routeList[route.id]) { + _routeList[route.id].el.remove(); + delete _routeList[route.id]; + } + }, + sessionUpdated: function (isAlive, session) { + if (session && session.sessionId && _sessions[session.sessionId]) { + _sessions[session.sessionId]._update(isAlive, session); + } + }, + mediaUpdated: function (isAlive, media) { + if (media && media.mediaSessionId !== undefined) { + if (_currentMedia) { + _currentMedia._update(isAlive, media); + } else { + _currentMedia = new chrome.cast.media.Media(media.sessionId, media.mediaSessionId); + _currentMedia.currentTime = media.currentTime; + _currentMedia.playerState = media.playerState; + _currentMedia.media = media.media; + + _sessions[media.sessionId].media[0] = _currentMedia; + _sessionListener && _sessionListener(_sessions[media.sessionId]); + } + } + }, + mediaLoaded: function (isAlive, media) { + if (_sessions[media.sessionId]) { + + if (!_currentMedia) { _currentMedia = new chrome.cast.media.Media(media.sessionId, media.mediaSessionId); } - _currentMedia._update(isAlive, media); - _sessions[media.sessionId].emit('_mediaListener', _currentMedia); - } else { - console.log('mediaLoaded --- but there is no session tied to it', media); - } - }, - sessionJoined: function(obj) { - var sessionId = obj.sessionId; - var appId = obj.appId; - var displayName = obj.displayName; - var appImages = obj.appImages || []; - var receiver = new chrome.cast.Receiver(obj.receiver.label, obj.receiver.friendlyName, obj.receiver.capabilities || [], obj.receiver.volume || null); - - var session = _sessions[sessionId] = new chrome.cast.Session(sessionId, appId, displayName, appImages, receiver); - - if (obj.media && obj.media.sessionId) - { + _currentMedia._update(isAlive, media); + _sessions[media.sessionId].emit('_mediaListener', _currentMedia); + } else { + console.log('mediaLoaded --- but there is no session tied to it', media); + } + }, + sessionJoined: function (obj) { + var sessionId = obj.sessionId; + var appId = obj.appId; + var displayName = obj.displayName; + var appImages = obj.appImages || []; + var receiver = new chrome.cast.Receiver(obj.receiver.label, obj.receiver.friendlyName, obj.receiver.capabilities || [], obj.receiver.volume || null); + + var session = _sessions[sessionId] = new chrome.cast.Session(sessionId, appId, displayName, appImages, receiver); + + if (obj.media && obj.media.sessionId) { _currentMedia = new chrome.cast.media.Media(sessionId, obj.media.mediaSessionId); _currentMedia.currentTime = obj.media.currentTime; _currentMedia.playerState = obj.media.playerState; @@ -1276,63 +1274,63 @@ chrome.cast._ = { session.media[0] = _currentMedia; } - _sessionListener && _sessionListener(session); - }, - onMessage: function(sessionId, namespace, message) { - if (_sessions[sessionId]) { - _sessions[sessionId].emit('message:' + namespace, namespace, message); - } - } + _sessionListener && _sessionListener(session); + }, + onMessage: function (sessionId, namespace, message) { + if (_sessions[sessionId]) { + _sessions[sessionId].emit('message:' + namespace, namespace, message); + } + } }; module.exports = chrome.cast; function execute (action) { - var args = [].slice.call(arguments); - args.shift(); - var callback; - if (args[args.length-1] instanceof Function) { - callback = args.pop(); - } - cordova.exec(function (result) { callback && callback(null, result); }, function(err) { callback && callback(err); }, "Chromecast", action, args); + var args = [].slice.call(arguments); + args.shift(); + var callback; + if (args[args.length - 1] instanceof Function) { + callback = args.pop(); + } + window.cordova.exec(function (result) { callback && callback(null, result); }, function (err) { callback && callback(err); }, 'Chromecast', action, args); } -function handleError(err, callback) { - var errorCode = chrome.cast.ErrorCode.UNKNOWN; - var errorDescription = err; - var errorData = {}; - - err = err || ""; - if (err.toUpperCase() === 'TIMEOUT') { - errorCode = chrome.cast.ErrorCode.TIMEOUT; - errorDescription = 'The operation timed out.'; - } else if (err.toUpperCase() === 'INVALID_PARAMETER') { - errorCode = chrome.cast.ErrorCode.INVALID_PARAMETER; - errorDescription = 'The parameters to the operation were not valid.'; - } else if (err.toUpperCase() === 'RECEIVER_UNAVAILABLE') { - errorCode = chrome.cast.ErrorCode.RECEIVER_UNAVAILABLE; - errorDescription = 'No receiver was compatible with the session request.'; - } else if (err.toUpperCase() === 'CANCEL') { - errorCode = chrome.cast.ErrorCode.CANCEL; - errorDescription = 'The operation was canceled by the user.'; - } else if (err.toUpperCase() === 'CHANNEL_ERROR') { - errorCode = chrome.cast.ErrorCode.CHANNEL_ERROR; - errorDescription = 'A channel to the receiver is not available.'; - } else if (err.toUpperCase() === 'SESSION_ERROR') { - errorCode = chrome.cast.ErrorCode.SESSION_ERROR; - errorDescription = 'A session could not be created, or a session was invalid.'; - } - - var error = new Error(errorCode, errorDescription, errorData); - if (callback) { - callback(error); - } +function handleError (err, callback) { + var errorCode = chrome.cast.ErrorCode.UNKNOWN; + var errorDescription = err; + var errorData = {}; + + err = err || ''; + if (err.toUpperCase() === 'TIMEOUT') { + errorCode = chrome.cast.ErrorCode.TIMEOUT; + errorDescription = 'The operation timed out.'; + } else if (err.toUpperCase() === 'INVALID_PARAMETER') { + errorCode = chrome.cast.ErrorCode.INVALID_PARAMETER; + errorDescription = 'The parameters to the operation were not valid.'; + } else if (err.toUpperCase() === 'RECEIVER_UNAVAILABLE') { + errorCode = chrome.cast.ErrorCode.RECEIVER_UNAVAILABLE; + errorDescription = 'No receiver was compatible with the session request.'; + } else if (err.toUpperCase() === 'CANCEL') { + errorCode = chrome.cast.ErrorCode.CANCEL; + errorDescription = 'The operation was canceled by the user.'; + } else if (err.toUpperCase() === 'CHANNEL_ERROR') { + errorCode = chrome.cast.ErrorCode.CHANNEL_ERROR; + errorDescription = 'A channel to the receiver is not available.'; + } else if (err.toUpperCase() === 'SESSION_ERROR') { + errorCode = chrome.cast.ErrorCode.SESSION_ERROR; + errorDescription = 'A session could not be created, or a session was invalid.'; + } + + var error = new Error(errorCode, errorDescription, errorData); + if (callback) { + callback(error); + } } -execute('setup', function(err) { - if (!err) { - chrome.cast.isAvailable = true; - } else { - throw new Error('Unable to setup chrome.cast API' + err); - } -}); \ No newline at end of file +execute('setup', function (err) { + if (!err) { + chrome.cast.isAvailable = true; + } else { + throw new Error('Unable to setup chrome.cast API' + err); + } +}); From ba79aaf12f8a9d4932c325d90ceb6a6eec170eb7 Mon Sep 17 00:00:00 2001 From: sel-en-ium Date: Tue, 30 Jul 2019 23:29:57 -0600 Subject: [PATCH 006/166] Updated the jasmine tests to use the newer cordova-plugin-test-framework version. (Most tests fail. Will try to fix in another commit.) # Conflicts: # README.md # tests/tests.js --- README.md | 33 +++++++-- package-lock.json | 164 +++++++++++---------------------------------- tests/README.md | 1 - tests/package.json | 14 ++++ tests/plugin.xml | 31 +++++++++ tests/tests.js | 7 +- 6 files changed, 115 insertions(+), 135 deletions(-) delete mode 100644 tests/README.md create mode 100644 tests/package.json create mode 100644 tests/plugin.xml diff --git a/README.md b/README.md index 6d41773..8f6e165 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,38 @@ When you call `chrome.cast.requestSession()` a popup will be displayed to select The project is now pretty much feature complete - the only things that will possibly break are missing parameters. We haven't done any checking for optional paramaters. When using the plugin make sure your constructors and function calls have every parameter you can find in the method declarations. -# Development +

Plugin Development

+ +* Link your local copy of the the plugin to a project for development and testing + * With admin permission run `cordova plugin add --link ` +* This links the plugin's **java** files directly to the Android platform. So you can modify the files from Android studio and re-deploy from there. +* Unfortunately it does **not** link the js files. +* To update the js files you must run + * `cordova plugin remove ` + * `cordova plugin add --link ` + * Don't forget the admin permission ## Formatting * Run `npm run test` (from the plugin directory) - * You may need to run `npm install` if you don't have eslint installed already + * If you get `Error: Cannot find module '\node_modules\eslint\bin\eslint'` + * Run `npm install` + * If it finds any formatting errors you can try and automatically fix them with: + * `node node_modules/eslint/bin/eslint --fix` + * Otherwise, please manually fix the error before commiting + +## Testing + +This plugin has [cordova-plugin-test-framework](https://github.com/apache/cordova-plugin-test-framework) tests. + +To run these tests you can follow one of the below instructions: + +* [Use your existing Cordova project](https://github.com/apache/cordova-plugin-test-framework) + * Will kind of mess up your project + * Probably will have to delete + re-create the platform folder when done at least +* [Use an empty project](https://github.com/miloproductionsinc/cordova-plugin-test-project) (recommended) + * Keep your project clean + * Most of the setup is done for you already + -If it finds errors you can try and automatically fix them with: -`node node_modules/eslint/bin/eslint --fix` \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 4b2032e..b17f042 100644 --- a/package-lock.json +++ b/package-lock.json @@ -70,16 +70,6 @@ "sprintf-js": "~1.0.2" } }, - "array-includes": { - "version": "3.0.3", - "resolved": "https://registry.npmjs.org/array-includes/-/array-includes-3.0.3.tgz", - "integrity": "sha1-GEtI9i2S10UrsxsyMWXH+L0CJm0=", - "dev": true, - "requires": { - "define-properties": "^1.1.2", - "es-abstract": "^1.7.0" - } - }, "babel-code-frame": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", @@ -113,6 +103,12 @@ "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==", "dev": true }, + "builtin-modules": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz", + "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=", + "dev": true + }, "caller-path": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/caller-path/-/caller-path-0.1.0.tgz", @@ -229,15 +225,6 @@ "integrity": "sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ=", "dev": true }, - "define-properties": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz", - "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==", - "dev": true, - "requires": { - "object-keys": "^1.0.12" - } - }, "doctrine": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-2.1.0.tgz", @@ -256,31 +243,6 @@ "is-arrayish": "^0.2.1" } }, - "es-abstract": { - "version": "1.13.0", - "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.13.0.tgz", - "integrity": "sha512-vDZfg/ykNxQVwup/8E1BZhVzFfBxs9NqMzGcvIJrqg5k2/5Za2bWo40dK2J1pgLngZ7c+Shh8lwYtLGyrwPutg==", - "dev": true, - "requires": { - "es-to-primitive": "^1.2.0", - "function-bind": "^1.1.1", - "has": "^1.0.3", - "is-callable": "^1.1.4", - "is-regex": "^1.0.4", - "object-keys": "^1.0.12" - } - }, - "es-to-primitive": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.0.tgz", - "integrity": "sha512-qZryBOJjV//LaxLTV6UC//WewneB3LcXOL9NP++ozKVXsIIIpm/2c13UDiD9Jp2eThsecw9m3jPqDwTyobcdbg==", - "dev": true, - "requires": { - "is-callable": "^1.1.4", - "is-date-object": "^1.0.1", - "is-symbol": "^1.0.2" - } - }, "es5-ext": { "version": "0.10.50", "resolved": "https://registry.npmjs.org/es5-ext/-/es5-ext-0.10.50.tgz", @@ -426,13 +388,14 @@ "dev": true }, "eslint-import-resolver-node": { - "version": "0.3.2", - "resolved": "https://registry.npmjs.org/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.2.tgz", - "integrity": "sha512-sfmTqJfPSizWu4aymbPr4Iidp5yKm8yDkHp+Ir3YiTHiiDfxh69mOUsmiqW6RZ9zRXFaF64GtYmN7e+8GHBv6Q==", + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/eslint-import-resolver-node/-/eslint-import-resolver-node-0.2.3.tgz", + "integrity": "sha1-Wt2BBujJKNssuiMrzZ76hG49oWw=", "dev": true, "requires": { - "debug": "^2.6.9", - "resolve": "^1.5.0" + "debug": "^2.2.0", + "object-assign": "^4.0.1", + "resolve": "^1.1.6" } }, "eslint-module-utils": { @@ -446,22 +409,21 @@ } }, "eslint-plugin-import": { - "version": "2.18.2", - "resolved": "https://registry.npmjs.org/eslint-plugin-import/-/eslint-plugin-import-2.18.2.tgz", - "integrity": "sha512-5ohpsHAiUBRNaBWAF08izwUGlbrJoJJ+W9/TBwsGoR1MnlgfwMIKrFeSjWbt6moabiXW9xNvtFz+97KHRfI4HQ==", + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-import/-/eslint-plugin-import-2.3.0.tgz", + "integrity": "sha1-N8gB4K2g4pbL3yDD85OstbUq82s=", "dev": true, "requires": { - "array-includes": "^3.0.3", + "builtin-modules": "^1.1.1", "contains-path": "^0.1.0", - "debug": "^2.6.9", + "debug": "^2.2.0", "doctrine": "1.5.0", - "eslint-import-resolver-node": "^0.3.2", - "eslint-module-utils": "^2.4.0", - "has": "^1.0.3", - "minimatch": "^3.0.4", - "object.values": "^1.1.0", - "read-pkg-up": "^2.0.0", - "resolve": "^1.11.0" + "eslint-import-resolver-node": "^0.2.0", + "eslint-module-utils": "^2.0.0", + "has": "^1.0.1", + "lodash.cond": "^4.3.0", + "minimatch": "^3.0.3", + "read-pkg-up": "^2.0.0" }, "dependencies": { "doctrine": { @@ -477,12 +439,12 @@ } }, "eslint-plugin-node": { - "version": "5.2.1", - "resolved": "https://registry.npmjs.org/eslint-plugin-node/-/eslint-plugin-node-5.2.1.tgz", - "integrity": "sha512-xhPXrh0Vl/b7870uEbaumb2Q+LxaEcOQ3kS1jtIXanBAwpMre1l5q/l2l/hESYJGEFKuI78bp6Uw50hlpr7B+g==", + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-node/-/eslint-plugin-node-5.0.0.tgz", + "integrity": "sha512-9xERRx9V/8ciUHlTDlz9S4JiTL6Dc5oO+jKTy2mvQpxjhycpYZXzTT1t90IXjf+nAYw6/8sDnZfkeixJHxromA==", "dev": true, "requires": { - "ignore": "^3.3.6", + "ignore": "^3.3.3", "minimatch": "^3.0.4", "resolve": "^1.3.3", "semver": "5.3.0" @@ -497,15 +459,15 @@ } }, "eslint-plugin-promise": { - "version": "3.8.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-3.8.0.tgz", - "integrity": "sha512-JiFL9UFR15NKpHyGii1ZcvmtIqa3UTwiDAGb8atSffe43qJ3+1czVGN6UtkklpcJ2DVnqvTMzEKRaJdBkAL2aQ==", + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-3.5.0.tgz", + "integrity": "sha1-ePu2/+BHIBYnVp6FpsU3OvKmj8o=", "dev": true }, "eslint-plugin-standard": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-standard/-/eslint-plugin-standard-3.1.0.tgz", - "integrity": "sha512-fVcdyuKRr0EZ4fjWl3c+gp1BANFJD1+RaWa2UPYfMZ6jCtp5RG00kSaXnK/dE5sYzt4kaWJ9qdxqUfc0d9kX0w==", + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/eslint-plugin-standard/-/eslint-plugin-standard-3.0.1.tgz", + "integrity": "sha1-NNDJFbRe3G8BA5PH7vOCOwhWXPI=", "dev": true }, "espree": { @@ -691,12 +653,6 @@ "ansi-regex": "^2.0.0" } }, - "has-symbols": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz", - "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=", - "dev": true - }, "hosted-git-info": { "version": "2.7.1", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.7.1.tgz", @@ -764,18 +720,6 @@ "integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=", "dev": true }, - "is-callable": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz", - "integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA==", - "dev": true - }, - "is-date-object": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz", - "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=", - "dev": true - }, "is-fullwidth-code-point": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", @@ -810,30 +754,12 @@ "integrity": "sha1-V/4cTkhHTt1lsJkR8msc1Ald2oQ=", "dev": true }, - "is-regex": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", - "integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=", - "dev": true, - "requires": { - "has": "^1.0.1" - } - }, "is-resolvable": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/is-resolvable/-/is-resolvable-1.1.0.tgz", "integrity": "sha512-qgDYXFSR5WvEfuS5dMj6oTMEbrrSaM0CrFk2Yiq/gXnBvD9pMa2jGXxyhGLfvhZpuMZe18CJpFxAt3CRs42NMg==", "dev": true }, - "is-symbol": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.2.tgz", - "integrity": "sha512-HS8bZ9ox60yCJLH9snBpIwv9pYUAkcuLhSA1oero1UB5y9aiQpRA8y2ex945AOtCZL1lJDeIk3G5LthswI46Lw==", - "dev": true, - "requires": { - "has-symbols": "^1.0.0" - } - }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", @@ -915,6 +841,12 @@ "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", "dev": true }, + "lodash.cond": { + "version": "4.5.2", + "resolved": "https://registry.npmjs.org/lodash.cond/-/lodash.cond-4.5.2.tgz", + "integrity": "sha1-9HGh2khr5g9quVXRcRVSPdHSVdU=", + "dev": true + }, "minimatch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", @@ -987,24 +919,6 @@ "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=", "dev": true }, - "object-keys": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", - "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==", - "dev": true - }, - "object.values": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/object.values/-/object.values-1.1.0.tgz", - "integrity": "sha512-8mf0nKLAoFX6VlNVdhGj31SVYpaNFtUnuoOXWyFEstsWRgU837AK+JYM0iAxwkSzGRbwn8cbFmgbyxj1j4VbXg==", - "dev": true, - "requires": { - "define-properties": "^1.1.3", - "es-abstract": "^1.12.0", - "function-bind": "^1.1.1", - "has": "^1.0.3" - } - }, "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", diff --git a/tests/README.md b/tests/README.md deleted file mode 100644 index 7c0474c..0000000 --- a/tests/README.md +++ /dev/null @@ -1 +0,0 @@ -See cordova-labs cdvtest branch if interested in autotests diff --git a/tests/package.json b/tests/package.json new file mode 100644 index 0000000..88536ec --- /dev/null +++ b/tests/package.json @@ -0,0 +1,14 @@ +{ + "name": "cordova-plugin-chromecast-tests", + "version": "0.0.0", + "description": "", + "author": "", + "license": "Apache 2.0", + "cordova": { + "id": "cordova-plugin-chromecast-tests", + "platforms": [ + "android" + ] + } + +} diff --git a/tests/plugin.xml b/tests/plugin.xml new file mode 100644 index 0000000..9c1d362 --- /dev/null +++ b/tests/plugin.xml @@ -0,0 +1,31 @@ + + + + + Cordova Chromecast Plugin Tests + Apache 2.0 + + + + + + \ No newline at end of file diff --git a/tests/tests.js b/tests/tests.js index 41bb90a..b7ac7f0 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1,10 +1,7 @@ -exports.init = function () { +exports.defineAutoTests = function () { /* eslint-disable no-undef */ - eval(require('org.apache.cordova.test-framework.test').injectJasmineInterface(this, 'this')); // eslint-disable-line no-eval - jasmine.DEFAULT_TIMEOUT_INTERVAL = 25000; - - // var cc = require('acidhax.cordova.chromecast.Chromecast'); + jasmine.DEFAULT_TIMEOUT_INTERVAL = 9000; var applicationID = 'CC1AD845'; var videoUrl = 'http://s3.nwgat.net/flvplayers3/bbb.mp4'; From a7a2e52207fd0823762c9b42b43ec34ffc65cfd2 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 7 Aug 2019 03:13:07 -0600 Subject: [PATCH 007/166] Switch to MediaRouteChooserDialog to simplify the dialog building process. (Chromecast.java) We shouldn't prevent requestSession from going through if there is no receiver. It handles this fine. It just shows that it is searching for a chromecast. This follows the behavior of chrome browser cast SDK. (chrome.cast.js) Remove unused imports. (Chromecast.java) Try to be a bit more safe with the threads. (Chromecast.java) Just pass the Chromecast instance to the callback on initialization. (Chromecast.java and ChromecastMediaRouterCallback.java) --- src/android/Chromecast.java | 96 ++++++++----------- .../ChromecastMediaRouterCallback.java | 2 +- www/chrome.cast.js | 4 - 3 files changed, 42 insertions(+), 60 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 8b39b37..a8ceafe 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -6,39 +6,33 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; -import java.util.HashMap; import java.util.List; -import java.util.ArrayList; import com.google.android.gms.cast.CastMediaControlIntent; import org.apache.cordova.CordovaPlugin; -import org.apache.cordova.CordovaWebView; import org.apache.cordova.CallbackContext; -import org.apache.cordova.CordovaInterface; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; -import android.app.AlertDialog; import android.content.DialogInterface; import android.content.SharedPreferences; +import androidx.appcompat.R; +import androidx.mediarouter.app.MediaRouteChooserDialog; import androidx.mediarouter.media.MediaRouter; import androidx.mediarouter.media.MediaRouteSelector; import androidx.mediarouter.media.MediaRouter.RouteInfo; -import android.util.Log; -import android.widget.ArrayAdapter; - public class Chromecast extends CordovaPlugin implements ChromecastOnMediaUpdatedListener, ChromecastOnSessionUpdatedListener { private static final String SETTINGS_NAME = "CordovaChromecastSettings"; - private MediaRouter mMediaRouter; - private MediaRouteSelector mMediaRouteSelector; - private volatile ChromecastMediaRouterCallback mMediaRouterCallback = new ChromecastMediaRouterCallback(); + private volatile MediaRouter mMediaRouter; + private volatile MediaRouteSelector mMediaRouteSelector; + private ChromecastMediaRouterCallback mMediaRouterCallback; private String appId; private boolean autoConnect = false; @@ -54,13 +48,15 @@ private void log(String s) { sendJavascript("console.log('" + s + "');"); } - public void initialize(final CordovaInterface cordova, CordovaWebView webView) { - super.initialize(cordova, webView); + @Override + protected void pluginInitialize() { + super.pluginInitialize(); // Restore preferences this.settings = this.cordova.getActivity().getSharedPreferences(SETTINGS_NAME, 0); this.lastSessionId = settings.getString("lastSessionId", ""); this.lastAppId = settings.getString("lastAppId", ""); + this.mMediaRouterCallback = new ChromecastMediaRouterCallback(this); } @Override @@ -163,12 +159,25 @@ public boolean initialize(final String appId, String autoJoinPolicy, String defa activity.runOnUiThread(new Runnable() { public void run() { - mMediaRouter = MediaRouter.getInstance(activity.getApplicationContext()); - mMediaRouteSelector = new MediaRouteSelector.Builder() - .addControlCategory(CastMediaControlIntent.categoryForCast(appId)) - .build(); - mMediaRouterCallback.registerCallbacks(that); - mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); + // Update the mediaRouteSelector and tha mediaRouter to use the current appId + synchronized(Chromecast.class) { + // Update the route selector + that.mMediaRouteSelector = new MediaRouteSelector.Builder() + .addControlCategory(CastMediaControlIntent.categoryForCast(appId)) + .build(); + + // Ensure the media router exists + if (that.mMediaRouter == null) { + // We need to initialize the router exists + that.mMediaRouter = MediaRouter.getInstance(activity.getApplicationContext()); + } else { + // If it previously existed we must remove the old callback + that.mMediaRouter.removeCallback(that.mMediaRouterCallback); + } + // Add (or re-add) the callback to work with the new selector + that.mMediaRouter.addCallback(that.mMediaRouteSelector, that.mMediaRouterCallback); + } + callbackContext.success(); Chromecast.this.checkReceiverAvailable(); @@ -186,6 +195,7 @@ public void run() { * @param callbackContext */ public boolean requestSession(final CallbackContext callbackContext) { + Chromecast that = this; if (this.currentSession != null) { callbackContext.success(this.currentSession.createSessionObject()); return true; @@ -193,48 +203,24 @@ public boolean requestSession(final CallbackContext callbackContext) { this.setLastSessionId(""); - final Activity activity = cordova.getActivity(); + final Activity activity = this.cordova.getActivity(); activity.runOnUiThread(new Runnable() { public void run() { - mMediaRouter = MediaRouter.getInstance(activity.getApplicationContext()); - final List routeList = mMediaRouter.getRoutes(); - AlertDialog.Builder builder = new AlertDialog.Builder(activity); - builder.setTitle("Choose a Chromecast"); - //CharSequence[] seq = new CharSequence[routeList.size() -1]; - ArrayList seq_tmp1 = new ArrayList(); + // Create the dialog + // TODO accept theme as a config.xml option + MediaRouteChooserDialog builder = new MediaRouteChooserDialog(activity, R.style.Theme_AppCompat_NoActionBar); + builder.setRouteSelector(that.mMediaRouteSelector); + builder.setCanceledOnTouchOutside(true); - final ArrayList seq_tmp_cnt_final = new ArrayList(); - for (int n = 1; n < routeList.size(); n++) { - RouteInfo route = routeList.get(n); - if (!route.getName().equals("Phone") && route.getId().indexOf("Cast") > -1) { - seq_tmp1.add(route.getName()); - seq_tmp_cnt_final.add(n); - //seq[n-1] = route.getName(); - } - } - - CharSequence[] seq; - seq = seq_tmp1.toArray(new CharSequence[seq_tmp1.size()]); - - builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + builder.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); + public void onCancel(DialogInterface dialog) { callbackContext.success("cancel"); } }); - builder.setItems(seq, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - which = seq_tmp_cnt_final.get(which); - RouteInfo selectedRoute = routeList.get(which); - Chromecast.this.createSession(selectedRoute, callbackContext); - } - }); - builder.show(); } }); @@ -426,8 +412,8 @@ public boolean addMessageListener(String namespace, CallbackContext callbackCont * @param contentType MIME type of the content * @param duration Duration of the content * @param streamType buffered | live | other - * @param loadRequest.autoPlay Whether or not to automatically start playing the media - * @param loadRequest.currentTime Where to begin playing from + * @param autoPlay Whether or not to automatically start playing the media + * @param currentTime Where to begin playing from * @param callbackContext */ public boolean loadMedia(String contentId, JSONObject customData, String contentType, Integer duration, String streamType, Boolean autoPlay, Double currentTime, JSONObject metadata, JSONObject textTrackStyle, final CallbackContext callbackContext) { @@ -654,12 +640,12 @@ public void run() { * Checks to see how many receivers are available - emits the receiver status down to Javascript */ private void checkReceiverAvailable() { + Chromecast that = this; final Activity activity = cordova.getActivity(); activity.runOnUiThread(new Runnable() { public void run() { - mMediaRouter = MediaRouter.getInstance(activity.getApplicationContext()); - List routeList = mMediaRouter.getRoutes(); + List routeList = that.mMediaRouter.getRoutes(); boolean available = false; for (RouteInfo route : routeList) { diff --git a/src/android/ChromecastMediaRouterCallback.java b/src/android/ChromecastMediaRouterCallback.java index e8656c3..930c708 100644 --- a/src/android/ChromecastMediaRouterCallback.java +++ b/src/android/ChromecastMediaRouterCallback.java @@ -11,7 +11,7 @@ public class ChromecastMediaRouterCallback extends MediaRouter.Callback { private Chromecast callback = null; - public void registerCallbacks(Chromecast instance) { + public ChromecastMediaRouterCallback(Chromecast instance) { this.callback = instance; } diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 0cd8a11..2a948c9 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -585,10 +585,6 @@ chrome.cast.requestSession = function (successCallback, errorCallback, opt_sessi errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); return; } - if (_receiverAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.RECEIVER_UNAVAILABLE, 'No receiver was compatible with the session request.', {})); - return; - } execute('requestSession', function (err, obj) { if (!err) { From 68cc66c533b94b8aa91c3a814a211191e125bca9 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 7 Aug 2019 03:17:55 -0600 Subject: [PATCH 008/166] We should not be logging to javascript all the time. It is an abuse of power and annoying. Use the cordova log utility instead. --- src/android/Chromecast.java | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index a8ceafe..6029cc0 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -12,6 +12,7 @@ import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CallbackContext; +import org.apache.cordova.LOG; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -28,6 +29,7 @@ public class Chromecast extends CordovaPlugin implements ChromecastOnMediaUpdatedListener, ChromecastOnSessionUpdatedListener { + private static final String TAG = "Chromecast"; private static final String SETTINGS_NAME = "CordovaChromecastSettings"; private volatile MediaRouter mMediaRouter; @@ -44,10 +46,6 @@ public class Chromecast extends CordovaPlugin implements ChromecastOnMediaUpdate private volatile ChromecastSession currentSession; - private void log(String s) { - sendJavascript("console.log('" + s + "');"); - } - @Override protected void pluginInitialize() { super.pluginInitialize(); @@ -148,12 +146,12 @@ public boolean initialize(final String appId, String autoJoinPolicy, String defa final Chromecast that = this; this.appId = appId; - log("initialize " + autoJoinPolicy + " " + appId + " " + this.lastAppId); + LOG.d(TAG, "initialize autoJoinPolicy: " + autoJoinPolicy + " appId: " + appId + " lastAppId: " + this.lastAppId); if (autoJoinPolicy.equals("origin_scoped") && appId.equals(this.lastAppId)) { - log("lastAppId " + lastAppId); + LOG.d(TAG, "lastAppId " + lastAppId); autoConnect = true; } else if (autoJoinPolicy.equals("origin_scoped")) { - log("setting lastAppId " + lastAppId); + LOG.d(TAG, "setting lastAppId " + lastAppId); setLastAppId(appId); } @@ -291,7 +289,7 @@ void onSuccess(Object object) { @Override void onError(String reason) { if (reason != null) { - Chromecast.this.log("createSession onError " + reason); + LOG.i(TAG, "createSession onError " + reason); if (callbackContext != null) { callbackContext.error(reason); } @@ -316,14 +314,14 @@ void onSuccess(Object object) { Chromecast.this.setLastSessionId(Chromecast.this.currentSession.getSessionId()); sendJavascript("chrome.cast._.sessionJoined(" + Chromecast.this.currentSession.createSessionObject().toString() + ");"); } catch (Exception e) { - log("wut.... " + e.getMessage() + e.getStackTrace()); + LOG.e(TAG, "wut.... " + e.getMessage() + e.getStackTrace()); } } } @Override void onError(String reason) { - log("sessionJoinAttempt error " + reason); + LOG.i(TAG, "sessionJoinAttempt error " + reason); } }); } @@ -551,7 +549,7 @@ public boolean mediaEditTracksInfo(JSONArray activeTrackIds, JSONObject textTrac trackIds[i] = activeTrackIds.getLong(i); } } catch (JSONException ignored) { - log("Wrong format in activeTrackIds"); + LOG.e(TAG, "Wrong format in activeTrackIds"); } @@ -689,10 +687,10 @@ public void onError(String reason) { */ protected void onRouteAdded(MediaRouter router, final RouteInfo route) { if (this.autoConnect && this.currentSession == null && !route.getName().equals("Phone")) { - log("Attempting to join route " + route.getName()); + LOG.d(TAG, "Attempting to join route " + route.getName()); this.joinSession(route); } else { - log("For some reason, not attempting to join route " + route.getName() + ", " + this.currentSession + ", " + this.autoConnect); + LOG.d(TAG, "For some reason, not attempting to join route " + route.getName() + ", " + this.currentSession + ", " + this.autoConnect); } if (!route.getName().equals("Phone") && route.getId().indexOf("Cast") > -1) { sendJavascript("chrome.cast._.routeAdded(" + routeToJSON(route) + ")"); @@ -761,7 +759,7 @@ public void onSessionUpdated(boolean isAlive, JSONObject session) { if (isAlive) { sendJavascript("chrome.cast._.sessionUpdated(true, " + session.toString() + ");"); } else { - log("SESSION DESTROYYYY"); + LOG.d(TAG, "SESSION DESTROYYYY"); sendJavascript("chrome.cast._.sessionUpdated(false, " + session.toString() + ");"); this.currentSession = null; } From 5218a318f3cdcfc626492fcd2ce39760e685c848 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 7 Aug 2019 06:39:49 -0600 Subject: [PATCH 009/166] WIP: Got the test framework to run. Basically all the tests are broken though. Updated the README.md with instructions on how to run these tests. We shouldn't load the tests.js file when the user is using the plugin for production use. We shouldn't include the test framework as a dependency. (Even a dependency of the test plugin.) Added a pull_request_template.md --- .github/pull_request_template.md | 33 ++++++++++++++++++++++++++++++++ README.md | 11 +---------- plugin.xml | 2 -- tests/plugin.xml | 3 +-- tests/tests.js | 11 +++++++++-- 5 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 .github/pull_request_template.md diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..bfc48ec --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,33 @@ + + +### Platforms affected + + + +### Motivation and Context + + + + + +### Description + + + +### Testing + + + + +### Checklist + + +- [ ] I've updated the documentation as necessary +- [ ] If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct [keyword to close issues using keywords](https://help.github.com/articles/closing-issues-using-keywords/)) +- [ ] I've run `npm run test` and no errors were found +- [ ] I've run the `test-framework` tests for Android (See Readme) +- [ ] I added automated test coverage as appropriate for this change \ No newline at end of file diff --git a/README.md b/README.md index 8f6e165..147f30a 100644 --- a/README.md +++ b/README.md @@ -42,14 +42,5 @@ The project is now pretty much feature complete - the only things that will poss This plugin has [cordova-plugin-test-framework](https://github.com/apache/cordova-plugin-test-framework) tests. -To run these tests you can follow one of the below instructions: - -* [Use your existing Cordova project](https://github.com/apache/cordova-plugin-test-framework) - * Will kind of mess up your project - * Probably will have to delete + re-create the platform folder when done at least -* [Use an empty project](https://github.com/miloproductionsinc/cordova-plugin-test-project) (recommended) - * Keep your project clean - * Most of the setup is done for you already - - +To run these tests you can follow [these instructions](https://github.com/miloproductionsinc/cordova-testing). diff --git a/plugin.xml b/plugin.xml index e87d36d..eed64e0 100644 --- a/plugin.xml +++ b/plugin.xml @@ -15,8 +15,6 @@ - - diff --git a/tests/plugin.xml b/tests/plugin.xml index 9c1d362..e11e4e9 100644 --- a/tests/plugin.xml +++ b/tests/plugin.xml @@ -27,5 +27,4 @@ - - \ No newline at end of file + diff --git a/tests/tests.js b/tests/tests.js index b7ac7f0..503683e 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1,9 +1,16 @@ +/** + * The order of the tests is very important! + * Unfortunately using nested describes and beforeAll does not work correctly. + * So just be careful with the order of tests! + */ + +/* eslint-env jasmine */ exports.defineAutoTests = function () { /* eslint-disable no-undef */ jasmine.DEFAULT_TIMEOUT_INTERVAL = 9000; - var applicationID = 'CC1AD845'; + var applicationID_default = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID; var videoUrl = 'http://s3.nwgat.net/flvplayers3/bbb.mp4'; describe('chrome.cast', function () { @@ -92,7 +99,7 @@ exports.defineAutoTests = function () { it('initialize should succeed', function (done) { console.log('initialize should succeed'); - var sessionRequest = new chrome.cast.SessionRequest(applicationID); + var sessionRequest = new chrome.cast.SessionRequest(applicationID_default); var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) { console.log('sessionCallback'); _session = session; From b1f9587ab2e4875e80d92536ec68d9863e92aec1 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 7 Aug 2019 10:02:36 -0600 Subject: [PATCH 010/166] WIP: Got the first couple tests working. Managed to match the desktop chrome SDK behavior regarding receiver updates on start up. Added some additional tests to cover more of the basic situations with receiver updates. Simplified all the get route stuff (will fix it up properly later) --- README.md | 10 +- package.json | 2 +- src/android/Chromecast.java | 108 +++---- .../ChromecastMediaRouterCallback.java | 22 -- tests/tests.js | 268 ++++++++++++------ www/chrome.cast.js | 4 - 6 files changed, 225 insertions(+), 189 deletions(-) diff --git a/README.md b/README.md index 147f30a..3784c45 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,12 @@ The project is now pretty much feature complete - the only things that will poss ## Formatting -* Run `npm run test` (from the plugin directory) +* Run `npm test` (from the plugin directory) * If you get `Error: Cannot find module '\node_modules\eslint\bin\eslint'` * Run `npm install` * If it finds any formatting errors you can try and automatically fix them with: * `node node_modules/eslint/bin/eslint --fix` - * Otherwise, please manually fix the error before commiting + * Otherwise, please manually fix the error before committing ## Testing @@ -44,3 +44,9 @@ This plugin has [cordova-plugin-test-framework](https://github.com/apache/cordov To run these tests you can follow [these instructions](https://github.com/miloproductionsinc/cordova-testing). +NOTE: You must run these tests from a project with the package name `com.miloproductionsinc.plugin_tests` otherwise `SPEC_00310` will fail. (It uses a custom receiver which are only allowed receive from one package name.) + + * You can temporarily rename the project you are testing from: + * config.xml > ` routeList = mMediaRouter.getRoutes(); + final List routeList = mMediaRouteManager.getRoutes(); for (RouteInfo route : routeList) { if (route.getId().equals(routeId)) { @@ -618,11 +596,10 @@ public boolean emitAllRoutes(CallbackContext callbackContext) { activity.runOnUiThread(new Runnable() { public void run() { - mMediaRouter = MediaRouter.getInstance(activity.getApplicationContext()); - List routeList = mMediaRouter.getRoutes(); + List routeList = mMediaRouteManager.getRoutes(); for (RouteInfo route : routeList) { - onRouteAdded(mMediaRouter, route); + sendJavascript("chrome.cast._.routeAdded(" + routeToJSON(route) + ")"); } } }); @@ -634,31 +611,23 @@ public void run() { return true; } - /** - * Checks to see how many receivers are available - emits the receiver status down to Javascript - */ - private void checkReceiverAvailable() { - Chromecast that = this; - final Activity activity = cordova.getActivity(); + /** + * Checks to see if any valid receivers are available - emits the receiver status out to Javascript + */ + public void sendReceiverUpdate() { + this.sendReceiverUpdate(mMediaRouteManager.isRouteAvailable()); + } - activity.runOnUiThread(new Runnable() { - public void run() { - List routeList = that.mMediaRouter.getRoutes(); - boolean available = false; - - for (RouteInfo route : routeList) { - if (!route.getName().equals("Phone") && route.getId().indexOf("Cast") > -1) { - available = true; - break; - } - } - if (available || (Chromecast.this.currentSession != null && Chromecast.this.currentSession.isConnected())) { - sendJavascript("chrome.cast._.receiverAvailable()"); - } else { - sendJavascript("chrome.cast._.receiverUnavailable()"); - } - } - }); + /** + * sends the receiverState. + * @param receiverState + */ + private void sendReceiverUpdate(boolean receiverState) { + if (receiverState) { + this.sendJavascript("chrome.cast._.receiverAvailable()"); + } else { + this.sendJavascript("chrome.cast._.receiverUnavailable()"); + } } /** @@ -692,10 +661,6 @@ protected void onRouteAdded(MediaRouter router, final RouteInfo route) { } else { LOG.d(TAG, "For some reason, not attempting to join route " + route.getName() + ", " + this.currentSession + ", " + this.autoConnect); } - if (!route.getName().equals("Phone") && route.getId().indexOf("Cast") > -1) { - sendJavascript("chrome.cast._.routeAdded(" + routeToJSON(route) + ")"); - } - this.checkReceiverAvailable(); } /** @@ -704,7 +669,6 @@ protected void onRouteAdded(MediaRouter router, final RouteInfo route) { * @param route */ protected void onRouteRemoved(MediaRouter router, RouteInfo route) { - this.checkReceiverAvailable(); if (!route.getName().equals("Phone") && route.getId().indexOf("Cast") > -1) { sendJavascript("chrome.cast._.routeRemoved(" + routeToJSON(route) + ")"); } diff --git a/src/android/ChromecastMediaRouterCallback.java b/src/android/ChromecastMediaRouterCallback.java index 930c708..827dca5 100644 --- a/src/android/ChromecastMediaRouterCallback.java +++ b/src/android/ChromecastMediaRouterCallback.java @@ -1,13 +1,9 @@ package acidhax.cordova.chromecast; -import java.util.ArrayList; -import java.util.Collection; - import androidx.mediarouter.media.MediaRouter; import androidx.mediarouter.media.MediaRouter.RouteInfo; public class ChromecastMediaRouterCallback extends MediaRouter.Callback { - private volatile ArrayList routes = new ArrayList(); private Chromecast callback = null; @@ -15,26 +11,9 @@ public ChromecastMediaRouterCallback(Chromecast instance) { this.callback = instance; } - public synchronized RouteInfo getRoute(String id) { - for (RouteInfo i : this.routes) { - if (i.getId().equals(id)) { - return i; - } - } - return null; - } - - public synchronized RouteInfo getRoute(int index) { - return routes.get(index); - } - - public synchronized Collection getRoutes() { - return routes; - } @Override public synchronized void onRouteAdded(MediaRouter router, RouteInfo route) { - routes.add(route); if (this.callback != null) { this.callback.onRouteAdded(router, route); } @@ -42,7 +21,6 @@ public synchronized void onRouteAdded(MediaRouter router, RouteInfo route) { @Override public void onRouteRemoved(MediaRouter router, RouteInfo route) { - routes.remove(route); if (this.callback != null) { this.callback.onRouteRemoved(router, route); } diff --git a/tests/tests.js b/tests/tests.js index 503683e..d8dcfb6 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -9,122 +9,191 @@ exports.defineAutoTests = function () { /* eslint-disable no-undef */ jasmine.DEFAULT_TIMEOUT_INTERVAL = 9000; + var USER_INTERACTION_TIMEOUT = 60 * 1000; // 1 min var applicationID_default = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID; + var applicationID_custom = 'F5EEDC6C'; var videoUrl = 'http://s3.nwgat.net/flvplayers3/bbb.mp4'; describe('chrome.cast', function () { var _session = null; - var _receiverAvailability = null; + var _receiverAvailability = []; var _sessionUpdatedFired = false; var _mediaUpdatedFired = false; - // var _currentMedia = null; + var _currentMedia; + + it('SPEC_00100 should contain definitions', function () { + expect(chrome.cast.VERSION).toBeDefined(); + expect(chrome.cast.ReceiverAvailability).toBeDefined(); + expect(chrome.cast.ReceiverType).toBeDefined(); + expect(chrome.cast.SenderPlatform).toBeDefined(); + expect(chrome.cast.AutoJoinPolicy).toBeDefined(); + expect(chrome.cast.Capability).toBeDefined(); + expect(chrome.cast.DefaultActionPolicy).toBeDefined(); + expect(chrome.cast.ErrorCode).toBeDefined(); + expect(chrome.cast.timeout).toBeDefined(); + expect(chrome.cast.isAvailable).toBeDefined(); + expect(chrome.cast.ApiConfig).toBeDefined(); + expect(chrome.cast.Receiver).toBeDefined(); + expect(chrome.cast.DialRequest).toBeDefined(); + expect(chrome.cast.SessionRequest).toBeDefined(); + expect(chrome.cast.Error).toBeDefined(); + expect(chrome.cast.Image).toBeDefined(); + expect(chrome.cast.SenderApplication).toBeDefined(); + expect(chrome.cast.Volume).toBeDefined(); + expect(chrome.cast.media).toBeDefined(); + expect(chrome.cast.initialize).toBeDefined(); + expect(chrome.cast.requestSession).toBeDefined(); + expect(chrome.cast.setCustomReceivers).toBeDefined(); + expect(chrome.cast.Session).toBeDefined(); + expect(chrome.cast.media.PlayerState).toBeDefined(); + expect(chrome.cast.media.ResumeState).toBeDefined(); + expect(chrome.cast.media.MediaCommand).toBeDefined(); + expect(chrome.cast.media.MetadataType).toBeDefined(); + expect(chrome.cast.media.StreamType).toBeDefined(); + expect(chrome.cast.media.timeout).toBeDefined(); + expect(chrome.cast.media.LoadRequest).toBeDefined(); + expect(chrome.cast.media.PlayRequest).toBeDefined(); + expect(chrome.cast.media.SeekRequest).toBeDefined(); + expect(chrome.cast.media.VolumeRequest).toBeDefined(); + expect(chrome.cast.media.StopRequest).toBeDefined(); + expect(chrome.cast.media.PauseRequest).toBeDefined(); + expect(chrome.cast.media.GenericMediaMetadata).toBeDefined(); + expect(chrome.cast.media.MovieMediaMetadata).toBeDefined(); + expect(chrome.cast.media.MusicTrackMediaMetadata).toBeDefined(); + expect(chrome.cast.media.PhotoMediaMetadata).toBeDefined(); + expect(chrome.cast.media.TvShowMediaMetadata).toBeDefined(); + expect(chrome.cast.media.MediaInfo).toBeDefined(); + expect(chrome.cast.media.Media).toBeDefined(); + expect(chrome.cast.Session.prototype.setReceiverVolumeLevel).toBeDefined(); + expect(chrome.cast.Session.prototype.setReceiverMuted).toBeDefined(); + expect(chrome.cast.Session.prototype.stop).toBeDefined(); + expect(chrome.cast.Session.prototype.sendMessage).toBeDefined(); + expect(chrome.cast.Session.prototype.addUpdateListener).toBeDefined(); + expect(chrome.cast.Session.prototype.removeUpdateListener).toBeDefined(); + expect(chrome.cast.Session.prototype.addMessageListener).toBeDefined(); + expect(chrome.cast.Session.prototype.removeMessageListener).toBeDefined(); + expect(chrome.cast.Session.prototype.addMediaListener).toBeDefined(); + expect(chrome.cast.Session.prototype.removeMediaListener).toBeDefined(); + expect(chrome.cast.Session.prototype.loadMedia).toBeDefined(); + expect(chrome.cast.media.Media.prototype.play).toBeDefined(); + expect(chrome.cast.media.Media.prototype.pause).toBeDefined(); + expect(chrome.cast.media.Media.prototype.seek).toBeDefined(); + expect(chrome.cast.media.Media.prototype.stop).toBeDefined(); + expect(chrome.cast.media.Media.prototype.setVolume).toBeDefined(); + expect(chrome.cast.media.Media.prototype.supportsCommand).toBeDefined(); + expect(chrome.cast.media.Media.prototype.getEstimatedTime).toBeDefined(); + expect(chrome.cast.media.Media.prototype.addUpdateListener).toBeDefined(); + expect(chrome.cast.media.Media.prototype.removeUpdateListener).toBeDefined(); + }); - it('should contain definitions', function (done) { - setTimeout(function () { - expect(chrome.cast.VERSION).toBeDefined(); - expect(chrome.cast.ReceiverAvailability).toBeDefined(); - expect(chrome.cast.ReceiverType).toBeDefined(); - expect(chrome.cast.SenderPlatform).toBeDefined(); - expect(chrome.cast.AutoJoinPolicy).toBeDefined(); - expect(chrome.cast.Capability).toBeDefined(); - expect(chrome.cast.DefaultActionPolicy).toBeDefined(); - expect(chrome.cast.ErrorCode).toBeDefined(); - expect(chrome.cast.timeout).toBeDefined(); - expect(chrome.cast.isAvailable).toBeDefined(); - expect(chrome.cast.ApiConfig).toBeDefined(); - expect(chrome.cast.Receiver).toBeDefined(); - expect(chrome.cast.DialRequest).toBeDefined(); - expect(chrome.cast.SessionRequest).toBeDefined(); - expect(chrome.cast.Error).toBeDefined(); - expect(chrome.cast.Image).toBeDefined(); - expect(chrome.cast.SenderApplication).toBeDefined(); - expect(chrome.cast.Volume).toBeDefined(); - expect(chrome.cast.media).toBeDefined(); - expect(chrome.cast.initialize).toBeDefined(); - expect(chrome.cast.requestSession).toBeDefined(); - expect(chrome.cast.setCustomReceivers).toBeDefined(); - expect(chrome.cast.Session).toBeDefined(); - expect(chrome.cast.media.PlayerState).toBeDefined(); - expect(chrome.cast.media.ResumeState).toBeDefined(); - expect(chrome.cast.media.MediaCommand).toBeDefined(); - expect(chrome.cast.media.MetadataType).toBeDefined(); - expect(chrome.cast.media.StreamType).toBeDefined(); - expect(chrome.cast.media.timeout).toBeDefined(); - expect(chrome.cast.media.LoadRequest).toBeDefined(); - expect(chrome.cast.media.PlayRequest).toBeDefined(); - expect(chrome.cast.media.SeekRequest).toBeDefined(); - expect(chrome.cast.media.VolumeRequest).toBeDefined(); - expect(chrome.cast.media.StopRequest).toBeDefined(); - expect(chrome.cast.media.PauseRequest).toBeDefined(); - expect(chrome.cast.media.GenericMediaMetadata).toBeDefined(); - expect(chrome.cast.media.MovieMediaMetadata).toBeDefined(); - expect(chrome.cast.media.MusicTrackMediaMetadata).toBeDefined(); - expect(chrome.cast.media.PhotoMediaMetadata).toBeDefined(); - expect(chrome.cast.media.TvShowMediaMetadata).toBeDefined(); - expect(chrome.cast.media.MediaInfo).toBeDefined(); - expect(chrome.cast.media.Media).toBeDefined(); - expect(chrome.cast.Session.prototype.setReceiverVolumeLevel).toBeDefined(); - expect(chrome.cast.Session.prototype.setReceiverMuted).toBeDefined(); - expect(chrome.cast.Session.prototype.stop).toBeDefined(); - expect(chrome.cast.Session.prototype.sendMessage).toBeDefined(); - expect(chrome.cast.Session.prototype.addUpdateListener).toBeDefined(); - expect(chrome.cast.Session.prototype.removeUpdateListener).toBeDefined(); - expect(chrome.cast.Session.prototype.addMessageListener).toBeDefined(); - expect(chrome.cast.Session.prototype.removeMessageListener).toBeDefined(); - expect(chrome.cast.Session.prototype.addMediaListener).toBeDefined(); - expect(chrome.cast.Session.prototype.removeMediaListener).toBeDefined(); - expect(chrome.cast.Session.prototype.loadMedia).toBeDefined(); - expect(chrome.cast.media.Media.prototype.play).toBeDefined(); - expect(chrome.cast.media.Media.prototype.pause).toBeDefined(); - expect(chrome.cast.media.Media.prototype.seek).toBeDefined(); - expect(chrome.cast.media.Media.prototype.stop).toBeDefined(); - expect(chrome.cast.media.Media.prototype.setVolume).toBeDefined(); - expect(chrome.cast.media.Media.prototype.supportsCommand).toBeDefined(); - expect(chrome.cast.media.Media.prototype.getEstimatedTime).toBeDefined(); - expect(chrome.cast.media.Media.prototype.addUpdateListener).toBeDefined(); - expect(chrome.cast.media.Media.prototype.removeUpdateListener).toBeDefined(); - done(); - }, 1000); + it('SPEC_00200 api should be available', function (done) { + tryUntilSuccess(function () { + return chrome.cast.isAvailable; + }, done); }); - it('api should be available', function (done) { - setTimeout(function () { - console.log('api should be available'); - expect(chrome.cast.isAvailable).toEqual(true); - done(); - }, 4000); + describe('Custom Receiver', function () { + var _customReceiverAvailability = []; + + it('SPEC_00300 initialize should succeed (custom receiver)', function (done) { + var sessionRequest = new chrome.cast.SessionRequest(applicationID_custom); + var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) { + _session = session; + }, function (available) { + _customReceiverAvailability.push(available); + }); + + chrome.cast.initialize(apiConfig, function () { + expect('success').toBeDefined(); + done(); + }, function (err) { + expect(err).toBe(null); + done(); + }); + }); + + /** + * Pre-requisite: You must have a valid receiver (chromecast) plugged in and available. + * You must also be running this test from a project with the package name: + * com.miloproductionsinc.plugin_tests + * You can rename your project, or clone this: + * https://github.com/miloproductionsinc/cordova-testing + */ + it('SPEC_00310 receiver available (custom receiver)', function (done) { + tryUntilSuccess(function () { + + if (_customReceiverAvailability.length >= 1) { + // We should see that the receiver is unavailable always first + expect(_customReceiverAvailability[0]).toBe(chrome.cast.ReceiverAvailability.UNAVAILABLE); + return true; + } + // We need to return false until the first entry to _receiverAvailability is added + return false; + + }, function () { + + tryUntilSuccess(function () { + if (_customReceiverAvailability.length >= 2) { + // Then we should receive an available notification + expect(_customReceiverAvailability[1]).toBe(chrome.cast.ReceiverAvailability.AVAILABLE); + return true; + } + // We need to return false until the second entry to _receiverAvailability is added + return false; + + }, done); + }); + }, USER_INTERACTION_TIMEOUT); }); - it('initialize should succeed', function (done) { - console.log('initialize should succeed'); + it('SPEC_00400 initialize should succeed (default receiver)', function (done) { + _receiverAvailability = []; var sessionRequest = new chrome.cast.SessionRequest(applicationID_default); var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) { - console.log('sessionCallback'); _session = session; }, function (available) { - console.log('receiverCallback'); - _receiverAvailability = available; + _receiverAvailability.push(available); }); chrome.cast.initialize(apiConfig, function () { - console.log('initialize done'); + expect('success').toBeDefined(); done(); }, function (err) { - console.log('initialize error', err); expect(err).toBe(null); done(); }); }); - it('receiver available', function (done) { - setTimeout(function () { - console.log('receiver available', _receiverAvailability); - expect(_receiverAvailability).toEqual(chrome.cast.ReceiverAvailability.AVAILABLE); - done(); - }, 2000); - }); + /** + * Pre-requisite: You must have a valid receiver (chromecast) plugged in and available + */ + it('SPEC_00410 receiver available (default receiver)', function (done) { + tryUntilSuccess(function () { + + if (_receiverAvailability.length >= 1) { + // We should see that the receiver is unavailable always first + expect(_receiverAvailability[0]).toBe(chrome.cast.ReceiverAvailability.UNAVAILABLE); + return true; + } + // We need to return false until the first entry to _receiverAvailability is added + return false; + + }, function () { + + tryUntilSuccess(function () { + if (_receiverAvailability.length >= 2) { + // Then we should receive an available notification + expect(_receiverAvailability[1]).toBe(chrome.cast.ReceiverAvailability.AVAILABLE); + return true; + } + // We need to return false until the second entry to _receiverAvailability is added + return false; + + }, done); + }); + }, USER_INTERACTION_TIMEOUT); it('requestSession should succeed', function (done) { chrome.cast.requestSession(function (session) { @@ -281,5 +350,28 @@ exports.defineAutoTests = function () { }); }); + it('SPEC_01200 should pass auto tests on second run', function () { + alert('---TEST INSTRUCTION---\nPlease hit "Reset App" at the top and ensure all ' + + 'tests pass again. (This simulates navigation to a new page where the ' + + 'plugin is not loaded from scratch again).'); + expect('succes').toBeDefined(); + }); + }); }; + +//* ***************************************************************************************** +//* ***********************************Helper Functions************************************** +//* ***************************************************************************************** + +function tryUntilSuccess (successFn, callback, waitBetweenTries) { + waitBetweenTries = waitBetweenTries || 500; + if (successFn()) { + expect(callback).toBeDefined(); + callback(); + } else { + setTimeout(function () { + tryUntilSuccess(successFn, callback, waitBetweenTries); + }, waitBetweenTries); + } +} diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 2a948c9..5441ccd 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -535,8 +535,6 @@ _routeListEl.classList.add('route-list'); var _routeList = {}; var _routeRefreshInterval = null; -var _receiverAvailable = false; - /** * Initializes the API. Note that either successCallback and errorCallback will be invoked once the API has finished initialization. * The sessionListener and receiverListener may be invoked at any time afterwards, and possibly more than once. @@ -1201,11 +1199,9 @@ chrome.cast._emitConnecting = function () { chrome.cast._ = { receiverUnavailable: function () { _receiverListener(chrome.cast.ReceiverAvailability.UNAVAILABLE); - _receiverAvailable = false; }, receiverAvailable: function () { _receiverListener(chrome.cast.ReceiverAvailability.AVAILABLE); - _receiverAvailable = true; }, routeAdded: function (route) { if (!_routeList[route.id]) { From 3fda81008be82cbfdd7a4cec62d427be4d0188f9 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 26 Aug 2019 01:17:34 -0600 Subject: [PATCH 011/166] WIP: Got request session somewhat working capture routeUnselected reason --- plugin.xml | 1 + src/android/Chromecast.java | 69 ++++---- .../ChromecastMediaRouterCallback.java | 12 +- src/android/ChromecastMediaRouterManager.java | 164 ++++++++++++++++++ tests/tests.js | 31 ++-- 5 files changed, 215 insertions(+), 62 deletions(-) create mode 100644 src/android/ChromecastMediaRouterManager.java diff --git a/plugin.xml b/plugin.xml index eed64e0..94de1dc 100644 --- a/plugin.xml +++ b/plugin.xml @@ -40,6 +40,7 @@ + diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index fb066a3..1f4b5ef 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -16,11 +16,8 @@ import org.json.JSONObject; import android.app.Activity; -import android.content.DialogInterface; import android.content.SharedPreferences; -import androidx.appcompat.R; -import androidx.mediarouter.app.MediaRouteChooserDialog; import androidx.mediarouter.media.MediaRouter; import androidx.mediarouter.media.MediaRouter.RouteInfo; @@ -153,10 +150,13 @@ public boolean initialize(final String appId, String autoJoinPolicy, String defa // This matches the Chrome Desktop SDK behavior. that.sendReceiverUpdate(false); - // Update the mediaRouteSelector and tha mediaRouter to use the current appId - mMediaRouteManager.updateMediaRouter(appId, new ChromecastMediaRouterManager.Callback() { + // Search for a receiver for the appId for 1 minute. + // After that there probably won't be any routes, so don't drain their battery. + // The scan should will be triggered each time the client does api initialize + // (aka. every time the user goes to a new page that supports chromecast) + mMediaRouteManager.scanForReceiver(appId, 60000, new ChromecastMediaRouterManager.ScanCallback() { @Override - public void onFoundRoute() { + public void onFoundReceiver() { that.sendReceiverUpdate(true); } }); @@ -173,34 +173,30 @@ public void onFoundRoute() { * @param callbackContext */ public boolean requestSession(final CallbackContext callbackContext) { - Chromecast that = this; - if (this.currentSession != null) { - callbackContext.success(this.currentSession.createSessionObject()); - return true; - } + setLastSessionId(""); - this.setLastSessionId(""); - - final Activity activity = this.cordova.getActivity(); - activity.runOnUiThread(new Runnable() { - public void run() { - - // Create the dialog - // TODO accept theme as a config.xml option - MediaRouteChooserDialog builder = new MediaRouteChooserDialog(activity, R.style.Theme_AppCompat_NoActionBar); - builder.setRouteSelector(mMediaRouteManager.getMediaRouteSelector()); - builder.setCanceledOnTouchOutside(true); + if (currentSession == null) { + // Show the route selection Dialog + mMediaRouteManager.showRouteSelectionDialog(new ChromecastMediaRouterManager.DialogCallback() { + @Override + void onConnect(RouteInfo route) { + super.onConnect(route); + createSession(route, callbackContext); + } - builder.setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { + @Override + void onError(String errorCode) { + super.onError(errorCode); + if (errorCode.equals("CANCEL")) { callbackContext.success("cancel"); + } else { + callbackContext.error(errorCode); } - }); - - builder.show(); - } - }); + } + }); + } else { + // TODO Show the disconnect dialog + } return true; } @@ -674,21 +670,14 @@ protected void onRouteRemoved(MediaRouter router, RouteInfo route) { } } - /** - * Called when a route is selected through the MediaRouter - * @param router - * @param route - */ - protected void onRouteSelected(MediaRouter router, RouteInfo route) { - this.createSession(route, null); - } - /** * Called when a route is unselected through the MediaRouter * @param router * @param route */ - protected void onRouteUnselected(MediaRouter router, RouteInfo route) { + protected void onRouteUnselected(MediaRouter router, RouteInfo route, int reason) { + // Let the client know they have been disconnected + sendJavascript("chrome.cast._.receiverUnavailable()"); } /** diff --git a/src/android/ChromecastMediaRouterCallback.java b/src/android/ChromecastMediaRouterCallback.java index 827dca5..14e841b 100644 --- a/src/android/ChromecastMediaRouterCallback.java +++ b/src/android/ChromecastMediaRouterCallback.java @@ -27,16 +27,10 @@ public void onRouteRemoved(MediaRouter router, RouteInfo route) { } @Override - public void onRouteSelected(MediaRouter router, RouteInfo info) { + public void onRouteUnselected(MediaRouter router, RouteInfo route, int reason) { + super.onRouteUnselected(router, route, reason); if (this.callback != null) { - this.callback.onRouteSelected(router, info); - } - } - - @Override - public void onRouteUnselected(MediaRouter router, RouteInfo info) { - if (this.callback != null) { - this.callback.onRouteUnselected(router, info); + this.callback.onRouteUnselected(router, route, reason); } } } diff --git a/src/android/ChromecastMediaRouterManager.java b/src/android/ChromecastMediaRouterManager.java new file mode 100644 index 0000000..4fd6a34 --- /dev/null +++ b/src/android/ChromecastMediaRouterManager.java @@ -0,0 +1,164 @@ +package acidhax.cordova.chromecast; + +import android.app.Activity; +import android.content.DialogInterface; +import android.os.Handler; + +import androidx.mediarouter.app.MediaRouteChooserDialog; +import androidx.mediarouter.media.MediaRouteSelector; +import androidx.mediarouter.media.MediaRouter; +import androidx.mediarouter.media.MediaRouter.RouteInfo; + +import com.google.android.gms.cast.CastMediaControlIntent; + +import java.util.List; + +public class ChromecastMediaRouterManager { + + private Activity mActivity; + private String mAppId; + private volatile MediaRouter mMediaRouter; + private volatile MediaRouteSelector mMediaRouteSelector; + private MediaRouter.Callback mMediaRouterCallback; + private volatile boolean mDialogCancelled; + + public ChromecastMediaRouterManager(Activity context, MediaRouter.Callback mediaRouterCallback) { + mActivity = context; + mMediaRouterCallback = mediaRouterCallback; + } + + /** + * Actively searches for a valid receiver for appId for seekDuration. + * This uses lots of battery power, so it's best to limit the search time. + * @param appId + * @param seekDuration (milli seconds) + * @param callback + */ + public void scanForReceiver(String appId, int seekDuration, ScanCallback callback) { + mActivity.runOnUiThread(new Runnable() { + public void run() { + synchronized(ChromecastMediaRouterManager.class) { + + boolean appIdIsSame = appId.equals(mAppId); + mAppId = appId; + + // Ensure the media router exists + if (mMediaRouter == null) { + // We need to initialize the router exists + mMediaRouter = MediaRouter.getInstance(mActivity); + } + + if (appIdIsSame && isRouteAvailable()) { + // In this case, it most likely this the user navigated to a new page and called api initialize again. + // To replicate chrome desktop behavior, we must manually send the receiver available update. + // Scan will not find a new route since we already have one. + callback.onFoundReceiver(); + + } else { + // Else, scan because, the app Id has changed, this is our first time here, or no routes are known + + // Update/create the route selector as needed + if (!appIdIsSame || mMediaRouteSelector == null) { + mMediaRouteSelector = new MediaRouteSelector.Builder() + .addControlCategory(CastMediaControlIntent.categoryForCast(appId)) + .build(); + } + + // Add a callback that will solely search for receivers. + // We will remove the active seeking callback after it finds a route or + // after seekDuration. It uses a lot of power. We will then add a less + // power-hungry callback. + + // Create our route detection callback + MediaRouter.Callback routeChangedCallback = new MediaRouter.Callback() { + @Override + public void onRouteChanged(MediaRouter router, RouteInfo route) { + super.onRouteChanged(router, route); + if (isRouteAvailable()) { + callback.onFoundReceiver(); + mMediaRouter.removeCallback(this); + } + } + }; + + // Create and start our timeout + final Handler handler = new Handler(); + final Runnable r = new Runnable() { + @Override + public void run() { + // Quit scanning + mMediaRouter.removeCallback(routeChangedCallback); + } + }; + handler.postDelayed(r, seekDuration); + + // Add the callback in active scan mode + mMediaRouter.addCallback(mMediaRouteSelector, routeChangedCallback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); + } + } + + } + }); + } + + public void showRouteSelectionDialog(DialogCallback callback) { + mDialogCancelled = false; + + mActivity.runOnUiThread(new Runnable() { + public void run() { + + // Create the dialog + // TODO accept theme as a config.xml option + MediaRouteChooserDialog builder = new MediaRouteChooserDialog(mActivity, androidx.appcompat.R.style.Theme_AppCompat_NoActionBar); + builder.setRouteSelector(mMediaRouteSelector); + builder.setCanceledOnTouchOutside(true); + builder.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + mDialogCancelled = true; + } + }); + builder.setOnDismissListener(new DialogInterface.OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialog) { + RouteInfo route = mMediaRouter.getSelectedRoute(); + if (mDialogCancelled) { + callback.onError("CANCEL"); + } else if (route.isDefault()) { + callback.onError("RECEIVER_UNAVAILABLE"); + } else { + callback.onConnect(route); + } + } + }); + + builder.show(); + } + }); + } + + /** + * Checks if it appears that any routes are available. + * @return True, if there has been a receiver available (it is possible that it has been disconnected). + * False, if there has been no confirmed receiver. + */ + public boolean isRouteAvailable() { + if (mMediaRouter != null && mMediaRouteSelector != null) { + return mMediaRouter.isRouteAvailable(mMediaRouteSelector, MediaRouter.AVAILABILITY_FLAG_IGNORE_DEFAULT_ROUTE); + } + return false; + } + + public List getRoutes() { + return mMediaRouter.getRoutes(); + } + + public static class ScanCallback { + void onFoundReceiver() {} + } + + public static class DialogCallback { + void onConnect(RouteInfo route) {} + void onError(String errorCode) {} + } +} diff --git a/tests/tests.js b/tests/tests.js index d8dcfb6..78f8258 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -150,6 +150,7 @@ exports.defineAutoTests = function () { it('SPEC_00400 initialize should succeed (default receiver)', function (done) { _receiverAvailability = []; + _session = 'no_session'; var sessionRequest = new chrome.cast.SessionRequest(applicationID_default); var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) { _session = session; @@ -158,7 +159,7 @@ exports.defineAutoTests = function () { }); chrome.cast.initialize(apiConfig, function () { - expect('success').toBeDefined(); + expect('success').toBeTruthy(); done(); }, function (err) { expect(err).toBe(null); @@ -195,17 +196,10 @@ exports.defineAutoTests = function () { }); }, USER_INTERACTION_TIMEOUT); - it('requestSession should succeed', function (done) { + it('SPEC_01000 Everything Session', function (done) { + alert('---TEST INSTRUCTION---\nPlease select a valid chromecast in the next dialog.'); chrome.cast.requestSession(function (session) { - console.log('request session success'); - _session = session; - expect(session).toBeDefined(); - expect(session.appId).toBeDefined(); - expect(session.displayName).toBeDefined(); - expect(session.receiver).toBeDefined(); - expect(session.receiver.friendlyName).toBeDefined(); - expect(session.addUpdateListener).toBeDefined(); - expect(session.removeUpdateListener).toBeDefined(); + checkSessionProperties(session); var updateListener = function (isAlive) { _sessionUpdatedFired = true; @@ -213,13 +207,24 @@ exports.defineAutoTests = function () { }; session.addUpdateListener(updateListener); + done(); }, function (err) { - console.log('request session error'); expect(err).toBe(null); done(); }); - }); + }, USER_INTERACTION_TIMEOUT); + + function checkSessionProperties (session) { + expect(session).toBeTruthy(); + expect(session).not.toBe('no_session'); + expect(session.appId).toBeTruthy(); + expect(session.hasOwnProperty('displayName')).toBeTruthy(); + expect(session.hasOwnProperty('receiver')).toBeTruthy(); + expect(session.hasOwnProperty('receiver.friendlyName')).toBeTruthy(); + expect(session.hasOwnProperty('addUpdateListener')).toBeTruthy(); + expect(session.hasOwnProperty('removeUpdateListener')).toBeTruthy(); + } it('loadRequest should work', function (done) { var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); From d9c59e8d0347e3b080b930158ff33ce281b47b5f Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 26 Aug 2019 04:48:28 -0600 Subject: [PATCH 012/166] WIP: Add a video that will actually play and fix the load media test --- tests/plugin.xml | 1 + tests/res/test.mp4 | Bin 0 -> 72807 bytes tests/tests.js | 130 ++++++++++++++++++++++++--------------------- 3 files changed, 71 insertions(+), 60 deletions(-) create mode 100644 tests/res/test.mp4 diff --git a/tests/plugin.xml b/tests/plugin.xml index e11e4e9..27fd886 100644 --- a/tests/plugin.xml +++ b/tests/plugin.xml @@ -27,4 +27,5 @@ + diff --git a/tests/res/test.mp4 b/tests/res/test.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..cb5053daf8854033eeed99d90eb66b60b29b2b1e GIT binary patch literal 72807 zcmX_n1z1$i_x@cvmo8awQM$W9LXZ$dN~K%6mK3BBke2RLy1N%?DM9JZ1(ELlFMhti z|31&|-h1xMz4OjF^UgVE<^li!wQ%utw1PR<0RRf{@Q?iR8oQbD*g5j^004kx;cRXW z0M{Sw%#2--W%4l4@9(qb<+j^4XA~L|*{7j1Pxtmccm?^P98gmSXLBfzfDrOfffFg> zQc;t8!NCue)|N$HGcz|uUXXQg^t3g%aDno2bMtZVa`Olyg_bTZj-p&#?(XiKZdPXI z4z|YjoDR^LS3BAZEdY!61-4h4`DM?7o^D4 z$xebB*#={?4-WRo3OwvQJWvZ`n2Vt!%*M*`p~QbTaB?(su&{ucyGU^GLR~DKku_iv zJbX}F2L~HtOXQj1{|R}aFk35AWMlqMzzwx`{`U}5D?4MChlW_$yO=xM8Y2rKdGrVe(F#xBTnQ)Dl>I2&8pBX5B$=xqE@#=_ay&KxEo3^g%y^h6$8nITb_7#f=y zJG%TkGO;p-Jv_w9*&GRs59)4i_1@CO1o4k>KZkXr{CAL!UaE!z_{2olOn@KU@jHhjR&2XA7vE39@4! zU?EQ=1du9$Iz51q;O0b@adLdP@xOm#4+%jLqyXk(?kK?zwQ@vi3275ZB_f|??1Z#{ z2Xz8|005#i5A_Eg-ye9bWCH*y0B{``)Cz5Y?e|HulMbo%R>>C5sNH&hP`mfcbus^L ztw>L+LF-rn#MN=ge&x{3$oSB)&-o<(DCAGWz->Cp{NvU|7LnF|RBrRAckC&?YG-CK z-bTlH*|U6q`Ho~WtS7p2yC!ZoJdLf6lcAR1Upw@NO;t;f=>%B*)~(FlUyQ7jKpBU( z(`mJvMI12)tDL-^%Olo6jcY=@D5vE&P*-lZ%-T|N8=;gOe-9$XWfu^90*P+;g8z3# z1{LDdcx>DRur+!$LlGgD)*}>&K}YkgB_qjkj6U&y#b;5;gM|TEuUcW~v>132f4(V6YsaSSlzSMdxXK~he2(&8p1`h&; zP0yb{-w3oO(#(64Y#gcjWkK?tIp%wd%*$r|SCKvj zd5K)ZUJPL!1i#=pD;s`z04w+1yR)ElaqpU4@{k+J7yzZ%>NNV>+aBpdcw`Ogt|VOu zVTA0V+q|^@3S!w5lgMEDsdKk7%R*!f>>NeH3>b}UJjEB}%YMIowDrjB+LHGbB~hse zZdLMJ1dTWZ*-QA+nS@Lf_e8OGFa4-_K#VEL64BPx^2KdCqf1!^9v9p1?te`e{$Aa= zSmnLAf7Z~KC5vc~5&@CnkYK-iFI5zzsbe&&2GejK-XhQH|peQX3OF_QLx8J z6d^XY^fMlvW_s+BKUUA3Bj<=V?jr!#=+Hjcd{7H-}JtIdTtBn$Kq1mvwcG?Yx9StY;VmVSFxS&i* zl~?yb4iM)DtS3c?6L1wk9YR$*93(AHG;hm4-oKi%&5kYmVuKC9Ti7;SO^Qac41g^w=9zrO|vTSKwd;V4pq{uj3u2@}^4C+Y9sZ;jCkPCLr( z49Muee8E;AQd0c-q!uk;a)LKHiYb`#<0}kG%-;vnchScZ3S0z_z&je~?Hcyx z5esd7Jq^X3RfQdu#U&j+w>Lr+&A0cAlS^{_&cm%y^22$)!dn0s3B^M?m2z7E1!ask znc^N|qY?4^PyS1nw=^oS=hL!i^r@jc_u>AHAb45*)??*%sFGb5?`Q+F&p2hyNz#@% zhLT2$MBJOMahlODq%XaR@=?$kf=*Jt4tKIF%naRF1T}Yvgb_gsWDV(5fs|Wmv&Ok* z^R{DK{m(=o(o>EYZRN`TbpYjJDq7++X_wMkYY-IqS~pNvXDM9rjq@V32hUa1#DN2! zz=55X@ZDf^V)iL#KwiNcr8#dwN7?fCDru;Q+;CJlGoI7PA=)jL=Bo5PnxcD7^}6w2 zpb(VKYON53h8;qh=rU)%_Gz zg9TvbUd~cbJ}@Ul^Dh6xt;l z6j6;K01(^MpGH)Nul*ZrOl>?CYQeE1G?2Xzh0SfSFP^xcc;FD|9KiMG28x0G2f`tc z&V+vLxyP?N09sYCJ~x1%0<2gC@8UsIMojgW7l)@w&Sy&$nQ$y6@X33U3!k__Hgm0Q z+1Io)-Ff~q-Hec{It5^{luKH%uP|2ggOJqnMRalG=VqeEvme`kuJKO(L{stYNWE%X z9U8+Bn0#~9_o?E3>GF=`$Y9iYIPzLZi2vMb&u+oXtv)i(xW>NA%VVyoaduv?!aVS! z?M(ampnavaoGq9A@aAEQ+vIKU@TWCPA^gpAdYoy{I4lF_yxR{&gB=C_7PRN(CyYcy z2+Cv*&b!C|CJO%o!4|1v0zDC6uKd~|Gp5e5JKnWje)@ssPigpc^S;s`iQJL&oNj# zHMm>f8#Qfp(@7-V)A{1~F-hD;-|FhhKxhBP0DO5}WwsiaY%vb@v8}pQ8{>VVLU@P7Wa_%5|CCo9EfkECtEFVbU`k0 z`Wj(l=-*nQkuoFU@1z#gfBX)`I)B~Xpf96O&9h17p1$qGEkc&@zCZF^WBz2EmpGT1 z{)n#aFA;l19kLdOvIdLZ&PHlM`}KOT8cfu4L4t1M*z+b+w%-%gi28h_cZzwDdQNbc zIx_8C1VdL)XipG7?Q30&kpjAB3N#5xB$J;(T zxznD)M)pz8=)_$YZwkow)D!J-12Y00NT=-I*)Qi1c@0f_&%a37`E%rl$zGIuP;$3% z`aR-~j!S~X&jci|Y2=U`+;rQzcHHc_AW1bM2oskyO&LVYPk+!4 z^opy2(}11EMG5d+V9h*W8xj1QXeQ>lx<<%IX5!g7&(DJpzVs~s(kJvn7$IIEd_>TS zIz%gVMB8wodV$#toP!n~>@Zn6m8GZ-1!We=w;*4LZS316$oX^X=QwlKng%RN4JYww zO&W5ovHMW?P~+XGB~NHsEHBHrdP(p`O*n>x>n}0W6_B+`L1mas`qp$fzbh`Tb}_r^ z%+}!AL0<0sW=PtH@=gHys(2AyU$lueE9o)Xpe+?wc)^+MqaZ9D*0w3?51(MS;k38GR)I^u|(4j!sEmI)mDAVM3)6PUVbxm#DdWUx-hc|n7Td1i& zEd<8Qbi@rV&Qvun)y^aNOsIZ$dZ`2U1?-3Uif8mErdU2?FDfp16S9mXF(a@aaIFL^ zBY6r5LZeXu0Bp(-s=POomuH6vuFsWJ%nc^5-yL|9Ox4ThDQybI+>#p@5U!K}Ywunu zVW+1M9ybJ5h;nu%zgMEmXF-LAldix+(m9t=K}BQa&A24>*VY|ucI{W(xPKp|-o)Gf z?lZ^v;ch=E7};Pizqz}4?4Fd=V3V-EP%G5X@Yl|Ec6Qxweg?YGk<>cZVAW#WVj5$M zpB5Ry1v8rI@N%j>cJ@?RX|b_{EQ15{f$PBC+q{B;WUwEKKa>$@bVM2ov2-TS^QwC^ z&3=w$>P5^%Zn13SkmslrRIcrZ0}$=Hoe?kPl-^&WgE64R7l5iv86` z)y2ai4ll=Bvw!^>@WwLRF9~@f&(**q65S?2tAFp8`{y`c3SiE>(`$4D3z+2iiFQlH zlTDAt0NlJ}J$>2cQkrDmySca5;$?F}X}73je+3&FoZZ9&Js=tFLDW&cs3}7(QCYYt zzlLBi>bb~vSj^Gig#qz7u)L43r2B~uDB?FWtc zBGxu6{bm_>>;jkgNxMu0r1&7=;(;+!jv@R z+9KH0IdstME$QR5f|^#4GA=G8B<59+m2SV#(z#;*TB#3F&KP&iP)_SyKNf=h-CXLh zYRR&nN*0G7V+ z9?L>ais%5u$j5@uWD4pn@p_cmXCT^A&T97r*c$j5C7wstNRZ$7lJJq0hH;TJDxsI; zAPyU8>|!X+bhIOixs{(t>DpOGBLJ$Y+b+i7$KeKsMVRvX+-ED!%|13iLyIJnWUdvbFu7 zGQUZ0`;wB>2}A3#URO;5jl@0W)GyTBC<~}FGX)!mcMbQi1ce)W{W8pDj_4ZD6D%HS z3%v7C>mXquvCetj&!B5B2>LDR=?5c7m7zrF8ES2NEXD(r-O|h$Ux9ws-iT^EQO}0G zxyqT=js2y%mL6wn_b3gY&E4I!`rQqC+jVO9G)ULipW9no2S{INf1$;x-?=`MLII<9 zp^aKqIm<{;P_TaAjyg^7NK|_C=QcIq84@Gzw+-3LBFNsxfIqgSXQzRqwyHh2#so+; z+5Wmm<8Lhpse+<%BB!N)?1Dn)fquDpgF3tVwS2F_)y}{YBbdP`>cCw_;unL(#WFgN zT6Fs_Y)&cu(B9k7>Nxzbis4+9DABI>^iceHksYz4_VE4hsR765KaW9=)nA&0N(Q(A z6|AV0e8pGXXZ2C?3)k*KM`!g74og)vKEq}6RV|JpO(FiHzeZ-))9UvYs(vhtAP@&& zrxhBE&4uBz;mt0X{hh&x_U7)j2(^e9{v0(}^_PvQ+gc2M+FX5ho^@i|)m_kp!Yni66k%Rsly$3WDdi zT3CkqdU#yKJSS2-80lSXyFON(MtZMm$IG%47|_+m-V$pr3y#^<8c-U3)XoBnLm-lR zn4@Xy5Fbcmx(M>?DeSn`Pw?nORXJI>(etJWwldIJT6NJTA79-bkhHcmpEv5U-M*rpw)yLD}Kj&o_1+2{I zr_6HacQbtce*KnPPYW2UZaOETu(R`Ci&JO)GHYd;F!)9wyy z5WE(9|G`w;A*rY5N3pB^xo5AjXk&OybYB+d3rt!)k4A5igXPurl_Q8pdK42USkJR5 zy%F%vqM$%~vYNyHtX%7x2dgGedz`~cs_Y&>G2zq!t9ekk)d@|Kny_fqOSKhLC( z3_cRJookMF&3k9c$URnFbt2>FGxC>qN^%o6mwPv8QwB1!zP`N?<-+MTl+_L(O2z9* zf=s1I$!EoBZnO&P?(Uyc7G8r$k%nGZ8Meg*aYo;Xn5_9^!y&iBC6)Fv%cV!L@VSy+ z9T$mk><9+_>om)mqIV)qDt+~EZ{qicp85I(F4(#EQySQl`(7pGKJ80cM3PjE*2c{r zJPmzy1NcfeOGt`yub;W2=y@DpfW=!qE|xwMc9BUcb4>?%zTkmhI*W)j{&aK>J}4BX zMH)m?7V0Xk@;zvU)+~!R-V`sPUW2R$drpDsf)PbeJbB!AE?VhoB`1)K# zye+biuByZ8hwb#z;0A$ohpEr)n$KT{K8PP*Z@4TDZ_c<#=Lqa%Z*gwjfm)Z^eb*kW z(JyqQ8bdwu{*^Y()2wV{uu6aj!^tLpDGZ|IEB#$pi5ewkXuuX$+V(oqI&Mqm@jIpu zTjRuuO3h8+o)@&*n1qs(wG= z%Nxm$H=d8VyjE^5lcEpFZk%QkY9b2^KkvA&_A-(kTsAhP3p-wmFYycIQ3M;MGiN*vIax=VlIRomzOtsjzvUZh*7_boAe8a z$kEE-rqkttqNN4{8OMyX|irriH zC94PA}Zw#gOByG`zoDjbz5olYh8nPVfcq zBa_m*>`$LI(#fDdmKx;z< z8#E4rBjF(Mi%n!Jczf5N3*NJjdnHRHO5k)?*%%DKvg zrW$($%Z8Ak^Ci^Ic5$g=-(84+&0;t*y)E*lEIo0z_wHRKW&koxGC$(uxalG?5~2)u z3HJ&xy^@P0mYR?`cDE$*gxNm(RC-N#J-I zas*o32hG#`S~!I-g9!Oc7WovO0^19_2G@0s3;EaYjI_-EAw;MTjA(4f*n-8 ztLG(Uf4TJ!!L@kx;wr+OAusbtar86GmoRg|-Ol8cCaXm+i$&GH=*sU&*3Qp;<`$+n zd`v5p*Y8u4rX>uonm_jCJ0swThCZ_321Nwsljf^=$!A=FAtYHs1AWm#d?B>r!Z+zn z!iSQ2$bs3ouUOk%R``nc8Li&s5ljTOyk7q#mi7xBM8x^R3F4!Q3F=#Q#zORK1jalj z^au$byz!)e?$`Ko8L0|nQc2zRr7uPiV=@P@ADs+hCg?IPIn?}hWr}w>8e5qK#ue#r6a{8C-HNMGI7+S?w?Ugt5F1;D4TbP2suTWyz zKZ{dZ?$pnt#Na-yE-p@%%Af8B`8oYI&VdIe5h~F0Kmw^@-CVh|Rqds+qO2vj)+OxU z!{MyCpDP-kX%=9uwnD56`1QNLA7W5!IjmlaNP35iCUG+4ZhfHTmqPS~46P0K93X~| z*Bu}8t*ph9|E&ZNkV#qxr%nA50t{l&SMl=@}YY8!xt?5eO{6;bOs?5Fp1d z0syTE0;!7cSog}VmoY2z%j^!i2S@{{qbHs-PEZKC>2p9%dIa<6Xw<`e%Q`3`F z_%jbOC7OGu!{4)_&-T>r6Wb7pcojfoW~;pp{a^Dd4><@8}kvh z`x0SB>Q8y!Q*7J^Dp55ZRNWC6w(nitubeKdwePotIAX$@L3?7i=jC7!u|Qe81utHV zB@!%v(b4NEmD5xD*#CM&(~53YAs0ObXX3HLMzK~+OrS^YdQNsm*Jnw6r~HWyW|Fr; zlc{Sn=IYJ&$`_mpQA%ZpB)hs#0_9b{7GO}Dp8s7*b}MAs528UKI(;7yQbe^frOCY)2RFn6C0PQ|!SbSC&hc*xca?Y>zfeT)ph9 zJJk$#oSUCOhjq0V#Guct8aV zT4m(p=PM`r6cm`WU_(1*OUX^c1o;b=*QRcwQ7H>IB19N>xdeZ|*ob3KUZz~RY(EW> z)W>ll>t*1|2#eC7QT!t+tDdeXhqD{RBDL4D*dEGAaBlip>cIwmq*Ha7hyT&+e>{tM zPbFz1^U=SdX!Sy+d@-PX=!0@}6H20zLoEJ%w%JF-L{NQxG=l=m^O3;|*;0w< zRz%wO-t3|8RSq}<)VQ$M@#ByB;iSJ)Bk~@(N?fxJJx!vKSk)tCohv50Fx&d!f}$iq zr#D*900$BBu}3mLsyp6VtDs#J@jRStx<;gEWH$6(^a!w`qK}jtw?-RJwDMDk3%1kN zWIbEib;cEVBle+?&%oc(Fl9yyAiuNg29|exN||%R?dq?O@&4d<8g456@>lCaZX= zz6$_QyEhFyN5$tUisu+!WrD?ph0`x?ctlNN(>IHKUPv0!IwW43I967B* zJZ~SLOkzO(8p<_xvsP1!Sws;*q?Rv)5FPSZgv_kSl>p~R7;i@zF=rsTAruDhU9MED~-7C zAX2SmQH1`yh%m0#>Q3NoF=`36De3_z(ELMVP#C8A)c)rIoBY)?1eQ<_gjYZiq;9?t zXLO*BxE?vZLr9d&Hf>pdeWHmftlkYY3G@AM$V?I0U4`&$d^gwzj0yr-ooO99Vr5KS zFW*aH86wd51w?%kt(E_DuvO;nC~c%(eKKWiE^Y{%6W00F^etB2arp_p2{Q8tb^3k{ zD?VtS|I?K&L+qdIAFE}jJ@!$1E%H2=VmjFOSzuOCn*^su&GBILXj~Gl{R>WUJ-tgv zx~F$^lB7KtG9P7{mcRkIzbL%>mi+I z56NOeq^c6ANY($Tst@9IPZwZ3*xuzIl=)}{;t>WtO|K|FtRfbxLma3sud`B2X(Dd5 zTmubOAUKEpEcB-G%Jnq`gug`{^64qzD#0W-{UclrOWclN>PAoL+Hg#l8k(~Zc1x*| z7Or#mtQ`GD5Bz0BrQhl1H4Ft}jjdiX9Yc=SqdA;_HGbp8>F~V!rF1vDeiFkB)8lk< z@30qh)tHU(3-3RcUNU&S@%N-mpsaphApU&&Q_DSpRH1f=2UVCKh}bL+u!w>Dqgibk ztO)CWXtGW=y77CP4P8G5A(>A@nch$BfG-Wi@QCF{YhQ3P7CrB=4E}5o-o+E`cR9ot2QE4Pj72Gf)rjUrvP74 z7wHxsi3fN~DPU@V#Sxg*EFaSUd@C4V_&n^DoOj9IMmMKIluS+=M{V$|V}ihcRIY}F z;@j`WQtBDw3P|`|0sou2@SwjFU_#$rnvFg5Za>=nic8>sL~Da*_K#>u1i+H2TvwE?Ro)FwEdraGK!DidsaDN(lb$pRL;brisTwgN>^~ zE>km*yDodDFS$w6CA(ID_~@pb)|R zbN!GkaZg1l>Ox44c_!RLH#$&1%r}3!Ok;{hEh6QrLYlo{G~&lxu`kGm@EVH9bFn4! zQt+5Bev`+R^=bFAUFBfXrJl?A6EM`~1^%Ai6Z&q>;5=SIeC1Q>#fs`^qRY-T-iu5`9(t(&zt zbLixNxUepS3DsySzd)Y+SeN2Z*e}*EqH1r%i7o0yVH=;DPEJqcLZy=xz1#JNKu8c+ zdpoA+n?H*zX zbocAC0E*h~Z<2#EDLzNj`?L1uprL&(XKD!vQk%`gqYlv`iL({eZXci0ZyI?ih5Z7g zC?8V%#;{gjQ0p9s1soGH{=y;`O$h2^iK)Q?vDm$VVvcN27Nqv34QTak9B%Y)Iy#+> zzmp=tfGNu9V{Yn}8#)BK`@}`tdJ_n7>Rmd+*4Mg{2tp`?z~1eg-olXi7m}Qhk#i-a z@!%2?g^39ujv$wS$EBx-ryoH#fSN=>2pBHGNY1L&#W|6cjUb5Ka-(^a@d0K(NFR4ndrJEeO4-^7w=N=gPW9PZwTmd78ghe+p&N!57mYyhVfW zh42DJRG^=^gs}dg{h(GJ2-wM9tC{0ZP3`rNT>dc= zInq;?S7cU0N^#*z_bnh9FUv0;f663mlUeCzv#!v+TCtZspkta7r{}+?H@zxMTtH5! ze_CV-eOpr(K18S3jwZ}#(dufFk*QUDFV+O0^s<7mSxcwykZgGTiaL(+m)oH^F-3mL zi777a{Tkl`@^$}%Zya?1Axhtci^WEUHW;`aWB83hn8x&C<{Huk(QJ+IXg-*N7-x={e%=29@!5ZMyUZO6el=g71R*_#&a3OY17F!ye1@ZEvgP#;gv2B4JdCL14=AITxfOj!g0BJA z*aj7=-(n$+WdoR{=VG~m3e4GAH$E2+wBRP4>b03IrTg!r!>IAFR}P)vp3C!_^WHBp zTSM8N)MS6(nX$kq2CNguwQ#IX+2P60OF>hHXV<6!tY1V+f9Q2`wF&w9#vvLFOSe6l z;$etJc1*je=u3{z_2gr=YP9`rc$1b!ELeYr?CW&8&d$u+pvZB|$*uJo3nd3gkpfWE zl-k_chJ1vlt=SP*Eqaxl?lC)yKbnUouAkc7S~YUC z8i37%b4~=4F23D#7Hkplg-XKiHKae^swUCmM2IZ{+9i(=G(W!2jTrxQNdo=od zvdhnwARGY>M^~ge8qf8 zz{}Hr3Tt>y`|as|JBPs>ypraVPeXg1yJLzGZBIEg{EY!Wx&8jg^iob*h}O6BxtX;6 z=H3rtQkf)SiWe!=VdCL6xwR@Np+pK$^&`d{IBCGSzg=N*hgfZO2h0_uN0fUKOs!4 zW38bwream4QfI^Z$-lWm4ZbCu#2TVmx?SOC-D|n~x3C*5of=f&kAjBtpQS?s;l4&t z=#v1)$3qV$k9$vH5c5_+kc4|5dj?Op0fUHr)yn%!A8;;p@zx6y{3vZDLN7Y1T=^(? zj&H25sweNLv;s@ANXl>Ur9~&KbU0V7E{$_qK1DTQ%#yom;!mHRLan=7I^gb<>&~iT{wb(Ee68U<)(7TN);lH=HAVeFAk)^@ z9Ta0)@3ibr(W%~lyhu|?LoTPE?a|O;zIY~#nv4+1KusCh7KQ9(zL#>b7ZDqtOVgzW z8xM(o5PcyFb36Mv{XmS!&4%%K$eFm@&s_Q;F10m(Lik@?dMhO{K*cp|oN2v-7{3J;J2|{ zDr2Qp3eGsYAU&bKPkI5yQu$;=KBxNBO<*ktB0!0+l5O^`wW@^~H_Wn#xv%6kTi$02 zMmzD_I9MAShWW?#5tQBlRVAxMmTw4SDi-1yJo^M_Ow&q_eCy~Ds zcbFq_U27DgIfZf_NpmMRxh62;Zt0pK6P?5Y9Pw`S=xQ<#p#gxcal1@2LU$GU=nRuC0Z&WxCA#)vt>2 zgicZTM?F3BUGIIXS*F9o#Mv7)S+yVFl0;Iaw|2a+%`!yUtk)eY*9+08l)$@0;`+z>d z0mtZi>V3yA&sm)4>7Kkt)ml^Rtcdi*49WH7D0=R?`1-|*c{lXgAH;6gwnt|q&s9)3 zK6q6|%b=jtxvpgpW$&sL=a%%&uVpg}zLg>++M^`kR>t>6ozS5LdB`MgO9C&i;%PUu z|L9jf%{TrVppJOd)|%$EdvYJ0wU8y3S9^=dEK22IJ^pN_)mS%$7*XR;94$O>ZiCjNLW_;-71)lc4r z?lPh=*qUm;FpKzU%OkqplZq~NR*m5$57(Up$dmRhm=3oVOz8>zkJI1WgONAs>~;As zjj1o*p>^8%;A>F&VUsHcjoZbqC(F!CTu8hr>4R`Hurlqv!X>i$szx!TLsjJRUWhaZ z!8(&h6IY?|PReH79xE(!S;Bt3h|m#fbetmrIH4mTLOvHS7m@r`?ZBpkrapehg|dvX+=aZ^tN(I z8Kc|m^)gcM->KlNOWPW=rjaXiPC8Z>g({GugT2f8NOt-zeDMZ7R|KpwIKO6Ws94a6 zH%};N6TPa=wkY$QM%yr{LJP5wqD3@H%)Mohd{7TkasP8{DFrxUJRcPXF(buT*-3XN zQsSTU=CaJ_Ma^i_(@WmzWp-&^q1o>Kj*Uw-ejQX~UOz8aqO^xt25Oyr>FvlzO^oPY z7_(_^#01O>jBQIgt+ZV($P<75<126qIXV73*?OBItG{@=6Vdgq)PaaOaYu_f_x7XC z@DHuG@(pI)bzhy8EIF)M94QpKgh&3?nbc(*jLroh@L!tL;O&1~ECBmgww`E*{thfl zGsj!)3HMj=xKS)8e2s!~&PgIii)^-?bZP=)wv<1}I&l*D>4(8@{rvnsQKKF|%#E;-ppAwql%4MzKS|6k3C+oXJ6`r!3a>jsn~#Qlo9%5*!>3EVK1&}B zMO)S;FXH@1@8C9Ba=(}N2(W+Ibd-NqhalZS=1`2=ph>N?9;FeKBCGj#Ex}2NG&|2t$ZvKEUSu?=o($XBIi8hp#I z@7|*N`T?Ti&pH4Cj^oy7Ue34t{sI@YZ(=ZGj`}QrM?}$!n}`Huv$=F z%bBohgO}di&(U;l7kvE)nU;{b7D7h)* z)+;kfm@UtbeqMEysKatHa8_`%r1AXACGU76y$c3I1~42;otM~WD?%zRKk|_ph_HB} z$SrotZ)zq4WGaM>5BI;t=@l$MIo_I1qY(3Kev9}*6{$nImsq!5GV@PqjA1oXUR>En0cP`rA9+`tdt`zCNM;fEHPsb3l>;92f~sjq|{&N1R6YDEhzjj z)2@&3t7^0y#N~1AWE?{cbIM+5@!#_{kU=;}?(uaS`>B2eRK?Ti*}6uVd<%wtmNt{3 zi>JAN`(f*g1l&Di$K;bQonG~(Nw|=~RcHBDB;H_C+an&n62m$o&ek zo2SdTdn@GrWGG{QvgM@}sxAr5k+?%|SDt`Q&`W$Hi{n${J7=b;`dw}#t9ReH9frj~ zRx%xi4b}EMY`oySfzthR9eT*Z6rR$3esEb6rfiHuX@*Q)x{3K;a7?GYFI?7a92k0e;|yEu^j$v8L%*vDaBOWEHMzLx(M-@ zZK8!`^M7_T>c4tTr9vyEt=2)c0(W=LSLz6e9R z0p2j^wAO1aUUX+F&A!F)@fl(+z5G>y=Y9OUyi@aAM$PnAyW3_*ZPNSRm3LcTZFsgf z>R*iBrIK{cOba=Hg&rqe3EjVOo0WisR}8sjxi8^kifm4BR@~q1(+Z`H;JZ7iSXtku zKUvNcx)c+j9}aGk{~Pml3;>xg@Zjx?C~MGvm1Um~>|!`AfBQEo;?yoC?a6ZK69aY9 z{T4WVK!8p>?e_g87&s8%{3#-Z)(ztAC`6*c?Yn+29p(8e_J%NQndlD$FNvV@i{BSD zY>rnu>^?p=CwJQS*YY}a`H7dsCpL}u2_>DJ9%zRInc8m=pTtrlhG2t_21Vy3LSw&L zd@%9w>fk$d{}iJ)5RdJMvZ+WBC`c>%1F9kgj7A(E_|6Mu$QAIPaGK95wYf4S2cPA9 zO!l}j2#KKfnBVV&p_=CGEpYZOd|S4iX^9>JFwPDDP_0l}y^93-a9(8hnqa-%>OLwg zO0eM#{t(9t?|pZ#&K-1gB@Pid_@mhoNy(E&BV45HQn|a}w#smlaY!t1Dh$d)6SsqTNC-UH?K6GzAg&^bmjav$fs_4!ECj)NTib4=c@>_(_pVd$(m=$%XNx+CoDr`WeNEH=r$6}jFF zM7_V^5wsUHs`KBmP0Cv!2Cv?PQtWR%UtXJsY)~+8Uyh2h#Kv*HxRLY56$ep1MVD7f z5$hO?K@3Uqbz&PnHf*SVX^}hQIoqjOV*D;tGXX9i_pN;WLqt!KU=9 z6@}k0WRg@xiQ>5$w1}@`$II^zE~nVS;f^CxHRYtiym${x0bpKP)MeYKygsOdI6I%3 z&ESRV?g~B;lMlqyIT5RIw-yvCTqDV3$HivH_1F$pk}FzM!-?as(<76$O7+WNoynhw zJJ|l(puA@NnnTAwVLVs5R25G|YVwiaT9n))e9Eq5(Wes)$tiA#+2L!oq^;HUrjuLSQI z@P1CNG{Gl6as_$C6B&NA+N@#-&KF3-|FgsM@mnN#sDmzl{r5HR?Et$N^D$0?`gB;y zI>V>9iBdY`9+L_@CSk&t550Bkkja&u`Jop^gk3^6Ta(V|7aA%9ktC+L+w0zb0wTzh zx)Edf5wI>kR(09F*(lQfjbQBZ<*E4J^5nO^6EEu*tLH!;HP1^8D4vOE7ZBI+U+VnbC$2^XjY~Guy_*<{cV5B@UHs(=Ru|884tzRE=Ux z3jOlnZlN`@chLaB42UK!^uhr_YG8U}-*%>I&{+_Z&F!4Y=T5_yuCB&`&1W}lcyrBS zVYyI6KruY`WtkE>WBb278c5b{8~W$}VdFw`a}Be-de|OPR`DKe5kcR8FgOx3>gn}r zC&XXkrZ0yWxt9C4?_TEbi&-xX{p-8cm0#eIHPJd58clw|C#a|PgfXtge=O2eh)Q?N zUgVABz{Phj2R75Q9IxzO*6{WSrLU`?q@(^ma)ehC6i0vJowacp?CAoDH+l%W;)+^byLOfw(YpG=Hsh7 zlh>81urr3vW1|%*26Zu@6S81ApkQHp`8gsJ1ErPgWEg09Peb7{Qb;Vwbv%@y4`=&- z_0sw|Cg=Qf(6mRzDs$%Vqr#Kk1ic-8#>dw3GZiB)_192GAew0&>$k7xL?&0C{Y+|1v&ft-+oq47HB3AxF^(25c#x6liN+yd;P$U-3I zS4>9XSZhl=Pw_dpWAy#@8*YK6M_*OcwWFD1EHDz|dRXVVv~V=#$>o>TOrJ1D+xWwg zjYM)z&)@&NA!OEkJf1iRW8*bK?m;5R|q^T|R8(U%{u zS)z%*2u_*B8VvdP9F+7L{ym!9?y373*;KWMDA zz=#y3E?#%a-#aMuT$o4ZdtcI}QKozHm4z!6-#u46BxQqW(!J5tbNtDTo}r*xaS2?j z3$Mn2QYcEKZ+fZ5?EshuY*EvVICG(c&ugCeN9*f{dP^aBrTi?~764V%H3HNs!*Eoe z<(bB1BnV`z+573gvHCDr|BY22C>9<8a%Bh=;JPQa--vRv(xRgbOl>AHu$X2fajbN^ z0tE8Rg)uNQIHbEuJ}A&~S&~Ca3DJAnXw17bb&e1Y1Bv{e7ym!D-ZCoAC0Nv+8EkNOcXtmY3>t!aa0$UZK(GKqaJS$d z+zIXwG`K@>2=49>zG3fu?)}y}mo>kp-!*h~byq)KT~#ma-vbgJ4t`OijOhN=1G25s zQwOq3MIo^HIW+9v+7Cap<8)4x32fE~Mu^u45o*F|xW*G)Tja@EUe=ozFAW`-8)VZ~ zn#(Y1SZ@|}XSyvAj!#cl3tfpTs!D*XyXsYKBc-3;{6@QZjt*QR#1Oic)*U%`a5s$* zH=yK!W2F}gOH9=M^*yj+T1G#(U{w3>=$B8YKhZRf3SYenJ)3f=RQMc>kr?xaPBjJW z(rf=gh7yN4N%v2_EzQfw!5p!p3jY#PxD9l=1lh{wvCB&86W+q3uK-|+wr4lcb__Z-Rw;@Oolm+H|PNn znZLI86>qHAOUoyw{P8)^?wU@JXG)oRKftpYho@gO7Vx|8IyScT_nh*@MrlZju2(a8 zOb?ZWD0ObaQv)KwvanASa}Hf@NWg#Yz1bHRzoS%ZP%{%#jDYKic(X3wQ6gP!vD$<^U5Lf7HEKJ}qDaO*EOz1*WzpOfAxXUiI7#>LNLw3Yji5d;6<( zFW$slzj_-}Vz0>1)ss}nOka=`6KoaO+0%C>T3)FS7@tMe_(A)=TnA{QKTAq(zCO`# z>WeJTVPiMNNV}GBC#^dLABY&K(NpI7HpD?9wXU9oLo)*8` z3atSU*92|tF&N3~)|-w``t6)aC5;!WY?l@g^WF!wNFgY+0A zyt>m<*23|Hhm)i@5qsSt0ekAP!)*kzYv$Acmk5Ku%YUarA(^XvY-@CEte zQzSlkPnNI1sm@(W-l!mdC{?#LFF`Cal$;@TnOR7$eEb{x;b)Nhm_&$T%~8;H(eWNW zxLU*HC5U9Yfj$U+A4r@Dg-wNa`Hd$gBn~WMx7c4&t2n|3JU8aWR|jMY)%ne3bgtHR zg%#^Xbd*9z1N4|;s=g^a{QlN=oivE93eQ*+{lTBxPg2X>Z(48r*jtij$8vHoVyfpV zoh$m}sd<~`%{q5hROHPSKDCsT+cywcC4BZ4{cWj=JQ5WKG}~XHs)8L;7%HZVzga|+ zR$OT$O#a43)x;Jmselm3zBX;aiHfmmp)Z-<ygBf&}?B@?I0 zezsInq5P`Rj311znBJ+ZzLt{C_EAL&?4B;S2haprW~Yqo)QcI`f9c*pTfSHM)9z*t zE_0sa;}t0>CR6CE*u>uXl|x`$%Uu5!0hw_Tt1qykEOXBDy7@qzGs6Xrudp|zm7<}T zq+&p87j2p$H0YByX2nx%)@%&W3(C!oEh~csm*y@0eRl2TpjF$*9vYI*|I%%2Utj*Y zj?T%XRk_I?D33e4>nyPVgBV7UnNH+2!C%Zof&5_s(xRzWR9}wislqA9a+Fl6Bu-Ky zyag}JlY>~mjFs`3H|oy-FXj(=DH8lj)lxPyS;anDZSA+h)KMaIZj8R-@Tlr!=UfPM zjc`Wsow8^+$44#4bP~vCPVt=_YiaGyp?P3UsJ3~Mv^@hhlgM9RCC5x$apoi|(;RyX zlp{Wl^m4h98Qij;#*K(7fpH|k&daa6XNy{yPJEeO5&~hD>f2@3V(^aY&@ZT=FUOT( zQ(1U9SPKC2@**{d;Mvor?m!xAa1B`!Eekaw-|vt!xo6LQ&*KkXbs=%fx9sCIGn;OL z-p_U0=m;UYwFi1XatyJayWGkjXPRF<&KAvv)2{S#8nb(xPZa>z5IFD+NQU*ytQ3(Q zbXiU>XpOR>&xseQKD%;(!O0^MHb1tYK<&R`Zp+Q5*#p{i^T}N*T=xmt)P@Y>yXDF?zKm|8-`2C-VgVb@*2=8GlZ^_nd7I#Xy zd+reL`%bzDFGJKBx8W4j+*f9BP?$E#`G)Y&RdaygP8up$hk*xwNp^MxH_=)iy1)T4 zZ?Gc3k(1a-Y}^}1hQ^zdDbK^SVj@2`-4D<0`KhCZ2+mc_->0-i4^)^9(dIk3?vX}M zwgq_|SPm3j5i$>cCE<)r&qP#8Y=*K>BYuO#Xl7k?|Slcu(WUSS2q0fv{(~NUgl` zQrp|mH--7uQzuO~-L!W!ccx--AuYFSyF`~jJg&0X_mcf)17{0HuM8Az9=x3TT=_-c zLzH~gBfj@cW|bcBT_uu7aZH&^pT6>Oznr-nI~XwJqMN*=lcUWZAg|NK$7!+obl3m5 zJ7H49E^_kIWo-S|hHFbkF#6x2tK0&SbYr09VE}!w-YwtGGV+>Y`$zQGR;>@mpHBkk z(EAyu+rWlZLyJS7 zghlSWM%CS~GUw`FXJ0SD1_G8ha`G=yR|t!s?D%xnMO5n*NQln z+zaFYfI<}QdL%?t!X;wp!;&NC@I!pr)3ar@PW6&Mb=6hIAT`5XYAWG<^}^;qV1}CL z1S}^U2LwU2tMWDRO$;Q80!-r||1GmHPI&8x{HrhItl z*F4Ng;{Pd`S2ZMETRXdYT8)|*)JcFvhh6Y(?_lRc*|L~<`==z#`PF4+HqBX9AO`F- z3)9E92R8JsJXlaF1f~zrYDbm2?lOh=tH$)t2A~69|6dLy>A_wnoS3Uaq|Dw<8J=BP z_Kkt2@GCS;2Fhf5T;xq=;j@#CuZ9LPEr)*+MtS9`OF_|Wk^sPK;m7-RtGygZ*CbcJ z1Ou6r>UsR+si%rx+W|>n`{LN^AR>d;!HeF(qQ&VYSWUr%aG6XY-YQjUbv16Wbb@+v zpptjW0(d`hLq|J;)m-%7X|c9SkblpOJ86pp0&KBmHd!Rr{zni0p<@m#T&faF@J#^7 z1dyWlGQ)h{aE?V!GyR(CAK5aEUE?26^Scj^e!!+Y6m!h9%8~q)NeM6xIZJ={*wYbv zZ{l}A)J`cXwK{_Af!YOuT3$yqPM*nVDmrjFxa2eRp`jK%w)YJjSyE7>0;zEj{m}SX z`NynI#}RftqRu>fQ$V1gPryS^=*Db> zRc5{qW$*PBr1{4MH%#;Fc#EWHeO>r3qXhgr3edj+&ddhr$kRh{v^g+zWAs;zhHc#~ ziZ7$q2b^aWb278U(o*i5*>*|l5}eAg9n^l1bB9+h#=Yx{vgn;_!n#b68ljI9VqlsL;9xtD>b8MrQ8YnA{ze;!{9j=Xzk_Z_=q z{rC#YH{-Aj7%cz=h+wk2M5cB3Fu>MCd_%mCmI-gNImBM;H~P|A*8WC{F=KOHRz9Oqxp zmbka+4JX2>*FV$wdJ*H=SV?xweSZG@*Gq30Yv=@WBr`u<{pas0dQIGNQ-`bjGx;)t zTFZ^DE>4^}7Z$LHn?-K_;&g0Z%Gx@4N>!|G{Ai3)UJK0Zq{_-k@a$41un#=&JU*@Z zrAqowk_60hJ`aD%{z~3~&?;usbq>>4IbSImx{?$S83Gd!?rul)5vj3y%*jG6w$J|E zWUWGXXP~!tRi0h;X_wrITt?eERo+|;)D!Wfp%8ndQ(y7ERoZ#*!Xfi| z)&4~!u4M1XNEkZqYE)a=$njI1v~;${OOxeHHb-Yn+S{jGTE?7#K?l+wK0WYS+&Lob z3I!e`j!QcS!8!~UCWj2S_)mrbDliKP24w#l&zEiC0S#G}Mm(gG28$}-MTL|%7+&-8 zxKu=W++Rq;58dlt6>$~KNmbs^Ekym8ygJyw@O%sfd^$3t(R^p+h||QBT5DZCUn$$o zVXtX&{;@Ax50A}&iN8JbFKL4+Eq`^QYmt$H#d`*&kx3>I)YHjW)X~6$89gPVk&%U4 z;ftR8n~=Kgrz<6SIn#qpI@GaZK`I>G{z4*-5-bX@n`UQ<-dG@Vp-JlD+SAF3t|GGr zE%6e52oVbsEDV(sOHd#H(d?z$Z+ygJL^-f8zf>rNhF&h^pno~Hhbo2Ue_S@NRlhr7 zZSp{(+G^u!PH1ew&krn&xggbjcO0Xm)4^X(XR4oI9}#5A61dkmWVw0jQvZt({;t zmdK7*EBVFT>(QO_(KU(&*Mpb)#ur8XAGn0AG{;gjd^uWRDBg>-&+XcDK9+Yl(xiKv~3Z=(*-cJDNciK`*qjCf&Rpj!zr`6eTPn=#_U}Jw|X>j?o>{~}8EWtlh z@u@x1-ie0VF^&mYm34r-6V`Ph@if5yK$@Q3e-_jT|F{Ul0C@tZEAmzrRqt#B2%-}W z92m>>b*=r-UmQjgHoE(Aj&}?vLk=n&4{@QaR>40Nj5Uz?ZTiHbBYY|E1s)qE;4KE?La()za4U3?R(gogaM ziG|_nIZ#ZI7dnm)bwZMmw)EJc&MWHn@8w}bBGCq?aTXq3E&!V!$_2LS2caQz_sm{~ z5+FPZJwg5^P+`zbGj2;nK_>`(Ng)+0#bzs7*X(2Ze(RmSj;%xcnE|B0(%7D(QyncQ&F<4 zMU$)bM5M-@V;KL?WSVwx@Q0y4yp-P*W(sOdjU11X!Y9aQXA>&_?h?kw2O;n!nTJOz zQ3KnRDy(MQHPzzY3FWvGsn7WsQ6-|%@pDmHdx%n_?7j;OENes;dL!i?n&^U|7l%*G zDwLk;*YT<#-)#{)-FfS>1I~~1IASleg2(0^5K;vc6lhE~FKf7T_v!vttuD(^_c+@y zuExay4;kjrrvkxA161QcjQ`+fBYhCc7Pe)8?!QoWS@Sql`4B=uG&4lp8A-O)0UxnB zc*_6Y)pM=U!RpE0w0xm}s;Jw(6YkmEu|RR#(<3dRlJRXCA^bzTDv=JXj#TzR{qF#U z!^eYPya0brK-1^Ya!aHP8-^lsCjH<28I%o9;C30L`O4pml1x=hT*A^h#G|@h#_zKD z;x>v0ULihuD(9lsYH*_Nj%cQYv;dUg(X4pz17{;#GX)Lcal%qXZ?8r$3gPVm86aw> zm-N<`HHh_pNE4PccPh*sE&<2$(uC61_0L0PZtbs0B@k zGhS|E^*b}3i`O7<2QrB0ZuP;#7lK=EB#+t!YMS9}6NAGmuzixX5FN7j2-5aNJ$?R8DaM4@~jgce~ zyD~m+UN7Oll@1T=_1^Xm$x#=_Bjn1Upky@UIiBCpuNg58a^LDhLQF4s|8(53;AE4e-QQbzAg6lx*et2LDKwi$f#=$0int#VAY?kn{X0)? zbLN8Wt;+=HqP7N?#uQeG*lWlTOotHHT8Aeg9E9`pWj6rV6*t8D}2 zNN{NW$&=5P5o(tPN@~o{fKXiuLz6>R*RSO_bV)mWWj;>VFtqdV{iM;PiW^}2svY>* z^}SmVu9JqMUF7R)i9uzz*h9j}z2Ar!gKVL`P>kZ+ryHjiSJLGW8MXo) zAO!Li3pN%oZ-nYyG_M}U;5dqHb&nxLnA9kv5dPat&pR-UaPEbqHa?|b&F5P51 zIhVR|4!^ouXvzT(=B1;d#vb`BeLRON#TdY!G|lU8R8SBZAxX+bde^e|$@a4c|avWh5hlKuZ6?ViELo}4WyP!Jd^tEohLg7j*e}8x>Mj-SZfgR`I>qLQ5eq%JER4?8`24&K?X zPcw>Z>9dio0kady9DFk$BkFJKw3Stbw@uz_+eFgyh{S%+DQYxb?|V+Lq#H{xFKkGs zok{#IwjInMq^}oSG~Lk2!&PE?BJ8d*PNlHoKn%Ot$I>&Z+)bQjIXX&Bjj8_g5%c18 z;UGJf6`EQ|-uatv^V(~Fs5r!tXXwKK1y%p?7uA3Kl?~YEWxH%)#g_92G8x?hK+H_D zOG<*8CEf}tV5Ux z%(c*Cl$`ekl7_PbyNTBs+8#u>hq-z+gNRrj$%k`nBJbbql@PBDqJw!n$#tc8LYkpv z@bPc14QBQSm;o%vQECPpOr+vF?w+U7taUv<<^7UdxK(111G~PS>#G=2G0OcLy2B=( zM*Y8_k@FezIB?);D70nk)w@~|ko=}vSD)PA@k=e#%L6oYC0bx|Crmzc-((b*KkeH^ zA#dM76BeXwn!^ixANxBWrYbsS?*eQCT07c!%2YBa7E}$t$_ zW{f4SBZj^G{0l?{mV$Q6PUy&7GE@8Mi0~aSue^Oy{?Zox;a6e`)%)AO26k(MLdb4Av}o5$+sD=>`p%&W+ZEO301$yWPFGQ~^U==HFe|@Jjvnh(Ug02a z9fMX_+LV_Wil5o0HPy@f!xYbA{ud8{<`$j)elUy9cIo~)wx@6$>O%8!+6Epu+yUgw z3V-veibnP6_T%WvGEfHJPvH9xQK8ha$IlmD%T_Y zd&S~txU`L92-S4oyc!j&6g0Gl*=$3V77FV=%wTZC;0kD!l#R^=goo9qYBY@vfP*v| z&HcDaDc@O52~W4!IobGAK6BA>@uN_V)Zlb-lC&! z3w6Rx<|NR(-q94Ea4aHVhHg7U@%Keenu6C|uvGja(+jp|M;4>swIXrtZUX=p4x3O1 zhxryaBiH&MO>=xBJFP_(L_CCb={sXs7>_CzK>M57e)?Z9z!7#dt>pMX5dcpO40#b1 zsb%pI_+_!3=U6FCW#8!-Ewl=DtS(OQr!@Lte?CS^b$~d`_^o4qE4ua>Rr%S0Ly4;V zNKqK-T)XYWi^yf$NA^@iHSSWHSICFs5V^QpsoBk=m~}zt?(#U}%;0cQ;UPee=DN#W zgbBf2kAGKk?Hp46RApQc5zo8czLwxce~?mPj`1pTk^e*s(`ebG`NBqn(ap6`7+W2y zgM`H@fV-1B8-VRmVUEo!tZeSv_E@k#ZkgPf`}+eIJg@*jKp*jPnS%_|*b659Q$eEq zTa#=EA_MGe0A)n_7tkhRsrQ|sG(!I`=&6(nGqpNC20=@1u3cNbhdm-?Tq7u${MhRge+2GN?!QwWo1ZYt^f7w653;f%-*w zG?=XcP_PP6bJ-zKMS6NT78hn3-@>+NDu+QhrP*pVuvq-Q|Km}9EuO~n|6nfU zlRJM@cUf{e$IL=i>i8!L z1>q+g6QsJstYH;`cawP*0FyH&-OMNEGSMAXmmA^X&Ih}L=P4p$=bXTstdi^Nqu(n_ z+oncx-Ai;(=VUUKNT~7QgTrX%LfC8#?_cY$Ipq zYoU#i`U>;$(QtwPaS^?yyct3x0W|;dA!anL`$?o;6J0B3y5YmJ)0$9F&Q*_Kjg+Fw z@`L$83}?Cib|O%nHVZ8tX{lZ&NzT$co~hrLgg*f0A%3->B;X5CBOv*uXM02#qkcV2Ec4|4WmIU_grB z3rk)W(MJa4ju)v&aDl(U9>G+hJYl1Y_hX~&c5&sjKO=4F zDJHBz6BhVUU(p)36;^ripcb_|DN@t(RQ_vt*5KB}o6Pa#ZQuivYyvUmAsOQ*!gZ(5 z9%zF8gZx`9c)BWuOt?k7_<~?a zGea63390fHOg^q3O!T1#fHv6KjA;S#zQb0h z_={%`wmR{6*}r*_VJiX0p)pjjCZvX^Zm+ltLDtx09MO0TrA7w#P53gjEZy00qekz= zDp#Ctji~u&3%#(bx5wlMpX38A(KiGdtcS#?PES#qD|||1*#}9;T(EgA1Z@f>&)Zctj&Uc~|WdSwPOVL3}TUL5T3N2~lJVF1l9_0+sEf52mkFtx5OTgrzu zX{68nnlAJe)$N`4!*Rp6pIYv;nQmY#;iG4k)vPk825KT@oBMq3`BYvMF%0d-Ck@QR zXG5gFw#NlxDf=M?jzm=}_SDF%(dVp>?}#fMN2 zQw!k!naI04w)axtU?B1ARZuqTCUQ?$3X8TCH}Zj=CjEej>8uye{E^U&<0x+w-J&h? zE4}6<;k5ki`iUB}omWx&cs)yB73E@NCXw@#>R%yafBt%TkiQC^Q{^B($ z&ObJLqWEaZniKFGV49^yk@XC3JL#aA67;v{`wSC09K|9BuoJ_wWyufgyrSS zo7e(e%w;#0e>ySnzpYaIbqzoU9sd)0Ca^o66{7FX5gE zjnZm~iv$ZUD|7Yp7;!3D9w6@rlQ4*q3cNgRC#>UW;#qS465ETaUwrF}wCFuFCRnvJ zSm5Lr(L5<{Dm-(9>X-4q+iu3?8h?vfrvEc(%pr!Tci0w~*j9FcP#{$Lt}EZS_Krd`?E#{Nn^LGRkV|=2AE3UmHC(v_Ap~MdI}EX_=ctO z4rwYk0GBXTKzUh<$PmxS#+JAo@^ReY%l*Aty)f?sWlo(*ijL*!$ybEV$a!$=j7lUi zZ6SgR4FViD6;8wT>E8iLgJFc>w_GkDYFt2*ZZ^#a0)-#;Fs6Ym$wNU_cv%n*6ebGN z?Z&I@rgH0jbYuBkCTTzl^(8qXNsoO6r`tj1@L2f_QN}?IV(KrBKxIj2D|7F;x&~Hn zs`i2|v9bxv%~ar_tUt(k?^|-DC)_^UtL!R}pAOHEP{enVVMY}mnG~l9bKWAA!jhv+ z?UA8iH{U4!{Zjn^zYGyOk*+njvzv!Qht$E`pC>duMfRH~eeYcmSl$5wr4o%9b=}GD z>$Lko!YX2X*ZKTxf42HYDF;gx!*H2{* zx@?NXq3cigdmlAgI3M&KjuGB|`ZG$np}MofMs~yqcrYbPKbEXRRTF{s?k3*ie#<8m2e$c$MKrJH z&XvxkfBm;NsQpK7unVnI8#Ju`95sLo6*d-{#EUYs-)>w13!h)hG*fGFv}Q`@&05n5 zt`9g{WsP$n z*jw#e6XA9-`5Y`Kh5x~l_5J1NPs4_bp@ZQ3$+^T z?@B?asbF2F_)d9q_5~3+FWx06n3=7w>mx>KQ7|-V&Om3YKww3vjj|{)O9WjDzPhfp zsltI-!mGFV)wNR!Ubg#;wuQBA`E}FgQR^!=tA_LdFIcsX@kIHNoc)dag3~@)TmwDT zYO#Q?n3(M~Tr@FNnAfUGvLR$s_G{{hsQ1s;NfID#aC@1QwD#m!*!)pmK!6bVoBazCH~`x)TA@62jdI$Ghv){w)p%$J>#F zUi+5t&caVu1Q}K}XUfG?C>y}?O2)x|Cp9Kxp@SH=SnBKhMpP+UCxnRxM=B-tmlG|F zzjXiQFXRx|TcBW}8*H_E_@RR`fL(|LCuZ0`-s0JbMSSA*pGNWAO3$+D zMxaSK7|lIa1gZ*K#YIdPX#a^$seJYG{nF=EuOWYtT;lCsEzasAbK{;5Cyz5+b-&97 zL;xVI8EsU6_rSoAm3J)6Ac%^&fPk7{9)?FI(+UTBvl`9vmzlM)%fYsqB|E z)0eerRjXyhPd(85iZU;DX!=@XfW1q*mX=G+UzeYGhF|mjKK_&M9}PF6I}{!R+P;JK>3>8$yt9|0?lR_ID!JMIst32F^3t!5C2gkEJ5K^k`}%XjtBNV z0Q2Dlvg9cB>Q}Oo-0L{}+!!I*b#*nhpbIxU{mjhfKs-T|5876ZcNH9M$Kae zaY81w<$v!(eAzkLo~B%axePg#@k%B27KfH@JPY(|a6&D20icheM6-mFr6vabA3o%L z1)TQ&;>U40v`xM$<9X1?ns2LUdjqyw_e)&wP)0+m-4WiK&)6pM9<>@C&)xO%U-oTz zzJiEk-xxhd>$UqRdr=IQce1xXI-%{NA$=i&JTC#B|N1no7#viHJU;+VG7lP0PXCkh zKL~^E%(cJhBUR`CDNtAo@b3~&&w9gn3LhO6R}?KkiH6mqhlau+He%ClzEI@YP)1#r zrBvgZU(N7m{8VAzY3An+uERK#QWWwZYqYNpB(S@`nx0e`$gpO1&s`4ym!YjlK18m7 z_v;(MiRqzfzb5jOvQ{c&PgCN~6FJm@EFJNY?N0$jK5+CK?k zSbAv4@;R#D&JQm(;kDnhSZY?Ic=ml^{LtBeU6hiV?4SvJZ=$pn1N1%Kk`jgSc|tPN zD-SppsblsQ`pzbT3knGeISpaH5-W!cEyMD@EBV`7+WH~4^jDul{iESK{9_ne;P~SN{)Pmhb_*wuGVUmzOB|xtaG1&t%C(FwWvbVsB$xd zKq1mYYs~RAnVHP;`Gd$%n@9uOcA*xeXR_j8M>4VBZo}`H=HhDS;I?y$bXh|%W%PmJZ436Nwk^@|vAf7g$iN^7dhQH0-z8yEg^+3NbO*8Rzmq;4qH zJ-L9%%!6`jI4>=50_X3B zOviE_jTW-FOyn=c`3YCp=L($p#|a8OvMVaCMy zKcafcJo6$${=bVX-FXwrc4eO=$x|ii`&Sb(TK+ zlX*SHYSGQiwVqZqq;0si&IYde7bsD?v)tY*W^$!R8gECq?)nRml@}vZQ5De)CAS+) zFeTVe?O%a|!+f(st*MDm_L^dhxXnZ_SV7JXR>Q@@&5W-_nQ~MoU21_`XBim?k1LSE zuS2iyDaFG2=JlO)Dg!qioaVT67*?p}cFwCyl^6yV1WSNzu=|NQ%w>+p&v6WkCX#*8 zi*NqfF>!Ot#e;{|J9JFc(eR}F2_Qo*F6jmB4Kw(ftWci+sp z{-G#V>h6w(!_#yY6)9E#c*md$4wfeIuU`JcKUgG5Z8%98{69f6m>0cp!lC|WACdK= z-a=zbK5!Up_(kO_1}pSg-Sx>wSh1+_eihow2u)<4aG&cjRB*BP53RCG1-@SM8?3)b z?eVbU0%?$ut#zO66n;{coYoh1*?;28EBHA5M*?_foMoNbakWu(OX-&D^ z`uBda#FK@^96Ne(c{XgC)i5i1|6AgB?kSRQGYy~Np$O*msmQCSdzaQ9vLm$pow*iB zkbmJ(X{s@c*9~-c;n%@Ke&8;>9>+9l>4b>}yBTo#UnFJ3;_m*d?c*q{j=T2s?-Q~1 z_u%L(y=Zb6KbC(hqKOyX%%x(|W=_|2{0PM1c-{K&c)IIHEI+!uKS;{~iOv`MnAuIqe#gs&v3K*9JzlvV zn21I%@`t##L~D3auHSj$fw5gX`&8k&@@Cnq6v>zD!=W`%UldkwoyGyV-3=|^L z=;fXbz?&Vf?5JK+}W*-UDtnz*aHkE*z0fK*1c4?A^jmN)`xYLvtcm(IHK zm#mH7p!a+oc`)#OUe$(}c5CbeFl*+-M~e|v!7e@kH0tfgn6AfB&n}pH?MN_iXa8pP zuKXW+i1gIyFk^JHhr_iMvmqQOiw({zJnkN+deeD0bm}9g8m35QE2l#+ig-5oC5z6E zS+jM$zkA%Rq!|6uvps%7f(yXY%9vKQ*&!NjmonB+5JA!8iSEUPg5a_;y^Hu>JRhC#B|h3 zvAj%~CW?h=oh&Xkt{DvzhG`EH7w{K`u`pEFAG!vce6YRB|34svf$z8u7giTY74|(W z0P;}Zk+n?wOi-&Riok?Op8iwq4_%$yL&}mzyM*>HuFuKLhLU+Fg{dX$KAvtN^p+XY z*P+^cTt*$&C8q^N@fh=Yoc1Q3&t{j-=h0qyNgP&&vzMu~UThVHXmfcAcoqfD+H85+ zUb)#n{l@tAlCnJsGZ4oVGlwy}`r!_L`gbTHM{->pTl<%oeDCH695sC>xc|f(S>x=p zgX@dkyRZNNh5ZNEFd$3Mo;S%dZ-}q$AQt#$FtApbuvY7zIU}vVc z;)FC~jIe0qHsJK@C84!?>#v6oBK;y~=W>=I1B~&Nh)eA#0C-I$rL{Z)G5C`4`Xx(_ z4$kV5IfcOE-#;B$KIK}$O`hhKuq zp_8e*A{6F-CO1d>Xj+iEY7U_h13w4fq4)jZpS*Gc@8Aas+ja>NxE1#&b>>FX#c0KkE4P*lUV_j7J#KlG%q71elmeS7C35Bp>?2x4`i zWfC^Xd1SB)MUHU>?2ssGK#Dd@3V_(j`zrgax;I!*^O&Kp7XGFRG(rT<&Sy9{*F*Ta zTi~=G*^BV+C{7T6Y|9^+7xfG-c$NBmP}->vDkx|>=(v-QQ8Xl3n9ejbOe(_&{Yv0z zs>6p4hh*p)rO1CiTOSI&KRuZj9t@tW58NfoU768#o`4v6*CF6}7^no2+jXC%1#Lvf zFKc+-?FZc2Nv=iV^}sPpInX^CF#o7hDN;D*(BEX?CZqdBFZ3OZlU)M#pQWg(R(4$$ z>26zrz4I3)0&D^PPu0H{UuivkXpldzUj!Ab3HTgiYBOTQ%gUDo>@rAB%THwbMTSpQ z)?sP2|7Emfs(NWrcDwClgg=B*&c0zX&I`A%QeL%E(2BvvZ^LOcP5!JU3xm0Rp|Vf3 zU0)*oYZyywjU5^z)=*R{qXit<%ywJJ5*k&yumdIu5oL%_VojO`JTb`q(952AHS!8f z`qop;0eK_9vJTXMb?8wuzhzYNe3_l@nm2jWXqhT?^?*<=#~mpgJ=yK_nV5ED^TwS1fe3IsGxnOyY zMIX^eJa$)eX$h@Q0_s+y?s~8SS*=f+Qc|3qEpy*c&mW+|{j1#}xL(43r2hSz#-Uzr z2LQon#)Q|rv_>F*;?o_ilu^aDxiqqER-$@OeJA1_?l|Y}Z=0X4qvWls9N<20&vyP^ zT0s9xYGF`UW52xUcC_&6`V;I;cSrL>q@Rx+ehb(M+Aj$>EX~(=)Y>h#0{heqBgD`*>68v$#R_8D)tjr>G+e*MPw}03+CmbJ&5GlDN}teAxCO>#RA9SD zav^E+&qXCwb@gycTrwSnjl%Fks|H`@j?yhJ4~A*nOc7F8sZUZF)lX)Lc;OK{4(zpX z_Rp+c`vAaV>nHv4mc^UH1wmlTw=Ds5DC`}&KPN^pGUM}yf$K;x1+j>yCHyZ)WPGMA z9)6lz&wPaqMN|(&mpni}tHK2Qtv*-{;Dtw$Ajzq}KelOb_1Anu;=+Qh9eSEx696Y1t1@)`Y##y;Y&f=EZCzSyYq4Bx1P%c{ zMZv8PbZ!b~Qg4g;b;1OPS;O>y3G2nnjqwt}Dg@sJ;)OK#?#-3X)VqLy}3OCVgQ1 zlKeH-BYNkQkY%&KionjQ_lU%lT^n4c?~MPz(T1&q!_prg`c}c0H&L2*y91=(Ibr3I z!u}MGT9)iNaZMURYn+|UmmjYEO=_3mX5Aign*p()Vn3SfHY6;v7dFAWrp^YLzlezR zw539PL%7hShJanue<2c)n(JOMWJEfAyRRy3IT2SwI2( zB4-L$U88jg-zklC1vdSY`js(X_;Vmz(RVuPPB2E(8!X9)!%q8Ji(D+zK1-0r#zvkd zZ$6p$A89C|BVk`4HWeRjfe#+zyFRaddGx+Zu?x0 zahv{4H4O6wUxhB1RaUQo(Xl%Wx20B|f2n7{BSXUKaAKPeB_UnGZ^ody#I9B&$AkBx z{0ADpF=We8CW)zI0#hg&;PIH$m6n!f3gSJJ6^2P@tf7re0D=WC!&Rw3sL|Ns$J{R} z#Aa~@>h&d2(hP~YLcpbCxaKRr1J4~;-(faC??32*r2>x zGTrX!HfsrYCF4C2@sSZICAT!>vZm&h4AKv_UcYiG-y0WY}tX0?^ z?bEY?W}K&-_!Zj)k@)z#Di#UgFSdU=lztlmGq?EgpBTgGJ-Edjp|-6<*E-QC@d zG)Q-McO#8-cS(tKcb6a_(p}QhbvGW*``&wxANauL_qb-yvu4ej_z#$4mk;5*p7*4T zcdWz71@EYpy4g8uM8;Ni*IVHevMu#(#EE2~!T7h;cX!9X6(ZVuwz_!X?QGSPmg z)ZEqijZk5EL$J@*N`Lx0s>?~n*)IV`I7pbjI|y!Wb#*k*0m-j;$0Y=u;9Q8p$qdc< zT|!VOPKTa5Seg`82rA2^YSioWi-SYwbJ_Ld4_4PZozUD49k-u7$o59ldwX{+amaeW zEKNmzz5AAPZf<0f-X13)iQ?1)Xh2#dtK@&2#Q>^jG-o}&FbQso{83`27QM1V{epy# z@bI@0<)D%jNUQI7C&8r48a5snXzWj}ZjD<^6M#R-kgP!w6-f<>3IskF;>DyZl$LX( zDiXQ?nF~L=_+1fdFwF0F^|Xetwmhv~?J{H(>M?Ms3!hieP;``6vx4Ww%j)0tY4hp@!uC6O>o{ z*Q+IJD2n;&r3_ZmFAQl6OPk-M%FT1WFY(;O-g35ri?n!a(t*F;>)R&qR!R)h6Nssg zSpUYbjUlHqP>tqIKJWE7>nX?65vVnwrAC-Etu#nW_P45|s!CxDXl|-xxcYr3FCVC6 z;qjaA+lD0N>ds8)kX+6W!(eWT+fV=t($x$@nQM`zqi%U@f{$D%leF+B{$)3^evI9s z%u_oKOEMB%S!bhEdwXf}^j3HSST`bAULz2sgAN2TXd7>_EqXabI7tWK2?*l(?i8lY zCDCI~q+ZNG=XW2vVn{(rgAQ40wCBiTtfuKh*P2NTbH^j+Oi|ZWOg#~`w))=VE8jfs z{ldXurVi?2-Kv24#w$Fpif|pj{pl(dh6@V3rQE*A307)ZQ$$&TyYHMByuiWLx;G9E zMP}rBvzrG8uD+i<%I?0872oXf?JZ{u$J~9ShUEk#16N^0&B&_dvPs-~<0jY?mc7sTW5D}x`eCKghkyus7yhVumCYWh^CfS-UHWrg<9=}+aTF&V7D@m#mi8~1>(6!Nol^z+3@IMdWp3PaI+gN=VYbY{y~Zbnq%Ma`=&Sx+z-*- zr@*(wpOi?}8`SM@U;h7T`2q|JhRr&@^-REaOco4mga=n%S>6pp+Kb=23w@7VT1N1L z--gZjuhKrD?$-WtOZ(9&i2LY)MnB^l+416%?b%aIaSJ#&mXH~O89BpmZ>~ZkA zocVTxAYQ@wMvJNv+#80o8}ZxzOa4`YYR~POPQ&@dA)Um6v_rJ=f{*V%-+UVhpb_lG zXW9A0&?JzJL1JJ$zj|4CbJ z3Myy=&;sCP0_eH7HjOtg6X=tg5Hp08>;ayDy$Ey*1xEFJm3|kesuxAB%N}|c5V^&r zvnyhYRINlxPEs4B3UB&8X1~HOR<={2gU_l(pAwVZ+L((rs|p*;$XI5hWqjMX$anzi(U`+C2P zo5VI0;q+F6DkW*GqiKVLV+hyHniJ`4SJlwEVm)%=>OZ5Gg}m0jMpvXZ9h-k58-A~N zwaWBZmq>srtH{()T zeSTIYzm;^7xr5P`+2oZWBAncpOPS!l+~tTIOL%7S3pe|)`_Cv8&?6u;sPCD{@k@RC8S14@zlNT5|jCdl{e7M!md1i`FK2w16~B=U)g9)ng;Tw&5-(s^KrxdVM$LjnP5^hL7Xhrbtiz>eOLw*3+zX%0{Z=ZmeOC{P%|Ac11uaEh?tP_8av;J<@-sY9r-EmPV^fVJ7) zo&dw?N$D5kN@EO?UU}aBrL-Wpo4w}htG}1Gka?sf1Hzf?@OpoZR^UOBD>LWtO9w2X z+2Rovxd7?6feB7q;Y{^7%8$_x$Qi7U0@TtDRT5@tIW6RwDUkV0+8}0o957?Pp%SWw z(eTu`dLNyh&=cyzX{2_oF*a2^8$3@ln?FZQb%|ru_Qku-LFJ?^pw|VyGcABCE!A=z z0W22ihX9y)Z$<`Ah6}`L1l(pBC(LLf|C7lgn!KI{k6FSilT6|;_Q&3Xv}rpilqaW9 z){)M@5iyF;4o~Tq_AD*>Eo*QX-L&vtO8XcX@#%{+-50?$z&*$~ve%T$-0fdmWm(VC zy#;s3TR?k`@F3~>X>R`I3DoJWKAyTz|L45t5{)w*MbXV7tmV$>3oyStpL$oyQg^m8 z5d0yK0m0!5|M_q?ZE*esZ8KsBB2xoX1Nchi-o4ZI@CXbWraxDBvX!L!R&}fIjymnASsaJrnQ;gJ!96!s3g9-0@$h0&W$aZR?9{ zntHzX3o_33S87zkud6-BBhM;x@fFgzD2MEiuhkLWp%s+dC%?2PK2_$IQ%h|{D<{P_ z6cl#*Ww0$(eDP#--q8;E3_k_xdwtJv`vMy%9KXa%lJ^DQt}<}wq0L*MauOVwcR2h!l=OxIYWzQn;1iA^!3~0`pzrYtI}hj1N7vwF5az!{c>}{ za(!GR0dh6`AWbH7a>pfq6P{>A12#{w)HeYJ`1qir6+ROi0#-Dz9b)0I2-_((-D z(bt)69;z5mfp0by!e$Nd)}w(qb;IxdlM1>L^NqZnV*3C1ulnIsNvM#Q!7^@eI-xC>8qXs-RFFZce`Y2^$xRPOsr$W(y}9& zEy+dY63ENbVOeN7GEvoWYQ$M=#FH7-EtaTrIp-LU}Jai@UaYBES__M0{g;ae z*v|h0ej-~fgqm;57nZpHLk_yS*Iybq`vCL)QVTtd-QB8}{$o)A+fLl6zwjIlFf5DV zhb3(}c&Tq7@OeZh<_Lc(QFf9wM0g9)t|48g$Q(i~EXB`6Dc{YAALIVej{Ahh22)^J zycs|yM5SVItHh}c`7EjO`b}#8Y#3yL} z>8U~~j{5|%*#XD%ltnnMRC~1rVN`lA`i4$8}cP=jXEsi`q z*}cP&&iFkb5b$Grz!8M)P2)gi2Uy~qa_~L2SdKU3C_1G+ zEzXSxe{ryl6{ZS95iJJekcAo~n1;2P#-xBRg{j&pW=4ClfMxy^h~k^HxJK-g_L_8m zm+e~gEFZxKuYziXb4SUfR=i(M#o}tVmC@!RPGW7h!o*tdi@H~VR5@_9W!6B4{qz@G z4)OeTN9FVCg|uPg$gUf<-K>-8o!uunX&3G$lZw@M)d=Zc%;QXZ`PwF*HEbC*V~NC0 z|J2<{fkG<{Hb0f>4_d?Y&+X7Y(TSFbl4UsGP(Tf7!MOxAd=E{_^Zo3P9NKwK9bOe( zO)VhLAouvbE=s5gf~&orE&Yh+ej2=xu&^%{&hzR^^_d%CnyB~sM1 z;ZOr=)d@5>ObA-&==x1_tn zQmE0Ee#32!yMM~wBa%x|DI(W|!FwLOcn1(0{>Z?8-FNdJZpQ0VQQXW@EU_=Cu=#m> z-bO6Z1rw0GD9XXSMA)#MSQS0IaOPB5<&4+CXBoyR;?@( zK_FJ5>L(@I5|`_@=l#iFL5yh6J=p84^F74M(KaCGml)q(jjWE32W@laFe9z6}} zD2}Iuo2UP(tp7stfFs~-cDNQB;wLJgCtVWtO%$Qw9-}a3onFdh^!;B+n#`O&EceuP zzl<_6cC)j*z8h`hXu{(mBV|wRo_Lf#EK-3T3>_}CTC8ln;5PVZjG8IAb*_?m@YhTC zn6$y&>eXVYFct?SH(6jXZLwi_>z`S2^X|m5CTc|WeT5m54!LTV5b9{vH@$;aVbD{|A@% z|F@bQ!4uf4H>hpEG$?$cS(FS|zLMPjumZUEUC6$kjyo(4^TV*8!555nv~`Zv__?GH zPVu#;FpP+<)V#XLfpYqq1UH%Nx3@#!u)Ns3LhqIq`;mM^w{?A}_`c9ASdMc9RqL6@ zB@A<$Hz|-)lpJ20bh3ZbArgk*-DRm>LwX$NF;}JPs>xPTQA6JIA^yOt^^Wxn>x@RO zv5jk>s@kXB@r-)oA&LdzNmh%zz5zzp(U^IaETNX?&gDP*8&j@}zj_gIEPD2Tx-y9F z3qQ^%nuV`>?QKLdnTYBX7y9yC$i6(siVsJ%#n|h9TK-WTtz` z3=LsrQ{nMyAA(Q5Pt9u;Lsw<^DGP?GDe&vJthp=r zU{I%UzP+zn?Gn6Dmo(B7W;dZMILuT@e%iQoXz{d0$ZzV)wH^6In{JHi<$!eds z9Gi1jDj*y-eeoq>H4QWR?a=Y%*YjcYRUGcZO&FO&xSEx0W#g@J(0qC}+>RgV-A<;e zYaoNNN6z^v-^MB7*;^=3o);apPl7G*Uzo^rgZhA0gZbE&hHn)%Hyn=N@gNDkhW01& zGL>DPf#hMdwI-3*pX5TUba51CQ$2=Q_b<6y9~^iOBkAmJZiWVW$A8;o=8{-R`W7>n z$0crtc{SLxllvJ01OQaq6W<`cp2wQ0 zD2zsQ5Zj5&*!um^^}_0NRgyPiy~MZ-)$PnN+fc?xg{PUSisCJ7)uuke3}60r&vj>i z+h4rxn@0XQpdb#$-yF)YBj;}#2^hgbQSW$X>g@P+=j-!c*)kgctZjuGLT@CFzAkT) zUy&t#g2d(@)Fg_WsI9!)SIv0m?g#3p{$ipGkK=jfHuJj$eay|>oQt1recK(lvsFub zn7?yz>jdh(E7L!ao2KKkbc;gx9mR_V6v=aW_tXM_KpdT6 zuqT`#GS2tU0l#DiY_JEP+gD`mb-`n}O<-=3udLA9YB_hL`Ogh*LPAW5Zcuoy?#2ttF)f?f zv`nwp-qxun`Oh{)-;RCEb!OJd;Ch_*D#1HJW%EY}e&!^t4m}@-jr>6xt&?Yc7CC zY{&58tU7ix^T+CyW=!6z`$B$(3S^alQ{SByb%aWl`c36Rm(--i)3RdZ9R&gooK!k+ z#3LjGT_IoZ#D*3Q`1H-9zFo18A%s&jwTk zH-7H~AsFCmnhbEa26D#%%Lk%_Z)m-LQ9?w(7ibhglqo<3xSem(@xIOlk>9-r?NEL8 z-$+yV4S6pnGu`W4Z3fCLmhW3mDKZ<7k2#L^;?$oQsA<^Z=`)2Uu>qw=Y@Fd?%}<}a zdnNvyc14YF#}KbJ`_NFKqPQLazjM$moq<8Z4{JGy@e@v(xQoEF{rqgBFa@RtI_8HV z9oLsB{;MYxV@)Wf^Yfg6-GlRSNQEi&P_ zQ9sVuxWpgl4A$b`8Ahn+D$AYrsWd@7fCRv-hreh&z<2^_+nG?7mN&Uz4EBv>n$ll* zP4mTS4!#tiCbNnlL$06CdeodI4=O!F^{C;_&q-r-#eo;?s{Hc&Dec{~lY8)WVvl^P zwwvTL=|LOaD6SQlMJUd6)VNcqQLSQ+9wyYvvEvsase&yDEpY-$VH*+88tF|&#^Y@U zy^}~*py5@pi}yyW?1MdN-B7Y3tzzA=X10S0&7|ppY$#N)h_`;=r!aLe!PhpB_q2A; z+fG51WQC0^;CNdy=oMcMfv<6!C^Xp*ObG5ul&MAz#=68$e)N*kYJ)IX8f?cu?MEv^ zXAKYLw&}Xela3pmhr%s<#NF3MkIv_%#5EWiliK$s9;5wH@yZsT2(ZulYgku)i=}!r zpJR@2sKKqTs^Yp)pi<@&O0JjrUxV1jvs_2NGih6}dE43)IAi-oU@vyd6pfJ25R&JG+0`gFAeY4rU6 zl;!09CFuS2Jb|kKZX&c8TOc4F#QpSeElDSgPL0aPBN@p)^wAH0cz<}~H8FHZ1`vtf z*&jyr7F?`LQ6?V$XjD{$5#wF^^(%?2D;4ii9D!lg%Ouid)P7xmJ!+bZXM)?R>C;s4 zoB~%XT2E2XRXC=Kz!wW@W-PD^?OsMKjL5E%CXe;=qMx!!x7I&fMTOBIMNm>~GPM1# z{UzX@eNx?j)S(C6_Dru|3d!q4&EyEuP(>Q_QVSh9{dpOfZ1D|^Ay6tLz zTRlKI{$GF|(<_6XF?ZxwJtWcsUGU&aOe(2#7LRA|d#Nj31RLnKNx~e4+5r!x9R1c? zx0yG6%d6qy%R(~0eInSaiqvh2(Dk^AsW)Pu8oVqSs}QfXK0Wqf2VWf2>v#Pw>dnen$+n$B=srmvB}%~tyfd`Msm63*elyGZJF6&+nsrNJC;P_D zB;++<{hFeukpo-&okGDDMBPGe-2+B56F@1dj;a{5tZh-j4p$ZD#b@m-FJT znHFK7>6hGcOxt18xw`n6!f+ZSyg8OuH^+4AG^euB#m{i=3APrT-Exvxi80?6^sobW z%)_#QfbNua2VApqDw$FrF5fj&nooQ7JmqtNMpEy>XrYa%B+Vcnj6D)tqg#G#DqRqz z|MF-13OARSd`7BIvw$@KRoeIoH59@p3An8S;Y?;z65$Z6rD(QI)z<8XsmqNQPv*Q<&yUSS0NJ9qzjI*#$rtB+(W&R;Fk zdTSfLDaP)vE1Z}2hhEuvtc5k@Ta_}d?BVDJ2Zy+t=S|=V!sOiUYMHZFxIz7hFOJ^r4So9UH7wVPP z1g%w73R`^L-c6gg%49|8RjS2f^%l?*U=07F^MK6<4w!m#+Vn^&Q2N`L0U*nauhn7& zz3mujhPsnt*`ks2D8F>04V`rtPk!PMkz~=y{%BMM|6?wlQwjB(!V&uW$B6!n*Tnng z{LEaT48iAZaa7!BNrgiEG*COQVvl?0SnS#~eGe3^_Oo~Bi0pG?HW`zkK-Aa=@7$k* z8&_-Uc&$^)zvsluiv+JciXL&T%Y*$p)_)_)mZ#r#Qblw+_y&K?lGkDQQ%E%Ok^CnJ~Z zB`YD5$V1`R5v}38$7Q*ux7^R$)@rd+cvKG6$dtB{z%=5uc&O4|u{OcYyuKCu?1w%S4F@Pr148XL!2Hf2MU(vo@yvQ} zdN^$MRKg1I)Ck0o4wA0gp(bKIg3LoA+aB=P8q4E*TS+ogg+yqK(raD6&W1@v^OiW; z(zFs=m$n#(;rE2O_})kGfmkwqube4JRk8iLA$-Sd!(d{J-xrHl!%*XW{sgb_cZPgX zi9s!ZM8rro&#DysMUc?ff$ITcE-Shf0`aBcwhjZwn2 zXSqQWtvt5Xl}j^?y_xH@_~8jj=V(Q4Nk+jdhwvhQ4(h`%$JVJQ`GbzBzAqk{a}&z> zvIo^PhNk(T0!^KYXBPoUS;x9gzw=GPH32I_o2-QA4{CKfdPn6%;@%L+&guyug| zkCp4Sw!rJ0wfmLVr;tG4od8DjA4l(RvpXBZObQJJ_}@6+Ym5DFB!n%jomq&!q{_>8 zZtvLJV6QeJk+{KG4fA-2@O9ali5U2Mp@;5~GaqUkMeEn694!UO#&F*&QaAG8v%oEK!kee4R;lpmnn}hxEu_TkV3g^Yf9)$B0<%Ge zyyo^q7K>Qv$u2d=0WM7%T6s#l>zQd}f0>$Mj&6#!iaqvsI}v}8p--*K7=}1u1rmC9 z%a&@m-{Xl$E?u1Noi6u4M5rK$$Q_6@rZhp{F5eaI^UcfcODCtj{mc8V0OGyfE8hrY z10csv$nVMRxaWk7?;ile7S%hwx3jZ{?*-y4OP3Fx(a#`XIVYl{_swzHwcJ1$yaFh>!DyMyc7Qta469=mEB8{D#*b(|PRb z{vpfUsTfzcXyvfxhdqhV_+ARCbMj$}0k_t7os&ez+htWJ3O7kH1kK%ZA~xUg8PVBk z?0zBt35w!7El$7?k>4qbyC$gmXEy)x^!|Ed)flO?sGox00Y!~(0t7Vfi^M%1dTUa4 zgr007xKz4Z)qov`1}hL{IBoFp?x)cm5k@tgH9wyWw$mHh5yl_zmU(T%XKbqCTePs; zl43$V95oFOFz`)xcC+h~_-ig}GJb5o#YW1mn3tB}bgOT^RV(NoC*2!l_y|S+1I+0M zh_RhRV!6<8kfja|NuoNdVgHPN0`8l}f-ll6`3$E@Oa*`1OhdD`Wj60NcWR9BmsVwZ z3PYmp(@5+1=e4>wL!%;+`M*p$s#m5qsHu2bJ1OBU(o>5>goCFF$=+jz|k3o58i9uM|W^Z-4sM#{K2i{&aLi!c(BY_ZK1E4uXl3sj6B~Gk$O-?Eou01b%g2 zRe5DTJaq$W;p0J5e$y9gM7sv?Pt@?=icp&)P@V3a?(qFZ%|9h*xo`(M;foT?1e|*G zDT$Da>o5au4-gY(4`)S+)!){1H1_vpv?nwhXT3&0Sl`jI9<<DvpH@Pg$CK#b%7i=oMOWBye6>A4#c7U z2eO_?dAuoVeQ6a#G zt5%D`fP*C``4kBJ3E1#rUc+P5Q1Q*N$PPJqM8KZ-*G)>Bn@$4`V)ap&o!RArv8~C4 zyLJ=y46Y?Kp}HQ}>IK_$RNNfNI^v(Zbn(NMXIPGe2d&+vO0i3OCO){R{aF<2E_Lys z2%Q`-u{o(tW;{k>6Gz+?#-KAIE&ep4LVidV{uez9n3y*r-`g1hl!`NRzQ>3$1%nl) z!~|pkpF=D zQ>Us7>pSM}3Mzc(jO9|s5iNYZ5^}Gnr(iw6m|g~qaC#_cc`)wv6%ZH(F>L&OxsZTG zM*s~e2U-eyxGmexEERt<(Qv)YcJiMF8T2#qC4c6Xc)D%A+yykgu*@IbN=I(@s$Utx z+sCPgGMVUZH9s}lh$n238g3#HRRr!p20dr{p1YVA;?`dy2gfH8l}Ig}ob7i)bjE6N zo!h?{jb~gY8DB-@f4RH zZ$vuSo>x3q@>L)T@g>S%66Bj&{yCgLP`1vMgX-E)1{CERg0q1eegg&!_lw~;%{#NMSV;Zs7mYLnb1`Awv| zN_xBp1^vnj_SZI_jW*b;!(nS>(c42@4qJec!=unh|IGq|R@%axp6LT6#W~dusIw^&fS^Q`r0jd?aCs1R5fN|Qp3J$+Xqv(#%gxt23D&n zlKH=gJ*HQYc4*xEXLi(b4E)3*L-|=<2QsQ=s;m;e7Sk7cPoo3dmwdkI*a7PC!&QoN zj@3zL|FgO!Cx`Rw6UCz#CW42rZ+mVgnK%|&3=QmWSuW`L1(HmDIw(@koXVO6b}rJe z;Bz8pU4b!1x>DGSRsw`bG|#tDuZ(5h_g|E^`Abh#isx;!8*WQ=Dh=bNX(>L6cl)$u zy)PpbWMd4QfJ>4 zSEG>}1~6rTUtn)B7@%lcg&O={z}@p%EWw#@VHjJFKS;5pLl)|&2Lo}9Xl6daPd*S$ zIfB~Q<;TbGG;#ac{;{_g(U~!yOVGTJx>7ssHrpg)H0+Nk2l+fc%s8BVsvjnb03m|( z=<2OL*x}k?w21eFLWP|n8UitbM7Y}O5;75RW^y!Y5F6wi+>lr#R}YBf`wZJ{7hbdh zul?)f!*s#PgLBqCQ>}S?;#sA77V+0djyCg&Pe0_P5ss~>&_$hQ=h2ahr=7ww`^~s!ViP%P*iYhbC;=*rjT%i2I@%^IZ9s4Ld%_ipjqK4_-7{1GC z_d9S>!zvYGRZWWLOpG{~konx7&h@N4L{XIQ5 z9<0RngrL{rcilV__CskKf2>-}h)t%VQ7^YYhl6ZQf&FO?vLSfK1@MXU14!I)*PFYd zhD(0=tg28dPiB#c)9SN~2|>c&nx?8)ziRC^*}Vn~Z#3rB_S4MWDB9FoMf(v-U1VuD zKG!8hVZnU>F_occV|Mk>@JDb%M8|-$t!#h@?a@X+Z;lvXAOFkO1B^kkxePV@UxWtC zQ!Gt2)2ANtMEtn3(!_99X2-l2)IfCVDD)>x<>QO{&}py1&fco)EN2x2B~e~_(lHyg zjt_?04s#}{TtZxTusaA@oZhBGt?NNIn_W7!Q;%}+l|`#lffn{^`Yuo!KVhP|L144( zMqoXuOyneuGgj|sBgjRu!E`6;P7t<&`7U*zSa=SK{t)076v1TvQBxbJRiJG?i?DJC z4$L`HLo)VG4(w3O7g4J(#gVc4lm*%V!~upp{x47Oucd5wZ_gn)5`t-uo%9aY{Tea0 zYtu|I&otX5o{BBTXquVd>~>UdR&V8L910grT! zp=@DXxsO+iveC0yR4M*0&+OIA0sCptJ~0OAw0F4tTGHg^M(d7=&%pCba|oKOp?>A2m}q zlKO~kw!ha%silnPbpouJ@08^3DWh~hMC*;!-2*$%LTJ*{T@~jhGC?k5hjvyXNtQX3zn++-LAnT(j0eL z!((5=6$CO-Z&AZAOE9xkNjFHr)RkzS0khP{3d6kxYWjgg46yyIh~Y-08Gj=%LdRV^ z8^WYsd3;?KXf%x@*0Qpt(lMY{h(`A?toJc2{;I*vct3p<%kA23M3J>l4AAw%o7?={ zJxCbooN2O_3~s|%$2wjJ{!oBP5G=Vc=T}pIAYJcB#`FsPp{}Y)-1-IQFyL|T(}LR} z9gnbPWZA=xjy*oQJJqcjWp(BAuM6rh++_v+*w2L^8r#;6HH>%oqnw+mzq>mTYXB*vW+u6`eSOLaJ|d4BczX1_3r?rW%%LH`Ry z|3T~nvwalm5ha9?H>N;;>v2+kO7FV&TI;I7`oCJh-2X!KfZz~DDh>0@n=mL>-$x?7 zpd}k(Tf$i%tSl%SF)gdIKY_1csc5eL=G8~&fL+tHa&qTUnZ9H~b;_F6hGt?Ihzv=0%oi+n$6Zxv_9uSl56wE1e&;jcT;6lKMup2Z%ybGZ1sBB z_td+8QHZYn^zACfC6f8SG(Es#HkQ29Iu$kZ5qSFJ=^>bQEopK74~OQ1FA~3q5vDQK*?w+# z%9ldN)N^mQp?_(gzO+PoVw)Vy99+)bj_%jO~W5#0?6ownAkv<## zVR3G;A8y@tcK~y4GYLl6>g1?CxNP63HG2H4J;D-=B^_kS0_jk!vz2kW`;2Uxv|kx00$tHAelzMP)ZTP(h*?S(fd8;JA$ zBkaB1;okfb4RmrKI{gn!w7m3L*clJ6m zV6Y9BlA@fjdh@KghwC2GTCY!mw|>Je3uh1g3w(K_KwJPCNQbzwA*tpAVu8J|L*ov$ zC<>sl*hRY7pXHP#4Qi8*4-66>MqA{TBF*%PC<;Vey`R#dAUe8Cyk15ldFMC1Ib=@X zUwklnlrYtsTR~gs1M{%Hr(`fH^Yz=s`hxP8eyHEwY<*;0VuUzQ?86*&76<#z2D{fL;oy& zbnF|Kmv_lEiu@Q1lN)p^xR>3Zv3u*xY)=-+6Ospm?0e6{ zb*$Ll( z5CkI#0>XqY$V}-jE?^V*!rT!cEU%8?f3M;9fB4COGcfbR3GJnwx5oR}Ysh+jUKT4E zomvOtA)_YV&#F=vGNgR{r<-~aXHit{trxKlYIqEJ)K8tB_mQ<1Ulq!@q;tNV2j(!?#<2FXJCv6NRkS`Sz| zkgWY*wjI-}J7eN?*NI>o9z^jiPRBujhJ2TB1+P*LpG*Rd76}ye6K9Bj=AEm)pepSt z7&2Qm;2cm(gDD4s08kRjL%iSQRf`NP=QAsA+?>RDTT(}DP*u){%0V>Zh*h?HbLB?` z?$k%ul~V$a9_mwwd>Y*eo#5-}S+dOF){L@Pq!J^Q?7HJ3bODZFH?sxy@0MUux?@f3 z>uk(+#=o)=)*!DnRtb<8Da`cLc(isd+o~zit(eztwKAtkIx-(OpjXoz8g6%q74pXm zwP36rdk~BIY>X&GsO)BGcJjtw>F^d94VrY8B+D60M)xj2m)yXl@fv0`F*s>sP`WTF zD)3%Ff`a)p7Ux8T;;65J3`=$x~K!qcPIT z+}-08as_{(uRVbzU?J=Ihf7PWPz5S#zl;Ynt4F{0!z-PfIJrhRg22cJJVX%RP^?}-Ue3TY+k;h=k;v#Qg@cIH+DV2C3x}Ax4zZq0G!D;n zBFz&2JgPE~wL0~^E6a%`RNgj{-@@3v@LcQ+GB6*;U#D_>@i?s+!&BV8{V1MjEgOvA zJGqFRCVzY;u~E9`yY}MPfQ=D*yq>>s1vlDvu+zt`mbYd&`adP`7@3rB3x>Hr+#ca_ zI={z{EJXde$KbJVl7z>={1g(N2?W%IcTg{x8uTGJDzJzSLYB z4>c&u#M+3$xJ8q#(tYRaGYAbtY^lkLee(sR?q_NDL0+5zy4dy4g{IX)kKU=cw8+J- z>GE0WdGq&QRY#v>FP##g-8;FKC^N3;>0`i@vc=Qapg78;(7!Pm!-7|T?)W&^7jucK zl?SZy?QQ=H{Jq`PLuLN9o)B$D)Wx|ISeFhdPQMc@&TSpIXpu)HS0?zezDu{P@WXi9 z^6}oW8f`ExMUj1eEPL-qSW-5p)X$Fw1g#$81YPRe**?X14t(7XOrMh?kQQ%Tz70nO zLgDVJWACbCAT>aLya|K`6aH?SwrOv=$=hjWoDA@>gxhv@j(nCng2`h156~dz(~&X5OuEMR#TTve) zy&6fvr?S6#eD*q~=kd@eUJu=rplI$%ste|rmVh^n7sGCgmo?0K%FXsI-A~cON3!_{ z>A>N$s1r*E!$HSrV)DUS8C8n#r4MtQt%PWd$qeih)YD6dDwuq0{%(SaM6)hagoTG@ z{g_XsJp6C)yWB7@1=CV^KJXg(GR0qQDO)#7H0H%ItrIuyjn}5F@x<$;>Rh9v3d^eT zpm1FHA5?Wm$D;EPs-+rgRe|`zC@>_%+J?v?1Kh|E@gQX19!Hor=W|AlhZ>m83T&W@ zp%rA&o!Cq8$oWvQcuzuY9-3BBlv+_rL~-b^Q)g#+qdT!ob@*k^+OpTvgxFGueed0w zG$fnW8^yn#4=Z4{U^ zx@Br^fg%^efHIe0OPL#5#F_g1?s31awJ)aiCjqeveL3h1#X?(ftCB(RDt)l3g{q}7 zO}2F45x!>v#>r~1)o7koZ>}b?NbWy&A2Q^~JLbQ^q+r?@>HpSRiV^IaM2trf8CYGB zvXP^OgpmKd!*8Qc`C-$-W54QOUJSGI6Cp`O&dp-NTv(1Ipn9kBvBb23D9wqcaBVCL zM#XBp6OtTDaD8XFc7XkKUP=hBg%o6<-n_1MGj6B3DbOgFgQ=VH0awLlIs0ZJK^VJY zX-oX(y@G-WrRJ(F$1|RYKbbWJWwwSAiEdRTRp_y&yys}n)*h|dRJ%hix8``-bZtV- z`0X8?)M#|K1zC)XuooCk&W15ZEpThbH}IT;qUjV2^pRXNwI}qevOT${hOiws*>hYi zeY-XUT)J+R-1lE<^?ZshpQo;#M0$X8n2}js3~3hB@g~e~h*b>yQsHnicq0^2FX0RV zUCOgSIe6K5FL;=nS3Xztl8j%gRXOG{BUC#=BD6`keKmhB=w1@O=c41?C*wHy4!ay2 zpR2Se{n8-Rr#)Zq*VNb-@(%AtdP3<%U<{h!2)Y%0#+&rIzq-rrX#IcM`|hYJesABQ zt5l^bU5W}wmkt7gfS@4K6r^{gH>D#TM2ZMVM`_X#rASo~l-{IwsnVO$c?pX5-0$z+ z``%q|t@p=U_pG(gOg>3=cJ@v($)1_aQz}{=!kFQ&t8QiGWnw0T0puLX>>?g$>o!~K z-@n*w3*fN3wZ$+r#R{&u)8Vj64%(e!c{%t=Wcg*zDqddpj*k$+(}0u&7nTf~Ha@B4 zm|Uwg{!;ib`Ia_%*LzDyn$PX~LP3#T~4Es(j;R8s+lK_kxIy`vtBS z?Ij&*oEe5l^jk69>rOeEqQfUrM()qq(ML28u+97Pp+}%kr<$mWI$lDQRjPmLQ(uaU zkv|tnOga}>rM87F6raSYNk-o&_3{Dzkf^imJi*)uSR++?F8%&+r@Tbmf3&~f!!I6xvZADq@3Wk2{B(XDC-d@5=poiAZBKkZre*KSZ`{__r)St3N}9pl z=e)PqZ%}4e*0YAp5wq|zfH`gljO1&*s)Qh)f{QV#|vu^b$w{b zlrkz1L*}4A^sR>fQCh5_QCTqlyqctPnLMry#(bBjvB;<5w;XfRoqbB=ey! z#KdAosbHvL-;o#BQD>jQTRe3U&zk6?O$z!D;a=6}_?%y&)*Sgi)My*^39&+5OU*Tx z1aFM|U{KGiNu84S{<7|qs`9P|_ndm6hEr<@w(-Q*u44H+ox9IVlJPjKrjQi;yR~D$ zq+msivK`|yZrP;i~+t?a|t)G?b!z| zofQpN37Hr#>=>q+k-j}&=|;d{uQIQuY}+k(kd|g*M3Z*AgkEP%CI8Cp=agBY3v@-z zaXqR<6@nzy%xID+xID4k-`22nCd4A+z|LL`g~^u*lC>#?Y|>9i-R`YP-0xPk_C0uG z$aI-ZWwqb=hV#2{ZtH^B37jtkCg4+y(*g-v2^b-uH`oYb#0MTtV7g1_MSfo|cEZm# zbPe%8Rb+VoCBjyOk_DG(kK9#OqZ~)NQzEY8z9G9Se_cNCYcr$tUM4fc5voFh0dP3hM6to^1OzwxZj$31^6@uv3`1R5N%4GIh6uQVNmcnM`n z9F6HmUNBEl(p;V7aHPRY+vOuyi$||r}U(=03Oz>GvCI&G4G8OjR>btUb1!^ z5*|x49D~{i{q1;*zY}2g>m+#k6tl7*h(KuPKJWZIr&^$3dERD3G3|Cr3NgM_?ap;a znMVD6PmP}i7t5(80v{wN^&HZ6RYZNBOB~wwr2HV{n8G9w;%Qf*6aZe*`I>tUXFQxTvG3) z)8D?;NJWek^S?2SsXCO@jv0Tm@2af)`NYN}9`S@%kM~>kv|GA>GbbE}{~;BX7oqS) zbYH3fx~V_={Ut^HotLo_8OjkI&4O|Eywm4XHhf=BaIF{$P5*dXYSqAWpLQicNpG-K zw2kx^RaKm6@6Uh`0Yj#mfkMu=4%HR-Ema5$#y| z^x(I53`VpTtfxK>(C>P*)?Ig*w+#$1;d_T=`g-kE7x zsf88I#aQ768!K)`KXc0Cl~G9C`7VlMlJEM!so=BmoU+QLF$?8dO9^AA)Lzp>3p!TM zdJHchx|~GZzi#;lXO*dop#wy@;vp**FKw+Q)_?A;OXL<+tGKP*mH<^Xc#BQzz>~i% zqAj^8x#|1rgq;m|9LH{Z^?Sp3?evxn4h|02Ohbszcw5*PTRTCjWUepU2{NZ^3C?3S z%^rF2!T5=?fgsrxwzm(tkJnIJT~8(X*Kh|H(k!I%Wv`1o)+NPeXVkNz5@mHt80L1h z|Av#^r5gJ3KBfPqt0yt%NQj;nF4Hy1c=#CDE>s1D>kG7^gKfY>pWQ0w?`_tKdZCkw zwPX6s+#*{(+@1+rolVz$#O69e<8QOq?zvjQlE<8cbLHzhD zpYx4E{?TrykCK-92I%yzoFU;sYH+}|koyb<=>`)CaG!Um zqdBv?=^mjNN@6Hls;X)8aTIp^}YhL5X9-J0Dm_Hva zDKIEFW7+qvJlekX;TM+q5 zN{1A&(kl<`vZfM5*zk|MnkqyxFfd z>z%`wZkr4JRW)3Fdeb9^bMD2u**6BbRE_(-ev=}mHJUdIlg150qtH&BX?0WfS0>6F zI$Tf3J%~MEB8#Xlt}>e67o5Y@?Wq)>+`x@38rV#jX~l~E1#Uf5{sLRRx@I~LIoDE; z9(HUK+Rjt`snnl&7zuA4Nz|Dq(WbCgU^8j?B7=kH=P6#CGCRjDJM4Y!jggZ&c{Jm% zy{l)ycibt8H&`q-8tfco;(3DT4LpX*Ssz(Z>bKZ&C7^AKN?$5`OuQUk(IK*B-}=+T zUarz@ax1LDl{SXq(9r#+7eio2o4vW|e0j+k4Wn4(^Skl=TM03&lUr1VVT%{6yZbXE zl~o1q*y8VZ+&iwig9v$niX*njUl$P;ng z`hj;KTq)~)fo3x z-TK-R#z}Nh;>NAjEXkhtRU(Hs9nPubX8*eHf&Sh3=Y5l?uTz;@!JOCyFNHm)&ZwG1 zvb`yj6`{dtHEd_Pue@3F)4LXIn840y6o1qf4|Qo}I#nfbIOFW=%G-FE?nSHnOBxRH zMPN2e2Wfa0%|^Jp7Uh$!9FSwwN^-qk zi)CR<4w0_cqm|P`GrTh?et8x|A4fgNQ{x7c9j>x7d|GxX8yhvigM zV9P5i53QjD#Q^dP{2v;U37<{aP;Cj=PmE0p0H08q;L5h0&DbPjGbU&=h6Xa~wS2(< z4?_wf1h)_YWQ>4a2AB$rjeoWYJbJ1cd~p4Mmt5425wN|*ks+{pC-hV{g*#26-+XX-E z27J5t02AKqPvkmm*Am85^e5+ldohkFYR`+38P;u}m% zRc3z&JhT1*2of2eW)djwZlk8s}n#DyM+S|mn(7LiYMIg(nVQxE<}M{98{5T z1pX}kkF%)aeBNBym-x)llLqa+w(c>_xRrbB0 z`LL^R|NJih;<z05Q4&lO8K)S^v z#XH>x%YGdCEAR~Orh9=L7|XxV#N4`TN|@9lt`a>2SHZUDbRS(jU33>j$92FwOJBQDz0|E z1*RkUcwnXQ=jy#zUG4F>dMM%~Cgc4g`maA#)KVh3(;hcQiVcmU@!u2;DVk(8F$}pm z9}{KWf9ggFC9ww2Eib>`Sre*Of9ztd@S(lIM}8U|o=Q5QYJ;P1@QA!rv9hyMOFydB zRNUo_E2{FBY<1C zbK!lIJ9?Y|W3R=z{gPR1uD_%hRoIEZ8!g<1ZzS;^a}jR1w_DG$QP*9_*$CPfWU|=3 zxpX)5jhX?AG*dv*6~EYbE)HvZG^8D(+VshOPNS--#uV+CXr4-bc8Up|iVqpb5FGy= z#fd==3Wd%MjhC5?U0nRn_+JhqH02}M*Sus2M&oIpobMT5oa3jTmU3y+db5j{`-oXm z_1h~<7bPhPw{XTRVgC2<2D8gZp>Tl>cwpu5;fb0<>QSA;eY9FoF);2O@A(J)WJWyr zvc>=gFP9)|VYRKVnN8D*Bff5%L`NvB*{`ONx#nHDH0yZiygrn3C%BWyv-0G0$F1U< z&FbE0xP~4^7wq-;MWp=_x0lvB3F+;O!MFUL)QqQlV$JCIeIw`>U$@cmU(dGwfJx9| zAVi^Q)-FCvZ`KgS0(QRapRS7>(NjxVWuNZ`sd@Beoid6JCI&p^7ma5ou##Q{%2G1 zB$(3r>3zgafloNQKfdrAd||bczy52A@shjDdM#RZ=xuJs3|6BW+^&Z2t7Ve;llI!u zTW77#v?t>y%r0UUj7t_RH#$QSZL9<>b{LLs5Z{v$B^zr;d8dElt9rB~*DRfzE&3@Voawy!Ij;N}h3_ku z*PTn{R#h%geWP7(8~b(DTxV)uk!^UArC8yWccj+yrWyBYns}UdjT6=M>S^>;6X!Ec zm=Y{|t?bP2I82G(oh|fRoTwpVS~cbF#vpjH_&i*Fue8B*ghMWAGy1}Nt>+Iv#Rf9H zuodUy(~Wwy7N0I<7&Kqyv0J^ARB9ufzg|Xo*gr%)KKu*%)A=Og{&nj=PjA~!3)vr( zRrkKG-c2nK@4^vErF-xpP3g>R*A3ImoJ9VO&a+ScJn^`5vVm7!B8wwjTQ^z# zG}nSp z*S>Yi3*3zAs*O^Xv4mExp-%JBnNJZ7#d#}RwCrD~<@QCEYa&%!OL}c8jM@42%RE>b zv%PIeomr$fl3k+DX?es~s#z29-N^J!%zB?CMKq+jHcUJ?Goj!VJ$*~&y!yS!(}XAM zW?rwox#aSr({!f$m08TK=vf?~0dPm*&jx?!l1Ti~!#N~=LPUW%(4vIBvsrv^-%z(b z9~-`MQl|b)zCK!5IK@qg51$;)tnFs@@Tm99tn&3F*olWGyFRh-S1g(pYIWBxc>Jd9 z>_WC<%~UK`&Fb9Fc6q|`)-1zJDfJxfCz{q@GTezZOhfZ3X2uooPmyDi+BOmS9DKQ0 zyMlHN58coDz6-~c!$JsUMwr+OEq13C1G_9~HSV*C>m|eif-k^#u9zFMl~wvapEncE z;gOcs#+##hnBd(IZi5j{C5}U!+)_J9b&GDiS^in90C26avoT%5mz3u90yVfG0?U)a@}eKLFW z6?}SLkbKjnE+e7wV$NP5^M9FRx47!doT=tu^m)*hLL!p@2hH(B__s6J&-EYIa0`E) zBG6ZD@OSLHVIm}E-NYt4FQ(X?TysxXGz7DrDUVWv*L53rJC7hc7bhozPGX@I`z3;o zL=vO}uaHtSx^6Kt#6BOud~b5*0CVBv2{p02rj;KqT**EL&z>xn?%geqEh=t8@71du z9n0HqvcI-OTdIGj?*7`HZkR=v4k!?(^XU*4%#j#);N2xASW z-i`bs)h0?8F#nRL?QEOnTnVmrzksu>%8QeW$K%QIW`E#KEVk#YnEr-HSuLmzM!|@4 z4#ij5`)T3=r$( zM*H2>J^fg7`*C`<9%Wu*kd}yNBG>dep4K7DEDqd7JX754q4!%joJh|&VS^A6k!15V zWo5d+7pHyST3`#@7LzQ=e>T%Pq+m{&9iJ6SQRn#X(*5W4=>!KOM21=A0{6DxMCMpl z|KP|T`b2^II$A{t(|nY($Nb5Gk}`hzg|sTscAB!y;o7C$#CEFz3{!;0M$okuJY01G zY)<4aL=vut7Aww71}liO=VQk|n@b^Oq(xu#@ieEbZ0%uF&k4*ZrAVu%hx1~7$^~Y= zt4y*>{iO=jwZn?xNh9(+HFB{naW13RrZa5lrof}HPF{oGRLjw|(3FS^;;Q&%Bhmc% znmyVCJr0YzQMv+sWJ(*O;0xtD2z{H{(M%hEa=XRr%B^&}L*BCKH+|LDF^?!IE7-899 zrP4|J6@7UoN^G;&X;pA)uEcMiU}~-Fa`f%;lKsH$TY}vC`FZ>eEA>L_0p=qwvze4L zA3JC*Ak&Hwh1o@g%iNTYnO_)>BHw7eB2JEg&zv{59<=Lau?Js0QQMd9a8+f$b&Jm^ zH?p?qCu0i%-ICedVQH8M@Dr=x=is=0?wE6pLA-ZIO{}6h3j{tZcx?_ZYaWXKS)IZC`k}{?CSeU=uTTb34KGF&rwdqua&e zhcn4$lJ@jxmMN=y7}7pYy1rx7!7YD_e|7zeRcQ?u^H-f|o5@%}iUPB|B=*Ux;bd@BKsfzOP{@zCV_z~`c;zn1gASi9-?l*HJwmMnE#dtgnR zI$Bk_KKGqE1?!Pd?Waf|iZXFREHSyrpAY2fnHy)Qys6qH?5cXquTV!nCcD1tMjW?s zkEs`9kNx{_f#)zz`S8^!>h+2xLYu?h!*W<70g1SO+2{`AFml7Ah-hx$LTeg2-?px^ ztA?;gG|dNoRr>@lwIX*`g+Y5=_y{fU^E151Pc`h+}!R+{wrF>oGZ`J4`dhc9mSsUr@*s7~GfVyAq}RV5s@~ zx4fSsRRwn)L>uif?5H=QXtNWyUP&#y`5s$*_D9pXx%YJiw1zxgjr{yp=+XBE?uc&Z zao)0f<~!Lp#Hh+?;FB@bgbJLy0A(?m|lWn9!P60O~A zx#N-ci1hJ(M(-QF1=+9@XQy#I>^*!5D?eHJ&F}1y6pq<`v9@#P@mSK)S&P>S9gG*$ zbSihfxtCX8KRn34=91cIWj$bNNvTzAAH}yxIe6_+sd4>q=RO*9LwrxyDJDf#%9`x~d0CzpncO*ozv4uIg7(JyxRdGYZH`I`177YBwb zoEjJ>t?~0}NAN$CVGpH8_{;*_bK=&CB&@@-8#sqBmG^~>8S5t>LsvpqxK{WxM0!{Lm-lT zfnw%UGXHs<)1`GuBHJ6bGo!8KF6Td{`RQ;Gy}OzH92?C_9MtNTNZHd{RG-2HpXyR! zc-M|0f>bid_@`I|))~BN0e9^K6?PE^&qw8Ue^w?@fmNpQ)Y5jKFq5Cxj^K?vM?ta9 zd;RoQ=>XqV^-t5W%hJUR^hVG3BI?$txbo2N&WAKnk=MV-eQGsJcDk2&5q!fmt0U`0 zDaF^&o5iA}p>x)X=^;U?*YnNzFS1)OGi6(zHsN7?x$TuPXM8)oB=Uz&IgLtAP<7pE zr!S3udwWN8(r0HswWLMA@05c9q!!eLQ(+^&NbC1^diY~8Cj&7rowy**a0xBthKTqW z=xOi`5t7J1D{e2j_$#YKk#7V=aO9brq#|AD$#zW9nuUUIeC0Nk^8H@CQ55rCF&mA5Sj^mn^x|8`d(zNqW5`6e8S<)p>7r>Ly}nxNuf0) z^EMVX{mIDbNI9&^+Fz4?OEiWb}~aBQyZv&$G{7 zm5Cyc4bTW;#yYi>i!OeX9enOA!>n%>&ne9FBble-t^ZQXLz8pa%s=kiAKYxOOTCDp zW$xtMP{UW{{-IAhFG+eg@#Cg4$IaJXM5Nwb+2$)(y<6|?8Q1@s5TV@|N~(w`6{H;Y^m96^T{_{Pvr{S@hS8v6~tvGZ{j>2%a5o{(jyhT_Wz+ z^glMJ$2g=&y2EzSibUnt)mOn%b|T{8tz? zIDR;pbMQ01q{wJ#|Exh`xtR6#dCdi4o*F6lJDa!->iO!guH&;Vm#Adk2@41_?|K`h z;$y8{nH`n(L+J|juUz5rp7XQpDg9Fn7zItba z-mRch@lxs%S1eE2e&)Mte0EBbDGQsdli<~WCIel4WvwX-_TC~xO0BhR1^Y)rj9#pz zkwV`p_dem=#P%XR92#ESV965`i*3|lj4e>F=)Ju)O=omEfBDjcfUx;vw`eF5%w(DiiO-{@H8k3z`RS z<~s?Zp!ZROS6`QO8MEpZni=&L3Xwf~KYxSR0>Axai^VnVkXvp{69OaT9lA6E?Z{Xn zOG)a`gU~N#Iqn+%`Mzc9B_H0YhbTXaKCN01t9P#F^6L2>zA{SrDtYhvbmNA>(8e84 zmSDVu6^;cm&jy|(%|_!Nvg!Pvvx7CR-H*e+-0;C^3EMVBo|xqq&Oyq1ek|tPPOc%( z!=u;SCD4*_njH@DJrj@~2Amx^{gr-Rn=nB;uKLfGe5Hum%Ld|MejSftqJ@AYp!mFz00 z+Dgo(q>4=8zu=Vr6aS;~SI;-+1@laMVoOH)_-?TLr^YCDxVyL8o;ADr(LnNZOnB5Q1Vm#}e0yksT(`P}|!Nf_8bPxHQJ7Dfve>@z+>y`7+qw#&Z`@NfG>xc;>HYbzT)^gGwL6o&0qeq+iren(nw^ zKoF13^L*abV3pT31_Hz#E$`%*)&?l-i*MX{?&Y%oscMOOb;&g|Pf)aL7`#QZB%FL0 z4oCJaiFo9nA1}f<)O&OllOXVdSf?!sX|%LNW2S(-=wx?a>G+~+jk z4;J3kV~wJ|@cn9|$oqQT(YdTQJDTlPU2Ko!TwPAS8}{MGdc#BW!a{A`r>)+kCW`Cw zoh<)l*6Pt|S;fYUj0Z8d%_DY1-^fWPO+8t|RP86bBCC_@-r7YQH@q9XCbZ?C!S>j7 zaz@R<#Y-b==>ypv8{YCOy24Ihol|7*xHwk&jtQsHs1hgy$R(cJaH6%CagcVT4o;&=xOnjLA-6=Lk$$QpPl)$mdJF0>=zFSOAf zn~ViN9A*^;^@rDkPbz;nedp476Y?!;s|U0{WIEM;T~B{Unt1xBe`@Ws*Xd^d_xXFb zmQuRYth(L!q+hoxIpoC$c)ZIUNaj{EO;X)RT(BqfbdpVDrppZJ(+F+IcO`f2ztnqw zZQ_ys`R3vh7sc?A=I?6fLa%eC1P}*ecqxovMB{E+)u1WTggsn*-uVGnvX98{ZLy8Q z6E7Us!_~u=K9$1NvzrZ$^EAc1u0B<@K<;vh_!9x%=$NO#cLBhUJ{0|qPf9xxCld*9 z>L7%NPsqEj1Aksxs-Z=`TKSV&f|RX@Z&Uu6?4dxinTFY!bMYqnoff?3=euI;?TK$bldEUA-mz5{!J5-%HHrug#3ly#0 z!}F9VmC7vf@~pk*FLowI_=JmZ0GBTjo~B|up`QWoTGLDZd5-H!8k6(_&1>=y+sP5;E>nBMv%YdXR;c!quN0f)@GB~1-C)!CR~zlvYW zD&vQxDUN%DLKOGC1N!_kxn=}2muqLH+pGA<6LFih{o2Y)2Og~lwR~u4!plPNcxo?K z^5n#b{K!IY&&lN^;MMffMpJh>8^Gd)9%5O`)tFs8)2iF?s0{!~LG?fQc8F(XS-3~mel(4-=Aksh}L!%h+VIA4`g8lBIL^6?r zTk0(RAqBUwk)Q9Wwx=u%T3j#pwQUyr3KpLb@Yj=cM53N?He8$Mux&(hv2}tvBZS*CnYvTYPVSqXCyEoq#--^Y zoHEiKr3$K5WtBI_zgIxcdE?i;g`m}&>CBA559U>UtY5~t&^@T)wZWjGq>@0#=MY1% z&Cn%tgVXa(d1=?##6+zgyb@ej@VDSeQrccLlP7HlUzimlv+*L<5!!u^OXjt?@v8L;BA-3dNr{lJ?pjhWDP*tKeWNcgiZeJv1F6D^s7 z(flffp**vmkVo*4xkfF1b7Z;j_W3gx3svx+@lKqr+g#YQj8`IV{z7C)8rA(?az5iS z7Tc-m`ar%wdp>;fa`LKgD$}iI`rj=G<(}5}gvb={9^j@^YoT4dlKVt*fDRFud1^Je zwOK7Rlzq!(ee}LKMvgptw)0GNLTlxQiVub9(7a%l=6f-`cK388E-aH}PBWOqOAt8)Lh7R_C1(neQdxt-)00+nyr}K9m@MkOy&GbMVou%PF z>+Z;jV4&Yrtvkwj+0^iX-5(x^lbPYsCNalhH4wIz)w42uUzOuc8{sVBHZ|`7#Y!H|x3Ea>)@)G|}2mVoi z3!vJv0DuI4sHOf!{`pUFcnBZ?!1WrMAwqUIj?kEZW3C28Q>4% zfUpsRe@k2a8v`aeiUY-g4^aQf3(^vY{`+_kI!brM8^lxU{0`$*fFsCv^!sn*|KIWd zzl}eV3&{EZO5Xpj{{C<4_doeTiv#vOZofzE55?a9qo4oL&;QTuDZj^T&|i+mZqy^}Lol7opLFm5uPL9v@eZKVfCvG(0U`zjR!j&!Aov6{86cuR z1moZik*G8V?ZzuJcr~B=$I!ALpfmDAV8FWVA&8q&SQ&m4smlp zHZ1BIG7glfO%p3Z9p(BkVMKjPW{5mo?U3KUEp&3}Y% z{UiKOeF^=8-#_&S?Tf09*B}f(6DJL54^R~#I0u7sA5b3*I(r8LYj6&Kb%21qu+D;c zRyMdUIfChaFoK){I>iO^J-kVvW{`&v@FAK5#e@S>IDjq!+8!CO8^t+5+hCg951QyG zm_1j5`!puNPc4oh^q^knLAea=2*L>FX-qQ+av>C~g+W`tbO1yI`PrrsgdNm}1GHz( zdIaGDW%Gdc%M1AP;Q|6n3V~+`A_%7cLeCLIZ6b&gNPF8B3<ncR0k16tr%?R z3FN*D^m9)JtOmP4lLB?o4go3yZ9x~vpa;_FgEWR#2x0`%m;f0}@esr;9dun^1hD}1 zvJ^!StBVL?U4|ewd0@x@ve>gAhyxgh9BUB7DGWg#UOH$DkYHW}3HgQ~PcjhXDM%LvbP+y-AQ5c{ z5;>0`Q7H)WTn|BFa1bO`3_)H1nd4r8Ar8ou2=tnygdoWi2$C{@ATM(f@Yy_z#M34_72=Wo6{{-ytISE1PZNPh@0Q&;kT%!wuG-)763kQO< zA_&q(k09;I2-4w%AYJDWq#M-Z8<4dJ*tHj6{lIPmst7W;h#*7d2r>fX90htAJ3x@} zMg*B?MvzIse+tMr{R=^6Ksj^3em_(YWS$v87J+`3fLzN#2(kjwtpVMyMU&YUdGx?8bs&)&0L%ZUWoE-&y0|$D@BVG(@MgF7ZroFYJ6>3p;K-lo#mYYN=f0mm>_P>{#$7%l$mYPI{f0ml&|5$3? zv$Wv_20Qxwn;OuM21x{n3V(b?_J;QG1jEbAiwQwTFu{>roD@Q6gr$(M0D^VqgxVB> z4bIS@s{gS}M58!T4{`!76M^ZDexQjSDIb;$3FdET7X=4Hz~A)vC^!KM4!Xi${zND^Xp(<%(B=N}Awj{RdXDQwhJu6P>@N;< z@|Vw96r2(Tr$WI&m;ald1_h@@!Rb)&^C&n23eJdvGoj$1yZz-4hLOK=f}!y*ehCF< zMZwun@XIJTI|>eljK6$1QE+Y)oCgKxMZvG2;Cv`JKMF2@f(xSH!YH^13J!+Xzw(Ho z;8#&_aTHt<1;2)Z!+vtyZlzK1>nOMk3NDL+%c0=%D7XR&4u;FW{J}8u*Di`ExDpDk zjDp`m!NHLFmk$_{|MF2o!PQZ44HWz?3a*KQYoXxxQE+V(Tn7c$MZxt^a5yF$_ZLGH z+z17SL-(}1%e6+q!KLOeZij-yk?=UZ0}Aelf`iN6 zUpbvo@P{ZkxMcn198AIf%9(?L=c3?wD0n^!UVwrZqTp{( z@M0AFEeie)1usFtOHuH06#P93UV(yFqTp31cr^-MgM!zh;2%-&PydEvI{f?PKb$Kd zU4U8u!TP{5djY}xkasPhHb8xVJ_Gs!s2|WUpbvn00D);O(gX;~SPN(n(04$PC*)lR zs2vcPf+C%OAdluhR11!+fM8k}-v9`f4Rr|1fcc65!E(9*!7^cfC>zWN`N1R9Ei4PB zgJ~c?SQmJNX<&I!##%rS8v_LE^A!-xKL7}p56gx1g*en3EEAqXzOY<~!Lp!SkSEj` z%nxzMzvR#P6ga|os2dm$Z8ii5@`B~VV?7|KODHFl8S;Q-K;E^0AV0_-;*bX zkO#CElyedg%m*<@Fwb$>Fb}L3YzOdo91rDz`Jp`Ud;(A{AlR2+*)ZKYASeUW5$rQC zKeXE_Aeauy59>Y;s1^{+2kT!52(||pR|W{mcq|({hvmXJsGB7~P|r|4SRS-5lpU4> z3Fe1&fV_|S!F;g3xqzUpVVSVbP#$;=Z3TE7&Chqhc>y38zX1s5gYp#tg7$~JVf~Kl zybsQyEU?`|Ibb?iF60Z#h5YgW9qaQAI0rHwt+1eeMgc*&pgoW43d`6Al=BCN=^#JY z2H+9471&0hj$!+Qx`Hyn`a}K)fS`Sjbu$3YkM#;=gM6U;ux?O(ST@vYEm&Sqp}10j z4lu6+R|*SorGT<IIMfHr|EB?Y)rz5!QCE^wuo0#}LGH|5;9pAMAt`s%kMxg=(+MRYhaHW_7HwpzHP=!E0K(=5h;7XYQ zt`uOSr_sQbVhmg<8^DbMbP);KLlkI(&q2BvV2fBK;7S2?iz@`K6p%j=l$`|hoeb)l zN(WpimB5t(be+xu+$cbwndrckk_lWX-k?c>bh$w8ye;5L0d1R_nx&bDj5isNd{!>8486eNh5^$x&0#}M27>Yo?MMf~JkOEf<(Az4o+gbw{ zT6z%Vrw(wXfV|r{pd0rgpds6v*chUL6Fch%55NKF1-w0S2>z+;^-XNR@i+z~I=WiJ z#QKhg4xl9Do~6;z5hkNYy`K)8B6fN!#Df>7j|BpC;$Ke literal 0 HcmV?d00001 diff --git a/tests/tests.js b/tests/tests.js index 78f8258..7975d93 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -13,7 +13,7 @@ exports.defineAutoTests = function () { var applicationID_default = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID; var applicationID_custom = 'F5EEDC6C'; - var videoUrl = 'http://s3.nwgat.net/flvplayers3/bbb.mp4'; + var videoUrl = location.origin + '/res/test.mp4'; describe('chrome.cast', function () { @@ -21,7 +21,7 @@ exports.defineAutoTests = function () { var _receiverAvailability = []; var _sessionUpdatedFired = false; var _mediaUpdatedFired = false; - var _currentMedia; + var media; it('SPEC_00100 should contain definitions', function () { expect(chrome.cast.VERSION).toBeDefined(); @@ -196,68 +196,78 @@ exports.defineAutoTests = function () { }); }, USER_INTERACTION_TIMEOUT); - it('SPEC_01000 Everything Session', function (done) { - alert('---TEST INSTRUCTION---\nPlease select a valid chromecast in the next dialog.'); - chrome.cast.requestSession(function (session) { - checkSessionProperties(session); + describe('Everything Session', function () { - var updateListener = function (isAlive) { - _sessionUpdatedFired = true; - session.removeUpdateListener(updateListener); - }; + it('SPEC_01000 Test valid session', function (done) { + alert('---TEST INSTRUCTION---\nPlease select a valid chromecast in the next dialog.'); + chrome.cast.requestSession(function (session) { + sessionProperties(session) + .then(loadMedia) + .then(done); - session.addUpdateListener(updateListener); + var updateListener = function (isAlive) { + console.log('session updated!!!!'); + _sessionUpdatedFired = true; + session.removeUpdateListener(updateListener); + }; - done(); - }, function (err) { - expect(err).toBe(null); - done(); - }); - }, USER_INTERACTION_TIMEOUT); + session.addUpdateListener(updateListener); + }, function (err) { + expect(err).toEqual('We should not get an error.'); + done(); + }); + }, USER_INTERACTION_TIMEOUT); - function checkSessionProperties (session) { - expect(session).toBeTruthy(); - expect(session).not.toBe('no_session'); - expect(session.appId).toBeTruthy(); - expect(session.hasOwnProperty('displayName')).toBeTruthy(); - expect(session.hasOwnProperty('receiver')).toBeTruthy(); - expect(session.hasOwnProperty('receiver.friendlyName')).toBeTruthy(); - expect(session.hasOwnProperty('addUpdateListener')).toBeTruthy(); - expect(session.hasOwnProperty('removeUpdateListener')).toBeTruthy(); - } - - it('loadRequest should work', function (done) { - var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); - var request = new chrome.cast.media.LoadRequest(mediaInfo); - expect(_session).not.toBeNull(); - _session.loadMedia(request, function (media) { - console.log('loadRequest success', media); - _currentMedia = media; - // expect(_currentMedia instanceof chrome.cast.media.Media).toBe(true); - - expect(_currentMedia.sessionId).toEqual(_session.sessionId); - expect(_currentMedia.addUpdateListener).toBeDefined(); - expect(_currentMedia.removeUpdateListener).toBeDefined(); - - var updateListener = function () { - _mediaUpdatedFired = true; - _currentMedia.removeUpdateListener(updateListener); - }; - - _currentMedia.addUpdateListener(updateListener); + function sessionProperties (session) { + return new Promise(function (resolve, reject) { + expect(session instanceof chrome.cast.Session).toBeTruthy(); + expect(session.appId).toBeDefined(); + expect(session.displayName).toBeDefined(); + expect(session.receiver).toBeDefined(); + expect(session.receiver.friendlyName).toBeDefined(); + expect(session.addUpdateListener).toBeDefined(); + expect(session.removeUpdateListener).toBeDefined(); + expect(session.loadMedia).toBeDefined(); + + resolve(session); + }); + } - done(); - }, function (err) { - console.log('loadRequest error', err); - expect(err).toBeNull(); - done(); - }); + function loadMedia (session) { + return new Promise(function (resolve, reject) { + var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + expect(mediaInfo).toBeTruthy(); + + var request = new chrome.cast.media.LoadRequest(mediaInfo); + expect(request).toBeTruthy(); + + session.loadMedia(request, function (media) { + expect(media instanceof chrome.cast.media.Media).toBeTruthy(); + + expect(media.sessionId).toEqual(session.sessionId); + expect(media.addUpdateListener).toBeDefined(); + expect(media.removeUpdateListener).toBeDefined(); + + var updateListener = function () { + _mediaUpdatedFired = true; + media.removeUpdateListener(updateListener); + }; + + media.addUpdateListener(updateListener); + + resolve(session); + }, function (err) { + expect(err).toEqual('We should not get an error.'); + reject(err); + }); + }); + } }); it('pause media should succeed', function (done) { setTimeout(function () { - _currentMedia.pause(null, function () { + media.pause(null, function () { console.log('pause success'); done(); }, function (err) { @@ -270,7 +280,7 @@ exports.defineAutoTests = function () { it('play media should succeed', function (done) { setTimeout(function () { - _currentMedia.play(null, function () { + media.play(null, function () { console.log('play success'); done(); }, function (err) { @@ -286,7 +296,7 @@ exports.defineAutoTests = function () { var request = new chrome.cast.media.SeekRequest(); request.currentTime = 10; - _currentMedia.seek(request, function () { + media.seek(request, function () { done(); }, function (err) { expect(err).toBeNull(); @@ -312,13 +322,13 @@ exports.defineAutoTests = function () { var request = new chrome.cast.media.VolumeRequest(); request.volume = volume; - _currentMedia.setVolume(request, function () { + media.setVolume(request, function () { var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, true)); - _currentMedia.setVolume(request, function () { + media.setVolume(request, function () { var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, false)); - _currentMedia.setVolume(request, function () { + media.setVolume(request, function () { done(); }, function (err) { expect(err).toBeNull(); @@ -338,7 +348,7 @@ exports.defineAutoTests = function () { }); it('stopping the video', function (done) { - _currentMedia.stop(null, function () { + media.stop(null, function () { setTimeout(done, 1000); }, function (err) { expect(err).toBeNull(); From ac1495ed5577bd54c63caf54d22e893558cc827a Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 26 Aug 2019 04:48:54 -0600 Subject: [PATCH 013/166] .gitignore update --- .gitignore | 1 + package-lock.json | 1424 --------------------------------------------- 2 files changed, 1 insertion(+), 1424 deletions(-) delete mode 100644 package-lock.json diff --git a/.gitignore b/.gitignore index 7589473..dfde986 100644 --- a/.gitignore +++ b/.gitignore @@ -137,3 +137,4 @@ Temporary Items # Created by .ignore support plugin (hsz.mobi) node_modules +package-lock.json diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index b17f042..0000000 --- a/package-lock.json +++ /dev/null @@ -1,1424 +0,0 @@ -{ - "name": "cordova-plugin-chromecast", - "version": "1.0.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "acorn": { - "version": "5.7.3", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-5.7.3.tgz", - "integrity": "sha512-T/zvzYRfbVojPWahDsE5evJdHb3oJoQfFbsrKM7w5Zcs++Tr257tia3BmMP8XYVjp1S9RZXQMh7gao96BlqZOw==", - "dev": true - }, - "acorn-jsx": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-3.0.1.tgz", - "integrity": "sha1-r9+UiPsezvyDSPb7IvRk4ypYs2s=", - "dev": true, - "requires": { - "acorn": "^3.0.4" - }, - "dependencies": { - "acorn": { - "version": "3.3.0", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-3.3.0.tgz", - "integrity": "sha1-ReN/s56No/JbruP/U2niu18iAXo=", - "dev": true - } - } - }, - "ajv": { - "version": "4.11.8", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-4.11.8.tgz", - "integrity": "sha1-gv+wKynmYq5TvcIK8VlHcGc5xTY=", - "dev": true, - "requires": { - "co": "^4.6.0", - "json-stable-stringify": "^1.0.1" - } - }, - "ajv-keywords": { - "version": "1.5.1", - "resolved": "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-1.5.1.tgz", - "integrity": "sha1-MU3QpLM2j609/NxU7eYXG4htrzw=", - "dev": true - }, - "ansi-escapes": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-1.4.0.tgz", - "integrity": "sha1-06ioOzGapneTZisT52HHkRQiMG4=", - "dev": true - }, - "ansi-regex": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", - "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", - "dev": true - }, - "ansi-styles": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", - "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=", - "dev": true - }, - "argparse": { - "version": "1.0.10", - "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", - "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", - "dev": true, - "requires": { - "sprintf-js": "~1.0.2" - } - }, - "babel-code-frame": { - "version": "6.26.0", - "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", - "integrity": "sha1-Y/1D99weO7fONZR9uP42mj9Yx0s=", - "dev": true, - "requires": { - "chalk": "^1.1.3", - "esutils": "^2.0.2", - "js-tokens": "^3.0.2" - } - }, - "balanced-match": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", - "dev": true - }, - "brace-expansion": { - "version": "1.1.11", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", - "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, - "requires": { - "balanced-match": "^1.0.0", - "concat-map": "0.0.1" - } - }, - "buffer-from": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", - "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==", - "dev": true - }, - "builtin-modules": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz", - "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=", - "dev": true - }, - "caller-path": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/caller-path/-/caller-path-0.1.0.tgz", - "integrity": "sha1-lAhe9jWB7NPaqSREqP6U6CV3dR8=", - "dev": true, - "requires": { - "callsites": "^0.2.0" - } - }, - "callsites": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/callsites/-/callsites-0.2.0.tgz", - "integrity": "sha1-r6uWJikQp/M8GaV3WCXGnzTjUMo=", - "dev": true - }, - "chalk": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", - "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", - "dev": true, - "requires": { - "ansi-styles": "^2.2.1", - "escape-string-regexp": "^1.0.2", - "has-ansi": "^2.0.0", - "strip-ansi": "^3.0.0", - "supports-color": "^2.0.0" - } - }, - "circular-json": { - "version": "0.3.3", - "resolved": "https://registry.npmjs.org/circular-json/-/circular-json-0.3.3.tgz", - "integrity": "sha512-UZK3NBx2Mca+b5LsG7bY183pHWt5Y1xts4P3Pz7ENTwGVnJOUWbRb3ocjvX7hx9tq/yTAdclXm9sZ38gNuem4A==", - "dev": true - }, - "cli-cursor": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-1.0.2.tgz", - "integrity": "sha1-ZNo/fValRBLll5S9Ytw1KV6PKYc=", - "dev": true, - "requires": { - "restore-cursor": "^1.0.1" - } - }, - "cli-width": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/cli-width/-/cli-width-2.2.0.tgz", - "integrity": "sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk=", - "dev": true - }, - "co": { - "version": "4.6.0", - "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", - "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=", - "dev": true - }, - "code-point-at": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz", - "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=", - "dev": true - }, - "concat-map": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true - }, - "concat-stream": { - "version": "1.6.2", - "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", - "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", - "dev": true, - "requires": { - "buffer-from": "^1.0.0", - "inherits": "^2.0.3", - "readable-stream": "^2.2.2", - "typedarray": "^0.0.6" - } - }, - "contains-path": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/contains-path/-/contains-path-0.1.0.tgz", - "integrity": "sha1-/ozxhP9mcLa67wGp1IYaXL7EEgo=", - "dev": true - }, - "core-util-is": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", - "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", - "dev": true - }, - "d": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/d/-/d-1.0.1.tgz", - "integrity": "sha512-m62ShEObQ39CfralilEQRjH6oAMtNCV1xJyEx5LpRYUVN+EviphDgUc/F3hnYbADmkiNs67Y+3ylmlG7Lnu+FA==", - "dev": true, - "requires": { - "es5-ext": "^0.10.50", - "type": "^1.0.1" - } - }, - "debug": { - "version": "2.6.9", - "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", - "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", - "dev": true, - "requires": { - "ms": "2.0.0" - } - }, - "deep-is": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz", - "integrity": "sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ=", - "dev": true - }, - "doctrine": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-2.1.0.tgz", - "integrity": "sha512-35mSku4ZXK0vfCuHEDAwt55dg2jNajHZ1odvF+8SSr82EsZY4QmXfuWso8oEd8zRhVObSN18aM0CjSdoBX7zIw==", - "dev": true, - "requires": { - "esutils": "^2.0.2" - } - }, - "error-ex": { - "version": "1.3.2", - "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", - "integrity": "sha512-7dFHNmqeFSEt2ZBsCriorKnn3Z2pj+fd9kmI6QoWw4//DL+icEBfc0U7qJCisqrTsKTjw4fNFy2pW9OqStD84g==", - "dev": true, - "requires": { - "is-arrayish": "^0.2.1" - } - }, - "es5-ext": { - "version": "0.10.50", - "resolved": "https://registry.npmjs.org/es5-ext/-/es5-ext-0.10.50.tgz", - "integrity": "sha512-KMzZTPBkeQV/JcSQhI5/z6d9VWJ3EnQ194USTUwIYZ2ZbpN8+SGXQKt1h68EX44+qt+Fzr8DO17vnxrw7c3agw==", - "dev": true, - "requires": { - "es6-iterator": "~2.0.3", - "es6-symbol": "~3.1.1", - "next-tick": "^1.0.0" - } - }, - "es6-iterator": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/es6-iterator/-/es6-iterator-2.0.3.tgz", - "integrity": "sha1-p96IkUGgWpSwhUQDstCg+/qY87c=", - "dev": true, - "requires": { - "d": "1", - "es5-ext": "^0.10.35", - "es6-symbol": "^3.1.1" - } - }, - "es6-map": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/es6-map/-/es6-map-0.1.5.tgz", - "integrity": "sha1-kTbgUD3MBqMBaQ8LsU/042TpSfA=", - "dev": true, - "requires": { - "d": "1", - "es5-ext": "~0.10.14", - "es6-iterator": "~2.0.1", - "es6-set": "~0.1.5", - "es6-symbol": "~3.1.1", - "event-emitter": "~0.3.5" - } - }, - "es6-set": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/es6-set/-/es6-set-0.1.5.tgz", - "integrity": "sha1-0rPsXU2ADO2BjbU40ol02wpzzLE=", - "dev": true, - "requires": { - "d": "1", - "es5-ext": "~0.10.14", - "es6-iterator": "~2.0.1", - "es6-symbol": "3.1.1", - "event-emitter": "~0.3.5" - } - }, - "es6-symbol": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/es6-symbol/-/es6-symbol-3.1.1.tgz", - "integrity": "sha1-vwDvT9q2uhtG7Le2KbTH7VcVzHc=", - "dev": true, - "requires": { - "d": "1", - "es5-ext": "~0.10.14" - } - }, - "es6-weak-map": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/es6-weak-map/-/es6-weak-map-2.0.3.tgz", - "integrity": "sha512-p5um32HOTO1kP+w7PRnB+5lQ43Z6muuMuIMffvDN8ZB4GcnjLBV6zGStpbASIMk4DCAvEaamhe2zhyCb/QXXsA==", - "dev": true, - "requires": { - "d": "1", - "es5-ext": "^0.10.46", - "es6-iterator": "^2.0.3", - "es6-symbol": "^3.1.1" - } - }, - "escape-string-regexp": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", - "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", - "dev": true - }, - "escope": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/escope/-/escope-3.6.0.tgz", - "integrity": "sha1-4Bl16BJ4GhY6ba392AOY3GTIicM=", - "dev": true, - "requires": { - "es6-map": "^0.1.3", - "es6-weak-map": "^2.0.1", - "esrecurse": "^4.1.0", - "estraverse": "^4.1.1" - } - }, - "eslint": { - "version": "3.19.0", - "resolved": "https://registry.npmjs.org/eslint/-/eslint-3.19.0.tgz", - "integrity": "sha1-yPxiAcf0DdCJQbh8CFdnOGpnmsw=", - "dev": true, - "requires": { - "babel-code-frame": "^6.16.0", - "chalk": "^1.1.3", - "concat-stream": "^1.5.2", - "debug": "^2.1.1", - "doctrine": "^2.0.0", - "escope": "^3.6.0", - "espree": "^3.4.0", - "esquery": "^1.0.0", - "estraverse": "^4.2.0", - "esutils": "^2.0.2", - "file-entry-cache": "^2.0.0", - "glob": "^7.0.3", - "globals": "^9.14.0", - "ignore": "^3.2.0", - "imurmurhash": "^0.1.4", - "inquirer": "^0.12.0", - "is-my-json-valid": "^2.10.0", - "is-resolvable": "^1.0.0", - "js-yaml": "^3.5.1", - "json-stable-stringify": "^1.0.0", - "levn": "^0.3.0", - "lodash": "^4.0.0", - "mkdirp": "^0.5.0", - "natural-compare": "^1.4.0", - "optionator": "^0.8.2", - "path-is-inside": "^1.0.1", - "pluralize": "^1.2.1", - "progress": "^1.1.8", - "require-uncached": "^1.0.2", - "shelljs": "^0.7.5", - "strip-bom": "^3.0.0", - "strip-json-comments": "~2.0.1", - "table": "^3.7.8", - "text-table": "~0.2.0", - "user-home": "^2.0.0" - } - }, - "eslint-config-semistandard": { - "version": "11.0.0", - "resolved": "https://registry.npmjs.org/eslint-config-semistandard/-/eslint-config-semistandard-11.0.0.tgz", - "integrity": "sha1-RO73z9/Uchnjp7gbkbVA6IC7JhU=", - "dev": true - }, - "eslint-config-standard": { - "version": "10.2.1", - "resolved": "https://registry.npmjs.org/eslint-config-standard/-/eslint-config-standard-10.2.1.tgz", - "integrity": "sha1-wGHk0GbzedwXzVYsZOgZtN1FRZE=", - "dev": true - }, - "eslint-import-resolver-node": { - "version": "0.2.3", - "resolved": "https://registry.npmjs.org/eslint-import-resolver-node/-/eslint-import-resolver-node-0.2.3.tgz", - "integrity": "sha1-Wt2BBujJKNssuiMrzZ76hG49oWw=", - "dev": true, - "requires": { - "debug": "^2.2.0", - "object-assign": "^4.0.1", - "resolve": "^1.1.6" - } - }, - "eslint-module-utils": { - "version": "2.4.1", - "resolved": "https://registry.npmjs.org/eslint-module-utils/-/eslint-module-utils-2.4.1.tgz", - "integrity": "sha512-H6DOj+ejw7Tesdgbfs4jeS4YMFrT8uI8xwd1gtQqXssaR0EQ26L+2O/w6wkYFy2MymON0fTwHmXBvvfLNZVZEw==", - "dev": true, - "requires": { - "debug": "^2.6.8", - "pkg-dir": "^2.0.0" - } - }, - "eslint-plugin-import": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-import/-/eslint-plugin-import-2.3.0.tgz", - "integrity": "sha1-N8gB4K2g4pbL3yDD85OstbUq82s=", - "dev": true, - "requires": { - "builtin-modules": "^1.1.1", - "contains-path": "^0.1.0", - "debug": "^2.2.0", - "doctrine": "1.5.0", - "eslint-import-resolver-node": "^0.2.0", - "eslint-module-utils": "^2.0.0", - "has": "^1.0.1", - "lodash.cond": "^4.3.0", - "minimatch": "^3.0.3", - "read-pkg-up": "^2.0.0" - }, - "dependencies": { - "doctrine": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-1.5.0.tgz", - "integrity": "sha1-N53Ocw9hZvds76TmcHoVmwLFpvo=", - "dev": true, - "requires": { - "esutils": "^2.0.2", - "isarray": "^1.0.0" - } - } - } - }, - "eslint-plugin-node": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-node/-/eslint-plugin-node-5.0.0.tgz", - "integrity": "sha512-9xERRx9V/8ciUHlTDlz9S4JiTL6Dc5oO+jKTy2mvQpxjhycpYZXzTT1t90IXjf+nAYw6/8sDnZfkeixJHxromA==", - "dev": true, - "requires": { - "ignore": "^3.3.3", - "minimatch": "^3.0.4", - "resolve": "^1.3.3", - "semver": "5.3.0" - }, - "dependencies": { - "semver": { - "version": "5.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.3.0.tgz", - "integrity": "sha1-myzl094C0XxgEq0yaqa00M9U+U8=", - "dev": true - } - } - }, - "eslint-plugin-promise": { - "version": "3.5.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-3.5.0.tgz", - "integrity": "sha1-ePu2/+BHIBYnVp6FpsU3OvKmj8o=", - "dev": true - }, - "eslint-plugin-standard": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/eslint-plugin-standard/-/eslint-plugin-standard-3.0.1.tgz", - "integrity": "sha1-NNDJFbRe3G8BA5PH7vOCOwhWXPI=", - "dev": true - }, - "espree": { - "version": "3.5.4", - "resolved": "https://registry.npmjs.org/espree/-/espree-3.5.4.tgz", - "integrity": "sha512-yAcIQxtmMiB/jL32dzEp2enBeidsB7xWPLNiw3IIkpVds1P+h7qF9YwJq1yUNzp2OKXgAprs4F61ih66UsoD1A==", - "dev": true, - "requires": { - "acorn": "^5.5.0", - "acorn-jsx": "^3.0.0" - } - }, - "esprima": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", - "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==", - "dev": true - }, - "esquery": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/esquery/-/esquery-1.0.1.tgz", - "integrity": "sha512-SmiyZ5zIWH9VM+SRUReLS5Q8a7GxtRdxEBVZpm98rJM7Sb+A9DVCndXfkeFUd3byderg+EbDkfnevfCwynWaNA==", - "dev": true, - "requires": { - "estraverse": "^4.0.0" - } - }, - "esrecurse": { - "version": "4.2.1", - "resolved": "https://registry.npmjs.org/esrecurse/-/esrecurse-4.2.1.tgz", - "integrity": "sha512-64RBB++fIOAXPw3P9cy89qfMlvZEXZkqqJkjqqXIvzP5ezRZjW+lPWjw35UX/3EhUPFYbg5ER4JYgDw4007/DQ==", - "dev": true, - "requires": { - "estraverse": "^4.1.0" - } - }, - "estraverse": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/estraverse/-/estraverse-4.2.0.tgz", - "integrity": "sha1-De4/7TH81GlhjOc0IJn8GvoL2xM=", - "dev": true - }, - "esutils": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz", - "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==", - "dev": true - }, - "event-emitter": { - "version": "0.3.5", - "resolved": "https://registry.npmjs.org/event-emitter/-/event-emitter-0.3.5.tgz", - "integrity": "sha1-34xp7vFkeSPHFXuc6DhAYQsCzDk=", - "dev": true, - "requires": { - "d": "1", - "es5-ext": "~0.10.14" - } - }, - "exit-hook": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/exit-hook/-/exit-hook-1.1.1.tgz", - "integrity": "sha1-8FyiM7SMBdVP/wd2XfhQfpXAL/g=", - "dev": true - }, - "fast-levenshtein": { - "version": "2.0.6", - "resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz", - "integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=", - "dev": true - }, - "figures": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/figures/-/figures-1.7.0.tgz", - "integrity": "sha1-y+Hjr/zxzUS4DK3+0o3Hk6lwHS4=", - "dev": true, - "requires": { - "escape-string-regexp": "^1.0.5", - "object-assign": "^4.1.0" - } - }, - "file-entry-cache": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-2.0.0.tgz", - "integrity": "sha1-w5KZDD5oR4PYOLjISkXYoEhFg2E=", - "dev": true, - "requires": { - "flat-cache": "^1.2.1", - "object-assign": "^4.0.1" - } - }, - "find-up": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-2.1.0.tgz", - "integrity": "sha1-RdG35QbHF93UgndaK3eSCjwMV6c=", - "dev": true, - "requires": { - "locate-path": "^2.0.0" - } - }, - "flat-cache": { - "version": "1.3.4", - "resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-1.3.4.tgz", - "integrity": "sha512-VwyB3Lkgacfik2vhqR4uv2rvebqmDvFu4jlN/C1RzWoJEo8I7z4Q404oiqYCkq41mni8EzQnm95emU9seckwtg==", - "dev": true, - "requires": { - "circular-json": "^0.3.1", - "graceful-fs": "^4.1.2", - "rimraf": "~2.6.2", - "write": "^0.2.1" - } - }, - "fs.realpath": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", - "dev": true - }, - "function-bind": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", - "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", - "dev": true - }, - "generate-function": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz", - "integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==", - "dev": true, - "requires": { - "is-property": "^1.0.2" - } - }, - "generate-object-property": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/generate-object-property/-/generate-object-property-1.2.0.tgz", - "integrity": "sha1-nA4cQDCM6AT0eDYYuTf6iPmdUNA=", - "dev": true, - "requires": { - "is-property": "^1.0.0" - } - }, - "glob": { - "version": "7.1.4", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.4.tgz", - "integrity": "sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A==", - "dev": true, - "requires": { - "fs.realpath": "^1.0.0", - "inflight": "^1.0.4", - "inherits": "2", - "minimatch": "^3.0.4", - "once": "^1.3.0", - "path-is-absolute": "^1.0.0" - } - }, - "globals": { - "version": "9.18.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-9.18.0.tgz", - "integrity": "sha512-S0nG3CLEQiY/ILxqtztTWH/3iRRdyBLw6KMDxnKMchrtbj2OFmehVh0WUCfW3DUrIgx/qFrJPICrq4Z4sTR9UQ==", - "dev": true - }, - "graceful-fs": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.0.tgz", - "integrity": "sha512-jpSvDPV4Cq/bgtpndIWbI5hmYxhQGHPC4d4cqBPb4DLniCfhJokdXhwhaDuLBGLQdvvRum/UiX6ECVIPvDXqdg==", - "dev": true - }, - "has": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", - "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", - "dev": true, - "requires": { - "function-bind": "^1.1.1" - } - }, - "has-ansi": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", - "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=", - "dev": true, - "requires": { - "ansi-regex": "^2.0.0" - } - }, - "hosted-git-info": { - "version": "2.7.1", - "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.7.1.tgz", - "integrity": "sha512-7T/BxH19zbcCTa8XkMlbK5lTo1WtgkFi3GvdWEyNuc4Vex7/9Dqbnpsf4JMydcfj9HCg4zUWFTL3Za6lapg5/w==", - "dev": true - }, - "ignore": { - "version": "3.3.10", - "resolved": "https://registry.npmjs.org/ignore/-/ignore-3.3.10.tgz", - "integrity": "sha512-Pgs951kaMm5GXP7MOvxERINe3gsaVjUWFm+UZPSq9xYriQAksyhg0csnS0KXSNRD5NmNdapXEpjxG49+AKh/ug==", - "dev": true - }, - "imurmurhash": { - "version": "0.1.4", - "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", - "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=", - "dev": true - }, - "inflight": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", - "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", - "dev": true, - "requires": { - "once": "^1.3.0", - "wrappy": "1" - } - }, - "inherits": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true - }, - "inquirer": { - "version": "0.12.0", - "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-0.12.0.tgz", - "integrity": "sha1-HvK/1jUE3wvHV4X/+MLEHfEvB34=", - "dev": true, - "requires": { - "ansi-escapes": "^1.1.0", - "ansi-regex": "^2.0.0", - "chalk": "^1.0.0", - "cli-cursor": "^1.0.1", - "cli-width": "^2.0.0", - "figures": "^1.3.5", - "lodash": "^4.3.0", - "readline2": "^1.0.1", - "run-async": "^0.1.0", - "rx-lite": "^3.1.2", - "string-width": "^1.0.1", - "strip-ansi": "^3.0.0", - "through": "^2.3.6" - } - }, - "interpret": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.2.0.tgz", - "integrity": "sha512-mT34yGKMNceBQUoVn7iCDKDntA7SC6gycMAWzGx1z/CMCTV7b2AAtXlo3nRyHZ1FelRkQbQjprHSYGwzLtkVbw==", - "dev": true - }, - "is-arrayish": { - "version": "0.2.1", - "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz", - "integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=", - "dev": true - }, - "is-fullwidth-code-point": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", - "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", - "dev": true, - "requires": { - "number-is-nan": "^1.0.0" - } - }, - "is-my-ip-valid": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-my-ip-valid/-/is-my-ip-valid-1.0.0.tgz", - "integrity": "sha512-gmh/eWXROncUzRnIa1Ubrt5b8ep/MGSnfAUI3aRp+sqTCs1tv1Isl8d8F6JmkN3dXKc3ehZMrtiPN9eL03NuaQ==", - "dev": true - }, - "is-my-json-valid": { - "version": "2.20.0", - "resolved": "https://registry.npmjs.org/is-my-json-valid/-/is-my-json-valid-2.20.0.tgz", - "integrity": "sha512-XTHBZSIIxNsIsZXg7XB5l8z/OBFosl1Wao4tXLpeC7eKU4Vm/kdop2azkPqULwnfGQjmeDIyey9g7afMMtdWAA==", - "dev": true, - "requires": { - "generate-function": "^2.0.0", - "generate-object-property": "^1.1.0", - "is-my-ip-valid": "^1.0.0", - "jsonpointer": "^4.0.0", - "xtend": "^4.0.0" - } - }, - "is-property": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", - "integrity": "sha1-V/4cTkhHTt1lsJkR8msc1Ald2oQ=", - "dev": true - }, - "is-resolvable": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/is-resolvable/-/is-resolvable-1.1.0.tgz", - "integrity": "sha512-qgDYXFSR5WvEfuS5dMj6oTMEbrrSaM0CrFk2Yiq/gXnBvD9pMa2jGXxyhGLfvhZpuMZe18CJpFxAt3CRs42NMg==", - "dev": true - }, - "isarray": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", - "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", - "dev": true - }, - "js-tokens": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz", - "integrity": "sha1-mGbfOVECEw449/mWvOtlRDIJwls=", - "dev": true - }, - "js-yaml": { - "version": "3.13.1", - "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", - "integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==", - "dev": true, - "requires": { - "argparse": "^1.0.7", - "esprima": "^4.0.0" - } - }, - "json-stable-stringify": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/json-stable-stringify/-/json-stable-stringify-1.0.1.tgz", - "integrity": "sha1-mnWdOcXy/1A/1TAGRu1EX4jE+a8=", - "dev": true, - "requires": { - "jsonify": "~0.0.0" - } - }, - "jsonify": { - "version": "0.0.0", - "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", - "integrity": "sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=", - "dev": true - }, - "jsonpointer": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/jsonpointer/-/jsonpointer-4.0.1.tgz", - "integrity": "sha1-T9kss04OnbPInIYi7PUfm5eMbLk=", - "dev": true - }, - "levn": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/levn/-/levn-0.3.0.tgz", - "integrity": "sha1-OwmSTt+fCDwEkP3UwLxEIeBHZO4=", - "dev": true, - "requires": { - "prelude-ls": "~1.1.2", - "type-check": "~0.3.2" - } - }, - "load-json-file": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-2.0.0.tgz", - "integrity": "sha1-eUfkIUmvgNaWy/eXvKq8/h/inKg=", - "dev": true, - "requires": { - "graceful-fs": "^4.1.2", - "parse-json": "^2.2.0", - "pify": "^2.0.0", - "strip-bom": "^3.0.0" - } - }, - "locate-path": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-2.0.0.tgz", - "integrity": "sha1-K1aLJl7slExtnA3pw9u7ygNUzY4=", - "dev": true, - "requires": { - "p-locate": "^2.0.0", - "path-exists": "^3.0.0" - } - }, - "lodash": { - "version": "4.17.15", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", - "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", - "dev": true - }, - "lodash.cond": { - "version": "4.5.2", - "resolved": "https://registry.npmjs.org/lodash.cond/-/lodash.cond-4.5.2.tgz", - "integrity": "sha1-9HGh2khr5g9quVXRcRVSPdHSVdU=", - "dev": true - }, - "minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, - "requires": { - "brace-expansion": "^1.1.7" - } - }, - "minimist": { - "version": "0.0.8", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", - "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", - "dev": true - }, - "mkdirp": { - "version": "0.5.1", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", - "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", - "dev": true, - "requires": { - "minimist": "0.0.8" - } - }, - "ms": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", - "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", - "dev": true - }, - "mute-stream": { - "version": "0.0.5", - "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.5.tgz", - "integrity": "sha1-j7+rsKmKJT0xhDMfno3rc3L6xsA=", - "dev": true - }, - "natural-compare": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", - "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", - "dev": true - }, - "next-tick": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.0.0.tgz", - "integrity": "sha1-yobR/ogoFpsBICCOPchCS524NCw=", - "dev": true - }, - "normalize-package-data": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.5.0.tgz", - "integrity": "sha512-/5CMN3T0R4XTj4DcGaexo+roZSdSFW/0AOOTROrjxzCG1wrWXEsGbRKevjlIL+ZDE4sZlJr5ED4YW0yqmkK+eA==", - "dev": true, - "requires": { - "hosted-git-info": "^2.1.4", - "resolve": "^1.10.0", - "semver": "2 || 3 || 4 || 5", - "validate-npm-package-license": "^3.0.1" - } - }, - "number-is-nan": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", - "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=", - "dev": true - }, - "object-assign": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", - "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=", - "dev": true - }, - "once": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", - "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "dev": true, - "requires": { - "wrappy": "1" - } - }, - "onetime": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz", - "integrity": "sha1-ofeDj4MUxRbwXs78vEzP4EtO14k=", - "dev": true - }, - "optionator": { - "version": "0.8.2", - "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.8.2.tgz", - "integrity": "sha1-NkxeQJ0/TWMB1sC0wFu6UBgK62Q=", - "dev": true, - "requires": { - "deep-is": "~0.1.3", - "fast-levenshtein": "~2.0.4", - "levn": "~0.3.0", - "prelude-ls": "~1.1.2", - "type-check": "~0.3.2", - "wordwrap": "~1.0.0" - } - }, - "os-homedir": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz", - "integrity": "sha1-/7xJiDNuDoM94MFox+8VISGqf7M=", - "dev": true - }, - "p-limit": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-1.3.0.tgz", - "integrity": "sha512-vvcXsLAJ9Dr5rQOPk7toZQZJApBl2K4J6dANSsEuh6QI41JYcsS/qhTGa9ErIUUgK3WNQoJYvylxvjqmiqEA9Q==", - "dev": true, - "requires": { - "p-try": "^1.0.0" - } - }, - "p-locate": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-2.0.0.tgz", - "integrity": "sha1-IKAQOyIqcMj9OcwuWAaA893l7EM=", - "dev": true, - "requires": { - "p-limit": "^1.1.0" - } - }, - "p-try": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/p-try/-/p-try-1.0.0.tgz", - "integrity": "sha1-y8ec26+P1CKOE/Yh8rGiN8GyB7M=", - "dev": true - }, - "parse-json": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-2.2.0.tgz", - "integrity": "sha1-9ID0BDTvgHQfhGkJn43qGPVaTck=", - "dev": true, - "requires": { - "error-ex": "^1.2.0" - } - }, - "path-exists": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", - "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", - "dev": true - }, - "path-is-absolute": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", - "dev": true - }, - "path-is-inside": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/path-is-inside/-/path-is-inside-1.0.2.tgz", - "integrity": "sha1-NlQX3t5EQw0cEa9hAn+s8HS9/FM=", - "dev": true - }, - "path-parse": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", - "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", - "dev": true - }, - "path-type": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/path-type/-/path-type-2.0.0.tgz", - "integrity": "sha1-8BLMuEFbcJb8LaoQVMPXI4lZTHM=", - "dev": true, - "requires": { - "pify": "^2.0.0" - } - }, - "pify": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", - "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=", - "dev": true - }, - "pkg-dir": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-2.0.0.tgz", - "integrity": "sha1-9tXREJ4Z1j7fQo4L1X4Sd3YVM0s=", - "dev": true, - "requires": { - "find-up": "^2.1.0" - } - }, - "pluralize": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-1.2.1.tgz", - "integrity": "sha1-0aIUg/0iu0HlihL6NCGCMUCJfEU=", - "dev": true - }, - "prelude-ls": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz", - "integrity": "sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=", - "dev": true - }, - "process-nextick-args": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", - "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", - "dev": true - }, - "progress": { - "version": "1.1.8", - "resolved": "https://registry.npmjs.org/progress/-/progress-1.1.8.tgz", - "integrity": "sha1-4mDHj2Fhzdmw5WzD4Khd4Xx6V74=", - "dev": true - }, - "read-pkg": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-2.0.0.tgz", - "integrity": "sha1-jvHAYjxqbbDcZxPEv6xGMysjaPg=", - "dev": true, - "requires": { - "load-json-file": "^2.0.0", - "normalize-package-data": "^2.3.2", - "path-type": "^2.0.0" - } - }, - "read-pkg-up": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/read-pkg-up/-/read-pkg-up-2.0.0.tgz", - "integrity": "sha1-a3KoBImE4MQeeVEP1en6mbO1Sb4=", - "dev": true, - "requires": { - "find-up": "^2.0.0", - "read-pkg": "^2.0.0" - } - }, - "readable-stream": { - "version": "2.3.6", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", - "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", - "dev": true, - "requires": { - "core-util-is": "~1.0.0", - "inherits": "~2.0.3", - "isarray": "~1.0.0", - "process-nextick-args": "~2.0.0", - "safe-buffer": "~5.1.1", - "string_decoder": "~1.1.1", - "util-deprecate": "~1.0.1" - } - }, - "readline2": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/readline2/-/readline2-1.0.1.tgz", - "integrity": "sha1-QQWWCP/BVHV7cV2ZidGZ/783LjU=", - "dev": true, - "requires": { - "code-point-at": "^1.0.0", - "is-fullwidth-code-point": "^1.0.0", - "mute-stream": "0.0.5" - } - }, - "rechoir": { - "version": "0.6.2", - "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", - "integrity": "sha1-hSBLVNuoLVdC4oyWdW70OvUOM4Q=", - "dev": true, - "requires": { - "resolve": "^1.1.6" - } - }, - "require-uncached": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/require-uncached/-/require-uncached-1.0.3.tgz", - "integrity": "sha1-Tg1W1slmL9MeQwEcS5WqSZVUIdM=", - "dev": true, - "requires": { - "caller-path": "^0.1.0", - "resolve-from": "^1.0.0" - } - }, - "resolve": { - "version": "1.11.1", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.11.1.tgz", - "integrity": "sha512-vIpgF6wfuJOZI7KKKSP+HmiKggadPQAdsp5HiC1mvqnfp0gF1vdwgBWZIdrVft9pgqoMFQN+R7BSWZiBxx+BBw==", - "dev": true, - "requires": { - "path-parse": "^1.0.6" - } - }, - "resolve-from": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-1.0.1.tgz", - "integrity": "sha1-Jsv+k10a7uq7Kbw/5a6wHpPUQiY=", - "dev": true - }, - "restore-cursor": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-1.0.1.tgz", - "integrity": "sha1-NGYfRohjJ/7SmRR5FSJS35LapUE=", - "dev": true, - "requires": { - "exit-hook": "^1.0.0", - "onetime": "^1.0.0" - } - }, - "rimraf": { - "version": "2.6.3", - "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.3.tgz", - "integrity": "sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA==", - "dev": true, - "requires": { - "glob": "^7.1.3" - } - }, - "run-async": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/run-async/-/run-async-0.1.0.tgz", - "integrity": "sha1-yK1KXhEGYeQCp9IbUw4AnyX444k=", - "dev": true, - "requires": { - "once": "^1.3.0" - } - }, - "rx-lite": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/rx-lite/-/rx-lite-3.1.2.tgz", - "integrity": "sha1-Gc5QLKVyZl87ZHsQk5+X/RYV8QI=", - "dev": true - }, - "safe-buffer": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", - "dev": true - }, - "semver": { - "version": "5.7.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", - "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==", - "dev": true - }, - "shelljs": { - "version": "0.7.8", - "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.7.8.tgz", - "integrity": "sha1-3svPh0sNHl+3LhSxZKloMEjprLM=", - "dev": true, - "requires": { - "glob": "^7.0.0", - "interpret": "^1.0.0", - "rechoir": "^0.6.2" - } - }, - "slice-ansi": { - "version": "0.0.4", - "resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-0.0.4.tgz", - "integrity": "sha1-7b+JA/ZvfOL46v1s7tZeJkyDGzU=", - "dev": true - }, - "spdx-correct": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.1.0.tgz", - "integrity": "sha512-lr2EZCctC2BNR7j7WzJ2FpDznxky1sjfxvvYEyzxNyb6lZXHODmEoJeFu4JupYlkfha1KZpJyoqiJ7pgA1qq8Q==", - "dev": true, - "requires": { - "spdx-expression-parse": "^3.0.0", - "spdx-license-ids": "^3.0.0" - } - }, - "spdx-exceptions": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/spdx-exceptions/-/spdx-exceptions-2.2.0.tgz", - "integrity": "sha512-2XQACfElKi9SlVb1CYadKDXvoajPgBVPn/gOQLrTvHdElaVhr7ZEbqJaRnJLVNeaI4cMEAgVCeBMKF6MWRDCRA==", - "dev": true - }, - "spdx-expression-parse": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/spdx-expression-parse/-/spdx-expression-parse-3.0.0.tgz", - "integrity": "sha512-Yg6D3XpRD4kkOmTpdgbUiEJFKghJH03fiC1OPll5h/0sO6neh2jqRDVHOQ4o/LMea0tgCkbMgea5ip/e+MkWyg==", - "dev": true, - "requires": { - "spdx-exceptions": "^2.1.0", - "spdx-license-ids": "^3.0.0" - } - }, - "spdx-license-ids": { - "version": "3.0.5", - "resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-3.0.5.tgz", - "integrity": "sha512-J+FWzZoynJEXGphVIS+XEh3kFSjZX/1i9gFBaWQcB+/tmpe2qUsSBABpcxqxnAxFdiUFEgAX1bjYGQvIZmoz9Q==", - "dev": true - }, - "sprintf-js": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", - "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", - "dev": true - }, - "string-width": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", - "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", - "dev": true, - "requires": { - "code-point-at": "^1.0.0", - "is-fullwidth-code-point": "^1.0.0", - "strip-ansi": "^3.0.0" - } - }, - "string_decoder": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", - "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", - "dev": true, - "requires": { - "safe-buffer": "~5.1.0" - } - }, - "strip-ansi": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", - "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", - "dev": true, - "requires": { - "ansi-regex": "^2.0.0" - } - }, - "strip-bom": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", - "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", - "dev": true - }, - "strip-json-comments": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", - "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=", - "dev": true - }, - "supports-color": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", - "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=", - "dev": true - }, - "table": { - "version": "3.8.3", - "resolved": "https://registry.npmjs.org/table/-/table-3.8.3.tgz", - "integrity": "sha1-K7xULw/amGGnVdOUf+/Ys/UThV8=", - "dev": true, - "requires": { - "ajv": "^4.7.0", - "ajv-keywords": "^1.0.0", - "chalk": "^1.1.1", - "lodash": "^4.0.0", - "slice-ansi": "0.0.4", - "string-width": "^2.0.0" - }, - "dependencies": { - "ansi-regex": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", - "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=", - "dev": true - }, - "is-fullwidth-code-point": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", - "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=", - "dev": true - }, - "string-width": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", - "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", - "dev": true, - "requires": { - "is-fullwidth-code-point": "^2.0.0", - "strip-ansi": "^4.0.0" - } - }, - "strip-ansi": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", - "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=", - "dev": true, - "requires": { - "ansi-regex": "^3.0.0" - } - } - } - }, - "text-table": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz", - "integrity": "sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=", - "dev": true - }, - "through": { - "version": "2.3.8", - "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", - "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=", - "dev": true - }, - "type": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/type/-/type-1.0.1.tgz", - "integrity": "sha512-MAM5dBMJCJNKs9E7JXo4CXRAansRfG0nlJxW7Wf6GZzSOvH31zClSaHdIMWLehe/EGMBkqeC55rrkaOr5Oo7Nw==", - "dev": true - }, - "type-check": { - "version": "0.3.2", - "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.3.2.tgz", - "integrity": "sha1-WITKtRLPHTVeP7eE8wgEsrUg23I=", - "dev": true, - "requires": { - "prelude-ls": "~1.1.2" - } - }, - "typedarray": { - "version": "0.0.6", - "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", - "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=", - "dev": true - }, - "user-home": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/user-home/-/user-home-2.0.0.tgz", - "integrity": "sha1-nHC/2Babwdy/SGBODwS4tJzenp8=", - "dev": true, - "requires": { - "os-homedir": "^1.0.0" - } - }, - "util-deprecate": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", - "dev": true - }, - "validate-npm-package-license": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz", - "integrity": "sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew==", - "dev": true, - "requires": { - "spdx-correct": "^3.0.0", - "spdx-expression-parse": "^3.0.0" - } - }, - "wordwrap": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz", - "integrity": "sha1-J1hIEIkUVqQXHI0CJkQa3pDLyus=", - "dev": true - }, - "wrappy": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "dev": true - }, - "write": { - "version": "0.2.1", - "resolved": "https://registry.npmjs.org/write/-/write-0.2.1.tgz", - "integrity": "sha1-X8A4KOJkzqP+kUVUdvejxWbLB1c=", - "dev": true, - "requires": { - "mkdirp": "^0.5.1" - } - }, - "xtend": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", - "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", - "dev": true - } - } -} From 2941f8cf22841a19be2aa95c4ee072101e2acd96 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 26 Aug 2019 07:02:12 -0600 Subject: [PATCH 014/166] Got all the tests to pass --- tests/tests.js | 228 +++++++++++++++++++++++-------------------------- 1 file changed, 108 insertions(+), 120 deletions(-) diff --git a/tests/tests.js b/tests/tests.js index 7975d93..81e529b 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -17,12 +17,6 @@ exports.defineAutoTests = function () { describe('chrome.cast', function () { - var _session = null; - var _receiverAvailability = []; - var _sessionUpdatedFired = false; - var _mediaUpdatedFired = false; - var media; - it('SPEC_00100 should contain definitions', function () { expect(chrome.cast.VERSION).toBeDefined(); expect(chrome.cast.ReceiverAvailability).toBeDefined(); @@ -200,22 +194,25 @@ exports.defineAutoTests = function () { it('SPEC_01000 Test valid session', function (done) { alert('---TEST INSTRUCTION---\nPlease select a valid chromecast in the next dialog.'); + + function handleErr (err) { + console.error(err); + console.log(new Error().stack); + expect(err).toEqual('Should not have gotten an error at all'); + done(); + } + chrome.cast.requestSession(function (session) { - sessionProperties(session) - .then(loadMedia) - .then(done); - var updateListener = function (isAlive) { - console.log('session updated!!!!'); - _sessionUpdatedFired = true; - session.removeUpdateListener(updateListener); - }; + // // Run all the session related tests + Promise.resolve(session) + .then(sessionProperties) + .then(loadMedia) + .then(stopSession) + .then(done) + .catch(handleErr); - session.addUpdateListener(updateListener); - }, function (err) { - expect(err).toEqual('We should not get an error.'); - done(); - }); + }, handleErr); }, USER_INTERACTION_TIMEOUT); function sessionProperties (session) { @@ -242,134 +239,125 @@ exports.defineAutoTests = function () { expect(request).toBeTruthy(); session.loadMedia(request, function (media) { - expect(media instanceof chrome.cast.media.Media).toBeTruthy(); - expect(media.sessionId).toEqual(session.sessionId); - expect(media.addUpdateListener).toBeDefined(); - expect(media.removeUpdateListener).toBeDefined(); - - var updateListener = function () { - _mediaUpdatedFired = true; - media.removeUpdateListener(updateListener); - }; - - media.addUpdateListener(updateListener); - - resolve(session); - }, function (err) { - expect(err).toEqual('We should not get an error.'); - reject(err); - }); + // Run all the media related tests + Promise.resolve({media: media, session: session}) + .then(mediaProperties) + .then(pauseSuccess) + .then(playSuccess) + .then(seekSuccess) + .then(setVolumeSuccess) + .then(muteVolumeSuccess) + .then(unmuteVolumeSuccess) + .then(stopSuccess) + .then(function (media) { + resolve(session); + }) + .catch(reject); + + }, reject); }); } - }); - - it('pause media should succeed', function (done) { - setTimeout(function () { - media.pause(null, function () { - console.log('pause success'); - done(); - }, function (err) { - console.log('pause error', err); - expect(err).toBeNull(); - done(); + function mediaProperties (data) { + return new Promise(function (resolve, reject) { + var media = data.media; + var session = data.session; + expect(media instanceof chrome.cast.media.Media).toBeTruthy(); + expect(media.sessionId).toEqual(session.sessionId); + expect(media.addUpdateListener).toBeDefined(); + expect(media.removeUpdateListener).toBeDefined(); + resolve(media); }); - }, 5000); - }); + } - it('play media should succeed', function (done) { - setTimeout(function () { - media.play(null, function () { - console.log('play success'); - done(); - }, function (err) { - console.log('play error', err); - expect(err).toBeNull(); - done(); + function pauseSuccess (media) { + return new Promise(function (resolve, reject) { + setTimeout(function () { + media.pause(null, function () { + resolve(media); + }, reject); + }, 500); }); - }, 1000); - }); - - it('seek media should succeed', function (done) { - setTimeout(function () { - var request = new chrome.cast.media.SeekRequest(); - request.currentTime = 10; + } - media.seek(request, function () { - done(); - }, function (err) { - expect(err).toBeNull(); - done(); + function playSuccess (media) { + return new Promise(function (resolve, reject) { + setTimeout(function () { + media.play(null, function () { + resolve(media); + }, reject); + }, 500); }); - }, 1000); - }); - - it('session updateListener', function (done) { - expect(_sessionUpdatedFired).toEqual(true); - done(); - }); + } - it('media updateListener', function (done) { - expect(_mediaUpdatedFired).toEqual(true); - done(); - }); + function seekSuccess (media) { + return new Promise(function (resolve, reject) { + setTimeout(function () { + var request = new chrome.cast.media.SeekRequest(); + request.currentTime = 3; + media.seek(request, function () { + resolve(media); + }, reject); + }, 500); + }); + } - it('volume and muting', function (done) { - var volume = new chrome.cast.Volume(); - volume.level = 0.5; + function setVolumeSuccess (media) { + return new Promise(function (resolve, reject) { + var volume = new chrome.cast.Volume(); + volume.level = 0.2; - var request = new chrome.cast.media.VolumeRequest(); - request.volume = volume; + var request = new chrome.cast.media.VolumeRequest(); + request.volume = volume; - media.setVolume(request, function () { + media.setVolume(request, function () { + resolve(media); + }, reject); + }); + } - var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, true)); - media.setVolume(request, function () { + function muteVolumeSuccess (media) { + return new Promise(function (resolve, reject) { + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, true)); + media.setVolume(request, function () { + resolve(media); + }, reject); + }); + } + function unmuteVolumeSuccess (media) { + return new Promise(function (resolve, reject) { var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, false)); media.setVolume(request, function () { - done(); - }, function (err) { - expect(err).toBeNull(); - done(); - }); - - }, function (err) { - expect(err).toBeNull(); - done(); + resolve(media); + }, reject); }); + } - }, function (err) { - expect(err).toBeNull(); - done(); - }); - - }); + function stopSuccess (media) { + return new Promise(function (resolve, reject) { + media.stop(null, function () { + resolve(media); + }, reject); + }); + } - it('stopping the video', function (done) { - media.stop(null, function () { - setTimeout(done, 1000); - }, function (err) { - expect(err).toBeNull(); - done(); - }); - }); + function stopSession (session) { + return new Promise(function (resolve, reject) { + session.stop(function () { + resolve(session); + }, reject); + }); + } - it('unloading the session', function (done) { - _session.stop(function () { - done(); - }, function (err) { - expect(err).toBeNull(); - done(); - }); }); it('SPEC_01200 should pass auto tests on second run', function () { alert('---TEST INSTRUCTION---\nPlease hit "Reset App" at the top and ensure all ' + 'tests pass again. (This simulates navigation to a new page where the ' + 'plugin is not loaded from scratch again).'); - expect('succes').toBeDefined(); + expect('success').toBeDefined(); }); }); From 7812934bddd4f8180f2580255ecc865d246303e8 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 26 Aug 2019 07:47:10 -0600 Subject: [PATCH 015/166] remove receiverAvailable because it does nothing stop spamming emaiAllRoutes --- www/chrome.cast.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 5441ccd..017536d 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -535,6 +535,8 @@ _routeListEl.classList.add('route-list'); var _routeList = {}; var _routeRefreshInterval = null; +var _receiverAvailable = false; + /** * Initializes the API. Note that either successCallback and errorCallback will be invoked once the API has finished initialization. * The sessionListener and receiverListener may be invoked at any time afterwards, and possibly more than once. @@ -557,13 +559,6 @@ chrome.cast.initialize = function (apiConfig, successCallback, errorCallback) { execute('initialize', _sessionRequest.appId, _autoJoinPolicy, _defaultActionPolicy, function (err) { if (!err) { successCallback(); - - clearInterval(_routeRefreshInterval); - _routeRefreshInterval = setInterval(function () { - execute('emitAllRoutes'); - }, 15000); - - setTimeout(function () { execute('emitAllRoutes'); }, 2000); } else { handleError(err, errorCallback); } @@ -792,7 +787,7 @@ chrome.cast.Session.prototype.loadMedia = function (loadRequest, successCallback _currentMedia.media.tracks = []; obj.media.tracks.forEach((track) => { - let newTrack = new chrome.cast.media.Track(track.trackId, track.type); + var newTrack = new chrome.cast.media.Track(track.trackId, track.type); newTrack.customData = track.customData || null; newTrack.language = track.language || null; newTrack.name = track.name || null; @@ -1199,9 +1194,11 @@ chrome.cast._emitConnecting = function () { chrome.cast._ = { receiverUnavailable: function () { _receiverListener(chrome.cast.ReceiverAvailability.UNAVAILABLE); + _receiverAvailable = false; }, receiverAvailable: function () { _receiverListener(chrome.cast.ReceiverAvailability.AVAILABLE); + _receiverAvailable = true; }, routeAdded: function (route) { if (!_routeList[route.id]) { From 2cda23724c332191eb08765b8e585f6cf63235b6 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 26 Aug 2019 07:49:27 -0600 Subject: [PATCH 016/166] Version bump to 1.0.0 --- plugin.xml | 2 +- tests/package.json | 2 +- tests/plugin.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin.xml b/plugin.xml index 94de1dc..443d760 100644 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="1.0.0"> diff --git a/tests/package.json b/tests/package.json index 88536ec..55ffe2c 100644 --- a/tests/package.json +++ b/tests/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-chromecast-tests", - "version": "0.0.0", + "version": "1.0.0", "description": "", "author": "", "license": "Apache 2.0", diff --git a/tests/plugin.xml b/tests/plugin.xml index 27fd886..a50628d 100644 --- a/tests/plugin.xml +++ b/tests/plugin.xml @@ -20,7 +20,7 @@ xmlns:rim="http://www.blackberry.com/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-chromecast-tests" - version="2.0.4-dev"> + version="1.0.0"> Cordova Chromecast Plugin Tests Apache 2.0 From b64e836fbbb6ffe6b51d0533a6ade2562cc4c547 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 28 Aug 2019 03:54:32 -0600 Subject: [PATCH 017/166] Should make sure assets are in the plugin folder so that we don't pollute the top level of www --- tests/plugin.xml | 2 +- tests/tests.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/plugin.xml b/tests/plugin.xml index a50628d..cf46f53 100644 --- a/tests/plugin.xml +++ b/tests/plugin.xml @@ -27,5 +27,5 @@ - + diff --git a/tests/tests.js b/tests/tests.js index 81e529b..ca223e9 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -13,7 +13,7 @@ exports.defineAutoTests = function () { var applicationID_default = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID; var applicationID_custom = 'F5EEDC6C'; - var videoUrl = location.origin + '/res/test.mp4'; + var videoUrl = location.origin + '/plugins/cordova-plugin-chromecast-tests/res/test.mp4'; describe('chrome.cast', function () { From 922908fe396d712c3702040d21feba89cef5ba2e Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 28 Aug 2019 04:27:38 -0600 Subject: [PATCH 018/166] reject and catch don't work great because jasmine does not give very good stack trace. So remove them and check error as soon as possible to the criminal code. --- tests/tests.js | 92 +++++++++++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/tests/tests.js b/tests/tests.js index ca223e9..5e9c66b 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -103,7 +103,7 @@ exports.defineAutoTests = function () { expect('success').toBeDefined(); done(); }, function (err) { - expect(err).toBe(null); + expect(err).toEqual('no_error_no'); done(); }); }); @@ -156,7 +156,7 @@ exports.defineAutoTests = function () { expect('success').toBeTruthy(); done(); }, function (err) { - expect(err).toBe(null); + expect(err).toEqual('no_error_no'); done(); }); }); @@ -195,28 +195,24 @@ exports.defineAutoTests = function () { it('SPEC_01000 Test valid session', function (done) { alert('---TEST INSTRUCTION---\nPlease select a valid chromecast in the next dialog.'); - function handleErr (err) { - console.error(err); - console.log(new Error().stack); - expect(err).toEqual('Should not have gotten an error at all'); - done(); - } - chrome.cast.requestSession(function (session) { - // // Run all the session related tests + // Run all the session related tests Promise.resolve(session) .then(sessionProperties) .then(loadMedia) .then(stopSession) - .then(done) - .catch(handleErr); + .then(done); + + }, function (err) { + expect(err).toEqual('no_error_no'); + done(); + }); - }, handleErr); }, USER_INTERACTION_TIMEOUT); function sessionProperties (session) { - return new Promise(function (resolve, reject) { + return new Promise(function (resolve) { expect(session instanceof chrome.cast.Session).toBeTruthy(); expect(session.appId).toBeDefined(); expect(session.displayName).toBeDefined(); @@ -231,7 +227,7 @@ exports.defineAutoTests = function () { } function loadMedia (session) { - return new Promise(function (resolve, reject) { + return new Promise(function (resolve) { var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); expect(mediaInfo).toBeTruthy(); @@ -252,15 +248,17 @@ exports.defineAutoTests = function () { .then(stopSuccess) .then(function (media) { resolve(session); - }) - .catch(reject); + }); - }, reject); + }, function (err) { + expect(err).toEqual('no_error_no'); + resolve(); + }); }); } function mediaProperties (data) { - return new Promise(function (resolve, reject) { + return new Promise(function (resolve) { var media = data.media; var session = data.session; expect(media instanceof chrome.cast.media.Media).toBeTruthy(); @@ -272,39 +270,48 @@ exports.defineAutoTests = function () { } function pauseSuccess (media) { - return new Promise(function (resolve, reject) { + return new Promise(function (resolve) { setTimeout(function () { media.pause(null, function () { resolve(media); - }, reject); + }, function (err) { + expect(err).toEqual('no_error_no'); + resolve(); + }); }, 500); }); } function playSuccess (media) { - return new Promise(function (resolve, reject) { + return new Promise(function (resolve) { setTimeout(function () { media.play(null, function () { resolve(media); - }, reject); + }, function (err) { + expect(err).toEqual('no_error_no'); + resolve(); + }); }, 500); }); } function seekSuccess (media) { - return new Promise(function (resolve, reject) { + return new Promise(function (resolve) { setTimeout(function () { var request = new chrome.cast.media.SeekRequest(); request.currentTime = 3; media.seek(request, function () { resolve(media); - }, reject); + }, function (err) { + expect(err).toEqual('no_error_no'); + resolve(); + }); }, 500); }); } function setVolumeSuccess (media) { - return new Promise(function (resolve, reject) { + return new Promise(function (resolve) { var volume = new chrome.cast.Volume(); volume.level = 0.2; @@ -313,41 +320,56 @@ exports.defineAutoTests = function () { media.setVolume(request, function () { resolve(media); - }, reject); + }, function (err) { + expect(err).toEqual('no_error_no'); + resolve(); + }); }); } function muteVolumeSuccess (media) { - return new Promise(function (resolve, reject) { + return new Promise(function (resolve) { var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, true)); media.setVolume(request, function () { resolve(media); - }, reject); + }, function (err) { + expect(err).toEqual('no_error_no'); + resolve(); + }); }); } function unmuteVolumeSuccess (media) { - return new Promise(function (resolve, reject) { + return new Promise(function (resolve) { var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, false)); media.setVolume(request, function () { resolve(media); - }, reject); + }, function (err) { + expect(err).toEqual('no_error_no'); + resolve(); + }); }); } function stopSuccess (media) { - return new Promise(function (resolve, reject) { + return new Promise(function (resolve) { media.stop(null, function () { resolve(media); - }, reject); + }, function (err) { + expect(err).toEqual('no_error_no'); + resolve(); + }); }); } function stopSession (session) { - return new Promise(function (resolve, reject) { + return new Promise(function (resolve) { session.stop(function () { resolve(session); - }, reject); + }, function (err) { + expect(err).toEqual('no_error_no'); + resolve(); + }); }); } From d4058fe659862b1a7a9c42d9e91a759c22a631bd Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 28 Aug 2019 04:30:39 -0600 Subject: [PATCH 019/166] Update package.json to match licensing --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 069ab41..db2ceb2 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "eslint": "node node_modules/eslint/bin/eslint src && node node_modules/eslint/bin/eslint www && node node_modules/eslint/bin/eslint tests" }, "author": "", - "license": "GPL-2.0-only", + "license": "dual GPLv3/MPLv2", "readme": "README.md", "description": "README.md", "devDependencies": { From 9ae454baeb955991af18983875d51e308fc19cb3 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 28 Aug 2019 04:44:25 -0600 Subject: [PATCH 020/166] Fix on requestSession cancel handling to match the documentation and chrome desktop chromecast behavior. We are supposed to receive a callback to the error handler. Not have it silenced. This is important if you have started connection animations. If you never receive a callback you don't know when to stop the animations, and whether to transition back to the unconnected state or to a connected stated. If you wish to ignore the cancel error, just filter it out in your error callback. eg. if (err.code === 'cancel') { //ignore } --- src/android/Chromecast.java | 2 +- tests/tests.js | 11 +++++++++++ www/chrome.cast.js | 5 +---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 1f4b5ef..748ba4b 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -188,7 +188,7 @@ void onConnect(RouteInfo route) { void onError(String errorCode) { super.onError(errorCode); if (errorCode.equals("CANCEL")) { - callbackContext.success("cancel"); + callbackContext.error("cancel"); } else { callbackContext.error(errorCode); } diff --git a/tests/tests.js b/tests/tests.js index 5e9c66b..a603d5e 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -190,6 +190,17 @@ exports.defineAutoTests = function () { }); }, USER_INTERACTION_TIMEOUT); + it('requestSession click outside of dialog should return the cancel error', function (done) { + alert('---TEST INSTRUCTION---\nPlease click outside of the next dialog to dismiss it.'); + chrome.cast.requestSession(function () { + fail('We should not reach here on dismiss'); + }, function (err) { + expect(err instanceof chrome.cast.Error).toBeTruthy(); + expect(err.code).toBe(chrome.cast.ErrorCode.CANCEL); + done(); + }); + }, USER_INTERACTION_TIMEOUT); + describe('Everything Session', function () { it('SPEC_01000 Test valid session', function (done) { diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 017536d..41abc7f 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -581,9 +581,6 @@ chrome.cast.requestSession = function (successCallback, errorCallback, opt_sessi execute('requestSession', function (err, obj) { if (!err) { - if (obj === 'cancel') { - return; - } var sessionId = obj.sessionId; var appId = obj.appId; var displayName = obj.displayName; @@ -1310,7 +1307,7 @@ function handleError (err, callback) { errorDescription = 'A session could not be created, or a session was invalid.'; } - var error = new Error(errorCode, errorDescription, errorData); + var error = new chrome.cast.Error(errorCode, errorDescription, errorData); if (callback) { callback(error); } From e9dcf7690691bd531084ac39dd5402f60daa4cd5 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 28 Aug 2019 05:09:44 -0600 Subject: [PATCH 021/166] Forgot to remove unused references to receiverAvailable related vars. --- www/chrome.cast.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 41abc7f..44ba370 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -533,9 +533,6 @@ var _currentMedia = null; var _routeListEl = document.createElement('ul'); _routeListEl.classList.add('route-list'); var _routeList = {}; -var _routeRefreshInterval = null; - -var _receiverAvailable = false; /** * Initializes the API. Note that either successCallback and errorCallback will be invoked once the API has finished initialization. @@ -1191,11 +1188,9 @@ chrome.cast._emitConnecting = function () { chrome.cast._ = { receiverUnavailable: function () { _receiverListener(chrome.cast.ReceiverAvailability.UNAVAILABLE); - _receiverAvailable = false; }, receiverAvailable: function () { _receiverListener(chrome.cast.ReceiverAvailability.AVAILABLE); - _receiverAvailable = true; }, routeAdded: function (route) { if (!_routeList[route.id]) { From c921583693962c5551d632c60d64f02a0c616842 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 28 Aug 2019 05:19:00 -0600 Subject: [PATCH 022/166] Update readme --- .github/pull_request_template.md | 2 +- README.md | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index bfc48ec..dee554e 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -28,6 +28,6 @@ Thanks! - [ ] I've updated the documentation as necessary - [ ] If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct [keyword to close issues using keywords](https://help.github.com/articles/closing-issues-using-keywords/)) -- [ ] I've run `npm run test` and no errors were found +- [ ] I've run `npm test` and no errors were found - [ ] I've run the `test-framework` tests for Android (See Readme) - [ ] I added automated test coverage as appropriate for this change \ No newline at end of file diff --git a/README.md b/README.md index 3784c45..bf5d845 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,11 @@ The project is now pretty much feature complete - the only things that will poss * With admin permission run `cordova plugin add --link ` * This links the plugin's **java** files directly to the Android platform. So you can modify the files from Android studio and re-deploy from there. * Unfortunately it does **not** link the js files. -* To update the js files you must run +* To update the js files you must run: * `cordova plugin remove ` * `cordova plugin add --link ` * Don't forget the admin permission + * Or, you can follow these [hot reloading js instructions](https://github.com/miloproductionsinc/cordova-testing#hot-reload-js) ## Formatting @@ -40,6 +41,12 @@ The project is now pretty much feature complete - the only things that will poss ## Testing +**1)** + +Run `npm test` to ensure your code fits the styling. It will also pick some errors. + +**2)** + This plugin has [cordova-plugin-test-framework](https://github.com/apache/cordova-plugin-test-framework) tests. To run these tests you can follow [these instructions](https://github.com/miloproductionsinc/cordova-testing). @@ -50,3 +57,8 @@ NOTE: You must run these tests from a project with the package name `com.milopro * config.xml > ` Date: Fri, 30 Aug 2019 09:47:06 -0600 Subject: [PATCH 023/166] Integrate receiverListener test into initialize. Having cross-test dependencies is not great. Though, we are on our way to a monolith test this way. :/ Pick our poison I guess. --- tests/tests.js | 147 ++++++++++++++----------------------------------- 1 file changed, 40 insertions(+), 107 deletions(-) diff --git a/tests/tests.js b/tests/tests.js index a603d5e..305df57 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -83,77 +83,20 @@ exports.defineAutoTests = function () { }); it('SPEC_00200 api should be available', function (done) { - tryUntilSuccess(function () { - return chrome.cast.isAvailable; - }, done); - }); - - describe('Custom Receiver', function () { - var _customReceiverAvailability = []; - - it('SPEC_00300 initialize should succeed (custom receiver)', function (done) { - var sessionRequest = new chrome.cast.SessionRequest(applicationID_custom); - var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) { - _session = session; - }, function (available) { - _customReceiverAvailability.push(available); - }); - - chrome.cast.initialize(apiConfig, function () { - expect('success').toBeDefined(); + var interval = setInterval(function () { + if (chrome.cast.isAvailable) { + expect(chrome.cast.isAvailable).toEqual(true); + clearInterval(interval); done(); - }, function (err) { - expect(err).toEqual('no_error_no'); - done(); - }); - }); - - /** - * Pre-requisite: You must have a valid receiver (chromecast) plugged in and available. - * You must also be running this test from a project with the package name: - * com.miloproductionsinc.plugin_tests - * You can rename your project, or clone this: - * https://github.com/miloproductionsinc/cordova-testing - */ - it('SPEC_00310 receiver available (custom receiver)', function (done) { - tryUntilSuccess(function () { - - if (_customReceiverAvailability.length >= 1) { - // We should see that the receiver is unavailable always first - expect(_customReceiverAvailability[0]).toBe(chrome.cast.ReceiverAvailability.UNAVAILABLE); - return true; - } - // We need to return false until the first entry to _receiverAvailability is added - return false; - - }, function () { - - tryUntilSuccess(function () { - if (_customReceiverAvailability.length >= 2) { - // Then we should receive an available notification - expect(_customReceiverAvailability[1]).toBe(chrome.cast.ReceiverAvailability.AVAILABLE); - return true; - } - // We need to return false until the second entry to _receiverAvailability is added - return false; - - }, done); - }); - }, USER_INTERACTION_TIMEOUT); + } + }, 500); }); - it('SPEC_00400 initialize should succeed (default receiver)', function (done) { - _receiverAvailability = []; - _session = 'no_session'; - var sessionRequest = new chrome.cast.SessionRequest(applicationID_default); - var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) { - _session = session; - }, function (available) { - _receiverAvailability.push(available); - }); - + it('SPEC_00300 initialize should succeed (custom receiver)', function (done) { + var sessionRequest = new chrome.cast.SessionRequest(applicationID_custom); + var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) {}, function (available) {}); chrome.cast.initialize(apiConfig, function () { - expect('success').toBeTruthy(); + expect('success').toBeDefined(); done(); }, function (err) { expect(err).toEqual('no_error_no'); @@ -162,33 +105,39 @@ exports.defineAutoTests = function () { }); /** - * Pre-requisite: You must have a valid receiver (chromecast) plugged in and available + * Pre-requisite: You must have a valid receiver (chromecast) plugged in and available. */ - it('SPEC_00410 receiver available (default receiver)', function (done) { - tryUntilSuccess(function () { - - if (_receiverAvailability.length >= 1) { - // We should see that the receiver is unavailable always first - expect(_receiverAvailability[0]).toBe(chrome.cast.ReceiverAvailability.UNAVAILABLE); - return true; - } - // We need to return false until the first entry to _receiverAvailability is added - return false; - - }, function () { - - tryUntilSuccess(function () { - if (_receiverAvailability.length >= 2) { - // Then we should receive an available notification - expect(_receiverAvailability[1]).toBe(chrome.cast.ReceiverAvailability.AVAILABLE); - return true; + it('SPEC_00400 initialize should succeed (default receiver)', function (done) { + var step = 1; + var sessionRequest = new chrome.cast.SessionRequest(applicationID_default); + var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) {}, function receiverListener (available) { + switch (step) { + case 1: + // The first update must be unavailable + if (available === 'unavailable') { + step++; + } else { + expect(available).toEqual('unavailable'); } - // We need to return false until the second entry to _receiverAvailability is added - return false; - - }, done); + break; + case 2: + // The second step waits until we receive available + // Can receive unavailable while waiting + if (available === 'available') { + expect(available).toEqual('available'); + step++; + done(); + } else { + expect(available).toEqual('unavailable'); + } + break; + } }); - }, USER_INTERACTION_TIMEOUT); + chrome.cast.initialize(apiConfig, function () {}, function (err) { + expect(err).toEqual('no_error_no'); + done(); + }); + }); it('requestSession click outside of dialog should return the cancel error', function (done) { alert('---TEST INSTRUCTION---\nPlease click outside of the next dialog to dismiss it.'); @@ -395,19 +344,3 @@ exports.defineAutoTests = function () { }); }; - -//* ***************************************************************************************** -//* ***********************************Helper Functions************************************** -//* ***************************************************************************************** - -function tryUntilSuccess (successFn, callback, waitBetweenTries) { - waitBetweenTries = waitBetweenTries || 500; - if (successFn()) { - expect(callback).toBeDefined(); - callback(); - } else { - setTimeout(function () { - tryUntilSuccess(successFn, callback, waitBetweenTries); - }, waitBetweenTries); - } -} From 8a1348db28cf34df73a30aca4c900ee3ac0e2fda Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Thu, 5 Sep 2019 09:26:45 -0600 Subject: [PATCH 024/166] rename for next commit --- plugin.xml | 2 +- ...omecastMediaRouterManager.java => ChromecastConnection.java} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/android/{ChromecastMediaRouterManager.java => ChromecastConnection.java} (100%) diff --git a/plugin.xml b/plugin.xml index 443d760..73d3216 100644 --- a/plugin.xml +++ b/plugin.xml @@ -38,9 +38,9 @@ + - diff --git a/src/android/ChromecastMediaRouterManager.java b/src/android/ChromecastConnection.java similarity index 100% rename from src/android/ChromecastMediaRouterManager.java rename to src/android/ChromecastConnection.java From a822f61656583d51bf722c8b4c7d00aec296fffb Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sat, 7 Sep 2019 07:54:29 -0600 Subject: [PATCH 025/166] Update to cast v3 sender to remove deprecated functions. Session is automatically "rejoined" when the webview goes to a new page and calls initialize (this follows chromecast desktop sdk) Do not call sessionListener on requestSession success (this does not follow chromecast sdk desktop behavior) --- plugin.xml | 10 +- src/android/CastOptionsProvider.java | 22 + src/android/Chromecast.java | 522 +++------- src/android/ChromecastConnection.java | 411 +++++--- src/android/ChromecastException.java | 4 - src/android/ChromecastMediaController.java | 137 --- .../ChromecastMediaRouterCallback.java | 36 - .../ChromecastOnMediaUpdatedListener.java | 8 - .../ChromecastOnSessionUpdatedListener.java | 8 - src/android/ChromecastSession.java | 916 +++++++++--------- src/android/ChromecastSessionCallback.java | 10 - tests/tests.js | 172 +++- www/chrome.cast.js | 41 +- 13 files changed, 1061 insertions(+), 1236 deletions(-) create mode 100644 src/android/CastOptionsProvider.java delete mode 100644 src/android/ChromecastException.java delete mode 100644 src/android/ChromecastMediaController.java delete mode 100644 src/android/ChromecastMediaRouterCallback.java delete mode 100644 src/android/ChromecastOnMediaUpdatedListener.java delete mode 100644 src/android/ChromecastOnSessionUpdatedListener.java delete mode 100644 src/android/ChromecastSessionCallback.java diff --git a/plugin.xml b/plugin.xml index 73d3216..9c32537 100644 --- a/plugin.xml +++ b/plugin.xml @@ -30,22 +30,18 @@ + + - + - - - - - - diff --git a/src/android/CastOptionsProvider.java b/src/android/CastOptionsProvider.java new file mode 100644 index 0000000..7c42b84 --- /dev/null +++ b/src/android/CastOptionsProvider.java @@ -0,0 +1,22 @@ +package acidhax.cordova.chromecast; + +import java.util.List; + +import com.google.android.gms.cast.framework.CastOptions; +import com.google.android.gms.cast.framework.OptionsProvider; +import com.google.android.gms.cast.framework.SessionProvider; + +import android.content.Context; + +public class CastOptionsProvider implements OptionsProvider { + + @Override + public CastOptions getCastOptions(Context context) { + return new CastOptions.Builder() + .build(); + } + @Override + public List getAdditionalSessionProviders(Context context) { + return null; + } +} \ No newline at end of file diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 748ba4b..7b2b65d 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -6,7 +6,6 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; -import java.util.List; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CallbackContext; @@ -15,37 +14,33 @@ import org.json.JSONException; import org.json.JSONObject; -import android.app.Activity; -import android.content.SharedPreferences; +import android.os.Handler; +import android.util.Log; -import androidx.mediarouter.media.MediaRouter; import androidx.mediarouter.media.MediaRouter.RouteInfo; -public class Chromecast extends CordovaPlugin implements ChromecastOnMediaUpdatedListener, ChromecastOnSessionUpdatedListener { +import com.google.android.gms.cast.framework.CastSession; +import com.google.android.gms.cast.framework.media.RemoteMediaClient; - private static final String TAG = "Chromecast"; - private static final String SETTINGS_NAME = "CordovaChromecastSettings"; - - private ChromecastMediaRouterManager mMediaRouteManager; private String appId; - - private boolean autoConnect = false; - private String lastSessionId = null; - private String lastAppId = null; - - private SharedPreferences settings; +public class Chromecast extends CordovaPlugin { + private static final String TAG = "Chromecast"; - private volatile ChromecastSession currentSession; + private ChromecastConnection connection; + private volatile ChromecastSession media; @Override protected void pluginInitialize() { super.pluginInitialize(); - // Restore preferences - this.settings = this.cordova.getActivity().getSharedPreferences(SETTINGS_NAME, 0); - this.lastSessionId = settings.getString("lastSessionId", ""); - this.lastAppId = settings.getString("lastAppId", ""); - this.mMediaRouteManager = new ChromecastMediaRouterManager(this.cordova.getActivity(), new ChromecastMediaRouterCallback(this)); + this.media = new ChromecastSession(cordova.getActivity(), remoteMediaClientCallback); + this.connection = new ChromecastConnection(cordova.getActivity(), this.media, new ChromecastConnection.ConnectionListener() { + @Override + public void onDisconnected(int reason) { + sendJavascript("chrome.cast.Session.prototype._update(false, {});"); +// sendJavascript("chrome.cast._.sessionUpdated(false, " + session.toString() + ");"); + } + }); } @Override @@ -104,16 +99,6 @@ public boolean execute(String action, JSONArray args, CallbackContext cbContext) } } - private void setLastSessionId(String sessionId) { - this.lastSessionId = sessionId; - this.settings.edit().putString("lastSessionId", sessionId).apply(); - } - - private void setLastAppId(String appId) { - this.lastAppId = appId; - this.settings.edit().putString("lastAppId", appId).apply(); - } - /** * Do everything you need to for "setup" - calling back sets the isAvailable and lets every function on the * javascript side actually do stuff. @@ -133,71 +118,82 @@ public boolean setup(CallbackContext callbackContext) { * @param callbackContext */ public boolean initialize(final String appId, String autoJoinPolicy, String defaultActionPolicy, final CallbackContext callbackContext) { - final Chromecast that = this; - boolean appIdStayedSame = appId.equals(this.lastAppId); - this.appId = appId; - this.setLastAppId(appId); - - LOG.d(TAG, "initialize autoJoinPolicy: " + autoJoinPolicy + " appId: " + appId); - if (autoJoinPolicy.equals("origin_scoped") && appIdStayedSame) { - LOG.d(TAG, "lastAppId " + lastAppId); - autoConnect = true; - } else if (autoJoinPolicy.equals("origin_scoped")) { - LOG.d(TAG, "setting lastAppId " + lastAppId); - } - - // Send no receivers available update while the new routeSelector is built. - // This matches the Chrome Desktop SDK behavior. - that.sendReceiverUpdate(false); - // Search for a receiver for the appId for 1 minute. - // After that there probably won't be any routes, so don't drain their battery. - // The scan should will be triggered each time the client does api initialize - // (aka. every time the user goes to a new page that supports chromecast) - mMediaRouteManager.scanForReceiver(appId, 60000, new ChromecastMediaRouterManager.ScanCallback() { + this.connection.initialize(appId, new ChromecastConnection.Callback() { @Override - public void onFoundReceiver() { - that.sendReceiverUpdate(true); + public void run() { + callbackContext.success(); + + // Send receiver unavailable update while the new routeSelector is built. + // This matches the Chrome Desktop SDK behavior. + sendReceiverUpdate(false); + + + // See if there are any available routes + ChromecastConnection.ScanCallback scanCallback = new ChromecastConnection.ScanCallback() { + @Override + public void onRouteUpdate(RouteInfo route) { + // We found at least 1 route! so stop the scan + connection.stopScan(this); + + // and send out receiver available + sendReceiverUpdate(true); + + // Attempt to rejoin existing session if exists + connection.rejoin(new ChromecastConnection.JoinCallback() { + @Override + public void onJoin(CastSession session) { + // If we were able to join that means the client likely navigated to + // a new page and the code has called initialize again + // so, send out the session + sendJavascript("chrome.cast._.sessionListener(" + media.createSessionObject().toString() + ");"); + } + + @Override + public void onError(String errorCode) { + Log.d(TAG, "Error rejoining session: " + errorCode); + } + }); + } + }; + connection.scanForRoutes(scanCallback); + + // Also start a time out to cancel the scan + // after 5 seconds to save power + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + connection.stopScan(scanCallback); + } + }, 5000); } }); - callbackContext.success(); - return true; } /** * Request the session for the previously sent appId * THIS IS WHAT LAUNCHES THE CHROMECAST PICKER - * NOTE: Make a request session that is automatic - it'll do most of this code - refactor will be required + * or, if we already have a session launch the connection options + * dialog which will have the option to stop casting at minimum. * @param callbackContext */ public boolean requestSession(final CallbackContext callbackContext) { - setLastSessionId(""); - - if (currentSession == null) { - // Show the route selection Dialog - mMediaRouteManager.showRouteSelectionDialog(new ChromecastMediaRouterManager.DialogCallback() { - @Override - void onConnect(RouteInfo route) { - super.onConnect(route); - createSession(route, callbackContext); - } - - @Override - void onError(String errorCode) { - super.onError(errorCode); - if (errorCode.equals("CANCEL")) { - callbackContext.error("cancel"); - } else { - callbackContext.error(errorCode); - } + connection.showConnectionDialog(new ChromecastConnection.JoinCallback() { + @Override + public void onJoin(CastSession session) { + callbackContext.success(media.createSessionObject()); + } + public void onError(String errorCode) { + if (errorCode.equals("CANCEL")) { + callbackContext.error("cancel"); + } else { + // TODO maybe handle some of the error codes better + callbackContext.error("SESSION_ERROR"); } - }); - } else { - // TODO Show the disconnect dialog - } - + } + }); return true; } @@ -208,96 +204,18 @@ void onError(String errorCode) { * @return */ public boolean selectRoute(final String routeId, final CallbackContext callbackContext) { - if (this.currentSession != null) { - callbackContext.success(this.currentSession.createSessionObject()); - return true; - } - - this.setLastSessionId(""); - - final Activity activity = cordova.getActivity(); - activity.runOnUiThread(new Runnable() { - public void run() { - final List routeList = mMediaRouteManager.getRoutes(); - - for (RouteInfo route : routeList) { - if (route.getId().equals(routeId)) { - Chromecast.this.createSession(route, callbackContext); - return; - } - } - - callbackContext.error("No route found"); - } - }); - - return true; - } - - /** - * Helper for the creating of a session! The user-selected RouteInfo needs to be passed to a new ChromecastSession - * @param routeInfo - * @param callbackContext - */ - private void createSession(RouteInfo routeInfo, final CallbackContext callbackContext) { - this.currentSession = new ChromecastSession(routeInfo, this.cordova, this, this); - - // launch the app - this.currentSession.launch(this.appId, new ChromecastSessionCallback() { - @Override - void onSuccess(Object object) { - ChromecastSession session = (ChromecastSession) object; - if (object == null) { - onError("unknown"); - } else if (session == Chromecast.this.currentSession) { - Chromecast.this.setLastSessionId(Chromecast.this.currentSession.getSessionId()); - - if (callbackContext != null) { - callbackContext.success(session.createSessionObject()); - } else { - sendJavascript("chrome.cast._.sessionJoined(" + Chromecast.this.currentSession.createSessionObject().toString() + ");"); - } - } - } - - @Override - void onError(String reason) { - if (reason != null) { - LOG.i(TAG, "createSession onError " + reason); - if (callbackContext != null) { - callbackContext.error(reason); - } - } else { - if (callbackContext != null) { - callbackContext.error("unknown"); - } - } - } - }); - } - - private void joinSession(RouteInfo routeInfo) { - ChromecastSession sessionJoinAttempt = new ChromecastSession(routeInfo, this.cordova, this, this); - sessionJoinAttempt.join(this.appId, this.lastSessionId, new ChromecastSessionCallback() { - + connection.join(routeId, new ChromecastConnection.JoinCallback() { @Override - void onSuccess(Object object) { - if (Chromecast.this.currentSession == null) { - try { - Chromecast.this.currentSession = (ChromecastSession) object; - Chromecast.this.setLastSessionId(Chromecast.this.currentSession.getSessionId()); - sendJavascript("chrome.cast._.sessionJoined(" + Chromecast.this.currentSession.createSessionObject().toString() + ");"); - } catch (Exception e) { - LOG.e(TAG, "wut.... " + e.getMessage() + e.getStackTrace()); - } - } + public void onJoin(CastSession castSession) { + callbackContext.success(media.createSessionObject()); } @Override - void onError(String reason) { - LOG.i(TAG, "sessionJoinAttempt error " + reason); + public void onError(String errorCode) { + callbackContext.error(errorCode); } }); + return true; } /** @@ -305,11 +223,7 @@ void onError(String reason) { * @param newLevel */ public boolean setReceiverVolumeLevel(Double newLevel, CallbackContext callbackContext) { - if (this.currentSession != null) { - this.currentSession.setVolume(newLevel, genericCallback(callbackContext)); - } else { - callbackContext.error("session_error"); - } + this.media.setVolume(newLevel, callbackContext); return true; } @@ -323,11 +237,7 @@ public boolean setReceiverVolumeLevel(Integer newLevel, CallbackContext callback * @param callbackContext */ public boolean setReceiverMuted(Boolean muted, CallbackContext callbackContext) { - if (this.currentSession != null) { - this.currentSession.setMute(muted, genericCallback(callbackContext)); - } else { - callbackContext.error("session_error"); - } + this.media.setMute(muted, callbackContext); return true; } @@ -336,7 +246,7 @@ public boolean setReceiverMuted(Boolean muted, CallbackContext callbackContext) * @param callbackContext [description] */ public boolean stopSession(CallbackContext callbackContext) { - callbackContext.error("not_implemented"); + connection.kill(); return true; } @@ -347,19 +257,7 @@ public boolean stopSession(CallbackContext callbackContext) { * @param callbackContext */ public boolean sendMessage(String namespace, String message, final CallbackContext callbackContext) { - if (this.currentSession != null) { - this.currentSession.sendMessage(namespace, message, new ChromecastSessionCallback() { - @Override - void onSuccess(Object object) { - callbackContext.success(); - } - - @Override - void onError(String reason) { - callbackContext.error(reason); - } - }); - } + this.media.sendMessage(namespace, message, callbackContext); return true; } @@ -371,10 +269,9 @@ void onError(String reason) { * @return */ public boolean addMessageListener(String namespace, CallbackContext callbackContext) { - if (this.currentSession != null) { - this.currentSession.addMessageListener(namespace); - callbackContext.success(); - } + this.media.addMessageListener(namespace); +// sendJavascript("chrome.cast._.onMessage('" + session.getSessionId() + "', '" + namespace + "', '" + message.replace("\\", "\\\\") + "')"); + callbackContext.success(); return true; } @@ -389,27 +286,9 @@ public boolean addMessageListener(String namespace, CallbackContext callbackCont * @param callbackContext */ public boolean loadMedia(String contentId, JSONObject customData, String contentType, Integer duration, String streamType, Boolean autoPlay, Double currentTime, JSONObject metadata, JSONObject textTrackStyle, final CallbackContext callbackContext) { - if (this.currentSession != null) { - return this.currentSession.loadMedia(contentId, customData, contentType, duration, streamType, autoPlay, currentTime, metadata, textTrackStyle, - new ChromecastSessionCallback() { - @Override - void onSuccess(Object object) { - if (object == null) { - onError("unknown"); - } else { - callbackContext.success((JSONObject) object); - } - } - - @Override - void onError(String reason) { - callbackContext.error(reason); - } - }); - } else { - callbackContext.error("session_error"); - return false; - } + this.media.loadMedia(contentId, customData, contentType, duration, streamType, autoPlay, currentTime, metadata, textTrackStyle, callbackContext); + return true; +// sendJavascript("chrome.cast._.mediaLoaded(true, " + media.toString() + ");"); } public boolean loadMedia(String contentId, JSONObject customData, String contentType, Integer duration, String streamType, Boolean autoPlay, Integer currentTime, JSONObject metadata, JSONObject textTrackStyle, final CallbackContext callbackContext) { @@ -422,11 +301,7 @@ public boolean loadMedia(String contentId, JSONObject customData, String content * @return */ public boolean mediaPlay(CallbackContext callbackContext) { - if (currentSession != null) { - currentSession.mediaPlay(genericCallback(callbackContext)); - } else { - callbackContext.error("session_error"); - } + media.mediaPlay(callbackContext); return true; } @@ -436,11 +311,7 @@ public boolean mediaPlay(CallbackContext callbackContext) { * @return */ public boolean mediaPause(CallbackContext callbackContext) { - if (currentSession != null) { - currentSession.mediaPause(genericCallback(callbackContext)); - } else { - callbackContext.error("session_error"); - } + media.mediaPause(callbackContext); return true; } @@ -452,11 +323,7 @@ public boolean mediaPause(CallbackContext callbackContext) { * @return */ public boolean mediaSeek(Integer seekTime, String resumeState, CallbackContext callbackContext) { - if (currentSession != null) { - currentSession.mediaSeek(seekTime.longValue() * 1000, resumeState, genericCallback(callbackContext)); - } else { - callbackContext.error("session_error"); - } + media.mediaSeek(seekTime.longValue() * 1000, resumeState, callbackContext); return true; } @@ -468,12 +335,7 @@ public boolean mediaSeek(Integer seekTime, String resumeState, CallbackContext c * @return */ public boolean setMediaVolume(Double level, CallbackContext callbackContext) { - if (currentSession != null) { - currentSession.mediaSetVolume(level, genericCallback(callbackContext)); - } else { - callbackContext.error("session_error"); - } - + media.mediaSetVolume(level, callbackContext); return true; } @@ -484,12 +346,7 @@ public boolean setMediaVolume(Double level, CallbackContext callbackContext) { * @return */ public boolean setMediaMuted(Boolean muted, CallbackContext callbackContext) { - if (currentSession != null) { - currentSession.mediaSetMuted(muted, genericCallback(callbackContext)); - } else { - callbackContext.error("session_error"); - } - + media.mediaSetMuted(muted, callbackContext); return true; } @@ -499,12 +356,7 @@ public boolean setMediaMuted(Boolean muted, CallbackContext callbackContext) { * @return */ public boolean mediaStop(CallbackContext callbackContext) { - if (currentSession != null) { - currentSession.mediaStop(genericCallback(callbackContext)); - } else { - callbackContext.error("session_error"); - } - + media.mediaStop(callbackContext); return true; } @@ -526,31 +378,8 @@ public boolean mediaEditTracksInfo(JSONArray activeTrackIds, JSONObject textTrac LOG.e(TAG, "Wrong format in activeTrackIds"); } - - if (currentSession != null) { - this.currentSession.mediaEditTracksInfo(trackIds, textTrackStyle, - new ChromecastSessionCallback() { - - @Override - void onSuccess(Object object) { - if (object == null) { - onError("unknown"); - } else { - callbackContext.success((JSONObject) object); - } - } - - @Override - void onError(String reason) { - callbackContext.error(reason); - } - }); - - return true; - } else { - callbackContext.error("session_error"); - return false; - } + this.media.mediaEditTracksInfo(trackIds, textTrackStyle, callbackContext); + return true; } /** @@ -559,14 +388,8 @@ void onError(String reason) { * @return */ public boolean sessionStop(CallbackContext callbackContext) { - if (this.currentSession != null) { - this.currentSession.kill(genericCallback(callbackContext)); - this.currentSession = null; - this.setLastSessionId(""); - } else { - callbackContext.success(); - } - + connection.kill(); + callbackContext.success(); return true; } @@ -576,44 +399,17 @@ public boolean sessionStop(CallbackContext callbackContext) { * @return */ public boolean sessionLeave(CallbackContext callbackContext) { - if (this.currentSession != null) { - this.currentSession.leave(genericCallback(callbackContext)); - this.currentSession = null; - this.setLastSessionId(""); - } else { - callbackContext.success(); - } - + connection.leave(); + callbackContext.success(); return true; } public boolean emitAllRoutes(CallbackContext callbackContext) { - final Activity activity = cordova.getActivity(); - - activity.runOnUiThread(new Runnable() { - public void run() { - List routeList = mMediaRouteManager.getRoutes(); - - for (RouteInfo route : routeList) { - sendJavascript("chrome.cast._.routeAdded(" + routeToJSON(route) + ")"); - } - } - }); - - if (callbackContext != null) { - callbackContext.success(); - } + // TODO will use connection.scanForRoutes(); return true; } - /** - * Checks to see if any valid receivers are available - emits the receiver status out to Javascript - */ - public void sendReceiverUpdate() { - this.sendReceiverUpdate(mMediaRouteManager.isRouteAvailable()); - } - /** * sends the receiverState. * @param receiverState @@ -626,60 +422,6 @@ private void sendReceiverUpdate(boolean receiverState) { } } - /** - * Creates a ChromecastSessionCallback that's generic for a CallbackContext - * @param callbackContext - * @return - */ - private ChromecastSessionCallback genericCallback(final CallbackContext callbackContext) { - return new ChromecastSessionCallback() { - @Override - public void onSuccess(Object object) { - callbackContext.success(); - } - - @Override - public void onError(String reason) { - callbackContext.error(reason); - } - }; - } - - /** - * Called when a route is discovered - * @param router - * @param route - */ - protected void onRouteAdded(MediaRouter router, final RouteInfo route) { - if (this.autoConnect && this.currentSession == null && !route.getName().equals("Phone")) { - LOG.d(TAG, "Attempting to join route " + route.getName()); - this.joinSession(route); - } else { - LOG.d(TAG, "For some reason, not attempting to join route " + route.getName() + ", " + this.currentSession + ", " + this.autoConnect); - } - } - - /** - * Called when a discovered route is lost - * @param router - * @param route - */ - protected void onRouteRemoved(MediaRouter router, RouteInfo route) { - if (!route.getName().equals("Phone") && route.getId().indexOf("Cast") > -1) { - sendJavascript("chrome.cast._.routeRemoved(" + routeToJSON(route) + ")"); - } - } - - /** - * Called when a route is unselected through the MediaRouter - * @param router - * @param route - */ - protected void onRouteUnselected(MediaRouter router, RouteInfo route, int reason) { - // Let the client know they have been disconnected - sendJavascript("chrome.cast._.receiverUnavailable()"); - } - /** * Simple helper to convert a route to JSON for passing down to the javascript side * @param route @@ -698,41 +440,43 @@ private JSONObject routeToJSON(RouteInfo route) { return obj; } - @Override - public void onMediaUpdated(boolean isAlive, JSONObject media) { - if (isAlive) { - sendJavascript("chrome.cast._.mediaUpdated(true, " + media.toString() + ");"); - } else { - sendJavascript("chrome.cast._.mediaUpdated(false, " + media.toString() + ");"); + private RemoteMediaClient.Callback remoteMediaClientCallback = new RemoteMediaClient.Callback() { + @Override + public void onStatusUpdated() { + super.onStatusUpdated(); } - } - @Override - public void onSessionUpdated(boolean isAlive, JSONObject session) { - if (isAlive) { - sendJavascript("chrome.cast._.sessionUpdated(true, " + session.toString() + ");"); - } else { - LOG.d(TAG, "SESSION DESTROYYYY"); - sendJavascript("chrome.cast._.sessionUpdated(false, " + session.toString() + ");"); - this.currentSession = null; + @Override + public void onMetadataUpdated() { + super.onMetadataUpdated(); +// sendJavascript("chrome.cast._.mediaUpdated(true, " + media.createMediaInfo() + ");"); } - } - @Override - public void onMediaLoaded(JSONObject media) { - sendJavascript("chrome.cast._.mediaLoaded(true, " + media.toString() + ");"); - } + @Override + public void onQueueStatusUpdated() { + super.onQueueStatusUpdated(); + } - @Override - public void onMessage(ChromecastSession session, String namespace, String message) { - sendJavascript("chrome.cast._.onMessage('" + session.getSessionId() + "', '" + namespace + "', '" + message.replace("\\", "\\\\") + "')"); - } + @Override + public void onPreloadStatusUpdated() { + super.onPreloadStatusUpdated(); + } + + @Override + public void onSendingRemoteMediaRequest() { + super.onSendingRemoteMediaRequest(); + } + + @Override + public void onAdBreakStatusUpdated() { + super.onAdBreakStatusUpdated(); + } + }; //Change all @deprecated this.webView.sendJavascript(String) to this local function sendJavascript(String) @TargetApi(Build.VERSION_CODES.KITKAT) private void sendJavascript(final String javascript) { webView.getView().post(new Runnable() { - @Override public void run() { // See: https://github.com/GoogleChrome/chromium-webview-samples/blob/master/jsinterface-example/app/src/main/java/jsinterfacesample/android/chrome/google/com/jsinterface_example/MainFragment.java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index 4fd6a34..0fd0ffc 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -2,7 +2,7 @@ import android.app.Activity; import android.content.DialogInterface; -import android.os.Handler; +import android.content.Intent; import androidx.mediarouter.app.MediaRouteChooserDialog; import androidx.mediarouter.media.MediaRouteSelector; @@ -10,155 +10,346 @@ import androidx.mediarouter.media.MediaRouter.RouteInfo; import com.google.android.gms.cast.CastMediaControlIntent; +import com.google.android.gms.cast.framework.CastContext; +import com.google.android.gms.cast.framework.CastSession; +import com.google.android.gms.cast.framework.SessionManager; +import com.google.android.gms.cast.framework.SessionManagerListener; -import java.util.List; +public class ChromecastConnection { -public class ChromecastMediaRouterManager { + // Lifetime variables + private Activity activity; + private ChromecastSession media; + private SessionListener newConnectionListener; - private Activity mActivity; - private String mAppId; - private volatile MediaRouter mMediaRouter; - private volatile MediaRouteSelector mMediaRouteSelector; - private MediaRouter.Callback mMediaRouterCallback; - private volatile boolean mDialogCancelled; + // initialize lifetime variables + private String appId; - public ChromecastMediaRouterManager(Activity context, MediaRouter.Callback mediaRouterCallback) { - mActivity = context; - mMediaRouterCallback = mediaRouterCallback; + // session lifetime variables + private CastSession session; + + public ChromecastConnection(Activity activity, ChromecastSession media, ConnectionListener listener) { + this.activity = activity; + this.media = media; + + // Add the session end/disconnect listener + activity.runOnUiThread(new Runnable() { + public void run() { + getSessionManager().addSessionManagerListener(new SessionListener() { + @Override + public void onSessionEnded(CastSession castSession, int error) { + setSession(null); + if (listener != null) { + listener.onDisconnected(error); + } + } + }, CastSession.class); + } + }); } /** - * Actively searches for a valid receiver for appId for seekDuration. - * This uses lots of battery power, so it's best to limit the search time. + * Must be called each time the appId changes and at least once before any other method is called. * @param appId - * @param seekDuration (milli seconds) * @param callback */ - public void scanForReceiver(String appId, int seekDuration, ScanCallback callback) { - mActivity.runOnUiThread(new Runnable() { + public void initialize(String appId, Callback callback) { + // If the appId changed + if (!appId.equals(this.appId)) { + // Else we need to save the new appId + this.setAppId(appId); + // And reset the session + this.setSession(null); + } + + activity.runOnUiThread(new Runnable() { public void run() { - synchronized(ChromecastMediaRouterManager.class) { + // Set the new appId + CastContext.getSharedInstance(activity).setReceiverApplicationId(appId); + // Tell the client we are done + callback.run(); + } + }); + } - boolean appIdIsSame = appId.equals(mAppId); - mAppId = appId; + private MediaRouter getMediaRouter() { + return MediaRouter.getInstance(activity); + } - // Ensure the media router exists - if (mMediaRouter == null) { - // We need to initialize the router exists - mMediaRouter = MediaRouter.getInstance(mActivity); - } + private SessionManager getSessionManager() { + return CastContext.getSharedInstance(activity).getSessionManager(); + } - if (appIdIsSame && isRouteAvailable()) { - // In this case, it most likely this the user navigated to a new page and called api initialize again. - // To replicate chrome desktop behavior, we must manually send the receiver available update. - // Scan will not find a new route since we already have one. - callback.onFoundReceiver(); + private void setAppId(String appId) { + this.appId = appId; + } - } else { - // Else, scan because, the app Id has changed, this is our first time here, or no routes are known + private void setSession(CastSession castSession) { + this.session = castSession; + this.media.setSession(this.session); + } - // Update/create the route selector as needed - if (!appIdIsSame || mMediaRouteSelector == null) { - mMediaRouteSelector = new MediaRouteSelector.Builder() - .addControlCategory(CastMediaControlIntent.categoryForCast(appId)) - .build(); - } + /** + * Attempts to join the last route we were connected to. + * + * @param callback + */ + public void rejoin(JoinCallback callback) { + if (session != null) { + callback.onJoin(session); + } + // TODO is it even possible to rejoin a session automatically? + // https://stackoverflow.com/questions/57801427/how-to-rejoin-cast-session-after-app-restart + // https://stackoverflow.com/questions/57832467/how-to-check-if-mediarouter-routeinfo-route-is-already-in-use + } - // Add a callback that will solely search for receivers. - // We will remove the active seeking callback after it finds a route or - // after seekDuration. It uses a lot of power. We will then add a less - // power-hungry callback. - - // Create our route detection callback - MediaRouter.Callback routeChangedCallback = new MediaRouter.Callback() { - @Override - public void onRouteChanged(MediaRouter router, RouteInfo route) { - super.onRouteChanged(router, route); - if (isRouteAvailable()) { - callback.onFoundReceiver(); - mMediaRouter.removeCallback(this); - } - } - }; - - // Create and start our timeout - final Handler handler = new Handler(); - final Runnable r = new Runnable() { - @Override - public void run() { - // Quit scanning - mMediaRouter.removeCallback(routeChangedCallback); - } - }; - handler.postDelayed(r, seekDuration); - - // Add the callback in active scan mode - mMediaRouter.addCallback(mMediaRouteSelector, routeChangedCallback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); - } + /** + * This will create a new session or seamlessly join an existing one if we created it. + * @param routeId + * @param callback + */ + public void join(String routeId, JoinCallback callback) { + activity.runOnUiThread(new Runnable() { + public void run() { + if (session != null) { + // We are are already connected to a route + callback.onJoin(session); + return; } + listenForConnection(callback); + + Intent castIntent = new Intent(); + castIntent.putExtra("CAST_INTENT_TO_CAST_ROUTE_ID_KEY", routeId); + // Not sure of this one's purpose, possibly just for display + // castIntent.putExtra("CAST_INTENT_TO_CAST_DEVICE_NAME_KEY", deviceName); + castIntent.putExtra("CAST_INTENT_TO_CAST_NO_TOAST_KEY", false); + getSessionManager().startSession(castIntent); } }); } - public void showRouteSelectionDialog(DialogCallback callback) { - mDialogCancelled = false; - - mActivity.runOnUiThread(new Runnable() { + /** + * Will do one of two things: + * + * If no current connection will: + * 1) + * Displays the built in native prompt to the user. + * It will actively scan for routes and display them to the user. + * Upon selection it will immediately attempt to join the route. + * Will call onJoin or onError of callback. + * + * Else we have a connection, so: + * 2) + * Displays the active connection dialog which includes the option + * to disconnect. + * Will only call onError of callback if the user cancels the dialog. + * + * @param callback + */ + public void showConnectionDialog(JoinCallback callback) { + activity.runOnUiThread(new Runnable() { public void run() { + if (session == null) { + // show the "choose a connection" dialog - // Create the dialog - // TODO accept theme as a config.xml option - MediaRouteChooserDialog builder = new MediaRouteChooserDialog(mActivity, androidx.appcompat.R.style.Theme_AppCompat_NoActionBar); - builder.setRouteSelector(mMediaRouteSelector); - builder.setCanceledOnTouchOutside(true); - builder.setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - mDialogCancelled = true; - } - }); - builder.setOnDismissListener(new DialogInterface.OnDismissListener() { - @Override - public void onDismiss(DialogInterface dialog) { - RouteInfo route = mMediaRouter.getSelectedRoute(); - if (mDialogCancelled) { + // Add the connection listener callback + listenForConnection(callback); + + // Create the dialog + // TODO accept theme as a config.xml option + MediaRouteChooserDialog builder = new MediaRouteChooserDialog(activity, androidx.appcompat.R.style.Theme_AppCompat_NoActionBar); + builder.setRouteSelector(new MediaRouteSelector.Builder() + .addControlCategory(CastMediaControlIntent.categoryForCast(appId)) + .build()); + builder.setCanceledOnTouchOutside(true); + builder.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + getSessionManager().removeSessionManagerListener(newConnectionListener, CastSession.class); callback.onError("CANCEL"); - } else if (route.isDefault()) { - callback.onError("RECEIVER_UNAVAILABLE"); - } else { - callback.onConnect(route); } - } - }); + }); + builder.show(); + } else { + // We are are already connected, so show the "connection options" Dialog + // TODO + } + } + }); + } - builder.show(); + /** + * Must be called from the main thread + * @param callback + */ + private void listenForConnection(JoinCallback callback) { + // We should only ever have one of these listeners active at a time, so remove previous + getSessionManager().removeSessionManagerListener(newConnectionListener, CastSession.class); + newConnectionListener = new SessionListener() { + @Override + public void onSessionStarted(CastSession castSession, String sessionId) { + getSessionManager().removeSessionManagerListener(this, CastSession.class); + setSession(castSession); + callback.onJoin(session); + } + @Override + public void onSessionStartFailed(CastSession castSession, int errCode) { + getSessionManager().removeSessionManagerListener(this, CastSession.class); + setSession(null); + callback.onError(Integer.toString(errCode)); + } + }; + getSessionManager().addSessionManagerListener(newConnectionListener, CastSession.class); + } + + + /** + * Starts listening for receiver updates. + * Must call stopScan(callback) or the battery will drain with non-stop active scanning. + * @param callback + */ + public void scanForRoutes(ScanCallback callback) { + // Add the callback in active scan mode + activity.runOnUiThread(new Runnable() { + public void run() { + + // Send out the initial routes + for (RouteInfo route : getMediaRouter().getRoutes()) { + callback.onFilteredRouteUpdate(route); + } + + // Add the callback in active scan mode + getMediaRouter().addCallback(new MediaRouteSelector.Builder() + .addControlCategory(CastMediaControlIntent.categoryForCast(appId)) + .build(), + callback, + MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); } }); } /** - * Checks if it appears that any routes are available. - * @return True, if there has been a receiver available (it is possible that it has been disconnected). - * False, if there has been no confirmed receiver. + * Call to stop the active scan if any exist. */ - public boolean isRouteAvailable() { - if (mMediaRouter != null && mMediaRouteSelector != null) { - return mMediaRouter.isRouteAvailable(mMediaRouteSelector, MediaRouter.AVAILABILITY_FLAG_IGNORE_DEFAULT_ROUTE); + public void stopScan(ScanCallback callback) { + activity.runOnUiThread(new Runnable() { + public void run() { + callback.stop(); + getMediaRouter().removeCallback(callback); + } + }); + } + + public void leave() { + activity.runOnUiThread(new Runnable() { + public void run() { + setSession(null); + getMediaRouter().unselect(MediaRouter.UNSELECT_REASON_DISCONNECTED); + } + }); + } + + public void kill() { + activity.runOnUiThread(new Runnable() { + public void run() { + setSession(null); + getSessionManager().endCurrentSession(true); + } + }); + } + + private class SessionListener implements SessionManagerListener { + + @Override + public void onSessionStarting(CastSession castSession) { + } + + @Override + public void onSessionStarted(CastSession castSession, String sessionId) { + } + + @Override + public void onSessionStartFailed(CastSession castSession, int error) { + } + + @Override + public void onSessionEnding(CastSession castSession) { + } + + @Override + public void onSessionEnded(CastSession castSession, int error) { + } + + @Override + public void onSessionResuming(CastSession castSession, String sessionId) { + } + + @Override + public void onSessionResumed(CastSession castSession, boolean wasSuspended) { + } + + @Override + public void onSessionResumeFailed(CastSession castSession, int error) { + } + + @Override + public void onSessionSuspended(CastSession castSession, int reason) { } - return false; } - public List getRoutes() { - return mMediaRouter.getRoutes(); + public interface Callback { + void run(); + } + + public interface ConnectionListener { + /** + * Called whenever a connection ends + */ + void onDisconnected(int reason); } - public static class ScanCallback { - void onFoundReceiver() {} + public interface JoinCallback { + /** + * Successfully joined a session on a route. + */ + void onJoin(CastSession session); + + /** + * @param errorCode "CANCEL" means the user cancelled + * If the errorCode is an integer, you can find the meaning here: + * https://developers.google.com/android/reference/com/google/android/gms/cast/CastStatusCodes + */ + void onError(String errorCode); } - public static class DialogCallback { - void onConnect(RouteInfo route) {} - void onError(String errorCode) {} + public static abstract class ScanCallback extends MediaRouter.Callback { + abstract void onRouteUpdate(RouteInfo route); + + private boolean stopped = false; + void stop() { + stopped = true; + } + private void onFilteredRouteUpdate(RouteInfo route) { + if (stopped) { + return; + } + if (!route.isDefault()) { + onRouteUpdate(route); + } + } + @Override + public void onRouteAdded(MediaRouter router, RouteInfo route) { + onFilteredRouteUpdate(route); + } + @Override + public void onRouteChanged(MediaRouter router, RouteInfo route) { + onFilteredRouteUpdate(route); + } + @Override + public void onRouteRemoved(MediaRouter router, RouteInfo route) { + onFilteredRouteUpdate(route); + } } + } diff --git a/src/android/ChromecastException.java b/src/android/ChromecastException.java deleted file mode 100644 index 9aa3d30..0000000 --- a/src/android/ChromecastException.java +++ /dev/null @@ -1,4 +0,0 @@ -package acidhax.cordova.chromecast; - -public class ChromecastException extends Exception { -} \ No newline at end of file diff --git a/src/android/ChromecastMediaController.java b/src/android/ChromecastMediaController.java deleted file mode 100644 index 62121dd..0000000 --- a/src/android/ChromecastMediaController.java +++ /dev/null @@ -1,137 +0,0 @@ -package acidhax.cordova.chromecast; - -import com.google.android.gms.cast.MediaInfo; -import com.google.android.gms.cast.MediaMetadata; -import com.google.android.gms.cast.RemoteMediaPlayer; -import com.google.android.gms.cast.RemoteMediaPlayer.MediaChannelResult; -import com.google.android.gms.cast.TextTrackStyle; -import com.google.android.gms.common.api.GoogleApiClient; -import com.google.android.gms.common.api.PendingResult; -import com.google.android.gms.common.api.ResultCallback; -import com.google.android.gms.common.images.WebImage; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import android.net.Uri; -import android.util.Log; - -public class ChromecastMediaController { - private RemoteMediaPlayer remote = null; - - public ChromecastMediaController(RemoteMediaPlayer mRemoteMediaPlayer) { - this.remote = mRemoteMediaPlayer; - } - - public MediaInfo createLoadUrlRequest(String contentId, JSONObject customData, String contentType, long duration, String streamType, JSONObject metadata, JSONObject textTrackStyle) { - // create GENERIC MediaMetadata first and fallback to movie - MediaMetadata mediaMetadata = new MediaMetadata(); - try { - int metadataType = metadata.has("metadataType") ? metadata.getInt("metadataType") : MediaMetadata.MEDIA_TYPE_MOVIE; - if (metadataType == MediaMetadata.MEDIA_TYPE_GENERIC) { - mediaMetadata.putString(MediaMetadata.KEY_TITLE, (metadata.has("title")) ? metadata.getString("title") : "[Title not set]"); // TODO: What should it default to? - mediaMetadata.putString(MediaMetadata.KEY_SUBTITLE, (metadata.has("title")) ? metadata.getString("subtitle") : "[Subtitle not set]"); // TODO: What should it default to? - mediaMetadata = addImages(metadata, mediaMetadata); - } - } catch (Exception e) { - e.printStackTrace(); - mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE); - } - - int _streamType; - if (streamType.equals("buffered")) { - _streamType = MediaInfo.STREAM_TYPE_BUFFERED; - } else if (streamType.equals("live")) { - _streamType = MediaInfo.STREAM_TYPE_LIVE; - } else { - _streamType = MediaInfo.STREAM_TYPE_NONE; - } - - TextTrackStyle trackStyle = ChromecastUtilities.parseTextTrackStyle(textTrackStyle); - MediaInfo mediaInfo = new MediaInfo.Builder(contentId) - .setContentType(contentType) - .setCustomData(customData) - .setStreamType(_streamType) - .setStreamDuration(duration) - .setMetadata(mediaMetadata) - .setTextTrackStyle(trackStyle) - .build(); - - return mediaInfo; - } - - public void play(GoogleApiClient apiClient, ChromecastSessionCallback callback) { - PendingResult res = this.remote.play(apiClient); - res.setResultCallback(this.createMediaCallback(callback)); - } - - public void pause(GoogleApiClient apiClient, ChromecastSessionCallback callback) { - PendingResult res = this.remote.pause(apiClient); - res.setResultCallback(this.createMediaCallback(callback)); - } - - public void stop(GoogleApiClient apiClient, ChromecastSessionCallback callback) { - PendingResult res = this.remote.stop(apiClient); - res.setResultCallback(this.createMediaCallback(callback)); - } - - public void seek(long seekPosition, String resumeState, GoogleApiClient apiClient, final ChromecastSessionCallback callback) { - PendingResult res = null; - if (resumeState != null && !resumeState.equals("")) { - if (resumeState.equals("PLAYBACK_PAUSE")) { - res = this.remote.seek(apiClient, seekPosition, RemoteMediaPlayer.RESUME_STATE_PAUSE); - } else if (resumeState.equals("PLAYBACK_START")) { - res = this.remote.seek(apiClient, seekPosition, RemoteMediaPlayer.RESUME_STATE_PLAY); - } else { - res = this.remote.seek(apiClient, seekPosition, RemoteMediaPlayer.RESUME_STATE_UNCHANGED); - } - } - - if (res == null) { - res = this.remote.seek(apiClient, seekPosition); - } - - res.setResultCallback(this.createMediaCallback(callback)); - } - - public void setVolume(double volume, GoogleApiClient apiClient, final ChromecastSessionCallback callback) { - PendingResult res = this.remote.setStreamVolume(apiClient, volume); - res.setResultCallback(this.createMediaCallback(callback)); - } - - public void setMuted(boolean muted, GoogleApiClient apiClient, final ChromecastSessionCallback callback) { - PendingResult res = this.remote.setStreamMute(apiClient, muted); - res.setResultCallback(this.createMediaCallback(callback)); - } - - private ResultCallback createMediaCallback(final ChromecastSessionCallback callback) { - return new ResultCallback() { - @Override - public void onResult(MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.onSuccess(); - } else { - callback.onError("channel_error"); - } - } - }; - } - - private MediaMetadata addImages(JSONObject metadata, MediaMetadata mediaMetadata) throws JSONException { - if (metadata.has("images")) { - JSONArray imageUrls = metadata.getJSONArray("images"); - for (int i = 0; i < imageUrls.length(); i++) { - JSONObject imageObj = imageUrls.getJSONObject(i); - String imageUrl = imageObj.has("url") ? imageObj.getString("url") : "undefined"; - if (imageUrl.indexOf("http://") < 0) { - continue; - } - Uri imageURI = Uri.parse(imageUrl); - WebImage webImage = new WebImage(imageURI); - mediaMetadata.addImage(webImage); - } - } - return mediaMetadata; - } -} \ No newline at end of file diff --git a/src/android/ChromecastMediaRouterCallback.java b/src/android/ChromecastMediaRouterCallback.java deleted file mode 100644 index 14e841b..0000000 --- a/src/android/ChromecastMediaRouterCallback.java +++ /dev/null @@ -1,36 +0,0 @@ -package acidhax.cordova.chromecast; - -import androidx.mediarouter.media.MediaRouter; -import androidx.mediarouter.media.MediaRouter.RouteInfo; - -public class ChromecastMediaRouterCallback extends MediaRouter.Callback { - - private Chromecast callback = null; - - public ChromecastMediaRouterCallback(Chromecast instance) { - this.callback = instance; - } - - - @Override - public synchronized void onRouteAdded(MediaRouter router, RouteInfo route) { - if (this.callback != null) { - this.callback.onRouteAdded(router, route); - } - } - - @Override - public void onRouteRemoved(MediaRouter router, RouteInfo route) { - if (this.callback != null) { - this.callback.onRouteRemoved(router, route); - } - } - - @Override - public void onRouteUnselected(MediaRouter router, RouteInfo route, int reason) { - super.onRouteUnselected(router, route, reason); - if (this.callback != null) { - this.callback.onRouteUnselected(router, route, reason); - } - } -} diff --git a/src/android/ChromecastOnMediaUpdatedListener.java b/src/android/ChromecastOnMediaUpdatedListener.java deleted file mode 100644 index 034c796..0000000 --- a/src/android/ChromecastOnMediaUpdatedListener.java +++ /dev/null @@ -1,8 +0,0 @@ -package acidhax.cordova.chromecast; - -import org.json.JSONObject; - -public interface ChromecastOnMediaUpdatedListener { - void onMediaLoaded(JSONObject media); - void onMediaUpdated(boolean isAlive, JSONObject media); -} \ No newline at end of file diff --git a/src/android/ChromecastOnSessionUpdatedListener.java b/src/android/ChromecastOnSessionUpdatedListener.java deleted file mode 100644 index 4172e49..0000000 --- a/src/android/ChromecastOnSessionUpdatedListener.java +++ /dev/null @@ -1,8 +0,0 @@ -package acidhax.cordova.chromecast; - -import org.json.JSONObject; - -public interface ChromecastOnSessionUpdatedListener { - void onSessionUpdated(boolean isAlive, JSONObject properties); - void onMessage(ChromecastSession session, String namespace, String message); -} \ No newline at end of file diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index 23801c5..8896ea5 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -1,111 +1,90 @@ package acidhax.cordova.chromecast; import java.io.IOException; -import java.util.HashSet; import java.util.List; -import org.apache.cordova.CordovaInterface; +import org.apache.cordova.CallbackContext; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import com.google.android.gms.cast.ApplicationMetadata; import com.google.android.gms.cast.Cast; -import com.google.android.gms.cast.Cast.ApplicationConnectionResult; import com.google.android.gms.cast.CastDevice; import com.google.android.gms.cast.MediaInfo; +import com.google.android.gms.cast.MediaLoadRequestData; +import com.google.android.gms.cast.MediaMetadata; +import com.google.android.gms.cast.MediaSeekOptions; import com.google.android.gms.cast.MediaStatus; import com.google.android.gms.cast.MediaTrack; -import com.google.android.gms.cast.RemoteMediaPlayer; -import com.google.android.gms.cast.RemoteMediaPlayer.MediaChannelResult; -import com.google.android.gms.cast.RemoteMediaPlayer.OnMetadataUpdatedListener; -import com.google.android.gms.cast.RemoteMediaPlayer.OnStatusUpdatedListener; -import com.google.android.gms.common.ConnectionResult; -import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.cast.TextTrackStyle; +import com.google.android.gms.cast.framework.CastSession; +import com.google.android.gms.cast.framework.media.RemoteMediaClient; +import com.google.android.gms.cast.framework.media.RemoteMediaClient.MediaChannelResult; import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.common.api.Status; import com.google.android.gms.common.images.WebImage; -import android.os.Bundle; - -import androidx.mediarouter.media.MediaRouter.RouteInfo; +import android.app.Activity; +import android.net.Uri; +import androidx.annotation.NonNull; /* * All of the Chromecast session specific functions should start here. */ -public class ChromecastSession - extends Cast.Listener - implements - GoogleApiClient.ConnectionCallbacks, - GoogleApiClient.OnConnectionFailedListener, - OnMetadataUpdatedListener, - OnStatusUpdatedListener, - Cast.MessageReceivedCallback { - - private RouteInfo routeInfo = null; - private volatile GoogleApiClient mApiClient = null; - private volatile RemoteMediaPlayer mRemoteMediaPlayer; - private CordovaInterface cordova = null; - private CastDevice device = null; - private ChromecastMediaController chromecastMediaController; - private ChromecastOnMediaUpdatedListener onMediaUpdatedListener; - private ChromecastOnSessionUpdatedListener onSessionUpdatedListener; - - private volatile String appId; - private volatile String displayName; - private volatile List appImages; - private volatile String sessionId = null; - private volatile String lastSessionId = null; - private boolean isConnected = false; - - private ChromecastSessionCallback launchCallback; - private ChromecastSessionCallback joinSessionCallback; - - private boolean joinInsteadOfConnecting = false; - private HashSet messageNamespaces = new HashSet(); - - public ChromecastSession(RouteInfo routeInfo, CordovaInterface cordovaInterface, - ChromecastOnMediaUpdatedListener onMediaUpdatedListener, ChromecastOnSessionUpdatedListener onSessionUpdatedListener) { - this.cordova = cordovaInterface; - this.onMediaUpdatedListener = onMediaUpdatedListener; - this.onSessionUpdatedListener = onSessionUpdatedListener; - this.routeInfo = routeInfo; - this.device = CastDevice.getFromBundle(this.routeInfo.getExtras()); - - this.mRemoteMediaPlayer = new RemoteMediaPlayer(); - this.mRemoteMediaPlayer.setOnMetadataUpdatedListener(this); - this.mRemoteMediaPlayer.setOnStatusUpdatedListener(this); - - this.chromecastMediaController = new ChromecastMediaController(mRemoteMediaPlayer); - } +public class ChromecastSession { - /** - * Sets the wheels in motion - connects to the Chromecast and launches the given app - * @param appId - */ - public void launch(String appId, ChromecastSessionCallback launchCallback) { - this.appId = appId; - this.launchCallback = launchCallback; - this.connectToDevice(); + private Activity activity; + private RemoteMediaClient.Callback remoteMediaCallback; + private RemoteMediaClient client; + private CastSession session; + + public ChromecastSession(Activity activity, RemoteMediaClient.Callback callback) { + this.activity = activity; + this.remoteMediaCallback = callback; } - public boolean isConnected() { - return this.isConnected; + public void setSession(CastSession castSession) { + activity.runOnUiThread(new Runnable() { + public void run() { + if (client != null) { + client.unregisterCallback(remoteMediaCallback); + } + session = castSession; + if (session == null) { + client = null; + } else { + client = session.getRemoteMediaClient(); + client.registerCallback(remoteMediaCallback); + } + } + }); } + /** * Adds a message listener if one does not already exist * @param namespace */ public void addMessageListener(String namespace) { - if (messageNamespaces.contains(namespace) == false) { - try { - Cast.CastApi.setMessageReceivedCallbacks(mApiClient, namespace, this); - messageNamespaces.add(namespace); - } catch (Exception e) { - e.printStackTrace(); - } + if (client == null || session == null) { + return; } + activity.runOnUiThread(new Runnable() { + public void run() { + try { + session.setMessageReceivedCallbacks(namespace, new Cast.MessageReceivedCallback() { + @Override + public void onMessageReceived(CastDevice castDevice, String s, String s1) { + // if (this.onSessionUpdatedListener != null) { + // this.onSessionUpdatedListener.onMessage(this, namespace, message); + // } + } + }); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); } /** @@ -114,64 +93,26 @@ public void addMessageListener(String namespace) { * @param message * @param callback */ - public void sendMessage(String namespace, String message, final ChromecastSessionCallback callback) { - try { - Cast.CastApi.sendMessage(mApiClient, namespace, message).setResultCallback(new ResultCallback() { - @Override - public void onResult(Status result) { - if (!result.isSuccess()) { - callback.onSuccess(); - } else { - callback.onError(result.toString()); - } - } - }); - } catch (Exception e) { - callback.onError(e.getMessage()); - } - } - - /** - * Join a currently running app with an appId and a session - * @param appId - * @param sessionId - * @param joinSessionCallback - */ - public void join(String appId, String sessionId, ChromecastSessionCallback joinSessionCallback) { - this.appId = appId; - this.joinSessionCallback = joinSessionCallback; - this.joinInsteadOfConnecting = true; - this.lastSessionId = sessionId; - this.connectToDevice(); - } - - /** - * Kills a session and it's underlying media player - * @param callback - */ - public void kill(final ChromecastSessionCallback callback) { - try { - Cast.CastApi.stopApplication(mApiClient); - mApiClient.disconnect(); - } catch (Exception e) { - e.printStackTrace(); - } - - callback.onSuccess(); - } - - /** - * Leaves the session. - * @param callback - */ - public void leave(final ChromecastSessionCallback callback) { - try { - Cast.CastApi.leaveApplication(mApiClient); - } catch (Exception e) { - e.printStackTrace(); + public void sendMessage(String namespace, String message, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; } + activity.runOnUiThread(new Runnable() { + public void run() { + session.sendMessage(namespace, message).setResultCallback(new ResultCallback() { + @Override + public void onResult(Status result) { + if (!result.isSuccess()) { + callback.success(); + } else { + callback.error(result.toString()); + } + } + }); - callback.onSuccess(); + } + }); } /** @@ -185,47 +126,136 @@ public void leave(final ChromecastSessionCallback callback) { * @param callback * @return */ - public boolean loadMedia(String contentId, JSONObject customData, String contentType, long duration, String streamType, boolean autoPlay, double currentTime, JSONObject metadata, JSONObject textTrackStyle, final ChromecastSessionCallback callback) { - try { - MediaInfo mediaInfo = chromecastMediaController.createLoadUrlRequest(contentId, customData, contentType, duration, streamType, metadata, textTrackStyle); - - mRemoteMediaPlayer.load(mApiClient, mediaInfo, autoPlay, (long) (currentTime * 1000)) - .setResultCallback(new ResultCallback() { - @Override - public void onResult(MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - System.out.println("Media loaded successfully"); - - ChromecastSession.this.onMediaUpdatedListener.onMediaLoaded(ChromecastSession.this.createMediaObject()); - callback.onSuccess(ChromecastSession.this.createMediaObject()); - } else { - callback.onError("session_error"); - } + public void loadMedia(String contentId, JSONObject customData, String contentType, long duration, String streamType, boolean autoPlay, double currentTime, JSONObject metadata, JSONObject textTrackStyle, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + MediaInfo mediaInfo = createMediaInfo(contentId, customData, contentType, duration, streamType, metadata, textTrackStyle); + MediaLoadRequestData loadRequest = new MediaLoadRequestData.Builder() + .setMediaInfo(mediaInfo) + .setAutoplay(autoPlay) + .setCurrentTime((long) currentTime * 1000) + .build(); + + client.load(loadRequest).setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(createMediaObject()); + } else { + callback.error("SESSION_ERROR"); } - }); + } + }); + } + }); + } + + private MediaInfo createMediaInfo(String contentId, JSONObject customData, String contentType, long duration, String streamType, JSONObject metadata, JSONObject textTrackStyle) { + // create GENERIC MediaMetadata first and fallback to movie + MediaMetadata mediaMetadata = new MediaMetadata(); + try { + int metadataType = metadata.has("metadataType") ? metadata.getInt("metadataType") : MediaMetadata.MEDIA_TYPE_MOVIE; + if (metadataType == MediaMetadata.MEDIA_TYPE_GENERIC) { + mediaMetadata.putString(MediaMetadata.KEY_TITLE, (metadata.has("title")) ? metadata.getString("title") : "[Title not set]"); // TODO: What should it default to? + mediaMetadata.putString(MediaMetadata.KEY_SUBTITLE, (metadata.has("title")) ? metadata.getString("subtitle") : "[Subtitle not set]"); // TODO: What should it default to? + mediaMetadata = addImages(metadata, mediaMetadata); + } } catch (Exception e) { e.printStackTrace(); - callback.onError("session_error"); - System.out.println("Problem opening media during loading"); - return false; + mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE); } - return true; + + int _streamType; + if (streamType.equals("buffered")) { + _streamType = MediaInfo.STREAM_TYPE_BUFFERED; + } else if (streamType.equals("live")) { + _streamType = MediaInfo.STREAM_TYPE_LIVE; + } else { + _streamType = MediaInfo.STREAM_TYPE_NONE; + } + + TextTrackStyle trackStyle = ChromecastUtilities.parseTextTrackStyle(textTrackStyle); + MediaInfo mediaInfo = new MediaInfo.Builder(contentId) + .setContentType(contentType) + .setCustomData(customData) + .setStreamType(_streamType) + .setStreamDuration(duration) + .setMetadata(mediaMetadata) + .setTextTrackStyle(trackStyle) + .build(); + + return mediaInfo; + } + + private MediaMetadata addImages(JSONObject metadata, MediaMetadata mediaMetadata) throws JSONException { + if (metadata.has("images")) { + JSONArray imageUrls = metadata.getJSONArray("images"); + for (int i = 0; i < imageUrls.length(); i++) { + JSONObject imageObj = imageUrls.getJSONObject(i); + String imageUrl = imageObj.has("url") ? imageObj.getString("url") : "undefined"; + if (!imageUrl.contains("http://")) { + continue; + } + Uri imageURI = Uri.parse(imageUrl); + WebImage webImage = new WebImage(imageURI); + mediaMetadata.addImage(webImage); + } + } + return mediaMetadata; } /** * Media API - Calls play on the current media * @param callback */ - public void mediaPlay(ChromecastSessionCallback callback) { - chromecastMediaController.play(mApiClient, callback); + public void mediaPlay(CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + client.play().setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to play with code: " + result.getStatus().getStatusCode()); + } + } + }); + } + }); } /** * Media API - Calls pause on the current media * @param callback */ - public void mediaPause(ChromecastSessionCallback callback) { - chromecastMediaController.pause(mApiClient, callback); + public void mediaPause(CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + client.pause().setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to pause with code: " + result.getStatus().getStatusCode()); + } + } + }); + } + }); } /** @@ -234,8 +264,41 @@ public void mediaPause(ChromecastSessionCallback callback) { * @param resumeState - Resume state once seeking is complete: PLAYBACK_PAUSE or PLAYBACK_START * @param callback */ - public void mediaSeek(long seekPosition, String resumeState, ChromecastSessionCallback callback) { - chromecastMediaController.seek(seekPosition, resumeState, mApiClient, callback); + public void mediaSeek(long seekPosition, String resumeState, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + int resState; + switch (resumeState) { + case "PLAYBACK_START": + resState = MediaSeekOptions.RESUME_STATE_PLAY; + break; + case "PLAYBACK_PAUSE": + resState = MediaSeekOptions.RESUME_STATE_PAUSE; + break; + default: + resState = MediaSeekOptions.RESUME_STATE_UNCHANGED; + } + + client.seek(new MediaSeekOptions.Builder() + .setPosition(seekPosition) + .setResumeState(resState) + .build() + ).setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to seek with code: " + result.getStatus().getStatusCode()); + } + } + }); + } + }); } /** @@ -243,8 +306,25 @@ public void mediaSeek(long seekPosition, String resumeState, ChromecastSessionCa * @param level * @param callback */ - public void mediaSetVolume(double level, ChromecastSessionCallback callback) { - chromecastMediaController.setVolume(level, mApiClient, callback); + public void mediaSetVolume(double level, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + client.play().setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to set volume with code: " + result.getStatus().getStatusCode()); + } + } + }); + } + }); } /** @@ -252,16 +332,50 @@ public void mediaSetVolume(double level, ChromecastSessionCallback callback) { * @param muted * @param callback */ - public void mediaSetMuted(boolean muted, ChromecastSessionCallback callback) { - chromecastMediaController.setMuted(muted, mApiClient, callback); + public void mediaSetMuted(boolean muted, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + client.setStreamMute(muted).setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to mute/unmute with code: " + result.getStatus().getStatusCode()); + } + } + }); + } + }); } /** * Media API - Stops and unloads the current playing media * @param callback */ - public void mediaStop(ChromecastSessionCallback callback) { - chromecastMediaController.stop(mApiClient, callback); + public void mediaStop(CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + client.stop().setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to stop with code: " + result.getStatus().getStatusCode()); + } + } + }); + } + }); } /** @@ -270,26 +384,35 @@ public void mediaStop(ChromecastSessionCallback callback) { * @param textTrackStyle * @param callback */ - public void mediaEditTracksInfo(long[] activeTracksIds, JSONObject textTrackStyle, ChromecastSessionCallback callback) { - mRemoteMediaPlayer.setActiveMediaTracks(mApiClient, activeTracksIds) - .setResultCallback(new ResultCallback() { + public void mediaEditTracksInfo(long[] activeTracksIds, JSONObject textTrackStyle, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + client.setActiveMediaTracks(activeTracksIds).setResultCallback(new ResultCallback() { @Override - public void onResult(MediaChannelResult result) { - if (!result.getStatus().isSuccess()) { - callback.onError("Failed to set tracks with code: " + result.getStatus().getStatusCode()); + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to set active media tracks with code: " + result.getStatus().getStatusCode()); } } }); - - mRemoteMediaPlayer.setTextTrackStyle(mApiClient, ChromecastUtilities.parseTextTrackStyle(textTrackStyle)) - .setResultCallback(new ResultCallback() { + client.setTextTrackStyle(ChromecastUtilities.parseTextTrackStyle(textTrackStyle)).setResultCallback(new ResultCallback() { @Override - public void onResult(MediaChannelResult result) { - if (!result.getStatus().isSuccess()) { - callback.onError("Failed to set tracks style with code: " + result.getStatus().getStatusCode()); + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to set text track style with code: " + result.getStatus().getStatusCode()); } } }); + } + }); } @@ -298,14 +421,21 @@ public void onResult(MediaChannelResult result) { * @param volume * @param callback */ - public void setVolume(double volume, ChromecastSessionCallback callback) { - try { - Cast.CastApi.setVolume(mApiClient, volume); - callback.onSuccess(); - } catch (Exception e) { - e.printStackTrace(); - callback.onError(e.getMessage()); + public void setVolume(double volume, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; } + activity.runOnUiThread(new Runnable() { + public void run() { + try { + session.setVolume(volume); + callback.success(); + } catch (IOException e) { + callback.error("CHANNEL_ERROR"); + } + } + }); } /** @@ -313,133 +443,22 @@ public void setVolume(double volume, ChromecastSessionCallback callback) { * @param muted * @param callback */ - public void setMute(boolean muted, ChromecastSessionCallback callback) { - try { - Cast.CastApi.setMute(mApiClient, muted); - callback.onSuccess(); - } catch (Exception e) { - e.printStackTrace(); - callback.onError(e.getMessage()); - } - } - - - /** - * Connects to the device with all callbacks and things - */ - private void connectToDevice() { - try { - Cast.CastOptions.Builder apiOptionsBuilder = Cast.CastOptions.builder(this.device, this); - this.mApiClient = new GoogleApiClient.Builder(this.cordova.getActivity().getApplicationContext()) - .addApi(Cast.API, apiOptionsBuilder.build()) - .addConnectionCallbacks(this) - .addOnConnectionFailedListener(this) - .build(); - this.mApiClient.connect(); - } catch (Exception e) { - e.printStackTrace(); + public void setMute(boolean muted, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; } - } - - /** - * Launches the application and gets a new session - */ - private void launchApplication() { - Cast.CastApi.launchApplication(mApiClient, this.appId, false) - .setResultCallback(launchApplicationResultCallback); - } - - /** - * Attemps to join an already running session - */ - private void joinApplication() { - Cast.CastApi.joinApplication(this.mApiClient, this.appId, this.lastSessionId) - .setResultCallback(joinApplicationResultCallback); - } - - /** - * Connects to the remote media player on the receiver - * @throws IllegalStateException - * @throws IOException - */ - private void connectRemoteMediaPlayer() throws IllegalStateException, IOException { - Cast.CastApi.setMessageReceivedCallbacks(mApiClient, mRemoteMediaPlayer.getNamespace(), mRemoteMediaPlayer); - mRemoteMediaPlayer.requestStatus(mApiClient) - .setResultCallback(connectRemoteMediaPlayerCallback); - } - - /** - * launchApplication callback - */ - private ResultCallback launchApplicationResultCallback = new ResultCallback() { - @Override - public void onResult(ApplicationConnectionResult result) { - - ApplicationMetadata metadata = result.getApplicationMetadata(); - ChromecastSession.this.sessionId = result.getSessionId(); - ChromecastSession.this.displayName = metadata.getName(); - ChromecastSession.this.appImages = metadata.getImages(); - - Status status = result.getStatus(); - if (status.isSuccess()) { + activity.runOnUiThread(new Runnable() { + public void run() { try { - ChromecastSession.this.launchCallback.onSuccess(ChromecastSession.this); - connectRemoteMediaPlayer(); - ChromecastSession.this.isConnected = true; - } catch (IllegalStateException e) { - e.printStackTrace(); + session.setMute(muted); + callback.success(); } catch (IOException e) { - e.printStackTrace(); + callback.error("CHANNEL_ERROR"); } - } else { - ChromecastSession.this.isConnected = false; } - } - }; - - /** - * joinApplication callback - */ - private ResultCallback joinApplicationResultCallback = new ResultCallback() { - @Override - public void onResult(ApplicationConnectionResult result) { - Status status = result.getStatus(); - if (status.isSuccess()) { - try { - ApplicationMetadata metadata = result.getApplicationMetadata(); - ChromecastSession.this.sessionId = result.getSessionId(); - ChromecastSession.this.displayName = metadata.getName(); - ChromecastSession.this.appImages = metadata.getImages(); - - ChromecastSession.this.joinSessionCallback.onSuccess(ChromecastSession.this); - connectRemoteMediaPlayer(); - ChromecastSession.this.isConnected = true; - } catch (IllegalStateException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } else { - ChromecastSession.this.joinSessionCallback.onError(status.toString()); - ChromecastSession.this.isConnected = false; - } - } - }; - - /** - * connectRemoteMediaPlayer callback - */ - private ResultCallback connectRemoteMediaPlayerCallback = new ResultCallback() { - @Override - public void onResult(MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - ChromecastSession.this.onMediaUpdatedListener.onMediaUpdated(true, ChromecastSession.this.createMediaObject()); - /*ChromecastSession.this.onMediaUpdatedListener.onMediaLoaded(ChromecastSession.this.createMediaObject());*/ - } else { - System.out.println("Failed to request status."); - } - } - }; + }); + } /** * Creates a JSON representation of this session @@ -447,106 +466,62 @@ public void onResult(MediaChannelResult result) { */ public JSONObject createSessionObject() { JSONObject out = new JSONObject(); + try { - out.put("appId", this.appId); + out.put("appId", session.getApplicationMetadata().getApplicationId()); + out.put("appImages", createAppImagesObject()); + out.put("displayName", session.getApplicationMetadata().getName()); out.put("media", createMediaObject()); + out.put("receiver", createReceiverObject()); + out.put("sessionId", this.session.getSessionId()); - if (this.appImages != null) { - JSONArray appImages = new JSONArray(); - for (WebImage o : this.appImages) { - appImages.put(o.toString()); - } - } - - out.put("appImages", appImages); - out.put("sessionId", this.sessionId); - out.put("displayName", this.displayName); - - JSONObject receiver = new JSONObject(); - receiver.put("friendlyName", this.device.getFriendlyName()); - receiver.put("label", this.device.getDeviceId()); - - JSONObject volume = new JSONObject(); - try { - volume.put("level", Cast.CastApi.getVolume(mApiClient)); - volume.put("muted", Cast.CastApi.isMute(mApiClient)); - } catch (JSONException e) { - e.printStackTrace(); - } - - receiver.put("volume", volume); - out.put("receiver", receiver); } catch (JSONException e) { e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); } return out; } - /** - * Creates a JSON representation of all Tracks available in the current media. - * @return - */ - private JSONArray createMediaInfoTracks() { - JSONArray out = new JSONArray(); - - MediaStatus mediaStatus = mRemoteMediaPlayer.getMediaStatus(); - MediaInfo mediaInfo = mediaStatus.getMediaInfo(); - - if (mediaInfo.getMediaTracks() == null) { - return out; - } - - for (MediaTrack track : mediaInfo.getMediaTracks()) { - JSONObject jsonTrack = new JSONObject(); - - try { - jsonTrack.put("trackId", track.getId()); - jsonTrack.put("customData", track.getCustomData()); - jsonTrack.put("language", track.getLanguage()); - jsonTrack.put("name", track.getName()); - jsonTrack.put("subtype", ChromecastUtilities.getTrackSubtype(track)); - jsonTrack.put("trackContentId", track.getContentId()); - jsonTrack.put("trackContentType", track.getContentType()); - jsonTrack.put("type", ChromecastUtilities.getTrackType(track)); - - out.put(jsonTrack); - } catch (JSONException e) { - e.printStackTrace(); + private JSONObject createAppImagesObject() { + JSONObject out = new JSONObject(); + try { + MediaMetadata metadata = client.getMediaInfo().getMetadata(); + List images = metadata.getImages(); + JSONArray appImages = new JSONArray(); + if (images != null) { + for (WebImage o : images) { + appImages.put(o.toString()); + } } + } catch (NullPointerException e) { + e.printStackTrace(); } - return out; } - - /** - * Creates a JSON representation of current MediaInfo of the session. - * @return - */ - private JSONObject createMediaInfoObject() { + private JSONObject createReceiverObject() { JSONObject out = new JSONObject(); - - MediaStatus mediaStatus = mRemoteMediaPlayer.getMediaStatus(); - MediaInfo mediaInfo = mediaStatus.getMediaInfo(); - try { - out.put("contentId", mediaInfo.getContentId()); - out.put("contentType", mediaInfo.getContentType()); - out.put("customData", mediaInfo.getCustomData()); - out.put("duration", mediaInfo.getStreamDuration() / 1000.0); - out.put("streamType", ChromecastUtilities.getMediaInfoStreamType(mediaInfo)); - out.put("tracks", this.createMediaInfoTracks()); - out.put("textTrackStyle", ChromecastUtilities.createTextTrackObject(mediaInfo.getTextTrackStyle())); + out.put("friendlyName", this.session.getCastDevice().getFriendlyName()); + out.put("label", this.session.getCastDevice().getDeviceId()); - // TODO: Check if it's useful - //out.put("metadata", mediaInfo.getMetadata()); + JSONObject volume = new JSONObject(); + try { + volume.put("level", session.getVolume()); + volume.put("muted", session.isMute()); + } catch (JSONException e) { + e.printStackTrace(); + } + out.put("volume", volume); - return out; } catch (JSONException e) { e.printStackTrace(); - return out; + } catch (NullPointerException e) { + e.printStackTrace(); } + return out; } /** @@ -556,27 +531,32 @@ private JSONObject createMediaInfoObject() { private JSONObject createMediaObject() { JSONObject out = new JSONObject(); - MediaStatus mediaStatus = mRemoteMediaPlayer.getMediaStatus(); - if (mediaStatus == null) { - return out; - } - try { + MediaStatus mediaStatus = client.getMediaStatus(); + + + // TODO: Missing attributes are commented out. + // These are returned by the chromecast desktop SDK, we should probbaly return them too + //out.put("breakStatus",); out.put("currentItemId", mediaStatus.getCurrentItemId()); out.put("currentTime", mediaStatus.getStreamPosition() / 1000.0); out.put("customData", mediaStatus.getCustomData()); + //out.put("extendedStatus",); out.put("idleReason", ChromecastUtilities.getMediaIdleReason(mediaStatus)); + //out.put("items", mediaStatus.getQueueItems()); + //out.put("liveSeekableRange",); out.put("loadingItemId", mediaStatus.getLoadingItemId()); out.put("media", this.createMediaInfoObject()); out.put("mediaSessionId", 1); out.put("playbackRate", mediaStatus.getPlaybackRate()); out.put("playerState", ChromecastUtilities.getMediaPlayerState(mediaStatus)); out.put("preloadedItemId", mediaStatus.getPreloadedItemId()); - out.put("sessionId", this.sessionId); - - // TODO: We can add Queue Items to make the plugin more generic - //out.put("items", mediaStatus.getQueueItems()); + //out.put("queueData", ); //out.put("repeatMode", mediaStatus.getQueueRepeatMode()); + out.put("sessionId", this.session.getSessionId()); + //out.put("supportedMediaCommands", ); + //out.put("videoInfo", ); + JSONObject volume = new JSONObject(); volume.put("level", mediaStatus.getStreamVolume()); @@ -592,110 +572,90 @@ private JSONObject createMediaObject() { out.put("activeTrackIds", activeTracks); } - return out; } catch (JSONException e) { e.printStackTrace(); - return out; + } catch (NullPointerException e) { + e.printStackTrace(); } - } - /* GoogleApiClient.ConnectionCallbacks implementation - * Called when we successfully connect to the API - * (non-Javadoc) - * @see com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks#onConnected(android.os.Bundle) - */ - @Override - public void onConnected(Bundle connectionHint) { - if (this.joinInsteadOfConnecting) { - this.joinApplication(); - } else { - this.launchApplication(); - } + return out; } - /* GoogleApiClient.ConnectionCallbacks implementation - * (non-Javadoc) - * @see com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks#onConnectionSuspended(android.os.Bundle) + /** + * Creates a JSON representation of all Tracks available in the current media. + * @return */ - @Override - public void onConnectionSuspended(int cause) { - if (this.onSessionUpdatedListener != null) { - this.isConnected = false; - this.onSessionUpdatedListener.onSessionUpdated(false, this.createSessionObject()); - } - } + private JSONArray createMediaInfoTracks() { + JSONArray out = new JSONArray(); - /* - * GoogleApiClient.OnConnectionFailedListener implementation - * When Google API fails to connect. - * (non-Javadoc) - * @see com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener#onConnectionFailed(com.google.android.gms.common.ConnectionResult) - */ - @Override - public void onConnectionFailed(ConnectionResult result) { - if (this.launchCallback != null) { - this.isConnected = false; - this.launchCallback.onError("channel_error"); - } - } + try { + MediaStatus mediaStatus = client.getMediaStatus(); + MediaInfo mediaInfo = mediaStatus.getMediaInfo(); - /** - * Cast.Listener implementation - * When Chromecast application status changed - */ - @Override - public void onApplicationStatusChanged() { - if (this.onSessionUpdatedListener != null) { - ChromecastSession.this.isConnected = true; - this.onSessionUpdatedListener.onSessionUpdated(true, createSessionObject()); - } - } + if (mediaInfo.getMediaTracks() == null) { + return out; + } - /** - * Cast.Listener implementation - * When the volume is changed on the Chromecast - */ - @Override - public void onVolumeChanged() { - if (this.onSessionUpdatedListener != null) { - this.onSessionUpdatedListener.onSessionUpdated(true, createSessionObject()); + for (MediaTrack track : mediaInfo.getMediaTracks()) { + JSONObject jsonTrack = new JSONObject(); + + + // TODO: Missing attributes are commented out. + // These are returned by the chromecast desktop SDK, we should probbaly return them too + + jsonTrack.put("trackId", track.getId()); + jsonTrack.put("customData", track.getCustomData()); + jsonTrack.put("language", track.getLanguage()); + jsonTrack.put("name", track.getName()); + jsonTrack.put("subtype", ChromecastUtilities.getTrackSubtype(track)); + jsonTrack.put("trackContentId", track.getContentId()); + jsonTrack.put("trackContentType", track.getContentType()); + jsonTrack.put("type", ChromecastUtilities.getTrackType(track)); + + out.put(jsonTrack); + } + } catch (JSONException e) { + e.printStackTrace(); } + + return out; } + /** - * Cast.Listener implementation - * When the application is disconnected + * Creates a JSON representation of current MediaInfo of the session. + * @return */ - @Override - public void onApplicationDisconnected(int errorCode) { - if (this.onSessionUpdatedListener != null) { - this.isConnected = false; - this.onSessionUpdatedListener.onSessionUpdated(false, this.createSessionObject()); - } - } + private JSONObject createMediaInfoObject() { + JSONObject out = new JSONObject(); - @Override - public void onMetadataUpdated() { - if (this.onMediaUpdatedListener != null) { - this.onMediaUpdatedListener.onMediaUpdated(true, this.createMediaObject()); - } - } + try { + MediaStatus mediaStatus = client.getMediaStatus(); + MediaInfo mediaInfo = mediaStatus.getMediaInfo(); - @Override - public void onStatusUpdated() { - if (this.onMediaUpdatedListener != null) { - this.onMediaUpdatedListener.onMediaUpdated(true, this.createMediaObject()); - } - } - public String getSessionId() { - return this.sessionId; - } + // TODO: Missing attributes are commented out. + // These are returned by the chromecast desktop SDK, we should probbaly return them too + //out.put("breakClips",); + //out.put("breaks",); + out.put("contentId", mediaInfo.getContentId()); + out.put("contentType", mediaInfo.getContentType()); + out.put("customData", mediaInfo.getCustomData()); + //out.put("idleReason",); + //out.put("items",); + out.put("duration", mediaInfo.getStreamDuration() / 1000.0); + //out.put("mediaCategory",); + out.put("streamType", ChromecastUtilities.getMediaInfoStreamType(mediaInfo)); + out.put("tracks", this.createMediaInfoTracks()); + out.put("textTrackStyle", ChromecastUtilities.createTextTrackObject(mediaInfo.getTextTrackStyle())); - @Override - public void onMessageReceived(CastDevice castDevice, String namespace, String message) { - if (this.onSessionUpdatedListener != null) { - this.onSessionUpdatedListener.onMessage(this, namespace, message); + // TODO: Check if it's useful + //out.put("metadata", mediaInfo.getMetadata()); + } catch (JSONException e) { + e.printStackTrace(); } + + return out; } -} \ No newline at end of file + +} diff --git a/src/android/ChromecastSessionCallback.java b/src/android/ChromecastSessionCallback.java deleted file mode 100644 index fa85d61..0000000 --- a/src/android/ChromecastSessionCallback.java +++ /dev/null @@ -1,10 +0,0 @@ -package acidhax.cordova.chromecast; - -public abstract class ChromecastSessionCallback { - public void onSuccess() { - onSuccess(null); - } - - abstract void onSuccess(Object object); - abstract void onError(String reason); -} \ No newline at end of file diff --git a/tests/tests.js b/tests/tests.js index 305df57..142e611 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -99,7 +99,7 @@ exports.defineAutoTests = function () { expect('success').toBeDefined(); done(); }, function (err) { - expect(err).toEqual('no_error_no'); + fail(err.code + ': ' + err.description); done(); }); }); @@ -110,39 +110,48 @@ exports.defineAutoTests = function () { it('SPEC_00400 initialize should succeed (default receiver)', function (done) { var step = 1; var sessionRequest = new chrome.cast.SessionRequest(applicationID_default); - var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) {}, function receiverListener (available) { - switch (step) { - case 1: - // The first update must be unavailable - if (available === 'unavailable') { - step++; + var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) { + fail('should not receive a session (make sure there is no active cast session when starting the tests)'); + }, function receiverListener (available) { + if (step === 1) { + fail('SPEC_00400 - Did not hit initialize Step 1 first'); + } + if (step === 2) { + // Step 2 - We must get the unavailable notification + if (available !== 'unavailable') { + fail('SPEC_00400 - Step 2 - Hit receiver listener with non-unavailable status'); } else { - expect(available).toEqual('unavailable'); + step++; + } + } + if (step === 3) { + // Step 3 - We are allowed to receive multiple unavailable until we receive the first available in this step + if (available !== 'unavailable' && available !== 'available') { + fail('SPEC_00400 - Step 3 - Hit receiver listener with incorrect status'); } - break; - case 2: - // The second step waits until we receive available - // Can receive unavailable while waiting if (available === 'available') { - expect(available).toEqual('available'); - step++; + expect('success').toBeDefined(); done(); - } else { - expect(available).toEqual('unavailable'); } - break; } }); - chrome.cast.initialize(apiConfig, function () {}, function (err) { - expect(err).toEqual('no_error_no'); + chrome.cast.initialize(apiConfig, function () { + // Step 1 + if (step !== 1) { + fail('SPEC_00400 - Step 1 - Expected to hit this first, but did not'); + } + step++; + }, function (err) { + fail(err.code + ': ' + err.description); done(); }); }); it('requestSession click outside of dialog should return the cancel error', function (done) { alert('---TEST INSTRUCTION---\nPlease click outside of the next dialog to dismiss it.'); - chrome.cast.requestSession(function () { - fail('We should not reach here on dismiss'); + chrome.cast.requestSession(function (session) { + fail('We should not reach here on dismiss (make sure you cancelled the dialog for this test!)'); + done(); }, function (err) { expect(err instanceof chrome.cast.Error).toBeTruthy(); expect(err.code).toBe(chrome.cast.ErrorCode.CANCEL); @@ -160,17 +169,70 @@ exports.defineAutoTests = function () { // Run all the session related tests Promise.resolve(session) .then(sessionProperties) + .then(newPageSharesSession) + .then(sessionProperties) .then(loadMedia) .then(stopSession) .then(done); }, function (err) { - expect(err).toEqual('no_error_no'); + fail(err.code + ': ' + err.description); done(); }); }, USER_INTERACTION_TIMEOUT); + /** + * When on a new page, it will call initialize again + * This should result in the session being passed to the + * session listener as long as teh requested appId does + * not change. + */ + function newPageSharesSession (session) { + return new Promise(function (resolve) { + var step = 1; + var sessionRequest = new window.chrome.cast.SessionRequest(window.chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID); + var apiConfig = new window.chrome.cast.ApiConfig(sessionRequest, function (session) { + if (step !== 4) { + fail('newPageSharesSession.js - Did not hit session listener as 4th step'); + } + resolve(session); + }, function receiverListener (available) { + if (step === 1) { + fail('newPageSharesSession.js - Did not hit initialize Step 1 first'); + } + if (step === 2) { + // Step 2 - We must get the unavailable notification + if (available !== 'unavailable') { + fail('newPageSharesSession.js - Step 2 - Hit receiver listener with non-unavailable status'); + } else { + step++; + } + } + if (step === 3) { + // Step 3 - We are allowed to receive multiple unavailable until we receive the first available in this step + if (available !== 'unavailable' && available !== 'available') { + fail('newPageSharesSession.js - Step 3 - Hit receiver listener with incorrect status'); + } + if (available === 'available') { + step++; + } + } + }); + + // Initialize + window.chrome.cast.initialize(apiConfig, function () { + // Step 1 + if (step !== 1) { + fail('newPageSharesSession.js - Step 1 - Expected to hit this first, but did not'); + } + step++; + }, function (err) { + fail(err.code + ': ' + err.description); + }); + }); + } + function sessionProperties (session) { return new Promise(function (resolve) { expect(session instanceof chrome.cast.Session).toBeTruthy(); @@ -211,7 +273,7 @@ exports.defineAutoTests = function () { }); }, function (err) { - expect(err).toEqual('no_error_no'); + fail(err.code + ': ' + err.description); resolve(); }); }); @@ -235,7 +297,7 @@ exports.defineAutoTests = function () { media.pause(null, function () { resolve(media); }, function (err) { - expect(err).toEqual('no_error_no'); + fail(err.code + ': ' + err.description); resolve(); }); }, 500); @@ -248,7 +310,7 @@ exports.defineAutoTests = function () { media.play(null, function () { resolve(media); }, function (err) { - expect(err).toEqual('no_error_no'); + fail(err.code + ': ' + err.description); resolve(); }); }, 500); @@ -263,7 +325,7 @@ exports.defineAutoTests = function () { media.seek(request, function () { resolve(media); }, function (err) { - expect(err).toEqual('no_error_no'); + fail(err.code + ': ' + err.description); resolve(); }); }, 500); @@ -281,7 +343,7 @@ exports.defineAutoTests = function () { media.setVolume(request, function () { resolve(media); }, function (err) { - expect(err).toEqual('no_error_no'); + fail(err.code + ': ' + err.description); resolve(); }); }); @@ -293,7 +355,7 @@ exports.defineAutoTests = function () { media.setVolume(request, function () { resolve(media); }, function (err) { - expect(err).toEqual('no_error_no'); + fail(err.code + ': ' + err.description); resolve(); }); }); @@ -305,7 +367,7 @@ exports.defineAutoTests = function () { media.setVolume(request, function () { resolve(media); }, function (err) { - expect(err).toEqual('no_error_no'); + fail(err.code + ': ' + err.description); resolve(); }); }); @@ -316,7 +378,7 @@ exports.defineAutoTests = function () { media.stop(null, function () { resolve(media); }, function (err) { - expect(err).toEqual('no_error_no'); + fail(err.code + ': ' + err.description); resolve(); }); }); @@ -327,7 +389,7 @@ exports.defineAutoTests = function () { session.stop(function () { resolve(session); }, function (err) { - expect(err).toEqual('no_error_no'); + fail(err.code + ': ' + err.description); resolve(); }); }); @@ -335,11 +397,49 @@ exports.defineAutoTests = function () { }); - it('SPEC_01200 should pass auto tests on second run', function () { - alert('---TEST INSTRUCTION---\nPlease hit "Reset App" at the top and ensure all ' - + 'tests pass again. (This simulates navigation to a new page where the ' - + 'plugin is not loaded from scratch again).'); - expect('success').toBeDefined(); + /** + * This tests that after the session has been stopped that we do not receive + * the old (dead) session on initialize if we ask to initialize again with + * the same app id. + */ + it('SPEC_01400 initialize should succeed (default receiver) but no session', function (done) { + var step = 1; + var sessionRequest = new chrome.cast.SessionRequest(applicationID_default); + var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) { + fail('SPEC_01400 should not receive a session'); + }, function receiverListener (available) { + if (step === 1) { + fail('SPEC_01400 - Did not hit initialize Step 1 first'); + } + if (step === 2) { + // Step 2 - We must get the unavailable notification + if (available !== 'unavailable') { + fail('SPEC_01400 - Step 2 - Hit receiver listener with non-unavailable status'); + } else { + step++; + } + } + if (step === 3) { + // Step 3 - We are allowed to receive multiple unavailable until we receive the first available in this step + if (available !== 'unavailable' && available !== 'available') { + fail('SPEC_01400 - Step 3 - Hit receiver listener with incorrect status'); + } + if (available === 'available') { + expect('success').toBeDefined(); + done(); + } + } + }); + chrome.cast.initialize(apiConfig, function () { + // Step 1 + if (step !== 1) { + fail('SPEC_01400 - Step 1 - Expected to hit this first, but did not'); + } + step++; + }, function (err) { + fail(err.code + ': ' + err.description); + done(); + }); }); }); diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 44ba370..5957ff2 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -547,15 +547,16 @@ chrome.cast.initialize = function (apiConfig, successCallback, errorCallback) { return; } - _sessionListener = apiConfig.sessionListener; _autoJoinPolicy = apiConfig.autoJoinPolicy; _defaultActionPolicy = apiConfig.defaultActionPolicy; - _receiverListener = apiConfig.receiverListener; _sessionRequest = apiConfig.sessionRequest; execute('initialize', _sessionRequest.appId, _autoJoinPolicy, _defaultActionPolicy, function (err) { if (!err) { successCallback(); + // Only set the listeners once initialize has completed successfully + _sessionListener = apiConfig.sessionListener; + _receiverListener = apiConfig.receiverListener; } else { handleError(err, errorCallback); } @@ -595,7 +596,6 @@ chrome.cast.requestSession = function (successCallback, errorCallback, opt_sessi } successCallback(session); - _sessionListener(session); /* Fix - Already has a sessionListener */ } else { handleError(err, errorCallback); } @@ -780,7 +780,9 @@ chrome.cast.Session.prototype.loadMedia = function (loadRequest, successCallback _currentMedia.media.tracks = []; - obj.media.tracks.forEach((track) => { + var track; + for (var i = 0; i < obj.media.tracks.length; i++) { + track = obj.media.tracks[i]; var newTrack = new chrome.cast.media.Track(track.trackId, track.type); newTrack.customData = track.customData || null; newTrack.language = track.language || null; @@ -790,7 +792,7 @@ chrome.cast.Session.prototype.loadMedia = function (loadRequest, successCallback newTrack.trackContentType = track.trackContentType || null; _currentMedia.media.tracks.push(newTrack); - }); + } successCallback(_currentMedia); @@ -1238,17 +1240,15 @@ chrome.cast._ = { console.log('mediaLoaded --- but there is no session tied to it', media); } }, + sessionListener: function (javaSession) { + var session = getJsSession(javaSession); + _sessionListener && _sessionListener(session); + }, sessionJoined: function (obj) { - var sessionId = obj.sessionId; - var appId = obj.appId; - var displayName = obj.displayName; - var appImages = obj.appImages || []; - var receiver = new chrome.cast.Receiver(obj.receiver.label, obj.receiver.friendlyName, obj.receiver.capabilities || [], obj.receiver.volume || null); - - var session = _sessions[sessionId] = new chrome.cast.Session(sessionId, appId, displayName, appImages, receiver); + var session = getJsSession(obj); if (obj.media && obj.media.sessionId) { - _currentMedia = new chrome.cast.media.Media(sessionId, obj.media.mediaSessionId); + _currentMedia = new chrome.cast.media.Media(session.sessionId, obj.media.mediaSessionId); _currentMedia.currentTime = obj.media.currentTime; _currentMedia.playerState = obj.media.playerState; _currentMedia.media = obj.media.media; @@ -1266,6 +1266,21 @@ chrome.cast._ = { module.exports = chrome.cast; +function getJsSession (javaSession) { + return new chrome.cast.Session( + javaSession.sessionId, + javaSession.appId, + javaSession.displayName, + javaSession.appImages || [], + new chrome.cast.Receiver( + javaSession.receiver.label, + javaSession.receiver.friendlyName, + javaSession.receiver.capabilities || [], + javaSession.receiver.volume || null + ) + ); +} + function execute (action) { var args = [].slice.call(arguments); args.shift(); From 14f236ebcb28bca1abbb6dfc1e2cf0bfdd8d905a Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 9 Sep 2019 05:00:11 -0600 Subject: [PATCH 026/166] Added a java file style checker that enforces 4 spaces. (So I changed all the tabs to spaces in the java files. Sorry commit history. o.o) It was a lot of work, so I disable a couple of the java checkstyle rules. They are listed in the first comment in check_style.xml. Issue #36 progress --- .github/pull_request_template.md | 2 +- README.md | 2 +- check_style.xml | 196 ++++ package.json | 6 +- src/android/CastOptionsProvider.java | 22 +- src/android/Chromecast.java | 934 +++++++++--------- src/android/ChromecastConnection.java | 701 +++++++------- src/android/ChromecastSession.java | 1266 +++++++++++++------------ src/android/ChromecastUtilities.java | 10 +- 9 files changed, 1704 insertions(+), 1435 deletions(-) create mode 100644 check_style.xml diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index dee554e..d04bb4b 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -28,6 +28,6 @@ Thanks! - [ ] I've updated the documentation as necessary - [ ] If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct [keyword to close issues using keywords](https://help.github.com/articles/closing-issues-using-keywords/)) -- [ ] I've run `npm test` and no errors were found +- [ ] I've run `npm run style` and no errors were found - [ ] I've run the `test-framework` tests for Android (See Readme) - [ ] I added automated test coverage as appropriate for this change \ No newline at end of file diff --git a/README.md b/README.md index bf5d845..a25484f 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ The project is now pretty much feature complete - the only things that will poss ## Formatting -* Run `npm test` (from the plugin directory) +* Run `npm run style` (from the plugin directory) * If you get `Error: Cannot find module '\node_modules\eslint\bin\eslint'` * Run `npm install` * If it finds any formatting errors you can try and automatically fix them with: diff --git a/check_style.xml b/check_style.xml new file mode 100644 index 0000000..cec5dc6 --- /dev/null +++ b/check_style.xml @@ -0,0 +1,196 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/package.json b/package.json index db2ceb2..6e1e6f0 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,7 @@ "name": "cordova-plugin-chromecast", "version": "1.0.0", "scripts": { - "test": "npm run eslint", - "eslint": "node node_modules/eslint/bin/eslint src && node node_modules/eslint/bin/eslint www && node node_modules/eslint/bin/eslint tests" + "style": "node node_modules/eslint/bin/eslint src && node node_modules/eslint/bin/eslint www && node node_modules/eslint/bin/eslint tests && node ./node_modules/java-checkstyle/bin/index.js ./src/android/ -c ./check_style.xml" }, "author": "", "license": "dual GPLv3/MPLv2", @@ -16,6 +15,7 @@ "eslint-plugin-import": "~2.3.0", "eslint-plugin-node": "~5.0.0", "eslint-plugin-promise": "~3.5.0", - "eslint-plugin-standard": "~3.0.1" + "eslint-plugin-standard": "~3.0.1", + "java-checkstyle": "0.0.1" } } diff --git a/src/android/CastOptionsProvider.java b/src/android/CastOptionsProvider.java index 7c42b84..535dad4 100644 --- a/src/android/CastOptionsProvider.java +++ b/src/android/CastOptionsProvider.java @@ -8,15 +8,15 @@ import android.content.Context; -public class CastOptionsProvider implements OptionsProvider { +public final class CastOptionsProvider implements OptionsProvider { - @Override - public CastOptions getCastOptions(Context context) { - return new CastOptions.Builder() - .build(); - } - @Override - public List getAdditionalSessionProviders(Context context) { - return null; - } -} \ No newline at end of file + @Override + public CastOptions getCastOptions(Context context) { + return new CastOptions.Builder() + .build(); + } + @Override + public List getAdditionalSessionProviders(Context context) { + return null; + } +} diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 7b2b65d..85c9ead 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -22,469 +22,489 @@ import com.google.android.gms.cast.framework.CastSession; import com.google.android.gms.cast.framework.media.RemoteMediaClient; -public class Chromecast extends CordovaPlugin { - - private static final String TAG = "Chromecast"; - - private ChromecastConnection connection; - private volatile ChromecastSession media; - - @Override - protected void pluginInitialize() { - super.pluginInitialize(); - - this.media = new ChromecastSession(cordova.getActivity(), remoteMediaClientCallback); - this.connection = new ChromecastConnection(cordova.getActivity(), this.media, new ChromecastConnection.ConnectionListener() { - @Override - public void onDisconnected(int reason) { - sendJavascript("chrome.cast.Session.prototype._update(false, {});"); -// sendJavascript("chrome.cast._.sessionUpdated(false, " + session.toString() + ");"); - } - }); - } - - @Override - public boolean execute(String action, JSONArray args, CallbackContext cbContext) throws JSONException { - try { - Method[] list = this.getClass().getMethods(); - Method methodToExecute = null; - for (Method method : list) { - if (method.getName().equals(action)) { - Type[] types = method.getGenericParameterTypes(); - // +1 is the cbContext - if (args.length() + 1 == types.length) { - boolean isValid = true; - for (int i = 0; i < args.length(); i++) { - Class arg = args.get(i).getClass(); - if (types[i] == arg) { - isValid = true; - } else { - isValid = false; - break; - } - } - if (isValid) { - methodToExecute = method; - break; - } - } - } - } - if (methodToExecute != null) { - Type[] types = methodToExecute.getGenericParameterTypes(); - Object[] variableArgs = new Object[types.length]; - for (int i = 0; i < args.length(); i++) { - variableArgs[i] = args.get(i); - } - variableArgs[variableArgs.length - 1] = cbContext; - Class r = methodToExecute.getReturnType(); - if (r == boolean.class) { - return (Boolean) methodToExecute.invoke(this, variableArgs); - } else { - methodToExecute.invoke(this, variableArgs); - return true; - } - } else { - return false; - } - } catch (IllegalAccessException e) { - e.printStackTrace(); - return false; - } catch (IllegalArgumentException e) { - e.printStackTrace(); - return false; - } catch (InvocationTargetException e) { - e.printStackTrace(); - return false; - } - } - - /** - * Do everything you need to for "setup" - calling back sets the isAvailable and lets every function on the - * javascript side actually do stuff. - * @param callbackContext - */ - public boolean setup(CallbackContext callbackContext) { - callbackContext.success(); - return true; - } - - /** - * Initialize all of the MediaRouter stuff with the AppId - * For now, ignore the autoJoinPolicy and defaultActionPolicy; those will come later - * @param appId The appId we're going to use for ALL session requests - * @param autoJoinPolicy tab_and_origin_scoped | origin_scoped | page_scoped - * @param defaultActionPolicy create_session | cast_this_tab - * @param callbackContext - */ - public boolean initialize(final String appId, String autoJoinPolicy, String defaultActionPolicy, final CallbackContext callbackContext) { - - this.connection.initialize(appId, new ChromecastConnection.Callback() { - @Override - public void run() { - callbackContext.success(); - - // Send receiver unavailable update while the new routeSelector is built. - // This matches the Chrome Desktop SDK behavior. - sendReceiverUpdate(false); - - - // See if there are any available routes - ChromecastConnection.ScanCallback scanCallback = new ChromecastConnection.ScanCallback() { - @Override - public void onRouteUpdate(RouteInfo route) { - // We found at least 1 route! so stop the scan - connection.stopScan(this); - - // and send out receiver available - sendReceiverUpdate(true); - - // Attempt to rejoin existing session if exists - connection.rejoin(new ChromecastConnection.JoinCallback() { - @Override - public void onJoin(CastSession session) { - // If we were able to join that means the client likely navigated to - // a new page and the code has called initialize again - // so, send out the session - sendJavascript("chrome.cast._.sessionListener(" + media.createSessionObject().toString() + ");"); - } - - @Override - public void onError(String errorCode) { - Log.d(TAG, "Error rejoining session: " + errorCode); - } - }); - } - }; - connection.scanForRoutes(scanCallback); - - // Also start a time out to cancel the scan - // after 5 seconds to save power - new Handler().postDelayed(new Runnable() { - @Override - public void run() { - connection.stopScan(scanCallback); - } - }, 5000); - } - }); - - return true; - } - - /** - * Request the session for the previously sent appId - * THIS IS WHAT LAUNCHES THE CHROMECAST PICKER - * or, if we already have a session launch the connection options - * dialog which will have the option to stop casting at minimum. - * @param callbackContext - */ - public boolean requestSession(final CallbackContext callbackContext) { - connection.showConnectionDialog(new ChromecastConnection.JoinCallback() { - @Override - public void onJoin(CastSession session) { - callbackContext.success(media.createSessionObject()); - } - public void onError(String errorCode) { - if (errorCode.equals("CANCEL")) { - callbackContext.error("cancel"); - } else { - // TODO maybe handle some of the error codes better - callbackContext.error("SESSION_ERROR"); - } - } - }); - return true; - } - - /** - * Selects a route by its id - * @param routeId - * @param callbackContext - * @return - */ - public boolean selectRoute(final String routeId, final CallbackContext callbackContext) { - connection.join(routeId, new ChromecastConnection.JoinCallback() { - @Override - public void onJoin(CastSession castSession) { - callbackContext.success(media.createSessionObject()); - } - - @Override - public void onError(String errorCode) { - callbackContext.error(errorCode); - } - }); - return true; - } - - /** - * Set the volume level on the receiver - this is a Chromecast volume, not a Media volume - * @param newLevel - */ - public boolean setReceiverVolumeLevel(Double newLevel, CallbackContext callbackContext) { - this.media.setVolume(newLevel, callbackContext); - return true; - } - - public boolean setReceiverVolumeLevel(Integer newLevel, CallbackContext callbackContext) { - return this.setReceiverVolumeLevel(newLevel.doubleValue(), callbackContext); - } - - /** - * Sets the muted boolean on the receiver - this is a Chromecast mute, not a Media mute - * @param muted - * @param callbackContext - */ - public boolean setReceiverMuted(Boolean muted, CallbackContext callbackContext) { - this.media.setMute(muted, callbackContext); - return true; - } - - /** - * Stop the session! Disconnect! All of that jazz! - * @param callbackContext [description] - */ - public boolean stopSession(CallbackContext callbackContext) { - connection.kill(); - return true; - } - - /** - * Send a custom message to the receiver - we don't need this just yet... it was just simple to implement on the js side - * @param namespace - * @param message - * @param callbackContext - */ - public boolean sendMessage(String namespace, String message, final CallbackContext callbackContext) { - this.media.sendMessage(namespace, message, callbackContext); - return true; - } - - - /** - * Adds a listener to a specific namespace - * @param namespace - * @param callbackContext - * @return - */ - public boolean addMessageListener(String namespace, CallbackContext callbackContext) { - this.media.addMessageListener(namespace); -// sendJavascript("chrome.cast._.onMessage('" + session.getSessionId() + "', '" + namespace + "', '" + message.replace("\\", "\\\\") + "')"); - callbackContext.success(); - return true; - } - - /** - * Loads some media on the Chromecast using the media APIs - * @param contentId The URL of the media item - * @param contentType MIME type of the content - * @param duration Duration of the content - * @param streamType buffered | live | other - * @param autoPlay Whether or not to automatically start playing the media - * @param currentTime Where to begin playing from - * @param callbackContext - */ - public boolean loadMedia(String contentId, JSONObject customData, String contentType, Integer duration, String streamType, Boolean autoPlay, Double currentTime, JSONObject metadata, JSONObject textTrackStyle, final CallbackContext callbackContext) { - this.media.loadMedia(contentId, customData, contentType, duration, streamType, autoPlay, currentTime, metadata, textTrackStyle, callbackContext); - return true; -// sendJavascript("chrome.cast._.mediaLoaded(true, " + media.toString() + ");"); - } - - public boolean loadMedia(String contentId, JSONObject customData, String contentType, Integer duration, String streamType, Boolean autoPlay, Integer currentTime, JSONObject metadata, JSONObject textTrackStyle, final CallbackContext callbackContext) { - return this.loadMedia(contentId, customData, contentType, duration, streamType, autoPlay, new Double(currentTime.doubleValue()), metadata, textTrackStyle, callbackContext); - } - - /** - * Play on the current media in the current session - * @param callbackContext - * @return - */ - public boolean mediaPlay(CallbackContext callbackContext) { - media.mediaPlay(callbackContext); - return true; - } - - /** - * Pause on the current media in the current session - * @param callbackContext - * @return - */ - public boolean mediaPause(CallbackContext callbackContext) { - media.mediaPause(callbackContext); - return true; - } - - /** - * Seeks the current media in the current session - * @param seekTime - * @param resumeState - * @param callbackContext - * @return - */ - public boolean mediaSeek(Integer seekTime, String resumeState, CallbackContext callbackContext) { - media.mediaSeek(seekTime.longValue() * 1000, resumeState, callbackContext); - return true; - } - - - /** - * Set the volume on the media - * @param level - * @param callbackContext - * @return - */ - public boolean setMediaVolume(Double level, CallbackContext callbackContext) { - media.mediaSetVolume(level, callbackContext); - return true; - } - - /** - * Set the muted on the media - * @param muted - * @param callbackContext - * @return - */ - public boolean setMediaMuted(Boolean muted, CallbackContext callbackContext) { - media.mediaSetMuted(muted, callbackContext); - return true; - } - - /** - * Stops the current media! - * @param callbackContext - * @return - */ - public boolean mediaStop(CallbackContext callbackContext) { - media.mediaStop(callbackContext); - return true; - } - - /** - * Handle Track changes. - * @param activeTrackIds track Ids to set. - * @param textTrackStyle text track style to set. - * @param callbackContext - * @return - */ - public boolean mediaEditTracksInfo(JSONArray activeTrackIds, JSONObject textTrackStyle, final CallbackContext callbackContext) { - long[] trackIds = new long[activeTrackIds.length()]; - - try { - for (int i = 0; i < activeTrackIds.length(); i++) { - trackIds[i] = activeTrackIds.getLong(i); - } - } catch (JSONException ignored) { - LOG.e(TAG, "Wrong format in activeTrackIds"); - } - - this.media.mediaEditTracksInfo(trackIds, textTrackStyle, callbackContext); - return true; - } - - /** - * Stops the session - * @param callbackContext - * @return - */ - public boolean sessionStop(CallbackContext callbackContext) { - connection.kill(); - callbackContext.success(); - return true; - } - - /** - * Stops the session - * @param callbackContext - * @return - */ - public boolean sessionLeave(CallbackContext callbackContext) { - connection.leave(); - callbackContext.success(); - return true; - } - - public boolean emitAllRoutes(CallbackContext callbackContext) { - // TODO will use connection.scanForRoutes(); - - return true; - } +public final class Chromecast extends CordovaPlugin { + + /** Tag for logging. */ + private static final String TAG = "Chromecast"; + /** Object to control the connection to the chromecast. */ + private ChromecastConnection connection; + /** Object to control the media. */ + private volatile ChromecastSession media; + + @Override + protected void pluginInitialize() { + super.pluginInitialize(); + + this.media = new ChromecastSession(cordova.getActivity(), remoteMediaClientCallback); + this.connection = new ChromecastConnection(cordova.getActivity(), this.media, new ChromecastConnection.ConnectionListener() { + @Override + public void onDisconnected(int reason) { + sendJavascript("chrome.cast.Session.prototype._update(false, {});"); +// sendJavascript("chrome.cast._.sessionUpdated(false, " + session.toString() + ");"); + } + }); + } + + @Override + public boolean execute(String action, JSONArray args, CallbackContext cbContext) throws JSONException { + try { + Method[] list = this.getClass().getMethods(); + Method methodToExecute = null; + for (Method method : list) { + if (method.getName().equals(action)) { + Type[] types = method.getGenericParameterTypes(); + // +1 is the cbContext + if (args.length() + 1 == types.length) { + boolean isValid = true; + for (int i = 0; i < args.length(); i++) { + Class arg = args.get(i).getClass(); + if (types[i] == arg) { + isValid = true; + } else { + isValid = false; + break; + } + } + if (isValid) { + methodToExecute = method; + break; + } + } + } + } + if (methodToExecute != null) { + Type[] types = methodToExecute.getGenericParameterTypes(); + Object[] variableArgs = new Object[types.length]; + for (int i = 0; i < args.length(); i++) { + variableArgs[i] = args.get(i); + } + variableArgs[variableArgs.length - 1] = cbContext; + Class r = methodToExecute.getReturnType(); + if (r == boolean.class) { + return (Boolean) methodToExecute.invoke(this, variableArgs); + } else { + methodToExecute.invoke(this, variableArgs); + return true; + } + } else { + return false; + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + return false; + } catch (IllegalArgumentException e) { + e.printStackTrace(); + return false; + } catch (InvocationTargetException e) { + e.printStackTrace(); + return false; + } + } + + /** + * Do everything you need to for "setup" - calling back sets the isAvailable and lets every function on the + * javascript side actually do stuff. + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean setup(CallbackContext callbackContext) { + callbackContext.success(); + return true; + } + + /** + * Initialize all of the MediaRouter stuff with the AppId. + * For now, ignore the autoJoinPolicy and defaultActionPolicy; those will come later + * @param appId The appId we're going to use for ALL session requests + * @param autoJoinPolicy tab_and_origin_scoped | origin_scoped | page_scoped + * @param defaultActionPolicy create_session | cast_this_tab + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean initialize(final String appId, String autoJoinPolicy, String defaultActionPolicy, final CallbackContext callbackContext) { + + this.connection.initialize(appId, new ChromecastConnection.Callback() { + @Override + public void run() { + callbackContext.success(); + + // Send receiver unavailable update while the new routeSelector is built. + // This matches the Chrome Desktop SDK behavior. + sendReceiverUpdate(false); + + + // See if there are any available routes + ChromecastConnection.ScanCallback scanCallback = new ChromecastConnection.ScanCallback() { + @Override + public void onRouteUpdate(RouteInfo route) { + // We found at least 1 route! so stop the scan + connection.stopScan(this); + + // and send out receiver available + sendReceiverUpdate(true); + + // Attempt to rejoin existing session if exists + connection.rejoin(new ChromecastConnection.JoinCallback() { + @Override + public void onJoin(CastSession session) { + // If we were able to join that means the client likely navigated to + // a new page and the code has called initialize again + // so, send out the session + sendJavascript("chrome.cast._.sessionListener(" + media.createSessionObject().toString() + ");"); + } + + @Override + public void onError(String errorCode) { + Log.d(TAG, "Error rejoining session: " + errorCode); + } + }); + } + }; + connection.scanForRoutes(scanCallback); + + // Also start a time out to cancel the scan + // after 5 seconds to save power + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + connection.stopScan(scanCallback); + } + }, 5000); + } + }); + + return true; + } + + /** + * Request the session for the previously sent appId. + * THIS IS WHAT LAUNCHES THE CHROMECAST PICKER + * or, if we already have a session launch the connection options + * dialog which will have the option to stop casting at minimum. + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean requestSession(final CallbackContext callbackContext) { + connection.showConnectionDialog(new ChromecastConnection.JoinCallback() { + @Override + public void onJoin(CastSession session) { + callbackContext.success(media.createSessionObject()); + } + public void onError(String errorCode) { + if (errorCode.equals("CANCEL")) { + callbackContext.error("cancel"); + } else { + // TODO maybe handle some of the error codes better + callbackContext.error("SESSION_ERROR"); + } + } + }); + return true; + } + + /** + * Selects a route by its id. + * @param routeId the id of the route to join + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean selectRoute(final String routeId, final CallbackContext callbackContext) { + connection.join(routeId, new ChromecastConnection.JoinCallback() { + @Override + public void onJoin(CastSession castSession) { + callbackContext.success(media.createSessionObject()); + } + + @Override + public void onError(String errorCode) { + callbackContext.error(errorCode); + } + }); + return true; + } + + /** + * Set the volume level on the receiver - this is a Chromecast volume, not a Media volume. + * @param newLevel the level to set the volume to + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean setReceiverVolumeLevel(Integer newLevel, CallbackContext callbackContext) { + return this.setReceiverVolumeLevel(newLevel.doubleValue(), callbackContext); + } + + private boolean setReceiverVolumeLevel(Double newLevel, CallbackContext callbackContext) { + this.media.setVolume(newLevel, callbackContext); + return true; + } + + /** + * Sets the muted boolean on the receiver - this is a Chromecast mute, not a Media mute. + * @param muted if true set the media to muted, else, unmute + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean setReceiverMuted(Boolean muted, CallbackContext callbackContext) { + this.media.setMute(muted, callbackContext); + return true; + } + + /** + * Stop the session. + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean stopSession(CallbackContext callbackContext) { + connection.kill(); + return true; + } + + /** + * Send a custom message to the receiver - we don't need this just yet... it was just simple to implement on the js side. + * @param namespace namespace + * @param message the message to send + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean sendMessage(String namespace, String message, final CallbackContext callbackContext) { + this.media.sendMessage(namespace, message, callbackContext); + return true; + } + + /** + * Adds a listener to a specific namespace. + * @param namespace namespace + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean addMessageListener(String namespace, CallbackContext callbackContext) { + this.media.addMessageListener(namespace); +// sendJavascript("chrome.cast._.onMessage('" + session.getSessionId() + "', '" + namespace + "', '" + message.replace("\\", "\\\\") + "')"); + callbackContext.success(); + return true; + } + + /** + * Loads some media on the Chromecast using the media APIs. + * @param contentId The URL of the media item + * @param customData CustomData + * @param contentType MIME type of the content + * @param duration Duration of the content + * @param streamType buffered | live | other + * @param autoPlay Whether or not to automatically start playing the media + * @param currentTime Where to begin playing from + * @param metadata Metadata + * @param textTrackStyle The text track style + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean loadMedia(String contentId, JSONObject customData, String contentType, Integer duration, String streamType, Boolean autoPlay, Integer currentTime, JSONObject metadata, JSONObject textTrackStyle, final CallbackContext callbackContext) { + return this.loadMedia(contentId, customData, contentType, duration, streamType, autoPlay, new Double(currentTime.doubleValue()), metadata, textTrackStyle, callbackContext); + } + + private boolean loadMedia(String contentId, JSONObject customData, String contentType, Integer duration, String streamType, Boolean autoPlay, Double currentTime, JSONObject metadata, JSONObject textTrackStyle, final CallbackContext callbackContext) { + this.media.loadMedia(contentId, customData, contentType, duration, streamType, autoPlay, currentTime, metadata, textTrackStyle, callbackContext); + return true; +// sendJavascript("chrome.cast._.mediaLoaded(true, " + media.toString() + ");"); + } + + /** + * Play on the current media in the current session. + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean mediaPlay(CallbackContext callbackContext) { + media.mediaPlay(callbackContext); + return true; + } + + /** + * Pause on the current media in the current session. + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean mediaPause(CallbackContext callbackContext) { + media.mediaPause(callbackContext); + return true; + } + + /** + * Seeks the current media in the current session. + * @param seekTime - Seconds to seek to + * @param resumeState - Resume state once seeking is complete: PLAYBACK_PAUSE or PLAYBACK_START + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean mediaSeek(Integer seekTime, String resumeState, CallbackContext callbackContext) { + media.mediaSeek(seekTime.longValue() * 1000, resumeState, callbackContext); + return true; + } + + + /** + * Set the volume on the media. + * @param level the level to set the volume to + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean setMediaVolume(Double level, CallbackContext callbackContext) { + media.mediaSetVolume(level, callbackContext); + return true; + } + + /** + * Set the muted on the media. + * @param muted if true set the media to muted, else, unmute + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean setMediaMuted(Boolean muted, CallbackContext callbackContext) { + media.mediaSetMuted(muted, callbackContext); + return true; + } + + /** + * Stops the current media. + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean mediaStop(CallbackContext callbackContext) { + media.mediaStop(callbackContext); + return true; + } + + /** + * Handle Track changes. + * @param activeTrackIds track Ids to set. + * @param textTrackStyle text track style to set. + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean mediaEditTracksInfo(JSONArray activeTrackIds, JSONObject textTrackStyle, final CallbackContext callbackContext) { + long[] trackIds = new long[activeTrackIds.length()]; + + try { + for (int i = 0; i < activeTrackIds.length(); i++) { + trackIds[i] = activeTrackIds.getLong(i); + } + } catch (JSONException ignored) { + LOG.e(TAG, "Wrong format in activeTrackIds"); + } + + this.media.mediaEditTracksInfo(trackIds, textTrackStyle, callbackContext); + return true; + } + + /** + * Stops the session. + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean sessionStop(CallbackContext callbackContext) { + connection.kill(); + callbackContext.success(); + return true; + } + + /** + * Stops the session. + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean sessionLeave(CallbackContext callbackContext) { + connection.leave(); + callbackContext.success(); + return true; + } /** - * sends the receiverState. - * @param receiverState + * Emits all routes. + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova */ - private void sendReceiverUpdate(boolean receiverState) { + public boolean emitAllRoutes(CallbackContext callbackContext) { + // TODO will use connection.scanForRoutes(); + + return true; + } + + /** + * Sends the receiverState. + * @param receiverState true if we should send receiverAvailable, + * false if we should send receiverUnavailable + */ + private void sendReceiverUpdate(boolean receiverState) { if (receiverState) { this.sendJavascript("chrome.cast._.receiverAvailable()"); } else { this.sendJavascript("chrome.cast._.receiverUnavailable()"); } - } - - /** - * Simple helper to convert a route to JSON for passing down to the javascript side - * @param route - * @return - */ - private JSONObject routeToJSON(RouteInfo route) { - JSONObject obj = new JSONObject(); - - try { - obj.put("name", route.getName()); - obj.put("id", route.getId()); - } catch (JSONException e) { - e.printStackTrace(); - } - - return obj; - } - - private RemoteMediaClient.Callback remoteMediaClientCallback = new RemoteMediaClient.Callback() { - @Override - public void onStatusUpdated() { - super.onStatusUpdated(); - } - - @Override - public void onMetadataUpdated() { - super.onMetadataUpdated(); -// sendJavascript("chrome.cast._.mediaUpdated(true, " + media.createMediaInfo() + ");"); - } - - @Override - public void onQueueStatusUpdated() { - super.onQueueStatusUpdated(); - } - - @Override - public void onPreloadStatusUpdated() { - super.onPreloadStatusUpdated(); - } - - @Override - public void onSendingRemoteMediaRequest() { - super.onSendingRemoteMediaRequest(); - } - - @Override - public void onAdBreakStatusUpdated() { - super.onAdBreakStatusUpdated(); - } - }; - - //Change all @deprecated this.webView.sendJavascript(String) to this local function sendJavascript(String) - @TargetApi(Build.VERSION_CODES.KITKAT) - private void sendJavascript(final String javascript) { - webView.getView().post(new Runnable() { - public void run() { - // See: https://github.com/GoogleChrome/chromium-webview-samples/blob/master/jsinterface-example/app/src/main/java/jsinterfacesample/android/chrome/google/com/jsinterface_example/MainFragment.java - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - webView.sendJavascript(javascript); - } else { - webView.loadUrl("javascript:" + javascript); - } - } - }); - } + } + + /** + * Simple helper to convert a route to JSON for passing down to the javascript side. + * @param route the route to convert + * @return a JSON representation of the route + */ + private JSONObject routeToJSON(RouteInfo route) { + JSONObject obj = new JSONObject(); + + try { + obj.put("name", route.getName()); + obj.put("id", route.getId()); + } catch (JSONException e) { + e.printStackTrace(); + } + + return obj; + } + + /** Handles remoteMediaClient callbacks. */ + private RemoteMediaClient.Callback remoteMediaClientCallback = new RemoteMediaClient.Callback() { + @Override + public void onStatusUpdated() { + super.onStatusUpdated(); + } + + @Override + public void onMetadataUpdated() { + super.onMetadataUpdated(); +// sendJavascript("chrome.cast._.mediaUpdated(true, " + media.createMediaInfo() + ");"); + } + + @Override + public void onQueueStatusUpdated() { + super.onQueueStatusUpdated(); + } + + @Override + public void onPreloadStatusUpdated() { + super.onPreloadStatusUpdated(); + } + + @Override + public void onSendingRemoteMediaRequest() { + super.onSendingRemoteMediaRequest(); + } + + @Override + public void onAdBreakStatusUpdated() { + super.onAdBreakStatusUpdated(); + } + }; + + //Change all @deprecated this.webView.sendJavascript(String) to this local function sendJavascript(String) + @TargetApi(Build.VERSION_CODES.KITKAT) + private void sendJavascript(final String javascript) { + webView.getView().post(new Runnable() { + public void run() { + // See: https://github.com/GoogleChrome/chromium-webview-samples/blob/master/jsinterface-example/app/src/main/java/jsinterfacesample/android/chrome/google/com/jsinterface_example/MainFragment.java + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + webView.sendJavascript(javascript); + } else { + webView.loadUrl("javascript:" + javascript); + } + } + }); + } } diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index 0fd0ffc..2995b99 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -17,339 +17,372 @@ public class ChromecastConnection { - // Lifetime variables - private Activity activity; - private ChromecastSession media; - private SessionListener newConnectionListener; - - // initialize lifetime variables - private String appId; - - // session lifetime variables - private CastSession session; - - public ChromecastConnection(Activity activity, ChromecastSession media, ConnectionListener listener) { - this.activity = activity; - this.media = media; - - // Add the session end/disconnect listener - activity.runOnUiThread(new Runnable() { - public void run() { - getSessionManager().addSessionManagerListener(new SessionListener() { - @Override - public void onSessionEnded(CastSession castSession, int error) { - setSession(null); - if (listener != null) { - listener.onDisconnected(error); - } - } - }, CastSession.class); - } - }); - } - - /** - * Must be called each time the appId changes and at least once before any other method is called. - * @param appId - * @param callback - */ - public void initialize(String appId, Callback callback) { - // If the appId changed - if (!appId.equals(this.appId)) { - // Else we need to save the new appId - this.setAppId(appId); - // And reset the session - this.setSession(null); - } - - activity.runOnUiThread(new Runnable() { - public void run() { - // Set the new appId - CastContext.getSharedInstance(activity).setReceiverApplicationId(appId); - // Tell the client we are done - callback.run(); - } - }); - } - - private MediaRouter getMediaRouter() { - return MediaRouter.getInstance(activity); - } - - private SessionManager getSessionManager() { - return CastContext.getSharedInstance(activity).getSessionManager(); - } - - private void setAppId(String appId) { - this.appId = appId; - } - - private void setSession(CastSession castSession) { - this.session = castSession; - this.media.setSession(this.session); - } - - /** - * Attempts to join the last route we were connected to. - * - * @param callback - */ - public void rejoin(JoinCallback callback) { - if (session != null) { - callback.onJoin(session); - } - // TODO is it even possible to rejoin a session automatically? - // https://stackoverflow.com/questions/57801427/how-to-rejoin-cast-session-after-app-restart - // https://stackoverflow.com/questions/57832467/how-to-check-if-mediarouter-routeinfo-route-is-already-in-use - } - - /** - * This will create a new session or seamlessly join an existing one if we created it. - * @param routeId - * @param callback - */ - public void join(String routeId, JoinCallback callback) { - activity.runOnUiThread(new Runnable() { - public void run() { - if (session != null) { - // We are are already connected to a route - callback.onJoin(session); - return; - } - listenForConnection(callback); - - Intent castIntent = new Intent(); - castIntent.putExtra("CAST_INTENT_TO_CAST_ROUTE_ID_KEY", routeId); - // Not sure of this one's purpose, possibly just for display - // castIntent.putExtra("CAST_INTENT_TO_CAST_DEVICE_NAME_KEY", deviceName); - castIntent.putExtra("CAST_INTENT_TO_CAST_NO_TOAST_KEY", false); - - getSessionManager().startSession(castIntent); - } - }); - } - - /** - * Will do one of two things: - * - * If no current connection will: - * 1) - * Displays the built in native prompt to the user. - * It will actively scan for routes and display them to the user. - * Upon selection it will immediately attempt to join the route. - * Will call onJoin or onError of callback. - * - * Else we have a connection, so: - * 2) - * Displays the active connection dialog which includes the option - * to disconnect. - * Will only call onError of callback if the user cancels the dialog. - * - * @param callback - */ - public void showConnectionDialog(JoinCallback callback) { - activity.runOnUiThread(new Runnable() { - public void run() { - if (session == null) { - // show the "choose a connection" dialog - - // Add the connection listener callback - listenForConnection(callback); - - // Create the dialog - // TODO accept theme as a config.xml option - MediaRouteChooserDialog builder = new MediaRouteChooserDialog(activity, androidx.appcompat.R.style.Theme_AppCompat_NoActionBar); - builder.setRouteSelector(new MediaRouteSelector.Builder() - .addControlCategory(CastMediaControlIntent.categoryForCast(appId)) - .build()); - builder.setCanceledOnTouchOutside(true); - builder.setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - getSessionManager().removeSessionManagerListener(newConnectionListener, CastSession.class); - callback.onError("CANCEL"); - } - }); - builder.show(); - } else { - // We are are already connected, so show the "connection options" Dialog - // TODO - } - } - }); - } - - /** - * Must be called from the main thread - * @param callback - */ - private void listenForConnection(JoinCallback callback) { - // We should only ever have one of these listeners active at a time, so remove previous - getSessionManager().removeSessionManagerListener(newConnectionListener, CastSession.class); - newConnectionListener = new SessionListener() { - @Override - public void onSessionStarted(CastSession castSession, String sessionId) { - getSessionManager().removeSessionManagerListener(this, CastSession.class); - setSession(castSession); - callback.onJoin(session); - } - @Override - public void onSessionStartFailed(CastSession castSession, int errCode) { - getSessionManager().removeSessionManagerListener(this, CastSession.class); - setSession(null); - callback.onError(Integer.toString(errCode)); - } - }; - getSessionManager().addSessionManagerListener(newConnectionListener, CastSession.class); - } - - - /** - * Starts listening for receiver updates. - * Must call stopScan(callback) or the battery will drain with non-stop active scanning. - * @param callback - */ - public void scanForRoutes(ScanCallback callback) { - // Add the callback in active scan mode - activity.runOnUiThread(new Runnable() { - public void run() { - - // Send out the initial routes - for (RouteInfo route : getMediaRouter().getRoutes()) { - callback.onFilteredRouteUpdate(route); - } - - // Add the callback in active scan mode - getMediaRouter().addCallback(new MediaRouteSelector.Builder() - .addControlCategory(CastMediaControlIntent.categoryForCast(appId)) - .build(), - callback, - MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); - } - }); - } - - /** - * Call to stop the active scan if any exist. - */ - public void stopScan(ScanCallback callback) { - activity.runOnUiThread(new Runnable() { - public void run() { - callback.stop(); - getMediaRouter().removeCallback(callback); - } - }); - } - - public void leave() { - activity.runOnUiThread(new Runnable() { - public void run() { - setSession(null); - getMediaRouter().unselect(MediaRouter.UNSELECT_REASON_DISCONNECTED); - } - }); - } - - public void kill() { - activity.runOnUiThread(new Runnable() { - public void run() { - setSession(null); - getSessionManager().endCurrentSession(true); - } - }); - } - - private class SessionListener implements SessionManagerListener { - - @Override - public void onSessionStarting(CastSession castSession) { - } - - @Override - public void onSessionStarted(CastSession castSession, String sessionId) { - } - - @Override - public void onSessionStartFailed(CastSession castSession, int error) { - } - - @Override - public void onSessionEnding(CastSession castSession) { - } - - @Override - public void onSessionEnded(CastSession castSession, int error) { - } - - @Override - public void onSessionResuming(CastSession castSession, String sessionId) { - } - - @Override - public void onSessionResumed(CastSession castSession, boolean wasSuspended) { - } - - @Override - public void onSessionResumeFailed(CastSession castSession, int error) { - } - - @Override - public void onSessionSuspended(CastSession castSession, int reason) { - } - } - - public interface Callback { - void run(); - } - - public interface ConnectionListener { - /** - * Called whenever a connection ends - */ - void onDisconnected(int reason); - } - - public interface JoinCallback { - /** - * Successfully joined a session on a route. - */ - void onJoin(CastSession session); - - /** - * @param errorCode "CANCEL" means the user cancelled - * If the errorCode is an integer, you can find the meaning here: - * https://developers.google.com/android/reference/com/google/android/gms/cast/CastStatusCodes - */ - void onError(String errorCode); - } - - public static abstract class ScanCallback extends MediaRouter.Callback { - abstract void onRouteUpdate(RouteInfo route); - - private boolean stopped = false; - void stop() { - stopped = true; - } - private void onFilteredRouteUpdate(RouteInfo route) { - if (stopped) { - return; - } - if (!route.isDefault()) { - onRouteUpdate(route); - } - } - @Override - public void onRouteAdded(MediaRouter router, RouteInfo route) { - onFilteredRouteUpdate(route); - } - @Override - public void onRouteChanged(MediaRouter router, RouteInfo route) { - onFilteredRouteUpdate(route); - } - @Override - public void onRouteRemoved(MediaRouter router, RouteInfo route) { - onFilteredRouteUpdate(route); - } - } + /** Lifetime variable. */ + private Activity activity; + /** Lifetime variable. */ + private ChromecastSession media; + /** Lifetime variable. */ + private SessionListener newConnectionListener; + + /** Initialize lifetime variable. */ + private String appId; + + /** Session lifetime variable. */ + private CastSession session; + + /** + * Constructor. + * @param act the current context + * @param chromecastSession the chromecastSession object that we should load with a new sessions + * @param listener the listener that listens for session end event + */ + public ChromecastConnection(Activity act, ChromecastSession chromecastSession, ConnectionListener listener) { + this.activity = act; + this.media = chromecastSession; + + // Add the session end/disconnect listener + act.runOnUiThread(new Runnable() { + public void run() { + getSessionManager().addSessionManagerListener(new SessionListener() { + @Override + public void onSessionEnded(CastSession castSession, int error) { + setSession(null); + if (listener != null) { + listener.onDisconnected(error); + } + } + }, CastSession.class); + } + }); + } + + /** + * Must be called each time the appId changes and at least once before any other method is called. + * @param applicationId the app id to use + * @param callback called when initialization is complete + */ + public void initialize(String applicationId, Callback callback) { + // If the appId changed + if (!applicationId.equals(this.appId)) { + // Else we need to save the new appId + this.setAppId(applicationId); + // And reset the session + this.setSession(null); + } + + activity.runOnUiThread(new Runnable() { + public void run() { + // Set the new appId + CastContext.getSharedInstance(activity).setReceiverApplicationId(appId); + // Tell the client we are done + callback.run(); + } + }); + } + + private MediaRouter getMediaRouter() { + return MediaRouter.getInstance(activity); + } + + private SessionManager getSessionManager() { + return CastContext.getSharedInstance(activity).getSessionManager(); + } + + private void setAppId(String applicationId) { + this.appId = applicationId; + } + + private void setSession(CastSession castSession) { + this.session = castSession; + this.media.setSession(this.session); + } + + /** + * Attempts to join the last route we were connected to. + * @param callback calls callback.onJoin if we have joined a session + */ + public void rejoin(JoinCallback callback) { + if (session != null) { + callback.onJoin(session); + } + // TODO is it even possible to rejoin a session automatically? + // https://stackoverflow.com/questions/57801427/how-to-rejoin-cast-session-after-app-restart + // https://stackoverflow.com/questions/57832467/how-to-check-if-mediarouter-routeinfo-route-is-already-in-use + } + + /** + * This will create a new session or seamlessly join an existing one if we created it. + * @param routeId the id of the route to join + * @param callback calls callback.onJoin when we have joined a session, + * or callback.onError if an error occurred + */ + public void join(String routeId, JoinCallback callback) { + activity.runOnUiThread(new Runnable() { + public void run() { + if (session != null) { + // We are are already connected to a route + callback.onJoin(session); + return; + } + listenForConnection(callback); + + Intent castIntent = new Intent(); + castIntent.putExtra("CAST_INTENT_TO_CAST_ROUTE_ID_KEY", routeId); + // Not sure of this one's purpose, possibly just for display + // castIntent.putExtra("CAST_INTENT_TO_CAST_DEVICE_NAME_KEY", deviceName); + castIntent.putExtra("CAST_INTENT_TO_CAST_NO_TOAST_KEY", false); + + getSessionManager().startSession(castIntent); + } + }); + } + + /** + * Will do one of two things: + * + * If no current connection will: + * 1) + * Displays the built in native prompt to the user. + * It will actively scan for routes and display them to the user. + * Upon selection it will immediately attempt to join the route. + * Will call onJoin or onError of callback. + * + * Else we have a connection, so: + * 2) + * Displays the active connection dialog which includes the option + * to disconnect. + * Will only call onError of callback if the user cancels the dialog. + * + * @param callback calls callback.success when we have joined a session, + * or callback.error if an error occurred or if the dialog was dismissed + */ + public void showConnectionDialog(JoinCallback callback) { + activity.runOnUiThread(new Runnable() { + public void run() { + if (session == null) { + // show the "choose a connection" dialog + + // Add the connection listener callback + listenForConnection(callback); + + // Create the dialog + // TODO accept theme as a config.xml option + MediaRouteChooserDialog builder = new MediaRouteChooserDialog(activity, androidx.appcompat.R.style.Theme_AppCompat_NoActionBar); + builder.setRouteSelector(new MediaRouteSelector.Builder() + .addControlCategory(CastMediaControlIntent.categoryForCast(appId)) + .build()); + builder.setCanceledOnTouchOutside(true); + builder.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + getSessionManager().removeSessionManagerListener(newConnectionListener, CastSession.class); + callback.onError("CANCEL"); + } + }); + builder.show(); + } else { + // We are are already connected, so show the "connection options" Dialog + // TODO + } + } + }); + } + + /** + * Must be called from the main thread. + * @param callback calls callback.success when we have joined, or callback.error if an error occurred + */ + private void listenForConnection(JoinCallback callback) { + // We should only ever have one of these listeners active at a time, so remove previous + getSessionManager().removeSessionManagerListener(newConnectionListener, CastSession.class); + newConnectionListener = new SessionListener() { + @Override + public void onSessionStarted(CastSession castSession, String sessionId) { + getSessionManager().removeSessionManagerListener(this, CastSession.class); + setSession(castSession); + callback.onJoin(session); + } + @Override + public void onSessionStartFailed(CastSession castSession, int errCode) { + getSessionManager().removeSessionManagerListener(this, CastSession.class); + setSession(null); + callback.onError(Integer.toString(errCode)); + } + }; + getSessionManager().addSessionManagerListener(newConnectionListener, CastSession.class); + } + + + /** + * Starts listening for receiver updates. + * Must call stopScan(callback) or the battery will drain with non-stop active scanning. + * @param callback the callback to receive route updates on + */ + public void scanForRoutes(ScanCallback callback) { + // Add the callback in active scan mode + activity.runOnUiThread(new Runnable() { + public void run() { + + // Send out the initial routes + for (RouteInfo route : getMediaRouter().getRoutes()) { + callback.onFilteredRouteUpdate(route); + } + + // Add the callback in active scan mode + getMediaRouter().addCallback(new MediaRouteSelector.Builder() + .addControlCategory(CastMediaControlIntent.categoryForCast(appId)) + .build(), + callback, + MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); + } + }); + } + + /** + * Call to stop the active scan if any exist. + * @param callback the callback to stop and remove + */ + public void stopScan(ScanCallback callback) { + activity.runOnUiThread(new Runnable() { + public void run() { + callback.stop(); + getMediaRouter().removeCallback(callback); + } + }); + } + + /** + * Leaves the session. + */ + public void leave() { + activity.runOnUiThread(new Runnable() { + public void run() { + setSession(null); + getMediaRouter().unselect(MediaRouter.UNSELECT_REASON_DISCONNECTED); + } + }); + } + + /** + * Kills the session. + */ + public void kill() { + activity.runOnUiThread(new Runnable() { + public void run() { + setSession(null); + getSessionManager().endCurrentSession(true); + } + }); + } + + private class SessionListener implements SessionManagerListener { + + @Override + public void onSessionStarting(CastSession castSession) { + } + + @Override + public void onSessionStarted(CastSession castSession, String sessionId) { + } + + @Override + public void onSessionStartFailed(CastSession castSession, int error) { + } + + @Override + public void onSessionEnding(CastSession castSession) { + } + + @Override + public void onSessionEnded(CastSession castSession, int error) { + } + + @Override + public void onSessionResuming(CastSession castSession, String sessionId) { + } + + @Override + public void onSessionResumed(CastSession castSession, boolean wasSuspended) { + } + + @Override + public void onSessionResumeFailed(CastSession castSession, int error) { + } + + @Override + public void onSessionSuspended(CastSession castSession, int reason) { + } + } + + public interface Callback { + /** + * The callback function. + */ + void run(); + } + + public interface ConnectionListener { + /** + * Called whenever a connection ends. + * @param reason the reason for disconnection + */ + void onDisconnected(int reason); + } + + public interface JoinCallback { + /** + * Successfully joined a session on a route. + * @param session the session we joined + */ + void onJoin(CastSession session); + + /** + * Called if we received an error. + * @param errorCode "CANCEL" means the user cancelled + * If the errorCode is an integer, you can find the meaning here: + * https://developers.google.com/android/reference/com/google/android/gms/cast/CastStatusCodes + */ + void onError(String errorCode); + } + + public abstract static class ScanCallback extends MediaRouter.Callback { + /** + * Called whenever a route is updated. + * @param route the route that was just updated + */ + abstract void onRouteUpdate(RouteInfo route); + + /** records whether we have been stopped or not. */ + private boolean stopped = false; + + /** + * Call this method when you wish to stop scanning. + * It is important that it is called, otherwise battery + * life will drain more quickly. + */ + void stop() { + stopped = true; + } + private void onFilteredRouteUpdate(RouteInfo route) { + if (stopped) { + return; + } + if (!route.isDefault()) { + onRouteUpdate(route); + } + } + @Override + public final void onRouteAdded(MediaRouter router, RouteInfo route) { + onFilteredRouteUpdate(route); + } + @Override + public final void onRouteChanged(MediaRouter router, RouteInfo route) { + onFilteredRouteUpdate(route); + } + @Override + public final void onRouteRemoved(MediaRouter router, RouteInfo route) { + onFilteredRouteUpdate(route); + } + } } diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index 8896ea5..fbf9547 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -33,629 +33,647 @@ */ public class ChromecastSession { - private Activity activity; - private RemoteMediaClient.Callback remoteMediaCallback; - private RemoteMediaClient client; - private CastSession session; - - public ChromecastSession(Activity activity, RemoteMediaClient.Callback callback) { - this.activity = activity; - this.remoteMediaCallback = callback; - } - - public void setSession(CastSession castSession) { - activity.runOnUiThread(new Runnable() { - public void run() { - if (client != null) { - client.unregisterCallback(remoteMediaCallback); - } - session = castSession; - if (session == null) { - client = null; - } else { - client = session.getRemoteMediaClient(); - client.registerCallback(remoteMediaCallback); - } - } - }); - } - - - /** - * Adds a message listener if one does not already exist - * @param namespace - */ - public void addMessageListener(String namespace) { - if (client == null || session == null) { - return; - } - activity.runOnUiThread(new Runnable() { - public void run() { - try { - session.setMessageReceivedCallbacks(namespace, new Cast.MessageReceivedCallback() { - @Override - public void onMessageReceived(CastDevice castDevice, String s, String s1) { - // if (this.onSessionUpdatedListener != null) { - // this.onSessionUpdatedListener.onMessage(this, namespace, message); - // } - } - }); - } catch (IOException e) { - e.printStackTrace(); - } - } - }); - } - - /** - * Sends a message to a specified namespace - * @param namespace - * @param message - * @param callback - */ - public void sendMessage(String namespace, String message, CallbackContext callback) { - if (client == null || session == null) { - callback.error("SESSION_ERROR"); - return; - } - activity.runOnUiThread(new Runnable() { - public void run() { - session.sendMessage(namespace, message).setResultCallback(new ResultCallback() { - @Override - public void onResult(Status result) { - if (!result.isSuccess()) { - callback.success(); - } else { - callback.error(result.toString()); - } - } - }); - - } - }); - } - - /** - * Loads media over the media API - * @param contentId - The URL of the content - * @param contentType - The MIME type of the content - * @param duration - The length of the video (if known) - * @param streamType - * @param autoPlay - Whether or not to start the video playing or not - * @param currentTime - Where in the video to begin playing from - * @param callback - * @return - */ - public void loadMedia(String contentId, JSONObject customData, String contentType, long duration, String streamType, boolean autoPlay, double currentTime, JSONObject metadata, JSONObject textTrackStyle, CallbackContext callback) { - if (client == null || session == null) { - callback.error("SESSION_ERROR"); - return; - } - activity.runOnUiThread(new Runnable() { - public void run() { - MediaInfo mediaInfo = createMediaInfo(contentId, customData, contentType, duration, streamType, metadata, textTrackStyle); - MediaLoadRequestData loadRequest = new MediaLoadRequestData.Builder() - .setMediaInfo(mediaInfo) - .setAutoplay(autoPlay) - .setCurrentTime((long) currentTime * 1000) - .build(); - - client.load(loadRequest).setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(createMediaObject()); - } else { - callback.error("SESSION_ERROR"); - } - } - }); - } - }); - } - - private MediaInfo createMediaInfo(String contentId, JSONObject customData, String contentType, long duration, String streamType, JSONObject metadata, JSONObject textTrackStyle) { - // create GENERIC MediaMetadata first and fallback to movie - MediaMetadata mediaMetadata = new MediaMetadata(); - try { - int metadataType = metadata.has("metadataType") ? metadata.getInt("metadataType") : MediaMetadata.MEDIA_TYPE_MOVIE; - if (metadataType == MediaMetadata.MEDIA_TYPE_GENERIC) { - mediaMetadata.putString(MediaMetadata.KEY_TITLE, (metadata.has("title")) ? metadata.getString("title") : "[Title not set]"); // TODO: What should it default to? - mediaMetadata.putString(MediaMetadata.KEY_SUBTITLE, (metadata.has("title")) ? metadata.getString("subtitle") : "[Subtitle not set]"); // TODO: What should it default to? - mediaMetadata = addImages(metadata, mediaMetadata); - } - } catch (Exception e) { - e.printStackTrace(); - mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE); - } - - int _streamType; - if (streamType.equals("buffered")) { - _streamType = MediaInfo.STREAM_TYPE_BUFFERED; - } else if (streamType.equals("live")) { - _streamType = MediaInfo.STREAM_TYPE_LIVE; - } else { - _streamType = MediaInfo.STREAM_TYPE_NONE; - } - - TextTrackStyle trackStyle = ChromecastUtilities.parseTextTrackStyle(textTrackStyle); - MediaInfo mediaInfo = new MediaInfo.Builder(contentId) - .setContentType(contentType) - .setCustomData(customData) - .setStreamType(_streamType) - .setStreamDuration(duration) - .setMetadata(mediaMetadata) - .setTextTrackStyle(trackStyle) - .build(); - - return mediaInfo; - } - - private MediaMetadata addImages(JSONObject metadata, MediaMetadata mediaMetadata) throws JSONException { - if (metadata.has("images")) { - JSONArray imageUrls = metadata.getJSONArray("images"); - for (int i = 0; i < imageUrls.length(); i++) { - JSONObject imageObj = imageUrls.getJSONObject(i); - String imageUrl = imageObj.has("url") ? imageObj.getString("url") : "undefined"; - if (!imageUrl.contains("http://")) { - continue; - } - Uri imageURI = Uri.parse(imageUrl); - WebImage webImage = new WebImage(imageURI); - mediaMetadata.addImage(webImage); - } - } - return mediaMetadata; - } - - /** - * Media API - Calls play on the current media - * @param callback - */ - public void mediaPlay(CallbackContext callback) { - if (client == null || session == null) { - callback.error("SESSION_ERROR"); - return; - } - activity.runOnUiThread(new Runnable() { - public void run() { - client.play().setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to play with code: " + result.getStatus().getStatusCode()); - } - } - }); - } - }); - } - - /** - * Media API - Calls pause on the current media - * @param callback - */ - public void mediaPause(CallbackContext callback) { - if (client == null || session == null) { - callback.error("SESSION_ERROR"); - return; - } - activity.runOnUiThread(new Runnable() { - public void run() { - client.pause().setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to pause with code: " + result.getStatus().getStatusCode()); - } - } - }); - } - }); - } - - /** - * Media API - Seeks the current playing media - * @param seekPosition - Seconds to seek to - * @param resumeState - Resume state once seeking is complete: PLAYBACK_PAUSE or PLAYBACK_START - * @param callback - */ - public void mediaSeek(long seekPosition, String resumeState, CallbackContext callback) { - if (client == null || session == null) { - callback.error("SESSION_ERROR"); - return; - } - activity.runOnUiThread(new Runnable() { - public void run() { - int resState; - switch (resumeState) { - case "PLAYBACK_START": - resState = MediaSeekOptions.RESUME_STATE_PLAY; - break; - case "PLAYBACK_PAUSE": - resState = MediaSeekOptions.RESUME_STATE_PAUSE; - break; - default: - resState = MediaSeekOptions.RESUME_STATE_UNCHANGED; - } - - client.seek(new MediaSeekOptions.Builder() - .setPosition(seekPosition) - .setResumeState(resState) - .build() - ).setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to seek with code: " + result.getStatus().getStatusCode()); - } - } - }); - } - }); - } - - /** - * Media API - Sets the volume on the current playing media object NOT ON THE CHROMECAST DIRECTLY - * @param level - * @param callback - */ - public void mediaSetVolume(double level, CallbackContext callback) { - if (client == null || session == null) { - callback.error("SESSION_ERROR"); - return; - } - activity.runOnUiThread(new Runnable() { - public void run() { - client.play().setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to set volume with code: " + result.getStatus().getStatusCode()); - } - } - }); - } - }); - } - - /** - * Media API - Sets the muted state on the current playing media NOT THE CHROMECAST DIRECTLY - * @param muted - * @param callback - */ - public void mediaSetMuted(boolean muted, CallbackContext callback) { - if (client == null || session == null) { - callback.error("SESSION_ERROR"); - return; - } - activity.runOnUiThread(new Runnable() { - public void run() { - client.setStreamMute(muted).setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to mute/unmute with code: " + result.getStatus().getStatusCode()); - } - } - }); - } - }); - } - - /** - * Media API - Stops and unloads the current playing media - * @param callback - */ - public void mediaStop(CallbackContext callback) { - if (client == null || session == null) { - callback.error("SESSION_ERROR"); - return; - } - activity.runOnUiThread(new Runnable() { - public void run() { - client.stop().setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to stop with code: " + result.getStatus().getStatusCode()); - } - } - }); - } - }); - } - - /** - * Handle track changed. - * @param activeTracksIds - * @param textTrackStyle - * @param callback - */ - public void mediaEditTracksInfo(long[] activeTracksIds, JSONObject textTrackStyle, CallbackContext callback) { - if (client == null || session == null) { - callback.error("SESSION_ERROR"); - return; - } - activity.runOnUiThread(new Runnable() { - public void run() { - client.setActiveMediaTracks(activeTracksIds).setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to set active media tracks with code: " + result.getStatus().getStatusCode()); - } - } - }); - client.setTextTrackStyle(ChromecastUtilities.parseTextTrackStyle(textTrackStyle)).setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to set text track style with code: " + result.getStatus().getStatusCode()); - } - } - }); - } - }); - } - - - /** - * Sets the receiver volume level - * @param volume - * @param callback - */ - public void setVolume(double volume, CallbackContext callback) { - if (client == null || session == null) { - callback.error("SESSION_ERROR"); - return; - } - activity.runOnUiThread(new Runnable() { - public void run() { - try { - session.setVolume(volume); - callback.success(); - } catch (IOException e) { - callback.error("CHANNEL_ERROR"); - } - } - }); - } - - /** - * Mutes the receiver - * @param muted - * @param callback - */ - public void setMute(boolean muted, CallbackContext callback) { - if (client == null || session == null) { - callback.error("SESSION_ERROR"); - return; - } - activity.runOnUiThread(new Runnable() { - public void run() { - try { - session.setMute(muted); - callback.success(); - } catch (IOException e) { - callback.error("CHANNEL_ERROR"); - } - } - }); - } - - /** - * Creates a JSON representation of this session - * @return - */ - public JSONObject createSessionObject() { - JSONObject out = new JSONObject(); - - try { - out.put("appId", session.getApplicationMetadata().getApplicationId()); - out.put("appImages", createAppImagesObject()); - out.put("displayName", session.getApplicationMetadata().getName()); - out.put("media", createMediaObject()); - out.put("receiver", createReceiverObject()); - out.put("sessionId", this.session.getSessionId()); - - } catch (JSONException e) { - e.printStackTrace(); - } catch (NullPointerException e) { - e.printStackTrace(); - } - - return out; - } - - private JSONObject createAppImagesObject() { - JSONObject out = new JSONObject(); - try { - MediaMetadata metadata = client.getMediaInfo().getMetadata(); - List images = metadata.getImages(); - JSONArray appImages = new JSONArray(); - if (images != null) { - for (WebImage o : images) { - appImages.put(o.toString()); - } - } - } catch (NullPointerException e) { - e.printStackTrace(); - } - return out; - } - - private JSONObject createReceiverObject() { - JSONObject out = new JSONObject(); - try { - out.put("friendlyName", this.session.getCastDevice().getFriendlyName()); - out.put("label", this.session.getCastDevice().getDeviceId()); - - JSONObject volume = new JSONObject(); - try { - volume.put("level", session.getVolume()); - volume.put("muted", session.isMute()); - } catch (JSONException e) { - e.printStackTrace(); - } - out.put("volume", volume); - - } catch (JSONException e) { - e.printStackTrace(); - } catch (NullPointerException e) { - e.printStackTrace(); - } - return out; - } - - /** - * Creates a JSON representation of the current playing media - * @return - */ - private JSONObject createMediaObject() { - JSONObject out = new JSONObject(); - - try { - MediaStatus mediaStatus = client.getMediaStatus(); - - - // TODO: Missing attributes are commented out. - // These are returned by the chromecast desktop SDK, we should probbaly return them too - //out.put("breakStatus",); - out.put("currentItemId", mediaStatus.getCurrentItemId()); - out.put("currentTime", mediaStatus.getStreamPosition() / 1000.0); - out.put("customData", mediaStatus.getCustomData()); - //out.put("extendedStatus",); - out.put("idleReason", ChromecastUtilities.getMediaIdleReason(mediaStatus)); - //out.put("items", mediaStatus.getQueueItems()); - //out.put("liveSeekableRange",); - out.put("loadingItemId", mediaStatus.getLoadingItemId()); - out.put("media", this.createMediaInfoObject()); - out.put("mediaSessionId", 1); - out.put("playbackRate", mediaStatus.getPlaybackRate()); - out.put("playerState", ChromecastUtilities.getMediaPlayerState(mediaStatus)); - out.put("preloadedItemId", mediaStatus.getPreloadedItemId()); - //out.put("queueData", ); - //out.put("repeatMode", mediaStatus.getQueueRepeatMode()); - out.put("sessionId", this.session.getSessionId()); - //out.put("supportedMediaCommands", ); - //out.put("videoInfo", ); - - - JSONObject volume = new JSONObject(); - volume.put("level", mediaStatus.getStreamVolume()); - volume.put("muted", mediaStatus.isMute()); - out.put("volume", volume); - - long[] activeTrackIds = mediaStatus.getActiveTrackIds(); - if (activeTrackIds != null) { - JSONArray activeTracks = new JSONArray(); - for (long activeTrackId : activeTrackIds) { - activeTracks.put(activeTrackId); - } - out.put("activeTrackIds", activeTracks); - } - - } catch (JSONException e) { - e.printStackTrace(); - } catch (NullPointerException e) { - e.printStackTrace(); - } - - return out; - } - - /** - * Creates a JSON representation of all Tracks available in the current media. - * @return - */ - private JSONArray createMediaInfoTracks() { - JSONArray out = new JSONArray(); - - try { - MediaStatus mediaStatus = client.getMediaStatus(); - MediaInfo mediaInfo = mediaStatus.getMediaInfo(); - - if (mediaInfo.getMediaTracks() == null) { - return out; - } - - for (MediaTrack track : mediaInfo.getMediaTracks()) { - JSONObject jsonTrack = new JSONObject(); - - - // TODO: Missing attributes are commented out. - // These are returned by the chromecast desktop SDK, we should probbaly return them too - - jsonTrack.put("trackId", track.getId()); - jsonTrack.put("customData", track.getCustomData()); - jsonTrack.put("language", track.getLanguage()); - jsonTrack.put("name", track.getName()); - jsonTrack.put("subtype", ChromecastUtilities.getTrackSubtype(track)); - jsonTrack.put("trackContentId", track.getContentId()); - jsonTrack.put("trackContentType", track.getContentType()); - jsonTrack.put("type", ChromecastUtilities.getTrackType(track)); - - out.put(jsonTrack); - } - } catch (JSONException e) { - e.printStackTrace(); - } - - return out; - } - - - /** - * Creates a JSON representation of current MediaInfo of the session. - * @return - */ - private JSONObject createMediaInfoObject() { - JSONObject out = new JSONObject(); - - try { - MediaStatus mediaStatus = client.getMediaStatus(); - MediaInfo mediaInfo = mediaStatus.getMediaInfo(); - - - // TODO: Missing attributes are commented out. - // These are returned by the chromecast desktop SDK, we should probbaly return them too - //out.put("breakClips",); - //out.put("breaks",); - out.put("contentId", mediaInfo.getContentId()); - out.put("contentType", mediaInfo.getContentType()); - out.put("customData", mediaInfo.getCustomData()); - //out.put("idleReason",); - //out.put("items",); - out.put("duration", mediaInfo.getStreamDuration() / 1000.0); - //out.put("mediaCategory",); - out.put("streamType", ChromecastUtilities.getMediaInfoStreamType(mediaInfo)); - out.put("tracks", this.createMediaInfoTracks()); - out.put("textTrackStyle", ChromecastUtilities.createTextTrackObject(mediaInfo.getTextTrackStyle())); - - // TODO: Check if it's useful - //out.put("metadata", mediaInfo.getMetadata()); - } catch (JSONException e) { - e.printStackTrace(); - } - - return out; - } + /** The current context. */ + private Activity activity; + /** A registered callback that we will un-register and re-register each time the session changes. */ + private RemoteMediaClient.Callback remoteMediaCallback; + /** The current session. */ + private CastSession session; + /** The current session's client for controlling playback. */ + private RemoteMediaClient client; + + /** + * ChromecastSession constructor. + * @param act the current activity + * @param callback the callback will be used notify about session end + */ + public ChromecastSession(Activity act, RemoteMediaClient.Callback callback) { + this.activity = act; + this.remoteMediaCallback = callback; + } + + /** + * Sets the session object the will be used for other commands in this class. + * @param castSession the session to use + */ + public void setSession(CastSession castSession) { + activity.runOnUiThread(new Runnable() { + public void run() { + if (client != null) { + client.unregisterCallback(remoteMediaCallback); + } + session = castSession; + if (session == null) { + client = null; + } else { + client = session.getRemoteMediaClient(); + client.registerCallback(remoteMediaCallback); + } + } + }); + } + + + /** + * Adds a message listener if one does not already exist. + * @param namespace namespace + */ + public void addMessageListener(String namespace) { + if (client == null || session == null) { + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + try { + session.setMessageReceivedCallbacks(namespace, new Cast.MessageReceivedCallback() { + @Override + public void onMessageReceived(CastDevice castDevice, String s, String s1) { + // if (this.onSessionUpdatedListener != null) { + // this.onSessionUpdatedListener.onMessage(this, namespace, message); + // } + } + }); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + } + + /** + * Sends a message to a specified namespace. + * @param namespace namespace + * @param message the message to send + * @param callback called with success or error + */ + public void sendMessage(String namespace, String message, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + session.sendMessage(namespace, message).setResultCallback(new ResultCallback() { + @Override + public void onResult(Status result) { + if (!result.isSuccess()) { + callback.success(); + } else { + callback.error(result.toString()); + } + } + }); + + } + }); + } + + /** + * Loads media over the media API. + * @param contentId - The URL of the content + * @param customData - CustomData + * @param contentType - The MIME type of the content + * @param duration - The length of the video (if known) + * @param streamType - The stream type + * @param autoPlay - Whether or not to start the video playing or not + * @param currentTime - Where in the video to begin playing from + * @param metadata - Metadata + * @param textTrackStyle - The text track style + * @param callback called with success or error + */ + public void loadMedia(String contentId, JSONObject customData, String contentType, long duration, String streamType, boolean autoPlay, double currentTime, JSONObject metadata, JSONObject textTrackStyle, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + MediaInfo mediaInfo = createMediaInfo(contentId, customData, contentType, duration, streamType, metadata, textTrackStyle); + MediaLoadRequestData loadRequest = new MediaLoadRequestData.Builder() + .setMediaInfo(mediaInfo) + .setAutoplay(autoPlay) + .setCurrentTime((long) currentTime * 1000) + .build(); + + client.load(loadRequest).setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(createMediaObject()); + } else { + callback.error("SESSION_ERROR"); + } + } + }); + } + }); + } + + private MediaInfo createMediaInfo(String contentId, JSONObject customData, String contentType, long duration, String streamType, JSONObject metadata, JSONObject textTrackStyle) { + // create GENERIC MediaMetadata first and fallback to movie + MediaMetadata mediaMetadata = new MediaMetadata(); + try { + int metadataType = metadata.has("metadataType") ? metadata.getInt("metadataType") : MediaMetadata.MEDIA_TYPE_MOVIE; + if (metadataType == MediaMetadata.MEDIA_TYPE_GENERIC) { + mediaMetadata.putString(MediaMetadata.KEY_TITLE, (metadata.has("title")) ? metadata.getString("title") : "[Title not set]"); // TODO: What should it default to? + mediaMetadata.putString(MediaMetadata.KEY_SUBTITLE, (metadata.has("title")) ? metadata.getString("subtitle") : "[Subtitle not set]"); // TODO: What should it default to? + mediaMetadata = addImages(metadata, mediaMetadata); + } + } catch (Exception e) { + e.printStackTrace(); + mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE); + } + + int intStreamType; + switch (streamType) { + case "buffered": + intStreamType = MediaInfo.STREAM_TYPE_BUFFERED; + break; + case "live": + intStreamType = MediaInfo.STREAM_TYPE_LIVE; + break; + default: + intStreamType = MediaInfo.STREAM_TYPE_NONE; + } + + TextTrackStyle trackStyle = ChromecastUtilities.parseTextTrackStyle(textTrackStyle); + MediaInfo mediaInfo = new MediaInfo.Builder(contentId) + .setContentType(contentType) + .setCustomData(customData) + .setStreamType(intStreamType) + .setStreamDuration(duration) + .setMetadata(mediaMetadata) + .setTextTrackStyle(trackStyle) + .build(); + + return mediaInfo; + } + + private MediaMetadata addImages(JSONObject metadata, MediaMetadata mediaMetadata) throws JSONException { + if (metadata.has("images")) { + JSONArray imageUrls = metadata.getJSONArray("images"); + for (int i = 0; i < imageUrls.length(); i++) { + JSONObject imageObj = imageUrls.getJSONObject(i); + String imageUrl = imageObj.has("url") ? imageObj.getString("url") : "undefined"; + if (!imageUrl.contains("http://")) { + continue; + } + Uri imageURI = Uri.parse(imageUrl); + WebImage webImage = new WebImage(imageURI); + mediaMetadata.addImage(webImage); + } + } + return mediaMetadata; + } + + /** + * Media API - Calls play on the current media. + * @param callback called with success or error + */ + public void mediaPlay(CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + client.play().setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to play with code: " + result.getStatus().getStatusCode()); + } + } + }); + } + }); + } + + /** + * Media API - Calls pause on the current media. + * @param callback called with success or error + */ + public void mediaPause(CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + client.pause().setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to pause with code: " + result.getStatus().getStatusCode()); + } + } + }); + } + }); + } + + /** + * Media API - Seeks the current playing media. + * @param seekPosition - Seconds to seek to + * @param resumeState - Resume state once seeking is complete: PLAYBACK_PAUSE or PLAYBACK_START + * @param callback called with success or error + */ + public void mediaSeek(long seekPosition, String resumeState, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + int resState; + switch (resumeState) { + case "PLAYBACK_START": + resState = MediaSeekOptions.RESUME_STATE_PLAY; + break; + case "PLAYBACK_PAUSE": + resState = MediaSeekOptions.RESUME_STATE_PAUSE; + break; + default: + resState = MediaSeekOptions.RESUME_STATE_UNCHANGED; + } + + client.seek(new MediaSeekOptions.Builder() + .setPosition(seekPosition) + .setResumeState(resState) + .build() + ).setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to seek with code: " + result.getStatus().getStatusCode()); + } + } + }); + } + }); + } + + /** + * Media API - Sets the volume on the current playing media object, NOT ON THE CHROMECAST DIRECTLY. + * @param level the level to set the volume to + * @param callback called with success or error + */ + public void mediaSetVolume(double level, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + client.play().setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to set volume with code: " + result.getStatus().getStatusCode()); + } + } + }); + } + }); + } + + /** + * Media API - Sets the muted state on the current playing media, NOT THE CHROMECAST DIRECTLY. + * @param muted if true set the media to muted, else, unmute + * @param callback called with success or error + */ + public void mediaSetMuted(boolean muted, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + client.setStreamMute(muted).setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to mute/unmute with code: " + result.getStatus().getStatusCode()); + } + } + }); + } + }); + } + + /** + * Media API - Stops and unloads the current playing media. + * @param callback called with success or error + */ + public void mediaStop(CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + client.stop().setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to stop with code: " + result.getStatus().getStatusCode()); + } + } + }); + } + }); + } + + /** + * Handle track changed. + * @param activeTracksIds active track ids + * @param textTrackStyle track style + * @param callback called with success or error + */ + public void mediaEditTracksInfo(long[] activeTracksIds, JSONObject textTrackStyle, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + client.setActiveMediaTracks(activeTracksIds).setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to set active media tracks with code: " + result.getStatus().getStatusCode()); + } + } + }); + client.setTextTrackStyle(ChromecastUtilities.parseTextTrackStyle(textTrackStyle)).setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + callback.error("Failed to set text track style with code: " + result.getStatus().getStatusCode()); + } + } + }); + } + }); + } + + + /** + * Sets the receiver volume level. + * @param volume volume to set the receiver to + * @param callback called with success or error + */ + public void setVolume(double volume, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + try { + session.setVolume(volume); + callback.success(); + } catch (IOException e) { + callback.error("CHANNEL_ERROR"); + } + } + }); + } + + /** + * Mutes the receiver. + * @param muted if true mute, else, unmute + * @param callback called with success or error + */ + public void setMute(boolean muted, CallbackContext callback) { + if (client == null || session == null) { + callback.error("SESSION_ERROR"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + try { + session.setMute(muted); + callback.success(); + } catch (IOException e) { + callback.error("CHANNEL_ERROR"); + } + } + }); + } + + /** + * Creates a JSON representation of this session. + * @return a JSON representation of this session + */ + public JSONObject createSessionObject() { + JSONObject out = new JSONObject(); + + try { + out.put("appId", session.getApplicationMetadata().getApplicationId()); + out.put("appImages", createAppImagesObject()); + out.put("displayName", session.getApplicationMetadata().getName()); + out.put("media", createMediaObject()); + out.put("receiver", createReceiverObject()); + out.put("sessionId", this.session.getSessionId()); + + } catch (JSONException e) { + e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); + } + + return out; + } + + private JSONObject createAppImagesObject() { + JSONObject out = new JSONObject(); + try { + MediaMetadata metadata = client.getMediaInfo().getMetadata(); + List images = metadata.getImages(); + JSONArray appImages = new JSONArray(); + if (images != null) { + for (WebImage o : images) { + appImages.put(o.toString()); + } + } + } catch (NullPointerException e) { + e.printStackTrace(); + } + return out; + } + + private JSONObject createReceiverObject() { + JSONObject out = new JSONObject(); + try { + out.put("friendlyName", this.session.getCastDevice().getFriendlyName()); + out.put("label", this.session.getCastDevice().getDeviceId()); + + JSONObject volume = new JSONObject(); + try { + volume.put("level", session.getVolume()); + volume.put("muted", session.isMute()); + } catch (JSONException e) { + e.printStackTrace(); + } + out.put("volume", volume); + + } catch (JSONException e) { + e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); + } + return out; + } + + /** + * Creates a JSON representation of the current playing media. + * @return a JSON representation of the current playing media + */ + private JSONObject createMediaObject() { + JSONObject out = new JSONObject(); + + try { + MediaStatus mediaStatus = client.getMediaStatus(); + + + // TODO: Missing attributes are commented out. + // These are returned by the chromecast desktop SDK, we should probbaly return them too + //out.put("breakStatus",); + out.put("currentItemId", mediaStatus.getCurrentItemId()); + out.put("currentTime", mediaStatus.getStreamPosition() / 1000.0); + out.put("customData", mediaStatus.getCustomData()); + //out.put("extendedStatus",); + out.put("idleReason", ChromecastUtilities.getMediaIdleReason(mediaStatus)); + //out.put("items", mediaStatus.getQueueItems()); + //out.put("liveSeekableRange",); + out.put("loadingItemId", mediaStatus.getLoadingItemId()); + out.put("media", this.createMediaInfoObject()); + out.put("mediaSessionId", 1); + out.put("playbackRate", mediaStatus.getPlaybackRate()); + out.put("playerState", ChromecastUtilities.getMediaPlayerState(mediaStatus)); + out.put("preloadedItemId", mediaStatus.getPreloadedItemId()); + //out.put("queueData", ); + //out.put("repeatMode", mediaStatus.getQueueRepeatMode()); + out.put("sessionId", this.session.getSessionId()); + //out.put("supportedMediaCommands", ); + //out.put("videoInfo", ); + + + JSONObject volume = new JSONObject(); + volume.put("level", mediaStatus.getStreamVolume()); + volume.put("muted", mediaStatus.isMute()); + out.put("volume", volume); + + long[] activeTrackIds = mediaStatus.getActiveTrackIds(); + if (activeTrackIds != null) { + JSONArray activeTracks = new JSONArray(); + for (long activeTrackId : activeTrackIds) { + activeTracks.put(activeTrackId); + } + out.put("activeTrackIds", activeTracks); + } + + } catch (JSONException e) { + e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); + } + + return out; + } + + /** + * Creates a JSON representation of all Tracks available in the current media. + * @return a JSON representation of all Tracks available in the current media + */ + private JSONArray createMediaInfoTracks() { + JSONArray out = new JSONArray(); + + try { + MediaStatus mediaStatus = client.getMediaStatus(); + MediaInfo mediaInfo = mediaStatus.getMediaInfo(); + + if (mediaInfo.getMediaTracks() == null) { + return out; + } + + for (MediaTrack track : mediaInfo.getMediaTracks()) { + JSONObject jsonTrack = new JSONObject(); + + + // TODO: Missing attributes are commented out. + // These are returned by the chromecast desktop SDK, we should probbaly return them too + + jsonTrack.put("trackId", track.getId()); + jsonTrack.put("customData", track.getCustomData()); + jsonTrack.put("language", track.getLanguage()); + jsonTrack.put("name", track.getName()); + jsonTrack.put("subtype", ChromecastUtilities.getTrackSubtype(track)); + jsonTrack.put("trackContentId", track.getContentId()); + jsonTrack.put("trackContentType", track.getContentType()); + jsonTrack.put("type", ChromecastUtilities.getTrackType(track)); + + out.put(jsonTrack); + } + } catch (JSONException e) { + e.printStackTrace(); + } + + return out; + } + + + /** + * Creates a JSON representation of current MediaInfo of the session. + * @return a JSON representation of current MediaInfo of the session + */ + private JSONObject createMediaInfoObject() { + JSONObject out = new JSONObject(); + + try { + MediaStatus mediaStatus = client.getMediaStatus(); + MediaInfo mediaInfo = mediaStatus.getMediaInfo(); + + + // TODO: Missing attributes are commented out. + // These are returned by the chromecast desktop SDK, we should probbaly return them too + //out.put("breakClips",); + //out.put("breaks",); + out.put("contentId", mediaInfo.getContentId()); + out.put("contentType", mediaInfo.getContentType()); + out.put("customData", mediaInfo.getCustomData()); + //out.put("idleReason",); + //out.put("items",); + out.put("duration", mediaInfo.getStreamDuration() / 1000.0); + //out.put("mediaCategory",); + out.put("streamType", ChromecastUtilities.getMediaInfoStreamType(mediaInfo)); + out.put("tracks", this.createMediaInfoTracks()); + out.put("textTrackStyle", ChromecastUtilities.createTextTrackObject(mediaInfo.getTextTrackStyle())); + + // TODO: Check if it's useful + //out.put("metadata", mediaInfo.getMetadata()); + } catch (JSONException e) { + e.printStackTrace(); + } + + return out; + } } diff --git a/src/android/ChromecastUtilities.java b/src/android/ChromecastUtilities.java index 0d32220..d09d619 100644 --- a/src/android/ChromecastUtilities.java +++ b/src/android/ChromecastUtilities.java @@ -2,8 +2,6 @@ import android.graphics.Color; -import androidx.core.content.ContextCompat; - import com.google.android.gms.cast.MediaInfo; import com.google.android.gms.cast.MediaStatus; import com.google.android.gms.cast.MediaTrack; @@ -11,9 +9,13 @@ import org.json.JSONException; import org.json.JSONObject; -import org.w3c.dom.Text; -class ChromecastUtilities { +final class ChromecastUtilities { + + private ChromecastUtilities() { + //not called + } + static String getMediaIdleReason(MediaStatus mediaStatus) { switch (mediaStatus.getIdleReason()) { case MediaStatus.IDLE_REASON_CANCELED: From 8c05c82dacf931d5890da67cfbd40224dc52c885 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 9 Sep 2019 06:49:46 -0600 Subject: [PATCH 027/166] Added stop casting dialog. finally got the tests to early terminate on error (It's ugly. We should consider switching to mocha.) Work done for Issue #36 --- src/android/ChromecastConnection.java | 17 +- tests/tests.js | 569 ++++++++++++-------------- 2 files changed, 286 insertions(+), 300 deletions(-) diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index 2995b99..a8cdcdd 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -1,6 +1,7 @@ package acidhax.cordova.chromecast; import android.app.Activity; +import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; @@ -182,7 +183,21 @@ public void onCancel(DialogInterface dialog) { builder.show(); } else { // We are are already connected, so show the "connection options" Dialog - // TODO + AlertDialog.Builder builder = new AlertDialog.Builder(activity); + builder.setTitle(session.getCastDevice().getFriendlyName()); + builder.setOnDismissListener(new DialogInterface.OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialog) { + callback.onError("CANCEL"); + } + }); + builder.setPositiveButton("Stop Casting", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + kill(); + } + }); + builder.show(); } } }); diff --git a/tests/tests.js b/tests/tests.js index 142e611..224aa14 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -11,8 +11,7 @@ exports.defineAutoTests = function () { jasmine.DEFAULT_TIMEOUT_INTERVAL = 9000; var USER_INTERACTION_TIMEOUT = 60 * 1000; // 1 min - var applicationID_default = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID; - var applicationID_custom = 'F5EEDC6C'; + var appId = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID; var videoUrl = location.origin + '/plugins/cordova-plugin-chromecast-tests/res/test.mp4'; describe('chrome.cast', function () { @@ -82,129 +81,83 @@ exports.defineAutoTests = function () { expect(chrome.cast.media.Media.prototype.removeUpdateListener).toBeDefined(); }); - it('SPEC_00200 api should be available', function (done) { - var interval = setInterval(function () { - if (chrome.cast.isAvailable) { - expect(chrome.cast.isAvailable).toEqual(true); - clearInterval(interval); - done(); - } - }, 500); - }); - - it('SPEC_00300 initialize should succeed (custom receiver)', function (done) { - var sessionRequest = new chrome.cast.SessionRequest(applicationID_custom); - var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) {}, function (available) {}); - chrome.cast.initialize(apiConfig, function () { - expect('success').toBeDefined(); - done(); - }, function (err) { - fail(err.code + ': ' + err.description); - done(); - }); - }); - /** * Pre-requisite: You must have a valid receiver (chromecast) plugged in and available. */ - it('SPEC_00400 initialize should succeed (default receiver)', function (done) { - var step = 1; - var sessionRequest = new chrome.cast.SessionRequest(applicationID_default); - var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) { - fail('should not receive a session (make sure there is no active cast session when starting the tests)'); - }, function receiverListener (available) { - if (step === 1) { - fail('SPEC_00400 - Did not hit initialize Step 1 first'); - } - if (step === 2) { - // Step 2 - We must get the unavailable notification - if (available !== 'unavailable') { - fail('SPEC_00400 - Step 2 - Hit receiver listener with non-unavailable status'); - } else { - step++; - } - } - if (step === 3) { - // Step 3 - We are allowed to receive multiple unavailable until we receive the first available in this step - if (available !== 'unavailable' && available !== 'available') { - fail('SPEC_00400 - Step 3 - Hit receiver listener with incorrect status'); - } - if (available === 'available') { - expect('success').toBeDefined(); - done(); - } - } - }); - chrome.cast.initialize(apiConfig, function () { - // Step 1 - if (step !== 1) { - fail('SPEC_00400 - Step 1 - Expected to hit this first, but did not'); - } - step++; - }, function (err) { - fail(err.code + ': ' + err.description); - done(); - }); - }); + it('SPEC_00400 Normal usage cycle', function (done) { + setupEarlyTerminator(done); + Promise.resolve() + .then(apiAvailable) + .then(initialize('SPEC_00400', function (session) { + test().fail('should not receive a session (make sure there is no active cast session when starting the tests)'); + })) + .then(requestSessionCancel) + .then(requestSessionSuccess) + .then(loadMediaVideo) + .then(requestSessionStopCastingUiCancel) + .then(requestSessionStopCastingUiStopSuccess) + .then(done); - it('requestSession click outside of dialog should return the cancel error', function (done) { - alert('---TEST INSTRUCTION---\nPlease click outside of the next dialog to dismiss it.'); - chrome.cast.requestSession(function (session) { - fail('We should not reach here on dismiss (make sure you cancelled the dialog for this test!)'); - done(); - }, function (err) { - expect(err instanceof chrome.cast.Error).toBeTruthy(); - expect(err.code).toBe(chrome.cast.ErrorCode.CANCEL); - done(); - }); }, USER_INTERACTION_TIMEOUT); - describe('Everything Session', function () { - - it('SPEC_01000 Test valid session', function (done) { - alert('---TEST INSTRUCTION---\nPlease select a valid chromecast in the next dialog.'); - - chrome.cast.requestSession(function (session) { - - // Run all the session related tests - Promise.resolve(session) - .then(sessionProperties) - .then(newPageSharesSession) - .then(sessionProperties) - .then(loadMedia) - .then(stopSession) - .then(done); - - }, function (err) { - fail(err.code + ': ' + err.description); - done(); - }); + /** + * When on a new page, initialize should be called again + * This should result in the session being passed to the + * session listener as long as teh requested appId does + * not change. + */ + it('SPEC_00500 stopSession and new page simulation', function (done) { + setupEarlyTerminator(done); + Promise.resolve() + .then(apiAvailable) + .then(initialize('SPEC_00503', function (session) { + throw new Error('should not receive a session (make sure there is no active cast session when starting the tests)'); + })) + .then(requestSessionSuccess) + .then(initialize('SPEC_00506', function (session) { + Promise.resolve(session) + .then(sessionProperties) + .then(sessionStop) + .then(done); + })); + }, USER_INTERACTION_TIMEOUT); - }, USER_INTERACTION_TIMEOUT); + function setupEarlyTerminator (done) { + // Add this so that thrown errors are not obscured. + // This is required for early test termination + window.addEventListener('cordovacallbackerror', function (e) { + window.removeEventListener('cordovacallbackerror', this); + fail(e.stack); + done(); + }); + expect('just to make jasmine happy that there is an expect in the tests').toBeDefined(); + } + + function apiAvailable () { + return new Promise(function (resolve) { + var interval = setInterval(function () { + if (chrome.cast.isAvailable) { + test(chrome.cast.isAvailable).toEqual(true); + clearInterval(interval); + resolve(); + } + }, 500); + }); + } - /** - * When on a new page, it will call initialize again - * This should result in the session being passed to the - * session listener as long as teh requested appId does - * not change. - */ - function newPageSharesSession (session) { + function initialize (specName, sessionListener) { + return function () { return new Promise(function (resolve) { var step = 1; - var sessionRequest = new window.chrome.cast.SessionRequest(window.chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID); - var apiConfig = new window.chrome.cast.ApiConfig(sessionRequest, function (session) { - if (step !== 4) { - fail('newPageSharesSession.js - Did not hit session listener as 4th step'); - } - resolve(session); - }, function receiverListener (available) { + var sessionRequest = new chrome.cast.SessionRequest(appId); + var apiConfig = new chrome.cast.ApiConfig(sessionRequest, sessionListener, function receiverListener (available) { if (step === 1) { - fail('newPageSharesSession.js - Did not hit initialize Step 1 first'); + test().fail(specName + ' - Initialize did not hit Step 1 first'); } if (step === 2) { // Step 2 - We must get the unavailable notification if (available !== 'unavailable') { - fail('newPageSharesSession.js - Step 2 - Hit receiver listener with non-unavailable status'); + test().fail(specName + ' - Initialize - Step 2 - Hit receiver listener with non-unavailable status'); } else { step++; } @@ -212,235 +165,253 @@ exports.defineAutoTests = function () { if (step === 3) { // Step 3 - We are allowed to receive multiple unavailable until we receive the first available in this step if (available !== 'unavailable' && available !== 'available') { - fail('newPageSharesSession.js - Step 3 - Hit receiver listener with incorrect status'); + test().fail(specName + ' - Initialize - Step 3 - Hit receiver listener with incorrect status'); } if (available === 'available') { - step++; + resolve(); } } }); - - // Initialize - window.chrome.cast.initialize(apiConfig, function () { + chrome.cast.initialize(apiConfig, function () { // Step 1 if (step !== 1) { - fail('newPageSharesSession.js - Step 1 - Expected to hit this first, but did not'); + test().fail(specName + ' - Initialize - Step 1 - Expected to hit this first, but did not'); } step++; }, function (err) { - fail(err.code + ': ' + err.description); - }); - }); - } - - function sessionProperties (session) { - return new Promise(function (resolve) { - expect(session instanceof chrome.cast.Session).toBeTruthy(); - expect(session.appId).toBeDefined(); - expect(session.displayName).toBeDefined(); - expect(session.receiver).toBeDefined(); - expect(session.receiver.friendlyName).toBeDefined(); - expect(session.addUpdateListener).toBeDefined(); - expect(session.removeUpdateListener).toBeDefined(); - expect(session.loadMedia).toBeDefined(); - - resolve(session); - }); - } - - function loadMedia (session) { - return new Promise(function (resolve) { - var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); - expect(mediaInfo).toBeTruthy(); - - var request = new chrome.cast.media.LoadRequest(mediaInfo); - expect(request).toBeTruthy(); - - session.loadMedia(request, function (media) { - - // Run all the media related tests - Promise.resolve({media: media, session: session}) - .then(mediaProperties) - .then(pauseSuccess) - .then(playSuccess) - .then(seekSuccess) - .then(setVolumeSuccess) - .then(muteVolumeSuccess) - .then(unmuteVolumeSuccess) - .then(stopSuccess) - .then(function (media) { - resolve(session); - }); - - }, function (err) { - fail(err.code + ': ' + err.description); - resolve(); + test().fail(err.code + ': ' + err.description); }); }); - } + }; + } - function mediaProperties (data) { - return new Promise(function (resolve) { - var media = data.media; - var session = data.session; - expect(media instanceof chrome.cast.media.Media).toBeTruthy(); - expect(media.sessionId).toEqual(session.sessionId); - expect(media.addUpdateListener).toBeDefined(); - expect(media.removeUpdateListener).toBeDefined(); - resolve(media); - }); - } - - function pauseSuccess (media) { - return new Promise(function (resolve) { - setTimeout(function () { - media.pause(null, function () { - resolve(media); - }, function (err) { - fail(err.code + ': ' + err.description); - resolve(); - }); - }, 500); + function requestSessionCancel () { + return new Promise(function (resolve) { + alert('---TEST INSTRUCTION---\nPlease click outside of the next dialog to dismiss it.'); + chrome.cast.requestSession(function (session) { + test().fail('We should not reach here on dismiss (make sure you cancelled the dialog for this test!)'); + }, function (err) { + test(err).toBeInstanceOf(chrome.cast.Error); + test(err.code).toEqual(chrome.cast.ErrorCode.CANCEL); + resolve(); }); - } + }); + } - function playSuccess (media) { - return new Promise(function (resolve) { - setTimeout(function () { - media.play(null, function () { - resolve(media); - }, function (err) { - fail(err.code + ': ' + err.description); - resolve(); - }); - }, 500); + function requestSessionSuccess () { + return new Promise(function (resolve) { + alert('---TEST INSTRUCTION---\nPlease select a valid chromecast in the next dialog.'); + chrome.cast.requestSession(function (session) { + Promise.resolve(session) + .then(sessionProperties) + .then(resolve); + }, function (err) { + test().fail(err.code + ': ' + err.description); }); - } + }); + } + + function sessionProperties (session) { + return new Promise(function (resolve) { + test(session).toBeInstanceOf(chrome.cast.Session); + test(session.appId).toBeDefined(); + test(session.displayName).toBeDefined(); + test(session.receiver).toBeDefined(); + test(session.receiver.friendlyName).toBeDefined(); + test(session.addUpdateListener).toBeDefined(); + test(session.removeUpdateListener).toBeDefined(); + test(session.loadMedia).toBeDefined(); + + resolve(session); + }); + } + + function loadMediaVideo (session) { + return new Promise(function (resolve) { + var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + test(mediaInfo).toBeDefined(); + + var request = new chrome.cast.media.LoadRequest(mediaInfo); + test(request).toBeDefined(); + + session.loadMedia(request, function (media) { + + // Run all the media related tests + Promise.resolve({media: media, session: session}) + .then(mediaProperties) + .then(pauseSuccess) + .then(playSuccess) + .then(seekSuccess) + .then(setVolumeSuccess) + .then(muteVolumeSuccess) + .then(unmuteVolumeSuccess) + .then(stopSuccess) + .then(function (media) { + resolve(session); + }); - function seekSuccess (media) { - return new Promise(function (resolve) { - setTimeout(function () { - var request = new chrome.cast.media.SeekRequest(); - request.currentTime = 3; - media.seek(request, function () { - resolve(media); - }, function (err) { - fail(err.code + ': ' + err.description); - resolve(); - }); - }, 500); + }, function (err) { + test().fail(err.code + ': ' + err.description); }); - } - - function setVolumeSuccess (media) { - return new Promise(function (resolve) { - var volume = new chrome.cast.Volume(); - volume.level = 0.2; - - var request = new chrome.cast.media.VolumeRequest(); - request.volume = volume; + }); + } + + function mediaProperties (data) { + return new Promise(function (resolve) { + var media = data.media; + var session = data.session; + test(media).toBeInstanceOf(chrome.cast.media.Media); + test(media.sessionId).toEqual(session.sessionId); + test(media.addUpdateListener).toBeDefined(); + test(media.removeUpdateListener).toBeDefined(); + resolve(media); + }); + } - media.setVolume(request, function () { + function pauseSuccess (media) { + return new Promise(function (resolve) { + setTimeout(function () { + media.pause(null, function () { resolve(media); }, function (err) { - fail(err.code + ': ' + err.description); - resolve(); + test().fail(err.code + ': ' + err.description); }); - }); - } + }, 500); + }); + } - function muteVolumeSuccess (media) { - return new Promise(function (resolve) { - var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, true)); - media.setVolume(request, function () { + function playSuccess (media) { + return new Promise(function (resolve) { + setTimeout(function () { + media.play(null, function () { resolve(media); }, function (err) { - fail(err.code + ': ' + err.description); - resolve(); + test().fail(err.code + ': ' + err.description); }); - }); - } - - function unmuteVolumeSuccess (media) { - return new Promise(function (resolve) { - var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, false)); - media.setVolume(request, function () { + }, 500); + }); + } + + function seekSuccess (media) { + return new Promise(function (resolve) { + setTimeout(function () { + var request = new chrome.cast.media.SeekRequest(); + request.currentTime = 3; + media.seek(request, function () { resolve(media); }, function (err) { - fail(err.code + ': ' + err.description); - resolve(); + test().fail(err.code + ': ' + err.description); }); + }, 500); + }); + } + + function setVolumeSuccess (media) { + return new Promise(function (resolve) { + var volume = new chrome.cast.Volume(); + volume.level = 0.2; + + var request = new chrome.cast.media.VolumeRequest(); + request.volume = volume; + + media.setVolume(request, function () { + resolve(media); + }, function (err) { + test().fail(err.code + ': ' + err.description); }); - } + }); + } - function stopSuccess (media) { - return new Promise(function (resolve) { - media.stop(null, function () { - resolve(media); - }, function (err) { - fail(err.code + ': ' + err.description); - resolve(); - }); + function muteVolumeSuccess (media) { + return new Promise(function (resolve) { + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, true)); + media.setVolume(request, function () { + resolve(media); + }, function (err) { + test().fail(err.code + ': ' + err.description); }); - } + }); + } - function stopSession (session) { - return new Promise(function (resolve) { - session.stop(function () { - resolve(session); - }, function (err) { - fail(err.code + ': ' + err.description); - resolve(); - }); + function unmuteVolumeSuccess (media) { + return new Promise(function (resolve) { + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, false)); + media.setVolume(request, function () { + resolve(media); + }, function (err) { + test().fail(err.code + ': ' + err.description); }); - } + }); + } - }); + function stopSuccess (media) { + return new Promise(function (resolve) { + media.stop(null, function () { + resolve(media); + }, function (err) { + test().fail(err.code + ': ' + err.description); + }); + }); + } - /** - * This tests that after the session has been stopped that we do not receive - * the old (dead) session on initialize if we ask to initialize again with - * the same app id. - */ - it('SPEC_01400 initialize should succeed (default receiver) but no session', function (done) { - var step = 1; - var sessionRequest = new chrome.cast.SessionRequest(applicationID_default); - var apiConfig = new chrome.cast.ApiConfig(sessionRequest, function (session) { - fail('SPEC_01400 should not receive a session'); - }, function receiverListener (available) { - if (step === 1) { - fail('SPEC_01400 - Did not hit initialize Step 1 first'); - } - if (step === 2) { - // Step 2 - We must get the unavailable notification - if (available !== 'unavailable') { - fail('SPEC_01400 - Step 2 - Hit receiver listener with non-unavailable status'); - } else { - step++; + function requestSessionStopCastingUiCancel (session) { + return new Promise(function (resolve) { + alert('---TEST INSTRUCTION---\nPlease click outside of the next dialog to dismiss it.'); + chrome.cast.requestSession(function () { + test().fail('We should not reach here on dismiss'); + }, function (err) { + test(err).toBeInstanceOf(chrome.cast.Error); + test(err.code).toEqual(chrome.cast.ErrorCode.CANCEL); + resolve(session); + }); + }); + } + + function requestSessionStopCastingUiStopSuccess (session) { + return new Promise(function (resolve) { + alert('---TEST INSTRUCTION---\nPlease click "Stop casting"'); + chrome.cast.requestSession(function () { + test().fail('We should not reach here on stop casting'); + }, function (err) { + test(err).toBeInstanceOf(chrome.cast.Error); + test(err.code).toEqual(chrome.cast.ErrorCode.CANCEL); + resolve(session); + }); + }); + } + + function sessionStop (session) { + return new Promise(function (resolve) { + session.stop(function () { + resolve(session); + }, function (err) { + test().fail(err.code + ': ' + err.description); + }); + }); + } + + function test (actual) { + return { + toEqual: function (expected) { + if (actual !== expected) { + throw new Error('Expected "' + actual + '" to be "' + expected + '"'); } - } - if (step === 3) { - // Step 3 - We are allowed to receive multiple unavailable until we receive the first available in this step - if (available !== 'unavailable' && available !== 'available') { - fail('SPEC_01400 - Step 3 - Hit receiver listener with incorrect status'); + }, + toBeInstanceOf: function (expected) { + if (!(actual instanceof expected)) { + throw new Error('Expected "' + actual + '" to be an instance of "' + expected.name + '"'); } - if (available === 'available') { - expect('success').toBeDefined(); - done(); + }, + toBeDefined: function () { + if (actual === null || actual === undefined) { + throw new Error('Expected "' + actual + '" to be defined.'); } + }, + fail: function (message) { + throw new Error(message); } - }); - chrome.cast.initialize(apiConfig, function () { - // Step 1 - if (step !== 1) { - fail('SPEC_01400 - Step 1 - Expected to hit this first, but did not'); - } - step++; - }, function (err) { - fail(err.code + ': ' + err.description); - done(); - }); - }); + }; + } }); }; From a5902dd98ba427021b6ec8db33bad1a218aa5eee Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 9 Sep 2019 07:50:43 -0600 Subject: [PATCH 028/166] Apparently my changes for scanning for routes, stopping the scan, and selecting the route do work. So I am going to commit so that they are in the history. Also removed the private _sessions array in chrome.cast.js. The app side can only ever maintain 1 session, so it is strange for the client side to track multiple sessions. (If it does have multiple sessions, all but the latest will be dead to it anyways. Relevant to Issue #36 --- README.md | 26 +++- src/android/Chromecast.java | 79 +++++++++---- src/android/ChromecastConnection.java | 51 +++++--- tests/tests.js | 60 ++++++++++ www/chrome.cast.js | 164 +++++++++++++------------- 5 files changed, 263 insertions(+), 117 deletions(-) diff --git a/README.md b/README.md index a25484f..aa3680b 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,31 @@ cordova plugin add https://github.com/jellyfin/cordova-plugin-chromecast.git This project attempts to implement the official Google Cast SDK for Chrome within Cordova. We've made a lot of progress in making this possible, so check out the [offical documentation](https://developers.google.com/cast/docs/chrome_sender) for examples. -When you call `chrome.cast.requestSession()` a popup will be displayed to select a Chromecast. If you would prefer to make your own interface you can call `chrome.cast.getRouteListElement()` which will return a `
    ` tag that contains the Chromecasts in a list. All you have to do is style that bad boy and you're off to the races! +When you call `chrome.cast.requestSession()` a popup will be displayed to select a Chromecast. + +Calling `chrome.cast.requestSession()` when you have an active session will display a popup with the option to "Stop Casting". + +**Specific to this plugin** (Not supported on desktop chrome) + +To make your own custom route selector use this: +``` +// This will begin an active scan for routes +chrome.cast.cordova.scanForRoutes(function (routes) { + // Here is where you should update your route selector view with the current routes + // This will called each time the routes change +}); + +// When the user selects a route +// stop the scan to save battery power +chrome.cast.cordova.stopScan(); +// and use the selected route.id to join the route +chrome.cast.cordova.selectRoute(route.id, function (session) { + // Save the session for your use +}, function (err) { + +}); + +``` ## Status diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 85c9ead..f0afac0 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -6,10 +6,12 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; +import java.util.List; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CallbackContext; import org.apache.cordova.LOG; +import org.apache.cordova.PluginResult; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -29,7 +31,9 @@ public final class Chromecast extends CordovaPlugin { /** Object to control the connection to the chromecast. */ private ChromecastConnection connection; /** Object to control the media. */ - private volatile ChromecastSession media; + private ChromecastSession media; + /** Holds the reference to the current client initiated scan. */ + private ChromecastConnection.ScanCallback clientScan; @Override protected void pluginInitialize() { @@ -136,7 +140,12 @@ public void run() { // See if there are any available routes ChromecastConnection.ScanCallback scanCallback = new ChromecastConnection.ScanCallback() { @Override - public void onRouteUpdate(RouteInfo route) { + public void onRouteUpdate(List routes) { + if (routes.size() == 0) { + // If no routes, just return, wait for a real route to be discovered + return; + } + // We found at least 1 route! so stop the scan connection.stopScan(this); @@ -205,11 +214,12 @@ public void onError(String errorCode) { /** * Selects a route by its id. * @param routeId the id of the route to join + * @param routeName the name of the route * @param callbackContext called with .success or .error depending on the result * @return true for cordova */ - public boolean selectRoute(final String routeId, final CallbackContext callbackContext) { - connection.join(routeId, new ChromecastConnection.JoinCallback() { + public boolean selectRoute(final String routeId, final String routeName, final CallbackContext callbackContext) { + connection.join(routeId, routeName, new ChromecastConnection.JoinCallback() { @Override public void onJoin(CastSession castSession) { callbackContext.success(media.createSessionObject()); @@ -418,18 +428,46 @@ public boolean sessionLeave(CallbackContext callbackContext) { } /** - * Emits all routes. + * Will actively scan for routes and send a json array to the client. + * It is super important that client calls "stopScan", otherwise the + * battery could drain quickly. * @param callbackContext called with .success or .error depending on the result * @return true for cordova */ - public boolean emitAllRoutes(CallbackContext callbackContext) { - // TODO will use connection.scanForRoutes(); + public boolean startRouteScan(CallbackContext callbackContext) { + if (clientScan != null) { + // Stop any other existing clientScan + connection.stopScan(clientScan); + } + clientScan = new ChromecastConnection.ScanCallback() { + @Override + void onRouteUpdate(List routes) { + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, routesToJSON(routes)); + pluginResult.setKeepCallback(true); + callbackContext.sendPluginResult(pluginResult); + } + }; + connection.scanForRoutes(clientScan); return true; } /** - * Sends the receiverState. + * Stops the scan started by startRouteScan. + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean stopRouteScan(CallbackContext callbackContext) { + if (clientScan != null) { + // Stop any other existing clientScan + connection.stopScan(clientScan); + } + callbackContext.success(); + return true; + } + + /** + * sends the receiverState. * @param receiverState true if we should send receiverAvailable, * false if we should send receiverUnavailable */ @@ -443,20 +481,21 @@ private void sendReceiverUpdate(boolean receiverState) { /** * Simple helper to convert a route to JSON for passing down to the javascript side. - * @param route the route to convert - * @return a JSON representation of the route + * @param routes the routes to convert + * @return a JSON Array of JSON representations of the routes */ - private JSONObject routeToJSON(RouteInfo route) { - JSONObject obj = new JSONObject(); - - try { - obj.put("name", route.getName()); - obj.put("id", route.getId()); - } catch (JSONException e) { - e.printStackTrace(); + private JSONArray routesToJSON(List routes) { + JSONArray routesArray = new JSONArray(); + for (RouteInfo route : routes) { + try { + JSONObject obj = new JSONObject(); + obj.put("name", route.getName()); + obj.put("id", route.getId()); + routesArray.put(obj); + } catch (JSONException e) { + } } - - return obj; + return routesArray; } /** Handles remoteMediaClient callbacks. */ diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index a8cdcdd..2b820a9 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -16,6 +16,9 @@ import com.google.android.gms.cast.framework.SessionManager; import com.google.android.gms.cast.framework.SessionManagerListener; +import java.util.ArrayList; +import java.util.List; + public class ChromecastConnection { /** Lifetime variable. */ @@ -114,10 +117,11 @@ public void rejoin(JoinCallback callback) { /** * This will create a new session or seamlessly join an existing one if we created it. * @param routeId the id of the route to join + * @param routeName the name of the route * @param callback calls callback.onJoin when we have joined a session, * or callback.onError if an error occurred */ - public void join(String routeId, JoinCallback callback) { + public void join(final String routeId, final String routeName, JoinCallback callback) { activity.runOnUiThread(new Runnable() { public void run() { if (session != null) { @@ -129,8 +133,8 @@ public void run() { Intent castIntent = new Intent(); castIntent.putExtra("CAST_INTENT_TO_CAST_ROUTE_ID_KEY", routeId); - // Not sure of this one's purpose, possibly just for display - // castIntent.putExtra("CAST_INTENT_TO_CAST_DEVICE_NAME_KEY", deviceName); + // RouteName and toast are just for display + castIntent.putExtra("CAST_INTENT_TO_CAST_DEVICE_NAME_KEY", routeName); castIntent.putExtra("CAST_INTENT_TO_CAST_NO_TOAST_KEY", false); getSessionManager().startSession(castIntent); @@ -237,11 +241,10 @@ public void scanForRoutes(ScanCallback callback) { // Add the callback in active scan mode activity.runOnUiThread(new Runnable() { public void run() { + callback.setMediaRouter(getMediaRouter()); // Send out the initial routes - for (RouteInfo route : getMediaRouter().getRoutes()) { - callback.onFilteredRouteUpdate(route); - } + callback.onFilteredRouteUpdate(); // Add the callback in active scan mode getMediaRouter().addCallback(new MediaRouteSelector.Builder() @@ -363,12 +366,22 @@ public interface JoinCallback { public abstract static class ScanCallback extends MediaRouter.Callback { /** * Called whenever a route is updated. - * @param route the route that was just updated + * @param routes the currently available routes */ - abstract void onRouteUpdate(RouteInfo route); + abstract void onRouteUpdate(List routes); /** records whether we have been stopped or not. */ private boolean stopped = false; + /** Global mediaRouter object. */ + private MediaRouter mediaRouter; + + /** + * Sets the mediaRouter object. + * @param router mediaRouter object + */ + void setMediaRouter(MediaRouter router) { + this.mediaRouter = router; + } /** * Call this method when you wish to stop scanning. @@ -378,25 +391,33 @@ public abstract static class ScanCallback extends MediaRouter.Callback { void stop() { stopped = true; } - private void onFilteredRouteUpdate(RouteInfo route) { - if (stopped) { + private void onFilteredRouteUpdate() { + if (stopped || mediaRouter == null) { return; } - if (!route.isDefault()) { - onRouteUpdate(route); + List routes = mediaRouter.getRoutes(); + List outRoutes = new ArrayList<>(); + // Filter the routes + for (RouteInfo route : routes) { + // We don't want default routes + // or multizone duplicates https://github.com/jellyfin/cordova-plugin-chromecast/issues/32 + if (!route.isDefault()) { + outRoutes.add(route); + } } + onRouteUpdate(outRoutes); } @Override public final void onRouteAdded(MediaRouter router, RouteInfo route) { - onFilteredRouteUpdate(route); + onFilteredRouteUpdate(); } @Override public final void onRouteChanged(MediaRouter router, RouteInfo route) { - onFilteredRouteUpdate(route); + onFilteredRouteUpdate(); } @Override public final void onRouteRemoved(MediaRouter router, RouteInfo route) { - onFilteredRouteUpdate(route); + onFilteredRouteUpdate(); } } diff --git a/tests/tests.js b/tests/tests.js index 224aa14..b3e8bd7 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -81,6 +81,20 @@ exports.defineAutoTests = function () { expect(chrome.cast.media.Media.prototype.removeUpdateListener).toBeDefined(); }); + it('SPEC_00300 chrome.cast.cordova functions', function (done) { + setupEarlyTerminator(done); + Promise.resolve() + .then(apiAvailable) + .then(initialize('SPEC_00300', function (session) { + throw new Error('should not receive a session (make sure there is no active cast session when starting the tests)'); + })) + .then(startRouteScan) + .then(stopRouteScan) + .then(selectRoute) + .then(sessionStop) + .then(done); + }, USER_INTERACTION_TIMEOUT); + /** * Pre-requisite: You must have a valid receiver (chromecast) plugged in and available. */ @@ -390,6 +404,52 @@ exports.defineAutoTests = function () { }); } + function startRouteScan () { + return new Promise(function (resolve) { + var once = true; + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (once && routes.length > 0) { + once = false; + + var route = routes[0]; + test(route).toBeInstanceOf(chrome.cast.cordova.Route); + test(route.id).toBeDefined(); + test(route.name).toBeDefined(); + + resolve(routes); + } + }, function (err) { + fail(err.code + ': ' + err.description); + resolve(); + }); + }); + } + + function stopRouteScan (routes) { + return new Promise(function (resolve) { + // Make sure we can stop the scan + chrome.cast.cordova.stopRouteScan(function () { + resolve(routes); + }, function (err) { + test().fail(err.code + ': ' + err.description); + resolve(); + }); + }); + } + + function selectRoute (routes) { + return new Promise(function (resolve) { + chrome.cast.cordova.selectRoute(routes[0], function (session) { + Promise.resolve(session) + .then(sessionProperties) + .then(resolve); + }, function (err) { + fail(err.code + ': ' + err.description); + resolve(routes); + }); + }); + } + function test (actual) { return { toEqual: function (expected) { diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 5957ff2..97ee9a3 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -528,11 +528,8 @@ var _defaultActionPolicy = null; var _receiverListener = null; var _sessionListener = null; -var _sessions = {}; +var _session; var _currentMedia = null; -var _routeListEl = document.createElement('ul'); -_routeListEl.classList.add('route-list'); -var _routeList = {}; /** * Initializes the API. Note that either successCallback and errorCallback will be invoked once the API has finished initialization. @@ -579,23 +576,7 @@ chrome.cast.requestSession = function (successCallback, errorCallback, opt_sessi execute('requestSession', function (err, obj) { if (!err) { - var sessionId = obj.sessionId; - var appId = obj.appId; - var displayName = obj.displayName; - var appImages = obj.appImages || []; - var receiver = new chrome.cast.Receiver(obj.receiver.label, obj.receiver.friendlyName, obj.receiver.capabilities || [], obj.receiver.volume || null); - - var session = _sessions[sessionId] = new chrome.cast.Session(sessionId, appId, displayName, appImages, receiver); - - if (obj.media && obj.media.sessionId) { - _currentMedia = new chrome.cast.media.Media(sessionId, obj.media.mediaSessionId); - _currentMedia.currentTime = obj.media.currentTime; - _currentMedia.playerState = obj.media.playerState; - _currentMedia.media = obj.media.media; - session.media[0] = _currentMedia; - } - - successCallback(session); + successCallback(updateSession(obj)); } else { handleError(err, errorCallback); } @@ -819,6 +800,10 @@ chrome.cast.Session.prototype.addUpdateListener = function (listener) { */ chrome.cast.Session.prototype.removeUpdateListener = function (listener) { this.removeListener('_sessionUpdated', listener); + // var index = array.indexOf(5); + // if (index > -1) { + // array.splice(index, 1); + // } }; /** @@ -1129,44 +1114,67 @@ chrome.cast.media.Media.prototype._update = function (isAlive, obj) { this.emit('_mediaUpdated', isAlive); }; -function createRouteElement (route) { - var el = document.createElement('li'); - el.classList.add('route'); - el.addEventListener('touchstart', onRouteClick); - el.textContent = route.name; - el.setAttribute('data-routeid', route.id); - return el; -} - -function onRouteClick () { - var id = this.getAttribute('data-routeid'); - - if (id) { - try { - chrome.cast._emitConnecting(); - } catch (e) { - console.error('Error in connectingListener', e); - } +/** + * This contains function exclusive the cordova plugin + * and equivalents are not available in the chromecast + * desktop SDK. Use with caution if you also want your + * site to work with chrome on desktop. + */ +chrome.cast.cordova = { - execute('selectRoute', id, function (err, obj) { - if (err) { - // TODO + /** + * Will actively scan for routes and send the complete list of + * active routes whenever a route change is detected. + * It is super important that client calls "stopScan", otherwise the + * battery could drain quickly. + * @param {function(routes)} successCallback + * @param {function(chrome.cast.Error)} successCallback + */ + startRouteScan: function (successCallback, errorCallback) { + execute('startRouteScan', function (err, routes) { + if (!err) { + for (var i = 0; i < routes.length; i++) { + routes[i] = new chrome.cast.cordova.Route(routes[i].id, routes[i].name); + } + successCallback(routes); + } else { + handleError(err, errorCallback); + } + }); + }, + /** + * Stops any active scanForRoutes. + * @param {function(routes)} successCallback + * @param {function(chrome.cast.Error)} successCallback + */ + stopRouteScan: function (successCallback, errorCallback) { + execute('stopRouteScan', function (err) { + if (!err) { + successCallback(); + } else { + handleError(err, errorCallback); + } + }); + }, + /** + * Attempts to join the requested route + * @param {chrome.cast.cordova.Route} route + * @param {function(routes)} successCallback + * @param {function(chrome.cast.Error)} successCallback + */ + selectRoute: function (route, successCallback, errorCallback) { + execute('selectRoute', route.id, route.name, function (err, session) { + if (!err) { + successCallback(updateSession(session)); + } else { + handleError(err, errorCallback); } - var sessionId = obj.sessionId; - var appId = obj.appId; - var displayName = obj.displayName; - var appImages = obj.appImages || []; - var receiver = new chrome.cast.Receiver(obj.receiver.label, obj.receiver.friendlyName, obj.receiver.capabilities || [], obj.receiver.volume || null); - - var session = _sessions[sessionId] = new chrome.cast.Session(sessionId, appId, displayName, appImages, receiver); - - _sessionListener && _sessionListener(session); }); + }, + Route: function (id, name) { + this.id = id; + this.name = name; } -} - -chrome.cast.getRouteListElement = function () { - return _routeListEl; }; var _connectingListeners = []; @@ -1194,23 +1202,9 @@ chrome.cast._ = { receiverAvailable: function () { _receiverListener(chrome.cast.ReceiverAvailability.AVAILABLE); }, - routeAdded: function (route) { - if (!_routeList[route.id]) { - route.el = createRouteElement(route); - _routeList[route.id] = route; - - _routeListEl.appendChild(route.el); - } - }, - routeRemoved: function (route) { - if (_routeList[route.id]) { - _routeList[route.id].el.remove(); - delete _routeList[route.id]; - } - }, sessionUpdated: function (isAlive, session) { - if (session && session.sessionId && _sessions[session.sessionId]) { - _sessions[session.sessionId]._update(isAlive, session); + if (session && session.sessionId && _session) { + _session._update(isAlive, session); } }, mediaUpdated: function (isAlive, media) { @@ -1223,29 +1217,29 @@ chrome.cast._ = { _currentMedia.playerState = media.playerState; _currentMedia.media = media.media; - _sessions[media.sessionId].media[0] = _currentMedia; - _sessionListener && _sessionListener(_sessions[media.sessionId]); + _session.media[0] = _currentMedia; + _sessionListener && _sessionListener(_session); } } }, mediaLoaded: function (isAlive, media) { - if (_sessions[media.sessionId]) { + if (_session) { if (!_currentMedia) { _currentMedia = new chrome.cast.media.Media(media.sessionId, media.mediaSessionId); } _currentMedia._update(isAlive, media); - _sessions[media.sessionId].emit('_mediaListener', _currentMedia); + _session.emit('_mediaListener', _currentMedia); } else { console.log('mediaLoaded --- but there is no session tied to it', media); } }, sessionListener: function (javaSession) { - var session = getJsSession(javaSession); + var session = updateSession(javaSession); _sessionListener && _sessionListener(session); }, sessionJoined: function (obj) { - var session = getJsSession(obj); + var session = updateSession(obj); if (obj.media && obj.media.sessionId) { _currentMedia = new chrome.cast.media.Media(session.sessionId, obj.media.mediaSessionId); @@ -1258,16 +1252,16 @@ chrome.cast._ = { _sessionListener && _sessionListener(session); }, onMessage: function (sessionId, namespace, message) { - if (_sessions[sessionId]) { - _sessions[sessionId].emit('message:' + namespace, namespace, message); + if (_session) { + _session.emit('message:' + namespace, namespace, message); } } }; module.exports = chrome.cast; -function getJsSession (javaSession) { - return new chrome.cast.Session( +function updateSession (javaSession) { + _session = new chrome.cast.Session( javaSession.sessionId, javaSession.appId, javaSession.displayName, @@ -1279,6 +1273,14 @@ function getJsSession (javaSession) { javaSession.receiver.volume || null ) ); + if (javaSession.media && javaSession.media.sessionId) { + _currentMedia = new chrome.cast.media.Media(javaSession.sessionId, javaSession.media.mediaSessionId); + _currentMedia.currentTime = javaSession.media.currentTime; + _currentMedia.playerState = javaSession.media.playerState; + _currentMedia.media = javaSession.media.media; + _session.media[0] = _currentMedia; + } + return _session; } function execute (action) { From 4f92a94e143ee2474742388ca50c8dec7051a210 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 9 Sep 2019 07:51:23 -0600 Subject: [PATCH 029/166] fix createAppImagesObject --- src/android/ChromecastSession.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index fbf9547..16308f1 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -433,7 +433,6 @@ public void onResult(@NonNull MediaChannelResult result) { }); } - /** * Sets the receiver volume level. * @param volume volume to set the receiver to @@ -502,12 +501,11 @@ public JSONObject createSessionObject() { return out; } - private JSONObject createAppImagesObject() { - JSONObject out = new JSONObject(); + private JSONArray createAppImagesObject() { + JSONArray appImages = new JSONArray(); try { MediaMetadata metadata = client.getMediaInfo().getMetadata(); List images = metadata.getImages(); - JSONArray appImages = new JSONArray(); if (images != null) { for (WebImage o : images) { appImages.put(o.toString()); @@ -516,7 +514,7 @@ private JSONObject createAppImagesObject() { } catch (NullPointerException e) { e.printStackTrace(); } - return out; + return appImages; } private JSONObject createReceiverObject() { From 20e5381aaaf6d2fa45d91e0277cabb5ceb24b0ba Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Tue, 10 Sep 2019 05:01:29 -0600 Subject: [PATCH 030/166] Added tests for session Leave and session Stop. Cleaned up their functionality to match chrome desktop SDK a bit more. Add names to constructors in chrome.cast.js to improve out in test.js test().toBeDefined() --- src/android/Chromecast.java | 37 ++--- src/android/ChromecastConnection.java | 60 +++++--- tests/tests.js | 122 +++++++++++++++- www/chrome.cast.js | 203 ++++++++++++++++---------- 4 files changed, 301 insertions(+), 121 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index f0afac0..93f547e 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -40,11 +40,10 @@ protected void pluginInitialize() { super.pluginInitialize(); this.media = new ChromecastSession(cordova.getActivity(), remoteMediaClientCallback); - this.connection = new ChromecastConnection(cordova.getActivity(), this.media, new ChromecastConnection.ConnectionListener() { + this.connection = new ChromecastConnection(cordova.getActivity(), this.media, new ChromecastConnection.Callback() { @Override - public void onDisconnected(int reason) { - sendJavascript("chrome.cast.Session.prototype._update(false, {});"); -// sendJavascript("chrome.cast._.sessionUpdated(false, " + session.toString() + ");"); + public void run() { + sendSessionUpdate("stopped"); } }); } @@ -259,16 +258,6 @@ public boolean setReceiverMuted(Boolean muted, CallbackContext callbackContext) return true; } - /** - * Stop the session. - * @param callbackContext called with .success or .error depending on the result - * @return true for cordova - */ - public boolean stopSession(CallbackContext callbackContext) { - connection.kill(); - return true; - } - /** * Send a custom message to the receiver - we don't need this just yet... it was just simple to implement on the js side. * @param namespace namespace @@ -411,8 +400,12 @@ public boolean mediaEditTracksInfo(JSONArray activeTrackIds, JSONObject textTrac * @return true for cordova */ public boolean sessionStop(CallbackContext callbackContext) { - connection.kill(); - callbackContext.success(); + connection.endSession(true, callbackContext, new ChromecastConnection.Callback() { + @Override + public void run() { + sendSessionUpdate("stopped"); + } + }); return true; } @@ -422,8 +415,12 @@ public boolean sessionStop(CallbackContext callbackContext) { * @return true for cordova */ public boolean sessionLeave(CallbackContext callbackContext) { - connection.leave(); - callbackContext.success(); + connection.endSession(true, callbackContext, new ChromecastConnection.Callback() { + @Override + public void run() { + sendSessionUpdate("disconnected"); + } + }); return true; } @@ -466,6 +463,10 @@ public boolean stopRouteScan(CallbackContext callbackContext) { return true; } + private void sendSessionUpdate(String status) { + sendJavascript("chrome.cast._.sessionUpdated('" + status + "');"); + } + /** * sends the receiverState. * @param receiverState true if we should send receiverAvailable, diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index af42d6f..6bdc2fc 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -18,6 +18,8 @@ import com.google.android.gms.cast.framework.SessionManager; import com.google.android.gms.cast.framework.SessionManagerListener; +import org.apache.cordova.CallbackContext; + import java.util.ArrayList; import java.util.List; @@ -29,6 +31,8 @@ public class ChromecastConnection { private ChromecastSession media; /** Lifetime variable. */ private SessionListener newConnectionListener; + /** Should we pass disconnects to the client's externalDisconnectListener. */ + private boolean enableClientExtenalDisconnectListener = false; /** Initialize lifetime variable. */ private String appId; @@ -42,7 +46,7 @@ public class ChromecastConnection { * @param chromecastSession the chromecastSession object that we should load with a new sessions * @param listener the listener that listens for session end event */ - public ChromecastConnection(Activity act, ChromecastSession chromecastSession, ConnectionListener listener) { + public ChromecastConnection(Activity act, ChromecastSession chromecastSession, Callback listener) { this.activity = act; this.media = chromecastSession; @@ -53,8 +57,8 @@ public void run() { @Override public void onSessionEnded(CastSession castSession, int error) { setSession(null); - if (listener != null) { - listener.onDisconnected(error); + if (listener != null && enableClientExtenalDisconnectListener) { + listener.run(); } } }, CastSession.class); @@ -190,7 +194,9 @@ public void onCancel(DialogInterface dialog) { } else { // We are are already connected, so show the "connection options" Dialog AlertDialog.Builder builder = new AlertDialog.Builder(activity); - builder.setTitle(session.getCastDevice().getFriendlyName()); + if (session.getCastDevice() != null) { + builder.setTitle(session.getCastDevice().getFriendlyName()); + } builder.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { @@ -200,7 +206,7 @@ public void onDismiss(DialogInterface dialog) { builder.setPositiveButton("Stop Casting", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - kill(); + endSession(true, null, null); } }); builder.show(); @@ -272,25 +278,41 @@ public void run() { } /** - * Leaves the session. + * Exits the current session. + * @param stopCasting should the receiver application be stopped as well? + * @param callback called with .success or .error depending on the initial result + * @param disconnected called when the session actually ends */ - public void leave() { + public void endSession(boolean stopCasting, CallbackContext callback, Callback disconnected) { activity.runOnUiThread(new Runnable() { public void run() { setSession(null); - getMediaRouter().unselect(MediaRouter.UNSELECT_REASON_DISCONNECTED); - } - }); - } + if (getSessionManager().getCurrentCastSession() == null) { + if (callback != null) { + callback.error("invalid_parameter"); + } + return; + } - /** - * Kills the session. - */ - public void kill() { - activity.runOnUiThread(new Runnable() { - public void run() { - setSession(null); - getSessionManager().endCurrentSession(true); + // Disable the externalDisconnectListener temporarily + enableClientExtenalDisconnectListener = false; + + getSessionManager().addSessionManagerListener(new SessionListener() { + @Override + public void onSessionEnded(CastSession castSession, int error) { + getSessionManager().removeSessionManagerListener(this, CastSession.class); + // Re-enable the externalDisconnectListener + enableClientExtenalDisconnectListener = true; + if (disconnected != null) { + disconnected.run(); + } + } + }, CastSession.class); + + getSessionManager().endCurrentSession(stopCasting); + if (callback != null) { + callback.success(); + } } }); } diff --git a/tests/tests.js b/tests/tests.js index b3e8bd7..4dab002 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -81,17 +81,18 @@ exports.defineAutoTests = function () { expect(chrome.cast.media.Media.prototype.removeUpdateListener).toBeDefined(); }); - it('SPEC_00300 chrome.cast.cordova functions', function (done) { + it('SPEC_00300 chrome.cast.cordova functions and leaveSession', function (done) { setupEarlyTerminator(done); Promise.resolve() .then(apiAvailable) .then(initialize('SPEC_00300', function (session) { - throw new Error('should not receive a session (make sure there is no active cast session when starting the tests)'); + test().fail('should not receive a session (make sure there is no active cast session when starting the tests)'); })) .then(startRouteScan) .then(stopRouteScan) .then(selectRoute) - .then(sessionStop) + .then(sessionLeaveSuccess) + .then(sessionLeaveError_alreadyLeft) .then(done); }, USER_INTERACTION_TIMEOUT); @@ -131,7 +132,9 @@ exports.defineAutoTests = function () { .then(initialize('SPEC_00506', function (session) { Promise.resolve(session) .then(sessionProperties) - .then(sessionStop) + .then(sessionStopSuccess) + .then(sessionStopError_noSession) + .then(sessionLeaveError_noSession) .then(done); })); }, USER_INTERACTION_TIMEOUT); @@ -384,26 +387,135 @@ exports.defineAutoTests = function () { function requestSessionStopCastingUiStopSuccess (session) { return new Promise(function (resolve) { alert('---TEST INSTRUCTION---\nPlease click "Stop casting"'); + // We need to hit both of there callbacks + var hitUpdateListener = false; + var hitErrHandler = false; + session.addUpdateListener(function (isAlive) { + if (hitUpdateListener) { + test().fail('requestSessionStopCastingUISuccess - we already hit the updateListener once! What is going on?'); + } + hitUpdateListener = true; + test(isAlive).toEqual(false); + if (hitErrHandler) { + resolve(session); + } + }); + chrome.cast.requestSession(function () { test().fail('We should not reach here on stop casting'); }, function (err) { + if (hitErrHandler) { + test().fail('requestSessionStopCastingUISuccess - we already hit the errorHandler once! What is going on?'); + } + hitErrHandler = true; test(err).toBeInstanceOf(chrome.cast.Error); test(err.code).toEqual(chrome.cast.ErrorCode.CANCEL); + if (hitUpdateListener) { + resolve(session); + } + }); + }); + } + + function sessionLeaveSuccess (session) { + return new Promise(function (resolve) { + // We need to hit both of there callbacks + var hitUpdateListener = false; + var hitErrHandler = false; + session.addUpdateListener(function (isAlive) { + if (hitUpdateListener) { + test().fail('session.leave - we already hit the updateListener once! What is going on?'); + } + hitUpdateListener = true; + test(isAlive).toEqual(true); + test(session.status).toEqual(chrome.cast.SessionStatus.DISCONNECTED); + if (hitErrHandler) { + resolve(session); + } + }); + session.leave(function () { + if (hitErrHandler) { + test().fail('session.leave - we already hit the errorHandler once! What is going on?'); + } + hitErrHandler = true; + if (hitUpdateListener) { + resolve(session); + } + }, function (err) { + test().fail(err.code + ': ' + err.description); + }); + }); + } + + function sessionLeaveError_alreadyLeft (session) { + return new Promise(function (resolve) { + session.stop(function () { + test().fail('session.leave - Should not call success'); + }, function (err) { + test(err).toBeInstanceOf(chrome.cast.Error); + test(err.code).toEqual(chrome.cast.Error.INVALID_PARAMETER); + test(err.description).toEqual('No active session'); resolve(session); }); }); } - function sessionStop (session) { + function sessionLeaveError_noSession (session) { return new Promise(function (resolve) { session.stop(function () { + test().fail('session.leave - Should not call success'); + }, function (err) { + test(err).toBeInstanceOf(chrome.cast.Error); + test(err.code).toEqual(chrome.cast.Error.INVALID_PARAMETER); + test(err.description).toEqual('No active session'); resolve(session); + }); + }); + } + + function sessionStopSuccess (session) { + return new Promise(function (resolve) { + // We need to hit both of there callbacks + var hitUpdateListener = false; + var hitErrHandler = false; + session.addUpdateListener(function (isAlive) { + if (hitUpdateListener) { + test().fail('session.stop - we already hit the updateListener once! What is going on?'); + } + hitUpdateListener = true; + test(isAlive).toEqual(false); + test(session.status).toEqual(chrome.cast.SessionStatus.STOPPED); + if (hitErrHandler) { + resolve(session); + } + }); + session.stop(function () { + if (hitErrHandler) { + test().fail('session.stop - we already hit the errorHandler once! What is going on?'); + } + hitErrHandler = true; + if (hitUpdateListener) { + resolve(session); + } }, function (err) { test().fail(err.code + ': ' + err.description); }); }); } + function sessionStopError_noSession (session) { + return new Promise(function (resolve) { + session.stop(function () { + test().fail('session.stop - Should not call success'); + }, function (err) { + test(err).toBeInstanceOf(chrome.cast.Error); + test(err.code).toEqual(chrome.cast.Error.INVALID_PARAMETER); + test(err.description).toEqual('No active session'); + resolve(session); + }); + }); + } + function startRouteScan () { return new Promise(function (resolve) { var once = true; diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 97ee9a3..1735c58 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -98,6 +98,8 @@ chrome.cast = { NOT_IMPLEMENTED: 'not_implemented' }, + SessionStatus: { CONNECTED: 'connected', DISCONNECTED: 'disconnected', STOPPED: 'stopped' }, + /** * TODO: Update when the official API docs are finished * https://developers.google.com/cast/docs/reference/chrome/chrome.cast.timeout @@ -291,7 +293,7 @@ chrome.cast = { * @property {number} currentTime Seconds from the beginning of the media to start playback. * @property {Object} customData Custom data for the receiver application. */ - LoadRequest: function (media) { + LoadRequest: function LoadRequest (media) { this.type = 'LOAD'; this.sessionId = this.requestId = this.customData = this.currentTime = null; this.media = media; @@ -302,7 +304,7 @@ chrome.cast = { * A request to play the currently paused media. * @property {Object} customData Custom data for the receiver application. */ - PlayRequest: function () { + PlayRequest: function PlayRequest () { this.customData = null; }, @@ -312,7 +314,7 @@ chrome.cast = { * @property {chrome.cast.media.ResumeState} resumeState The desired media player state after the seek is complete. * @property {Object} customData Custom data for the receiver application. */ - SeekRequest: function () { + SeekRequest: function SeekRequest () { this.customData = this.resumeState = this.currentTime = null; }, @@ -321,7 +323,7 @@ chrome.cast = { * @param {chrome.cast.Volume} volume The new volume of the stream. * @property {Object} customData Custom data for the receiver application. */ - VolumeRequest: function (volume) { + VolumeRequest: function VolumeRequest (volume) { this.volume = volume; this.customData = null; }, @@ -330,7 +332,7 @@ chrome.cast = { * A request to stop the media player. * @property {Object} customData Custom data for the receiver application. */ - StopRequest: function () { + StopRequest: function StopRequest () { this.customData = null; }, @@ -338,7 +340,7 @@ chrome.cast = { * A request to pause the currently playing media. * @property {Object} customData Custom data for the receiver application. */ - PauseRequest: function () { + PauseRequest: function PauseRequest () { this.customData = null; }, @@ -351,7 +353,7 @@ chrome.cast = { * @property {string} title Content title. * @property {chrome.cast.media.MetadataType} type The type of metadata. */ - GenericMediaMetadata: function () { + GenericMediaMetadata: function GenericMediaMetadata () { this.metadataType = this.type = chrome.cast.media.MetadataType.GENERIC; this.releaseDate = this.releaseYear = this.images = this.subtitle = this.title = null; }, @@ -366,7 +368,7 @@ chrome.cast = { * @property {string} title Content title. * @property {chrome.cast.media.MetadataType} type The type of metadata. */ - MovieMediaMetadata: function () { + MovieMediaMetadata: function MovieMediaMetadata () { this.metadataType = this.type = chrome.cast.media.MetadataType.MOVIE; this.releaseDate = this.releaseYear = this.images = this.subtitle = this.studio = this.title = null; }, @@ -387,7 +389,7 @@ chrome.cast = { * @property {number} trackNumber Track number in album. * @property {chrome.cast.media.MetadataType} type The type of metadata. */ - MusicTrackMediaMetadata: function () { + MusicTrackMediaMetadata: function MusicTrackMediaMetadata () { this.metadataType = this.type = chrome.cast.media.MetadataType.MUSIC_TRACK; this.releaseDate = this.releaseYear = this.images = this.discNumber = this.trackNumber = this.artistName = this.songName = this.composer = this.artist = this.albumArtist = this.title = this.albumName = null; }, @@ -405,7 +407,7 @@ chrome.cast = { * @property {chrome.cast.media.MetadataType} type The type of metadata. * @property {number} width Photo width, in pixels. */ - PhotoMediaMetadata: function () { + PhotoMediaMetadata: function PhotoMediaMetadata () { this.metadataType = this.type = chrome.cast.media.MetadataType.PHOTO; this.creationDateTime = this.height = this.width = this.longitude = this.latitude = this.images = this.location = this.artist = this.title = null; }, @@ -424,7 +426,7 @@ chrome.cast = { * @property {string} title TV episode title. * @property {chrome.cast.media.MetadataType} type The type of metadata. */ - TvShowMediaMetadata: function () { + TvShowMediaMetadata: function TvShowMediaMetadata () { this.metadataType = this.type = chrome.cast.media.MetadataType.TV_SHOW; this.originalAirdate = this.releaseYear = this.images = this.episode = this.episodeNumber = this.season = this.seasonNumber = this.episodeTitle = this.title = this.seriesTitle = null; }, @@ -438,7 +440,7 @@ chrome.cast = { * @property {any type} metadata Describes the media content. * @property {chrome.cast.media.StreamType} streamType The type of media stream. */ - MediaInfo: function (contentId, contentType) { + MediaInfo: function MediaInfo (contentId, contentType) { this.contentId = contentId; this.streamType = chrome.cast.media.StreamType.BUFFERED; this.contentType = contentType; @@ -460,7 +462,7 @@ chrome.cast = { * @param {number} trackId Unique identifier of the track within the context of a chrome.cast.media.MediaInfo objects * @param {chrome.cast.media.TrackType} trackType The type of track. Value must not be null. */ - Track: function (trackId, trackType) { + Track: function Track (trackId, trackType) { this.trackId = trackId; this.type = trackType; this.customData = this.language = this.name = this.subtype = this.trackContentId = this.trackContentType = null; @@ -500,7 +502,7 @@ chrome.cast = { * the 0-255 value for the specific channel/color. It follows CSS 8-digit hex color notation (See * http://dev.w3.org/csswg/css-color/#hex-notation). */ - TextTrackStyle: function () { + TextTrackStyle: function TextTrackStyle () { this.backgroundColor = this.customData = this.edgeColor = this.edgeType = this.fontFamily = this.fontGenericFamily = this.fontScale = this.fontStyle = this.foregroundColor = this.windowColor = this.windowRoundedCornerRadius = @@ -514,7 +516,7 @@ chrome.cast = { * @param {number[]} opt_activeTrackIds Optional. * @param {chrome.cast.media.TextTrackStyle} opt_textTrackSytle Optional. **/ - EditTracksInfoRequest: function (opt_activeTrackIds, opt_textTrackSytle) { + EditTracksInfoRequest: function EditTracksInfoRequest (opt_activeTrackIds, opt_textTrackSytle) { this.activeTrackIds = opt_activeTrackIds; this.textTrackSytle = opt_textTrackSytle; this.requestId = null; @@ -607,7 +609,7 @@ chrome.cast.setCustomReceivers = function (receivers, successCallback, errorCall * @property {chrome.cast.SenderApplication} senderApps The sender applications supported by the receiver application. * @property {string} statusText Descriptive text for the current application content, for example “My Wedding Slideshow”. */ -chrome.cast.Session = function (sessionId, appId, displayName, appImages, receiver) { +chrome.cast.Session = function Session (sessionId, appId, displayName, appImages, receiver) { EventEmitter.call(this); this.sessionId = sessionId; this.appId = appId; @@ -615,6 +617,7 @@ chrome.cast.Session = function (sessionId, appId, displayName, appImages, receiv this.appImages = appImages || []; this.receiver = receiver; this.media = []; + this.status = chrome.cast.SessionStatus.CONNECTED; }; chrome.cast.Session.prototype = Object.create(EventEmitter.prototype); @@ -671,11 +674,19 @@ chrome.cast.Session.prototype.stop = function (successCallback, errorCallback) { errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); return; } - + if (this.status !== chrome.cast.SessionStatus.CONNECTED) { + errorCallback(new chrome.cast.Error(chrome.cast.Error.INVALID_PARAMETER, 'No active session', null)); + return; + } execute('sessionStop', function (err) { if (!err) { + this.status = chrome.cast.SessionStatus.STOPPED; successCallback && successCallback(); } else { + if (err === chrome.cast.ErrorCode.INVALID_PARAMETER) { + errorCallback(new chrome.cast.Error(chrome.cast.Error.INVALID_PARAMETER, 'No active session', null)); + return; + } handleError(err, errorCallback); } }); @@ -691,11 +702,19 @@ chrome.cast.Session.prototype.leave = function (successCallback, errorCallback) errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); return; } - + if (this.status !== chrome.cast.SessionStatus.CONNECTED) { + errorCallback(new chrome.cast.Error(chrome.cast.Error.INVALID_PARAMETER, 'No active session', null)); + return; + } execute('sessionLeave', function (err) { if (!err) { + this.status = chrome.cast.SessionStatus.DISCONNECTED; successCallback && successCallback(); } else { + if (err === chrome.cast.ErrorCode.INVALID_PARAMETER) { + errorCallback(new chrome.cast.Error(chrome.cast.Error.INVALID_PARAMETER, 'No active session', null)); + return; + } handleError(err, errorCallback); } }); @@ -784,10 +803,15 @@ chrome.cast.Session.prototype.loadMedia = function (loadRequest, successCallback }; /** - * Adds a listener that is invoked when the status of the Session has changed. - * Changes to the following properties will trigger the listener: statusText, namespaces, customData, and the volume of the receiver. - * The callback will be invoked with 'true' (isAlive) parameter. - * When this session is ended, the callback will be invoked with 'false' (isAlive); + * Adds a listener that is invoked when the Session has changed. + * Changes to the following properties will trigger the listener: + * statusText, namespaces, status, and the volume of the receiver. + * + * Listeners should check the status property of the Session to + * determine its connection status. The boolean parameter isAlive is + * deprecated in favor of the status Session property. The isAlive + * parameter is still passed in for backwards compatibility, and is + * true unless status = chrome.cast.SessionStatus.STOPPED. * @param {function} listener The listener to add. */ chrome.cast.Session.prototype.addUpdateListener = function (listener) { @@ -800,10 +824,6 @@ chrome.cast.Session.prototype.addUpdateListener = function (listener) { */ chrome.cast.Session.prototype.removeUpdateListener = function (listener) { this.removeListener('_sessionUpdated', listener); - // var index = array.indexOf(5); - // if (index > -1) { - // array.splice(index, 1); - // } }; /** @@ -842,26 +862,6 @@ chrome.cast.Session.prototype.removeMediaListener = function (listener) { this.removeListener('_mediaListener', listener); }; -chrome.cast.Session.prototype._update = function (isAlive, obj) { - this.appId = obj.appId; - this.appImages = obj.appImages; - this.displayName = obj.displayName; - - if (obj.receiver) { - if (!this.receiver) { - this.receiver = new chrome.cast.Receiver(null, null, null, null); - } - this.receiver.friendlyName = obj.receiver.friendlyName; - this.receiver.label = obj.receiver.label; - - if (obj.receiver.volume) { - this.receiver.volume = new chrome.cast.Volume(obj.receiver.volume.level, obj.receiver.volume.muted); - } - } - - this.emit('_sessionUpdated', isAlive); -}; - /** * Represents a media item that has been loaded into the receiver application. * @param {string} sessionId Identifies the session that is hosting the media. @@ -876,7 +876,7 @@ chrome.cast.Session.prototype._update = function (isAlive, obj) { * @property {chrome.cast.Volume} volume The media stream volume. * @property {string} idleReason Reason for idling */ -chrome.cast.media.Media = function (sessionId, mediaSessionId) { +chrome.cast.media.Media = function Media (sessionId, mediaSessionId) { EventEmitter.call(this); this.sessionId = sessionId; this.mediaSessionId = mediaSessionId; @@ -1189,6 +1189,14 @@ chrome.cast.removeConnectingListener = function (cb) { } }; +/** ************* Cordova Events ********************/ +// TODO +/** + * These are events that are triggered from the plugin using "sendJavascript" + * It is recommended by cordova that we avoid sendJavascript usage + * so we should try to remove all of these functions eventually. + */ + chrome.cast._emitConnecting = function () { for (var n = 0; n < _connectingListeners.length; n++) { _connectingListeners[n](); @@ -1202,9 +1210,27 @@ chrome.cast._ = { receiverAvailable: function () { _receiverListener(chrome.cast.ReceiverAvailability.AVAILABLE); }, - sessionUpdated: function (isAlive, session) { - if (session && session.sessionId && _session) { - _session._update(isAlive, session); + /** + * Function called from cordova when the Session has changed. + * Changes to the following properties will trigger the listener: + * statusText, namespaces, status, and the volume of the receiver. + * + * Listeners should check the status property of the Session to + * determine its connection status. The boolean parameter isAlive is + * deprecated in favor of the status Session property. The isAlive + * parameter is still passed in for backwards compatibility, and is + * true unless status = chrome.cast.SessionStatus.STOPPED. + * @param {function} listener The listener to add. + */ + sessionUpdated: function (status) { + var isAlive = (status !== chrome.cast.SessionStatus.STOPPED); + if (_session) { + _session.status = status; + // Call all the sessionUpdate listeners + _session.emit('_sessionUpdated', isAlive); + } + if (!isAlive) { + updateSession(null); } }, mediaUpdated: function (isAlive, media) { @@ -1260,29 +1286,54 @@ chrome.cast._ = { module.exports = chrome.cast; +/** + * Updates the current session with the incoming javaSession + */ function updateSession (javaSession) { + // Should we reset the sesion? + if (!javaSession) { + _session = undefined; + return; + } _session = new chrome.cast.Session( javaSession.sessionId, javaSession.appId, javaSession.displayName, javaSession.appImages || [], - new chrome.cast.Receiver( - javaSession.receiver.label, - javaSession.receiver.friendlyName, - javaSession.receiver.capabilities || [], - javaSession.receiver.volume || null - ) + createReceiver(javaSession.receiver) ); - if (javaSession.media && javaSession.media.sessionId) { - _currentMedia = new chrome.cast.media.Media(javaSession.sessionId, javaSession.media.mediaSessionId); - _currentMedia.currentTime = javaSession.media.currentTime; - _currentMedia.playerState = javaSession.media.playerState; - _currentMedia.media = javaSession.media.media; - _session.media[0] = _currentMedia; - } + _session.status = chrome.cast.SessionStatus.CONNECTED; + _session.media[0] = createMedia(javaSession.media, javaSession.sessionId); + return _session; } +function createMedia (media, sessionId) { + if (media && media.sessionId) { + _currentMedia = new chrome.cast.media.Media(sessionId, media.mediaSessionId); + _currentMedia.currentTime = media.currentTime; + _currentMedia.playerState = media.playerState; + _currentMedia.media = media.media; + } + return _currentMedia; +} + +function createReceiver (receiver) { + if (!receiver) { + return new chrome.cast.Receiver(null, null, null, null); + } + var outReceiver = new chrome.cast.Receiver( + receiver.label, + receiver.friendlyName, + receiver.capabilities || [], + null + ); + if (receiver.volume) { + outReceiver.volume = new chrome.cast.Volume(receiver.volume.level, receiver.volume.muted); + } + return outReceiver; +} + function execute (action) { var args = [].slice.call(arguments); args.shift(); @@ -1294,32 +1345,26 @@ function execute (action) { } function handleError (err, callback) { - var errorCode = chrome.cast.ErrorCode.UNKNOWN; var errorDescription = err; - var errorData = {}; - err = err || ''; - if (err.toUpperCase() === 'TIMEOUT') { - errorCode = chrome.cast.ErrorCode.TIMEOUT; + err = err.toLowerCase() || ''; + if (err === chrome.cast.ErrorCode.TIMEOUT) { errorDescription = 'The operation timed out.'; - } else if (err.toUpperCase() === 'INVALID_PARAMETER') { - errorCode = chrome.cast.ErrorCode.INVALID_PARAMETER; + } else if (err === chrome.cast.ErrorCode.INVALID_PARAMETER) { errorDescription = 'The parameters to the operation were not valid.'; - } else if (err.toUpperCase() === 'RECEIVER_UNAVAILABLE') { - errorCode = chrome.cast.ErrorCode.RECEIVER_UNAVAILABLE; + } else if (err === chrome.cast.ErrorCode.RECEIVER_UNAVAILABLE) { errorDescription = 'No receiver was compatible with the session request.'; - } else if (err.toUpperCase() === 'CANCEL') { - errorCode = chrome.cast.ErrorCode.CANCEL; + } else if (err === chrome.cast.ErrorCode.CANCEL) { errorDescription = 'The operation was canceled by the user.'; - } else if (err.toUpperCase() === 'CHANNEL_ERROR') { - errorCode = chrome.cast.ErrorCode.CHANNEL_ERROR; + } else if (err === chrome.cast.ErrorCode.CHANNEL_ERROR) { errorDescription = 'A channel to the receiver is not available.'; - } else if (err.toUpperCase() === 'SESSION_ERROR') { - errorCode = chrome.cast.ErrorCode.SESSION_ERROR; + } else if (err === chrome.cast.ErrorCode.SESSION_ERROR) { errorDescription = 'A session could not be created, or a session was invalid.'; + } else { + err = chrome.cast.ErrorCode.UNKNOWN; } - var error = new chrome.cast.Error(errorCode, errorDescription, errorData); + var error = new chrome.cast.Error(err, errorDescription, {}); if (callback) { callback(error); } From 3d4792b8ce9aa374a04e259bdabab0c2b0f18c7f Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Tue, 10 Sep 2019 05:04:15 -0600 Subject: [PATCH 031/166] Added package.json script to automatically fix the js errors it can. --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 6e1e6f0..c7448cb 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,9 @@ "name": "cordova-plugin-chromecast", "version": "1.0.0", "scripts": { - "style": "node node_modules/eslint/bin/eslint src && node node_modules/eslint/bin/eslint www && node node_modules/eslint/bin/eslint tests && node ./node_modules/java-checkstyle/bin/index.js ./src/android/ -c ./check_style.xml" + "style-fix-js": "node node_modules/eslint/bin/eslint --fix src && node node_modules/eslint/bin/eslint --fix www && node node_modules/eslint/bin/eslint --fix tests", + "style-check": "node node_modules/eslint/bin/eslint src && node node_modules/eslint/bin/eslint www && node node_modules/eslint/bin/eslint tests && node ./node_modules/java-checkstyle/bin/index.js ./src/android/ -c ./check_style.xml", + "style": "npm run style-fix-js && npm run style-check" }, "author": "", "license": "dual GPLv3/MPLv2", From 101a69f2e4a357a6466f0c44c49fe42cb377e459 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 11 Sep 2019 02:40:47 -0600 Subject: [PATCH 032/166] Got the plugin to automatically resume sessions after being the app has been force quit and restarted. This was rough. Added defaults to _sessionListener and _receiverListener. Issue #36 --- plugin.xml | 1 + src/android/CastOptionsProvider.java | 12 ++ src/android/Chromecast.java | 66 ++------ src/android/ChromecastConnection.java | 212 ++++++++++++++++++++------ tests/tests.js | 1 + www/chrome.cast.js | 19 ++- 6 files changed, 200 insertions(+), 111 deletions(-) diff --git a/plugin.xml b/plugin.xml index 9c32537..39b27c2 100644 --- a/plugin.xml +++ b/plugin.xml @@ -19,6 +19,7 @@ + diff --git a/src/android/CastOptionsProvider.java b/src/android/CastOptionsProvider.java index 535dad4..7d73110 100644 --- a/src/android/CastOptionsProvider.java +++ b/src/android/CastOptionsProvider.java @@ -10,9 +10,21 @@ public final class CastOptionsProvider implements OptionsProvider { + /** The app id. */ + private static String appId; + + /** + * Sets the app ID. + * @param applicationId appId + */ + public static void setAppId(String applicationId) { + appId = applicationId; + } + @Override public CastOptions getCastOptions(Context context) { return new CastOptions.Builder() + .setReceiverApplicationId(appId) .build(); } @Override diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 93f547e..450e90f 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -16,9 +16,6 @@ import org.json.JSONException; import org.json.JSONObject; -import android.os.Handler; -import android.util.Log; - import androidx.mediarouter.media.MediaRouter.RouteInfo; import com.google.android.gms.cast.framework.CastSession; @@ -125,62 +122,25 @@ public boolean setup(CallbackContext callbackContext) { * @return true for cordova */ public boolean initialize(final String appId, String autoJoinPolicy, String defaultActionPolicy, final CallbackContext callbackContext) { - - this.connection.initialize(appId, new ChromecastConnection.Callback() { + connection.setReceiverListener(new ChromecastConnection.ReceiverListener() { @Override - public void run() { - callbackContext.success(); + public void onReceiverAvailable() { + sendReceiverUpdate(true); + } - // Send receiver unavailable update while the new routeSelector is built. - // This matches the Chrome Desktop SDK behavior. + @Override + public void onReceiverUnavailable() { sendReceiverUpdate(false); - - - // See if there are any available routes - ChromecastConnection.ScanCallback scanCallback = new ChromecastConnection.ScanCallback() { - @Override - public void onRouteUpdate(List routes) { - if (routes.size() == 0) { - // If no routes, just return, wait for a real route to be discovered - return; - } - - // We found at least 1 route! so stop the scan - connection.stopScan(this); - - // and send out receiver available - sendReceiverUpdate(true); - - // Attempt to rejoin existing session if exists - connection.rejoin(new ChromecastConnection.JoinCallback() { - @Override - public void onJoin(CastSession session) { - // If we were able to join that means the client likely navigated to - // a new page and the code has called initialize again - // so, send out the session - sendJavascript("chrome.cast._.sessionListener(" + media.createSessionObject().toString() + ");"); - } - - @Override - public void onError(String errorCode) { - Log.d(TAG, "Error rejoining session: " + errorCode); - } - }); - } - }; - connection.scanForRoutes(scanCallback); - - // Also start a time out to cancel the scan - // after 5 seconds to save power - new Handler().postDelayed(new Runnable() { - @Override - public void run() { - connection.stopScan(scanCallback); - } - }, 5000); } }); + connection.initialize(appId, callbackContext, new ChromecastConnection.Callback() { + @Override + public void run() { + // We found a session! + sendJavascript("chrome.cast._.sessionListener(" + media.createSessionObject().toString() + ");"); + } + }); return true; } diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index 6bdc2fc..94a1b73 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -4,7 +4,9 @@ import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; +import android.os.Handler; import androidx.mediarouter.app.MediaRouteChooserDialog; import androidx.mediarouter.media.MediaRouteSelector; @@ -15,6 +17,8 @@ import com.google.android.gms.cast.CastMediaControlIntent; import com.google.android.gms.cast.framework.CastContext; import com.google.android.gms.cast.framework.CastSession; +import com.google.android.gms.cast.framework.CastState; +import com.google.android.gms.cast.framework.CastStateListener; import com.google.android.gms.cast.framework.SessionManager; import com.google.android.gms.cast.framework.SessionManagerListener; @@ -27,12 +31,16 @@ public class ChromecastConnection { /** Lifetime variable. */ private Activity activity; + /** settings object. */ + private SharedPreferences settings; /** Lifetime variable. */ private ChromecastSession media; /** Lifetime variable. */ private SessionListener newConnectionListener; /** Should we pass disconnects to the client's externalDisconnectListener. */ - private boolean enableClientExtenalDisconnectListener = false; + private boolean enableClientExtenalDisconnectListener = true; + /** The ReceiverListener callback. */ + private ReceiverListener receiverListener; /** Initialize lifetime variable. */ private String appId; @@ -48,58 +56,142 @@ public class ChromecastConnection { */ public ChromecastConnection(Activity act, ChromecastSession chromecastSession, Callback listener) { this.activity = act; + this.settings = activity.getSharedPreferences("CORDOVA-PLUGIN-CHROMECAST_ChromecastConnection", 0); this.media = chromecastSession; + this.appId = settings.getString("appId", CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID); + // Set the initial appId + CastOptionsProvider.setAppId(appId); + // Set the receiverLister to an empty default (saves us some null check later) + this.setReceiverListener(null); + + // Add the permanent session end/disconnect, and resume listener, and receiver update listener + getSessionManager().addSessionManagerListener(new SessionListener() { + @Override + public void onSessionEnded(CastSession castSession, int error) { + setSession(null); + if (listener != null && enableClientExtenalDisconnectListener) { + listener.run(); + } + } + @Override + public void onSessionResumed(CastSession castSession, boolean wasSuspended) { + // This catches any sessions we are able to rejoin + setSession(castSession); + } + }, CastSession.class); + + // This is the first call to getContext which will start up the + // CastContext and prep it for searching for a session to rejoin + getContext().addCastStateListener(new CastStateListener() { + @Override + public void onCastStateChanged(int i) { + receiverListener.sendUpdate(i); + } + }); + } - // Add the session end/disconnect listener - act.runOnUiThread(new Runnable() { + /** + * Must be called each time the appId changes and at least once before any other method is called. + * @param applicationId the app id to use + * @param callback called when initialization is complete + * @param onSessionFound called when (if) an active session is found + */ + public void initialize(String applicationId, CallbackContext callback, Callback onSessionFound) { + activity.runOnUiThread(new Runnable() { public void run() { - getSessionManager().addSessionManagerListener(new SessionListener() { + + // If the app Id changed, get it again + if (!applicationId.equals(appId)) { + setAppId(applicationId); + } + + // Tell the client that initialization was a success + callback.success(); + + lookForAvailableReceiver(new Callback() { @Override - public void onSessionEnded(CastSession castSession, int error) { - setSession(null); - if (listener != null && enableClientExtenalDisconnectListener) { - listener.run(); + public void run() { + // Update the session + setSession(getSessionManager().getCurrentCastSession()); + if (session != null) { + // Let the client know we have found a session + onSessionFound.run(); } } - }, CastSession.class); + }); } }); } /** - * Must be called each time the appId changes and at least once before any other method is called. - * @param applicationId the app id to use - * @param callback called when initialization is complete + * Must be called from the main thread. + * @param foundReceiver called if a receiver is found */ - public void initialize(String applicationId, Callback callback) { - // If the appId changed - if (!applicationId.equals(this.appId)) { - // Else we need to save the new appId - this.setAppId(applicationId); - // And reset the session - this.setSession(null); + private void lookForAvailableReceiver(Callback foundReceiver) { + // check current state + if (ReceiverListener.isReceiverAvailable(getContext().getCastState())) { + // If we already have a receiver, notify and return + receiverListener.sendUpdate(getContext().getCastState()); + foundReceiver.run(); + return; } - activity.runOnUiThread(new Runnable() { + // Create callbacks + MediaRouter.Callback mediaRouterCallback = new MediaRouter.Callback() { }; + CastStateListener castStateListener = new CastStateListener() { + @Override + public void onCastStateChanged(int state) { + if (ReceiverListener.isReceiverAvailable(state)) { + // Remove callbacks + getContext().removeCastStateListener(this); + getMediaRouter().removeCallback(mediaRouterCallback); + // And let the client know we found a receiver + foundReceiver.run(); + } + } + }; + + // Listen for any available receiver + getContext().addCastStateListener(castStateListener); + + + // Start an active scan for available routes + getMediaRouter().addCallback(new MediaRouteSelector.Builder() + .addControlCategory(CastMediaControlIntent.categoryForCast(appId)) + .build(), + mediaRouterCallback, + MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); + + // If we didn't find any routes by 5 seconds remove the callbacks + new Handler().postDelayed(new Runnable() { + @Override public void run() { - // Set the new appId - CastContext.getSharedInstance(activity).setReceiverApplicationId(appId); - // Tell the client we are done - callback.run(); + // And stop the scan for routes + getMediaRouter().removeCallback(mediaRouterCallback); + // And remove the castStateListener callback + getContext().removeCastStateListener(castStateListener); } - }); + }, 5000); } private MediaRouter getMediaRouter() { return MediaRouter.getInstance(activity); } + private CastContext getContext() { + return CastContext.getSharedInstance(activity); + } + private SessionManager getSessionManager() { - return CastContext.getSharedInstance(activity).getSessionManager(); + return getContext().getSessionManager(); } private void setAppId(String applicationId) { this.appId = applicationId; + this.settings.edit().putString("appId", appId).apply(); + getContext().setReceiverApplicationId(appId); + // Invalidate any old session + this.setSession(null); } private void setSession(CastSession castSession) { @@ -107,19 +199,6 @@ private void setSession(CastSession castSession) { this.media.setSession(this.session); } - /** - * Attempts to join the last route we were connected to. - * @param callback calls callback.onJoin if we have joined a session - */ - public void rejoin(JoinCallback callback) { - if (session != null) { - callback.onJoin(session); - } - // TODO is it even possible to rejoin a session automatically? - // https://stackoverflow.com/questions/57801427/how-to-rejoin-cast-session-after-app-restart - // https://stackoverflow.com/questions/57832467/how-to-check-if-mediarouter-routeinfo-route-is-already-in-use - } - /** * This will create a new session or seamlessly join an existing one if we created it. * @param routeId the id of the route to join @@ -239,7 +318,6 @@ public void onSessionStartFailed(CastSession castSession, int errCode) { getSessionManager().addSessionManagerListener(newConnectionListener, CastSession.class); } - /** * Starts listening for receiver updates. * Must call stopScan(callback) or the battery will drain with non-stop active scanning. @@ -281,28 +359,24 @@ public void run() { * Exits the current session. * @param stopCasting should the receiver application be stopped as well? * @param callback called with .success or .error depending on the initial result - * @param disconnected called when the session actually ends + * @param disconnected overrides the default disconnect listener if set + * only called once we actually disconnect */ public void endSession(boolean stopCasting, CallbackContext callback, Callback disconnected) { activity.runOnUiThread(new Runnable() { public void run() { - setSession(null); - if (getSessionManager().getCurrentCastSession() == null) { - if (callback != null) { - callback.error("invalid_parameter"); - } - return; + if (disconnected != null) { + // Disable the externalDisconnectListener temporarily + enableClientExtenalDisconnectListener = false; } - // Disable the externalDisconnectListener temporarily - enableClientExtenalDisconnectListener = false; - getSessionManager().addSessionManagerListener(new SessionListener() { @Override public void onSessionEnded(CastSession castSession, int error) { getSessionManager().removeSessionManagerListener(this, CastSession.class); // Re-enable the externalDisconnectListener enableClientExtenalDisconnectListener = true; + setSession(null); if (disconnected != null) { disconnected.run(); } @@ -317,6 +391,26 @@ public void onSessionEnded(CastSession castSession, int error) { }); } + /** + * Sets the permanent ReceiverListener. + * @param listener called when there are receiver updates + */ + public void setReceiverListener(ReceiverListener listener) { + if (listener != null) { + this.receiverListener = listener; + } else { + // Make sure the receiverLister always has something + this.receiverListener = new ReceiverListener() { + @Override + void onReceiverAvailable() { + } + @Override + void onReceiverUnavailable() { + } + }; + } + } + private class SessionListener implements SessionManagerListener { @Override @@ -452,4 +546,22 @@ public final void onRouteRemoved(MediaRouter router, RouteInfo route) { } } + abstract static class ReceiverListener { + private static boolean isReceiverAvailable(int state) { + return state != CastState.NO_DEVICES_AVAILABLE; + } + /** + * Sends the appropriate update. + * @param state CastState + */ + private void sendUpdate(int state) { + if (isReceiverAvailable(state)) { + this.onReceiverAvailable(); + } else { + this.onReceiverUnavailable(); + } + } + abstract void onReceiverAvailable(); + abstract void onReceiverUnavailable(); + } } diff --git a/tests/tests.js b/tests/tests.js index 4dab002..da72714 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -396,6 +396,7 @@ exports.defineAutoTests = function () { } hitUpdateListener = true; test(isAlive).toEqual(false); + test(session.status).toEqual(chrome.cast.SessionStatus.STOPPED); if (hitErrHandler) { resolve(session); } diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 1735c58..0cdd1ea 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -527,8 +527,8 @@ chrome.cast = { var _sessionRequest = null; var _autoJoinPolicy = null; var _defaultActionPolicy = null; -var _receiverListener = null; -var _sessionListener = null; +var _sessionListener = function () {}; +var _receiverListener = function () {}; var _session; var _currentMedia = null; @@ -549,13 +549,13 @@ chrome.cast.initialize = function (apiConfig, successCallback, errorCallback) { _autoJoinPolicy = apiConfig.autoJoinPolicy; _defaultActionPolicy = apiConfig.defaultActionPolicy; _sessionRequest = apiConfig.sessionRequest; + _sessionListener = apiConfig.sessionListener; + _receiverListener = apiConfig.receiverListener; execute('initialize', _sessionRequest.appId, _autoJoinPolicy, _defaultActionPolicy, function (err) { if (!err) { successCallback(); - // Only set the listeners once initialize has completed successfully - _sessionListener = apiConfig.sessionListener; - _receiverListener = apiConfig.receiverListener; + chrome.cast._.receiverUnavailable(); } else { handleError(err, errorCallback); } @@ -1244,7 +1244,7 @@ chrome.cast._ = { _currentMedia.media = media.media; _session.media[0] = _currentMedia; - _sessionListener && _sessionListener(_session); + _sessionListener(_session); } } }, @@ -1262,7 +1262,7 @@ chrome.cast._ = { }, sessionListener: function (javaSession) { var session = updateSession(javaSession); - _sessionListener && _sessionListener(session); + _sessionListener(session); }, sessionJoined: function (obj) { var session = updateSession(obj); @@ -1275,7 +1275,7 @@ chrome.cast._ = { session.media[0] = _currentMedia; } - _sessionListener && _sessionListener(session); + _sessionListener(session); }, onMessage: function (sessionId, namespace, message) { if (_session) { @@ -1293,6 +1293,8 @@ function updateSession (javaSession) { // Should we reset the sesion? if (!javaSession) { _session = undefined; + _sessionListener = function () {}; + _receiverListener = function () {}; return; } _session = new chrome.cast.Session( @@ -1361,6 +1363,7 @@ function handleError (err, callback) { } else if (err === chrome.cast.ErrorCode.SESSION_ERROR) { errorDescription = 'A session could not be created, or a session was invalid.'; } else { + errorDescription = err; err = chrome.cast.ErrorCode.UNKNOWN; } From b31da28e190a4bd9f7d058ad64ebaebd519a3485 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 11 Sep 2019 03:01:00 -0600 Subject: [PATCH 033/166] Should technically mark the package version as a "dev" version until completely stable. --- package.json | 2 +- tests/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c7448cb..3a48d34 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-chromecast", - "version": "1.0.0", + "version": "1.0.0-dev", "scripts": { "style-fix-js": "node node_modules/eslint/bin/eslint --fix src && node node_modules/eslint/bin/eslint --fix www && node node_modules/eslint/bin/eslint --fix tests", "style-check": "node node_modules/eslint/bin/eslint src && node node_modules/eslint/bin/eslint www && node node_modules/eslint/bin/eslint tests && node ./node_modules/java-checkstyle/bin/index.js ./src/android/ -c ./check_style.xml", diff --git a/tests/package.json b/tests/package.json index 55ffe2c..ecaf6d1 100644 --- a/tests/package.json +++ b/tests/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-chromecast-tests", - "version": "1.0.0", + "version": "1.0.0-dev", "description": "", "author": "", "license": "Apache 2.0", From 39dad442e70100b25b719ede097ee1e2660120fd Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 11 Sep 2019 03:04:51 -0600 Subject: [PATCH 034/166] (android) Make the constants match the desktop chrome SDK. Issue #36 --- src/android/ChromecastUtilities.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/android/ChromecastUtilities.java b/src/android/ChromecastUtilities.java index d09d619..74991fe 100644 --- a/src/android/ChromecastUtilities.java +++ b/src/android/ChromecastUtilities.java @@ -19,15 +19,15 @@ private ChromecastUtilities() { static String getMediaIdleReason(MediaStatus mediaStatus) { switch (mediaStatus.getIdleReason()) { case MediaStatus.IDLE_REASON_CANCELED: - return "canceled"; + return "CANCELED"; case MediaStatus.IDLE_REASON_ERROR: - return "error"; + return "ERROR"; case MediaStatus.IDLE_REASON_FINISHED: - return "finished"; + return "FINISHED"; case MediaStatus.IDLE_REASON_INTERRUPTED: - return "interrupted"; + return "INTERRUPTED"; case MediaStatus.IDLE_REASON_NONE: - return "none"; + return "NONE"; default: return null; } @@ -53,11 +53,11 @@ static String getMediaPlayerState(MediaStatus mediaStatus) { static String getMediaInfoStreamType(MediaInfo mediaInfo) { switch (mediaInfo.getStreamType()) { case MediaInfo.STREAM_TYPE_BUFFERED: - return "buffered"; + return "BUFFERED"; case MediaInfo.STREAM_TYPE_LIVE: - return "live"; + return "LIVE"; case MediaInfo.STREAM_TYPE_NONE: - return "other"; + return "OTHER"; default: return null; } From aaac51ee45512bc82c8caa7dbaf6bb0c41886b3b Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Thu, 12 Sep 2019 03:51:04 -0600 Subject: [PATCH 035/166] Fixed all kinds of problems with setVolume (media and receiver) - following the chrome SDK behavior for setVolume more closely - need to trigger sessionUpdate on receiver volume change - added method which accepts Integer for setMediaVolume (in case client puts exactly 0 or 1) Allowed null arguments to be received on the Android side - since all our android method entry points accept nullable objects this should be allowed - in particular, this allows us to have one setMediaVolume (instead of setMediaVolume and setMediaMuted) to accept null args which mean "don't update the level/mute state" Session handling fixes - fixed how session.status is added to session - fixed leave session (was passing true to endSession, oops) - changed "SESSION_ERROR" instances in android to "session_error" to match the SDK directly Cleaned up and improved tests - more tests checked for accurate status updates - made some tests more readable with callOrder Cleaning - removed ChromecastConnection.Callback since it was exactly the same as Runnable, just use that. Streamlined the session end event callback process - Chromecast.java has only one location which triggers the client's session update listener (with end status) - ChromecastSession.java detects external reasons for disconnect with onApplicationDisconnected (eg. timeout and someone else stealing the cast) - ChromecastConnection.java detects self-triggered reasons for disconnect (eg. sessionEnd, sessionLeave) (in which case it notifies ChromecastSession.java that a session has ended, which then tells Chromecast.java) Simplified receiverListener - just pass a boolean to _receiverUpdate Untested fix: - maybe fixed the message functions and updates (needs a test) Issue #36 (1.0.0 development) --- src/android/Chromecast.java | 142 ++++------- src/android/ChromecastConnection.java | 157 +++--------- src/android/ChromecastSession.java | 319 ++++++++++++++--------- tests/tests.js | 352 ++++++++++++++++++-------- www/chrome.cast.js | 125 ++++----- 5 files changed, 592 insertions(+), 503 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 450e90f..b76d911 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -18,8 +18,8 @@ import androidx.mediarouter.media.MediaRouter.RouteInfo; +import com.google.android.gms.cast.CastDevice; import com.google.android.gms.cast.framework.CastSession; -import com.google.android.gms.cast.framework.media.RemoteMediaClient; public final class Chromecast extends CordovaPlugin { @@ -36,11 +36,24 @@ public final class Chromecast extends CordovaPlugin { protected void pluginInitialize() { super.pluginInitialize(); - this.media = new ChromecastSession(cordova.getActivity(), remoteMediaClientCallback); - this.connection = new ChromecastConnection(cordova.getActivity(), this.media, new ChromecastConnection.Callback() { + this.media = new ChromecastSession(cordova.getActivity(), new ChromecastSession.Listener() { @Override - public void run() { - sendSessionUpdate("stopped"); + public void onSessionUpdate(JSONObject session) { + sendJavascript("chrome.cast._.sessionUpdated(" + session + ");"); + } + @Override + public void onMediaUpdate(JSONObject mediaObject) { + sendJavascript("chrome.cast._.mediaUpdated(true, " + mediaObject + ");"); + } + @Override + public void onMessageReceived(CastDevice castDevice, String namespace, String message) { + sendJavascript("chrome.cast._.onMessage('" + namespace + "', '" + message.replace("\\", "\\\\") + "')"); + } + }); + this.connection = new ChromecastConnection(cordova.getActivity(), this.media, new ChromecastConnection.Listener() { + @Override + void onReceiverAvailableUpdate(boolean available) { + sendJavascript("chrome.cast._.receiverUpdate(" + available + ")"); } }); } @@ -57,10 +70,12 @@ public boolean execute(String action, JSONArray args, CallbackContext cbContext) if (args.length() + 1 == types.length) { boolean isValid = true; for (int i = 0; i < args.length(); i++) { + // Handle null/undefined arguments + if (JSONObject.NULL.equals(args.get(i))) { + continue; + } Class arg = args.get(i).getClass(); - if (types[i] == arg) { - isValid = true; - } else { + if (types[i] != arg) { isValid = false; break; } @@ -77,6 +92,10 @@ public boolean execute(String action, JSONArray args, CallbackContext cbContext) Object[] variableArgs = new Object[types.length]; for (int i = 0; i < args.length(); i++) { variableArgs[i] = args.get(i); + // Translate null JSONObject to null + if (JSONObject.NULL.equals(variableArgs[i])) { + variableArgs[i] = null; + } } variableArgs[variableArgs.length - 1] = cbContext; Class r = methodToExecute.getReturnType(); @@ -122,19 +141,7 @@ public boolean setup(CallbackContext callbackContext) { * @return true for cordova */ public boolean initialize(final String appId, String autoJoinPolicy, String defaultActionPolicy, final CallbackContext callbackContext) { - connection.setReceiverListener(new ChromecastConnection.ReceiverListener() { - @Override - public void onReceiverAvailable() { - sendReceiverUpdate(true); - } - - @Override - public void onReceiverUnavailable() { - sendReceiverUpdate(false); - } - }); - - connection.initialize(appId, callbackContext, new ChromecastConnection.Callback() { + connection.initialize(appId, callbackContext, new Runnable() { @Override public void run() { // We found a session! @@ -163,7 +170,7 @@ public void onError(String errorCode) { callbackContext.error("cancel"); } else { // TODO maybe handle some of the error codes better - callbackContext.error("SESSION_ERROR"); + callbackContext.error("session_error"); } } }); @@ -202,7 +209,13 @@ public boolean setReceiverVolumeLevel(Integer newLevel, CallbackContext callback return this.setReceiverVolumeLevel(newLevel.doubleValue(), callbackContext); } - private boolean setReceiverVolumeLevel(Double newLevel, CallbackContext callbackContext) { + /** + * Set the volume level on the receiver - this is a Chromecast volume, not a Media volume. + * @param newLevel the level to set the volume to + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean setReceiverVolumeLevel(Double newLevel, CallbackContext callbackContext) { this.media.setVolume(newLevel, callbackContext); return true; } @@ -238,7 +251,6 @@ public boolean sendMessage(String namespace, String message, final CallbackConte */ public boolean addMessageListener(String namespace, CallbackContext callbackContext) { this.media.addMessageListener(namespace); -// sendJavascript("chrome.cast._.onMessage('" + session.getSessionId() + "', '" + namespace + "', '" + message.replace("\\", "\\\\") + "')"); callbackContext.success(); return true; } @@ -263,8 +275,8 @@ public boolean loadMedia(String contentId, JSONObject customData, String content private boolean loadMedia(String contentId, JSONObject customData, String contentType, Integer duration, String streamType, Boolean autoPlay, Double currentTime, JSONObject metadata, JSONObject textTrackStyle, final CallbackContext callbackContext) { this.media.loadMedia(contentId, customData, contentType, duration, streamType, autoPlay, currentTime, metadata, textTrackStyle, callbackContext); +// sendJavascript("chrome.cast._.mediaLoaded(true, " + media.createMediaObject().toString() + ");"); return true; -// sendJavascript("chrome.cast._.mediaLoaded(true, " + media.toString() + ");"); } /** @@ -301,24 +313,25 @@ public boolean mediaSeek(Integer seekTime, String resumeState, CallbackContext c /** - * Set the volume on the media. + * Set the volume level and mute state on the media. * @param level the level to set the volume to + * @param muted if true set the media to muted, else, unmute * @param callbackContext called with .success or .error depending on the result * @return true for cordova */ - public boolean setMediaVolume(Double level, CallbackContext callbackContext) { - media.mediaSetVolume(level, callbackContext); - return true; + public boolean setMediaVolume(Integer level, Boolean muted, CallbackContext callbackContext) { + return this.setMediaVolume(level.doubleValue(), muted, callbackContext); } /** - * Set the muted on the media. + * Set the volume level and mute state on the media. + * @param level the level to set the volume to * @param muted if true set the media to muted, else, unmute * @param callbackContext called with .success or .error depending on the result * @return true for cordova */ - public boolean setMediaMuted(Boolean muted, CallbackContext callbackContext) { - media.mediaSetMuted(muted, callbackContext); + public boolean setMediaVolume(Double level, Boolean muted, CallbackContext callbackContext) { + media.mediaSetVolume(level, muted, callbackContext); return true; } @@ -360,12 +373,7 @@ public boolean mediaEditTracksInfo(JSONArray activeTrackIds, JSONObject textTrac * @return true for cordova */ public boolean sessionStop(CallbackContext callbackContext) { - connection.endSession(true, callbackContext, new ChromecastConnection.Callback() { - @Override - public void run() { - sendSessionUpdate("stopped"); - } - }); + connection.endSession(true, callbackContext); return true; } @@ -375,12 +383,7 @@ public void run() { * @return true for cordova */ public boolean sessionLeave(CallbackContext callbackContext) { - connection.endSession(true, callbackContext, new ChromecastConnection.Callback() { - @Override - public void run() { - sendSessionUpdate("disconnected"); - } - }); + connection.endSession(false, callbackContext); return true; } @@ -423,23 +426,6 @@ public boolean stopRouteScan(CallbackContext callbackContext) { return true; } - private void sendSessionUpdate(String status) { - sendJavascript("chrome.cast._.sessionUpdated('" + status + "');"); - } - - /** - * sends the receiverState. - * @param receiverState true if we should send receiverAvailable, - * false if we should send receiverUnavailable - */ - private void sendReceiverUpdate(boolean receiverState) { - if (receiverState) { - this.sendJavascript("chrome.cast._.receiverAvailable()"); - } else { - this.sendJavascript("chrome.cast._.receiverUnavailable()"); - } - } - /** * Simple helper to convert a route to JSON for passing down to the javascript side. * @param routes the routes to convert @@ -459,40 +445,6 @@ private JSONArray routesToJSON(List routes) { return routesArray; } - /** Handles remoteMediaClient callbacks. */ - private RemoteMediaClient.Callback remoteMediaClientCallback = new RemoteMediaClient.Callback() { - @Override - public void onStatusUpdated() { - super.onStatusUpdated(); - } - - @Override - public void onMetadataUpdated() { - super.onMetadataUpdated(); -// sendJavascript("chrome.cast._.mediaUpdated(true, " + media.createMediaInfo() + ");"); - } - - @Override - public void onQueueStatusUpdated() { - super.onQueueStatusUpdated(); - } - - @Override - public void onPreloadStatusUpdated() { - super.onPreloadStatusUpdated(); - } - - @Override - public void onSendingRemoteMediaRequest() { - super.onSendingRemoteMediaRequest(); - } - - @Override - public void onAdBreakStatusUpdated() { - super.onAdBreakStatusUpdated(); - } - }; - //Change all @deprecated this.webView.sendJavascript(String) to this local function sendJavascript(String) @TargetApi(Build.VERSION_CODES.KITKAT) private void sendJavascript(final String javascript) { diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index 94a1b73..7b3fe0a 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -37,10 +37,8 @@ public class ChromecastConnection { private ChromecastSession media; /** Lifetime variable. */ private SessionListener newConnectionListener; - /** Should we pass disconnects to the client's externalDisconnectListener. */ - private boolean enableClientExtenalDisconnectListener = true; - /** The ReceiverListener callback. */ - private ReceiverListener receiverListener; + /** The Listener callback. */ + private Listener listener; /** Initialize lifetime variable. */ private String appId; @@ -52,27 +50,19 @@ public class ChromecastConnection { * Constructor. * @param act the current context * @param chromecastSession the chromecastSession object that we should load with a new sessions - * @param listener the listener that listens for session end event + * @param connectionListener client callbacks for specific events */ - public ChromecastConnection(Activity act, ChromecastSession chromecastSession, Callback listener) { + public ChromecastConnection(Activity act, ChromecastSession chromecastSession, Listener connectionListener) { this.activity = act; this.settings = activity.getSharedPreferences("CORDOVA-PLUGIN-CHROMECAST_ChromecastConnection", 0); this.media = chromecastSession; this.appId = settings.getString("appId", CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID); + this.listener = connectionListener; // Set the initial appId CastOptionsProvider.setAppId(appId); - // Set the receiverLister to an empty default (saves us some null check later) - this.setReceiverListener(null); // Add the permanent session end/disconnect, and resume listener, and receiver update listener getSessionManager().addSessionManagerListener(new SessionListener() { - @Override - public void onSessionEnded(CastSession castSession, int error) { - setSession(null); - if (listener != null && enableClientExtenalDisconnectListener) { - listener.run(); - } - } @Override public void onSessionResumed(CastSession castSession, boolean wasSuspended) { // This catches any sessions we are able to rejoin @@ -82,12 +72,8 @@ public void onSessionResumed(CastSession castSession, boolean wasSuspended) { // This is the first call to getContext which will start up the // CastContext and prep it for searching for a session to rejoin - getContext().addCastStateListener(new CastStateListener() { - @Override - public void onCastStateChanged(int i) { - receiverListener.sendUpdate(i); - } - }); + // Also adds the receiver update callback + getContext().addCastStateListener(listener); } /** @@ -96,7 +82,7 @@ public void onCastStateChanged(int i) { * @param callback called when initialization is complete * @param onSessionFound called when (if) an active session is found */ - public void initialize(String applicationId, CallbackContext callback, Callback onSessionFound) { + public void initialize(String applicationId, CallbackContext callback, Runnable onSessionFound) { activity.runOnUiThread(new Runnable() { public void run() { @@ -108,7 +94,7 @@ public void run() { // Tell the client that initialization was a success callback.success(); - lookForAvailableReceiver(new Callback() { + lookForAvailableReceiver(new Runnable() { @Override public void run() { // Update the session @@ -127,11 +113,11 @@ public void run() { * Must be called from the main thread. * @param foundReceiver called if a receiver is found */ - private void lookForAvailableReceiver(Callback foundReceiver) { - // check current state - if (ReceiverListener.isReceiverAvailable(getContext().getCastState())) { - // If we already have a receiver, notify and return - receiverListener.sendUpdate(getContext().getCastState()); + private void lookForAvailableReceiver(Runnable foundReceiver) { + // check current state, if we already have a receiver + if (getContext().getCastState() != CastState.NO_DEVICES_AVAILABLE) { + // Notify and return + listener.onReceiverAvailableUpdate(true); foundReceiver.run(); return; } @@ -141,7 +127,8 @@ private void lookForAvailableReceiver(Callback foundReceiver) { CastStateListener castStateListener = new CastStateListener() { @Override public void onCastStateChanged(int state) { - if (ReceiverListener.isReceiverAvailable(state)) { + // If there is a receiver + if (getContext().getCastState() != CastState.NO_DEVICES_AVAILABLE) { // Remove callbacks getContext().removeCastStateListener(this); getMediaRouter().removeCallback(mediaRouterCallback); @@ -285,7 +272,7 @@ public void onDismiss(DialogInterface dialog) { builder.setPositiveButton("Stop Casting", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - endSession(true, null, null); + endSession(true, null); } }); builder.show(); @@ -359,27 +346,16 @@ public void run() { * Exits the current session. * @param stopCasting should the receiver application be stopped as well? * @param callback called with .success or .error depending on the initial result - * @param disconnected overrides the default disconnect listener if set - * only called once we actually disconnect */ - public void endSession(boolean stopCasting, CallbackContext callback, Callback disconnected) { + public void endSession(boolean stopCasting, CallbackContext callback) { activity.runOnUiThread(new Runnable() { public void run() { - if (disconnected != null) { - // Disable the externalDisconnectListener temporarily - enableClientExtenalDisconnectListener = false; - } - getSessionManager().addSessionManagerListener(new SessionListener() { @Override public void onSessionEnded(CastSession castSession, int error) { getSessionManager().removeSessionManagerListener(this, CastSession.class); - // Re-enable the externalDisconnectListener - enableClientExtenalDisconnectListener = true; setSession(null); - if (disconnected != null) { - disconnected.run(); - } + media.onSessionEnd(stopCasting ? "stopped" : "disconnected"); } }, CastSession.class); @@ -392,77 +368,28 @@ public void onSessionEnded(CastSession castSession, int error) { } /** - * Sets the permanent ReceiverListener. - * @param listener called when there are receiver updates + * Create this empty class so that we don't have to override every function + * each time we need a SessionManagerListener. */ - public void setReceiverListener(ReceiverListener listener) { - if (listener != null) { - this.receiverListener = listener; - } else { - // Make sure the receiverLister always has something - this.receiverListener = new ReceiverListener() { - @Override - void onReceiverAvailable() { - } - @Override - void onReceiverUnavailable() { - } - }; - } - } - private class SessionListener implements SessionManagerListener { - @Override - public void onSessionStarting(CastSession castSession) { - } - + public void onSessionStarting(CastSession castSession) { } @Override - public void onSessionStarted(CastSession castSession, String sessionId) { - } - + public void onSessionStarted(CastSession castSession, String sessionId) { } @Override - public void onSessionStartFailed(CastSession castSession, int error) { - } - + public void onSessionStartFailed(CastSession castSession, int error) { } @Override - public void onSessionEnding(CastSession castSession) { - } - + public void onSessionEnding(CastSession castSession) { } @Override - public void onSessionEnded(CastSession castSession, int error) { - } - + public void onSessionEnded(CastSession castSession, int error) { } @Override - public void onSessionResuming(CastSession castSession, String sessionId) { - } - + public void onSessionResuming(CastSession castSession, String sessionId) { } @Override - public void onSessionResumed(CastSession castSession, boolean wasSuspended) { - } - + public void onSessionResumed(CastSession castSession, boolean wasSuspended) { } @Override - public void onSessionResumeFailed(CastSession castSession, int error) { - } - + public void onSessionResumeFailed(CastSession castSession, int error) { } @Override - public void onSessionSuspended(CastSession castSession, int reason) { - } - } - - public interface Callback { - /** - * The callback function. - */ - void run(); - } - - public interface ConnectionListener { - /** - * Called whenever a connection ends. - * @param reason the reason for disconnection - */ - void onDisconnected(int reason); + public void onSessionSuspended(CastSession castSession, int reason) { } } public interface JoinCallback { @@ -546,22 +473,14 @@ public final void onRouteRemoved(MediaRouter router, RouteInfo route) { } } - abstract static class ReceiverListener { - private static boolean isReceiverAvailable(int state) { - return state != CastState.NO_DEVICES_AVAILABLE; - } - /** - * Sends the appropriate update. - * @param state CastState - */ - private void sendUpdate(int state) { - if (isReceiverAvailable(state)) { - this.onReceiverAvailable(); - } else { - this.onReceiverUnavailable(); - } + abstract static class Listener implements CastStateListener { + abstract void onReceiverAvailableUpdate(boolean available); + + /** CastStateListener functions. */ + @Override + public void onCastStateChanged(int state) { + onReceiverAvailableUpdate(state != CastState.NO_DEVICES_AVAILABLE); } - abstract void onReceiverAvailable(); - abstract void onReceiverUnavailable(); } + } diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index 16308f1..63f622c 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -8,8 +8,8 @@ import org.json.JSONException; import org.json.JSONObject; +import com.google.android.gms.cast.ApplicationMetadata; import com.google.android.gms.cast.Cast; -import com.google.android.gms.cast.CastDevice; import com.google.android.gms.cast.MediaInfo; import com.google.android.gms.cast.MediaLoadRequestData; import com.google.android.gms.cast.MediaMetadata; @@ -36,7 +36,7 @@ public class ChromecastSession { /** The current context. */ private Activity activity; /** A registered callback that we will un-register and re-register each time the session changes. */ - private RemoteMediaClient.Callback remoteMediaCallback; + private Listener clientListener; /** The current session. */ private CastSession session; /** The current session's client for controlling playback. */ @@ -45,11 +45,11 @@ public class ChromecastSession { /** * ChromecastSession constructor. * @param act the current activity - * @param callback the callback will be used notify about session end + * @param listener callback that will notify of certain events */ - public ChromecastSession(Activity act, RemoteMediaClient.Callback callback) { + public ChromecastSession(Activity act, @NonNull Listener listener) { this.activity = act; - this.remoteMediaCallback = callback; + this.clientListener = listener; } /** @@ -59,20 +59,101 @@ public ChromecastSession(Activity act, RemoteMediaClient.Callback callback) { public void setSession(CastSession castSession) { activity.runOnUiThread(new Runnable() { public void run() { - if (client != null) { - client.unregisterCallback(remoteMediaCallback); - } session = castSession; if (session == null) { client = null; - } else { - client = session.getRemoteMediaClient(); - client.registerCallback(remoteMediaCallback); + return; } + client = session.getRemoteMediaClient(); + client.registerCallback(new RemoteMediaClient.Callback() { + @Override + public void onStatusUpdated() { + super.onStatusUpdated(); + clientListener.onMediaUpdate(createMediaObject()); + } + + @Override + public void onMetadataUpdated() { + super.onMetadataUpdated(); + clientListener.onMediaUpdate(createMediaObject()); + } + + @Override + public void onQueueStatusUpdated() { + super.onQueueStatusUpdated(); + clientListener.onMediaUpdate(createMediaObject()); + } + + @Override + public void onPreloadStatusUpdated() { + super.onPreloadStatusUpdated(); + clientListener.onMediaUpdate(createMediaObject()); + } + + @Override + public void onSendingRemoteMediaRequest() { + super.onSendingRemoteMediaRequest(); + clientListener.onMediaUpdate(createMediaObject()); + } + + @Override + public void onAdBreakStatusUpdated() { + super.onAdBreakStatusUpdated(); + clientListener.onMediaUpdate(createMediaObject()); + } + }); + session.addCastListener(new Cast.Listener() { + @Override + public void onApplicationStatusChanged() { + super.onApplicationStatusChanged(); + clientListener.onSessionUpdate(createSessionObject()); + } + + @Override + public void onApplicationMetadataChanged(ApplicationMetadata applicationMetadata) { + super.onApplicationMetadataChanged(applicationMetadata); + clientListener.onSessionUpdate(createSessionObject()); + } + + @Override + public void onApplicationDisconnected(int i) { + super.onApplicationDisconnected(i); + onSessionEnd("stopped"); + } + + @Override + public void onActiveInputStateChanged(int i) { + super.onActiveInputStateChanged(i); + clientListener.onSessionUpdate(createSessionObject()); + } + + @Override + public void onStandbyStateChanged(int i) { + super.onStandbyStateChanged(i); + clientListener.onSessionUpdate(createSessionObject()); + } + + @Override + public void onVolumeChanged() { + super.onVolumeChanged(); + clientListener.onSessionUpdate(createSessionObject()); + } + }); } }); } + final void onSessionEnd(String state) { + JSONObject s = createSessionObject(); + if (state != null) { + try { + s.put("status", state); + } catch (JSONException e) { + + } + } + clientListener.onSessionUpdate(s); + } /** * Adds a message listener if one does not already exist. @@ -85,14 +166,7 @@ public void addMessageListener(String namespace) { activity.runOnUiThread(new Runnable() { public void run() { try { - session.setMessageReceivedCallbacks(namespace, new Cast.MessageReceivedCallback() { - @Override - public void onMessageReceived(CastDevice castDevice, String s, String s1) { - // if (this.onSessionUpdatedListener != null) { - // this.onSessionUpdatedListener.onMessage(this, namespace, message); - // } - } - }); + session.setMessageReceivedCallbacks(namespace, clientListener); } catch (IOException e) { e.printStackTrace(); } @@ -108,7 +182,7 @@ public void onMessageReceived(CastDevice castDevice, String s, String s1) { */ public void sendMessage(String namespace, String message, CallbackContext callback) { if (client == null || session == null) { - callback.error("SESSION_ERROR"); + callback.error("session_error"); return; } activity.runOnUiThread(new Runnable() { @@ -143,7 +217,7 @@ public void onResult(Status result) { */ public void loadMedia(String contentId, JSONObject customData, String contentType, long duration, String streamType, boolean autoPlay, double currentTime, JSONObject metadata, JSONObject textTrackStyle, CallbackContext callback) { if (client == null || session == null) { - callback.error("SESSION_ERROR"); + callback.error("session_error"); return; } activity.runOnUiThread(new Runnable() { @@ -161,7 +235,7 @@ public void onResult(@NonNull MediaChannelResult result) { if (result.getStatus().isSuccess()) { callback.success(createMediaObject()); } else { - callback.error("SESSION_ERROR"); + callback.error("session_error"); } } }); @@ -232,21 +306,13 @@ private MediaMetadata addImages(JSONObject metadata, MediaMetadata mediaMetadata */ public void mediaPlay(CallbackContext callback) { if (client == null || session == null) { - callback.error("SESSION_ERROR"); + callback.error("session_error"); return; } activity.runOnUiThread(new Runnable() { public void run() { - client.play().setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to play with code: " + result.getStatus().getStatusCode()); - } - } - }); + client.play() + .setResultCallback(getResultCallback(callback, "Failed to play.")); } }); } @@ -257,21 +323,13 @@ public void onResult(@NonNull MediaChannelResult result) { */ public void mediaPause(CallbackContext callback) { if (client == null || session == null) { - callback.error("SESSION_ERROR"); + callback.error("session_error"); return; } activity.runOnUiThread(new Runnable() { public void run() { - client.pause().setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to pause with code: " + result.getStatus().getStatusCode()); - } - } - }); + client.pause() + .setResultCallback(getResultCallback(callback, "Failed to pause.")); } }); } @@ -284,7 +342,7 @@ public void onResult(@NonNull MediaChannelResult result) { */ public void mediaSeek(long seekPosition, String resumeState, CallbackContext callback) { if (client == null || session == null) { - callback.error("SESSION_ERROR"); + callback.error("session_error"); return; } activity.runOnUiThread(new Runnable() { @@ -305,16 +363,7 @@ public void run() { .setPosition(seekPosition) .setResumeState(resState) .build() - ).setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to seek with code: " + result.getStatus().getStatusCode()); - } - } - }); + ).setResultCallback(getResultCallback(callback, "Failed to seek.")); } }); } @@ -322,51 +371,69 @@ public void onResult(@NonNull MediaChannelResult result) { /** * Media API - Sets the volume on the current playing media object, NOT ON THE CHROMECAST DIRECTLY. * @param level the level to set the volume to + * @param muted if true set the media to muted, else, unmute * @param callback called with success or error */ - public void mediaSetVolume(double level, CallbackContext callback) { + public void mediaSetVolume(Double level, Boolean muted, CallbackContext callback) { if (client == null || session == null) { - callback.error("SESSION_ERROR"); + callback.error("session_error"); return; } activity.runOnUiThread(new Runnable() { public void run() { - client.play().setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to set volume with code: " + result.getStatus().getStatusCode()); + // Figure out the number of callbacks we expect to receive + int calls = 0; + if (level != null) { + calls++; + } + if (muted != null) { + calls++; + } + if (calls == 0) { + // No change + callback.success(); + return; + } + + // We need this callback so that we can wait for a variable number of calls to come back + final int expectedCalls = calls; + ResultCallback cb = new ResultCallback() { + private int callsCompleted = 0; + private String finalErr = null; + private void completionCall() { + callsCompleted++; + if (callsCompleted >= expectedCalls) { + // Both the setvolume an setMute have returned + if (finalErr != null) { + callback.error(finalErr); + } else { + callback.success(); + } } } - }); - } - }); - } - - /** - * Media API - Sets the muted state on the current playing media, NOT THE CHROMECAST DIRECTLY. - * @param muted if true set the media to muted, else, unmute - * @param callback called with success or error - */ - public void mediaSetMuted(boolean muted, CallbackContext callback) { - if (client == null || session == null) { - callback.error("SESSION_ERROR"); - return; - } - activity.runOnUiThread(new Runnable() { - public void run() { - client.setStreamMute(muted).setResultCallback(new ResultCallback() { @Override public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to mute/unmute with code: " + result.getStatus().getStatusCode()); + if (!result.getStatus().isSuccess()) { + if (finalErr == null) { + finalErr = "Failed to set media volume/mute state:\n"; + } + JSONObject errorResult = result.getCustomData(); + if (errorResult != null) { + finalErr += "\n" + errorResult; + } } + completionCall(); } - }); + }; + + if (level != null) { + client.setStreamVolume(level) + .setResultCallback(cb); + } + if (muted != null) { + client.setStreamMute(muted) + .setResultCallback(cb); + } } }); } @@ -377,21 +444,13 @@ public void onResult(@NonNull MediaChannelResult result) { */ public void mediaStop(CallbackContext callback) { if (client == null || session == null) { - callback.error("SESSION_ERROR"); + callback.error("session_error"); return; } activity.runOnUiThread(new Runnable() { public void run() { - client.stop().setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to stop with code: " + result.getStatus().getStatusCode()); - } - } - }); + client.stop() + .setResultCallback(getResultCallback(callback, "Failed to stop.")); } }); } @@ -404,31 +463,15 @@ public void onResult(@NonNull MediaChannelResult result) { */ public void mediaEditTracksInfo(long[] activeTracksIds, JSONObject textTrackStyle, CallbackContext callback) { if (client == null || session == null) { - callback.error("SESSION_ERROR"); + callback.error("session_error"); return; } activity.runOnUiThread(new Runnable() { public void run() { - client.setActiveMediaTracks(activeTracksIds).setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to set active media tracks with code: " + result.getStatus().getStatusCode()); - } - } - }); - client.setTextTrackStyle(ChromecastUtilities.parseTextTrackStyle(textTrackStyle)).setResultCallback(new ResultCallback() { - @Override - public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - callback.success(); - } else { - callback.error("Failed to set text track style with code: " + result.getStatus().getStatusCode()); - } - } - }); + client.setActiveMediaTracks(activeTracksIds) + .setResultCallback(getResultCallback(callback, "Failed to set active media tracks.")); + client.setTextTrackStyle(ChromecastUtilities.parseTextTrackStyle(textTrackStyle)) + .setResultCallback(getResultCallback(callback, "Failed to set text track style.")); } }); } @@ -440,7 +483,7 @@ public void onResult(@NonNull MediaChannelResult result) { */ public void setVolume(double volume, CallbackContext callback) { if (client == null || session == null) { - callback.error("SESSION_ERROR"); + callback.error("session_error"); return; } activity.runOnUiThread(new Runnable() { @@ -462,7 +505,7 @@ public void run() { */ public void setMute(boolean muted, CallbackContext callback) { if (client == null || session == null) { - callback.error("SESSION_ERROR"); + callback.error("session_error"); return; } activity.runOnUiThread(new Runnable() { @@ -477,6 +520,30 @@ public void run() { }); } + /** + * Returns a resultCallback that wraps the callback and calls the onMediaUpdate listener. + * @param callback client callback + * @param errorMsg error message if failure + * @return a callback for use in PendingResult.setResultCallback() + */ + private ResultCallback getResultCallback(CallbackContext callback, String errorMsg) { + return new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + JSONObject errorResult = result.getCustomData(); + String error = errorMsg; + if (errorResult != null) { + error += "\nError details: " + errorMsg; + } + callback.error(error); + } + } + }; + } + /** * Creates a JSON representation of this session. * @return a JSON representation of this session @@ -544,7 +611,7 @@ private JSONObject createReceiverObject() { * Creates a JSON representation of the current playing media. * @return a JSON representation of the current playing media */ - private JSONObject createMediaObject() { + public JSONObject createMediaObject() { JSONObject out = new JSONObject(); try { @@ -632,6 +699,8 @@ private JSONArray createMediaInfoTracks() { } } catch (JSONException e) { e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); } return out; @@ -669,9 +738,15 @@ private JSONObject createMediaInfoObject() { //out.put("metadata", mediaInfo.getMetadata()); } catch (JSONException e) { e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); } return out; } + interface Listener extends Cast.MessageReceivedCallback { + void onMediaUpdate(JSONObject session); + void onSessionUpdate(JSONObject session); + } } diff --git a/tests/tests.js b/tests/tests.js index da72714..d27bb29 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -9,7 +9,6 @@ exports.defineAutoTests = function () { /* eslint-disable no-undef */ jasmine.DEFAULT_TIMEOUT_INTERVAL = 9000; - var USER_INTERACTION_TIMEOUT = 60 * 1000; // 1 min var appId = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID; var videoUrl = location.origin + '/plugins/cordova-plugin-chromecast-tests/res/test.mp4'; @@ -81,7 +80,7 @@ exports.defineAutoTests = function () { expect(chrome.cast.media.Media.prototype.removeUpdateListener).toBeDefined(); }); - it('SPEC_00300 chrome.cast.cordova functions and leaveSession', function (done) { + it('SPEC_00300 chrome.cast.cordova functions, receiver volume and leaveSession', function (done) { setupEarlyTerminator(done); Promise.resolve() .then(apiAvailable) @@ -91,10 +90,17 @@ exports.defineAutoTests = function () { .then(startRouteScan) .then(stopRouteScan) .then(selectRoute) + .then(session_setReceiverVolumeLevel_success) + .then(session_setReceiverMuted_success) .then(sessionLeaveSuccess) + .then(initialize('SPEC_00330', function (session) { + test().fail('should not receive a session (we did sessionLeave so we shouldnt be able to auto rejoin rejoin)'); + })) .then(sessionLeaveError_alreadyLeft) - .then(done); - }, USER_INTERACTION_TIMEOUT); + .then(function () { + done(); + }); + }, 15 * 1000); /** * Pre-requisite: You must have a valid receiver (chromecast) plugged in and available. @@ -113,7 +119,7 @@ exports.defineAutoTests = function () { .then(requestSessionStopCastingUiStopSuccess) .then(done); - }, USER_INTERACTION_TIMEOUT); + }, 60 * 1000); /** * When on a new page, initialize should be called again @@ -137,7 +143,7 @@ exports.defineAutoTests = function () { .then(sessionLeaveError_noSession) .then(done); })); - }, USER_INTERACTION_TIMEOUT); + }, 20 * 1000); function setupEarlyTerminator (done) { // Add this so that thrown errors are not obscured. @@ -162,39 +168,38 @@ exports.defineAutoTests = function () { }); } - function initialize (specName, sessionListener) { - return function () { + function initialize (spec, sessionListener) { + return function (arg) { + var specName = spec + '_' + initialize.name; + var success = 'success'; + var unavailable = 'unavailable'; + var available = 'available'; return new Promise(function (resolve) { - var step = 1; - var sessionRequest = new chrome.cast.SessionRequest(appId); - var apiConfig = new chrome.cast.ApiConfig(sessionRequest, sessionListener, function receiverListener (available) { - if (step === 1) { - test().fail(specName + ' - Initialize did not hit Step 1 first'); - } - if (step === 2) { - // Step 2 - We must get the unavailable notification - if (available !== 'unavailable') { - test().fail(specName + ' - Initialize - Step 2 - Hit receiver listener with non-unavailable status'); - } else { - step++; - } + var called = callOrder(specName, [success, unavailable, available], {}, function () { + resolve(arg); + }); + var gotUnavailable = false; + var finished = false; // Need this so we stop testing after being finished + var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(appId), sessionListener, function receiverListener (availability) { + if (finished) { + return; } - if (step === 3) { - // Step 3 - We are allowed to receive multiple unavailable until we receive the first available in this step - if (available !== 'unavailable' && available !== 'available') { - test().fail(specName + ' - Initialize - Step 3 - Hit receiver listener with incorrect status'); + if (!gotUnavailable) { + // Wait until we get the first unavailable + if (availability === unavailable) { + gotUnavailable = true; + called(unavailable); } - if (available === 'available') { - resolve(); + } else { + // We are allowed to have multiple unavailable before available + if (availability === available) { + finished = true; + called(available); } } }); chrome.cast.initialize(apiConfig, function () { - // Step 1 - if (step !== 1) { - test().fail(specName + ' - Initialize - Step 1 - Expected to hit this first, but did not'); - } - step++; + called(success); }, function (err) { test().fail(err.code + ': ' + err.description); }); @@ -259,9 +264,9 @@ exports.defineAutoTests = function () { .then(pauseSuccess) .then(playSuccess) .then(seekSuccess) - .then(setVolumeSuccess) - .then(muteVolumeSuccess) - .then(unmuteVolumeSuccess) + .then(media_setVolume_level_success) + .then(media_setVolume_muted_success) + .then(media_setVolume_level_and_unmuted_success) .then(stopSuccess) .then(function (media) { resolve(session); @@ -323,38 +328,159 @@ exports.defineAutoTests = function () { }); } - function setVolumeSuccess (media) { + function media_setVolume_level_success (media) { + // Set up the call order + var specName = media_setVolume_level_success.name; + var success = 'success'; + var update = 'update'; return new Promise(function (resolve) { - var volume = new chrome.cast.Volume(); - volume.level = 0.2; + var called = callOrder(specName, [success, update], { anyOrder: true }, function () { + resolve(media); + }); + + // Ensure we select a different volume + var vol = media.volume.media; + if (vol) { + vol = Math.abs(vol - 0.5); + } else { + vol = Math.round(Math.random() * 100) / 100; + } + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(vol)); - var request = new chrome.cast.media.VolumeRequest(); - request.volume = volume; + media.addUpdateListener(function listener (isAlive) { + test(isAlive).toEqual(true); + test(media.volume).toBeInstanceOf(chrome.cast.Volume); + if (media.volume.level === vol) { + media.removeUpdateListener(listener); + called(update); + } + }); media.setVolume(request, function () { - resolve(media); + called(success); }, function (err) { test().fail(err.code + ': ' + err.description); }); }); } - function muteVolumeSuccess (media) { + function media_setVolume_muted_success (media) { + // Set up the call order + var specName = media_setVolume_muted_success.name; + var success = 'success'; + var update = 'update'; return new Promise(function (resolve) { - var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, true)); - media.setVolume(request, function () { + var called = callOrder(specName, [success, update], { anyOrder: true }, function () { resolve(media); + }); + + var muted = true; + + media.addUpdateListener(function listener (isAlive) { + test(isAlive).toEqual(true); + test(media.volume).toBeInstanceOf(chrome.cast.Volume); + if (media.volume.muted === muted) { + media.removeUpdateListener(listener); + called(update); + } + }); + + media.setVolume(new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, muted)), function () { + called(success); }, function (err) { test().fail(err.code + ': ' + err.description); }); }); } - function unmuteVolumeSuccess (media) { + function media_setVolume_level_and_unmuted_success (media) { + // Set up the call order + var specName = media_setVolume_level_and_unmuted_success.name; + var success = 'success'; + var update = 'update'; return new Promise(function (resolve) { - var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, false)); - media.setVolume(request, function () { + var called = callOrder(specName, [success, update], { anyOrder: true }, function () { resolve(media); + }); + + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(0.2, false)); + + media.addUpdateListener(function listener (isAlive) { + test(isAlive).toEqual(true); + test(media.volume).toBeInstanceOf(chrome.cast.Volume); + if (media.volume.level === request.volume.level + && media.volume.muted === request.volume.muted) { + media.removeUpdateListener(listener); + called(update); + } + }); + + media.setVolume(request, function () { + called(success); + }, function (err) { + test().fail(err.code + ': ' + err.description); + }); + }); + } + + function session_setReceiverVolumeLevel_success (session) { + // Set up the call order + var specName = session_setReceiverVolumeLevel_success.name; + var success = 'success'; + var update = 'update'; + return new Promise(function (resolve) { + var called = callOrder(specName, [success, update], { anyOrder: true }, function () { + resolve(session); + }); + + // Make sure the request volume is significantly different + var requestedVolume = Math.abs(session.receiver.volume.level - 0.5); + + session.addUpdateListener(function listener (isAlive) { + test(isAlive).toEqual(true); + test(session.receiver).toBeDefined(); + test(session.receiver.volume).toBeDefined(); + // The receiver volume is approximate + if (session.receiver.volume.level > requestedVolume - 0.1 && + session.receiver.volume.level < requestedVolume + 0.1) { + session.removeUpdateListener(listener); + called(update); + } + }); + + session.setReceiverVolumeLevel(requestedVolume, function () { + called(success); + }, function (err) { + test().fail(err.code + ': ' + err.description); + }); + }); + } + + function session_setReceiverMuted_success (session) { + // Set up the call order + var specName = session_setReceiverMuted_success.name; + var success = 'success'; + var update = 'update'; + return new Promise(function (resolve) { + var called = callOrder(specName, [success, update], { anyOrder: true }, function () { + resolve(session); + }); + + // Do the opposite mute state as current + var muted = !session.receiver.volume.muted; + + session.addUpdateListener(function listener (isAlive) { + test(isAlive).toEqual(true); + test(session.receiver).toBeDefined(); + test(session.receiver.volume).toBeDefined(); + if (session.receiver.volume.muted === muted) { + session.removeUpdateListener(listener); + called(update); + } + }); + + session.setReceiverMuted(muted, function () { + called(success); }, function (err) { test().fail(err.code + ': ' + err.description); }); @@ -385,63 +511,52 @@ exports.defineAutoTests = function () { } function requestSessionStopCastingUiStopSuccess (session) { + // Set up the call order + var specName = requestSessionStopCastingUiStopSuccess.name; + var error = 'error'; + var update = 'update'; return new Promise(function (resolve) { + var called = callOrder(specName, [error, update], { anyOrder: true }, function () { + resolve(session); + }); alert('---TEST INSTRUCTION---\nPlease click "Stop casting"'); - // We need to hit both of there callbacks - var hitUpdateListener = false; - var hitErrHandler = false; - session.addUpdateListener(function (isAlive) { - if (hitUpdateListener) { - test().fail('requestSessionStopCastingUISuccess - we already hit the updateListener once! What is going on?'); - } - hitUpdateListener = true; - test(isAlive).toEqual(false); - test(session.status).toEqual(chrome.cast.SessionStatus.STOPPED); - if (hitErrHandler) { - resolve(session); + session.addUpdateListener(function listener (isAlive) { + if (session.status === chrome.cast.SessionStatus.STOPPED) { + test(isAlive).toEqual(false); + session.removeUpdateListener(listener); + called(update); } }); chrome.cast.requestSession(function () { test().fail('We should not reach here on stop casting'); }, function (err) { - if (hitErrHandler) { - test().fail('requestSessionStopCastingUISuccess - we already hit the errorHandler once! What is going on?'); - } - hitErrHandler = true; test(err).toBeInstanceOf(chrome.cast.Error); test(err.code).toEqual(chrome.cast.ErrorCode.CANCEL); - if (hitUpdateListener) { - resolve(session); - } + called(error); }); }); } function sessionLeaveSuccess (session) { + // Set up the call order + var specName = sessionLeaveSuccess.name; + var success = 'success'; + var update = 'update'; return new Promise(function (resolve) { - // We need to hit both of there callbacks - var hitUpdateListener = false; - var hitErrHandler = false; - session.addUpdateListener(function (isAlive) { - if (hitUpdateListener) { - test().fail('session.leave - we already hit the updateListener once! What is going on?'); - } - hitUpdateListener = true; + // We need to hit both of the callbacks + var called = callOrder(specName, [success, update], { anyOrder: true }, function () { + resolve(session); + }); + session.addUpdateListener(function listener (isAlive) { test(isAlive).toEqual(true); - test(session.status).toEqual(chrome.cast.SessionStatus.DISCONNECTED); - if (hitErrHandler) { - resolve(session); + if (session.status === chrome.cast.SessionStatus.DISCONNECTED) { + session.removeUpdateListener(listener); + called(update); } }); session.leave(function () { - if (hitErrHandler) { - test().fail('session.leave - we already hit the errorHandler once! What is going on?'); - } - hitErrHandler = true; - if (hitUpdateListener) { - resolve(session); - } + called(success); }, function (err) { test().fail(err.code + ': ' + err.description); }); @@ -450,7 +565,7 @@ exports.defineAutoTests = function () { function sessionLeaveError_alreadyLeft (session) { return new Promise(function (resolve) { - session.stop(function () { + session.leave(function () { test().fail('session.leave - Should not call success'); }, function (err) { test(err).toBeInstanceOf(chrome.cast.Error); @@ -463,7 +578,7 @@ exports.defineAutoTests = function () { function sessionLeaveError_noSession (session) { return new Promise(function (resolve) { - session.stop(function () { + session.leave(function () { test().fail('session.leave - Should not call success'); }, function (err) { test(err).toBeInstanceOf(chrome.cast.Error); @@ -475,29 +590,23 @@ exports.defineAutoTests = function () { } function sessionStopSuccess (session) { + // Set up the call order + var specName = sessionStopSuccess.name; + var success = 'success'; + var update = 'update'; return new Promise(function (resolve) { - // We need to hit both of there callbacks - var hitUpdateListener = false; - var hitErrHandler = false; - session.addUpdateListener(function (isAlive) { - if (hitUpdateListener) { - test().fail('session.stop - we already hit the updateListener once! What is going on?'); - } - hitUpdateListener = true; - test(isAlive).toEqual(false); - test(session.status).toEqual(chrome.cast.SessionStatus.STOPPED); - if (hitErrHandler) { - resolve(session); + var called = callOrder(specName, [success, update], { anyOrder: true }, function () { + resolve(session); + }); + session.addUpdateListener(function listener (isAlive) { + if (session.status === chrome.cast.SessionStatus.STOPPED) { + test(isAlive).toEqual(false); + session.removeUpdateListener(listener); + called(update); } }); session.stop(function () { - if (hitErrHandler) { - test().fail('session.stop - we already hit the errorHandler once! What is going on?'); - } - hitErrHandler = true; - if (hitUpdateListener) { - resolve(session); - } + called(success); }, function (err) { test().fail(err.code + ': ' + err.description); }); @@ -586,5 +695,38 @@ exports.defineAutoTests = function () { }; } + /** + * Set up the callOrder outside of a promise and it will automatically + * add the calling function name to outputs. + * @param {string} spec - (optional) name of test for outputting on failure + * @param {array} order - array of strings that dictate the expected order of calls + * @param {object} options - + * @property {boolean} anyOrder - if the order calls can happen in any order + * @param {function} callback - called when all the calls in order have happened + */ + function callOrder (spec, order, options, callback) { + options = options || {}; + var called = []; + spec = spec ? spec + '_' : ''; + + return function (callName) { + if (options.anyOrder) { + var index = order.indexOf(callName); + if (index > -1) { + called.push(order.splice(index, 1)[0]); + } else if (called.indexOf(callName) === -1) { + test().fail('Did not expect this call: ' + spec + callName); + } + } else { + var expected = order.splice(0, 1)[0]; + if (callName !== expected) { + test().fail('Expected call, "' + spec + expected + '", got, "' + spec + callName + '"'); + } + } + if (order.length === 0) { + callback(); + } + }; + } }); }; diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 0cdd1ea..45fd6f3 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -213,10 +213,9 @@ chrome.cast = { * @param {boolean} muted Whether the receiver is muted, independent of the volume level. */ Volume: function (level, muted) { - this.level = level || null; - this.muted = null; - if (muted === true || muted === false) { - this.muted = muted; + this.level = level; + if (muted || muted === false) { + this.muted = !!muted; } }, @@ -555,7 +554,7 @@ chrome.cast.initialize = function (apiConfig, successCallback, errorCallback) { execute('initialize', _sessionRequest.appId, _autoJoinPolicy, _defaultActionPolicy, function (err) { if (!err) { successCallback(); - chrome.cast._.receiverUnavailable(); + chrome.cast._.receiverUpdate(false); } else { handleError(err, errorCallback); } @@ -654,7 +653,6 @@ chrome.cast.Session.prototype.setReceiverMuted = function (muted, successCallbac errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); return; } - execute('setReceiverMuted', muted, function (err) { if (!err) { successCallback && successCallback(); @@ -680,7 +678,6 @@ chrome.cast.Session.prototype.stop = function (successCallback, errorCallback) { } execute('sessionStop', function (err) { if (!err) { - this.status = chrome.cast.SessionStatus.STOPPED; successCallback && successCallback(); } else { if (err === chrome.cast.ErrorCode.INVALID_PARAMETER) { @@ -708,7 +705,6 @@ chrome.cast.Session.prototype.leave = function (successCallback, errorCallback) } execute('sessionLeave', function (err) { if (!err) { - this.status = chrome.cast.SessionStatus.DISCONNECTED; successCallback && successCallback(); } else { if (err === chrome.cast.ErrorCode.INVALID_PARAMETER) { @@ -862,6 +858,30 @@ chrome.cast.Session.prototype.removeMediaListener = function (listener) { this.removeListener('_mediaListener', listener); }; +chrome.cast.Session.prototype._update = function (obj) { + var isAlive = (obj.status !== chrome.cast.SessionStatus.STOPPED); + this.status = obj.status || this.status; + this.appId = obj.appId; + this.appImages = obj.appImages; + this.displayName = obj.displayName; + + if (obj.receiver) { + if (!this.receiver) { + this.receiver = new chrome.cast.Receiver(null, null, null, null); + } + this.receiver.friendlyName = obj.receiver.friendlyName; + this.receiver.label = obj.receiver.label; + + if (obj.receiver.volume) { + this.receiver.volume = new chrome.cast.Volume(obj.receiver.volume.level, obj.receiver.volume.muted); + } + } else { + this.receiver = null; + } + + this.emit('_sessionUpdated', isAlive); +}; + /** * Represents a media item that has been loaded into the receiver application. * @param {string} sessionId Identifies the session that is hosting the media. @@ -994,36 +1014,19 @@ chrome.cast.media.Media.prototype.setVolume = function (volumeRequest, successCa errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); return; } - var argsMuted = []; - var argsVolume = []; - if (volumeRequest.volume.muted !== null) { - argsMuted.push('setMediaMuted'); - argsMuted.push(volumeRequest.volume.muted); - } - - if (volumeRequest.volume.level) { - argsVolume.push('setMediaVolume'); - argsVolume.push(volumeRequest.volume.level); + if (!volumeRequest.volume || (volumeRequest.volume.level == null && volumeRequest.volume.muted === null)) { + errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.SESSION_ERROR), 'INVALID_PARAMS', { reason: 'INVALID_PARAMS', type: 'INVALID_REQUEST' }); + return; } - if (argsMuted.length < 2 && argsVolume.length < 2) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.INVALID_PARAMETER), 'Invalid request.', {}); - } else { - var callback = function (err) { - if (!err) { - successCallback && successCallback(); - } else { - handleError(err, errorCallback); - } - }; - - argsMuted.push(callback); - argsVolume.push(callback); - - execute.apply(null, argsMuted); - execute.apply(null, argsVolume); - } + execute('setMediaVolume', volumeRequest.volume.level, volumeRequest.volume.muted, function (err) { + if (!err) { + successCallback && successCallback(); + } else { + handleError(err, errorCallback); + } + }); }; /** @@ -1102,12 +1105,14 @@ chrome.cast.media.Media.prototype._update = function (isAlive, obj) { this.playerState = obj.playerState || this.playerState; if (obj.media && obj.media.duration) { + this.media = this.media || {}; this.media.duration = obj.media.duration || this.media.duration; this.media.streamType = obj.media.streamType || this.media.streamType; } - this.volume.level = obj.volume.level; - this.volume.muted = obj.volume.muted; + if (obj.volume && obj.volume.level) { + this.volume = new chrome.cast.Volume(obj.volume.level, obj.volume.muted); + } this._lastUpdatedTime = Date.now(); @@ -1204,11 +1209,15 @@ chrome.cast._emitConnecting = function () { }; chrome.cast._ = { - receiverUnavailable: function () { - _receiverListener(chrome.cast.ReceiverAvailability.UNAVAILABLE); - }, - receiverAvailable: function () { - _receiverListener(chrome.cast.ReceiverAvailability.AVAILABLE); + /** + * @param {boolean} available + */ + receiverUpdate: function (available) { + if (available) { + _receiverListener(chrome.cast.ReceiverAvailability.AVAILABLE); + } else { + _receiverListener(chrome.cast.ReceiverAvailability.UNAVAILABLE); + } }, /** * Function called from cordova when the Session has changed. @@ -1222,31 +1231,23 @@ chrome.cast._ = { * true unless status = chrome.cast.SessionStatus.STOPPED. * @param {function} listener The listener to add. */ - sessionUpdated: function (status) { - var isAlive = (status !== chrome.cast.SessionStatus.STOPPED); + sessionUpdated: function (obj) { if (_session) { - _session.status = status; - // Call all the sessionUpdate listeners - _session.emit('_sessionUpdated', isAlive); - } - if (!isAlive) { - updateSession(null); + _session._update(obj); } }, mediaUpdated: function (isAlive, media) { - if (media && media.mediaSessionId !== undefined) { - if (_currentMedia) { - _currentMedia._update(isAlive, media); - } else { - _currentMedia = new chrome.cast.media.Media(media.sessionId, media.mediaSessionId); - _currentMedia.currentTime = media.currentTime; - _currentMedia.playerState = media.playerState; - _currentMedia.media = media.media; + if (_currentMedia) { + _currentMedia._update(isAlive, media); + } else { + _currentMedia = new chrome.cast.media.Media(media.sessionId, media.mediaSessionId); + _currentMedia.currentTime = media.currentTime; + _currentMedia.playerState = media.playerState; + _currentMedia.media = media.media; - _session.media[0] = _currentMedia; - _sessionListener(_session); - } + _session.media[0] = _currentMedia; } + _currentMedia.emit('_mediaUpdated', isAlive); }, mediaLoaded: function (isAlive, media) { if (_session) { @@ -1277,7 +1278,7 @@ chrome.cast._ = { _sessionListener(session); }, - onMessage: function (sessionId, namespace, message) { + onMessage: function (namespace, message) { if (_session) { _session.emit('message:' + namespace, namespace, message); } From eaf54c1787bba9d9c7d2d22a8650b04351aee377 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Thu, 12 Sep 2019 05:09:02 -0600 Subject: [PATCH 036/166] Got a remote video for testing instead! Wonderful! Thanks @anthonylavado! Issue #36 --- tests/res/test.mp4 | Bin 72807 -> 0 bytes tests/tests.js | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 tests/res/test.mp4 diff --git a/tests/res/test.mp4 b/tests/res/test.mp4 deleted file mode 100644 index cb5053daf8854033eeed99d90eb66b60b29b2b1e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 72807 zcmX_n1z1$i_x@cvmo8awQM$W9LXZ$dN~K%6mK3BBke2RLy1N%?DM9JZ1(ELlFMhti z|31&|-h1xMz4OjF^UgVE<^li!wQ%utw1PR<0RRf{@Q?iR8oQbD*g5j^004kx;cRXW z0M{Sw%#2--W%4l4@9(qb<+j^4XA~L|*{7j1Pxtmccm?^P98gmSXLBfzfDrOfffFg> zQc;t8!NCue)|N$HGcz|uUXXQg^t3g%aDno2bMtZVa`Olyg_bTZj-p&#?(XiKZdPXI z4z|YjoDR^LS3BAZEdY!61-4h4`DM?7o^D4 z$xebB*#={?4-WRo3OwvQJWvZ`n2Vt!%*M*`p~QbTaB?(su&{ucyGU^GLR~DKku_iv zJbX}F2L~HtOXQj1{|R}aFk35AWMlqMzzwx`{`U}5D?4MChlW_$yO=xM8Y2rKdGrVe(F#xBTnQ)Dl>I2&8pBX5B$=xqE@#=_ay&KxEo3^g%y^h6$8nITb_7#f=y zJG%TkGO;p-Jv_w9*&GRs59)4i_1@CO1o4k>KZkXr{CAL!UaE!z_{2olOn@KU@jHhjR&2XA7vE39@4! zU?EQ=1du9$Iz51q;O0b@adLdP@xOm#4+%jLqyXk(?kK?zwQ@vi3275ZB_f|??1Z#{ z2Xz8|005#i5A_Eg-ye9bWCH*y0B{``)Cz5Y?e|HulMbo%R>>C5sNH&hP`mfcbus^L ztw>L+LF-rn#MN=ge&x{3$oSB)&-o<(DCAGWz->Cp{NvU|7LnF|RBrRAckC&?YG-CK z-bTlH*|U6q`Ho~WtS7p2yC!ZoJdLf6lcAR1Upw@NO;t;f=>%B*)~(FlUyQ7jKpBU( z(`mJvMI12)tDL-^%Olo6jcY=@D5vE&P*-lZ%-T|N8=;gOe-9$XWfu^90*P+;g8z3# z1{LDdcx>DRur+!$LlGgD)*}>&K}YkgB_qjkj6U&y#b;5;gM|TEuUcW~v>132f4(V6YsaSSlzSMdxXK~he2(&8p1`h&; zP0yb{-w3oO(#(64Y#gcjWkK?tIp%wd%*$r|SCKvj zd5K)ZUJPL!1i#=pD;s`z04w+1yR)ElaqpU4@{k+J7yzZ%>NNV>+aBpdcw`Ogt|VOu zVTA0V+q|^@3S!w5lgMEDsdKk7%R*!f>>NeH3>b}UJjEB}%YMIowDrjB+LHGbB~hse zZdLMJ1dTWZ*-QA+nS@Lf_e8OGFa4-_K#VEL64BPx^2KdCqf1!^9v9p1?te`e{$Aa= zSmnLAf7Z~KC5vc~5&@CnkYK-iFI5zzsbe&&2GejK-XhQH|peQX3OF_QLx8J z6d^XY^fMlvW_s+BKUUA3Bj<=V?jr!#=+Hjcd{7H-}JtIdTtBn$Kq1mvwcG?Yx9StY;VmVSFxS&i* zl~?yb4iM)DtS3c?6L1wk9YR$*93(AHG;hm4-oKi%&5kYmVuKC9Ti7;SO^Qac41g^w=9zrO|vTSKwd;V4pq{uj3u2@}^4C+Y9sZ;jCkPCLr( z49Muee8E;AQd0c-q!uk;a)LKHiYb`#<0}kG%-;vnchScZ3S0z_z&je~?Hcyx z5esd7Jq^X3RfQdu#U&j+w>Lr+&A0cAlS^{_&cm%y^22$)!dn0s3B^M?m2z7E1!ask znc^N|qY?4^PyS1nw=^oS=hL!i^r@jc_u>AHAb45*)??*%sFGb5?`Q+F&p2hyNz#@% zhLT2$MBJOMahlODq%XaR@=?$kf=*Jt4tKIF%naRF1T}Yvgb_gsWDV(5fs|Wmv&Ok* z^R{DK{m(=o(o>EYZRN`TbpYjJDq7++X_wMkYY-IqS~pNvXDM9rjq@V32hUa1#DN2! zz=55X@ZDf^V)iL#KwiNcr8#dwN7?fCDru;Q+;CJlGoI7PA=)jL=Bo5PnxcD7^}6w2 zpb(VKYON53h8;qh=rU)%_Gz zg9TvbUd~cbJ}@Ul^Dh6xt;l z6j6;K01(^MpGH)Nul*ZrOl>?CYQeE1G?2Xzh0SfSFP^xcc;FD|9KiMG28x0G2f`tc z&V+vLxyP?N09sYCJ~x1%0<2gC@8UsIMojgW7l)@w&Sy&$nQ$y6@X33U3!k__Hgm0Q z+1Io)-Ff~q-Hec{It5^{luKH%uP|2ggOJqnMRalG=VqeEvme`kuJKO(L{stYNWE%X z9U8+Bn0#~9_o?E3>GF=`$Y9iYIPzLZi2vMb&u+oXtv)i(xW>NA%VVyoaduv?!aVS! z?M(ampnavaoGq9A@aAEQ+vIKU@TWCPA^gpAdYoy{I4lF_yxR{&gB=C_7PRN(CyYcy z2+Cv*&b!C|CJO%o!4|1v0zDC6uKd~|Gp5e5JKnWje)@ssPigpc^S;s`iQJL&oNj# zHMm>f8#Qfp(@7-V)A{1~F-hD;-|FhhKxhBP0DO5}WwsiaY%vb@v8}pQ8{>VVLU@P7Wa_%5|CCo9EfkECtEFVbU`k0 z`Wj(l=-*nQkuoFU@1z#gfBX)`I)B~Xpf96O&9h17p1$qGEkc&@zCZF^WBz2EmpGT1 z{)n#aFA;l19kLdOvIdLZ&PHlM`}KOT8cfu4L4t1M*z+b+w%-%gi28h_cZzwDdQNbc zIx_8C1VdL)XipG7?Q30&kpjAB3N#5xB$J;(T zxznD)M)pz8=)_$YZwkow)D!J-12Y00NT=-I*)Qi1c@0f_&%a37`E%rl$zGIuP;$3% z`aR-~j!S~X&jci|Y2=U`+;rQzcHHc_AW1bM2oskyO&LVYPk+!4 z^opy2(}11EMG5d+V9h*W8xj1QXeQ>lx<<%IX5!g7&(DJpzVs~s(kJvn7$IIEd_>TS zIz%gVMB8wodV$#toP!n~>@Zn6m8GZ-1!We=w;*4LZS316$oX^X=QwlKng%RN4JYww zO&W5ovHMW?P~+XGB~NHsEHBHrdP(p`O*n>x>n}0W6_B+`L1mas`qp$fzbh`Tb}_r^ z%+}!AL0<0sW=PtH@=gHys(2AyU$lueE9o)Xpe+?wc)^+MqaZ9D*0w3?51(MS;k38GR)I^u|(4j!sEmI)mDAVM3)6PUVbxm#DdWUx-hc|n7Td1i& zEd<8Qbi@rV&Qvun)y^aNOsIZ$dZ`2U1?-3Uif8mErdU2?FDfp16S9mXF(a@aaIFL^ zBY6r5LZeXu0Bp(-s=POomuH6vuFsWJ%nc^5-yL|9Ox4ThDQybI+>#p@5U!K}Ywunu zVW+1M9ybJ5h;nu%zgMEmXF-LAldix+(m9t=K}BQa&A24>*VY|ucI{W(xPKp|-o)Gf z?lZ^v;ch=E7};Pizqz}4?4Fd=V3V-EP%G5X@Yl|Ec6Qxweg?YGk<>cZVAW#WVj5$M zpB5Ry1v8rI@N%j>cJ@?RX|b_{EQ15{f$PBC+q{B;WUwEKKa>$@bVM2ov2-TS^QwC^ z&3=w$>P5^%Zn13SkmslrRIcrZ0}$=Hoe?kPl-^&WgE64R7l5iv86` z)y2ai4ll=Bvw!^>@WwLRF9~@f&(**q65S?2tAFp8`{y`c3SiE>(`$4D3z+2iiFQlH zlTDAt0NlJ}J$>2cQkrDmySca5;$?F}X}73je+3&FoZZ9&Js=tFLDW&cs3}7(QCYYt zzlLBi>bb~vSj^Gig#qz7u)L43r2B~uDB?FWtc zBGxu6{bm_>>;jkgNxMu0r1&7=;(;+!jv@R z+9KH0IdstME$QR5f|^#4GA=G8B<59+m2SV#(z#;*TB#3F&KP&iP)_SyKNf=h-CXLh zYRR&nN*0G7V+ z9?L>ais%5u$j5@uWD4pn@p_cmXCT^A&T97r*c$j5C7wstNRZ$7lJJq0hH;TJDxsI; zAPyU8>|!X+bhIOixs{(t>DpOGBLJ$Y+b+i7$KeKsMVRvX+-ED!%|13iLyIJnWUdvbFu7 zGQUZ0`;wB>2}A3#URO;5jl@0W)GyTBC<~}FGX)!mcMbQi1ce)W{W8pDj_4ZD6D%HS z3%v7C>mXquvCetj&!B5B2>LDR=?5c7m7zrF8ES2NEXD(r-O|h$Ux9ws-iT^EQO}0G zxyqT=js2y%mL6wn_b3gY&E4I!`rQqC+jVO9G)ULipW9no2S{INf1$;x-?=`MLII<9 zp^aKqIm<{;P_TaAjyg^7NK|_C=QcIq84@Gzw+-3LBFNsxfIqgSXQzRqwyHh2#so+; z+5Wmm<8Lhpse+<%BB!N)?1Dn)fquDpgF3tVwS2F_)y}{YBbdP`>cCw_;unL(#WFgN zT6Fs_Y)&cu(B9k7>Nxzbis4+9DABI>^iceHksYz4_VE4hsR765KaW9=)nA&0N(Q(A z6|AV0e8pGXXZ2C?3)k*KM`!g74og)vKEq}6RV|JpO(FiHzeZ-))9UvYs(vhtAP@&& zrxhBE&4uBz;mt0X{hh&x_U7)j2(^e9{v0(}^_PvQ+gc2M+FX5ho^@i|)m_kp!Yni66k%Rsly$3WDdi zT3CkqdU#yKJSS2-80lSXyFON(MtZMm$IG%47|_+m-V$pr3y#^<8c-U3)XoBnLm-lR zn4@Xy5Fbcmx(M>?DeSn`Pw?nORXJI>(etJWwldIJT6NJTA79-bkhHcmpEv5U-M*rpw)yLD}Kj&o_1+2{I zr_6HacQbtce*KnPPYW2UZaOETu(R`Ci&JO)GHYd;F!)9wyy z5WE(9|G`w;A*rY5N3pB^xo5AjXk&OybYB+d3rt!)k4A5igXPurl_Q8pdK42USkJR5 zy%F%vqM$%~vYNyHtX%7x2dgGedz`~cs_Y&>G2zq!t9ekk)d@|Kny_fqOSKhLC( z3_cRJookMF&3k9c$URnFbt2>FGxC>qN^%o6mwPv8QwB1!zP`N?<-+MTl+_L(O2z9* zf=s1I$!EoBZnO&P?(Uyc7G8r$k%nGZ8Meg*aYo;Xn5_9^!y&iBC6)Fv%cV!L@VSy+ z9T$mk><9+_>om)mqIV)qDt+~EZ{qicp85I(F4(#EQySQl`(7pGKJ80cM3PjE*2c{r zJPmzy1NcfeOGt`yub;W2=y@DpfW=!qE|xwMc9BUcb4>?%zTkmhI*W)j{&aK>J}4BX zMH)m?7V0Xk@;zvU)+~!R-V`sPUW2R$drpDsf)PbeJbB!AE?VhoB`1)K# zye+biuByZ8hwb#z;0A$ohpEr)n$KT{K8PP*Z@4TDZ_c<#=Lqa%Z*gwjfm)Z^eb*kW z(JyqQ8bdwu{*^Y()2wV{uu6aj!^tLpDGZ|IEB#$pi5ewkXuuX$+V(oqI&Mqm@jIpu zTjRuuO3h8+o)@&*n1qs(wG= z%Nxm$H=d8VyjE^5lcEpFZk%QkY9b2^KkvA&_A-(kTsAhP3p-wmFYycIQ3M;MGiN*vIax=VlIRomzOtsjzvUZh*7_boAe8a z$kEE-rqkttqNN4{8OMyX|irriH zC94PA}Zw#gOByG`zoDjbz5olYh8nPVfcq zBa_m*>`$LI(#fDdmKx;z< z8#E4rBjF(Mi%n!Jczf5N3*NJjdnHRHO5k)?*%%DKvg zrW$($%Z8Ak^Ci^Ic5$g=-(84+&0;t*y)E*lEIo0z_wHRKW&koxGC$(uxalG?5~2)u z3HJ&xy^@P0mYR?`cDE$*gxNm(RC-N#J-I zas*o32hG#`S~!I-g9!Oc7WovO0^19_2G@0s3;EaYjI_-EAw;MTjA(4f*n-8 ztLG(Uf4TJ!!L@kx;wr+OAusbtar86GmoRg|-Ol8cCaXm+i$&GH=*sU&*3Qp;<`$+n zd`v5p*Y8u4rX>uonm_jCJ0swThCZ_321Nwsljf^=$!A=FAtYHs1AWm#d?B>r!Z+zn z!iSQ2$bs3ouUOk%R``nc8Li&s5ljTOyk7q#mi7xBM8x^R3F4!Q3F=#Q#zORK1jalj z^au$byz!)e?$`Ko8L0|nQc2zRr7uPiV=@P@ADs+hCg?IPIn?}hWr}w>8e5qK#ue#r6a{8C-HNMGI7+S?w?Ugt5F1;D4TbP2suTWyz zKZ{dZ?$pnt#Na-yE-p@%%Af8B`8oYI&VdIe5h~F0Kmw^@-CVh|Rqds+qO2vj)+OxU z!{MyCpDP-kX%=9uwnD56`1QNLA7W5!IjmlaNP35iCUG+4ZhfHTmqPS~46P0K93X~| z*Bu}8t*ph9|E&ZNkV#qxr%nA50t{l&SMl=@}YY8!xt?5eO{6;bOs?5Fp1d z0syTE0;!7cSog}VmoY2z%j^!i2S@{{qbHs-PEZKC>2p9%dIa<6Xw<`e%Q`3`F z_%jbOC7OGu!{4)_&-T>r6Wb7pcojfoW~;pp{a^Dd4><@8}kvh z`x0SB>Q8y!Q*7J^Dp55ZRNWC6w(nitubeKdwePotIAX$@L3?7i=jC7!u|Qe81utHV zB@!%v(b4NEmD5xD*#CM&(~53YAs0ObXX3HLMzK~+OrS^YdQNsm*Jnw6r~HWyW|Fr; zlc{Sn=IYJ&$`_mpQA%ZpB)hs#0_9b{7GO}Dp8s7*b}MAs528UKI(;7yQbe^frOCY)2RFn6C0PQ|!SbSC&hc*xca?Y>zfeT)ph9 zJJk$#oSUCOhjq0V#Guct8aV zT4m(p=PM`r6cm`WU_(1*OUX^c1o;b=*QRcwQ7H>IB19N>xdeZ|*ob3KUZz~RY(EW> z)W>ll>t*1|2#eC7QT!t+tDdeXhqD{RBDL4D*dEGAaBlip>cIwmq*Ha7hyT&+e>{tM zPbFz1^U=SdX!Sy+d@-PX=!0@}6H20zLoEJ%w%JF-L{NQxG=l=m^O3;|*;0w< zRz%wO-t3|8RSq}<)VQ$M@#ByB;iSJ)Bk~@(N?fxJJx!vKSk)tCohv50Fx&d!f}$iq zr#D*900$BBu}3mLsyp6VtDs#J@jRStx<;gEWH$6(^a!w`qK}jtw?-RJwDMDk3%1kN zWIbEib;cEVBle+?&%oc(Fl9yyAiuNg29|exN||%R?dq?O@&4d<8g456@>lCaZX= zz6$_QyEhFyN5$tUisu+!WrD?ph0`x?ctlNN(>IHKUPv0!IwW43I967B* zJZ~SLOkzO(8p<_xvsP1!Sws;*q?Rv)5FPSZgv_kSl>p~R7;i@zF=rsTAruDhU9MED~-7C zAX2SmQH1`yh%m0#>Q3NoF=`36De3_z(ELMVP#C8A)c)rIoBY)?1eQ<_gjYZiq;9?t zXLO*BxE?vZLr9d&Hf>pdeWHmftlkYY3G@AM$V?I0U4`&$d^gwzj0yr-ooO99Vr5KS zFW*aH86wd51w?%kt(E_DuvO;nC~c%(eKKWiE^Y{%6W00F^etB2arp_p2{Q8tb^3k{ zD?VtS|I?K&L+qdIAFE}jJ@!$1E%H2=VmjFOSzuOCn*^su&GBILXj~Gl{R>WUJ-tgv zx~F$^lB7KtG9P7{mcRkIzbL%>mi+I z56NOeq^c6ANY($Tst@9IPZwZ3*xuzIl=)}{;t>WtO|K|FtRfbxLma3sud`B2X(Dd5 zTmubOAUKEpEcB-G%Jnq`gug`{^64qzD#0W-{UclrOWclN>PAoL+Hg#l8k(~Zc1x*| z7Or#mtQ`GD5Bz0BrQhl1H4Ft}jjdiX9Yc=SqdA;_HGbp8>F~V!rF1vDeiFkB)8lk< z@30qh)tHU(3-3RcUNU&S@%N-mpsaphApU&&Q_DSpRH1f=2UVCKh}bL+u!w>Dqgibk ztO)CWXtGW=y77CP4P8G5A(>A@nch$BfG-Wi@QCF{YhQ3P7CrB=4E}5o-o+E`cR9ot2QE4Pj72Gf)rjUrvP74 z7wHxsi3fN~DPU@V#Sxg*EFaSUd@C4V_&n^DoOj9IMmMKIluS+=M{V$|V}ihcRIY}F z;@j`WQtBDw3P|`|0sou2@SwjFU_#$rnvFg5Za>=nic8>sL~Da*_K#>u1i+H2TvwE?Ro)FwEdraGK!DidsaDN(lb$pRL;brisTwgN>^~ zE>km*yDodDFS$w6CA(ID_~@pb)|R zbN!GkaZg1l>Ox44c_!RLH#$&1%r}3!Ok;{hEh6QrLYlo{G~&lxu`kGm@EVH9bFn4! zQt+5Bev`+R^=bFAUFBfXrJl?A6EM`~1^%Ai6Z&q>;5=SIeC1Q>#fs`^qRY-T-iu5`9(t(&zt zbLixNxUepS3DsySzd)Y+SeN2Z*e}*EqH1r%i7o0yVH=;DPEJqcLZy=xz1#JNKu8c+ zdpoA+n?H*zX zbocAC0E*h~Z<2#EDLzNj`?L1uprL&(XKD!vQk%`gqYlv`iL({eZXci0ZyI?ih5Z7g zC?8V%#;{gjQ0p9s1soGH{=y;`O$h2^iK)Q?vDm$VVvcN27Nqv34QTak9B%Y)Iy#+> zzmp=tfGNu9V{Yn}8#)BK`@}`tdJ_n7>Rmd+*4Mg{2tp`?z~1eg-olXi7m}Qhk#i-a z@!%2?g^39ujv$wS$EBx-ryoH#fSN=>2pBHGNY1L&#W|6cjUb5Ka-(^a@d0K(NFR4ndrJEeO4-^7w=N=gPW9PZwTmd78ghe+p&N!57mYyhVfW zh42DJRG^=^gs}dg{h(GJ2-wM9tC{0ZP3`rNT>dc= zInq;?S7cU0N^#*z_bnh9FUv0;f663mlUeCzv#!v+TCtZspkta7r{}+?H@zxMTtH5! ze_CV-eOpr(K18S3jwZ}#(dufFk*QUDFV+O0^s<7mSxcwykZgGTiaL(+m)oH^F-3mL zi777a{Tkl`@^$}%Zya?1Axhtci^WEUHW;`aWB83hn8x&C<{Huk(QJ+IXg-*N7-x={e%=29@!5ZMyUZO6el=g71R*_#&a3OY17F!ye1@ZEvgP#;gv2B4JdCL14=AITxfOj!g0BJA z*aj7=-(n$+WdoR{=VG~m3e4GAH$E2+wBRP4>b03IrTg!r!>IAFR}P)vp3C!_^WHBp zTSM8N)MS6(nX$kq2CNguwQ#IX+2P60OF>hHXV<6!tY1V+f9Q2`wF&w9#vvLFOSe6l z;$etJc1*je=u3{z_2gr=YP9`rc$1b!ELeYr?CW&8&d$u+pvZB|$*uJo3nd3gkpfWE zl-k_chJ1vlt=SP*Eqaxl?lC)yKbnUouAkc7S~YUC z8i37%b4~=4F23D#7Hkplg-XKiHKae^swUCmM2IZ{+9i(=G(W!2jTrxQNdo=od zvdhnwARGY>M^~ge8qf8 zz{}Hr3Tt>y`|as|JBPs>ypraVPeXg1yJLzGZBIEg{EY!Wx&8jg^iob*h}O6BxtX;6 z=H3rtQkf)SiWe!=VdCL6xwR@Np+pK$^&`d{IBCGSzg=N*hgfZO2h0_uN0fUKOs!4 zW38bwream4QfI^Z$-lWm4ZbCu#2TVmx?SOC-D|n~x3C*5of=f&kAjBtpQS?s;l4&t z=#v1)$3qV$k9$vH5c5_+kc4|5dj?Op0fUHr)yn%!A8;;p@zx6y{3vZDLN7Y1T=^(? zj&H25sweNLv;s@ANXl>Ur9~&KbU0V7E{$_qK1DTQ%#yom;!mHRLan=7I^gb<>&~iT{wb(Ee68U<)(7TN);lH=HAVeFAk)^@ z9Ta0)@3ibr(W%~lyhu|?LoTPE?a|O;zIY~#nv4+1KusCh7KQ9(zL#>b7ZDqtOVgzW z8xM(o5PcyFb36Mv{XmS!&4%%K$eFm@&s_Q;F10m(Lik@?dMhO{K*cp|oN2v-7{3J;J2|{ zDr2Qp3eGsYAU&bKPkI5yQu$;=KBxNBO<*ktB0!0+l5O^`wW@^~H_Wn#xv%6kTi$02 zMmzD_I9MAShWW?#5tQBlRVAxMmTw4SDi-1yJo^M_Ow&q_eCy~Ds zcbFq_U27DgIfZf_NpmMRxh62;Zt0pK6P?5Y9Pw`S=xQ<#p#gxcal1@2LU$GU=nRuC0Z&WxCA#)vt>2 zgicZTM?F3BUGIIXS*F9o#Mv7)S+yVFl0;Iaw|2a+%`!yUtk)eY*9+08l)$@0;`+z>d z0mtZi>V3yA&sm)4>7Kkt)ml^Rtcdi*49WH7D0=R?`1-|*c{lXgAH;6gwnt|q&s9)3 zK6q6|%b=jtxvpgpW$&sL=a%%&uVpg}zLg>++M^`kR>t>6ozS5LdB`MgO9C&i;%PUu z|L9jf%{TrVppJOd)|%$EdvYJ0wU8y3S9^=dEK22IJ^pN_)mS%$7*XR;94$O>ZiCjNLW_;-71)lc4r z?lPh=*qUm;FpKzU%OkqplZq~NR*m5$57(Up$dmRhm=3oVOz8>zkJI1WgONAs>~;As zjj1o*p>^8%;A>F&VUsHcjoZbqC(F!CTu8hr>4R`Hurlqv!X>i$szx!TLsjJRUWhaZ z!8(&h6IY?|PReH79xE(!S;Bt3h|m#fbetmrIH4mTLOvHS7m@r`?ZBpkrapehg|dvX+=aZ^tN(I z8Kc|m^)gcM->KlNOWPW=rjaXiPC8Z>g({GugT2f8NOt-zeDMZ7R|KpwIKO6Ws94a6 zH%};N6TPa=wkY$QM%yr{LJP5wqD3@H%)Mohd{7TkasP8{DFrxUJRcPXF(buT*-3XN zQsSTU=CaJ_Ma^i_(@WmzWp-&^q1o>Kj*Uw-ejQX~UOz8aqO^xt25Oyr>FvlzO^oPY z7_(_^#01O>jBQIgt+ZV($P<75<126qIXV73*?OBItG{@=6Vdgq)PaaOaYu_f_x7XC z@DHuG@(pI)bzhy8EIF)M94QpKgh&3?nbc(*jLroh@L!tL;O&1~ECBmgww`E*{thfl zGsj!)3HMj=xKS)8e2s!~&PgIii)^-?bZP=)wv<1}I&l*D>4(8@{rvnsQKKF|%#E;-ppAwql%4MzKS|6k3C+oXJ6`r!3a>jsn~#Qlo9%5*!>3EVK1&}B zMO)S;FXH@1@8C9Ba=(}N2(W+Ibd-NqhalZS=1`2=ph>N?9;FeKBCGj#Ex}2NG&|2t$ZvKEUSu?=o($XBIi8hp#I z@7|*N`T?Ti&pH4Cj^oy7Ue34t{sI@YZ(=ZGj`}QrM?}$!n}`Huv$=F z%bBohgO}di&(U;l7kvE)nU;{b7D7h)* z)+;kfm@UtbeqMEysKatHa8_`%r1AXACGU76y$c3I1~42;otM~WD?%zRKk|_ph_HB} z$SrotZ)zq4WGaM>5BI;t=@l$MIo_I1qY(3Kev9}*6{$nImsq!5GV@PqjA1oXUR>En0cP`rA9+`tdt`zCNM;fEHPsb3l>;92f~sjq|{&N1R6YDEhzjj z)2@&3t7^0y#N~1AWE?{cbIM+5@!#_{kU=;}?(uaS`>B2eRK?Ti*}6uVd<%wtmNt{3 zi>JAN`(f*g1l&Di$K;bQonG~(Nw|=~RcHBDB;H_C+an&n62m$o&ek zo2SdTdn@GrWGG{QvgM@}sxAr5k+?%|SDt`Q&`W$Hi{n${J7=b;`dw}#t9ReH9frj~ zRx%xi4b}EMY`oySfzthR9eT*Z6rR$3esEb6rfiHuX@*Q)x{3K;a7?GYFI?7a92k0e;|yEu^j$v8L%*vDaBOWEHMzLx(M-@ zZK8!`^M7_T>c4tTr9vyEt=2)c0(W=LSLz6e9R z0p2j^wAO1aUUX+F&A!F)@fl(+z5G>y=Y9OUyi@aAM$PnAyW3_*ZPNSRm3LcTZFsgf z>R*iBrIK{cOba=Hg&rqe3EjVOo0WisR}8sjxi8^kifm4BR@~q1(+Z`H;JZ7iSXtku zKUvNcx)c+j9}aGk{~Pml3;>xg@Zjx?C~MGvm1Um~>|!`AfBQEo;?yoC?a6ZK69aY9 z{T4WVK!8p>?e_g87&s8%{3#-Z)(ztAC`6*c?Yn+29p(8e_J%NQndlD$FNvV@i{BSD zY>rnu>^?p=CwJQS*YY}a`H7dsCpL}u2_>DJ9%zRInc8m=pTtrlhG2t_21Vy3LSw&L zd@%9w>fk$d{}iJ)5RdJMvZ+WBC`c>%1F9kgj7A(E_|6Mu$QAIPaGK95wYf4S2cPA9 zO!l}j2#KKfnBVV&p_=CGEpYZOd|S4iX^9>JFwPDDP_0l}y^93-a9(8hnqa-%>OLwg zO0eM#{t(9t?|pZ#&K-1gB@Pid_@mhoNy(E&BV45HQn|a}w#smlaY!t1Dh$d)6SsqTNC-UH?K6GzAg&^bmjav$fs_4!ECj)NTib4=c@>_(_pVd$(m=$%XNx+CoDr`WeNEH=r$6}jFF zM7_V^5wsUHs`KBmP0Cv!2Cv?PQtWR%UtXJsY)~+8Uyh2h#Kv*HxRLY56$ep1MVD7f z5$hO?K@3Uqbz&PnHf*SVX^}hQIoqjOV*D;tGXX9i_pN;WLqt!KU=9 z6@}k0WRg@xiQ>5$w1}@`$II^zE~nVS;f^CxHRYtiym${x0bpKP)MeYKygsOdI6I%3 z&ESRV?g~B;lMlqyIT5RIw-yvCTqDV3$HivH_1F$pk}FzM!-?as(<76$O7+WNoynhw zJJ|l(puA@NnnTAwVLVs5R25G|YVwiaT9n))e9Eq5(Wes)$tiA#+2L!oq^;HUrjuLSQI z@P1CNG{Gl6as_$C6B&NA+N@#-&KF3-|FgsM@mnN#sDmzl{r5HR?Et$N^D$0?`gB;y zI>V>9iBdY`9+L_@CSk&t550Bkkja&u`Jop^gk3^6Ta(V|7aA%9ktC+L+w0zb0wTzh zx)Edf5wI>kR(09F*(lQfjbQBZ<*E4J^5nO^6EEu*tLH!;HP1^8D4vOE7ZBI+U+VnbC$2^XjY~Guy_*<{cV5B@UHs(=Ru|884tzRE=Ux z3jOlnZlN`@chLaB42UK!^uhr_YG8U}-*%>I&{+_Z&F!4Y=T5_yuCB&`&1W}lcyrBS zVYyI6KruY`WtkE>WBb278c5b{8~W$}VdFw`a}Be-de|OPR`DKe5kcR8FgOx3>gn}r zC&XXkrZ0yWxt9C4?_TEbi&-xX{p-8cm0#eIHPJd58clw|C#a|PgfXtge=O2eh)Q?N zUgVABz{Phj2R75Q9IxzO*6{WSrLU`?q@(^ma)ehC6i0vJowacp?CAoDH+l%W;)+^byLOfw(YpG=Hsh7 zlh>81urr3vW1|%*26Zu@6S81ApkQHp`8gsJ1ErPgWEg09Peb7{Qb;Vwbv%@y4`=&- z_0sw|Cg=Qf(6mRzDs$%Vqr#Kk1ic-8#>dw3GZiB)_192GAew0&>$k7xL?&0C{Y+|1v&ft-+oq47HB3AxF^(25c#x6liN+yd;P$U-3I zS4>9XSZhl=Pw_dpWAy#@8*YK6M_*OcwWFD1EHDz|dRXVVv~V=#$>o>TOrJ1D+xWwg zjYM)z&)@&NA!OEkJf1iRW8*bK?m;5R|q^T|R8(U%{u zS)z%*2u_*B8VvdP9F+7L{ym!9?y373*;KWMDA zz=#y3E?#%a-#aMuT$o4ZdtcI}QKozHm4z!6-#u46BxQqW(!J5tbNtDTo}r*xaS2?j z3$Mn2QYcEKZ+fZ5?EshuY*EvVICG(c&ugCeN9*f{dP^aBrTi?~764V%H3HNs!*Eoe z<(bB1BnV`z+573gvHCDr|BY22C>9<8a%Bh=;JPQa--vRv(xRgbOl>AHu$X2fajbN^ z0tE8Rg)uNQIHbEuJ}A&~S&~Ca3DJAnXw17bb&e1Y1Bv{e7ym!D-ZCoAC0Nv+8EkNOcXtmY3>t!aa0$UZK(GKqaJS$d z+zIXwG`K@>2=49>zG3fu?)}y}mo>kp-!*h~byq)KT~#ma-vbgJ4t`OijOhN=1G25s zQwOq3MIo^HIW+9v+7Cap<8)4x32fE~Mu^u45o*F|xW*G)Tja@EUe=ozFAW`-8)VZ~ zn#(Y1SZ@|}XSyvAj!#cl3tfpTs!D*XyXsYKBc-3;{6@QZjt*QR#1Oic)*U%`a5s$* zH=yK!W2F}gOH9=M^*yj+T1G#(U{w3>=$B8YKhZRf3SYenJ)3f=RQMc>kr?xaPBjJW z(rf=gh7yN4N%v2_EzQfw!5p!p3jY#PxD9l=1lh{wvCB&86W+q3uK-|+wr4lcb__Z-Rw;@Oolm+H|PNn znZLI86>qHAOUoyw{P8)^?wU@JXG)oRKftpYho@gO7Vx|8IyScT_nh*@MrlZju2(a8 zOb?ZWD0ObaQv)KwvanASa}Hf@NWg#Yz1bHRzoS%ZP%{%#jDYKic(X3wQ6gP!vD$<^U5Lf7HEKJ}qDaO*EOz1*WzpOfAxXUiI7#>LNLw3Yji5d;6<( zFW$slzj_-}Vz0>1)ss}nOka=`6KoaO+0%C>T3)FS7@tMe_(A)=TnA{QKTAq(zCO`# z>WeJTVPiMNNV}GBC#^dLABY&K(NpI7HpD?9wXU9oLo)*8` z3atSU*92|tF&N3~)|-w``t6)aC5;!WY?l@g^WF!wNFgY+0A zyt>m<*23|Hhm)i@5qsSt0ekAP!)*kzYv$Acmk5Ku%YUarA(^XvY-@CEte zQzSlkPnNI1sm@(W-l!mdC{?#LFF`Cal$;@TnOR7$eEb{x;b)Nhm_&$T%~8;H(eWNW zxLU*HC5U9Yfj$U+A4r@Dg-wNa`Hd$gBn~WMx7c4&t2n|3JU8aWR|jMY)%ne3bgtHR zg%#^Xbd*9z1N4|;s=g^a{QlN=oivE93eQ*+{lTBxPg2X>Z(48r*jtij$8vHoVyfpV zoh$m}sd<~`%{q5hROHPSKDCsT+cywcC4BZ4{cWj=JQ5WKG}~XHs)8L;7%HZVzga|+ zR$OT$O#a43)x;Jmselm3zBX;aiHfmmp)Z-<ygBf&}?B@?I0 zezsInq5P`Rj311znBJ+ZzLt{C_EAL&?4B;S2haprW~Yqo)QcI`f9c*pTfSHM)9z*t zE_0sa;}t0>CR6CE*u>uXl|x`$%Uu5!0hw_Tt1qykEOXBDy7@qzGs6Xrudp|zm7<}T zq+&p87j2p$H0YByX2nx%)@%&W3(C!oEh~csm*y@0eRl2TpjF$*9vYI*|I%%2Utj*Y zj?T%XRk_I?D33e4>nyPVgBV7UnNH+2!C%Zof&5_s(xRzWR9}wislqA9a+Fl6Bu-Ky zyag}JlY>~mjFs`3H|oy-FXj(=DH8lj)lxPyS;anDZSA+h)KMaIZj8R-@Tlr!=UfPM zjc`Wsow8^+$44#4bP~vCPVt=_YiaGyp?P3UsJ3~Mv^@hhlgM9RCC5x$apoi|(;RyX zlp{Wl^m4h98Qij;#*K(7fpH|k&daa6XNy{yPJEeO5&~hD>f2@3V(^aY&@ZT=FUOT( zQ(1U9SPKC2@**{d;Mvor?m!xAa1B`!Eekaw-|vt!xo6LQ&*KkXbs=%fx9sCIGn;OL z-p_U0=m;UYwFi1XatyJayWGkjXPRF<&KAvv)2{S#8nb(xPZa>z5IFD+NQU*ytQ3(Q zbXiU>XpOR>&xseQKD%;(!O0^MHb1tYK<&R`Zp+Q5*#p{i^T}N*T=xmt)P@Y>yXDF?zKm|8-`2C-VgVb@*2=8GlZ^_nd7I#Xy zd+reL`%bzDFGJKBx8W4j+*f9BP?$E#`G)Y&RdaygP8up$hk*xwNp^MxH_=)iy1)T4 zZ?Gc3k(1a-Y}^}1hQ^zdDbK^SVj@2`-4D<0`KhCZ2+mc_->0-i4^)^9(dIk3?vX}M zwgq_|SPm3j5i$>cCE<)r&qP#8Y=*K>BYuO#Xl7k?|Slcu(WUSS2q0fv{(~NUgl` zQrp|mH--7uQzuO~-L!W!ccx--AuYFSyF`~jJg&0X_mcf)17{0HuM8Az9=x3TT=_-c zLzH~gBfj@cW|bcBT_uu7aZH&^pT6>Oznr-nI~XwJqMN*=lcUWZAg|NK$7!+obl3m5 zJ7H49E^_kIWo-S|hHFbkF#6x2tK0&SbYr09VE}!w-YwtGGV+>Y`$zQGR;>@mpHBkk z(EAyu+rWlZLyJS7 zghlSWM%CS~GUw`FXJ0SD1_G8ha`G=yR|t!s?D%xnMO5n*NQln z+zaFYfI<}QdL%?t!X;wp!;&NC@I!pr)3ar@PW6&Mb=6hIAT`5XYAWG<^}^;qV1}CL z1S}^U2LwU2tMWDRO$;Q80!-r||1GmHPI&8x{HrhItl z*F4Ng;{Pd`S2ZMETRXdYT8)|*)JcFvhh6Y(?_lRc*|L~<`==z#`PF4+HqBX9AO`F- z3)9E92R8JsJXlaF1f~zrYDbm2?lOh=tH$)t2A~69|6dLy>A_wnoS3Uaq|Dw<8J=BP z_Kkt2@GCS;2Fhf5T;xq=;j@#CuZ9LPEr)*+MtS9`OF_|Wk^sPK;m7-RtGygZ*CbcJ z1Ou6r>UsR+si%rx+W|>n`{LN^AR>d;!HeF(qQ&VYSWUr%aG6XY-YQjUbv16Wbb@+v zpptjW0(d`hLq|J;)m-%7X|c9SkblpOJ86pp0&KBmHd!Rr{zni0p<@m#T&faF@J#^7 z1dyWlGQ)h{aE?V!GyR(CAK5aEUE?26^Scj^e!!+Y6m!h9%8~q)NeM6xIZJ={*wYbv zZ{l}A)J`cXwK{_Af!YOuT3$yqPM*nVDmrjFxa2eRp`jK%w)YJjSyE7>0;zEj{m}SX z`NynI#}RftqRu>fQ$V1gPryS^=*Db> zRc5{qW$*PBr1{4MH%#;Fc#EWHeO>r3qXhgr3edj+&ddhr$kRh{v^g+zWAs;zhHc#~ ziZ7$q2b^aWb278U(o*i5*>*|l5}eAg9n^l1bB9+h#=Yx{vgn;_!n#b68ljI9VqlsL;9xtD>b8MrQ8YnA{ze;!{9j=Xzk_Z_=q z{rC#YH{-Aj7%cz=h+wk2M5cB3Fu>MCd_%mCmI-gNImBM;H~P|A*8WC{F=KOHRz9Oqxp zmbka+4JX2>*FV$wdJ*H=SV?xweSZG@*Gq30Yv=@WBr`u<{pas0dQIGNQ-`bjGx;)t zTFZ^DE>4^}7Z$LHn?-K_;&g0Z%Gx@4N>!|G{Ai3)UJK0Zq{_-k@a$41un#=&JU*@Z zrAqowk_60hJ`aD%{z~3~&?;usbq>>4IbSImx{?$S83Gd!?rul)5vj3y%*jG6w$J|E zWUWGXXP~!tRi0h;X_wrITt?eERo+|;)D!Wfp%8ndQ(y7ERoZ#*!Xfi| z)&4~!u4M1XNEkZqYE)a=$njI1v~;${OOxeHHb-Yn+S{jGTE?7#K?l+wK0WYS+&Lob z3I!e`j!QcS!8!~UCWj2S_)mrbDliKP24w#l&zEiC0S#G}Mm(gG28$}-MTL|%7+&-8 zxKu=W++Rq;58dlt6>$~KNmbs^Ekym8ygJyw@O%sfd^$3t(R^p+h||QBT5DZCUn$$o zVXtX&{;@Ax50A}&iN8JbFKL4+Eq`^QYmt$H#d`*&kx3>I)YHjW)X~6$89gPVk&%U4 z;ftR8n~=Kgrz<6SIn#qpI@GaZK`I>G{z4*-5-bX@n`UQ<-dG@Vp-JlD+SAF3t|GGr zE%6e52oVbsEDV(sOHd#H(d?z$Z+ygJL^-f8zf>rNhF&h^pno~Hhbo2Ue_S@NRlhr7 zZSp{(+G^u!PH1ew&krn&xggbjcO0Xm)4^X(XR4oI9}#5A61dkmWVw0jQvZt({;t zmdK7*EBVFT>(QO_(KU(&*Mpb)#ur8XAGn0AG{;gjd^uWRDBg>-&+XcDK9+Yl(xiKv~3Z=(*-cJDNciK`*qjCf&Rpj!zr`6eTPn=#_U}Jw|X>j?o>{~}8EWtlh z@u@x1-ie0VF^&mYm34r-6V`Ph@if5yK$@Q3e-_jT|F{Ul0C@tZEAmzrRqt#B2%-}W z92m>>b*=r-UmQjgHoE(Aj&}?vLk=n&4{@QaR>40Nj5Uz?ZTiHbBYY|E1s)qE;4KE?La()za4U3?R(gogaM ziG|_nIZ#ZI7dnm)bwZMmw)EJc&MWHn@8w}bBGCq?aTXq3E&!V!$_2LS2caQz_sm{~ z5+FPZJwg5^P+`zbGj2;nK_>`(Ng)+0#bzs7*X(2Ze(RmSj;%xcnE|B0(%7D(QyncQ&F<4 zMU$)bM5M-@V;KL?WSVwx@Q0y4yp-P*W(sOdjU11X!Y9aQXA>&_?h?kw2O;n!nTJOz zQ3KnRDy(MQHPzzY3FWvGsn7WsQ6-|%@pDmHdx%n_?7j;OENes;dL!i?n&^U|7l%*G zDwLk;*YT<#-)#{)-FfS>1I~~1IASleg2(0^5K;vc6lhE~FKf7T_v!vttuD(^_c+@y zuExay4;kjrrvkxA161QcjQ`+fBYhCc7Pe)8?!QoWS@Sql`4B=uG&4lp8A-O)0UxnB zc*_6Y)pM=U!RpE0w0xm}s;Jw(6YkmEu|RR#(<3dRlJRXCA^bzTDv=JXj#TzR{qF#U z!^eYPya0brK-1^Ya!aHP8-^lsCjH<28I%o9;C30L`O4pml1x=hT*A^h#G|@h#_zKD z;x>v0ULihuD(9lsYH*_Nj%cQYv;dUg(X4pz17{;#GX)Lcal%qXZ?8r$3gPVm86aw> zm-N<`HHh_pNE4PccPh*sE&<2$(uC61_0L0PZtbs0B@k zGhS|E^*b}3i`O7<2QrB0ZuP;#7lK=EB#+t!YMS9}6NAGmuzixX5FN7j2-5aNJ$?R8DaM4@~jgce~ zyD~m+UN7Oll@1T=_1^Xm$x#=_Bjn1Upky@UIiBCpuNg58a^LDhLQF4s|8(53;AE4e-QQbzAg6lx*et2LDKwi$f#=$0int#VAY?kn{X0)? zbLN8Wt;+=HqP7N?#uQeG*lWlTOotHHT8Aeg9E9`pWj6rV6*t8D}2 zNN{NW$&=5P5o(tPN@~o{fKXiuLz6>R*RSO_bV)mWWj;>VFtqdV{iM;PiW^}2svY>* z^}SmVu9JqMUF7R)i9uzz*h9j}z2Ar!gKVL`P>kZ+ryHjiSJLGW8MXo) zAO!Li3pN%oZ-nYyG_M}U;5dqHb&nxLnA9kv5dPat&pR-UaPEbqHa?|b&F5P51 zIhVR|4!^ouXvzT(=B1;d#vb`BeLRON#TdY!G|lU8R8SBZAxX+bde^e|$@a4c|avWh5hlKuZ6?ViELo}4WyP!Jd^tEohLg7j*e}8x>Mj-SZfgR`I>qLQ5eq%JER4?8`24&K?X zPcw>Z>9dio0kady9DFk$BkFJKw3Stbw@uz_+eFgyh{S%+DQYxb?|V+Lq#H{xFKkGs zok{#IwjInMq^}oSG~Lk2!&PE?BJ8d*PNlHoKn%Ot$I>&Z+)bQjIXX&Bjj8_g5%c18 z;UGJf6`EQ|-uatv^V(~Fs5r!tXXwKK1y%p?7uA3Kl?~YEWxH%)#g_92G8x?hK+H_D zOG<*8CEf}tV5Ux z%(c*Cl$`ekl7_PbyNTBs+8#u>hq-z+gNRrj$%k`nBJbbql@PBDqJw!n$#tc8LYkpv z@bPc14QBQSm;o%vQECPpOr+vF?w+U7taUv<<^7UdxK(111G~PS>#G=2G0OcLy2B=( zM*Y8_k@FezIB?);D70nk)w@~|ko=}vSD)PA@k=e#%L6oYC0bx|Crmzc-((b*KkeH^ zA#dM76BeXwn!^ixANxBWrYbsS?*eQCT07c!%2YBa7E}$t$_ zW{f4SBZj^G{0l?{mV$Q6PUy&7GE@8Mi0~aSue^Oy{?Zox;a6e`)%)AO26k(MLdb4Av}o5$+sD=>`p%&W+ZEO301$yWPFGQ~^U==HFe|@Jjvnh(Ug02a z9fMX_+LV_Wil5o0HPy@f!xYbA{ud8{<`$j)elUy9cIo~)wx@6$>O%8!+6Epu+yUgw z3V-veibnP6_T%WvGEfHJPvH9xQK8ha$IlmD%T_Y zd&S~txU`L92-S4oyc!j&6g0Gl*=$3V77FV=%wTZC;0kD!l#R^=goo9qYBY@vfP*v| z&HcDaDc@O52~W4!IobGAK6BA>@uN_V)Zlb-lC&! z3w6Rx<|NR(-q94Ea4aHVhHg7U@%Keenu6C|uvGja(+jp|M;4>swIXrtZUX=p4x3O1 zhxryaBiH&MO>=xBJFP_(L_CCb={sXs7>_CzK>M57e)?Z9z!7#dt>pMX5dcpO40#b1 zsb%pI_+_!3=U6FCW#8!-Ewl=DtS(OQr!@Lte?CS^b$~d`_^o4qE4ua>Rr%S0Ly4;V zNKqK-T)XYWi^yf$NA^@iHSSWHSICFs5V^QpsoBk=m~}zt?(#U}%;0cQ;UPee=DN#W zgbBf2kAGKk?Hp46RApQc5zo8czLwxce~?mPj`1pTk^e*s(`ebG`NBqn(ap6`7+W2y zgM`H@fV-1B8-VRmVUEo!tZeSv_E@k#ZkgPf`}+eIJg@*jKp*jPnS%_|*b659Q$eEq zTa#=EA_MGe0A)n_7tkhRsrQ|sG(!I`=&6(nGqpNC20=@1u3cNbhdm-?Tq7u${MhRge+2GN?!QwWo1ZYt^f7w653;f%-*w zG?=XcP_PP6bJ-zKMS6NT78hn3-@>+NDu+QhrP*pVuvq-Q|Km}9EuO~n|6nfU zlRJM@cUf{e$IL=i>i8!L z1>q+g6QsJstYH;`cawP*0FyH&-OMNEGSMAXmmA^X&Ih}L=P4p$=bXTstdi^Nqu(n_ z+oncx-Ai;(=VUUKNT~7QgTrX%LfC8#?_cY$Ipq zYoU#i`U>;$(QtwPaS^?yyct3x0W|;dA!anL`$?o;6J0B3y5YmJ)0$9F&Q*_Kjg+Fw z@`L$83}?Cib|O%nHVZ8tX{lZ&NzT$co~hrLgg*f0A%3->B;X5CBOv*uXM02#qkcV2Ec4|4WmIU_grB z3rk)W(MJa4ju)v&aDl(U9>G+hJYl1Y_hX~&c5&sjKO=4F zDJHBz6BhVUU(p)36;^ripcb_|DN@t(RQ_vt*5KB}o6Pa#ZQuivYyvUmAsOQ*!gZ(5 z9%zF8gZx`9c)BWuOt?k7_<~?a zGea63390fHOg^q3O!T1#fHv6KjA;S#zQb0h z_={%`wmR{6*}r*_VJiX0p)pjjCZvX^Zm+ltLDtx09MO0TrA7w#P53gjEZy00qekz= zDp#Ctji~u&3%#(bx5wlMpX38A(KiGdtcS#?PES#qD|||1*#}9;T(EgA1Z@f>&)Zctj&Uc~|WdSwPOVL3}TUL5T3N2~lJVF1l9_0+sEf52mkFtx5OTgrzu zX{68nnlAJe)$N`4!*Rp6pIYv;nQmY#;iG4k)vPk825KT@oBMq3`BYvMF%0d-Ck@QR zXG5gFw#NlxDf=M?jzm=}_SDF%(dVp>?}#fMN2 zQw!k!naI04w)axtU?B1ARZuqTCUQ?$3X8TCH}Zj=CjEej>8uye{E^U&<0x+w-J&h? zE4}6<;k5ki`iUB}omWx&cs)yB73E@NCXw@#>R%yafBt%TkiQC^Q{^B($ z&ObJLqWEaZniKFGV49^yk@XC3JL#aA67;v{`wSC09K|9BuoJ_wWyufgyrSS zo7e(e%w;#0e>ySnzpYaIbqzoU9sd)0Ca^o66{7FX5gE zjnZm~iv$ZUD|7Yp7;!3D9w6@rlQ4*q3cNgRC#>UW;#qS465ETaUwrF}wCFuFCRnvJ zSm5Lr(L5<{Dm-(9>X-4q+iu3?8h?vfrvEc(%pr!Tci0w~*j9FcP#{$Lt}EZS_Krd`?E#{Nn^LGRkV|=2AE3UmHC(v_Ap~MdI}EX_=ctO z4rwYk0GBXTKzUh<$PmxS#+JAo@^ReY%l*Aty)f?sWlo(*ijL*!$ybEV$a!$=j7lUi zZ6SgR4FViD6;8wT>E8iLgJFc>w_GkDYFt2*ZZ^#a0)-#;Fs6Ym$wNU_cv%n*6ebGN z?Z&I@rgH0jbYuBkCTTzl^(8qXNsoO6r`tj1@L2f_QN}?IV(KrBKxIj2D|7F;x&~Hn zs`i2|v9bxv%~ar_tUt(k?^|-DC)_^UtL!R}pAOHEP{enVVMY}mnG~l9bKWAA!jhv+ z?UA8iH{U4!{Zjn^zYGyOk*+njvzv!Qht$E`pC>duMfRH~eeYcmSl$5wr4o%9b=}GD z>$Lko!YX2X*ZKTxf42HYDF;gx!*H2{* zx@?NXq3cigdmlAgI3M&KjuGB|`ZG$np}MofMs~yqcrYbPKbEXRRTF{s?k3*ie#<8m2e$c$MKrJH z&XvxkfBm;NsQpK7unVnI8#Ju`95sLo6*d-{#EUYs-)>w13!h)hG*fGFv}Q`@&05n5 zt`9g{WsP$n z*jw#e6XA9-`5Y`Kh5x~l_5J1NPs4_bp@ZQ3$+^T z?@B?asbF2F_)d9q_5~3+FWx06n3=7w>mx>KQ7|-V&Om3YKww3vjj|{)O9WjDzPhfp zsltI-!mGFV)wNR!Ubg#;wuQBA`E}FgQR^!=tA_LdFIcsX@kIHNoc)dag3~@)TmwDT zYO#Q?n3(M~Tr@FNnAfUGvLR$s_G{{hsQ1s;NfID#aC@1QwD#m!*!)pmK!6bVoBazCH~`x)TA@62jdI$Ghv){w)p%$J>#F zUi+5t&caVu1Q}K}XUfG?C>y}?O2)x|Cp9Kxp@SH=SnBKhMpP+UCxnRxM=B-tmlG|F zzjXiQFXRx|TcBW}8*H_E_@RR`fL(|LCuZ0`-s0JbMSSA*pGNWAO3$+D zMxaSK7|lIa1gZ*K#YIdPX#a^$seJYG{nF=EuOWYtT;lCsEzasAbK{;5Cyz5+b-&97 zL;xVI8EsU6_rSoAm3J)6Ac%^&fPk7{9)?FI(+UTBvl`9vmzlM)%fYsqB|E z)0eerRjXyhPd(85iZU;DX!=@XfW1q*mX=G+UzeYGhF|mjKK_&M9}PF6I}{!R+P;JK>3>8$yt9|0?lR_ID!JMIst32F^3t!5C2gkEJ5K^k`}%XjtBNV z0Q2Dlvg9cB>Q}Oo-0L{}+!!I*b#*nhpbIxU{mjhfKs-T|5876ZcNH9M$Kae zaY81w<$v!(eAzkLo~B%axePg#@k%B27KfH@JPY(|a6&D20icheM6-mFr6vabA3o%L z1)TQ&;>U40v`xM$<9X1?ns2LUdjqyw_e)&wP)0+m-4WiK&)6pM9<>@C&)xO%U-oTz zzJiEk-xxhd>$UqRdr=IQce1xXI-%{NA$=i&JTC#B|N1no7#viHJU;+VG7lP0PXCkh zKL~^E%(cJhBUR`CDNtAo@b3~&&w9gn3LhO6R}?KkiH6mqhlau+He%ClzEI@YP)1#r zrBvgZU(N7m{8VAzY3An+uERK#QWWwZYqYNpB(S@`nx0e`$gpO1&s`4ym!YjlK18m7 z_v;(MiRqzfzb5jOvQ{c&PgCN~6FJm@EFJNY?N0$jK5+CK?k zSbAv4@;R#D&JQm(;kDnhSZY?Ic=ml^{LtBeU6hiV?4SvJZ=$pn1N1%Kk`jgSc|tPN zD-SppsblsQ`pzbT3knGeISpaH5-W!cEyMD@EBV`7+WH~4^jDul{iESK{9_ne;P~SN{)Pmhb_*wuGVUmzOB|xtaG1&t%C(FwWvbVsB$xd zKq1mYYs~RAnVHP;`Gd$%n@9uOcA*xeXR_j8M>4VBZo}`H=HhDS;I?y$bXh|%W%PmJZ436Nwk^@|vAf7g$iN^7dhQH0-z8yEg^+3NbO*8Rzmq;4qH zJ-L9%%!6`jI4>=50_X3B zOviE_jTW-FOyn=c`3YCp=L($p#|a8OvMVaCMy zKcafcJo6$${=bVX-FXwrc4eO=$x|ii`&Sb(TK+ zlX*SHYSGQiwVqZqq;0si&IYde7bsD?v)tY*W^$!R8gECq?)nRml@}vZQ5De)CAS+) zFeTVe?O%a|!+f(st*MDm_L^dhxXnZ_SV7JXR>Q@@&5W-_nQ~MoU21_`XBim?k1LSE zuS2iyDaFG2=JlO)Dg!qioaVT67*?p}cFwCyl^6yV1WSNzu=|NQ%w>+p&v6WkCX#*8 zi*NqfF>!Ot#e;{|J9JFc(eR}F2_Qo*F6jmB4Kw(ftWci+sp z{-G#V>h6w(!_#yY6)9E#c*md$4wfeIuU`JcKUgG5Z8%98{69f6m>0cp!lC|WACdK= z-a=zbK5!Up_(kO_1}pSg-Sx>wSh1+_eihow2u)<4aG&cjRB*BP53RCG1-@SM8?3)b z?eVbU0%?$ut#zO66n;{coYoh1*?;28EBHA5M*?_foMoNbakWu(OX-&D^ z`uBda#FK@^96Ne(c{XgC)i5i1|6AgB?kSRQGYy~Np$O*msmQCSdzaQ9vLm$pow*iB zkbmJ(X{s@c*9~-c;n%@Ke&8;>9>+9l>4b>}yBTo#UnFJ3;_m*d?c*q{j=T2s?-Q~1 z_u%L(y=Zb6KbC(hqKOyX%%x(|W=_|2{0PM1c-{K&c)IIHEI+!uKS;{~iOv`MnAuIqe#gs&v3K*9JzlvV zn21I%@`t##L~D3auHSj$fw5gX`&8k&@@Cnq6v>zD!=W`%UldkwoyGyV-3=|^L z=;fXbz?&Vf?5JK+}W*-UDtnz*aHkE*z0fK*1c4?A^jmN)`xYLvtcm(IHK zm#mH7p!a+oc`)#OUe$(}c5CbeFl*+-M~e|v!7e@kH0tfgn6AfB&n}pH?MN_iXa8pP zuKXW+i1gIyFk^JHhr_iMvmqQOiw({zJnkN+deeD0bm}9g8m35QE2l#+ig-5oC5z6E zS+jM$zkA%Rq!|6uvps%7f(yXY%9vKQ*&!NjmonB+5JA!8iSEUPg5a_;y^Hu>JRhC#B|h3 zvAj%~CW?h=oh&Xkt{DvzhG`EH7w{K`u`pEFAG!vce6YRB|34svf$z8u7giTY74|(W z0P;}Zk+n?wOi-&Riok?Op8iwq4_%$yL&}mzyM*>HuFuKLhLU+Fg{dX$KAvtN^p+XY z*P+^cTt*$&C8q^N@fh=Yoc1Q3&t{j-=h0qyNgP&&vzMu~UThVHXmfcAcoqfD+H85+ zUb)#n{l@tAlCnJsGZ4oVGlwy}`r!_L`gbTHM{->pTl<%oeDCH695sC>xc|f(S>x=p zgX@dkyRZNNh5ZNEFd$3Mo;S%dZ-}q$AQt#$FtApbuvY7zIU}vVc z;)FC~jIe0qHsJK@C84!?>#v6oBK;y~=W>=I1B~&Nh)eA#0C-I$rL{Z)G5C`4`Xx(_ z4$kV5IfcOE-#;B$KIK}$O`hhKuq zp_8e*A{6F-CO1d>Xj+iEY7U_h13w4fq4)jZpS*Gc@8Aas+ja>NxE1#&b>>FX#c0KkE4P*lUV_j7J#KlG%q71elmeS7C35Bp>?2x4`i zWfC^Xd1SB)MUHU>?2ssGK#Dd@3V_(j`zrgax;I!*^O&Kp7XGFRG(rT<&Sy9{*F*Ta zTi~=G*^BV+C{7T6Y|9^+7xfG-c$NBmP}->vDkx|>=(v-QQ8Xl3n9ejbOe(_&{Yv0z zs>6p4hh*p)rO1CiTOSI&KRuZj9t@tW58NfoU768#o`4v6*CF6}7^no2+jXC%1#Lvf zFKc+-?FZc2Nv=iV^}sPpInX^CF#o7hDN;D*(BEX?CZqdBFZ3OZlU)M#pQWg(R(4$$ z>26zrz4I3)0&D^PPu0H{UuivkXpldzUj!Ab3HTgiYBOTQ%gUDo>@rAB%THwbMTSpQ z)?sP2|7Emfs(NWrcDwClgg=B*&c0zX&I`A%QeL%E(2BvvZ^LOcP5!JU3xm0Rp|Vf3 zU0)*oYZyywjU5^z)=*R{qXit<%ywJJ5*k&yumdIu5oL%_VojO`JTb`q(952AHS!8f z`qop;0eK_9vJTXMb?8wuzhzYNe3_l@nm2jWXqhT?^?*<=#~mpgJ=yK_nV5ED^TwS1fe3IsGxnOyY zMIX^eJa$)eX$h@Q0_s+y?s~8SS*=f+Qc|3qEpy*c&mW+|{j1#}xL(43r2hSz#-Uzr z2LQon#)Q|rv_>F*;?o_ilu^aDxiqqER-$@OeJA1_?l|Y}Z=0X4qvWls9N<20&vyP^ zT0s9xYGF`UW52xUcC_&6`V;I;cSrL>q@Rx+ehb(M+Aj$>EX~(=)Y>h#0{heqBgD`*>68v$#R_8D)tjr>G+e*MPw}03+CmbJ&5GlDN}teAxCO>#RA9SD zav^E+&qXCwb@gycTrwSnjl%Fks|H`@j?yhJ4~A*nOc7F8sZUZF)lX)Lc;OK{4(zpX z_Rp+c`vAaV>nHv4mc^UH1wmlTw=Ds5DC`}&KPN^pGUM}yf$K;x1+j>yCHyZ)WPGMA z9)6lz&wPaqMN|(&mpni}tHK2Qtv*-{;Dtw$Ajzq}KelOb_1Anu;=+Qh9eSEx696Y1t1@)`Y##y;Y&f=EZCzSyYq4Bx1P%c{ zMZv8PbZ!b~Qg4g;b;1OPS;O>y3G2nnjqwt}Dg@sJ;)OK#?#-3X)VqLy}3OCVgQ1 zlKeH-BYNkQkY%&KionjQ_lU%lT^n4c?~MPz(T1&q!_prg`c}c0H&L2*y91=(Ibr3I z!u}MGT9)iNaZMURYn+|UmmjYEO=_3mX5Aign*p()Vn3SfHY6;v7dFAWrp^YLzlezR zw539PL%7hShJanue<2c)n(JOMWJEfAyRRy3IT2SwI2( zB4-L$U88jg-zklC1vdSY`js(X_;Vmz(RVuPPB2E(8!X9)!%q8Ji(D+zK1-0r#zvkd zZ$6p$A89C|BVk`4HWeRjfe#+zyFRaddGx+Zu?x0 zahv{4H4O6wUxhB1RaUQo(Xl%Wx20B|f2n7{BSXUKaAKPeB_UnGZ^ody#I9B&$AkBx z{0ADpF=We8CW)zI0#hg&;PIH$m6n!f3gSJJ6^2P@tf7re0D=WC!&Rw3sL|Ns$J{R} z#Aa~@>h&d2(hP~YLcpbCxaKRr1J4~;-(faC??32*r2>x zGTrX!HfsrYCF4C2@sSZICAT!>vZm&h4AKv_UcYiG-y0WY}tX0?^ z?bEY?W}K&-_!Zj)k@)z#Di#UgFSdU=lztlmGq?EgpBTgGJ-Edjp|-6<*E-QC@d zG)Q-McO#8-cS(tKcb6a_(p}QhbvGW*``&wxANauL_qb-yvu4ej_z#$4mk;5*p7*4T zcdWz71@EYpy4g8uM8;Ni*IVHevMu#(#EE2~!T7h;cX!9X6(ZVuwz_!X?QGSPmg z)ZEqijZk5EL$J@*N`Lx0s>?~n*)IV`I7pbjI|y!Wb#*k*0m-j;$0Y=u;9Q8p$qdc< zT|!VOPKTa5Seg`82rA2^YSioWi-SYwbJ_Ld4_4PZozUD49k-u7$o59ldwX{+amaeW zEKNmzz5AAPZf<0f-X13)iQ?1)Xh2#dtK@&2#Q>^jG-o}&FbQso{83`27QM1V{epy# z@bI@0<)D%jNUQI7C&8r48a5snXzWj}ZjD<^6M#R-kgP!w6-f<>3IskF;>DyZl$LX( zDiXQ?nF~L=_+1fdFwF0F^|Xetwmhv~?J{H(>M?Ms3!hieP;``6vx4Ww%j)0tY4hp@!uC6O>o{ z*Q+IJD2n;&r3_ZmFAQl6OPk-M%FT1WFY(;O-g35ri?n!a(t*F;>)R&qR!R)h6Nssg zSpUYbjUlHqP>tqIKJWE7>nX?65vVnwrAC-Etu#nW_P45|s!CxDXl|-xxcYr3FCVC6 z;qjaA+lD0N>ds8)kX+6W!(eWT+fV=t($x$@nQM`zqi%U@f{$D%leF+B{$)3^evI9s z%u_oKOEMB%S!bhEdwXf}^j3HSST`bAULz2sgAN2TXd7>_EqXabI7tWK2?*l(?i8lY zCDCI~q+ZNG=XW2vVn{(rgAQ40wCBiTtfuKh*P2NTbH^j+Oi|ZWOg#~`w))=VE8jfs z{ldXurVi?2-Kv24#w$Fpif|pj{pl(dh6@V3rQE*A307)ZQ$$&TyYHMByuiWLx;G9E zMP}rBvzrG8uD+i<%I?0872oXf?JZ{u$J~9ShUEk#16N^0&B&_dvPs-~<0jY?mc7sTW5D}x`eCKghkyus7yhVumCYWh^CfS-UHWrg<9=}+aTF&V7D@m#mi8~1>(6!Nol^z+3@IMdWp3PaI+gN=VYbY{y~Zbnq%Ma`=&Sx+z-*- zr@*(wpOi?}8`SM@U;h7T`2q|JhRr&@^-REaOco4mga=n%S>6pp+Kb=23w@7VT1N1L z--gZjuhKrD?$-WtOZ(9&i2LY)MnB^l+416%?b%aIaSJ#&mXH~O89BpmZ>~ZkA zocVTxAYQ@wMvJNv+#80o8}ZxzOa4`YYR~POPQ&@dA)Um6v_rJ=f{*V%-+UVhpb_lG zXW9A0&?JzJL1JJ$zj|4CbJ z3Myy=&;sCP0_eH7HjOtg6X=tg5Hp08>;ayDy$Ey*1xEFJm3|kesuxAB%N}|c5V^&r zvnyhYRINlxPEs4B3UB&8X1~HOR<={2gU_l(pAwVZ+L((rs|p*;$XI5hWqjMX$anzi(U`+C2P zo5VI0;q+F6DkW*GqiKVLV+hyHniJ`4SJlwEVm)%=>OZ5Gg}m0jMpvXZ9h-k58-A~N zwaWBZmq>srtH{()T zeSTIYzm;^7xr5P`+2oZWBAncpOPS!l+~tTIOL%7S3pe|)`_Cv8&?6u;sPCD{@k@RC8S14@zlNT5|jCdl{e7M!md1i`FK2w16~B=U)g9)ng;Tw&5-(s^KrxdVM$LjnP5^hL7Xhrbtiz>eOLw*3+zX%0{Z=ZmeOC{P%|Ac11uaEh?tP_8av;J<@-sY9r-EmPV^fVJ7) zo&dw?N$D5kN@EO?UU}aBrL-Wpo4w}htG}1Gka?sf1Hzf?@OpoZR^UOBD>LWtO9w2X z+2Rovxd7?6feB7q;Y{^7%8$_x$Qi7U0@TtDRT5@tIW6RwDUkV0+8}0o957?Pp%SWw z(eTu`dLNyh&=cyzX{2_oF*a2^8$3@ln?FZQb%|ru_Qku-LFJ?^pw|VyGcABCE!A=z z0W22ihX9y)Z$<`Ah6}`L1l(pBC(LLf|C7lgn!KI{k6FSilT6|;_Q&3Xv}rpilqaW9 z){)M@5iyF;4o~Tq_AD*>Eo*QX-L&vtO8XcX@#%{+-50?$z&*$~ve%T$-0fdmWm(VC zy#;s3TR?k`@F3~>X>R`I3DoJWKAyTz|L45t5{)w*MbXV7tmV$>3oyStpL$oyQg^m8 z5d0yK0m0!5|M_q?ZE*esZ8KsBB2xoX1Nchi-o4ZI@CXbWraxDBvX!L!R&}fIjymnASsaJrnQ;gJ!96!s3g9-0@$h0&W$aZR?9{ zntHzX3o_33S87zkud6-BBhM;x@fFgzD2MEiuhkLWp%s+dC%?2PK2_$IQ%h|{D<{P_ z6cl#*Ww0$(eDP#--q8;E3_k_xdwtJv`vMy%9KXa%lJ^DQt}<}wq0L*MauOVwcR2h!l=OxIYWzQn;1iA^!3~0`pzrYtI}hj1N7vwF5az!{c>}{ za(!GR0dh6`AWbH7a>pfq6P{>A12#{w)HeYJ`1qir6+ROi0#-Dz9b)0I2-_((-D z(bt)69;z5mfp0by!e$Nd)}w(qb;IxdlM1>L^NqZnV*3C1ulnIsNvM#Q!7^@eI-xC>8qXs-RFFZce`Y2^$xRPOsr$W(y}9& zEy+dY63ENbVOeN7GEvoWYQ$M=#FH7-EtaTrIp-LU}Jai@UaYBES__M0{g;ae z*v|h0ej-~fgqm;57nZpHLk_yS*Iybq`vCL)QVTtd-QB8}{$o)A+fLl6zwjIlFf5DV zhb3(}c&Tq7@OeZh<_Lc(QFf9wM0g9)t|48g$Q(i~EXB`6Dc{YAALIVej{Ahh22)^J zycs|yM5SVItHh}c`7EjO`b}#8Y#3yL} z>8U~~j{5|%*#XD%ltnnMRC~1rVN`lA`i4$8}cP=jXEsi`q z*}cP&&iFkb5b$Grz!8M)P2)gi2Uy~qa_~L2SdKU3C_1G+ zEzXSxe{ryl6{ZS95iJJekcAo~n1;2P#-xBRg{j&pW=4ClfMxy^h~k^HxJK-g_L_8m zm+e~gEFZxKuYziXb4SUfR=i(M#o}tVmC@!RPGW7h!o*tdi@H~VR5@_9W!6B4{qz@G z4)OeTN9FVCg|uPg$gUf<-K>-8o!uunX&3G$lZw@M)d=Zc%;QXZ`PwF*HEbC*V~NC0 z|J2<{fkG<{Hb0f>4_d?Y&+X7Y(TSFbl4UsGP(Tf7!MOxAd=E{_^Zo3P9NKwK9bOe( zO)VhLAouvbE=s5gf~&orE&Yh+ej2=xu&^%{&hzR^^_d%CnyB~sM1 z;ZOr=)d@5>ObA-&==x1_tn zQmE0Ee#32!yMM~wBa%x|DI(W|!FwLOcn1(0{>Z?8-FNdJZpQ0VQQXW@EU_=Cu=#m> z-bO6Z1rw0GD9XXSMA)#MSQS0IaOPB5<&4+CXBoyR;?@( zK_FJ5>L(@I5|`_@=l#iFL5yh6J=p84^F74M(KaCGml)q(jjWE32W@laFe9z6}} zD2}Iuo2UP(tp7stfFs~-cDNQB;wLJgCtVWtO%$Qw9-}a3onFdh^!;B+n#`O&EceuP zzl<_6cC)j*z8h`hXu{(mBV|wRo_Lf#EK-3T3>_}CTC8ln;5PVZjG8IAb*_?m@YhTC zn6$y&>eXVYFct?SH(6jXZLwi_>z`S2^X|m5CTc|WeT5m54!LTV5b9{vH@$;aVbD{|A@% z|F@bQ!4uf4H>hpEG$?$cS(FS|zLMPjumZUEUC6$kjyo(4^TV*8!555nv~`Zv__?GH zPVu#;FpP+<)V#XLfpYqq1UH%Nx3@#!u)Ns3LhqIq`;mM^w{?A}_`c9ASdMc9RqL6@ zB@A<$Hz|-)lpJ20bh3ZbArgk*-DRm>LwX$NF;}JPs>xPTQA6JIA^yOt^^Wxn>x@RO zv5jk>s@kXB@r-)oA&LdzNmh%zz5zzp(U^IaETNX?&gDP*8&j@}zj_gIEPD2Tx-y9F z3qQ^%nuV`>?QKLdnTYBX7y9yC$i6(siVsJ%#n|h9TK-WTtz` z3=LsrQ{nMyAA(Q5Pt9u;Lsw<^DGP?GDe&vJthp=r zU{I%UzP+zn?Gn6Dmo(B7W;dZMILuT@e%iQoXz{d0$ZzV)wH^6In{JHi<$!eds z9Gi1jDj*y-eeoq>H4QWR?a=Y%*YjcYRUGcZO&FO&xSEx0W#g@J(0qC}+>RgV-A<;e zYaoNNN6z^v-^MB7*;^=3o);apPl7G*Uzo^rgZhA0gZbE&hHn)%Hyn=N@gNDkhW01& zGL>DPf#hMdwI-3*pX5TUba51CQ$2=Q_b<6y9~^iOBkAmJZiWVW$A8;o=8{-R`W7>n z$0crtc{SLxllvJ01OQaq6W<`cp2wQ0 zD2zsQ5Zj5&*!um^^}_0NRgyPiy~MZ-)$PnN+fc?xg{PUSisCJ7)uuke3}60r&vj>i z+h4rxn@0XQpdb#$-yF)YBj;}#2^hgbQSW$X>g@P+=j-!c*)kgctZjuGLT@CFzAkT) zUy&t#g2d(@)Fg_WsI9!)SIv0m?g#3p{$ipGkK=jfHuJj$eay|>oQt1recK(lvsFub zn7?yz>jdh(E7L!ao2KKkbc;gx9mR_V6v=aW_tXM_KpdT6 zuqT`#GS2tU0l#DiY_JEP+gD`mb-`n}O<-=3udLA9YB_hL`Ogh*LPAW5Zcuoy?#2ttF)f?f zv`nwp-qxun`Oh{)-;RCEb!OJd;Ch_*D#1HJW%EY}e&!^t4m}@-jr>6xt&?Yc7CC zY{&58tU7ix^T+CyW=!6z`$B$(3S^alQ{SByb%aWl`c36Rm(--i)3RdZ9R&gooK!k+ z#3LjGT_IoZ#D*3Q`1H-9zFo18A%s&jwTk zH-7H~AsFCmnhbEa26D#%%Lk%_Z)m-LQ9?w(7ibhglqo<3xSem(@xIOlk>9-r?NEL8 z-$+yV4S6pnGu`W4Z3fCLmhW3mDKZ<7k2#L^;?$oQsA<^Z=`)2Uu>qw=Y@Fd?%}<}a zdnNvyc14YF#}KbJ`_NFKqPQLazjM$moq<8Z4{JGy@e@v(xQoEF{rqgBFa@RtI_8HV z9oLsB{;MYxV@)Wf^Yfg6-GlRSNQEi&P_ zQ9sVuxWpgl4A$b`8Ahn+D$AYrsWd@7fCRv-hreh&z<2^_+nG?7mN&Uz4EBv>n$ll* zP4mTS4!#tiCbNnlL$06CdeodI4=O!F^{C;_&q-r-#eo;?s{Hc&Dec{~lY8)WVvl^P zwwvTL=|LOaD6SQlMJUd6)VNcqQLSQ+9wyYvvEvsase&yDEpY-$VH*+88tF|&#^Y@U zy^}~*py5@pi}yyW?1MdN-B7Y3tzzA=X10S0&7|ppY$#N)h_`;=r!aLe!PhpB_q2A; z+fG51WQC0^;CNdy=oMcMfv<6!C^Xp*ObG5ul&MAz#=68$e)N*kYJ)IX8f?cu?MEv^ zXAKYLw&}Xela3pmhr%s<#NF3MkIv_%#5EWiliK$s9;5wH@yZsT2(ZulYgku)i=}!r zpJR@2sKKqTs^Yp)pi<@&O0JjrUxV1jvs_2NGih6}dE43)IAi-oU@vyd6pfJ25R&JG+0`gFAeY4rU6 zl;!09CFuS2Jb|kKZX&c8TOc4F#QpSeElDSgPL0aPBN@p)^wAH0cz<}~H8FHZ1`vtf z*&jyr7F?`LQ6?V$XjD{$5#wF^^(%?2D;4ii9D!lg%Ouid)P7xmJ!+bZXM)?R>C;s4 zoB~%XT2E2XRXC=Kz!wW@W-PD^?OsMKjL5E%CXe;=qMx!!x7I&fMTOBIMNm>~GPM1# z{UzX@eNx?j)S(C6_Dru|3d!q4&EyEuP(>Q_QVSh9{dpOfZ1D|^Ay6tLz zTRlKI{$GF|(<_6XF?ZxwJtWcsUGU&aOe(2#7LRA|d#Nj31RLnKNx~e4+5r!x9R1c? zx0yG6%d6qy%R(~0eInSaiqvh2(Dk^AsW)Pu8oVqSs}QfXK0Wqf2VWf2>v#Pw>dnen$+n$B=srmvB}%~tyfd`Msm63*elyGZJF6&+nsrNJC;P_D zB;++<{hFeukpo-&okGDDMBPGe-2+B56F@1dj;a{5tZh-j4p$ZD#b@m-FJT znHFK7>6hGcOxt18xw`n6!f+ZSyg8OuH^+4AG^euB#m{i=3APrT-Exvxi80?6^sobW z%)_#QfbNua2VApqDw$FrF5fj&nooQ7JmqtNMpEy>XrYa%B+Vcnj6D)tqg#G#DqRqz z|MF-13OARSd`7BIvw$@KRoeIoH59@p3An8S;Y?;z65$Z6rD(QI)z<8XsmqNQPv*Q<&yUSS0NJ9qzjI*#$rtB+(W&R;Fk zdTSfLDaP)vE1Z}2hhEuvtc5k@Ta_}d?BVDJ2Zy+t=S|=V!sOiUYMHZFxIz7hFOJ^r4So9UH7wVPP z1g%w73R`^L-c6gg%49|8RjS2f^%l?*U=07F^MK6<4w!m#+Vn^&Q2N`L0U*nauhn7& zz3mujhPsnt*`ks2D8F>04V`rtPk!PMkz~=y{%BMM|6?wlQwjB(!V&uW$B6!n*Tnng z{LEaT48iAZaa7!BNrgiEG*COQVvl?0SnS#~eGe3^_Oo~Bi0pG?HW`zkK-Aa=@7$k* z8&_-Uc&$^)zvsluiv+JciXL&T%Y*$p)_)_)mZ#r#Qblw+_y&K?lGkDQQ%E%Ok^CnJ~Z zB`YD5$V1`R5v}38$7Q*ux7^R$)@rd+cvKG6$dtB{z%=5uc&O4|u{OcYyuKCu?1w%S4F@Pr148XL!2Hf2MU(vo@yvQ} zdN^$MRKg1I)Ck0o4wA0gp(bKIg3LoA+aB=P8q4E*TS+ogg+yqK(raD6&W1@v^OiW; z(zFs=m$n#(;rE2O_})kGfmkwqube4JRk8iLA$-Sd!(d{J-xrHl!%*XW{sgb_cZPgX zi9s!ZM8rro&#DysMUc?ff$ITcE-Shf0`aBcwhjZwn2 zXSqQWtvt5Xl}j^?y_xH@_~8jj=V(Q4Nk+jdhwvhQ4(h`%$JVJQ`GbzBzAqk{a}&z> zvIo^PhNk(T0!^KYXBPoUS;x9gzw=GPH32I_o2-QA4{CKfdPn6%;@%L+&guyug| zkCp4Sw!rJ0wfmLVr;tG4od8DjA4l(RvpXBZObQJJ_}@6+Ym5DFB!n%jomq&!q{_>8 zZtvLJV6QeJk+{KG4fA-2@O9ali5U2Mp@;5~GaqUkMeEn694!UO#&F*&QaAG8v%oEK!kee4R;lpmnn}hxEu_TkV3g^Yf9)$B0<%Ge zyyo^q7K>Qv$u2d=0WM7%T6s#l>zQd}f0>$Mj&6#!iaqvsI}v}8p--*K7=}1u1rmC9 z%a&@m-{Xl$E?u1Noi6u4M5rK$$Q_6@rZhp{F5eaI^UcfcODCtj{mc8V0OGyfE8hrY z10csv$nVMRxaWk7?;ile7S%hwx3jZ{?*-y4OP3Fx(a#`XIVYl{_swzHwcJ1$yaFh>!DyMyc7Qta469=mEB8{D#*b(|PRb z{vpfUsTfzcXyvfxhdqhV_+ARCbMj$}0k_t7os&ez+htWJ3O7kH1kK%ZA~xUg8PVBk z?0zBt35w!7El$7?k>4qbyC$gmXEy)x^!|Ed)flO?sGox00Y!~(0t7Vfi^M%1dTUa4 zgr007xKz4Z)qov`1}hL{IBoFp?x)cm5k@tgH9wyWw$mHh5yl_zmU(T%XKbqCTePs; zl43$V95oFOFz`)xcC+h~_-ig}GJb5o#YW1mn3tB}bgOT^RV(NoC*2!l_y|S+1I+0M zh_RhRV!6<8kfja|NuoNdVgHPN0`8l}f-ll6`3$E@Oa*`1OhdD`Wj60NcWR9BmsVwZ z3PYmp(@5+1=e4>wL!%;+`M*p$s#m5qsHu2bJ1OBU(o>5>goCFF$=+jz|k3o58i9uM|W^Z-4sM#{K2i{&aLi!c(BY_ZK1E4uXl3sj6B~Gk$O-?Eou01b%g2 zRe5DTJaq$W;p0J5e$y9gM7sv?Pt@?=icp&)P@V3a?(qFZ%|9h*xo`(M;foT?1e|*G zDT$Da>o5au4-gY(4`)S+)!){1H1_vpv?nwhXT3&0Sl`jI9<<DvpH@Pg$CK#b%7i=oMOWBye6>A4#c7U z2eO_?dAuoVeQ6a#G zt5%D`fP*C``4kBJ3E1#rUc+P5Q1Q*N$PPJqM8KZ-*G)>Bn@$4`V)ap&o!RArv8~C4 zyLJ=y46Y?Kp}HQ}>IK_$RNNfNI^v(Zbn(NMXIPGe2d&+vO0i3OCO){R{aF<2E_Lys z2%Q`-u{o(tW;{k>6Gz+?#-KAIE&ep4LVidV{uez9n3y*r-`g1hl!`NRzQ>3$1%nl) z!~|pkpF=D zQ>Us7>pSM}3Mzc(jO9|s5iNYZ5^}Gnr(iw6m|g~qaC#_cc`)wv6%ZH(F>L&OxsZTG zM*s~e2U-eyxGmexEERt<(Qv)YcJiMF8T2#qC4c6Xc)D%A+yykgu*@IbN=I(@s$Utx z+sCPgGMVUZH9s}lh$n238g3#HRRr!p20dr{p1YVA;?`dy2gfH8l}Ig}ob7i)bjE6N zo!h?{jb~gY8DB-@f4RH zZ$vuSo>x3q@>L)T@g>S%66Bj&{yCgLP`1vMgX-E)1{CERg0q1eegg&!_lw~;%{#NMSV;Zs7mYLnb1`Awv| zN_xBp1^vnj_SZI_jW*b;!(nS>(c42@4qJec!=unh|IGq|R@%axp6LT6#W~dusIw^&fS^Q`r0jd?aCs1R5fN|Qp3J$+Xqv(#%gxt23D&n zlKH=gJ*HQYc4*xEXLi(b4E)3*L-|=<2QsQ=s;m;e7Sk7cPoo3dmwdkI*a7PC!&QoN zj@3zL|FgO!Cx`Rw6UCz#CW42rZ+mVgnK%|&3=QmWSuW`L1(HmDIw(@koXVO6b}rJe z;Bz8pU4b!1x>DGSRsw`bG|#tDuZ(5h_g|E^`Abh#isx;!8*WQ=Dh=bNX(>L6cl)$u zy)PpbWMd4QfJ>4 zSEG>}1~6rTUtn)B7@%lcg&O={z}@p%EWw#@VHjJFKS;5pLl)|&2Lo}9Xl6daPd*S$ zIfB~Q<;TbGG;#ac{;{_g(U~!yOVGTJx>7ssHrpg)H0+Nk2l+fc%s8BVsvjnb03m|( z=<2OL*x}k?w21eFLWP|n8UitbM7Y}O5;75RW^y!Y5F6wi+>lr#R}YBf`wZJ{7hbdh zul?)f!*s#PgLBqCQ>}S?;#sA77V+0djyCg&Pe0_P5ss~>&_$hQ=h2ahr=7ww`^~s!ViP%P*iYhbC;=*rjT%i2I@%^IZ9s4Ld%_ipjqK4_-7{1GC z_d9S>!zvYGRZWWLOpG{~konx7&h@N4L{XIQ5 z9<0RngrL{rcilV__CskKf2>-}h)t%VQ7^YYhl6ZQf&FO?vLSfK1@MXU14!I)*PFYd zhD(0=tg28dPiB#c)9SN~2|>c&nx?8)ziRC^*}Vn~Z#3rB_S4MWDB9FoMf(v-U1VuD zKG!8hVZnU>F_occV|Mk>@JDb%M8|-$t!#h@?a@X+Z;lvXAOFkO1B^kkxePV@UxWtC zQ!Gt2)2ANtMEtn3(!_99X2-l2)IfCVDD)>x<>QO{&}py1&fco)EN2x2B~e~_(lHyg zjt_?04s#}{TtZxTusaA@oZhBGt?NNIn_W7!Q;%}+l|`#lffn{^`Yuo!KVhP|L144( zMqoXuOyneuGgj|sBgjRu!E`6;P7t<&`7U*zSa=SK{t)076v1TvQBxbJRiJG?i?DJC z4$L`HLo)VG4(w3O7g4J(#gVc4lm*%V!~upp{x47Oucd5wZ_gn)5`t-uo%9aY{Tea0 zYtu|I&otX5o{BBTXquVd>~>UdR&V8L910grT! zp=@DXxsO+iveC0yR4M*0&+OIA0sCptJ~0OAw0F4tTGHg^M(d7=&%pCba|oKOp?>A2m}q zlKO~kw!ha%silnPbpouJ@08^3DWh~hMC*;!-2*$%LTJ*{T@~jhGC?k5hjvyXNtQX3zn++-LAnT(j0eL z!((5=6$CO-Z&AZAOE9xkNjFHr)RkzS0khP{3d6kxYWjgg46yyIh~Y-08Gj=%LdRV^ z8^WYsd3;?KXf%x@*0Qpt(lMY{h(`A?toJc2{;I*vct3p<%kA23M3J>l4AAw%o7?={ zJxCbooN2O_3~s|%$2wjJ{!oBP5G=Vc=T}pIAYJcB#`FsPp{}Y)-1-IQFyL|T(}LR} z9gnbPWZA=xjy*oQJJqcjWp(BAuM6rh++_v+*w2L^8r#;6HH>%oqnw+mzq>mTYXB*vW+u6`eSOLaJ|d4BczX1_3r?rW%%LH`Ry z|3T~nvwalm5ha9?H>N;;>v2+kO7FV&TI;I7`oCJh-2X!KfZz~DDh>0@n=mL>-$x?7 zpd}k(Tf$i%tSl%SF)gdIKY_1csc5eL=G8~&fL+tHa&qTUnZ9H~b;_F6hGt?Ihzv=0%oi+n$6Zxv_9uSl56wE1e&;jcT;6lKMup2Z%ybGZ1sBB z_td+8QHZYn^zACfC6f8SG(Es#HkQ29Iu$kZ5qSFJ=^>bQEopK74~OQ1FA~3q5vDQK*?w+# z%9ldN)N^mQp?_(gzO+PoVw)Vy99+)bj_%jO~W5#0?6ownAkv<## zVR3G;A8y@tcK~y4GYLl6>g1?CxNP63HG2H4J;D-=B^_kS0_jk!vz2kW`;2Uxv|kx00$tHAelzMP)ZTP(h*?S(fd8;JA$ zBkaB1;okfb4RmrKI{gn!w7m3L*clJ6m zV6Y9BlA@fjdh@KghwC2GTCY!mw|>Je3uh1g3w(K_KwJPCNQbzwA*tpAVu8J|L*ov$ zC<>sl*hRY7pXHP#4Qi8*4-66>MqA{TBF*%PC<;Vey`R#dAUe8Cyk15ldFMC1Ib=@X zUwklnlrYtsTR~gs1M{%Hr(`fH^Yz=s`hxP8eyHEwY<*;0VuUzQ?86*&76<#z2D{fL;oy& zbnF|Kmv_lEiu@Q1lN)p^xR>3Zv3u*xY)=-+6Ospm?0e6{ zb*$Ll( z5CkI#0>XqY$V}-jE?^V*!rT!cEU%8?f3M;9fB4COGcfbR3GJnwx5oR}Ysh+jUKT4E zomvOtA)_YV&#F=vGNgR{r<-~aXHit{trxKlYIqEJ)K8tB_mQ<1Ulq!@q;tNV2j(!?#<2FXJCv6NRkS`Sz| zkgWY*wjI-}J7eN?*NI>o9z^jiPRBujhJ2TB1+P*LpG*Rd76}ye6K9Bj=AEm)pepSt z7&2Qm;2cm(gDD4s08kRjL%iSQRf`NP=QAsA+?>RDTT(}DP*u){%0V>Zh*h?HbLB?` z?$k%ul~V$a9_mwwd>Y*eo#5-}S+dOF){L@Pq!J^Q?7HJ3bODZFH?sxy@0MUux?@f3 z>uk(+#=o)=)*!DnRtb<8Da`cLc(isd+o~zit(eztwKAtkIx-(OpjXoz8g6%q74pXm zwP36rdk~BIY>X&GsO)BGcJjtw>F^d94VrY8B+D60M)xj2m)yXl@fv0`F*s>sP`WTF zD)3%Ff`a)p7Ux8T;;65J3`=$x~K!qcPIT z+}-08as_{(uRVbzU?J=Ihf7PWPz5S#zl;Ynt4F{0!z-PfIJrhRg22cJJVX%RP^?}-Ue3TY+k;h=k;v#Qg@cIH+DV2C3x}Ax4zZq0G!D;n zBFz&2JgPE~wL0~^E6a%`RNgj{-@@3v@LcQ+GB6*;U#D_>@i?s+!&BV8{V1MjEgOvA zJGqFRCVzY;u~E9`yY}MPfQ=D*yq>>s1vlDvu+zt`mbYd&`adP`7@3rB3x>Hr+#ca_ zI={z{EJXde$KbJVl7z>={1g(N2?W%IcTg{x8uTGJDzJzSLYB z4>c&u#M+3$xJ8q#(tYRaGYAbtY^lkLee(sR?q_NDL0+5zy4dy4g{IX)kKU=cw8+J- z>GE0WdGq&QRY#v>FP##g-8;FKC^N3;>0`i@vc=Qapg78;(7!Pm!-7|T?)W&^7jucK zl?SZy?QQ=H{Jq`PLuLN9o)B$D)Wx|ISeFhdPQMc@&TSpIXpu)HS0?zezDu{P@WXi9 z^6}oW8f`ExMUj1eEPL-qSW-5p)X$Fw1g#$81YPRe**?X14t(7XOrMh?kQQ%Tz70nO zLgDVJWACbCAT>aLya|K`6aH?SwrOv=$=hjWoDA@>gxhv@j(nCng2`h156~dz(~&X5OuEMR#TTve) zy&6fvr?S6#eD*q~=kd@eUJu=rplI$%ste|rmVh^n7sGCgmo?0K%FXsI-A~cON3!_{ z>A>N$s1r*E!$HSrV)DUS8C8n#r4MtQt%PWd$qeih)YD6dDwuq0{%(SaM6)hagoTG@ z{g_XsJp6C)yWB7@1=CV^KJXg(GR0qQDO)#7H0H%ItrIuyjn}5F@x<$;>Rh9v3d^eT zpm1FHA5?Wm$D;EPs-+rgRe|`zC@>_%+J?v?1Kh|E@gQX19!Hor=W|AlhZ>m83T&W@ zp%rA&o!Cq8$oWvQcuzuY9-3BBlv+_rL~-b^Q)g#+qdT!ob@*k^+OpTvgxFGueed0w zG$fnW8^yn#4=Z4{U^ zx@Br^fg%^efHIe0OPL#5#F_g1?s31awJ)aiCjqeveL3h1#X?(ftCB(RDt)l3g{q}7 zO}2F45x!>v#>r~1)o7koZ>}b?NbWy&A2Q^~JLbQ^q+r?@>HpSRiV^IaM2trf8CYGB zvXP^OgpmKd!*8Qc`C-$-W54QOUJSGI6Cp`O&dp-NTv(1Ipn9kBvBb23D9wqcaBVCL zM#XBp6OtTDaD8XFc7XkKUP=hBg%o6<-n_1MGj6B3DbOgFgQ=VH0awLlIs0ZJK^VJY zX-oX(y@G-WrRJ(F$1|RYKbbWJWwwSAiEdRTRp_y&yys}n)*h|dRJ%hix8``-bZtV- z`0X8?)M#|K1zC)XuooCk&W15ZEpThbH}IT;qUjV2^pRXNwI}qevOT${hOiws*>hYi zeY-XUT)J+R-1lE<^?ZshpQo;#M0$X8n2}js3~3hB@g~e~h*b>yQsHnicq0^2FX0RV zUCOgSIe6K5FL;=nS3Xztl8j%gRXOG{BUC#=BD6`keKmhB=w1@O=c41?C*wHy4!ay2 zpR2Se{n8-Rr#)Zq*VNb-@(%AtdP3<%U<{h!2)Y%0#+&rIzq-rrX#IcM`|hYJesABQ zt5l^bU5W}wmkt7gfS@4K6r^{gH>D#TM2ZMVM`_X#rASo~l-{IwsnVO$c?pX5-0$z+ z``%q|t@p=U_pG(gOg>3=cJ@v($)1_aQz}{=!kFQ&t8QiGWnw0T0puLX>>?g$>o!~K z-@n*w3*fN3wZ$+r#R{&u)8Vj64%(e!c{%t=Wcg*zDqddpj*k$+(}0u&7nTf~Ha@B4 zm|Uwg{!;ib`Ia_%*LzDyn$PX~LP3#T~4Es(j;R8s+lK_kxIy`vtBS z?Ij&*oEe5l^jk69>rOeEqQfUrM()qq(ML28u+97Pp+}%kr<$mWI$lDQRjPmLQ(uaU zkv|tnOga}>rM87F6raSYNk-o&_3{Dzkf^imJi*)uSR++?F8%&+r@Tbmf3&~f!!I6xvZADq@3Wk2{B(XDC-d@5=poiAZBKkZre*KSZ`{__r)St3N}9pl z=e)PqZ%}4e*0YAp5wq|zfH`gljO1&*s)Qh)f{QV#|vu^b$w{b zlrkz1L*}4A^sR>fQCh5_QCTqlyqctPnLMry#(bBjvB;<5w;XfRoqbB=ey! z#KdAosbHvL-;o#BQD>jQTRe3U&zk6?O$z!D;a=6}_?%y&)*Sgi)My*^39&+5OU*Tx z1aFM|U{KGiNu84S{<7|qs`9P|_ndm6hEr<@w(-Q*u44H+ox9IVlJPjKrjQi;yR~D$ zq+msivK`|yZrP;i~+t?a|t)G?b!z| zofQpN37Hr#>=>q+k-j}&=|;d{uQIQuY}+k(kd|g*M3Z*AgkEP%CI8Cp=agBY3v@-z zaXqR<6@nzy%xID+xID4k-`22nCd4A+z|LL`g~^u*lC>#?Y|>9i-R`YP-0xPk_C0uG z$aI-ZWwqb=hV#2{ZtH^B37jtkCg4+y(*g-v2^b-uH`oYb#0MTtV7g1_MSfo|cEZm# zbPe%8Rb+VoCBjyOk_DG(kK9#OqZ~)NQzEY8z9G9Se_cNCYcr$tUM4fc5voFh0dP3hM6to^1OzwxZj$31^6@uv3`1R5N%4GIh6uQVNmcnM`n z9F6HmUNBEl(p;V7aHPRY+vOuyi$||r}U(=03Oz>GvCI&G4G8OjR>btUb1!^ z5*|x49D~{i{q1;*zY}2g>m+#k6tl7*h(KuPKJWZIr&^$3dERD3G3|Cr3NgM_?ap;a znMVD6PmP}i7t5(80v{wN^&HZ6RYZNBOB~wwr2HV{n8G9w;%Qf*6aZe*`I>tUXFQxTvG3) z)8D?;NJWek^S?2SsXCO@jv0Tm@2af)`NYN}9`S@%kM~>kv|GA>GbbE}{~;BX7oqS) zbYH3fx~V_={Ut^HotLo_8OjkI&4O|Eywm4XHhf=BaIF{$P5*dXYSqAWpLQicNpG-K zw2kx^RaKm6@6Uh`0Yj#mfkMu=4%HR-Ema5$#y| z^x(I53`VpTtfxK>(C>P*)?Ig*w+#$1;d_T=`g-kE7x zsf88I#aQ768!K)`KXc0Cl~G9C`7VlMlJEM!so=BmoU+QLF$?8dO9^AA)Lzp>3p!TM zdJHchx|~GZzi#;lXO*dop#wy@;vp**FKw+Q)_?A;OXL<+tGKP*mH<^Xc#BQzz>~i% zqAj^8x#|1rgq;m|9LH{Z^?Sp3?evxn4h|02Ohbszcw5*PTRTCjWUepU2{NZ^3C?3S z%^rF2!T5=?fgsrxwzm(tkJnIJT~8(X*Kh|H(k!I%Wv`1o)+NPeXVkNz5@mHt80L1h z|Av#^r5gJ3KBfPqt0yt%NQj;nF4Hy1c=#CDE>s1D>kG7^gKfY>pWQ0w?`_tKdZCkw zwPX6s+#*{(+@1+rolVz$#O69e<8QOq?zvjQlE<8cbLHzhD zpYx4E{?TrykCK-92I%yzoFU;sYH+}|koyb<=>`)CaG!Um zqdBv?=^mjNN@6Hls;X)8aTIp^}YhL5X9-J0Dm_Hva zDKIEFW7+qvJlekX;TM+q5 zN{1A&(kl<`vZfM5*zk|MnkqyxFfd z>z%`wZkr4JRW)3Fdeb9^bMD2u**6BbRE_(-ev=}mHJUdIlg150qtH&BX?0WfS0>6F zI$Tf3J%~MEB8#Xlt}>e67o5Y@?Wq)>+`x@38rV#jX~l~E1#Uf5{sLRRx@I~LIoDE; z9(HUK+Rjt`snnl&7zuA4Nz|Dq(WbCgU^8j?B7=kH=P6#CGCRjDJM4Y!jggZ&c{Jm% zy{l)ycibt8H&`q-8tfco;(3DT4LpX*Ssz(Z>bKZ&C7^AKN?$5`OuQUk(IK*B-}=+T zUarz@ax1LDl{SXq(9r#+7eio2o4vW|e0j+k4Wn4(^Skl=TM03&lUr1VVT%{6yZbXE zl~o1q*y8VZ+&iwig9v$niX*njUl$P;ng z`hj;KTq)~)fo3x z-TK-R#z}Nh;>NAjEXkhtRU(Hs9nPubX8*eHf&Sh3=Y5l?uTz;@!JOCyFNHm)&ZwG1 zvb`yj6`{dtHEd_Pue@3F)4LXIn840y6o1qf4|Qo}I#nfbIOFW=%G-FE?nSHnOBxRH zMPN2e2Wfa0%|^Jp7Uh$!9FSwwN^-qk zi)CR<4w0_cqm|P`GrTh?et8x|A4fgNQ{x7c9j>x7d|GxX8yhvigM zV9P5i53QjD#Q^dP{2v;U37<{aP;Cj=PmE0p0H08q;L5h0&DbPjGbU&=h6Xa~wS2(< z4?_wf1h)_YWQ>4a2AB$rjeoWYJbJ1cd~p4Mmt5425wN|*ks+{pC-hV{g*#26-+XX-E z27J5t02AKqPvkmm*Am85^e5+ldohkFYR`+38P;u}m% zRc3z&JhT1*2of2eW)djwZlk8s}n#DyM+S|mn(7LiYMIg(nVQxE<}M{98{5T z1pX}kkF%)aeBNBym-x)llLqa+w(c>_xRrbB0 z`LL^R|NJih;<z05Q4&lO8K)S^v z#XH>x%YGdCEAR~Orh9=L7|XxV#N4`TN|@9lt`a>2SHZUDbRS(jU33>j$92FwOJBQDz0|E z1*RkUcwnXQ=jy#zUG4F>dMM%~Cgc4g`maA#)KVh3(;hcQiVcmU@!u2;DVk(8F$}pm z9}{KWf9ggFC9ww2Eib>`Sre*Of9ztd@S(lIM}8U|o=Q5QYJ;P1@QA!rv9hyMOFydB zRNUo_E2{FBY<1C zbK!lIJ9?Y|W3R=z{gPR1uD_%hRoIEZ8!g<1ZzS;^a}jR1w_DG$QP*9_*$CPfWU|=3 zxpX)5jhX?AG*dv*6~EYbE)HvZG^8D(+VshOPNS--#uV+CXr4-bc8Up|iVqpb5FGy= z#fd==3Wd%MjhC5?U0nRn_+JhqH02}M*Sus2M&oIpobMT5oa3jTmU3y+db5j{`-oXm z_1h~<7bPhPw{XTRVgC2<2D8gZp>Tl>cwpu5;fb0<>QSA;eY9FoF);2O@A(J)WJWyr zvc>=gFP9)|VYRKVnN8D*Bff5%L`NvB*{`ONx#nHDH0yZiygrn3C%BWyv-0G0$F1U< z&FbE0xP~4^7wq-;MWp=_x0lvB3F+;O!MFUL)QqQlV$JCIeIw`>U$@cmU(dGwfJx9| zAVi^Q)-FCvZ`KgS0(QRapRS7>(NjxVWuNZ`sd@Beoid6JCI&p^7ma5ou##Q{%2G1 zB$(3r>3zgafloNQKfdrAd||bczy52A@shjDdM#RZ=xuJs3|6BW+^&Z2t7Ve;llI!u zTW77#v?t>y%r0UUj7t_RH#$QSZL9<>b{LLs5Z{v$B^zr;d8dElt9rB~*DRfzE&3@Voawy!Ij;N}h3_ku z*PTn{R#h%geWP7(8~b(DTxV)uk!^UArC8yWccj+yrWyBYns}UdjT6=M>S^>;6X!Ec zm=Y{|t?bP2I82G(oh|fRoTwpVS~cbF#vpjH_&i*Fue8B*ghMWAGy1}Nt>+Iv#Rf9H zuodUy(~Wwy7N0I<7&Kqyv0J^ARB9ufzg|Xo*gr%)KKu*%)A=Og{&nj=PjA~!3)vr( zRrkKG-c2nK@4^vErF-xpP3g>R*A3ImoJ9VO&a+ScJn^`5vVm7!B8wwjTQ^z# zG}nSp z*S>Yi3*3zAs*O^Xv4mExp-%JBnNJZ7#d#}RwCrD~<@QCEYa&%!OL}c8jM@42%RE>b zv%PIeomr$fl3k+DX?es~s#z29-N^J!%zB?CMKq+jHcUJ?Goj!VJ$*~&y!yS!(}XAM zW?rwox#aSr({!f$m08TK=vf?~0dPm*&jx?!l1Ti~!#N~=LPUW%(4vIBvsrv^-%z(b z9~-`MQl|b)zCK!5IK@qg51$;)tnFs@@Tm99tn&3F*olWGyFRh-S1g(pYIWBxc>Jd9 z>_WC<%~UK`&Fb9Fc6q|`)-1zJDfJxfCz{q@GTezZOhfZ3X2uooPmyDi+BOmS9DKQ0 zyMlHN58coDz6-~c!$JsUMwr+OEq13C1G_9~HSV*C>m|eif-k^#u9zFMl~wvapEncE z;gOcs#+##hnBd(IZi5j{C5}U!+)_J9b&GDiS^in90C26avoT%5mz3u90yVfG0?U)a@}eKLFW z6?}SLkbKjnE+e7wV$NP5^M9FRx47!doT=tu^m)*hLL!p@2hH(B__s6J&-EYIa0`E) zBG6ZD@OSLHVIm}E-NYt4FQ(X?TysxXGz7DrDUVWv*L53rJC7hc7bhozPGX@I`z3;o zL=vO}uaHtSx^6Kt#6BOud~b5*0CVBv2{p02rj;KqT**EL&z>xn?%geqEh=t8@71du z9n0HqvcI-OTdIGj?*7`HZkR=v4k!?(^XU*4%#j#);N2xASW z-i`bs)h0?8F#nRL?QEOnTnVmrzksu>%8QeW$K%QIW`E#KEVk#YnEr-HSuLmzM!|@4 z4#ij5`)T3=r$( zM*H2>J^fg7`*C`<9%Wu*kd}yNBG>dep4K7DEDqd7JX754q4!%joJh|&VS^A6k!15V zWo5d+7pHyST3`#@7LzQ=e>T%Pq+m{&9iJ6SQRn#X(*5W4=>!KOM21=A0{6DxMCMpl z|KP|T`b2^II$A{t(|nY($Nb5Gk}`hzg|sTscAB!y;o7C$#CEFz3{!;0M$okuJY01G zY)<4aL=vut7Aww71}liO=VQk|n@b^Oq(xu#@ieEbZ0%uF&k4*ZrAVu%hx1~7$^~Y= zt4y*>{iO=jwZn?xNh9(+HFB{naW13RrZa5lrof}HPF{oGRLjw|(3FS^;;Q&%Bhmc% znmyVCJr0YzQMv+sWJ(*O;0xtD2z{H{(M%hEa=XRr%B^&}L*BCKH+|LDF^?!IE7-899 zrP4|J6@7UoN^G;&X;pA)uEcMiU}~-Fa`f%;lKsH$TY}vC`FZ>eEA>L_0p=qwvze4L zA3JC*Ak&Hwh1o@g%iNTYnO_)>BHw7eB2JEg&zv{59<=Lau?Js0QQMd9a8+f$b&Jm^ zH?p?qCu0i%-ICedVQH8M@Dr=x=is=0?wE6pLA-ZIO{}6h3j{tZcx?_ZYaWXKS)IZC`k}{?CSeU=uTTb34KGF&rwdqua&e zhcn4$lJ@jxmMN=y7}7pYy1rx7!7YD_e|7zeRcQ?u^H-f|o5@%}iUPB|B=*Ux;bd@BKsfzOP{@zCV_z~`c;zn1gASi9-?l*HJwmMnE#dtgnR zI$Bk_KKGqE1?!Pd?Waf|iZXFREHSyrpAY2fnHy)Qys6qH?5cXquTV!nCcD1tMjW?s zkEs`9kNx{_f#)zz`S8^!>h+2xLYu?h!*W<70g1SO+2{`AFml7Ah-hx$LTeg2-?px^ ztA?;gG|dNoRr>@lwIX*`g+Y5=_y{fU^E151Pc`h+}!R+{wrF>oGZ`J4`dhc9mSsUr@*s7~GfVyAq}RV5s@~ zx4fSsRRwn)L>uif?5H=QXtNWyUP&#y`5s$*_D9pXx%YJiw1zxgjr{yp=+XBE?uc&Z zao)0f<~!Lp#Hh+?;FB@bgbJLy0A(?m|lWn9!P60O~A zx#N-ci1hJ(M(-QF1=+9@XQy#I>^*!5D?eHJ&F}1y6pq<`v9@#P@mSK)S&P>S9gG*$ zbSihfxtCX8KRn34=91cIWj$bNNvTzAAH}yxIe6_+sd4>q=RO*9LwrxyDJDf#%9`x~d0CzpncO*ozv4uIg7(JyxRdGYZH`I`177YBwb zoEjJ>t?~0}NAN$CVGpH8_{;*_bK=&CB&@@-8#sqBmG^~>8S5t>LsvpqxK{WxM0!{Lm-lT zfnw%UGXHs<)1`GuBHJ6bGo!8KF6Td{`RQ;Gy}OzH92?C_9MtNTNZHd{RG-2HpXyR! zc-M|0f>bid_@`I|))~BN0e9^K6?PE^&qw8Ue^w?@fmNpQ)Y5jKFq5Cxj^K?vM?ta9 zd;RoQ=>XqV^-t5W%hJUR^hVG3BI?$txbo2N&WAKnk=MV-eQGsJcDk2&5q!fmt0U`0 zDaF^&o5iA}p>x)X=^;U?*YnNzFS1)OGi6(zHsN7?x$TuPXM8)oB=Uz&IgLtAP<7pE zr!S3udwWN8(r0HswWLMA@05c9q!!eLQ(+^&NbC1^diY~8Cj&7rowy**a0xBthKTqW z=xOi`5t7J1D{e2j_$#YKk#7V=aO9brq#|AD$#zW9nuUUIeC0Nk^8H@CQ55rCF&mA5Sj^mn^x|8`d(zNqW5`6e8S<)p>7r>Ly}nxNuf0) z^EMVX{mIDbNI9&^+Fz4?OEiWb}~aBQyZv&$G{7 zm5Cyc4bTW;#yYi>i!OeX9enOA!>n%>&ne9FBble-t^ZQXLz8pa%s=kiAKYxOOTCDp zW$xtMP{UW{{-IAhFG+eg@#Cg4$IaJXM5Nwb+2$)(y<6|?8Q1@s5TV@|N~(w`6{H;Y^m96^T{_{Pvr{S@hS8v6~tvGZ{j>2%a5o{(jyhT_Wz+ z^glMJ$2g=&y2EzSibUnt)mOn%b|T{8tz? zIDR;pbMQ01q{wJ#|Exh`xtR6#dCdi4o*F6lJDa!->iO!guH&;Vm#Adk2@41_?|K`h z;$y8{nH`n(L+J|juUz5rp7XQpDg9Fn7zItba z-mRch@lxs%S1eE2e&)Mte0EBbDGQsdli<~WCIel4WvwX-_TC~xO0BhR1^Y)rj9#pz zkwV`p_dem=#P%XR92#ESV965`i*3|lj4e>F=)Ju)O=omEfBDjcfUx;vw`eF5%w(DiiO-{@H8k3z`RS z<~s?Zp!ZROS6`QO8MEpZni=&L3Xwf~KYxSR0>Axai^VnVkXvp{69OaT9lA6E?Z{Xn zOG)a`gU~N#Iqn+%`Mzc9B_H0YhbTXaKCN01t9P#F^6L2>zA{SrDtYhvbmNA>(8e84 zmSDVu6^;cm&jy|(%|_!Nvg!Pvvx7CR-H*e+-0;C^3EMVBo|xqq&Oyq1ek|tPPOc%( z!=u;SCD4*_njH@DJrj@~2Amx^{gr-Rn=nB;uKLfGe5Hum%Ld|MejSftqJ@AYp!mFz00 z+Dgo(q>4=8zu=Vr6aS;~SI;-+1@laMVoOH)_-?TLr^YCDxVyL8o;ADr(LnNZOnB5Q1Vm#}e0yksT(`P}|!Nf_8bPxHQJ7Dfve>@z+>y`7+qw#&Z`@NfG>xc;>HYbzT)^gGwL6o&0qeq+iren(nw^ zKoF13^L*abV3pT31_Hz#E$`%*)&?l-i*MX{?&Y%oscMOOb;&g|Pf)aL7`#QZB%FL0 z4oCJaiFo9nA1}f<)O&OllOXVdSf?!sX|%LNW2S(-=wx?a>G+~+jk z4;J3kV~wJ|@cn9|$oqQT(YdTQJDTlPU2Ko!TwPAS8}{MGdc#BW!a{A`r>)+kCW`Cw zoh<)l*6Pt|S;fYUj0Z8d%_DY1-^fWPO+8t|RP86bBCC_@-r7YQH@q9XCbZ?C!S>j7 zaz@R<#Y-b==>ypv8{YCOy24Ihol|7*xHwk&jtQsHs1hgy$R(cJaH6%CagcVT4o;&=xOnjLA-6=Lk$$QpPl)$mdJF0>=zFSOAf zn~ViN9A*^;^@rDkPbz;nedp476Y?!;s|U0{WIEM;T~B{Unt1xBe`@Ws*Xd^d_xXFb zmQuRYth(L!q+hoxIpoC$c)ZIUNaj{EO;X)RT(BqfbdpVDrppZJ(+F+IcO`f2ztnqw zZQ_ys`R3vh7sc?A=I?6fLa%eC1P}*ecqxovMB{E+)u1WTggsn*-uVGnvX98{ZLy8Q z6E7Us!_~u=K9$1NvzrZ$^EAc1u0B<@K<;vh_!9x%=$NO#cLBhUJ{0|qPf9xxCld*9 z>L7%NPsqEj1Aksxs-Z=`TKSV&f|RX@Z&Uu6?4dxinTFY!bMYqnoff?3=euI;?TK$bldEUA-mz5{!J5-%HHrug#3ly#0 z!}F9VmC7vf@~pk*FLowI_=JmZ0GBTjo~B|up`QWoTGLDZd5-H!8k6(_&1>=y+sP5;E>nBMv%YdXR;c!quN0f)@GB~1-C)!CR~zlvYW zD&vQxDUN%DLKOGC1N!_kxn=}2muqLH+pGA<6LFih{o2Y)2Og~lwR~u4!plPNcxo?K z^5n#b{K!IY&&lN^;MMffMpJh>8^Gd)9%5O`)tFs8)2iF?s0{!~LG?fQc8F(XS-3~mel(4-=Aksh}L!%h+VIA4`g8lBIL^6?r zTk0(RAqBUwk)Q9Wwx=u%T3j#pwQUyr3KpLb@Yj=cM53N?He8$Mux&(hv2}tvBZS*CnYvTYPVSqXCyEoq#--^Y zoHEiKr3$K5WtBI_zgIxcdE?i;g`m}&>CBA559U>UtY5~t&^@T)wZWjGq>@0#=MY1% z&Cn%tgVXa(d1=?##6+zgyb@ej@VDSeQrccLlP7HlUzimlv+*L<5!!u^OXjt?@v8L;BA-3dNr{lJ?pjhWDP*tKeWNcgiZeJv1F6D^s7 z(flffp**vmkVo*4xkfF1b7Z;j_W3gx3svx+@lKqr+g#YQj8`IV{z7C)8rA(?az5iS z7Tc-m`ar%wdp>;fa`LKgD$}iI`rj=G<(}5}gvb={9^j@^YoT4dlKVt*fDRFud1^Je zwOK7Rlzq!(ee}LKMvgptw)0GNLTlxQiVub9(7a%l=6f-`cK388E-aH}PBWOqOAt8)Lh7R_C1(neQdxt-)00+nyr}K9m@MkOy&GbMVou%PF z>+Z;jV4&Yrtvkwj+0^iX-5(x^lbPYsCNalhH4wIz)w42uUzOuc8{sVBHZ|`7#Y!H|x3Ea>)@)G|}2mVoi z3!vJv0DuI4sHOf!{`pUFcnBZ?!1WrMAwqUIj?kEZW3C28Q>4% zfUpsRe@k2a8v`aeiUY-g4^aQf3(^vY{`+_kI!brM8^lxU{0`$*fFsCv^!sn*|KIWd zzl}eV3&{EZO5Xpj{{C<4_doeTiv#vOZofzE55?a9qo4oL&;QTuDZj^T&|i+mZqy^}Lol7opLFm5uPL9v@eZKVfCvG(0U`zjR!j&!Aov6{86cuR z1moZik*G8V?ZzuJcr~B=$I!ALpfmDAV8FWVA&8q&SQ&m4smlp zHZ1BIG7glfO%p3Z9p(BkVMKjPW{5mo?U3KUEp&3}Y% z{UiKOeF^=8-#_&S?Tf09*B}f(6DJL54^R~#I0u7sA5b3*I(r8LYj6&Kb%21qu+D;c zRyMdUIfChaFoK){I>iO^J-kVvW{`&v@FAK5#e@S>IDjq!+8!CO8^t+5+hCg951QyG zm_1j5`!puNPc4oh^q^knLAea=2*L>FX-qQ+av>C~g+W`tbO1yI`PrrsgdNm}1GHz( zdIaGDW%Gdc%M1AP;Q|6n3V~+`A_%7cLeCLIZ6b&gNPF8B3<ncR0k16tr%?R z3FN*D^m9)JtOmP4lLB?o4go3yZ9x~vpa;_FgEWR#2x0`%m;f0}@esr;9dun^1hD}1 zvJ^!StBVL?U4|ewd0@x@ve>gAhyxgh9BUB7DGWg#UOH$DkYHW}3HgQ~PcjhXDM%LvbP+y-AQ5c{ z5;>0`Q7H)WTn|BFa1bO`3_)H1nd4r8Ar8ou2=tnygdoWi2$C{@ATM(f@Yy_z#M34_72=Wo6{{-ytISE1PZNPh@0Q&;kT%!wuG-)763kQO< zA_&q(k09;I2-4w%AYJDWq#M-Z8<4dJ*tHj6{lIPmst7W;h#*7d2r>fX90htAJ3x@} zMg*B?MvzIse+tMr{R=^6Ksj^3em_(YWS$v87J+`3fLzN#2(kjwtpVMyMU&YUdGx?8bs&)&0L%ZUWoE-&y0|$D@BVG(@MgF7ZroFYJ6>3p;K-lo#mYYN=f0mm>_P>{#$7%l$mYPI{f0ml&|5$3? zv$Wv_20Qxwn;OuM21x{n3V(b?_J;QG1jEbAiwQwTFu{>roD@Q6gr$(M0D^VqgxVB> z4bIS@s{gS}M58!T4{`!76M^ZDexQjSDIb;$3FdET7X=4Hz~A)vC^!KM4!Xi${zND^Xp(<%(B=N}Awj{RdXDQwhJu6P>@N;< z@|Vw96r2(Tr$WI&m;ald1_h@@!Rb)&^C&n23eJdvGoj$1yZz-4hLOK=f}!y*ehCF< zMZwun@XIJTI|>eljK6$1QE+Y)oCgKxMZvG2;Cv`JKMF2@f(xSH!YH^13J!+Xzw(Ho z;8#&_aTHt<1;2)Z!+vtyZlzK1>nOMk3NDL+%c0=%D7XR&4u;FW{J}8u*Di`ExDpDk zjDp`m!NHLFmk$_{|MF2o!PQZ44HWz?3a*KQYoXxxQE+V(Tn7c$MZxt^a5yF$_ZLGH z+z17SL-(}1%e6+q!KLOeZij-yk?=UZ0}Aelf`iN6 zUpbvo@P{ZkxMcn198AIf%9(?L=c3?wD0n^!UVwrZqTp{( z@M0AFEeie)1usFtOHuH06#P93UV(yFqTp31cr^-MgM!zh;2%-&PydEvI{f?PKb$Kd zU4U8u!TP{5djY}xkasPhHb8xVJ_Gs!s2|WUpbvn00D);O(gX;~SPN(n(04$PC*)lR zs2vcPf+C%OAdluhR11!+fM8k}-v9`f4Rr|1fcc65!E(9*!7^cfC>zWN`N1R9Ei4PB zgJ~c?SQmJNX<&I!##%rS8v_LE^A!-xKL7}p56gx1g*en3EEAqXzOY<~!Lp!SkSEj` z%nxzMzvR#P6ga|os2dm$Z8ii5@`B~VV?7|KODHFl8S;Q-K;E^0AV0_-;*bX zkO#CElyedg%m*<@Fwb$>Fb}L3YzOdo91rDz`Jp`Ud;(A{AlR2+*)ZKYASeUW5$rQC zKeXE_Aeauy59>Y;s1^{+2kT!52(||pR|W{mcq|({hvmXJsGB7~P|r|4SRS-5lpU4> z3Fe1&fV_|S!F;g3xqzUpVVSVbP#$;=Z3TE7&Chqhc>y38zX1s5gYp#tg7$~JVf~Kl zybsQyEU?`|Ibb?iF60Z#h5YgW9qaQAI0rHwt+1eeMgc*&pgoW43d`6Al=BCN=^#JY z2H+9471&0hj$!+Qx`Hyn`a}K)fS`Sjbu$3YkM#;=gM6U;ux?O(ST@vYEm&Sqp}10j z4lu6+R|*SorGT<IIMfHr|EB?Y)rz5!QCE^wuo0#}LGH|5;9pAMAt`s%kMxg=(+MRYhaHW_7HwpzHP=!E0K(=5h;7XYQ zt`uOSr_sQbVhmg<8^DbMbP);KLlkI(&q2BvV2fBK;7S2?iz@`K6p%j=l$`|hoeb)l zN(WpimB5t(be+xu+$cbwndrckk_lWX-k?c>bh$w8ye;5L0d1R_nx&bDj5isNd{!>8486eNh5^$x&0#}M27>Yo?MMf~JkOEf<(Az4o+gbw{ zT6z%Vrw(wXfV|r{pd0rgpds6v*chUL6Fch%55NKF1-w0S2>z+;^-XNR@i+z~I=WiJ z#QKhg4xl9Do~6;z5hkNYy`K)8B6fN!#Df>7j|BpC;$Ke diff --git a/tests/tests.js b/tests/tests.js index d27bb29..14addd5 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -11,7 +11,7 @@ exports.defineAutoTests = function () { jasmine.DEFAULT_TIMEOUT_INTERVAL = 9000; var appId = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID; - var videoUrl = location.origin + '/plugins/cordova-plugin-chromecast-tests/res/test.mp4'; + var videoUrl = 'https://archive.org/download/CosmosLaundromatFirstCycle/Cosmos%20Laundromat%20-%20First%20Cycle%20%281080p%29.mp4' describe('chrome.cast', function () { From 125f37f0cef6e2fecc6116464c7dcbd3979be602 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 15 Sep 2019 02:37:23 -0600 Subject: [PATCH 037/166] SessionManager.startSession did not work reliably on android 4.4.2, so switch to MediaRouter.selectRoute() Add isNearbyDevice to routes output from startRouteScan (mostly so testing can avoid attempting to join these routes, since they require manual input. But could be useful to the user as well.) Remove unnecessary chrome.cast.js global vars. Added polyfill for Promise to tests since Android 4.4.3- does not have Promise natively. Contributing to Issue #36 Relevant to Issue #22 --- src/android/Chromecast.java | 8 ++- src/android/ChromecastConnection.java | 84 +++++++++++++++++++++++---- tests/tests.js | 44 +++++++++----- www/chrome.cast.js | 21 +++---- 4 files changed, 119 insertions(+), 38 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index b76d911..48bc898 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -407,7 +407,7 @@ void onRouteUpdate(List routes) { callbackContext.sendPluginResult(pluginResult); } }; - connection.scanForRoutes(clientScan); + connection.scanForRoutes(null, clientScan, null); return true; } @@ -438,6 +438,12 @@ private JSONArray routesToJSON(List routes) { JSONObject obj = new JSONObject(); obj.put("name", route.getName()); obj.put("id", route.getId()); + + CastDevice device = CastDevice.getFromBundle(route.getExtras()); + if (device != null) { + obj.put("isNearbyDevice", !device.isOnLocalNetwork()); + } + routesArray.put(obj); } catch (JSONException e) { } diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index 7b3fe0a..2f7417f 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -3,7 +3,6 @@ import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; -import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; @@ -201,15 +200,51 @@ public void run() { callback.onJoin(session); return; } - listenForConnection(callback); - Intent castIntent = new Intent(); - castIntent.putExtra("CAST_INTENT_TO_CAST_ROUTE_ID_KEY", routeId); - // RouteName and toast are just for display - castIntent.putExtra("CAST_INTENT_TO_CAST_DEVICE_NAME_KEY", routeName); - castIntent.putExtra("CAST_INTENT_TO_CAST_NO_TOAST_KEY", false); + // We need this hack so that we can access the foundRoute value + // Without having to store it as a global variable. + // Just always access first element + final boolean[] foundRoute = {false}; + + listenForConnection(callback); - getSessionManager().startSession(castIntent); + // We need to start an active scan because getMediaRouter().getRoutes() may be out + // of date. Also, maintaining a list of known routes doesn't work. It is possible + // to have a route in your "known" routes list, but is not in + // getMediaRouter().getRoutes() which will result in "Ignoring attempt to select + // removed route: ", even if that route *should* be available. This state could + // happen because routes are periodically "removed" and "added", and if the last + // time media router was scanning ended when the route was temporarily removed the + // getRoutes() fn will have no record of the route. We need the active scan to + // avoid this situation as well. PS. Just running the scan non-stop is a poor idea + // since it will drain battery power quickly. + ScanCallback scan = new ScanCallback() { + @Override + void onRouteUpdate(List routes) { + // Look for the matching route + for (RouteInfo route : routes) { + if (!foundRoute[0] && route.getId().equals(routeId)) { + // Found the route! + foundRoute[0] = true; + // So stop the scan + stopScan(this); + // And select it! + getMediaRouter().selectRoute(route); + } + } + } + }; + scanForRoutes(5000L, scan, new Runnable() { + @Override + public void run() { + // If we were not able to find the route + if (!foundRoute[0]) { + stopScan(scan); + callback.onError("TIMEOUT Could not find active route with id: " + + routeId + " after 5s."); + } + } + }); } }); } @@ -308,9 +343,13 @@ public void onSessionStartFailed(CastSession castSession, int errCode) { /** * Starts listening for receiver updates. * Must call stopScan(callback) or the battery will drain with non-stop active scanning. + * @param timeout ms until the scan automatically stops, + * if 0 only calls callback.onRouteUpdate once with the currently known routes + * if null, will scan until stopScan is called * @param callback the callback to receive route updates on + * @param onTimeout called when the timeout hits */ - public void scanForRoutes(ScanCallback callback) { + public void scanForRoutes(Long timeout, ScanCallback callback, Runnable onTimeout) { // Add the callback in active scan mode activity.runOnUiThread(new Runnable() { public void run() { @@ -319,12 +358,31 @@ public void run() { // Send out the initial routes callback.onFilteredRouteUpdate(); + if (timeout != null && timeout == 0) { + return; + } + // Add the callback in active scan mode getMediaRouter().addCallback(new MediaRouteSelector.Builder() .addControlCategory(CastMediaControlIntent.categoryForCast(appId)) .build(), callback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); + + if (timeout != null) { + // remove the callback after timeout ms, and notify caller + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + // And stop the scan for routes + getMediaRouter().removeCallback(callback); + // Notify + if (onTimeout != null) { + onTimeout.run(); + } + } + }, timeout); + } } }); } @@ -440,10 +498,9 @@ private void onFilteredRouteUpdate() { if (stopped || mediaRouter == null) { return; } - List routes = mediaRouter.getRoutes(); List outRoutes = new ArrayList<>(); // Filter the routes - for (RouteInfo route : routes) { + for (RouteInfo route : mediaRouter.getRoutes()) { // We don't want default routes, or duplicate active routes // or multizone duplicates https://github.com/jellyfin/cordova-plugin-chromecast/issues/32 Bundle extras = route.getExtras(); @@ -453,7 +510,10 @@ private void onFilteredRouteUpdate() { continue; } } - if (!route.isDefault() && !route.getDescription().equals("Google Cast Multizone Member")) { + if (!route.isDefault() + && !route.getDescription().equals("Google Cast Multizone Member") + && route.getPlaybackType() == RouteInfo.PLAYBACK_TYPE_REMOTE + ) { outRoutes.add(route); } } diff --git a/tests/tests.js b/tests/tests.js index 14addd5..4188c0b 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -2,8 +2,15 @@ * The order of the tests is very important! * Unfortunately using nested describes and beforeAll does not work correctly. * So just be careful with the order of tests! + * Edit: TODO We should really switch to mocha. */ +// We need a promise polyfill for Android < 4.4.3 +// from https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js +/*eslint-disable */ +!(function (e, n) { typeof exports === 'object' && typeof module !== 'undefined' ? n() : typeof define === 'function' && define.amd ? define(n) : n(); }(0, function () { 'use strict'; function e (e) { var n = this.constructor; return this.then(function (t) { return n.resolve(e()).then(function () { return t; }); }, function (t) { return n.resolve(e()).then(function () { return n.reject(t); }); }); } function n (e) { return !(!e || typeof e.length === 'undefined'); } function t () {} function o (e) { if (!(this instanceof o)) throw new TypeError('Promises must be constructed via new'); if (typeof e !== 'function') throw new TypeError('not a function'); this._state = 0, this._handled = !1, this._value = undefined, this._deferreds = [], c(e, this); } function r (e, n) { for (;e._state === 3;)e = e._value; e._state !== 0 ? (e._handled = !0, o._immediateFn(function () { var t = e._state === 1 ? n.onFulfilled : n.onRejected; if (t !== null) { var o; try { o = t(e._value); } catch (r) { return void f(n.promise, r); }i(n.promise, o); } else (e._state === 1 ? i : f)(n.promise, e._value); })) : e._deferreds.push(n); } function i (e, n) { try { if (n === e) throw new TypeError('A promise cannot be resolved with itself.'); if (n && (typeof n === 'object' || typeof n === 'function')) { var t = n.then; if (n instanceof o) return e._state = 3, e._value = n, void u(e); if (typeof t === 'function') return void c((function (e, n) { return function () { e.apply(n, arguments); }; }(t, n)), e); }e._state = 1, e._value = n, u(e); } catch (r) { f(e, r); } } function f (e, n) { e._state = 2, e._value = n, u(e); } function u (e) { e._state === 2 && e._deferreds.length === 0 && o._immediateFn(function () { e._handled || o._unhandledRejectionFn(e._value); }); for (var n = 0, t = e._deferreds.length; t > n; n++)r(e, e._deferreds[n]); e._deferreds = null; } function c (e, n) { var t = !1; try { e(function (e) { t || (t = !0, i(n, e)); }, function (e) { t || (t = !0, f(n, e)); }); } catch (o) { if (t) return; t = !0, f(n, o); } } var a = setTimeout; o.prototype['catch'] = function (e) { return this.then(null, e); }, o.prototype.then = function (e, n) { var o = new this.constructor(t); return r(this, new function (e, n, t) { this.onFulfilled = typeof e === 'function' ? e : null, this.onRejected = typeof n === 'function' ? n : null, this.promise = t; }(e, n, o)), o; }, o.prototype['finally'] = e, o.all = function (e) { return new o(function (t, o) { function r (e, n) { try { if (n && (typeof n === 'object' || typeof n === 'function')) { var u = n.then; if (typeof u === 'function') return void u.call(n, function (n) { r(e, n); }, o); }i[e] = n, --f == 0 && t(i); } catch (c) { o(c); } } if (!n(e)) return o(new TypeError('Promise.all accepts an array')); var i = Array.prototype.slice.call(e); if (i.length === 0) return t([]); for (var f = i.length, u = 0; i.length > u; u++)r(u, i[u]); }); }, o.resolve = function (e) { return e && typeof e === 'object' && e.constructor === o ? e : new o(function (n) { n(e); }); }, o.reject = function (e) { return new o(function (n, t) { t(e); }); }, o.race = function (e) { return new o(function (t, r) { if (!n(e)) return r(new TypeError('Promise.race accepts an array')); for (var i = 0, f = e.length; f > i; i++)o.resolve(e[i]).then(t, r); }); }, o._immediateFn = typeof setImmediate === 'function' && function (e) { setImmediate(e); } || function (e) { a(e, 0); }, o._unhandledRejectionFn = function (e) { void 0 !== console && console && console.warn('Possible Unhandled Promise Rejection:', e); }; var l = (function () { if (typeof self !== 'undefined') return self; if (typeof window !== 'undefined') return window; if (typeof global !== 'undefined') return global; throw Error('unable to locate global object'); }()); 'Promise' in l ? l.Promise.prototype['finally'] || (l.Promise.prototype['finally'] = e) : l.Promise = o; })); +/*eslint-enable */ + /* eslint-env jasmine */ exports.defineAutoTests = function () { /* eslint-disable no-undef */ @@ -11,7 +18,7 @@ exports.defineAutoTests = function () { jasmine.DEFAULT_TIMEOUT_INTERVAL = 9000; var appId = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID; - var videoUrl = 'https://archive.org/download/CosmosLaundromatFirstCycle/Cosmos%20Laundromat%20-%20First%20Cycle%20%281080p%29.mp4' + var videoUrl = 'https://archive.org/download/CosmosLaundromatFirstCycle/Cosmos%20Laundromat%20-%20First%20Cycle%20%281080p%29.mp4'; describe('chrome.cast', function () { @@ -628,46 +635,57 @@ exports.defineAutoTests = function () { function startRouteScan () { return new Promise(function (resolve) { - var once = true; + var finished = false; chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { - if (once && routes.length > 0) { - once = false; - - var route = routes[0]; + if (finished) { + return; + } + for (var i = 0; i < routes.length; i++) { + var route = routes[i]; test(route).toBeInstanceOf(chrome.cast.cordova.Route); test(route.id).toBeDefined(); test(route.name).toBeDefined(); - + test(route.isNearbyDevice).toBeDefined(); + if (!route.isNearbyDevice) { + finished = true; + } + } + if (finished) { resolve(routes); } }, function (err) { fail(err.code + ': ' + err.description); - resolve(); }); }); } - function stopRouteScan (routes) { + function stopRouteScan (arg) { return new Promise(function (resolve) { // Make sure we can stop the scan chrome.cast.cordova.stopRouteScan(function () { - resolve(routes); + resolve(arg); }, function (err) { test().fail(err.code + ': ' + err.description); - resolve(); }); }); } function selectRoute (routes) { return new Promise(function (resolve) { - chrome.cast.cordova.selectRoute(routes[0], function (session) { + // Find a non-nearby device so that the join is automatic + var route; + for (var i = 0; i < routes.length; i++) { + route = routes[i]; + if (!route.isNearbyDevice) { + break; + } + } + chrome.cast.cordova.selectRoute(route, function (session) { Promise.resolve(session) .then(sessionProperties) .then(resolve); }, function (err) { fail(err.code + ': ' + err.description); - resolve(routes); }); }); } diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 45fd6f3..17e916c 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -523,9 +523,6 @@ chrome.cast = { } }; -var _sessionRequest = null; -var _autoJoinPolicy = null; -var _defaultActionPolicy = null; var _sessionListener = function () {}; var _receiverListener = function () {}; @@ -545,14 +542,12 @@ chrome.cast.initialize = function (apiConfig, successCallback, errorCallback) { return; } - _autoJoinPolicy = apiConfig.autoJoinPolicy; - _defaultActionPolicy = apiConfig.defaultActionPolicy; - _sessionRequest = apiConfig.sessionRequest; - _sessionListener = apiConfig.sessionListener; - _receiverListener = apiConfig.receiverListener; - - execute('initialize', _sessionRequest.appId, _autoJoinPolicy, _defaultActionPolicy, function (err) { + execute('initialize', apiConfig.sessionRequest.appId, apiConfig.autoJoinPolicy, apiConfig.defaultActionPolicy, function (err) { if (!err) { + // Don't set the listeners config until success + _sessionListener = apiConfig.sessionListener; + _receiverListener = apiConfig.receiverListener; + successCallback(); chrome.cast._.receiverUpdate(false); } else { @@ -1139,7 +1134,8 @@ chrome.cast.cordova = { execute('startRouteScan', function (err, routes) { if (!err) { for (var i = 0; i < routes.length; i++) { - routes[i] = new chrome.cast.cordova.Route(routes[i].id, routes[i].name); + var route = routes[i]; + routes[i] = new chrome.cast.cordova.Route(route.id, route.name, route.isNearbyDevice); } successCallback(routes); } else { @@ -1176,9 +1172,10 @@ chrome.cast.cordova = { } }); }, - Route: function (id, name) { + Route: function (id, name, isNearbyDevice) { this.id = id; this.name = name; + this.isNearbyDevice = isNearbyDevice; } }; From 1b066d1ed36c56ef1bedf6d7be4e16041298eba8 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 15 Sep 2019 08:16:11 -0600 Subject: [PATCH 038/166] Fixed up mediaLoaded. It should be called whenever a new media is loaded, even by an external sender. Towards issue #36 --- src/android/Chromecast.java | 23 +++++++++-------- src/android/ChromecastConnection.java | 7 +++-- src/android/ChromecastSession.java | 37 +++++++++++++++++---------- tests/tests.js | 25 ++++++++++-------- www/chrome.cast.js | 7 ++--- 5 files changed, 57 insertions(+), 42 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 44c3ca8..7989c3d 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -38,21 +38,23 @@ protected void pluginInitialize() { this.media = new ChromecastSession(cordova.getActivity(), new ChromecastSession.Listener() { @Override - public void onSessionUpdate(JSONObject session) { - sendJavascript("chrome.cast._.sessionUpdated(" + session + ");"); + public void onSessionUpdate(JSONObject jsonSession) { + sendJavascript("chrome.cast._.sessionUpdated(" + jsonSession + ");"); } @Override - public void onMediaUpdate(JSONObject mediaObject) { - sendJavascript("chrome.cast._.mediaUpdated(true, " + mediaObject + ");"); + public void onMediaLoaded(JSONObject jsonMedia) { + sendJavascript("chrome.cast._.mediaLoaded(" + jsonMedia + ");"); } @Override - public void onMessageReceived(CastDevice castDevice, String namespace, String message) { + public void onMediaUpdate(JSONObject jsonMedia) { + sendJavascript("chrome.cast._.mediaUpdated(true, " + jsonMedia + ");"); + } + @Override + public void onMessageReceived(CastDevice device, String namespace, String message) { try { - JSONObject json = new JSONObject(message); - - sendJavascript("chrome.cast._.onMessage('" + namespace + "', '" + json + "')"); - - + JSONObject jsonMessage = new JSONObject(message); + sendJavascript( + "chrome.cast._.onMessage('" + namespace + "', '" + jsonMessage + "')"); } catch (Exception e) { e.printStackTrace(); } @@ -283,7 +285,6 @@ public boolean loadMedia(String contentId, JSONObject customData, String content private boolean loadMedia(String contentId, JSONObject customData, String contentType, Integer duration, String streamType, Boolean autoPlay, Double currentTime, JSONObject metadata, JSONObject textTrackStyle, final CallbackContext callbackContext) { this.media.loadMedia(contentId, customData, contentType, duration, streamType, autoPlay, currentTime, metadata, textTrackStyle, callbackContext); -// sendJavascript("chrome.cast._.mediaLoaded(true, " + media.createMediaObject().toString() + ");"); return true; } diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index 2f7417f..c741646 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -96,8 +96,11 @@ public void run() { lookForAvailableReceiver(new Runnable() { @Override public void run() { - // Update the session - setSession(getSessionManager().getCurrentCastSession()); + // Found a receiver + // Since there is an available receiver we may have an active session + CastSession session = getSessionManager().getCurrentCastSession(); + // so update the session + setSession(session); if (session != null) { // Let the client know we have found a session onSessionFound.run(); diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index 63f622c..35b3d32 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -59,43 +59,56 @@ public ChromecastSession(Activity act, @NonNull Listener listener) { public void setSession(CastSession castSession) { activity.runOnUiThread(new Runnable() { public void run() { - session = castSession; - if (session == null) { + if (castSession == null) { client = null; return; } + if (castSession.equals(session)) { + // Don't client and listeners if session did not change + return; + } + session = castSession; client = session.getRemoteMediaClient(); + if (client == null) { + return; + } client.registerCallback(new RemoteMediaClient.Callback() { + private String currentMedia = ""; @Override public void onStatusUpdated() { super.onStatusUpdated(); clientListener.onMediaUpdate(createMediaObject()); } - @Override public void onMetadataUpdated() { super.onMetadataUpdated(); + MediaInfo info = client.getMediaInfo(); + if (info == null) { + currentMedia = ""; + } else { + String newMedia = info.getContentId(); + if (!currentMedia.equals(newMedia)) { + currentMedia = newMedia; + clientListener.onMediaLoaded(createMediaObject()); + } + } clientListener.onMediaUpdate(createMediaObject()); } - @Override public void onQueueStatusUpdated() { super.onQueueStatusUpdated(); clientListener.onMediaUpdate(createMediaObject()); } - @Override public void onPreloadStatusUpdated() { super.onPreloadStatusUpdated(); clientListener.onMediaUpdate(createMediaObject()); } - @Override public void onSendingRemoteMediaRequest() { super.onSendingRemoteMediaRequest(); clientListener.onMediaUpdate(createMediaObject()); } - @Override public void onAdBreakStatusUpdated() { super.onAdBreakStatusUpdated(); @@ -108,31 +121,26 @@ public void onApplicationStatusChanged() { super.onApplicationStatusChanged(); clientListener.onSessionUpdate(createSessionObject()); } - @Override public void onApplicationMetadataChanged(ApplicationMetadata applicationMetadata) { super.onApplicationMetadataChanged(applicationMetadata); clientListener.onSessionUpdate(createSessionObject()); } - @Override public void onApplicationDisconnected(int i) { super.onApplicationDisconnected(i); onSessionEnd("stopped"); } - @Override public void onActiveInputStateChanged(int i) { super.onActiveInputStateChanged(i); clientListener.onSessionUpdate(createSessionObject()); } - @Override public void onStandbyStateChanged(int i) { super.onStandbyStateChanged(i); clientListener.onSessionUpdate(createSessionObject()); } - @Override public void onVolumeChanged() { super.onVolumeChanged(); @@ -746,7 +754,8 @@ private JSONObject createMediaInfoObject() { } interface Listener extends Cast.MessageReceivedCallback { - void onMediaUpdate(JSONObject session); - void onSessionUpdate(JSONObject session); + void onMediaLoaded(JSONObject jsonMedia); + void onMediaUpdate(JSONObject jsonMedia); + void onSessionUpdate(JSONObject jsonSession); } } diff --git a/tests/tests.js b/tests/tests.js index 4188c0b..ed7e446 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -256,17 +256,14 @@ exports.defineAutoTests = function () { } function loadMediaVideo (session) { + var specName = loadMediaVideo.name; + var success = 'success'; + var loaded = 'loaded'; return new Promise(function (resolve) { - var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); - test(mediaInfo).toBeDefined(); - - var request = new chrome.cast.media.LoadRequest(mediaInfo); - test(request).toBeDefined(); - - session.loadMedia(request, function (media) { - + var loadedMedia; + var called = callOrder(specName, [success, loaded], { anyOrder: true }, function () { // Run all the media related tests - Promise.resolve({media: media, session: session}) + Promise.resolve({media: loadedMedia, session: session}) .then(mediaProperties) .then(pauseSuccess) .then(playSuccess) @@ -278,7 +275,15 @@ exports.defineAutoTests = function () { .then(function (media) { resolve(session); }); - + }); + session.addMediaListener(function (media) { + called(loaded); + }); + session.loadMedia(new chrome.cast.media.LoadRequest( + new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4') + ), function (media) { + loadedMedia = media; + called(success); }, function (err) { test().fail(err.code + ': ' + err.description); }); diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 17e916c..3d619aa 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -1246,16 +1246,13 @@ chrome.cast._ = { } _currentMedia.emit('_mediaUpdated', isAlive); }, - mediaLoaded: function (isAlive, media) { + mediaLoaded: function (media) { if (_session) { - if (!_currentMedia) { _currentMedia = new chrome.cast.media.Media(media.sessionId, media.mediaSessionId); } - _currentMedia._update(isAlive, media); + _currentMedia._update(true, media); _session.emit('_mediaListener', _currentMedia); - } else { - console.log('mediaLoaded --- but there is no session tied to it', media); } }, sessionListener: function (javaSession) { From 188ffe0bb01504cb0df1bbb83514d2f8e61371fe Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 15 Sep 2019 09:41:13 -0600 Subject: [PATCH 039/166] _mediaUpdated event is already fired by Media.prototype._update, so we are emitting twice each update. In the case that we are creating a new media object, it is impossible for there to be any listeners on it yet, so no point in emitting. Towards issue #36 --- www/chrome.cast.js | 1 - 1 file changed, 1 deletion(-) diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 3d619aa..0b22445 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -1244,7 +1244,6 @@ chrome.cast._ = { _session.media[0] = _currentMedia; } - _currentMedia.emit('_mediaUpdated', isAlive); }, mediaLoaded: function (media) { if (_session) { From 314b33ee8c6f5d1936708c5b04684e1f62a55384 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 15 Sep 2019 10:10:19 -0600 Subject: [PATCH 040/166] Only ChromecastSession needs to hold a reference to session. Since we are keeping scanForRoutes, might as well use it while checking for available receivers. Make the JSON creator functions static so they aren't state dependent. Issue #36 --- src/android/Chromecast.java | 27 +++-- src/android/ChromecastConnection.java | 144 ++++++++------------------ src/android/ChromecastSession.java | 94 +++++++++-------- 3 files changed, 109 insertions(+), 156 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 7989c3d..248e7fa 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -37,11 +37,19 @@ protected void pluginInitialize() { super.pluginInitialize(); this.media = new ChromecastSession(cordova.getActivity(), new ChromecastSession.Listener() { + @Override + public void onSessionRejoined(JSONObject jsonSession) { + sendJavascript("chrome.cast._.sessionListener(" + jsonSession + ");"); + } @Override public void onSessionUpdate(JSONObject jsonSession) { sendJavascript("chrome.cast._.sessionUpdated(" + jsonSession + ");"); } @Override + public void onReceiverAvailableUpdate(boolean available) { + sendJavascript("chrome.cast._.receiverUpdate(" + available + ")"); + } + @Override public void onMediaLoaded(JSONObject jsonMedia) { sendJavascript("chrome.cast._.mediaLoaded(" + jsonMedia + ");"); } @@ -60,12 +68,7 @@ public void onMessageReceived(CastDevice device, String namespace, String messag } } }); - this.connection = new ChromecastConnection(cordova.getActivity(), this.media, new ChromecastConnection.Listener() { - @Override - void onReceiverAvailableUpdate(boolean available) { - sendJavascript("chrome.cast._.receiverUpdate(" + available + ")"); - } - }); + this.connection = new ChromecastConnection(cordova.getActivity(), this.media); } @Override @@ -151,13 +154,7 @@ public boolean setup(CallbackContext callbackContext) { * @return true for cordova */ public boolean initialize(final String appId, String autoJoinPolicy, String defaultActionPolicy, final CallbackContext callbackContext) { - connection.initialize(appId, callbackContext, new Runnable() { - @Override - public void run() { - // We found a session! - sendJavascript("chrome.cast._.sessionListener(" + media.createSessionObject().toString() + ");"); - } - }); + connection.initialize(appId, callbackContext); return true; } @@ -173,7 +170,7 @@ public boolean requestSession(final CallbackContext callbackContext) { connection.showConnectionDialog(new ChromecastConnection.JoinCallback() { @Override public void onJoin(CastSession session) { - callbackContext.success(media.createSessionObject()); + callbackContext.success(ChromecastSession.createSessionObject(session)); } public void onError(String errorCode) { if (errorCode.equals("CANCEL")) { @@ -198,7 +195,7 @@ public boolean selectRoute(final String routeId, final String routeName, final C connection.join(routeId, routeName, new ChromecastConnection.JoinCallback() { @Override public void onJoin(CastSession castSession) { - callbackContext.success(media.createSessionObject()); + callbackContext.success(ChromecastSession.createSessionObject(castSession)); } @Override diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index c741646..1e4bf66 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -32,8 +32,7 @@ public class ChromecastConnection { private Activity activity; /** settings object. */ private SharedPreferences settings; - /** Lifetime variable. */ - private ChromecastSession media; + /** Lifetime variable. */ private SessionListener newConnectionListener; /** The Listener callback. */ @@ -42,33 +41,19 @@ public class ChromecastConnection { /** Initialize lifetime variable. */ private String appId; - /** Session lifetime variable. */ - private CastSession session; - /** * Constructor. * @param act the current context - * @param chromecastSession the chromecastSession object that we should load with a new sessions * @param connectionListener client callbacks for specific events */ - public ChromecastConnection(Activity act, ChromecastSession chromecastSession, Listener connectionListener) { + public ChromecastConnection(Activity act, Listener connectionListener) { this.activity = act; this.settings = activity.getSharedPreferences("CORDOVA-PLUGIN-CHROMECAST_ChromecastConnection", 0); - this.media = chromecastSession; this.appId = settings.getString("appId", CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID); this.listener = connectionListener; // Set the initial appId CastOptionsProvider.setAppId(appId); - // Add the permanent session end/disconnect, and resume listener, and receiver update listener - getSessionManager().addSessionManagerListener(new SessionListener() { - @Override - public void onSessionResumed(CastSession castSession, boolean wasSuspended) { - // This catches any sessions we are able to rejoin - setSession(castSession); - } - }, CastSession.class); - // This is the first call to getContext which will start up the // CastContext and prep it for searching for a session to rejoin // Also adds the receiver update callback @@ -79,13 +64,12 @@ public void onSessionResumed(CastSession castSession, boolean wasSuspended) { * Must be called each time the appId changes and at least once before any other method is called. * @param applicationId the app id to use * @param callback called when initialization is complete - * @param onSessionFound called when (if) an active session is found */ - public void initialize(String applicationId, CallbackContext callback, Runnable onSessionFound) { + public void initialize(String applicationId, CallbackContext callback) { activity.runOnUiThread(new Runnable() { public void run() { - // If the app Id changed, get it again + // If the app Id changed, set it again if (!applicationId.equals(appId)) { setAppId(applicationId); } @@ -93,76 +77,31 @@ public void run() { // Tell the client that initialization was a success callback.success(); - lookForAvailableReceiver(new Runnable() { + // Check if there is any available receivers for 5 seconds + scanForRoutes(5000L, new ScanCallback() { @Override - public void run() { - // Found a receiver - // Since there is an available receiver we may have an active session - CastSession session = getSessionManager().getCurrentCastSession(); - // so update the session - setSession(session); - if (session != null) { - // Let the client know we have found a session - onSessionFound.run(); + void onRouteUpdate(List routes) { + // if the routes have changed, we may have an available device + // If there is at least one device available + if (getContext().getCastState() != CastState.NO_DEVICES_AVAILABLE) { + // Stop the scan + stopScan(this); + // Let the client know a receiver is available + listener.onReceiverAvailableUpdate(true); + // Since we have a receiver we may also have an active session + CastSession session = getSessionManager().getCurrentCastSession(); + // If we do have a session + if (session != null) { + // Let the client know + listener.onSessionRejoined(session); + } } } - }); + }, null); } }); } - /** - * Must be called from the main thread. - * @param foundReceiver called if a receiver is found - */ - private void lookForAvailableReceiver(Runnable foundReceiver) { - // check current state, if we already have a receiver - if (getContext().getCastState() != CastState.NO_DEVICES_AVAILABLE) { - // Notify and return - listener.onReceiverAvailableUpdate(true); - foundReceiver.run(); - return; - } - - // Create callbacks - MediaRouter.Callback mediaRouterCallback = new MediaRouter.Callback() { }; - CastStateListener castStateListener = new CastStateListener() { - @Override - public void onCastStateChanged(int state) { - // If there is a receiver - if (getContext().getCastState() != CastState.NO_DEVICES_AVAILABLE) { - // Remove callbacks - getContext().removeCastStateListener(this); - getMediaRouter().removeCallback(mediaRouterCallback); - // And let the client know we found a receiver - foundReceiver.run(); - } - } - }; - - // Listen for any available receiver - getContext().addCastStateListener(castStateListener); - - - // Start an active scan for available routes - getMediaRouter().addCallback(new MediaRouteSelector.Builder() - .addControlCategory(CastMediaControlIntent.categoryForCast(appId)) - .build(), - mediaRouterCallback, - MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); - - // If we didn't find any routes by 5 seconds remove the callbacks - new Handler().postDelayed(new Runnable() { - @Override - public void run() { - // And stop the scan for routes - getMediaRouter().removeCallback(mediaRouterCallback); - // And remove the castStateListener callback - getContext().removeCastStateListener(castStateListener); - } - }, 5000); - } - private MediaRouter getMediaRouter() { return MediaRouter.getInstance(activity); } @@ -175,17 +114,16 @@ private SessionManager getSessionManager() { return getContext().getSessionManager(); } + private CastSession getSession() { + return getSessionManager().getCurrentCastSession(); + } + private void setAppId(String applicationId) { this.appId = applicationId; this.settings.edit().putString("appId", appId).apply(); getContext().setReceiverApplicationId(appId); // Invalidate any old session - this.setSession(null); - } - - private void setSession(CastSession castSession) { - this.session = castSession; - this.media.setSession(this.session); + listener.onInvalidateSession(); } /** @@ -198,9 +136,9 @@ private void setSession(CastSession castSession) { public void join(final String routeId, final String routeName, JoinCallback callback) { activity.runOnUiThread(new Runnable() { public void run() { - if (session != null) { + if (getSession() != null) { // We are are already connected to a route - callback.onJoin(session); + callback.onJoin(getSession()); return; } @@ -274,6 +212,7 @@ public void run() { public void showConnectionDialog(JoinCallback callback) { activity.runOnUiThread(new Runnable() { public void run() { + CastSession session = getSession(); if (session == null) { // show the "choose a connection" dialog @@ -330,13 +269,12 @@ private void listenForConnection(JoinCallback callback) { @Override public void onSessionStarted(CastSession castSession, String sessionId) { getSessionManager().removeSessionManagerListener(this, CastSession.class); - setSession(castSession); - callback.onJoin(session); + listener.onSessionStarted(castSession); + callback.onJoin(castSession); } @Override public void onSessionStartFailed(CastSession castSession, int errCode) { getSessionManager().removeSessionManagerListener(this, CastSession.class); - setSession(null); callback.onError(Integer.toString(errCode)); } }; @@ -358,10 +296,9 @@ public void scanForRoutes(Long timeout, ScanCallback callback, Runnable onTimeou public void run() { callback.setMediaRouter(getMediaRouter()); - // Send out the initial routes - callback.onFilteredRouteUpdate(); - if (timeout != null && timeout == 0) { + // Send out the one time routes + callback.onFilteredRouteUpdate(); return; } @@ -372,6 +309,12 @@ public void run() { callback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); + // Send out the initial routes after the callback has been added. + // This is important because if the callback calls stopScan only once, and it + // happens during this call of "onFilterRouteUpdate", there must actually be an + // added callback to remove to stop the scan. + callback.onFilteredRouteUpdate(); + if (timeout != null) { // remove the callback after timeout ms, and notify caller new Handler().postDelayed(new Runnable() { @@ -415,8 +358,7 @@ public void run() { @Override public void onSessionEnded(CastSession castSession, int error) { getSessionManager().removeSessionManagerListener(this, CastSession.class); - setSession(null); - media.onSessionEnd(stopCasting ? "stopped" : "disconnected"); + listener.onSessionEnd(castSession, stopCasting ? "stopped" : "disconnected"); } }, CastSession.class); @@ -538,6 +480,10 @@ public final void onRouteRemoved(MediaRouter router, RouteInfo route) { abstract static class Listener implements CastStateListener { abstract void onReceiverAvailableUpdate(boolean available); + abstract void onSessionStarted(CastSession castSession); + abstract void onSessionRejoined(CastSession castSession); + abstract void onSessionEnd(CastSession castSession, String state); + abstract void onInvalidateSession(); /** CastStateListener functions. */ @Override diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index 35b3d32..0a7cc79 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -31,7 +31,7 @@ /* * All of the Chromecast session specific functions should start here. */ -public class ChromecastSession { +public class ChromecastSession extends ChromecastConnection.Listener { /** The current context. */ private Activity activity; @@ -129,7 +129,7 @@ public void onApplicationMetadataChanged(ApplicationMetadata applicationMetadata @Override public void onApplicationDisconnected(int i) { super.onApplicationDisconnected(i); - onSessionEnd("stopped"); + onSessionEnd(session, "stopped"); } @Override public void onActiveInputStateChanged(int i) { @@ -151,8 +151,24 @@ public void onVolumeChanged() { }); } - final void onSessionEnd(String state) { - JSONObject s = createSessionObject(); + /** ChromecastConnection.Listener implementations. */ + @Override + void onInvalidateSession() { + setSession(null); + } + @Override + final void onSessionStarted(CastSession castSession) { + setSession(castSession); + } + @Override + final void onSessionRejoined(CastSession castSession) { + setSession(castSession); + clientListener.onSessionRejoined(createSessionObject()); + } + @Override + final void onSessionEnd(CastSession castSession, String state) { + onInvalidateSession(); + JSONObject s = createSessionObject(castSession); if (state != null) { try { s.put("status", state); @@ -162,6 +178,10 @@ final void onSessionEnd(String state) { } clientListener.onSessionUpdate(s); } + @Override + void onReceiverAvailableUpdate(boolean available) { + clientListener.onReceiverAvailableUpdate(available); + } /** * Adds a message listener if one does not already exist. @@ -552,20 +572,19 @@ public void onResult(@NonNull MediaChannelResult result) { }; } - /** - * Creates a JSON representation of this session. - * @return a JSON representation of this session - */ - public JSONObject createSessionObject() { + private JSONObject createSessionObject() { + return createSessionObject(session); + } + static JSONObject createSessionObject(CastSession session) { JSONObject out = new JSONObject(); try { out.put("appId", session.getApplicationMetadata().getApplicationId()); - out.put("appImages", createAppImagesObject()); + out.put("appImages", createAppImagesObject(session)); out.put("displayName", session.getApplicationMetadata().getName()); - out.put("media", createMediaObject()); - out.put("receiver", createReceiverObject()); - out.put("sessionId", this.session.getSessionId()); + out.put("media", createMediaObject(session)); + out.put("receiver", createReceiverObject(session)); + out.put("sessionId", session.getSessionId()); } catch (JSONException e) { e.printStackTrace(); @@ -576,10 +595,10 @@ public JSONObject createSessionObject() { return out; } - private JSONArray createAppImagesObject() { + private static JSONArray createAppImagesObject(CastSession session) { JSONArray appImages = new JSONArray(); try { - MediaMetadata metadata = client.getMediaInfo().getMetadata(); + MediaMetadata metadata = session.getRemoteMediaClient().getMediaInfo().getMetadata(); List images = metadata.getImages(); if (images != null) { for (WebImage o : images) { @@ -592,11 +611,11 @@ private JSONArray createAppImagesObject() { return appImages; } - private JSONObject createReceiverObject() { + private static JSONObject createReceiverObject(CastSession session) { JSONObject out = new JSONObject(); try { - out.put("friendlyName", this.session.getCastDevice().getFriendlyName()); - out.put("label", this.session.getCastDevice().getDeviceId()); + out.put("friendlyName", session.getCastDevice().getFriendlyName()); + out.put("label", session.getCastDevice().getDeviceId()); JSONObject volume = new JSONObject(); try { @@ -615,16 +634,15 @@ private JSONObject createReceiverObject() { return out; } - /** - * Creates a JSON representation of the current playing media. - * @return a JSON representation of the current playing media - */ - public JSONObject createMediaObject() { + private JSONObject createMediaObject() { + return createMediaObject(session); + } + + private static JSONObject createMediaObject(CastSession session) { JSONObject out = new JSONObject(); try { - MediaStatus mediaStatus = client.getMediaStatus(); - + MediaStatus mediaStatus = session.getRemoteMediaClient().getMediaStatus(); // TODO: Missing attributes are commented out. // These are returned by the chromecast desktop SDK, we should probbaly return them too @@ -637,14 +655,14 @@ public JSONObject createMediaObject() { //out.put("items", mediaStatus.getQueueItems()); //out.put("liveSeekableRange",); out.put("loadingItemId", mediaStatus.getLoadingItemId()); - out.put("media", this.createMediaInfoObject()); + out.put("media", createMediaInfoObject(session)); out.put("mediaSessionId", 1); out.put("playbackRate", mediaStatus.getPlaybackRate()); out.put("playerState", ChromecastUtilities.getMediaPlayerState(mediaStatus)); out.put("preloadedItemId", mediaStatus.getPreloadedItemId()); //out.put("queueData", ); //out.put("repeatMode", mediaStatus.getQueueRepeatMode()); - out.put("sessionId", this.session.getSessionId()); + out.put("sessionId", session.getSessionId()); //out.put("supportedMediaCommands", ); //out.put("videoInfo", ); @@ -672,15 +690,11 @@ public JSONObject createMediaObject() { return out; } - /** - * Creates a JSON representation of all Tracks available in the current media. - * @return a JSON representation of all Tracks available in the current media - */ - private JSONArray createMediaInfoTracks() { + private static JSONArray createMediaInfoTracks(CastSession session) { JSONArray out = new JSONArray(); try { - MediaStatus mediaStatus = client.getMediaStatus(); + MediaStatus mediaStatus = session.getRemoteMediaClient().getMediaStatus(); MediaInfo mediaInfo = mediaStatus.getMediaInfo(); if (mediaInfo.getMediaTracks() == null) { @@ -714,19 +728,13 @@ private JSONArray createMediaInfoTracks() { return out; } - - /** - * Creates a JSON representation of current MediaInfo of the session. - * @return a JSON representation of current MediaInfo of the session - */ - private JSONObject createMediaInfoObject() { + private static JSONObject createMediaInfoObject(CastSession session) { JSONObject out = new JSONObject(); try { - MediaStatus mediaStatus = client.getMediaStatus(); + MediaStatus mediaStatus = session.getRemoteMediaClient().getMediaStatus(); MediaInfo mediaInfo = mediaStatus.getMediaInfo(); - // TODO: Missing attributes are commented out. // These are returned by the chromecast desktop SDK, we should probbaly return them too //out.put("breakClips",); @@ -739,7 +747,7 @@ private JSONObject createMediaInfoObject() { out.put("duration", mediaInfo.getStreamDuration() / 1000.0); //out.put("mediaCategory",); out.put("streamType", ChromecastUtilities.getMediaInfoStreamType(mediaInfo)); - out.put("tracks", this.createMediaInfoTracks()); + out.put("tracks", createMediaInfoTracks(session)); out.put("textTrackStyle", ChromecastUtilities.createTextTrackObject(mediaInfo.getTextTrackStyle())); // TODO: Check if it's useful @@ -756,6 +764,8 @@ private JSONObject createMediaInfoObject() { interface Listener extends Cast.MessageReceivedCallback { void onMediaLoaded(JSONObject jsonMedia); void onMediaUpdate(JSONObject jsonMedia); + void onSessionRejoined(JSONObject jsonSession); void onSessionUpdate(JSONObject jsonSession); + void onReceiverAvailableUpdate(boolean available); } } From 605e2a34e7b2c7ef3e9fd047dd3c021e93f430a6 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 15 Sep 2019 10:38:43 -0600 Subject: [PATCH 041/166] (android) (Refactor) Move JSONObject creator functions to utilities. --- src/android/Chromecast.java | 4 +- src/android/ChromecastSession.java | 224 ++------------------------- src/android/ChromecastUtilities.java | 188 ++++++++++++++++++++++ 3 files changed, 206 insertions(+), 210 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 248e7fa..4088bd4 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -170,7 +170,7 @@ public boolean requestSession(final CallbackContext callbackContext) { connection.showConnectionDialog(new ChromecastConnection.JoinCallback() { @Override public void onJoin(CastSession session) { - callbackContext.success(ChromecastSession.createSessionObject(session)); + callbackContext.success(ChromecastUtilities.createSessionObject(session)); } public void onError(String errorCode) { if (errorCode.equals("CANCEL")) { @@ -195,7 +195,7 @@ public boolean selectRoute(final String routeId, final String routeName, final C connection.join(routeId, routeName, new ChromecastConnection.JoinCallback() { @Override public void onJoin(CastSession castSession) { - callbackContext.success(ChromecastSession.createSessionObject(castSession)); + callbackContext.success(ChromecastUtilities.createSessionObject(castSession)); } @Override diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index 0a7cc79..fdbd4e0 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -1,7 +1,6 @@ package acidhax.cordova.chromecast; import java.io.IOException; -import java.util.List; import org.apache.cordova.CallbackContext; import org.json.JSONArray; @@ -14,8 +13,6 @@ import com.google.android.gms.cast.MediaLoadRequestData; import com.google.android.gms.cast.MediaMetadata; import com.google.android.gms.cast.MediaSeekOptions; -import com.google.android.gms.cast.MediaStatus; -import com.google.android.gms.cast.MediaTrack; import com.google.android.gms.cast.TextTrackStyle; import com.google.android.gms.cast.framework.CastSession; import com.google.android.gms.cast.framework.media.RemoteMediaClient; @@ -77,7 +74,7 @@ public void run() { @Override public void onStatusUpdated() { super.onStatusUpdated(); - clientListener.onMediaUpdate(createMediaObject()); + clientListener.onMediaUpdate(ChromecastUtilities.createMediaObject(session)); } @Override public void onMetadataUpdated() { @@ -89,42 +86,42 @@ public void onMetadataUpdated() { String newMedia = info.getContentId(); if (!currentMedia.equals(newMedia)) { currentMedia = newMedia; - clientListener.onMediaLoaded(createMediaObject()); + clientListener.onMediaLoaded(ChromecastUtilities.createMediaObject(session)); } } - clientListener.onMediaUpdate(createMediaObject()); + clientListener.onMediaUpdate(ChromecastUtilities.createMediaObject(session)); } @Override public void onQueueStatusUpdated() { super.onQueueStatusUpdated(); - clientListener.onMediaUpdate(createMediaObject()); + clientListener.onMediaUpdate(ChromecastUtilities.createMediaObject(session)); } @Override public void onPreloadStatusUpdated() { super.onPreloadStatusUpdated(); - clientListener.onMediaUpdate(createMediaObject()); + clientListener.onMediaUpdate(ChromecastUtilities.createMediaObject(session)); } @Override public void onSendingRemoteMediaRequest() { super.onSendingRemoteMediaRequest(); - clientListener.onMediaUpdate(createMediaObject()); + clientListener.onMediaUpdate(ChromecastUtilities.createMediaObject(session)); } @Override public void onAdBreakStatusUpdated() { super.onAdBreakStatusUpdated(); - clientListener.onMediaUpdate(createMediaObject()); + clientListener.onMediaUpdate(ChromecastUtilities.createMediaObject(session)); } }); session.addCastListener(new Cast.Listener() { @Override public void onApplicationStatusChanged() { super.onApplicationStatusChanged(); - clientListener.onSessionUpdate(createSessionObject()); + clientListener.onSessionUpdate(ChromecastUtilities.createSessionObject(session)); } @Override public void onApplicationMetadataChanged(ApplicationMetadata applicationMetadata) { super.onApplicationMetadataChanged(applicationMetadata); - clientListener.onSessionUpdate(createSessionObject()); + clientListener.onSessionUpdate(ChromecastUtilities.createSessionObject(session)); } @Override public void onApplicationDisconnected(int i) { @@ -134,17 +131,17 @@ public void onApplicationDisconnected(int i) { @Override public void onActiveInputStateChanged(int i) { super.onActiveInputStateChanged(i); - clientListener.onSessionUpdate(createSessionObject()); + clientListener.onSessionUpdate(ChromecastUtilities.createSessionObject(session)); } @Override public void onStandbyStateChanged(int i) { super.onStandbyStateChanged(i); - clientListener.onSessionUpdate(createSessionObject()); + clientListener.onSessionUpdate(ChromecastUtilities.createSessionObject(session)); } @Override public void onVolumeChanged() { super.onVolumeChanged(); - clientListener.onSessionUpdate(createSessionObject()); + clientListener.onSessionUpdate(ChromecastUtilities.createSessionObject(session)); } }); } @@ -163,12 +160,12 @@ final void onSessionStarted(CastSession castSession) { @Override final void onSessionRejoined(CastSession castSession) { setSession(castSession); - clientListener.onSessionRejoined(createSessionObject()); + clientListener.onSessionRejoined(ChromecastUtilities.createSessionObject(session)); } @Override final void onSessionEnd(CastSession castSession, String state) { onInvalidateSession(); - JSONObject s = createSessionObject(castSession); + JSONObject s = ChromecastUtilities.createSessionObject(castSession); if (state != null) { try { s.put("status", state); @@ -179,7 +176,7 @@ final void onSessionEnd(CastSession castSession, String state) { clientListener.onSessionUpdate(s); } @Override - void onReceiverAvailableUpdate(boolean available) { + final void onReceiverAvailableUpdate(boolean available) { clientListener.onReceiverAvailableUpdate(available); } @@ -261,7 +258,7 @@ public void run() { @Override public void onResult(@NonNull MediaChannelResult result) { if (result.getStatus().isSuccess()) { - callback.success(createMediaObject()); + callback.success(ChromecastUtilities.createMediaObject(session)); } else { callback.error("session_error"); } @@ -572,195 +569,6 @@ public void onResult(@NonNull MediaChannelResult result) { }; } - private JSONObject createSessionObject() { - return createSessionObject(session); - } - static JSONObject createSessionObject(CastSession session) { - JSONObject out = new JSONObject(); - - try { - out.put("appId", session.getApplicationMetadata().getApplicationId()); - out.put("appImages", createAppImagesObject(session)); - out.put("displayName", session.getApplicationMetadata().getName()); - out.put("media", createMediaObject(session)); - out.put("receiver", createReceiverObject(session)); - out.put("sessionId", session.getSessionId()); - - } catch (JSONException e) { - e.printStackTrace(); - } catch (NullPointerException e) { - e.printStackTrace(); - } - - return out; - } - - private static JSONArray createAppImagesObject(CastSession session) { - JSONArray appImages = new JSONArray(); - try { - MediaMetadata metadata = session.getRemoteMediaClient().getMediaInfo().getMetadata(); - List images = metadata.getImages(); - if (images != null) { - for (WebImage o : images) { - appImages.put(o.toString()); - } - } - } catch (NullPointerException e) { - e.printStackTrace(); - } - return appImages; - } - - private static JSONObject createReceiverObject(CastSession session) { - JSONObject out = new JSONObject(); - try { - out.put("friendlyName", session.getCastDevice().getFriendlyName()); - out.put("label", session.getCastDevice().getDeviceId()); - - JSONObject volume = new JSONObject(); - try { - volume.put("level", session.getVolume()); - volume.put("muted", session.isMute()); - } catch (JSONException e) { - e.printStackTrace(); - } - out.put("volume", volume); - - } catch (JSONException e) { - e.printStackTrace(); - } catch (NullPointerException e) { - e.printStackTrace(); - } - return out; - } - - private JSONObject createMediaObject() { - return createMediaObject(session); - } - - private static JSONObject createMediaObject(CastSession session) { - JSONObject out = new JSONObject(); - - try { - MediaStatus mediaStatus = session.getRemoteMediaClient().getMediaStatus(); - - // TODO: Missing attributes are commented out. - // These are returned by the chromecast desktop SDK, we should probbaly return them too - //out.put("breakStatus",); - out.put("currentItemId", mediaStatus.getCurrentItemId()); - out.put("currentTime", mediaStatus.getStreamPosition() / 1000.0); - out.put("customData", mediaStatus.getCustomData()); - //out.put("extendedStatus",); - out.put("idleReason", ChromecastUtilities.getMediaIdleReason(mediaStatus)); - //out.put("items", mediaStatus.getQueueItems()); - //out.put("liveSeekableRange",); - out.put("loadingItemId", mediaStatus.getLoadingItemId()); - out.put("media", createMediaInfoObject(session)); - out.put("mediaSessionId", 1); - out.put("playbackRate", mediaStatus.getPlaybackRate()); - out.put("playerState", ChromecastUtilities.getMediaPlayerState(mediaStatus)); - out.put("preloadedItemId", mediaStatus.getPreloadedItemId()); - //out.put("queueData", ); - //out.put("repeatMode", mediaStatus.getQueueRepeatMode()); - out.put("sessionId", session.getSessionId()); - //out.put("supportedMediaCommands", ); - //out.put("videoInfo", ); - - - JSONObject volume = new JSONObject(); - volume.put("level", mediaStatus.getStreamVolume()); - volume.put("muted", mediaStatus.isMute()); - out.put("volume", volume); - - long[] activeTrackIds = mediaStatus.getActiveTrackIds(); - if (activeTrackIds != null) { - JSONArray activeTracks = new JSONArray(); - for (long activeTrackId : activeTrackIds) { - activeTracks.put(activeTrackId); - } - out.put("activeTrackIds", activeTracks); - } - - } catch (JSONException e) { - e.printStackTrace(); - } catch (NullPointerException e) { - e.printStackTrace(); - } - - return out; - } - - private static JSONArray createMediaInfoTracks(CastSession session) { - JSONArray out = new JSONArray(); - - try { - MediaStatus mediaStatus = session.getRemoteMediaClient().getMediaStatus(); - MediaInfo mediaInfo = mediaStatus.getMediaInfo(); - - if (mediaInfo.getMediaTracks() == null) { - return out; - } - - for (MediaTrack track : mediaInfo.getMediaTracks()) { - JSONObject jsonTrack = new JSONObject(); - - - // TODO: Missing attributes are commented out. - // These are returned by the chromecast desktop SDK, we should probbaly return them too - - jsonTrack.put("trackId", track.getId()); - jsonTrack.put("customData", track.getCustomData()); - jsonTrack.put("language", track.getLanguage()); - jsonTrack.put("name", track.getName()); - jsonTrack.put("subtype", ChromecastUtilities.getTrackSubtype(track)); - jsonTrack.put("trackContentId", track.getContentId()); - jsonTrack.put("trackContentType", track.getContentType()); - jsonTrack.put("type", ChromecastUtilities.getTrackType(track)); - - out.put(jsonTrack); - } - } catch (JSONException e) { - e.printStackTrace(); - } catch (NullPointerException e) { - e.printStackTrace(); - } - - return out; - } - - private static JSONObject createMediaInfoObject(CastSession session) { - JSONObject out = new JSONObject(); - - try { - MediaStatus mediaStatus = session.getRemoteMediaClient().getMediaStatus(); - MediaInfo mediaInfo = mediaStatus.getMediaInfo(); - - // TODO: Missing attributes are commented out. - // These are returned by the chromecast desktop SDK, we should probbaly return them too - //out.put("breakClips",); - //out.put("breaks",); - out.put("contentId", mediaInfo.getContentId()); - out.put("contentType", mediaInfo.getContentType()); - out.put("customData", mediaInfo.getCustomData()); - //out.put("idleReason",); - //out.put("items",); - out.put("duration", mediaInfo.getStreamDuration() / 1000.0); - //out.put("mediaCategory",); - out.put("streamType", ChromecastUtilities.getMediaInfoStreamType(mediaInfo)); - out.put("tracks", createMediaInfoTracks(session)); - out.put("textTrackStyle", ChromecastUtilities.createTextTrackObject(mediaInfo.getTextTrackStyle())); - - // TODO: Check if it's useful - //out.put("metadata", mediaInfo.getMetadata()); - } catch (JSONException e) { - e.printStackTrace(); - } catch (NullPointerException e) { - e.printStackTrace(); - } - - return out; - } - interface Listener extends Cast.MessageReceivedCallback { void onMediaLoaded(JSONObject jsonMedia); void onMediaUpdate(JSONObject jsonMedia); diff --git a/src/android/ChromecastUtilities.java b/src/android/ChromecastUtilities.java index 74991fe..1062c11 100644 --- a/src/android/ChromecastUtilities.java +++ b/src/android/ChromecastUtilities.java @@ -3,13 +3,19 @@ import android.graphics.Color; import com.google.android.gms.cast.MediaInfo; +import com.google.android.gms.cast.MediaMetadata; import com.google.android.gms.cast.MediaStatus; import com.google.android.gms.cast.MediaTrack; import com.google.android.gms.cast.TextTrackStyle; +import com.google.android.gms.cast.framework.CastSession; +import com.google.android.gms.common.images.WebImage; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.util.List; + final class ChromecastUtilities { private ChromecastUtilities() { @@ -188,6 +194,188 @@ static String getHexColor(int color) { return "#" + Integer.toHexString(color); } + static JSONObject createSessionObject(CastSession session) { + JSONObject out = new JSONObject(); + + try { + out.put("appId", session.getApplicationMetadata().getApplicationId()); + out.put("appImages", createAppImagesObject(session)); + out.put("displayName", session.getApplicationMetadata().getName()); + out.put("media", createMediaObject(session)); + out.put("receiver", createReceiverObject(session)); + out.put("sessionId", session.getSessionId()); + + } catch (JSONException e) { + e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); + } + + return out; + } + + private static JSONArray createAppImagesObject(CastSession session) { + JSONArray appImages = new JSONArray(); + try { + MediaMetadata metadata = session.getRemoteMediaClient().getMediaInfo().getMetadata(); + List images = metadata.getImages(); + if (images != null) { + for (WebImage o : images) { + appImages.put(o.toString()); + } + } + } catch (NullPointerException e) { + e.printStackTrace(); + } + return appImages; + } + + private static JSONObject createReceiverObject(CastSession session) { + JSONObject out = new JSONObject(); + try { + out.put("friendlyName", session.getCastDevice().getFriendlyName()); + out.put("label", session.getCastDevice().getDeviceId()); + + JSONObject volume = new JSONObject(); + try { + volume.put("level", session.getVolume()); + volume.put("muted", session.isMute()); + } catch (JSONException e) { + e.printStackTrace(); + } + out.put("volume", volume); + + } catch (JSONException e) { + e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); + } + return out; + } + + static JSONObject createMediaObject(CastSession session) { + JSONObject out = new JSONObject(); + + try { + MediaStatus mediaStatus = session.getRemoteMediaClient().getMediaStatus(); + + // TODO: Missing attributes are commented out. + // These are returned by the chromecast desktop SDK, we should probbaly return them too + //out.put("breakStatus",); + out.put("currentItemId", mediaStatus.getCurrentItemId()); + out.put("currentTime", mediaStatus.getStreamPosition() / 1000.0); + out.put("customData", mediaStatus.getCustomData()); + //out.put("extendedStatus",); + out.put("idleReason", ChromecastUtilities.getMediaIdleReason(mediaStatus)); + //out.put("items", mediaStatus.getQueueItems()); + //out.put("liveSeekableRange",); + out.put("loadingItemId", mediaStatus.getLoadingItemId()); + out.put("media", createMediaInfoObject(session)); + out.put("mediaSessionId", 1); + out.put("playbackRate", mediaStatus.getPlaybackRate()); + out.put("playerState", ChromecastUtilities.getMediaPlayerState(mediaStatus)); + out.put("preloadedItemId", mediaStatus.getPreloadedItemId()); + //out.put("queueData", ); + //out.put("repeatMode", mediaStatus.getQueueRepeatMode()); + out.put("sessionId", session.getSessionId()); + //out.put("supportedMediaCommands", ); + //out.put("videoInfo", ); + + + JSONObject volume = new JSONObject(); + volume.put("level", mediaStatus.getStreamVolume()); + volume.put("muted", mediaStatus.isMute()); + out.put("volume", volume); + + long[] activeTrackIds = mediaStatus.getActiveTrackIds(); + if (activeTrackIds != null) { + JSONArray activeTracks = new JSONArray(); + for (long activeTrackId : activeTrackIds) { + activeTracks.put(activeTrackId); + } + out.put("activeTrackIds", activeTracks); + } + + } catch (JSONException e) { + e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); + } + + return out; + } + + private static JSONArray createMediaInfoTracks(CastSession session) { + JSONArray out = new JSONArray(); + + try { + MediaStatus mediaStatus = session.getRemoteMediaClient().getMediaStatus(); + MediaInfo mediaInfo = mediaStatus.getMediaInfo(); + + if (mediaInfo.getMediaTracks() == null) { + return out; + } + + for (MediaTrack track : mediaInfo.getMediaTracks()) { + JSONObject jsonTrack = new JSONObject(); + + + // TODO: Missing attributes are commented out. + // These are returned by the chromecast desktop SDK, we should probbaly return them too + + jsonTrack.put("trackId", track.getId()); + jsonTrack.put("customData", track.getCustomData()); + jsonTrack.put("language", track.getLanguage()); + jsonTrack.put("name", track.getName()); + jsonTrack.put("subtype", ChromecastUtilities.getTrackSubtype(track)); + jsonTrack.put("trackContentId", track.getContentId()); + jsonTrack.put("trackContentType", track.getContentType()); + jsonTrack.put("type", ChromecastUtilities.getTrackType(track)); + + out.put(jsonTrack); + } + } catch (JSONException e) { + e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); + } + + return out; + } + + private static JSONObject createMediaInfoObject(CastSession session) { + JSONObject out = new JSONObject(); + + try { + MediaStatus mediaStatus = session.getRemoteMediaClient().getMediaStatus(); + MediaInfo mediaInfo = mediaStatus.getMediaInfo(); + + // TODO: Missing attributes are commented out. + // These are returned by the chromecast desktop SDK, we should probbaly return them too + //out.put("breakClips",); + //out.put("breaks",); + out.put("contentId", mediaInfo.getContentId()); + out.put("contentType", mediaInfo.getContentType()); + out.put("customData", mediaInfo.getCustomData()); + //out.put("idleReason",); + //out.put("items",); + out.put("duration", mediaInfo.getStreamDuration() / 1000.0); + //out.put("mediaCategory",); + out.put("streamType", ChromecastUtilities.getMediaInfoStreamType(mediaInfo)); + out.put("tracks", createMediaInfoTracks(session)); + out.put("textTrackStyle", ChromecastUtilities.createTextTrackObject(mediaInfo.getTextTrackStyle())); + + // TODO: Check if it's useful + //out.put("metadata", mediaInfo.getMetadata()); + } catch (JSONException e) { + e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); + } + + return out; + } + static JSONObject createTextTrackObject(TextTrackStyle textTrackStyle) { JSONObject out = new JSONObject(); try { From f5fdfb77577208e7be899600e7765581c219d133 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 15 Sep 2019 13:55:31 -0600 Subject: [PATCH 042/166] (android) (Refactor) Decouple the 3 way dependency between Chromecast.java, ChromecastConnection.java, and ChromecastSession.java a little bit. Move more JSON creation methods to utilities (createSessionObject with state, and createRoutesArray). connection.selectRoute does not use routeName anymore. Chromecast.java shouldn't know about CastSession if we can avoid it. --- src/android/Chromecast.java | 62 +++++++-------------- src/android/ChromecastConnection.java | 71 +++++++++++++----------- src/android/ChromecastSession.java | 78 ++++++++++----------------- src/android/ChromecastUtilities.java | 40 ++++++++++++++ www/chrome.cast.js | 2 +- 5 files changed, 128 insertions(+), 125 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 4088bd4..c33e08a 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -19,7 +19,6 @@ import androidx.mediarouter.media.MediaRouter.RouteInfo; import com.google.android.gms.cast.CastDevice; -import com.google.android.gms.cast.framework.CastSession; public final class Chromecast extends CordovaPlugin { @@ -36,9 +35,9 @@ public final class Chromecast extends CordovaPlugin { protected void pluginInitialize() { super.pluginInitialize(); - this.media = new ChromecastSession(cordova.getActivity(), new ChromecastSession.Listener() { + this.connection = new ChromecastConnection(cordova.getActivity(), new ChromecastConnection.Listener() { @Override - public void onSessionRejoined(JSONObject jsonSession) { + public void onSessionRejoin(JSONObject jsonSession) { sendJavascript("chrome.cast._.sessionListener(" + jsonSession + ");"); } @Override @@ -46,6 +45,10 @@ public void onSessionUpdate(JSONObject jsonSession) { sendJavascript("chrome.cast._.sessionUpdated(" + jsonSession + ");"); } @Override + public void onSessionEnd(JSONObject jsonSession) { + sendJavascript("chrome.cast._.sessionUpdated(" + jsonSession + ");"); + } + @Override public void onReceiverAvailableUpdate(boolean available) { sendJavascript("chrome.cast._.receiverUpdate(" + available + ")"); } @@ -68,7 +71,7 @@ public void onMessageReceived(CastDevice device, String namespace, String messag } } }); - this.connection = new ChromecastConnection(cordova.getActivity(), this.media); + this.media = connection.getChromecastSession(); } @Override @@ -167,10 +170,10 @@ public boolean initialize(final String appId, String autoJoinPolicy, String defa * @return true for cordova */ public boolean requestSession(final CallbackContext callbackContext) { - connection.showConnectionDialog(new ChromecastConnection.JoinCallback() { + connection.requestSession(new ChromecastConnection.JoinCallback() { @Override - public void onJoin(CastSession session) { - callbackContext.success(ChromecastUtilities.createSessionObject(session)); + public void onJoin(JSONObject jsonSession) { + callbackContext.success(jsonSession); } public void onError(String errorCode) { if (errorCode.equals("CANCEL")) { @@ -187,15 +190,14 @@ public void onError(String errorCode) { /** * Selects a route by its id. * @param routeId the id of the route to join - * @param routeName the name of the route * @param callbackContext called with .success or .error depending on the result * @return true for cordova */ - public boolean selectRoute(final String routeId, final String routeName, final CallbackContext callbackContext) { - connection.join(routeId, routeName, new ChromecastConnection.JoinCallback() { + public boolean selectRoute(final String routeId, final CallbackContext callbackContext) { + connection.selectRoute(routeId, new ChromecastConnection.JoinCallback() { @Override - public void onJoin(CastSession castSession) { - callbackContext.success(ChromecastUtilities.createSessionObject(castSession)); + public void onJoin(JSONObject jsonSession) { + callbackContext.success(jsonSession); } @Override @@ -395,7 +397,7 @@ public boolean sessionLeave(CallbackContext callbackContext) { /** * Will actively scan for routes and send a json array to the client. - * It is super important that client calls "stopScan", otherwise the + * It is super important that client calls "stopRouteScan", otherwise the * battery could drain quickly. * @param callbackContext called with .success or .error depending on the result * @return true for cordova @@ -403,17 +405,18 @@ public boolean sessionLeave(CallbackContext callbackContext) { public boolean startRouteScan(CallbackContext callbackContext) { if (clientScan != null) { // Stop any other existing clientScan - connection.stopScan(clientScan); + connection.stopRouteScan(clientScan); } clientScan = new ChromecastConnection.ScanCallback() { @Override void onRouteUpdate(List routes) { - PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, routesToJSON(routes)); + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, + ChromecastUtilities.createRoutesArray(routes)); pluginResult.setKeepCallback(true); callbackContext.sendPluginResult(pluginResult); } }; - connection.scanForRoutes(null, clientScan, null); + connection.startRouteScan(null, clientScan, null); return true; } @@ -426,37 +429,12 @@ void onRouteUpdate(List routes) { public boolean stopRouteScan(CallbackContext callbackContext) { if (clientScan != null) { // Stop any other existing clientScan - connection.stopScan(clientScan); + connection.stopRouteScan(clientScan); } callbackContext.success(); return true; } - /** - * Simple helper to convert a route to JSON for passing down to the javascript side. - * @param routes the routes to convert - * @return a JSON Array of JSON representations of the routes - */ - private JSONArray routesToJSON(List routes) { - JSONArray routesArray = new JSONArray(); - for (RouteInfo route : routes) { - try { - JSONObject obj = new JSONObject(); - obj.put("name", route.getName()); - obj.put("id", route.getId()); - - CastDevice device = CastDevice.getFromBundle(route.getExtras()); - if (device != null) { - obj.put("isNearbyDevice", !device.isOnLocalNetwork()); - } - - routesArray.put(obj); - } catch (JSONException e) { - } - } - return routesArray; - } - //Change all @deprecated this.webView.sendJavascript(String) to this local function sendJavascript(String) @TargetApi(Build.VERSION_CODES.KITKAT) private void sendJavascript(final String javascript) { diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index 1e4bf66..32eeb03 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -22,6 +22,7 @@ import com.google.android.gms.cast.framework.SessionManagerListener; import org.apache.cordova.CallbackContext; +import org.json.JSONObject; import java.util.ArrayList; import java.util.List; @@ -32,6 +33,8 @@ public class ChromecastConnection { private Activity activity; /** settings object. */ private SharedPreferences settings; + /** Controls the media. */ + private ChromecastSession media; /** Lifetime variable. */ private SessionListener newConnectionListener; @@ -46,11 +49,13 @@ public class ChromecastConnection { * @param act the current context * @param connectionListener client callbacks for specific events */ - public ChromecastConnection(Activity act, Listener connectionListener) { + ChromecastConnection(Activity act, Listener connectionListener) { this.activity = act; this.settings = activity.getSharedPreferences("CORDOVA-PLUGIN-CHROMECAST_ChromecastConnection", 0); this.appId = settings.getString("appId", CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID); this.listener = connectionListener; + this.media = new ChromecastSession(activity, listener); + // Set the initial appId CastOptionsProvider.setAppId(appId); @@ -60,6 +65,14 @@ public ChromecastConnection(Activity act, Listener connectionListener) { getContext().addCastStateListener(listener); } + /** + * Get the ChromecastSession object for controlling media and receiver functions. + * @return the ChromecastSession object + */ + ChromecastSession getChromecastSession() { + return this.media; + } + /** * Must be called each time the appId changes and at least once before any other method is called. * @param applicationId the app id to use @@ -78,14 +91,14 @@ public void run() { callback.success(); // Check if there is any available receivers for 5 seconds - scanForRoutes(5000L, new ScanCallback() { + startRouteScan(5000L, new ScanCallback() { @Override void onRouteUpdate(List routes) { // if the routes have changed, we may have an available device // If there is at least one device available if (getContext().getCastState() != CastState.NO_DEVICES_AVAILABLE) { // Stop the scan - stopScan(this); + stopRouteScan(this); // Let the client know a receiver is available listener.onReceiverAvailableUpdate(true); // Since we have a receiver we may also have an active session @@ -93,7 +106,8 @@ void onRouteUpdate(List routes) { // If we do have a session if (session != null) { // Let the client know - listener.onSessionRejoined(session); + media.setSession(session); + listener.onSessionRejoin(ChromecastUtilities.createSessionObject(session)); } } } @@ -122,18 +136,15 @@ private void setAppId(String applicationId) { this.appId = applicationId; this.settings.edit().putString("appId", appId).apply(); getContext().setReceiverApplicationId(appId); - // Invalidate any old session - listener.onInvalidateSession(); } /** - * This will create a new session or seamlessly join an existing one if we created it. - * @param routeId the id of the route to join - * @param routeName the name of the route + * This will create a new session or seamlessly selectRoute an existing one if we created it. + * @param routeId the id of the route to selectRoute * @param callback calls callback.onJoin when we have joined a session, * or callback.onError if an error occurred */ - public void join(final String routeId, final String routeName, JoinCallback callback) { + public void selectRoute(final String routeId, JoinCallback callback) { activity.runOnUiThread(new Runnable() { public void run() { if (getSession() != null) { @@ -168,19 +179,19 @@ void onRouteUpdate(List routes) { // Found the route! foundRoute[0] = true; // So stop the scan - stopScan(this); + stopRouteScan(this); // And select it! getMediaRouter().selectRoute(route); } } } }; - scanForRoutes(5000L, scan, new Runnable() { + startRouteScan(5000L, scan, new Runnable() { @Override public void run() { // If we were not able to find the route if (!foundRoute[0]) { - stopScan(scan); + stopRouteScan(scan); callback.onError("TIMEOUT Could not find active route with id: " + routeId + " after 5s."); } @@ -197,7 +208,7 @@ public void run() { * 1) * Displays the built in native prompt to the user. * It will actively scan for routes and display them to the user. - * Upon selection it will immediately attempt to join the route. + * Upon selection it will immediately attempt to selectRoute the route. * Will call onJoin or onError of callback. * * Else we have a connection, so: @@ -209,7 +220,7 @@ public void run() { * @param callback calls callback.success when we have joined a session, * or callback.error if an error occurred or if the dialog was dismissed */ - public void showConnectionDialog(JoinCallback callback) { + public void requestSession(JoinCallback callback) { activity.runOnUiThread(new Runnable() { public void run() { CastSession session = getSession(); @@ -269,8 +280,8 @@ private void listenForConnection(JoinCallback callback) { @Override public void onSessionStarted(CastSession castSession, String sessionId) { getSessionManager().removeSessionManagerListener(this, CastSession.class); - listener.onSessionStarted(castSession); - callback.onJoin(castSession); + media.setSession(castSession); + callback.onJoin(ChromecastUtilities.createSessionObject(castSession)); } @Override public void onSessionStartFailed(CastSession castSession, int errCode) { @@ -283,14 +294,14 @@ public void onSessionStartFailed(CastSession castSession, int errCode) { /** * Starts listening for receiver updates. - * Must call stopScan(callback) or the battery will drain with non-stop active scanning. + * Must call stopRouteScan(callback) or the battery will drain with non-stop active scanning. * @param timeout ms until the scan automatically stops, * if 0 only calls callback.onRouteUpdate once with the currently known routes - * if null, will scan until stopScan is called + * if null, will scan until stopRouteScan is called * @param callback the callback to receive route updates on * @param onTimeout called when the timeout hits */ - public void scanForRoutes(Long timeout, ScanCallback callback, Runnable onTimeout) { + public void startRouteScan(Long timeout, ScanCallback callback, Runnable onTimeout) { // Add the callback in active scan mode activity.runOnUiThread(new Runnable() { public void run() { @@ -310,7 +321,7 @@ public void run() { MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); // Send out the initial routes after the callback has been added. - // This is important because if the callback calls stopScan only once, and it + // This is important because if the callback calls stopRouteScan only once, and it // happens during this call of "onFilterRouteUpdate", there must actually be an // added callback to remove to stop the scan. callback.onFilteredRouteUpdate(); @@ -337,7 +348,7 @@ public void run() { * Call to stop the active scan if any exist. * @param callback the callback to stop and remove */ - public void stopScan(ScanCallback callback) { + public void stopRouteScan(ScanCallback callback) { activity.runOnUiThread(new Runnable() { public void run() { callback.stop(); @@ -351,14 +362,15 @@ public void run() { * @param stopCasting should the receiver application be stopped as well? * @param callback called with .success or .error depending on the initial result */ - public void endSession(boolean stopCasting, CallbackContext callback) { + void endSession(boolean stopCasting, CallbackContext callback) { activity.runOnUiThread(new Runnable() { public void run() { getSessionManager().addSessionManagerListener(new SessionListener() { @Override public void onSessionEnded(CastSession castSession, int error) { getSessionManager().removeSessionManagerListener(this, CastSession.class); - listener.onSessionEnd(castSession, stopCasting ? "stopped" : "disconnected"); + media.setSession(null); + listener.onSessionEnd(ChromecastUtilities.createSessionObject(castSession, stopCasting ? "stopped" : "disconnected")); } }, CastSession.class); @@ -398,9 +410,9 @@ public void onSessionSuspended(CastSession castSession, int reason) { } public interface JoinCallback { /** * Successfully joined a session on a route. - * @param session the session we joined + * @param jsonSession the session we joined */ - void onJoin(CastSession session); + void onJoin(JSONObject jsonSession); /** * Called if we received an error. @@ -478,12 +490,9 @@ public final void onRouteRemoved(MediaRouter router, RouteInfo route) { } } - abstract static class Listener implements CastStateListener { + abstract static class Listener implements CastStateListener, ChromecastSession.Listener { abstract void onReceiverAvailableUpdate(boolean available); - abstract void onSessionStarted(CastSession castSession); - abstract void onSessionRejoined(CastSession castSession); - abstract void onSessionEnd(CastSession castSession, String state); - abstract void onInvalidateSession(); + abstract void onSessionRejoin(JSONObject jsonSession); /** CastStateListener functions. */ @Override diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index fdbd4e0..05d7173 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -28,7 +28,7 @@ /* * All of the Chromecast session specific functions should start here. */ -public class ChromecastSession extends ChromecastConnection.Listener { +public class ChromecastSession { /** The current context. */ private Activity activity; @@ -74,7 +74,7 @@ public void run() { @Override public void onStatusUpdated() { super.onStatusUpdated(); - clientListener.onMediaUpdate(ChromecastUtilities.createMediaObject(session)); + clientListener.onMediaUpdate(createMediaObject()); } @Override public void onMetadataUpdated() { @@ -86,100 +86,69 @@ public void onMetadataUpdated() { String newMedia = info.getContentId(); if (!currentMedia.equals(newMedia)) { currentMedia = newMedia; - clientListener.onMediaLoaded(ChromecastUtilities.createMediaObject(session)); + clientListener.onMediaLoaded(createMediaObject()); } } - clientListener.onMediaUpdate(ChromecastUtilities.createMediaObject(session)); + clientListener.onMediaUpdate(createMediaObject()); } @Override public void onQueueStatusUpdated() { super.onQueueStatusUpdated(); - clientListener.onMediaUpdate(ChromecastUtilities.createMediaObject(session)); + clientListener.onMediaUpdate(createMediaObject()); } @Override public void onPreloadStatusUpdated() { super.onPreloadStatusUpdated(); - clientListener.onMediaUpdate(ChromecastUtilities.createMediaObject(session)); + clientListener.onMediaUpdate(createMediaObject()); } @Override public void onSendingRemoteMediaRequest() { super.onSendingRemoteMediaRequest(); - clientListener.onMediaUpdate(ChromecastUtilities.createMediaObject(session)); + clientListener.onMediaUpdate(createMediaObject()); } @Override public void onAdBreakStatusUpdated() { super.onAdBreakStatusUpdated(); - clientListener.onMediaUpdate(ChromecastUtilities.createMediaObject(session)); + clientListener.onMediaUpdate(createMediaObject()); } }); session.addCastListener(new Cast.Listener() { @Override public void onApplicationStatusChanged() { super.onApplicationStatusChanged(); - clientListener.onSessionUpdate(ChromecastUtilities.createSessionObject(session)); + clientListener.onSessionUpdate(createSessionObject()); } @Override - public void onApplicationMetadataChanged(ApplicationMetadata applicationMetadata) { - super.onApplicationMetadataChanged(applicationMetadata); - clientListener.onSessionUpdate(ChromecastUtilities.createSessionObject(session)); + public void onApplicationMetadataChanged(ApplicationMetadata appMetadata) { + super.onApplicationMetadataChanged(appMetadata); + clientListener.onSessionUpdate(createSessionObject()); } @Override public void onApplicationDisconnected(int i) { super.onApplicationDisconnected(i); - onSessionEnd(session, "stopped"); + clientListener.onSessionEnd( + ChromecastUtilities.createSessionObject(session, "stopped")); } @Override public void onActiveInputStateChanged(int i) { super.onActiveInputStateChanged(i); - clientListener.onSessionUpdate(ChromecastUtilities.createSessionObject(session)); + clientListener.onSessionUpdate(createSessionObject()); } @Override public void onStandbyStateChanged(int i) { super.onStandbyStateChanged(i); - clientListener.onSessionUpdate(ChromecastUtilities.createSessionObject(session)); + clientListener.onSessionUpdate(createSessionObject()); } @Override public void onVolumeChanged() { super.onVolumeChanged(); - clientListener.onSessionUpdate(ChromecastUtilities.createSessionObject(session)); + clientListener.onSessionUpdate(createSessionObject()); } }); } }); } - /** ChromecastConnection.Listener implementations. */ - @Override - void onInvalidateSession() { - setSession(null); - } - @Override - final void onSessionStarted(CastSession castSession) { - setSession(castSession); - } - @Override - final void onSessionRejoined(CastSession castSession) { - setSession(castSession); - clientListener.onSessionRejoined(ChromecastUtilities.createSessionObject(session)); - } - @Override - final void onSessionEnd(CastSession castSession, String state) { - onInvalidateSession(); - JSONObject s = ChromecastUtilities.createSessionObject(castSession); - if (state != null) { - try { - s.put("status", state); - } catch (JSONException e) { - - } - } - clientListener.onSessionUpdate(s); - } - @Override - final void onReceiverAvailableUpdate(boolean available) { - clientListener.onReceiverAvailableUpdate(available); - } - /** * Adds a message listener if one does not already exist. * @param namespace namespace @@ -258,7 +227,7 @@ public void run() { @Override public void onResult(@NonNull MediaChannelResult result) { if (result.getStatus().isSuccess()) { - callback.success(ChromecastUtilities.createMediaObject(session)); + callback.success(createMediaObject()); } else { callback.error("session_error"); } @@ -569,11 +538,18 @@ public void onResult(@NonNull MediaChannelResult result) { }; } + private JSONObject createSessionObject() { + return ChromecastUtilities.createSessionObject(session); + } + + private JSONObject createMediaObject() { + return ChromecastUtilities.createMediaObject(session); + } + interface Listener extends Cast.MessageReceivedCallback { void onMediaLoaded(JSONObject jsonMedia); void onMediaUpdate(JSONObject jsonMedia); - void onSessionRejoined(JSONObject jsonSession); void onSessionUpdate(JSONObject jsonSession); - void onReceiverAvailableUpdate(boolean available); + void onSessionEnd(JSONObject jsonSession); } } diff --git a/src/android/ChromecastUtilities.java b/src/android/ChromecastUtilities.java index 1062c11..f129499 100644 --- a/src/android/ChromecastUtilities.java +++ b/src/android/ChromecastUtilities.java @@ -2,6 +2,9 @@ import android.graphics.Color; +import androidx.mediarouter.media.MediaRouter; + +import com.google.android.gms.cast.CastDevice; import com.google.android.gms.cast.MediaInfo; import com.google.android.gms.cast.MediaMetadata; import com.google.android.gms.cast.MediaStatus; @@ -194,6 +197,18 @@ static String getHexColor(int color) { return "#" + Integer.toHexString(color); } + static JSONObject createSessionObject(CastSession session, String state) { + JSONObject s = createSessionObject(session); + if (state != null) { + try { + s.put("status", state); + } catch (JSONException e) { + + } + } + return s; + } + static JSONObject createSessionObject(CastSession session) { JSONObject out = new JSONObject(); @@ -397,4 +412,29 @@ static JSONObject createTextTrackObject(TextTrackStyle textTrackStyle) { return out; } + + /** + * Simple helper to convert a route to JSON for passing down to the javascript side. + * @param routes the routes to convert + * @return a JSON Array of JSON representations of the routes + */ + static JSONArray createRoutesArray(List routes) { + JSONArray routesArray = new JSONArray(); + for (MediaRouter.RouteInfo route : routes) { + try { + JSONObject obj = new JSONObject(); + obj.put("name", route.getName()); + obj.put("id", route.getId()); + + CastDevice device = CastDevice.getFromBundle(route.getExtras()); + if (device != null) { + obj.put("isNearbyDevice", !device.isOnLocalNetwork()); + } + + routesArray.put(obj); + } catch (JSONException e) { + } + } + return routesArray; + } } diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 0b22445..dcb4229 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -1164,7 +1164,7 @@ chrome.cast.cordova = { * @param {function(chrome.cast.Error)} successCallback */ selectRoute: function (route, successCallback, errorCallback) { - execute('selectRoute', route.id, route.name, function (err, session) { + execute('selectRoute', route.id, function (err, session) { if (!err) { successCallback(updateSession(session)); } else { From 2c4cdadc009ae1e8b8f0faa1fd5cb8c98b352def Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 15 Sep 2019 15:46:17 -0600 Subject: [PATCH 043/166] Improve selectRoute with some error handling. Add isCastGroup to routes returned to startRouteScan. Relevant to Issue #22 Improvement for issue #36 --- src/android/ChromecastConnection.java | 4 +-- src/android/ChromecastUtilities.java | 1 + tests/tests.js | 43 ++++++++++++++++----------- www/chrome.cast.js | 23 +++++++------- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index 32eeb03..85a1e17 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -148,9 +148,7 @@ public void selectRoute(final String routeId, JoinCallback callback) { activity.runOnUiThread(new Runnable() { public void run() { if (getSession() != null) { - // We are are already connected to a route - callback.onJoin(getSession()); - return; + callback.onError("cordova_already_joined"); } // We need this hack so that we can access the foundRoute value diff --git a/src/android/ChromecastUtilities.java b/src/android/ChromecastUtilities.java index f129499..e68c027 100644 --- a/src/android/ChromecastUtilities.java +++ b/src/android/ChromecastUtilities.java @@ -429,6 +429,7 @@ static JSONArray createRoutesArray(List routes) { CastDevice device = CastDevice.getFromBundle(route.getExtras()); if (device != null) { obj.put("isNearbyDevice", !device.isOnLocalNetwork()); + obj.put("isCastGroup", route instanceof MediaRouter.RouteGroup); } routesArray.put(obj); diff --git a/tests/tests.js b/tests/tests.js index ed7e446..e7758ab 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -97,6 +97,7 @@ exports.defineAutoTests = function () { .then(startRouteScan) .then(stopRouteScan) .then(selectRoute) + .then(selectRoute_fail_alreadyJoined) .then(session_setReceiverVolumeLevel_success) .then(session_setReceiverMuted_success) .then(sessionLeaveSuccess) @@ -107,7 +108,7 @@ exports.defineAutoTests = function () { .then(function () { done(); }); - }, 15 * 1000); + }, 25 * 1000); /** * Pre-requisite: You must have a valid receiver (chromecast) plugged in and available. @@ -640,9 +641,9 @@ exports.defineAutoTests = function () { function startRouteScan () { return new Promise(function (resolve) { - var finished = false; + var foundRoute; chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { - if (finished) { + if (foundRoute) { return; } for (var i = 0; i < routes.length; i++) { @@ -651,15 +652,17 @@ exports.defineAutoTests = function () { test(route.id).toBeDefined(); test(route.name).toBeDefined(); test(route.isNearbyDevice).toBeDefined(); + test(route.isCastGroup).toBeDefined(); + // Find a non-nearby device so that the join is automatic if (!route.isNearbyDevice) { - finished = true; + foundRoute = route; } } - if (finished) { - resolve(routes); + if (foundRoute) { + resolve(foundRoute); } }, function (err) { - fail(err.code + ': ' + err.description); + test().fail(err.code + ': ' + err.description); }); }); } @@ -675,22 +678,26 @@ exports.defineAutoTests = function () { }); } - function selectRoute (routes) { + function selectRoute (route) { return new Promise(function (resolve) { - // Find a non-nearby device so that the join is automatic - var route; - for (var i = 0; i < routes.length; i++) { - route = routes[i]; - if (!route.isNearbyDevice) { - break; - } - } - chrome.cast.cordova.selectRoute(route, function (session) { + chrome.cast.cordova.selectRoute(route.id, function (session) { Promise.resolve(session) .then(sessionProperties) .then(resolve); }, function (err) { - fail(err.code + ': ' + err.description); + test().fail(err.code + ': ' + err.description); + }); + }); + } + + function selectRoute_fail_alreadyJoined (arg) { + return new Promise(function (resolve) { + chrome.cast.cordova.selectRoute('', function (session) { + test().fail('Should not be allowed to selectRoute when already in session'); + }, function (err) { + test(err).toBeInstanceOf(chrome.cast.Error); + test(err.code).toEqual(chrome.cast.ErrorCode.CORDOVA_ALREADY_JOINED); + resolve(arg); }); }); } diff --git a/www/chrome.cast.js b/www/chrome.cast.js index dcb4229..9163a8f 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -95,7 +95,8 @@ chrome.cast = { SESSION_ERROR: 'session_error', TIMEOUT: 'timeout', UNKNOWN: 'unknown', - NOT_IMPLEMENTED: 'not_implemented' + NOT_IMPLEMENTED: 'not_implemented', + CORDOVA_ALREADY_JOINED: 'cordova_already_joined' }, SessionStatus: { CONNECTED: 'connected', DISCONNECTED: 'disconnected', STOPPED: 'stopped' }, @@ -1134,8 +1135,7 @@ chrome.cast.cordova = { execute('startRouteScan', function (err, routes) { if (!err) { for (var i = 0; i < routes.length; i++) { - var route = routes[i]; - routes[i] = new chrome.cast.cordova.Route(route.id, route.name, route.isNearbyDevice); + routes[i] = new chrome.cast.cordova.Route(routes[i]); } successCallback(routes); } else { @@ -1159,12 +1159,12 @@ chrome.cast.cordova = { }, /** * Attempts to join the requested route - * @param {chrome.cast.cordova.Route} route + * @param {string} routeId * @param {function(routes)} successCallback * @param {function(chrome.cast.Error)} successCallback */ - selectRoute: function (route, successCallback, errorCallback) { - execute('selectRoute', route.id, function (err, session) { + selectRoute: function (routeId, successCallback, errorCallback) { + execute('selectRoute', routeId, function (err, session) { if (!err) { successCallback(updateSession(session)); } else { @@ -1172,10 +1172,11 @@ chrome.cast.cordova = { } }); }, - Route: function (id, name, isNearbyDevice) { - this.id = id; - this.name = name; - this.isNearbyDevice = isNearbyDevice; + Route: function (jsonRoute) { + this.id = jsonRoute.id; + this.name = jsonRoute.name; + this.isNearbyDevice = jsonRoute.isNearbyDevice; + this.isCastGroup = jsonRoute.isCastGroup; } }; @@ -1356,6 +1357,8 @@ function handleError (err, callback) { errorDescription = 'A channel to the receiver is not available.'; } else if (err === chrome.cast.ErrorCode.SESSION_ERROR) { errorDescription = 'A session could not be created, or a session was invalid.'; + } else if (err === chrome.cast.ErrorCode.CORDOVA_ALREADY_JOINED) { + errorDescription = 'Leave or stop current session before attempting to join new session.'; } else { errorDescription = err; err = chrome.cast.ErrorCode.UNKNOWN; From d99c93a9b081eabb2986f9776be085ea48e7e040 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 15 Sep 2019 15:53:46 -0600 Subject: [PATCH 044/166] Improve loadMediaVideo test Issue #36 --- tests/tests.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/tests.js b/tests/tests.js index e7758ab..7590123 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -278,7 +278,11 @@ exports.defineAutoTests = function () { }); }); session.addMediaListener(function (media) { - called(loaded); + Promise.resolve({media: media, session: session}) + .then(mediaProperties) + .then(function () { + called(loaded); + }); }); session.loadMedia(new chrome.cast.media.LoadRequest( new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4') From 1769e7275e052c32ca982a79b5040286aa865222 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 18 Sep 2019 18:20:38 -0600 Subject: [PATCH 045/166] Add test to check for video finished state --- tests/tests.js | 83 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 22 deletions(-) diff --git a/tests/tests.js b/tests/tests.js index 7590123..156dd4f 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -87,7 +87,7 @@ exports.defineAutoTests = function () { expect(chrome.cast.media.Media.prototype.removeUpdateListener).toBeDefined(); }); - it('SPEC_00300 chrome.cast.cordova functions, receiver volume and leaveSession', function (done) { + it('SPEC_00300 chrome.cast.cordova functions, receiver volume, videoEnd, and leaveSession', function (done) { setupEarlyTerminator(done); Promise.resolve() .then(apiAvailable) @@ -100,13 +100,19 @@ exports.defineAutoTests = function () { .then(selectRoute_fail_alreadyJoined) .then(session_setReceiverVolumeLevel_success) .then(session_setReceiverMuted_success) - .then(sessionLeaveSuccess) - .then(initialize('SPEC_00330', function (session) { - test().fail('should not receive a session (we did sessionLeave so we shouldnt be able to auto rejoin rejoin)'); - })) - .then(sessionLeaveError_alreadyLeft) - .then(function () { - done(); + .then(function (session) { + Promise.resolve(session) + .then(loadMediaVideo) + .then(videoEnd) + .then(function (media) { + Promise.resolve(session) + .then(sessionLeaveSuccess) + .then(initialize('SPEC_00330', function (session) { + test().fail('should not receive a session (we did sessionLeave so we shouldnt be able to auto rejoin rejoin)'); + })) + .then(sessionLeaveError_alreadyLeft) + .then(done); + }); }); }, 25 * 1000); @@ -122,10 +128,23 @@ exports.defineAutoTests = function () { })) .then(requestSessionCancel) .then(requestSessionSuccess) - .then(loadMediaVideo) - .then(requestSessionStopCastingUiCancel) - .then(requestSessionStopCastingUiStopSuccess) - .then(done); + .then(function (session) { + Promise.resolve(session) + .then(loadMediaVideo) + .then(pauseSuccess) + .then(playSuccess) + .then(seekSuccess) + .then(media_setVolume_level_success) + .then(media_setVolume_muted_success) + .then(media_setVolume_level_and_unmuted_success) + .then(stopSuccess) + .then(function (media) { + Promise.resolve(session) + .then(requestSessionStopCastingUiCancel) + .then(requestSessionStopCastingUiStopSuccess) + .then(done); + }); + }); }, 60 * 1000); @@ -266,16 +285,7 @@ exports.defineAutoTests = function () { // Run all the media related tests Promise.resolve({media: loadedMedia, session: session}) .then(mediaProperties) - .then(pauseSuccess) - .then(playSuccess) - .then(seekSuccess) - .then(media_setVolume_level_success) - .then(media_setVolume_muted_success) - .then(media_setVolume_level_and_unmuted_success) - .then(stopSuccess) - .then(function (media) { - resolve(session); - }); + .then(resolve); }); session.addMediaListener(function (media) { Promise.resolve({media: media, session: session}) @@ -345,6 +355,35 @@ exports.defineAutoTests = function () { }); } + function videoEnd (media) { + var specName = videoEnd.name; + var success = 'success'; + var videoFinished = 'videoFinished'; + return new Promise(function (resolve) { + var finished = false; + var called = callOrder(specName, [success, videoFinished], { anyOrder: true }, function () { + resolve(media); + }); + media.addUpdateListener(function (isAlive) { + test(media.playerState).toBeDefined(); + if (!finished && media.playerState === 'IDLE' + && media.idleReason === 'FINISHED') { + finished = true; + called(videoFinished); + } + }); + setTimeout(function () { + var request = new chrome.cast.media.SeekRequest(); + request.currentTime = media.media.duration; + media.seek(request, function () { + called(success); + }, function (err) { + test().fail(err.code + ': ' + err.description); + }); + }, 500); + }); + } + function media_setVolume_level_success (media) { // Set up the call order var specName = media_setVolume_level_success.name; From 0fe247b390c79d819cb0d6ab735034ebfae9abbb Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 22 Sep 2019 00:38:29 -0600 Subject: [PATCH 046/166] (android) Remove sendJavascript and replace with an event callback that is set during setup. Part of issue #36 Fixes issue #41 for android --- src/android/Chromecast.java | 53 +++++------- www/chrome.cast.js | 168 ++++++++++++++---------------------- 2 files changed, 89 insertions(+), 132 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index c33e08a..5228b90 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -1,8 +1,5 @@ package acidhax.cordova.chromecast; -import android.annotation.TargetApi; -import android.os.Build; - import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; @@ -30,6 +27,8 @@ public final class Chromecast extends CordovaPlugin { private ChromecastSession media; /** Holds the reference to the current client initiated scan. */ private ChromecastConnection.ScanCallback clientScan; + /** Client's event listener callback. */ + private CallbackContext eventCallback; @Override protected void pluginInitialize() { @@ -38,37 +37,31 @@ protected void pluginInitialize() { this.connection = new ChromecastConnection(cordova.getActivity(), new ChromecastConnection.Listener() { @Override public void onSessionRejoin(JSONObject jsonSession) { - sendJavascript("chrome.cast._.sessionListener(" + jsonSession + ");"); + sendEvent("SESSION_LISTENER", new JSONArray().put(jsonSession)); } @Override public void onSessionUpdate(JSONObject jsonSession) { - sendJavascript("chrome.cast._.sessionUpdated(" + jsonSession + ");"); + sendEvent("SESSION_UPDATE", new JSONArray().put(jsonSession)); } @Override public void onSessionEnd(JSONObject jsonSession) { - sendJavascript("chrome.cast._.sessionUpdated(" + jsonSession + ");"); + onSessionUpdate(jsonSession); } @Override public void onReceiverAvailableUpdate(boolean available) { - sendJavascript("chrome.cast._.receiverUpdate(" + available + ")"); + sendEvent("RECEIVER_LISTENER", new JSONArray().put(available)); } @Override public void onMediaLoaded(JSONObject jsonMedia) { - sendJavascript("chrome.cast._.mediaLoaded(" + jsonMedia + ");"); + sendEvent("MEDIA_LOAD", new JSONArray().put(jsonMedia)); } @Override public void onMediaUpdate(JSONObject jsonMedia) { - sendJavascript("chrome.cast._.mediaUpdated(true, " + jsonMedia + ");"); + sendEvent("MEDIA_UPDATE", new JSONArray().put(jsonMedia)); } @Override public void onMessageReceived(CastDevice device, String namespace, String message) { - try { - JSONObject jsonMessage = new JSONObject(message); - sendJavascript( - "chrome.cast._.onMessage('" + namespace + "', '" + jsonMessage + "')"); - } catch (Exception e) { - e.printStackTrace(); - } + sendEvent("RECEIVER_MESSAGE", new JSONArray().put(namespace).put(message)); } }); this.media = connection.getChromecastSession(); @@ -143,7 +136,8 @@ public boolean execute(String action, JSONArray args, CallbackContext cbContext) * @return true for cordova */ public boolean setup(CallbackContext callbackContext) { - callbackContext.success(); + this.eventCallback = callbackContext; + sendEvent("SETUP", new JSONArray()); return true; } @@ -435,18 +429,17 @@ public boolean stopRouteScan(CallbackContext callbackContext) { return true; } - //Change all @deprecated this.webView.sendJavascript(String) to this local function sendJavascript(String) - @TargetApi(Build.VERSION_CODES.KITKAT) - private void sendJavascript(final String javascript) { - webView.getView().post(new Runnable() { - public void run() { - // See: https://github.com/GoogleChrome/chromium-webview-samples/blob/master/jsinterface-example/app/src/main/java/jsinterfacesample/android/chrome/google/com/jsinterface_example/MainFragment.java - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - webView.sendJavascript(javascript); - } else { - webView.loadUrl("javascript:" + javascript); - } - } - }); + /** + * This triggers an event on the JS-side. + * @param eventName - The name of the JS event to trigger + * @param args - The arguments to pass the JS event + */ + private void sendEvent(String eventName, JSONArray args) { + if (eventCallback == null) { + return; + } + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, new JSONArray().put(eventName).put(args)); + pluginResult.setKeepCallback(true); + eventCallback.sendPluginResult(pluginResult); } } diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 9163a8f..566fc5b 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -548,9 +548,8 @@ chrome.cast.initialize = function (apiConfig, successCallback, errorCallback) { // Don't set the listeners config until success _sessionListener = apiConfig.sessionListener; _receiverListener = apiConfig.receiverListener; - successCallback(); - chrome.cast._.receiverUpdate(false); + _receiverListener(chrome.cast.ReceiverAvailability.UNAVAILABLE); } else { handleError(err, errorCallback); } @@ -1092,7 +1091,7 @@ chrome.cast.media.Media.prototype.removeUpdateListener = function (listener) { this.removeListener('_mediaUpdated', listener); }; -chrome.cast.media.Media.prototype._update = function (isAlive, obj) { +chrome.cast.media.Media.prototype._update = function (obj) { this.currentTime = obj.currentTime || this.currentTime; this.idleReason = obj.idleReason || this.idleReason; this.sessionId = obj.sessionId || this.sessionId; @@ -1112,7 +1111,7 @@ chrome.cast.media.Media.prototype._update = function (isAlive, obj) { this._lastUpdatedTime = Date.now(); - this.emit('_mediaUpdated', isAlive); + this.emit('_mediaUpdated', this.playerState !== 'IDLE'); }; /** @@ -1180,104 +1179,77 @@ chrome.cast.cordova = { } }; -var _connectingListeners = []; - -chrome.cast.addConnectingListener = function (cb) { - _connectingListeners.push(cb); -}; - -chrome.cast.removeConnectingListener = function (cb) { - if (_connectingListeners.indexOf(cb) > -1) { - _connectingListeners.splice(_connectingListeners.indexOf(cb), 1); +execute('setup', function (err, args) { + if (err) { + throw new Error('cordova-plugin-chromecast: Unable to setup chrome.cast API' + err); } -}; - -/** ************* Cordova Events ********************/ -// TODO -/** - * These are events that are triggered from the plugin using "sendJavascript" - * It is recommended by cordova that we avoid sendJavascript usage - * so we should try to remove all of these functions eventually. - */ - -chrome.cast._emitConnecting = function () { - for (var n = 0; n < _connectingListeners.length; n++) { - _connectingListeners[n](); + if (args === 'OK') { + return; } -}; - -chrome.cast._ = { - /** - * @param {boolean} available - */ - receiverUpdate: function (available) { - if (available) { - _receiverListener(chrome.cast.ReceiverAvailability.AVAILABLE); - } else { - _receiverListener(chrome.cast.ReceiverAvailability.UNAVAILABLE); - } - }, - /** - * Function called from cordova when the Session has changed. - * Changes to the following properties will trigger the listener: - * statusText, namespaces, status, and the volume of the receiver. - * - * Listeners should check the status property of the Session to - * determine its connection status. The boolean parameter isAlive is - * deprecated in favor of the status Session property. The isAlive - * parameter is still passed in for backwards compatibility, and is - * true unless status = chrome.cast.SessionStatus.STOPPED. - * @param {function} listener The listener to add. - */ - sessionUpdated: function (obj) { - if (_session) { - _session._update(obj); - } - }, - mediaUpdated: function (isAlive, media) { - if (_currentMedia) { - _currentMedia._update(isAlive, media); - } else { - _currentMedia = new chrome.cast.media.Media(media.sessionId, media.mediaSessionId); - _currentMedia.currentTime = media.currentTime; - _currentMedia.playerState = media.playerState; - _currentMedia.media = media.media; - _session.media[0] = _currentMedia; - } - }, - mediaLoaded: function (media) { - if (_session) { + var eventName = args[0]; + args = args[1]; + var events = { + SETUP: function () { + chrome.cast.isAvailable = true; + }, + RECEIVER_LISTENER: function (available) { + if (available) { + _receiverListener(chrome.cast.ReceiverAvailability.AVAILABLE); + } else { + _receiverListener(chrome.cast.ReceiverAvailability.UNAVAILABLE); + } + }, + /** + * Function called from cordova when the Session has changed. + * Changes to the following properties will trigger the listener: + * statusText, namespaces, status, and the volume of the receiver. + * + * Listeners should check the status property of the Session to + * determine its connection status. The boolean parameter isAlive is + * deprecated in favor of the status Session property. The isAlive + * parameter is still passed in for backwards compatibility, and is + * true unless status = chrome.cast.SessionStatus.STOPPED. + * @param {function} listener The listener to add. + */ + SESSION_UPDATE: function (obj) { + if (_session) { + _session._update(obj); + } + }, + MEDIA_UPDATE: function (media) { if (!_currentMedia) { _currentMedia = new chrome.cast.media.Media(media.sessionId, media.mediaSessionId); } - _currentMedia._update(true, media); - _session.emit('_mediaListener', _currentMedia); - } - }, - sessionListener: function (javaSession) { - var session = updateSession(javaSession); - _sessionListener(session); - }, - sessionJoined: function (obj) { - var session = updateSession(obj); - - if (obj.media && obj.media.sessionId) { - _currentMedia = new chrome.cast.media.Media(session.sessionId, obj.media.mediaSessionId); - _currentMedia.currentTime = obj.media.currentTime; - _currentMedia.playerState = obj.media.playerState; - _currentMedia.media = obj.media.media; - session.media[0] = _currentMedia; + _currentMedia._update(media); + _session.media[0] = _currentMedia; + }, + MEDIA_LOAD: function (media) { + if (_session) { + if (!_currentMedia) { + _currentMedia = new chrome.cast.media.Media(media.sessionId, media.mediaSessionId); + } + _currentMedia._update(media); + _session.emit('_mediaListener', _currentMedia); + } + }, + SESSION_LISTENER: function (javaSession) { + var session = updateSession(javaSession); + _sessionListener(session); + }, + RECEIVER_MESSAGE: function (namespace, message) { + if (_session) { + _session.emit('message:' + namespace, namespace, message); + } } + }; - _sessionListener(session); - }, - onMessage: function (namespace, message) { - if (_session) { - _session.emit('message:' + namespace, namespace, message); - } + var event = events[eventName]; + if (!event) { + throw new Error('cordova-plugin-chromecast: No event called "' + eventName + '".'); } -}; + event.apply(null, args); +}); module.exports = chrome.cast; @@ -1298,7 +1270,7 @@ function updateSession (javaSession) { javaSession.displayName, javaSession.appImages || [], createReceiver(javaSession.receiver) - ); + ); _session.status = chrome.cast.SessionStatus.CONNECTED; _session.media[0] = createMedia(javaSession.media, javaSession.sessionId); @@ -1369,11 +1341,3 @@ function handleError (err, callback) { callback(error); } } - -execute('setup', function (err) { - if (!err) { - chrome.cast.isAvailable = true; - } else { - throw new Error('Unable to setup chrome.cast API' + err); - } -}); From 5dc73a6435e9a9a86d3a67cb2cb9809b23312893 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 25 Sep 2019 05:06:05 -0600 Subject: [PATCH 047/166] Update package script to have the standard "npm test" --- .github/pull_request_template.md | 2 +- package.json | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index d04bb4b..86c8299 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -28,6 +28,6 @@ Thanks! - [ ] I've updated the documentation as necessary - [ ] If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct [keyword to close issues using keywords](https://help.github.com/articles/closing-issues-using-keywords/)) -- [ ] I've run `npm run style` and no errors were found +- [ ] I've run `npm run test` and no errors were found (run `npm style` to auto-fix errors it can) - [ ] I've run the `test-framework` tests for Android (See Readme) - [ ] I added automated test coverage as appropriate for this change \ No newline at end of file diff --git a/package.json b/package.json index 3a48d34..9cf9197 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,9 @@ "name": "cordova-plugin-chromecast", "version": "1.0.0-dev", "scripts": { - "style-fix-js": "node node_modules/eslint/bin/eslint --fix src && node node_modules/eslint/bin/eslint --fix www && node node_modules/eslint/bin/eslint --fix tests", - "style-check": "node node_modules/eslint/bin/eslint src && node node_modules/eslint/bin/eslint www && node node_modules/eslint/bin/eslint tests && node ./node_modules/java-checkstyle/bin/index.js ./src/android/ -c ./check_style.xml", - "style": "npm run style-fix-js && npm run style-check" + "style-fix-js": "node node_modules/eslint/bin/eslint --fix src && node node_modules/eslint/bin/eslint --fix www && node node_modules/eslint/bin/eslint --fix tests/www", + "test": "node node_modules/eslint/bin/eslint src && node node_modules/eslint/bin/eslint www && node node_modules/eslint/bin/eslint tests/www && node ./node_modules/java-checkstyle/bin/index.js ./src/android/ -c ./check_style.xml", + "style": "npm run style-fix-js && npm run test" }, "author": "", "license": "dual GPLv3/MPLv2", From ad759777b156bcf3924fc29f2e7d692190039f9a Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 25 Sep 2019 05:21:21 -0600 Subject: [PATCH 048/166] Move the Api isAvailable check to a central location --- www/chrome.cast.js | 72 ++++------------------------------------------ 1 file changed, 5 insertions(+), 67 deletions(-) diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 566fc5b..7776383 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -538,11 +538,6 @@ var _currentMedia = null; * @param {function} errorCallback */ chrome.cast.initialize = function (apiConfig, successCallback, errorCallback) { - if (!chrome.cast.isAvailable) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - execute('initialize', apiConfig.sessionRequest.appId, apiConfig.autoJoinPolicy, apiConfig.defaultActionPolicy, function (err) { if (!err) { // Don't set the listeners config until success @@ -565,11 +560,6 @@ chrome.cast.initialize = function (apiConfig, successCallback, errorCallback) { * @param {chrome.cast.SessionRequest} opt_sessionRequest */ chrome.cast.requestSession = function (successCallback, errorCallback, opt_sessionRequest) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - execute('requestSession', function (err, obj) { if (!err) { successCallback(updateSession(obj)); @@ -623,11 +613,6 @@ chrome.cast.Session.prototype = Object.create(EventEmitter.prototype); * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.setReceiverVolumeLevel = function (newLevel, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - execute('setReceiverVolumeLevel', newLevel, function (err) { if (!err) { successCallback && successCallback(); @@ -644,10 +629,6 @@ chrome.cast.Session.prototype.setReceiverVolumeLevel = function (newLevel, succe * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.setReceiverMuted = function (muted, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } execute('setReceiverMuted', muted, function (err) { if (!err) { successCallback && successCallback(); @@ -663,10 +644,6 @@ chrome.cast.Session.prototype.setReceiverMuted = function (muted, successCallbac * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, CHANNEL_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.stop = function (successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } if (this.status !== chrome.cast.SessionStatus.CONNECTED) { errorCallback(new chrome.cast.Error(chrome.cast.Error.INVALID_PARAMETER, 'No active session', null)); return; @@ -690,10 +667,6 @@ chrome.cast.Session.prototype.stop = function (successCallback, errorCallback) { * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, CHANNEL_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.leave = function (successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } if (this.status !== chrome.cast.SessionStatus.CONNECTED) { errorCallback(new chrome.cast.Error(chrome.cast.Error.INVALID_PARAMETER, 'No active session', null)); return; @@ -721,11 +694,6 @@ chrome.cast.Session.prototype.leave = function (successCallback, errorCallback) * @param {[type]} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING */ chrome.cast.Session.prototype.sendMessage = function (namespace, message, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - if (typeof message === 'object') { message = JSON.stringify(message); } @@ -745,11 +713,6 @@ chrome.cast.Session.prototype.sendMessage = function (namespace, message, succes * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.loadMedia = function (loadRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - var self = this; var mediaInfo = loadRequest.media; @@ -918,11 +881,6 @@ chrome.cast.media.Media.prototype = Object.create(EventEmitter.prototype); * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.play = function (playRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - execute('mediaPlay', function (err) { if (!err) { successCallback && successCallback(); @@ -939,11 +897,6 @@ chrome.cast.media.Media.prototype.play = function (playRequest, successCallback, * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.pause = function (pauseRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - execute('mediaPause', function (err) { if (!err) { successCallback && successCallback(); @@ -960,11 +913,6 @@ chrome.cast.media.Media.prototype.pause = function (pauseRequest, successCallbac * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.seek = function (seekRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - const currentTime = Math.round(seekRequest.currentTime); const resumeState = seekRequest.resumeState || ''; @@ -984,11 +932,6 @@ chrome.cast.media.Media.prototype.seek = function (seekRequest, successCallback, * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.stop = function (stopRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - execute('mediaStop', function (err) { if (!err) { successCallback && successCallback(); @@ -1005,11 +948,6 @@ chrome.cast.media.Media.prototype.stop = function (stopRequest, successCallback, * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.setVolume = function (volumeRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - if (!volumeRequest.volume || (volumeRequest.volume.level == null && volumeRequest.volume.muted === null)) { errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.SESSION_ERROR), 'INVALID_PARAMS', { reason: 'INVALID_PARAMS', type: 'INVALID_REQUEST' }); return; @@ -1056,11 +994,6 @@ chrome.cast.media.Media.prototype.getEstimatedTime = function () { * @param {function(not-null chrome.cast.Error)} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. **/ chrome.cast.media.Media.prototype.editTracksInfo = function (editTracksInfoRequest, successCallback, errorCallback) { - if (chrome.cast.isAvailable === false) { - errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); - return; - } - var activeTracks = editTracksInfoRequest.activeTrackIds; var textTrackSytle = editTracksInfoRequest.textTrackSytle; @@ -1310,6 +1243,11 @@ function execute (action) { if (args[args.length - 1] instanceof Function) { callback = args.pop(); } + + // Reasons to not execute + if (action !== 'setup' && !chrome.cast.isAvailable) { + return callback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + } window.cordova.exec(function (result) { callback && callback(null, result); }, function (err) { callback && callback(err); }, 'Chromecast', action, args); } From dce633f5020426ee0d77555756df24937faae3c9 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 25 Sep 2019 05:23:02 -0600 Subject: [PATCH 049/166] (android) Add some notes and error detection for Android 4.4 mysterious, intermittent failure to create a session during selectRoute. --- src/android/ChromecastConnection.java | 32 +++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index 85a1e17..002683b 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -147,7 +147,7 @@ private void setAppId(String applicationId) { public void selectRoute(final String routeId, JoinCallback callback) { activity.runOnUiThread(new Runnable() { public void run() { - if (getSession() != null) { + if (getSession() != null && getSession().isConnected()) { callback.onError("cordova_already_joined"); } @@ -281,10 +281,38 @@ public void onSessionStarted(CastSession castSession, String sessionId) { media.setSession(castSession); callback.onJoin(ChromecastUtilities.createSessionObject(castSession)); } + /** + * Possible error codes for onSessionStartFailed and onSessionEnded + * https://developers.google.com/android/reference/com/google/android/gms/cast/CastStatusCodes.html + * https://developers.google.com/android/reference/com/google/android/gms/common/api/CommonStatusCodes.html + */ @Override public void onSessionStartFailed(CastSession castSession, int errCode) { getSessionManager().removeSessionManagerListener(this, CastSession.class); - callback.onError(Integer.toString(errCode)); + callback.onError("Failed to start session with error code: " + errCode); + } + @Override + public void onSessionEnded(CastSession castSession, int errCode) { + // Can hit here on occasion. + // This seems to happen mostly on Android 4.4. + // Reasons from Media router include: + // - "Ignoring attempt to select removed route: " + // - "Unselecting the current route because it is no longer selectable: " + // Both reasons result in onRouteUnselected being triggered with + // reason = 0, (MediaRouter.UNSELECT_REASON_UNKNOWN) + + // If you retry selecting the route long enough it seems like eventually you will + // succeed. But sometimes it can take up to 9 tries (~4.5 seconds per try) to + // successfully join. That is way too long, so just return an error. + + // More details: In these cases the event order will be: + // onSessionStarting, + // onRouteUnselected, (at this point, before as well, getSession() will return + // non-null, but session.isConnected() == false) + // onSessionEnding, + // onSessionEnded (errCode == 0 (success)) + getSessionManager().removeSessionManagerListener(this, CastSession.class); + callback.onError("Failed to finish starting session. Session ended with error code: " + errCode); } }; getSessionManager().addSessionManagerListener(newConnectionListener, CastSession.class); From fcb1dafe7fd311b7477078b73f60ecc87426b747 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 25 Sep 2019 05:27:52 -0600 Subject: [PATCH 050/166] Add check to make sure initialize has been successfully called before most functions are able to work. --- www/chrome.cast.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 7776383..aa6ea06 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -524,6 +524,7 @@ chrome.cast = { } }; +var _initialized = false; var _sessionListener = function () {}; var _receiverListener = function () {}; @@ -541,6 +542,7 @@ chrome.cast.initialize = function (apiConfig, successCallback, errorCallback) { execute('initialize', apiConfig.sessionRequest.appId, apiConfig.autoJoinPolicy, apiConfig.defaultActionPolicy, function (err) { if (!err) { // Don't set the listeners config until success + _initialized = true; _sessionListener = apiConfig.sessionListener; _receiverListener = apiConfig.receiverListener; successCallback(); @@ -1248,6 +1250,10 @@ function execute (action) { if (action !== 'setup' && !chrome.cast.isAvailable) { return callback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); } + if (action !== 'setup' && action !== 'initialize' && !_initialized) { + throw new Error('Not initialized. Must call chrome.cast.initialize first.'); + } + window.cordova.exec(function (result) { callback && callback(null, result); }, function (err) { callback && callback(err); }, 'Chromecast', action, args); } From ff3d595cab5aed199c6b1bc6a26633fb32e2e397 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Thu, 26 Sep 2019 00:15:06 -0600 Subject: [PATCH 051/166] Should make sure _session exists before trying to use it since this event can be called at any time --- www/chrome.cast.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/www/chrome.cast.js b/www/chrome.cast.js index aa6ea06..bc5478e 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -1157,7 +1157,9 @@ execute('setup', function (err, args) { _currentMedia = new chrome.cast.media.Media(media.sessionId, media.mediaSessionId); } _currentMedia._update(media); - _session.media[0] = _currentMedia; + if (_session) { + _session.media[0] = _currentMedia; + } }, MEDIA_LOAD: function (media) { if (_session) { From 444286ba3309287ab54cfcbb0d44a3ff8262133d Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Thu, 26 Sep 2019 00:16:34 -0600 Subject: [PATCH 052/166] Improvements for selectRoute and requestSession. Make unique callbacks for selectRoute and requestSession. Enable retrying to join a route for selectRoute under certain situations. See issues: https://github.com/jellyfin/cordova-plugin-chromecast/issues/49 https://github.com/jellyfin/cordova-plugin-chromecast/issues/48 --- src/android/Chromecast.java | 24 ++-- src/android/ChromecastConnection.java | 169 ++++++++++++++++++-------- 2 files changed, 129 insertions(+), 64 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 5228b90..952711a 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -164,18 +164,19 @@ public boolean initialize(final String appId, String autoJoinPolicy, String defa * @return true for cordova */ public boolean requestSession(final CallbackContext callbackContext) { - connection.requestSession(new ChromecastConnection.JoinCallback() { + connection.requestSession(new ChromecastConnection.RequestSessionCallback() { @Override public void onJoin(JSONObject jsonSession) { callbackContext.success(jsonSession); } - public void onError(String errorCode) { - if (errorCode.equals("CANCEL")) { - callbackContext.error("cancel"); - } else { - // TODO maybe handle some of the error codes better - callbackContext.error("session_error"); - } + @Override + public void onError(int errorCode) { + // TODO maybe handle some of the error codes better + callbackContext.error("session_error"); + } + @Override + public void onCancel() { + callbackContext.error("cancel"); } }); return true; @@ -188,15 +189,14 @@ public void onError(String errorCode) { * @return true for cordova */ public boolean selectRoute(final String routeId, final CallbackContext callbackContext) { - connection.selectRoute(routeId, new ChromecastConnection.JoinCallback() { + connection.selectRoute(routeId, new ChromecastConnection.SelectRouteCallback() { @Override public void onJoin(JSONObject jsonSession) { callbackContext.success(jsonSession); } - @Override - public void onError(String errorCode) { - callbackContext.error(errorCode); + public void onError(String message) { + callbackContext.error(message); } }); return true; diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index 002683b..6bccde2 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -7,6 +7,7 @@ import android.os.Bundle; import android.os.Handler; +import androidx.arch.core.util.Function; import androidx.mediarouter.app.MediaRouteChooserDialog; import androidx.mediarouter.media.MediaRouteSelector; import androidx.mediarouter.media.MediaRouter; @@ -144,19 +145,18 @@ private void setAppId(String applicationId) { * @param callback calls callback.onJoin when we have joined a session, * or callback.onError if an error occurred */ - public void selectRoute(final String routeId, JoinCallback callback) { + public void selectRoute(final String routeId, SelectRouteCallback callback) { activity.runOnUiThread(new Runnable() { public void run() { if (getSession() != null && getSession().isConnected()) { callback.onError("cordova_already_joined"); } - // We need this hack so that we can access the foundRoute value - // Without having to store it as a global variable. - // Just always access first element + // We need this hack so that we can access these values in callbacks without having + // to store it as a global variable, just always access first element final boolean[] foundRoute = {false}; - - listenForConnection(callback); + final boolean[] sentResult = {false}; + final int[] retries = {0}; // We need to start an active scan because getMediaRouter().getRoutes() may be out // of date. Also, maintaining a list of known routes doesn't work. It is possible @@ -176,25 +176,81 @@ void onRouteUpdate(List routes) { if (!foundRoute[0] && route.getId().equals(routeId)) { // Found the route! foundRoute[0] = true; - // So stop the scan - stopRouteScan(this); - // And select it! - getMediaRouter().selectRoute(route); + // try-catch for issue: + // https://github.com/jellyfin/cordova-plugin-chromecast/issues/48 + try { + // Try selecting the route! + getMediaRouter().selectRoute(route); + } catch (NullPointerException e) { + // Let it try to find the route again + foundRoute[0] = false; + } } } } }; - startRouteScan(5000L, scan, new Runnable() { + + Runnable retry = new Runnable() { @Override public void run() { - // If we were not able to find the route - if (!foundRoute[0]) { + // Reset foundRoute + foundRoute[0] = false; + // Feed current routes into scan so that it can retry. + // If route is there, it will try to join, + // if not, it should wait for the scan to find the route + scan.onRouteUpdate(getMediaRouter().getRoutes()); + } + }; + + Function sendErrorResult = new Function() { + @Override + public Void apply(String message) { + if (!sentResult[0]) { + sentResult[0] = true; stopRouteScan(scan); - callback.onError("TIMEOUT Could not find active route with id: " - + routeId + " after 5s."); + callback.onError(message); + } + return null; + } + }; + + listenForConnection(new ConnectionCallback() { + @Override + public void onJoin(JSONObject jsonSession) { + sentResult[0] = true; + stopRouteScan(scan); + callback.onJoin(jsonSession); + } + @Override + public boolean onSessionStartFailed(int errorCode) { + if (errorCode == 7 || errorCode == 15) { + // It network or timeout error retry + retry.run(); + return false; + } else { + sendErrorResult.apply("Failed to start session with error code: " + errorCode); + return true; + } + } + @Override + public boolean onSessionEndedBeforeStart(int errorCode) { + if (retries[0] < 10) { + retries[0]++; + retry.run(); + return false; + } else { + sendErrorResult.apply("Failed to to join existing route (" + routeId + ") " + retries[0] + 1 + " times before giving up."); + return true; } } }); + + startRouteScan(15000L, scan, new Runnable() { + @Override + public void run() { + sendErrorResult.apply("TIMEOUT Failed to to join existing route (" + routeId + ") after 15s and " + retries[0] + 1 + " trys."); + } + }); } }); } @@ -207,18 +263,18 @@ public void run() { * Displays the built in native prompt to the user. * It will actively scan for routes and display them to the user. * Upon selection it will immediately attempt to selectRoute the route. - * Will call onJoin or onError of callback. + * Will call onJoin, onError or onCancel, of callback. * * Else we have a connection, so: * 2) * Displays the active connection dialog which includes the option * to disconnect. - * Will only call onError of callback if the user cancels the dialog. + * Will only call onCancel of callback if the user cancels the dialog. * * @param callback calls callback.success when we have joined a session, * or callback.error if an error occurred or if the dialog was dismissed */ - public void requestSession(JoinCallback callback) { + public void requestSession(RequestSessionCallback callback) { activity.runOnUiThread(new Runnable() { public void run() { CastSession session = getSession(); @@ -239,7 +295,7 @@ public void run() { @Override public void onCancel(DialogInterface dialog) { getSessionManager().removeSessionManagerListener(newConnectionListener, CastSession.class); - callback.onError("CANCEL"); + callback.onCancel(); } }); builder.show(); @@ -252,7 +308,7 @@ public void onCancel(DialogInterface dialog) { builder.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { - callback.onError("CANCEL"); + callback.onCancel(); } }); builder.setPositiveButton("Stop Casting", new DialogInterface.OnClickListener() { @@ -271,7 +327,7 @@ public void onClick(DialogInterface dialog, int which) { * Must be called from the main thread. * @param callback calls callback.success when we have joined, or callback.error if an error occurred */ - private void listenForConnection(JoinCallback callback) { + private void listenForConnection(ConnectionCallback callback) { // We should only ever have one of these listeners active at a time, so remove previous getSessionManager().removeSessionManagerListener(newConnectionListener, CastSession.class); newConnectionListener = new SessionListener() { @@ -281,38 +337,17 @@ public void onSessionStarted(CastSession castSession, String sessionId) { media.setSession(castSession); callback.onJoin(ChromecastUtilities.createSessionObject(castSession)); } - /** - * Possible error codes for onSessionStartFailed and onSessionEnded - * https://developers.google.com/android/reference/com/google/android/gms/cast/CastStatusCodes.html - * https://developers.google.com/android/reference/com/google/android/gms/common/api/CommonStatusCodes.html - */ @Override public void onSessionStartFailed(CastSession castSession, int errCode) { - getSessionManager().removeSessionManagerListener(this, CastSession.class); - callback.onError("Failed to start session with error code: " + errCode); + if (callback.onSessionStartFailed(errCode)) { + getSessionManager().removeSessionManagerListener(this, CastSession.class); + } } @Override public void onSessionEnded(CastSession castSession, int errCode) { - // Can hit here on occasion. - // This seems to happen mostly on Android 4.4. - // Reasons from Media router include: - // - "Ignoring attempt to select removed route: " - // - "Unselecting the current route because it is no longer selectable: " - // Both reasons result in onRouteUnselected being triggered with - // reason = 0, (MediaRouter.UNSELECT_REASON_UNKNOWN) - - // If you retry selecting the route long enough it seems like eventually you will - // succeed. But sometimes it can take up to 9 tries (~4.5 seconds per try) to - // successfully join. That is way too long, so just return an error. - - // More details: In these cases the event order will be: - // onSessionStarting, - // onRouteUnselected, (at this point, before as well, getSession() will return - // non-null, but session.isConnected() == false) - // onSessionEnding, - // onSessionEnded (errCode == 0 (success)) - getSessionManager().removeSessionManagerListener(this, CastSession.class); - callback.onError("Failed to finish starting session. Session ended with error code: " + errCode); + if (callback.onSessionEndedBeforeStart(errCode)) { + getSessionManager().removeSessionManagerListener(this, CastSession.class); + } } }; getSessionManager().addSessionManagerListener(newConnectionListener, CastSession.class); @@ -433,7 +468,27 @@ public void onSessionResumeFailed(CastSession castSession, int error) { } public void onSessionSuspended(CastSession castSession, int reason) { } } - public interface JoinCallback { + interface SelectRouteCallback { + void onJoin(JSONObject jsonSession); + void onError(String message); + } + + abstract static class RequestSessionCallback implements ConnectionCallback { + abstract void onError(int errorCode); + abstract void onCancel(); + @Override + public final boolean onSessionEndedBeforeStart(int errorCode) { + onSessionStartFailed(errorCode); + return true; + } + @Override + public final boolean onSessionStartFailed(int errorCode) { + onError(errorCode); + return true; + } + } + + interface ConnectionCallback { /** * Successfully joined a session on a route. * @param jsonSession the session we joined @@ -442,11 +497,21 @@ public interface JoinCallback { /** * Called if we received an error. - * @param errorCode "CANCEL" means the user cancelled - * If the errorCode is an integer, you can find the meaning here: + * @param errorCode You can find the error meaning here: * https://developers.google.com/android/reference/com/google/android/gms/cast/CastStatusCodes + * @return true if we are done listening for join, false, if we to keep listening + */ + boolean onSessionStartFailed(int errorCode); + + /** + * Called when we detect a session ended event before session started. + * See issues: + * https://github.com/jellyfin/cordova-plugin-chromecast/issues/49 + * https://github.com/jellyfin/cordova-plugin-chromecast/issues/48 + * @param errorCode error to output + * @return true if we are done listening for join, false, if we to keep listening */ - void onError(String errorCode); + boolean onSessionEndedBeforeStart(int errorCode); } public abstract static class ScanCallback extends MediaRouter.Callback { From 227aa8369ecb1b7ca9c28ad08c15046b9e33d401 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Thu, 26 Sep 2019 01:09:20 -0600 Subject: [PATCH 053/166] Improve error handling to allow native responses to send a JSONObject with "code" and "description" parameters instead of just the error code string. Make selectRoute use the full error object. Towards issue #36 --- src/android/Chromecast.java | 2 +- src/android/ChromecastConnection.java | 18 +++++++++++------- src/android/ChromecastUtilities.java | 11 +++++++++++ www/chrome.cast.js | 25 +++++++++++-------------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 952711a..2a2f4b5 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -195,7 +195,7 @@ public void onJoin(JSONObject jsonSession) { callbackContext.success(jsonSession); } @Override - public void onError(String message) { + public void onError(JSONObject message) { callbackContext.error(message); } }); diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index 6bccde2..93a0cd6 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -149,7 +149,8 @@ public void selectRoute(final String routeId, SelectRouteCallback callback) { activity.runOnUiThread(new Runnable() { public void run() { if (getSession() != null && getSession().isConnected()) { - callback.onError("cordova_already_joined"); + callback.onError(ChromecastUtilities.createError("session_error", + "Leave or stop current session before attempting to join new session.")); } // We need this hack so that we can access these values in callbacks without having @@ -202,9 +203,9 @@ public void run() { } }; - Function sendErrorResult = new Function() { + Function sendErrorResult = new Function() { @Override - public Void apply(String message) { + public Void apply(JSONObject message) { if (!sentResult[0]) { sentResult[0] = true; stopRouteScan(scan); @@ -228,7 +229,8 @@ public boolean onSessionStartFailed(int errorCode) { retry.run(); return false; } else { - sendErrorResult.apply("Failed to start session with error code: " + errorCode); + sendErrorResult.apply(ChromecastUtilities.createError("session_error", + "Failed to start session with error code: " + errorCode)); return true; } } @@ -239,7 +241,8 @@ public boolean onSessionEndedBeforeStart(int errorCode) { retry.run(); return false; } else { - sendErrorResult.apply("Failed to to join existing route (" + routeId + ") " + retries[0] + 1 + " times before giving up."); + sendErrorResult.apply(ChromecastUtilities.createError("session_error", + "Failed to to join existing route (" + routeId + ") " + retries[0] + 1 + " times before giving up.")); return true; } } @@ -248,7 +251,8 @@ public boolean onSessionEndedBeforeStart(int errorCode) { startRouteScan(15000L, scan, new Runnable() { @Override public void run() { - sendErrorResult.apply("TIMEOUT Failed to to join existing route (" + routeId + ") after 15s and " + retries[0] + 1 + " trys."); + sendErrorResult.apply(ChromecastUtilities.createError("timeout", + "Failed to to join route (" + routeId + ") after 15s and " + retries[0] + 1 + " trys.")); } }); } @@ -470,7 +474,7 @@ public void onSessionSuspended(CastSession castSession, int reason) { } interface SelectRouteCallback { void onJoin(JSONObject jsonSession); - void onError(String message); + void onError(JSONObject message); } abstract static class RequestSessionCallback implements ConnectionCallback { diff --git a/src/android/ChromecastUtilities.java b/src/android/ChromecastUtilities.java index e68c027..95d0aa5 100644 --- a/src/android/ChromecastUtilities.java +++ b/src/android/ChromecastUtilities.java @@ -438,4 +438,15 @@ static JSONArray createRoutesArray(List routes) { } return routesArray; } + + static JSONObject createError(String code, String message) { + JSONObject out = new JSONObject(); + try { + out.put("code", code); + out.put("description", message); + } catch (JSONException e) { + e.printStackTrace(); + } + return out; + } } diff --git a/www/chrome.cast.js b/www/chrome.cast.js index bc5478e..14136fc 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -95,8 +95,7 @@ chrome.cast = { SESSION_ERROR: 'session_error', TIMEOUT: 'timeout', UNKNOWN: 'unknown', - NOT_IMPLEMENTED: 'not_implemented', - CORDOVA_ALREADY_JOINED: 'cordova_already_joined' + NOT_IMPLEMENTED: 'not_implemented' }, SessionStatus: { CONNECTED: 'connected', DISCONNECTED: 'disconnected', STOPPED: 'stopped' }, @@ -1260,29 +1259,27 @@ function execute (action) { } function handleError (err, callback) { - var errorDescription = err; + var desc = err && err.description; + err = (err.code || err).toLowerCase(); - err = err.toLowerCase() || ''; if (err === chrome.cast.ErrorCode.TIMEOUT) { - errorDescription = 'The operation timed out.'; + desc = desc || 'The operation timed out.'; } else if (err === chrome.cast.ErrorCode.INVALID_PARAMETER) { - errorDescription = 'The parameters to the operation were not valid.'; + desc = desc || 'The parameters to the operation were not valid.'; } else if (err === chrome.cast.ErrorCode.RECEIVER_UNAVAILABLE) { - errorDescription = 'No receiver was compatible with the session request.'; + desc = desc || 'No receiver was compatible with the session request.'; } else if (err === chrome.cast.ErrorCode.CANCEL) { - errorDescription = 'The operation was canceled by the user.'; + desc = desc || 'The operation was canceled by the user.'; } else if (err === chrome.cast.ErrorCode.CHANNEL_ERROR) { - errorDescription = 'A channel to the receiver is not available.'; + desc = desc || 'A channel to the receiver is not available.'; } else if (err === chrome.cast.ErrorCode.SESSION_ERROR) { - errorDescription = 'A session could not be created, or a session was invalid.'; - } else if (err === chrome.cast.ErrorCode.CORDOVA_ALREADY_JOINED) { - errorDescription = 'Leave or stop current session before attempting to join new session.'; + desc = desc || 'A session could not be created, or a session was invalid.'; } else { - errorDescription = err; + desc = err + ' ' + desc; err = chrome.cast.ErrorCode.UNKNOWN; } - var error = new chrome.cast.Error(err, errorDescription, {}); + var error = new chrome.cast.Error(err, desc, {}); if (callback) { callback(error); } From 6afa22d4d07fc604107d40365e9e15ff52d4fa6a Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sat, 28 Sep 2019 02:52:39 -0600 Subject: [PATCH 054/166] Protect against user passing null as _receiverListener or _sessionListener --- www/chrome.cast.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 14136fc..11a33d6 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -524,8 +524,8 @@ chrome.cast = { }; var _initialized = false; -var _sessionListener = function () {}; -var _receiverListener = function () {}; +var _sessionListener; +var _receiverListener; var _session; var _currentMedia = null; @@ -545,7 +545,7 @@ chrome.cast.initialize = function (apiConfig, successCallback, errorCallback) { _sessionListener = apiConfig.sessionListener; _receiverListener = apiConfig.receiverListener; successCallback(); - _receiverListener(chrome.cast.ReceiverAvailability.UNAVAILABLE); + _receiverListener && _receiverListener(chrome.cast.ReceiverAvailability.UNAVAILABLE); } else { handleError(err, errorCallback); } @@ -1061,6 +1061,7 @@ chrome.cast.cordova = { * active routes whenever a route change is detected. * It is super important that client calls "stopScan", otherwise the * battery could drain quickly. + * https://github.com/jellyfin/cordova-plugin-chromecast/issues/22#issuecomment-530773677 * @param {function(routes)} successCallback * @param {function(chrome.cast.Error)} successCallback */ @@ -1128,6 +1129,9 @@ execute('setup', function (err, args) { chrome.cast.isAvailable = true; }, RECEIVER_LISTENER: function (available) { + if (!_receiverListener) { + return; + } if (available) { _receiverListener(chrome.cast.ReceiverAvailability.AVAILABLE); } else { @@ -1171,7 +1175,7 @@ execute('setup', function (err, args) { }, SESSION_LISTENER: function (javaSession) { var session = updateSession(javaSession); - _sessionListener(session); + _sessionListener && _sessionListener(session); }, RECEIVER_MESSAGE: function (namespace, message) { if (_session) { @@ -1196,8 +1200,8 @@ function updateSession (javaSession) { // Should we reset the sesion? if (!javaSession) { _session = undefined; - _sessionListener = function () {}; - _receiverListener = function () {}; + _sessionListener = undefined; + _receiverListener = undefined; return; } _session = new chrome.cast.Session( From 390d456a0ccb755acc6d88e0b6a96688e3199cce Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sat, 28 Sep 2019 04:17:29 -0600 Subject: [PATCH 055/166] Improved startRouteScan/stopRouteScan to handle multiple calls better. startRouteScan also has it's error callback called with a 'cancel' error when the scan is stopped. update documentation Issue #36 --- README.md | 64 +++++++++++++++++---------- src/android/Chromecast.java | 55 ++++++++++++++++------- src/android/ChromecastConnection.java | 19 +++++--- 3 files changed, 93 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index aa3680b..0974e0d 100644 --- a/README.md +++ b/README.md @@ -1,58 +1,74 @@

    cordova-plugin-chromecast

    -

    Chromecast in Cordova

    +

    Control Chromecast from your Cordova app

    -## Installation -Add the plugin with the command below in your cordova project directory. +# Installation ``` cordova plugin add https://github.com/jellyfin/cordova-plugin-chromecast.git ``` -## Usage +# Usage -This project attempts to implement the official Google Cast SDK for Chrome within Cordova. We've made a lot of progress in making this possible, so check out the [offical documentation](https://developers.google.com/cast/docs/chrome_sender) for examples. +This project attempts to implement the [official Google Cast API for Chrome](https://developers.google.com/cast/docs/reference/chrome/) within the Cordova webview. +This means that you should be able to write almost identical code in cordova as you would if you were developing for desktop Chrome. -When you call `chrome.cast.requestSession()` a popup will be displayed to select a Chromecast. +We have not implemented every function in the [API](https://developers.google.com/cast/docs/reference/chrome/) but most of the core functions are there. If you find a function is missing we welcome [pull requests](#contributing)! Alternatively, you can file an [issue](https://github.com/jellyfin/cordova-plugin-chromecast/issues), please include a code sample of the expected functionality if possible! -Calling `chrome.cast.requestSession()` when you have an active session will display a popup with the option to "Stop Casting". +The only significant difference between the [cast API](https://developers.google.com/cast/docs/reference/chrome/) and this plugin is the initialization. -**Specific to this plugin** (Not supported on desktop chrome) +In **Chrome desktop** you would do: +```js +window['__onGCastApiAvailable'] = function(isAvailable, err) { + if (isAvailable) { + // start using the api! + } +}; +``` -To make your own custom route selector use this: +But in **cordova-plugin-chromecast** you do: +```js +document.addEventListener("deviceready", function () { + // start using the api! +}); ``` + +## Specific to this plugin +We have added some additional methods beyond the [Chromecast API]((https://developers.google.com/cast/docs/reference/chrome/)). +They can all be found in the `chrome.cast.cordova` object. + +To make your own **custom route selector** use this: +```js // This will begin an active scan for routes chrome.cast.cordova.scanForRoutes(function (routes) { // Here is where you should update your route selector view with the current routes // This will called each time the routes change + // routes is an array of "Route" objects (see below) +}, function (err) { + // Will return with err.code === chrome.cast.ErrorCode.CANCEL when the scan has been ended }); // When the user selects a route // stop the scan to save battery power chrome.cast.cordova.stopScan(); + // and use the selected route.id to join the route chrome.cast.cordova.selectRoute(route.id, function (session) { // Save the session for your use }, function (err) { - + // Failed to connect to the route }); ``` -## Status - -The project is now pretty much feature complete - the only things that will possibly break are missing parameters. We haven't done any checking for optional paramaters. When using the plugin make sure your constructors and function calls have every parameter you can find in the method declarations. - -

    Plugin Development

    +**Route** object +```text +id {string} - Route id +name {string} - User friendly route name +isCastGroup {boolean} - Is the route a cast group? +isNearbyDevice {boolean} - Is it a device only accessible via guest mode? + (aka. probably not on the same network, but is nearby and allows guests) +``` -* Link your local copy of the the plugin to a project for development and testing - * With admin permission run `cordova plugin add --link ` -* This links the plugin's **java** files directly to the Android platform. So you can modify the files from Android studio and re-deploy from there. -* Unfortunately it does **not** link the js files. -* To update the js files you must run: - * `cordova plugin remove ` - * `cordova plugin add --link ` - * Don't forget the admin permission - * Or, you can follow these [hot reloading js instructions](https://github.com/miloproductionsinc/cordova-testing#hot-reload-js) ## Formatting diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 2a2f4b5..e1ca19b 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -27,6 +27,8 @@ public final class Chromecast extends CordovaPlugin { private ChromecastSession media; /** Holds the reference to the current client initiated scan. */ private ChromecastConnection.ScanCallback clientScan; + /** Holds the reference to the current client initiated scan callback. */ + private CallbackContext scanCallback; /** Client's event listener callback. */ private CallbackContext eventCallback; @@ -397,21 +399,36 @@ public boolean sessionLeave(CallbackContext callbackContext) { * @return true for cordova */ public boolean startRouteScan(CallbackContext callbackContext) { - if (clientScan != null) { - // Stop any other existing clientScan - connection.stopRouteScan(clientScan); + if (scanCallback != null) { + scanCallback.error(ChromecastUtilities.createError("cancel", "Started a new route scan before stopping previous one.")); } - clientScan = new ChromecastConnection.ScanCallback() { + scanCallback = callbackContext; + Runnable startScan = new Runnable() { @Override - void onRouteUpdate(List routes) { - PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, - ChromecastUtilities.createRoutesArray(routes)); - pluginResult.setKeepCallback(true); - callbackContext.sendPluginResult(pluginResult); + public void run() { + clientScan = new ChromecastConnection.ScanCallback() { + @Override + void onRouteUpdate(List routes) { + if (scanCallback != null) { + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, + ChromecastUtilities.createRoutesArray(routes)); + pluginResult.setKeepCallback(true); + scanCallback.sendPluginResult(pluginResult); + } else { + // Try to get the scan to stop because we already ended the scanCallback + connection.stopRouteScan(clientScan, null); + } + } + }; + connection.startRouteScan(null, clientScan, null); } }; - connection.startRouteScan(null, clientScan, null); - + if (clientScan != null) { + // Stop any other existing clientScan + connection.stopRouteScan(clientScan, startScan); + } else { + startScan.run(); + } return true; } @@ -421,11 +438,17 @@ void onRouteUpdate(List routes) { * @return true for cordova */ public boolean stopRouteScan(CallbackContext callbackContext) { - if (clientScan != null) { - // Stop any other existing clientScan - connection.stopRouteScan(clientScan); - } - callbackContext.success(); + // Stop any other existing clientScan + connection.stopRouteScan(clientScan, new Runnable() { + @Override + public void run() { + if (scanCallback != null) { + scanCallback.error(ChromecastUtilities.createError("cancel", "Scan stopped.")); + scanCallback = null; + } + callbackContext.success(); + } + }); return true; } diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index 93a0cd6..f9f913e 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -99,7 +99,7 @@ void onRouteUpdate(List routes) { // If there is at least one device available if (getContext().getCastState() != CastState.NO_DEVICES_AVAILABLE) { // Stop the scan - stopRouteScan(this); + stopRouteScan(this, null); // Let the client know a receiver is available listener.onReceiverAvailableUpdate(true); // Since we have a receiver we may also have an active session @@ -151,6 +151,7 @@ public void run() { if (getSession() != null && getSession().isConnected()) { callback.onError(ChromecastUtilities.createError("session_error", "Leave or stop current session before attempting to join new session.")); + return; } // We need this hack so that we can access these values in callbacks without having @@ -208,7 +209,7 @@ public void run() { public Void apply(JSONObject message) { if (!sentResult[0]) { sentResult[0] = true; - stopRouteScan(scan); + stopRouteScan(scan, null); callback.onError(message); } return null; @@ -219,7 +220,7 @@ public Void apply(JSONObject message) { @Override public void onJoin(JSONObject jsonSession) { sentResult[0] = true; - stopRouteScan(scan); + stopRouteScan(scan, null); callback.onJoin(jsonSession); } @Override @@ -252,7 +253,7 @@ public boolean onSessionEndedBeforeStart(int errorCode) { @Override public void run() { sendErrorResult.apply(ChromecastUtilities.createError("timeout", - "Failed to to join route (" + routeId + ") after 15s and " + retries[0] + 1 + " trys.")); + "Failed to join route (" + routeId + ") after 15s and " + (retries[0] + 1) + " tries.")); } }); } @@ -412,12 +413,20 @@ public void run() { /** * Call to stop the active scan if any exist. * @param callback the callback to stop and remove + * @param completionCallback called on completion */ - public void stopRouteScan(ScanCallback callback) { + public void stopRouteScan(ScanCallback callback, Runnable completionCallback) { + if (callback == null) { + completionCallback.run(); + return; + } activity.runOnUiThread(new Runnable() { public void run() { callback.stop(); getMediaRouter().removeCallback(callback); + if (completionCallback != null) { + completionCallback.run(); + } } }); } From a7cc809e20781cff0e72191f8cbb5801a51f06b9 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sat, 28 Sep 2019 04:19:51 -0600 Subject: [PATCH 056/166] (android) Remove e.printStackTrace when building JSONObjects. Since we expect those exceptions to be thrown often they are basically just annoying. If a user has experiences unexpected responses they won't help immediately anyways (since there are so many and so frequent). --- src/android/ChromecastUtilities.java | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/android/ChromecastUtilities.java b/src/android/ChromecastUtilities.java index 95d0aa5..c5296f4 100644 --- a/src/android/ChromecastUtilities.java +++ b/src/android/ChromecastUtilities.java @@ -223,7 +223,7 @@ static JSONObject createSessionObject(CastSession session) { } catch (JSONException e) { e.printStackTrace(); } catch (NullPointerException e) { - e.printStackTrace(); + } return out; @@ -240,7 +240,7 @@ private static JSONArray createAppImagesObject(CastSession session) { } } } catch (NullPointerException e) { - e.printStackTrace(); + } return appImages; } @@ -256,14 +256,14 @@ private static JSONObject createReceiverObject(CastSession session) { volume.put("level", session.getVolume()); volume.put("muted", session.isMute()); } catch (JSONException e) { - e.printStackTrace(); + } out.put("volume", volume); } catch (JSONException e) { - e.printStackTrace(); + } catch (NullPointerException e) { - e.printStackTrace(); + } return out; } @@ -312,9 +312,9 @@ static JSONObject createMediaObject(CastSession session) { } } catch (JSONException e) { - e.printStackTrace(); + } catch (NullPointerException e) { - e.printStackTrace(); + } return out; @@ -350,9 +350,9 @@ private static JSONArray createMediaInfoTracks(CastSession session) { out.put(jsonTrack); } } catch (JSONException e) { - e.printStackTrace(); + } catch (NullPointerException e) { - e.printStackTrace(); + } return out; @@ -383,9 +383,9 @@ private static JSONObject createMediaInfoObject(CastSession session) { // TODO: Check if it's useful //out.put("metadata", mediaInfo.getMetadata()); } catch (JSONException e) { - e.printStackTrace(); + } catch (NullPointerException e) { - e.printStackTrace(); + } return out; @@ -407,7 +407,7 @@ static JSONObject createTextTrackObject(TextTrackStyle textTrackStyle) { out.put("windowRoundedCornerRadius", textTrackStyle.getWindowCornerRadius()); out.put("windowType", getWindowType(textTrackStyle)); } catch (JSONException e) { - e.printStackTrace(); + } return out; @@ -445,7 +445,7 @@ static JSONObject createError(String code, String message) { out.put("code", code); out.put("description", message); } catch (JSONException e) { - e.printStackTrace(); + } return out; } From a04f0e4cb741644ac81b126f2c10a8d9dc3b8ef7 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sat, 28 Sep 2019 20:48:56 -0600 Subject: [PATCH 057/166] Kill any route scan that may be going on when setup is called. Do this because a route scan may have been triggered, then the user navigated away from the page before stopRouteScan was called. The new page load will call setup and stop any scan. Issue #36 --- src/android/Chromecast.java | 13 +++++++++++-- src/android/ChromecastConnection.java | 6 +++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index e1ca19b..0129823 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -139,7 +139,17 @@ public boolean execute(String action, JSONArray args, CallbackContext cbContext) */ public boolean setup(CallbackContext callbackContext) { this.eventCallback = callbackContext; - sendEvent("SETUP", new JSONArray()); + // Ensure any existing scan is stopped + connection.stopRouteScan(clientScan, new Runnable() { + @Override + public void run() { + if (scanCallback != null) { + scanCallback.error(ChromecastUtilities.createError("cancel", "Scan stopped because setup triggered.")); + scanCallback = null; + } + sendEvent("SETUP", new JSONArray()); + } + }); return true; } @@ -451,7 +461,6 @@ public void run() { }); return true; } - /** * This triggers an event on the JS-side. * @param eventName - The name of the JS event to trigger diff --git a/src/android/ChromecastConnection.java b/src/android/ChromecastConnection.java index f9f913e..e1afbc5 100644 --- a/src/android/ChromecastConnection.java +++ b/src/android/ChromecastConnection.java @@ -444,14 +444,14 @@ public void run() { public void onSessionEnded(CastSession castSession, int error) { getSessionManager().removeSessionManagerListener(this, CastSession.class); media.setSession(null); + if (callback != null) { + callback.success(); + } listener.onSessionEnd(ChromecastUtilities.createSessionObject(castSession, stopCasting ? "stopped" : "disconnected")); } }, CastSession.class); getSessionManager().endCurrentSession(stopCasting); - if (callback != null) { - callback.success(); - } } }); } From 6408db0e4771f28ddc116a0a72252124bf872769 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sat, 28 Sep 2019 23:25:44 -0600 Subject: [PATCH 058/166] Only update session.addMediaListnener (MEDIA_LOADED event) when an external sender loads media. Handle PLAYER_STATE_LOADING, returning buffering is better than unknown. Remove calls to super because they do nothing. session.getApplicationMetadata() throws IllegalStateException if called before connected, catch it like the others. --- src/android/ChromecastSession.java | 43 +++++++++++++--------------- src/android/ChromecastUtilities.java | 18 ++---------- 2 files changed, 22 insertions(+), 39 deletions(-) diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index 05d7173..ead184a 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -13,6 +13,7 @@ import com.google.android.gms.cast.MediaLoadRequestData; import com.google.android.gms.cast.MediaMetadata; import com.google.android.gms.cast.MediaSeekOptions; +import com.google.android.gms.cast.MediaStatus; import com.google.android.gms.cast.TextTrackStyle; import com.google.android.gms.cast.framework.CastSession; import com.google.android.gms.cast.framework.media.RemoteMediaClient; @@ -70,78 +71,74 @@ public void run() { return; } client.registerCallback(new RemoteMediaClient.Callback() { - private String currentMedia = ""; + private String currentState = "idle"; @Override public void onStatusUpdated() { - super.onStatusUpdated(); + MediaStatus status = client.getMediaStatus(); + if (status != null) { + switch (status.getPlayerState()) { + case MediaStatus.PLAYER_STATE_LOADING: + case MediaStatus.PLAYER_STATE_IDLE: + if (!currentState.equals("requesting")) { + currentState = "loading"; + } + break; + default: + if (currentState.equals("loading")) { + clientListener.onMediaLoaded(createMediaObject()); + } + currentState = "loaded"; + break; + } + } clientListener.onMediaUpdate(createMediaObject()); } @Override public void onMetadataUpdated() { - super.onMetadataUpdated(); - MediaInfo info = client.getMediaInfo(); - if (info == null) { - currentMedia = ""; - } else { - String newMedia = info.getContentId(); - if (!currentMedia.equals(newMedia)) { - currentMedia = newMedia; - clientListener.onMediaLoaded(createMediaObject()); - } - } clientListener.onMediaUpdate(createMediaObject()); } @Override public void onQueueStatusUpdated() { - super.onQueueStatusUpdated(); clientListener.onMediaUpdate(createMediaObject()); } @Override public void onPreloadStatusUpdated() { - super.onPreloadStatusUpdated(); clientListener.onMediaUpdate(createMediaObject()); } @Override public void onSendingRemoteMediaRequest() { - super.onSendingRemoteMediaRequest(); + currentState = "requesting"; clientListener.onMediaUpdate(createMediaObject()); } @Override public void onAdBreakStatusUpdated() { - super.onAdBreakStatusUpdated(); clientListener.onMediaUpdate(createMediaObject()); } }); session.addCastListener(new Cast.Listener() { @Override public void onApplicationStatusChanged() { - super.onApplicationStatusChanged(); clientListener.onSessionUpdate(createSessionObject()); } @Override public void onApplicationMetadataChanged(ApplicationMetadata appMetadata) { - super.onApplicationMetadataChanged(appMetadata); clientListener.onSessionUpdate(createSessionObject()); } @Override public void onApplicationDisconnected(int i) { - super.onApplicationDisconnected(i); clientListener.onSessionEnd( ChromecastUtilities.createSessionObject(session, "stopped")); } @Override public void onActiveInputStateChanged(int i) { - super.onActiveInputStateChanged(i); clientListener.onSessionUpdate(createSessionObject()); } @Override public void onStandbyStateChanged(int i) { - super.onStandbyStateChanged(i); clientListener.onSessionUpdate(createSessionObject()); } @Override public void onVolumeChanged() { - super.onVolumeChanged(); clientListener.onSessionUpdate(createSessionObject()); } }); diff --git a/src/android/ChromecastUtilities.java b/src/android/ChromecastUtilities.java index c5296f4..3d443c2 100644 --- a/src/android/ChromecastUtilities.java +++ b/src/android/ChromecastUtilities.java @@ -44,6 +44,7 @@ static String getMediaIdleReason(MediaStatus mediaStatus) { static String getMediaPlayerState(MediaStatus mediaStatus) { switch (mediaStatus.getPlayerState()) { + case MediaStatus.PLAYER_STATE_LOADING: case MediaStatus.PLAYER_STATE_BUFFERING: return "BUFFERING"; case MediaStatus.PLAYER_STATE_IDLE: @@ -187,7 +188,6 @@ static TextTrackStyle parseTextTrackStyle(JSONObject textTrackSytle) { out.setForegroundColor(Color.parseColor(textTrackSytle.getString("foregroundColor"))); } } catch (JSONException e) { - e.printStackTrace(); } return out; @@ -203,7 +203,6 @@ static JSONObject createSessionObject(CastSession session, String state) { try { s.put("status", state); } catch (JSONException e) { - } } return s; @@ -221,9 +220,8 @@ static JSONObject createSessionObject(CastSession session) { out.put("sessionId", session.getSessionId()); } catch (JSONException e) { - e.printStackTrace(); } catch (NullPointerException e) { - + } catch (IllegalStateException e) { } return out; @@ -240,7 +238,6 @@ private static JSONArray createAppImagesObject(CastSession session) { } } } catch (NullPointerException e) { - } return appImages; } @@ -256,14 +253,11 @@ private static JSONObject createReceiverObject(CastSession session) { volume.put("level", session.getVolume()); volume.put("muted", session.isMute()); } catch (JSONException e) { - } out.put("volume", volume); } catch (JSONException e) { - } catch (NullPointerException e) { - } return out; } @@ -312,9 +306,7 @@ static JSONObject createMediaObject(CastSession session) { } } catch (JSONException e) { - } catch (NullPointerException e) { - } return out; @@ -350,9 +342,7 @@ private static JSONArray createMediaInfoTracks(CastSession session) { out.put(jsonTrack); } } catch (JSONException e) { - } catch (NullPointerException e) { - } return out; @@ -383,9 +373,7 @@ private static JSONObject createMediaInfoObject(CastSession session) { // TODO: Check if it's useful //out.put("metadata", mediaInfo.getMetadata()); } catch (JSONException e) { - } catch (NullPointerException e) { - } return out; @@ -407,7 +395,6 @@ static JSONObject createTextTrackObject(TextTrackStyle textTrackStyle) { out.put("windowRoundedCornerRadius", textTrackStyle.getWindowCornerRadius()); out.put("windowType", getWindowType(textTrackStyle)); } catch (JSONException e) { - } return out; @@ -445,7 +432,6 @@ static JSONObject createError(String code, String message) { out.put("code", code); out.put("description", message); } catch (JSONException e) { - } return out; } From 9f3b23521ede53dc9841ea20235ab2535387fc11 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Tue, 1 Oct 2019 09:11:16 -0600 Subject: [PATCH 059/166] Issue #50 Issue #36 Converted the existing auto tests to mocha and misc improvements/fixes. Fixed some bugs: - typo in ChromecastUtilities "CANCELED" - session building, in particulate the media array was not being built correctly Improvements: - Improved many of the tests to be more specific and encourage following desktop chrome behavior more closely - Update the readme with more accurate information/documentation/and info about Mocha tests - Improved the utils functions (callOrder and waitForAllCalls) - Added pre-checks to match chrome behavior. (Checks for existing session and valid sessionId) - followed the _update pattern completely in chrome.cast --- .github/pull_request_template.md | 4 +- README.md | 56 +- src/android/ChromecastUtilities.java | 15 +- tests/package.json | 4 +- tests/plugin.xml | 7 +- tests/tests.js | 805 -- tests/www/css/tests.css | 12 + tests/www/html/tests.html | 27 + tests/www/html/tests_auto.html | 40 + tests/www/html/tests_manual.html | 25 + tests/www/js/runner.js | 52 + tests/www/js/tests_auto.js | 942 ++ tests/www/js/utils.js | 187 + tests/www/lib/mocha.css | 325 + tests/www/lib/mocha.js | 18229 +++++++++++++++++++++++++ tests/www/lib/readme.md | 9 + www/chrome.cast.js | 414 +- 17 files changed, 20143 insertions(+), 1010 deletions(-) delete mode 100644 tests/tests.js create mode 100644 tests/www/css/tests.css create mode 100644 tests/www/html/tests.html create mode 100644 tests/www/html/tests_auto.html create mode 100644 tests/www/html/tests_manual.html create mode 100644 tests/www/js/runner.js create mode 100644 tests/www/js/tests_auto.js create mode 100644 tests/www/js/utils.js create mode 100644 tests/www/lib/mocha.css create mode 100644 tests/www/lib/mocha.js create mode 100644 tests/www/lib/readme.md diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 86c8299..c9ce7ae 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -28,6 +28,6 @@ Thanks! - [ ] I've updated the documentation as necessary - [ ] If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct [keyword to close issues using keywords](https://help.github.com/articles/closing-issues-using-keywords/)) -- [ ] I've run `npm run test` and no errors were found (run `npm style` to auto-fix errors it can) -- [ ] I've run the `test-framework` tests for Android (See Readme) +- [ ] I've run `npm test` and no errors were found (run `npm style` to auto-fix errors it can) +- [ ] I've run the tests (See Readme) - [ ] I added automated test coverage as appropriate for this change \ No newline at end of file diff --git a/README.md b/README.md index 0974e0d..ebdb1bc 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,12 @@ cordova plugin add https://github.com/jellyfin/cordova-plugin-chromecast.git # Usage -This project attempts to implement the [official Google Cast API for Chrome](https://developers.google.com/cast/docs/reference/chrome/) within the Cordova webview. +This project attempts to implement the [official Google Cast API for Chrome](https://developers.google.com/cast/docs/reference/chrome#chrome.cast) within the Cordova webview. This means that you should be able to write almost identical code in cordova as you would if you were developing for desktop Chrome. -We have not implemented every function in the [API](https://developers.google.com/cast/docs/reference/chrome/) but most of the core functions are there. If you find a function is missing we welcome [pull requests](#contributing)! Alternatively, you can file an [issue](https://github.com/jellyfin/cordova-plugin-chromecast/issues), please include a code sample of the expected functionality if possible! +We have not implemented every function in the [API](https://developers.google.com/cast/docs/reference/chrome#chrome.cast) but most of the core functions are there. If you find a function is missing we welcome [pull requests](#contributing)! Alternatively, you can file an [issue](https://github.com/jellyfin/cordova-plugin-chromecast/issues), please include a code sample of the expected functionality if possible! -The only significant difference between the [cast API](https://developers.google.com/cast/docs/reference/chrome/) and this plugin is the initialization. +The only significant difference between the [cast API](https://developers.google.com/cast/docs/reference/chrome#chrome.cast) and this plugin is the initialization. In **Chrome desktop** you would do: ```js @@ -33,7 +33,7 @@ document.addEventListener("deviceready", function () { ``` ## Specific to this plugin -We have added some additional methods beyond the [Chromecast API]((https://developers.google.com/cast/docs/reference/chrome/)). +We have added some additional methods beyond the [Chromecast API]((https://developers.google.com/cast/docs/reference/chrome#chrome.cast)). They can all be found in the `chrome.cast.cordova` object. To make your own **custom route selector** use this: @@ -70,35 +70,45 @@ isNearbyDevice {boolean} - Is it a device only accessible via guest mode? ``` -## Formatting +# Plugin Development -* Run `npm run style` (from the plugin directory) - * If you get `Error: Cannot find module '\node_modules\eslint\bin\eslint'` - * Run `npm install` - * If it finds any formatting errors you can try and automatically fix them with: - * `node node_modules/eslint/bin/eslint --fix` - * Otherwise, please manually fix the error before committing +## Setup + +Follow these direction to set up for plugin development: + +* You will need an existing cordova project or [create a new cordova project](https://cordova.apache.org/#getstarted). +* With **admin permission** run: `cordova plugin add --link ` + +### About the `--link` flag +The `--link` flag allows you to modify the native code (java/swift/obj-c) directly in the relative platform folder if desired. + * This means you can work directly from Android Studio/Xcode! + * Note: Be careful about adding and deleting files. These changes will be exclusive to the platform folder and will not be transferred back to your plugin folder. + * Note: The link only works for native files. Other files such as js/css/html/etc must **not** be modified in the platform folder, these changes will be lost. + * To update the js/css/html/etc files you must run: + * `cordova plugin remove ` + * With **admin permission**: `cordova plugin add --link ` ## Testing -**1)** +### Code Format -Run `npm test` to ensure your code fits the styling. It will also pick some errors. +Run `npm test` to ensure your code fits the styling. It will also find some errors. -**2)** + * If errors are found, you can try running `npm run style`, this will attempt to automatically fix the errors. -This plugin has [cordova-plugin-test-framework](https://github.com/apache/cordova-plugin-test-framework) tests. +### Tests -To run these tests you can follow [these instructions](https://github.com/miloproductionsinc/cordova-testing). +How to run the tests: +* With **admin permission** run `cordova plugin add --link /tests` +* Change `config.xml`'s content tag to `` +* You must a valid chromecast on your network to run the tests. +* Run the app, let auto tests do its thing, and then follow the directions for manual tests. -NOTE: You must run these tests from a project with the package name `com.miloproductionsinc.plugin_tests` otherwise `SPEC_00310` will fail. (It uses a custom receiver which are only allowed receive from one package name.) - - * You can temporarily rename the project you are testing from: - * config.xml > `Cordova Chromecast Plugin Tests Apache 2.0 - - - - + + + diff --git a/tests/tests.js b/tests/tests.js deleted file mode 100644 index 156dd4f..0000000 --- a/tests/tests.js +++ /dev/null @@ -1,805 +0,0 @@ -/** - * The order of the tests is very important! - * Unfortunately using nested describes and beforeAll does not work correctly. - * So just be careful with the order of tests! - * Edit: TODO We should really switch to mocha. - */ - -// We need a promise polyfill for Android < 4.4.3 -// from https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js -/*eslint-disable */ -!(function (e, n) { typeof exports === 'object' && typeof module !== 'undefined' ? n() : typeof define === 'function' && define.amd ? define(n) : n(); }(0, function () { 'use strict'; function e (e) { var n = this.constructor; return this.then(function (t) { return n.resolve(e()).then(function () { return t; }); }, function (t) { return n.resolve(e()).then(function () { return n.reject(t); }); }); } function n (e) { return !(!e || typeof e.length === 'undefined'); } function t () {} function o (e) { if (!(this instanceof o)) throw new TypeError('Promises must be constructed via new'); if (typeof e !== 'function') throw new TypeError('not a function'); this._state = 0, this._handled = !1, this._value = undefined, this._deferreds = [], c(e, this); } function r (e, n) { for (;e._state === 3;)e = e._value; e._state !== 0 ? (e._handled = !0, o._immediateFn(function () { var t = e._state === 1 ? n.onFulfilled : n.onRejected; if (t !== null) { var o; try { o = t(e._value); } catch (r) { return void f(n.promise, r); }i(n.promise, o); } else (e._state === 1 ? i : f)(n.promise, e._value); })) : e._deferreds.push(n); } function i (e, n) { try { if (n === e) throw new TypeError('A promise cannot be resolved with itself.'); if (n && (typeof n === 'object' || typeof n === 'function')) { var t = n.then; if (n instanceof o) return e._state = 3, e._value = n, void u(e); if (typeof t === 'function') return void c((function (e, n) { return function () { e.apply(n, arguments); }; }(t, n)), e); }e._state = 1, e._value = n, u(e); } catch (r) { f(e, r); } } function f (e, n) { e._state = 2, e._value = n, u(e); } function u (e) { e._state === 2 && e._deferreds.length === 0 && o._immediateFn(function () { e._handled || o._unhandledRejectionFn(e._value); }); for (var n = 0, t = e._deferreds.length; t > n; n++)r(e, e._deferreds[n]); e._deferreds = null; } function c (e, n) { var t = !1; try { e(function (e) { t || (t = !0, i(n, e)); }, function (e) { t || (t = !0, f(n, e)); }); } catch (o) { if (t) return; t = !0, f(n, o); } } var a = setTimeout; o.prototype['catch'] = function (e) { return this.then(null, e); }, o.prototype.then = function (e, n) { var o = new this.constructor(t); return r(this, new function (e, n, t) { this.onFulfilled = typeof e === 'function' ? e : null, this.onRejected = typeof n === 'function' ? n : null, this.promise = t; }(e, n, o)), o; }, o.prototype['finally'] = e, o.all = function (e) { return new o(function (t, o) { function r (e, n) { try { if (n && (typeof n === 'object' || typeof n === 'function')) { var u = n.then; if (typeof u === 'function') return void u.call(n, function (n) { r(e, n); }, o); }i[e] = n, --f == 0 && t(i); } catch (c) { o(c); } } if (!n(e)) return o(new TypeError('Promise.all accepts an array')); var i = Array.prototype.slice.call(e); if (i.length === 0) return t([]); for (var f = i.length, u = 0; i.length > u; u++)r(u, i[u]); }); }, o.resolve = function (e) { return e && typeof e === 'object' && e.constructor === o ? e : new o(function (n) { n(e); }); }, o.reject = function (e) { return new o(function (n, t) { t(e); }); }, o.race = function (e) { return new o(function (t, r) { if (!n(e)) return r(new TypeError('Promise.race accepts an array')); for (var i = 0, f = e.length; f > i; i++)o.resolve(e[i]).then(t, r); }); }, o._immediateFn = typeof setImmediate === 'function' && function (e) { setImmediate(e); } || function (e) { a(e, 0); }, o._unhandledRejectionFn = function (e) { void 0 !== console && console && console.warn('Possible Unhandled Promise Rejection:', e); }; var l = (function () { if (typeof self !== 'undefined') return self; if (typeof window !== 'undefined') return window; if (typeof global !== 'undefined') return global; throw Error('unable to locate global object'); }()); 'Promise' in l ? l.Promise.prototype['finally'] || (l.Promise.prototype['finally'] = e) : l.Promise = o; })); -/*eslint-enable */ - -/* eslint-env jasmine */ -exports.defineAutoTests = function () { - /* eslint-disable no-undef */ - - jasmine.DEFAULT_TIMEOUT_INTERVAL = 9000; - - var appId = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID; - var videoUrl = 'https://archive.org/download/CosmosLaundromatFirstCycle/Cosmos%20Laundromat%20-%20First%20Cycle%20%281080p%29.mp4'; - - describe('chrome.cast', function () { - - it('SPEC_00100 should contain definitions', function () { - expect(chrome.cast.VERSION).toBeDefined(); - expect(chrome.cast.ReceiverAvailability).toBeDefined(); - expect(chrome.cast.ReceiverType).toBeDefined(); - expect(chrome.cast.SenderPlatform).toBeDefined(); - expect(chrome.cast.AutoJoinPolicy).toBeDefined(); - expect(chrome.cast.Capability).toBeDefined(); - expect(chrome.cast.DefaultActionPolicy).toBeDefined(); - expect(chrome.cast.ErrorCode).toBeDefined(); - expect(chrome.cast.timeout).toBeDefined(); - expect(chrome.cast.isAvailable).toBeDefined(); - expect(chrome.cast.ApiConfig).toBeDefined(); - expect(chrome.cast.Receiver).toBeDefined(); - expect(chrome.cast.DialRequest).toBeDefined(); - expect(chrome.cast.SessionRequest).toBeDefined(); - expect(chrome.cast.Error).toBeDefined(); - expect(chrome.cast.Image).toBeDefined(); - expect(chrome.cast.SenderApplication).toBeDefined(); - expect(chrome.cast.Volume).toBeDefined(); - expect(chrome.cast.media).toBeDefined(); - expect(chrome.cast.initialize).toBeDefined(); - expect(chrome.cast.requestSession).toBeDefined(); - expect(chrome.cast.setCustomReceivers).toBeDefined(); - expect(chrome.cast.Session).toBeDefined(); - expect(chrome.cast.media.PlayerState).toBeDefined(); - expect(chrome.cast.media.ResumeState).toBeDefined(); - expect(chrome.cast.media.MediaCommand).toBeDefined(); - expect(chrome.cast.media.MetadataType).toBeDefined(); - expect(chrome.cast.media.StreamType).toBeDefined(); - expect(chrome.cast.media.timeout).toBeDefined(); - expect(chrome.cast.media.LoadRequest).toBeDefined(); - expect(chrome.cast.media.PlayRequest).toBeDefined(); - expect(chrome.cast.media.SeekRequest).toBeDefined(); - expect(chrome.cast.media.VolumeRequest).toBeDefined(); - expect(chrome.cast.media.StopRequest).toBeDefined(); - expect(chrome.cast.media.PauseRequest).toBeDefined(); - expect(chrome.cast.media.GenericMediaMetadata).toBeDefined(); - expect(chrome.cast.media.MovieMediaMetadata).toBeDefined(); - expect(chrome.cast.media.MusicTrackMediaMetadata).toBeDefined(); - expect(chrome.cast.media.PhotoMediaMetadata).toBeDefined(); - expect(chrome.cast.media.TvShowMediaMetadata).toBeDefined(); - expect(chrome.cast.media.MediaInfo).toBeDefined(); - expect(chrome.cast.media.Media).toBeDefined(); - expect(chrome.cast.Session.prototype.setReceiverVolumeLevel).toBeDefined(); - expect(chrome.cast.Session.prototype.setReceiverMuted).toBeDefined(); - expect(chrome.cast.Session.prototype.stop).toBeDefined(); - expect(chrome.cast.Session.prototype.sendMessage).toBeDefined(); - expect(chrome.cast.Session.prototype.addUpdateListener).toBeDefined(); - expect(chrome.cast.Session.prototype.removeUpdateListener).toBeDefined(); - expect(chrome.cast.Session.prototype.addMessageListener).toBeDefined(); - expect(chrome.cast.Session.prototype.removeMessageListener).toBeDefined(); - expect(chrome.cast.Session.prototype.addMediaListener).toBeDefined(); - expect(chrome.cast.Session.prototype.removeMediaListener).toBeDefined(); - expect(chrome.cast.Session.prototype.loadMedia).toBeDefined(); - expect(chrome.cast.media.Media.prototype.play).toBeDefined(); - expect(chrome.cast.media.Media.prototype.pause).toBeDefined(); - expect(chrome.cast.media.Media.prototype.seek).toBeDefined(); - expect(chrome.cast.media.Media.prototype.stop).toBeDefined(); - expect(chrome.cast.media.Media.prototype.setVolume).toBeDefined(); - expect(chrome.cast.media.Media.prototype.supportsCommand).toBeDefined(); - expect(chrome.cast.media.Media.prototype.getEstimatedTime).toBeDefined(); - expect(chrome.cast.media.Media.prototype.addUpdateListener).toBeDefined(); - expect(chrome.cast.media.Media.prototype.removeUpdateListener).toBeDefined(); - }); - - it('SPEC_00300 chrome.cast.cordova functions, receiver volume, videoEnd, and leaveSession', function (done) { - setupEarlyTerminator(done); - Promise.resolve() - .then(apiAvailable) - .then(initialize('SPEC_00300', function (session) { - test().fail('should not receive a session (make sure there is no active cast session when starting the tests)'); - })) - .then(startRouteScan) - .then(stopRouteScan) - .then(selectRoute) - .then(selectRoute_fail_alreadyJoined) - .then(session_setReceiverVolumeLevel_success) - .then(session_setReceiverMuted_success) - .then(function (session) { - Promise.resolve(session) - .then(loadMediaVideo) - .then(videoEnd) - .then(function (media) { - Promise.resolve(session) - .then(sessionLeaveSuccess) - .then(initialize('SPEC_00330', function (session) { - test().fail('should not receive a session (we did sessionLeave so we shouldnt be able to auto rejoin rejoin)'); - })) - .then(sessionLeaveError_alreadyLeft) - .then(done); - }); - }); - }, 25 * 1000); - - /** - * Pre-requisite: You must have a valid receiver (chromecast) plugged in and available. - */ - it('SPEC_00400 Normal usage cycle', function (done) { - setupEarlyTerminator(done); - Promise.resolve() - .then(apiAvailable) - .then(initialize('SPEC_00400', function (session) { - test().fail('should not receive a session (make sure there is no active cast session when starting the tests)'); - })) - .then(requestSessionCancel) - .then(requestSessionSuccess) - .then(function (session) { - Promise.resolve(session) - .then(loadMediaVideo) - .then(pauseSuccess) - .then(playSuccess) - .then(seekSuccess) - .then(media_setVolume_level_success) - .then(media_setVolume_muted_success) - .then(media_setVolume_level_and_unmuted_success) - .then(stopSuccess) - .then(function (media) { - Promise.resolve(session) - .then(requestSessionStopCastingUiCancel) - .then(requestSessionStopCastingUiStopSuccess) - .then(done); - }); - }); - - }, 60 * 1000); - - /** - * When on a new page, initialize should be called again - * This should result in the session being passed to the - * session listener as long as teh requested appId does - * not change. - */ - it('SPEC_00500 stopSession and new page simulation', function (done) { - setupEarlyTerminator(done); - Promise.resolve() - .then(apiAvailable) - .then(initialize('SPEC_00503', function (session) { - throw new Error('should not receive a session (make sure there is no active cast session when starting the tests)'); - })) - .then(requestSessionSuccess) - .then(initialize('SPEC_00506', function (session) { - Promise.resolve(session) - .then(sessionProperties) - .then(sessionStopSuccess) - .then(sessionStopError_noSession) - .then(sessionLeaveError_noSession) - .then(done); - })); - }, 20 * 1000); - - function setupEarlyTerminator (done) { - // Add this so that thrown errors are not obscured. - // This is required for early test termination - window.addEventListener('cordovacallbackerror', function (e) { - window.removeEventListener('cordovacallbackerror', this); - fail(e.stack); - done(); - }); - expect('just to make jasmine happy that there is an expect in the tests').toBeDefined(); - } - - function apiAvailable () { - return new Promise(function (resolve) { - var interval = setInterval(function () { - if (chrome.cast.isAvailable) { - test(chrome.cast.isAvailable).toEqual(true); - clearInterval(interval); - resolve(); - } - }, 500); - }); - } - - function initialize (spec, sessionListener) { - return function (arg) { - var specName = spec + '_' + initialize.name; - var success = 'success'; - var unavailable = 'unavailable'; - var available = 'available'; - return new Promise(function (resolve) { - var called = callOrder(specName, [success, unavailable, available], {}, function () { - resolve(arg); - }); - var gotUnavailable = false; - var finished = false; // Need this so we stop testing after being finished - var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(appId), sessionListener, function receiverListener (availability) { - if (finished) { - return; - } - if (!gotUnavailable) { - // Wait until we get the first unavailable - if (availability === unavailable) { - gotUnavailable = true; - called(unavailable); - } - } else { - // We are allowed to have multiple unavailable before available - if (availability === available) { - finished = true; - called(available); - } - } - }); - chrome.cast.initialize(apiConfig, function () { - called(success); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - }; - } - - function requestSessionCancel () { - return new Promise(function (resolve) { - alert('---TEST INSTRUCTION---\nPlease click outside of the next dialog to dismiss it.'); - chrome.cast.requestSession(function (session) { - test().fail('We should not reach here on dismiss (make sure you cancelled the dialog for this test!)'); - }, function (err) { - test(err).toBeInstanceOf(chrome.cast.Error); - test(err.code).toEqual(chrome.cast.ErrorCode.CANCEL); - resolve(); - }); - }); - } - - function requestSessionSuccess () { - return new Promise(function (resolve) { - alert('---TEST INSTRUCTION---\nPlease select a valid chromecast in the next dialog.'); - chrome.cast.requestSession(function (session) { - Promise.resolve(session) - .then(sessionProperties) - .then(resolve); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - } - - function sessionProperties (session) { - return new Promise(function (resolve) { - test(session).toBeInstanceOf(chrome.cast.Session); - test(session.appId).toBeDefined(); - test(session.displayName).toBeDefined(); - test(session.receiver).toBeDefined(); - test(session.receiver.friendlyName).toBeDefined(); - test(session.addUpdateListener).toBeDefined(); - test(session.removeUpdateListener).toBeDefined(); - test(session.loadMedia).toBeDefined(); - - resolve(session); - }); - } - - function loadMediaVideo (session) { - var specName = loadMediaVideo.name; - var success = 'success'; - var loaded = 'loaded'; - return new Promise(function (resolve) { - var loadedMedia; - var called = callOrder(specName, [success, loaded], { anyOrder: true }, function () { - // Run all the media related tests - Promise.resolve({media: loadedMedia, session: session}) - .then(mediaProperties) - .then(resolve); - }); - session.addMediaListener(function (media) { - Promise.resolve({media: media, session: session}) - .then(mediaProperties) - .then(function () { - called(loaded); - }); - }); - session.loadMedia(new chrome.cast.media.LoadRequest( - new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4') - ), function (media) { - loadedMedia = media; - called(success); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - } - - function mediaProperties (data) { - return new Promise(function (resolve) { - var media = data.media; - var session = data.session; - test(media).toBeInstanceOf(chrome.cast.media.Media); - test(media.sessionId).toEqual(session.sessionId); - test(media.addUpdateListener).toBeDefined(); - test(media.removeUpdateListener).toBeDefined(); - resolve(media); - }); - } - - function pauseSuccess (media) { - return new Promise(function (resolve) { - setTimeout(function () { - media.pause(null, function () { - resolve(media); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }, 500); - }); - } - - function playSuccess (media) { - return new Promise(function (resolve) { - setTimeout(function () { - media.play(null, function () { - resolve(media); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }, 500); - }); - } - - function seekSuccess (media) { - return new Promise(function (resolve) { - setTimeout(function () { - var request = new chrome.cast.media.SeekRequest(); - request.currentTime = 3; - media.seek(request, function () { - resolve(media); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }, 500); - }); - } - - function videoEnd (media) { - var specName = videoEnd.name; - var success = 'success'; - var videoFinished = 'videoFinished'; - return new Promise(function (resolve) { - var finished = false; - var called = callOrder(specName, [success, videoFinished], { anyOrder: true }, function () { - resolve(media); - }); - media.addUpdateListener(function (isAlive) { - test(media.playerState).toBeDefined(); - if (!finished && media.playerState === 'IDLE' - && media.idleReason === 'FINISHED') { - finished = true; - called(videoFinished); - } - }); - setTimeout(function () { - var request = new chrome.cast.media.SeekRequest(); - request.currentTime = media.media.duration; - media.seek(request, function () { - called(success); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }, 500); - }); - } - - function media_setVolume_level_success (media) { - // Set up the call order - var specName = media_setVolume_level_success.name; - var success = 'success'; - var update = 'update'; - return new Promise(function (resolve) { - var called = callOrder(specName, [success, update], { anyOrder: true }, function () { - resolve(media); - }); - - // Ensure we select a different volume - var vol = media.volume.media; - if (vol) { - vol = Math.abs(vol - 0.5); - } else { - vol = Math.round(Math.random() * 100) / 100; - } - var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(vol)); - - media.addUpdateListener(function listener (isAlive) { - test(isAlive).toEqual(true); - test(media.volume).toBeInstanceOf(chrome.cast.Volume); - if (media.volume.level === vol) { - media.removeUpdateListener(listener); - called(update); - } - }); - - media.setVolume(request, function () { - called(success); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - } - - function media_setVolume_muted_success (media) { - // Set up the call order - var specName = media_setVolume_muted_success.name; - var success = 'success'; - var update = 'update'; - return new Promise(function (resolve) { - var called = callOrder(specName, [success, update], { anyOrder: true }, function () { - resolve(media); - }); - - var muted = true; - - media.addUpdateListener(function listener (isAlive) { - test(isAlive).toEqual(true); - test(media.volume).toBeInstanceOf(chrome.cast.Volume); - if (media.volume.muted === muted) { - media.removeUpdateListener(listener); - called(update); - } - }); - - media.setVolume(new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, muted)), function () { - called(success); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - } - - function media_setVolume_level_and_unmuted_success (media) { - // Set up the call order - var specName = media_setVolume_level_and_unmuted_success.name; - var success = 'success'; - var update = 'update'; - return new Promise(function (resolve) { - var called = callOrder(specName, [success, update], { anyOrder: true }, function () { - resolve(media); - }); - - var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(0.2, false)); - - media.addUpdateListener(function listener (isAlive) { - test(isAlive).toEqual(true); - test(media.volume).toBeInstanceOf(chrome.cast.Volume); - if (media.volume.level === request.volume.level - && media.volume.muted === request.volume.muted) { - media.removeUpdateListener(listener); - called(update); - } - }); - - media.setVolume(request, function () { - called(success); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - } - - function session_setReceiverVolumeLevel_success (session) { - // Set up the call order - var specName = session_setReceiverVolumeLevel_success.name; - var success = 'success'; - var update = 'update'; - return new Promise(function (resolve) { - var called = callOrder(specName, [success, update], { anyOrder: true }, function () { - resolve(session); - }); - - // Make sure the request volume is significantly different - var requestedVolume = Math.abs(session.receiver.volume.level - 0.5); - - session.addUpdateListener(function listener (isAlive) { - test(isAlive).toEqual(true); - test(session.receiver).toBeDefined(); - test(session.receiver.volume).toBeDefined(); - // The receiver volume is approximate - if (session.receiver.volume.level > requestedVolume - 0.1 && - session.receiver.volume.level < requestedVolume + 0.1) { - session.removeUpdateListener(listener); - called(update); - } - }); - - session.setReceiverVolumeLevel(requestedVolume, function () { - called(success); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - } - - function session_setReceiverMuted_success (session) { - // Set up the call order - var specName = session_setReceiverMuted_success.name; - var success = 'success'; - var update = 'update'; - return new Promise(function (resolve) { - var called = callOrder(specName, [success, update], { anyOrder: true }, function () { - resolve(session); - }); - - // Do the opposite mute state as current - var muted = !session.receiver.volume.muted; - - session.addUpdateListener(function listener (isAlive) { - test(isAlive).toEqual(true); - test(session.receiver).toBeDefined(); - test(session.receiver.volume).toBeDefined(); - if (session.receiver.volume.muted === muted) { - session.removeUpdateListener(listener); - called(update); - } - }); - - session.setReceiverMuted(muted, function () { - called(success); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - } - - function stopSuccess (media) { - return new Promise(function (resolve) { - media.stop(null, function () { - resolve(media); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - } - - function requestSessionStopCastingUiCancel (session) { - return new Promise(function (resolve) { - alert('---TEST INSTRUCTION---\nPlease click outside of the next dialog to dismiss it.'); - chrome.cast.requestSession(function () { - test().fail('We should not reach here on dismiss'); - }, function (err) { - test(err).toBeInstanceOf(chrome.cast.Error); - test(err.code).toEqual(chrome.cast.ErrorCode.CANCEL); - resolve(session); - }); - }); - } - - function requestSessionStopCastingUiStopSuccess (session) { - // Set up the call order - var specName = requestSessionStopCastingUiStopSuccess.name; - var error = 'error'; - var update = 'update'; - return new Promise(function (resolve) { - var called = callOrder(specName, [error, update], { anyOrder: true }, function () { - resolve(session); - }); - alert('---TEST INSTRUCTION---\nPlease click "Stop casting"'); - session.addUpdateListener(function listener (isAlive) { - if (session.status === chrome.cast.SessionStatus.STOPPED) { - test(isAlive).toEqual(false); - session.removeUpdateListener(listener); - called(update); - } - }); - - chrome.cast.requestSession(function () { - test().fail('We should not reach here on stop casting'); - }, function (err) { - test(err).toBeInstanceOf(chrome.cast.Error); - test(err.code).toEqual(chrome.cast.ErrorCode.CANCEL); - called(error); - }); - }); - } - - function sessionLeaveSuccess (session) { - // Set up the call order - var specName = sessionLeaveSuccess.name; - var success = 'success'; - var update = 'update'; - return new Promise(function (resolve) { - // We need to hit both of the callbacks - var called = callOrder(specName, [success, update], { anyOrder: true }, function () { - resolve(session); - }); - session.addUpdateListener(function listener (isAlive) { - test(isAlive).toEqual(true); - if (session.status === chrome.cast.SessionStatus.DISCONNECTED) { - session.removeUpdateListener(listener); - called(update); - } - }); - session.leave(function () { - called(success); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - } - - function sessionLeaveError_alreadyLeft (session) { - return new Promise(function (resolve) { - session.leave(function () { - test().fail('session.leave - Should not call success'); - }, function (err) { - test(err).toBeInstanceOf(chrome.cast.Error); - test(err.code).toEqual(chrome.cast.Error.INVALID_PARAMETER); - test(err.description).toEqual('No active session'); - resolve(session); - }); - }); - } - - function sessionLeaveError_noSession (session) { - return new Promise(function (resolve) { - session.leave(function () { - test().fail('session.leave - Should not call success'); - }, function (err) { - test(err).toBeInstanceOf(chrome.cast.Error); - test(err.code).toEqual(chrome.cast.Error.INVALID_PARAMETER); - test(err.description).toEqual('No active session'); - resolve(session); - }); - }); - } - - function sessionStopSuccess (session) { - // Set up the call order - var specName = sessionStopSuccess.name; - var success = 'success'; - var update = 'update'; - return new Promise(function (resolve) { - var called = callOrder(specName, [success, update], { anyOrder: true }, function () { - resolve(session); - }); - session.addUpdateListener(function listener (isAlive) { - if (session.status === chrome.cast.SessionStatus.STOPPED) { - test(isAlive).toEqual(false); - session.removeUpdateListener(listener); - called(update); - } - }); - session.stop(function () { - called(success); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - } - - function sessionStopError_noSession (session) { - return new Promise(function (resolve) { - session.stop(function () { - test().fail('session.stop - Should not call success'); - }, function (err) { - test(err).toBeInstanceOf(chrome.cast.Error); - test(err.code).toEqual(chrome.cast.Error.INVALID_PARAMETER); - test(err.description).toEqual('No active session'); - resolve(session); - }); - }); - } - - function startRouteScan () { - return new Promise(function (resolve) { - var foundRoute; - chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { - if (foundRoute) { - return; - } - for (var i = 0; i < routes.length; i++) { - var route = routes[i]; - test(route).toBeInstanceOf(chrome.cast.cordova.Route); - test(route.id).toBeDefined(); - test(route.name).toBeDefined(); - test(route.isNearbyDevice).toBeDefined(); - test(route.isCastGroup).toBeDefined(); - // Find a non-nearby device so that the join is automatic - if (!route.isNearbyDevice) { - foundRoute = route; - } - } - if (foundRoute) { - resolve(foundRoute); - } - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - } - - function stopRouteScan (arg) { - return new Promise(function (resolve) { - // Make sure we can stop the scan - chrome.cast.cordova.stopRouteScan(function () { - resolve(arg); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - } - - function selectRoute (route) { - return new Promise(function (resolve) { - chrome.cast.cordova.selectRoute(route.id, function (session) { - Promise.resolve(session) - .then(sessionProperties) - .then(resolve); - }, function (err) { - test().fail(err.code + ': ' + err.description); - }); - }); - } - - function selectRoute_fail_alreadyJoined (arg) { - return new Promise(function (resolve) { - chrome.cast.cordova.selectRoute('', function (session) { - test().fail('Should not be allowed to selectRoute when already in session'); - }, function (err) { - test(err).toBeInstanceOf(chrome.cast.Error); - test(err.code).toEqual(chrome.cast.ErrorCode.CORDOVA_ALREADY_JOINED); - resolve(arg); - }); - }); - } - - function test (actual) { - return { - toEqual: function (expected) { - if (actual !== expected) { - throw new Error('Expected "' + actual + '" to be "' + expected + '"'); - } - }, - toBeInstanceOf: function (expected) { - if (!(actual instanceof expected)) { - throw new Error('Expected "' + actual + '" to be an instance of "' + expected.name + '"'); - } - }, - toBeDefined: function () { - if (actual === null || actual === undefined) { - throw new Error('Expected "' + actual + '" to be defined.'); - } - }, - fail: function (message) { - throw new Error(message); - } - }; - } - - /** - * Set up the callOrder outside of a promise and it will automatically - * add the calling function name to outputs. - * @param {string} spec - (optional) name of test for outputting on failure - * @param {array} order - array of strings that dictate the expected order of calls - * @param {object} options - - * @property {boolean} anyOrder - if the order calls can happen in any order - * @param {function} callback - called when all the calls in order have happened - */ - function callOrder (spec, order, options, callback) { - options = options || {}; - var called = []; - spec = spec ? spec + '_' : ''; - - return function (callName) { - if (options.anyOrder) { - var index = order.indexOf(callName); - if (index > -1) { - called.push(order.splice(index, 1)[0]); - } else if (called.indexOf(callName) === -1) { - test().fail('Did not expect this call: ' + spec + callName); - } - } else { - var expected = order.splice(0, 1)[0]; - if (callName !== expected) { - test().fail('Expected call, "' + spec + expected + '", got, "' + spec + callName + '"'); - } - } - if (order.length === 0) { - callback(); - } - }; - } - }); -}; diff --git a/tests/www/css/tests.css b/tests/www/css/tests.css new file mode 100644 index 0000000..3414998 --- /dev/null +++ b/tests/www/css/tests.css @@ -0,0 +1,12 @@ +button { + height: 3em; + width: 10em; + margin-top: 1em; + margin-bottom: 0em; +} +.center-horizontal { + display: block; + margin-left: auto; + margin-right: auto; + text-align: center; + } \ No newline at end of file diff --git a/tests/www/html/tests.html b/tests/www/html/tests.html new file mode 100644 index 0000000..6c56ac5 --- /dev/null +++ b/tests/www/html/tests.html @@ -0,0 +1,27 @@ + + + Cordova tests + + + + + + + + + +

    cordova-plugin-chromecast Tests

    + + + diff --git a/tests/www/html/tests_auto.html b/tests/www/html/tests_auto.html new file mode 100644 index 0000000..b9c8476 --- /dev/null +++ b/tests/www/html/tests_auto.html @@ -0,0 +1,40 @@ + + + Cordova tests + + + + + + + + + + + + + + +

    Auto Tests

    + + +
    + + + + diff --git a/tests/www/html/tests_manual.html b/tests/www/html/tests_manual.html new file mode 100644 index 0000000..b7bd95a --- /dev/null +++ b/tests/www/html/tests_manual.html @@ -0,0 +1,25 @@ + + + Cordova tests + + + + + + + + + + + +

    Manual Tests

    + + + + diff --git a/tests/www/js/runner.js b/tests/www/js/runner.js new file mode 100644 index 0000000..acc881f --- /dev/null +++ b/tests/www/js/runner.js @@ -0,0 +1,52 @@ +/** + * Dependencies: + * - Load after the mocha scrip has been loaded. + */ +(function () { + 'use strict'; + /* eslint-env mocha */ + + // Save htmlReporter reference + var htmlReporter = mocha._reporter; + + // Create a custom reporter so that we can console log errors + // with linking to source for quick debugging in dev tools + var myReporter = function (runner, options) { + // Add the error listener + runner.on('fail', function (test, err) { + // Need to add the full code location path + // so that the debugger can link to it + + // get the prepend path + var prependPath = window.location.href.split('/'); + prependPath.pop(); + prependPath = prependPath.join('/') + '/'; + + var lines = err.stack.split('\n'); + var line, filePath; + for (var i = 1; i < lines.length; i++) { + line = lines[i]; + // Make sure the line fits the format of a line with a code link + if (line.match(/^ *at .* \([^(]*\)$/)) { + line = line.split('('); + filePath = line[line.length - 1]; + // Does the path need pre-pending? + if (filePath.indexOf(window.location.origin) === -1) { + // Insert the full path to the file + line[line.length - 1] = prependPath + filePath; + // Rejoin the line + lines[i] = line.join('('); + } + } + } + // Log the error + console.error(lines.join('\n')); + }); + // And return the default HTML reporter + htmlReporter.call(this, runner, options); + }; + myReporter.prototype = htmlReporter.prototype; + + window['cordova-plugin-chromecast-tests'] = window['cordova-plugin-chromecast-tests'] || {}; + window['cordova-plugin-chromecast-tests'].customHtmlReporter = myReporter; +}()); diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js new file mode 100644 index 0000000..26b89a4 --- /dev/null +++ b/tests/www/js/tests_auto.js @@ -0,0 +1,942 @@ +/** + * The order of these tests and this.bail(true) is very important. + * + * Rather than nesting deep with describes and before's we just ensure the + * tests occur in the correct order. + * The major advantage to this is not having to repeat test code frequently + * making the suite slow. + * eg. To truly isolate and test session.leave we would need a before which + * runs startScan, get a valid route, stopScan, and selectRoute. And these + * would all need to be tested before using them in the before. This is + * where the duplication and significant slowing would come from. + */ + +(function () { + 'use strict'; + /* eslint-env mocha */ + /* global chrome */ + + // Set the reporter + mocha.setup({ + ui: 'bdd', + reporter: window['cordova-plugin-chromecast-tests'].customHtmlReporter + }); + + var assert = window.chai.assert; + var utils = window['cordova-plugin-chromecast-tests'].utils; + + describe('cordova-plugin-chromecast', function () { + this.timeout(10000); + this.slow(8000); + this.bail(true); + + var videoUrl = 'https://archive.org/download/CosmosLaundromatFirstCycle/Cosmos%20Laundromat%20-%20First%20Cycle%20%281080p%29.mp4'; + + // callOrder constants that are re-used frequently + var success = 'success'; + var update = 'update'; + var stopped = 'stopped'; + + var session; + + it('API should be available', function (done) { + var interval = setInterval(function () { + if (chrome && chrome.cast && chrome.cast.isAvailable) { + clearInterval(interval); + done(); + } + }, 100); + }); + + it('chrome.cast should contain definitions', function () { + assert.exists(chrome.cast.VERSION); + assert.exists(chrome.cast.ReceiverAvailability); + assert.exists(chrome.cast.ReceiverType); + assert.exists(chrome.cast.SenderPlatform); + assert.exists(chrome.cast.AutoJoinPolicy); + assert.exists(chrome.cast.Capability); + assert.exists(chrome.cast.DefaultActionPolicy); + assert.exists(chrome.cast.ErrorCode); + assert.exists(chrome.cast.timeout); + assert.exists(chrome.cast.isAvailable); + assert.exists(chrome.cast.ApiConfig); + assert.exists(chrome.cast.Receiver); + assert.exists(chrome.cast.DialRequest); + assert.exists(chrome.cast.SessionRequest); + assert.exists(chrome.cast.Error); + assert.exists(chrome.cast.Image); + assert.exists(chrome.cast.SenderApplication); + assert.exists(chrome.cast.Volume); + assert.exists(chrome.cast.media); + assert.exists(chrome.cast.initialize); + assert.exists(chrome.cast.requestSession); + assert.exists(chrome.cast.setCustomReceivers); + assert.exists(chrome.cast.Session); + assert.exists(chrome.cast.media.PlayerState); + assert.exists(chrome.cast.media.ResumeState); + assert.exists(chrome.cast.media.MediaCommand); + assert.exists(chrome.cast.media.MetadataType); + assert.exists(chrome.cast.media.StreamType); + assert.exists(chrome.cast.media.timeout); + assert.exists(chrome.cast.media.LoadRequest); + assert.exists(chrome.cast.media.PlayRequest); + assert.exists(chrome.cast.media.SeekRequest); + assert.exists(chrome.cast.media.VolumeRequest); + assert.exists(chrome.cast.media.StopRequest); + assert.exists(chrome.cast.media.PauseRequest); + assert.exists(chrome.cast.media.GenericMediaMetadata); + assert.exists(chrome.cast.media.MovieMediaMetadata); + assert.exists(chrome.cast.media.MusicTrackMediaMetadata); + assert.exists(chrome.cast.media.PhotoMediaMetadata); + assert.exists(chrome.cast.media.TvShowMediaMetadata); + assert.exists(chrome.cast.media.MediaInfo); + assert.exists(chrome.cast.media.Media); + assert.exists(chrome.cast.Session.prototype.setReceiverVolumeLevel); + assert.exists(chrome.cast.Session.prototype.setReceiverMuted); + assert.exists(chrome.cast.Session.prototype.stop); + assert.exists(chrome.cast.Session.prototype.sendMessage); + assert.exists(chrome.cast.Session.prototype.addUpdateListener); + assert.exists(chrome.cast.Session.prototype.removeUpdateListener); + assert.exists(chrome.cast.Session.prototype.addMessageListener); + assert.exists(chrome.cast.Session.prototype.removeMessageListener); + assert.exists(chrome.cast.Session.prototype.addMediaListener); + assert.exists(chrome.cast.Session.prototype.removeMediaListener); + assert.exists(chrome.cast.Session.prototype.loadMedia); + assert.exists(chrome.cast.media.Media.prototype.play); + assert.exists(chrome.cast.media.Media.prototype.pause); + assert.exists(chrome.cast.media.Media.prototype.seek); + assert.exists(chrome.cast.media.Media.prototype.stop); + assert.exists(chrome.cast.media.Media.prototype.setVolume); + assert.exists(chrome.cast.media.Media.prototype.supportsCommand); + assert.exists(chrome.cast.media.Media.prototype.getEstimatedTime); + assert.exists(chrome.cast.media.Media.prototype.addUpdateListener); + assert.exists(chrome.cast.media.Media.prototype.removeUpdateListener); + assert.exists(chrome.cast.cordova.startRouteScan); + assert.exists(chrome.cast.cordova.stopRouteScan); + assert.exists(chrome.cast.cordova.selectRoute); + assert.exists(chrome.cast.cordova.Route); + }); + + it('chrome.cast.initialize should successfully initialize', function (done) { + var unavailable = 'unavailable'; + var available = 'available'; + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: unavailable, repeats: true }, + { id: available, repeats: true } + ], function () { + finished = true; + done(); + }); + var finished = false; // Need this so we stop testing after being finished + var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function sessionListener (session) { + assert.fail('should not receive a session (make sure there is no active cast session when starting the tests)'); + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }); + chrome.cast.initialize(apiConfig, function () { + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + + describe('chrome.cast.cordova functions and session.leave', function () { + var _route; + it('should have definitions', function () { + assert.exists(chrome.cast.cordova); + assert.exists(chrome.cast.cordova.startRouteScan); + assert.exists(chrome.cast.cordova.stopRouteScan); + assert.exists(chrome.cast.cordova.selectRoute); + assert.exists(chrome.cast.cordova.Route); + }); + it('startRouteScan 2nd call should result in error for first', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + var secondStarted = false; + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (secondStarted) { + assert.fail('Should not be receiving route updates here anymore.'); + } + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + // We should get updates from this scan + called(update); + }, function (err) { + // The only acceptable way for this scan to stop + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped.'); + }); + }, function (err) { + secondStarted = true; + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Started a new route scan before stopping previous one.'); + called(success); + }); + }); + it('stopRouteScan 2nd call should succeed', function (done) { + chrome.cast.cordova.stopRouteScan(function () { + chrome.cast.cordova.stopRouteScan(function () { + done(); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('startRouteScan should find valid routes', function (done) { + _route = undefined; + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (_route) { + return; // we have already found a valid route + } + var route; + for (var i = 0; i < routes.length; i++) { + route = routes[i]; + assert.instanceOf(route, chrome.cast.cordova.Route); + assert.isString(route.id); + assert.isString(route.name); + assert.isBoolean(route.isNearbyDevice); + assert.isBoolean(route.isCastGroup); + if (!route.isNearbyDevice) { + _route = route; + } + } + if (_route) { + done(); + } + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + }); + }); + it('stopRouteScan should succeed and trigger cancel error in startRouteScan', function (done) { + var scanState = 'running'; + var called = utils.callOrder([ + { id: stopped, repeats: false }, + { id: success, repeats: false } + ], function () { + done(); + }); + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (scanState === 'stopped') { + assert.fail('Should not have gotten route update after scan was stopped'); + } + if (scanState === 'running') { + scanState = 'stopping'; + chrome.cast.cordova.stopRouteScan(function () { + scanState = 'stopped'; + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + } + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped.'); + called(stopped); + }); + }); + it('selectRoute should receive a TIMEOUT error if route does not exist', function (done) { + this.timeout(20000); + var routeId = 'non-existant-route-id'; + chrome.cast.cordova.selectRoute(routeId, function (session) { + assert.fail('should not have hit the success callback'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.TIMEOUT); + assert.match(err.description, new RegExp('^Failed to join route \\(' + routeId + '\\) after 15s and [0-9]* tries\\.$')); + done(); + }); + }); + it('selectRoute should return a valid session after selecting a route', function (done) { + chrome.cast.cordova.selectRoute(_route.id, function (sess) { + session = sess; + utils.testSessionProperties(sess); + done(); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('selectRoute should return error if already joined', function (done) { + chrome.cast.cordova.selectRoute('', function (session) { + assert.fail('Should not be allowed to selectRoute when already in session'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'Leave or stop current session before attempting to join new session.'); + done(); + }); + }); + it('session.leave should leave the session', function (done) { + // Set up the expected calls + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + session.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + if (session.status === chrome.cast.SessionStatus.DISCONNECTED) { + session.removeUpdateListener(listener); + called(update); + } + }); + session.leave(function () { + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('initialize should not receive a session after session.leave', function (done) { + var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function sessionListener (session) { + assert.fail('should not receive a session (we did sessionLeave so we shouldnt be able to auto rejoin rejoin)'); + }); + chrome.cast.initialize(apiConfig, function () { + done(); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('session.leave should give an error if session already left', function (done) { + session.leave(function () { + assert.fail('session.leave - Should not call success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.INVALID_PARAMETER); + assert.equal(err.description, 'No active session'); + done(); + }); + }); + after(function (done) { + // Make sure we have left the session + session.leave(function () { + done(); + }, function () { + done(); + }); + }); + }); + + describe('chrome.cast session functions', function () { + before(function (done) { + // need to have a valid session to run these tests + session = null; + var scanState = 'running'; + var foundRoute = null; + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (scanState === 'stopped') { + assert.fail('Should not have gotten route update after scan was stopped'); + } + var route; + for (var i = 0; i < routes.length; i++) { + route = routes[i]; + assert.instanceOf(route, chrome.cast.cordova.Route); + assert.isString(route.id); + assert.isString(route.name); + assert.isBoolean(route.isNearbyDevice); + assert.isBoolean(route.isCastGroup); + if (!route.isNearbyDevice && !route.isCastGroup) { + foundRoute = route; + } + } + if (foundRoute && scanState === 'running') { + scanState = 'stopping'; + chrome.cast.cordova.stopRouteScan(function () { + scanState = 'stopped'; + chrome.cast.cordova.selectRoute(foundRoute.id, function (sess) { + utils.testSessionProperties(sess); + session = sess; + done(); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + } + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped.'); + }); + }); + it('session.setReceiverMuted should mute the volume', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], function () { + session.removeUpdateListener(listener); + done(); + }); + + // Do the opposite mute state as current + var muted = !session.receiver.volume.muted; + + function listener (isAlive) { + + assert.isTrue(isAlive); + assert.isObject(session.receiver); + assert.isObject(session.receiver.volume); + if (session.receiver.volume.muted === muted) { + called(update); + } + } + session.addUpdateListener(listener); + session.setReceiverMuted(muted, function () { + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('session.setReceiverVolumeLevel should set the volume level', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], function () { + session.removeUpdateListener(listener); + done(); + }); + + // Make sure the request volume is significantly different + var requestedVolume = Math.abs(session.receiver.volume.level - 0.5); + + function listener (isAlive) { + assert.isTrue(isAlive); + assert.isObject(session.receiver); + assert.isObject(session.receiver.volume); + // Check that the receiver volume is approximate match + if (session.receiver.volume.level > requestedVolume - 0.1 && + session.receiver.volume.level < requestedVolume + 0.1) { + called(update); + } + } + session.addUpdateListener(listener); + session.setReceiverVolumeLevel(requestedVolume, function () { + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('session.stop should stop the session', function (done) { + // Set up the expected calls + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + session.addUpdateListener(function listener (isAlive) { + if (session.status === chrome.cast.SessionStatus.STOPPED) { + assert.isFalse(isAlive); + session.removeUpdateListener(listener); + called(update); + } + }); + session.stop(function () { + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('initialize should not receive a session after session.stop', function (done) { + var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function sessionListener (session) { + assert.fail('should not receive a session (we did sessionLeave so we shouldnt be able to auto rejoin rejoin)'); + }); + chrome.cast.initialize(apiConfig, function () { + done(); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('session.stop should give an error if session already stopped', function (done) { + session.stop(function () { + assert.fail('session.stop - Should not call success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.INVALID_PARAMETER); + assert.equal(err.description, 'No active session'); + done(); + }); + }); + after(function (done) { + // Ensure the session is stopped + session.stop(function () { + done(); + }, function () { + done(); + }); + }); + }); + + describe('chrome.cast media functions', function () { + var media; + var mediaListener = function (media) { + assert.fail('session.addMediaListener should only be called when an external sender loads media'); + }; + before(function (done) { + // need to have a valid session to run these tests + session = null; + var scanState = 'running'; + var foundRoute = null; + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (scanState === 'stopped') { + assert.fail('Should not have gotten route update after scan was stopped'); + } + var route; + for (var i = 0; i < routes.length; i++) { + route = routes[i]; + assert.instanceOf(route, chrome.cast.cordova.Route); + assert.isString(route.id); + assert.isString(route.name); + assert.isBoolean(route.isNearbyDevice); + assert.isBoolean(route.isCastGroup); + if (!route.isNearbyDevice && !route.isCastGroup) { + foundRoute = route; + } + } + if (foundRoute && scanState === 'running') { + scanState = 'stopping'; + chrome.cast.cordova.stopRouteScan(function () { + scanState = 'stopped'; + chrome.cast.cordova.selectRoute(foundRoute.id, function (sess) { + utils.testSessionProperties(sess); + session = sess; + done(); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + } + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped.'); + }); + }); + beforeEach(function () { + session.addMediaListener(mediaListener); + }); + afterEach(function () { + session.removeMediaListener(mediaListener); + }); + it('session.loadMedia should be able to load a remote video', function (done) { + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + session.loadMedia(new chrome.cast.media.LoadRequest( + new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4') + ), function (m) { + media = m; + assert.instanceOf(media, chrome.cast.media.Media); + assert.equal(media.sessionId, session.sessionId); + assert.isFunction(media.addUpdateListener); + assert.isFunction(media.removeUpdateListener); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + called(update); + } + }); + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('media.setVolume should set the volume', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + media.addUpdateListener(function listener (isAlive) { + assert.notEqual(media.playerState, chrome.cast.media.PlayerState.IDLE); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + called(update); + } + }); + // Ensure we select a different volume + var vol = media.volume.level; + if (vol) { + vol = Math.abs(vol - 0.5); + } else { + vol = Math.random(); + } + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(vol)); + + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + assert.instanceOf(media.volume, chrome.cast.Volume); + if (media.volume.level === vol) { + media.removeUpdateListener(listener); + called(update); + } + }); + + media.setVolume(request, function () { + assert.instanceOf(media.volume, chrome.cast.Volume); + assert.equal(media.volume.level, vol); + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('media.setVolume should set muted', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + media.addUpdateListener(function listener (isAlive) { + assert.notEqual(media.playerState, chrome.cast.media.PlayerState.IDLE); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + called(update); + } + }); + var muted = true; + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, muted)); + + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + assert.instanceOf(media.volume, chrome.cast.Volume); + if (media.volume.muted === muted) { + media.removeUpdateListener(listener); + called(update); + } + }); + + media.setVolume(request, function () { + assert.instanceOf(media.volume, chrome.cast.Volume); + assert.equal(media.volume.muted, muted); + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('media.setVolume should set the volume and mute state', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + media.addUpdateListener(function listener (isAlive) { + assert.notEqual(media.playerState, chrome.cast.media.PlayerState.IDLE); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + called(update); + } + }); + // Ensure we select a different volume + var vol = media.volume.level; + if (vol) { + vol = Math.abs(vol - 0.5); + } else { + vol = Math.round(Math.random() * 100) / 100; + } + var muted = false; + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(vol, muted)); + + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + assert.instanceOf(media.volume, chrome.cast.Volume); + if (media.volume.level === vol && + media.volume.muted === request.volume.muted) { + media.removeUpdateListener(listener); + called(update); + } + }); + + media.setVolume(request, function () { + assert.instanceOf(media.volume, chrome.cast.Volume); + assert.equal(media.volume.level, vol); + assert.equal(media.volume.muted, muted); + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('media.pause should pause playback', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + assert.notEqual(media.playerState, chrome.cast.media.PlayerState.IDLE); + if (media.playerState === chrome.cast.media.PlayerState.PAUSED) { + media.removeUpdateListener(listener); + called(update); + } + }); + media.pause(null, function () { + assert.equal(media.playerState, chrome.cast.media.PlayerState.PAUSED); + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('media.play should resume playback', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + assert.notEqual(media.playerState, chrome.cast.media.PlayerState.IDLE); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + called(update); + } + }); + media.play(null, function () { + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('media.seek should skip to requested position', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + var request = new chrome.cast.media.SeekRequest(); + request.currentTime = media.media.duration / 2; + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + if (media.getEstimatedTime() > request.currentTime - 1 && + media.getEstimatedTime() < request.currentTime + 1) { + media.removeUpdateListener(listener); + called(update); + } + }); + media.seek(request, function () { + assert.closeTo(media.getEstimatedTime(), request.currentTime, 1); + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('media.addUpdateListener should detect end of video', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + var request = new chrome.cast.media.SeekRequest(); + request.currentTime = media.media.duration; + media.addUpdateListener(function listener (isAlive) { + if (media.playerState === chrome.cast.media.PlayerState.IDLE) { + media.removeUpdateListener(listener); + assert.equal(media.idleReason, chrome.cast.media.IdleReason.FINISHED); + assert.isFalse(isAlive); + called(update); + } + }); + media.seek(request, function () { + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('media.setVolume should return error when media is finished', function (done) { + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume()); + media.setVolume(request, function () { + assert.fail('should not hit success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); + assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); + done(); + }); + }); + it('media.pause should return error when media is finished', function (done) { + media.pause(null, function () { + assert.fail('should not hit success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); + assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); + done(); + }); + }); + it('media.play should return error when media is finished', function (done) { + media.play(null, function () { + assert.fail('should not hit success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); + assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); + done(); + }); + }); + it('media.seek should return error when media is finished', function (done) { + var request = new chrome.cast.media.SeekRequest(); + request.currentTime = media.media.duration; + media.seek(request, function () { + assert.fail('should not hit success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); + assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); + done(); + }); + }); + it('media.stop should return error when media is finished', function (done) { + media.stop(null, function () { + assert.fail('should not hit success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); + assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); + done(); + }); + }); + it('session.loadMedia should be able to load a video twice in a row', function (done) { + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], function () { + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + session.loadMedia(new chrome.cast.media.LoadRequest( + new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4') + ), function (m) { + media = m; + assert.instanceOf(media, chrome.cast.media.Media); + assert.equal(media.sessionId, session.sessionId); + assert.isFunction(media.addUpdateListener); + assert.isFunction(media.removeUpdateListener); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + called(update); + } + }); + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + session.loadMedia(new chrome.cast.media.LoadRequest( + new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4') + ), function (m) { + media = m; + assert.instanceOf(media, chrome.cast.media.Media); + assert.equal(media.sessionId, session.sessionId); + assert.isFunction(media.addUpdateListener); + assert.isFunction(media.removeUpdateListener); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + called(update); + } + }); + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('media.stop should end video playback', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + media.addUpdateListener(function listener (isAlive) { + if (media.playerState === chrome.cast.media.PlayerState.IDLE) { + media.removeUpdateListener(listener); + assert.equal(media.idleReason, chrome.cast.media.IdleReason.CANCELLED); + assert.isFalse(isAlive); + called(update); + } + }); + media.stop(null, function () { + assert.equal(media.playerState, chrome.cast.media.PlayerState.IDLE); + assert.equal(media.idleReason, chrome.cast.media.IdleReason.CANCELLED); + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + after(function (done) { + // Set up the expected calls + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + session.addUpdateListener(function listener (isAlive) { + if (session.status === chrome.cast.SessionStatus.STOPPED) { + assert.isFalse(isAlive); + session.removeUpdateListener(listener); + called(update); + } + }); + session.stop(function () { + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + }); + + describe('Tests that break the suite that must come last', function () { + // This test will prevent all future events (eg. SESSION_UPDATE) + // from being received. So run last. + it('setup should stop any existing scan', function (done) { + var setupTriggered = false; + var called = utils.callOrder([ + { id: stopped, repeats: false }, + { id: success, repeats: false } + ], done); + // Listen for cancel error + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + // Wait for the scan to be loaded before adding the iframe + if (!setupTriggered) { + // Manually trigger setup + setupTriggered = true; + window.cordova.exec(function (result) { + if (result[0] === 'SETUP') { + called(success); + } + }, function (err) { + assert.fail(err); + }, 'Chromecast', 'setup', []); + } + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped because setup triggered.'); + called(stopped); + }); + }); + }); + + }); + +}()); diff --git a/tests/www/js/utils.js b/tests/www/js/utils.js new file mode 100644 index 0000000..08ebd78 --- /dev/null +++ b/tests/www/js/utils.js @@ -0,0 +1,187 @@ +/** + * The order of these tests and this.bail(true) is very important. + * + * Rather than nesting deep with describes and before's we just ensure the + * tests occur in the correct order. + * The major advantage to this is not having to repeat test code frequently + * making the suite slow. + * eg. To truly isolate and test session.leave we would need a before which + * runs startScan, get a valid route, stopScan, and selectRoute. And these + * would all need to be tested before using them in the before. This is + * where the duplication and significant slowing would come from. + */ + +(function () { + 'use strict'; + /* eslint-env mocha */ + /* global chrome */ + var assert = window.chai.assert; + + var utils = {}; + + /** + * Allows you to check that a set of calls happen in a specific order. + * @param {array} calls - array of expected callDetails to be receive in order + * details include { id: callId, repeats: boolean } + * repeats=> if the call is allowed to be repeated + * @param {function} callback - called when all the calls in order have happened + * @returns {function(callID)} - call this with the callId that represents each + * call. + */ + utils.callOrder = function (calls, callback) { + // Set called to 0 + for (var i = 0; i < calls.length; i++) { + calls[i].called = 0; + } + var expectedPos = 0; + var expectedCall; + return function (callId) { + var callDetails; + for (var i = 0; i < calls.length; i++) { + if (calls[i].id === callId) { + callDetails = calls[i]; + } + } + // Is it a valid call? + if (!callDetails) { + assert.fail('Did not expect call: ' + callId); + } + if (expectedPos === calls.length) { + assert.fail('Already completed call'); + } + expectedCall = calls[expectedPos]; + + if (expectedCall.repeats && expectedCall.called + && calls.length >= expectedPos + 1 + && callId === calls[expectedPos + 1].id) { + // if we've matched the second call after a + // previously called repeatable call, move on + expectedPos++; + expectedCall = calls[expectedPos]; + } + + if (callId === expectedCall.id) { + // If we are on the expected call, set called = true + expectedCall.called++; + if (!expectedCall.repeats) { + // Move on + expectedPos++; + } + } else { + assert.fail('Expected call, "' + expectedCall.id + + ((expectedCall.called && expectedCall.repeats + && calls.length >= expectedPos + 1) ? + '" or "' + calls[expectedPos + 1].id : '') + + '", got, "' + callId + '"'); + } + + if (calls.length === expectedPos || calls[calls.length - 1].called === 1) { + callback(); + } + }; + }; + + /** + * Allows you to check that a flexible amount of specific calls have occurred + * before moving forward. + * @param {array} calls - array of expected call details to receive + * details include { id: callId, repeats: boolean } + * repeats=> if the call is allowed to be repeated + * @param {function} callback - called when all the calls have occurred + * @returns {function(callID)} - call this with the callId that represents each + * call. + */ + utils.waitForAllCalls = function (calls, callback) { + var called = []; + + return function (callId) { + var callDetails; + for (var i = 0; i < calls.length; i++) { + if (calls[i].id === callId) { + callDetails = calls[i]; + } + } + // Is it a valid call? + if (!callDetails) { + assert.fail('Did not expect call: ' + callId); + } + // If it has been called already + if (called.indexOf(callId) !== -1) { + if (!callDetails.repeats) { + assert.fail('Did not expect repeat of call: ' + callId); + } + } else { + // Else, it has not been called before, so add it to called + called.push(callId); + if (called.length === calls.length) { + callback(); + } + } + }; + }; + + utils.getObjectValues = function (obj) { + var dataArray = []; + for (var o in obj) { + dataArray.push(obj[o]); + } + return dataArray; + }; + + utils.testSessionProperties = function (session) { + assert.instanceOf(session, chrome.cast.Session); + assert.isString(session.appId); + assert.isString(session.displayName); + assert.isArray(session.media); + for (var i = 0; i < session.media.length; i++) { + utils.testMediaProperties(session.media[i]); + } + if (session.receiver) { + var rec = session.receiver; + assert.isArray(rec.capabilities); + assert.isString(rec.friendlyName); + assert.isString(rec.label); + assert.isString(rec.friendlyName); + if (rec.volume.level) { + assert.isNumber(rec.volume.level); + } + if (rec.volume.muted !== null && rec.volume.muted !== undefined) { + assert.isBoolean(rec.volume.muted); + } + } + assert.isString(session.sessionId); + assert.oneOf(session.status, utils.getObjectValues(chrome.cast.SessionStatus)); + assert.isFunction(session.addUpdateListener); + assert.isFunction(session.removeUpdateListener); + assert.isFunction(session.loadMedia); + }; + + utils.testMediaProperties = function (media) { + assert.instanceOf(media, chrome.cast.media.Media); + assert.isNumber(media.currentItemId); + assert.isNumber(media.currentTime); + if (media.idleReason) { + assert.oneOf(utils.getObjectValues(chrome.cast.media.IdleReason), media.idleReason); + } + utils.testMediaInfoProperties(media.media); + assert.isNumber(media.mediaSessionId); + assert.isNumber(media.playbackRate); + assert.oneOf(media.playerState, utils.getObjectValues(chrome.cast.media.PlayerState)); + assert.oneOf(media.repeatMode, utils.getObjectValues(chrome.cast.media.RepeatMode)); + assert.isString(media.sessionId); + assert.isArray(media.supportedMediaCommands); + assert.instanceOf(media.volume, chrome.cast.Volume); + }; + + utils.testMediaInfoProperties = function (mediaInfo) { + assert.isObject(mediaInfo); + assert.isString(mediaInfo.contentId); + assert.isString(mediaInfo.contentType); + assert.isNumber(mediaInfo.duration); + assert.isString(mediaInfo.streamType); + assert.isArray(mediaInfo.tracks); + }; + + window['cordova-plugin-chromecast-tests'] = window['cordova-plugin-chromecast-tests'] || {}; + window['cordova-plugin-chromecast-tests'].utils = utils; +}()); diff --git a/tests/www/lib/mocha.css b/tests/www/lib/mocha.css new file mode 100644 index 0000000..4ca8fcb --- /dev/null +++ b/tests/www/lib/mocha.css @@ -0,0 +1,325 @@ +@charset "utf-8"; + +body { + margin:0; +} + +#mocha { + font: 20px/1.5 "Helvetica Neue", Helvetica, Arial, sans-serif; + margin: 60px 50px; +} + +#mocha ul, +#mocha li { + margin: 0; + padding: 0; +} + +#mocha ul { + list-style: none; +} + +#mocha h1, +#mocha h2 { + margin: 0; +} + +#mocha h1 { + margin-top: 15px; + font-size: 1em; + font-weight: 200; +} + +#mocha h1 a { + text-decoration: none; + color: inherit; +} + +#mocha h1 a:hover { + text-decoration: underline; +} + +#mocha .suite .suite h1 { + margin-top: 0; + font-size: .8em; +} + +#mocha .hidden { + display: none; +} + +#mocha h2 { + font-size: 12px; + font-weight: normal; + cursor: pointer; +} + +#mocha .suite { + margin-left: 15px; +} + +#mocha .test { + margin-left: 15px; + overflow: hidden; +} + +#mocha .test.pending:hover h2::after { + content: '(pending)'; + font-family: arial, sans-serif; +} + +#mocha .test.pass.medium .duration { + background: #c09853; +} + +#mocha .test.pass.slow .duration { + background: #b94a48; +} + +#mocha .test.pass::before { + content: '✓'; + font-size: 12px; + display: block; + float: left; + margin-right: 5px; + color: #00d6b2; +} + +#mocha .test.pass .duration { + font-size: 9px; + margin-left: 5px; + padding: 2px 5px; + color: #fff; + -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.2); + -moz-box-shadow: inset 0 1px 1px rgba(0,0,0,.2); + box-shadow: inset 0 1px 1px rgba(0,0,0,.2); + -webkit-border-radius: 5px; + -moz-border-radius: 5px; + -ms-border-radius: 5px; + -o-border-radius: 5px; + border-radius: 5px; +} + +#mocha .test.pass.fast .duration { + display: none; +} + +#mocha .test.pending { + color: #0b97c4; +} + +#mocha .test.pending::before { + content: '◦'; + color: #0b97c4; +} + +#mocha .test.fail { + color: #c00; +} + +#mocha .test.fail pre { + color: black; +} + +#mocha .test.fail::before { + content: '✖'; + font-size: 12px; + display: block; + float: left; + margin-right: 5px; + color: #c00; +} + +#mocha .test pre.error { + color: #c00; + max-height: 300px; + overflow: auto; +} + +#mocha .test .html-error { + overflow: auto; + color: black; + display: block; + float: left; + clear: left; + font: 12px/1.5 monaco, monospace; + margin: 5px; + padding: 15px; + border: 1px solid #eee; + max-width: 85%; /*(1)*/ + max-width: -webkit-calc(100% - 42px); + max-width: -moz-calc(100% - 42px); + max-width: calc(100% - 42px); /*(2)*/ + max-height: 300px; + word-wrap: break-word; + border-bottom-color: #ddd; + -webkit-box-shadow: 0 1px 3px #eee; + -moz-box-shadow: 0 1px 3px #eee; + box-shadow: 0 1px 3px #eee; + -webkit-border-radius: 3px; + -moz-border-radius: 3px; + border-radius: 3px; +} + +#mocha .test .html-error pre.error { + border: none; + -webkit-border-radius: 0; + -moz-border-radius: 0; + border-radius: 0; + -webkit-box-shadow: 0; + -moz-box-shadow: 0; + box-shadow: 0; + padding: 0; + margin: 0; + margin-top: 18px; + max-height: none; +} + +/** + * (1): approximate for browsers not supporting calc + * (2): 42 = 2*15 + 2*10 + 2*1 (padding + margin + border) + * ^^ seriously + */ +#mocha .test pre { + display: block; + float: left; + clear: left; + font: 12px/1.5 monaco, monospace; + margin: 5px; + padding: 15px; + border: 1px solid #eee; + max-width: 85%; /*(1)*/ + max-width: -webkit-calc(100% - 42px); + max-width: -moz-calc(100% - 42px); + max-width: calc(100% - 42px); /*(2)*/ + word-wrap: break-word; + border-bottom-color: #ddd; + -webkit-box-shadow: 0 1px 3px #eee; + -moz-box-shadow: 0 1px 3px #eee; + box-shadow: 0 1px 3px #eee; + -webkit-border-radius: 3px; + -moz-border-radius: 3px; + border-radius: 3px; +} + +#mocha .test h2 { + position: relative; +} + +#mocha .test a.replay { + position: absolute; + top: 3px; + right: 0; + text-decoration: none; + vertical-align: middle; + display: block; + width: 15px; + height: 15px; + line-height: 15px; + text-align: center; + background: #eee; + font-size: 15px; + -webkit-border-radius: 15px; + -moz-border-radius: 15px; + border-radius: 15px; + -webkit-transition:opacity 200ms; + -moz-transition:opacity 200ms; + -o-transition:opacity 200ms; + transition: opacity 200ms; + opacity: 0.3; + color: #888; +} + +#mocha .test:hover a.replay { + opacity: 1; +} + +#mocha-report.pass .test.fail { + display: none; +} + +#mocha-report.fail .test.pass { + display: none; +} + +#mocha-report.pending .test.pass, +#mocha-report.pending .test.fail { + display: none; +} +#mocha-report.pending .test.pass.pending { + display: block; +} + +#mocha-error { + color: #c00; + font-size: 1.5em; + font-weight: 100; + letter-spacing: 1px; +} + +#mocha-stats { + position: fixed; + top: 15px; + right: 10px; + font-size: 12px; + margin: 0; + color: #888; + z-index: 1; +} + +#mocha-stats .progress { + float: right; + padding-top: 0; + + /** + * Set safe initial values, so mochas .progress does not inherit these + * properties from Bootstrap .progress (which causes .progress height to + * equal line height set in Bootstrap). + */ + height: auto; + -webkit-box-shadow: none; + -moz-box-shadow: none; + box-shadow: none; + background-color: initial; +} + +#mocha-stats em { + color: black; +} + +#mocha-stats a { + text-decoration: none; + color: inherit; +} + +#mocha-stats a:hover { + border-bottom: 1px solid #eee; +} + +#mocha-stats li { + display: inline-block; + margin: 0 5px; + list-style: none; + padding-top: 11px; +} + +#mocha-stats canvas { + width: 40px; + height: 40px; +} + +#mocha code .comment { color: #ddd; } +#mocha code .init { color: #2f6fad; } +#mocha code .string { color: #5890ad; } +#mocha code .keyword { color: #8a6343; } +#mocha code .number { color: #2f6fad; } + +@media screen and (max-device-width: 480px) { + #mocha { + margin: 60px 0px; + } + + #mocha #stats { + position: absolute; + } +} diff --git a/tests/www/lib/mocha.js b/tests/www/lib/mocha.js new file mode 100644 index 0000000..8c8d304 --- /dev/null +++ b/tests/www/lib/mocha.js @@ -0,0 +1,18229 @@ +(function () { function r (e, n, t) { function o (i, f) { if (!n[i]) { if (!e[i]) { var c = typeof require === 'function' && require; if (!f && c) return c(i, !0); if (u) return u(i, !0); var a = new Error("Cannot find module '" + i + "'"); throw a.code = 'MODULE_NOT_FOUND', a; } var p = n[i] = {exports: {}}; e[i][0].call(p.exports, function (r) { var n = e[i][1][r]; return o(n || r); }, p, p.exports, r, e, n, t); } return n[i].exports; } for (var u = typeof require === 'function' && require, i = 0; i < t.length; i++)o(t[i]); return o; } return r; })()({1: [function (require, module, exports) { + (function (process, global) { + 'use strict'; + +/* eslint no-unused-vars: off */ +/* eslint-env commonjs */ + +/** + * Shim process.stdout. + */ + + process.stdout = require('browser-stdout')({label: false}); + + var Mocha = require('./lib/mocha'); + +/** + * Create a Mocha instance. + * + * @return {undefined} + */ + + var mocha = new Mocha({reporter: 'html'}); + +/** + * Save timer references to avoid Sinon interfering (see GH-237). + */ + + var Date = global.Date; + var setTimeout = global.setTimeout; + var setInterval = global.setInterval; + var clearTimeout = global.clearTimeout; + var clearInterval = global.clearInterval; + + var uncaughtExceptionHandlers = []; + + var originalOnerrorHandler = global.onerror; + +/** + * Remove uncaughtException listener. + * Revert to original onerror handler if previously defined. + */ + + process.removeListener = function (e, fn) { + if (e === 'uncaughtException') { + if (originalOnerrorHandler) { + global.onerror = originalOnerrorHandler; + } else { + global.onerror = function () {}; + } + var i = uncaughtExceptionHandlers.indexOf(fn); + if (i !== -1) { + uncaughtExceptionHandlers.splice(i, 1); + } + } + }; + +/** + * Implements uncaughtException listener. + */ + + process.on = function (e, fn) { + if (e === 'uncaughtException') { + global.onerror = function (msg, url, line, col, err) { + fn(err || new Error(msg + ' (' + url + ':' + line + ')')); + return !mocha.allowUncaught; + }; + uncaughtExceptionHandlers.push(fn); + } + }; + +// The BDD UI is registered by default, but no UI will be functional in the +// browser without an explicit call to the overridden `mocha.ui` (see below). +// Ensure that this default UI does not expose its methods to the global scope. + mocha.suite.removeAllListeners('pre-require'); + + var immediateQueue = []; + var immediateTimeout; + + function timeslice () { + var immediateStart = new Date().getTime(); + while (immediateQueue.length && new Date().getTime() - immediateStart < 100) { + immediateQueue.shift()(); + } + if (immediateQueue.length) { + immediateTimeout = setTimeout(timeslice, 0); + } else { + immediateTimeout = null; + } + } + +/** + * High-performance override of Runner.immediately. + */ + + Mocha.Runner.immediately = function (callback) { + immediateQueue.push(callback); + if (!immediateTimeout) { + immediateTimeout = setTimeout(timeslice, 0); + } + }; + +/** + * Function to allow assertion libraries to throw errors directly into mocha. + * This is useful when running tests in a browser because window.onerror will + * only receive the 'message' attribute of the Error. + */ + mocha.throwError = function (err) { + uncaughtExceptionHandlers.forEach(function (fn) { + fn(err); + }); + throw err; + }; + +/** + * Override ui to ensure that the ui functions are initialized. + * Normally this would happen in Mocha.prototype.loadFiles. + */ + + mocha.ui = function (ui) { + Mocha.prototype.ui.call(this, ui); + this.suite.emit('pre-require', global, null, this); + return this; + }; + +/** + * Setup mocha with the given setting options. + */ + + mocha.setup = function (opts) { + if (typeof opts === 'string') { + opts = {ui: opts}; + } + for (var opt in opts) { + if (opts.hasOwnProperty(opt)) { + this[opt](opts[opt]); + } + } + return this; + }; + +/** + * Run mocha, returning the Runner. + */ + + mocha.run = function (fn) { + var options = mocha.options; + mocha.globals('location'); + + var query = Mocha.utils.parseQuery(global.location.search || ''); + if (query.grep) { + mocha.grep(query.grep); + } + if (query.fgrep) { + mocha.fgrep(query.fgrep); + } + if (query.invert) { + mocha.invert(); + } + + return Mocha.prototype.run.call(mocha, function (err) { + // The DOM Document is not available in Web Workers. + var document = global.document; + if ( + document && + document.getElementById('mocha') && + options.noHighlighting !== true + ) { + Mocha.utils.highlightTags('code'); + } + if (fn) { + fn(err); + } + }); + }; + +/** + * Expose the process shim. + * https://github.com/mochajs/mocha/pull/916 + */ + + Mocha.process = process; + +/** + * Expose mocha. + */ + + global.Mocha = Mocha; + global.mocha = mocha; + +// this allows test/acceptance/required-tokens.js to pass; thus, +// you can now do `const describe = require('mocha').describe` in a +// browser context (assuming browserification). should fix #880 + module.exports = global; + + }).call(this, require('_process'), typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}); +}, {'./lib/mocha': 14, '_process': 70, 'browser-stdout': 41}], + 2: [function (require, module, exports) { + (function (process, global) { + 'use strict'; + +/** + * Web Notifications module. + * @module Growl + */ + +/** + * Save timer references to avoid Sinon interfering (see GH-237). + */ + var Date = global.Date; + var setTimeout = global.setTimeout; + var EVENT_RUN_END = require('../runner').constants.EVENT_RUN_END; + +/** + * Checks if browser notification support exists. + * + * @public + * @see {@link https://caniuse.com/#feat=notifications|Browser support (notifications)} + * @see {@link https://caniuse.com/#feat=promises|Browser support (promises)} + * @see {@link Mocha#growl} + * @see {@link Mocha#isGrowlCapable} + * @return {boolean} whether browser notification support exists + */ + exports.isCapable = function () { + var hasNotificationSupport = 'Notification' in window; + var hasPromiseSupport = typeof Promise === 'function'; + return process.browser && hasNotificationSupport && hasPromiseSupport; + }; + +/** + * Implements browser notifications as a pseudo-reporter. + * + * @public + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/notification|Notification API} + * @see {@link https://developers.google.com/web/fundamentals/push-notifications/display-a-notification|Displaying a Notification} + * @see {@link Growl#isPermitted} + * @see {@link Mocha#_growl} + * @param {Runner} runner - Runner instance. + */ + exports.notify = function (runner) { + var promise = isPermitted(); + + /** + * Attempt notification. + */ + var sendNotification = function () { + // If user hasn't responded yet... "No notification for you!" (Seinfeld) + Promise.race([promise, Promise.resolve(undefined)]) + .then(canNotify) + .then(function () { + display(runner); + }) + .catch(notPermitted); + }; + + runner.once(EVENT_RUN_END, sendNotification); + }; + +/** + * Checks if browser notification is permitted by user. + * + * @private + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Notification/permission|Notification.permission} + * @see {@link Mocha#growl} + * @see {@link Mocha#isGrowlPermitted} + * @returns {Promise} promise determining if browser notification + * permissible when fulfilled. + */ + function isPermitted () { + var permitted = { + granted: function allow () { + return Promise.resolve(true); + }, + denied: function deny () { + return Promise.resolve(false); + }, + default: function ask () { + return Notification.requestPermission().then(function (permission) { + return permission === 'granted'; + }); + } + }; + + return permitted[Notification.permission](); + } + +/** + * @summary + * Determines if notification should proceed. + * + * @description + * Notification shall not proceed unless `value` is true. + * + * `value` will equal one of: + *
      + *
    • true (from `isPermitted`)
    • + *
    • false (from `isPermitted`)
    • + *
    • undefined (from `Promise.race`)
    • + *
    + * + * @private + * @param {boolean|undefined} value - Determines if notification permissible. + * @returns {Promise} Notification can proceed + */ + function canNotify (value) { + if (!value) { + var why = value === false ? 'blocked' : 'unacknowledged'; + var reason = 'not permitted by user (' + why + ')'; + return Promise.reject(new Error(reason)); + } + return Promise.resolve(); + } + +/** + * Displays the notification. + * + * @private + * @param {Runner} runner - Runner instance. + */ + function display (runner) { + var stats = runner.stats; + var symbol = { + cross: '\u274C', + tick: '\u2705' + }; + var logo = require('../../package').notifyLogo; + var _message; + var message; + var title; + + if (stats.failures) { + _message = stats.failures + ' of ' + stats.tests + ' tests failed'; + message = symbol.cross + ' ' + _message; + title = 'Failed'; + } else { + _message = stats.passes + ' tests passed in ' + stats.duration + 'ms'; + message = symbol.tick + ' ' + _message; + title = 'Passed'; + } + + // Send notification + var options = { + badge: logo, + body: message, + dir: 'ltr', + icon: logo, + lang: 'en-US', + name: 'mocha', + requireInteraction: false, + timestamp: Date.now() + }; + var notification = new Notification(title, options); + + // Autoclose after brief delay (makes various browsers act same) + var FORCE_DURATION = 4000; + setTimeout(notification.close.bind(notification), FORCE_DURATION); + } + +/** + * As notifications are tangential to our purpose, just log the error. + * + * @private + * @param {Error} err - Why notification didn't happen. + */ + function notPermitted (err) { + console.error('notification error:', err.message); + } + + }).call(this, require('_process'), typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}); + }, {'../../package': 91, '../runner': 34, '_process': 70}], + 3: [function (require, module, exports) { + 'use strict'; + +/** + * Expose `Progress`. + */ + + module.exports = Progress; + +/** + * Initialize a new `Progress` indicator. + */ + function Progress () { + this.percent = 0; + this.size(0); + this.fontSize(11); + this.font('helvetica, arial, sans-serif'); + } + +/** + * Set progress size to `size`. + * + * @public + * @param {number} size + * @return {Progress} Progress instance. + */ + Progress.prototype.size = function (size) { + this._size = size; + return this; + }; + +/** + * Set text to `text`. + * + * @public + * @param {string} text + * @return {Progress} Progress instance. + */ + Progress.prototype.text = function (text) { + this._text = text; + return this; + }; + +/** + * Set font size to `size`. + * + * @public + * @param {number} size + * @return {Progress} Progress instance. + */ + Progress.prototype.fontSize = function (size) { + this._fontSize = size; + return this; + }; + +/** + * Set font to `family`. + * + * @param {string} family + * @return {Progress} Progress instance. + */ + Progress.prototype.font = function (family) { + this._font = family; + return this; + }; + +/** + * Update percentage to `n`. + * + * @param {number} n + * @return {Progress} Progress instance. + */ + Progress.prototype.update = function (n) { + this.percent = n; + return this; + }; + +/** + * Draw on `ctx`. + * + * @param {CanvasRenderingContext2d} ctx + * @return {Progress} Progress instance. + */ + Progress.prototype.draw = function (ctx) { + try { + var percent = Math.min(this.percent, 100); + var size = this._size; + var half = size / 2; + var x = half; + var y = half; + var rad = half - 1; + var fontSize = this._fontSize; + + ctx.font = fontSize + 'px ' + this._font; + + var angle = Math.PI * 2 * (percent / 100); + ctx.clearRect(0, 0, size, size); + + // outer circle + ctx.strokeStyle = '#9f9f9f'; + ctx.beginPath(); + ctx.arc(x, y, rad, 0, angle, false); + ctx.stroke(); + + // inner circle + ctx.strokeStyle = '#eee'; + ctx.beginPath(); + ctx.arc(x, y, rad - 1, 0, angle, true); + ctx.stroke(); + + // text + var text = this._text || (percent | 0) + '%'; + var w = ctx.measureText(text).width; + + ctx.fillText(text, x - w / 2 + 1, y + fontSize / 2 - 1); + } catch (ignore) { + // don't fail if we can't render progress + } + return this; + }; + + }, {}], + 4: [function (require, module, exports) { + (function (global) { + 'use strict'; + + exports.isatty = function isatty () { + return true; + }; + + exports.getWindowSize = function getWindowSize () { + if ('innerHeight' in global) { + return [global.innerHeight, global.innerWidth]; + } + // In a Web Worker, the DOM Window is not available. + return [640, 480]; + }; + + }).call(this, typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}); + }, {}], + 5: [function (require, module, exports) { + 'use strict'; +/** + * @module Context + */ +/** + * Expose `Context`. + */ + + module.exports = Context; + +/** + * Initialize a new `Context`. + * + * @private + */ + function Context () {} + +/** + * Set or get the context `Runnable` to `runnable`. + * + * @private + * @param {Runnable} runnable + * @return {Context} context + */ + Context.prototype.runnable = function (runnable) { + if (!arguments.length) { + return this._runnable; + } + this.test = this._runnable = runnable; + return this; + }; + +/** + * Set or get test timeout `ms`. + * + * @private + * @param {number} ms + * @return {Context} self + */ + Context.prototype.timeout = function (ms) { + if (!arguments.length) { + return this.runnable().timeout(); + } + this.runnable().timeout(ms); + return this; + }; + +/** + * Set test timeout `enabled`. + * + * @private + * @param {boolean} enabled + * @return {Context} self + */ + Context.prototype.enableTimeouts = function (enabled) { + if (!arguments.length) { + return this.runnable().enableTimeouts(); + } + this.runnable().enableTimeouts(enabled); + return this; + }; + +/** + * Set or get test slowness threshold `ms`. + * + * @private + * @param {number} ms + * @return {Context} self + */ + Context.prototype.slow = function (ms) { + if (!arguments.length) { + return this.runnable().slow(); + } + this.runnable().slow(ms); + return this; + }; + +/** + * Mark a test as skipped. + * + * @private + * @throws Pending + */ + Context.prototype.skip = function () { + this.runnable().skip(); + }; + +/** + * Set or get a number of allowed retries on failed tests + * + * @private + * @param {number} n + * @return {Context} self + */ + Context.prototype.retries = function (n) { + if (!arguments.length) { + return this.runnable().retries(); + } + this.runnable().retries(n); + return this; + }; + + }, {}], + 6: [function (require, module, exports) { + 'use strict'; +/** + * @module Errors + */ +/** + * Factory functions to create throwable error objects + */ + +/** + * Creates an error object to be thrown when no files to be tested could be found using specified pattern. + * + * @public + * @param {string} message - Error message to be displayed. + * @param {string} pattern - User-specified argument value. + * @returns {Error} instance detailing the error condition + */ + function createNoFilesMatchPatternError (message, pattern) { + var err = new Error(message); + err.code = 'ERR_MOCHA_NO_FILES_MATCH_PATTERN'; + err.pattern = pattern; + return err; + } + +/** + * Creates an error object to be thrown when the reporter specified in the options was not found. + * + * @public + * @param {string} message - Error message to be displayed. + * @param {string} reporter - User-specified reporter value. + * @returns {Error} instance detailing the error condition + */ + function createInvalidReporterError (message, reporter) { + var err = new TypeError(message); + err.code = 'ERR_MOCHA_INVALID_REPORTER'; + err.reporter = reporter; + return err; + } + +/** + * Creates an error object to be thrown when the interface specified in the options was not found. + * + * @public + * @param {string} message - Error message to be displayed. + * @param {string} ui - User-specified interface value. + * @returns {Error} instance detailing the error condition + */ + function createInvalidInterfaceError (message, ui) { + var err = new Error(message); + err.code = 'ERR_MOCHA_INVALID_INTERFACE'; + err.interface = ui; + return err; + } + +/** + * Creates an error object to be thrown when a behavior, option, or parameter is unsupported. + * + * @public + * @param {string} message - Error message to be displayed. + * @returns {Error} instance detailing the error condition + */ + function createUnsupportedError (message) { + var err = new Error(message); + err.code = 'ERR_MOCHA_UNSUPPORTED'; + return err; + } + +/** + * Creates an error object to be thrown when an argument is missing. + * + * @public + * @param {string} message - Error message to be displayed. + * @param {string} argument - Argument name. + * @param {string} expected - Expected argument datatype. + * @returns {Error} instance detailing the error condition + */ + function createMissingArgumentError (message, argument, expected) { + return createInvalidArgumentTypeError(message, argument, expected); + } + +/** + * Creates an error object to be thrown when an argument did not use the supported type + * + * @public + * @param {string} message - Error message to be displayed. + * @param {string} argument - Argument name. + * @param {string} expected - Expected argument datatype. + * @returns {Error} instance detailing the error condition + */ + function createInvalidArgumentTypeError (message, argument, expected) { + var err = new TypeError(message); + err.code = 'ERR_MOCHA_INVALID_ARG_TYPE'; + err.argument = argument; + err.expected = expected; + err.actual = typeof argument; + return err; + } + +/** + * Creates an error object to be thrown when an argument did not use the supported value + * + * @public + * @param {string} message - Error message to be displayed. + * @param {string} argument - Argument name. + * @param {string} value - Argument value. + * @param {string} [reason] - Why value is invalid. + * @returns {Error} instance detailing the error condition + */ + function createInvalidArgumentValueError (message, argument, value, reason) { + var err = new TypeError(message); + err.code = 'ERR_MOCHA_INVALID_ARG_VALUE'; + err.argument = argument; + err.value = value; + err.reason = typeof reason !== 'undefined' ? reason : 'is invalid'; + return err; + } + +/** + * Creates an error object to be thrown when an exception was caught, but the `Error` is falsy or undefined. + * + * @public + * @param {string} message - Error message to be displayed. + * @returns {Error} instance detailing the error condition + */ + function createInvalidExceptionError (message, value) { + var err = new Error(message); + err.code = 'ERR_MOCHA_INVALID_EXCEPTION'; + err.valueType = typeof value; + err.value = value; + return err; + } + + module.exports = { + createInvalidArgumentTypeError: createInvalidArgumentTypeError, + createInvalidArgumentValueError: createInvalidArgumentValueError, + createInvalidExceptionError: createInvalidExceptionError, + createInvalidInterfaceError: createInvalidInterfaceError, + createInvalidReporterError: createInvalidReporterError, + createMissingArgumentError: createMissingArgumentError, + createNoFilesMatchPatternError: createNoFilesMatchPatternError, + createUnsupportedError: createUnsupportedError + }; + + }, {}], + 7: [function (require, module, exports) { + 'use strict'; + + var Runnable = require('./runnable'); + var inherits = require('./utils').inherits; + +/** + * Expose `Hook`. + */ + + module.exports = Hook; + +/** + * Initialize a new `Hook` with the given `title` and callback `fn` + * + * @class + * @extends Runnable + * @param {String} title + * @param {Function} fn + */ + function Hook (title, fn) { + Runnable.call(this, title, fn); + this.type = 'hook'; + } + +/** + * Inherit from `Runnable.prototype`. + */ + inherits(Hook, Runnable); + +/** + * Get or set the test `err`. + * + * @memberof Hook + * @public + * @param {Error} err + * @return {Error} + */ + Hook.prototype.error = function (err) { + if (!arguments.length) { + err = this._error; + this._error = null; + return err; + } + + this._error = err; + }; + + }, {'./runnable': 33, './utils': 38}], + 8: [function (require, module, exports) { + 'use strict'; + + var Test = require('../test'); + var EVENT_FILE_PRE_REQUIRE = require('../suite').constants + .EVENT_FILE_PRE_REQUIRE; + +/** + * BDD-style interface: + * + * describe('Array', function() { + * describe('#indexOf()', function() { + * it('should return -1 when not present', function() { + * // ... + * }); + * + * it('should return the index when present', function() { + * // ... + * }); + * }); + * }); + * + * @param {Suite} suite Root suite. + */ + module.exports = function bddInterface (suite) { + var suites = [suite]; + + suite.on(EVENT_FILE_PRE_REQUIRE, function (context, file, mocha) { + var common = require('./common')(suites, context, mocha); + + context.before = common.before; + context.after = common.after; + context.beforeEach = common.beforeEach; + context.afterEach = common.afterEach; + context.run = mocha.options.delay && common.runWithSuite(suite); + /** + * Describe a "suite" with the given `title` + * and callback `fn` containing nested suites + * and/or tests. + */ + + context.describe = context.context = function (title, fn) { + return common.suite.create({ + title: title, + file: file, + fn: fn + }); + }; + + /** + * Pending describe. + */ + + context.xdescribe = context.xcontext = context.describe.skip = function ( + title, + fn + ) { + return common.suite.skip({ + title: title, + file: file, + fn: fn + }); + }; + + /** + * Exclusive suite. + */ + + context.describe.only = function (title, fn) { + return common.suite.only({ + title: title, + file: file, + fn: fn + }); + }; + + /** + * Describe a specification or test-case + * with the given `title` and callback `fn` + * acting as a thunk. + */ + + context.it = context.specify = function (title, fn) { + var suite = suites[0]; + if (suite.isPending()) { + fn = null; + } + var test = new Test(title, fn); + test.file = file; + suite.addTest(test); + return test; + }; + + /** + * Exclusive test-case. + */ + + context.it.only = function (title, fn) { + return common.test.only(mocha, context.it(title, fn)); + }; + + /** + * Pending test case. + */ + + context.xit = context.xspecify = context.it.skip = function (title) { + return context.it(title); + }; + + /** + * Number of attempts to retry. + */ + context.it.retries = function (n) { + context.retries(n); + }; + }); + }; + + module.exports.description = 'BDD or RSpec style [default]'; + + }, {'../suite': 36, '../test': 37, './common': 9}], + 9: [function (require, module, exports) { + 'use strict'; + + var Suite = require('../suite'); + var errors = require('../errors'); + var createMissingArgumentError = errors.createMissingArgumentError; + +/** + * Functions common to more than one interface. + * + * @param {Suite[]} suites + * @param {Context} context + * @param {Mocha} mocha + * @return {Object} An object containing common functions. + */ + module.exports = function (suites, context, mocha) { + /** + * Check if the suite should be tested. + * + * @private + * @param {Suite} suite - suite to check + * @returns {boolean} + */ + function shouldBeTested (suite) { + return ( + !mocha.options.grep || + (mocha.options.grep && + mocha.options.grep.test(suite.fullTitle()) && + !mocha.options.invert) + ); + } + + return { + /** + * This is only present if flag --delay is passed into Mocha. It triggers + * root suite execution. + * + * @param {Suite} suite The root suite. + * @return {Function} A function which runs the root suite + */ + runWithSuite: function runWithSuite (suite) { + return function run () { + suite.run(); + }; + }, + + /** + * Execute before running tests. + * + * @param {string} name + * @param {Function} fn + */ + before: function (name, fn) { + suites[0].beforeAll(name, fn); + }, + + /** + * Execute after running tests. + * + * @param {string} name + * @param {Function} fn + */ + after: function (name, fn) { + suites[0].afterAll(name, fn); + }, + + /** + * Execute before each test case. + * + * @param {string} name + * @param {Function} fn + */ + beforeEach: function (name, fn) { + suites[0].beforeEach(name, fn); + }, + + /** + * Execute after each test case. + * + * @param {string} name + * @param {Function} fn + */ + afterEach: function (name, fn) { + suites[0].afterEach(name, fn); + }, + + suite: { + /** + * Create an exclusive Suite; convenience function + * See docstring for create() below. + * + * @param {Object} opts + * @returns {Suite} + */ + only: function only (opts) { + opts.isOnly = true; + return this.create(opts); + }, + + /** + * Create a Suite, but skip it; convenience function + * See docstring for create() below. + * + * @param {Object} opts + * @returns {Suite} + */ + skip: function skip (opts) { + opts.pending = true; + return this.create(opts); + }, + + /** + * Creates a suite. + * + * @param {Object} opts Options + * @param {string} opts.title Title of Suite + * @param {Function} [opts.fn] Suite Function (not always applicable) + * @param {boolean} [opts.pending] Is Suite pending? + * @param {string} [opts.file] Filepath where this Suite resides + * @param {boolean} [opts.isOnly] Is Suite exclusive? + * @returns {Suite} + */ + create: function create (opts) { + var suite = Suite.create(suites[0], opts.title); + suite.pending = Boolean(opts.pending); + suite.file = opts.file; + suites.unshift(suite); + if (opts.isOnly) { + if (mocha.options.forbidOnly && shouldBeTested(suite)) { + throw new Error('`.only` forbidden'); + } + + suite.parent.appendOnlySuite(suite); + } + if (suite.pending) { + if (mocha.options.forbidPending && shouldBeTested(suite)) { + throw new Error('Pending test forbidden'); + } + } + if (typeof opts.fn === 'function') { + opts.fn.call(suite); + suites.shift(); + } else if (typeof opts.fn === 'undefined' && !suite.pending) { + throw createMissingArgumentError( + 'Suite "' + + suite.fullTitle() + + '" was defined but no callback was supplied. ' + + 'Supply a callback or explicitly skip the suite.', + 'callback', + 'function' + ); + } else if (!opts.fn && suite.pending) { + suites.shift(); + } + + return suite; + } + }, + + test: { + /** + * Exclusive test-case. + * + * @param {Object} mocha + * @param {Function} test + * @returns {*} + */ + only: function (mocha, test) { + test.parent.appendOnlyTest(test); + return test; + }, + + /** + * Pending test case. + * + * @param {string} title + */ + skip: function (title) { + context.test(title); + }, + + /** + * Number of retry attempts + * + * @param {number} n + */ + retries: function (n) { + context.retries(n); + } + } + }; + }; + + }, {'../errors': 6, '../suite': 36}], + 10: [function (require, module, exports) { + 'use strict'; + var Suite = require('../suite'); + var Test = require('../test'); + +/** + * Exports-style (as Node.js module) interface: + * + * exports.Array = { + * '#indexOf()': { + * 'should return -1 when the value is not present': function() { + * + * }, + * + * 'should return the correct index when the value is present': function() { + * + * } + * } + * }; + * + * @param {Suite} suite Root suite. + */ + module.exports = function (suite) { + var suites = [suite]; + + suite.on(Suite.constants.EVENT_FILE_REQUIRE, visit); + + function visit (obj, file) { + var suite; + for (var key in obj) { + if (typeof obj[key] === 'function') { + var fn = obj[key]; + switch (key) { + case 'before': + suites[0].beforeAll(fn); + break; + case 'after': + suites[0].afterAll(fn); + break; + case 'beforeEach': + suites[0].beforeEach(fn); + break; + case 'afterEach': + suites[0].afterEach(fn); + break; + default: + var test = new Test(key, fn); + test.file = file; + suites[0].addTest(test); + } + } else { + suite = Suite.create(suites[0], key); + suites.unshift(suite); + visit(obj[key], file); + suites.shift(); + } + } + } + }; + + module.exports.description = 'Node.js module ("exports") style'; + + }, {'../suite': 36, '../test': 37}], + 11: [function (require, module, exports) { + 'use strict'; + + exports.bdd = require('./bdd'); + exports.tdd = require('./tdd'); + exports.qunit = require('./qunit'); + exports.exports = require('./exports'); + + }, {'./bdd': 8, './exports': 10, './qunit': 12, './tdd': 13}], + 12: [function (require, module, exports) { + 'use strict'; + + var Test = require('../test'); + var EVENT_FILE_PRE_REQUIRE = require('../suite').constants + .EVENT_FILE_PRE_REQUIRE; + +/** + * QUnit-style interface: + * + * suite('Array'); + * + * test('#length', function() { + * var arr = [1,2,3]; + * ok(arr.length == 3); + * }); + * + * test('#indexOf()', function() { + * var arr = [1,2,3]; + * ok(arr.indexOf(1) == 0); + * ok(arr.indexOf(2) == 1); + * ok(arr.indexOf(3) == 2); + * }); + * + * suite('String'); + * + * test('#length', function() { + * ok('foo'.length == 3); + * }); + * + * @param {Suite} suite Root suite. + */ + module.exports = function qUnitInterface (suite) { + var suites = [suite]; + + suite.on(EVENT_FILE_PRE_REQUIRE, function (context, file, mocha) { + var common = require('./common')(suites, context, mocha); + + context.before = common.before; + context.after = common.after; + context.beforeEach = common.beforeEach; + context.afterEach = common.afterEach; + context.run = mocha.options.delay && common.runWithSuite(suite); + /** + * Describe a "suite" with the given `title`. + */ + + context.suite = function (title) { + if (suites.length > 1) { + suites.shift(); + } + return common.suite.create({ + title: title, + file: file, + fn: false + }); + }; + + /** + * Exclusive Suite. + */ + + context.suite.only = function (title) { + if (suites.length > 1) { + suites.shift(); + } + return common.suite.only({ + title: title, + file: file, + fn: false + }); + }; + + /** + * Describe a specification or test-case + * with the given `title` and callback `fn` + * acting as a thunk. + */ + + context.test = function (title, fn) { + var test = new Test(title, fn); + test.file = file; + suites[0].addTest(test); + return test; + }; + + /** + * Exclusive test-case. + */ + + context.test.only = function (title, fn) { + return common.test.only(mocha, context.test(title, fn)); + }; + + context.test.skip = common.test.skip; + context.test.retries = common.test.retries; + }); + }; + + module.exports.description = 'QUnit style'; + + }, {'../suite': 36, '../test': 37, './common': 9}], + 13: [function (require, module, exports) { + 'use strict'; + + var Test = require('../test'); + var EVENT_FILE_PRE_REQUIRE = require('../suite').constants + .EVENT_FILE_PRE_REQUIRE; + +/** + * TDD-style interface: + * + * suite('Array', function() { + * suite('#indexOf()', function() { + * suiteSetup(function() { + * + * }); + * + * test('should return -1 when not present', function() { + * + * }); + * + * test('should return the index when present', function() { + * + * }); + * + * suiteTeardown(function() { + * + * }); + * }); + * }); + * + * @param {Suite} suite Root suite. + */ + module.exports = function (suite) { + var suites = [suite]; + + suite.on(EVENT_FILE_PRE_REQUIRE, function (context, file, mocha) { + var common = require('./common')(suites, context, mocha); + + context.setup = common.beforeEach; + context.teardown = common.afterEach; + context.suiteSetup = common.before; + context.suiteTeardown = common.after; + context.run = mocha.options.delay && common.runWithSuite(suite); + + /** + * Describe a "suite" with the given `title` and callback `fn` containing + * nested suites and/or tests. + */ + context.suite = function (title, fn) { + return common.suite.create({ + title: title, + file: file, + fn: fn + }); + }; + + /** + * Pending suite. + */ + context.suite.skip = function (title, fn) { + return common.suite.skip({ + title: title, + file: file, + fn: fn + }); + }; + + /** + * Exclusive test-case. + */ + context.suite.only = function (title, fn) { + return common.suite.only({ + title: title, + file: file, + fn: fn + }); + }; + + /** + * Describe a specification or test-case with the given `title` and + * callback `fn` acting as a thunk. + */ + context.test = function (title, fn) { + var suite = suites[0]; + if (suite.isPending()) { + fn = null; + } + var test = new Test(title, fn); + test.file = file; + suite.addTest(test); + return test; + }; + + /** + * Exclusive test-case. + */ + + context.test.only = function (title, fn) { + return common.test.only(mocha, context.test(title, fn)); + }; + + context.test.skip = common.test.skip; + context.test.retries = common.test.retries; + }); + }; + + module.exports.description = + 'traditional "suite"/"test" instead of BDD\'s "describe"/"it"'; + + }, {'../suite': 36, '../test': 37, './common': 9}], + 14: [function (require, module, exports) { + (function (process, global) { + 'use strict'; + +/*! + * mocha + * Copyright(c) 2011 TJ Holowaychuk + * MIT Licensed + */ + + var escapeRe = require('escape-string-regexp'); + var path = require('path'); + var builtinReporters = require('./reporters'); + var growl = require('./growl'); + var utils = require('./utils'); + var mocharc = require('./mocharc.json'); + var errors = require('./errors'); + var Suite = require('./suite'); + var createStatsCollector = require('./stats-collector'); + var createInvalidReporterError = errors.createInvalidReporterError; + var createInvalidInterfaceError = errors.createInvalidInterfaceError; + var EVENT_FILE_PRE_REQUIRE = Suite.constants.EVENT_FILE_PRE_REQUIRE; + var EVENT_FILE_POST_REQUIRE = Suite.constants.EVENT_FILE_POST_REQUIRE; + var EVENT_FILE_REQUIRE = Suite.constants.EVENT_FILE_REQUIRE; + var sQuote = utils.sQuote; + + exports = module.exports = Mocha; + +/** + * To require local UIs and reporters when running in node. + */ + + if (!process.browser) { + var cwd = process.cwd(); + module.paths.push(cwd, path.join(cwd, 'node_modules')); + } + +/** + * Expose internals. + */ + +/** + * @public + * @class utils + * @memberof Mocha + */ + exports.utils = utils; + exports.interfaces = require('./interfaces'); +/** + * @public + * @memberof Mocha + */ + exports.reporters = builtinReporters; + exports.Runnable = require('./runnable'); + exports.Context = require('./context'); +/** + * + * @memberof Mocha + */ + exports.Runner = require('./runner'); + exports.Suite = Suite; + exports.Hook = require('./hook'); + exports.Test = require('./test'); + +/** + * Constructs a new Mocha instance with `options`. + * + * @public + * @class Mocha + * @param {Object} [options] - Settings object. + * @param {boolean} [options.allowUncaught] - Propagate uncaught errors? + * @param {boolean} [options.asyncOnly] - Force `done` callback or promise? + * @param {boolean} [options.bail] - Bail after first test failure? + * @param {boolean} [options.checkLeaks] - Check for global variable leaks? + * @param {boolean} [options.color] - Color TTY output from reporter? + * @param {boolean} [options.delay] - Delay root suite execution? + * @param {boolean} [options.diff] - Show diff on failure? + * @param {string} [options.fgrep] - Test filter given string. + * @param {boolean} [options.forbidOnly] - Tests marked `only` fail the suite? + * @param {boolean} [options.forbidPending] - Pending tests fail the suite? + * @param {boolean} [options.fullTrace] - Full stacktrace upon failure? + * @param {string[]} [options.global] - Variables expected in global scope. + * @param {RegExp|string} [options.grep] - Test filter given regular expression. + * @param {boolean} [options.growl] - Enable desktop notifications? + * @param {boolean} [options.inlineDiffs] - Display inline diffs? + * @param {boolean} [options.invert] - Invert test filter matches? + * @param {boolean} [options.noHighlighting] - Disable syntax highlighting? + * @param {string|constructor} [options.reporter] - Reporter name or constructor. + * @param {Object} [options.reporterOption] - Reporter settings object. + * @param {number} [options.retries] - Number of times to retry failed tests. + * @param {number} [options.slow] - Slow threshold value. + * @param {number|string} [options.timeout] - Timeout threshold value. + * @param {string} [options.ui] - Interface name. + */ + function Mocha (options) { + options = utils.assign({}, mocharc, options || {}); + this.files = []; + this.options = options; + // root suite + this.suite = new exports.Suite('', new exports.Context(), true); + + this.grep(options.grep) + .fgrep(options.fgrep) + .ui(options.ui) + .bail(options.bail) + .reporter(options.reporter, options.reporterOption) + .slow(options.slow) + .useInlineDiffs(options.inlineDiffs) + .globals(options.global); + + // this guard exists because Suite#timeout does not consider `undefined` to be valid input + if (typeof options.timeout !== 'undefined') { + this.timeout(options.timeout === false ? 0 : options.timeout); + } + + if ('retries' in options) { + this.retries(options.retries); + } + + [ + 'allowUncaught', + 'asyncOnly', + 'checkLeaks', + 'delay', + 'forbidOnly', + 'forbidPending', + 'fullTrace', + 'growl', + 'invert' + ].forEach(function (opt) { + if (options[opt]) { + this[opt](); + } + }, this); + } + +/** + * Enables or disables bailing on the first failure. + * + * @public + * @see {@link /#-bail-b|CLI option} + * @param {boolean} [bail=true] - Whether to bail on first error. + * @returns {Mocha} this + * @chainable + */ + Mocha.prototype.bail = function (bail) { + if (!arguments.length) { + bail = true; + } + this.suite.bail(bail); + return this; + }; + +/** + * @summary + * Adds `file` to be loaded for execution. + * + * @description + * Useful for generic setup code that must be included within test suite. + * + * @public + * @see {@link /#-file-filedirectoryglob|CLI option} + * @param {string} file - Pathname of file to be loaded. + * @returns {Mocha} this + * @chainable + */ + Mocha.prototype.addFile = function (file) { + this.files.push(file); + return this; + }; + +/** + * Sets reporter to `reporter`, defaults to "spec". + * + * @public + * @see {@link /#-reporter-name-r-name|CLI option} + * @see {@link /#reporters|Reporters} + * @param {String|Function} reporter - Reporter name or constructor. + * @param {Object} [reporterOptions] - Options used to configure the reporter. + * @returns {Mocha} this + * @chainable + * @throws {Error} if requested reporter cannot be loaded + * @example + * + * // Use XUnit reporter and direct its output to file + * mocha.reporter('xunit', { output: '/path/to/testspec.xunit.xml' }); + */ + Mocha.prototype.reporter = function (reporter, reporterOptions) { + if (typeof reporter === 'function') { + this._reporter = reporter; + } else { + reporter = reporter || 'spec'; + var _reporter; + // Try to load a built-in reporter. + if (builtinReporters[reporter]) { + _reporter = builtinReporters[reporter]; + } + // Try to load reporters from process.cwd() and node_modules + if (!_reporter) { + try { + _reporter = require(reporter); + } catch (err) { + if ( + err.code !== 'MODULE_NOT_FOUND' || + err.message.indexOf('Cannot find module') !== -1 + ) { + // Try to load reporters from a path (absolute or relative) + try { + _reporter = require(path.resolve(process.cwd(), reporter)); + } catch (_err) { + _err.code !== 'MODULE_NOT_FOUND' || + _err.message.indexOf('Cannot find module') !== -1 + ? console.warn(sQuote(reporter) + ' reporter not found') + : console.warn( + sQuote(reporter) + + ' reporter blew up with error:\n' + + err.stack + ); + } + } else { + console.warn( + sQuote(reporter) + ' reporter blew up with error:\n' + err.stack + ); + } + } + } + if (!_reporter) { + throw createInvalidReporterError( + 'invalid reporter ' + sQuote(reporter), + reporter + ); + } + this._reporter = _reporter; + } + this.options.reporterOption = reporterOptions; + // alias option name is used in public reporters xunit/tap/progress + this.options.reporterOptions = reporterOptions; + return this; + }; + +/** + * Sets test UI `name`, defaults to "bdd". + * + * @public + * @see {@link /#-ui-name-u-name|CLI option} + * @see {@link /#interfaces|Interface DSLs} + * @param {string|Function} [ui=bdd] - Interface name or class. + * @returns {Mocha} this + * @chainable + * @throws {Error} if requested interface cannot be loaded + */ + Mocha.prototype.ui = function (ui) { + var bindInterface; + if (typeof ui === 'function') { + bindInterface = ui; + } else { + ui = ui || 'bdd'; + bindInterface = exports.interfaces[ui]; + if (!bindInterface) { + try { + bindInterface = require(ui); + } catch (err) { + throw createInvalidInterfaceError( + 'invalid interface ' + sQuote(ui), + ui + ); + } + } + } + bindInterface(this.suite); + + this.suite.on(EVENT_FILE_PRE_REQUIRE, function (context) { + exports.afterEach = context.afterEach || context.teardown; + exports.after = context.after || context.suiteTeardown; + exports.beforeEach = context.beforeEach || context.setup; + exports.before = context.before || context.suiteSetup; + exports.describe = context.describe || context.suite; + exports.it = context.it || context.test; + exports.xit = context.xit || (context.test && context.test.skip); + exports.setup = context.setup || context.beforeEach; + exports.suiteSetup = context.suiteSetup || context.before; + exports.suiteTeardown = context.suiteTeardown || context.after; + exports.suite = context.suite || context.describe; + exports.teardown = context.teardown || context.afterEach; + exports.test = context.test || context.it; + exports.run = context.run; + }); + + return this; + }; + +/** + * Loads `files` prior to execution. + * + * @description + * The implementation relies on Node's `require` to execute + * the test interface functions and will be subject to its cache. + * + * @private + * @see {@link Mocha#addFile} + * @see {@link Mocha#run} + * @see {@link Mocha#unloadFiles} + * @param {Function} [fn] - Callback invoked upon completion. + */ + Mocha.prototype.loadFiles = function (fn) { + var self = this; + var suite = this.suite; + this.files.forEach(function (file) { + file = path.resolve(file); + suite.emit(EVENT_FILE_PRE_REQUIRE, global, file, self); + suite.emit(EVENT_FILE_REQUIRE, require(file), file, self); + suite.emit(EVENT_FILE_POST_REQUIRE, global, file, self); + }); + fn && fn(); + }; + +/** + * Removes a previously loaded file from Node's `require` cache. + * + * @private + * @static + * @see {@link Mocha#unloadFiles} + * @param {string} file - Pathname of file to be unloaded. + */ + Mocha.unloadFile = function (file) { + delete require.cache[require.resolve(file)]; + }; + +/** + * Unloads `files` from Node's `require` cache. + * + * @description + * This allows files to be "freshly" reloaded, providing the ability + * to reuse a Mocha instance programmatically. + * + * Intended for consumers — not used internally + * + * @public + * @see {@link Mocha.unloadFile} + * @see {@link Mocha#loadFiles} + * @see {@link Mocha#run} + * @returns {Mocha} this + * @chainable + */ + Mocha.prototype.unloadFiles = function () { + this.files.forEach(Mocha.unloadFile); + return this; + }; + +/** + * Sets `grep` filter after escaping RegExp special characters. + * + * @public + * @see {@link Mocha#grep} + * @param {string} str - Value to be converted to a regexp. + * @returns {Mocha} this + * @chainable + * @example + * + * // Select tests whose full title begins with `"foo"` followed by a period + * mocha.fgrep('foo.'); + */ + Mocha.prototype.fgrep = function (str) { + if (!str) { + return this; + } + return this.grep(new RegExp(escapeRe(str))); + }; + +/** + * @summary + * Sets `grep` filter used to select specific tests for execution. + * + * @description + * If `re` is a regexp-like string, it will be converted to regexp. + * The regexp is tested against the full title of each test (i.e., the + * name of the test preceded by titles of each its ancestral suites). + * As such, using an exact-match fixed pattern against the + * test name itself will not yield any matches. + *
    + * Previous filter value will be overwritten on each call! + * + * @public + * @see {@link /#grep-regexp-g-regexp|CLI option} + * @see {@link Mocha#fgrep} + * @see {@link Mocha#invert} + * @param {RegExp|String} re - Regular expression used to select tests. + * @return {Mocha} this + * @chainable + * @example + * + * // Select tests whose full title contains `"match"`, ignoring case + * mocha.grep(/match/i); + * @example + * + * // Same as above but with regexp-like string argument + * mocha.grep('/match/i'); + * @example + * + * // ## Anti-example + * // Given embedded test `it('only-this-test')`... + * mocha.grep('/^only-this-test$/'); // NO! Use `.only()` to do this! + */ + Mocha.prototype.grep = function (re) { + if (utils.isString(re)) { + // extract args if it's regex-like, i.e: [string, pattern, flag] + var arg = re.match(/^\/(.*)\/(g|i|)$|.*/); + this.options.grep = new RegExp(arg[1] || arg[0], arg[2]); + } else { + this.options.grep = re; + } + return this; + }; + +/** + * Inverts `grep` matches. + * + * @public + * @see {@link Mocha#grep} + * @return {Mocha} this + * @chainable + * @example + * + * // Select tests whose full title does *not* contain `"match"`, ignoring case + * mocha.grep(/match/i).invert(); + */ + Mocha.prototype.invert = function () { + this.options.invert = true; + return this; + }; + +/** + * Enables or disables ignoring global leaks. + * + * @public + * @see {@link Mocha#checkLeaks} + * @param {boolean} [ignoreLeaks=false] - Whether to ignore global leaks. + * @return {Mocha} this + * @chainable + * @example + * + * // Ignore global leaks + * mocha.ignoreLeaks(true); + */ + Mocha.prototype.ignoreLeaks = function (ignoreLeaks) { + this.options.checkLeaks = !ignoreLeaks; + return this; + }; + +/** + * Enables checking for global variables leaked while running tests. + * + * @public + * @see {@link /#-check-leaks|CLI option} + * @see {@link Mocha#ignoreLeaks} + * @return {Mocha} this + * @chainable + */ + Mocha.prototype.checkLeaks = function () { + this.options.checkLeaks = true; + return this; + }; + +/** + * Displays full stack trace upon test failure. + * + * @public + * @return {Mocha} this + * @chainable + */ + Mocha.prototype.fullTrace = function () { + this.options.fullTrace = true; + return this; + }; + +/** + * Enables desktop notification support if prerequisite software installed. + * + * @public + * @see {@link Mocha#isGrowlCapable} + * @see {@link Mocha#_growl} + * @return {Mocha} this + * @chainable + */ + Mocha.prototype.growl = function () { + this.options.growl = this.isGrowlCapable(); + if (!this.options.growl) { + var detail = process.browser + ? 'notification support not available in this browser...' + : 'notification support prerequisites not installed...'; + console.error(detail + ' cannot enable!'); + } + return this; + }; + +/** + * @summary + * Determines if Growl support seems likely. + * + * @description + * Not available when run in browser. + * + * @private + * @see {@link Growl#isCapable} + * @see {@link Mocha#growl} + * @return {boolean} whether Growl support can be expected + */ + Mocha.prototype.isGrowlCapable = growl.isCapable; + +/** + * Implements desktop notifications using a pseudo-reporter. + * + * @private + * @see {@link Mocha#growl} + * @see {@link Growl#notify} + * @param {Runner} runner - Runner instance. + */ + Mocha.prototype._growl = growl.notify; + +/** + * Specifies whitelist of variable names to be expected in global scope. + * + * @public + * @see {@link /#-global-variable-name|CLI option} + * @see {@link Mocha#checkLeaks} + * @param {String[]|String} globals - Accepted global variable name(s). + * @return {Mocha} this + * @chainable + * @example + * + * // Specify variables to be expected in global scope + * mocha.globals(['jQuery', 'MyLib']); + */ + Mocha.prototype.globals = function (globals) { + this.options.global = (this.options.global || []) + .concat(globals) + .filter(Boolean) + .filter(function (elt, idx, arr) { + return arr.indexOf(elt) === idx; + }); + return this; + }; + +/** + * Enables or disables TTY color output by screen-oriented reporters. + * + * @public + * @param {boolean} colors - Whether to enable color output. + * @return {Mocha} this + * @chainable + */ + Mocha.prototype.useColors = function (colors) { + if (colors !== undefined) { + this.options.color = colors; + } + return this; + }; + +/** + * Determines if reporter should use inline diffs (rather than +/-) + * in test failure output. + * + * @public + * @param {boolean} [inlineDiffs=false] - Whether to use inline diffs. + * @return {Mocha} this + * @chainable + */ + Mocha.prototype.useInlineDiffs = function (inlineDiffs) { + this.options.inlineDiffs = inlineDiffs !== undefined && inlineDiffs; + return this; + }; + +/** + * Determines if reporter should include diffs in test failure output. + * + * @public + * @param {boolean} [hideDiff=false] - Whether to hide diffs. + * @return {Mocha} this + * @chainable + */ + Mocha.prototype.hideDiff = function (hideDiff) { + this.options.diff = !(hideDiff === true); + return this; + }; + +/** + * @summary + * Sets timeout threshold value. + * + * @description + * A string argument can use shorthand (such as "2s") and will be converted. + * If the value is `0`, timeouts will be disabled. + * + * @public + * @see {@link /#-timeout-ms-t-ms|CLI option} + * @see {@link /#timeouts|Timeouts} + * @see {@link Mocha#enableTimeouts} + * @param {number|string} msecs - Timeout threshold value. + * @return {Mocha} this + * @chainable + * @example + * + * // Sets timeout to one second + * mocha.timeout(1000); + * @example + * + * // Same as above but using string argument + * mocha.timeout('1s'); + */ + Mocha.prototype.timeout = function (msecs) { + this.suite.timeout(msecs); + return this; + }; + +/** + * Sets the number of times to retry failed tests. + * + * @public + * @see {@link /#retry-tests|Retry Tests} + * @param {number} retry - Number of times to retry failed tests. + * @return {Mocha} this + * @chainable + * @example + * + * // Allow any failed test to retry one more time + * mocha.retries(1); + */ + Mocha.prototype.retries = function (n) { + this.suite.retries(n); + return this; + }; + +/** + * Sets slowness threshold value. + * + * @public + * @see {@link /#-slow-ms-s-ms|CLI option} + * @param {number} msecs - Slowness threshold value. + * @return {Mocha} this + * @chainable + * @example + * + * // Sets "slow" threshold to half a second + * mocha.slow(500); + * @example + * + * // Same as above but using string argument + * mocha.slow('0.5s'); + */ + Mocha.prototype.slow = function (msecs) { + this.suite.slow(msecs); + return this; + }; + +/** + * Enables or disables timeouts. + * + * @public + * @see {@link /#-timeout-ms-t-ms|CLI option} + * @param {boolean} enableTimeouts - Whether to enable timeouts. + * @return {Mocha} this + * @chainable + */ + Mocha.prototype.enableTimeouts = function (enableTimeouts) { + this.suite.enableTimeouts( + arguments.length && enableTimeouts !== undefined ? enableTimeouts : true + ); + return this; + }; + +/** + * Forces all tests to either accept a `done` callback or return a promise. + * + * @public + * @return {Mocha} this + * @chainable + */ + Mocha.prototype.asyncOnly = function () { + this.options.asyncOnly = true; + return this; + }; + +/** + * Disables syntax highlighting (in browser). + * + * @public + * @return {Mocha} this + * @chainable + */ + Mocha.prototype.noHighlighting = function () { + this.options.noHighlighting = true; + return this; + }; + +/** + * Enables uncaught errors to propagate (in browser). + * + * @public + * @return {Mocha} this + * @chainable + */ + Mocha.prototype.allowUncaught = function () { + this.options.allowUncaught = true; + return this; + }; + +/** + * @summary + * Delays root suite execution. + * + * @description + * Used to perform asynch operations before any suites are run. + * + * @public + * @see {@link /#delayed-root-suite|delayed root suite} + * @returns {Mocha} this + * @chainable + */ + Mocha.prototype.delay = function delay () { + this.options.delay = true; + return this; + }; + +/** + * Causes tests marked `only` to fail the suite. + * + * @public + * @returns {Mocha} this + * @chainable + */ + Mocha.prototype.forbidOnly = function () { + this.options.forbidOnly = true; + return this; + }; + +/** + * Causes pending tests and tests marked `skip` to fail the suite. + * + * @public + * @returns {Mocha} this + * @chainable + */ + Mocha.prototype.forbidPending = function () { + this.options.forbidPending = true; + return this; + }; + +/** + * Mocha version as specified by "package.json". + * + * @name Mocha#version + * @type string + * @readonly + */ + Object.defineProperty(Mocha.prototype, 'version', { + value: require('../package.json').version, + configurable: false, + enumerable: true, + writable: false + }); + +/** + * Callback to be invoked when test execution is complete. + * + * @callback DoneCB + * @param {number} failures - Number of failures that occurred. + */ + +/** + * Runs root suite and invokes `fn()` when complete. + * + * @description + * To run tests multiple times (or to run tests in files that are + * already in the `require` cache), make sure to clear them from + * the cache first! + * + * @public + * @see {@link Mocha#loadFiles} + * @see {@link Mocha#unloadFiles} + * @see {@link Runner#run} + * @param {DoneCB} [fn] - Callback invoked when test execution completed. + * @return {Runner} runner instance + */ + Mocha.prototype.run = function (fn) { + if (this.files.length) { + this.loadFiles(); + } + var suite = this.suite; + var options = this.options; + options.files = this.files; + var runner = new exports.Runner(suite, options.delay); + createStatsCollector(runner); + var reporter = new this._reporter(runner, options); + runner.checkLeaks = options.checkLeaks === true; + runner.fullStackTrace = options.fullTrace; + runner.asyncOnly = options.asyncOnly; + runner.allowUncaught = options.allowUncaught; + runner.forbidOnly = options.forbidOnly; + runner.forbidPending = options.forbidPending; + if (options.grep) { + runner.grep(options.grep, options.invert); + } + if (options.global) { + runner.globals(options.global); + } + if (options.growl) { + this._growl(runner); + } + if (options.color !== undefined) { + exports.reporters.Base.useColors = options.color; + } + exports.reporters.Base.inlineDiffs = options.inlineDiffs; + exports.reporters.Base.hideDiff = !options.diff; + + function done (failures) { + fn = fn || utils.noop; + if (reporter.done) { + reporter.done(failures, fn); + } else { + fn(failures); + } + } + + return runner.run(done); + }; + + }).call(this, require('_process'), typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}); + }, {'../package.json': 91, './context': 5, './errors': 6, './growl': 2, './hook': 7, './interfaces': 11, './mocharc.json': 15, './reporters': 21, './runnable': 33, './runner': 34, './stats-collector': 35, './suite': 36, './test': 37, './utils': 38, '_process': 70, 'escape-string-regexp': 49, 'path': 40}], + 15: [function (require, module, exports) { + module.exports = { + 'diff': true, + 'extension': ['js'], + 'opts': './test/mocha.opts', + 'package': './package.json', + 'reporter': 'spec', + 'slow': 75, + 'timeout': 2000, + 'ui': 'bdd', + 'watch-ignore': ['node_modules', '.git'] + }; + + }, {}], + 16: [function (require, module, exports) { + 'use strict'; + + module.exports = Pending; + +/** + * Initialize a new `Pending` error with the given message. + * + * @param {string} message + */ + function Pending (message) { + this.message = message; + } + + }, {}], + 17: [function (require, module, exports) { + (function (process) { + 'use strict'; +/** + * @module Base + */ +/** + * Module dependencies. + */ + + var tty = require('tty'); + var diff = require('diff'); + var milliseconds = require('ms'); + var utils = require('../utils'); + var supportsColor = process.browser ? null : require('supports-color'); + var constants = require('../runner').constants; + var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; + var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; + +/** + * Expose `Base`. + */ + + exports = module.exports = Base; + +/** + * Check if both stdio streams are associated with a tty. + */ + + var isatty = process.stdout.isTTY && process.stderr.isTTY; + +/** + * Save log references to avoid tests interfering (see GH-3604). + */ + var consoleLog = console.log; + +/** + * Enable coloring by default, except in the browser interface. + */ + + exports.useColors = + !process.browser && + (supportsColor.stdout || process.env.MOCHA_COLORS !== undefined); + +/** + * Inline diffs instead of +/- + */ + + exports.inlineDiffs = false; + +/** + * Default color map. + */ + + exports.colors = { + pass: 90, + fail: 31, + 'bright pass': 92, + 'bright fail': 91, + 'bright yellow': 93, + pending: 36, + suite: 0, + 'error title': 0, + 'error message': 31, + 'error stack': 90, + checkmark: 32, + fast: 90, + medium: 33, + slow: 31, + green: 32, + light: 90, + 'diff gutter': 90, + 'diff added': 32, + 'diff removed': 31 + }; + +/** + * Default symbol map. + */ + + exports.symbols = { + ok: '✓', + err: '✖', + dot: '․', + comma: ',', + bang: '!' + }; + +// With node.js on Windows: use symbols available in terminal default fonts + if (process.platform === 'win32') { + exports.symbols.ok = '\u221A'; + exports.symbols.err = '\u00D7'; + exports.symbols.dot = '.'; + } + +/** + * Color `str` with the given `type`, + * allowing colors to be disabled, + * as well as user-defined color + * schemes. + * + * @private + * @param {string} type + * @param {string} str + * @return {string} + */ + var color = (exports.color = function (type, str) { + if (!exports.useColors) { + return String(str); + } + return '\u001b[' + exports.colors[type] + 'm' + str + '\u001b[0m'; + }); + +/** + * Expose term window size, with some defaults for when stderr is not a tty. + */ + + exports.window = { + width: 75 + }; + + if (isatty) { + exports.window.width = process.stdout.getWindowSize + ? process.stdout.getWindowSize(1)[0] + : tty.getWindowSize()[1]; + } + +/** + * Expose some basic cursor interactions that are common among reporters. + */ + + exports.cursor = { + hide: function () { + isatty && process.stdout.write('\u001b[?25l'); + }, + + show: function () { + isatty && process.stdout.write('\u001b[?25h'); + }, + + deleteLine: function () { + isatty && process.stdout.write('\u001b[2K'); + }, + + beginningOfLine: function () { + isatty && process.stdout.write('\u001b[0G'); + }, + + CR: function () { + if (isatty) { + exports.cursor.deleteLine(); + exports.cursor.beginningOfLine(); + } else { + process.stdout.write('\r'); + } + } + }; + + function showDiff (err) { + return ( + err && + err.showDiff !== false && + sameType(err.actual, err.expected) && + err.expected !== undefined + ); + } + + function stringifyDiffObjs (err) { + if (!utils.isString(err.actual) || !utils.isString(err.expected)) { + err.actual = utils.stringify(err.actual); + err.expected = utils.stringify(err.expected); + } + } + +/** + * Returns a diff between 2 strings with coloured ANSI output. + * + * @description + * The diff will be either inline or unified dependent on the value + * of `Base.inlineDiff`. + * + * @param {string} actual + * @param {string} expected + * @return {string} Diff + */ + var generateDiff = (exports.generateDiff = function (actual, expected) { + return exports.inlineDiffs + ? inlineDiff(actual, expected) + : unifiedDiff(actual, expected); + }); + +/** + * Outputs the given `failures` as a list. + * + * @public + * @memberof Mocha.reporters.Base + * @variation 1 + * @param {Object[]} failures - Each is Test instance with corresponding + * Error property + */ + exports.list = function (failures) { + Base.consoleLog(); + failures.forEach(function (test, i) { + // format + var fmt = + color('error title', ' %s) %s:\n') + + color('error message', ' %s') + + color('error stack', '\n%s\n'); + + // msg + var msg; + var err = test.err; + var message; + if (err.message && typeof err.message.toString === 'function') { + message = err.message + ''; + } else if (typeof err.inspect === 'function') { + message = err.inspect() + ''; + } else { + message = ''; + } + var stack = err.stack || message; + var index = message ? stack.indexOf(message) : -1; + + if (index === -1) { + msg = message; + } else { + index += message.length; + msg = stack.slice(0, index); + // remove msg from stack + stack = stack.slice(index + 1); + } + + // uncaught + if (err.uncaught) { + msg = 'Uncaught ' + msg; + } + // explicitly show diff + if (!exports.hideDiff && showDiff(err)) { + stringifyDiffObjs(err); + fmt = + color('error title', ' %s) %s:\n%s') + color('error stack', '\n%s\n'); + var match = message.match(/^([^:]+): expected/); + msg = '\n ' + color('error message', match ? match[1] : msg); + + msg += generateDiff(err.actual, err.expected); + } + + // indent stack trace + stack = stack.replace(/^/gm, ' '); + + // indented test title + var testTitle = ''; + test.titlePath().forEach(function (str, index) { + if (index !== 0) { + testTitle += '\n '; + } + for (var i = 0; i < index; i++) { + testTitle += ' '; + } + testTitle += str; + }); + + Base.consoleLog(fmt, i + 1, testTitle, msg, stack); + }); + }; + +/** + * Constructs a new `Base` reporter instance. + * + * @description + * All other reporters generally inherit from this reporter. + * + * @public + * @class + * @memberof Mocha.reporters + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function Base (runner, options) { + var failures = (this.failures = []); + + if (!runner) { + throw new TypeError('Missing runner argument'); + } + this.options = options || {}; + this.runner = runner; + this.stats = runner.stats; // assigned so Reporters keep a closer reference + + runner.on(EVENT_TEST_PASS, function (test) { + if (test.duration > test.slow()) { + test.speed = 'slow'; + } else if (test.duration > test.slow() / 2) { + test.speed = 'medium'; + } else { + test.speed = 'fast'; + } + }); + + runner.on(EVENT_TEST_FAIL, function (test, err) { + if (showDiff(err)) { + stringifyDiffObjs(err); + } + test.err = err; + failures.push(test); + }); + } + +/** + * Outputs common epilogue used by many of the bundled reporters. + * + * @public + * @memberof Mocha.reporters + */ + Base.prototype.epilogue = function () { + var stats = this.stats; + var fmt; + + Base.consoleLog(); + + // passes + fmt = + color('bright pass', ' ') + + color('green', ' %d passing') + + color('light', ' (%s)'); + + Base.consoleLog(fmt, stats.passes || 0, milliseconds(stats.duration)); + + // pending + if (stats.pending) { + fmt = color('pending', ' ') + color('pending', ' %d pending'); + + Base.consoleLog(fmt, stats.pending); + } + + // failures + if (stats.failures) { + fmt = color('fail', ' %d failing'); + + Base.consoleLog(fmt, stats.failures); + + Base.list(this.failures); + Base.consoleLog(); + } + + Base.consoleLog(); + }; + +/** + * Pads the given `str` to `len`. + * + * @private + * @param {string} str + * @param {string} len + * @return {string} + */ + function pad (str, len) { + str = String(str); + return Array(len - str.length + 1).join(' ') + str; + } + +/** + * Returns inline diff between 2 strings with coloured ANSI output. + * + * @private + * @param {String} actual + * @param {String} expected + * @return {string} Diff + */ + function inlineDiff (actual, expected) { + var msg = errorDiff(actual, expected); + + // linenos + var lines = msg.split('\n'); + if (lines.length > 4) { + var width = String(lines.length).length; + msg = lines + .map(function (str, i) { + return pad(++i, width) + ' |' + ' ' + str; + }) + .join('\n'); + } + + // legend + msg = + '\n' + + color('diff removed', 'actual') + + ' ' + + color('diff added', 'expected') + + '\n\n' + + msg + + '\n'; + + // indent + msg = msg.replace(/^/gm, ' '); + return msg; + } + +/** + * Returns unified diff between two strings with coloured ANSI output. + * + * @private + * @param {String} actual + * @param {String} expected + * @return {string} The diff. + */ + function unifiedDiff (actual, expected) { + var indent = ' '; + function cleanUp (line) { + if (line[0] === '+') { + return indent + colorLines('diff added', line); + } + if (line[0] === '-') { + return indent + colorLines('diff removed', line); + } + if (line.match(/@@/)) { + return '--'; + } + if (line.match(/\\ No newline/)) { + return null; + } + return indent + line; + } + function notBlank (line) { + return typeof line !== 'undefined' && line !== null; + } + var msg = diff.createPatch('string', actual, expected); + var lines = msg.split('\n').splice(5); + return ( + '\n ' + + colorLines('diff added', '+ expected') + + ' ' + + colorLines('diff removed', '- actual') + + '\n\n' + + lines + .map(cleanUp) + .filter(notBlank) + .join('\n') + ); + } + +/** + * Returns character diff for `err`. + * + * @private + * @param {String} actual + * @param {String} expected + * @return {string} the diff + */ + function errorDiff (actual, expected) { + return diff + .diffWordsWithSpace(actual, expected) + .map(function (str) { + if (str.added) { + return colorLines('diff added', str.value); + } + if (str.removed) { + return colorLines('diff removed', str.value); + } + return str.value; + }) + .join(''); + } + +/** + * Colors lines for `str`, using the color `name`. + * + * @private + * @param {string} name + * @param {string} str + * @return {string} + */ + function colorLines (name, str) { + return str + .split('\n') + .map(function (str) { + return color(name, str); + }) + .join('\n'); + } + +/** + * Object#toString reference. + */ + var objToString = Object.prototype.toString; + +/** + * Checks that a / b have the same type. + * + * @private + * @param {Object} a + * @param {Object} b + * @return {boolean} + */ + function sameType (a, b) { + return objToString.call(a) === objToString.call(b); + } + + Base.consoleLog = consoleLog; + + Base.abstract = true; + + }).call(this, require('_process')); + }, {'../runner': 34, '../utils': 38, '_process': 70, 'diff': 48, 'ms': 60, 'supports-color': 40, 'tty': 4}], + 18: [function (require, module, exports) { + 'use strict'; +/** + * @module Doc + */ +/** + * Module dependencies. + */ + + var Base = require('./base'); + var utils = require('../utils'); + var constants = require('../runner').constants; + var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; + var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; + var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN; + var EVENT_SUITE_END = constants.EVENT_SUITE_END; + +/** + * Expose `Doc`. + */ + + exports = module.exports = Doc; + +/** + * Constructs a new `Doc` reporter instance. + * + * @public + * @class + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function Doc (runner, options) { + Base.call(this, runner, options); + + var indents = 2; + + function indent () { + return Array(indents).join(' '); + } + + runner.on(EVENT_SUITE_BEGIN, function (suite) { + if (suite.root) { + return; + } + ++indents; + Base.consoleLog('%s
    ', indent()); + ++indents; + Base.consoleLog('%s

    %s

    ', indent(), utils.escape(suite.title)); + Base.consoleLog('%s
    ', indent()); + }); + + runner.on(EVENT_SUITE_END, function (suite) { + if (suite.root) { + return; + } + Base.consoleLog('%s
    ', indent()); + --indents; + Base.consoleLog('%s
    ', indent()); + --indents; + }); + + runner.on(EVENT_TEST_PASS, function (test) { + Base.consoleLog('%s
    %s
    ', indent(), utils.escape(test.title)); + var code = utils.escape(utils.clean(test.body)); + Base.consoleLog('%s
    %s
    ', indent(), code); + }); + + runner.on(EVENT_TEST_FAIL, function (test, err) { + Base.consoleLog( + '%s
    %s
    ', + indent(), + utils.escape(test.title) + ); + var code = utils.escape(utils.clean(test.body)); + Base.consoleLog( + '%s
    %s
    ', + indent(), + code + ); + Base.consoleLog( + '%s
    %s
    ', + indent(), + utils.escape(err) + ); + }); + } + + Doc.description = 'HTML documentation'; + + }, {'../runner': 34, '../utils': 38, './base': 17}], + 19: [function (require, module, exports) { + (function (process) { + 'use strict'; +/** + * @module Dot + */ +/** + * Module dependencies. + */ + + var Base = require('./base'); + var inherits = require('../utils').inherits; + var constants = require('../runner').constants; + var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; + var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; + var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN; + var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING; + var EVENT_RUN_END = constants.EVENT_RUN_END; + +/** + * Expose `Dot`. + */ + + exports = module.exports = Dot; + +/** + * Constructs a new `Dot` reporter instance. + * + * @public + * @class + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function Dot (runner, options) { + Base.call(this, runner, options); + + var self = this; + var width = (Base.window.width * 0.75) | 0; + var n = -1; + + runner.on(EVENT_RUN_BEGIN, function () { + process.stdout.write('\n'); + }); + + runner.on(EVENT_TEST_PENDING, function () { + if (++n % width === 0) { + process.stdout.write('\n '); + } + process.stdout.write(Base.color('pending', Base.symbols.comma)); + }); + + runner.on(EVENT_TEST_PASS, function (test) { + if (++n % width === 0) { + process.stdout.write('\n '); + } + if (test.speed === 'slow') { + process.stdout.write(Base.color('bright yellow', Base.symbols.dot)); + } else { + process.stdout.write(Base.color(test.speed, Base.symbols.dot)); + } + }); + + runner.on(EVENT_TEST_FAIL, function () { + if (++n % width === 0) { + process.stdout.write('\n '); + } + process.stdout.write(Base.color('fail', Base.symbols.bang)); + }); + + runner.once(EVENT_RUN_END, function () { + process.stdout.write('\n'); + self.epilogue(); + }); + } + +/** + * Inherit from `Base.prototype`. + */ + inherits(Dot, Base); + + Dot.description = 'dot matrix representation'; + + }).call(this, require('_process')); + }, {'../runner': 34, '../utils': 38, './base': 17, '_process': 70}], + 20: [function (require, module, exports) { + (function (global) { + 'use strict'; + +/* eslint-env browser */ +/** + * @module HTML + */ +/** + * Module dependencies. + */ + + var Base = require('./base'); + var utils = require('../utils'); + var Progress = require('../browser/progress'); + var escapeRe = require('escape-string-regexp'); + var constants = require('../runner').constants; + var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; + var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; + var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN; + var EVENT_SUITE_END = constants.EVENT_SUITE_END; + var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING; + var escape = utils.escape; + +/** + * Save timer references to avoid Sinon interfering (see GH-237). + */ + + var Date = global.Date; + +/** + * Expose `HTML`. + */ + + exports = module.exports = HTML; + +/** + * Stats template. + */ + + var statsTemplate = + ''; + + var playIcon = '‣'; + +/** + * Constructs a new `HTML` reporter instance. + * + * @public + * @class + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function HTML (runner, options) { + Base.call(this, runner, options); + + var self = this; + var stats = this.stats; + var stat = fragment(statsTemplate); + var items = stat.getElementsByTagName('li'); + var passes = items[1].getElementsByTagName('em')[0]; + var passesLink = items[1].getElementsByTagName('a')[0]; + var failures = items[2].getElementsByTagName('em')[0]; + var failuresLink = items[2].getElementsByTagName('a')[0]; + var duration = items[3].getElementsByTagName('em')[0]; + var canvas = stat.getElementsByTagName('canvas')[0]; + var report = fragment('
      '); + var stack = [report]; + var progress; + var ctx; + var root = document.getElementById('mocha'); + + if (canvas.getContext) { + var ratio = window.devicePixelRatio || 1; + canvas.style.width = canvas.width; + canvas.style.height = canvas.height; + canvas.width *= ratio; + canvas.height *= ratio; + ctx = canvas.getContext('2d'); + ctx.scale(ratio, ratio); + progress = new Progress(); + } + + if (!root) { + return error('#mocha div missing, add it to your document'); + } + + // pass toggle + on(passesLink, 'click', function (evt) { + evt.preventDefault(); + unhide(); + var name = /pass/.test(report.className) ? '' : ' pass'; + report.className = report.className.replace(/fail|pass/g, '') + name; + if (report.className.trim()) { + hideSuitesWithout('test pass'); + } + }); + + // failure toggle + on(failuresLink, 'click', function (evt) { + evt.preventDefault(); + unhide(); + var name = /fail/.test(report.className) ? '' : ' fail'; + report.className = report.className.replace(/fail|pass/g, '') + name; + if (report.className.trim()) { + hideSuitesWithout('test fail'); + } + }); + + root.appendChild(stat); + root.appendChild(report); + + if (progress) { + progress.size(40); + } + + runner.on(EVENT_SUITE_BEGIN, function (suite) { + if (suite.root) { + return; + } + + // suite + var url = self.suiteURL(suite); + var el = fragment( + '
    • %s

    • ', + url, + escape(suite.title) + ); + + // container + stack[0].appendChild(el); + stack.unshift(document.createElement('ul')); + el.appendChild(stack[0]); + }); + + runner.on(EVENT_SUITE_END, function (suite) { + if (suite.root) { + updateStats(); + return; + } + stack.shift(); + }); + + runner.on(EVENT_TEST_PASS, function (test) { + var url = self.testURL(test); + var markup = + '
    • %e%ems ' + + '' + + playIcon + + '

    • '; + var el = fragment(markup, test.speed, test.title, test.duration, url); + self.addCodeToggle(el, test.body); + appendToStack(el); + updateStats(); + }); + + runner.on(EVENT_TEST_FAIL, function (test) { + var el = fragment( + '
    • %e ' + + playIcon + + '

    • ', + test.title, + self.testURL(test) + ); + var stackString; // Note: Includes leading newline + var message = test.err.toString(); + + // <=IE7 stringifies to [Object Error]. Since it can be overloaded, we + // check for the result of the stringifying. + if (message === '[object Error]') { + message = test.err.message; + } + + if (test.err.stack) { + var indexOfMessage = test.err.stack.indexOf(test.err.message); + if (indexOfMessage === -1) { + stackString = test.err.stack; + } else { + stackString = test.err.stack.substr( + test.err.message.length + indexOfMessage + ); + } + } else if (test.err.sourceURL && test.err.line !== undefined) { + // Safari doesn't give you a stack. Let's at least provide a source line. + stackString = '\n(' + test.err.sourceURL + ':' + test.err.line + ')'; + } + + stackString = stackString || ''; + + if (test.err.htmlMessage && stackString) { + el.appendChild( + fragment( + '
      %s\n
      %e
      ', + test.err.htmlMessage, + stackString + ) + ); + } else if (test.err.htmlMessage) { + el.appendChild( + fragment('
      %s
      ', test.err.htmlMessage) + ); + } else { + el.appendChild( + fragment('
      %e%e
      ', message, stackString) + ); + } + + self.addCodeToggle(el, test.body); + appendToStack(el); + updateStats(); + }); + + runner.on(EVENT_TEST_PENDING, function (test) { + var el = fragment( + '
    • %e

    • ', + test.title + ); + appendToStack(el); + updateStats(); + }); + + function appendToStack (el) { + // Don't call .appendChild if #mocha-report was already .shift()'ed off the stack. + if (stack[0]) { + stack[0].appendChild(el); + } + } + + function updateStats () { + // TODO: add to stats + var percent = ((stats.tests / runner.total) * 100) | 0; + if (progress) { + progress.update(percent).draw(ctx); + } + + // update stats + var ms = new Date() - stats.start; + text(passes, stats.passes); + text(failures, stats.failures); + text(duration, (ms / 1000).toFixed(2)); + } + } + +/** + * Makes a URL, preserving querystring ("search") parameters. + * + * @param {string} s + * @return {string} A new URL. + */ + function makeUrl (s) { + var search = window.location.search; + + // Remove previous grep query parameter if present + if (search) { + search = search.replace(/[?&]grep=[^&\s]*/g, '').replace(/^&/, '?'); + } + + return ( + window.location.pathname + + (search ? search + '&' : '?') + + 'grep=' + + encodeURIComponent(escapeRe(s)) + ); + } + +/** + * Provide suite URL. + * + * @param {Object} [suite] + */ + HTML.prototype.suiteURL = function (suite) { + return makeUrl(suite.fullTitle()); + }; + +/** + * Provide test URL. + * + * @param {Object} [test] + */ + HTML.prototype.testURL = function (test) { + return makeUrl(test.fullTitle()); + }; + +/** + * Adds code toggle functionality for the provided test's list element. + * + * @param {HTMLLIElement} el + * @param {string} contents + */ + HTML.prototype.addCodeToggle = function (el, contents) { + var h2 = el.getElementsByTagName('h2')[0]; + + on(h2, 'click', function () { + pre.style.display = pre.style.display === 'none' ? 'block' : 'none'; + }); + + var pre = fragment('
      %e
      ', utils.clean(contents)); + el.appendChild(pre); + pre.style.display = 'none'; + }; + +/** + * Display error `msg`. + * + * @param {string} msg + */ + function error (msg) { + document.body.appendChild(fragment('
      %s
      ', msg)); + } + +/** + * Return a DOM fragment from `html`. + * + * @param {string} html + */ + function fragment (html) { + var args = arguments; + var div = document.createElement('div'); + var i = 1; + + div.innerHTML = html.replace(/%([se])/g, function (_, type) { + switch (type) { + case 's': + return String(args[i++]); + case 'e': + return escape(args[i++]); + // no default + } + }); + + return div.firstChild; + } + +/** + * Check for suites that do not have elements + * with `classname`, and hide them. + * + * @param {text} classname + */ + function hideSuitesWithout (classname) { + var suites = document.getElementsByClassName('suite'); + for (var i = 0; i < suites.length; i++) { + var els = suites[i].getElementsByClassName(classname); + if (!els.length) { + suites[i].className += ' hidden'; + } + } + } + +/** + * Unhide .hidden suites. + */ + function unhide () { + var els = document.getElementsByClassName('suite hidden'); + for (var i = 0; i < els.length; ++i) { + els[i].className = els[i].className.replace('suite hidden', 'suite'); + } + } + +/** + * Set an element's text contents. + * + * @param {HTMLElement} el + * @param {string} contents + */ + function text (el, contents) { + if (el.textContent) { + el.textContent = contents; + } else { + el.innerText = contents; + } + } + +/** + * Listen on `event` with callback `fn`. + */ + function on (el, event, fn) { + if (el.addEventListener) { + el.addEventListener(event, fn, false); + } else { + el.attachEvent('on' + event, fn); + } + } + + HTML.browserOnly = true; + + }).call(this, typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}); + }, {'../browser/progress': 3, '../runner': 34, '../utils': 38, './base': 17, 'escape-string-regexp': 49}], + 21: [function (require, module, exports) { + 'use strict'; + +// Alias exports to a their normalized format Mocha#reporter to prevent a need +// for dynamic (try/catch) requires, which Browserify doesn't handle. + exports.Base = exports.base = require('./base'); + exports.Dot = exports.dot = require('./dot'); + exports.Doc = exports.doc = require('./doc'); + exports.TAP = exports.tap = require('./tap'); + exports.JSON = exports.json = require('./json'); + exports.HTML = exports.html = require('./html'); + exports.List = exports.list = require('./list'); + exports.Min = exports.min = require('./min'); + exports.Spec = exports.spec = require('./spec'); + exports.Nyan = exports.nyan = require('./nyan'); + exports.XUnit = exports.xunit = require('./xunit'); + exports.Markdown = exports.markdown = require('./markdown'); + exports.Progress = exports.progress = require('./progress'); + exports.Landing = exports.landing = require('./landing'); + exports.JSONStream = exports['json-stream'] = require('./json-stream'); + + }, {'./base': 17, './doc': 18, './dot': 19, './html': 20, './json': 23, './json-stream': 22, './landing': 24, './list': 25, './markdown': 26, './min': 27, './nyan': 28, './progress': 29, './spec': 30, './tap': 31, './xunit': 32}], + 22: [function (require, module, exports) { + (function (process) { + 'use strict'; +/** + * @module JSONStream + */ +/** + * Module dependencies. + */ + + var Base = require('./base'); + var constants = require('../runner').constants; + var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; + var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; + var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN; + var EVENT_RUN_END = constants.EVENT_RUN_END; + +/** + * Expose `JSONStream`. + */ + + exports = module.exports = JSONStream; + +/** + * Constructs a new `JSONStream` reporter instance. + * + * @public + * @class + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function JSONStream (runner, options) { + Base.call(this, runner, options); + + var self = this; + var total = runner.total; + + runner.once(EVENT_RUN_BEGIN, function () { + writeEvent(['start', {total: total}]); + }); + + runner.on(EVENT_TEST_PASS, function (test) { + writeEvent(['pass', clean(test)]); + }); + + runner.on(EVENT_TEST_FAIL, function (test, err) { + test = clean(test); + test.err = err.message; + test.stack = err.stack || null; + writeEvent(['fail', test]); + }); + + runner.once(EVENT_RUN_END, function () { + writeEvent(['end', self.stats]); + }); + } + +/** + * Mocha event to be written to the output stream. + * @typedef {Array} JSONStream~MochaEvent + */ + +/** + * Writes Mocha event to reporter output stream. + * + * @private + * @param {JSONStream~MochaEvent} event - Mocha event to be output. + */ + function writeEvent (event) { + process.stdout.write(JSON.stringify(event) + '\n'); + } + +/** + * Returns an object literal representation of `test` + * free of cyclic properties, etc. + * + * @private + * @param {Test} test - Instance used as data source. + * @return {Object} object containing pared-down test instance data + */ + function clean (test) { + return { + title: test.title, + fullTitle: test.fullTitle(), + duration: test.duration, + currentRetry: test.currentRetry() + }; + } + + JSONStream.description = 'newline delimited JSON events'; + + }).call(this, require('_process')); + }, {'../runner': 34, './base': 17, '_process': 70}], + 23: [function (require, module, exports) { + (function (process) { + 'use strict'; +/** + * @module JSON + */ +/** + * Module dependencies. + */ + + var Base = require('./base'); + var constants = require('../runner').constants; + var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; + var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; + var EVENT_TEST_END = constants.EVENT_TEST_END; + var EVENT_RUN_END = constants.EVENT_RUN_END; + var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING; + +/** + * Expose `JSON`. + */ + + exports = module.exports = JSONReporter; + +/** + * Constructs a new `JSON` reporter instance. + * + * @public + * @class JSON + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function JSONReporter (runner, options) { + Base.call(this, runner, options); + + var self = this; + var tests = []; + var pending = []; + var failures = []; + var passes = []; + + runner.on(EVENT_TEST_END, function (test) { + tests.push(test); + }); + + runner.on(EVENT_TEST_PASS, function (test) { + passes.push(test); + }); + + runner.on(EVENT_TEST_FAIL, function (test) { + failures.push(test); + }); + + runner.on(EVENT_TEST_PENDING, function (test) { + pending.push(test); + }); + + runner.once(EVENT_RUN_END, function () { + var obj = { + stats: self.stats, + tests: tests.map(clean), + pending: pending.map(clean), + failures: failures.map(clean), + passes: passes.map(clean) + }; + + runner.testResults = obj; + + process.stdout.write(JSON.stringify(obj, null, 2)); + }); + } + +/** + * Return a plain-object representation of `test` + * free of cyclic properties etc. + * + * @private + * @param {Object} test + * @return {Object} + */ + function clean (test) { + var err = test.err || {}; + if (err instanceof Error) { + err = errorJSON(err); + } + + return { + title: test.title, + fullTitle: test.fullTitle(), + duration: test.duration, + currentRetry: test.currentRetry(), + err: cleanCycles(err) + }; + } + +/** + * Replaces any circular references inside `obj` with '[object Object]' + * + * @private + * @param {Object} obj + * @return {Object} + */ + function cleanCycles (obj) { + var cache = []; + return JSON.parse( + JSON.stringify(obj, function (key, value) { + if (typeof value === 'object' && value !== null) { + if (cache.indexOf(value) !== -1) { + // Instead of going in a circle, we'll print [object Object] + return '' + value; + } + cache.push(value); + } + + return value; + }) + ); + } + +/** + * Transform an Error object into a JSON object. + * + * @private + * @param {Error} err + * @return {Object} + */ + function errorJSON (err) { + var res = {}; + Object.getOwnPropertyNames(err).forEach(function (key) { + res[key] = err[key]; + }, err); + return res; + } + + JSONReporter.description = 'single JSON object'; + + }).call(this, require('_process')); + }, {'../runner': 34, './base': 17, '_process': 70}], + 24: [function (require, module, exports) { + (function (process) { + 'use strict'; +/** + * @module Landing + */ +/** + * Module dependencies. + */ + + var Base = require('./base'); + var inherits = require('../utils').inherits; + var constants = require('../runner').constants; + var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN; + var EVENT_RUN_END = constants.EVENT_RUN_END; + var EVENT_TEST_END = constants.EVENT_TEST_END; + var STATE_FAILED = require('../runnable').constants.STATE_FAILED; + + var cursor = Base.cursor; + var color = Base.color; + +/** + * Expose `Landing`. + */ + + exports = module.exports = Landing; + +/** + * Airplane color. + */ + + Base.colors.plane = 0; + +/** + * Airplane crash color. + */ + + Base.colors['plane crash'] = 31; + +/** + * Runway color. + */ + + Base.colors.runway = 90; + +/** + * Constructs a new `Landing` reporter instance. + * + * @public + * @class + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function Landing (runner, options) { + Base.call(this, runner, options); + + var self = this; + var width = (Base.window.width * 0.75) | 0; + var total = runner.total; + var stream = process.stdout; + var plane = color('plane', '✈'); + var crashed = -1; + var n = 0; + + function runway () { + var buf = Array(width).join('-'); + return ' ' + color('runway', buf); + } + + runner.on(EVENT_RUN_BEGIN, function () { + stream.write('\n\n\n '); + cursor.hide(); + }); + + runner.on(EVENT_TEST_END, function (test) { + // check if the plane crashed + var col = crashed === -1 ? ((width * ++n) / total) | 0 : crashed; + + // show the crash + if (test.state === STATE_FAILED) { + plane = color('plane crash', '✈'); + crashed = col; + } + + // render landing strip + stream.write('\u001b[' + (width + 1) + 'D\u001b[2A'); + stream.write(runway()); + stream.write('\n '); + stream.write(color('runway', Array(col).join('⋅'))); + stream.write(plane); + stream.write(color('runway', Array(width - col).join('⋅') + '\n')); + stream.write(runway()); + stream.write('\u001b[0m'); + }); + + runner.once(EVENT_RUN_END, function () { + cursor.show(); + process.stdout.write('\n'); + self.epilogue(); + }); + } + +/** + * Inherit from `Base.prototype`. + */ + inherits(Landing, Base); + + Landing.description = 'Unicode landing strip'; + + }).call(this, require('_process')); + }, {'../runnable': 33, '../runner': 34, '../utils': 38, './base': 17, '_process': 70}], + 25: [function (require, module, exports) { + (function (process) { + 'use strict'; +/** + * @module List + */ +/** + * Module dependencies. + */ + + var Base = require('./base'); + var inherits = require('../utils').inherits; + var constants = require('../runner').constants; + var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN; + var EVENT_RUN_END = constants.EVENT_RUN_END; + var EVENT_TEST_BEGIN = constants.EVENT_TEST_BEGIN; + var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; + var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; + var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING; + var color = Base.color; + var cursor = Base.cursor; + +/** + * Expose `List`. + */ + + exports = module.exports = List; + +/** + * Constructs a new `List` reporter instance. + * + * @public + * @class + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function List (runner, options) { + Base.call(this, runner, options); + + var self = this; + var n = 0; + + runner.on(EVENT_RUN_BEGIN, function () { + Base.consoleLog(); + }); + + runner.on(EVENT_TEST_BEGIN, function (test) { + process.stdout.write(color('pass', ' ' + test.fullTitle() + ': ')); + }); + + runner.on(EVENT_TEST_PENDING, function (test) { + var fmt = color('checkmark', ' -') + color('pending', ' %s'); + Base.consoleLog(fmt, test.fullTitle()); + }); + + runner.on(EVENT_TEST_PASS, function (test) { + var fmt = + color('checkmark', ' ' + Base.symbols.ok) + + color('pass', ' %s: ') + + color(test.speed, '%dms'); + cursor.CR(); + Base.consoleLog(fmt, test.fullTitle(), test.duration); + }); + + runner.on(EVENT_TEST_FAIL, function (test) { + cursor.CR(); + Base.consoleLog(color('fail', ' %d) %s'), ++n, test.fullTitle()); + }); + + runner.once(EVENT_RUN_END, self.epilogue.bind(self)); + } + +/** + * Inherit from `Base.prototype`. + */ + inherits(List, Base); + + List.description = 'like "spec" reporter but flat'; + + }).call(this, require('_process')); + }, {'../runner': 34, '../utils': 38, './base': 17, '_process': 70}], + 26: [function (require, module, exports) { + (function (process) { + 'use strict'; +/** + * @module Markdown + */ +/** + * Module dependencies. + */ + + var Base = require('./base'); + var utils = require('../utils'); + var constants = require('../runner').constants; + var EVENT_RUN_END = constants.EVENT_RUN_END; + var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN; + var EVENT_SUITE_END = constants.EVENT_SUITE_END; + var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; + +/** + * Constants + */ + + var SUITE_PREFIX = '$'; + +/** + * Expose `Markdown`. + */ + + exports = module.exports = Markdown; + +/** + * Constructs a new `Markdown` reporter instance. + * + * @public + * @class + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function Markdown (runner, options) { + Base.call(this, runner, options); + + var level = 0; + var buf = ''; + + function title (str) { + return Array(level).join('#') + ' ' + str; + } + + function mapTOC (suite, obj) { + var ret = obj; + var key = SUITE_PREFIX + suite.title; + + obj = obj[key] = obj[key] || {suite: suite}; + suite.suites.forEach(function (suite) { + mapTOC(suite, obj); + }); + + return ret; + } + + function stringifyTOC (obj, level) { + ++level; + var buf = ''; + var link; + for (var key in obj) { + if (key === 'suite') { + continue; + } + if (key !== SUITE_PREFIX) { + link = ' - [' + key.substring(1) + ']'; + link += '(#' + utils.slug(obj[key].suite.fullTitle()) + ')\n'; + buf += Array(level).join(' ') + link; + } + buf += stringifyTOC(obj[key], level); + } + return buf; + } + + function generateTOC (suite) { + var obj = mapTOC(suite, {}); + return stringifyTOC(obj, 0); + } + + generateTOC(runner.suite); + + runner.on(EVENT_SUITE_BEGIN, function (suite) { + ++level; + var slug = utils.slug(suite.fullTitle()); + buf += '' + '\n'; + buf += title(suite.title) + '\n'; + }); + + runner.on(EVENT_SUITE_END, function () { + --level; + }); + + runner.on(EVENT_TEST_PASS, function (test) { + var code = utils.clean(test.body); + buf += test.title + '.\n'; + buf += '\n```js\n'; + buf += code + '\n'; + buf += '```\n\n'; + }); + + runner.once(EVENT_RUN_END, function () { + process.stdout.write('# TOC\n'); + process.stdout.write(generateTOC(runner.suite)); + process.stdout.write(buf); + }); + } + + Markdown.description = 'GitHub Flavored Markdown'; + + }).call(this, require('_process')); + }, {'../runner': 34, '../utils': 38, './base': 17, '_process': 70}], + 27: [function (require, module, exports) { + (function (process) { + 'use strict'; +/** + * @module Min + */ +/** + * Module dependencies. + */ + + var Base = require('./base'); + var inherits = require('../utils').inherits; + var constants = require('../runner').constants; + var EVENT_RUN_END = constants.EVENT_RUN_END; + var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN; + +/** + * Expose `Min`. + */ + + exports = module.exports = Min; + +/** + * Constructs a new `Min` reporter instance. + * + * @description + * This minimal test reporter is best used with '--watch'. + * + * @public + * @class + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function Min (runner, options) { + Base.call(this, runner, options); + + runner.on(EVENT_RUN_BEGIN, function () { + // clear screen + process.stdout.write('\u001b[2J'); + // set cursor position + process.stdout.write('\u001b[1;3H'); + }); + + runner.once(EVENT_RUN_END, this.epilogue.bind(this)); + } + +/** + * Inherit from `Base.prototype`. + */ + inherits(Min, Base); + + Min.description = 'essentially just a summary'; + + }).call(this, require('_process')); + }, {'../runner': 34, '../utils': 38, './base': 17, '_process': 70}], + 28: [function (require, module, exports) { + (function (process) { + 'use strict'; +/** + * @module Nyan + */ +/** + * Module dependencies. + */ + + var Base = require('./base'); + var constants = require('../runner').constants; + var inherits = require('../utils').inherits; + var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN; + var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING; + var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; + var EVENT_RUN_END = constants.EVENT_RUN_END; + var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; + +/** + * Expose `Dot`. + */ + + exports = module.exports = NyanCat; + +/** + * Constructs a new `Nyan` reporter instance. + * + * @public + * @class Nyan + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function NyanCat (runner, options) { + Base.call(this, runner, options); + + var self = this; + var width = (Base.window.width * 0.75) | 0; + var nyanCatWidth = (this.nyanCatWidth = 11); + + this.colorIndex = 0; + this.numberOfLines = 4; + this.rainbowColors = self.generateColors(); + this.scoreboardWidth = 5; + this.tick = 0; + this.trajectories = [[], [], [], []]; + this.trajectoryWidthMax = width - nyanCatWidth; + + runner.on(EVENT_RUN_BEGIN, function () { + Base.cursor.hide(); + self.draw(); + }); + + runner.on(EVENT_TEST_PENDING, function () { + self.draw(); + }); + + runner.on(EVENT_TEST_PASS, function () { + self.draw(); + }); + + runner.on(EVENT_TEST_FAIL, function () { + self.draw(); + }); + + runner.once(EVENT_RUN_END, function () { + Base.cursor.show(); + for (var i = 0; i < self.numberOfLines; i++) { + write('\n'); + } + self.epilogue(); + }); + } + +/** + * Inherit from `Base.prototype`. + */ + inherits(NyanCat, Base); + +/** + * Draw the nyan cat + * + * @private + */ + + NyanCat.prototype.draw = function () { + this.appendRainbow(); + this.drawScoreboard(); + this.drawRainbow(); + this.drawNyanCat(); + this.tick = !this.tick; + }; + +/** + * Draw the "scoreboard" showing the number + * of passes, failures and pending tests. + * + * @private + */ + + NyanCat.prototype.drawScoreboard = function () { + var stats = this.stats; + + function draw (type, n) { + write(' '); + write(Base.color(type, n)); + write('\n'); + } + + draw('green', stats.passes); + draw('fail', stats.failures); + draw('pending', stats.pending); + write('\n'); + + this.cursorUp(this.numberOfLines); + }; + +/** + * Append the rainbow. + * + * @private + */ + + NyanCat.prototype.appendRainbow = function () { + var segment = this.tick ? '_' : '-'; + var rainbowified = this.rainbowify(segment); + + for (var index = 0; index < this.numberOfLines; index++) { + var trajectory = this.trajectories[index]; + if (trajectory.length >= this.trajectoryWidthMax) { + trajectory.shift(); + } + trajectory.push(rainbowified); + } + }; + +/** + * Draw the rainbow. + * + * @private + */ + + NyanCat.prototype.drawRainbow = function () { + var self = this; + + this.trajectories.forEach(function (line) { + write('\u001b[' + self.scoreboardWidth + 'C'); + write(line.join('')); + write('\n'); + }); + + this.cursorUp(this.numberOfLines); + }; + +/** + * Draw the nyan cat + * + * @private + */ + NyanCat.prototype.drawNyanCat = function () { + var self = this; + var startWidth = this.scoreboardWidth + this.trajectories[0].length; + var dist = '\u001b[' + startWidth + 'C'; + var padding = ''; + + write(dist); + write('_,------,'); + write('\n'); + + write(dist); + padding = self.tick ? ' ' : ' '; + write('_|' + padding + '/\\_/\\ '); + write('\n'); + + write(dist); + padding = self.tick ? '_' : '__'; + var tail = self.tick ? '~' : '^'; + write(tail + '|' + padding + this.face() + ' '); + write('\n'); + + write(dist); + padding = self.tick ? ' ' : ' '; + write(padding + '"" "" '); + write('\n'); + + this.cursorUp(this.numberOfLines); + }; + +/** + * Draw nyan cat face. + * + * @private + * @return {string} + */ + + NyanCat.prototype.face = function () { + var stats = this.stats; + if (stats.failures) { + return '( x .x)'; + } else if (stats.pending) { + return '( o .o)'; + } else if (stats.passes) { + return '( ^ .^)'; + } + return '( - .-)'; + }; + +/** + * Move cursor up `n`. + * + * @private + * @param {number} n + */ + + NyanCat.prototype.cursorUp = function (n) { + write('\u001b[' + n + 'A'); + }; + +/** + * Move cursor down `n`. + * + * @private + * @param {number} n + */ + + NyanCat.prototype.cursorDown = function (n) { + write('\u001b[' + n + 'B'); + }; + +/** + * Generate rainbow colors. + * + * @private + * @return {Array} + */ + NyanCat.prototype.generateColors = function () { + var colors = []; + + for (var i = 0; i < 6 * 7; i++) { + var pi3 = Math.floor(Math.PI / 3); + var n = i * (1.0 / 6); + var r = Math.floor(3 * Math.sin(n) + 3); + var g = Math.floor(3 * Math.sin(n + 2 * pi3) + 3); + var b = Math.floor(3 * Math.sin(n + 4 * pi3) + 3); + colors.push(36 * r + 6 * g + b + 16); + } + + return colors; + }; + +/** + * Apply rainbow to the given `str`. + * + * @private + * @param {string} str + * @return {string} + */ + NyanCat.prototype.rainbowify = function (str) { + if (!Base.useColors) { + return str; + } + var color = this.rainbowColors[this.colorIndex % this.rainbowColors.length]; + this.colorIndex += 1; + return '\u001b[38;5;' + color + 'm' + str + '\u001b[0m'; + }; + +/** + * Stdout helper. + * + * @param {string} string A message to write to stdout. + */ + function write (string) { + process.stdout.write(string); + } + + NyanCat.description = '"nyan cat"'; + + }).call(this, require('_process')); + }, {'../runner': 34, '../utils': 38, './base': 17, '_process': 70}], + 29: [function (require, module, exports) { + (function (process) { + 'use strict'; +/** + * @module Progress + */ +/** + * Module dependencies. + */ + + var Base = require('./base'); + var constants = require('../runner').constants; + var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN; + var EVENT_TEST_END = constants.EVENT_TEST_END; + var EVENT_RUN_END = constants.EVENT_RUN_END; + var inherits = require('../utils').inherits; + var color = Base.color; + var cursor = Base.cursor; + +/** + * Expose `Progress`. + */ + + exports = module.exports = Progress; + +/** + * General progress bar color. + */ + + Base.colors.progress = 90; + +/** + * Constructs a new `Progress` reporter instance. + * + * @public + * @class + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function Progress (runner, options) { + Base.call(this, runner, options); + + var self = this; + var width = (Base.window.width * 0.5) | 0; + var total = runner.total; + var complete = 0; + var lastN = -1; + + // default chars + options = options || {}; + var reporterOptions = options.reporterOptions || {}; + + options.open = reporterOptions.open || '['; + options.complete = reporterOptions.complete || '▬'; + options.incomplete = reporterOptions.incomplete || Base.symbols.dot; + options.close = reporterOptions.close || ']'; + options.verbose = reporterOptions.verbose || false; + + // tests started + runner.on(EVENT_RUN_BEGIN, function () { + process.stdout.write('\n'); + cursor.hide(); + }); + + // tests complete + runner.on(EVENT_TEST_END, function () { + complete++; + + var percent = complete / total; + var n = (width * percent) | 0; + var i = width - n; + + if (n === lastN && !options.verbose) { + // Don't re-render the line if it hasn't changed + return; + } + lastN = n; + + cursor.CR(); + process.stdout.write('\u001b[J'); + process.stdout.write(color('progress', ' ' + options.open)); + process.stdout.write(Array(n).join(options.complete)); + process.stdout.write(Array(i).join(options.incomplete)); + process.stdout.write(color('progress', options.close)); + if (options.verbose) { + process.stdout.write(color('progress', ' ' + complete + ' of ' + total)); + } + }); + + // tests are complete, output some stats + // and the failures if any + runner.once(EVENT_RUN_END, function () { + cursor.show(); + process.stdout.write('\n'); + self.epilogue(); + }); + } + +/** + * Inherit from `Base.prototype`. + */ + inherits(Progress, Base); + + Progress.description = 'a progress bar'; + + }).call(this, require('_process')); + }, {'../runner': 34, '../utils': 38, './base': 17, '_process': 70}], + 30: [function (require, module, exports) { + 'use strict'; +/** + * @module Spec + */ +/** + * Module dependencies. + */ + + var Base = require('./base'); + var constants = require('../runner').constants; + var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN; + var EVENT_RUN_END = constants.EVENT_RUN_END; + var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN; + var EVENT_SUITE_END = constants.EVENT_SUITE_END; + var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; + var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; + var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING; + var inherits = require('../utils').inherits; + var color = Base.color; + +/** + * Expose `Spec`. + */ + + exports = module.exports = Spec; + +/** + * Constructs a new `Spec` reporter instance. + * + * @public + * @class + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function Spec (runner, options) { + Base.call(this, runner, options); + + var self = this; + var indents = 0; + var n = 0; + + function indent () { + return Array(indents).join(' '); + } + + runner.on(EVENT_RUN_BEGIN, function () { + Base.consoleLog(); + }); + + runner.on(EVENT_SUITE_BEGIN, function (suite) { + ++indents; + Base.consoleLog(color('suite', '%s%s'), indent(), suite.title); + }); + + runner.on(EVENT_SUITE_END, function () { + --indents; + if (indents === 1) { + Base.consoleLog(); + } + }); + + runner.on(EVENT_TEST_PENDING, function (test) { + var fmt = indent() + color('pending', ' - %s'); + Base.consoleLog(fmt, test.title); + }); + + runner.on(EVENT_TEST_PASS, function (test) { + var fmt; + if (test.speed === 'fast') { + fmt = + indent() + + color('checkmark', ' ' + Base.symbols.ok) + + color('pass', ' %s'); + Base.consoleLog(fmt, test.title); + } else { + fmt = + indent() + + color('checkmark', ' ' + Base.symbols.ok) + + color('pass', ' %s') + + color(test.speed, ' (%dms)'); + Base.consoleLog(fmt, test.title, test.duration); + } + }); + + runner.on(EVENT_TEST_FAIL, function (test) { + Base.consoleLog(indent() + color('fail', ' %d) %s'), ++n, test.title); + }); + + runner.once(EVENT_RUN_END, self.epilogue.bind(self)); + } + +/** + * Inherit from `Base.prototype`. + */ + inherits(Spec, Base); + + Spec.description = 'hierarchical & verbose [default]'; + + }, {'../runner': 34, '../utils': 38, './base': 17}], + 31: [function (require, module, exports) { + (function (process) { + 'use strict'; +/** + * @module TAP + */ +/** + * Module dependencies. + */ + + var util = require('util'); + var Base = require('./base'); + var constants = require('../runner').constants; + var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; + var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; + var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN; + var EVENT_RUN_END = constants.EVENT_RUN_END; + var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING; + var EVENT_TEST_END = constants.EVENT_TEST_END; + var inherits = require('../utils').inherits; + var sprintf = util.format; + +/** + * Expose `TAP`. + */ + + exports = module.exports = TAP; + +/** + * Constructs a new `TAP` reporter instance. + * + * @public + * @class + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function TAP (runner, options) { + Base.call(this, runner, options); + + var self = this; + var n = 1; + + var tapVersion = '12'; + if (options && options.reporterOptions) { + if (options.reporterOptions.tapVersion) { + tapVersion = options.reporterOptions.tapVersion.toString(); + } + } + + this._producer = createProducer(tapVersion); + + runner.once(EVENT_RUN_BEGIN, function () { + var ntests = runner.grepTotal(runner.suite); + self._producer.writeVersion(); + self._producer.writePlan(ntests); + }); + + runner.on(EVENT_TEST_END, function () { + ++n; + }); + + runner.on(EVENT_TEST_PENDING, function (test) { + self._producer.writePending(n, test); + }); + + runner.on(EVENT_TEST_PASS, function (test) { + self._producer.writePass(n, test); + }); + + runner.on(EVENT_TEST_FAIL, function (test, err) { + self._producer.writeFail(n, test, err); + }); + + runner.once(EVENT_RUN_END, function () { + self._producer.writeEpilogue(runner.stats); + }); + } + +/** + * Inherit from `Base.prototype`. + */ + inherits(TAP, Base); + +/** + * Returns a TAP-safe title of `test`. + * + * @private + * @param {Test} test - Test instance. + * @return {String} title with any hash character removed + */ + function title (test) { + return test.fullTitle().replace(/#/g, ''); + } + +/** + * Writes newline-terminated formatted string to reporter output stream. + * + * @private + * @param {string} format - `printf`-like format string + * @param {...*} [varArgs] - Format string arguments + */ + function println (format, varArgs) { + var vargs = Array.from(arguments); + vargs[0] += '\n'; + process.stdout.write(sprintf.apply(null, vargs)); + } + +/** + * Returns a `tapVersion`-appropriate TAP producer instance, if possible. + * + * @private + * @param {string} tapVersion - Version of TAP specification to produce. + * @returns {TAPProducer} specification-appropriate instance + * @throws {Error} if specification version has no associated producer. + */ + function createProducer (tapVersion) { + var producers = { + '12': new TAP12Producer(), + '13': new TAP13Producer() + }; + var producer = producers[tapVersion]; + + if (!producer) { + throw new Error( + 'invalid or unsupported TAP version: ' + JSON.stringify(tapVersion) + ); + } + + return producer; + } + +/** + * @summary + * Constructs a new TAPProducer. + * + * @description + * Only to be used as an abstract base class. + * + * @private + * @constructor + */ + function TAPProducer () {} + +/** + * Writes the TAP version to reporter output stream. + * + * @abstract + */ + TAPProducer.prototype.writeVersion = function () {}; + +/** + * Writes the plan to reporter output stream. + * + * @abstract + * @param {number} ntests - Number of tests that are planned to run. + */ + TAPProducer.prototype.writePlan = function (ntests) { + println('%d..%d', 1, ntests); + }; + +/** + * Writes that test passed to reporter output stream. + * + * @abstract + * @param {number} n - Index of test that passed. + * @param {Test} test - Instance containing test information. + */ + TAPProducer.prototype.writePass = function (n, test) { + println('ok %d %s', n, title(test)); + }; + +/** + * Writes that test was skipped to reporter output stream. + * + * @abstract + * @param {number} n - Index of test that was skipped. + * @param {Test} test - Instance containing test information. + */ + TAPProducer.prototype.writePending = function (n, test) { + println('ok %d %s # SKIP -', n, title(test)); + }; + +/** + * Writes that test failed to reporter output stream. + * + * @abstract + * @param {number} n - Index of test that failed. + * @param {Test} test - Instance containing test information. + * @param {Error} err - Reason the test failed. + */ + TAPProducer.prototype.writeFail = function (n, test, err) { + println('not ok %d %s', n, title(test)); + }; + +/** + * Writes the summary epilogue to reporter output stream. + * + * @abstract + * @param {Object} stats - Object containing run statistics. + */ + TAPProducer.prototype.writeEpilogue = function (stats) { + // :TBD: Why is this not counting pending tests? + println('# tests ' + (stats.passes + stats.failures)); + println('# pass ' + stats.passes); + // :TBD: Why are we not showing pending results? + println('# fail ' + stats.failures); + }; + +/** + * @summary + * Constructs a new TAP12Producer. + * + * @description + * Produces output conforming to the TAP12 specification. + * + * @private + * @constructor + * @extends TAPProducer + * @see {@link https://testanything.org/tap-specification.html|Specification} + */ + function TAP12Producer () { + /** + * Writes that test failed to reporter output stream, with error formatting. + * @override + */ + this.writeFail = function (n, test, err) { + TAPProducer.prototype.writeFail.call(this, n, test, err); + if (err.message) { + println(err.message.replace(/^/gm, ' ')); + } + if (err.stack) { + println(err.stack.replace(/^/gm, ' ')); + } + }; + } + +/** + * Inherit from `TAPProducer.prototype`. + */ + inherits(TAP12Producer, TAPProducer); + +/** + * @summary + * Constructs a new TAP13Producer. + * + * @description + * Produces output conforming to the TAP13 specification. + * + * @private + * @constructor + * @extends TAPProducer + * @see {@link https://testanything.org/tap-version-13-specification.html|Specification} + */ + function TAP13Producer () { + /** + * Writes the TAP version to reporter output stream. + * @override + */ + this.writeVersion = function () { + println('TAP version 13'); + }; + + /** + * Writes that test failed to reporter output stream, with error formatting. + * @override + */ + this.writeFail = function (n, test, err) { + TAPProducer.prototype.writeFail.call(this, n, test, err); + var emitYamlBlock = err.message != null || err.stack != null; + if (emitYamlBlock) { + println(indent(1) + '---'); + if (err.message) { + println(indent(2) + 'message: |-'); + println(err.message.replace(/^/gm, indent(3))); + } + if (err.stack) { + println(indent(2) + 'stack: |-'); + println(err.stack.replace(/^/gm, indent(3))); + } + println(indent(1) + '...'); + } + }; + + function indent (level) { + return Array(level + 1).join(' '); + } + } + +/** + * Inherit from `TAPProducer.prototype`. + */ + inherits(TAP13Producer, TAPProducer); + + TAP.description = 'TAP-compatible output'; + + }).call(this, require('_process')); + }, {'../runner': 34, '../utils': 38, './base': 17, '_process': 70, 'util': 90}], + 32: [function (require, module, exports) { + (function (process, global) { + 'use strict'; +/** + * @module XUnit + */ +/** + * Module dependencies. + */ + + var Base = require('./base'); + var utils = require('../utils'); + var fs = require('fs'); + var mkdirp = require('mkdirp'); + var path = require('path'); + var errors = require('../errors'); + var createUnsupportedError = errors.createUnsupportedError; + var constants = require('../runner').constants; + var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; + var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; + var EVENT_RUN_END = constants.EVENT_RUN_END; + var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING; + var STATE_FAILED = require('../runnable').constants.STATE_FAILED; + var inherits = utils.inherits; + var escape = utils.escape; + +/** + * Save timer references to avoid Sinon interfering (see GH-237). + */ + var Date = global.Date; + +/** + * Expose `XUnit`. + */ + + exports = module.exports = XUnit; + +/** + * Constructs a new `XUnit` reporter instance. + * + * @public + * @class + * @memberof Mocha.reporters + * @extends Mocha.reporters.Base + * @param {Runner} runner - Instance triggers reporter actions. + * @param {Object} [options] - runner options + */ + function XUnit (runner, options) { + Base.call(this, runner, options); + + var stats = this.stats; + var tests = []; + var self = this; + + // the name of the test suite, as it will appear in the resulting XML file + var suiteName; + + // the default name of the test suite if none is provided + var DEFAULT_SUITE_NAME = 'Mocha Tests'; + + if (options && options.reporterOptions) { + if (options.reporterOptions.output) { + if (!fs.createWriteStream) { + throw createUnsupportedError('file output not supported in browser'); + } + + mkdirp.sync(path.dirname(options.reporterOptions.output)); + self.fileStream = fs.createWriteStream(options.reporterOptions.output); + } + + // get the suite name from the reporter options (if provided) + suiteName = options.reporterOptions.suiteName; + } + + // fall back to the default suite name + suiteName = suiteName || DEFAULT_SUITE_NAME; + + runner.on(EVENT_TEST_PENDING, function (test) { + tests.push(test); + }); + + runner.on(EVENT_TEST_PASS, function (test) { + tests.push(test); + }); + + runner.on(EVENT_TEST_FAIL, function (test) { + tests.push(test); + }); + + runner.once(EVENT_RUN_END, function () { + self.write( + tag( + 'testsuite', + { + name: suiteName, + tests: stats.tests, + failures: 0, + errors: stats.failures, + skipped: stats.tests - stats.failures - stats.passes, + timestamp: new Date().toUTCString(), + time: stats.duration / 1000 || 0 + }, + false + ) + ); + + tests.forEach(function (t) { + self.test(t); + }); + + self.write(''); + }); + } + +/** + * Inherit from `Base.prototype`. + */ + inherits(XUnit, Base); + +/** + * Override done to close the stream (if it's a file). + * + * @param failures + * @param {Function} fn + */ + XUnit.prototype.done = function (failures, fn) { + if (this.fileStream) { + this.fileStream.end(function () { + fn(failures); + }); + } else { + fn(failures); + } + }; + +/** + * Write out the given line. + * + * @param {string} line + */ + XUnit.prototype.write = function (line) { + if (this.fileStream) { + this.fileStream.write(line + '\n'); + } else if (typeof process === 'object' && process.stdout) { + process.stdout.write(line + '\n'); + } else { + Base.consoleLog(line); + } + }; + +/** + * Output tag for the given `test.` + * + * @param {Test} test + */ + XUnit.prototype.test = function (test) { + Base.useColors = false; + + var attrs = { + classname: test.parent.fullTitle(), + name: test.title, + time: test.duration / 1000 || 0 + }; + + if (test.state === STATE_FAILED) { + var err = test.err; + var diff = + Base.hideDiff || !err.actual || !err.expected + ? '' + : '\n' + Base.generateDiff(err.actual, err.expected); + this.write( + tag( + 'testcase', + attrs, + false, + tag( + 'failure', + {}, + false, + escape(err.message) + escape(diff) + '\n' + escape(err.stack) + ) + ) + ); + } else if (test.isPending()) { + this.write(tag('testcase', attrs, false, tag('skipped', {}, true))); + } else { + this.write(tag('testcase', attrs, true)); + } + }; + +/** + * HTML tag helper. + * + * @param name + * @param attrs + * @param close + * @param content + * @return {string} + */ + function tag (name, attrs, close, content) { + var end = close ? '/>' : '>'; + var pairs = []; + var tag; + + for (var key in attrs) { + if (Object.prototype.hasOwnProperty.call(attrs, key)) { + pairs.push(key + '="' + escape(attrs[key]) + '"'); + } + } + + tag = '<' + name + (pairs.length ? ' ' + pairs.join(' ') : '') + end; + if (content) { + tag += content + '0, 2^31-1]. + * If clamped value matches either range endpoint, timeouts will be disabled. + * + * @private + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Maximum_delay_value} + * @param {number|string} ms - Timeout threshold value. + * @returns {Runnable} this + * @chainable + */ + Runnable.prototype.timeout = function (ms) { + if (!arguments.length) { + return this._timeout; + } + if (typeof ms === 'string') { + ms = milliseconds(ms); + } + + // Clamp to range + var INT_MAX = Math.pow(2, 31) - 1; + var range = [0, INT_MAX]; + ms = utils.clamp(ms, range); + + // see #1652 for reasoning + if (ms === range[0] || ms === range[1]) { + this._enableTimeouts = false; + } + debug('timeout %d', ms); + this._timeout = ms; + if (this.timer) { + this.resetTimeout(); + } + return this; + }; + +/** + * Set or get slow `ms`. + * + * @private + * @param {number|string} ms + * @return {Runnable|number} ms or Runnable instance. + */ + Runnable.prototype.slow = function (ms) { + if (!arguments.length || typeof ms === 'undefined') { + return this._slow; + } + if (typeof ms === 'string') { + ms = milliseconds(ms); + } + debug('slow %d', ms); + this._slow = ms; + return this; + }; + +/** + * Set and get whether timeout is `enabled`. + * + * @private + * @param {boolean} enabled + * @return {Runnable|boolean} enabled or Runnable instance. + */ + Runnable.prototype.enableTimeouts = function (enabled) { + if (!arguments.length) { + return this._enableTimeouts; + } + debug('enableTimeouts %s', enabled); + this._enableTimeouts = enabled; + return this; + }; + +/** + * Halt and mark as pending. + * + * @memberof Mocha.Runnable + * @public + */ + Runnable.prototype.skip = function () { + throw new Pending('sync skip'); + }; + +/** + * Check if this runnable or its parent suite is marked as pending. + * + * @private + */ + Runnable.prototype.isPending = function () { + return this.pending || (this.parent && this.parent.isPending()); + }; + +/** + * Return `true` if this Runnable has failed. + * @return {boolean} + * @private + */ + Runnable.prototype.isFailed = function () { + return !this.isPending() && this.state === constants.STATE_FAILED; + }; + +/** + * Return `true` if this Runnable has passed. + * @return {boolean} + * @private + */ + Runnable.prototype.isPassed = function () { + return !this.isPending() && this.state === constants.STATE_PASSED; + }; + +/** + * Set or get number of retries. + * + * @private + */ + Runnable.prototype.retries = function (n) { + if (!arguments.length) { + return this._retries; + } + this._retries = n; + }; + +/** + * Set or get current retry + * + * @private + */ + Runnable.prototype.currentRetry = function (n) { + if (!arguments.length) { + return this._currentRetry; + } + this._currentRetry = n; + }; + +/** + * Return the full title generated by recursively concatenating the parent's + * full title. + * + * @memberof Mocha.Runnable + * @public + * @return {string} + */ + Runnable.prototype.fullTitle = function () { + return this.titlePath().join(' '); + }; + +/** + * Return the title path generated by concatenating the parent's title path with the title. + * + * @memberof Mocha.Runnable + * @public + * @return {string} + */ + Runnable.prototype.titlePath = function () { + return this.parent.titlePath().concat([this.title]); + }; + +/** + * Clear the timeout. + * + * @private + */ + Runnable.prototype.clearTimeout = function () { + clearTimeout(this.timer); + }; + +/** + * Inspect the runnable void of private properties. + * + * @private + * @return {string} + */ + Runnable.prototype.inspect = function () { + return JSON.stringify( + this, + function (key, val) { + if (key[0] === '_') { + return; + } + if (key === 'parent') { + return '#'; + } + if (key === 'ctx') { + return '#'; + } + return val; + }, + 2 + ); + }; + +/** + * Reset the timeout. + * + * @private + */ + Runnable.prototype.resetTimeout = function () { + var self = this; + var ms = this.timeout() || 1e9; + + if (!this._enableTimeouts) { + return; + } + this.clearTimeout(); + this.timer = setTimeout(function () { + if (!self._enableTimeouts) { + return; + } + self.callback(self._timeoutError(ms)); + self.timedOut = true; + }, ms); + }; + +/** + * Set or get a list of whitelisted globals for this test run. + * + * @private + * @param {string[]} globals + */ + Runnable.prototype.globals = function (globals) { + if (!arguments.length) { + return this._allowedGlobals; + } + this._allowedGlobals = globals; + }; + +/** + * Run the test and invoke `fn(err)`. + * + * @param {Function} fn + * @private + */ + Runnable.prototype.run = function (fn) { + var self = this; + var start = new Date(); + var ctx = this.ctx; + var finished; + var emitted; + + // Sometimes the ctx exists, but it is not runnable + if (ctx && ctx.runnable) { + ctx.runnable(this); + } + + // called multiple times + function multiple (err) { + if (emitted) { + return; + } + emitted = true; + var msg = 'done() called multiple times'; + if (err && err.message) { + err.message += " (and Mocha's " + msg + ')'; + self.emit('error', err); + } else { + self.emit('error', new Error(msg)); + } + } + + // finished + function done (err) { + var ms = self.timeout(); + if (self.timedOut) { + return; + } + + if (finished) { + return multiple(err); + } + + self.clearTimeout(); + self.duration = new Date() - start; + finished = true; + if (!err && self.duration > ms && self._enableTimeouts) { + err = self._timeoutError(ms); + } + fn(err); + } + + // for .resetTimeout() + this.callback = done; + + // explicit async with `done` argument + if (this.async) { + this.resetTimeout(); + + // allows skip() to be used in an explicit async context + this.skip = function asyncSkip () { + done(new Pending('async skip call')); + // halt execution. the Runnable will be marked pending + // by the previous call, and the uncaught handler will ignore + // the failure. + throw new Pending('async skip; aborting execution'); + }; + + if (this.allowUncaught) { + return callFnAsync(this.fn); + } + try { + callFnAsync(this.fn); + } catch (err) { + emitted = true; + done(Runnable.toValueOrError(err)); + } + return; + } + + if (this.allowUncaught) { + if (this.isPending()) { + done(); + } else { + callFn(this.fn); + } + return; + } + + // sync or promise-returning + try { + if (this.isPending()) { + done(); + } else { + callFn(this.fn); + } + } catch (err) { + emitted = true; + done(Runnable.toValueOrError(err)); + } + + function callFn (fn) { + var result = fn.call(ctx); + if (result && typeof result.then === 'function') { + self.resetTimeout(); + result.then( + function () { + done(); + // Return null so libraries like bluebird do not warn about + // subsequently constructed Promises. + return null; + }, + function (reason) { + done(reason || new Error('Promise rejected with no or falsy reason')); + } + ); + } else { + if (self.asyncOnly) { + return done( + new Error( + '--async-only option in use without declaring `done()` or returning a promise' + ) + ); + } + + done(); + } + } + + function callFnAsync (fn) { + var result = fn.call(ctx, function (err) { + if (err instanceof Error || toString.call(err) === '[object Error]') { + return done(err); + } + if (err) { + if (Object.prototype.toString.call(err) === '[object Object]') { + return done( + new Error('done() invoked with non-Error: ' + JSON.stringify(err)) + ); + } + return done(new Error('done() invoked with non-Error: ' + err)); + } + if (result && utils.isPromise(result)) { + return done( + new Error( + 'Resolution method is overspecified. Specify a callback *or* return a Promise; not both.' + ) + ); + } + + done(); + }); + } + }; + +/** + * Instantiates a "timeout" error + * + * @param {number} ms - Timeout (in milliseconds) + * @returns {Error} a "timeout" error + * @private + */ + Runnable.prototype._timeoutError = function (ms) { + var msg = + 'Timeout of ' + + ms + + 'ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.'; + if (this.file) { + msg += ' (' + this.file + ')'; + } + return new Error(msg); + }; + + var constants = utils.defineConstants( + /** + * {@link Runnable}-related constants. + * @public + * @memberof Runnable + * @readonly + * @static + * @alias constants + * @enum {string} + */ + { + /** + * Value of `state` prop when a `Runnable` has failed + */ + STATE_FAILED: 'failed', + /** + * Value of `state` prop when a `Runnable` has passed + */ + STATE_PASSED: 'passed' + } +); + +/** + * Given `value`, return identity if truthy, otherwise create an "invalid exception" error and return that. + * @param {*} [value] - Value to return, if present + * @returns {*|Error} `value`, otherwise an `Error` + * @private + */ + Runnable.toValueOrError = function (value) { + return ( + value || + createInvalidExceptionError( + 'Runnable failed with falsy or undefined exception. Please throw an Error instead.', + value + ) + ); + }; + + Runnable.constants = constants; + + }).call(this, typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}); + }, {'./errors': 6, './pending': 16, './utils': 38, 'debug': 45, 'events': 50, 'ms': 60}], + 34: [function (require, module, exports) { + (function (process, global) { + 'use strict'; + +/** + * Module dependencies. + */ + var util = require('util'); + var EventEmitter = require('events').EventEmitter; + var Pending = require('./pending'); + var utils = require('./utils'); + var inherits = utils.inherits; + var debug = require('debug')('mocha:runner'); + var Runnable = require('./runnable'); + var Suite = require('./suite'); + var HOOK_TYPE_BEFORE_EACH = Suite.constants.HOOK_TYPE_BEFORE_EACH; + var HOOK_TYPE_AFTER_EACH = Suite.constants.HOOK_TYPE_AFTER_EACH; + var HOOK_TYPE_AFTER_ALL = Suite.constants.HOOK_TYPE_AFTER_ALL; + var HOOK_TYPE_BEFORE_ALL = Suite.constants.HOOK_TYPE_BEFORE_ALL; + var EVENT_ROOT_SUITE_RUN = Suite.constants.EVENT_ROOT_SUITE_RUN; + var STATE_FAILED = Runnable.constants.STATE_FAILED; + var STATE_PASSED = Runnable.constants.STATE_PASSED; + var dQuote = utils.dQuote; + var ngettext = utils.ngettext; + var sQuote = utils.sQuote; + var stackFilter = utils.stackTraceFilter(); + var stringify = utils.stringify; + var type = utils.type; + var createInvalidExceptionError = require('./errors') + .createInvalidExceptionError; + +/** + * Non-enumerable globals. + * @readonly + */ + var globals = [ + 'setTimeout', + 'clearTimeout', + 'setInterval', + 'clearInterval', + 'XMLHttpRequest', + 'Date', + 'setImmediate', + 'clearImmediate' + ]; + + var constants = utils.defineConstants( + /** + * {@link Runner}-related constants. + * @public + * @memberof Runner + * @readonly + * @alias constants + * @static + * @enum {string} + */ + { + /** + * Emitted when {@link Hook} execution begins + */ + EVENT_HOOK_BEGIN: 'hook', + /** + * Emitted when {@link Hook} execution ends + */ + EVENT_HOOK_END: 'hook end', + /** + * Emitted when Root {@link Suite} execution begins (all files have been parsed and hooks/tests are ready for execution) + */ + EVENT_RUN_BEGIN: 'start', + /** + * Emitted when Root {@link Suite} execution has been delayed via `delay` option + */ + EVENT_DELAY_BEGIN: 'waiting', + /** + * Emitted when delayed Root {@link Suite} execution is triggered by user via `global.run()` + */ + EVENT_DELAY_END: 'ready', + /** + * Emitted when Root {@link Suite} execution ends + */ + EVENT_RUN_END: 'end', + /** + * Emitted when {@link Suite} execution begins + */ + EVENT_SUITE_BEGIN: 'suite', + /** + * Emitted when {@link Suite} execution ends + */ + EVENT_SUITE_END: 'suite end', + /** + * Emitted when {@link Test} execution begins + */ + EVENT_TEST_BEGIN: 'test', + /** + * Emitted when {@link Test} execution ends + */ + EVENT_TEST_END: 'test end', + /** + * Emitted when {@link Test} execution fails + */ + EVENT_TEST_FAIL: 'fail', + /** + * Emitted when {@link Test} execution succeeds + */ + EVENT_TEST_PASS: 'pass', + /** + * Emitted when {@link Test} becomes pending + */ + EVENT_TEST_PENDING: 'pending', + /** + * Emitted when {@link Test} execution has failed, but will retry + */ + EVENT_TEST_RETRY: 'retry' + } +); + + module.exports = Runner; + +/** + * Initialize a `Runner` at the Root {@link Suite}, which represents a hierarchy of {@link Suite|Suites} and {@link Test|Tests}. + * + * @extends external:EventEmitter + * @public + * @class + * @param {Suite} suite Root suite + * @param {boolean} [delay] Whether or not to delay execution of root suite + * until ready. + */ + function Runner (suite, delay) { + var self = this; + this._globals = []; + this._abort = false; + this._delay = delay; + this.suite = suite; + this.started = false; + this.total = suite.total(); + this.failures = 0; + this.on(constants.EVENT_TEST_END, function (test) { + self.checkGlobals(test); + }); + this.on(constants.EVENT_HOOK_END, function (hook) { + self.checkGlobals(hook); + }); + this._defaultGrep = /.*/; + this.grep(this._defaultGrep); + this.globals(this.globalProps()); + } + +/** + * Wrapper for setImmediate, process.nextTick, or browser polyfill. + * + * @param {Function} fn + * @private + */ + Runner.immediately = global.setImmediate || process.nextTick; + +/** + * Inherit from `EventEmitter.prototype`. + */ + inherits(Runner, EventEmitter); + +/** + * Run tests with full titles matching `re`. Updates runner.total + * with number of tests matched. + * + * @public + * @memberof Runner + * @param {RegExp} re + * @param {boolean} invert + * @return {Runner} Runner instance. + */ + Runner.prototype.grep = function (re, invert) { + debug('grep %s', re); + this._grep = re; + this._invert = invert; + this.total = this.grepTotal(this.suite); + return this; + }; + +/** + * Returns the number of tests matching the grep search for the + * given suite. + * + * @memberof Runner + * @public + * @param {Suite} suite + * @return {number} + */ + Runner.prototype.grepTotal = function (suite) { + var self = this; + var total = 0; + + suite.eachTest(function (test) { + var match = self._grep.test(test.fullTitle()); + if (self._invert) { + match = !match; + } + if (match) { + total++; + } + }); + + return total; + }; + +/** + * Return a list of global properties. + * + * @return {Array} + * @private + */ + Runner.prototype.globalProps = function () { + var props = Object.keys(global); + + // non-enumerables + for (var i = 0; i < globals.length; ++i) { + if (~props.indexOf(globals[i])) { + continue; + } + props.push(globals[i]); + } + + return props; + }; + +/** + * Allow the given `arr` of globals. + * + * @public + * @memberof Runner + * @param {Array} arr + * @return {Runner} Runner instance. + */ + Runner.prototype.globals = function (arr) { + if (!arguments.length) { + return this._globals; + } + debug('globals %j', arr); + this._globals = this._globals.concat(arr); + return this; + }; + +/** + * Check for global variable leaks. + * + * @private + */ + Runner.prototype.checkGlobals = function (test) { + if (!this.checkLeaks) { + return; + } + var ok = this._globals; + + var globals = this.globalProps(); + var leaks; + + if (test) { + ok = ok.concat(test._allowedGlobals || []); + } + + if (this.prevGlobalsLength === globals.length) { + return; + } + this.prevGlobalsLength = globals.length; + + leaks = filterLeaks(ok, globals); + this._globals = this._globals.concat(leaks); + + if (leaks.length) { + var format = ngettext( + leaks.length, + 'global leak detected: %s', + 'global leaks detected: %s' + ); + var error = new Error(util.format(format, leaks.map(sQuote).join(', '))); + this.fail(test, error); + } + }; + +/** + * Fail the given `test`. + * + * @private + * @param {Test} test + * @param {Error} err + */ + Runner.prototype.fail = function (test, err) { + if (test.isPending()) { + return; + } + + ++this.failures; + test.state = STATE_FAILED; + + if (!isError(err)) { + err = thrown2Error(err); + } + + try { + err.stack = + this.fullStackTrace || !err.stack ? err.stack : stackFilter(err.stack); + } catch (ignore) { + // some environments do not take kindly to monkeying with the stack + } + + this.emit(constants.EVENT_TEST_FAIL, test, err); + }; + +/** + * Fail the given `hook` with `err`. + * + * Hook failures work in the following pattern: + * - If bail, run corresponding `after each` and `after` hooks, + * then exit + * - Failed `before` hook skips all tests in a suite and subsuites, + * but jumps to corresponding `after` hook + * - Failed `before each` hook skips remaining tests in a + * suite and jumps to corresponding `after each` hook, + * which is run only once + * - Failed `after` hook does not alter + * execution order + * - Failed `after each` hook skips remaining tests in a + * suite and subsuites, but executes other `after each` + * hooks + * + * @private + * @param {Hook} hook + * @param {Error} err + */ + Runner.prototype.failHook = function (hook, err) { + hook.originalTitle = hook.originalTitle || hook.title; + if (hook.ctx && hook.ctx.currentTest) { + hook.title = + hook.originalTitle + ' for ' + dQuote(hook.ctx.currentTest.title); + } else { + var parentTitle; + if (hook.parent.title) { + parentTitle = hook.parent.title; + } else { + parentTitle = hook.parent.root ? '{root}' : ''; + } + hook.title = hook.originalTitle + ' in ' + dQuote(parentTitle); + } + + this.fail(hook, err); + }; + +/** + * Run hook `name` callbacks and then invoke `fn()`. + * + * @private + * @param {string} name + * @param {Function} fn + */ + + Runner.prototype.hook = function (name, fn) { + var suite = this.suite; + var hooks = suite.getHooks(name); + var self = this; + + function next (i) { + var hook = hooks[i]; + if (!hook) { + return fn(); + } + self.currentRunnable = hook; + + if (name === HOOK_TYPE_BEFORE_ALL) { + hook.ctx.currentTest = hook.parent.tests[0]; + } else if (name === HOOK_TYPE_AFTER_ALL) { + hook.ctx.currentTest = hook.parent.tests[hook.parent.tests.length - 1]; + } else { + hook.ctx.currentTest = self.test; + } + + hook.allowUncaught = self.allowUncaught; + + self.emit(constants.EVENT_HOOK_BEGIN, hook); + + if (!hook.listeners('error').length) { + hook.on('error', function (err) { + self.failHook(hook, err); + }); + } + + hook.run(function (err) { + var testError = hook.error(); + if (testError) { + self.fail(self.test, testError); + } + if (err) { + if (err instanceof Pending) { + if (name === HOOK_TYPE_AFTER_ALL) { + utils.deprecate( + 'Skipping a test within an "after all" hook is DEPRECATED and will throw an exception in a future version of Mocha. ' + + 'Use a return statement or other means to abort hook execution.' + ); + } + if (name === HOOK_TYPE_BEFORE_EACH || name === HOOK_TYPE_AFTER_EACH) { + if (self.test) { + self.test.pending = true; + } + } else { + suite.tests.forEach(function (test) { + test.pending = true; + }); + suite.suites.forEach(function (suite) { + suite.pending = true; + }); + // a pending hook won't be executed twice. + hook.pending = true; + } + } else { + self.failHook(hook, err); + + // stop executing hooks, notify callee of hook err + return fn(err); + } + } + self.emit(constants.EVENT_HOOK_END, hook); + delete hook.ctx.currentTest; + next(++i); + }); + } + + Runner.immediately(function () { + next(0); + }); + }; + +/** + * Run hook `name` for the given array of `suites` + * in order, and callback `fn(err, errSuite)`. + * + * @private + * @param {string} name + * @param {Array} suites + * @param {Function} fn + */ + Runner.prototype.hooks = function (name, suites, fn) { + var self = this; + var orig = this.suite; + + function next (suite) { + self.suite = suite; + + if (!suite) { + self.suite = orig; + return fn(); + } + + self.hook(name, function (err) { + if (err) { + var errSuite = self.suite; + self.suite = orig; + return fn(err, errSuite); + } + + next(suites.pop()); + }); + } + + next(suites.pop()); + }; + +/** + * Run hooks from the top level down. + * + * @param {String} name + * @param {Function} fn + * @private + */ + Runner.prototype.hookUp = function (name, fn) { + var suites = [this.suite].concat(this.parents()).reverse(); + this.hooks(name, suites, fn); + }; + +/** + * Run hooks from the bottom up. + * + * @param {String} name + * @param {Function} fn + * @private + */ + Runner.prototype.hookDown = function (name, fn) { + var suites = [this.suite].concat(this.parents()); + this.hooks(name, suites, fn); + }; + +/** + * Return an array of parent Suites from + * closest to furthest. + * + * @return {Array} + * @private + */ + Runner.prototype.parents = function () { + var suite = this.suite; + var suites = []; + while (suite.parent) { + suite = suite.parent; + suites.push(suite); + } + return suites; + }; + +/** + * Run the current test and callback `fn(err)`. + * + * @param {Function} fn + * @private + */ + Runner.prototype.runTest = function (fn) { + var self = this; + var test = this.test; + + if (!test) { + return; + } + + var suite = this.parents().reverse()[0] || this.suite; + if (this.forbidOnly && suite.hasOnly()) { + fn(new Error('`.only` forbidden')); + return; + } + if (this.asyncOnly) { + test.asyncOnly = true; + } + test.on('error', function (err) { + self.fail(test, err); + }); + if (this.allowUncaught) { + test.allowUncaught = true; + return test.run(fn); + } + try { + test.run(fn); + } catch (err) { + fn(err); + } + }; + +/** + * Run tests in the given `suite` and invoke the callback `fn()` when complete. + * + * @private + * @param {Suite} suite + * @param {Function} fn + */ + Runner.prototype.runTests = function (suite, fn) { + var self = this; + var tests = suite.tests.slice(); + var test; + + function hookErr (_, errSuite, after) { + // before/after Each hook for errSuite failed: + var orig = self.suite; + + // for failed 'after each' hook start from errSuite parent, + // otherwise start from errSuite itself + self.suite = after ? errSuite.parent : errSuite; + + if (self.suite) { + // call hookUp afterEach + self.hookUp(HOOK_TYPE_AFTER_EACH, function (err2, errSuite2) { + self.suite = orig; + // some hooks may fail even now + if (err2) { + return hookErr(err2, errSuite2, true); + } + // report error suite + fn(errSuite); + }); + } else { + // there is no need calling other 'after each' hooks + self.suite = orig; + fn(errSuite); + } + } + + function next (err, errSuite) { + // if we bail after first err + if (self.failures && suite._bail) { + tests = []; + } + + if (self._abort) { + return fn(); + } + + if (err) { + return hookErr(err, errSuite, true); + } + + // next test + test = tests.shift(); + + // all done + if (!test) { + return fn(); + } + + // grep + var match = self._grep.test(test.fullTitle()); + if (self._invert) { + match = !match; + } + if (!match) { + // Run immediately only if we have defined a grep. When we + // define a grep — It can cause maximum callstack error if + // the grep is doing a large recursive loop by neglecting + // all tests. The run immediately function also comes with + // a performance cost. So we don't want to run immediately + // if we run the whole test suite, because running the whole + // test suite don't do any immediate recursive loops. Thus, + // allowing a JS runtime to breathe. + if (self._grep !== self._defaultGrep) { + Runner.immediately(next); + } else { + next(); + } + return; + } + + if (test.isPending()) { + if (self.forbidPending) { + test.isPending = alwaysFalse; + self.fail(test, new Error('Pending test forbidden')); + delete test.isPending; + } else { + self.emit(constants.EVENT_TEST_PENDING, test); + } + self.emit(constants.EVENT_TEST_END, test); + return next(); + } + + // execute test and hook(s) + self.emit(constants.EVENT_TEST_BEGIN, (self.test = test)); + self.hookDown(HOOK_TYPE_BEFORE_EACH, function (err, errSuite) { + if (test.isPending()) { + if (self.forbidPending) { + test.isPending = alwaysFalse; + self.fail(test, new Error('Pending test forbidden')); + delete test.isPending; + } else { + self.emit(constants.EVENT_TEST_PENDING, test); + } + self.emit(constants.EVENT_TEST_END, test); + return next(); + } + if (err) { + return hookErr(err, errSuite, false); + } + self.currentRunnable = self.test; + self.runTest(function (err) { + test = self.test; + if (err) { + var retry = test.currentRetry(); + if (err instanceof Pending && self.forbidPending) { + self.fail(test, new Error('Pending test forbidden')); + } else if (err instanceof Pending) { + test.pending = true; + self.emit(constants.EVENT_TEST_PENDING, test); + } else if (retry < test.retries()) { + var clonedTest = test.clone(); + clonedTest.currentRetry(retry + 1); + tests.unshift(clonedTest); + + self.emit(constants.EVENT_TEST_RETRY, test, err); + + // Early return + hook trigger so that it doesn't + // increment the count wrong + return self.hookUp(HOOK_TYPE_AFTER_EACH, next); + } else { + self.fail(test, err); + } + self.emit(constants.EVENT_TEST_END, test); + return self.hookUp(HOOK_TYPE_AFTER_EACH, next); + } + + test.state = STATE_PASSED; + self.emit(constants.EVENT_TEST_PASS, test); + self.emit(constants.EVENT_TEST_END, test); + self.hookUp(HOOK_TYPE_AFTER_EACH, next); + }); + }); + } + + this.next = next; + this.hookErr = hookErr; + next(); + }; + + function alwaysFalse () { + return false; + } + +/** + * Run the given `suite` and invoke the callback `fn()` when complete. + * + * @private + * @param {Suite} suite + * @param {Function} fn + */ + Runner.prototype.runSuite = function (suite, fn) { + var i = 0; + var self = this; + var total = this.grepTotal(suite); + var afterAllHookCalled = false; + + debug('run suite %s', suite.fullTitle()); + + if (!total || (self.failures && suite._bail)) { + return fn(); + } + + this.emit(constants.EVENT_SUITE_BEGIN, (this.suite = suite)); + + function next (errSuite) { + if (errSuite) { + // current suite failed on a hook from errSuite + if (errSuite === suite) { + // if errSuite is current suite + // continue to the next sibling suite + return done(); + } + // errSuite is among the parents of current suite + // stop execution of errSuite and all sub-suites + return done(errSuite); + } + + if (self._abort) { + return done(); + } + + var curr = suite.suites[i++]; + if (!curr) { + return done(); + } + + // Avoid grep neglecting large number of tests causing a + // huge recursive loop and thus a maximum call stack error. + // See comment in `this.runTests()` for more information. + if (self._grep !== self._defaultGrep) { + Runner.immediately(function () { + self.runSuite(curr, next); + }); + } else { + self.runSuite(curr, next); + } + } + + function done (errSuite) { + self.suite = suite; + self.nextSuite = next; + + if (afterAllHookCalled) { + fn(errSuite); + } else { + // mark that the afterAll block has been called once + // and so can be skipped if there is an error in it. + afterAllHookCalled = true; + + // remove reference to test + delete self.test; + + self.hook(HOOK_TYPE_AFTER_ALL, function () { + self.emit(constants.EVENT_SUITE_END, suite); + fn(errSuite); + }); + } + } + + this.nextSuite = next; + + this.hook(HOOK_TYPE_BEFORE_ALL, function (err) { + if (err) { + return done(); + } + self.runTests(suite, next); + }); + }; + +/** + * Handle uncaught exceptions. + * + * @param {Error} err + * @private + */ + Runner.prototype.uncaught = function (err) { + if (err instanceof Pending) { + return; + } + if (err) { + debug('uncaught exception %O', err); + } else { + debug('uncaught undefined/falsy exception'); + err = createInvalidExceptionError( + 'Caught falsy/undefined exception which would otherwise be uncaught. No stack trace found; try a debugger', + err + ); + } + + if (!isError(err)) { + err = thrown2Error(err); + } + err.uncaught = true; + + var runnable = this.currentRunnable; + + if (!runnable) { + runnable = new Runnable('Uncaught error outside test suite'); + runnable.parent = this.suite; + + if (this.started) { + this.fail(runnable, err); + } else { + // Can't recover from this failure + this.emit(constants.EVENT_RUN_BEGIN); + this.fail(runnable, err); + this.emit(constants.EVENT_RUN_END); + } + + return; + } + + runnable.clearTimeout(); + + // Ignore errors if already failed or pending + // See #3226 + if (runnable.isFailed() || runnable.isPending()) { + return; + } + // we cannot recover gracefully if a Runnable has already passed + // then fails asynchronously + var alreadyPassed = runnable.isPassed(); + // this will change the state to "failed" regardless of the current value + this.fail(runnable, err); + if (!alreadyPassed) { + // recover from test + if (runnable.type === constants.EVENT_TEST_BEGIN) { + this.emit(constants.EVENT_TEST_END, runnable); + this.hookUp(HOOK_TYPE_AFTER_EACH, this.next); + return; + } + debug(runnable); + + // recover from hooks + var errSuite = this.suite; + + // XXX how about a less awful way to determine this? + // if hook failure is in afterEach block + if (runnable.fullTitle().indexOf('after each') > -1) { + return this.hookErr(err, errSuite, true); + } + // if hook failure is in beforeEach block + if (runnable.fullTitle().indexOf('before each') > -1) { + return this.hookErr(err, errSuite, false); + } + // if hook failure is in after or before blocks + return this.nextSuite(errSuite); + } + + // bail + this.emit(constants.EVENT_RUN_END); + }; + +/** + * Run the root suite and invoke `fn(failures)` + * on completion. + * + * @public + * @memberof Runner + * @param {Function} fn + * @return {Runner} Runner instance. + */ + Runner.prototype.run = function (fn) { + var self = this; + var rootSuite = this.suite; + + fn = fn || function () {}; + + function uncaught (err) { + self.uncaught(err); + } + + function start () { + // If there is an `only` filter + if (rootSuite.hasOnly()) { + rootSuite.filterOnly(); + } + self.started = true; + if (self._delay) { + self.emit(constants.EVENT_DELAY_END); + } + self.emit(constants.EVENT_RUN_BEGIN); + + self.runSuite(rootSuite, function () { + debug('finished running'); + self.emit(constants.EVENT_RUN_END); + }); + } + + debug(constants.EVENT_RUN_BEGIN); + + // references cleanup to avoid memory leaks + this.on(constants.EVENT_SUITE_END, function (suite) { + suite.cleanReferences(); + }); + + // callback + this.on(constants.EVENT_RUN_END, function () { + debug(constants.EVENT_RUN_END); + process.removeListener('uncaughtException', uncaught); + fn(self.failures); + }); + + // uncaught exception + process.on('uncaughtException', uncaught); + + if (this._delay) { + // for reporters, I guess. + // might be nice to debounce some dots while we wait. + this.emit(constants.EVENT_DELAY_BEGIN, rootSuite); + rootSuite.once(EVENT_ROOT_SUITE_RUN, start); + } else { + start(); + } + + return this; + }; + +/** + * Cleanly abort execution. + * + * @memberof Runner + * @public + * @return {Runner} Runner instance. + */ + Runner.prototype.abort = function () { + debug('aborting'); + this._abort = true; + + return this; + }; + +/** + * Filter leaks with the given globals flagged as `ok`. + * + * @private + * @param {Array} ok + * @param {Array} globals + * @return {Array} + */ + function filterLeaks (ok, globals) { + return globals.filter(function (key) { + // Firefox and Chrome exposes iframes as index inside the window object + if (/^\d+/.test(key)) { + return false; + } + + // in firefox + // if runner runs in an iframe, this iframe's window.getInterface method + // not init at first it is assigned in some seconds + if (global.navigator && /^getInterface/.test(key)) { + return false; + } + + // an iframe could be approached by window[iframeIndex] + // in ie6,7,8 and opera, iframeIndex is enumerable, this could cause leak + if (global.navigator && /^\d+/.test(key)) { + return false; + } + + // Opera and IE expose global variables for HTML element IDs (issue #243) + if (/^mocha-/.test(key)) { + return false; + } + + var matched = ok.filter(function (ok) { + if (~ok.indexOf('*')) { + return key.indexOf(ok.split('*')[0]) === 0; + } + return key === ok; + }); + return !matched.length && (!global.navigator || key !== 'onerror'); + }); + } + +/** + * Check if argument is an instance of Error object or a duck-typed equivalent. + * + * @private + * @param {Object} err - object to check + * @param {string} err.message - error message + * @returns {boolean} + */ + function isError (err) { + return err instanceof Error || (err && typeof err.message === 'string'); + } + +/** + * + * Converts thrown non-extensible type into proper Error. + * + * @private + * @param {*} thrown - Non-extensible type thrown by code + * @return {Error} + */ + function thrown2Error (err) { + return new Error( + 'the ' + type(err) + ' ' + stringify(err) + ' was thrown, throw an Error :)' + ); + } + + Runner.constants = constants; + +/** + * Node.js' `EventEmitter` + * @external EventEmitter + * @see {@link https://nodejs.org/api/events.html#events_class_eventemitter} + */ + + }).call(this, require('_process'), typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}); + }, {'./errors': 6, './pending': 16, './runnable': 33, './suite': 36, './utils': 38, '_process': 70, 'debug': 45, 'events': 50, 'util': 90}], + 35: [function (require, module, exports) { + (function (global) { + 'use strict'; + +/** + * Provides a factory function for a {@link StatsCollector} object. + * @module + */ + + var constants = require('./runner').constants; + var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; + var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; + var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN; + var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN; + var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING; + var EVENT_RUN_END = constants.EVENT_RUN_END; + var EVENT_TEST_END = constants.EVENT_TEST_END; + +/** + * Test statistics collector. + * + * @public + * @typedef {Object} StatsCollector + * @property {number} suites - integer count of suites run. + * @property {number} tests - integer count of tests run. + * @property {number} passes - integer count of passing tests. + * @property {number} pending - integer count of pending tests. + * @property {number} failures - integer count of failed tests. + * @property {Date} start - time when testing began. + * @property {Date} end - time when testing concluded. + * @property {number} duration - number of msecs that testing took. + */ + + var Date = global.Date; + +/** + * Provides stats such as test duration, number of tests passed / failed etc., by listening for events emitted by `runner`. + * + * @private + * @param {Runner} runner - Runner instance + * @throws {TypeError} If falsy `runner` + */ + function createStatsCollector (runner) { + /** + * @type StatsCollector + */ + var stats = { + suites: 0, + tests: 0, + passes: 0, + pending: 0, + failures: 0 + }; + + if (!runner) { + throw new TypeError('Missing runner argument'); + } + + runner.stats = stats; + + runner.once(EVENT_RUN_BEGIN, function () { + stats.start = new Date(); + }); + runner.on(EVENT_SUITE_BEGIN, function (suite) { + suite.root || stats.suites++; + }); + runner.on(EVENT_TEST_PASS, function () { + stats.passes++; + }); + runner.on(EVENT_TEST_FAIL, function () { + stats.failures++; + }); + runner.on(EVENT_TEST_PENDING, function () { + stats.pending++; + }); + runner.on(EVENT_TEST_END, function () { + stats.tests++; + }); + runner.once(EVENT_RUN_END, function () { + stats.end = new Date(); + stats.duration = stats.end - stats.start; + }); + } + + module.exports = createStatsCollector; + + }).call(this, typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}); + }, {'./runner': 34}], + 36: [function (require, module, exports) { + 'use strict'; + +/** + * Module dependencies. + */ + var EventEmitter = require('events').EventEmitter; + var Hook = require('./hook'); + var utils = require('./utils'); + var inherits = utils.inherits; + var debug = require('debug')('mocha:suite'); + var milliseconds = require('ms'); + var errors = require('./errors'); + var createInvalidArgumentTypeError = errors.createInvalidArgumentTypeError; + +/** + * Expose `Suite`. + */ + + exports = module.exports = Suite; + +/** + * Create a new `Suite` with the given `title` and parent `Suite`. + * + * @public + * @param {Suite} parent - Parent suite (required!) + * @param {string} title - Title + * @return {Suite} + */ + Suite.create = function (parent, title) { + var suite = new Suite(title, parent.ctx); + suite.parent = parent; + title = suite.fullTitle(); + parent.addSuite(suite); + return suite; + }; + +/** + * Constructs a new `Suite` instance with the given `title`, `ctx`, and `isRoot`. + * + * @public + * @class + * @extends EventEmitter + * @see {@link https://nodejs.org/api/events.html#events_class_eventemitter|EventEmitter} + * @param {string} title - Suite title. + * @param {Context} parentContext - Parent context instance. + * @param {boolean} [isRoot=false] - Whether this is the root suite. + */ + function Suite (title, parentContext, isRoot) { + if (!utils.isString(title)) { + throw createInvalidArgumentTypeError( + 'Suite argument "title" must be a string. Received type "' + + typeof title + + '"', + 'title', + 'string' + ); + } + this.title = title; + function Context () {} + Context.prototype = parentContext; + this.ctx = new Context(); + this.suites = []; + this.tests = []; + this.pending = false; + this._beforeEach = []; + this._beforeAll = []; + this._afterEach = []; + this._afterAll = []; + this.root = isRoot === true; + this._timeout = 2000; + this._enableTimeouts = true; + this._slow = 75; + this._bail = false; + this._retries = -1; + this._onlyTests = []; + this._onlySuites = []; + this.delayed = false; + + this.on('newListener', function (event) { + if (deprecatedEvents[event]) { + utils.deprecate( + 'Event "' + + event + + '" is deprecated. Please let the Mocha team know about your use case: https://git.io/v6Lwm' + ); + } + }); + } + +/** + * Inherit from `EventEmitter.prototype`. + */ + inherits(Suite, EventEmitter); + +/** + * Return a clone of this `Suite`. + * + * @private + * @return {Suite} + */ + Suite.prototype.clone = function () { + var suite = new Suite(this.title); + debug('clone'); + suite.ctx = this.ctx; + suite.root = this.root; + suite.timeout(this.timeout()); + suite.retries(this.retries()); + suite.enableTimeouts(this.enableTimeouts()); + suite.slow(this.slow()); + suite.bail(this.bail()); + return suite; + }; + +/** + * Set or get timeout `ms` or short-hand such as "2s". + * + * @private + * @todo Do not attempt to set value if `ms` is undefined + * @param {number|string} ms + * @return {Suite|number} for chaining + */ + Suite.prototype.timeout = function (ms) { + if (!arguments.length) { + return this._timeout; + } + if (ms.toString() === '0') { + this._enableTimeouts = false; + } + if (typeof ms === 'string') { + ms = milliseconds(ms); + } + debug('timeout %d', ms); + this._timeout = parseInt(ms, 10); + return this; + }; + +/** + * Set or get number of times to retry a failed test. + * + * @private + * @param {number|string} n + * @return {Suite|number} for chaining + */ + Suite.prototype.retries = function (n) { + if (!arguments.length) { + return this._retries; + } + debug('retries %d', n); + this._retries = parseInt(n, 10) || 0; + return this; + }; + +/** + * Set or get timeout to `enabled`. + * + * @private + * @param {boolean} enabled + * @return {Suite|boolean} self or enabled + */ + Suite.prototype.enableTimeouts = function (enabled) { + if (!arguments.length) { + return this._enableTimeouts; + } + debug('enableTimeouts %s', enabled); + this._enableTimeouts = enabled; + return this; + }; + +/** + * Set or get slow `ms` or short-hand such as "2s". + * + * @private + * @param {number|string} ms + * @return {Suite|number} for chaining + */ + Suite.prototype.slow = function (ms) { + if (!arguments.length) { + return this._slow; + } + if (typeof ms === 'string') { + ms = milliseconds(ms); + } + debug('slow %d', ms); + this._slow = ms; + return this; + }; + +/** + * Set or get whether to bail after first error. + * + * @private + * @param {boolean} bail + * @return {Suite|number} for chaining + */ + Suite.prototype.bail = function (bail) { + if (!arguments.length) { + return this._bail; + } + debug('bail %s', bail); + this._bail = bail; + return this; + }; + +/** + * Check if this suite or its parent suite is marked as pending. + * + * @private + */ + Suite.prototype.isPending = function () { + return this.pending || (this.parent && this.parent.isPending()); + }; + +/** + * Generic hook-creator. + * @private + * @param {string} title - Title of hook + * @param {Function} fn - Hook callback + * @returns {Hook} A new hook + */ + Suite.prototype._createHook = function (title, fn) { + var hook = new Hook(title, fn); + hook.parent = this; + hook.timeout(this.timeout()); + hook.retries(this.retries()); + hook.enableTimeouts(this.enableTimeouts()); + hook.slow(this.slow()); + hook.ctx = this.ctx; + hook.file = this.file; + return hook; + }; + +/** + * Run `fn(test[, done])` before running tests. + * + * @private + * @param {string} title + * @param {Function} fn + * @return {Suite} for chaining + */ + Suite.prototype.beforeAll = function (title, fn) { + if (this.isPending()) { + return this; + } + if (typeof title === 'function') { + fn = title; + title = fn.name; + } + title = '"before all" hook' + (title ? ': ' + title : ''); + + var hook = this._createHook(title, fn); + this._beforeAll.push(hook); + this.emit(constants.EVENT_SUITE_ADD_HOOK_BEFORE_ALL, hook); + return this; + }; + +/** + * Run `fn(test[, done])` after running tests. + * + * @private + * @param {string} title + * @param {Function} fn + * @return {Suite} for chaining + */ + Suite.prototype.afterAll = function (title, fn) { + if (this.isPending()) { + return this; + } + if (typeof title === 'function') { + fn = title; + title = fn.name; + } + title = '"after all" hook' + (title ? ': ' + title : ''); + + var hook = this._createHook(title, fn); + this._afterAll.push(hook); + this.emit(constants.EVENT_SUITE_ADD_HOOK_AFTER_ALL, hook); + return this; + }; + +/** + * Run `fn(test[, done])` before each test case. + * + * @private + * @param {string} title + * @param {Function} fn + * @return {Suite} for chaining + */ + Suite.prototype.beforeEach = function (title, fn) { + if (this.isPending()) { + return this; + } + if (typeof title === 'function') { + fn = title; + title = fn.name; + } + title = '"before each" hook' + (title ? ': ' + title : ''); + + var hook = this._createHook(title, fn); + this._beforeEach.push(hook); + this.emit(constants.EVENT_SUITE_ADD_HOOK_BEFORE_EACH, hook); + return this; + }; + +/** + * Run `fn(test[, done])` after each test case. + * + * @private + * @param {string} title + * @param {Function} fn + * @return {Suite} for chaining + */ + Suite.prototype.afterEach = function (title, fn) { + if (this.isPending()) { + return this; + } + if (typeof title === 'function') { + fn = title; + title = fn.name; + } + title = '"after each" hook' + (title ? ': ' + title : ''); + + var hook = this._createHook(title, fn); + this._afterEach.push(hook); + this.emit(constants.EVENT_SUITE_ADD_HOOK_AFTER_EACH, hook); + return this; + }; + +/** + * Add a test `suite`. + * + * @private + * @param {Suite} suite + * @return {Suite} for chaining + */ + Suite.prototype.addSuite = function (suite) { + suite.parent = this; + suite.root = false; + suite.timeout(this.timeout()); + suite.retries(this.retries()); + suite.enableTimeouts(this.enableTimeouts()); + suite.slow(this.slow()); + suite.bail(this.bail()); + this.suites.push(suite); + this.emit(constants.EVENT_SUITE_ADD_SUITE, suite); + return this; + }; + +/** + * Add a `test` to this suite. + * + * @private + * @param {Test} test + * @return {Suite} for chaining + */ + Suite.prototype.addTest = function (test) { + test.parent = this; + test.timeout(this.timeout()); + test.retries(this.retries()); + test.enableTimeouts(this.enableTimeouts()); + test.slow(this.slow()); + test.ctx = this.ctx; + this.tests.push(test); + this.emit(constants.EVENT_SUITE_ADD_TEST, test); + return this; + }; + +/** + * Return the full title generated by recursively concatenating the parent's + * full title. + * + * @memberof Suite + * @public + * @return {string} + */ + Suite.prototype.fullTitle = function () { + return this.titlePath().join(' '); + }; + +/** + * Return the title path generated by recursively concatenating the parent's + * title path. + * + * @memberof Suite + * @public + * @return {string} + */ + Suite.prototype.titlePath = function () { + var result = []; + if (this.parent) { + result = result.concat(this.parent.titlePath()); + } + if (!this.root) { + result.push(this.title); + } + return result; + }; + +/** + * Return the total number of tests. + * + * @memberof Suite + * @public + * @return {number} + */ + Suite.prototype.total = function () { + return ( + this.suites.reduce(function (sum, suite) { + return sum + suite.total(); + }, 0) + this.tests.length + ); + }; + +/** + * Iterates through each suite recursively to find all tests. Applies a + * function in the format `fn(test)`. + * + * @private + * @param {Function} fn + * @return {Suite} + */ + Suite.prototype.eachTest = function (fn) { + this.tests.forEach(fn); + this.suites.forEach(function (suite) { + suite.eachTest(fn); + }); + return this; + }; + +/** + * This will run the root suite if we happen to be running in delayed mode. + * @private + */ + Suite.prototype.run = function run () { + if (this.root) { + this.emit(constants.EVENT_ROOT_SUITE_RUN); + } + }; + +/** + * Determines whether a suite has an `only` test or suite as a descendant. + * + * @private + * @returns {Boolean} + */ + Suite.prototype.hasOnly = function hasOnly () { + return ( + this._onlyTests.length > 0 || + this._onlySuites.length > 0 || + this.suites.some(function (suite) { + return suite.hasOnly(); + }) + ); + }; + +/** + * Filter suites based on `isOnly` logic. + * + * @private + * @returns {Boolean} + */ + Suite.prototype.filterOnly = function filterOnly () { + if (this._onlyTests.length) { + // If the suite contains `only` tests, run those and ignore any nested suites. + this.tests = this._onlyTests; + this.suites = []; + } else { + // Otherwise, do not run any of the tests in this suite. + this.tests = []; + this._onlySuites.forEach(function (onlySuite) { + // If there are other `only` tests/suites nested in the current `only` suite, then filter that `only` suite. + // Otherwise, all of the tests on this `only` suite should be run, so don't filter it. + if (onlySuite.hasOnly()) { + onlySuite.filterOnly(); + } + }); + // Run the `only` suites, as well as any other suites that have `only` tests/suites as descendants. + var onlySuites = this._onlySuites; + this.suites = this.suites.filter(function (childSuite) { + return onlySuites.indexOf(childSuite) !== -1 || childSuite.filterOnly(); + }); + } + // Keep the suite only if there is something to run + return this.tests.length > 0 || this.suites.length > 0; + }; + +/** + * Adds a suite to the list of subsuites marked `only`. + * + * @private + * @param {Suite} suite + */ + Suite.prototype.appendOnlySuite = function (suite) { + this._onlySuites.push(suite); + }; + +/** + * Adds a test to the list of tests marked `only`. + * + * @private + * @param {Test} test + */ + Suite.prototype.appendOnlyTest = function (test) { + this._onlyTests.push(test); + }; + +/** + * Returns the array of hooks by hook name; see `HOOK_TYPE_*` constants. + * @private + */ + Suite.prototype.getHooks = function getHooks (name) { + return this['_' + name]; + }; + +/** + * Cleans up the references to all the deferred functions + * (before/after/beforeEach/afterEach) and tests of a Suite. + * These must be deleted otherwise a memory leak can happen, + * as those functions may reference variables from closures, + * thus those variables can never be garbage collected as long + * as the deferred functions exist. + * + * @private + */ + Suite.prototype.cleanReferences = function cleanReferences () { + function cleanArrReferences (arr) { + for (var i = 0; i < arr.length; i++) { + delete arr[i].fn; + } + } + + if (Array.isArray(this._beforeAll)) { + cleanArrReferences(this._beforeAll); + } + + if (Array.isArray(this._beforeEach)) { + cleanArrReferences(this._beforeEach); + } + + if (Array.isArray(this._afterAll)) { + cleanArrReferences(this._afterAll); + } + + if (Array.isArray(this._afterEach)) { + cleanArrReferences(this._afterEach); + } + + for (var i = 0; i < this.tests.length; i++) { + delete this.tests[i].fn; + } + }; + + var constants = utils.defineConstants( + /** + * {@link Suite}-related constants. + * @public + * @memberof Suite + * @alias constants + * @readonly + * @static + * @enum {string} + */ + { + /** + * Event emitted after a test file has been loaded Not emitted in browser. + */ + EVENT_FILE_POST_REQUIRE: 'post-require', + /** + * Event emitted before a test file has been loaded. In browser, this is emitted once an interface has been selected. + */ + EVENT_FILE_PRE_REQUIRE: 'pre-require', + /** + * Event emitted immediately after a test file has been loaded. Not emitted in browser. + */ + EVENT_FILE_REQUIRE: 'require', + /** + * Event emitted when `global.run()` is called (use with `delay` option) + */ + EVENT_ROOT_SUITE_RUN: 'run', + + /** + * Namespace for collection of a `Suite`'s "after all" hooks + */ + HOOK_TYPE_AFTER_ALL: 'afterAll', + /** + * Namespace for collection of a `Suite`'s "after each" hooks + */ + HOOK_TYPE_AFTER_EACH: 'afterEach', + /** + * Namespace for collection of a `Suite`'s "before all" hooks + */ + HOOK_TYPE_BEFORE_ALL: 'beforeAll', + /** + * Namespace for collection of a `Suite`'s "before all" hooks + */ + HOOK_TYPE_BEFORE_EACH: 'beforeEach', + + // the following events are all deprecated + + /** + * Emitted after an "after all" `Hook` has been added to a `Suite`. Deprecated + */ + EVENT_SUITE_ADD_HOOK_AFTER_ALL: 'afterAll', + /** + * Emitted after an "after each" `Hook` has been added to a `Suite` Deprecated + */ + EVENT_SUITE_ADD_HOOK_AFTER_EACH: 'afterEach', + /** + * Emitted after an "before all" `Hook` has been added to a `Suite` Deprecated + */ + EVENT_SUITE_ADD_HOOK_BEFORE_ALL: 'beforeAll', + /** + * Emitted after an "before each" `Hook` has been added to a `Suite` Deprecated + */ + EVENT_SUITE_ADD_HOOK_BEFORE_EACH: 'beforeEach', + /** + * Emitted after a child `Suite` has been added to a `Suite`. Deprecated + */ + EVENT_SUITE_ADD_SUITE: 'suite', + /** + * Emitted after a `Test` has been added to a `Suite`. Deprecated + */ + EVENT_SUITE_ADD_TEST: 'test' + } +); + +/** + * @summary There are no known use cases for these events. + * @desc This is a `Set`-like object having all keys being the constant's string value and the value being `true`. + * @todo Remove eventually + * @type {Object} + * @ignore + */ + var deprecatedEvents = Object.keys(constants) + .filter(function (constant) { + return constant.substring(0, 15) === 'EVENT_SUITE_ADD'; + }) + .reduce(function (acc, constant) { + acc[constants[constant]] = true; + return acc; + }, utils.createMap()); + + Suite.constants = constants; + + }, {'./errors': 6, './hook': 7, './utils': 38, 'debug': 45, 'events': 50, 'ms': 60}], + 37: [function (require, module, exports) { + 'use strict'; + var Runnable = require('./runnable'); + var utils = require('./utils'); + var errors = require('./errors'); + var createInvalidArgumentTypeError = errors.createInvalidArgumentTypeError; + var isString = utils.isString; + + module.exports = Test; + +/** + * Initialize a new `Test` with the given `title` and callback `fn`. + * + * @public + * @class + * @extends Runnable + * @param {String} title - Test title (required) + * @param {Function} [fn] - Test callback. If omitted, the Test is considered "pending" + */ + function Test (title, fn) { + if (!isString(title)) { + throw createInvalidArgumentTypeError( + 'Test argument "title" should be a string. Received type "' + + typeof title + + '"', + 'title', + 'string' + ); + } + Runnable.call(this, title, fn); + this.pending = !fn; + this.type = 'test'; + } + +/** + * Inherit from `Runnable.prototype`. + */ + utils.inherits(Test, Runnable); + + Test.prototype.clone = function () { + var test = new Test(this.title, this.fn); + test.timeout(this.timeout()); + test.slow(this.slow()); + test.enableTimeouts(this.enableTimeouts()); + test.retries(this.retries()); + test.currentRetry(this.currentRetry()); + test.globals(this.globals()); + test.parent = this.parent; + test.file = this.file; + test.ctx = this.ctx; + return test; + }; + + }, {'./errors': 6, './runnable': 33, './utils': 38}], + 38: [function (require, module, exports) { + (function (process, Buffer) { + 'use strict'; + +/** + * Various utility functions used throughout Mocha's codebase. + * @module utils + */ + +/** + * Module dependencies. + */ + + var fs = require('fs'); + var path = require('path'); + var util = require('util'); + var glob = require('glob'); + var he = require('he'); + var errors = require('./errors'); + var createNoFilesMatchPatternError = errors.createNoFilesMatchPatternError; + var createMissingArgumentError = errors.createMissingArgumentError; + + var assign = (exports.assign = require('object.assign').getPolyfill()); + +/** + * Inherit the prototype methods from one constructor into another. + * + * @param {function} ctor - Constructor function which needs to inherit the + * prototype. + * @param {function} superCtor - Constructor function to inherit prototype from. + * @throws {TypeError} if either constructor is null, or if super constructor + * lacks a prototype. + */ + exports.inherits = util.inherits; + +/** + * Escape special characters in the given string of html. + * + * @private + * @param {string} html + * @return {string} + */ + exports.escape = function (html) { + return he.encode(String(html), {useNamedReferences: false}); + }; + +/** + * Test if the given obj is type of string. + * + * @private + * @param {Object} obj + * @return {boolean} + */ + exports.isString = function (obj) { + return typeof obj === 'string'; + }; + +/** + * Compute a slug from the given `str`. + * + * @private + * @param {string} str + * @return {string} + */ + exports.slug = function (str) { + return str + .toLowerCase() + .replace(/ +/g, '-') + .replace(/[^-\w]/g, ''); + }; + +/** + * Strip the function definition from `str`, and re-indent for pre whitespace. + * + * @param {string} str + * @return {string} + */ + exports.clean = function (str) { + str = str + .replace(/\r\n?|[\n\u2028\u2029]/g, '\n') + .replace(/^\uFEFF/, '') + // (traditional)-> space/name parameters body (lambda)-> parameters body multi-statement/single keep body content + .replace( + /^function(?:\s*|\s+[^(]*)\([^)]*\)\s*\{((?:.|\n)*?)\s*\}$|^\([^)]*\)\s*=>\s*(?:\{((?:.|\n)*?)\s*\}|((?:.|\n)*))$/, + '$1$2$3' + ); + + var spaces = str.match(/^\n?( *)/)[1].length; + var tabs = str.match(/^\n?(\t*)/)[1].length; + var re = new RegExp( + '^\n?' + (tabs ? '\t' : ' ') + '{' + (tabs || spaces) + '}', + 'gm' + ); + + str = str.replace(re, ''); + + return str.trim(); + }; + +/** + * Parse the given `qs`. + * + * @private + * @param {string} qs + * @return {Object} + */ + exports.parseQuery = function (qs) { + return qs + .replace('?', '') + .split('&') + .reduce(function (obj, pair) { + var i = pair.indexOf('='); + var key = pair.slice(0, i); + var val = pair.slice(++i); + + // Due to how the URLSearchParams API treats spaces + obj[key] = decodeURIComponent(val.replace(/\+/g, '%20')); + + return obj; + }, {}); + }; + +/** + * Highlight the given string of `js`. + * + * @private + * @param {string} js + * @return {string} + */ + function highlight (js) { + return js + .replace(//g, '>') + .replace(/\/\/(.*)/gm, '//$1') + .replace(/('.*?')/gm, '$1') + .replace(/(\d+\.\d+)/gm, '$1') + .replace(/(\d+)/gm, '$1') + .replace( + /\bnew[ \t]+(\w+)/gm, + 'new $1' + ) + .replace( + /\b(function|new|throw|return|var|if|else)\b/gm, + '$1' + ); + } + +/** + * Highlight the contents of tag `name`. + * + * @private + * @param {string} name + */ + exports.highlightTags = function (name) { + var code = document.getElementById('mocha').getElementsByTagName(name); + for (var i = 0, len = code.length; i < len; ++i) { + code[i].innerHTML = highlight(code[i].innerHTML); + } + }; + +/** + * If a value could have properties, and has none, this function is called, + * which returns a string representation of the empty value. + * + * Functions w/ no properties return `'[Function]'` + * Arrays w/ length === 0 return `'[]'` + * Objects w/ no properties return `'{}'` + * All else: return result of `value.toString()` + * + * @private + * @param {*} value The value to inspect. + * @param {string} typeHint The type of the value + * @returns {string} + */ + function emptyRepresentation (value, typeHint) { + switch (typeHint) { + case 'function': + return '[Function]'; + case 'object': + return '{}'; + case 'array': + return '[]'; + default: + return value.toString(); + } + } + +/** + * Takes some variable and asks `Object.prototype.toString()` what it thinks it + * is. + * + * @private + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString + * @param {*} value The value to test. + * @returns {string} Computed type + * @example + * type({}) // 'object' + * type([]) // 'array' + * type(1) // 'number' + * type(false) // 'boolean' + * type(Infinity) // 'number' + * type(null) // 'null' + * type(new Date()) // 'date' + * type(/foo/) // 'regexp' + * type('type') // 'string' + * type(global) // 'global' + * type(new String('foo') // 'object' + */ + var type = (exports.type = function type (value) { + if (value === undefined) { + return 'undefined'; + } else if (value === null) { + return 'null'; + } else if (Buffer.isBuffer(value)) { + return 'buffer'; + } + return Object.prototype.toString + .call(value) + .replace(/^\[.+\s(.+?)]$/, '$1') + .toLowerCase(); + }); + +/** + * Stringify `value`. Different behavior depending on type of value: + * + * - If `value` is undefined or null, return `'[undefined]'` or `'[null]'`, respectively. + * - If `value` is not an object, function or array, return result of `value.toString()` wrapped in double-quotes. + * - If `value` is an *empty* object, function, or array, return result of function + * {@link emptyRepresentation}. + * - If `value` has properties, call {@link exports.canonicalize} on it, then return result of + * JSON.stringify(). + * + * @private + * @see exports.type + * @param {*} value + * @return {string} + */ + exports.stringify = function (value) { + var typeHint = type(value); + + if (!~['object', 'array', 'function'].indexOf(typeHint)) { + if (typeHint === 'buffer') { + var json = Buffer.prototype.toJSON.call(value); + // Based on the toJSON result + return jsonStringify( + json.data && json.type ? json.data : json, + 2 + ).replace(/,(\n|$)/g, '$1'); + } + + // IE7/IE8 has a bizarre String constructor; needs to be coerced + // into an array and back to obj. + if (typeHint === 'string' && typeof value === 'object') { + value = value.split('').reduce(function (acc, char, idx) { + acc[idx] = char; + return acc; + }, {}); + typeHint = 'object'; + } else { + return jsonStringify(value); + } + } + + for (var prop in value) { + if (Object.prototype.hasOwnProperty.call(value, prop)) { + return jsonStringify( + exports.canonicalize(value, null, typeHint), + 2 + ).replace(/,(\n|$)/g, '$1'); + } + } + + return emptyRepresentation(value, typeHint); + }; + +/** + * like JSON.stringify but more sense. + * + * @private + * @param {Object} object + * @param {number=} spaces + * @param {number=} depth + * @returns {*} + */ + function jsonStringify (object, spaces, depth) { + if (typeof spaces === 'undefined') { + // primitive types + return _stringify(object); + } + + depth = depth || 1; + var space = spaces * depth; + var str = Array.isArray(object) ? '[' : '{'; + var end = Array.isArray(object) ? ']' : '}'; + var length = + typeof object.length === 'number' + ? object.length + : Object.keys(object).length; + // `.repeat()` polyfill + function repeat (s, n) { + return new Array(n).join(s); + } + + function _stringify (val) { + switch (type(val)) { + case 'null': + case 'undefined': + val = '[' + val + ']'; + break; + case 'array': + case 'object': + val = jsonStringify(val, spaces, depth + 1); + break; + case 'boolean': + case 'regexp': + case 'symbol': + case 'number': + val = + val === 0 && 1 / val === -Infinity // `-0` + ? '-0' + : val.toString(); + break; + case 'date': + var sDate = isNaN(val.getTime()) ? val.toString() : val.toISOString(); + val = '[Date: ' + sDate + ']'; + break; + case 'buffer': + var json = val.toJSON(); + // Based on the toJSON result + json = json.data && json.type ? json.data : json; + val = '[Buffer: ' + jsonStringify(json, 2, depth + 1) + ']'; + break; + default: + val = + val === '[Function]' || val === '[Circular]' + ? val + : JSON.stringify(val); // string + } + return val; + } + + for (var i in object) { + if (!Object.prototype.hasOwnProperty.call(object, i)) { + continue; // not my business + } + --length; + str += + '\n ' + + repeat(' ', space) + + (Array.isArray(object) ? '' : '"' + i + '": ') + // key + _stringify(object[i]) + // value + (length ? ',' : ''); // comma + } + + return ( + str + + // [], {} + (str.length !== 1 ? '\n' + repeat(' ', --space) + end : end) + ); + } + +/** + * Return a new Thing that has the keys in sorted order. Recursive. + * + * If the Thing... + * - has already been seen, return string `'[Circular]'` + * - is `undefined`, return string `'[undefined]'` + * - is `null`, return value `null` + * - is some other primitive, return the value + * - is not a primitive or an `Array`, `Object`, or `Function`, return the value of the Thing's `toString()` method + * - is a non-empty `Array`, `Object`, or `Function`, return the result of calling this function again. + * - is an empty `Array`, `Object`, or `Function`, return the result of calling `emptyRepresentation()` + * + * @private + * @see {@link exports.stringify} + * @param {*} value Thing to inspect. May or may not have properties. + * @param {Array} [stack=[]] Stack of seen values + * @param {string} [typeHint] Type hint + * @return {(Object|Array|Function|string|undefined)} + */ + exports.canonicalize = function canonicalize (value, stack, typeHint) { + var canonicalizedObj; + /* eslint-disable no-unused-vars */ + var prop; + /* eslint-enable no-unused-vars */ + typeHint = typeHint || type(value); + function withStack (value, fn) { + stack.push(value); + fn(); + stack.pop(); + } + + stack = stack || []; + + if (stack.indexOf(value) !== -1) { + return '[Circular]'; + } + + switch (typeHint) { + case 'undefined': + case 'buffer': + case 'null': + canonicalizedObj = value; + break; + case 'array': + withStack(value, function () { + canonicalizedObj = value.map(function (item) { + return exports.canonicalize(item, stack); + }); + }); + break; + case 'function': + /* eslint-disable guard-for-in */ + for (prop in value) { + canonicalizedObj = {}; + break; + } + /* eslint-enable guard-for-in */ + if (!canonicalizedObj) { + canonicalizedObj = emptyRepresentation(value, typeHint); + break; + } + /* falls through */ + case 'object': + canonicalizedObj = canonicalizedObj || {}; + withStack(value, function () { + Object.keys(value) + .sort() + .forEach(function (key) { + canonicalizedObj[key] = exports.canonicalize(value[key], stack); + }); + }); + break; + case 'date': + case 'number': + case 'regexp': + case 'boolean': + case 'symbol': + canonicalizedObj = value; + break; + default: + canonicalizedObj = value + ''; + } + + return canonicalizedObj; + }; + +/** + * Determines if pathname has a matching file extension. + * + * @private + * @param {string} pathname - Pathname to check for match. + * @param {string[]} exts - List of file extensions (sans period). + * @return {boolean} whether file extension matches. + * @example + * hasMatchingExtname('foo.html', ['js', 'css']); // => false + */ + function hasMatchingExtname (pathname, exts) { + var suffix = path.extname(pathname).slice(1); + return exts.some(function (element) { + return suffix === element; + }); + } + +/** + * Determines if pathname would be a "hidden" file (or directory) on UN*X. + * + * @description + * On UN*X, pathnames beginning with a full stop (aka dot) are hidden during + * typical usage. Dotfiles, plain-text configuration files, are prime examples. + * + * @see {@link http://xahlee.info/UnixResource_dir/writ/unix_origin_of_dot_filename.html|Origin of Dot File Names} + * + * @private + * @param {string} pathname - Pathname to check for match. + * @return {boolean} whether pathname would be considered a hidden file. + * @example + * isHiddenOnUnix('.profile'); // => true + */ + function isHiddenOnUnix (pathname) { + return path.basename(pathname)[0] === '.'; + } + +/** + * Lookup file names at the given `path`. + * + * @description + * Filenames are returned in _traversal_ order by the OS/filesystem. + * **Make no assumption that the names will be sorted in any fashion.** + * + * @public + * @memberof Mocha.utils + * @param {string} filepath - Base path to start searching from. + * @param {string[]} [extensions=[]] - File extensions to look for. + * @param {boolean} [recursive=false] - Whether to recurse into subdirectories. + * @return {string[]} An array of paths. + * @throws {Error} if no files match pattern. + * @throws {TypeError} if `filepath` is directory and `extensions` not provided. + */ + exports.lookupFiles = function lookupFiles (filepath, extensions, recursive) { + extensions = extensions || []; + recursive = recursive || false; + var files = []; + var stat; + + if (!fs.existsSync(filepath)) { + var pattern; + if (glob.hasMagic(filepath)) { + // Handle glob as is without extensions + pattern = filepath; + } else { + // glob pattern e.g. 'filepath+(.js|.ts)' + var strExtensions = extensions + .map(function (v) { + return '.' + v; + }) + .join('|'); + pattern = filepath + '+(' + strExtensions + ')'; + } + files = glob.sync(pattern, {nodir: true}); + if (!files.length) { + throw createNoFilesMatchPatternError( + 'Cannot find any files matching pattern ' + exports.dQuote(filepath), + filepath + ); + } + return files; + } + + // Handle file + try { + stat = fs.statSync(filepath); + if (stat.isFile()) { + return filepath; + } + } catch (err) { + // ignore error + return; + } + + // Handle directory + fs.readdirSync(filepath).forEach(function (dirent) { + var pathname = path.join(filepath, dirent); + var stat; + + try { + stat = fs.statSync(pathname); + if (stat.isDirectory()) { + if (recursive) { + files = files.concat(lookupFiles(pathname, extensions, recursive)); + } + return; + } + } catch (err) { + // ignore error + return; + } + if (!extensions.length) { + throw createMissingArgumentError( + util.format( + 'Argument %s required when argument %s is a directory', + exports.sQuote('extensions'), + exports.sQuote('filepath') + ), + 'extensions', + 'array' + ); + } + + if ( + !stat.isFile() || + !hasMatchingExtname(pathname, extensions) || + isHiddenOnUnix(pathname) + ) { + return; + } + files.push(pathname); + }); + + return files; + }; + +/** + * process.emitWarning or a polyfill + * @see https://nodejs.org/api/process.html#process_process_emitwarning_warning_options + * @ignore + */ + function emitWarning (msg, type) { + if (process.emitWarning) { + process.emitWarning(msg, type); + } else { + process.nextTick(function () { + console.warn(type + ': ' + msg); + }); + } + } + +/** + * Show a deprecation warning. Each distinct message is only displayed once. + * Ignores empty messages. + * + * @param {string} [msg] - Warning to print + * @private + */ + exports.deprecate = function deprecate (msg) { + msg = String(msg); + if (msg && !deprecate.cache[msg]) { + deprecate.cache[msg] = true; + emitWarning(msg, 'DeprecationWarning'); + } + }; + exports.deprecate.cache = {}; + +/** + * Show a generic warning. + * Ignores empty messages. + * + * @param {string} [msg] - Warning to print + * @private + */ + exports.warn = function warn (msg) { + if (msg) { + emitWarning(msg); + } + }; + +/** + * @summary + * This Filter based on `mocha-clean` module.(see: `github.com/rstacruz/mocha-clean`) + * @description + * When invoking this function you get a filter function that get the Error.stack as an input, + * and return a prettify output. + * (i.e: strip Mocha and internal node functions from stack trace). + * @returns {Function} + */ + exports.stackTraceFilter = function () { + // TODO: Replace with `process.browser` + var is = typeof document === 'undefined' ? {node: true} : {browser: true}; + var slash = path.sep; + var cwd; + if (is.node) { + cwd = process.cwd() + slash; + } else { + cwd = (typeof location === 'undefined' + ? window.location + : location + ).href.replace(/\/[^/]*$/, '/'); + slash = '/'; + } + + function isMochaInternal (line) { + return ( + ~line.indexOf('node_modules' + slash + 'mocha' + slash) || + ~line.indexOf(slash + 'mocha.js') || + ~line.indexOf(slash + 'mocha.min.js') + ); + } + + function isNodeInternal (line) { + return ( + ~line.indexOf('(timers.js:') || + ~line.indexOf('(events.js:') || + ~line.indexOf('(node.js:') || + ~line.indexOf('(module.js:') || + ~line.indexOf('GeneratorFunctionPrototype.next (native)') || + false + ); + } + + return function (stack) { + stack = stack.split('\n'); + + stack = stack.reduce(function (list, line) { + if (isMochaInternal(line)) { + return list; + } + + if (is.node && isNodeInternal(line)) { + return list; + } + + // Clean up cwd(absolute) + if (/:\d+:\d+\)?$/.test(line)) { + line = line.replace('(' + cwd, '('); + } + + list.push(line); + return list; + }, []); + + return stack.join('\n'); + }; + }; + +/** + * Crude, but effective. + * @public + * @param {*} value + * @returns {boolean} Whether or not `value` is a Promise + */ + exports.isPromise = function isPromise (value) { + return ( + typeof value === 'object' && + value !== null && + typeof value.then === 'function' + ); + }; + +/** + * Clamps a numeric value to an inclusive range. + * + * @param {number} value - Value to be clamped. + * @param {numer[]} range - Two element array specifying [min, max] range. + * @returns {number} clamped value + */ + exports.clamp = function clamp (value, range) { + return Math.min(Math.max(value, range[0]), range[1]); + }; + +/** + * Single quote text by combining with undirectional ASCII quotation marks. + * + * @description + * Provides a simple means of markup for quoting text to be used in output. + * Use this to quote names of variables, methods, and packages. + * + * package 'foo' cannot be found + * + * @private + * @param {string} str - Value to be quoted. + * @returns {string} quoted value + * @example + * sQuote('n') // => 'n' + */ + exports.sQuote = function (str) { + return "'" + str + "'"; + }; + +/** + * Double quote text by combining with undirectional ASCII quotation marks. + * + * @description + * Provides a simple means of markup for quoting text to be used in output. + * Use this to quote names of datatypes, classes, pathnames, and strings. + * + * argument 'value' must be "string" or "number" + * + * @private + * @param {string} str - Value to be quoted. + * @returns {string} quoted value + * @example + * dQuote('number') // => "number" + */ + exports.dQuote = function (str) { + return '"' + str + '"'; + }; + +/** + * Provides simplistic message translation for dealing with plurality. + * + * @description + * Use this to create messages which need to be singular or plural. + * Some languages have several plural forms, so _complete_ message clauses + * are preferable to generating the message on the fly. + * + * @private + * @param {number} n - Non-negative integer + * @param {string} msg1 - Message to be used in English for `n = 1` + * @param {string} msg2 - Message to be used in English for `n = 0, 2, 3, ...` + * @returns {string} message corresponding to value of `n` + * @example + * var sprintf = require('util').format; + * var pkgs = ['one', 'two']; + * var msg = sprintf( + * ngettext( + * pkgs.length, + * 'cannot load package: %s', + * 'cannot load packages: %s' + * ), + * pkgs.map(sQuote).join(', ') + * ); + * console.log(msg); // => cannot load packages: 'one', 'two' + */ + exports.ngettext = function (n, msg1, msg2) { + if (typeof n === 'number' && n >= 0) { + return n === 1 ? msg1 : msg2; + } + }; + +/** + * It's a noop. + * @public + */ + exports.noop = function () {}; + +/** + * Creates a map-like object. + * + * @description + * A "map" is an object with no prototype, for our purposes. In some cases + * this would be more appropriate than a `Map`, especially if your environment + * doesn't support it. Recommended for use in Mocha's public APIs. + * + * @public + * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map|MDN:Map} + * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#Custom_and_Null_objects|MDN:Object.create - Custom objects} + * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign|MDN:Object.assign} + * @param {...*} [obj] - Arguments to `Object.assign()`. + * @returns {Object} An object with no prototype, having `...obj` properties + */ + exports.createMap = function (obj) { + return assign.apply( + null, + [Object.create(null)].concat(Array.prototype.slice.call(arguments)) + ); + }; + +/** + * Creates a read-only map-like object. + * + * @description + * This differs from {@link module:utils.createMap createMap} only in that + * the argument must be non-empty, because the result is frozen. + * + * @see {@link module:utils.createMap createMap} + * @param {...*} [obj] - Arguments to `Object.assign()`. + * @returns {Object} A frozen object with no prototype, having `...obj` properties + * @throws {TypeError} if argument is not a non-empty object. + */ + exports.defineConstants = function (obj) { + if (type(obj) !== 'object' || !Object.keys(obj).length) { + throw new TypeError('Invalid argument; expected a non-empty object'); + } + return Object.freeze(exports.createMap(obj)); + }; + + }).call(this, require('_process'), require('buffer').Buffer); + }, {'./errors': 6, '_process': 70, 'buffer': 43, 'fs': 40, 'glob': 40, 'he': 54, 'object.assign': 65, 'path': 40, 'util': 90}], + 39: [function (require, module, exports) { + 'use strict'; + + exports.byteLength = byteLength; + exports.toByteArray = toByteArray; + exports.fromByteArray = fromByteArray; + + var lookup = []; + var revLookup = []; + var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array; + + var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + for (var i = 0, len = code.length; i < len; ++i) { + lookup[i] = code[i]; + revLookup[code.charCodeAt(i)] = i; + } + +// Support decoding URL-safe base64 strings, as Node.js does. +// See: https://en.wikipedia.org/wiki/Base64#URL_applications + revLookup['-'.charCodeAt(0)] = 62; + revLookup['_'.charCodeAt(0)] = 63; + + function getLens (b64) { + var len = b64.length; + + if (len % 4 > 0) { + throw new Error('Invalid string. Length must be a multiple of 4'); + } + + // Trim off extra bytes after placeholder bytes are found + // See: https://github.com/beatgammit/base64-js/issues/42 + var validLen = b64.indexOf('='); + if (validLen === -1) validLen = len; + + var placeHoldersLen = validLen === len + ? 0 + : 4 - (validLen % 4); + + return [validLen, placeHoldersLen]; + } + +// base64 is 4/3 + up to two characters of the original data + function byteLength (b64) { + var lens = getLens(b64); + var validLen = lens[0]; + var placeHoldersLen = lens[1]; + return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen; + } + + function _byteLength (b64, validLen, placeHoldersLen) { + return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen; + } + + function toByteArray (b64) { + var tmp; + var lens = getLens(b64); + var validLen = lens[0]; + var placeHoldersLen = lens[1]; + + var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen)); + + var curByte = 0; + + // if there are placeholders, only get up to the last complete 4 chars + var len = placeHoldersLen > 0 + ? validLen - 4 + : validLen; + + for (var i = 0; i < len; i += 4) { + tmp = + (revLookup[b64.charCodeAt(i)] << 18) | + (revLookup[b64.charCodeAt(i + 1)] << 12) | + (revLookup[b64.charCodeAt(i + 2)] << 6) | + revLookup[b64.charCodeAt(i + 3)]; + arr[curByte++] = (tmp >> 16) & 0xFF; + arr[curByte++] = (tmp >> 8) & 0xFF; + arr[curByte++] = tmp & 0xFF; + } + + if (placeHoldersLen === 2) { + tmp = + (revLookup[b64.charCodeAt(i)] << 2) | + (revLookup[b64.charCodeAt(i + 1)] >> 4); + arr[curByte++] = tmp & 0xFF; + } + + if (placeHoldersLen === 1) { + tmp = + (revLookup[b64.charCodeAt(i)] << 10) | + (revLookup[b64.charCodeAt(i + 1)] << 4) | + (revLookup[b64.charCodeAt(i + 2)] >> 2); + arr[curByte++] = (tmp >> 8) & 0xFF; + arr[curByte++] = tmp & 0xFF; + } + + return arr; + } + + function tripletToBase64 (num) { + return lookup[num >> 18 & 0x3F] + + lookup[num >> 12 & 0x3F] + + lookup[num >> 6 & 0x3F] + + lookup[num & 0x3F]; + } + + function encodeChunk (uint8, start, end) { + var tmp; + var output = []; + for (var i = start; i < end; i += 3) { + tmp = + ((uint8[i] << 16) & 0xFF0000) + + ((uint8[i + 1] << 8) & 0xFF00) + + (uint8[i + 2] & 0xFF); + output.push(tripletToBase64(tmp)); + } + return output.join(''); + } + + function fromByteArray (uint8) { + var tmp; + var len = uint8.length; + var extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes + var parts = []; + var maxChunkLength = 16383; // must be multiple of 3 + + // go through the array every three bytes, we'll deal with trailing stuff later + for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { + parts.push(encodeChunk( + uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength) + )); + } + + // pad the end with zeros, but make sure to not forget the extra bytes + if (extraBytes === 1) { + tmp = uint8[len - 1]; + parts.push( + lookup[tmp >> 2] + + lookup[(tmp << 4) & 0x3F] + + '==' + ); + } else if (extraBytes === 2) { + tmp = (uint8[len - 2] << 8) + uint8[len - 1]; + parts.push( + lookup[tmp >> 10] + + lookup[(tmp >> 4) & 0x3F] + + lookup[(tmp << 2) & 0x3F] + + '=' + ); + } + + return parts.join(''); + } + + }, {}], + 40: [function (require, module, exports) { + + }, {}], + 41: [function (require, module, exports) { + (function (process) { + var WritableStream = require('stream').Writable; + var inherits = require('util').inherits; + + module.exports = BrowserStdout; + + inherits(BrowserStdout, WritableStream); + + function BrowserStdout (opts) { + if (!(this instanceof BrowserStdout)) return new BrowserStdout(opts); + + opts = opts || {}; + WritableStream.call(this, opts); + this.label = (opts.label !== undefined) ? opts.label : 'stdout'; + } + + BrowserStdout.prototype._write = function (chunks, encoding, cb) { + var output = chunks.toString ? chunks.toString() : chunks; + if (this.label === false) { + console.log(output); + } else { + console.log(this.label + ':', output); + } + process.nextTick(cb); + }; + + }).call(this, require('_process')); + }, {'_process': 70, 'stream': 85, 'util': 90}], + 42: [function (require, module, exports) { + arguments[4][40][0].apply(exports, arguments); + }, {'dup': 40}], + 43: [function (require, module, exports) { + (function (Buffer) { +/*! + * The buffer module from node.js, for the browser. + * + * @author Feross Aboukhadijeh + * @license MIT + */ +/* eslint-disable no-proto */ + + 'use strict'; + + var base64 = require('base64-js'); + var ieee754 = require('ieee754'); + + exports.Buffer = Buffer; + exports.SlowBuffer = SlowBuffer; + exports.INSPECT_MAX_BYTES = 50; + + var K_MAX_LENGTH = 0x7fffffff; + exports.kMaxLength = K_MAX_LENGTH; + +/** + * If `Buffer.TYPED_ARRAY_SUPPORT`: + * === true Use Uint8Array implementation (fastest) + * === false Print warning and recommend using `buffer` v4.x which has an Object + * implementation (most compatible, even IE6) + * + * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, + * Opera 11.6+, iOS 4.2+. + * + * We report that the browser does not support typed arrays if the are not subclassable + * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array` + * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support + * for __proto__ and has a buggy typed array implementation. + */ + Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport(); + + if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' && + typeof console.error === 'function') { + console.error( + 'This browser lacks typed array (Uint8Array) support which is required by ' + + '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.' + ); + } + + function typedArraySupport () { + // Can typed array instances can be augmented? + try { + var arr = new Uint8Array(1); + arr.__proto__ = { __proto__: Uint8Array.prototype, foo: function () { return 42; } }; + return arr.foo() === 42; + } catch (e) { + return false; + } + } + + Object.defineProperty(Buffer.prototype, 'parent', { + enumerable: true, + get: function () { + if (!Buffer.isBuffer(this)) return undefined; + return this.buffer; + } + }); + + Object.defineProperty(Buffer.prototype, 'offset', { + enumerable: true, + get: function () { + if (!Buffer.isBuffer(this)) return undefined; + return this.byteOffset; + } + }); + + function createBuffer (length) { + if (length > K_MAX_LENGTH) { + throw new RangeError('The value "' + length + '" is invalid for option "size"'); + } + // Return an augmented `Uint8Array` instance + var buf = new Uint8Array(length); + buf.__proto__ = Buffer.prototype; + return buf; + } + +/** + * The Buffer constructor returns instances of `Uint8Array` that have their + * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of + * `Uint8Array`, so the returned instances will have all the node `Buffer` methods + * and the `Uint8Array` methods. Square bracket notation works as expected -- it + * returns a single octet. + * + * The `Uint8Array` prototype remains unmodified. + */ + + function Buffer (arg, encodingOrOffset, length) { + // Common case. + if (typeof arg === 'number') { + if (typeof encodingOrOffset === 'string') { + throw new TypeError( + 'The "string" argument must be of type string. Received type number' + ); + } + return allocUnsafe(arg); + } + return from(arg, encodingOrOffset, length); + } + +// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 + if (typeof Symbol !== 'undefined' && Symbol.species != null && + Buffer[Symbol.species] === Buffer) { + Object.defineProperty(Buffer, Symbol.species, { + value: null, + configurable: true, + enumerable: false, + writable: false + }); + } + + Buffer.poolSize = 8192; // not used by this implementation + + function from (value, encodingOrOffset, length) { + if (typeof value === 'string') { + return fromString(value, encodingOrOffset); + } + + if (ArrayBuffer.isView(value)) { + return fromArrayLike(value); + } + + if (value == null) { + throw TypeError( + 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' + + 'or Array-like Object. Received type ' + (typeof value) + ); + } + + if (isInstance(value, ArrayBuffer) || + (value && isInstance(value.buffer, ArrayBuffer))) { + return fromArrayBuffer(value, encodingOrOffset, length); + } + + if (typeof value === 'number') { + throw new TypeError( + 'The "value" argument must not be of type number. Received type number' + ); + } + + var valueOf = value.valueOf && value.valueOf(); + if (valueOf != null && valueOf !== value) { + return Buffer.from(valueOf, encodingOrOffset, length); + } + + var b = fromObject(value); + if (b) return b; + + if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null && + typeof value[Symbol.toPrimitive] === 'function') { + return Buffer.from( + value[Symbol.toPrimitive]('string'), encodingOrOffset, length + ); + } + + throw new TypeError( + 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' + + 'or Array-like Object. Received type ' + (typeof value) + ); + } + +/** + * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError + * if value is a number. + * Buffer.from(str[, encoding]) + * Buffer.from(array) + * Buffer.from(buffer) + * Buffer.from(arrayBuffer[, byteOffset[, length]]) + **/ + Buffer.from = function (value, encodingOrOffset, length) { + return from(value, encodingOrOffset, length); + }; + +// Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug: +// https://github.com/feross/buffer/pull/148 + Buffer.prototype.__proto__ = Uint8Array.prototype; + Buffer.__proto__ = Uint8Array; + + function assertSize (size) { + if (typeof size !== 'number') { + throw new TypeError('"size" argument must be of type number'); + } else if (size < 0) { + throw new RangeError('The value "' + size + '" is invalid for option "size"'); + } + } + + function alloc (size, fill, encoding) { + assertSize(size); + if (size <= 0) { + return createBuffer(size); + } + if (fill !== undefined) { + // Only pay attention to encoding if it's a string. This + // prevents accidentally sending in a number that would + // be interpretted as a start offset. + return typeof encoding === 'string' + ? createBuffer(size).fill(fill, encoding) + : createBuffer(size).fill(fill); + } + return createBuffer(size); + } + +/** + * Creates a new filled Buffer instance. + * alloc(size[, fill[, encoding]]) + **/ + Buffer.alloc = function (size, fill, encoding) { + return alloc(size, fill, encoding); + }; + + function allocUnsafe (size) { + assertSize(size); + return createBuffer(size < 0 ? 0 : checked(size) | 0); + } + +/** + * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance. + * */ + Buffer.allocUnsafe = function (size) { + return allocUnsafe(size); + }; +/** + * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance. + */ + Buffer.allocUnsafeSlow = function (size) { + return allocUnsafe(size); + }; + + function fromString (string, encoding) { + if (typeof encoding !== 'string' || encoding === '') { + encoding = 'utf8'; + } + + if (!Buffer.isEncoding(encoding)) { + throw new TypeError('Unknown encoding: ' + encoding); + } + + var length = byteLength(string, encoding) | 0; + var buf = createBuffer(length); + + var actual = buf.write(string, encoding); + + if (actual !== length) { + // Writing a hex string, for example, that contains invalid characters will + // cause everything after the first invalid character to be ignored. (e.g. + // 'abxxcd' will be treated as 'ab') + buf = buf.slice(0, actual); + } + + return buf; + } + + function fromArrayLike (array) { + var length = array.length < 0 ? 0 : checked(array.length) | 0; + var buf = createBuffer(length); + for (var i = 0; i < length; i += 1) { + buf[i] = array[i] & 255; + } + return buf; + } + + function fromArrayBuffer (array, byteOffset, length) { + if (byteOffset < 0 || array.byteLength < byteOffset) { + throw new RangeError('"offset" is outside of buffer bounds'); + } + + if (array.byteLength < byteOffset + (length || 0)) { + throw new RangeError('"length" is outside of buffer bounds'); + } + + var buf; + if (byteOffset === undefined && length === undefined) { + buf = new Uint8Array(array); + } else if (length === undefined) { + buf = new Uint8Array(array, byteOffset); + } else { + buf = new Uint8Array(array, byteOffset, length); + } + + // Return an augmented `Uint8Array` instance + buf.__proto__ = Buffer.prototype; + return buf; + } + + function fromObject (obj) { + if (Buffer.isBuffer(obj)) { + var len = checked(obj.length) | 0; + var buf = createBuffer(len); + + if (buf.length === 0) { + return buf; + } + + obj.copy(buf, 0, 0, len); + return buf; + } + + if (obj.length !== undefined) { + if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) { + return createBuffer(0); + } + return fromArrayLike(obj); + } + + if (obj.type === 'Buffer' && Array.isArray(obj.data)) { + return fromArrayLike(obj.data); + } + } + + function checked (length) { + // Note: cannot use `length < K_MAX_LENGTH` here because that fails when + // length is NaN (which is otherwise coerced to zero.) + if (length >= K_MAX_LENGTH) { + throw new RangeError('Attempt to allocate Buffer larger than maximum ' + + 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes'); + } + return length | 0; + } + + function SlowBuffer (length) { + if (+length != length) { // eslint-disable-line eqeqeq + length = 0; + } + return Buffer.alloc(+length); + } + + Buffer.isBuffer = function isBuffer (b) { + return b != null && b._isBuffer === true && + b !== Buffer.prototype; // so Buffer.isBuffer(Buffer.prototype) will be false + }; + + Buffer.compare = function compare (a, b) { + if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength); + if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength); + if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { + throw new TypeError( + 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array' + ); + } + + if (a === b) return 0; + + var x = a.length; + var y = b.length; + + for (var i = 0, len = Math.min(x, y); i < len; ++i) { + if (a[i] !== b[i]) { + x = a[i]; + y = b[i]; + break; + } + } + + if (x < y) return -1; + if (y < x) return 1; + return 0; + }; + + Buffer.isEncoding = function isEncoding (encoding) { + switch (String(encoding).toLowerCase()) { + case 'hex': + case 'utf8': + case 'utf-8': + case 'ascii': + case 'latin1': + case 'binary': + case 'base64': + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return true; + default: + return false; + } + }; + + Buffer.concat = function concat (list, length) { + if (!Array.isArray(list)) { + throw new TypeError('"list" argument must be an Array of Buffers'); + } + + if (list.length === 0) { + return Buffer.alloc(0); + } + + var i; + if (length === undefined) { + length = 0; + for (i = 0; i < list.length; ++i) { + length += list[i].length; + } + } + + var buffer = Buffer.allocUnsafe(length); + var pos = 0; + for (i = 0; i < list.length; ++i) { + var buf = list[i]; + if (isInstance(buf, Uint8Array)) { + buf = Buffer.from(buf); + } + if (!Buffer.isBuffer(buf)) { + throw new TypeError('"list" argument must be an Array of Buffers'); + } + buf.copy(buffer, pos); + pos += buf.length; + } + return buffer; + }; + + function byteLength (string, encoding) { + if (Buffer.isBuffer(string)) { + return string.length; + } + if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) { + return string.byteLength; + } + if (typeof string !== 'string') { + throw new TypeError( + 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' + + 'Received type ' + typeof string + ); + } + + var len = string.length; + var mustMatch = (arguments.length > 2 && arguments[2] === true); + if (!mustMatch && len === 0) return 0; + + // Use a for loop to avoid recursion + var loweredCase = false; + for (;;) { + switch (encoding) { + case 'ascii': + case 'latin1': + case 'binary': + return len; + case 'utf8': + case 'utf-8': + return utf8ToBytes(string).length; + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return len * 2; + case 'hex': + return len >>> 1; + case 'base64': + return base64ToBytes(string).length; + default: + if (loweredCase) { + return mustMatch ? -1 : utf8ToBytes(string).length; // assume utf8 + } + encoding = ('' + encoding).toLowerCase(); + loweredCase = true; + } + } + } + Buffer.byteLength = byteLength; + + function slowToString (encoding, start, end) { + var loweredCase = false; + + // No need to verify that "this.length <= MAX_UINT32" since it's a read-only + // property of a typed array. + + // This behaves neither like String nor Uint8Array in that we set start/end + // to their upper/lower bounds if the value passed is out of range. + // undefined is handled specially as per ECMA-262 6th Edition, + // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization. + if (start === undefined || start < 0) { + start = 0; + } + // Return early if start > this.length. Done here to prevent potential uint32 + // coercion fail below. + if (start > this.length) { + return ''; + } + + if (end === undefined || end > this.length) { + end = this.length; + } + + if (end <= 0) { + return ''; + } + + // Force coersion to uint32. This will also coerce falsey/NaN values to 0. + end >>>= 0; + start >>>= 0; + + if (end <= start) { + return ''; + } + + if (!encoding) encoding = 'utf8'; + + while (true) { + switch (encoding) { + case 'hex': + return hexSlice(this, start, end); + + case 'utf8': + case 'utf-8': + return utf8Slice(this, start, end); + + case 'ascii': + return asciiSlice(this, start, end); + + case 'latin1': + case 'binary': + return latin1Slice(this, start, end); + + case 'base64': + return base64Slice(this, start, end); + + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return utf16leSlice(this, start, end); + + default: + if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding); + encoding = (encoding + '').toLowerCase(); + loweredCase = true; + } + } + } + +// This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package) +// to detect a Buffer instance. It's not possible to use `instanceof Buffer` +// reliably in a browserify context because there could be multiple different +// copies of the 'buffer' package in use. This method works even for Buffer +// instances that were created from another copy of the `buffer` package. +// See: https://github.com/feross/buffer/issues/154 + Buffer.prototype._isBuffer = true; + + function swap (b, n, m) { + var i = b[n]; + b[n] = b[m]; + b[m] = i; + } + + Buffer.prototype.swap16 = function swap16 () { + var len = this.length; + if (len % 2 !== 0) { + throw new RangeError('Buffer size must be a multiple of 16-bits'); + } + for (var i = 0; i < len; i += 2) { + swap(this, i, i + 1); + } + return this; + }; + + Buffer.prototype.swap32 = function swap32 () { + var len = this.length; + if (len % 4 !== 0) { + throw new RangeError('Buffer size must be a multiple of 32-bits'); + } + for (var i = 0; i < len; i += 4) { + swap(this, i, i + 3); + swap(this, i + 1, i + 2); + } + return this; + }; + + Buffer.prototype.swap64 = function swap64 () { + var len = this.length; + if (len % 8 !== 0) { + throw new RangeError('Buffer size must be a multiple of 64-bits'); + } + for (var i = 0; i < len; i += 8) { + swap(this, i, i + 7); + swap(this, i + 1, i + 6); + swap(this, i + 2, i + 5); + swap(this, i + 3, i + 4); + } + return this; + }; + + Buffer.prototype.toString = function toString () { + var length = this.length; + if (length === 0) return ''; + if (arguments.length === 0) return utf8Slice(this, 0, length); + return slowToString.apply(this, arguments); + }; + + Buffer.prototype.toLocaleString = Buffer.prototype.toString; + + Buffer.prototype.equals = function equals (b) { + if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer'); + if (this === b) return true; + return Buffer.compare(this, b) === 0; + }; + + Buffer.prototype.inspect = function inspect () { + var str = ''; + var max = exports.INSPECT_MAX_BYTES; + str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim(); + if (this.length > max) str += ' ... '; + return ''; + }; + + Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) { + if (isInstance(target, Uint8Array)) { + target = Buffer.from(target, target.offset, target.byteLength); + } + if (!Buffer.isBuffer(target)) { + throw new TypeError( + 'The "target" argument must be one of type Buffer or Uint8Array. ' + + 'Received type ' + (typeof target) + ); + } + + if (start === undefined) { + start = 0; + } + if (end === undefined) { + end = target ? target.length : 0; + } + if (thisStart === undefined) { + thisStart = 0; + } + if (thisEnd === undefined) { + thisEnd = this.length; + } + + if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) { + throw new RangeError('out of range index'); + } + + if (thisStart >= thisEnd && start >= end) { + return 0; + } + if (thisStart >= thisEnd) { + return -1; + } + if (start >= end) { + return 1; + } + + start >>>= 0; + end >>>= 0; + thisStart >>>= 0; + thisEnd >>>= 0; + + if (this === target) return 0; + + var x = thisEnd - thisStart; + var y = end - start; + var len = Math.min(x, y); + + var thisCopy = this.slice(thisStart, thisEnd); + var targetCopy = target.slice(start, end); + + for (var i = 0; i < len; ++i) { + if (thisCopy[i] !== targetCopy[i]) { + x = thisCopy[i]; + y = targetCopy[i]; + break; + } + } + + if (x < y) return -1; + if (y < x) return 1; + return 0; + }; + +// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`, +// OR the last index of `val` in `buffer` at offset <= `byteOffset`. +// +// Arguments: +// - buffer - a Buffer to search +// - val - a string, Buffer, or number +// - byteOffset - an index into `buffer`; will be clamped to an int32 +// - encoding - an optional encoding, relevant is val is a string +// - dir - true for indexOf, false for lastIndexOf + function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) { + // Empty buffer means no match + if (buffer.length === 0) return -1; + + // Normalize byteOffset + if (typeof byteOffset === 'string') { + encoding = byteOffset; + byteOffset = 0; + } else if (byteOffset > 0x7fffffff) { + byteOffset = 0x7fffffff; + } else if (byteOffset < -0x80000000) { + byteOffset = -0x80000000; + } + byteOffset = +byteOffset; // Coerce to Number. + if (numberIsNaN(byteOffset)) { + // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer + byteOffset = dir ? 0 : (buffer.length - 1); + } + + // Normalize byteOffset: negative offsets start from the end of the buffer + if (byteOffset < 0) byteOffset = buffer.length + byteOffset; + if (byteOffset >= buffer.length) { + if (dir) return -1; + else byteOffset = buffer.length - 1; + } else if (byteOffset < 0) { + if (dir) byteOffset = 0; + else return -1; + } + + // Normalize val + if (typeof val === 'string') { + val = Buffer.from(val, encoding); + } + + // Finally, search either indexOf (if dir is true) or lastIndexOf + if (Buffer.isBuffer(val)) { + // Special case: looking for empty string/buffer always fails + if (val.length === 0) { + return -1; + } + return arrayIndexOf(buffer, val, byteOffset, encoding, dir); + } else if (typeof val === 'number') { + val = val & 0xFF; // Search for a byte value [0-255] + if (typeof Uint8Array.prototype.indexOf === 'function') { + if (dir) { + return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset); + } else { + return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset); + } + } + return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir); + } + + throw new TypeError('val must be string, number or Buffer'); + } + + function arrayIndexOf (arr, val, byteOffset, encoding, dir) { + var indexSize = 1; + var arrLength = arr.length; + var valLength = val.length; + + if (encoding !== undefined) { + encoding = String(encoding).toLowerCase(); + if (encoding === 'ucs2' || encoding === 'ucs-2' || + encoding === 'utf16le' || encoding === 'utf-16le') { + if (arr.length < 2 || val.length < 2) { + return -1; + } + indexSize = 2; + arrLength /= 2; + valLength /= 2; + byteOffset /= 2; + } + } + + function read (buf, i) { + if (indexSize === 1) { + return buf[i]; + } else { + return buf.readUInt16BE(i * indexSize); + } + } + + var i; + if (dir) { + var foundIndex = -1; + for (i = byteOffset; i < arrLength; i++) { + if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) { + if (foundIndex === -1) foundIndex = i; + if (i - foundIndex + 1 === valLength) return foundIndex * indexSize; + } else { + if (foundIndex !== -1) i -= i - foundIndex; + foundIndex = -1; + } + } + } else { + if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength; + for (i = byteOffset; i >= 0; i--) { + var found = true; + for (var j = 0; j < valLength; j++) { + if (read(arr, i + j) !== read(val, j)) { + found = false; + break; + } + } + if (found) return i; + } + } + + return -1; + } + + Buffer.prototype.includes = function includes (val, byteOffset, encoding) { + return this.indexOf(val, byteOffset, encoding) !== -1; + }; + + Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) { + return bidirectionalIndexOf(this, val, byteOffset, encoding, true); + }; + + Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) { + return bidirectionalIndexOf(this, val, byteOffset, encoding, false); + }; + + function hexWrite (buf, string, offset, length) { + offset = Number(offset) || 0; + var remaining = buf.length - offset; + if (!length) { + length = remaining; + } else { + length = Number(length); + if (length > remaining) { + length = remaining; + } + } + + var strLen = string.length; + + if (length > strLen / 2) { + length = strLen / 2; + } + for (var i = 0; i < length; ++i) { + var parsed = parseInt(string.substr(i * 2, 2), 16); + if (numberIsNaN(parsed)) return i; + buf[offset + i] = parsed; + } + return i; + } + + function utf8Write (buf, string, offset, length) { + return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length); + } + + function asciiWrite (buf, string, offset, length) { + return blitBuffer(asciiToBytes(string), buf, offset, length); + } + + function latin1Write (buf, string, offset, length) { + return asciiWrite(buf, string, offset, length); + } + + function base64Write (buf, string, offset, length) { + return blitBuffer(base64ToBytes(string), buf, offset, length); + } + + function ucs2Write (buf, string, offset, length) { + return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length); + } + + Buffer.prototype.write = function write (string, offset, length, encoding) { + // Buffer#write(string) + if (offset === undefined) { + encoding = 'utf8'; + length = this.length; + offset = 0; + // Buffer#write(string, encoding) + } else if (length === undefined && typeof offset === 'string') { + encoding = offset; + length = this.length; + offset = 0; + // Buffer#write(string, offset[, length][, encoding]) + } else if (isFinite(offset)) { + offset = offset >>> 0; + if (isFinite(length)) { + length = length >>> 0; + if (encoding === undefined) encoding = 'utf8'; + } else { + encoding = length; + length = undefined; + } + } else { + throw new Error( + 'Buffer.write(string, encoding, offset[, length]) is no longer supported' + ); + } + + var remaining = this.length - offset; + if (length === undefined || length > remaining) length = remaining; + + if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { + throw new RangeError('Attempt to write outside buffer bounds'); + } + + if (!encoding) encoding = 'utf8'; + + var loweredCase = false; + for (;;) { + switch (encoding) { + case 'hex': + return hexWrite(this, string, offset, length); + + case 'utf8': + case 'utf-8': + return utf8Write(this, string, offset, length); + + case 'ascii': + return asciiWrite(this, string, offset, length); + + case 'latin1': + case 'binary': + return latin1Write(this, string, offset, length); + + case 'base64': + // Warning: maxLength not taken into account in base64Write + return base64Write(this, string, offset, length); + + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return ucs2Write(this, string, offset, length); + + default: + if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding); + encoding = ('' + encoding).toLowerCase(); + loweredCase = true; + } + } + }; + + Buffer.prototype.toJSON = function toJSON () { + return { + type: 'Buffer', + data: Array.prototype.slice.call(this._arr || this, 0) + }; + }; + + function base64Slice (buf, start, end) { + if (start === 0 && end === buf.length) { + return base64.fromByteArray(buf); + } else { + return base64.fromByteArray(buf.slice(start, end)); + } + } + + function utf8Slice (buf, start, end) { + end = Math.min(buf.length, end); + var res = []; + + var i = start; + while (i < end) { + var firstByte = buf[i]; + var codePoint = null; + var bytesPerSequence = (firstByte > 0xEF) ? 4 + : (firstByte > 0xDF) ? 3 + : (firstByte > 0xBF) ? 2 + : 1; + + if (i + bytesPerSequence <= end) { + var secondByte, thirdByte, fourthByte, tempCodePoint; + + switch (bytesPerSequence) { + case 1: + if (firstByte < 0x80) { + codePoint = firstByte; + } + break; + case 2: + secondByte = buf[i + 1]; + if ((secondByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F); + if (tempCodePoint > 0x7F) { + codePoint = tempCodePoint; + } + } + break; + case 3: + secondByte = buf[i + 1]; + thirdByte = buf[i + 2]; + if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F); + if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { + codePoint = tempCodePoint; + } + } + break; + case 4: + secondByte = buf[i + 1]; + thirdByte = buf[i + 2]; + fourthByte = buf[i + 3]; + if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F); + if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { + codePoint = tempCodePoint; + } + } + } + } + + if (codePoint === null) { + // we did not generate a valid codePoint so insert a + // replacement char (U+FFFD) and advance only 1 byte + codePoint = 0xFFFD; + bytesPerSequence = 1; + } else if (codePoint > 0xFFFF) { + // encode to utf16 (surrogate pair dance) + codePoint -= 0x10000; + res.push(codePoint >>> 10 & 0x3FF | 0xD800); + codePoint = 0xDC00 | codePoint & 0x3FF; + } + + res.push(codePoint); + i += bytesPerSequence; + } + + return decodeCodePointsArray(res); + } + +// Based on http://stackoverflow.com/a/22747272/680742, the browser with +// the lowest limit is Chrome, with 0x10000 args. +// We go 1 magnitude less, for safety + var MAX_ARGUMENTS_LENGTH = 0x1000; + + function decodeCodePointsArray (codePoints) { + var len = codePoints.length; + if (len <= MAX_ARGUMENTS_LENGTH) { + return String.fromCharCode.apply(String, codePoints); // avoid extra slice() + } + + // Decode in chunks to avoid "call stack size exceeded". + var res = ''; + var i = 0; + while (i < len) { + res += String.fromCharCode.apply( + String, + codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH) + ); + } + return res; + } + + function asciiSlice (buf, start, end) { + var ret = ''; + end = Math.min(buf.length, end); + + for (var i = start; i < end; ++i) { + ret += String.fromCharCode(buf[i] & 0x7F); + } + return ret; + } + + function latin1Slice (buf, start, end) { + var ret = ''; + end = Math.min(buf.length, end); + + for (var i = start; i < end; ++i) { + ret += String.fromCharCode(buf[i]); + } + return ret; + } + + function hexSlice (buf, start, end) { + var len = buf.length; + + if (!start || start < 0) start = 0; + if (!end || end < 0 || end > len) end = len; + + var out = ''; + for (var i = start; i < end; ++i) { + out += toHex(buf[i]); + } + return out; + } + + function utf16leSlice (buf, start, end) { + var bytes = buf.slice(start, end); + var res = ''; + for (var i = 0; i < bytes.length; i += 2) { + res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256)); + } + return res; + } + + Buffer.prototype.slice = function slice (start, end) { + var len = this.length; + start = ~~start; + end = end === undefined ? len : ~~end; + + if (start < 0) { + start += len; + if (start < 0) start = 0; + } else if (start > len) { + start = len; + } + + if (end < 0) { + end += len; + if (end < 0) end = 0; + } else if (end > len) { + end = len; + } + + if (end < start) end = start; + + var newBuf = this.subarray(start, end); + // Return an augmented `Uint8Array` instance + newBuf.__proto__ = Buffer.prototype; + return newBuf; + }; + +/* + * Need to make sure that buffer isn't trying to write out of bounds. + */ + function checkOffset (offset, ext, length) { + if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint'); + if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length'); + } + + Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) { + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) checkOffset(offset, byteLength, this.length); + + var val = this[offset]; + var mul = 1; + var i = 0; + while (++i < byteLength && (mul *= 0x100)) { + val += this[offset + i] * mul; + } + + return val; + }; + + Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) { + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) { + checkOffset(offset, byteLength, this.length); + } + + var val = this[offset + --byteLength]; + var mul = 1; + while (byteLength > 0 && (mul *= 0x100)) { + val += this[offset + --byteLength] * mul; + } + + return val; + }; + + Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 1, this.length); + return this[offset]; + }; + + Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 2, this.length); + return this[offset] | (this[offset + 1] << 8); + }; + + Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 2, this.length); + return (this[offset] << 8) | this[offset + 1]; + }; + + Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 4, this.length); + + return ((this[offset]) | + (this[offset + 1] << 8) | + (this[offset + 2] << 16)) + + (this[offset + 3] * 0x1000000); + }; + + Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 4, this.length); + + return (this[offset] * 0x1000000) + + ((this[offset + 1] << 16) | + (this[offset + 2] << 8) | + this[offset + 3]); + }; + + Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) { + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) checkOffset(offset, byteLength, this.length); + + var val = this[offset]; + var mul = 1; + var i = 0; + while (++i < byteLength && (mul *= 0x100)) { + val += this[offset + i] * mul; + } + mul *= 0x80; + + if (val >= mul) val -= Math.pow(2, 8 * byteLength); + + return val; + }; + + Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) { + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) checkOffset(offset, byteLength, this.length); + + var i = byteLength; + var mul = 1; + var val = this[offset + --i]; + while (i > 0 && (mul *= 0x100)) { + val += this[offset + --i] * mul; + } + mul *= 0x80; + + if (val >= mul) val -= Math.pow(2, 8 * byteLength); + + return val; + }; + + Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 1, this.length); + if (!(this[offset] & 0x80)) return (this[offset]); + return ((0xff - this[offset] + 1) * -1); + }; + + Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 2, this.length); + var val = this[offset] | (this[offset + 1] << 8); + return (val & 0x8000) ? val | 0xFFFF0000 : val; + }; + + Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 2, this.length); + var val = this[offset + 1] | (this[offset] << 8); + return (val & 0x8000) ? val | 0xFFFF0000 : val; + }; + + Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 4, this.length); + + return (this[offset]) | + (this[offset + 1] << 8) | + (this[offset + 2] << 16) | + (this[offset + 3] << 24); + }; + + Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 4, this.length); + + return (this[offset] << 24) | + (this[offset + 1] << 16) | + (this[offset + 2] << 8) | + (this[offset + 3]); + }; + + Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 4, this.length); + return ieee754.read(this, offset, true, 23, 4); + }; + + Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 4, this.length); + return ieee754.read(this, offset, false, 23, 4); + }; + + Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 8, this.length); + return ieee754.read(this, offset, true, 52, 8); + }; + + Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 8, this.length); + return ieee754.read(this, offset, false, 52, 8); + }; + + function checkInt (buf, value, offset, ext, max, min) { + if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance'); + if (value > max || value < min) throw new RangeError('"value" argument is out of bounds'); + if (offset + ext > buf.length) throw new RangeError('Index out of range'); + } + + Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) { + value = +value; + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) { + var maxBytes = Math.pow(2, 8 * byteLength) - 1; + checkInt(this, value, offset, byteLength, maxBytes, 0); + } + + var mul = 1; + var i = 0; + this[offset] = value & 0xFF; + while (++i < byteLength && (mul *= 0x100)) { + this[offset + i] = (value / mul) & 0xFF; + } + + return offset + byteLength; + }; + + Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) { + value = +value; + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) { + var maxBytes = Math.pow(2, 8 * byteLength) - 1; + checkInt(this, value, offset, byteLength, maxBytes, 0); + } + + var i = byteLength - 1; + var mul = 1; + this[offset + i] = value & 0xFF; + while (--i >= 0 && (mul *= 0x100)) { + this[offset + i] = (value / mul) & 0xFF; + } + + return offset + byteLength; + }; + + Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0); + this[offset] = (value & 0xff); + return offset + 1; + }; + + Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0); + this[offset] = (value & 0xff); + this[offset + 1] = (value >>> 8); + return offset + 2; + }; + + Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0); + this[offset] = (value >>> 8); + this[offset + 1] = (value & 0xff); + return offset + 2; + }; + + Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0); + this[offset + 3] = (value >>> 24); + this[offset + 2] = (value >>> 16); + this[offset + 1] = (value >>> 8); + this[offset] = (value & 0xff); + return offset + 4; + }; + + Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0); + this[offset] = (value >>> 24); + this[offset + 1] = (value >>> 16); + this[offset + 2] = (value >>> 8); + this[offset + 3] = (value & 0xff); + return offset + 4; + }; + + Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) { + var limit = Math.pow(2, (8 * byteLength) - 1); + + checkInt(this, value, offset, byteLength, limit - 1, -limit); + } + + var i = 0; + var mul = 1; + var sub = 0; + this[offset] = value & 0xFF; + while (++i < byteLength && (mul *= 0x100)) { + if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) { + sub = 1; + } + this[offset + i] = ((value / mul) >> 0) - sub & 0xFF; + } + + return offset + byteLength; + }; + + Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) { + var limit = Math.pow(2, (8 * byteLength) - 1); + + checkInt(this, value, offset, byteLength, limit - 1, -limit); + } + + var i = byteLength - 1; + var mul = 1; + var sub = 0; + this[offset + i] = value & 0xFF; + while (--i >= 0 && (mul *= 0x100)) { + if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) { + sub = 1; + } + this[offset + i] = ((value / mul) >> 0) - sub & 0xFF; + } + + return offset + byteLength; + }; + + Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80); + if (value < 0) value = 0xff + value + 1; + this[offset] = (value & 0xff); + return offset + 1; + }; + + Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000); + this[offset] = (value & 0xff); + this[offset + 1] = (value >>> 8); + return offset + 2; + }; + + Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000); + this[offset] = (value >>> 8); + this[offset + 1] = (value & 0xff); + return offset + 2; + }; + + Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000); + this[offset] = (value & 0xff); + this[offset + 1] = (value >>> 8); + this[offset + 2] = (value >>> 16); + this[offset + 3] = (value >>> 24); + return offset + 4; + }; + + Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000); + if (value < 0) value = 0xffffffff + value + 1; + this[offset] = (value >>> 24); + this[offset + 1] = (value >>> 16); + this[offset + 2] = (value >>> 8); + this[offset + 3] = (value & 0xff); + return offset + 4; + }; + + function checkIEEE754 (buf, value, offset, ext, max, min) { + if (offset + ext > buf.length) throw new RangeError('Index out of range'); + if (offset < 0) throw new RangeError('Index out of range'); + } + + function writeFloat (buf, value, offset, littleEndian, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) { + checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38); + } + ieee754.write(buf, value, offset, littleEndian, 23, 4); + return offset + 4; + } + + Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) { + return writeFloat(this, value, offset, true, noAssert); + }; + + Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) { + return writeFloat(this, value, offset, false, noAssert); + }; + + function writeDouble (buf, value, offset, littleEndian, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) { + checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308); + } + ieee754.write(buf, value, offset, littleEndian, 52, 8); + return offset + 8; + } + + Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) { + return writeDouble(this, value, offset, true, noAssert); + }; + + Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) { + return writeDouble(this, value, offset, false, noAssert); + }; + +// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) + Buffer.prototype.copy = function copy (target, targetStart, start, end) { + if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer'); + if (!start) start = 0; + if (!end && end !== 0) end = this.length; + if (targetStart >= target.length) targetStart = target.length; + if (!targetStart) targetStart = 0; + if (end > 0 && end < start) end = start; + + // Copy 0 bytes; we're done + if (end === start) return 0; + if (target.length === 0 || this.length === 0) return 0; + + // Fatal error conditions + if (targetStart < 0) { + throw new RangeError('targetStart out of bounds'); + } + if (start < 0 || start >= this.length) throw new RangeError('Index out of range'); + if (end < 0) throw new RangeError('sourceEnd out of bounds'); + + // Are we oob? + if (end > this.length) end = this.length; + if (target.length - targetStart < end - start) { + end = target.length - targetStart + start; + } + + var len = end - start; + + if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') { + // Use built-in when available, missing from IE11 + this.copyWithin(targetStart, start, end); + } else if (this === target && start < targetStart && targetStart < end) { + // descending copy from end + for (var i = len - 1; i >= 0; --i) { + target[i + targetStart] = this[i + start]; + } + } else { + Uint8Array.prototype.set.call( + target, + this.subarray(start, end), + targetStart + ); + } + + return len; + }; + +// Usage: +// buffer.fill(number[, offset[, end]]) +// buffer.fill(buffer[, offset[, end]]) +// buffer.fill(string[, offset[, end]][, encoding]) + Buffer.prototype.fill = function fill (val, start, end, encoding) { + // Handle string cases: + if (typeof val === 'string') { + if (typeof start === 'string') { + encoding = start; + start = 0; + end = this.length; + } else if (typeof end === 'string') { + encoding = end; + end = this.length; + } + if (encoding !== undefined && typeof encoding !== 'string') { + throw new TypeError('encoding must be a string'); + } + if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { + throw new TypeError('Unknown encoding: ' + encoding); + } + if (val.length === 1) { + var code = val.charCodeAt(0); + if ((encoding === 'utf8' && code < 128) || + encoding === 'latin1') { + // Fast path: If `val` fits into a single byte, use that numeric value. + val = code; + } + } + } else if (typeof val === 'number') { + val = val & 255; + } + + // Invalid ranges are not set to a default, so can range check early. + if (start < 0 || this.length < start || this.length < end) { + throw new RangeError('Out of range index'); + } + + if (end <= start) { + return this; + } + + start = start >>> 0; + end = end === undefined ? this.length : end >>> 0; + + if (!val) val = 0; + + var i; + if (typeof val === 'number') { + for (i = start; i < end; ++i) { + this[i] = val; + } + } else { + var bytes = Buffer.isBuffer(val) + ? val + : Buffer.from(val, encoding); + var len = bytes.length; + if (len === 0) { + throw new TypeError('The value "' + val + + '" is invalid for argument "value"'); + } + for (i = 0; i < end - start; ++i) { + this[i + start] = bytes[i % len]; + } + } + + return this; + }; + +// HELPER FUNCTIONS +// ================ + + var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g; + + function base64clean (str) { + // Node takes equal signs as end of the Base64 encoding + str = str.split('=')[0]; + // Node strips out invalid characters like \n and \t from the string, base64-js does not + str = str.trim().replace(INVALID_BASE64_RE, ''); + // Node converts strings with length < 2 to '' + if (str.length < 2) return ''; + // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not + while (str.length % 4 !== 0) { + str = str + '='; + } + return str; + } + + function toHex (n) { + if (n < 16) return '0' + n.toString(16); + return n.toString(16); + } + + function utf8ToBytes (string, units) { + units = units || Infinity; + var codePoint; + var length = string.length; + var leadSurrogate = null; + var bytes = []; + + for (var i = 0; i < length; ++i) { + codePoint = string.charCodeAt(i); + + // is surrogate component + if (codePoint > 0xD7FF && codePoint < 0xE000) { + // last char was a lead + if (!leadSurrogate) { + // no lead yet + if (codePoint > 0xDBFF) { + // unexpected trail + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD); + continue; + } else if (i + 1 === length) { + // unpaired lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD); + continue; + } + + // valid lead + leadSurrogate = codePoint; + + continue; + } + + // 2 leads in a row + if (codePoint < 0xDC00) { + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD); + leadSurrogate = codePoint; + continue; + } + + // valid surrogate pair + codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000; + } else if (leadSurrogate) { + // valid bmp char, but last char was a lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD); + } + + leadSurrogate = null; + + // encode utf8 + if (codePoint < 0x80) { + if ((units -= 1) < 0) break; + bytes.push(codePoint); + } else if (codePoint < 0x800) { + if ((units -= 2) < 0) break; + bytes.push( + codePoint >> 0x6 | 0xC0, + codePoint & 0x3F | 0x80 + ); + } else if (codePoint < 0x10000) { + if ((units -= 3) < 0) break; + bytes.push( + codePoint >> 0xC | 0xE0, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ); + } else if (codePoint < 0x110000) { + if ((units -= 4) < 0) break; + bytes.push( + codePoint >> 0x12 | 0xF0, + codePoint >> 0xC & 0x3F | 0x80, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ); + } else { + throw new Error('Invalid code point'); + } + } + + return bytes; + } + + function asciiToBytes (str) { + var byteArray = []; + for (var i = 0; i < str.length; ++i) { + // Node's code seems to be doing this and not & 0x7F.. + byteArray.push(str.charCodeAt(i) & 0xFF); + } + return byteArray; + } + + function utf16leToBytes (str, units) { + var c, hi, lo; + var byteArray = []; + for (var i = 0; i < str.length; ++i) { + if ((units -= 2) < 0) break; + + c = str.charCodeAt(i); + hi = c >> 8; + lo = c % 256; + byteArray.push(lo); + byteArray.push(hi); + } + + return byteArray; + } + + function base64ToBytes (str) { + return base64.toByteArray(base64clean(str)); + } + + function blitBuffer (src, dst, offset, length) { + for (var i = 0; i < length; ++i) { + if ((i + offset >= dst.length) || (i >= src.length)) break; + dst[i + offset] = src[i]; + } + return i; + } + +// ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass +// the `instanceof` check but they should be treated as of that type. +// See: https://github.com/feross/buffer/issues/166 + function isInstance (obj, type) { + return obj instanceof type || + (obj != null && obj.constructor != null && obj.constructor.name != null && + obj.constructor.name === type.name); + } + function numberIsNaN (obj) { + // For IE11 support + return obj !== obj; // eslint-disable-line no-self-compare + } + + }).call(this, require('buffer').Buffer); + }, {'base64-js': 39, 'buffer': 43, 'ieee754': 55}], + 44: [function (require, module, exports) { + (function (Buffer) { +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// NOTE: These type checking functions intentionally don't use `instanceof` +// because it is fragile and can be easily faked with `Object.create()`. + + function isArray (arg) { + if (Array.isArray) { + return Array.isArray(arg); + } + return objectToString(arg) === '[object Array]'; + } + exports.isArray = isArray; + + function isBoolean (arg) { + return typeof arg === 'boolean'; + } + exports.isBoolean = isBoolean; + + function isNull (arg) { + return arg === null; + } + exports.isNull = isNull; + + function isNullOrUndefined (arg) { + return arg == null; + } + exports.isNullOrUndefined = isNullOrUndefined; + + function isNumber (arg) { + return typeof arg === 'number'; + } + exports.isNumber = isNumber; + + function isString (arg) { + return typeof arg === 'string'; + } + exports.isString = isString; + + function isSymbol (arg) { + return typeof arg === 'symbol'; + } + exports.isSymbol = isSymbol; + + function isUndefined (arg) { + return arg === void 0; + } + exports.isUndefined = isUndefined; + + function isRegExp (re) { + return objectToString(re) === '[object RegExp]'; + } + exports.isRegExp = isRegExp; + + function isObject (arg) { + return typeof arg === 'object' && arg !== null; + } + exports.isObject = isObject; + + function isDate (d) { + return objectToString(d) === '[object Date]'; + } + exports.isDate = isDate; + + function isError (e) { + return (objectToString(e) === '[object Error]' || e instanceof Error); + } + exports.isError = isError; + + function isFunction (arg) { + return typeof arg === 'function'; + } + exports.isFunction = isFunction; + + function isPrimitive (arg) { + return arg === null || + typeof arg === 'boolean' || + typeof arg === 'number' || + typeof arg === 'string' || + typeof arg === 'symbol' || // ES6 symbol + typeof arg === 'undefined'; + } + exports.isPrimitive = isPrimitive; + + exports.isBuffer = Buffer.isBuffer; + + function objectToString (o) { + return Object.prototype.toString.call(o); + } + + }).call(this, {'isBuffer': require('../../is-buffer/index.js')}); + }, {'../../is-buffer/index.js': 57}], + 45: [function (require, module, exports) { + (function (process) { + 'use strict'; + + function _typeof (obj) { if (typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol') { _typeof = function _typeof (obj) { return typeof obj; }; } else { _typeof = function _typeof (obj) { return obj && typeof Symbol === 'function' && obj.constructor === Symbol && obj !== Symbol.prototype ? 'symbol' : typeof obj; }; } return _typeof(obj); } + +/* eslint-env browser */ + +/** + * This is the web browser implementation of `debug()`. + */ + exports.log = log; + exports.formatArgs = formatArgs; + exports.save = save; + exports.load = load; + exports.useColors = useColors; + exports.storage = localstorage(); +/** + * Colors. + */ + + exports.colors = ['#0000CC', '#0000FF', '#0033CC', '#0033FF', '#0066CC', '#0066FF', '#0099CC', '#0099FF', '#00CC00', '#00CC33', '#00CC66', '#00CC99', '#00CCCC', '#00CCFF', '#3300CC', '#3300FF', '#3333CC', '#3333FF', '#3366CC', '#3366FF', '#3399CC', '#3399FF', '#33CC00', '#33CC33', '#33CC66', '#33CC99', '#33CCCC', '#33CCFF', '#6600CC', '#6600FF', '#6633CC', '#6633FF', '#66CC00', '#66CC33', '#9900CC', '#9900FF', '#9933CC', '#9933FF', '#99CC00', '#99CC33', '#CC0000', '#CC0033', '#CC0066', '#CC0099', '#CC00CC', '#CC00FF', '#CC3300', '#CC3333', '#CC3366', '#CC3399', '#CC33CC', '#CC33FF', '#CC6600', '#CC6633', '#CC9900', '#CC9933', '#CCCC00', '#CCCC33', '#FF0000', '#FF0033', '#FF0066', '#FF0099', '#FF00CC', '#FF00FF', '#FF3300', '#FF3333', '#FF3366', '#FF3399', '#FF33CC', '#FF33FF', '#FF6600', '#FF6633', '#FF9900', '#FF9933', '#FFCC00', '#FFCC33']; +/** + * Currently only WebKit-based Web Inspectors, Firefox >= v31, + * and the Firebug extension (any Firefox version) are known + * to support "%c" CSS customizations. + * + * TODO: add a `localStorage` variable to explicitly enable/disable colors + */ +// eslint-disable-next-line complexity + + function useColors () { + // NB: In an Electron preload script, document will be defined but not fully + // initialized. Since we know we're in Chrome, we'll just detect this case + // explicitly + if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) { + return true; + } // Internet Explorer and Edge do not support colors. + + if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) { + return false; + } // Is webkit? http://stackoverflow.com/a/16459606/376773 + // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632 + + return typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance || // Is firebug? http://stackoverflow.com/a/398120/376773 + typeof window !== 'undefined' && window.console && (window.console.firebug || window.console.exception && window.console.table) || // Is firefox >= v31? + // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages + typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31 || // Double check webkit in userAgent just in case we are in a worker + typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/); + } +/** + * Colorize log arguments if enabled. + * + * @api public + */ + + function formatArgs (args) { + args[0] = (this.useColors ? '%c' : '') + this.namespace + (this.useColors ? ' %c' : ' ') + args[0] + (this.useColors ? '%c ' : ' ') + '+' + module.exports.humanize(this.diff); + + if (!this.useColors) { + return; + } + + var c = 'color: ' + this.color; + args.splice(1, 0, c, 'color: inherit'); // The final "%c" is somewhat tricky, because there could be other + // arguments passed either before or after the %c, so we need to + // figure out the correct index to insert the CSS into + + var index = 0; + var lastC = 0; + args[0].replace(/%[a-zA-Z%]/g, function (match) { + if (match === '%%') { + return; + } + + index++; + + if (match === '%c') { + // We only are interested in the *last* %c + // (the user may have provided their own) + lastC = index; + } + }); + args.splice(lastC, 0, c); + } +/** + * Invokes `console.log()` when available. + * No-op when `console.log` is not a "function". + * + * @api public + */ + + function log () { + var _console; + + // This hackery is required for IE8/9, where + // the `console.log` function doesn't have 'apply' + return (typeof console === 'undefined' ? 'undefined' : _typeof(console)) === 'object' && console.log && (_console = console).log.apply(_console, arguments); + } +/** + * Save `namespaces`. + * + * @param {String} namespaces + * @api private + */ + + function save (namespaces) { + try { + if (namespaces) { + exports.storage.setItem('debug', namespaces); + } else { + exports.storage.removeItem('debug'); + } + } catch (error) { // Swallow + // XXX (@Qix-) should we be logging these? + } + } +/** + * Load `namespaces`. + * + * @return {String} returns the previously persisted debug modes + * @api private + */ + + function load () { + var r; + + try { + r = exports.storage.getItem('debug'); + } catch (error) {} // Swallow + // XXX (@Qix-) should we be logging these? + // If debug isn't set in LS, and we're in Electron, try to load $DEBUG + + if (!r && typeof process !== 'undefined' && 'env' in process) { + r = process.env.DEBUG; + } + + return r; + } +/** + * Localstorage attempts to return the localstorage. + * + * This is necessary because safari throws + * when a user disables cookies/localstorage + * and you attempt to access it. + * + * @return {LocalStorage} + * @api private + */ + + function localstorage () { + try { + // TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context + // The Browser also has localStorage in the global context. + return localStorage; + } catch (error) { // Swallow + // XXX (@Qix-) should we be logging these? + } + } + + module.exports = require('./common')(exports); + var formatters = module.exports.formatters; +/** + * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. + */ + + formatters.j = function (v) { + try { + return JSON.stringify(v); + } catch (error) { + return '[UnexpectedJSONParseError]: ' + error.message; + } + }; + + }).call(this, require('_process')); + }, {'./common': 46, '_process': 70}], + 46: [function (require, module, exports) { + 'use strict'; + +/** + * This is the common logic for both the Node.js and web browser + * implementations of `debug()`. + */ + function setup (env) { + createDebug.debug = createDebug; + createDebug.default = createDebug; + createDebug.coerce = coerce; + createDebug.disable = disable; + createDebug.enable = enable; + createDebug.enabled = enabled; + createDebug.humanize = require('ms'); + Object.keys(env).forEach(function (key) { + createDebug[key] = env[key]; + }); + /** + * Active `debug` instances. + */ + + createDebug.instances = []; + /** + * The currently active debug mode names, and names to skip. + */ + + createDebug.names = []; + createDebug.skips = []; + /** + * Map of special "%n" handling functions, for the debug "format" argument. + * + * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N". + */ + + createDebug.formatters = {}; + /** + * Selects a color for a debug namespace + * @param {String} namespace The namespace string for the for the debug instance to be colored + * @return {Number|String} An ANSI color code for the given namespace + * @api private + */ + + function selectColor (namespace) { + var hash = 0; + + for (var i = 0; i < namespace.length; i++) { + hash = (hash << 5) - hash + namespace.charCodeAt(i); + hash |= 0; // Convert to 32bit integer + } + + return createDebug.colors[Math.abs(hash) % createDebug.colors.length]; + } + + createDebug.selectColor = selectColor; + /** + * Create a debugger with the given `namespace`. + * + * @param {String} namespace + * @return {Function} + * @api public + */ + + function createDebug (namespace) { + var prevTime; + + function debug () { + // Disabled? + if (!debug.enabled) { + return; + } + + for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { + args[_key] = arguments[_key]; + } + + var self = debug; // Set `diff` timestamp + + var curr = Number(new Date()); + var ms = curr - (prevTime || curr); + self.diff = ms; + self.prev = prevTime; + self.curr = curr; + prevTime = curr; + args[0] = createDebug.coerce(args[0]); + + if (typeof args[0] !== 'string') { + // Anything else let's inspect with %O + args.unshift('%O'); + } // Apply any `formatters` transformations + + var index = 0; + args[0] = args[0].replace(/%([a-zA-Z%])/g, function (match, format) { + // If we encounter an escaped % then don't increase the array index + if (match === '%%') { + return match; + } + + index++; + var formatter = createDebug.formatters[format]; + + if (typeof formatter === 'function') { + var val = args[index]; + match = formatter.call(self, val); // Now we need to remove `args[index]` since it's inlined in the `format` + + args.splice(index, 1); + index--; + } + + return match; + }); // Apply env-specific formatting (colors, etc.) + + createDebug.formatArgs.call(self, args); + var logFn = self.log || createDebug.log; + logFn.apply(self, args); + } + + debug.namespace = namespace; + debug.enabled = createDebug.enabled(namespace); + debug.useColors = createDebug.useColors(); + debug.color = selectColor(namespace); + debug.destroy = destroy; + debug.extend = extend; // Debug.formatArgs = formatArgs; + // debug.rawLog = rawLog; + // env-specific initialization logic for debug instances + + if (typeof createDebug.init === 'function') { + createDebug.init(debug); + } + + createDebug.instances.push(debug); + return debug; + } + + function destroy () { + var index = createDebug.instances.indexOf(this); + + if (index !== -1) { + createDebug.instances.splice(index, 1); + return true; + } + + return false; + } + + function extend (namespace, delimiter) { + return createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace); + } + /** + * Enables a debug mode by namespaces. This can include modes + * separated by a colon and wildcards. + * + * @param {String} namespaces + * @api public + */ + + function enable (namespaces) { + createDebug.save(namespaces); + createDebug.names = []; + createDebug.skips = []; + var i; + var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/); + var len = split.length; + + for (i = 0; i < len; i++) { + if (!split[i]) { + // ignore empty strings + continue; + } + + namespaces = split[i].replace(/\*/g, '.*?'); + + if (namespaces[0] === '-') { + createDebug.skips.push(new RegExp('^' + namespaces.substr(1) + '$')); + } else { + createDebug.names.push(new RegExp('^' + namespaces + '$')); + } + } + + for (i = 0; i < createDebug.instances.length; i++) { + var instance = createDebug.instances[i]; + instance.enabled = createDebug.enabled(instance.namespace); + } + } + /** + * Disable debug output. + * + * @api public + */ + + function disable () { + createDebug.enable(''); + } + /** + * Returns true if the given mode name is enabled, false otherwise. + * + * @param {String} name + * @return {Boolean} + * @api public + */ + + function enabled (name) { + if (name[name.length - 1] === '*') { + return true; + } + + var i; + var len; + + for (i = 0, len = createDebug.skips.length; i < len; i++) { + if (createDebug.skips[i].test(name)) { + return false; + } + } + + for (i = 0, len = createDebug.names.length; i < len; i++) { + if (createDebug.names[i].test(name)) { + return true; + } + } + + return false; + } + /** + * Coerce `val`. + * + * @param {Mixed} val + * @return {Mixed} + * @api private + */ + + function coerce (val) { + if (val instanceof Error) { + return val.stack || val.message; + } + + return val; + } + + createDebug.enable(createDebug.load()); + return createDebug; + } + + module.exports = setup; + + }, {'ms': 60}], + 47: [function (require, module, exports) { + 'use strict'; + + var keys = require('object-keys'); + var hasSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol'; + + var toStr = Object.prototype.toString; + var concat = Array.prototype.concat; + var origDefineProperty = Object.defineProperty; + + var isFunction = function (fn) { + return typeof fn === 'function' && toStr.call(fn) === '[object Function]'; + }; + + var arePropertyDescriptorsSupported = function () { + var obj = {}; + try { + origDefineProperty(obj, 'x', { enumerable: false, value: obj }); + // eslint-disable-next-line no-unused-vars, no-restricted-syntax + for (var _ in obj) { // jscs:ignore disallowUnusedVariables + return false; + } + return obj.x === obj; + } catch (e) { /* this is IE 8. */ + return false; + } + }; + var supportsDescriptors = origDefineProperty && arePropertyDescriptorsSupported(); + + var defineProperty = function (object, name, value, predicate) { + if (name in object && (!isFunction(predicate) || !predicate())) { + return; + } + if (supportsDescriptors) { + origDefineProperty(object, name, { + configurable: true, + enumerable: false, + value: value, + writable: true + }); + } else { + object[name] = value; + } + }; + + var defineProperties = function (object, map) { + var predicates = arguments.length > 2 ? arguments[2] : {}; + var props = keys(map); + if (hasSymbols) { + props = concat.call(props, Object.getOwnPropertySymbols(map)); + } + for (var i = 0; i < props.length; i += 1) { + defineProperty(object, props[i], map[props[i]], predicates[props[i]]); + } + }; + + defineProperties.supportsDescriptors = !!supportsDescriptors; + + module.exports = defineProperties; + + }, {'object-keys': 62}], + 48: [function (require, module, exports) { +/*! + + diff v3.5.0 + +Software License Agreement (BSD License) + +Copyright (c) 2009-2015, Kevin Decker + +All rights reserved. + +Redistribution and use of this software in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above + copyright notice, this list of conditions and the + following disclaimer. + +* Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the + following disclaimer in the documentation and/or other + materials provided with the distribution. + +* Neither the name of Kevin Decker nor the names of its + contributors may be used to endorse or promote products + derived from this software without specific prior + written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +@license +*/ + (function webpackUniversalModuleDefinition (root, factory) { + if (typeof exports === 'object' && typeof module === 'object') { module.exports = factory(); } else if (false) { define([], factory); } else if (typeof exports === 'object') { exports['JsDiff'] = factory(); } else { root['JsDiff'] = factory(); } + })(this, function () { + return /******/ (function (modules) { // webpackBootstrap +/******/ // The module cache + /******/ var installedModules = {}; + +/******/ // The require function + /******/ function __webpack_require__ (moduleId) { + +/******/ // Check if module is in cache + /******/ if (installedModules[moduleId]) + /******/ { return installedModules[moduleId].exports; } + +/******/ // Create a new module (and put it into the cache) + /******/ var module = installedModules[moduleId] = { + /******/ exports: {}, + /******/ id: moduleId, + /******/ loaded: false + /******/ }; + +/******/ // Execute the module function + /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); + +/******/ // Flag the module as loaded + /******/ module.loaded = true; + +/******/ // Return the exports of the module + /******/ return module.exports; + /******/ } + +/******/ // expose the modules object (__webpack_modules__) + /******/ __webpack_require__.m = modules; + +/******/ // expose the module cache + /******/ __webpack_require__.c = installedModules; + +/******/ // __webpack_public_path__ + /******/ __webpack_require__.p = ''; + +/******/ // Load entry module and return exports + /******/ return __webpack_require__(0); + /******/ }) +/************************************************************************/ +/******/ ([ +/* 0 */ + /***/ function (module, exports, __webpack_require__) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.canonicalize = exports.convertChangesToXML = exports.convertChangesToDMP = exports.merge = exports.parsePatch = exports.applyPatches = exports.applyPatch = exports.createPatch = exports.createTwoFilesPatch = exports.structuredPatch = exports.diffArrays = exports.diffJson = exports.diffCss = exports.diffSentences = exports.diffTrimmedLines = exports.diffLines = exports.diffWordsWithSpace = exports.diffWords = exports.diffChars = exports.Diff = undefined; + + /* istanbul ignore end */var /* istanbul ignore start */_base = __webpack_require__(1); + + /* istanbul ignore start */var _base2 = _interopRequireDefault(_base); + + /* istanbul ignore end */var /* istanbul ignore start */_character = __webpack_require__(2); + + var /* istanbul ignore start */_word = __webpack_require__(3); + + var /* istanbul ignore start */_line = __webpack_require__(5); + + var /* istanbul ignore start */_sentence = __webpack_require__(6); + + var /* istanbul ignore start */_css = __webpack_require__(7); + + var /* istanbul ignore start */_json = __webpack_require__(8); + + var /* istanbul ignore start */_array = __webpack_require__(9); + + var /* istanbul ignore start */_apply = __webpack_require__(10); + + var /* istanbul ignore start */_parse = __webpack_require__(11); + + var /* istanbul ignore start */_merge = __webpack_require__(13); + + var /* istanbul ignore start */_create = __webpack_require__(14); + + var /* istanbul ignore start */_dmp = __webpack_require__(16); + + var /* istanbul ignore start */_xml = __webpack_require__(17); + + /* istanbul ignore start */function _interopRequireDefault (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + + /* See LICENSE file for terms of use */ + + /* + * Text diff implementation. + * + * This library supports the following APIS: + * JsDiff.diffChars: Character by character diff + * JsDiff.diffWords: Word (as defined by \b regex) diff which ignores whitespace + * JsDiff.diffLines: Line based diff + * + * JsDiff.diffCss: Diff targeted at CSS content + * + * These methods are based on the implementation proposed in + * "An O(ND) Difference Algorithm and its Variations" (Myers, 1986). + * http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927 + */ + exports.Diff = _base2['default']; + /* istanbul ignore start */exports.diffChars = _character.diffChars; + /* istanbul ignore start */exports.diffWords = _word.diffWords; + /* istanbul ignore start */exports.diffWordsWithSpace = _word.diffWordsWithSpace; + /* istanbul ignore start */exports.diffLines = _line.diffLines; + /* istanbul ignore start */exports.diffTrimmedLines = _line.diffTrimmedLines; + /* istanbul ignore start */exports.diffSentences = _sentence.diffSentences; + /* istanbul ignore start */exports.diffCss = _css.diffCss; + /* istanbul ignore start */exports.diffJson = _json.diffJson; + /* istanbul ignore start */exports.diffArrays = _array.diffArrays; + /* istanbul ignore start */exports.structuredPatch = _create.structuredPatch; + /* istanbul ignore start */exports.createTwoFilesPatch = _create.createTwoFilesPatch; + /* istanbul ignore start */exports.createPatch = _create.createPatch; + /* istanbul ignore start */exports.applyPatch = _apply.applyPatch; + /* istanbul ignore start */exports.applyPatches = _apply.applyPatches; + /* istanbul ignore start */exports.parsePatch = _parse.parsePatch; + /* istanbul ignore start */exports.merge = _merge.merge; + /* istanbul ignore start */exports.convertChangesToDMP = _dmp.convertChangesToDMP; + /* istanbul ignore start */exports.convertChangesToXML = _xml.convertChangesToXML; + /* istanbul ignore start */exports.canonicalize = _json.canonicalize; + + /***/ }, +/* 1 */ + /***/ function (module, exports) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports['default'] = /* istanbul ignore end */Diff; + function Diff () {} + + Diff.prototype = { + /* istanbul ignore start */ /* istanbul ignore end */diff: function diff (oldString, newString) { + /* istanbul ignore start */var /* istanbul ignore end */options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; + + var callback = options.callback; + if (typeof options === 'function') { + callback = options; + options = {}; + } + this.options = options; + + var self = this; + + function done (value) { + if (callback) { + setTimeout(function () { + callback(undefined, value); + }, 0); + return true; + } else { + return value; + } + } + + // Allow subclasses to massage the input prior to running + oldString = this.castInput(oldString); + newString = this.castInput(newString); + + oldString = this.removeEmpty(this.tokenize(oldString)); + newString = this.removeEmpty(this.tokenize(newString)); + + var newLen = newString.length, + oldLen = oldString.length; + var editLength = 1; + var maxEditLength = newLen + oldLen; + var bestPath = [{ newPos: -1, components: [] }]; + + // Seed editLength = 0, i.e. the content starts with the same values + var oldPos = this.extractCommon(bestPath[0], newString, oldString, 0); + if (bestPath[0].newPos + 1 >= newLen && oldPos + 1 >= oldLen) { + // Identity per the equality and tokenizer + return done([{ value: this.join(newString), count: newString.length }]); + } + + // Main worker method. checks all permutations of a given edit length for acceptance. + function execEditLength () { + for (var diagonalPath = -1 * editLength; diagonalPath <= editLength; diagonalPath += 2) { + var basePath = /* istanbul ignore start */void 0; + var addPath = bestPath[diagonalPath - 1], + removePath = bestPath[diagonalPath + 1], + _oldPos = (removePath ? removePath.newPos : 0) - diagonalPath; + if (addPath) { + // No one else is going to attempt to use this value, clear it + bestPath[diagonalPath - 1] = undefined; + } + + var canAdd = addPath && addPath.newPos + 1 < newLen, + canRemove = removePath && _oldPos >= 0 && _oldPos < oldLen; + if (!canAdd && !canRemove) { + // If this path is a terminal then prune + bestPath[diagonalPath] = undefined; + continue; + } + + // Select the diagonal that we want to branch from. We select the prior + // path whose position in the new string is the farthest from the origin + // and does not pass the bounds of the diff graph + if (!canAdd || canRemove && addPath.newPos < removePath.newPos) { + basePath = clonePath(removePath); + self.pushComponent(basePath.components, undefined, true); + } else { + basePath = addPath; // No need to clone, we've pulled it from the list + basePath.newPos++; + self.pushComponent(basePath.components, true, undefined); + } + + _oldPos = self.extractCommon(basePath, newString, oldString, diagonalPath); + + // If we have hit the end of both strings, then we are done + if (basePath.newPos + 1 >= newLen && _oldPos + 1 >= oldLen) { + return done(buildValues(self, basePath.components, newString, oldString, self.useLongestToken)); + } else { + // Otherwise track this path as a potential candidate and continue. + bestPath[diagonalPath] = basePath; + } + } + + editLength++; + } + + // Performs the length of edit iteration. Is a bit fugly as this has to support the + // sync and async mode which is never fun. Loops over execEditLength until a value + // is produced. + if (callback) { + (function exec () { + setTimeout(function () { + // This should not happen, but we want to be safe. + /* istanbul ignore next */ + if (editLength > maxEditLength) { + return callback(); + } + + if (!execEditLength()) { + exec(); + } + }, 0); + })(); + } else { + while (editLength <= maxEditLength) { + var ret = execEditLength(); + if (ret) { + return ret; + } + } + } + }, + /* istanbul ignore start */ /* istanbul ignore end */pushComponent: function pushComponent (components, added, removed) { + var last = components[components.length - 1]; + if (last && last.added === added && last.removed === removed) { + // We need to clone here as the component clone operation is just + // as shallow array clone + components[components.length - 1] = { count: last.count + 1, added: added, removed: removed }; + } else { + components.push({ count: 1, added: added, removed: removed }); + } + }, + /* istanbul ignore start */ /* istanbul ignore end */extractCommon: function extractCommon (basePath, newString, oldString, diagonalPath) { + var newLen = newString.length, + oldLen = oldString.length, + newPos = basePath.newPos, + oldPos = newPos - diagonalPath, + commonCount = 0; + while (newPos + 1 < newLen && oldPos + 1 < oldLen && this.equals(newString[newPos + 1], oldString[oldPos + 1])) { + newPos++; + oldPos++; + commonCount++; + } + + if (commonCount) { + basePath.components.push({ count: commonCount }); + } + + basePath.newPos = newPos; + return oldPos; + }, + /* istanbul ignore start */ /* istanbul ignore end */equals: function equals (left, right) { + if (this.options.comparator) { + return this.options.comparator(left, right); + } else { + return left === right || this.options.ignoreCase && left.toLowerCase() === right.toLowerCase(); + } + }, + /* istanbul ignore start */ /* istanbul ignore end */removeEmpty: function removeEmpty (array) { + var ret = []; + for (var i = 0; i < array.length; i++) { + if (array[i]) { + ret.push(array[i]); + } + } + return ret; + }, + /* istanbul ignore start */ /* istanbul ignore end */castInput: function castInput (value) { + return value; + }, + /* istanbul ignore start */ /* istanbul ignore end */tokenize: function tokenize (value) { + return value.split(''); + }, + /* istanbul ignore start */ /* istanbul ignore end */join: function join (chars) { + return chars.join(''); + } + }; + + function buildValues (diff, components, newString, oldString, useLongestToken) { + var componentPos = 0, + componentLen = components.length, + newPos = 0, + oldPos = 0; + + for (; componentPos < componentLen; componentPos++) { + var component = components[componentPos]; + if (!component.removed) { + if (!component.added && useLongestToken) { + var value = newString.slice(newPos, newPos + component.count); + value = value.map(function (value, i) { + var oldValue = oldString[oldPos + i]; + return oldValue.length > value.length ? oldValue : value; + }); + + component.value = diff.join(value); + } else { + component.value = diff.join(newString.slice(newPos, newPos + component.count)); + } + newPos += component.count; + + // Common case + if (!component.added) { + oldPos += component.count; + } + } else { + component.value = diff.join(oldString.slice(oldPos, oldPos + component.count)); + oldPos += component.count; + + // Reverse add and remove so removes are output first to match common convention + // The diffing algorithm is tied to add then remove output and this is the simplest + // route to get the desired output with minimal overhead. + if (componentPos && components[componentPos - 1].added) { + var tmp = components[componentPos - 1]; + components[componentPos - 1] = components[componentPos]; + components[componentPos] = tmp; + } + } + } + + // Special case handle for when one terminal is ignored (i.e. whitespace). + // For this case we merge the terminal into the prior string and drop the change. + // This is only available for string mode. + var lastComponent = components[componentLen - 1]; + if (componentLen > 1 && typeof lastComponent.value === 'string' && (lastComponent.added || lastComponent.removed) && diff.equals('', lastComponent.value)) { + components[componentLen - 2].value += lastComponent.value; + components.pop(); + } + + return components; + } + + function clonePath (path) { + return { newPos: path.newPos, components: path.components.slice(0) }; + } + + /***/ }, +/* 2 */ + /***/ function (module, exports, __webpack_require__) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.characterDiff = undefined; + exports.diffChars = diffChars; + + var /* istanbul ignore start */_base = __webpack_require__(1); + + /* istanbul ignore start */var _base2 = _interopRequireDefault(_base); + + function _interopRequireDefault (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + + /* istanbul ignore end */var characterDiff = /* istanbul ignore start */exports.characterDiff = new /* istanbul ignore start */_base2['default'](); + function diffChars (oldStr, newStr, options) { + return characterDiff.diff(oldStr, newStr, options); + } + + /***/ }, +/* 3 */ + /***/ function (module, exports, __webpack_require__) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.wordDiff = undefined; + exports.diffWords = diffWords; + /* istanbul ignore start */exports.diffWordsWithSpace = diffWordsWithSpace; + + var /* istanbul ignore start */_base = __webpack_require__(1); + + /* istanbul ignore start */var _base2 = _interopRequireDefault(_base); + + /* istanbul ignore end */var /* istanbul ignore start */_params = __webpack_require__(4); + + /* istanbul ignore start */function _interopRequireDefault (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + + /* istanbul ignore end */ // Based on https://en.wikipedia.org/wiki/Latin_script_in_Unicode + // + // Ranges and exceptions: + // Latin-1 Supplement, 0080–00FF + // - U+00D7 × Multiplication sign + // - U+00F7 ÷ Division sign + // Latin Extended-A, 0100–017F + // Latin Extended-B, 0180–024F + // IPA Extensions, 0250–02AF + // Spacing Modifier Letters, 02B0–02FF + // - U+02C7 ˇ ˇ Caron + // - U+02D8 ˘ ˘ Breve + // - U+02D9 ˙ ˙ Dot Above + // - U+02DA ˚ ˚ Ring Above + // - U+02DB ˛ ˛ Ogonek + // - U+02DC ˜ ˜ Small Tilde + // - U+02DD ˝ ˝ Double Acute Accent + // Latin Extended Additional, 1E00–1EFF + var extendedWordChars = /^[A-Za-z\xC0-\u02C6\u02C8-\u02D7\u02DE-\u02FF\u1E00-\u1EFF]+$/; + + var reWhitespace = /\S/; + + var wordDiff = /* istanbul ignore start */exports.wordDiff = new /* istanbul ignore start */_base2['default'](); + wordDiff.equals = function (left, right) { + if (this.options.ignoreCase) { + left = left.toLowerCase(); + right = right.toLowerCase(); + } + return left === right || this.options.ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right); + }; + wordDiff.tokenize = function (value) { + var tokens = value.split(/(\s+|\b)/); + + // Join the boundary splits that we do not consider to be boundaries. This is primarily the extended Latin character set. + for (var i = 0; i < tokens.length - 1; i++) { + // If we have an empty string in the next field and we have only word chars before and after, merge + if (!tokens[i + 1] && tokens[i + 2] && extendedWordChars.test(tokens[i]) && extendedWordChars.test(tokens[i + 2])) { + tokens[i] += tokens[i + 2]; + tokens.splice(i + 1, 2); + i--; + } + } + + return tokens; + }; + + function diffWords (oldStr, newStr, options) { + options = /* istanbul ignore start */(0, _params.generateOptions)(options, { ignoreWhitespace: true }); + return wordDiff.diff(oldStr, newStr, options); + } + + function diffWordsWithSpace (oldStr, newStr, options) { + return wordDiff.diff(oldStr, newStr, options); + } + + /***/ }, +/* 4 */ + /***/ function (module, exports) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.generateOptions = generateOptions; + function generateOptions (options, defaults) { + if (typeof options === 'function') { + defaults.callback = options; + } else if (options) { + for (var name in options) { + /* istanbul ignore else */ + if (options.hasOwnProperty(name)) { + defaults[name] = options[name]; + } + } + } + return defaults; + } + + /***/ }, +/* 5 */ + /***/ function (module, exports, __webpack_require__) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.lineDiff = undefined; + exports.diffLines = diffLines; + /* istanbul ignore start */exports.diffTrimmedLines = diffTrimmedLines; + + var /* istanbul ignore start */_base = __webpack_require__(1); + + /* istanbul ignore start */var _base2 = _interopRequireDefault(_base); + + /* istanbul ignore end */var /* istanbul ignore start */_params = __webpack_require__(4); + + /* istanbul ignore start */function _interopRequireDefault (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + + /* istanbul ignore end */var lineDiff = /* istanbul ignore start */exports.lineDiff = new /* istanbul ignore start */_base2['default'](); + lineDiff.tokenize = function (value) { + var retLines = [], + linesAndNewlines = value.split(/(\n|\r\n)/); + + // Ignore the final empty token that occurs if the string ends with a new line + if (!linesAndNewlines[linesAndNewlines.length - 1]) { + linesAndNewlines.pop(); + } + + // Merge the content and line separators into single tokens + for (var i = 0; i < linesAndNewlines.length; i++) { + var line = linesAndNewlines[i]; + + if (i % 2 && !this.options.newlineIsToken) { + retLines[retLines.length - 1] += line; + } else { + if (this.options.ignoreWhitespace) { + line = line.trim(); + } + retLines.push(line); + } + } + + return retLines; + }; + + function diffLines (oldStr, newStr, callback) { + return lineDiff.diff(oldStr, newStr, callback); + } + function diffTrimmedLines (oldStr, newStr, callback) { + var options = /* istanbul ignore start */(0, _params.generateOptions)(callback, { ignoreWhitespace: true }); + return lineDiff.diff(oldStr, newStr, options); + } + + /***/ }, +/* 6 */ + /***/ function (module, exports, __webpack_require__) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.sentenceDiff = undefined; + exports.diffSentences = diffSentences; + + var /* istanbul ignore start */_base = __webpack_require__(1); + + /* istanbul ignore start */var _base2 = _interopRequireDefault(_base); + + function _interopRequireDefault (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + + /* istanbul ignore end */var sentenceDiff = /* istanbul ignore start */exports.sentenceDiff = new /* istanbul ignore start */_base2['default'](); + sentenceDiff.tokenize = function (value) { + return value.split(/(\S.+?[.!?])(?=\s+|$)/); + }; + + function diffSentences (oldStr, newStr, callback) { + return sentenceDiff.diff(oldStr, newStr, callback); + } + + /***/ }, +/* 7 */ + /***/ function (module, exports, __webpack_require__) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.cssDiff = undefined; + exports.diffCss = diffCss; + + var /* istanbul ignore start */_base = __webpack_require__(1); + + /* istanbul ignore start */var _base2 = _interopRequireDefault(_base); + + function _interopRequireDefault (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + + /* istanbul ignore end */var cssDiff = /* istanbul ignore start */exports.cssDiff = new /* istanbul ignore start */_base2['default'](); + cssDiff.tokenize = function (value) { + return value.split(/([{}:;,]|\s+)/); + }; + + function diffCss (oldStr, newStr, callback) { + return cssDiff.diff(oldStr, newStr, callback); + } + + /***/ }, +/* 8 */ + /***/ function (module, exports, __webpack_require__) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.jsonDiff = undefined; + + var _typeof = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === 'function' && obj.constructor === Symbol && obj !== Symbol.prototype ? 'symbol' : typeof obj; }; + + exports.diffJson = diffJson; + /* istanbul ignore start */exports.canonicalize = canonicalize; + + var /* istanbul ignore start */_base = __webpack_require__(1); + + /* istanbul ignore start */var _base2 = _interopRequireDefault(_base); + + /* istanbul ignore end */var /* istanbul ignore start */_line = __webpack_require__(5); + + /* istanbul ignore start */function _interopRequireDefault (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + + /* istanbul ignore end */var objectPrototypeToString = Object.prototype.toString; + + var jsonDiff = /* istanbul ignore start */exports.jsonDiff = new /* istanbul ignore start */_base2['default'](); + // Discriminate between two lines of pretty-printed, serialized JSON where one of them has a + // dangling comma and the other doesn't. Turns out including the dangling comma yields the nicest output: + jsonDiff.useLongestToken = true; + + jsonDiff.tokenize = /* istanbul ignore start */_line.lineDiff.tokenize; + jsonDiff.castInput = function (value) { + /* istanbul ignore start */var _options = /* istanbul ignore end */this.options, + undefinedReplacement = _options.undefinedReplacement, + _options$stringifyRep = _options.stringifyReplacer, + stringifyReplacer = _options$stringifyRep === undefined ? function (k, v) /* istanbul ignore start */{ + return (/* istanbul ignore end */typeof v === 'undefined' ? undefinedReplacement : v + ); + } : _options$stringifyRep; + + return typeof value === 'string' ? value : JSON.stringify(canonicalize(value, null, null, stringifyReplacer), stringifyReplacer, ' '); + }; + jsonDiff.equals = function (left, right) { + return (/* istanbul ignore start */_base2['default'].prototype.equals.call(jsonDiff, left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1')) + ); + }; + + function diffJson (oldObj, newObj, options) { + return jsonDiff.diff(oldObj, newObj, options); + } + + // This function handles the presence of circular references by bailing out when encountering an + // object that is already on the "stack" of items being processed. Accepts an optional replacer + function canonicalize (obj, stack, replacementStack, replacer, key) { + stack = stack || []; + replacementStack = replacementStack || []; + + if (replacer) { + obj = replacer(key, obj); + } + + var i = /* istanbul ignore start */void 0; + + for (i = 0; i < stack.length; i += 1) { + if (stack[i] === obj) { + return replacementStack[i]; + } + } + + var canonicalizedObj = /* istanbul ignore start */void 0; + + if (objectPrototypeToString.call(obj) === '[object Array]') { + stack.push(obj); + canonicalizedObj = new Array(obj.length); + replacementStack.push(canonicalizedObj); + for (i = 0; i < obj.length; i += 1) { + canonicalizedObj[i] = canonicalize(obj[i], stack, replacementStack, replacer, key); + } + stack.pop(); + replacementStack.pop(); + return canonicalizedObj; + } + + if (obj && obj.toJSON) { + obj = obj.toJSON(); + } + + if (/* istanbul ignore start */(typeof /* istanbul ignore end */obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && obj !== null) { + stack.push(obj); + canonicalizedObj = {}; + replacementStack.push(canonicalizedObj); + var sortedKeys = [], + _key = /* istanbul ignore start */void 0; + for (_key in obj) { + /* istanbul ignore else */ + if (obj.hasOwnProperty(_key)) { + sortedKeys.push(_key); + } + } + sortedKeys.sort(); + for (i = 0; i < sortedKeys.length; i += 1) { + _key = sortedKeys[i]; + canonicalizedObj[_key] = canonicalize(obj[_key], stack, replacementStack, replacer, _key); + } + stack.pop(); + replacementStack.pop(); + } else { + canonicalizedObj = obj; + } + return canonicalizedObj; + } + + /***/ }, +/* 9 */ + /***/ function (module, exports, __webpack_require__) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.arrayDiff = undefined; + exports.diffArrays = diffArrays; + + var /* istanbul ignore start */_base = __webpack_require__(1); + + /* istanbul ignore start */var _base2 = _interopRequireDefault(_base); + + function _interopRequireDefault (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + + /* istanbul ignore end */var arrayDiff = /* istanbul ignore start */exports.arrayDiff = new /* istanbul ignore start */_base2['default'](); + arrayDiff.tokenize = function (value) { + return value.slice(); + }; + arrayDiff.join = arrayDiff.removeEmpty = function (value) { + return value; + }; + + function diffArrays (oldArr, newArr, callback) { + return arrayDiff.diff(oldArr, newArr, callback); + } + + /***/ }, +/* 10 */ + /***/ function (module, exports, __webpack_require__) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.applyPatch = applyPatch; + /* istanbul ignore start */exports.applyPatches = applyPatches; + + var /* istanbul ignore start */_parse = __webpack_require__(11); + + var /* istanbul ignore start */_distanceIterator = __webpack_require__(12); + + /* istanbul ignore start */var _distanceIterator2 = _interopRequireDefault(_distanceIterator); + + function _interopRequireDefault (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + + /* istanbul ignore end */function applyPatch (source, uniDiff) { + /* istanbul ignore start */var /* istanbul ignore end */options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; + + if (typeof uniDiff === 'string') { + uniDiff = /* istanbul ignore start */(0, _parse.parsePatch)(uniDiff); + } + + if (Array.isArray(uniDiff)) { + if (uniDiff.length > 1) { + throw new Error('applyPatch only works with a single input.'); + } + + uniDiff = uniDiff[0]; + } + + // Apply the diff to the input + var lines = source.split(/\r\n|[\n\v\f\r\x85]/), + delimiters = source.match(/\r\n|[\n\v\f\r\x85]/g) || [], + hunks = uniDiff.hunks, + compareLine = options.compareLine || function (lineNumber, line, operation, patchContent) /* istanbul ignore start */{ + return (/* istanbul ignore end */line === patchContent + ); + }, + errorCount = 0, + fuzzFactor = options.fuzzFactor || 0, + minLine = 0, + offset = 0, + removeEOFNL = /* istanbul ignore start */void 0 /* istanbul ignore end */, + addEOFNL = /* istanbul ignore start */void 0; + + /** + * Checks if the hunk exactly fits on the provided location + */ + function hunkFits (hunk, toPos) { + for (var j = 0; j < hunk.lines.length; j++) { + var line = hunk.lines[j], + operation = line.length > 0 ? line[0] : ' ', + content = line.length > 0 ? line.substr(1) : line; + + if (operation === ' ' || operation === '-') { + // Context sanity check + if (!compareLine(toPos + 1, lines[toPos], operation, content)) { + errorCount++; + + if (errorCount > fuzzFactor) { + return false; + } + } + toPos++; + } + } + + return true; + } + + // Search best fit offsets for each hunk based on the previous ones + for (var i = 0; i < hunks.length; i++) { + var hunk = hunks[i], + maxLine = lines.length - hunk.oldLines, + localOffset = 0, + toPos = offset + hunk.oldStart - 1; + + var iterator = /* istanbul ignore start */(0, _distanceIterator2['default'])(toPos, minLine, maxLine); + + for (; localOffset !== undefined; localOffset = iterator()) { + if (hunkFits(hunk, toPos + localOffset)) { + hunk.offset = offset += localOffset; + break; + } + } + + if (localOffset === undefined) { + return false; + } + + // Set lower text limit to end of the current hunk, so next ones don't try + // to fit over already patched text + minLine = hunk.offset + hunk.oldStart + hunk.oldLines; + } + + // Apply patch hunks + var diffOffset = 0; + for (var _i = 0; _i < hunks.length; _i++) { + var _hunk = hunks[_i], + _toPos = _hunk.oldStart + _hunk.offset + diffOffset - 1; + diffOffset += _hunk.newLines - _hunk.oldLines; + + if (_toPos < 0) { + // Creating a new file + _toPos = 0; + } + + for (var j = 0; j < _hunk.lines.length; j++) { + var line = _hunk.lines[j], + operation = line.length > 0 ? line[0] : ' ', + content = line.length > 0 ? line.substr(1) : line, + delimiter = _hunk.linedelimiters[j]; + + if (operation === ' ') { + _toPos++; + } else if (operation === '-') { + lines.splice(_toPos, 1); + delimiters.splice(_toPos, 1); + /* istanbul ignore else */ + } else if (operation === '+') { + lines.splice(_toPos, 0, content); + delimiters.splice(_toPos, 0, delimiter); + _toPos++; + } else if (operation === '\\') { + var previousOperation = _hunk.lines[j - 1] ? _hunk.lines[j - 1][0] : null; + if (previousOperation === '+') { + removeEOFNL = true; + } else if (previousOperation === '-') { + addEOFNL = true; + } + } + } + } + + // Handle EOFNL insertion/removal + if (removeEOFNL) { + while (!lines[lines.length - 1]) { + lines.pop(); + delimiters.pop(); + } + } else if (addEOFNL) { + lines.push(''); + delimiters.push('\n'); + } + for (var _k = 0; _k < lines.length - 1; _k++) { + lines[_k] = lines[_k] + delimiters[_k]; + } + return lines.join(''); + } + + // Wrapper that supports multiple file patches via callbacks. + function applyPatches (uniDiff, options) { + if (typeof uniDiff === 'string') { + uniDiff = /* istanbul ignore start */(0, _parse.parsePatch)(uniDiff); + } + + var currentIndex = 0; + function processIndex () { + var index = uniDiff[currentIndex++]; + if (!index) { + return options.complete(); + } + + options.loadFile(index, function (err, data) { + if (err) { + return options.complete(err); + } + + var updatedContent = applyPatch(data, index, options); + options.patched(index, updatedContent, function (err) { + if (err) { + return options.complete(err); + } + + processIndex(); + }); + }); + } + processIndex(); + } + + /***/ }, +/* 11 */ + /***/ function (module, exports) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.parsePatch = parsePatch; + function parsePatch (uniDiff) { + /* istanbul ignore start */var /* istanbul ignore end */options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; + + var diffstr = uniDiff.split(/\r\n|[\n\v\f\r\x85]/), + delimiters = uniDiff.match(/\r\n|[\n\v\f\r\x85]/g) || [], + list = [], + i = 0; + + function parseIndex () { + var index = {}; + list.push(index); + + // Parse diff metadata + while (i < diffstr.length) { + var line = diffstr[i]; + + // File header found, end parsing diff metadata + if (/^(\-\-\-|\+\+\+|@@)\s/.test(line)) { + break; + } + + // Diff index + var header = /^(?:Index:|diff(?: -r \w+)+)\s+(.+?)\s*$/.exec(line); + if (header) { + index.index = header[1]; + } + + i++; + } + + // Parse file headers if they are defined. Unified diff requires them, but + // there's no technical issues to have an isolated hunk without file header + parseFileHeader(index); + parseFileHeader(index); + + // Parse hunks + index.hunks = []; + + while (i < diffstr.length) { + var _line = diffstr[i]; + + if (/^(Index:|diff|\-\-\-|\+\+\+)\s/.test(_line)) { + break; + } else if (/^@@/.test(_line)) { + index.hunks.push(parseHunk()); + } else if (_line && options.strict) { + // Ignore unexpected content unless in strict mode + throw new Error('Unknown line ' + (i + 1) + ' ' + JSON.stringify(_line)); + } else { + i++; + } + } + } + + // Parses the --- and +++ headers, if none are found, no lines + // are consumed. + function parseFileHeader (index) { + var fileHeader = /^(---|\+\+\+)\s+(.*)$/.exec(diffstr[i]); + if (fileHeader) { + var keyPrefix = fileHeader[1] === '---' ? 'old' : 'new'; + var data = fileHeader[2].split('\t', 2); + var fileName = data[0].replace(/\\\\/g, '\\'); + if (/^".*"$/.test(fileName)) { + fileName = fileName.substr(1, fileName.length - 2); + } + index[keyPrefix + 'FileName'] = fileName; + index[keyPrefix + 'Header'] = (data[1] || '').trim(); + + i++; + } + } + + // Parses a hunk + // This assumes that we are at the start of a hunk. + function parseHunk () { + var chunkHeaderIndex = i, + chunkHeaderLine = diffstr[i++], + chunkHeader = chunkHeaderLine.split(/@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/); + + var hunk = { + oldStart: +chunkHeader[1], + oldLines: +chunkHeader[2] || 1, + newStart: +chunkHeader[3], + newLines: +chunkHeader[4] || 1, + lines: [], + linedelimiters: [] + }; + + var addCount = 0, + removeCount = 0; + for (; i < diffstr.length; i++) { + // Lines starting with '---' could be mistaken for the "remove line" operation + // But they could be the header for the next file. Therefore prune such cases out. + if (diffstr[i].indexOf('--- ') === 0 && i + 2 < diffstr.length && diffstr[i + 1].indexOf('+++ ') === 0 && diffstr[i + 2].indexOf('@@') === 0) { + break; + } + var operation = diffstr[i].length == 0 && i != diffstr.length - 1 ? ' ' : diffstr[i][0]; + + if (operation === '+' || operation === '-' || operation === ' ' || operation === '\\') { + hunk.lines.push(diffstr[i]); + hunk.linedelimiters.push(delimiters[i] || '\n'); + + if (operation === '+') { + addCount++; + } else if (operation === '-') { + removeCount++; + } else if (operation === ' ') { + addCount++; + removeCount++; + } + } else { + break; + } + } + + // Handle the empty block count case + if (!addCount && hunk.newLines === 1) { + hunk.newLines = 0; + } + if (!removeCount && hunk.oldLines === 1) { + hunk.oldLines = 0; + } + + // Perform optional sanity checking + if (options.strict) { + if (addCount !== hunk.newLines) { + throw new Error('Added line count did not match for hunk at line ' + (chunkHeaderIndex + 1)); + } + if (removeCount !== hunk.oldLines) { + throw new Error('Removed line count did not match for hunk at line ' + (chunkHeaderIndex + 1)); + } + } + + return hunk; + } + + while (i < diffstr.length) { + parseIndex(); + } + + return list; + } + + /***/ }, +/* 12 */ + /***/ function (module, exports) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + + exports['default'] = /* istanbul ignore end */function (start, minLine, maxLine) { + var wantForward = true, + backwardExhausted = false, + forwardExhausted = false, + localOffset = 1; + + return function iterator () { + if (wantForward && !forwardExhausted) { + if (backwardExhausted) { + localOffset++; + } else { + wantForward = false; + } + + // Check if trying to fit beyond text length, and if not, check it fits + // after offset location (or desired location on first iteration) + if (start + localOffset <= maxLine) { + return localOffset; + } + + forwardExhausted = true; + } + + if (!backwardExhausted) { + if (!forwardExhausted) { + wantForward = true; + } + + // Check if trying to fit before text beginning, and if not, check it fits + // before offset location + if (minLine <= start - localOffset) { + return -localOffset++; + } + + backwardExhausted = true; + return iterator(); + } + + // We tried to fit hunk before text beginning and beyond text length, then + // hunk can't fit on the text. Return undefined + }; + }; + + /***/ }, +/* 13 */ + /***/ function (module, exports, __webpack_require__) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.calcLineCount = calcLineCount; + /* istanbul ignore start */exports.merge = merge; + + var /* istanbul ignore start */_create = __webpack_require__(14); + + var /* istanbul ignore start */_parse = __webpack_require__(11); + + var /* istanbul ignore start */_array = __webpack_require__(15); + + /* istanbul ignore start */function _toConsumableArray (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } + + /* istanbul ignore end */function calcLineCount (hunk) { + /* istanbul ignore start */var _calcOldNewLineCount = /* istanbul ignore end */calcOldNewLineCount(hunk.lines), + oldLines = _calcOldNewLineCount.oldLines, + newLines = _calcOldNewLineCount.newLines; + + if (oldLines !== undefined) { + hunk.oldLines = oldLines; + } else { + delete hunk.oldLines; + } + + if (newLines !== undefined) { + hunk.newLines = newLines; + } else { + delete hunk.newLines; + } + } + + function merge (mine, theirs, base) { + mine = loadPatch(mine, base); + theirs = loadPatch(theirs, base); + + var ret = {}; + + // For index we just let it pass through as it doesn't have any necessary meaning. + // Leaving sanity checks on this to the API consumer that may know more about the + // meaning in their own context. + if (mine.index || theirs.index) { + ret.index = mine.index || theirs.index; + } + + if (mine.newFileName || theirs.newFileName) { + if (!fileNameChanged(mine)) { + // No header or no change in ours, use theirs (and ours if theirs does not exist) + ret.oldFileName = theirs.oldFileName || mine.oldFileName; + ret.newFileName = theirs.newFileName || mine.newFileName; + ret.oldHeader = theirs.oldHeader || mine.oldHeader; + ret.newHeader = theirs.newHeader || mine.newHeader; + } else if (!fileNameChanged(theirs)) { + // No header or no change in theirs, use ours + ret.oldFileName = mine.oldFileName; + ret.newFileName = mine.newFileName; + ret.oldHeader = mine.oldHeader; + ret.newHeader = mine.newHeader; + } else { + // Both changed... figure it out + ret.oldFileName = selectField(ret, mine.oldFileName, theirs.oldFileName); + ret.newFileName = selectField(ret, mine.newFileName, theirs.newFileName); + ret.oldHeader = selectField(ret, mine.oldHeader, theirs.oldHeader); + ret.newHeader = selectField(ret, mine.newHeader, theirs.newHeader); + } + } + + ret.hunks = []; + + var mineIndex = 0, + theirsIndex = 0, + mineOffset = 0, + theirsOffset = 0; + + while (mineIndex < mine.hunks.length || theirsIndex < theirs.hunks.length) { + var mineCurrent = mine.hunks[mineIndex] || { oldStart: Infinity }, + theirsCurrent = theirs.hunks[theirsIndex] || { oldStart: Infinity }; + + if (hunkBefore(mineCurrent, theirsCurrent)) { + // This patch does not overlap with any of the others, yay. + ret.hunks.push(cloneHunk(mineCurrent, mineOffset)); + mineIndex++; + theirsOffset += mineCurrent.newLines - mineCurrent.oldLines; + } else if (hunkBefore(theirsCurrent, mineCurrent)) { + // This patch does not overlap with any of the others, yay. + ret.hunks.push(cloneHunk(theirsCurrent, theirsOffset)); + theirsIndex++; + mineOffset += theirsCurrent.newLines - theirsCurrent.oldLines; + } else { + // Overlap, merge as best we can + var mergedHunk = { + oldStart: Math.min(mineCurrent.oldStart, theirsCurrent.oldStart), + oldLines: 0, + newStart: Math.min(mineCurrent.newStart + mineOffset, theirsCurrent.oldStart + theirsOffset), + newLines: 0, + lines: [] + }; + mergeLines(mergedHunk, mineCurrent.oldStart, mineCurrent.lines, theirsCurrent.oldStart, theirsCurrent.lines); + theirsIndex++; + mineIndex++; + + ret.hunks.push(mergedHunk); + } + } + + return ret; + } + + function loadPatch (param, base) { + if (typeof param === 'string') { + if (/^@@/m.test(param) || /^Index:/m.test(param)) { + return (/* istanbul ignore start */(0, _parse.parsePatch)(param)[0] + ); + } + + if (!base) { + throw new Error('Must provide a base reference or pass in a patch'); + } + return (/* istanbul ignore start */(0, _create.structuredPatch)(undefined, undefined, base, param) + ); + } + + return param; + } + + function fileNameChanged (patch) { + return patch.newFileName && patch.newFileName !== patch.oldFileName; + } + + function selectField (index, mine, theirs) { + if (mine === theirs) { + return mine; + } else { + index.conflict = true; + return { mine: mine, theirs: theirs }; + } + } + + function hunkBefore (test, check) { + return test.oldStart < check.oldStart && test.oldStart + test.oldLines < check.oldStart; + } + + function cloneHunk (hunk, offset) { + return { + oldStart: hunk.oldStart, + oldLines: hunk.oldLines, + newStart: hunk.newStart + offset, + newLines: hunk.newLines, + lines: hunk.lines + }; + } + + function mergeLines (hunk, mineOffset, mineLines, theirOffset, theirLines) { + // This will generally result in a conflicted hunk, but there are cases where the context + // is the only overlap where we can successfully merge the content here. + var mine = { offset: mineOffset, lines: mineLines, index: 0 }, + their = { offset: theirOffset, lines: theirLines, index: 0 }; + + // Handle any leading content + insertLeading(hunk, mine, their); + insertLeading(hunk, their, mine); + + // Now in the overlap content. Scan through and select the best changes from each. + while (mine.index < mine.lines.length && their.index < their.lines.length) { + var mineCurrent = mine.lines[mine.index], + theirCurrent = their.lines[their.index]; + + if ((mineCurrent[0] === '-' || mineCurrent[0] === '+') && (theirCurrent[0] === '-' || theirCurrent[0] === '+')) { + // Both modified ... + mutualChange(hunk, mine, their); + } else if (mineCurrent[0] === '+' && theirCurrent[0] === ' ') { + /* istanbul ignore start */var _hunk$lines; + + /* istanbul ignore end */ // Mine inserted + /* istanbul ignore start */(_hunk$lines = /* istanbul ignore end */hunk.lines).push.apply(/* istanbul ignore start */_hunk$lines /* istanbul ignore end */, /* istanbul ignore start */_toConsumableArray(/* istanbul ignore end */collectChange(mine))); + } else if (theirCurrent[0] === '+' && mineCurrent[0] === ' ') { + /* istanbul ignore start */var _hunk$lines2; + + /* istanbul ignore end */ // Theirs inserted + /* istanbul ignore start */(_hunk$lines2 = /* istanbul ignore end */hunk.lines).push.apply(/* istanbul ignore start */_hunk$lines2 /* istanbul ignore end */, /* istanbul ignore start */_toConsumableArray(/* istanbul ignore end */collectChange(their))); + } else if (mineCurrent[0] === '-' && theirCurrent[0] === ' ') { + // Mine removed or edited + removal(hunk, mine, their); + } else if (theirCurrent[0] === '-' && mineCurrent[0] === ' ') { + // Their removed or edited + removal(hunk, their, mine, true); + } else if (mineCurrent === theirCurrent) { + // Context identity + hunk.lines.push(mineCurrent); + mine.index++; + their.index++; + } else { + // Context mismatch + conflict(hunk, collectChange(mine), collectChange(their)); + } + } + + // Now push anything that may be remaining + insertTrailing(hunk, mine); + insertTrailing(hunk, their); + + calcLineCount(hunk); + } + + function mutualChange (hunk, mine, their) { + var myChanges = collectChange(mine), + theirChanges = collectChange(their); + + if (allRemoves(myChanges) && allRemoves(theirChanges)) { + // Special case for remove changes that are supersets of one another + if (/* istanbul ignore start */(0, _array.arrayStartsWith)(myChanges, theirChanges) && skipRemoveSuperset(their, myChanges, myChanges.length - theirChanges.length)) { + /* istanbul ignore start */var _hunk$lines3; + + /* istanbul ignore end */ /* istanbul ignore start */(_hunk$lines3 = /* istanbul ignore end */hunk.lines).push.apply(/* istanbul ignore start */_hunk$lines3 /* istanbul ignore end */, /* istanbul ignore start */_toConsumableArray(/* istanbul ignore end */myChanges)); + return; + } else if (/* istanbul ignore start */(0, _array.arrayStartsWith)(theirChanges, myChanges) && skipRemoveSuperset(mine, theirChanges, theirChanges.length - myChanges.length)) { + /* istanbul ignore start */var _hunk$lines4; + + /* istanbul ignore end */ /* istanbul ignore start */(_hunk$lines4 = /* istanbul ignore end */hunk.lines).push.apply(/* istanbul ignore start */_hunk$lines4 /* istanbul ignore end */, /* istanbul ignore start */_toConsumableArray(/* istanbul ignore end */theirChanges)); + return; + } + } else if (/* istanbul ignore start */(0, _array.arrayEqual)(myChanges, theirChanges)) { + /* istanbul ignore start */var _hunk$lines5; + + /* istanbul ignore end */ /* istanbul ignore start */(_hunk$lines5 = /* istanbul ignore end */hunk.lines).push.apply(/* istanbul ignore start */_hunk$lines5 /* istanbul ignore end */, /* istanbul ignore start */_toConsumableArray(/* istanbul ignore end */myChanges)); + return; + } + + conflict(hunk, myChanges, theirChanges); + } + + function removal (hunk, mine, their, swap) { + var myChanges = collectChange(mine), + theirChanges = collectContext(their, myChanges); + if (theirChanges.merged) { + /* istanbul ignore start */var _hunk$lines6; + + /* istanbul ignore end */ /* istanbul ignore start */(_hunk$lines6 = /* istanbul ignore end */hunk.lines).push.apply(/* istanbul ignore start */_hunk$lines6 /* istanbul ignore end */, /* istanbul ignore start */_toConsumableArray(/* istanbul ignore end */theirChanges.merged)); + } else { + conflict(hunk, swap ? theirChanges : myChanges, swap ? myChanges : theirChanges); + } + } + + function conflict (hunk, mine, their) { + hunk.conflict = true; + hunk.lines.push({ + conflict: true, + mine: mine, + theirs: their + }); + } + + function insertLeading (hunk, insert, their) { + while (insert.offset < their.offset && insert.index < insert.lines.length) { + var line = insert.lines[insert.index++]; + hunk.lines.push(line); + insert.offset++; + } + } + function insertTrailing (hunk, insert) { + while (insert.index < insert.lines.length) { + var line = insert.lines[insert.index++]; + hunk.lines.push(line); + } + } + + function collectChange (state) { + var ret = [], + operation = state.lines[state.index][0]; + while (state.index < state.lines.length) { + var line = state.lines[state.index]; + + // Group additions that are immediately after subtractions and treat them as one "atomic" modify change. + if (operation === '-' && line[0] === '+') { + operation = '+'; + } + + if (operation === line[0]) { + ret.push(line); + state.index++; + } else { + break; + } + } + + return ret; + } + function collectContext (state, matchChanges) { + var changes = [], + merged = [], + matchIndex = 0, + contextChanges = false, + conflicted = false; + while (matchIndex < matchChanges.length && state.index < state.lines.length) { + var change = state.lines[state.index], + match = matchChanges[matchIndex]; + + // Once we've hit our add, then we are done + if (match[0] === '+') { + break; + } + + contextChanges = contextChanges || change[0] !== ' '; + + merged.push(match); + matchIndex++; + + // Consume any additions in the other block as a conflict to attempt + // to pull in the remaining context after this + if (change[0] === '+') { + conflicted = true; + + while (change[0] === '+') { + changes.push(change); + change = state.lines[++state.index]; + } + } + + if (match.substr(1) === change.substr(1)) { + changes.push(change); + state.index++; + } else { + conflicted = true; + } + } + + if ((matchChanges[matchIndex] || '')[0] === '+' && contextChanges) { + conflicted = true; + } + + if (conflicted) { + return changes; + } + + while (matchIndex < matchChanges.length) { + merged.push(matchChanges[matchIndex++]); + } + + return { + merged: merged, + changes: changes + }; + } + + function allRemoves (changes) { + return changes.reduce(function (prev, change) { + return prev && change[0] === '-'; + }, true); + } + function skipRemoveSuperset (state, removeChanges, delta) { + for (var i = 0; i < delta; i++) { + var changeContent = removeChanges[removeChanges.length - delta + i].substr(1); + if (state.lines[state.index + i] !== ' ' + changeContent) { + return false; + } + } + + state.index += delta; + return true; + } + + function calcOldNewLineCount (lines) { + var oldLines = 0; + var newLines = 0; + + lines.forEach(function (line) { + if (typeof line !== 'string') { + var myCount = calcOldNewLineCount(line.mine); + var theirCount = calcOldNewLineCount(line.theirs); + + if (oldLines !== undefined) { + if (myCount.oldLines === theirCount.oldLines) { + oldLines += myCount.oldLines; + } else { + oldLines = undefined; + } + } + + if (newLines !== undefined) { + if (myCount.newLines === theirCount.newLines) { + newLines += myCount.newLines; + } else { + newLines = undefined; + } + } + } else { + if (newLines !== undefined && (line[0] === '+' || line[0] === ' ')) { + newLines++; + } + if (oldLines !== undefined && (line[0] === '-' || line[0] === ' ')) { + oldLines++; + } + } + }); + + return { oldLines: oldLines, newLines: newLines }; + } + + /***/ }, +/* 14 */ + /***/ function (module, exports, __webpack_require__) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.structuredPatch = structuredPatch; + /* istanbul ignore start */exports.createTwoFilesPatch = createTwoFilesPatch; + /* istanbul ignore start */exports.createPatch = createPatch; + + var /* istanbul ignore start */_line = __webpack_require__(5); + + /* istanbul ignore start */function _toConsumableArray (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } + + /* istanbul ignore end */function structuredPatch (oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options) { + if (!options) { + options = {}; + } + if (typeof options.context === 'undefined') { + options.context = 4; + } + + var diff = /* istanbul ignore start */(0, _line.diffLines)(oldStr, newStr, options); + diff.push({ value: '', lines: [] }); // Append an empty value to make cleanup easier + + function contextLines (lines) { + return lines.map(function (entry) { + return ' ' + entry; + }); + } + + var hunks = []; + var oldRangeStart = 0, + newRangeStart = 0, + curRange = [], + oldLine = 1, + newLine = 1; + + /* istanbul ignore start */var _loop = function _loop (/* istanbul ignore end */i) { + var current = diff[i], + lines = current.lines || current.value.replace(/\n$/, '').split('\n'); + current.lines = lines; + + if (current.added || current.removed) { + /* istanbul ignore start */var _curRange; + + /* istanbul ignore end */ // If we have previous context, start with that + if (!oldRangeStart) { + var prev = diff[i - 1]; + oldRangeStart = oldLine; + newRangeStart = newLine; + + if (prev) { + curRange = options.context > 0 ? contextLines(prev.lines.slice(-options.context)) : []; + oldRangeStart -= curRange.length; + newRangeStart -= curRange.length; + } + } + + // Output our changes + /* istanbul ignore start */(_curRange = /* istanbul ignore end */curRange).push.apply(/* istanbul ignore start */_curRange /* istanbul ignore end */, /* istanbul ignore start */_toConsumableArray(/* istanbul ignore end */lines.map(function (entry) { + return (current.added ? '+' : '-') + entry; + }))); + + // Track the updated file position + if (current.added) { + newLine += lines.length; + } else { + oldLine += lines.length; + } + } else { + // Identical context lines. Track line changes + if (oldRangeStart) { + // Close out any changes that have been output (or join overlapping) + if (lines.length <= options.context * 2 && i < diff.length - 2) { + /* istanbul ignore start */var _curRange2; + + /* istanbul ignore end */ // Overlapping + /* istanbul ignore start */(_curRange2 = /* istanbul ignore end */curRange).push.apply(/* istanbul ignore start */_curRange2 /* istanbul ignore end */, /* istanbul ignore start */_toConsumableArray(/* istanbul ignore end */contextLines(lines))); + } else { + /* istanbul ignore start */var _curRange3; + + /* istanbul ignore end */ // end the range and output + var contextSize = Math.min(lines.length, options.context); + /* istanbul ignore start */(_curRange3 = /* istanbul ignore end */curRange).push.apply(/* istanbul ignore start */_curRange3 /* istanbul ignore end */, /* istanbul ignore start */_toConsumableArray(/* istanbul ignore end */contextLines(lines.slice(0, contextSize)))); + + var hunk = { + oldStart: oldRangeStart, + oldLines: oldLine - oldRangeStart + contextSize, + newStart: newRangeStart, + newLines: newLine - newRangeStart + contextSize, + lines: curRange + }; + if (i >= diff.length - 2 && lines.length <= options.context) { + // EOF is inside this hunk + var oldEOFNewline = /\n$/.test(oldStr); + var newEOFNewline = /\n$/.test(newStr); + if (lines.length == 0 && !oldEOFNewline) { + // special case: old has no eol and no trailing context; no-nl can end up before adds + curRange.splice(hunk.oldLines, 0, '\\ No newline at end of file'); + } else if (!oldEOFNewline || !newEOFNewline) { + curRange.push('\\ No newline at end of file'); + } + } + hunks.push(hunk); + + oldRangeStart = 0; + newRangeStart = 0; + curRange = []; + } + } + oldLine += lines.length; + newLine += lines.length; + } + }; + + for (var i = 0; i < diff.length; i++) { + /* istanbul ignore start */_loop(/* istanbul ignore end */i); + } + + return { + oldFileName: oldFileName, + newFileName: newFileName, + oldHeader: oldHeader, + newHeader: newHeader, + hunks: hunks + }; + } + + function createTwoFilesPatch (oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options) { + var diff = structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options); + + var ret = []; + if (oldFileName == newFileName) { + ret.push('Index: ' + oldFileName); + } + ret.push('==================================================================='); + ret.push('--- ' + diff.oldFileName + (typeof diff.oldHeader === 'undefined' ? '' : '\t' + diff.oldHeader)); + ret.push('+++ ' + diff.newFileName + (typeof diff.newHeader === 'undefined' ? '' : '\t' + diff.newHeader)); + + for (var i = 0; i < diff.hunks.length; i++) { + var hunk = diff.hunks[i]; + ret.push('@@ -' + hunk.oldStart + ',' + hunk.oldLines + ' +' + hunk.newStart + ',' + hunk.newLines + ' @@'); + ret.push.apply(ret, hunk.lines); + } + + return ret.join('\n') + '\n'; + } + + function createPatch (fileName, oldStr, newStr, oldHeader, newHeader, options) { + return createTwoFilesPatch(fileName, fileName, oldStr, newStr, oldHeader, newHeader, options); + } + + /***/ }, +/* 15 */ + /***/ function (module, exports) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.arrayEqual = arrayEqual; + /* istanbul ignore start */exports.arrayStartsWith = arrayStartsWith; + function arrayEqual (a, b) { + if (a.length !== b.length) { + return false; + } + + return arrayStartsWith(a, b); + } + + function arrayStartsWith (array, start) { + if (start.length > array.length) { + return false; + } + + for (var i = 0; i < start.length; i++) { + if (start[i] !== array[i]) { + return false; + } + } + + return true; + } + + /***/ }, +/* 16 */ + /***/ function (module, exports) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.convertChangesToDMP = convertChangesToDMP; + // See: http://code.google.com/p/google-diff-match-patch/wiki/API + function convertChangesToDMP (changes) { + var ret = [], + change = /* istanbul ignore start */void 0 /* istanbul ignore end */, + operation = /* istanbul ignore start */void 0; + for (var i = 0; i < changes.length; i++) { + change = changes[i]; + if (change.added) { + operation = 1; + } else if (change.removed) { + operation = -1; + } else { + operation = 0; + } + + ret.push([operation, change.value]); + } + return ret; + } + + /***/ }, +/* 17 */ + /***/ function (module, exports) { + + /* istanbul ignore start */'use strict'; + + exports.__esModule = true; + exports.convertChangesToXML = convertChangesToXML; + function convertChangesToXML (changes) { + var ret = []; + for (var i = 0; i < changes.length; i++) { + var change = changes[i]; + if (change.added) { + ret.push(''); + } else if (change.removed) { + ret.push(''); + } + + ret.push(escapeHTML(change.value)); + + if (change.added) { + ret.push(''); + } else if (change.removed) { + ret.push(''); + } + } + return ret.join(''); + } + + function escapeHTML (s) { + var n = s; + n = n.replace(/&/g, '&'); + n = n.replace(//g, '>'); + n = n.replace(/"/g, '"'); + + return n; + } + + /***/ } +/******/ ]); + }); + + }, {}], + 49: [function (require, module, exports) { + 'use strict'; + + var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g; + + module.exports = function (str) { + if (typeof str !== 'string') { + throw new TypeError('Expected a string'); + } + + return str.replace(matchOperatorsRe, '\\$&'); + }; + + }, {}], + 50: [function (require, module, exports) { +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + + var objectCreate = Object.create || objectCreatePolyfill; + var objectKeys = Object.keys || objectKeysPolyfill; + var bind = Function.prototype.bind || functionBindPolyfill; + + function EventEmitter () { + if (!this._events || !Object.prototype.hasOwnProperty.call(this, '_events')) { + this._events = objectCreate(null); + this._eventsCount = 0; + } + + this._maxListeners = this._maxListeners || undefined; + } + module.exports = EventEmitter; + +// Backwards-compat with node 0.10.x + EventEmitter.EventEmitter = EventEmitter; + + EventEmitter.prototype._events = undefined; + EventEmitter.prototype._maxListeners = undefined; + +// By default EventEmitters will print a warning if more than 10 listeners are +// added to it. This is a useful default which helps finding memory leaks. + var defaultMaxListeners = 10; + + var hasDefineProperty; + try { + var o = {}; + if (Object.defineProperty) Object.defineProperty(o, 'x', { value: 0 }); + hasDefineProperty = o.x === 0; + } catch (err) { hasDefineProperty = false; } + if (hasDefineProperty) { + Object.defineProperty(EventEmitter, 'defaultMaxListeners', { + enumerable: true, + get: function () { + return defaultMaxListeners; + }, + set: function (arg) { + // check whether the input is a positive number (whose value is zero or + // greater and not a NaN). + if (typeof arg !== 'number' || arg < 0 || arg !== arg) { throw new TypeError('"defaultMaxListeners" must be a positive number'); } + defaultMaxListeners = arg; + } + }); + } else { + EventEmitter.defaultMaxListeners = defaultMaxListeners; + } + +// Obviously not all Emitters should be limited to 10. This function allows +// that to be increased. Set to zero for unlimited. + EventEmitter.prototype.setMaxListeners = function setMaxListeners (n) { + if (typeof n !== 'number' || n < 0 || isNaN(n)) { throw new TypeError('"n" argument must be a positive number'); } + this._maxListeners = n; + return this; + }; + + function $getMaxListeners (that) { + if (that._maxListeners === undefined) { return EventEmitter.defaultMaxListeners; } + return that._maxListeners; + } + + EventEmitter.prototype.getMaxListeners = function getMaxListeners () { + return $getMaxListeners(this); + }; + +// These standalone emit* functions are used to optimize calling of event +// handlers for fast cases because emit() itself often has a variable number of +// arguments and can be deoptimized because of that. These functions always have +// the same number of arguments and thus do not get deoptimized, so the code +// inside them can execute faster. + function emitNone (handler, isFn, self) { + if (isFn) { handler.call(self); } else { + var len = handler.length; + var listeners = arrayClone(handler, len); + for (var i = 0; i < len; ++i) { listeners[i].call(self); } + } + } + function emitOne (handler, isFn, self, arg1) { + if (isFn) { handler.call(self, arg1); } else { + var len = handler.length; + var listeners = arrayClone(handler, len); + for (var i = 0; i < len; ++i) { listeners[i].call(self, arg1); } + } + } + function emitTwo (handler, isFn, self, arg1, arg2) { + if (isFn) { handler.call(self, arg1, arg2); } else { + var len = handler.length; + var listeners = arrayClone(handler, len); + for (var i = 0; i < len; ++i) { listeners[i].call(self, arg1, arg2); } + } + } + function emitThree (handler, isFn, self, arg1, arg2, arg3) { + if (isFn) { handler.call(self, arg1, arg2, arg3); } else { + var len = handler.length; + var listeners = arrayClone(handler, len); + for (var i = 0; i < len; ++i) { listeners[i].call(self, arg1, arg2, arg3); } + } + } + + function emitMany (handler, isFn, self, args) { + if (isFn) { handler.apply(self, args); } else { + var len = handler.length; + var listeners = arrayClone(handler, len); + for (var i = 0; i < len; ++i) { listeners[i].apply(self, args); } + } + } + + EventEmitter.prototype.emit = function emit (type) { + var er, handler, len, args, i, events; + var doError = (type === 'error'); + + events = this._events; + if (events) { doError = (doError && events.error == null); } else if (!doError) { return false; } + + // If there is no 'error' event listener then throw. + if (doError) { + if (arguments.length > 1) { er = arguments[1]; } + if (er instanceof Error) { + throw er; // Unhandled 'error' event + } else { + // At least give some kind of context to the user + var err = new Error('Unhandled "error" event. (' + er + ')'); + err.context = er; + throw err; + } + return false; + } + + handler = events[type]; + + if (!handler) { return false; } + + var isFn = typeof handler === 'function'; + len = arguments.length; + switch (len) { + // fast cases + case 1: + emitNone(handler, isFn, this); + break; + case 2: + emitOne(handler, isFn, this, arguments[1]); + break; + case 3: + emitTwo(handler, isFn, this, arguments[1], arguments[2]); + break; + case 4: + emitThree(handler, isFn, this, arguments[1], arguments[2], arguments[3]); + break; + // slower + default: + args = new Array(len - 1); + for (i = 1; i < len; i++) { args[i - 1] = arguments[i]; } + emitMany(handler, isFn, this, args); + } + + return true; + }; + + function _addListener (target, type, listener, prepend) { + var m; + var events; + var existing; + + if (typeof listener !== 'function') { throw new TypeError('"listener" argument must be a function'); } + + events = target._events; + if (!events) { + events = target._events = objectCreate(null); + target._eventsCount = 0; + } else { + // To avoid recursion in the case that type === "newListener"! Before + // adding it to the listeners, first emit "newListener". + if (events.newListener) { + target.emit('newListener', type, + listener.listener ? listener.listener : listener); + + // Re-assign `events` because a newListener handler could have caused the + // this._events to be assigned to a new object + events = target._events; + } + existing = events[type]; + } + + if (!existing) { + // Optimize the case of one listener. Don't need the extra array object. + existing = events[type] = listener; + ++target._eventsCount; + } else { + if (typeof existing === 'function') { + // Adding the second element, need to change to array. + existing = events[type] = + prepend ? [listener, existing] : [existing, listener]; + } else { + // If we've already got an array, just append. + if (prepend) { + existing.unshift(listener); + } else { + existing.push(listener); + } + } + + // Check for listener leak + if (!existing.warned) { + m = $getMaxListeners(target); + if (m && m > 0 && existing.length > m) { + existing.warned = true; + var w = new Error('Possible EventEmitter memory leak detected. ' + + existing.length + ' "' + String(type) + '" listeners ' + + 'added. Use emitter.setMaxListeners() to ' + + 'increase limit.'); + w.name = 'MaxListenersExceededWarning'; + w.emitter = target; + w.type = type; + w.count = existing.length; + if (typeof console === 'object' && console.warn) { + console.warn('%s: %s', w.name, w.message); + } + } + } + } + + return target; + } + + EventEmitter.prototype.addListener = function addListener (type, listener) { + return _addListener(this, type, listener, false); + }; + + EventEmitter.prototype.on = EventEmitter.prototype.addListener; + + EventEmitter.prototype.prependListener = + function prependListener (type, listener) { + return _addListener(this, type, listener, true); + }; + + function onceWrapper () { + if (!this.fired) { + this.target.removeListener(this.type, this.wrapFn); + this.fired = true; + switch (arguments.length) { + case 0: + return this.listener.call(this.target); + case 1: + return this.listener.call(this.target, arguments[0]); + case 2: + return this.listener.call(this.target, arguments[0], arguments[1]); + case 3: + return this.listener.call(this.target, arguments[0], arguments[1], + arguments[2]); + default: + var args = new Array(arguments.length); + for (var i = 0; i < args.length; ++i) { args[i] = arguments[i]; } + this.listener.apply(this.target, args); + } + } + } + + function _onceWrap (target, type, listener) { + var state = { fired: false, wrapFn: undefined, target: target, type: type, listener: listener }; + var wrapped = bind.call(onceWrapper, state); + wrapped.listener = listener; + state.wrapFn = wrapped; + return wrapped; + } + + EventEmitter.prototype.once = function once (type, listener) { + if (typeof listener !== 'function') { throw new TypeError('"listener" argument must be a function'); } + this.on(type, _onceWrap(this, type, listener)); + return this; + }; + + EventEmitter.prototype.prependOnceListener = + function prependOnceListener (type, listener) { + if (typeof listener !== 'function') { throw new TypeError('"listener" argument must be a function'); } + this.prependListener(type, _onceWrap(this, type, listener)); + return this; + }; + +// Emits a 'removeListener' event if and only if the listener was removed. + EventEmitter.prototype.removeListener = + function removeListener (type, listener) { + var list, events, position, i, originalListener; + + if (typeof listener !== 'function') { throw new TypeError('"listener" argument must be a function'); } + + events = this._events; + if (!events) { return this; } + + list = events[type]; + if (!list) { return this; } + + if (list === listener || list.listener === listener) { + if (--this._eventsCount === 0) { this._events = objectCreate(null); } else { + delete events[type]; + if (events.removeListener) { this.emit('removeListener', type, list.listener || listener); } + } + } else if (typeof list !== 'function') { + position = -1; + + for (i = list.length - 1; i >= 0; i--) { + if (list[i] === listener || list[i].listener === listener) { + originalListener = list[i].listener; + position = i; + break; + } + } + + if (position < 0) { return this; } + + if (position === 0) { list.shift(); } else { spliceOne(list, position); } + + if (list.length === 1) { events[type] = list[0]; } + + if (events.removeListener) { this.emit('removeListener', type, originalListener || listener); } + } + + return this; + }; + + EventEmitter.prototype.removeAllListeners = + function removeAllListeners (type) { + var listeners, events, i; + + events = this._events; + if (!events) { return this; } + + // not listening for removeListener, no need to emit + if (!events.removeListener) { + if (arguments.length === 0) { + this._events = objectCreate(null); + this._eventsCount = 0; + } else if (events[type]) { + if (--this._eventsCount === 0) { this._events = objectCreate(null); } else { delete events[type]; } + } + return this; + } + + // emit removeListener for all listeners on all events + if (arguments.length === 0) { + var keys = objectKeys(events); + var key; + for (i = 0; i < keys.length; ++i) { + key = keys[i]; + if (key === 'removeListener') continue; + this.removeAllListeners(key); + } + this.removeAllListeners('removeListener'); + this._events = objectCreate(null); + this._eventsCount = 0; + return this; + } + + listeners = events[type]; + + if (typeof listeners === 'function') { + this.removeListener(type, listeners); + } else if (listeners) { + // LIFO order + for (i = listeners.length - 1; i >= 0; i--) { + this.removeListener(type, listeners[i]); + } + } + + return this; + }; + + function _listeners (target, type, unwrap) { + var events = target._events; + + if (!events) { return []; } + + var evlistener = events[type]; + if (!evlistener) { return []; } + + if (typeof evlistener === 'function') { return unwrap ? [evlistener.listener || evlistener] : [evlistener]; } + + return unwrap ? unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length); + } + + EventEmitter.prototype.listeners = function listeners (type) { + return _listeners(this, type, true); + }; + + EventEmitter.prototype.rawListeners = function rawListeners (type) { + return _listeners(this, type, false); + }; + + EventEmitter.listenerCount = function (emitter, type) { + if (typeof emitter.listenerCount === 'function') { + return emitter.listenerCount(type); + } else { + return listenerCount.call(emitter, type); + } + }; + + EventEmitter.prototype.listenerCount = listenerCount; + function listenerCount (type) { + var events = this._events; + + if (events) { + var evlistener = events[type]; + + if (typeof evlistener === 'function') { + return 1; + } else if (evlistener) { + return evlistener.length; + } + } + + return 0; + } + + EventEmitter.prototype.eventNames = function eventNames () { + return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : []; + }; + +// About 1.5x faster than the two-arg version of Array#splice(). + function spliceOne (list, index) { + for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1) { list[i] = list[k]; } + list.pop(); + } + + function arrayClone (arr, n) { + var copy = new Array(n); + for (var i = 0; i < n; ++i) { copy[i] = arr[i]; } + return copy; + } + + function unwrapListeners (arr) { + var ret = new Array(arr.length); + for (var i = 0; i < ret.length; ++i) { + ret[i] = arr[i].listener || arr[i]; + } + return ret; + } + + function objectCreatePolyfill (proto) { + var F = function () {}; + F.prototype = proto; + return new F(); + } + function objectKeysPolyfill (obj) { + var keys = []; + for (var k in obj) { + if (Object.prototype.hasOwnProperty.call(obj, k)) { + keys.push(k); + } + } + return k; + } + function functionBindPolyfill (context) { + var fn = this; + return function () { + return fn.apply(context, arguments); + }; + } + + }, {}], + 51: [function (require, module, exports) { + 'use strict'; + +/* eslint no-invalid-this: 1 */ + + var ERROR_MESSAGE = 'Function.prototype.bind called on incompatible '; + var slice = Array.prototype.slice; + var toStr = Object.prototype.toString; + var funcType = '[object Function]'; + + module.exports = function bind (that) { + var target = this; + if (typeof target !== 'function' || toStr.call(target) !== funcType) { + throw new TypeError(ERROR_MESSAGE + target); + } + var args = slice.call(arguments, 1); + + var bound; + var binder = function () { + if (this instanceof bound) { + var result = target.apply( + this, + args.concat(slice.call(arguments)) + ); + if (Object(result) === result) { + return result; + } + return this; + } else { + return target.apply( + that, + args.concat(slice.call(arguments)) + ); + } + }; + + var boundLength = Math.max(0, target.length - args.length); + var boundArgs = []; + for (var i = 0; i < boundLength; i++) { + boundArgs.push('$' + i); + } + + bound = Function('binder', 'return function (' + boundArgs.join(',') + '){ return binder.apply(this,arguments); }')(binder); + + if (target.prototype) { + var Empty = function Empty () {}; + Empty.prototype = target.prototype; + bound.prototype = new Empty(); + Empty.prototype = null; + } + + return bound; + }; + + }, {}], + 52: [function (require, module, exports) { + 'use strict'; + + var implementation = require('./implementation'); + + module.exports = Function.prototype.bind || implementation; + + }, {'./implementation': 51}], + 53: [function (require, module, exports) { + 'use strict'; + +/* eslint complexity: [2, 17], max-statements: [2, 33] */ + module.exports = function hasSymbols () { + if (typeof Symbol !== 'function' || typeof Object.getOwnPropertySymbols !== 'function') { return false; } + if (typeof Symbol.iterator === 'symbol') { return true; } + + var obj = {}; + var sym = Symbol('test'); + var symObj = Object(sym); + if (typeof sym === 'string') { return false; } + + if (Object.prototype.toString.call(sym) !== '[object Symbol]') { return false; } + if (Object.prototype.toString.call(symObj) !== '[object Symbol]') { return false; } + + // temp disabled per https://github.com/ljharb/object.assign/issues/17 + // if (sym instanceof Symbol) { return false; } + // temp disabled per https://github.com/WebReflection/get-own-property-symbols/issues/4 + // if (!(symObj instanceof Symbol)) { return false; } + + // if (typeof Symbol.prototype.toString !== 'function') { return false; } + // if (String(sym) !== Symbol.prototype.toString.call(sym)) { return false; } + + var symVal = 42; + obj[sym] = symVal; + for (sym in obj) { return false; } // eslint-disable-line no-restricted-syntax + if (typeof Object.keys === 'function' && Object.keys(obj).length !== 0) { return false; } + + if (typeof Object.getOwnPropertyNames === 'function' && Object.getOwnPropertyNames(obj).length !== 0) { return false; } + + var syms = Object.getOwnPropertySymbols(obj); + if (syms.length !== 1 || syms[0] !== sym) { return false; } + + if (!Object.prototype.propertyIsEnumerable.call(obj, sym)) { return false; } + + if (typeof Object.getOwnPropertyDescriptor === 'function') { + var descriptor = Object.getOwnPropertyDescriptor(obj, sym); + if (descriptor.value !== symVal || descriptor.enumerable !== true) { return false; } + } + + return true; + }; + + }, {}], + 54: [function (require, module, exports) { + (function (global) { +/*! https://mths.be/he v1.2.0 by @mathias | MIT license */ + (function (root) { + + // Detect free variables `exports`. + var freeExports = typeof exports === 'object' && exports; + + // Detect free variable `module`. + var freeModule = typeof module === 'object' && module && + module.exports == freeExports && module; + + // Detect free variable `global`, from Node.js or Browserified code, + // and use it as `root`. + var freeGlobal = typeof global === 'object' && global; + if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { + root = freeGlobal; + } + + /* -------------------------------------------------------------------------- */ + + // All astral symbols. + var regexAstralSymbols = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g; + // All ASCII symbols (not just printable ASCII) except those listed in the + // first column of the overrides table. + // https://html.spec.whatwg.org/multipage/syntax.html#table-charref-overrides + var regexAsciiWhitelist = /[\x01-\x7F]/g; + // All BMP symbols that are not ASCII newlines, printable ASCII symbols, or + // code points listed in the first column of the overrides table on + // https://html.spec.whatwg.org/multipage/syntax.html#table-charref-overrides. + var regexBmpWhitelist = /[\x01-\t\x0B\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g; + + var regexEncodeNonAscii = /<\u20D2|=\u20E5|>\u20D2|\u205F\u200A|\u219D\u0338|\u2202\u0338|\u2220\u20D2|\u2229\uFE00|\u222A\uFE00|\u223C\u20D2|\u223D\u0331|\u223E\u0333|\u2242\u0338|\u224B\u0338|\u224D\u20D2|\u224E\u0338|\u224F\u0338|\u2250\u0338|\u2261\u20E5|\u2264\u20D2|\u2265\u20D2|\u2266\u0338|\u2267\u0338|\u2268\uFE00|\u2269\uFE00|\u226A\u0338|\u226A\u20D2|\u226B\u0338|\u226B\u20D2|\u227F\u0338|\u2282\u20D2|\u2283\u20D2|\u228A\uFE00|\u228B\uFE00|\u228F\u0338|\u2290\u0338|\u2293\uFE00|\u2294\uFE00|\u22B4\u20D2|\u22B5\u20D2|\u22D8\u0338|\u22D9\u0338|\u22DA\uFE00|\u22DB\uFE00|\u22F5\u0338|\u22F9\u0338|\u2933\u0338|\u29CF\u0338|\u29D0\u0338|\u2A6D\u0338|\u2A70\u0338|\u2A7D\u0338|\u2A7E\u0338|\u2AA1\u0338|\u2AA2\u0338|\u2AAC\uFE00|\u2AAD\uFE00|\u2AAF\u0338|\u2AB0\u0338|\u2AC5\u0338|\u2AC6\u0338|\u2ACB\uFE00|\u2ACC\uFE00|\u2AFD\u20E5|[\xA0-\u0113\u0116-\u0122\u0124-\u012B\u012E-\u014D\u0150-\u017E\u0192\u01B5\u01F5\u0237\u02C6\u02C7\u02D8-\u02DD\u0311\u0391-\u03A1\u03A3-\u03A9\u03B1-\u03C9\u03D1\u03D2\u03D5\u03D6\u03DC\u03DD\u03F0\u03F1\u03F5\u03F6\u0401-\u040C\u040E-\u044F\u0451-\u045C\u045E\u045F\u2002-\u2005\u2007-\u2010\u2013-\u2016\u2018-\u201A\u201C-\u201E\u2020-\u2022\u2025\u2026\u2030-\u2035\u2039\u203A\u203E\u2041\u2043\u2044\u204F\u2057\u205F-\u2063\u20AC\u20DB\u20DC\u2102\u2105\u210A-\u2113\u2115-\u211E\u2122\u2124\u2127-\u2129\u212C\u212D\u212F-\u2131\u2133-\u2138\u2145-\u2148\u2153-\u215E\u2190-\u219B\u219D-\u21A7\u21A9-\u21AE\u21B0-\u21B3\u21B5-\u21B7\u21BA-\u21DB\u21DD\u21E4\u21E5\u21F5\u21FD-\u2205\u2207-\u2209\u220B\u220C\u220F-\u2214\u2216-\u2218\u221A\u221D-\u2238\u223A-\u2257\u2259\u225A\u225C\u225F-\u2262\u2264-\u228B\u228D-\u229B\u229D-\u22A5\u22A7-\u22B0\u22B2-\u22BB\u22BD-\u22DB\u22DE-\u22E3\u22E6-\u22F7\u22F9-\u22FE\u2305\u2306\u2308-\u2310\u2312\u2313\u2315\u2316\u231C-\u231F\u2322\u2323\u232D\u232E\u2336\u233D\u233F\u237C\u23B0\u23B1\u23B4-\u23B6\u23DC-\u23DF\u23E2\u23E7\u2423\u24C8\u2500\u2502\u250C\u2510\u2514\u2518\u251C\u2524\u252C\u2534\u253C\u2550-\u256C\u2580\u2584\u2588\u2591-\u2593\u25A1\u25AA\u25AB\u25AD\u25AE\u25B1\u25B3-\u25B5\u25B8\u25B9\u25BD-\u25BF\u25C2\u25C3\u25CA\u25CB\u25EC\u25EF\u25F8-\u25FC\u2605\u2606\u260E\u2640\u2642\u2660\u2663\u2665\u2666\u266A\u266D-\u266F\u2713\u2717\u2720\u2736\u2758\u2772\u2773\u27C8\u27C9\u27E6-\u27ED\u27F5-\u27FA\u27FC\u27FF\u2902-\u2905\u290C-\u2913\u2916\u2919-\u2920\u2923-\u292A\u2933\u2935-\u2939\u293C\u293D\u2945\u2948-\u294B\u294E-\u2976\u2978\u2979\u297B-\u297F\u2985\u2986\u298B-\u2996\u299A\u299C\u299D\u29A4-\u29B7\u29B9\u29BB\u29BC\u29BE-\u29C5\u29C9\u29CD-\u29D0\u29DC-\u29DE\u29E3-\u29E5\u29EB\u29F4\u29F6\u2A00-\u2A02\u2A04\u2A06\u2A0C\u2A0D\u2A10-\u2A17\u2A22-\u2A27\u2A29\u2A2A\u2A2D-\u2A31\u2A33-\u2A3C\u2A3F\u2A40\u2A42-\u2A4D\u2A50\u2A53-\u2A58\u2A5A-\u2A5D\u2A5F\u2A66\u2A6A\u2A6D-\u2A75\u2A77-\u2A9A\u2A9D-\u2AA2\u2AA4-\u2AB0\u2AB3-\u2AC8\u2ACB\u2ACC\u2ACF-\u2ADB\u2AE4\u2AE6-\u2AE9\u2AEB-\u2AF3\u2AFD\uFB00-\uFB04]|\uD835[\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDCCF\uDD04\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDD6B]/g; + var encodeMap = {'\xAD': 'shy', '\u200C': 'zwnj', '\u200D': 'zwj', '\u200E': 'lrm', '\u2063': 'ic', '\u2062': 'it', '\u2061': 'af', '\u200F': 'rlm', '\u200B': 'ZeroWidthSpace', '\u2060': 'NoBreak', '\u0311': 'DownBreve', '\u20DB': 'tdot', '\u20DC': 'DotDot', '\t': 'Tab', '\n': 'NewLine', '\u2008': 'puncsp', '\u205F': 'MediumSpace', '\u2009': 'thinsp', '\u200A': 'hairsp', '\u2004': 'emsp13', '\u2002': 'ensp', '\u2005': 'emsp14', '\u2003': 'emsp', '\u2007': 'numsp', '\xA0': 'nbsp', '\u205F\u200A': 'ThickSpace', '\u203E': 'oline', '_': 'lowbar', '\u2010': 'dash', '\u2013': 'ndash', '\u2014': 'mdash', '\u2015': 'horbar', ',': 'comma', ';': 'semi', '\u204F': 'bsemi', ':': 'colon', '\u2A74': 'Colone', '!': 'excl', '\xA1': 'iexcl', '?': 'quest', '\xBF': 'iquest', '.': 'period', '\u2025': 'nldr', '\u2026': 'mldr', '\xB7': 'middot', '\'': 'apos', '\u2018': 'lsquo', '\u2019': 'rsquo', '\u201A': 'sbquo', '\u2039': 'lsaquo', '\u203A': 'rsaquo', '"': 'quot', '\u201C': 'ldquo', '\u201D': 'rdquo', '\u201E': 'bdquo', '\xAB': 'laquo', '\xBB': 'raquo', '(': 'lpar', ')': 'rpar', '[': 'lsqb', ']': 'rsqb', '{': 'lcub', '}': 'rcub', '\u2308': 'lceil', '\u2309': 'rceil', '\u230A': 'lfloor', '\u230B': 'rfloor', '\u2985': 'lopar', '\u2986': 'ropar', '\u298B': 'lbrke', '\u298C': 'rbrke', '\u298D': 'lbrkslu', '\u298E': 'rbrksld', '\u298F': 'lbrksld', '\u2990': 'rbrkslu', '\u2991': 'langd', '\u2992': 'rangd', '\u2993': 'lparlt', '\u2994': 'rpargt', '\u2995': 'gtlPar', '\u2996': 'ltrPar', '\u27E6': 'lobrk', '\u27E7': 'robrk', '\u27E8': 'lang', '\u27E9': 'rang', '\u27EA': 'Lang', '\u27EB': 'Rang', '\u27EC': 'loang', '\u27ED': 'roang', '\u2772': 'lbbrk', '\u2773': 'rbbrk', '\u2016': 'Vert', '\xA7': 'sect', '\xB6': 'para', '@': 'commat', '*': 'ast', '/': 'sol', 'undefined': null, '&': 'amp', '#': 'num', '%': 'percnt', '\u2030': 'permil', '\u2031': 'pertenk', '\u2020': 'dagger', '\u2021': 'Dagger', '\u2022': 'bull', '\u2043': 'hybull', '\u2032': 'prime', '\u2033': 'Prime', '\u2034': 'tprime', '\u2057': 'qprime', '\u2035': 'bprime', '\u2041': 'caret', '`': 'grave', '\xB4': 'acute', '\u02DC': 'tilde', '^': 'Hat', '\xAF': 'macr', '\u02D8': 'breve', '\u02D9': 'dot', '\xA8': 'die', '\u02DA': 'ring', '\u02DD': 'dblac', '\xB8': 'cedil', '\u02DB': 'ogon', '\u02C6': 'circ', '\u02C7': 'caron', '\xB0': 'deg', '\xA9': 'copy', '\xAE': 'reg', '\u2117': 'copysr', '\u2118': 'wp', '\u211E': 'rx', '\u2127': 'mho', '\u2129': 'iiota', '\u2190': 'larr', '\u219A': 'nlarr', '\u2192': 'rarr', '\u219B': 'nrarr', '\u2191': 'uarr', '\u2193': 'darr', '\u2194': 'harr', '\u21AE': 'nharr', '\u2195': 'varr', '\u2196': 'nwarr', '\u2197': 'nearr', '\u2198': 'searr', '\u2199': 'swarr', '\u219D': 'rarrw', '\u219D\u0338': 'nrarrw', '\u219E': 'Larr', '\u219F': 'Uarr', '\u21A0': 'Rarr', '\u21A1': 'Darr', '\u21A2': 'larrtl', '\u21A3': 'rarrtl', '\u21A4': 'mapstoleft', '\u21A5': 'mapstoup', '\u21A6': 'map', '\u21A7': 'mapstodown', '\u21A9': 'larrhk', '\u21AA': 'rarrhk', '\u21AB': 'larrlp', '\u21AC': 'rarrlp', '\u21AD': 'harrw', '\u21B0': 'lsh', '\u21B1': 'rsh', '\u21B2': 'ldsh', '\u21B3': 'rdsh', '\u21B5': 'crarr', '\u21B6': 'cularr', '\u21B7': 'curarr', '\u21BA': 'olarr', '\u21BB': 'orarr', '\u21BC': 'lharu', '\u21BD': 'lhard', '\u21BE': 'uharr', '\u21BF': 'uharl', '\u21C0': 'rharu', '\u21C1': 'rhard', '\u21C2': 'dharr', '\u21C3': 'dharl', '\u21C4': 'rlarr', '\u21C5': 'udarr', '\u21C6': 'lrarr', '\u21C7': 'llarr', '\u21C8': 'uuarr', '\u21C9': 'rrarr', '\u21CA': 'ddarr', '\u21CB': 'lrhar', '\u21CC': 'rlhar', '\u21D0': 'lArr', '\u21CD': 'nlArr', '\u21D1': 'uArr', '\u21D2': 'rArr', '\u21CF': 'nrArr', '\u21D3': 'dArr', '\u21D4': 'iff', '\u21CE': 'nhArr', '\u21D5': 'vArr', '\u21D6': 'nwArr', '\u21D7': 'neArr', '\u21D8': 'seArr', '\u21D9': 'swArr', '\u21DA': 'lAarr', '\u21DB': 'rAarr', '\u21DD': 'zigrarr', '\u21E4': 'larrb', '\u21E5': 'rarrb', '\u21F5': 'duarr', '\u21FD': 'loarr', '\u21FE': 'roarr', '\u21FF': 'hoarr', '\u2200': 'forall', '\u2201': 'comp', '\u2202': 'part', '\u2202\u0338': 'npart', '\u2203': 'exist', '\u2204': 'nexist', '\u2205': 'empty', '\u2207': 'Del', '\u2208': 'in', '\u2209': 'notin', '\u220B': 'ni', '\u220C': 'notni', '\u03F6': 'bepsi', '\u220F': 'prod', '\u2210': 'coprod', '\u2211': 'sum', '+': 'plus', '\xB1': 'pm', '\xF7': 'div', '\xD7': 'times', '<': 'lt', '\u226E': 'nlt', '<\u20D2': 'nvlt', '=': 'equals', '\u2260': 'ne', '=\u20E5': 'bne', '\u2A75': 'Equal', '>': 'gt', '\u226F': 'ngt', '>\u20D2': 'nvgt', '\xAC': 'not', '|': 'vert', '\xA6': 'brvbar', '\u2212': 'minus', '\u2213': 'mp', '\u2214': 'plusdo', '\u2044': 'frasl', '\u2216': 'setmn', '\u2217': 'lowast', '\u2218': 'compfn', '\u221A': 'Sqrt', '\u221D': 'prop', '\u221E': 'infin', '\u221F': 'angrt', '\u2220': 'ang', '\u2220\u20D2': 'nang', '\u2221': 'angmsd', '\u2222': 'angsph', '\u2223': 'mid', '\u2224': 'nmid', '\u2225': 'par', '\u2226': 'npar', '\u2227': 'and', '\u2228': 'or', '\u2229': 'cap', '\u2229\uFE00': 'caps', '\u222A': 'cup', '\u222A\uFE00': 'cups', '\u222B': 'int', '\u222C': 'Int', '\u222D': 'tint', '\u2A0C': 'qint', '\u222E': 'oint', '\u222F': 'Conint', '\u2230': 'Cconint', '\u2231': 'cwint', '\u2232': 'cwconint', '\u2233': 'awconint', '\u2234': 'there4', '\u2235': 'becaus', '\u2236': 'ratio', '\u2237': 'Colon', '\u2238': 'minusd', '\u223A': 'mDDot', '\u223B': 'homtht', '\u223C': 'sim', '\u2241': 'nsim', '\u223C\u20D2': 'nvsim', '\u223D': 'bsim', '\u223D\u0331': 'race', '\u223E': 'ac', '\u223E\u0333': 'acE', '\u223F': 'acd', '\u2240': 'wr', '\u2242': 'esim', '\u2242\u0338': 'nesim', '\u2243': 'sime', '\u2244': 'nsime', '\u2245': 'cong', '\u2247': 'ncong', '\u2246': 'simne', '\u2248': 'ap', '\u2249': 'nap', '\u224A': 'ape', '\u224B': 'apid', '\u224B\u0338': 'napid', '\u224C': 'bcong', '\u224D': 'CupCap', '\u226D': 'NotCupCap', '\u224D\u20D2': 'nvap', '\u224E': 'bump', '\u224E\u0338': 'nbump', '\u224F': 'bumpe', '\u224F\u0338': 'nbumpe', '\u2250': 'doteq', '\u2250\u0338': 'nedot', '\u2251': 'eDot', '\u2252': 'efDot', '\u2253': 'erDot', '\u2254': 'colone', '\u2255': 'ecolon', '\u2256': 'ecir', '\u2257': 'cire', '\u2259': 'wedgeq', '\u225A': 'veeeq', '\u225C': 'trie', '\u225F': 'equest', '\u2261': 'equiv', '\u2262': 'nequiv', '\u2261\u20E5': 'bnequiv', '\u2264': 'le', '\u2270': 'nle', '\u2264\u20D2': 'nvle', '\u2265': 'ge', '\u2271': 'nge', '\u2265\u20D2': 'nvge', '\u2266': 'lE', '\u2266\u0338': 'nlE', '\u2267': 'gE', '\u2267\u0338': 'ngE', '\u2268\uFE00': 'lvnE', '\u2268': 'lnE', '\u2269': 'gnE', '\u2269\uFE00': 'gvnE', '\u226A': 'll', '\u226A\u0338': 'nLtv', '\u226A\u20D2': 'nLt', '\u226B': 'gg', '\u226B\u0338': 'nGtv', '\u226B\u20D2': 'nGt', '\u226C': 'twixt', '\u2272': 'lsim', '\u2274': 'nlsim', '\u2273': 'gsim', '\u2275': 'ngsim', '\u2276': 'lg', '\u2278': 'ntlg', '\u2277': 'gl', '\u2279': 'ntgl', '\u227A': 'pr', '\u2280': 'npr', '\u227B': 'sc', '\u2281': 'nsc', '\u227C': 'prcue', '\u22E0': 'nprcue', '\u227D': 'sccue', '\u22E1': 'nsccue', '\u227E': 'prsim', '\u227F': 'scsim', '\u227F\u0338': 'NotSucceedsTilde', '\u2282': 'sub', '\u2284': 'nsub', '\u2282\u20D2': 'vnsub', '\u2283': 'sup', '\u2285': 'nsup', '\u2283\u20D2': 'vnsup', '\u2286': 'sube', '\u2288': 'nsube', '\u2287': 'supe', '\u2289': 'nsupe', '\u228A\uFE00': 'vsubne', '\u228A': 'subne', '\u228B\uFE00': 'vsupne', '\u228B': 'supne', '\u228D': 'cupdot', '\u228E': 'uplus', '\u228F': 'sqsub', '\u228F\u0338': 'NotSquareSubset', '\u2290': 'sqsup', '\u2290\u0338': 'NotSquareSuperset', '\u2291': 'sqsube', '\u22E2': 'nsqsube', '\u2292': 'sqsupe', '\u22E3': 'nsqsupe', '\u2293': 'sqcap', '\u2293\uFE00': 'sqcaps', '\u2294': 'sqcup', '\u2294\uFE00': 'sqcups', '\u2295': 'oplus', '\u2296': 'ominus', '\u2297': 'otimes', '\u2298': 'osol', '\u2299': 'odot', '\u229A': 'ocir', '\u229B': 'oast', '\u229D': 'odash', '\u229E': 'plusb', '\u229F': 'minusb', '\u22A0': 'timesb', '\u22A1': 'sdotb', '\u22A2': 'vdash', '\u22AC': 'nvdash', '\u22A3': 'dashv', '\u22A4': 'top', '\u22A5': 'bot', '\u22A7': 'models', '\u22A8': 'vDash', '\u22AD': 'nvDash', '\u22A9': 'Vdash', '\u22AE': 'nVdash', '\u22AA': 'Vvdash', '\u22AB': 'VDash', '\u22AF': 'nVDash', '\u22B0': 'prurel', '\u22B2': 'vltri', '\u22EA': 'nltri', '\u22B3': 'vrtri', '\u22EB': 'nrtri', '\u22B4': 'ltrie', '\u22EC': 'nltrie', '\u22B4\u20D2': 'nvltrie', '\u22B5': 'rtrie', '\u22ED': 'nrtrie', '\u22B5\u20D2': 'nvrtrie', '\u22B6': 'origof', '\u22B7': 'imof', '\u22B8': 'mumap', '\u22B9': 'hercon', '\u22BA': 'intcal', '\u22BB': 'veebar', '\u22BD': 'barvee', '\u22BE': 'angrtvb', '\u22BF': 'lrtri', '\u22C0': 'Wedge', '\u22C1': 'Vee', '\u22C2': 'xcap', '\u22C3': 'xcup', '\u22C4': 'diam', '\u22C5': 'sdot', '\u22C6': 'Star', '\u22C7': 'divonx', '\u22C8': 'bowtie', '\u22C9': 'ltimes', '\u22CA': 'rtimes', '\u22CB': 'lthree', '\u22CC': 'rthree', '\u22CD': 'bsime', '\u22CE': 'cuvee', '\u22CF': 'cuwed', '\u22D0': 'Sub', '\u22D1': 'Sup', '\u22D2': 'Cap', '\u22D3': 'Cup', '\u22D4': 'fork', '\u22D5': 'epar', '\u22D6': 'ltdot', '\u22D7': 'gtdot', '\u22D8': 'Ll', '\u22D8\u0338': 'nLl', '\u22D9': 'Gg', '\u22D9\u0338': 'nGg', '\u22DA\uFE00': 'lesg', '\u22DA': 'leg', '\u22DB': 'gel', '\u22DB\uFE00': 'gesl', '\u22DE': 'cuepr', '\u22DF': 'cuesc', '\u22E6': 'lnsim', '\u22E7': 'gnsim', '\u22E8': 'prnsim', '\u22E9': 'scnsim', '\u22EE': 'vellip', '\u22EF': 'ctdot', '\u22F0': 'utdot', '\u22F1': 'dtdot', '\u22F2': 'disin', '\u22F3': 'isinsv', '\u22F4': 'isins', '\u22F5': 'isindot', '\u22F5\u0338': 'notindot', '\u22F6': 'notinvc', '\u22F7': 'notinvb', '\u22F9': 'isinE', '\u22F9\u0338': 'notinE', '\u22FA': 'nisd', '\u22FB': 'xnis', '\u22FC': 'nis', '\u22FD': 'notnivc', '\u22FE': 'notnivb', '\u2305': 'barwed', '\u2306': 'Barwed', '\u230C': 'drcrop', '\u230D': 'dlcrop', '\u230E': 'urcrop', '\u230F': 'ulcrop', '\u2310': 'bnot', '\u2312': 'profline', '\u2313': 'profsurf', '\u2315': 'telrec', '\u2316': 'target', '\u231C': 'ulcorn', '\u231D': 'urcorn', '\u231E': 'dlcorn', '\u231F': 'drcorn', '\u2322': 'frown', '\u2323': 'smile', '\u232D': 'cylcty', '\u232E': 'profalar', '\u2336': 'topbot', '\u233D': 'ovbar', '\u233F': 'solbar', '\u237C': 'angzarr', '\u23B0': 'lmoust', '\u23B1': 'rmoust', '\u23B4': 'tbrk', '\u23B5': 'bbrk', '\u23B6': 'bbrktbrk', '\u23DC': 'OverParenthesis', '\u23DD': 'UnderParenthesis', '\u23DE': 'OverBrace', '\u23DF': 'UnderBrace', '\u23E2': 'trpezium', '\u23E7': 'elinters', '\u2423': 'blank', '\u2500': 'boxh', '\u2502': 'boxv', '\u250C': 'boxdr', '\u2510': 'boxdl', '\u2514': 'boxur', '\u2518': 'boxul', '\u251C': 'boxvr', '\u2524': 'boxvl', '\u252C': 'boxhd', '\u2534': 'boxhu', '\u253C': 'boxvh', '\u2550': 'boxH', '\u2551': 'boxV', '\u2552': 'boxdR', '\u2553': 'boxDr', '\u2554': 'boxDR', '\u2555': 'boxdL', '\u2556': 'boxDl', '\u2557': 'boxDL', '\u2558': 'boxuR', '\u2559': 'boxUr', '\u255A': 'boxUR', '\u255B': 'boxuL', '\u255C': 'boxUl', '\u255D': 'boxUL', '\u255E': 'boxvR', '\u255F': 'boxVr', '\u2560': 'boxVR', '\u2561': 'boxvL', '\u2562': 'boxVl', '\u2563': 'boxVL', '\u2564': 'boxHd', '\u2565': 'boxhD', '\u2566': 'boxHD', '\u2567': 'boxHu', '\u2568': 'boxhU', '\u2569': 'boxHU', '\u256A': 'boxvH', '\u256B': 'boxVh', '\u256C': 'boxVH', '\u2580': 'uhblk', '\u2584': 'lhblk', '\u2588': 'block', '\u2591': 'blk14', '\u2592': 'blk12', '\u2593': 'blk34', '\u25A1': 'squ', '\u25AA': 'squf', '\u25AB': 'EmptyVerySmallSquare', '\u25AD': 'rect', '\u25AE': 'marker', '\u25B1': 'fltns', '\u25B3': 'xutri', '\u25B4': 'utrif', '\u25B5': 'utri', '\u25B8': 'rtrif', '\u25B9': 'rtri', '\u25BD': 'xdtri', '\u25BE': 'dtrif', '\u25BF': 'dtri', '\u25C2': 'ltrif', '\u25C3': 'ltri', '\u25CA': 'loz', '\u25CB': 'cir', '\u25EC': 'tridot', '\u25EF': 'xcirc', '\u25F8': 'ultri', '\u25F9': 'urtri', '\u25FA': 'lltri', '\u25FB': 'EmptySmallSquare', '\u25FC': 'FilledSmallSquare', '\u2605': 'starf', '\u2606': 'star', '\u260E': 'phone', '\u2640': 'female', '\u2642': 'male', '\u2660': 'spades', '\u2663': 'clubs', '\u2665': 'hearts', '\u2666': 'diams', '\u266A': 'sung', '\u2713': 'check', '\u2717': 'cross', '\u2720': 'malt', '\u2736': 'sext', '\u2758': 'VerticalSeparator', '\u27C8': 'bsolhsub', '\u27C9': 'suphsol', '\u27F5': 'xlarr', '\u27F6': 'xrarr', '\u27F7': 'xharr', '\u27F8': 'xlArr', '\u27F9': 'xrArr', '\u27FA': 'xhArr', '\u27FC': 'xmap', '\u27FF': 'dzigrarr', '\u2902': 'nvlArr', '\u2903': 'nvrArr', '\u2904': 'nvHarr', '\u2905': 'Map', '\u290C': 'lbarr', '\u290D': 'rbarr', '\u290E': 'lBarr', '\u290F': 'rBarr', '\u2910': 'RBarr', '\u2911': 'DDotrahd', '\u2912': 'UpArrowBar', '\u2913': 'DownArrowBar', '\u2916': 'Rarrtl', '\u2919': 'latail', '\u291A': 'ratail', '\u291B': 'lAtail', '\u291C': 'rAtail', '\u291D': 'larrfs', '\u291E': 'rarrfs', '\u291F': 'larrbfs', '\u2920': 'rarrbfs', '\u2923': 'nwarhk', '\u2924': 'nearhk', '\u2925': 'searhk', '\u2926': 'swarhk', '\u2927': 'nwnear', '\u2928': 'toea', '\u2929': 'tosa', '\u292A': 'swnwar', '\u2933': 'rarrc', '\u2933\u0338': 'nrarrc', '\u2935': 'cudarrr', '\u2936': 'ldca', '\u2937': 'rdca', '\u2938': 'cudarrl', '\u2939': 'larrpl', '\u293C': 'curarrm', '\u293D': 'cularrp', '\u2945': 'rarrpl', '\u2948': 'harrcir', '\u2949': 'Uarrocir', '\u294A': 'lurdshar', '\u294B': 'ldrushar', '\u294E': 'LeftRightVector', '\u294F': 'RightUpDownVector', '\u2950': 'DownLeftRightVector', '\u2951': 'LeftUpDownVector', '\u2952': 'LeftVectorBar', '\u2953': 'RightVectorBar', '\u2954': 'RightUpVectorBar', '\u2955': 'RightDownVectorBar', '\u2956': 'DownLeftVectorBar', '\u2957': 'DownRightVectorBar', '\u2958': 'LeftUpVectorBar', '\u2959': 'LeftDownVectorBar', '\u295A': 'LeftTeeVector', '\u295B': 'RightTeeVector', '\u295C': 'RightUpTeeVector', '\u295D': 'RightDownTeeVector', '\u295E': 'DownLeftTeeVector', '\u295F': 'DownRightTeeVector', '\u2960': 'LeftUpTeeVector', '\u2961': 'LeftDownTeeVector', '\u2962': 'lHar', '\u2963': 'uHar', '\u2964': 'rHar', '\u2965': 'dHar', '\u2966': 'luruhar', '\u2967': 'ldrdhar', '\u2968': 'ruluhar', '\u2969': 'rdldhar', '\u296A': 'lharul', '\u296B': 'llhard', '\u296C': 'rharul', '\u296D': 'lrhard', '\u296E': 'udhar', '\u296F': 'duhar', '\u2970': 'RoundImplies', '\u2971': 'erarr', '\u2972': 'simrarr', '\u2973': 'larrsim', '\u2974': 'rarrsim', '\u2975': 'rarrap', '\u2976': 'ltlarr', '\u2978': 'gtrarr', '\u2979': 'subrarr', '\u297B': 'suplarr', '\u297C': 'lfisht', '\u297D': 'rfisht', '\u297E': 'ufisht', '\u297F': 'dfisht', '\u299A': 'vzigzag', '\u299C': 'vangrt', '\u299D': 'angrtvbd', '\u29A4': 'ange', '\u29A5': 'range', '\u29A6': 'dwangle', '\u29A7': 'uwangle', '\u29A8': 'angmsdaa', '\u29A9': 'angmsdab', '\u29AA': 'angmsdac', '\u29AB': 'angmsdad', '\u29AC': 'angmsdae', '\u29AD': 'angmsdaf', '\u29AE': 'angmsdag', '\u29AF': 'angmsdah', '\u29B0': 'bemptyv', '\u29B1': 'demptyv', '\u29B2': 'cemptyv', '\u29B3': 'raemptyv', '\u29B4': 'laemptyv', '\u29B5': 'ohbar', '\u29B6': 'omid', '\u29B7': 'opar', '\u29B9': 'operp', '\u29BB': 'olcross', '\u29BC': 'odsold', '\u29BE': 'olcir', '\u29BF': 'ofcir', '\u29C0': 'olt', '\u29C1': 'ogt', '\u29C2': 'cirscir', '\u29C3': 'cirE', '\u29C4': 'solb', '\u29C5': 'bsolb', '\u29C9': 'boxbox', '\u29CD': 'trisb', '\u29CE': 'rtriltri', '\u29CF': 'LeftTriangleBar', '\u29CF\u0338': 'NotLeftTriangleBar', '\u29D0': 'RightTriangleBar', '\u29D0\u0338': 'NotRightTriangleBar', '\u29DC': 'iinfin', '\u29DD': 'infintie', '\u29DE': 'nvinfin', '\u29E3': 'eparsl', '\u29E4': 'smeparsl', '\u29E5': 'eqvparsl', '\u29EB': 'lozf', '\u29F4': 'RuleDelayed', '\u29F6': 'dsol', '\u2A00': 'xodot', '\u2A01': 'xoplus', '\u2A02': 'xotime', '\u2A04': 'xuplus', '\u2A06': 'xsqcup', '\u2A0D': 'fpartint', '\u2A10': 'cirfnint', '\u2A11': 'awint', '\u2A12': 'rppolint', '\u2A13': 'scpolint', '\u2A14': 'npolint', '\u2A15': 'pointint', '\u2A16': 'quatint', '\u2A17': 'intlarhk', '\u2A22': 'pluscir', '\u2A23': 'plusacir', '\u2A24': 'simplus', '\u2A25': 'plusdu', '\u2A26': 'plussim', '\u2A27': 'plustwo', '\u2A29': 'mcomma', '\u2A2A': 'minusdu', '\u2A2D': 'loplus', '\u2A2E': 'roplus', '\u2A2F': 'Cross', '\u2A30': 'timesd', '\u2A31': 'timesbar', '\u2A33': 'smashp', '\u2A34': 'lotimes', '\u2A35': 'rotimes', '\u2A36': 'otimesas', '\u2A37': 'Otimes', '\u2A38': 'odiv', '\u2A39': 'triplus', '\u2A3A': 'triminus', '\u2A3B': 'tritime', '\u2A3C': 'iprod', '\u2A3F': 'amalg', '\u2A40': 'capdot', '\u2A42': 'ncup', '\u2A43': 'ncap', '\u2A44': 'capand', '\u2A45': 'cupor', '\u2A46': 'cupcap', '\u2A47': 'capcup', '\u2A48': 'cupbrcap', '\u2A49': 'capbrcup', '\u2A4A': 'cupcup', '\u2A4B': 'capcap', '\u2A4C': 'ccups', '\u2A4D': 'ccaps', '\u2A50': 'ccupssm', '\u2A53': 'And', '\u2A54': 'Or', '\u2A55': 'andand', '\u2A56': 'oror', '\u2A57': 'orslope', '\u2A58': 'andslope', '\u2A5A': 'andv', '\u2A5B': 'orv', '\u2A5C': 'andd', '\u2A5D': 'ord', '\u2A5F': 'wedbar', '\u2A66': 'sdote', '\u2A6A': 'simdot', '\u2A6D': 'congdot', '\u2A6D\u0338': 'ncongdot', '\u2A6E': 'easter', '\u2A6F': 'apacir', '\u2A70': 'apE', '\u2A70\u0338': 'napE', '\u2A71': 'eplus', '\u2A72': 'pluse', '\u2A73': 'Esim', '\u2A77': 'eDDot', '\u2A78': 'equivDD', '\u2A79': 'ltcir', '\u2A7A': 'gtcir', '\u2A7B': 'ltquest', '\u2A7C': 'gtquest', '\u2A7D': 'les', '\u2A7D\u0338': 'nles', '\u2A7E': 'ges', '\u2A7E\u0338': 'nges', '\u2A7F': 'lesdot', '\u2A80': 'gesdot', '\u2A81': 'lesdoto', '\u2A82': 'gesdoto', '\u2A83': 'lesdotor', '\u2A84': 'gesdotol', '\u2A85': 'lap', '\u2A86': 'gap', '\u2A87': 'lne', '\u2A88': 'gne', '\u2A89': 'lnap', '\u2A8A': 'gnap', '\u2A8B': 'lEg', '\u2A8C': 'gEl', '\u2A8D': 'lsime', '\u2A8E': 'gsime', '\u2A8F': 'lsimg', '\u2A90': 'gsiml', '\u2A91': 'lgE', '\u2A92': 'glE', '\u2A93': 'lesges', '\u2A94': 'gesles', '\u2A95': 'els', '\u2A96': 'egs', '\u2A97': 'elsdot', '\u2A98': 'egsdot', '\u2A99': 'el', '\u2A9A': 'eg', '\u2A9D': 'siml', '\u2A9E': 'simg', '\u2A9F': 'simlE', '\u2AA0': 'simgE', '\u2AA1': 'LessLess', '\u2AA1\u0338': 'NotNestedLessLess', '\u2AA2': 'GreaterGreater', '\u2AA2\u0338': 'NotNestedGreaterGreater', '\u2AA4': 'glj', '\u2AA5': 'gla', '\u2AA6': 'ltcc', '\u2AA7': 'gtcc', '\u2AA8': 'lescc', '\u2AA9': 'gescc', '\u2AAA': 'smt', '\u2AAB': 'lat', '\u2AAC': 'smte', '\u2AAC\uFE00': 'smtes', '\u2AAD': 'late', '\u2AAD\uFE00': 'lates', '\u2AAE': 'bumpE', '\u2AAF': 'pre', '\u2AAF\u0338': 'npre', '\u2AB0': 'sce', '\u2AB0\u0338': 'nsce', '\u2AB3': 'prE', '\u2AB4': 'scE', '\u2AB5': 'prnE', '\u2AB6': 'scnE', '\u2AB7': 'prap', '\u2AB8': 'scap', '\u2AB9': 'prnap', '\u2ABA': 'scnap', '\u2ABB': 'Pr', '\u2ABC': 'Sc', '\u2ABD': 'subdot', '\u2ABE': 'supdot', '\u2ABF': 'subplus', '\u2AC0': 'supplus', '\u2AC1': 'submult', '\u2AC2': 'supmult', '\u2AC3': 'subedot', '\u2AC4': 'supedot', '\u2AC5': 'subE', '\u2AC5\u0338': 'nsubE', '\u2AC6': 'supE', '\u2AC6\u0338': 'nsupE', '\u2AC7': 'subsim', '\u2AC8': 'supsim', '\u2ACB\uFE00': 'vsubnE', '\u2ACB': 'subnE', '\u2ACC\uFE00': 'vsupnE', '\u2ACC': 'supnE', '\u2ACF': 'csub', '\u2AD0': 'csup', '\u2AD1': 'csube', '\u2AD2': 'csupe', '\u2AD3': 'subsup', '\u2AD4': 'supsub', '\u2AD5': 'subsub', '\u2AD6': 'supsup', '\u2AD7': 'suphsub', '\u2AD8': 'supdsub', '\u2AD9': 'forkv', '\u2ADA': 'topfork', '\u2ADB': 'mlcp', '\u2AE4': 'Dashv', '\u2AE6': 'Vdashl', '\u2AE7': 'Barv', '\u2AE8': 'vBar', '\u2AE9': 'vBarv', '\u2AEB': 'Vbar', '\u2AEC': 'Not', '\u2AED': 'bNot', '\u2AEE': 'rnmid', '\u2AEF': 'cirmid', '\u2AF0': 'midcir', '\u2AF1': 'topcir', '\u2AF2': 'nhpar', '\u2AF3': 'parsim', '\u2AFD': 'parsl', '\u2AFD\u20E5': 'nparsl', '\u266D': 'flat', '\u266E': 'natur', '\u266F': 'sharp', '\xA4': 'curren', '\xA2': 'cent', '$': 'dollar', '\xA3': 'pound', '\xA5': 'yen', '\u20AC': 'euro', '\xB9': 'sup1', '\xBD': 'half', '\u2153': 'frac13', '\xBC': 'frac14', '\u2155': 'frac15', '\u2159': 'frac16', '\u215B': 'frac18', '\xB2': 'sup2', '\u2154': 'frac23', '\u2156': 'frac25', '\xB3': 'sup3', '\xBE': 'frac34', '\u2157': 'frac35', '\u215C': 'frac38', '\u2158': 'frac45', '\u215A': 'frac56', '\u215D': 'frac58', '\u215E': 'frac78', '\uD835\uDCB6': 'ascr', '\uD835\uDD52': 'aopf', '\uD835\uDD1E': 'afr', '\uD835\uDD38': 'Aopf', '\uD835\uDD04': 'Afr', '\uD835\uDC9C': 'Ascr', '\xAA': 'ordf', '\xE1': 'aacute', '\xC1': 'Aacute', '\xE0': 'agrave', '\xC0': 'Agrave', '\u0103': 'abreve', '\u0102': 'Abreve', '\xE2': 'acirc', '\xC2': 'Acirc', '\xE5': 'aring', '\xC5': 'angst', '\xE4': 'auml', '\xC4': 'Auml', '\xE3': 'atilde', '\xC3': 'Atilde', '\u0105': 'aogon', '\u0104': 'Aogon', '\u0101': 'amacr', '\u0100': 'Amacr', '\xE6': 'aelig', '\xC6': 'AElig', '\uD835\uDCB7': 'bscr', '\uD835\uDD53': 'bopf', '\uD835\uDD1F': 'bfr', '\uD835\uDD39': 'Bopf', '\u212C': 'Bscr', '\uD835\uDD05': 'Bfr', '\uD835\uDD20': 'cfr', '\uD835\uDCB8': 'cscr', '\uD835\uDD54': 'copf', '\u212D': 'Cfr', '\uD835\uDC9E': 'Cscr', '\u2102': 'Copf', '\u0107': 'cacute', '\u0106': 'Cacute', '\u0109': 'ccirc', '\u0108': 'Ccirc', '\u010D': 'ccaron', '\u010C': 'Ccaron', '\u010B': 'cdot', '\u010A': 'Cdot', '\xE7': 'ccedil', '\xC7': 'Ccedil', '\u2105': 'incare', '\uD835\uDD21': 'dfr', '\u2146': 'dd', '\uD835\uDD55': 'dopf', '\uD835\uDCB9': 'dscr', '\uD835\uDC9F': 'Dscr', '\uD835\uDD07': 'Dfr', '\u2145': 'DD', '\uD835\uDD3B': 'Dopf', '\u010F': 'dcaron', '\u010E': 'Dcaron', '\u0111': 'dstrok', '\u0110': 'Dstrok', '\xF0': 'eth', '\xD0': 'ETH', '\u2147': 'ee', '\u212F': 'escr', '\uD835\uDD22': 'efr', '\uD835\uDD56': 'eopf', '\u2130': 'Escr', '\uD835\uDD08': 'Efr', '\uD835\uDD3C': 'Eopf', '\xE9': 'eacute', '\xC9': 'Eacute', '\xE8': 'egrave', '\xC8': 'Egrave', '\xEA': 'ecirc', '\xCA': 'Ecirc', '\u011B': 'ecaron', '\u011A': 'Ecaron', '\xEB': 'euml', '\xCB': 'Euml', '\u0117': 'edot', '\u0116': 'Edot', '\u0119': 'eogon', '\u0118': 'Eogon', '\u0113': 'emacr', '\u0112': 'Emacr', '\uD835\uDD23': 'ffr', '\uD835\uDD57': 'fopf', '\uD835\uDCBB': 'fscr', '\uD835\uDD09': 'Ffr', '\uD835\uDD3D': 'Fopf', '\u2131': 'Fscr', '\uFB00': 'fflig', '\uFB03': 'ffilig', '\uFB04': 'ffllig', '\uFB01': 'filig', 'fj': 'fjlig', '\uFB02': 'fllig', '\u0192': 'fnof', '\u210A': 'gscr', '\uD835\uDD58': 'gopf', '\uD835\uDD24': 'gfr', '\uD835\uDCA2': 'Gscr', '\uD835\uDD3E': 'Gopf', '\uD835\uDD0A': 'Gfr', '\u01F5': 'gacute', '\u011F': 'gbreve', '\u011E': 'Gbreve', '\u011D': 'gcirc', '\u011C': 'Gcirc', '\u0121': 'gdot', '\u0120': 'Gdot', '\u0122': 'Gcedil', '\uD835\uDD25': 'hfr', '\u210E': 'planckh', '\uD835\uDCBD': 'hscr', '\uD835\uDD59': 'hopf', '\u210B': 'Hscr', '\u210C': 'Hfr', '\u210D': 'Hopf', '\u0125': 'hcirc', '\u0124': 'Hcirc', '\u210F': 'hbar', '\u0127': 'hstrok', '\u0126': 'Hstrok', '\uD835\uDD5A': 'iopf', '\uD835\uDD26': 'ifr', '\uD835\uDCBE': 'iscr', '\u2148': 'ii', '\uD835\uDD40': 'Iopf', '\u2110': 'Iscr', '\u2111': 'Im', '\xED': 'iacute', '\xCD': 'Iacute', '\xEC': 'igrave', '\xCC': 'Igrave', '\xEE': 'icirc', '\xCE': 'Icirc', '\xEF': 'iuml', '\xCF': 'Iuml', '\u0129': 'itilde', '\u0128': 'Itilde', '\u0130': 'Idot', '\u012F': 'iogon', '\u012E': 'Iogon', '\u012B': 'imacr', '\u012A': 'Imacr', '\u0133': 'ijlig', '\u0132': 'IJlig', '\u0131': 'imath', '\uD835\uDCBF': 'jscr', '\uD835\uDD5B': 'jopf', '\uD835\uDD27': 'jfr', '\uD835\uDCA5': 'Jscr', '\uD835\uDD0D': 'Jfr', '\uD835\uDD41': 'Jopf', '\u0135': 'jcirc', '\u0134': 'Jcirc', '\u0237': 'jmath', '\uD835\uDD5C': 'kopf', '\uD835\uDCC0': 'kscr', '\uD835\uDD28': 'kfr', '\uD835\uDCA6': 'Kscr', '\uD835\uDD42': 'Kopf', '\uD835\uDD0E': 'Kfr', '\u0137': 'kcedil', '\u0136': 'Kcedil', '\uD835\uDD29': 'lfr', '\uD835\uDCC1': 'lscr', '\u2113': 'ell', '\uD835\uDD5D': 'lopf', '\u2112': 'Lscr', '\uD835\uDD0F': 'Lfr', '\uD835\uDD43': 'Lopf', '\u013A': 'lacute', '\u0139': 'Lacute', '\u013E': 'lcaron', '\u013D': 'Lcaron', '\u013C': 'lcedil', '\u013B': 'Lcedil', '\u0142': 'lstrok', '\u0141': 'Lstrok', '\u0140': 'lmidot', '\u013F': 'Lmidot', '\uD835\uDD2A': 'mfr', '\uD835\uDD5E': 'mopf', '\uD835\uDCC2': 'mscr', '\uD835\uDD10': 'Mfr', '\uD835\uDD44': 'Mopf', '\u2133': 'Mscr', '\uD835\uDD2B': 'nfr', '\uD835\uDD5F': 'nopf', '\uD835\uDCC3': 'nscr', '\u2115': 'Nopf', '\uD835\uDCA9': 'Nscr', '\uD835\uDD11': 'Nfr', '\u0144': 'nacute', '\u0143': 'Nacute', '\u0148': 'ncaron', '\u0147': 'Ncaron', '\xF1': 'ntilde', '\xD1': 'Ntilde', '\u0146': 'ncedil', '\u0145': 'Ncedil', '\u2116': 'numero', '\u014B': 'eng', '\u014A': 'ENG', '\uD835\uDD60': 'oopf', '\uD835\uDD2C': 'ofr', '\u2134': 'oscr', '\uD835\uDCAA': 'Oscr', '\uD835\uDD12': 'Ofr', '\uD835\uDD46': 'Oopf', '\xBA': 'ordm', '\xF3': 'oacute', '\xD3': 'Oacute', '\xF2': 'ograve', '\xD2': 'Ograve', '\xF4': 'ocirc', '\xD4': 'Ocirc', '\xF6': 'ouml', '\xD6': 'Ouml', '\u0151': 'odblac', '\u0150': 'Odblac', '\xF5': 'otilde', '\xD5': 'Otilde', '\xF8': 'oslash', '\xD8': 'Oslash', '\u014D': 'omacr', '\u014C': 'Omacr', '\u0153': 'oelig', '\u0152': 'OElig', '\uD835\uDD2D': 'pfr', '\uD835\uDCC5': 'pscr', '\uD835\uDD61': 'popf', '\u2119': 'Popf', '\uD835\uDD13': 'Pfr', '\uD835\uDCAB': 'Pscr', '\uD835\uDD62': 'qopf', '\uD835\uDD2E': 'qfr', '\uD835\uDCC6': 'qscr', '\uD835\uDCAC': 'Qscr', '\uD835\uDD14': 'Qfr', '\u211A': 'Qopf', '\u0138': 'kgreen', '\uD835\uDD2F': 'rfr', '\uD835\uDD63': 'ropf', '\uD835\uDCC7': 'rscr', '\u211B': 'Rscr', '\u211C': 'Re', '\u211D': 'Ropf', '\u0155': 'racute', '\u0154': 'Racute', '\u0159': 'rcaron', '\u0158': 'Rcaron', '\u0157': 'rcedil', '\u0156': 'Rcedil', '\uD835\uDD64': 'sopf', '\uD835\uDCC8': 'sscr', '\uD835\uDD30': 'sfr', '\uD835\uDD4A': 'Sopf', '\uD835\uDD16': 'Sfr', '\uD835\uDCAE': 'Sscr', '\u24C8': 'oS', '\u015B': 'sacute', '\u015A': 'Sacute', '\u015D': 'scirc', '\u015C': 'Scirc', '\u0161': 'scaron', '\u0160': 'Scaron', '\u015F': 'scedil', '\u015E': 'Scedil', '\xDF': 'szlig', '\uD835\uDD31': 'tfr', '\uD835\uDCC9': 'tscr', '\uD835\uDD65': 'topf', '\uD835\uDCAF': 'Tscr', '\uD835\uDD17': 'Tfr', '\uD835\uDD4B': 'Topf', '\u0165': 'tcaron', '\u0164': 'Tcaron', '\u0163': 'tcedil', '\u0162': 'Tcedil', '\u2122': 'trade', '\u0167': 'tstrok', '\u0166': 'Tstrok', '\uD835\uDCCA': 'uscr', '\uD835\uDD66': 'uopf', '\uD835\uDD32': 'ufr', '\uD835\uDD4C': 'Uopf', '\uD835\uDD18': 'Ufr', '\uD835\uDCB0': 'Uscr', '\xFA': 'uacute', '\xDA': 'Uacute', '\xF9': 'ugrave', '\xD9': 'Ugrave', '\u016D': 'ubreve', '\u016C': 'Ubreve', '\xFB': 'ucirc', '\xDB': 'Ucirc', '\u016F': 'uring', '\u016E': 'Uring', '\xFC': 'uuml', '\xDC': 'Uuml', '\u0171': 'udblac', '\u0170': 'Udblac', '\u0169': 'utilde', '\u0168': 'Utilde', '\u0173': 'uogon', '\u0172': 'Uogon', '\u016B': 'umacr', '\u016A': 'Umacr', '\uD835\uDD33': 'vfr', '\uD835\uDD67': 'vopf', '\uD835\uDCCB': 'vscr', '\uD835\uDD19': 'Vfr', '\uD835\uDD4D': 'Vopf', '\uD835\uDCB1': 'Vscr', '\uD835\uDD68': 'wopf', '\uD835\uDCCC': 'wscr', '\uD835\uDD34': 'wfr', '\uD835\uDCB2': 'Wscr', '\uD835\uDD4E': 'Wopf', '\uD835\uDD1A': 'Wfr', '\u0175': 'wcirc', '\u0174': 'Wcirc', '\uD835\uDD35': 'xfr', '\uD835\uDCCD': 'xscr', '\uD835\uDD69': 'xopf', '\uD835\uDD4F': 'Xopf', '\uD835\uDD1B': 'Xfr', '\uD835\uDCB3': 'Xscr', '\uD835\uDD36': 'yfr', '\uD835\uDCCE': 'yscr', '\uD835\uDD6A': 'yopf', '\uD835\uDCB4': 'Yscr', '\uD835\uDD1C': 'Yfr', '\uD835\uDD50': 'Yopf', '\xFD': 'yacute', '\xDD': 'Yacute', '\u0177': 'ycirc', '\u0176': 'Ycirc', '\xFF': 'yuml', '\u0178': 'Yuml', '\uD835\uDCCF': 'zscr', '\uD835\uDD37': 'zfr', '\uD835\uDD6B': 'zopf', '\u2128': 'Zfr', '\u2124': 'Zopf', '\uD835\uDCB5': 'Zscr', '\u017A': 'zacute', '\u0179': 'Zacute', '\u017E': 'zcaron', '\u017D': 'Zcaron', '\u017C': 'zdot', '\u017B': 'Zdot', '\u01B5': 'imped', '\xFE': 'thorn', '\xDE': 'THORN', '\u0149': 'napos', '\u03B1': 'alpha', '\u0391': 'Alpha', '\u03B2': 'beta', '\u0392': 'Beta', '\u03B3': 'gamma', '\u0393': 'Gamma', '\u03B4': 'delta', '\u0394': 'Delta', '\u03B5': 'epsi', '\u03F5': 'epsiv', '\u0395': 'Epsilon', '\u03DD': 'gammad', '\u03DC': 'Gammad', '\u03B6': 'zeta', '\u0396': 'Zeta', '\u03B7': 'eta', '\u0397': 'Eta', '\u03B8': 'theta', '\u03D1': 'thetav', '\u0398': 'Theta', '\u03B9': 'iota', '\u0399': 'Iota', '\u03BA': 'kappa', '\u03F0': 'kappav', '\u039A': 'Kappa', '\u03BB': 'lambda', '\u039B': 'Lambda', '\u03BC': 'mu', '\xB5': 'micro', '\u039C': 'Mu', '\u03BD': 'nu', '\u039D': 'Nu', '\u03BE': 'xi', '\u039E': 'Xi', '\u03BF': 'omicron', '\u039F': 'Omicron', '\u03C0': 'pi', '\u03D6': 'piv', '\u03A0': 'Pi', '\u03C1': 'rho', '\u03F1': 'rhov', '\u03A1': 'Rho', '\u03C3': 'sigma', '\u03A3': 'Sigma', '\u03C2': 'sigmaf', '\u03C4': 'tau', '\u03A4': 'Tau', '\u03C5': 'upsi', '\u03A5': 'Upsilon', '\u03D2': 'Upsi', '\u03C6': 'phi', '\u03D5': 'phiv', '\u03A6': 'Phi', '\u03C7': 'chi', '\u03A7': 'Chi', '\u03C8': 'psi', '\u03A8': 'Psi', '\u03C9': 'omega', '\u03A9': 'ohm', '\u0430': 'acy', '\u0410': 'Acy', '\u0431': 'bcy', '\u0411': 'Bcy', '\u0432': 'vcy', '\u0412': 'Vcy', '\u0433': 'gcy', '\u0413': 'Gcy', '\u0453': 'gjcy', '\u0403': 'GJcy', '\u0434': 'dcy', '\u0414': 'Dcy', '\u0452': 'djcy', '\u0402': 'DJcy', '\u0435': 'iecy', '\u0415': 'IEcy', '\u0451': 'iocy', '\u0401': 'IOcy', '\u0454': 'jukcy', '\u0404': 'Jukcy', '\u0436': 'zhcy', '\u0416': 'ZHcy', '\u0437': 'zcy', '\u0417': 'Zcy', '\u0455': 'dscy', '\u0405': 'DScy', '\u0438': 'icy', '\u0418': 'Icy', '\u0456': 'iukcy', '\u0406': 'Iukcy', '\u0457': 'yicy', '\u0407': 'YIcy', '\u0439': 'jcy', '\u0419': 'Jcy', '\u0458': 'jsercy', '\u0408': 'Jsercy', '\u043A': 'kcy', '\u041A': 'Kcy', '\u045C': 'kjcy', '\u040C': 'KJcy', '\u043B': 'lcy', '\u041B': 'Lcy', '\u0459': 'ljcy', '\u0409': 'LJcy', '\u043C': 'mcy', '\u041C': 'Mcy', '\u043D': 'ncy', '\u041D': 'Ncy', '\u045A': 'njcy', '\u040A': 'NJcy', '\u043E': 'ocy', '\u041E': 'Ocy', '\u043F': 'pcy', '\u041F': 'Pcy', '\u0440': 'rcy', '\u0420': 'Rcy', '\u0441': 'scy', '\u0421': 'Scy', '\u0442': 'tcy', '\u0422': 'Tcy', '\u045B': 'tshcy', '\u040B': 'TSHcy', '\u0443': 'ucy', '\u0423': 'Ucy', '\u045E': 'ubrcy', '\u040E': 'Ubrcy', '\u0444': 'fcy', '\u0424': 'Fcy', '\u0445': 'khcy', '\u0425': 'KHcy', '\u0446': 'tscy', '\u0426': 'TScy', '\u0447': 'chcy', '\u0427': 'CHcy', '\u045F': 'dzcy', '\u040F': 'DZcy', '\u0448': 'shcy', '\u0428': 'SHcy', '\u0449': 'shchcy', '\u0429': 'SHCHcy', '\u044A': 'hardcy', '\u042A': 'HARDcy', '\u044B': 'ycy', '\u042B': 'Ycy', '\u044C': 'softcy', '\u042C': 'SOFTcy', '\u044D': 'ecy', '\u042D': 'Ecy', '\u044E': 'yucy', '\u042E': 'YUcy', '\u044F': 'yacy', '\u042F': 'YAcy', '\u2135': 'aleph', '\u2136': 'beth', '\u2137': 'gimel', '\u2138': 'daleth'}; + + var regexEscape = /["&'<>`]/g; + var escapeMap = { + '"': '"', + '&': '&', + '\'': ''', + '<': '<', + // See https://mathiasbynens.be/notes/ambiguous-ampersands: in HTML, the + // following is not strictly necessary unless it’s part of a tag or an + // unquoted attribute value. We’re only escaping it to support those + // situations, and for XML support. + '>': '>', + // In Internet Explorer ≤ 8, the backtick character can be used + // to break out of (un)quoted attribute values or HTML comments. + // See http://html5sec.org/#102, http://html5sec.org/#108, and + // http://html5sec.org/#133. + '`': '`' + }; + + var regexInvalidEntity = /&#(?:[xX][^a-fA-F0-9]|[^0-9xX])/; + var regexInvalidRawCodePoint = /[\0-\x08\x0B\x0E-\x1F\x7F-\x9F\uFDD0-\uFDEF\uFFFE\uFFFF]|[\uD83F\uD87F\uD8BF\uD8FF\uD93F\uD97F\uD9BF\uD9FF\uDA3F\uDA7F\uDABF\uDAFF\uDB3F\uDB7F\uDBBF\uDBFF][\uDFFE\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/; + var regexDecode = /&(CounterClockwiseContourIntegral|DoubleLongLeftRightArrow|ClockwiseContourIntegral|NotNestedGreaterGreater|NotSquareSupersetEqual|DiacriticalDoubleAcute|NotRightTriangleEqual|NotSucceedsSlantEqual|NotPrecedesSlantEqual|CloseCurlyDoubleQuote|NegativeVeryThinSpace|DoubleContourIntegral|FilledVerySmallSquare|CapitalDifferentialD|OpenCurlyDoubleQuote|EmptyVerySmallSquare|NestedGreaterGreater|DoubleLongRightArrow|NotLeftTriangleEqual|NotGreaterSlantEqual|ReverseUpEquilibrium|DoubleLeftRightArrow|NotSquareSubsetEqual|NotDoubleVerticalBar|RightArrowLeftArrow|NotGreaterFullEqual|NotRightTriangleBar|SquareSupersetEqual|DownLeftRightVector|DoubleLongLeftArrow|leftrightsquigarrow|LeftArrowRightArrow|NegativeMediumSpace|blacktriangleright|RightDownVectorBar|PrecedesSlantEqual|RightDoubleBracket|SucceedsSlantEqual|NotLeftTriangleBar|RightTriangleEqual|SquareIntersection|RightDownTeeVector|ReverseEquilibrium|NegativeThickSpace|longleftrightarrow|Longleftrightarrow|LongLeftRightArrow|DownRightTeeVector|DownRightVectorBar|GreaterSlantEqual|SquareSubsetEqual|LeftDownVectorBar|LeftDoubleBracket|VerticalSeparator|rightleftharpoons|NotGreaterGreater|NotSquareSuperset|blacktriangleleft|blacktriangledown|NegativeThinSpace|LeftDownTeeVector|NotLessSlantEqual|leftrightharpoons|DoubleUpDownArrow|DoubleVerticalBar|LeftTriangleEqual|FilledSmallSquare|twoheadrightarrow|NotNestedLessLess|DownLeftTeeVector|DownLeftVectorBar|RightAngleBracket|NotTildeFullEqual|NotReverseElement|RightUpDownVector|DiacriticalTilde|NotSucceedsTilde|circlearrowright|NotPrecedesEqual|rightharpoondown|DoubleRightArrow|NotSucceedsEqual|NonBreakingSpace|NotRightTriangle|LessEqualGreater|RightUpTeeVector|LeftAngleBracket|GreaterFullEqual|DownArrowUpArrow|RightUpVectorBar|twoheadleftarrow|GreaterEqualLess|downharpoonright|RightTriangleBar|ntrianglerighteq|NotSupersetEqual|LeftUpDownVector|DiacriticalAcute|rightrightarrows|vartriangleright|UpArrowDownArrow|DiacriticalGrave|UnderParenthesis|EmptySmallSquare|LeftUpVectorBar|leftrightarrows|DownRightVector|downharpoonleft|trianglerighteq|ShortRightArrow|OverParenthesis|DoubleLeftArrow|DoubleDownArrow|NotSquareSubset|bigtriangledown|ntrianglelefteq|UpperRightArrow|curvearrowright|vartriangleleft|NotLeftTriangle|nleftrightarrow|LowerRightArrow|NotHumpDownHump|NotGreaterTilde|rightthreetimes|LeftUpTeeVector|NotGreaterEqual|straightepsilon|LeftTriangleBar|rightsquigarrow|ContourIntegral|rightleftarrows|CloseCurlyQuote|RightDownVector|LeftRightVector|nLeftrightarrow|leftharpoondown|circlearrowleft|SquareSuperset|OpenCurlyQuote|hookrightarrow|HorizontalLine|DiacriticalDot|NotLessGreater|ntriangleright|DoubleRightTee|InvisibleComma|InvisibleTimes|LowerLeftArrow|DownLeftVector|NotSubsetEqual|curvearrowleft|trianglelefteq|NotVerticalBar|TildeFullEqual|downdownarrows|NotGreaterLess|RightTeeVector|ZeroWidthSpace|looparrowright|LongRightArrow|doublebarwedge|ShortLeftArrow|ShortDownArrow|RightVectorBar|GreaterGreater|ReverseElement|rightharpoonup|LessSlantEqual|leftthreetimes|upharpoonright|rightarrowtail|LeftDownVector|Longrightarrow|NestedLessLess|UpperLeftArrow|nshortparallel|leftleftarrows|leftrightarrow|Leftrightarrow|LeftRightArrow|longrightarrow|upharpoonleft|RightArrowBar|ApplyFunction|LeftTeeVector|leftarrowtail|NotEqualTilde|varsubsetneqq|varsupsetneqq|RightTeeArrow|SucceedsEqual|SucceedsTilde|LeftVectorBar|SupersetEqual|hookleftarrow|DifferentialD|VerticalTilde|VeryThinSpace|blacktriangle|bigtriangleup|LessFullEqual|divideontimes|leftharpoonup|UpEquilibrium|ntriangleleft|RightTriangle|measuredangle|shortparallel|longleftarrow|Longleftarrow|LongLeftArrow|DoubleLeftTee|Poincareplane|PrecedesEqual|triangleright|DoubleUpArrow|RightUpVector|fallingdotseq|looparrowleft|PrecedesTilde|NotTildeEqual|NotTildeTilde|smallsetminus|Proportional|triangleleft|triangledown|UnderBracket|NotHumpEqual|exponentiale|ExponentialE|NotLessTilde|HilbertSpace|RightCeiling|blacklozenge|varsupsetneq|HumpDownHump|GreaterEqual|VerticalLine|LeftTeeArrow|NotLessEqual|DownTeeArrow|LeftTriangle|varsubsetneq|Intersection|NotCongruent|DownArrowBar|LeftUpVector|LeftArrowBar|risingdotseq|GreaterTilde|RoundImplies|SquareSubset|ShortUpArrow|NotSuperset|quaternions|precnapprox|backepsilon|preccurlyeq|OverBracket|blacksquare|MediumSpace|VerticalBar|circledcirc|circleddash|CircleMinus|CircleTimes|LessGreater|curlyeqprec|curlyeqsucc|diamondsuit|UpDownArrow|Updownarrow|RuleDelayed|Rrightarrow|updownarrow|RightVector|nRightarrow|nrightarrow|eqslantless|LeftCeiling|Equilibrium|SmallCircle|expectation|NotSucceeds|thickapprox|GreaterLess|SquareUnion|NotPrecedes|NotLessLess|straightphi|succnapprox|succcurlyeq|SubsetEqual|sqsupseteq|Proportion|Laplacetrf|ImaginaryI|supsetneqq|NotGreater|gtreqqless|NotElement|ThickSpace|TildeEqual|TildeTilde|Fouriertrf|rmoustache|EqualTilde|eqslantgtr|UnderBrace|LeftVector|UpArrowBar|nLeftarrow|nsubseteqq|subsetneqq|nsupseteqq|nleftarrow|succapprox|lessapprox|UpTeeArrow|upuparrows|curlywedge|lesseqqgtr|varepsilon|varnothing|RightFloor|complement|CirclePlus|sqsubseteq|Lleftarrow|circledast|RightArrow|Rightarrow|rightarrow|lmoustache|Bernoullis|precapprox|mapstoleft|mapstodown|longmapsto|dotsquare|downarrow|DoubleDot|nsubseteq|supsetneq|leftarrow|nsupseteq|subsetneq|ThinSpace|ngeqslant|subseteqq|HumpEqual|NotSubset|triangleq|NotCupCap|lesseqgtr|heartsuit|TripleDot|Leftarrow|Coproduct|Congruent|varpropto|complexes|gvertneqq|LeftArrow|LessTilde|supseteqq|MinusPlus|CircleDot|nleqslant|NotExists|gtreqless|nparallel|UnionPlus|LeftFloor|checkmark|CenterDot|centerdot|Mellintrf|gtrapprox|bigotimes|OverBrace|spadesuit|therefore|pitchfork|rationals|PlusMinus|Backslash|Therefore|DownBreve|backsimeq|backprime|DownArrow|nshortmid|Downarrow|lvertneqq|eqvparsl|imagline|imagpart|infintie|integers|Integral|intercal|LessLess|Uarrocir|intlarhk|sqsupset|angmsdaf|sqsubset|llcorner|vartheta|cupbrcap|lnapprox|Superset|SuchThat|succnsim|succneqq|angmsdag|biguplus|curlyvee|trpezium|Succeeds|NotTilde|bigwedge|angmsdah|angrtvbd|triminus|cwconint|fpartint|lrcorner|smeparsl|subseteq|urcorner|lurdshar|laemptyv|DDotrahd|approxeq|ldrushar|awconint|mapstoup|backcong|shortmid|triangle|geqslant|gesdotol|timesbar|circledR|circledS|setminus|multimap|naturals|scpolint|ncongdot|RightTee|boxminus|gnapprox|boxtimes|andslope|thicksim|angmsdaa|varsigma|cirfnint|rtriltri|angmsdab|rppolint|angmsdac|barwedge|drbkarow|clubsuit|thetasym|bsolhsub|capbrcup|dzigrarr|doteqdot|DotEqual|dotminus|UnderBar|NotEqual|realpart|otimesas|ulcorner|hksearow|hkswarow|parallel|PartialD|elinters|emptyset|plusacir|bbrktbrk|angmsdad|pointint|bigoplus|angmsdae|Precedes|bigsqcup|varkappa|notindot|supseteq|precneqq|precnsim|profalar|profline|profsurf|leqslant|lesdotor|raemptyv|subplus|notnivb|notnivc|subrarr|zigrarr|vzigzag|submult|subedot|Element|between|cirscir|larrbfs|larrsim|lotimes|lbrksld|lbrkslu|lozenge|ldrdhar|dbkarow|bigcirc|epsilon|simrarr|simplus|ltquest|Epsilon|luruhar|gtquest|maltese|npolint|eqcolon|npreceq|bigodot|ddagger|gtrless|bnequiv|harrcir|ddotseq|equivDD|backsim|demptyv|nsqsube|nsqsupe|Upsilon|nsubset|upsilon|minusdu|nsucceq|swarrow|nsupset|coloneq|searrow|boxplus|napprox|natural|asympeq|alefsym|congdot|nearrow|bigstar|diamond|supplus|tritime|LeftTee|nvinfin|triplus|NewLine|nvltrie|nvrtrie|nwarrow|nexists|Diamond|ruluhar|Implies|supmult|angzarr|suplarr|suphsub|questeq|because|digamma|Because|olcross|bemptyv|omicron|Omicron|rotimes|NoBreak|intprod|angrtvb|orderof|uwangle|suphsol|lesdoto|orslope|DownTee|realine|cudarrl|rdldhar|OverBar|supedot|lessdot|supdsub|topfork|succsim|rbrkslu|rbrksld|pertenk|cudarrr|isindot|planckh|lessgtr|pluscir|gesdoto|plussim|plustwo|lesssim|cularrp|rarrsim|Cayleys|notinva|notinvb|notinvc|UpArrow|Uparrow|uparrow|NotLess|dwangle|precsim|Product|curarrm|Cconint|dotplus|rarrbfs|ccupssm|Cedilla|cemptyv|notniva|quatint|frac35|frac38|frac45|frac56|frac58|frac78|tridot|xoplus|gacute|gammad|Gammad|lfisht|lfloor|bigcup|sqsupe|gbreve|Gbreve|lharul|sqsube|sqcups|Gcedil|apacir|llhard|lmidot|Lmidot|lmoust|andand|sqcaps|approx|Abreve|spades|circeq|tprime|divide|topcir|Assign|topbot|gesdot|divonx|xuplus|timesd|gesles|atilde|solbar|SOFTcy|loplus|timesb|lowast|lowbar|dlcorn|dlcrop|softcy|dollar|lparlt|thksim|lrhard|Atilde|lsaquo|smashp|bigvee|thinsp|wreath|bkarow|lsquor|lstrok|Lstrok|lthree|ltimes|ltlarr|DotDot|simdot|ltrPar|weierp|xsqcup|angmsd|sigmav|sigmaf|zeetrf|Zcaron|zcaron|mapsto|vsupne|thetav|cirmid|marker|mcomma|Zacute|vsubnE|there4|gtlPar|vsubne|bottom|gtrarr|SHCHcy|shchcy|midast|midcir|middot|minusb|minusd|gtrdot|bowtie|sfrown|mnplus|models|colone|seswar|Colone|mstpos|searhk|gtrsim|nacute|Nacute|boxbox|telrec|hairsp|Tcedil|nbumpe|scnsim|ncaron|Ncaron|ncedil|Ncedil|hamilt|Scedil|nearhk|hardcy|HARDcy|tcedil|Tcaron|commat|nequiv|nesear|tcaron|target|hearts|nexist|varrho|scedil|Scaron|scaron|hellip|Sacute|sacute|hercon|swnwar|compfn|rtimes|rthree|rsquor|rsaquo|zacute|wedgeq|homtht|barvee|barwed|Barwed|rpargt|horbar|conint|swarhk|roplus|nltrie|hslash|hstrok|Hstrok|rmoust|Conint|bprime|hybull|hyphen|iacute|Iacute|supsup|supsub|supsim|varphi|coprod|brvbar|agrave|Supset|supset|igrave|Igrave|notinE|Agrave|iiiint|iinfin|copysr|wedbar|Verbar|vangrt|becaus|incare|verbar|inodot|bullet|drcorn|intcal|drcrop|cularr|vellip|Utilde|bumpeq|cupcap|dstrok|Dstrok|CupCap|cupcup|cupdot|eacute|Eacute|supdot|iquest|easter|ecaron|Ecaron|ecolon|isinsv|utilde|itilde|Itilde|curarr|succeq|Bumpeq|cacute|ulcrop|nparsl|Cacute|nprcue|egrave|Egrave|nrarrc|nrarrw|subsup|subsub|nrtrie|jsercy|nsccue|Jsercy|kappav|kcedil|Kcedil|subsim|ulcorn|nsimeq|egsdot|veebar|kgreen|capand|elsdot|Subset|subset|curren|aacute|lacute|Lacute|emptyv|ntilde|Ntilde|lagran|lambda|Lambda|capcap|Ugrave|langle|subdot|emsp13|numero|emsp14|nvdash|nvDash|nVdash|nVDash|ugrave|ufisht|nvHarr|larrfs|nvlArr|larrhk|larrlp|larrpl|nvrArr|Udblac|nwarhk|larrtl|nwnear|oacute|Oacute|latail|lAtail|sstarf|lbrace|odblac|Odblac|lbrack|udblac|odsold|eparsl|lcaron|Lcaron|ograve|Ograve|lcedil|Lcedil|Aacute|ssmile|ssetmn|squarf|ldquor|capcup|ominus|cylcty|rharul|eqcirc|dagger|rfloor|rfisht|Dagger|daleth|equals|origof|capdot|equest|dcaron|Dcaron|rdquor|oslash|Oslash|otilde|Otilde|otimes|Otimes|urcrop|Ubreve|ubreve|Yacute|Uacute|uacute|Rcedil|rcedil|urcorn|parsim|Rcaron|Vdashl|rcaron|Tstrok|percnt|period|permil|Exists|yacute|rbrack|rbrace|phmmat|ccaron|Ccaron|planck|ccedil|plankv|tstrok|female|plusdo|plusdu|ffilig|plusmn|ffllig|Ccedil|rAtail|dfisht|bernou|ratail|Rarrtl|rarrtl|angsph|rarrpl|rarrlp|rarrhk|xwedge|xotime|forall|ForAll|Vvdash|vsupnE|preceq|bigcap|frac12|frac13|frac14|primes|rarrfs|prnsim|frac15|Square|frac16|square|lesdot|frac18|frac23|propto|prurel|rarrap|rangle|puncsp|frac25|Racute|qprime|racute|lesges|frac34|abreve|AElig|eqsim|utdot|setmn|urtri|Equal|Uring|seArr|uring|searr|dashv|Dashv|mumap|nabla|iogon|Iogon|sdote|sdotb|scsim|napid|napos|equiv|natur|Acirc|dblac|erarr|nbump|iprod|erDot|ucirc|awint|esdot|angrt|ncong|isinE|scnap|Scirc|scirc|ndash|isins|Ubrcy|nearr|neArr|isinv|nedot|ubrcy|acute|Ycirc|iukcy|Iukcy|xutri|nesim|caret|jcirc|Jcirc|caron|twixt|ddarr|sccue|exist|jmath|sbquo|ngeqq|angst|ccaps|lceil|ngsim|UpTee|delta|Delta|rtrif|nharr|nhArr|nhpar|rtrie|jukcy|Jukcy|kappa|rsquo|Kappa|nlarr|nlArr|TSHcy|rrarr|aogon|Aogon|fflig|xrarr|tshcy|ccirc|nleqq|filig|upsih|nless|dharl|nlsim|fjlig|ropar|nltri|dharr|robrk|roarr|fllig|fltns|roang|rnmid|subnE|subne|lAarr|trisb|Ccirc|acirc|ccups|blank|VDash|forkv|Vdash|langd|cedil|blk12|blk14|laquo|strns|diams|notin|vDash|larrb|blk34|block|disin|uplus|vdash|vBarv|aelig|starf|Wedge|check|xrArr|lates|lbarr|lBarr|notni|lbbrk|bcong|frasl|lbrke|frown|vrtri|vprop|vnsup|gamma|Gamma|wedge|xodot|bdquo|srarr|doteq|ldquo|boxdl|boxdL|gcirc|Gcirc|boxDl|boxDL|boxdr|boxdR|boxDr|TRADE|trade|rlhar|boxDR|vnsub|npart|vltri|rlarr|boxhd|boxhD|nprec|gescc|nrarr|nrArr|boxHd|boxHD|boxhu|boxhU|nrtri|boxHu|clubs|boxHU|times|colon|Colon|gimel|xlArr|Tilde|nsime|tilde|nsmid|nspar|THORN|thorn|xlarr|nsube|nsubE|thkap|xhArr|comma|nsucc|boxul|boxuL|nsupe|nsupE|gneqq|gnsim|boxUl|boxUL|grave|boxur|boxuR|boxUr|boxUR|lescc|angle|bepsi|boxvh|varpi|boxvH|numsp|Theta|gsime|gsiml|theta|boxVh|boxVH|boxvl|gtcir|gtdot|boxvL|boxVl|boxVL|crarr|cross|Cross|nvsim|boxvr|nwarr|nwArr|sqsup|dtdot|Uogon|lhard|lharu|dtrif|ocirc|Ocirc|lhblk|duarr|odash|sqsub|Hacek|sqcup|llarr|duhar|oelig|OElig|ofcir|boxvR|uogon|lltri|boxVr|csube|uuarr|ohbar|csupe|ctdot|olarr|olcir|harrw|oline|sqcap|omacr|Omacr|omega|Omega|boxVR|aleph|lneqq|lnsim|loang|loarr|rharu|lobrk|hcirc|operp|oplus|rhard|Hcirc|orarr|Union|order|ecirc|Ecirc|cuepr|szlig|cuesc|breve|reals|eDDot|Breve|hoarr|lopar|utrif|rdquo|Umacr|umacr|efDot|swArr|ultri|alpha|rceil|ovbar|swarr|Wcirc|wcirc|smtes|smile|bsemi|lrarr|aring|parsl|lrhar|bsime|uhblk|lrtri|cupor|Aring|uharr|uharl|slarr|rbrke|bsolb|lsime|rbbrk|RBarr|lsimg|phone|rBarr|rbarr|icirc|lsquo|Icirc|emacr|Emacr|ratio|simne|plusb|simlE|simgE|simeq|pluse|ltcir|ltdot|empty|xharr|xdtri|iexcl|Alpha|ltrie|rarrw|pound|ltrif|xcirc|bumpe|prcue|bumpE|asymp|amacr|cuvee|Sigma|sigma|iiint|udhar|iiota|ijlig|IJlig|supnE|imacr|Imacr|prime|Prime|image|prnap|eogon|Eogon|rarrc|mdash|mDDot|cuwed|imath|supne|imped|Amacr|udarr|prsim|micro|rarrb|cwint|raquo|infin|eplus|range|rangd|Ucirc|radic|minus|amalg|veeeq|rAarr|epsiv|ycirc|quest|sharp|quot|zwnj|Qscr|race|qscr|Qopf|qopf|qint|rang|Rang|Zscr|zscr|Zopf|zopf|rarr|rArr|Rarr|Pscr|pscr|prop|prod|prnE|prec|ZHcy|zhcy|prap|Zeta|zeta|Popf|popf|Zdot|plus|zdot|Yuml|yuml|phiv|YUcy|yucy|Yscr|yscr|perp|Yopf|yopf|part|para|YIcy|Ouml|rcub|yicy|YAcy|rdca|ouml|osol|Oscr|rdsh|yacy|real|oscr|xvee|andd|rect|andv|Xscr|oror|ordm|ordf|xscr|ange|aopf|Aopf|rHar|Xopf|opar|Oopf|xopf|xnis|rhov|oopf|omid|xmap|oint|apid|apos|ogon|ascr|Ascr|odot|odiv|xcup|xcap|ocir|oast|nvlt|nvle|nvgt|nvge|nvap|Wscr|wscr|auml|ntlg|ntgl|nsup|nsub|nsim|Nscr|nscr|nsce|Wopf|ring|npre|wopf|npar|Auml|Barv|bbrk|Nopf|nopf|nmid|nLtv|beta|ropf|Ropf|Beta|beth|nles|rpar|nleq|bnot|bNot|nldr|NJcy|rscr|Rscr|Vscr|vscr|rsqb|njcy|bopf|nisd|Bopf|rtri|Vopf|nGtv|ngtr|vopf|boxh|boxH|boxv|nges|ngeq|boxV|bscr|scap|Bscr|bsim|Vert|vert|bsol|bull|bump|caps|cdot|ncup|scnE|ncap|nbsp|napE|Cdot|cent|sdot|Vbar|nang|vBar|chcy|Mscr|mscr|sect|semi|CHcy|Mopf|mopf|sext|circ|cire|mldr|mlcp|cirE|comp|shcy|SHcy|vArr|varr|cong|copf|Copf|copy|COPY|malt|male|macr|lvnE|cscr|ltri|sime|ltcc|simg|Cscr|siml|csub|Uuml|lsqb|lsim|uuml|csup|Lscr|lscr|utri|smid|lpar|cups|smte|lozf|darr|Lopf|Uscr|solb|lopf|sopf|Sopf|lneq|uscr|spar|dArr|lnap|Darr|dash|Sqrt|LJcy|ljcy|lHar|dHar|Upsi|upsi|diam|lesg|djcy|DJcy|leqq|dopf|Dopf|dscr|Dscr|dscy|ldsh|ldca|squf|DScy|sscr|Sscr|dsol|lcub|late|star|Star|Uopf|Larr|lArr|larr|uopf|dtri|dzcy|sube|subE|Lang|lang|Kscr|kscr|Kopf|kopf|KJcy|kjcy|KHcy|khcy|DZcy|ecir|edot|eDot|Jscr|jscr|succ|Jopf|jopf|Edot|uHar|emsp|ensp|Iuml|iuml|eopf|isin|Iscr|iscr|Eopf|epar|sung|epsi|escr|sup1|sup2|sup3|Iota|iota|supe|supE|Iopf|iopf|IOcy|iocy|Escr|esim|Esim|imof|Uarr|QUOT|uArr|uarr|euml|IEcy|iecy|Idot|Euml|euro|excl|Hscr|hscr|Hopf|hopf|TScy|tscy|Tscr|hbar|tscr|flat|tbrk|fnof|hArr|harr|half|fopf|Fopf|tdot|gvnE|fork|trie|gtcc|fscr|Fscr|gdot|gsim|Gscr|gscr|Gopf|gopf|gneq|Gdot|tosa|gnap|Topf|topf|geqq|toea|GJcy|gjcy|tint|gesl|mid|Sfr|ggg|top|ges|gla|glE|glj|geq|gne|gEl|gel|gnE|Gcy|gcy|gap|Tfr|tfr|Tcy|tcy|Hat|Tau|Ffr|tau|Tab|hfr|Hfr|ffr|Fcy|fcy|icy|Icy|iff|ETH|eth|ifr|Ifr|Eta|eta|int|Int|Sup|sup|ucy|Ucy|Sum|sum|jcy|ENG|ufr|Ufr|eng|Jcy|jfr|els|ell|egs|Efr|efr|Jfr|uml|kcy|Kcy|Ecy|ecy|kfr|Kfr|lap|Sub|sub|lat|lcy|Lcy|leg|Dot|dot|lEg|leq|les|squ|div|die|lfr|Lfr|lgE|Dfr|dfr|Del|deg|Dcy|dcy|lne|lnE|sol|loz|smt|Cup|lrm|cup|lsh|Lsh|sim|shy|map|Map|mcy|Mcy|mfr|Mfr|mho|gfr|Gfr|sfr|cir|Chi|chi|nap|Cfr|vcy|Vcy|cfr|Scy|scy|ncy|Ncy|vee|Vee|Cap|cap|nfr|scE|sce|Nfr|nge|ngE|nGg|vfr|Vfr|ngt|bot|nGt|nis|niv|Rsh|rsh|nle|nlE|bne|Bfr|bfr|nLl|nlt|nLt|Bcy|bcy|not|Not|rlm|wfr|Wfr|npr|nsc|num|ocy|ast|Ocy|ofr|xfr|Xfr|Ofr|ogt|ohm|apE|olt|Rho|ape|rho|Rfr|rfr|ord|REG|ang|reg|orv|And|and|AMP|Rcy|amp|Afr|ycy|Ycy|yen|yfr|Yfr|rcy|par|pcy|Pcy|pfr|Pfr|phi|Phi|afr|Acy|acy|zcy|Zcy|piv|acE|acd|zfr|Zfr|pre|prE|psi|Psi|qfr|Qfr|zwj|Or|ge|Gg|gt|gg|el|oS|lt|Lt|LT|Re|lg|gl|eg|ne|Im|it|le|DD|wp|wr|nu|Nu|dd|lE|Sc|sc|pi|Pi|ee|af|ll|Ll|rx|gE|xi|pm|Xi|ic|pr|Pr|in|ni|mp|mu|ac|Mu|or|ap|Gt|GT|ii);|&(Aacute|Agrave|Atilde|Ccedil|Eacute|Egrave|Iacute|Igrave|Ntilde|Oacute|Ograve|Oslash|Otilde|Uacute|Ugrave|Yacute|aacute|agrave|atilde|brvbar|ccedil|curren|divide|eacute|egrave|frac12|frac14|frac34|iacute|igrave|iquest|middot|ntilde|oacute|ograve|oslash|otilde|plusmn|uacute|ugrave|yacute|AElig|Acirc|Aring|Ecirc|Icirc|Ocirc|THORN|Ucirc|acirc|acute|aelig|aring|cedil|ecirc|icirc|iexcl|laquo|micro|ocirc|pound|raquo|szlig|thorn|times|ucirc|Auml|COPY|Euml|Iuml|Ouml|QUOT|Uuml|auml|cent|copy|euml|iuml|macr|nbsp|ordf|ordm|ouml|para|quot|sect|sup1|sup2|sup3|uuml|yuml|AMP|ETH|REG|amp|deg|eth|not|reg|shy|uml|yen|GT|LT|gt|lt)(?!;)([=a-zA-Z0-9]?)|&#([0-9]+)(;?)|&#[xX]([a-fA-F0-9]+)(;?)|&([0-9a-zA-Z]+)/g; + var decodeMap = {'aacute': '\xE1', 'Aacute': '\xC1', 'abreve': '\u0103', 'Abreve': '\u0102', 'ac': '\u223E', 'acd': '\u223F', 'acE': '\u223E\u0333', 'acirc': '\xE2', 'Acirc': '\xC2', 'acute': '\xB4', 'acy': '\u0430', 'Acy': '\u0410', 'aelig': '\xE6', 'AElig': '\xC6', 'af': '\u2061', 'afr': '\uD835\uDD1E', 'Afr': '\uD835\uDD04', 'agrave': '\xE0', 'Agrave': '\xC0', 'alefsym': '\u2135', 'aleph': '\u2135', 'alpha': '\u03B1', 'Alpha': '\u0391', 'amacr': '\u0101', 'Amacr': '\u0100', 'amalg': '\u2A3F', 'amp': '&', 'AMP': '&', 'and': '\u2227', 'And': '\u2A53', 'andand': '\u2A55', 'andd': '\u2A5C', 'andslope': '\u2A58', 'andv': '\u2A5A', 'ang': '\u2220', 'ange': '\u29A4', 'angle': '\u2220', 'angmsd': '\u2221', 'angmsdaa': '\u29A8', 'angmsdab': '\u29A9', 'angmsdac': '\u29AA', 'angmsdad': '\u29AB', 'angmsdae': '\u29AC', 'angmsdaf': '\u29AD', 'angmsdag': '\u29AE', 'angmsdah': '\u29AF', 'angrt': '\u221F', 'angrtvb': '\u22BE', 'angrtvbd': '\u299D', 'angsph': '\u2222', 'angst': '\xC5', 'angzarr': '\u237C', 'aogon': '\u0105', 'Aogon': '\u0104', 'aopf': '\uD835\uDD52', 'Aopf': '\uD835\uDD38', 'ap': '\u2248', 'apacir': '\u2A6F', 'ape': '\u224A', 'apE': '\u2A70', 'apid': '\u224B', 'apos': '\'', 'ApplyFunction': '\u2061', 'approx': '\u2248', 'approxeq': '\u224A', 'aring': '\xE5', 'Aring': '\xC5', 'ascr': '\uD835\uDCB6', 'Ascr': '\uD835\uDC9C', 'Assign': '\u2254', 'ast': '*', 'asymp': '\u2248', 'asympeq': '\u224D', 'atilde': '\xE3', 'Atilde': '\xC3', 'auml': '\xE4', 'Auml': '\xC4', 'awconint': '\u2233', 'awint': '\u2A11', 'backcong': '\u224C', 'backepsilon': '\u03F6', 'backprime': '\u2035', 'backsim': '\u223D', 'backsimeq': '\u22CD', 'Backslash': '\u2216', 'Barv': '\u2AE7', 'barvee': '\u22BD', 'barwed': '\u2305', 'Barwed': '\u2306', 'barwedge': '\u2305', 'bbrk': '\u23B5', 'bbrktbrk': '\u23B6', 'bcong': '\u224C', 'bcy': '\u0431', 'Bcy': '\u0411', 'bdquo': '\u201E', 'becaus': '\u2235', 'because': '\u2235', 'Because': '\u2235', 'bemptyv': '\u29B0', 'bepsi': '\u03F6', 'bernou': '\u212C', 'Bernoullis': '\u212C', 'beta': '\u03B2', 'Beta': '\u0392', 'beth': '\u2136', 'between': '\u226C', 'bfr': '\uD835\uDD1F', 'Bfr': '\uD835\uDD05', 'bigcap': '\u22C2', 'bigcirc': '\u25EF', 'bigcup': '\u22C3', 'bigodot': '\u2A00', 'bigoplus': '\u2A01', 'bigotimes': '\u2A02', 'bigsqcup': '\u2A06', 'bigstar': '\u2605', 'bigtriangledown': '\u25BD', 'bigtriangleup': '\u25B3', 'biguplus': '\u2A04', 'bigvee': '\u22C1', 'bigwedge': '\u22C0', 'bkarow': '\u290D', 'blacklozenge': '\u29EB', 'blacksquare': '\u25AA', 'blacktriangle': '\u25B4', 'blacktriangledown': '\u25BE', 'blacktriangleleft': '\u25C2', 'blacktriangleright': '\u25B8', 'blank': '\u2423', 'blk12': '\u2592', 'blk14': '\u2591', 'blk34': '\u2593', 'block': '\u2588', 'bne': '=\u20E5', 'bnequiv': '\u2261\u20E5', 'bnot': '\u2310', 'bNot': '\u2AED', 'bopf': '\uD835\uDD53', 'Bopf': '\uD835\uDD39', 'bot': '\u22A5', 'bottom': '\u22A5', 'bowtie': '\u22C8', 'boxbox': '\u29C9', 'boxdl': '\u2510', 'boxdL': '\u2555', 'boxDl': '\u2556', 'boxDL': '\u2557', 'boxdr': '\u250C', 'boxdR': '\u2552', 'boxDr': '\u2553', 'boxDR': '\u2554', 'boxh': '\u2500', 'boxH': '\u2550', 'boxhd': '\u252C', 'boxhD': '\u2565', 'boxHd': '\u2564', 'boxHD': '\u2566', 'boxhu': '\u2534', 'boxhU': '\u2568', 'boxHu': '\u2567', 'boxHU': '\u2569', 'boxminus': '\u229F', 'boxplus': '\u229E', 'boxtimes': '\u22A0', 'boxul': '\u2518', 'boxuL': '\u255B', 'boxUl': '\u255C', 'boxUL': '\u255D', 'boxur': '\u2514', 'boxuR': '\u2558', 'boxUr': '\u2559', 'boxUR': '\u255A', 'boxv': '\u2502', 'boxV': '\u2551', 'boxvh': '\u253C', 'boxvH': '\u256A', 'boxVh': '\u256B', 'boxVH': '\u256C', 'boxvl': '\u2524', 'boxvL': '\u2561', 'boxVl': '\u2562', 'boxVL': '\u2563', 'boxvr': '\u251C', 'boxvR': '\u255E', 'boxVr': '\u255F', 'boxVR': '\u2560', 'bprime': '\u2035', 'breve': '\u02D8', 'Breve': '\u02D8', 'brvbar': '\xA6', 'bscr': '\uD835\uDCB7', 'Bscr': '\u212C', 'bsemi': '\u204F', 'bsim': '\u223D', 'bsime': '\u22CD', 'bsol': '\\', 'bsolb': '\u29C5', 'bsolhsub': '\u27C8', 'bull': '\u2022', 'bullet': '\u2022', 'bump': '\u224E', 'bumpe': '\u224F', 'bumpE': '\u2AAE', 'bumpeq': '\u224F', 'Bumpeq': '\u224E', 'cacute': '\u0107', 'Cacute': '\u0106', 'cap': '\u2229', 'Cap': '\u22D2', 'capand': '\u2A44', 'capbrcup': '\u2A49', 'capcap': '\u2A4B', 'capcup': '\u2A47', 'capdot': '\u2A40', 'CapitalDifferentialD': '\u2145', 'caps': '\u2229\uFE00', 'caret': '\u2041', 'caron': '\u02C7', 'Cayleys': '\u212D', 'ccaps': '\u2A4D', 'ccaron': '\u010D', 'Ccaron': '\u010C', 'ccedil': '\xE7', 'Ccedil': '\xC7', 'ccirc': '\u0109', 'Ccirc': '\u0108', 'Cconint': '\u2230', 'ccups': '\u2A4C', 'ccupssm': '\u2A50', 'cdot': '\u010B', 'Cdot': '\u010A', 'cedil': '\xB8', 'Cedilla': '\xB8', 'cemptyv': '\u29B2', 'cent': '\xA2', 'centerdot': '\xB7', 'CenterDot': '\xB7', 'cfr': '\uD835\uDD20', 'Cfr': '\u212D', 'chcy': '\u0447', 'CHcy': '\u0427', 'check': '\u2713', 'checkmark': '\u2713', 'chi': '\u03C7', 'Chi': '\u03A7', 'cir': '\u25CB', 'circ': '\u02C6', 'circeq': '\u2257', 'circlearrowleft': '\u21BA', 'circlearrowright': '\u21BB', 'circledast': '\u229B', 'circledcirc': '\u229A', 'circleddash': '\u229D', 'CircleDot': '\u2299', 'circledR': '\xAE', 'circledS': '\u24C8', 'CircleMinus': '\u2296', 'CirclePlus': '\u2295', 'CircleTimes': '\u2297', 'cire': '\u2257', 'cirE': '\u29C3', 'cirfnint': '\u2A10', 'cirmid': '\u2AEF', 'cirscir': '\u29C2', 'ClockwiseContourIntegral': '\u2232', 'CloseCurlyDoubleQuote': '\u201D', 'CloseCurlyQuote': '\u2019', 'clubs': '\u2663', 'clubsuit': '\u2663', 'colon': ':', 'Colon': '\u2237', 'colone': '\u2254', 'Colone': '\u2A74', 'coloneq': '\u2254', 'comma': ',', 'commat': '@', 'comp': '\u2201', 'compfn': '\u2218', 'complement': '\u2201', 'complexes': '\u2102', 'cong': '\u2245', 'congdot': '\u2A6D', 'Congruent': '\u2261', 'conint': '\u222E', 'Conint': '\u222F', 'ContourIntegral': '\u222E', 'copf': '\uD835\uDD54', 'Copf': '\u2102', 'coprod': '\u2210', 'Coproduct': '\u2210', 'copy': '\xA9', 'COPY': '\xA9', 'copysr': '\u2117', 'CounterClockwiseContourIntegral': '\u2233', 'crarr': '\u21B5', 'cross': '\u2717', 'Cross': '\u2A2F', 'cscr': '\uD835\uDCB8', 'Cscr': '\uD835\uDC9E', 'csub': '\u2ACF', 'csube': '\u2AD1', 'csup': '\u2AD0', 'csupe': '\u2AD2', 'ctdot': '\u22EF', 'cudarrl': '\u2938', 'cudarrr': '\u2935', 'cuepr': '\u22DE', 'cuesc': '\u22DF', 'cularr': '\u21B6', 'cularrp': '\u293D', 'cup': '\u222A', 'Cup': '\u22D3', 'cupbrcap': '\u2A48', 'cupcap': '\u2A46', 'CupCap': '\u224D', 'cupcup': '\u2A4A', 'cupdot': '\u228D', 'cupor': '\u2A45', 'cups': '\u222A\uFE00', 'curarr': '\u21B7', 'curarrm': '\u293C', 'curlyeqprec': '\u22DE', 'curlyeqsucc': '\u22DF', 'curlyvee': '\u22CE', 'curlywedge': '\u22CF', 'curren': '\xA4', 'curvearrowleft': '\u21B6', 'curvearrowright': '\u21B7', 'cuvee': '\u22CE', 'cuwed': '\u22CF', 'cwconint': '\u2232', 'cwint': '\u2231', 'cylcty': '\u232D', 'dagger': '\u2020', 'Dagger': '\u2021', 'daleth': '\u2138', 'darr': '\u2193', 'dArr': '\u21D3', 'Darr': '\u21A1', 'dash': '\u2010', 'dashv': '\u22A3', 'Dashv': '\u2AE4', 'dbkarow': '\u290F', 'dblac': '\u02DD', 'dcaron': '\u010F', 'Dcaron': '\u010E', 'dcy': '\u0434', 'Dcy': '\u0414', 'dd': '\u2146', 'DD': '\u2145', 'ddagger': '\u2021', 'ddarr': '\u21CA', 'DDotrahd': '\u2911', 'ddotseq': '\u2A77', 'deg': '\xB0', 'Del': '\u2207', 'delta': '\u03B4', 'Delta': '\u0394', 'demptyv': '\u29B1', 'dfisht': '\u297F', 'dfr': '\uD835\uDD21', 'Dfr': '\uD835\uDD07', 'dHar': '\u2965', 'dharl': '\u21C3', 'dharr': '\u21C2', 'DiacriticalAcute': '\xB4', 'DiacriticalDot': '\u02D9', 'DiacriticalDoubleAcute': '\u02DD', 'DiacriticalGrave': '`', 'DiacriticalTilde': '\u02DC', 'diam': '\u22C4', 'diamond': '\u22C4', 'Diamond': '\u22C4', 'diamondsuit': '\u2666', 'diams': '\u2666', 'die': '\xA8', 'DifferentialD': '\u2146', 'digamma': '\u03DD', 'disin': '\u22F2', 'div': '\xF7', 'divide': '\xF7', 'divideontimes': '\u22C7', 'divonx': '\u22C7', 'djcy': '\u0452', 'DJcy': '\u0402', 'dlcorn': '\u231E', 'dlcrop': '\u230D', 'dollar': '$', 'dopf': '\uD835\uDD55', 'Dopf': '\uD835\uDD3B', 'dot': '\u02D9', 'Dot': '\xA8', 'DotDot': '\u20DC', 'doteq': '\u2250', 'doteqdot': '\u2251', 'DotEqual': '\u2250', 'dotminus': '\u2238', 'dotplus': '\u2214', 'dotsquare': '\u22A1', 'doublebarwedge': '\u2306', 'DoubleContourIntegral': '\u222F', 'DoubleDot': '\xA8', 'DoubleDownArrow': '\u21D3', 'DoubleLeftArrow': '\u21D0', 'DoubleLeftRightArrow': '\u21D4', 'DoubleLeftTee': '\u2AE4', 'DoubleLongLeftArrow': '\u27F8', 'DoubleLongLeftRightArrow': '\u27FA', 'DoubleLongRightArrow': '\u27F9', 'DoubleRightArrow': '\u21D2', 'DoubleRightTee': '\u22A8', 'DoubleUpArrow': '\u21D1', 'DoubleUpDownArrow': '\u21D5', 'DoubleVerticalBar': '\u2225', 'downarrow': '\u2193', 'Downarrow': '\u21D3', 'DownArrow': '\u2193', 'DownArrowBar': '\u2913', 'DownArrowUpArrow': '\u21F5', 'DownBreve': '\u0311', 'downdownarrows': '\u21CA', 'downharpoonleft': '\u21C3', 'downharpoonright': '\u21C2', 'DownLeftRightVector': '\u2950', 'DownLeftTeeVector': '\u295E', 'DownLeftVector': '\u21BD', 'DownLeftVectorBar': '\u2956', 'DownRightTeeVector': '\u295F', 'DownRightVector': '\u21C1', 'DownRightVectorBar': '\u2957', 'DownTee': '\u22A4', 'DownTeeArrow': '\u21A7', 'drbkarow': '\u2910', 'drcorn': '\u231F', 'drcrop': '\u230C', 'dscr': '\uD835\uDCB9', 'Dscr': '\uD835\uDC9F', 'dscy': '\u0455', 'DScy': '\u0405', 'dsol': '\u29F6', 'dstrok': '\u0111', 'Dstrok': '\u0110', 'dtdot': '\u22F1', 'dtri': '\u25BF', 'dtrif': '\u25BE', 'duarr': '\u21F5', 'duhar': '\u296F', 'dwangle': '\u29A6', 'dzcy': '\u045F', 'DZcy': '\u040F', 'dzigrarr': '\u27FF', 'eacute': '\xE9', 'Eacute': '\xC9', 'easter': '\u2A6E', 'ecaron': '\u011B', 'Ecaron': '\u011A', 'ecir': '\u2256', 'ecirc': '\xEA', 'Ecirc': '\xCA', 'ecolon': '\u2255', 'ecy': '\u044D', 'Ecy': '\u042D', 'eDDot': '\u2A77', 'edot': '\u0117', 'eDot': '\u2251', 'Edot': '\u0116', 'ee': '\u2147', 'efDot': '\u2252', 'efr': '\uD835\uDD22', 'Efr': '\uD835\uDD08', 'eg': '\u2A9A', 'egrave': '\xE8', 'Egrave': '\xC8', 'egs': '\u2A96', 'egsdot': '\u2A98', 'el': '\u2A99', 'Element': '\u2208', 'elinters': '\u23E7', 'ell': '\u2113', 'els': '\u2A95', 'elsdot': '\u2A97', 'emacr': '\u0113', 'Emacr': '\u0112', 'empty': '\u2205', 'emptyset': '\u2205', 'EmptySmallSquare': '\u25FB', 'emptyv': '\u2205', 'EmptyVerySmallSquare': '\u25AB', 'emsp': '\u2003', 'emsp13': '\u2004', 'emsp14': '\u2005', 'eng': '\u014B', 'ENG': '\u014A', 'ensp': '\u2002', 'eogon': '\u0119', 'Eogon': '\u0118', 'eopf': '\uD835\uDD56', 'Eopf': '\uD835\uDD3C', 'epar': '\u22D5', 'eparsl': '\u29E3', 'eplus': '\u2A71', 'epsi': '\u03B5', 'epsilon': '\u03B5', 'Epsilon': '\u0395', 'epsiv': '\u03F5', 'eqcirc': '\u2256', 'eqcolon': '\u2255', 'eqsim': '\u2242', 'eqslantgtr': '\u2A96', 'eqslantless': '\u2A95', 'Equal': '\u2A75', 'equals': '=', 'EqualTilde': '\u2242', 'equest': '\u225F', 'Equilibrium': '\u21CC', 'equiv': '\u2261', 'equivDD': '\u2A78', 'eqvparsl': '\u29E5', 'erarr': '\u2971', 'erDot': '\u2253', 'escr': '\u212F', 'Escr': '\u2130', 'esdot': '\u2250', 'esim': '\u2242', 'Esim': '\u2A73', 'eta': '\u03B7', 'Eta': '\u0397', 'eth': '\xF0', 'ETH': '\xD0', 'euml': '\xEB', 'Euml': '\xCB', 'euro': '\u20AC', 'excl': '!', 'exist': '\u2203', 'Exists': '\u2203', 'expectation': '\u2130', 'exponentiale': '\u2147', 'ExponentialE': '\u2147', 'fallingdotseq': '\u2252', 'fcy': '\u0444', 'Fcy': '\u0424', 'female': '\u2640', 'ffilig': '\uFB03', 'fflig': '\uFB00', 'ffllig': '\uFB04', 'ffr': '\uD835\uDD23', 'Ffr': '\uD835\uDD09', 'filig': '\uFB01', 'FilledSmallSquare': '\u25FC', 'FilledVerySmallSquare': '\u25AA', 'fjlig': 'fj', 'flat': '\u266D', 'fllig': '\uFB02', 'fltns': '\u25B1', 'fnof': '\u0192', 'fopf': '\uD835\uDD57', 'Fopf': '\uD835\uDD3D', 'forall': '\u2200', 'ForAll': '\u2200', 'fork': '\u22D4', 'forkv': '\u2AD9', 'Fouriertrf': '\u2131', 'fpartint': '\u2A0D', 'frac12': '\xBD', 'frac13': '\u2153', 'frac14': '\xBC', 'frac15': '\u2155', 'frac16': '\u2159', 'frac18': '\u215B', 'frac23': '\u2154', 'frac25': '\u2156', 'frac34': '\xBE', 'frac35': '\u2157', 'frac38': '\u215C', 'frac45': '\u2158', 'frac56': '\u215A', 'frac58': '\u215D', 'frac78': '\u215E', 'frasl': '\u2044', 'frown': '\u2322', 'fscr': '\uD835\uDCBB', 'Fscr': '\u2131', 'gacute': '\u01F5', 'gamma': '\u03B3', 'Gamma': '\u0393', 'gammad': '\u03DD', 'Gammad': '\u03DC', 'gap': '\u2A86', 'gbreve': '\u011F', 'Gbreve': '\u011E', 'Gcedil': '\u0122', 'gcirc': '\u011D', 'Gcirc': '\u011C', 'gcy': '\u0433', 'Gcy': '\u0413', 'gdot': '\u0121', 'Gdot': '\u0120', 'ge': '\u2265', 'gE': '\u2267', 'gel': '\u22DB', 'gEl': '\u2A8C', 'geq': '\u2265', 'geqq': '\u2267', 'geqslant': '\u2A7E', 'ges': '\u2A7E', 'gescc': '\u2AA9', 'gesdot': '\u2A80', 'gesdoto': '\u2A82', 'gesdotol': '\u2A84', 'gesl': '\u22DB\uFE00', 'gesles': '\u2A94', 'gfr': '\uD835\uDD24', 'Gfr': '\uD835\uDD0A', 'gg': '\u226B', 'Gg': '\u22D9', 'ggg': '\u22D9', 'gimel': '\u2137', 'gjcy': '\u0453', 'GJcy': '\u0403', 'gl': '\u2277', 'gla': '\u2AA5', 'glE': '\u2A92', 'glj': '\u2AA4', 'gnap': '\u2A8A', 'gnapprox': '\u2A8A', 'gne': '\u2A88', 'gnE': '\u2269', 'gneq': '\u2A88', 'gneqq': '\u2269', 'gnsim': '\u22E7', 'gopf': '\uD835\uDD58', 'Gopf': '\uD835\uDD3E', 'grave': '`', 'GreaterEqual': '\u2265', 'GreaterEqualLess': '\u22DB', 'GreaterFullEqual': '\u2267', 'GreaterGreater': '\u2AA2', 'GreaterLess': '\u2277', 'GreaterSlantEqual': '\u2A7E', 'GreaterTilde': '\u2273', 'gscr': '\u210A', 'Gscr': '\uD835\uDCA2', 'gsim': '\u2273', 'gsime': '\u2A8E', 'gsiml': '\u2A90', 'gt': '>', 'Gt': '\u226B', 'GT': '>', 'gtcc': '\u2AA7', 'gtcir': '\u2A7A', 'gtdot': '\u22D7', 'gtlPar': '\u2995', 'gtquest': '\u2A7C', 'gtrapprox': '\u2A86', 'gtrarr': '\u2978', 'gtrdot': '\u22D7', 'gtreqless': '\u22DB', 'gtreqqless': '\u2A8C', 'gtrless': '\u2277', 'gtrsim': '\u2273', 'gvertneqq': '\u2269\uFE00', 'gvnE': '\u2269\uFE00', 'Hacek': '\u02C7', 'hairsp': '\u200A', 'half': '\xBD', 'hamilt': '\u210B', 'hardcy': '\u044A', 'HARDcy': '\u042A', 'harr': '\u2194', 'hArr': '\u21D4', 'harrcir': '\u2948', 'harrw': '\u21AD', 'Hat': '^', 'hbar': '\u210F', 'hcirc': '\u0125', 'Hcirc': '\u0124', 'hearts': '\u2665', 'heartsuit': '\u2665', 'hellip': '\u2026', 'hercon': '\u22B9', 'hfr': '\uD835\uDD25', 'Hfr': '\u210C', 'HilbertSpace': '\u210B', 'hksearow': '\u2925', 'hkswarow': '\u2926', 'hoarr': '\u21FF', 'homtht': '\u223B', 'hookleftarrow': '\u21A9', 'hookrightarrow': '\u21AA', 'hopf': '\uD835\uDD59', 'Hopf': '\u210D', 'horbar': '\u2015', 'HorizontalLine': '\u2500', 'hscr': '\uD835\uDCBD', 'Hscr': '\u210B', 'hslash': '\u210F', 'hstrok': '\u0127', 'Hstrok': '\u0126', 'HumpDownHump': '\u224E', 'HumpEqual': '\u224F', 'hybull': '\u2043', 'hyphen': '\u2010', 'iacute': '\xED', 'Iacute': '\xCD', 'ic': '\u2063', 'icirc': '\xEE', 'Icirc': '\xCE', 'icy': '\u0438', 'Icy': '\u0418', 'Idot': '\u0130', 'iecy': '\u0435', 'IEcy': '\u0415', 'iexcl': '\xA1', 'iff': '\u21D4', 'ifr': '\uD835\uDD26', 'Ifr': '\u2111', 'igrave': '\xEC', 'Igrave': '\xCC', 'ii': '\u2148', 'iiiint': '\u2A0C', 'iiint': '\u222D', 'iinfin': '\u29DC', 'iiota': '\u2129', 'ijlig': '\u0133', 'IJlig': '\u0132', 'Im': '\u2111', 'imacr': '\u012B', 'Imacr': '\u012A', 'image': '\u2111', 'ImaginaryI': '\u2148', 'imagline': '\u2110', 'imagpart': '\u2111', 'imath': '\u0131', 'imof': '\u22B7', 'imped': '\u01B5', 'Implies': '\u21D2', 'in': '\u2208', 'incare': '\u2105', 'infin': '\u221E', 'infintie': '\u29DD', 'inodot': '\u0131', 'int': '\u222B', 'Int': '\u222C', 'intcal': '\u22BA', 'integers': '\u2124', 'Integral': '\u222B', 'intercal': '\u22BA', 'Intersection': '\u22C2', 'intlarhk': '\u2A17', 'intprod': '\u2A3C', 'InvisibleComma': '\u2063', 'InvisibleTimes': '\u2062', 'iocy': '\u0451', 'IOcy': '\u0401', 'iogon': '\u012F', 'Iogon': '\u012E', 'iopf': '\uD835\uDD5A', 'Iopf': '\uD835\uDD40', 'iota': '\u03B9', 'Iota': '\u0399', 'iprod': '\u2A3C', 'iquest': '\xBF', 'iscr': '\uD835\uDCBE', 'Iscr': '\u2110', 'isin': '\u2208', 'isindot': '\u22F5', 'isinE': '\u22F9', 'isins': '\u22F4', 'isinsv': '\u22F3', 'isinv': '\u2208', 'it': '\u2062', 'itilde': '\u0129', 'Itilde': '\u0128', 'iukcy': '\u0456', 'Iukcy': '\u0406', 'iuml': '\xEF', 'Iuml': '\xCF', 'jcirc': '\u0135', 'Jcirc': '\u0134', 'jcy': '\u0439', 'Jcy': '\u0419', 'jfr': '\uD835\uDD27', 'Jfr': '\uD835\uDD0D', 'jmath': '\u0237', 'jopf': '\uD835\uDD5B', 'Jopf': '\uD835\uDD41', 'jscr': '\uD835\uDCBF', 'Jscr': '\uD835\uDCA5', 'jsercy': '\u0458', 'Jsercy': '\u0408', 'jukcy': '\u0454', 'Jukcy': '\u0404', 'kappa': '\u03BA', 'Kappa': '\u039A', 'kappav': '\u03F0', 'kcedil': '\u0137', 'Kcedil': '\u0136', 'kcy': '\u043A', 'Kcy': '\u041A', 'kfr': '\uD835\uDD28', 'Kfr': '\uD835\uDD0E', 'kgreen': '\u0138', 'khcy': '\u0445', 'KHcy': '\u0425', 'kjcy': '\u045C', 'KJcy': '\u040C', 'kopf': '\uD835\uDD5C', 'Kopf': '\uD835\uDD42', 'kscr': '\uD835\uDCC0', 'Kscr': '\uD835\uDCA6', 'lAarr': '\u21DA', 'lacute': '\u013A', 'Lacute': '\u0139', 'laemptyv': '\u29B4', 'lagran': '\u2112', 'lambda': '\u03BB', 'Lambda': '\u039B', 'lang': '\u27E8', 'Lang': '\u27EA', 'langd': '\u2991', 'langle': '\u27E8', 'lap': '\u2A85', 'Laplacetrf': '\u2112', 'laquo': '\xAB', 'larr': '\u2190', 'lArr': '\u21D0', 'Larr': '\u219E', 'larrb': '\u21E4', 'larrbfs': '\u291F', 'larrfs': '\u291D', 'larrhk': '\u21A9', 'larrlp': '\u21AB', 'larrpl': '\u2939', 'larrsim': '\u2973', 'larrtl': '\u21A2', 'lat': '\u2AAB', 'latail': '\u2919', 'lAtail': '\u291B', 'late': '\u2AAD', 'lates': '\u2AAD\uFE00', 'lbarr': '\u290C', 'lBarr': '\u290E', 'lbbrk': '\u2772', 'lbrace': '{', 'lbrack': '[', 'lbrke': '\u298B', 'lbrksld': '\u298F', 'lbrkslu': '\u298D', 'lcaron': '\u013E', 'Lcaron': '\u013D', 'lcedil': '\u013C', 'Lcedil': '\u013B', 'lceil': '\u2308', 'lcub': '{', 'lcy': '\u043B', 'Lcy': '\u041B', 'ldca': '\u2936', 'ldquo': '\u201C', 'ldquor': '\u201E', 'ldrdhar': '\u2967', 'ldrushar': '\u294B', 'ldsh': '\u21B2', 'le': '\u2264', 'lE': '\u2266', 'LeftAngleBracket': '\u27E8', 'leftarrow': '\u2190', 'Leftarrow': '\u21D0', 'LeftArrow': '\u2190', 'LeftArrowBar': '\u21E4', 'LeftArrowRightArrow': '\u21C6', 'leftarrowtail': '\u21A2', 'LeftCeiling': '\u2308', 'LeftDoubleBracket': '\u27E6', 'LeftDownTeeVector': '\u2961', 'LeftDownVector': '\u21C3', 'LeftDownVectorBar': '\u2959', 'LeftFloor': '\u230A', 'leftharpoondown': '\u21BD', 'leftharpoonup': '\u21BC', 'leftleftarrows': '\u21C7', 'leftrightarrow': '\u2194', 'Leftrightarrow': '\u21D4', 'LeftRightArrow': '\u2194', 'leftrightarrows': '\u21C6', 'leftrightharpoons': '\u21CB', 'leftrightsquigarrow': '\u21AD', 'LeftRightVector': '\u294E', 'LeftTee': '\u22A3', 'LeftTeeArrow': '\u21A4', 'LeftTeeVector': '\u295A', 'leftthreetimes': '\u22CB', 'LeftTriangle': '\u22B2', 'LeftTriangleBar': '\u29CF', 'LeftTriangleEqual': '\u22B4', 'LeftUpDownVector': '\u2951', 'LeftUpTeeVector': '\u2960', 'LeftUpVector': '\u21BF', 'LeftUpVectorBar': '\u2958', 'LeftVector': '\u21BC', 'LeftVectorBar': '\u2952', 'leg': '\u22DA', 'lEg': '\u2A8B', 'leq': '\u2264', 'leqq': '\u2266', 'leqslant': '\u2A7D', 'les': '\u2A7D', 'lescc': '\u2AA8', 'lesdot': '\u2A7F', 'lesdoto': '\u2A81', 'lesdotor': '\u2A83', 'lesg': '\u22DA\uFE00', 'lesges': '\u2A93', 'lessapprox': '\u2A85', 'lessdot': '\u22D6', 'lesseqgtr': '\u22DA', 'lesseqqgtr': '\u2A8B', 'LessEqualGreater': '\u22DA', 'LessFullEqual': '\u2266', 'LessGreater': '\u2276', 'lessgtr': '\u2276', 'LessLess': '\u2AA1', 'lesssim': '\u2272', 'LessSlantEqual': '\u2A7D', 'LessTilde': '\u2272', 'lfisht': '\u297C', 'lfloor': '\u230A', 'lfr': '\uD835\uDD29', 'Lfr': '\uD835\uDD0F', 'lg': '\u2276', 'lgE': '\u2A91', 'lHar': '\u2962', 'lhard': '\u21BD', 'lharu': '\u21BC', 'lharul': '\u296A', 'lhblk': '\u2584', 'ljcy': '\u0459', 'LJcy': '\u0409', 'll': '\u226A', 'Ll': '\u22D8', 'llarr': '\u21C7', 'llcorner': '\u231E', 'Lleftarrow': '\u21DA', 'llhard': '\u296B', 'lltri': '\u25FA', 'lmidot': '\u0140', 'Lmidot': '\u013F', 'lmoust': '\u23B0', 'lmoustache': '\u23B0', 'lnap': '\u2A89', 'lnapprox': '\u2A89', 'lne': '\u2A87', 'lnE': '\u2268', 'lneq': '\u2A87', 'lneqq': '\u2268', 'lnsim': '\u22E6', 'loang': '\u27EC', 'loarr': '\u21FD', 'lobrk': '\u27E6', 'longleftarrow': '\u27F5', 'Longleftarrow': '\u27F8', 'LongLeftArrow': '\u27F5', 'longleftrightarrow': '\u27F7', 'Longleftrightarrow': '\u27FA', 'LongLeftRightArrow': '\u27F7', 'longmapsto': '\u27FC', 'longrightarrow': '\u27F6', 'Longrightarrow': '\u27F9', 'LongRightArrow': '\u27F6', 'looparrowleft': '\u21AB', 'looparrowright': '\u21AC', 'lopar': '\u2985', 'lopf': '\uD835\uDD5D', 'Lopf': '\uD835\uDD43', 'loplus': '\u2A2D', 'lotimes': '\u2A34', 'lowast': '\u2217', 'lowbar': '_', 'LowerLeftArrow': '\u2199', 'LowerRightArrow': '\u2198', 'loz': '\u25CA', 'lozenge': '\u25CA', 'lozf': '\u29EB', 'lpar': '(', 'lparlt': '\u2993', 'lrarr': '\u21C6', 'lrcorner': '\u231F', 'lrhar': '\u21CB', 'lrhard': '\u296D', 'lrm': '\u200E', 'lrtri': '\u22BF', 'lsaquo': '\u2039', 'lscr': '\uD835\uDCC1', 'Lscr': '\u2112', 'lsh': '\u21B0', 'Lsh': '\u21B0', 'lsim': '\u2272', 'lsime': '\u2A8D', 'lsimg': '\u2A8F', 'lsqb': '[', 'lsquo': '\u2018', 'lsquor': '\u201A', 'lstrok': '\u0142', 'Lstrok': '\u0141', 'lt': '<', 'Lt': '\u226A', 'LT': '<', 'ltcc': '\u2AA6', 'ltcir': '\u2A79', 'ltdot': '\u22D6', 'lthree': '\u22CB', 'ltimes': '\u22C9', 'ltlarr': '\u2976', 'ltquest': '\u2A7B', 'ltri': '\u25C3', 'ltrie': '\u22B4', 'ltrif': '\u25C2', 'ltrPar': '\u2996', 'lurdshar': '\u294A', 'luruhar': '\u2966', 'lvertneqq': '\u2268\uFE00', 'lvnE': '\u2268\uFE00', 'macr': '\xAF', 'male': '\u2642', 'malt': '\u2720', 'maltese': '\u2720', 'map': '\u21A6', 'Map': '\u2905', 'mapsto': '\u21A6', 'mapstodown': '\u21A7', 'mapstoleft': '\u21A4', 'mapstoup': '\u21A5', 'marker': '\u25AE', 'mcomma': '\u2A29', 'mcy': '\u043C', 'Mcy': '\u041C', 'mdash': '\u2014', 'mDDot': '\u223A', 'measuredangle': '\u2221', 'MediumSpace': '\u205F', 'Mellintrf': '\u2133', 'mfr': '\uD835\uDD2A', 'Mfr': '\uD835\uDD10', 'mho': '\u2127', 'micro': '\xB5', 'mid': '\u2223', 'midast': '*', 'midcir': '\u2AF0', 'middot': '\xB7', 'minus': '\u2212', 'minusb': '\u229F', 'minusd': '\u2238', 'minusdu': '\u2A2A', 'MinusPlus': '\u2213', 'mlcp': '\u2ADB', 'mldr': '\u2026', 'mnplus': '\u2213', 'models': '\u22A7', 'mopf': '\uD835\uDD5E', 'Mopf': '\uD835\uDD44', 'mp': '\u2213', 'mscr': '\uD835\uDCC2', 'Mscr': '\u2133', 'mstpos': '\u223E', 'mu': '\u03BC', 'Mu': '\u039C', 'multimap': '\u22B8', 'mumap': '\u22B8', 'nabla': '\u2207', 'nacute': '\u0144', 'Nacute': '\u0143', 'nang': '\u2220\u20D2', 'nap': '\u2249', 'napE': '\u2A70\u0338', 'napid': '\u224B\u0338', 'napos': '\u0149', 'napprox': '\u2249', 'natur': '\u266E', 'natural': '\u266E', 'naturals': '\u2115', 'nbsp': '\xA0', 'nbump': '\u224E\u0338', 'nbumpe': '\u224F\u0338', 'ncap': '\u2A43', 'ncaron': '\u0148', 'Ncaron': '\u0147', 'ncedil': '\u0146', 'Ncedil': '\u0145', 'ncong': '\u2247', 'ncongdot': '\u2A6D\u0338', 'ncup': '\u2A42', 'ncy': '\u043D', 'Ncy': '\u041D', 'ndash': '\u2013', 'ne': '\u2260', 'nearhk': '\u2924', 'nearr': '\u2197', 'neArr': '\u21D7', 'nearrow': '\u2197', 'nedot': '\u2250\u0338', 'NegativeMediumSpace': '\u200B', 'NegativeThickSpace': '\u200B', 'NegativeThinSpace': '\u200B', 'NegativeVeryThinSpace': '\u200B', 'nequiv': '\u2262', 'nesear': '\u2928', 'nesim': '\u2242\u0338', 'NestedGreaterGreater': '\u226B', 'NestedLessLess': '\u226A', 'NewLine': '\n', 'nexist': '\u2204', 'nexists': '\u2204', 'nfr': '\uD835\uDD2B', 'Nfr': '\uD835\uDD11', 'nge': '\u2271', 'ngE': '\u2267\u0338', 'ngeq': '\u2271', 'ngeqq': '\u2267\u0338', 'ngeqslant': '\u2A7E\u0338', 'nges': '\u2A7E\u0338', 'nGg': '\u22D9\u0338', 'ngsim': '\u2275', 'ngt': '\u226F', 'nGt': '\u226B\u20D2', 'ngtr': '\u226F', 'nGtv': '\u226B\u0338', 'nharr': '\u21AE', 'nhArr': '\u21CE', 'nhpar': '\u2AF2', 'ni': '\u220B', 'nis': '\u22FC', 'nisd': '\u22FA', 'niv': '\u220B', 'njcy': '\u045A', 'NJcy': '\u040A', 'nlarr': '\u219A', 'nlArr': '\u21CD', 'nldr': '\u2025', 'nle': '\u2270', 'nlE': '\u2266\u0338', 'nleftarrow': '\u219A', 'nLeftarrow': '\u21CD', 'nleftrightarrow': '\u21AE', 'nLeftrightarrow': '\u21CE', 'nleq': '\u2270', 'nleqq': '\u2266\u0338', 'nleqslant': '\u2A7D\u0338', 'nles': '\u2A7D\u0338', 'nless': '\u226E', 'nLl': '\u22D8\u0338', 'nlsim': '\u2274', 'nlt': '\u226E', 'nLt': '\u226A\u20D2', 'nltri': '\u22EA', 'nltrie': '\u22EC', 'nLtv': '\u226A\u0338', 'nmid': '\u2224', 'NoBreak': '\u2060', 'NonBreakingSpace': '\xA0', 'nopf': '\uD835\uDD5F', 'Nopf': '\u2115', 'not': '\xAC', 'Not': '\u2AEC', 'NotCongruent': '\u2262', 'NotCupCap': '\u226D', 'NotDoubleVerticalBar': '\u2226', 'NotElement': '\u2209', 'NotEqual': '\u2260', 'NotEqualTilde': '\u2242\u0338', 'NotExists': '\u2204', 'NotGreater': '\u226F', 'NotGreaterEqual': '\u2271', 'NotGreaterFullEqual': '\u2267\u0338', 'NotGreaterGreater': '\u226B\u0338', 'NotGreaterLess': '\u2279', 'NotGreaterSlantEqual': '\u2A7E\u0338', 'NotGreaterTilde': '\u2275', 'NotHumpDownHump': '\u224E\u0338', 'NotHumpEqual': '\u224F\u0338', 'notin': '\u2209', 'notindot': '\u22F5\u0338', 'notinE': '\u22F9\u0338', 'notinva': '\u2209', 'notinvb': '\u22F7', 'notinvc': '\u22F6', 'NotLeftTriangle': '\u22EA', 'NotLeftTriangleBar': '\u29CF\u0338', 'NotLeftTriangleEqual': '\u22EC', 'NotLess': '\u226E', 'NotLessEqual': '\u2270', 'NotLessGreater': '\u2278', 'NotLessLess': '\u226A\u0338', 'NotLessSlantEqual': '\u2A7D\u0338', 'NotLessTilde': '\u2274', 'NotNestedGreaterGreater': '\u2AA2\u0338', 'NotNestedLessLess': '\u2AA1\u0338', 'notni': '\u220C', 'notniva': '\u220C', 'notnivb': '\u22FE', 'notnivc': '\u22FD', 'NotPrecedes': '\u2280', 'NotPrecedesEqual': '\u2AAF\u0338', 'NotPrecedesSlantEqual': '\u22E0', 'NotReverseElement': '\u220C', 'NotRightTriangle': '\u22EB', 'NotRightTriangleBar': '\u29D0\u0338', 'NotRightTriangleEqual': '\u22ED', 'NotSquareSubset': '\u228F\u0338', 'NotSquareSubsetEqual': '\u22E2', 'NotSquareSuperset': '\u2290\u0338', 'NotSquareSupersetEqual': '\u22E3', 'NotSubset': '\u2282\u20D2', 'NotSubsetEqual': '\u2288', 'NotSucceeds': '\u2281', 'NotSucceedsEqual': '\u2AB0\u0338', 'NotSucceedsSlantEqual': '\u22E1', 'NotSucceedsTilde': '\u227F\u0338', 'NotSuperset': '\u2283\u20D2', 'NotSupersetEqual': '\u2289', 'NotTilde': '\u2241', 'NotTildeEqual': '\u2244', 'NotTildeFullEqual': '\u2247', 'NotTildeTilde': '\u2249', 'NotVerticalBar': '\u2224', 'npar': '\u2226', 'nparallel': '\u2226', 'nparsl': '\u2AFD\u20E5', 'npart': '\u2202\u0338', 'npolint': '\u2A14', 'npr': '\u2280', 'nprcue': '\u22E0', 'npre': '\u2AAF\u0338', 'nprec': '\u2280', 'npreceq': '\u2AAF\u0338', 'nrarr': '\u219B', 'nrArr': '\u21CF', 'nrarrc': '\u2933\u0338', 'nrarrw': '\u219D\u0338', 'nrightarrow': '\u219B', 'nRightarrow': '\u21CF', 'nrtri': '\u22EB', 'nrtrie': '\u22ED', 'nsc': '\u2281', 'nsccue': '\u22E1', 'nsce': '\u2AB0\u0338', 'nscr': '\uD835\uDCC3', 'Nscr': '\uD835\uDCA9', 'nshortmid': '\u2224', 'nshortparallel': '\u2226', 'nsim': '\u2241', 'nsime': '\u2244', 'nsimeq': '\u2244', 'nsmid': '\u2224', 'nspar': '\u2226', 'nsqsube': '\u22E2', 'nsqsupe': '\u22E3', 'nsub': '\u2284', 'nsube': '\u2288', 'nsubE': '\u2AC5\u0338', 'nsubset': '\u2282\u20D2', 'nsubseteq': '\u2288', 'nsubseteqq': '\u2AC5\u0338', 'nsucc': '\u2281', 'nsucceq': '\u2AB0\u0338', 'nsup': '\u2285', 'nsupe': '\u2289', 'nsupE': '\u2AC6\u0338', 'nsupset': '\u2283\u20D2', 'nsupseteq': '\u2289', 'nsupseteqq': '\u2AC6\u0338', 'ntgl': '\u2279', 'ntilde': '\xF1', 'Ntilde': '\xD1', 'ntlg': '\u2278', 'ntriangleleft': '\u22EA', 'ntrianglelefteq': '\u22EC', 'ntriangleright': '\u22EB', 'ntrianglerighteq': '\u22ED', 'nu': '\u03BD', 'Nu': '\u039D', 'num': '#', 'numero': '\u2116', 'numsp': '\u2007', 'nvap': '\u224D\u20D2', 'nvdash': '\u22AC', 'nvDash': '\u22AD', 'nVdash': '\u22AE', 'nVDash': '\u22AF', 'nvge': '\u2265\u20D2', 'nvgt': '>\u20D2', 'nvHarr': '\u2904', 'nvinfin': '\u29DE', 'nvlArr': '\u2902', 'nvle': '\u2264\u20D2', 'nvlt': '<\u20D2', 'nvltrie': '\u22B4\u20D2', 'nvrArr': '\u2903', 'nvrtrie': '\u22B5\u20D2', 'nvsim': '\u223C\u20D2', 'nwarhk': '\u2923', 'nwarr': '\u2196', 'nwArr': '\u21D6', 'nwarrow': '\u2196', 'nwnear': '\u2927', 'oacute': '\xF3', 'Oacute': '\xD3', 'oast': '\u229B', 'ocir': '\u229A', 'ocirc': '\xF4', 'Ocirc': '\xD4', 'ocy': '\u043E', 'Ocy': '\u041E', 'odash': '\u229D', 'odblac': '\u0151', 'Odblac': '\u0150', 'odiv': '\u2A38', 'odot': '\u2299', 'odsold': '\u29BC', 'oelig': '\u0153', 'OElig': '\u0152', 'ofcir': '\u29BF', 'ofr': '\uD835\uDD2C', 'Ofr': '\uD835\uDD12', 'ogon': '\u02DB', 'ograve': '\xF2', 'Ograve': '\xD2', 'ogt': '\u29C1', 'ohbar': '\u29B5', 'ohm': '\u03A9', 'oint': '\u222E', 'olarr': '\u21BA', 'olcir': '\u29BE', 'olcross': '\u29BB', 'oline': '\u203E', 'olt': '\u29C0', 'omacr': '\u014D', 'Omacr': '\u014C', 'omega': '\u03C9', 'Omega': '\u03A9', 'omicron': '\u03BF', 'Omicron': '\u039F', 'omid': '\u29B6', 'ominus': '\u2296', 'oopf': '\uD835\uDD60', 'Oopf': '\uD835\uDD46', 'opar': '\u29B7', 'OpenCurlyDoubleQuote': '\u201C', 'OpenCurlyQuote': '\u2018', 'operp': '\u29B9', 'oplus': '\u2295', 'or': '\u2228', 'Or': '\u2A54', 'orarr': '\u21BB', 'ord': '\u2A5D', 'order': '\u2134', 'orderof': '\u2134', 'ordf': '\xAA', 'ordm': '\xBA', 'origof': '\u22B6', 'oror': '\u2A56', 'orslope': '\u2A57', 'orv': '\u2A5B', 'oS': '\u24C8', 'oscr': '\u2134', 'Oscr': '\uD835\uDCAA', 'oslash': '\xF8', 'Oslash': '\xD8', 'osol': '\u2298', 'otilde': '\xF5', 'Otilde': '\xD5', 'otimes': '\u2297', 'Otimes': '\u2A37', 'otimesas': '\u2A36', 'ouml': '\xF6', 'Ouml': '\xD6', 'ovbar': '\u233D', 'OverBar': '\u203E', 'OverBrace': '\u23DE', 'OverBracket': '\u23B4', 'OverParenthesis': '\u23DC', 'par': '\u2225', 'para': '\xB6', 'parallel': '\u2225', 'parsim': '\u2AF3', 'parsl': '\u2AFD', 'part': '\u2202', 'PartialD': '\u2202', 'pcy': '\u043F', 'Pcy': '\u041F', 'percnt': '%', 'period': '.', 'permil': '\u2030', 'perp': '\u22A5', 'pertenk': '\u2031', 'pfr': '\uD835\uDD2D', 'Pfr': '\uD835\uDD13', 'phi': '\u03C6', 'Phi': '\u03A6', 'phiv': '\u03D5', 'phmmat': '\u2133', 'phone': '\u260E', 'pi': '\u03C0', 'Pi': '\u03A0', 'pitchfork': '\u22D4', 'piv': '\u03D6', 'planck': '\u210F', 'planckh': '\u210E', 'plankv': '\u210F', 'plus': '+', 'plusacir': '\u2A23', 'plusb': '\u229E', 'pluscir': '\u2A22', 'plusdo': '\u2214', 'plusdu': '\u2A25', 'pluse': '\u2A72', 'PlusMinus': '\xB1', 'plusmn': '\xB1', 'plussim': '\u2A26', 'plustwo': '\u2A27', 'pm': '\xB1', 'Poincareplane': '\u210C', 'pointint': '\u2A15', 'popf': '\uD835\uDD61', 'Popf': '\u2119', 'pound': '\xA3', 'pr': '\u227A', 'Pr': '\u2ABB', 'prap': '\u2AB7', 'prcue': '\u227C', 'pre': '\u2AAF', 'prE': '\u2AB3', 'prec': '\u227A', 'precapprox': '\u2AB7', 'preccurlyeq': '\u227C', 'Precedes': '\u227A', 'PrecedesEqual': '\u2AAF', 'PrecedesSlantEqual': '\u227C', 'PrecedesTilde': '\u227E', 'preceq': '\u2AAF', 'precnapprox': '\u2AB9', 'precneqq': '\u2AB5', 'precnsim': '\u22E8', 'precsim': '\u227E', 'prime': '\u2032', 'Prime': '\u2033', 'primes': '\u2119', 'prnap': '\u2AB9', 'prnE': '\u2AB5', 'prnsim': '\u22E8', 'prod': '\u220F', 'Product': '\u220F', 'profalar': '\u232E', 'profline': '\u2312', 'profsurf': '\u2313', 'prop': '\u221D', 'Proportion': '\u2237', 'Proportional': '\u221D', 'propto': '\u221D', 'prsim': '\u227E', 'prurel': '\u22B0', 'pscr': '\uD835\uDCC5', 'Pscr': '\uD835\uDCAB', 'psi': '\u03C8', 'Psi': '\u03A8', 'puncsp': '\u2008', 'qfr': '\uD835\uDD2E', 'Qfr': '\uD835\uDD14', 'qint': '\u2A0C', 'qopf': '\uD835\uDD62', 'Qopf': '\u211A', 'qprime': '\u2057', 'qscr': '\uD835\uDCC6', 'Qscr': '\uD835\uDCAC', 'quaternions': '\u210D', 'quatint': '\u2A16', 'quest': '?', 'questeq': '\u225F', 'quot': '"', 'QUOT': '"', 'rAarr': '\u21DB', 'race': '\u223D\u0331', 'racute': '\u0155', 'Racute': '\u0154', 'radic': '\u221A', 'raemptyv': '\u29B3', 'rang': '\u27E9', 'Rang': '\u27EB', 'rangd': '\u2992', 'range': '\u29A5', 'rangle': '\u27E9', 'raquo': '\xBB', 'rarr': '\u2192', 'rArr': '\u21D2', 'Rarr': '\u21A0', 'rarrap': '\u2975', 'rarrb': '\u21E5', 'rarrbfs': '\u2920', 'rarrc': '\u2933', 'rarrfs': '\u291E', 'rarrhk': '\u21AA', 'rarrlp': '\u21AC', 'rarrpl': '\u2945', 'rarrsim': '\u2974', 'rarrtl': '\u21A3', 'Rarrtl': '\u2916', 'rarrw': '\u219D', 'ratail': '\u291A', 'rAtail': '\u291C', 'ratio': '\u2236', 'rationals': '\u211A', 'rbarr': '\u290D', 'rBarr': '\u290F', 'RBarr': '\u2910', 'rbbrk': '\u2773', 'rbrace': '}', 'rbrack': ']', 'rbrke': '\u298C', 'rbrksld': '\u298E', 'rbrkslu': '\u2990', 'rcaron': '\u0159', 'Rcaron': '\u0158', 'rcedil': '\u0157', 'Rcedil': '\u0156', 'rceil': '\u2309', 'rcub': '}', 'rcy': '\u0440', 'Rcy': '\u0420', 'rdca': '\u2937', 'rdldhar': '\u2969', 'rdquo': '\u201D', 'rdquor': '\u201D', 'rdsh': '\u21B3', 'Re': '\u211C', 'real': '\u211C', 'realine': '\u211B', 'realpart': '\u211C', 'reals': '\u211D', 'rect': '\u25AD', 'reg': '\xAE', 'REG': '\xAE', 'ReverseElement': '\u220B', 'ReverseEquilibrium': '\u21CB', 'ReverseUpEquilibrium': '\u296F', 'rfisht': '\u297D', 'rfloor': '\u230B', 'rfr': '\uD835\uDD2F', 'Rfr': '\u211C', 'rHar': '\u2964', 'rhard': '\u21C1', 'rharu': '\u21C0', 'rharul': '\u296C', 'rho': '\u03C1', 'Rho': '\u03A1', 'rhov': '\u03F1', 'RightAngleBracket': '\u27E9', 'rightarrow': '\u2192', 'Rightarrow': '\u21D2', 'RightArrow': '\u2192', 'RightArrowBar': '\u21E5', 'RightArrowLeftArrow': '\u21C4', 'rightarrowtail': '\u21A3', 'RightCeiling': '\u2309', 'RightDoubleBracket': '\u27E7', 'RightDownTeeVector': '\u295D', 'RightDownVector': '\u21C2', 'RightDownVectorBar': '\u2955', 'RightFloor': '\u230B', 'rightharpoondown': '\u21C1', 'rightharpoonup': '\u21C0', 'rightleftarrows': '\u21C4', 'rightleftharpoons': '\u21CC', 'rightrightarrows': '\u21C9', 'rightsquigarrow': '\u219D', 'RightTee': '\u22A2', 'RightTeeArrow': '\u21A6', 'RightTeeVector': '\u295B', 'rightthreetimes': '\u22CC', 'RightTriangle': '\u22B3', 'RightTriangleBar': '\u29D0', 'RightTriangleEqual': '\u22B5', 'RightUpDownVector': '\u294F', 'RightUpTeeVector': '\u295C', 'RightUpVector': '\u21BE', 'RightUpVectorBar': '\u2954', 'RightVector': '\u21C0', 'RightVectorBar': '\u2953', 'ring': '\u02DA', 'risingdotseq': '\u2253', 'rlarr': '\u21C4', 'rlhar': '\u21CC', 'rlm': '\u200F', 'rmoust': '\u23B1', 'rmoustache': '\u23B1', 'rnmid': '\u2AEE', 'roang': '\u27ED', 'roarr': '\u21FE', 'robrk': '\u27E7', 'ropar': '\u2986', 'ropf': '\uD835\uDD63', 'Ropf': '\u211D', 'roplus': '\u2A2E', 'rotimes': '\u2A35', 'RoundImplies': '\u2970', 'rpar': ')', 'rpargt': '\u2994', 'rppolint': '\u2A12', 'rrarr': '\u21C9', 'Rrightarrow': '\u21DB', 'rsaquo': '\u203A', 'rscr': '\uD835\uDCC7', 'Rscr': '\u211B', 'rsh': '\u21B1', 'Rsh': '\u21B1', 'rsqb': ']', 'rsquo': '\u2019', 'rsquor': '\u2019', 'rthree': '\u22CC', 'rtimes': '\u22CA', 'rtri': '\u25B9', 'rtrie': '\u22B5', 'rtrif': '\u25B8', 'rtriltri': '\u29CE', 'RuleDelayed': '\u29F4', 'ruluhar': '\u2968', 'rx': '\u211E', 'sacute': '\u015B', 'Sacute': '\u015A', 'sbquo': '\u201A', 'sc': '\u227B', 'Sc': '\u2ABC', 'scap': '\u2AB8', 'scaron': '\u0161', 'Scaron': '\u0160', 'sccue': '\u227D', 'sce': '\u2AB0', 'scE': '\u2AB4', 'scedil': '\u015F', 'Scedil': '\u015E', 'scirc': '\u015D', 'Scirc': '\u015C', 'scnap': '\u2ABA', 'scnE': '\u2AB6', 'scnsim': '\u22E9', 'scpolint': '\u2A13', 'scsim': '\u227F', 'scy': '\u0441', 'Scy': '\u0421', 'sdot': '\u22C5', 'sdotb': '\u22A1', 'sdote': '\u2A66', 'searhk': '\u2925', 'searr': '\u2198', 'seArr': '\u21D8', 'searrow': '\u2198', 'sect': '\xA7', 'semi': ';', 'seswar': '\u2929', 'setminus': '\u2216', 'setmn': '\u2216', 'sext': '\u2736', 'sfr': '\uD835\uDD30', 'Sfr': '\uD835\uDD16', 'sfrown': '\u2322', 'sharp': '\u266F', 'shchcy': '\u0449', 'SHCHcy': '\u0429', 'shcy': '\u0448', 'SHcy': '\u0428', 'ShortDownArrow': '\u2193', 'ShortLeftArrow': '\u2190', 'shortmid': '\u2223', 'shortparallel': '\u2225', 'ShortRightArrow': '\u2192', 'ShortUpArrow': '\u2191', 'shy': '\xAD', 'sigma': '\u03C3', 'Sigma': '\u03A3', 'sigmaf': '\u03C2', 'sigmav': '\u03C2', 'sim': '\u223C', 'simdot': '\u2A6A', 'sime': '\u2243', 'simeq': '\u2243', 'simg': '\u2A9E', 'simgE': '\u2AA0', 'siml': '\u2A9D', 'simlE': '\u2A9F', 'simne': '\u2246', 'simplus': '\u2A24', 'simrarr': '\u2972', 'slarr': '\u2190', 'SmallCircle': '\u2218', 'smallsetminus': '\u2216', 'smashp': '\u2A33', 'smeparsl': '\u29E4', 'smid': '\u2223', 'smile': '\u2323', 'smt': '\u2AAA', 'smte': '\u2AAC', 'smtes': '\u2AAC\uFE00', 'softcy': '\u044C', 'SOFTcy': '\u042C', 'sol': '/', 'solb': '\u29C4', 'solbar': '\u233F', 'sopf': '\uD835\uDD64', 'Sopf': '\uD835\uDD4A', 'spades': '\u2660', 'spadesuit': '\u2660', 'spar': '\u2225', 'sqcap': '\u2293', 'sqcaps': '\u2293\uFE00', 'sqcup': '\u2294', 'sqcups': '\u2294\uFE00', 'Sqrt': '\u221A', 'sqsub': '\u228F', 'sqsube': '\u2291', 'sqsubset': '\u228F', 'sqsubseteq': '\u2291', 'sqsup': '\u2290', 'sqsupe': '\u2292', 'sqsupset': '\u2290', 'sqsupseteq': '\u2292', 'squ': '\u25A1', 'square': '\u25A1', 'Square': '\u25A1', 'SquareIntersection': '\u2293', 'SquareSubset': '\u228F', 'SquareSubsetEqual': '\u2291', 'SquareSuperset': '\u2290', 'SquareSupersetEqual': '\u2292', 'SquareUnion': '\u2294', 'squarf': '\u25AA', 'squf': '\u25AA', 'srarr': '\u2192', 'sscr': '\uD835\uDCC8', 'Sscr': '\uD835\uDCAE', 'ssetmn': '\u2216', 'ssmile': '\u2323', 'sstarf': '\u22C6', 'star': '\u2606', 'Star': '\u22C6', 'starf': '\u2605', 'straightepsilon': '\u03F5', 'straightphi': '\u03D5', 'strns': '\xAF', 'sub': '\u2282', 'Sub': '\u22D0', 'subdot': '\u2ABD', 'sube': '\u2286', 'subE': '\u2AC5', 'subedot': '\u2AC3', 'submult': '\u2AC1', 'subne': '\u228A', 'subnE': '\u2ACB', 'subplus': '\u2ABF', 'subrarr': '\u2979', 'subset': '\u2282', 'Subset': '\u22D0', 'subseteq': '\u2286', 'subseteqq': '\u2AC5', 'SubsetEqual': '\u2286', 'subsetneq': '\u228A', 'subsetneqq': '\u2ACB', 'subsim': '\u2AC7', 'subsub': '\u2AD5', 'subsup': '\u2AD3', 'succ': '\u227B', 'succapprox': '\u2AB8', 'succcurlyeq': '\u227D', 'Succeeds': '\u227B', 'SucceedsEqual': '\u2AB0', 'SucceedsSlantEqual': '\u227D', 'SucceedsTilde': '\u227F', 'succeq': '\u2AB0', 'succnapprox': '\u2ABA', 'succneqq': '\u2AB6', 'succnsim': '\u22E9', 'succsim': '\u227F', 'SuchThat': '\u220B', 'sum': '\u2211', 'Sum': '\u2211', 'sung': '\u266A', 'sup': '\u2283', 'Sup': '\u22D1', 'sup1': '\xB9', 'sup2': '\xB2', 'sup3': '\xB3', 'supdot': '\u2ABE', 'supdsub': '\u2AD8', 'supe': '\u2287', 'supE': '\u2AC6', 'supedot': '\u2AC4', 'Superset': '\u2283', 'SupersetEqual': '\u2287', 'suphsol': '\u27C9', 'suphsub': '\u2AD7', 'suplarr': '\u297B', 'supmult': '\u2AC2', 'supne': '\u228B', 'supnE': '\u2ACC', 'supplus': '\u2AC0', 'supset': '\u2283', 'Supset': '\u22D1', 'supseteq': '\u2287', 'supseteqq': '\u2AC6', 'supsetneq': '\u228B', 'supsetneqq': '\u2ACC', 'supsim': '\u2AC8', 'supsub': '\u2AD4', 'supsup': '\u2AD6', 'swarhk': '\u2926', 'swarr': '\u2199', 'swArr': '\u21D9', 'swarrow': '\u2199', 'swnwar': '\u292A', 'szlig': '\xDF', 'Tab': '\t', 'target': '\u2316', 'tau': '\u03C4', 'Tau': '\u03A4', 'tbrk': '\u23B4', 'tcaron': '\u0165', 'Tcaron': '\u0164', 'tcedil': '\u0163', 'Tcedil': '\u0162', 'tcy': '\u0442', 'Tcy': '\u0422', 'tdot': '\u20DB', 'telrec': '\u2315', 'tfr': '\uD835\uDD31', 'Tfr': '\uD835\uDD17', 'there4': '\u2234', 'therefore': '\u2234', 'Therefore': '\u2234', 'theta': '\u03B8', 'Theta': '\u0398', 'thetasym': '\u03D1', 'thetav': '\u03D1', 'thickapprox': '\u2248', 'thicksim': '\u223C', 'ThickSpace': '\u205F\u200A', 'thinsp': '\u2009', 'ThinSpace': '\u2009', 'thkap': '\u2248', 'thksim': '\u223C', 'thorn': '\xFE', 'THORN': '\xDE', 'tilde': '\u02DC', 'Tilde': '\u223C', 'TildeEqual': '\u2243', 'TildeFullEqual': '\u2245', 'TildeTilde': '\u2248', 'times': '\xD7', 'timesb': '\u22A0', 'timesbar': '\u2A31', 'timesd': '\u2A30', 'tint': '\u222D', 'toea': '\u2928', 'top': '\u22A4', 'topbot': '\u2336', 'topcir': '\u2AF1', 'topf': '\uD835\uDD65', 'Topf': '\uD835\uDD4B', 'topfork': '\u2ADA', 'tosa': '\u2929', 'tprime': '\u2034', 'trade': '\u2122', 'TRADE': '\u2122', 'triangle': '\u25B5', 'triangledown': '\u25BF', 'triangleleft': '\u25C3', 'trianglelefteq': '\u22B4', 'triangleq': '\u225C', 'triangleright': '\u25B9', 'trianglerighteq': '\u22B5', 'tridot': '\u25EC', 'trie': '\u225C', 'triminus': '\u2A3A', 'TripleDot': '\u20DB', 'triplus': '\u2A39', 'trisb': '\u29CD', 'tritime': '\u2A3B', 'trpezium': '\u23E2', 'tscr': '\uD835\uDCC9', 'Tscr': '\uD835\uDCAF', 'tscy': '\u0446', 'TScy': '\u0426', 'tshcy': '\u045B', 'TSHcy': '\u040B', 'tstrok': '\u0167', 'Tstrok': '\u0166', 'twixt': '\u226C', 'twoheadleftarrow': '\u219E', 'twoheadrightarrow': '\u21A0', 'uacute': '\xFA', 'Uacute': '\xDA', 'uarr': '\u2191', 'uArr': '\u21D1', 'Uarr': '\u219F', 'Uarrocir': '\u2949', 'ubrcy': '\u045E', 'Ubrcy': '\u040E', 'ubreve': '\u016D', 'Ubreve': '\u016C', 'ucirc': '\xFB', 'Ucirc': '\xDB', 'ucy': '\u0443', 'Ucy': '\u0423', 'udarr': '\u21C5', 'udblac': '\u0171', 'Udblac': '\u0170', 'udhar': '\u296E', 'ufisht': '\u297E', 'ufr': '\uD835\uDD32', 'Ufr': '\uD835\uDD18', 'ugrave': '\xF9', 'Ugrave': '\xD9', 'uHar': '\u2963', 'uharl': '\u21BF', 'uharr': '\u21BE', 'uhblk': '\u2580', 'ulcorn': '\u231C', 'ulcorner': '\u231C', 'ulcrop': '\u230F', 'ultri': '\u25F8', 'umacr': '\u016B', 'Umacr': '\u016A', 'uml': '\xA8', 'UnderBar': '_', 'UnderBrace': '\u23DF', 'UnderBracket': '\u23B5', 'UnderParenthesis': '\u23DD', 'Union': '\u22C3', 'UnionPlus': '\u228E', 'uogon': '\u0173', 'Uogon': '\u0172', 'uopf': '\uD835\uDD66', 'Uopf': '\uD835\uDD4C', 'uparrow': '\u2191', 'Uparrow': '\u21D1', 'UpArrow': '\u2191', 'UpArrowBar': '\u2912', 'UpArrowDownArrow': '\u21C5', 'updownarrow': '\u2195', 'Updownarrow': '\u21D5', 'UpDownArrow': '\u2195', 'UpEquilibrium': '\u296E', 'upharpoonleft': '\u21BF', 'upharpoonright': '\u21BE', 'uplus': '\u228E', 'UpperLeftArrow': '\u2196', 'UpperRightArrow': '\u2197', 'upsi': '\u03C5', 'Upsi': '\u03D2', 'upsih': '\u03D2', 'upsilon': '\u03C5', 'Upsilon': '\u03A5', 'UpTee': '\u22A5', 'UpTeeArrow': '\u21A5', 'upuparrows': '\u21C8', 'urcorn': '\u231D', 'urcorner': '\u231D', 'urcrop': '\u230E', 'uring': '\u016F', 'Uring': '\u016E', 'urtri': '\u25F9', 'uscr': '\uD835\uDCCA', 'Uscr': '\uD835\uDCB0', 'utdot': '\u22F0', 'utilde': '\u0169', 'Utilde': '\u0168', 'utri': '\u25B5', 'utrif': '\u25B4', 'uuarr': '\u21C8', 'uuml': '\xFC', 'Uuml': '\xDC', 'uwangle': '\u29A7', 'vangrt': '\u299C', 'varepsilon': '\u03F5', 'varkappa': '\u03F0', 'varnothing': '\u2205', 'varphi': '\u03D5', 'varpi': '\u03D6', 'varpropto': '\u221D', 'varr': '\u2195', 'vArr': '\u21D5', 'varrho': '\u03F1', 'varsigma': '\u03C2', 'varsubsetneq': '\u228A\uFE00', 'varsubsetneqq': '\u2ACB\uFE00', 'varsupsetneq': '\u228B\uFE00', 'varsupsetneqq': '\u2ACC\uFE00', 'vartheta': '\u03D1', 'vartriangleleft': '\u22B2', 'vartriangleright': '\u22B3', 'vBar': '\u2AE8', 'Vbar': '\u2AEB', 'vBarv': '\u2AE9', 'vcy': '\u0432', 'Vcy': '\u0412', 'vdash': '\u22A2', 'vDash': '\u22A8', 'Vdash': '\u22A9', 'VDash': '\u22AB', 'Vdashl': '\u2AE6', 'vee': '\u2228', 'Vee': '\u22C1', 'veebar': '\u22BB', 'veeeq': '\u225A', 'vellip': '\u22EE', 'verbar': '|', 'Verbar': '\u2016', 'vert': '|', 'Vert': '\u2016', 'VerticalBar': '\u2223', 'VerticalLine': '|', 'VerticalSeparator': '\u2758', 'VerticalTilde': '\u2240', 'VeryThinSpace': '\u200A', 'vfr': '\uD835\uDD33', 'Vfr': '\uD835\uDD19', 'vltri': '\u22B2', 'vnsub': '\u2282\u20D2', 'vnsup': '\u2283\u20D2', 'vopf': '\uD835\uDD67', 'Vopf': '\uD835\uDD4D', 'vprop': '\u221D', 'vrtri': '\u22B3', 'vscr': '\uD835\uDCCB', 'Vscr': '\uD835\uDCB1', 'vsubne': '\u228A\uFE00', 'vsubnE': '\u2ACB\uFE00', 'vsupne': '\u228B\uFE00', 'vsupnE': '\u2ACC\uFE00', 'Vvdash': '\u22AA', 'vzigzag': '\u299A', 'wcirc': '\u0175', 'Wcirc': '\u0174', 'wedbar': '\u2A5F', 'wedge': '\u2227', 'Wedge': '\u22C0', 'wedgeq': '\u2259', 'weierp': '\u2118', 'wfr': '\uD835\uDD34', 'Wfr': '\uD835\uDD1A', 'wopf': '\uD835\uDD68', 'Wopf': '\uD835\uDD4E', 'wp': '\u2118', 'wr': '\u2240', 'wreath': '\u2240', 'wscr': '\uD835\uDCCC', 'Wscr': '\uD835\uDCB2', 'xcap': '\u22C2', 'xcirc': '\u25EF', 'xcup': '\u22C3', 'xdtri': '\u25BD', 'xfr': '\uD835\uDD35', 'Xfr': '\uD835\uDD1B', 'xharr': '\u27F7', 'xhArr': '\u27FA', 'xi': '\u03BE', 'Xi': '\u039E', 'xlarr': '\u27F5', 'xlArr': '\u27F8', 'xmap': '\u27FC', 'xnis': '\u22FB', 'xodot': '\u2A00', 'xopf': '\uD835\uDD69', 'Xopf': '\uD835\uDD4F', 'xoplus': '\u2A01', 'xotime': '\u2A02', 'xrarr': '\u27F6', 'xrArr': '\u27F9', 'xscr': '\uD835\uDCCD', 'Xscr': '\uD835\uDCB3', 'xsqcup': '\u2A06', 'xuplus': '\u2A04', 'xutri': '\u25B3', 'xvee': '\u22C1', 'xwedge': '\u22C0', 'yacute': '\xFD', 'Yacute': '\xDD', 'yacy': '\u044F', 'YAcy': '\u042F', 'ycirc': '\u0177', 'Ycirc': '\u0176', 'ycy': '\u044B', 'Ycy': '\u042B', 'yen': '\xA5', 'yfr': '\uD835\uDD36', 'Yfr': '\uD835\uDD1C', 'yicy': '\u0457', 'YIcy': '\u0407', 'yopf': '\uD835\uDD6A', 'Yopf': '\uD835\uDD50', 'yscr': '\uD835\uDCCE', 'Yscr': '\uD835\uDCB4', 'yucy': '\u044E', 'YUcy': '\u042E', 'yuml': '\xFF', 'Yuml': '\u0178', 'zacute': '\u017A', 'Zacute': '\u0179', 'zcaron': '\u017E', 'Zcaron': '\u017D', 'zcy': '\u0437', 'Zcy': '\u0417', 'zdot': '\u017C', 'Zdot': '\u017B', 'zeetrf': '\u2128', 'ZeroWidthSpace': '\u200B', 'zeta': '\u03B6', 'Zeta': '\u0396', 'zfr': '\uD835\uDD37', 'Zfr': '\u2128', 'zhcy': '\u0436', 'ZHcy': '\u0416', 'zigrarr': '\u21DD', 'zopf': '\uD835\uDD6B', 'Zopf': '\u2124', 'zscr': '\uD835\uDCCF', 'Zscr': '\uD835\uDCB5', 'zwj': '\u200D', 'zwnj': '\u200C'}; + var decodeMapLegacy = {'aacute': '\xE1', 'Aacute': '\xC1', 'acirc': '\xE2', 'Acirc': '\xC2', 'acute': '\xB4', 'aelig': '\xE6', 'AElig': '\xC6', 'agrave': '\xE0', 'Agrave': '\xC0', 'amp': '&', 'AMP': '&', 'aring': '\xE5', 'Aring': '\xC5', 'atilde': '\xE3', 'Atilde': '\xC3', 'auml': '\xE4', 'Auml': '\xC4', 'brvbar': '\xA6', 'ccedil': '\xE7', 'Ccedil': '\xC7', 'cedil': '\xB8', 'cent': '\xA2', 'copy': '\xA9', 'COPY': '\xA9', 'curren': '\xA4', 'deg': '\xB0', 'divide': '\xF7', 'eacute': '\xE9', 'Eacute': '\xC9', 'ecirc': '\xEA', 'Ecirc': '\xCA', 'egrave': '\xE8', 'Egrave': '\xC8', 'eth': '\xF0', 'ETH': '\xD0', 'euml': '\xEB', 'Euml': '\xCB', 'frac12': '\xBD', 'frac14': '\xBC', 'frac34': '\xBE', 'gt': '>', 'GT': '>', 'iacute': '\xED', 'Iacute': '\xCD', 'icirc': '\xEE', 'Icirc': '\xCE', 'iexcl': '\xA1', 'igrave': '\xEC', 'Igrave': '\xCC', 'iquest': '\xBF', 'iuml': '\xEF', 'Iuml': '\xCF', 'laquo': '\xAB', 'lt': '<', 'LT': '<', 'macr': '\xAF', 'micro': '\xB5', 'middot': '\xB7', 'nbsp': '\xA0', 'not': '\xAC', 'ntilde': '\xF1', 'Ntilde': '\xD1', 'oacute': '\xF3', 'Oacute': '\xD3', 'ocirc': '\xF4', 'Ocirc': '\xD4', 'ograve': '\xF2', 'Ograve': '\xD2', 'ordf': '\xAA', 'ordm': '\xBA', 'oslash': '\xF8', 'Oslash': '\xD8', 'otilde': '\xF5', 'Otilde': '\xD5', 'ouml': '\xF6', 'Ouml': '\xD6', 'para': '\xB6', 'plusmn': '\xB1', 'pound': '\xA3', 'quot': '"', 'QUOT': '"', 'raquo': '\xBB', 'reg': '\xAE', 'REG': '\xAE', 'sect': '\xA7', 'shy': '\xAD', 'sup1': '\xB9', 'sup2': '\xB2', 'sup3': '\xB3', 'szlig': '\xDF', 'thorn': '\xFE', 'THORN': '\xDE', 'times': '\xD7', 'uacute': '\xFA', 'Uacute': '\xDA', 'ucirc': '\xFB', 'Ucirc': '\xDB', 'ugrave': '\xF9', 'Ugrave': '\xD9', 'uml': '\xA8', 'uuml': '\xFC', 'Uuml': '\xDC', 'yacute': '\xFD', 'Yacute': '\xDD', 'yen': '\xA5', 'yuml': '\xFF'}; + var decodeMapNumeric = {'0': '\uFFFD', '128': '\u20AC', '130': '\u201A', '131': '\u0192', '132': '\u201E', '133': '\u2026', '134': '\u2020', '135': '\u2021', '136': '\u02C6', '137': '\u2030', '138': '\u0160', '139': '\u2039', '140': '\u0152', '142': '\u017D', '145': '\u2018', '146': '\u2019', '147': '\u201C', '148': '\u201D', '149': '\u2022', '150': '\u2013', '151': '\u2014', '152': '\u02DC', '153': '\u2122', '154': '\u0161', '155': '\u203A', '156': '\u0153', '158': '\u017E', '159': '\u0178'}; + var invalidReferenceCodePoints = [1, 2, 3, 4, 5, 6, 7, 8, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 64976, 64977, 64978, 64979, 64980, 64981, 64982, 64983, 64984, 64985, 64986, 64987, 64988, 64989, 64990, 64991, 64992, 64993, 64994, 64995, 64996, 64997, 64998, 64999, 65000, 65001, 65002, 65003, 65004, 65005, 65006, 65007, 65534, 65535, 131070, 131071, 196606, 196607, 262142, 262143, 327678, 327679, 393214, 393215, 458750, 458751, 524286, 524287, 589822, 589823, 655358, 655359, 720894, 720895, 786430, 786431, 851966, 851967, 917502, 917503, 983038, 983039, 1048574, 1048575, 1114110, 1114111]; + + /* -------------------------------------------------------------------------- */ + + var stringFromCharCode = String.fromCharCode; + + var object = {}; + var hasOwnProperty = object.hasOwnProperty; + var has = function (object, propertyName) { + return hasOwnProperty.call(object, propertyName); + }; + + var contains = function (array, value) { + var index = -1; + var length = array.length; + while (++index < length) { + if (array[index] == value) { + return true; + } + } + return false; + }; + + var merge = function (options, defaults) { + if (!options) { + return defaults; + } + var result = {}; + var key; + for (key in defaults) { + // A `hasOwnProperty` check is not needed here, since only recognized + // option names are used anyway. Any others are ignored. + result[key] = has(options, key) ? options[key] : defaults[key]; + } + return result; + }; + + // Modified version of `ucs2encode`; see https://mths.be/punycode. + var codePointToSymbol = function (codePoint, strict) { + var output = ''; + if ((codePoint >= 0xD800 && codePoint <= 0xDFFF) || codePoint > 0x10FFFF) { + // See issue #4: + // “Otherwise, if the number is in the range 0xD800 to 0xDFFF or is + // greater than 0x10FFFF, then this is a parse error. Return a U+FFFD + // REPLACEMENT CHARACTER.” + if (strict) { + parseError('character reference outside the permissible Unicode range'); + } + return '\uFFFD'; + } + if (has(decodeMapNumeric, codePoint)) { + if (strict) { + parseError('disallowed character reference'); + } + return decodeMapNumeric[codePoint]; + } + if (strict && contains(invalidReferenceCodePoints, codePoint)) { + parseError('disallowed character reference'); + } + if (codePoint > 0xFFFF) { + codePoint -= 0x10000; + output += stringFromCharCode(codePoint >>> 10 & 0x3FF | 0xD800); + codePoint = 0xDC00 | codePoint & 0x3FF; + } + output += stringFromCharCode(codePoint); + return output; + }; + + var hexEscape = function (codePoint) { + return '&#x' + codePoint.toString(16).toUpperCase() + ';'; + }; + + var decEscape = function (codePoint) { + return '&#' + codePoint + ';'; + }; + + var parseError = function (message) { + throw Error('Parse error: ' + message); + }; + + /* -------------------------------------------------------------------------- */ + + var encode = function (string, options) { + options = merge(options, encode.options); + var strict = options.strict; + if (strict && regexInvalidRawCodePoint.test(string)) { + parseError('forbidden code point'); + } + var encodeEverything = options.encodeEverything; + var useNamedReferences = options.useNamedReferences; + var allowUnsafeSymbols = options.allowUnsafeSymbols; + var escapeCodePoint = options.decimal ? decEscape : hexEscape; + + var escapeBmpSymbol = function (symbol) { + return escapeCodePoint(symbol.charCodeAt(0)); + }; + + if (encodeEverything) { + // Encode ASCII symbols. + string = string.replace(regexAsciiWhitelist, function (symbol) { + // Use named references if requested & possible. + if (useNamedReferences && has(encodeMap, symbol)) { + return '&' + encodeMap[symbol] + ';'; + } + return escapeBmpSymbol(symbol); + }); + // Shorten a few escapes that represent two symbols, of which at least one + // is within the ASCII range. + if (useNamedReferences) { + string = string + .replace(/>\u20D2/g, '>⃒') + .replace(/<\u20D2/g, '<⃒') + .replace(/fj/g, 'fj'); + } + // Encode non-ASCII symbols. + if (useNamedReferences) { + // Encode non-ASCII symbols that can be replaced with a named reference. + string = string.replace(regexEncodeNonAscii, function (string) { + // Note: there is no need to check `has(encodeMap, string)` here. + return '&' + encodeMap[string] + ';'; + }); + } + // Note: any remaining non-ASCII symbols are handled outside of the `if`. + } else if (useNamedReferences) { + // Apply named character references. + // Encode `<>"'&` using named character references. + if (!allowUnsafeSymbols) { + string = string.replace(regexEscape, function (string) { + return '&' + encodeMap[string] + ';'; // no need to check `has()` here + }); + } + // Shorten escapes that represent two symbols, of which at least one is + // `<>"'&`. + string = string + .replace(/>\u20D2/g, '>⃒') + .replace(/<\u20D2/g, '<⃒'); + // Encode non-ASCII symbols that can be replaced with a named reference. + string = string.replace(regexEncodeNonAscii, function (string) { + // Note: there is no need to check `has(encodeMap, string)` here. + return '&' + encodeMap[string] + ';'; + }); + } else if (!allowUnsafeSymbols) { + // Encode `<>"'&` using hexadecimal escapes, now that they’re not handled + // using named character references. + string = string.replace(regexEscape, escapeBmpSymbol); + } + return string + // Encode astral symbols. + .replace(regexAstralSymbols, function ($0) { + // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae + var high = $0.charCodeAt(0); + var low = $0.charCodeAt(1); + var codePoint = (high - 0xD800) * 0x400 + low - 0xDC00 + 0x10000; + return escapeCodePoint(codePoint); +}) + // Encode any remaining BMP symbols that are not printable ASCII symbols + // using a hexadecimal escape. + .replace(regexBmpWhitelist, escapeBmpSymbol); + }; + // Expose default options (so they can be overridden globally). + encode.options = { + 'allowUnsafeSymbols': false, + 'encodeEverything': false, + 'strict': false, + 'useNamedReferences': false, + 'decimal': false + }; + + var decode = function (html, options) { + options = merge(options, decode.options); + var strict = options.strict; + if (strict && regexInvalidEntity.test(html)) { + parseError('malformed character reference'); + } + return html.replace(regexDecode, function ($0, $1, $2, $3, $4, $5, $6, $7, $8) { + var codePoint; + var semicolon; + var decDigits; + var hexDigits; + var reference; + var next; + + if ($1) { + reference = $1; + // Note: there is no need to check `has(decodeMap, reference)`. + return decodeMap[reference]; + } + + if ($2) { + // Decode named character references without trailing `;`, e.g. `&`. + // This is only a parse error if it gets converted to `&`, or if it is + // followed by `=` in an attribute context. + reference = $2; + next = $3; + if (next && options.isAttributeValue) { + if (strict && next == '=') { + parseError('`&` did not start a character reference'); + } + return $0; + } else { + if (strict) { + parseError( + 'named character reference was not terminated by a semicolon' + ); + } + // Note: there is no need to check `has(decodeMapLegacy, reference)`. + return decodeMapLegacy[reference] + (next || ''); + } + } + + if ($4) { + // Decode decimal escapes, e.g. `𝌆`. + decDigits = $4; + semicolon = $5; + if (strict && !semicolon) { + parseError('character reference was not terminated by a semicolon'); + } + codePoint = parseInt(decDigits, 10); + return codePointToSymbol(codePoint, strict); + } + + if ($6) { + // Decode hexadecimal escapes, e.g. `𝌆`. + hexDigits = $6; + semicolon = $7; + if (strict && !semicolon) { + parseError('character reference was not terminated by a semicolon'); + } + codePoint = parseInt(hexDigits, 16); + return codePointToSymbol(codePoint, strict); + } + + // If we’re still here, `if ($7)` is implied; it’s an ambiguous + // ampersand for sure. https://mths.be/notes/ambiguous-ampersands + if (strict) { + parseError( + 'named character reference was not terminated by a semicolon' + ); + } + return $0; + }); + }; + // Expose default options (so they can be overridden globally). + decode.options = { + 'isAttributeValue': false, + 'strict': false + }; + + var escape = function (string) { + return string.replace(regexEscape, function ($0) { + // Note: there is no need to check `has(escapeMap, $0)` here. + return escapeMap[$0]; + }); + }; + + /* -------------------------------------------------------------------------- */ + + var he = { + 'version': '1.2.0', + 'encode': encode, + 'decode': decode, + 'escape': escape, + 'unescape': decode + }; + + // Some AMD build optimizers, like r.js, check for specific condition patterns + // like the following: + if ( + false + ) { + define(function () { + return he; + }); + } else if (freeExports && !freeExports.nodeType) { + if (freeModule) { // in Node.js, io.js, or RingoJS v0.8.0+ + freeModule.exports = he; + } else { // in Narwhal or RingoJS v0.7.0- + for (var key in he) { + has(he, key) && (freeExports[key] = he[key]); + } + } + } else { // in Rhino or a web browser + root.he = he; + } + + }(this)); + + }).call(this, typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}); + }, {}], + 55: [function (require, module, exports) { + exports.read = function (buffer, offset, isLE, mLen, nBytes) { + var e, m; + var eLen = (nBytes * 8) - mLen - 1; + var eMax = (1 << eLen) - 1; + var eBias = eMax >> 1; + var nBits = -7; + var i = isLE ? (nBytes - 1) : 0; + var d = isLE ? -1 : 1; + var s = buffer[offset + i]; + + i += d; + + e = s & ((1 << (-nBits)) - 1); + s >>= (-nBits); + nBits += eLen; + for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {} + + m = e & ((1 << (-nBits)) - 1); + e >>= (-nBits); + nBits += mLen; + for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {} + + if (e === 0) { + e = 1 - eBias; + } else if (e === eMax) { + return m ? NaN : ((s ? -1 : 1) * Infinity); + } else { + m = m + Math.pow(2, mLen); + e = e - eBias; + } + return (s ? -1 : 1) * m * Math.pow(2, e - mLen); + }; + + exports.write = function (buffer, value, offset, isLE, mLen, nBytes) { + var e, m, c; + var eLen = (nBytes * 8) - mLen - 1; + var eMax = (1 << eLen) - 1; + var eBias = eMax >> 1; + var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0); + var i = isLE ? 0 : (nBytes - 1); + var d = isLE ? 1 : -1; + var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0; + + value = Math.abs(value); + + if (isNaN(value) || value === Infinity) { + m = isNaN(value) ? 1 : 0; + e = eMax; + } else { + e = Math.floor(Math.log(value) / Math.LN2); + if (value * (c = Math.pow(2, -e)) < 1) { + e--; + c *= 2; + } + if (e + eBias >= 1) { + value += rt / c; + } else { + value += rt * Math.pow(2, 1 - eBias); + } + if (value * c >= 2) { + e++; + c /= 2; + } + + if (e + eBias >= eMax) { + m = 0; + e = eMax; + } else if (e + eBias >= 1) { + m = ((value * c) - 1) * Math.pow(2, mLen); + e = e + eBias; + } else { + m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen); + e = 0; + } + } + + for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {} + + e = (e << mLen) | m; + eLen += mLen; + for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {} + + buffer[offset + i - d] |= s * 128; + }; + + }, {}], + 56: [function (require, module, exports) { + if (typeof Object.create === 'function') { + // implementation from standard node.js 'util' module + module.exports = function inherits (ctor, superCtor) { + ctor.super_ = superCtor; + ctor.prototype = Object.create(superCtor.prototype, { + constructor: { + value: ctor, + enumerable: false, + writable: true, + configurable: true + } + }); + }; + } else { + // old school shim for old browsers + module.exports = function inherits (ctor, superCtor) { + ctor.super_ = superCtor; + var TempCtor = function () {}; + TempCtor.prototype = superCtor.prototype; + ctor.prototype = new TempCtor(); + ctor.prototype.constructor = ctor; + }; + } + + }, {}], + 57: [function (require, module, exports) { +/*! + * Determine if an object is a Buffer + * + * @author Feross Aboukhadijeh + * @license MIT + */ + +// The _isBuffer check is for Safari 5-7 support, because it's missing +// Object.prototype.constructor. Remove this eventually + module.exports = function (obj) { + return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer); + }; + + function isBuffer (obj) { + return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj); + } + +// For Node v0.10 support. Remove this eventually. + function isSlowBuffer (obj) { + return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0)); + } + + }, {}], + 58: [function (require, module, exports) { + var toString = {}.toString; + + module.exports = Array.isArray || function (arr) { + return toString.call(arr) == '[object Array]'; + }; + + }, {}], + 59: [function (require, module, exports) { + (function (process) { + var path = require('path'); + var fs = require('fs'); + var _0777 = parseInt('0777', 8); + + module.exports = mkdirP.mkdirp = mkdirP.mkdirP = mkdirP; + + function mkdirP (p, opts, f, made) { + if (typeof opts === 'function') { + f = opts; + opts = {}; + } else if (!opts || typeof opts !== 'object') { + opts = { mode: opts }; + } + + var mode = opts.mode; + var xfs = opts.fs || fs; + + if (mode === undefined) { + mode = _0777 & (~process.umask()); + } + if (!made) made = null; + + var cb = f || function () {}; + p = path.resolve(p); + + xfs.mkdir(p, mode, function (er) { + if (!er) { + made = made || p; + return cb(null, made); + } + switch (er.code) { + case 'ENOENT': + mkdirP(path.dirname(p), opts, function (er, made) { + if (er) cb(er, made); + else mkdirP(p, opts, cb, made); + }); + break; + + // In the case of any other error, just see if there's a dir + // there already. If so, then hooray! If not, then something + // is borked. + default: + xfs.stat(p, function (er2, stat) { + // if the stat fails, then that's super weird. + // let the original error be the failure reason. + if (er2 || !stat.isDirectory()) cb(er, made); + else cb(null, made); + }); + break; + } + }); + } + + mkdirP.sync = function sync (p, opts, made) { + if (!opts || typeof opts !== 'object') { + opts = { mode: opts }; + } + + var mode = opts.mode; + var xfs = opts.fs || fs; + + if (mode === undefined) { + mode = _0777 & (~process.umask()); + } + if (!made) made = null; + + p = path.resolve(p); + + try { + xfs.mkdirSync(p, mode); + made = made || p; + } catch (err0) { + switch (err0.code) { + case 'ENOENT' : + made = sync(path.dirname(p), opts, made); + sync(p, opts, made); + break; + + // In the case of any other error, just see if there's a dir + // there already. If so, then hooray! If not, then something + // is borked. + default: + var stat; + try { + stat = xfs.statSync(p); + } catch (err1) { + throw err0; + } + if (!stat.isDirectory()) throw err0; + break; + } + } + + return made; + }; + + }).call(this, require('_process')); + }, {'_process': 70, 'fs': 42, 'path': 68}], + 60: [function (require, module, exports) { +/** + * Helpers. + */ + + var s = 1000; + var m = s * 60; + var h = m * 60; + var d = h * 24; + var w = d * 7; + var y = d * 365.25; + +/** + * Parse or format the given `val`. + * + * Options: + * + * - `long` verbose formatting [false] + * + * @param {String|Number} val + * @param {Object} [options] + * @throws {Error} throw an error if val is not a non-empty string or a number + * @return {String|Number} + * @api public + */ + + module.exports = function (val, options) { + options = options || {}; + var type = typeof val; + if (type === 'string' && val.length > 0) { + return parse(val); + } else if (type === 'number' && isNaN(val) === false) { + return options.long ? fmtLong(val) : fmtShort(val); + } + throw new Error( + 'val is not a non-empty string or a valid number. val=' + + JSON.stringify(val) + ); + }; + +/** + * Parse the given `str` and return milliseconds. + * + * @param {String} str + * @return {Number} + * @api private + */ + + function parse (str) { + str = String(str); + if (str.length > 100) { + return; + } + var match = /^((?:\d+)?\-?\d?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec( + str + ); + if (!match) { + return; + } + var n = parseFloat(match[1]); + var type = (match[2] || 'ms').toLowerCase(); + switch (type) { + case 'years': + case 'year': + case 'yrs': + case 'yr': + case 'y': + return n * y; + case 'weeks': + case 'week': + case 'w': + return n * w; + case 'days': + case 'day': + case 'd': + return n * d; + case 'hours': + case 'hour': + case 'hrs': + case 'hr': + case 'h': + return n * h; + case 'minutes': + case 'minute': + case 'mins': + case 'min': + case 'm': + return n * m; + case 'seconds': + case 'second': + case 'secs': + case 'sec': + case 's': + return n * s; + case 'milliseconds': + case 'millisecond': + case 'msecs': + case 'msec': + case 'ms': + return n; + default: + return undefined; + } + } + +/** + * Short format for `ms`. + * + * @param {Number} ms + * @return {String} + * @api private + */ + + function fmtShort (ms) { + var msAbs = Math.abs(ms); + if (msAbs >= d) { + return Math.round(ms / d) + 'd'; + } + if (msAbs >= h) { + return Math.round(ms / h) + 'h'; + } + if (msAbs >= m) { + return Math.round(ms / m) + 'm'; + } + if (msAbs >= s) { + return Math.round(ms / s) + 's'; + } + return ms + 'ms'; + } + +/** + * Long format for `ms`. + * + * @param {Number} ms + * @return {String} + * @api private + */ + + function fmtLong (ms) { + var msAbs = Math.abs(ms); + if (msAbs >= d) { + return plural(ms, msAbs, d, 'day'); + } + if (msAbs >= h) { + return plural(ms, msAbs, h, 'hour'); + } + if (msAbs >= m) { + return plural(ms, msAbs, m, 'minute'); + } + if (msAbs >= s) { + return plural(ms, msAbs, s, 'second'); + } + return ms + ' ms'; + } + +/** + * Pluralization helper. + */ + + function plural (ms, msAbs, n, name) { + var isPlural = msAbs >= n * 1.5; + return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : ''); + } + + }, {}], + 61: [function (require, module, exports) { + 'use strict'; + + var keysShim; + if (!Object.keys) { + // modified from https://github.com/es-shims/es5-shim + var has = Object.prototype.hasOwnProperty; + var toStr = Object.prototype.toString; + var isArgs = require('./isArguments'); // eslint-disable-line global-require + var isEnumerable = Object.prototype.propertyIsEnumerable; + var hasDontEnumBug = !isEnumerable.call({ toString: null }, 'toString'); + var hasProtoEnumBug = isEnumerable.call(function () {}, 'prototype'); + var dontEnums = [ + 'toString', + 'toLocaleString', + 'valueOf', + 'hasOwnProperty', + 'isPrototypeOf', + 'propertyIsEnumerable', + 'constructor' + ]; + var equalsConstructorPrototype = function (o) { + var ctor = o.constructor; + return ctor && ctor.prototype === o; + }; + var excludedKeys = { + $applicationCache: true, + $console: true, + $external: true, + $frame: true, + $frameElement: true, + $frames: true, + $innerHeight: true, + $innerWidth: true, + $outerHeight: true, + $outerWidth: true, + $pageXOffset: true, + $pageYOffset: true, + $parent: true, + $scrollLeft: true, + $scrollTop: true, + $scrollX: true, + $scrollY: true, + $self: true, + $webkitIndexedDB: true, + $webkitStorageInfo: true, + $window: true + }; + var hasAutomationEqualityBug = (function () { + /* global window */ + if (typeof window === 'undefined') { return false; } + for (var k in window) { + try { + if (!excludedKeys['$' + k] && has.call(window, k) && window[k] !== null && typeof window[k] === 'object') { + try { + equalsConstructorPrototype(window[k]); + } catch (e) { + return true; + } + } + } catch (e) { + return true; + } + } + return false; + }()); + var equalsConstructorPrototypeIfNotBuggy = function (o) { + /* global window */ + if (typeof window === 'undefined' || !hasAutomationEqualityBug) { + return equalsConstructorPrototype(o); + } + try { + return equalsConstructorPrototype(o); + } catch (e) { + return false; + } + }; + + keysShim = function keys (object) { + var isObject = object !== null && typeof object === 'object'; + var isFunction = toStr.call(object) === '[object Function]'; + var isArguments = isArgs(object); + var isString = isObject && toStr.call(object) === '[object String]'; + var theKeys = []; + + if (!isObject && !isFunction && !isArguments) { + throw new TypeError('Object.keys called on a non-object'); + } + + var skipProto = hasProtoEnumBug && isFunction; + if (isString && object.length > 0 && !has.call(object, 0)) { + for (var i = 0; i < object.length; ++i) { + theKeys.push(String(i)); + } + } + + if (isArguments && object.length > 0) { + for (var j = 0; j < object.length; ++j) { + theKeys.push(String(j)); + } + } else { + for (var name in object) { + if (!(skipProto && name === 'prototype') && has.call(object, name)) { + theKeys.push(String(name)); + } + } + } + + if (hasDontEnumBug) { + var skipConstructor = equalsConstructorPrototypeIfNotBuggy(object); + + for (var k = 0; k < dontEnums.length; ++k) { + if (!(skipConstructor && dontEnums[k] === 'constructor') && has.call(object, dontEnums[k])) { + theKeys.push(dontEnums[k]); + } + } + } + return theKeys; + }; + } + module.exports = keysShim; + + }, {'./isArguments': 63}], + 62: [function (require, module, exports) { + 'use strict'; + + var slice = Array.prototype.slice; + var isArgs = require('./isArguments'); + + var origKeys = Object.keys; + var keysShim = origKeys ? function keys (o) { return origKeys(o); } : require('./implementation'); + + var originalKeys = Object.keys; + + keysShim.shim = function shimObjectKeys () { + if (Object.keys) { + var keysWorksWithArguments = (function () { + // Safari 5.0 bug + var args = Object.keys(arguments); + return args && args.length === arguments.length; + }(1, 2)); + if (!keysWorksWithArguments) { + Object.keys = function keys (object) { // eslint-disable-line func-name-matching + if (isArgs(object)) { + return originalKeys(slice.call(object)); + } + return originalKeys(object); + }; + } + } else { + Object.keys = keysShim; + } + return Object.keys || keysShim; + }; + + module.exports = keysShim; + + }, {'./implementation': 61, './isArguments': 63}], + 63: [function (require, module, exports) { + 'use strict'; + + var toStr = Object.prototype.toString; + + module.exports = function isArguments (value) { + var str = toStr.call(value); + var isArgs = str === '[object Arguments]'; + if (!isArgs) { + isArgs = str !== '[object Array]' && + value !== null && + typeof value === 'object' && + typeof value.length === 'number' && + value.length >= 0 && + toStr.call(value.callee) === '[object Function]'; + } + return isArgs; + }; + + }, {}], + 64: [function (require, module, exports) { + 'use strict'; + +// modified from https://github.com/es-shims/es6-shim + var keys = require('object-keys'); + var bind = require('function-bind'); + var canBeObject = function (obj) { + return typeof obj !== 'undefined' && obj !== null; + }; + var hasSymbols = require('has-symbols/shams')(); + var toObject = Object; + var push = bind.call(Function.call, Array.prototype.push); + var propIsEnumerable = bind.call(Function.call, Object.prototype.propertyIsEnumerable); + var originalGetSymbols = hasSymbols ? Object.getOwnPropertySymbols : null; + + module.exports = function assign (target, source1) { + if (!canBeObject(target)) { throw new TypeError('target must be an object'); } + var objTarget = toObject(target); + var s, source, i, props, syms, value, key; + for (s = 1; s < arguments.length; ++s) { + source = toObject(arguments[s]); + props = keys(source); + var getSymbols = hasSymbols && (Object.getOwnPropertySymbols || originalGetSymbols); + if (getSymbols) { + syms = getSymbols(source); + for (i = 0; i < syms.length; ++i) { + key = syms[i]; + if (propIsEnumerable(source, key)) { + push(props, key); + } + } + } + for (i = 0; i < props.length; ++i) { + key = props[i]; + value = source[key]; + if (propIsEnumerable(source, key)) { + objTarget[key] = value; + } + } + } + return objTarget; + }; + + }, {'function-bind': 52, 'has-symbols/shams': 53, 'object-keys': 62}], + 65: [function (require, module, exports) { + 'use strict'; + + var defineProperties = require('define-properties'); + + var implementation = require('./implementation'); + var getPolyfill = require('./polyfill'); + var shim = require('./shim'); + + var polyfill = getPolyfill(); + + defineProperties(polyfill, { + getPolyfill: getPolyfill, + implementation: implementation, + shim: shim + }); + + module.exports = polyfill; + + }, {'./implementation': 64, './polyfill': 66, './shim': 67, 'define-properties': 47}], + 66: [function (require, module, exports) { + 'use strict'; + + var implementation = require('./implementation'); + + var lacksProperEnumerationOrder = function () { + if (!Object.assign) { + return false; + } + // v8, specifically in node 4.x, has a bug with incorrect property enumeration order + // note: this does not detect the bug unless there's 20 characters + var str = 'abcdefghijklmnopqrst'; + var letters = str.split(''); + var map = {}; + for (var i = 0; i < letters.length; ++i) { + map[letters[i]] = letters[i]; + } + var obj = Object.assign({}, map); + var actual = ''; + for (var k in obj) { + actual += k; + } + return str !== actual; + }; + + var assignHasPendingExceptions = function () { + if (!Object.assign || !Object.preventExtensions) { + return false; + } + // Firefox 37 still has "pending exception" logic in its Object.assign implementation, + // which is 72% slower than our shim, and Firefox 40's native implementation. + var thrower = Object.preventExtensions({ 1: 2 }); + try { + Object.assign(thrower, 'xy'); + } catch (e) { + return thrower[1] === 'y'; + } + return false; + }; + + module.exports = function getPolyfill () { + if (!Object.assign) { + return implementation; + } + if (lacksProperEnumerationOrder()) { + return implementation; + } + if (assignHasPendingExceptions()) { + return implementation; + } + return Object.assign; + }; + + }, {'./implementation': 64}], + 67: [function (require, module, exports) { + 'use strict'; + + var define = require('define-properties'); + var getPolyfill = require('./polyfill'); + + module.exports = function shimAssign () { + var polyfill = getPolyfill(); + define( + Object, + { assign: polyfill }, + { assign: function () { return Object.assign !== polyfill; } } + ); + return polyfill; + }; + + }, {'./polyfill': 66, 'define-properties': 47}], + 68: [function (require, module, exports) { + (function (process) { +// .dirname, .basename, and .extname methods are extracted from Node.js v8.11.1, +// backported and transplited with Babel, with backwards-compat fixes + +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// resolves . and .. elements in a path array with directory names there +// must be no slashes, empty elements, or device names (c:\) in the array +// (so also no leading and trailing slashes - it does not distinguish +// relative and absolute paths) + function normalizeArray (parts, allowAboveRoot) { + // if the path tries to go above the root, `up` ends up > 0 + var up = 0; + for (var i = parts.length - 1; i >= 0; i--) { + var last = parts[i]; + if (last === '.') { + parts.splice(i, 1); + } else if (last === '..') { + parts.splice(i, 1); + up++; + } else if (up) { + parts.splice(i, 1); + up--; + } + } + + // if the path is allowed to go above the root, restore leading ..s + if (allowAboveRoot) { + for (; up--; up) { + parts.unshift('..'); + } + } + + return parts; + } + +// path.resolve([from ...], to) +// posix version + exports.resolve = function () { + var resolvedPath = '', + resolvedAbsolute = false; + + for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) { + var path = (i >= 0) ? arguments[i] : process.cwd(); + + // Skip empty and invalid entries + if (typeof path !== 'string') { + throw new TypeError('Arguments to path.resolve must be strings'); + } else if (!path) { + continue; + } + + resolvedPath = path + '/' + resolvedPath; + resolvedAbsolute = path.charAt(0) === '/'; + } + + // At this point the path should be resolved to a full absolute path, but + // handle relative paths to be safe (might happen when process.cwd() fails) + + // Normalize the path + resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function (p) { + return !!p; + }), !resolvedAbsolute).join('/'); + + return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.'; + }; + +// path.normalize(path) +// posix version + exports.normalize = function (path) { + var isAbsolute = exports.isAbsolute(path), + trailingSlash = substr(path, -1) === '/'; + + // Normalize the path + path = normalizeArray(filter(path.split('/'), function (p) { + return !!p; + }), !isAbsolute).join('/'); + + if (!path && !isAbsolute) { + path = '.'; + } + if (path && trailingSlash) { + path += '/'; + } + + return (isAbsolute ? '/' : '') + path; + }; + +// posix version + exports.isAbsolute = function (path) { + return path.charAt(0) === '/'; + }; + +// posix version + exports.join = function () { + var paths = Array.prototype.slice.call(arguments, 0); + return exports.normalize(filter(paths, function (p, index) { + if (typeof p !== 'string') { + throw new TypeError('Arguments to path.join must be strings'); + } + return p; + }).join('/')); + }; + +// path.relative(from, to) +// posix version + exports.relative = function (from, to) { + from = exports.resolve(from).substr(1); + to = exports.resolve(to).substr(1); + + function trim (arr) { + var start = 0; + for (; start < arr.length; start++) { + if (arr[start] !== '') break; + } + + var end = arr.length - 1; + for (; end >= 0; end--) { + if (arr[end] !== '') break; + } + + if (start > end) return []; + return arr.slice(start, end - start + 1); + } + + var fromParts = trim(from.split('/')); + var toParts = trim(to.split('/')); + + var length = Math.min(fromParts.length, toParts.length); + var samePartsLength = length; + for (var i = 0; i < length; i++) { + if (fromParts[i] !== toParts[i]) { + samePartsLength = i; + break; + } + } + + var outputParts = []; + for (var i = samePartsLength; i < fromParts.length; i++) { + outputParts.push('..'); + } + + outputParts = outputParts.concat(toParts.slice(samePartsLength)); + + return outputParts.join('/'); + }; + + exports.sep = '/'; + exports.delimiter = ':'; + + exports.dirname = function (path) { + if (typeof path !== 'string') path = path + ''; + if (path.length === 0) return '.'; + var code = path.charCodeAt(0); + var hasRoot = code === 47; + var end = -1; + var matchedSlash = true; + for (var i = path.length - 1; i >= 1; --i) { + code = path.charCodeAt(i); + if (code === 47 /* / */) { + if (!matchedSlash) { + end = i; + break; + } + } else { + // We saw the first non-path separator + matchedSlash = false; + } + } + + if (end === -1) return hasRoot ? '/' : '.'; + if (hasRoot && end === 1) { + // return '//'; + // Backwards-compat fix: + return '/'; + } + return path.slice(0, end); + }; + + function basename (path) { + if (typeof path !== 'string') path = path + ''; + + var start = 0; + var end = -1; + var matchedSlash = true; + var i; + + for (i = path.length - 1; i >= 0; --i) { + if (path.charCodeAt(i) === 47 /* / */) { + // If we reached a path separator that was not part of a set of path + // separators at the end of the string, stop now + if (!matchedSlash) { + start = i + 1; + break; + } + } else if (end === -1) { + // We saw the first non-path separator, mark this as the end of our + // path component + matchedSlash = false; + end = i + 1; + } + } + + if (end === -1) return ''; + return path.slice(start, end); + } + +// Uses a mixed approach for backwards-compatibility, as ext behavior changed +// in new Node.js versions, so only basename() above is backported here + exports.basename = function (path, ext) { + var f = basename(path); + if (ext && f.substr(-1 * ext.length) === ext) { + f = f.substr(0, f.length - ext.length); + } + return f; + }; + + exports.extname = function (path) { + if (typeof path !== 'string') path = path + ''; + var startDot = -1; + var startPart = 0; + var end = -1; + var matchedSlash = true; + // Track the state of characters (if any) we see before our first dot and + // after any path separator we find + var preDotState = 0; + for (var i = path.length - 1; i >= 0; --i) { + var code = path.charCodeAt(i); + if (code === 47 /* / */) { + // If we reached a path separator that was not part of a set of path + // separators at the end of the string, stop now + if (!matchedSlash) { + startPart = i + 1; + break; + } + continue; + } + if (end === -1) { + // We saw the first non-path separator, mark this as the end of our + // extension + matchedSlash = false; + end = i + 1; + } + if (code === 46 /* . */) { + // If this is our first dot, mark it as the start of our extension + if (startDot === -1) { startDot = i; } else if (preDotState !== 1) { preDotState = 1; } + } else if (startDot !== -1) { + // We saw a non-dot and non-path separator before our dot, so we should + // have a good chance at having a non-empty extension + preDotState = -1; + } + } + + if (startDot === -1 || end === -1 || + // We saw a non-dot character immediately before the dot + preDotState === 0 || + // The (right-most) trimmed path component is exactly '..' + preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) { + return ''; + } + return path.slice(startDot, end); + }; + + function filter (xs, f) { + if (xs.filter) return xs.filter(f); + var res = []; + for (var i = 0; i < xs.length; i++) { + if (f(xs[i], i, xs)) res.push(xs[i]); + } + return res; + } + +// String.prototype.substr - negative index don't work in IE8 + var substr = 'ab'.substr(-1) === 'b' + ? function (str, start, len) { return str.substr(start, len); } + : function (str, start, len) { + if (start < 0) start = str.length + start; + return str.substr(start, len); + } +; + + }).call(this, require('_process')); + }, {'_process': 70}], + 69: [function (require, module, exports) { + (function (process) { + 'use strict'; + + if (!process.version || + process.version.indexOf('v0.') === 0 || + process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) { + module.exports = { nextTick: nextTick }; + } else { + module.exports = process; + } + + function nextTick (fn, arg1, arg2, arg3) { + if (typeof fn !== 'function') { + throw new TypeError('"callback" argument must be a function'); + } + var len = arguments.length; + var args, i; + switch (len) { + case 0: + case 1: + return process.nextTick(fn); + case 2: + return process.nextTick(function afterTickOne () { + fn.call(null, arg1); + }); + case 3: + return process.nextTick(function afterTickTwo () { + fn.call(null, arg1, arg2); + }); + case 4: + return process.nextTick(function afterTickThree () { + fn.call(null, arg1, arg2, arg3); + }); + default: + args = new Array(len - 1); + i = 0; + while (i < args.length) { + args[i++] = arguments[i]; + } + return process.nextTick(function afterTick () { + fn.apply(null, args); + }); + } + } + + }).call(this, require('_process')); + }, {'_process': 70}], + 70: [function (require, module, exports) { +// shim for using process in browser + var process = module.exports = {}; + +// cached from whatever global is present so that test runners that stub it +// don't break things. But we need to wrap it in a try catch in case it is +// wrapped in strict mode code which doesn't define any globals. It's inside a +// function because try/catches deoptimize in certain engines. + + var cachedSetTimeout; + var cachedClearTimeout; + + function defaultSetTimout () { + throw new Error('setTimeout has not been defined'); + } + function defaultClearTimeout () { + throw new Error('clearTimeout has not been defined'); + } + (function () { + try { + if (typeof setTimeout === 'function') { + cachedSetTimeout = setTimeout; + } else { + cachedSetTimeout = defaultSetTimout; + } + } catch (e) { + cachedSetTimeout = defaultSetTimout; + } + try { + if (typeof clearTimeout === 'function') { + cachedClearTimeout = clearTimeout; + } else { + cachedClearTimeout = defaultClearTimeout; + } + } catch (e) { + cachedClearTimeout = defaultClearTimeout; + } + }()); + function runTimeout (fun) { + if (cachedSetTimeout === setTimeout) { + // normal enviroments in sane situations + return setTimeout(fun, 0); + } + // if setTimeout wasn't available but was latter defined + if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) { + cachedSetTimeout = setTimeout; + return setTimeout(fun, 0); + } + try { + // when when somebody has screwed with setTimeout but no I.E. maddness + return cachedSetTimeout(fun, 0); + } catch (e) { + try { + // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally + return cachedSetTimeout.call(null, fun, 0); + } catch (e) { + // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error + return cachedSetTimeout.call(this, fun, 0); + } + } + + } + function runClearTimeout (marker) { + if (cachedClearTimeout === clearTimeout) { + // normal enviroments in sane situations + return clearTimeout(marker); + } + // if clearTimeout wasn't available but was latter defined + if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) { + cachedClearTimeout = clearTimeout; + return clearTimeout(marker); + } + try { + // when when somebody has screwed with setTimeout but no I.E. maddness + return cachedClearTimeout(marker); + } catch (e) { + try { + // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally + return cachedClearTimeout.call(null, marker); + } catch (e) { + // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error. + // Some versions of I.E. have different rules for clearTimeout vs setTimeout + return cachedClearTimeout.call(this, marker); + } + } + + } + var queue = []; + var draining = false; + var currentQueue; + var queueIndex = -1; + + function cleanUpNextTick () { + if (!draining || !currentQueue) { + return; + } + draining = false; + if (currentQueue.length) { + queue = currentQueue.concat(queue); + } else { + queueIndex = -1; + } + if (queue.length) { + drainQueue(); + } + } + + function drainQueue () { + if (draining) { + return; + } + var timeout = runTimeout(cleanUpNextTick); + draining = true; + + var len = queue.length; + while (len) { + currentQueue = queue; + queue = []; + while (++queueIndex < len) { + if (currentQueue) { + currentQueue[queueIndex].run(); + } + } + queueIndex = -1; + len = queue.length; + } + currentQueue = null; + draining = false; + runClearTimeout(timeout); + } + + process.nextTick = function (fun) { + var args = new Array(arguments.length - 1); + if (arguments.length > 1) { + for (var i = 1; i < arguments.length; i++) { + args[i - 1] = arguments[i]; + } + } + queue.push(new Item(fun, args)); + if (queue.length === 1 && !draining) { + runTimeout(drainQueue); + } + }; + +// v8 likes predictible objects + function Item (fun, array) { + this.fun = fun; + this.array = array; + } + Item.prototype.run = function () { + this.fun.apply(null, this.array); + }; + process.title = 'browser'; + process.browser = true; + process.env = {}; + process.argv = []; + process.version = ''; // empty string to avoid regexp issues + process.versions = {}; + + function noop () {} + + process.on = noop; + process.addListener = noop; + process.once = noop; + process.off = noop; + process.removeListener = noop; + process.removeAllListeners = noop; + process.emit = noop; + process.prependListener = noop; + process.prependOnceListener = noop; + + process.listeners = function (name) { return []; }; + + process.binding = function (name) { + throw new Error('process.binding is not supported'); + }; + + process.cwd = function () { return '/'; }; + process.chdir = function (dir) { + throw new Error('process.chdir is not supported'); + }; + process.umask = function () { return 0; }; + + }, {}], + 71: [function (require, module, exports) { + module.exports = require('./lib/_stream_duplex.js'); + + }, {'./lib/_stream_duplex.js': 72}], + 72: [function (require, module, exports) { +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// a duplex stream is just a stream that is both readable and writable. +// Since JS doesn't have multiple prototypal inheritance, this class +// prototypally inherits from Readable, and then parasitically from +// Writable. + + 'use strict'; + +/* */ + + var pna = require('process-nextick-args'); +/* */ + +/* */ + var objectKeys = Object.keys || function (obj) { + var keys = []; + for (var key in obj) { + keys.push(key); + } return keys; + }; +/* */ + + module.exports = Duplex; + +/* */ + var util = require('core-util-is'); + util.inherits = require('inherits'); +/* */ + + var Readable = require('./_stream_readable'); + var Writable = require('./_stream_writable'); + + util.inherits(Duplex, Readable); + + { + // avoid scope creep, the keys array can then be collected + var keys = objectKeys(Writable.prototype); + for (var v = 0; v < keys.length; v++) { + var method = keys[v]; + if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method]; + } + } + + function Duplex (options) { + if (!(this instanceof Duplex)) return new Duplex(options); + + Readable.call(this, options); + Writable.call(this, options); + + if (options && options.readable === false) this.readable = false; + + if (options && options.writable === false) this.writable = false; + + this.allowHalfOpen = true; + if (options && options.allowHalfOpen === false) this.allowHalfOpen = false; + + this.once('end', onend); + } + + Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', { + // making it explicit this property is not enumerable + // because otherwise some prototype manipulation in + // userland will fail + enumerable: false, + get: function () { + return this._writableState.highWaterMark; + } + }); + +// the no-half-open enforcer + function onend () { + // if we allow half-open state, or if the writable side ended, + // then we're ok. + if (this.allowHalfOpen || this._writableState.ended) return; + + // no more data can be written. + // But allow more writes to happen in this tick. + pna.nextTick(onEndNT, this); + } + + function onEndNT (self) { + self.end(); + } + + Object.defineProperty(Duplex.prototype, 'destroyed', { + get: function () { + if (this._readableState === undefined || this._writableState === undefined) { + return false; + } + return this._readableState.destroyed && this._writableState.destroyed; + }, + set: function (value) { + // we ignore the value if the stream + // has not been initialized yet + if (this._readableState === undefined || this._writableState === undefined) { + return; + } + + // backward compatibility, the user is explicitly + // managing destroyed + this._readableState.destroyed = value; + this._writableState.destroyed = value; + } + }); + + Duplex.prototype._destroy = function (err, cb) { + this.push(null); + this.end(); + + pna.nextTick(cb, err); + }; + }, {'./_stream_readable': 74, './_stream_writable': 76, 'core-util-is': 44, 'inherits': 56, 'process-nextick-args': 69}], + 73: [function (require, module, exports) { +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// a passthrough stream. +// basically just the most minimal sort of Transform stream. +// Every written chunk gets output as-is. + + 'use strict'; + + module.exports = PassThrough; + + var Transform = require('./_stream_transform'); + +/* */ + var util = require('core-util-is'); + util.inherits = require('inherits'); +/* */ + + util.inherits(PassThrough, Transform); + + function PassThrough (options) { + if (!(this instanceof PassThrough)) return new PassThrough(options); + + Transform.call(this, options); + } + + PassThrough.prototype._transform = function (chunk, encoding, cb) { + cb(null, chunk); + }; + }, {'./_stream_transform': 75, 'core-util-is': 44, 'inherits': 56}], + 74: [function (require, module, exports) { + (function (process, global) { +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + + 'use strict'; + +/* */ + + var pna = require('process-nextick-args'); +/* */ + + module.exports = Readable; + +/* */ + var isArray = require('isarray'); +/* */ + +/* */ + var Duplex; +/* */ + + Readable.ReadableState = ReadableState; + +/* */ + var EE = require('events').EventEmitter; + + var EElistenerCount = function (emitter, type) { + return emitter.listeners(type).length; + }; +/* */ + +/* */ + var Stream = require('./internal/streams/stream'); +/* */ + +/* */ + + var Buffer = require('safe-buffer').Buffer; + var OurUint8Array = global.Uint8Array || function () {}; + function _uint8ArrayToBuffer (chunk) { + return Buffer.from(chunk); + } + function _isUint8Array (obj) { + return Buffer.isBuffer(obj) || obj instanceof OurUint8Array; + } + +/* */ + +/* */ + var util = require('core-util-is'); + util.inherits = require('inherits'); +/* */ + +/* */ + var debugUtil = require('util'); + var debug = void 0; + if (debugUtil && debugUtil.debuglog) { + debug = debugUtil.debuglog('stream'); + } else { + debug = function () {}; + } +/* */ + + var BufferList = require('./internal/streams/BufferList'); + var destroyImpl = require('./internal/streams/destroy'); + var StringDecoder; + + util.inherits(Readable, Stream); + + var kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume']; + + function prependListener (emitter, event, fn) { + // Sadly this is not cacheable as some libraries bundle their own + // event emitter implementation with them. + if (typeof emitter.prependListener === 'function') return emitter.prependListener(event, fn); + + // This is a hack to make sure that our error handler is attached before any + // userland ones. NEVER DO THIS. This is here only because this code needs + // to continue to work with older versions of Node.js that do not include + // the prependListener() method. The goal is to eventually remove this hack. + if (!emitter._events || !emitter._events[event]) emitter.on(event, fn); else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn); else emitter._events[event] = [fn, emitter._events[event]]; + } + + function ReadableState (options, stream) { + Duplex = Duplex || require('./_stream_duplex'); + + options = options || {}; + + // Duplex streams are both readable and writable, but share + // the same options object. + // However, some cases require setting options to different + // values for the readable and the writable sides of the duplex stream. + // These options can be provided separately as readableXXX and writableXXX. + var isDuplex = stream instanceof Duplex; + + // object stream flag. Used to make read(n) ignore n and to + // make all the buffer merging and length checks go away + this.objectMode = !!options.objectMode; + + if (isDuplex) this.objectMode = this.objectMode || !!options.readableObjectMode; + + // the point at which it stops calling _read() to fill the buffer + // Note: 0 is a valid value, means "don't call _read preemptively ever" + var hwm = options.highWaterMark; + var readableHwm = options.readableHighWaterMark; + var defaultHwm = this.objectMode ? 16 : 16 * 1024; + + if (hwm || hwm === 0) this.highWaterMark = hwm; else if (isDuplex && (readableHwm || readableHwm === 0)) this.highWaterMark = readableHwm; else this.highWaterMark = defaultHwm; + + // cast to ints. + this.highWaterMark = Math.floor(this.highWaterMark); + + // A linked list is used to store data chunks instead of an array because the + // linked list can remove elements from the beginning faster than + // array.shift() + this.buffer = new BufferList(); + this.length = 0; + this.pipes = null; + this.pipesCount = 0; + this.flowing = null; + this.ended = false; + this.endEmitted = false; + this.reading = false; + + // a flag to be able to tell if the event 'readable'/'data' is emitted + // immediately, or on a later tick. We set this to true at first, because + // any actions that shouldn't happen until "later" should generally also + // not happen before the first read call. + this.sync = true; + + // whenever we return null, then we set a flag to say + // that we're awaiting a 'readable' event emission. + this.needReadable = false; + this.emittedReadable = false; + this.readableListening = false; + this.resumeScheduled = false; + + // has it been destroyed + this.destroyed = false; + + // Crypto is kind of old and crusty. Historically, its default string + // encoding is 'binary' so we have to make this configurable. + // Everything else in the universe uses 'utf8', though. + this.defaultEncoding = options.defaultEncoding || 'utf8'; + + // the number of writers that are awaiting a drain event in .pipe()s + this.awaitDrain = 0; + + // if true, a maybeReadMore has been scheduled + this.readingMore = false; + + this.decoder = null; + this.encoding = null; + if (options.encoding) { + if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; + this.decoder = new StringDecoder(options.encoding); + this.encoding = options.encoding; + } + } + + function Readable (options) { + Duplex = Duplex || require('./_stream_duplex'); + + if (!(this instanceof Readable)) return new Readable(options); + + this._readableState = new ReadableState(options, this); + + // legacy + this.readable = true; + + if (options) { + if (typeof options.read === 'function') this._read = options.read; + + if (typeof options.destroy === 'function') this._destroy = options.destroy; + } + + Stream.call(this); + } + + Object.defineProperty(Readable.prototype, 'destroyed', { + get: function () { + if (this._readableState === undefined) { + return false; + } + return this._readableState.destroyed; + }, + set: function (value) { + // we ignore the value if the stream + // has not been initialized yet + if (!this._readableState) { + return; + } + + // backward compatibility, the user is explicitly + // managing destroyed + this._readableState.destroyed = value; + } + }); + + Readable.prototype.destroy = destroyImpl.destroy; + Readable.prototype._undestroy = destroyImpl.undestroy; + Readable.prototype._destroy = function (err, cb) { + this.push(null); + cb(err); + }; + +// Manually shove something into the read() buffer. +// This returns true if the highWaterMark has not been hit yet, +// similar to how Writable.write() returns true if you should +// write() some more. + Readable.prototype.push = function (chunk, encoding) { + var state = this._readableState; + var skipChunkCheck; + + if (!state.objectMode) { + if (typeof chunk === 'string') { + encoding = encoding || state.defaultEncoding; + if (encoding !== state.encoding) { + chunk = Buffer.from(chunk, encoding); + encoding = ''; + } + skipChunkCheck = true; + } + } else { + skipChunkCheck = true; + } + + return readableAddChunk(this, chunk, encoding, false, skipChunkCheck); + }; + +// Unshift should *always* be something directly out of read() + Readable.prototype.unshift = function (chunk) { + return readableAddChunk(this, chunk, null, true, false); + }; + + function readableAddChunk (stream, chunk, encoding, addToFront, skipChunkCheck) { + var state = stream._readableState; + if (chunk === null) { + state.reading = false; + onEofChunk(stream, state); + } else { + var er; + if (!skipChunkCheck) er = chunkInvalid(state, chunk); + if (er) { + stream.emit('error', er); + } else if (state.objectMode || chunk && chunk.length > 0) { + if (typeof chunk !== 'string' && !state.objectMode && Object.getPrototypeOf(chunk) !== Buffer.prototype) { + chunk = _uint8ArrayToBuffer(chunk); + } + + if (addToFront) { + if (state.endEmitted) stream.emit('error', new Error('stream.unshift() after end event')); else addChunk(stream, state, chunk, true); + } else if (state.ended) { + stream.emit('error', new Error('stream.push() after EOF')); + } else { + state.reading = false; + if (state.decoder && !encoding) { + chunk = state.decoder.write(chunk); + if (state.objectMode || chunk.length !== 0) addChunk(stream, state, chunk, false); else maybeReadMore(stream, state); + } else { + addChunk(stream, state, chunk, false); + } + } + } else if (!addToFront) { + state.reading = false; + } + } + + return needMoreData(state); + } + + function addChunk (stream, state, chunk, addToFront) { + if (state.flowing && state.length === 0 && !state.sync) { + stream.emit('data', chunk); + stream.read(0); + } else { + // update the buffer info. + state.length += state.objectMode ? 1 : chunk.length; + if (addToFront) state.buffer.unshift(chunk); else state.buffer.push(chunk); + + if (state.needReadable) emitReadable(stream); + } + maybeReadMore(stream, state); + } + + function chunkInvalid (state, chunk) { + var er; + if (!_isUint8Array(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { + er = new TypeError('Invalid non-string/buffer chunk'); + } + return er; + } + +// if it's past the high water mark, we can push in some more. +// Also, if we have no data yet, we can stand some +// more bytes. This is to work around cases where hwm=0, +// such as the repl. Also, if the push() triggered a +// readable event, and the user called read(largeNumber) such that +// needReadable was set, then we ought to push more, so that another +// 'readable' event will be triggered. + function needMoreData (state) { + return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0); + } + + Readable.prototype.isPaused = function () { + return this._readableState.flowing === false; + }; + +// backwards compatibility. + Readable.prototype.setEncoding = function (enc) { + if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; + this._readableState.decoder = new StringDecoder(enc); + this._readableState.encoding = enc; + return this; + }; + +// Don't raise the hwm > 8MB + var MAX_HWM = 0x800000; + function computeNewHighWaterMark (n) { + if (n >= MAX_HWM) { + n = MAX_HWM; + } else { + // Get the next highest power of 2 to prevent increasing hwm excessively in + // tiny amounts + n--; + n |= n >>> 1; + n |= n >>> 2; + n |= n >>> 4; + n |= n >>> 8; + n |= n >>> 16; + n++; + } + return n; + } + +// This function is designed to be inlinable, so please take care when making +// changes to the function body. + function howMuchToRead (n, state) { + if (n <= 0 || state.length === 0 && state.ended) return 0; + if (state.objectMode) return 1; + if (n !== n) { + // Only flow one buffer at a time + if (state.flowing && state.length) return state.buffer.head.data.length; else return state.length; + } + // If we're asking for more than the current hwm, then raise the hwm. + if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n); + if (n <= state.length) return n; + // Don't have enough + if (!state.ended) { + state.needReadable = true; + return 0; + } + return state.length; + } + +// you can override either this method, or the async _read(n) below. + Readable.prototype.read = function (n) { + debug('read', n); + n = parseInt(n, 10); + var state = this._readableState; + var nOrig = n; + + if (n !== 0) state.emittedReadable = false; + + // if we're doing read(0) to trigger a readable event, but we + // already have a bunch of data in the buffer, then just trigger + // the 'readable' event and move on. + if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) { + debug('read: emitReadable', state.length, state.ended); + if (state.length === 0 && state.ended) endReadable(this); else emitReadable(this); + return null; + } + + n = howMuchToRead(n, state); + + // if we've ended, and we're now clear, then finish it up. + if (n === 0 && state.ended) { + if (state.length === 0) endReadable(this); + return null; + } + + // All the actual chunk generation logic needs to be + // *below* the call to _read. The reason is that in certain + // synthetic stream cases, such as passthrough streams, _read + // may be a completely synchronous operation which may change + // the state of the read buffer, providing enough data when + // before there was *not* enough. + // + // So, the steps are: + // 1. Figure out what the state of things will be after we do + // a read from the buffer. + // + // 2. If that resulting state will trigger a _read, then call _read. + // Note that this may be asynchronous, or synchronous. Yes, it is + // deeply ugly to write APIs this way, but that still doesn't mean + // that the Readable class should behave improperly, as streams are + // designed to be sync/async agnostic. + // Take note if the _read call is sync or async (ie, if the read call + // has returned yet), so that we know whether or not it's safe to emit + // 'readable' etc. + // + // 3. Actually pull the requested chunks out of the buffer and return. + + // if we need a readable event, then we need to do some reading. + var doRead = state.needReadable; + debug('need readable', doRead); + + // if we currently have less than the highWaterMark, then also read some + if (state.length === 0 || state.length - n < state.highWaterMark) { + doRead = true; + debug('length less than watermark', doRead); + } + + // however, if we've ended, then there's no point, and if we're already + // reading, then it's unnecessary. + if (state.ended || state.reading) { + doRead = false; + debug('reading or ended', doRead); + } else if (doRead) { + debug('do read'); + state.reading = true; + state.sync = true; + // if the length is currently zero, then we *need* a readable event. + if (state.length === 0) state.needReadable = true; + // call internal read method + this._read(state.highWaterMark); + state.sync = false; + // If _read pushed data synchronously, then `reading` will be false, + // and we need to re-evaluate how much data we can return to the user. + if (!state.reading) n = howMuchToRead(nOrig, state); + } + + var ret; + if (n > 0) ret = fromList(n, state); else ret = null; + + if (ret === null) { + state.needReadable = true; + n = 0; + } else { + state.length -= n; + } + + if (state.length === 0) { + // If we have nothing in the buffer, then we want to know + // as soon as we *do* get something into the buffer. + if (!state.ended) state.needReadable = true; + + // If we tried to read() past the EOF, then emit end on the next tick. + if (nOrig !== n && state.ended) endReadable(this); + } + + if (ret !== null) this.emit('data', ret); + + return ret; + }; + + function onEofChunk (stream, state) { + if (state.ended) return; + if (state.decoder) { + var chunk = state.decoder.end(); + if (chunk && chunk.length) { + state.buffer.push(chunk); + state.length += state.objectMode ? 1 : chunk.length; + } + } + state.ended = true; + + // emit 'readable' now to make sure it gets picked up. + emitReadable(stream); + } + +// Don't emit readable right away in sync mode, because this can trigger +// another read() call => stack overflow. This way, it might trigger +// a nextTick recursion warning, but that's not so bad. + function emitReadable (stream) { + var state = stream._readableState; + state.needReadable = false; + if (!state.emittedReadable) { + debug('emitReadable', state.flowing); + state.emittedReadable = true; + if (state.sync) pna.nextTick(emitReadable_, stream); else emitReadable_(stream); + } + } + + function emitReadable_ (stream) { + debug('emit readable'); + stream.emit('readable'); + flow(stream); + } + +// at this point, the user has presumably seen the 'readable' event, +// and called read() to consume some data. that may have triggered +// in turn another _read(n) call, in which case reading = true if +// it's in progress. +// However, if we're not ended, or reading, and the length < hwm, +// then go ahead and try to read some more preemptively. + function maybeReadMore (stream, state) { + if (!state.readingMore) { + state.readingMore = true; + pna.nextTick(maybeReadMore_, stream, state); + } + } + + function maybeReadMore_ (stream, state) { + var len = state.length; + while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) { + debug('maybeReadMore read 0'); + stream.read(0); + if (len === state.length) + // didn't get any data, stop spinning. + { break; } else len = state.length; + } + state.readingMore = false; + } + +// abstract method. to be overridden in specific implementation classes. +// call cb(er, data) where data is <= n in length. +// for virtual (non-string, non-buffer) streams, "length" is somewhat +// arbitrary, and perhaps not very meaningful. + Readable.prototype._read = function (n) { + this.emit('error', new Error('_read() is not implemented')); + }; + + Readable.prototype.pipe = function (dest, pipeOpts) { + var src = this; + var state = this._readableState; + + switch (state.pipesCount) { + case 0: + state.pipes = dest; + break; + case 1: + state.pipes = [state.pipes, dest]; + break; + default: + state.pipes.push(dest); + break; + } + state.pipesCount += 1; + debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); + + var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr; + + var endFn = doEnd ? onend : unpipe; + if (state.endEmitted) pna.nextTick(endFn); else src.once('end', endFn); + + dest.on('unpipe', onunpipe); + function onunpipe (readable, unpipeInfo) { + debug('onunpipe'); + if (readable === src) { + if (unpipeInfo && unpipeInfo.hasUnpiped === false) { + unpipeInfo.hasUnpiped = true; + cleanup(); + } + } + } + + function onend () { + debug('onend'); + dest.end(); + } + + // when the dest drains, it reduces the awaitDrain counter + // on the source. This would be more elegant with a .once() + // handler in flow(), but adding and removing repeatedly is + // too slow. + var ondrain = pipeOnDrain(src); + dest.on('drain', ondrain); + + var cleanedUp = false; + function cleanup () { + debug('cleanup'); + // cleanup event handlers once the pipe is broken + dest.removeListener('close', onclose); + dest.removeListener('finish', onfinish); + dest.removeListener('drain', ondrain); + dest.removeListener('error', onerror); + dest.removeListener('unpipe', onunpipe); + src.removeListener('end', onend); + src.removeListener('end', unpipe); + src.removeListener('data', ondata); + + cleanedUp = true; + + // if the reader is waiting for a drain event from this + // specific writer, then it would cause it to never start + // flowing again. + // So, if this is awaiting a drain, then we just call it now. + // If we don't know, then assume that we are waiting for one. + if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain(); + } + + // If the user pushes more data while we're writing to dest then we'll end up + // in ondata again. However, we only want to increase awaitDrain once because + // dest will only emit one 'drain' event for the multiple writes. + // => Introduce a guard on increasing awaitDrain. + var increasedAwaitDrain = false; + src.on('data', ondata); + function ondata (chunk) { + debug('ondata'); + increasedAwaitDrain = false; + var ret = dest.write(chunk); + if (ret === false && !increasedAwaitDrain) { + // If the user unpiped during `dest.write()`, it is possible + // to get stuck in a permanently paused state if that write + // also returned false. + // => Check whether `dest` is still a piping destination. + if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) { + debug('false write response, pause', src._readableState.awaitDrain); + src._readableState.awaitDrain++; + increasedAwaitDrain = true; + } + src.pause(); + } + } + + // if the dest has an error, then stop piping into it. + // however, don't suppress the throwing behavior for this. + function onerror (er) { + debug('onerror', er); + unpipe(); + dest.removeListener('error', onerror); + if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er); + } + + // Make sure our error handler is attached before userland ones. + prependListener(dest, 'error', onerror); + + // Both close and finish should trigger unpipe, but only once. + function onclose () { + dest.removeListener('finish', onfinish); + unpipe(); + } + dest.once('close', onclose); + function onfinish () { + debug('onfinish'); + dest.removeListener('close', onclose); + unpipe(); + } + dest.once('finish', onfinish); + + function unpipe () { + debug('unpipe'); + src.unpipe(dest); + } + + // tell the dest that it's being piped to + dest.emit('pipe', src); + + // start the flow if it hasn't been started already. + if (!state.flowing) { + debug('pipe resume'); + src.resume(); + } + + return dest; + }; + + function pipeOnDrain (src) { + return function () { + var state = src._readableState; + debug('pipeOnDrain', state.awaitDrain); + if (state.awaitDrain) state.awaitDrain--; + if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) { + state.flowing = true; + flow(src); + } + }; + } + + Readable.prototype.unpipe = function (dest) { + var state = this._readableState; + var unpipeInfo = { hasUnpiped: false }; + + // if we're not piping anywhere, then do nothing. + if (state.pipesCount === 0) return this; + + // just one destination. most common case. + if (state.pipesCount === 1) { + // passed in one, but it's not the right one. + if (dest && dest !== state.pipes) return this; + + if (!dest) dest = state.pipes; + + // got a match. + state.pipes = null; + state.pipesCount = 0; + state.flowing = false; + if (dest) dest.emit('unpipe', this, unpipeInfo); + return this; + } + + // slow case. multiple pipe destinations. + + if (!dest) { + // remove all. + var dests = state.pipes; + var len = state.pipesCount; + state.pipes = null; + state.pipesCount = 0; + state.flowing = false; + + for (var i = 0; i < len; i++) { + dests[i].emit('unpipe', this, unpipeInfo); + } return this; + } + + // try to find the right one. + var index = indexOf(state.pipes, dest); + if (index === -1) return this; + + state.pipes.splice(index, 1); + state.pipesCount -= 1; + if (state.pipesCount === 1) state.pipes = state.pipes[0]; + + dest.emit('unpipe', this, unpipeInfo); + + return this; + }; + +// set up data events if they are asked for +// Ensure readable listeners eventually get something + Readable.prototype.on = function (ev, fn) { + var res = Stream.prototype.on.call(this, ev, fn); + + if (ev === 'data') { + // Start flowing on next tick if stream isn't explicitly paused + if (this._readableState.flowing !== false) this.resume(); + } else if (ev === 'readable') { + var state = this._readableState; + if (!state.endEmitted && !state.readableListening) { + state.readableListening = state.needReadable = true; + state.emittedReadable = false; + if (!state.reading) { + pna.nextTick(nReadingNextTick, this); + } else if (state.length) { + emitReadable(this); + } + } + } + + return res; + }; + Readable.prototype.addListener = Readable.prototype.on; + + function nReadingNextTick (self) { + debug('readable nexttick read 0'); + self.read(0); + } + +// pause() and resume() are remnants of the legacy readable stream API +// If the user uses them, then switch into old mode. + Readable.prototype.resume = function () { + var state = this._readableState; + if (!state.flowing) { + debug('resume'); + state.flowing = true; + resume(this, state); + } + return this; + }; + + function resume (stream, state) { + if (!state.resumeScheduled) { + state.resumeScheduled = true; + pna.nextTick(resume_, stream, state); + } + } + + function resume_ (stream, state) { + if (!state.reading) { + debug('resume read 0'); + stream.read(0); + } + + state.resumeScheduled = false; + state.awaitDrain = 0; + stream.emit('resume'); + flow(stream); + if (state.flowing && !state.reading) stream.read(0); + } + + Readable.prototype.pause = function () { + debug('call pause flowing=%j', this._readableState.flowing); + if (this._readableState.flowing !== false) { + debug('pause'); + this._readableState.flowing = false; + this.emit('pause'); + } + return this; + }; + + function flow (stream) { + var state = stream._readableState; + debug('flow', state.flowing); + while (state.flowing && stream.read() !== null) {} + } + +// wrap an old-style stream as the async data source. +// This is *not* part of the readable stream interface. +// It is an ugly unfortunate mess of history. + Readable.prototype.wrap = function (stream) { + var _this = this; + + var state = this._readableState; + var paused = false; + + stream.on('end', function () { + debug('wrapped end'); + if (state.decoder && !state.ended) { + var chunk = state.decoder.end(); + if (chunk && chunk.length) _this.push(chunk); + } + + _this.push(null); + }); + + stream.on('data', function (chunk) { + debug('wrapped data'); + if (state.decoder) chunk = state.decoder.write(chunk); + + // don't skip over falsy values in objectMode + if (state.objectMode && (chunk === null || chunk === undefined)) return; else if (!state.objectMode && (!chunk || !chunk.length)) return; + + var ret = _this.push(chunk); + if (!ret) { + paused = true; + stream.pause(); + } + }); + + // proxy all the other methods. + // important when wrapping filters and duplexes. + for (var i in stream) { + if (this[i] === undefined && typeof stream[i] === 'function') { + this[i] = (function (method) { + return function () { + return stream[method].apply(stream, arguments); + }; + }(i)); + } + } + + // proxy certain important events. + for (var n = 0; n < kProxyEvents.length; n++) { + stream.on(kProxyEvents[n], this.emit.bind(this, kProxyEvents[n])); + } + + // when we try to consume some more bytes, simply unpause the + // underlying stream. + this._read = function (n) { + debug('wrapped _read', n); + if (paused) { + paused = false; + stream.resume(); + } + }; + + return this; + }; + + Object.defineProperty(Readable.prototype, 'readableHighWaterMark', { + // making it explicit this property is not enumerable + // because otherwise some prototype manipulation in + // userland will fail + enumerable: false, + get: function () { + return this._readableState.highWaterMark; + } + }); + +// exposed for testing purposes only. + Readable._fromList = fromList; + +// Pluck off n bytes from an array of buffers. +// Length is the combined lengths of all the buffers in the list. +// This function is designed to be inlinable, so please take care when making +// changes to the function body. + function fromList (n, state) { + // nothing buffered + if (state.length === 0) return null; + + var ret; + if (state.objectMode) ret = state.buffer.shift(); else if (!n || n >= state.length) { + // read it all, truncate the list + if (state.decoder) ret = state.buffer.join(''); else if (state.buffer.length === 1) ret = state.buffer.head.data; else ret = state.buffer.concat(state.length); + state.buffer.clear(); + } else { + // read part of list + ret = fromListPartial(n, state.buffer, state.decoder); + } + + return ret; + } + +// Extracts only enough buffered data to satisfy the amount requested. +// This function is designed to be inlinable, so please take care when making +// changes to the function body. + function fromListPartial (n, list, hasStrings) { + var ret; + if (n < list.head.data.length) { + // slice is the same for buffers and strings + ret = list.head.data.slice(0, n); + list.head.data = list.head.data.slice(n); + } else if (n === list.head.data.length) { + // first chunk is a perfect match + ret = list.shift(); + } else { + // result spans more than one buffer + ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list); + } + return ret; + } + +// Copies a specified amount of characters from the list of buffered data +// chunks. +// This function is designed to be inlinable, so please take care when making +// changes to the function body. + function copyFromBufferString (n, list) { + var p = list.head; + var c = 1; + var ret = p.data; + n -= ret.length; + while (p = p.next) { + var str = p.data; + var nb = n > str.length ? str.length : n; + if (nb === str.length) ret += str; else ret += str.slice(0, n); + n -= nb; + if (n === 0) { + if (nb === str.length) { + ++c; + if (p.next) list.head = p.next; else list.head = list.tail = null; + } else { + list.head = p; + p.data = str.slice(nb); + } + break; + } + ++c; + } + list.length -= c; + return ret; + } + +// Copies a specified amount of bytes from the list of buffered data chunks. +// This function is designed to be inlinable, so please take care when making +// changes to the function body. + function copyFromBuffer (n, list) { + var ret = Buffer.allocUnsafe(n); + var p = list.head; + var c = 1; + p.data.copy(ret); + n -= p.data.length; + while (p = p.next) { + var buf = p.data; + var nb = n > buf.length ? buf.length : n; + buf.copy(ret, ret.length - n, 0, nb); + n -= nb; + if (n === 0) { + if (nb === buf.length) { + ++c; + if (p.next) list.head = p.next; else list.head = list.tail = null; + } else { + list.head = p; + p.data = buf.slice(nb); + } + break; + } + ++c; + } + list.length -= c; + return ret; + } + + function endReadable (stream) { + var state = stream._readableState; + + // If we get here before consuming all the bytes, then that is a + // bug in node. Should never happen. + if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream'); + + if (!state.endEmitted) { + state.ended = true; + pna.nextTick(endReadableNT, state, stream); + } + } + + function endReadableNT (state, stream) { + // Check that we didn't get one last unshift. + if (!state.endEmitted && state.length === 0) { + state.endEmitted = true; + stream.readable = false; + stream.emit('end'); + } + } + + function indexOf (xs, x) { + for (var i = 0, l = xs.length; i < l; i++) { + if (xs[i] === x) return i; + } + return -1; + } + }).call(this, require('_process'), typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}); + }, {'./_stream_duplex': 72, './internal/streams/BufferList': 77, './internal/streams/destroy': 78, './internal/streams/stream': 79, '_process': 70, 'core-util-is': 44, 'events': 50, 'inherits': 56, 'isarray': 58, 'process-nextick-args': 69, 'safe-buffer': 84, 'string_decoder/': 86, 'util': 40}], + 75: [function (require, module, exports) { +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// a transform stream is a readable/writable stream where you do +// something with the data. Sometimes it's called a "filter", +// but that's not a great name for it, since that implies a thing where +// some bits pass through, and others are simply ignored. (That would +// be a valid example of a transform, of course.) +// +// While the output is causally related to the input, it's not a +// necessarily symmetric or synchronous transformation. For example, +// a zlib stream might take multiple plain-text writes(), and then +// emit a single compressed chunk some time in the future. +// +// Here's how this works: +// +// The Transform stream has all the aspects of the readable and writable +// stream classes. When you write(chunk), that calls _write(chunk,cb) +// internally, and returns false if there's a lot of pending writes +// buffered up. When you call read(), that calls _read(n) until +// there's enough pending readable data buffered up. +// +// In a transform stream, the written data is placed in a buffer. When +// _read(n) is called, it transforms the queued up data, calling the +// buffered _write cb's as it consumes chunks. If consuming a single +// written chunk would result in multiple output chunks, then the first +// outputted bit calls the readcb, and subsequent chunks just go into +// the read buffer, and will cause it to emit 'readable' if necessary. +// +// This way, back-pressure is actually determined by the reading side, +// since _read has to be called to start processing a new chunk. However, +// a pathological inflate type of transform can cause excessive buffering +// here. For example, imagine a stream where every byte of input is +// interpreted as an integer from 0-255, and then results in that many +// bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in +// 1kb of data being output. In this case, you could write a very small +// amount of input, and end up with a very large amount of output. In +// such a pathological inflating mechanism, there'd be no way to tell +// the system to stop doing the transform. A single 4MB write could +// cause the system to run out of memory. +// +// However, even in such a pathological case, only a single written chunk +// would be consumed, and then the rest would wait (un-transformed) until +// the results of the previous transformed chunk were consumed. + + 'use strict'; + + module.exports = Transform; + + var Duplex = require('./_stream_duplex'); + +/* */ + var util = require('core-util-is'); + util.inherits = require('inherits'); +/* */ + + util.inherits(Transform, Duplex); + + function afterTransform (er, data) { + var ts = this._transformState; + ts.transforming = false; + + var cb = ts.writecb; + + if (!cb) { + return this.emit('error', new Error('write callback called multiple times')); + } + + ts.writechunk = null; + ts.writecb = null; + + if (data != null) // single equals check for both `null` and `undefined` + { this.push(data); } + + cb(er); + + var rs = this._readableState; + rs.reading = false; + if (rs.needReadable || rs.length < rs.highWaterMark) { + this._read(rs.highWaterMark); + } + } + + function Transform (options) { + if (!(this instanceof Transform)) return new Transform(options); + + Duplex.call(this, options); + + this._transformState = { + afterTransform: afterTransform.bind(this), + needTransform: false, + transforming: false, + writecb: null, + writechunk: null, + writeencoding: null + }; + + // start out asking for a readable event once data is transformed. + this._readableState.needReadable = true; + + // we have implemented the _read method, and done the other things + // that Readable wants before the first _read call, so unset the + // sync guard flag. + this._readableState.sync = false; + + if (options) { + if (typeof options.transform === 'function') this._transform = options.transform; + + if (typeof options.flush === 'function') this._flush = options.flush; + } + + // When the writable side finishes, then flush out anything remaining. + this.on('prefinish', prefinish); + } + + function prefinish () { + var _this = this; + + if (typeof this._flush === 'function') { + this._flush(function (er, data) { + done(_this, er, data); + }); + } else { + done(this, null, null); + } + } + + Transform.prototype.push = function (chunk, encoding) { + this._transformState.needTransform = false; + return Duplex.prototype.push.call(this, chunk, encoding); + }; + +// This is the part where you do stuff! +// override this function in implementation classes. +// 'chunk' is an input chunk. +// +// Call `push(newChunk)` to pass along transformed output +// to the readable side. You may call 'push' zero or more times. +// +// Call `cb(err)` when you are done with this chunk. If you pass +// an error, then that'll put the hurt on the whole operation. If you +// never call cb(), then you'll never get another chunk. + Transform.prototype._transform = function (chunk, encoding, cb) { + throw new Error('_transform() is not implemented'); + }; + + Transform.prototype._write = function (chunk, encoding, cb) { + var ts = this._transformState; + ts.writecb = cb; + ts.writechunk = chunk; + ts.writeencoding = encoding; + if (!ts.transforming) { + var rs = this._readableState; + if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark); + } + }; + +// Doesn't matter what the args are here. +// _transform does all the work. +// That we got here means that the readable side wants more data. + Transform.prototype._read = function (n) { + var ts = this._transformState; + + if (ts.writechunk !== null && ts.writecb && !ts.transforming) { + ts.transforming = true; + this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); + } else { + // mark that we need a transform, so that any data that comes in + // will get processed, now that we've asked for it. + ts.needTransform = true; + } + }; + + Transform.prototype._destroy = function (err, cb) { + var _this2 = this; + + Duplex.prototype._destroy.call(this, err, function (err2) { + cb(err2); + _this2.emit('close'); + }); + }; + + function done (stream, er, data) { + if (er) return stream.emit('error', er); + + if (data != null) // single equals check for both `null` and `undefined` + { stream.push(data); } + + // if there's nothing in the write buffer, then that means + // that nothing more will ever be provided + if (stream._writableState.length) throw new Error('Calling transform done when ws.length != 0'); + + if (stream._transformState.transforming) throw new Error('Calling transform done when still transforming'); + + return stream.push(null); + } + }, {'./_stream_duplex': 72, 'core-util-is': 44, 'inherits': 56}], + 76: [function (require, module, exports) { + (function (process, global, setImmediate) { +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// A bit simpler than readable streams. +// Implement an async ._write(chunk, encoding, cb), and it'll handle all +// the drain event emission and buffering. + + 'use strict'; + +/* */ + + var pna = require('process-nextick-args'); +/* */ + + module.exports = Writable; + +/* */ + function WriteReq (chunk, encoding, cb) { + this.chunk = chunk; + this.encoding = encoding; + this.callback = cb; + this.next = null; + } + +// It seems a linked list but it is not +// there will be only 2 of these for each stream + function CorkedRequest (state) { + var _this = this; + + this.next = null; + this.entry = null; + this.finish = function () { + onCorkedFinish(_this, state); + }; + } +/* */ + +/* */ + var asyncWrite = !process.browser && ['v0.10', 'v0.9.'].indexOf(process.version.slice(0, 5)) > -1 ? setImmediate : pna.nextTick; +/* */ + +/* */ + var Duplex; +/* */ + + Writable.WritableState = WritableState; + +/* */ + var util = require('core-util-is'); + util.inherits = require('inherits'); +/* */ + +/* */ + var internalUtil = { + deprecate: require('util-deprecate') + }; +/* */ + +/* */ + var Stream = require('./internal/streams/stream'); +/* */ + +/* */ + + var Buffer = require('safe-buffer').Buffer; + var OurUint8Array = global.Uint8Array || function () {}; + function _uint8ArrayToBuffer (chunk) { + return Buffer.from(chunk); + } + function _isUint8Array (obj) { + return Buffer.isBuffer(obj) || obj instanceof OurUint8Array; + } + +/* */ + + var destroyImpl = require('./internal/streams/destroy'); + + util.inherits(Writable, Stream); + + function nop () {} + + function WritableState (options, stream) { + Duplex = Duplex || require('./_stream_duplex'); + + options = options || {}; + + // Duplex streams are both readable and writable, but share + // the same options object. + // However, some cases require setting options to different + // values for the readable and the writable sides of the duplex stream. + // These options can be provided separately as readableXXX and writableXXX. + var isDuplex = stream instanceof Duplex; + + // object stream flag to indicate whether or not this stream + // contains buffers or objects. + this.objectMode = !!options.objectMode; + + if (isDuplex) this.objectMode = this.objectMode || !!options.writableObjectMode; + + // the point at which write() starts returning false + // Note: 0 is a valid value, means that we always return false if + // the entire buffer is not flushed immediately on write() + var hwm = options.highWaterMark; + var writableHwm = options.writableHighWaterMark; + var defaultHwm = this.objectMode ? 16 : 16 * 1024; + + if (hwm || hwm === 0) this.highWaterMark = hwm; else if (isDuplex && (writableHwm || writableHwm === 0)) this.highWaterMark = writableHwm; else this.highWaterMark = defaultHwm; + + // cast to ints. + this.highWaterMark = Math.floor(this.highWaterMark); + + // if _final has been called + this.finalCalled = false; + + // drain event flag. + this.needDrain = false; + // at the start of calling end() + this.ending = false; + // when end() has been called, and returned + this.ended = false; + // when 'finish' is emitted + this.finished = false; + + // has it been destroyed + this.destroyed = false; + + // should we decode strings into buffers before passing to _write? + // this is here so that some node-core streams can optimize string + // handling at a lower level. + var noDecode = options.decodeStrings === false; + this.decodeStrings = !noDecode; + + // Crypto is kind of old and crusty. Historically, its default string + // encoding is 'binary' so we have to make this configurable. + // Everything else in the universe uses 'utf8', though. + this.defaultEncoding = options.defaultEncoding || 'utf8'; + + // not an actual buffer we keep track of, but a measurement + // of how much we're waiting to get pushed to some underlying + // socket or file. + this.length = 0; + + // a flag to see when we're in the middle of a write. + this.writing = false; + + // when true all writes will be buffered until .uncork() call + this.corked = 0; + + // a flag to be able to tell if the onwrite cb is called immediately, + // or on a later tick. We set this to true at first, because any + // actions that shouldn't happen until "later" should generally also + // not happen before the first write call. + this.sync = true; + + // a flag to know if we're processing previously buffered items, which + // may call the _write() callback in the same tick, so that we don't + // end up in an overlapped onwrite situation. + this.bufferProcessing = false; + + // the callback that's passed to _write(chunk,cb) + this.onwrite = function (er) { + onwrite(stream, er); + }; + + // the callback that the user supplies to write(chunk,encoding,cb) + this.writecb = null; + + // the amount that is being written when _write is called. + this.writelen = 0; + + this.bufferedRequest = null; + this.lastBufferedRequest = null; + + // number of pending user-supplied write callbacks + // this must be 0 before 'finish' can be emitted + this.pendingcb = 0; + + // emit prefinish if the only thing we're waiting for is _write cbs + // This is relevant for synchronous Transform streams + this.prefinished = false; + + // True if the error was already emitted and should not be thrown again + this.errorEmitted = false; + + // count buffered requests + this.bufferedRequestCount = 0; + + // allocate the first CorkedRequest, there is always + // one allocated and free to use, and we maintain at most two + this.corkedRequestsFree = new CorkedRequest(this); + } + + WritableState.prototype.getBuffer = function getBuffer () { + var current = this.bufferedRequest; + var out = []; + while (current) { + out.push(current); + current = current.next; + } + return out; + }; + + (function () { + try { + Object.defineProperty(WritableState.prototype, 'buffer', { + get: internalUtil.deprecate(function () { + return this.getBuffer(); + }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.', 'DEP0003') + }); + } catch (_) {} + })(); + +// Test _writableState for inheritance to account for Duplex streams, +// whose prototype chain only points to Readable. + var realHasInstance; + if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') { + realHasInstance = Function.prototype[Symbol.hasInstance]; + Object.defineProperty(Writable, Symbol.hasInstance, { + value: function (object) { + if (realHasInstance.call(this, object)) return true; + if (this !== Writable) return false; + + return object && object._writableState instanceof WritableState; + } + }); + } else { + realHasInstance = function (object) { + return object instanceof this; + }; + } + + function Writable (options) { + Duplex = Duplex || require('./_stream_duplex'); + + // Writable ctor is applied to Duplexes, too. + // `realHasInstance` is necessary because using plain `instanceof` + // would return false, as no `_writableState` property is attached. + + // Trying to use the custom `instanceof` for Writable here will also break the + // Node.js LazyTransform implementation, which has a non-trivial getter for + // `_writableState` that would lead to infinite recursion. + if (!realHasInstance.call(Writable, this) && !(this instanceof Duplex)) { + return new Writable(options); + } + + this._writableState = new WritableState(options, this); + + // legacy. + this.writable = true; + + if (options) { + if (typeof options.write === 'function') this._write = options.write; + + if (typeof options.writev === 'function') this._writev = options.writev; + + if (typeof options.destroy === 'function') this._destroy = options.destroy; + + if (typeof options.final === 'function') this._final = options.final; + } + + Stream.call(this); + } + +// Otherwise people can pipe Writable streams, which is just wrong. + Writable.prototype.pipe = function () { + this.emit('error', new Error('Cannot pipe, not readable')); + }; + + function writeAfterEnd (stream, cb) { + var er = new Error('write after end'); + // TODO: defer error events consistently everywhere, not just the cb + stream.emit('error', er); + pna.nextTick(cb, er); + } + +// Checks that a user-supplied chunk is valid, especially for the particular +// mode the stream is in. Currently this means that `null` is never accepted +// and undefined/non-string values are only allowed in object mode. + function validChunk (stream, state, chunk, cb) { + var valid = true; + var er = false; + + if (chunk === null) { + er = new TypeError('May not write null values to stream'); + } else if (typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { + er = new TypeError('Invalid non-string/buffer chunk'); + } + if (er) { + stream.emit('error', er); + pna.nextTick(cb, er); + valid = false; + } + return valid; + } + + Writable.prototype.write = function (chunk, encoding, cb) { + var state = this._writableState; + var ret = false; + var isBuf = !state.objectMode && _isUint8Array(chunk); + + if (isBuf && !Buffer.isBuffer(chunk)) { + chunk = _uint8ArrayToBuffer(chunk); + } + + if (typeof encoding === 'function') { + cb = encoding; + encoding = null; + } + + if (isBuf) encoding = 'buffer'; else if (!encoding) encoding = state.defaultEncoding; + + if (typeof cb !== 'function') cb = nop; + + if (state.ended) writeAfterEnd(this, cb); else if (isBuf || validChunk(this, state, chunk, cb)) { + state.pendingcb++; + ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb); + } + + return ret; + }; + + Writable.prototype.cork = function () { + var state = this._writableState; + + state.corked++; + }; + + Writable.prototype.uncork = function () { + var state = this._writableState; + + if (state.corked) { + state.corked--; + + if (!state.writing && !state.corked && !state.finished && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state); + } + }; + + Writable.prototype.setDefaultEncoding = function setDefaultEncoding (encoding) { + // node::ParseEncoding() requires lower case. + if (typeof encoding === 'string') encoding = encoding.toLowerCase(); + if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new TypeError('Unknown encoding: ' + encoding); + this._writableState.defaultEncoding = encoding; + return this; + }; + + function decodeChunk (state, chunk, encoding) { + if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') { + chunk = Buffer.from(chunk, encoding); + } + return chunk; + } + + Object.defineProperty(Writable.prototype, 'writableHighWaterMark', { + // making it explicit this property is not enumerable + // because otherwise some prototype manipulation in + // userland will fail + enumerable: false, + get: function () { + return this._writableState.highWaterMark; + } + }); + +// if we're already writing something, then just put this +// in the queue, and wait our turn. Otherwise, call _write +// If we return false, then we need a drain event, so set that flag. + function writeOrBuffer (stream, state, isBuf, chunk, encoding, cb) { + if (!isBuf) { + var newChunk = decodeChunk(state, chunk, encoding); + if (chunk !== newChunk) { + isBuf = true; + encoding = 'buffer'; + chunk = newChunk; + } + } + var len = state.objectMode ? 1 : chunk.length; + + state.length += len; + + var ret = state.length < state.highWaterMark; + // we must ensure that previous needDrain will not be reset to false. + if (!ret) state.needDrain = true; + + if (state.writing || state.corked) { + var last = state.lastBufferedRequest; + state.lastBufferedRequest = { + chunk: chunk, + encoding: encoding, + isBuf: isBuf, + callback: cb, + next: null + }; + if (last) { + last.next = state.lastBufferedRequest; + } else { + state.bufferedRequest = state.lastBufferedRequest; + } + state.bufferedRequestCount += 1; + } else { + doWrite(stream, state, false, len, chunk, encoding, cb); + } + + return ret; + } + + function doWrite (stream, state, writev, len, chunk, encoding, cb) { + state.writelen = len; + state.writecb = cb; + state.writing = true; + state.sync = true; + if (writev) stream._writev(chunk, state.onwrite); else stream._write(chunk, encoding, state.onwrite); + state.sync = false; + } + + function onwriteError (stream, state, sync, er, cb) { + --state.pendingcb; + + if (sync) { + // defer the callback if we are being called synchronously + // to avoid piling up things on the stack + pna.nextTick(cb, er); + // this can emit finish, and it will always happen + // after error + pna.nextTick(finishMaybe, stream, state); + stream._writableState.errorEmitted = true; + stream.emit('error', er); + } else { + // the caller expect this to happen before if + // it is async + cb(er); + stream._writableState.errorEmitted = true; + stream.emit('error', er); + // this can emit finish, but finish must + // always follow error + finishMaybe(stream, state); + } + } + + function onwriteStateUpdate (state) { + state.writing = false; + state.writecb = null; + state.length -= state.writelen; + state.writelen = 0; + } + + function onwrite (stream, er) { + var state = stream._writableState; + var sync = state.sync; + var cb = state.writecb; + + onwriteStateUpdate(state); + + if (er) onwriteError(stream, state, sync, er, cb); else { + // Check if we're actually ready to finish, but don't emit yet + var finished = needFinish(state); + + if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) { + clearBuffer(stream, state); + } + + if (sync) { + /* */ + asyncWrite(afterWrite, stream, state, finished, cb); + /* */ + } else { + afterWrite(stream, state, finished, cb); + } + } + } + + function afterWrite (stream, state, finished, cb) { + if (!finished) onwriteDrain(stream, state); + state.pendingcb--; + cb(); + finishMaybe(stream, state); + } + +// Must force callback to be called on nextTick, so that we don't +// emit 'drain' before the write() consumer gets the 'false' return +// value, and has a chance to attach a 'drain' listener. + function onwriteDrain (stream, state) { + if (state.length === 0 && state.needDrain) { + state.needDrain = false; + stream.emit('drain'); + } + } + +// if there's something in the buffer waiting, then process it + function clearBuffer (stream, state) { + state.bufferProcessing = true; + var entry = state.bufferedRequest; + + if (stream._writev && entry && entry.next) { + // Fast case, write everything using _writev() + var l = state.bufferedRequestCount; + var buffer = new Array(l); + var holder = state.corkedRequestsFree; + holder.entry = entry; + + var count = 0; + var allBuffers = true; + while (entry) { + buffer[count] = entry; + if (!entry.isBuf) allBuffers = false; + entry = entry.next; + count += 1; + } + buffer.allBuffers = allBuffers; + + doWrite(stream, state, true, state.length, buffer, '', holder.finish); + + // doWrite is almost always async, defer these to save a bit of time + // as the hot path ends with doWrite + state.pendingcb++; + state.lastBufferedRequest = null; + if (holder.next) { + state.corkedRequestsFree = holder.next; + holder.next = null; + } else { + state.corkedRequestsFree = new CorkedRequest(state); + } + state.bufferedRequestCount = 0; + } else { + // Slow case, write chunks one-by-one + while (entry) { + var chunk = entry.chunk; + var encoding = entry.encoding; + var cb = entry.callback; + var len = state.objectMode ? 1 : chunk.length; + + doWrite(stream, state, false, len, chunk, encoding, cb); + entry = entry.next; + state.bufferedRequestCount--; + // if we didn't call the onwrite immediately, then + // it means that we need to wait until it does. + // also, that means that the chunk and cb are currently + // being processed, so move the buffer counter past them. + if (state.writing) { + break; + } + } + + if (entry === null) state.lastBufferedRequest = null; + } + + state.bufferedRequest = entry; + state.bufferProcessing = false; + } + + Writable.prototype._write = function (chunk, encoding, cb) { + cb(new Error('_write() is not implemented')); + }; + + Writable.prototype._writev = null; + + Writable.prototype.end = function (chunk, encoding, cb) { + var state = this._writableState; + + if (typeof chunk === 'function') { + cb = chunk; + chunk = null; + encoding = null; + } else if (typeof encoding === 'function') { + cb = encoding; + encoding = null; + } + + if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); + + // .end() fully uncorks + if (state.corked) { + state.corked = 1; + this.uncork(); + } + + // ignore unnecessary end() calls. + if (!state.ending && !state.finished) endWritable(this, state, cb); + }; + + function needFinish (state) { + return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing; + } + function callFinal (stream, state) { + stream._final(function (err) { + state.pendingcb--; + if (err) { + stream.emit('error', err); + } + state.prefinished = true; + stream.emit('prefinish'); + finishMaybe(stream, state); + }); + } + function prefinish (stream, state) { + if (!state.prefinished && !state.finalCalled) { + if (typeof stream._final === 'function') { + state.pendingcb++; + state.finalCalled = true; + pna.nextTick(callFinal, stream, state); + } else { + state.prefinished = true; + stream.emit('prefinish'); + } + } + } + + function finishMaybe (stream, state) { + var need = needFinish(state); + if (need) { + prefinish(stream, state); + if (state.pendingcb === 0) { + state.finished = true; + stream.emit('finish'); + } + } + return need; + } + + function endWritable (stream, state, cb) { + state.ending = true; + finishMaybe(stream, state); + if (cb) { + if (state.finished) pna.nextTick(cb); else stream.once('finish', cb); + } + state.ended = true; + stream.writable = false; + } + + function onCorkedFinish (corkReq, state, err) { + var entry = corkReq.entry; + corkReq.entry = null; + while (entry) { + var cb = entry.callback; + state.pendingcb--; + cb(err); + entry = entry.next; + } + if (state.corkedRequestsFree) { + state.corkedRequestsFree.next = corkReq; + } else { + state.corkedRequestsFree = corkReq; + } + } + + Object.defineProperty(Writable.prototype, 'destroyed', { + get: function () { + if (this._writableState === undefined) { + return false; + } + return this._writableState.destroyed; + }, + set: function (value) { + // we ignore the value if the stream + // has not been initialized yet + if (!this._writableState) { + return; + } + + // backward compatibility, the user is explicitly + // managing destroyed + this._writableState.destroyed = value; + } + }); + + Writable.prototype.destroy = destroyImpl.destroy; + Writable.prototype._undestroy = destroyImpl.undestroy; + Writable.prototype._destroy = function (err, cb) { + this.end(); + cb(err); + }; + }).call(this, require('_process'), typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}, require('timers').setImmediate); + }, {'./_stream_duplex': 72, './internal/streams/destroy': 78, './internal/streams/stream': 79, '_process': 70, 'core-util-is': 44, 'inherits': 56, 'process-nextick-args': 69, 'safe-buffer': 84, 'timers': 87, 'util-deprecate': 88}], + 77: [function (require, module, exports) { + 'use strict'; + + function _classCallCheck (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } + + var Buffer = require('safe-buffer').Buffer; + var util = require('util'); + + function copyBuffer (src, target, offset) { + src.copy(target, offset); + } + + module.exports = (function () { + function BufferList () { + _classCallCheck(this, BufferList); + + this.head = null; + this.tail = null; + this.length = 0; + } + + BufferList.prototype.push = function push (v) { + var entry = { data: v, next: null }; + if (this.length > 0) this.tail.next = entry; else this.head = entry; + this.tail = entry; + ++this.length; + }; + + BufferList.prototype.unshift = function unshift (v) { + var entry = { data: v, next: this.head }; + if (this.length === 0) this.tail = entry; + this.head = entry; + ++this.length; + }; + + BufferList.prototype.shift = function shift () { + if (this.length === 0) return; + var ret = this.head.data; + if (this.length === 1) this.head = this.tail = null; else this.head = this.head.next; + --this.length; + return ret; + }; + + BufferList.prototype.clear = function clear () { + this.head = this.tail = null; + this.length = 0; + }; + + BufferList.prototype.join = function join (s) { + if (this.length === 0) return ''; + var p = this.head; + var ret = '' + p.data; + while (p = p.next) { + ret += s + p.data; + } return ret; + }; + + BufferList.prototype.concat = function concat (n) { + if (this.length === 0) return Buffer.alloc(0); + if (this.length === 1) return this.head.data; + var ret = Buffer.allocUnsafe(n >>> 0); + var p = this.head; + var i = 0; + while (p) { + copyBuffer(p.data, ret, i); + i += p.data.length; + p = p.next; + } + return ret; + }; + + return BufferList; + }()); + + if (util && util.inspect && util.inspect.custom) { + module.exports.prototype[util.inspect.custom] = function () { + var obj = util.inspect({ length: this.length }); + return this.constructor.name + ' ' + obj; + }; + } + }, {'safe-buffer': 84, 'util': 40}], + 78: [function (require, module, exports) { + 'use strict'; + +/* */ + + var pna = require('process-nextick-args'); +/* */ + +// undocumented cb() API, needed for core, not for public API + function destroy (err, cb) { + var _this = this; + + var readableDestroyed = this._readableState && this._readableState.destroyed; + var writableDestroyed = this._writableState && this._writableState.destroyed; + + if (readableDestroyed || writableDestroyed) { + if (cb) { + cb(err); + } else if (err && (!this._writableState || !this._writableState.errorEmitted)) { + pna.nextTick(emitErrorNT, this, err); + } + return this; + } + + // we set destroyed to true before firing error callbacks in order + // to make it re-entrance safe in case destroy() is called within callbacks + + if (this._readableState) { + this._readableState.destroyed = true; + } + + // if this is a duplex stream mark the writable part as destroyed as well + if (this._writableState) { + this._writableState.destroyed = true; + } + + this._destroy(err || null, function (err) { + if (!cb && err) { + pna.nextTick(emitErrorNT, _this, err); + if (_this._writableState) { + _this._writableState.errorEmitted = true; + } + } else if (cb) { + cb(err); + } + }); + + return this; + } + + function undestroy () { + if (this._readableState) { + this._readableState.destroyed = false; + this._readableState.reading = false; + this._readableState.ended = false; + this._readableState.endEmitted = false; + } + + if (this._writableState) { + this._writableState.destroyed = false; + this._writableState.ended = false; + this._writableState.ending = false; + this._writableState.finished = false; + this._writableState.errorEmitted = false; + } + } + + function emitErrorNT (self, err) { + self.emit('error', err); + } + + module.exports = { + destroy: destroy, + undestroy: undestroy + }; + }, {'process-nextick-args': 69}], + 79: [function (require, module, exports) { + module.exports = require('events').EventEmitter; + + }, {'events': 50}], + 80: [function (require, module, exports) { + module.exports = require('./readable').PassThrough; + + }, {'./readable': 81}], + 81: [function (require, module, exports) { + exports = module.exports = require('./lib/_stream_readable.js'); + exports.Stream = exports; + exports.Readable = exports; + exports.Writable = require('./lib/_stream_writable.js'); + exports.Duplex = require('./lib/_stream_duplex.js'); + exports.Transform = require('./lib/_stream_transform.js'); + exports.PassThrough = require('./lib/_stream_passthrough.js'); + + }, {'./lib/_stream_duplex.js': 72, './lib/_stream_passthrough.js': 73, './lib/_stream_readable.js': 74, './lib/_stream_transform.js': 75, './lib/_stream_writable.js': 76}], + 82: [function (require, module, exports) { + module.exports = require('./readable').Transform; + + }, {'./readable': 81}], + 83: [function (require, module, exports) { + module.exports = require('./lib/_stream_writable.js'); + + }, {'./lib/_stream_writable.js': 76}], + 84: [function (require, module, exports) { +/* eslint-disable node/no-deprecated-api */ + var buffer = require('buffer'); + var Buffer = buffer.Buffer; + +// alternative to using Object.keys for old browsers + function copyProps (src, dst) { + for (var key in src) { + dst[key] = src[key]; + } + } + if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) { + module.exports = buffer; + } else { + // Copy properties from require('buffer') + copyProps(buffer, exports); + exports.Buffer = SafeBuffer; + } + + function SafeBuffer (arg, encodingOrOffset, length) { + return Buffer(arg, encodingOrOffset, length); + } + +// Copy static methods from Buffer + copyProps(Buffer, SafeBuffer); + + SafeBuffer.from = function (arg, encodingOrOffset, length) { + if (typeof arg === 'number') { + throw new TypeError('Argument must not be a number'); + } + return Buffer(arg, encodingOrOffset, length); + }; + + SafeBuffer.alloc = function (size, fill, encoding) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number'); + } + var buf = Buffer(size); + if (fill !== undefined) { + if (typeof encoding === 'string') { + buf.fill(fill, encoding); + } else { + buf.fill(fill); + } + } else { + buf.fill(0); + } + return buf; + }; + + SafeBuffer.allocUnsafe = function (size) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number'); + } + return Buffer(size); + }; + + SafeBuffer.allocUnsafeSlow = function (size) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number'); + } + return buffer.SlowBuffer(size); + }; + + }, {'buffer': 43}], + 85: [function (require, module, exports) { +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + + module.exports = Stream; + + var EE = require('events').EventEmitter; + var inherits = require('inherits'); + + inherits(Stream, EE); + Stream.Readable = require('readable-stream/readable.js'); + Stream.Writable = require('readable-stream/writable.js'); + Stream.Duplex = require('readable-stream/duplex.js'); + Stream.Transform = require('readable-stream/transform.js'); + Stream.PassThrough = require('readable-stream/passthrough.js'); + +// Backwards-compat with node 0.4.x + Stream.Stream = Stream; + +// old-style streams. Note that the pipe method (the only relevant +// part of this class) is overridden in the Readable class. + + function Stream () { + EE.call(this); + } + + Stream.prototype.pipe = function (dest, options) { + var source = this; + + function ondata (chunk) { + if (dest.writable) { + if (dest.write(chunk) === false && source.pause) { + source.pause(); + } + } + } + + source.on('data', ondata); + + function ondrain () { + if (source.readable && source.resume) { + source.resume(); + } + } + + dest.on('drain', ondrain); + + // If the 'end' option is not supplied, dest.end() will be called when + // source gets the 'end' or 'close' events. Only dest.end() once. + if (!dest._isStdio && (!options || options.end !== false)) { + source.on('end', onend); + source.on('close', onclose); + } + + var didOnEnd = false; + function onend () { + if (didOnEnd) return; + didOnEnd = true; + + dest.end(); + } + + function onclose () { + if (didOnEnd) return; + didOnEnd = true; + + if (typeof dest.destroy === 'function') dest.destroy(); + } + + // don't leave dangling pipes when there are errors. + function onerror (er) { + cleanup(); + if (EE.listenerCount(this, 'error') === 0) { + throw er; // Unhandled stream error in pipe. + } + } + + source.on('error', onerror); + dest.on('error', onerror); + + // remove all the event listeners that were added. + function cleanup () { + source.removeListener('data', ondata); + dest.removeListener('drain', ondrain); + + source.removeListener('end', onend); + source.removeListener('close', onclose); + + source.removeListener('error', onerror); + dest.removeListener('error', onerror); + + source.removeListener('end', cleanup); + source.removeListener('close', cleanup); + + dest.removeListener('close', cleanup); + } + + source.on('end', cleanup); + source.on('close', cleanup); + + dest.on('close', cleanup); + + dest.emit('pipe', source); + + // Allow for unix-like usage: A.pipe(B).pipe(C) + return dest; + }; + + }, {'events': 50, 'inherits': 56, 'readable-stream/duplex.js': 71, 'readable-stream/passthrough.js': 80, 'readable-stream/readable.js': 81, 'readable-stream/transform.js': 82, 'readable-stream/writable.js': 83}], + 86: [function (require, module, exports) { +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + + 'use strict'; + +/* */ + + var Buffer = require('safe-buffer').Buffer; +/* */ + + var isEncoding = Buffer.isEncoding || function (encoding) { + encoding = '' + encoding; + switch (encoding && encoding.toLowerCase()) { + case 'hex':case 'utf8':case 'utf-8':case 'ascii':case 'binary':case 'base64':case 'ucs2':case 'ucs-2':case 'utf16le':case 'utf-16le':case 'raw': + return true; + default: + return false; + } + }; + + function _normalizeEncoding (enc) { + if (!enc) return 'utf8'; + var retried; + while (true) { + switch (enc) { + case 'utf8': + case 'utf-8': + return 'utf8'; + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return 'utf16le'; + case 'latin1': + case 'binary': + return 'latin1'; + case 'base64': + case 'ascii': + case 'hex': + return enc; + default: + if (retried) return; // undefined + enc = ('' + enc).toLowerCase(); + retried = true; + } + } + } + +// Do not cache `Buffer.isEncoding` when checking encoding names as some +// modules monkey-patch it to support additional encodings + function normalizeEncoding (enc) { + var nenc = _normalizeEncoding(enc); + if (typeof nenc !== 'string' && (Buffer.isEncoding === isEncoding || !isEncoding(enc))) throw new Error('Unknown encoding: ' + enc); + return nenc || enc; + } + +// StringDecoder provides an interface for efficiently splitting a series of +// buffers into a series of JS strings without breaking apart multi-byte +// characters. + exports.StringDecoder = StringDecoder; + function StringDecoder (encoding) { + this.encoding = normalizeEncoding(encoding); + var nb; + switch (this.encoding) { + case 'utf16le': + this.text = utf16Text; + this.end = utf16End; + nb = 4; + break; + case 'utf8': + this.fillLast = utf8FillLast; + nb = 4; + break; + case 'base64': + this.text = base64Text; + this.end = base64End; + nb = 3; + break; + default: + this.write = simpleWrite; + this.end = simpleEnd; + return; + } + this.lastNeed = 0; + this.lastTotal = 0; + this.lastChar = Buffer.allocUnsafe(nb); + } + + StringDecoder.prototype.write = function (buf) { + if (buf.length === 0) return ''; + var r; + var i; + if (this.lastNeed) { + r = this.fillLast(buf); + if (r === undefined) return ''; + i = this.lastNeed; + this.lastNeed = 0; + } else { + i = 0; + } + if (i < buf.length) return r ? r + this.text(buf, i) : this.text(buf, i); + return r || ''; + }; + + StringDecoder.prototype.end = utf8End; + +// Returns only complete characters in a Buffer + StringDecoder.prototype.text = utf8Text; + +// Attempts to complete a partial non-UTF-8 character using bytes from a Buffer + StringDecoder.prototype.fillLast = function (buf) { + if (this.lastNeed <= buf.length) { + buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed); + return this.lastChar.toString(this.encoding, 0, this.lastTotal); + } + buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length); + this.lastNeed -= buf.length; + }; + +// Checks the type of a UTF-8 byte, whether it's ASCII, a leading byte, or a +// continuation byte. If an invalid byte is detected, -2 is returned. + function utf8CheckByte (byte) { + if (byte <= 0x7F) return 0; else if (byte >> 5 === 0x06) return 2; else if (byte >> 4 === 0x0E) return 3; else if (byte >> 3 === 0x1E) return 4; + return byte >> 6 === 0x02 ? -1 : -2; + } + +// Checks at most 3 bytes at the end of a Buffer in order to detect an +// incomplete multi-byte UTF-8 character. The total number of bytes (2, 3, or 4) +// needed to complete the UTF-8 character (if applicable) are returned. + function utf8CheckIncomplete (self, buf, i) { + var j = buf.length - 1; + if (j < i) return 0; + var nb = utf8CheckByte(buf[j]); + if (nb >= 0) { + if (nb > 0) self.lastNeed = nb - 1; + return nb; + } + if (--j < i || nb === -2) return 0; + nb = utf8CheckByte(buf[j]); + if (nb >= 0) { + if (nb > 0) self.lastNeed = nb - 2; + return nb; + } + if (--j < i || nb === -2) return 0; + nb = utf8CheckByte(buf[j]); + if (nb >= 0) { + if (nb > 0) { + if (nb === 2) nb = 0; else self.lastNeed = nb - 3; + } + return nb; + } + return 0; + } + +// Validates as many continuation bytes for a multi-byte UTF-8 character as +// needed or are available. If we see a non-continuation byte where we expect +// one, we "replace" the validated continuation bytes we've seen so far with +// a single UTF-8 replacement character ('\ufffd'), to match v8's UTF-8 decoding +// behavior. The continuation byte check is included three times in the case +// where all of the continuation bytes for a character exist in the same buffer. +// It is also done this way as a slight performance increase instead of using a +// loop. + function utf8CheckExtraBytes (self, buf, p) { + if ((buf[0] & 0xC0) !== 0x80) { + self.lastNeed = 0; + return '\ufffd'; + } + if (self.lastNeed > 1 && buf.length > 1) { + if ((buf[1] & 0xC0) !== 0x80) { + self.lastNeed = 1; + return '\ufffd'; + } + if (self.lastNeed > 2 && buf.length > 2) { + if ((buf[2] & 0xC0) !== 0x80) { + self.lastNeed = 2; + return '\ufffd'; + } + } + } + } + +// Attempts to complete a multi-byte UTF-8 character using bytes from a Buffer. + function utf8FillLast (buf) { + var p = this.lastTotal - this.lastNeed; + var r = utf8CheckExtraBytes(this, buf, p); + if (r !== undefined) return r; + if (this.lastNeed <= buf.length) { + buf.copy(this.lastChar, p, 0, this.lastNeed); + return this.lastChar.toString(this.encoding, 0, this.lastTotal); + } + buf.copy(this.lastChar, p, 0, buf.length); + this.lastNeed -= buf.length; + } + +// Returns all complete UTF-8 characters in a Buffer. If the Buffer ended on a +// partial character, the character's bytes are buffered until the required +// number of bytes are available. + function utf8Text (buf, i) { + var total = utf8CheckIncomplete(this, buf, i); + if (!this.lastNeed) return buf.toString('utf8', i); + this.lastTotal = total; + var end = buf.length - (total - this.lastNeed); + buf.copy(this.lastChar, 0, end); + return buf.toString('utf8', i, end); + } + +// For UTF-8, a replacement character is added when ending on a partial +// character. + function utf8End (buf) { + var r = buf && buf.length ? this.write(buf) : ''; + if (this.lastNeed) return r + '\ufffd'; + return r; + } + +// UTF-16LE typically needs two bytes per character, but even if we have an even +// number of bytes available, we need to check if we end on a leading/high +// surrogate. In that case, we need to wait for the next two bytes in order to +// decode the last character properly. + function utf16Text (buf, i) { + if ((buf.length - i) % 2 === 0) { + var r = buf.toString('utf16le', i); + if (r) { + var c = r.charCodeAt(r.length - 1); + if (c >= 0xD800 && c <= 0xDBFF) { + this.lastNeed = 2; + this.lastTotal = 4; + this.lastChar[0] = buf[buf.length - 2]; + this.lastChar[1] = buf[buf.length - 1]; + return r.slice(0, -1); + } + } + return r; + } + this.lastNeed = 1; + this.lastTotal = 2; + this.lastChar[0] = buf[buf.length - 1]; + return buf.toString('utf16le', i, buf.length - 1); + } + +// For UTF-16LE we do not explicitly append special replacement characters if we +// end on a partial character, we simply let v8 handle that. + function utf16End (buf) { + var r = buf && buf.length ? this.write(buf) : ''; + if (this.lastNeed) { + var end = this.lastTotal - this.lastNeed; + return r + this.lastChar.toString('utf16le', 0, end); + } + return r; + } + + function base64Text (buf, i) { + var n = (buf.length - i) % 3; + if (n === 0) return buf.toString('base64', i); + this.lastNeed = 3 - n; + this.lastTotal = 3; + if (n === 1) { + this.lastChar[0] = buf[buf.length - 1]; + } else { + this.lastChar[0] = buf[buf.length - 2]; + this.lastChar[1] = buf[buf.length - 1]; + } + return buf.toString('base64', i, buf.length - n); + } + + function base64End (buf) { + var r = buf && buf.length ? this.write(buf) : ''; + if (this.lastNeed) return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed); + return r; + } + +// Pass bytes on through for single-byte encodings (e.g. ascii, latin1, hex) + function simpleWrite (buf) { + return buf.toString(this.encoding); + } + + function simpleEnd (buf) { + return buf && buf.length ? this.write(buf) : ''; + } + }, {'safe-buffer': 84}], + 87: [function (require, module, exports) { + (function (setImmediate, clearImmediate) { + var nextTick = require('process/browser.js').nextTick; + var apply = Function.prototype.apply; + var slice = Array.prototype.slice; + var immediateIds = {}; + var nextImmediateId = 0; + +// DOM APIs, for completeness + + exports.setTimeout = function () { + return new Timeout(apply.call(setTimeout, window, arguments), clearTimeout); + }; + exports.setInterval = function () { + return new Timeout(apply.call(setInterval, window, arguments), clearInterval); + }; + exports.clearTimeout = +exports.clearInterval = function (timeout) { timeout.close(); }; + + function Timeout (id, clearFn) { + this._id = id; + this._clearFn = clearFn; + } + Timeout.prototype.unref = Timeout.prototype.ref = function () {}; + Timeout.prototype.close = function () { + this._clearFn.call(window, this._id); + }; + +// Does not start the time, just sets up the members needed. + exports.enroll = function (item, msecs) { + clearTimeout(item._idleTimeoutId); + item._idleTimeout = msecs; + }; + + exports.unenroll = function (item) { + clearTimeout(item._idleTimeoutId); + item._idleTimeout = -1; + }; + + exports._unrefActive = exports.active = function (item) { + clearTimeout(item._idleTimeoutId); + + var msecs = item._idleTimeout; + if (msecs >= 0) { + item._idleTimeoutId = setTimeout(function onTimeout () { + if (item._onTimeout) { item._onTimeout(); } + }, msecs); + } + }; + +// That's not how node.js implements it but the exposed api is the same. + exports.setImmediate = typeof setImmediate === 'function' ? setImmediate : function (fn) { + var id = nextImmediateId++; + var args = arguments.length < 2 ? false : slice.call(arguments, 1); + + immediateIds[id] = true; + + nextTick(function onNextTick () { + if (immediateIds[id]) { + // fn.call() is faster so we optimize for the common use-case + // @see http://jsperf.com/call-apply-segu + if (args) { + fn.apply(null, args); + } else { + fn.call(null); + } + // Prevent ids from leaking + exports.clearImmediate(id); + } + }); + + return id; + }; + + exports.clearImmediate = typeof clearImmediate === 'function' ? clearImmediate : function (id) { + delete immediateIds[id]; + }; + }).call(this, require('timers').setImmediate, require('timers').clearImmediate); + }, {'process/browser.js': 70, 'timers': 87}], + 88: [function (require, module, exports) { + (function (global) { + +/** + * Module exports. + */ + + module.exports = deprecate; + +/** + * Mark that a method should not be used. + * Returns a modified function which warns once by default. + * + * If `localStorage.noDeprecation = true` is set, then it is a no-op. + * + * If `localStorage.throwDeprecation = true` is set, then deprecated functions + * will throw an Error when invoked. + * + * If `localStorage.traceDeprecation = true` is set, then deprecated functions + * will invoke `console.trace()` instead of `console.error()`. + * + * @param {Function} fn - the function to deprecate + * @param {String} msg - the string to print to the console when `fn` is invoked + * @returns {Function} a new "deprecated" version of `fn` + * @api public + */ + + function deprecate (fn, msg) { + if (config('noDeprecation')) { + return fn; + } + + var warned = false; + function deprecated () { + if (!warned) { + if (config('throwDeprecation')) { + throw new Error(msg); + } else if (config('traceDeprecation')) { + console.trace(msg); + } else { + console.warn(msg); + } + warned = true; + } + return fn.apply(this, arguments); + } + + return deprecated; + } + +/** + * Checks `localStorage` for boolean values for the given `name`. + * + * @param {String} name + * @returns {Boolean} + * @api private + */ + + function config (name) { + // accessing global.localStorage can trigger a DOMException in sandboxed iframes + try { + if (!global.localStorage) return false; + } catch (_) { + return false; + } + var val = global.localStorage[name]; + if (val == null) return false; + return String(val).toLowerCase() === 'true'; + } + + }).call(this, typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}); + }, {}], + 89: [function (require, module, exports) { + module.exports = function isBuffer (arg) { + return arg && typeof arg === 'object' + && typeof arg.copy === 'function' + && typeof arg.fill === 'function' + && typeof arg.readUInt8 === 'function'; + }; + }, {}], + 90: [function (require, module, exports) { + (function (process, global) { +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + + var formatRegExp = /%[sdj%]/g; + exports.format = function (f) { + if (!isString(f)) { + var objects = []; + for (var i = 0; i < arguments.length; i++) { + objects.push(inspect(arguments[i])); + } + return objects.join(' '); + } + + var i = 1; + var args = arguments; + var len = args.length; + var str = String(f).replace(formatRegExp, function (x) { + if (x === '%%') return '%'; + if (i >= len) return x; + switch (x) { + case '%s': return String(args[i++]); + case '%d': return Number(args[i++]); + case '%j': + try { + return JSON.stringify(args[i++]); + } catch (_) { + return '[Circular]'; + } + default: + return x; + } + }); + for (var x = args[i]; i < len; x = args[++i]) { + if (isNull(x) || !isObject(x)) { + str += ' ' + x; + } else { + str += ' ' + inspect(x); + } + } + return str; + }; + +// Mark that a method should not be used. +// Returns a modified function which warns once by default. +// If --no-deprecation is set, then it is a no-op. + exports.deprecate = function (fn, msg) { + // Allow for deprecating things in the process of starting up. + if (isUndefined(global.process)) { + return function () { + return exports.deprecate(fn, msg).apply(this, arguments); + }; + } + + if (process.noDeprecation === true) { + return fn; + } + + var warned = false; + function deprecated () { + if (!warned) { + if (process.throwDeprecation) { + throw new Error(msg); + } else if (process.traceDeprecation) { + console.trace(msg); + } else { + console.error(msg); + } + warned = true; + } + return fn.apply(this, arguments); + } + + return deprecated; + }; + + var debugs = {}; + var debugEnviron; + exports.debuglog = function (set) { + if (isUndefined(debugEnviron)) { debugEnviron = process.env.NODE_DEBUG || ''; } + set = set.toUpperCase(); + if (!debugs[set]) { + if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) { + var pid = process.pid; + debugs[set] = function () { + var msg = exports.format.apply(exports, arguments); + console.error('%s %d: %s', set, pid, msg); + }; + } else { + debugs[set] = function () {}; + } + } + return debugs[set]; + }; + +/** + * Echos the value of a value. Trys to print the value out + * in the best way possible given the different types. + * + * @param {Object} obj The object to print out. + * @param {Object} opts Optional options object that alters the output. + */ +/* legacy: obj, showHidden, depth, colors */ + function inspect (obj, opts) { + // default options + var ctx = { + seen: [], + stylize: stylizeNoColor + }; + // legacy... + if (arguments.length >= 3) ctx.depth = arguments[2]; + if (arguments.length >= 4) ctx.colors = arguments[3]; + if (isBoolean(opts)) { + // legacy... + ctx.showHidden = opts; + } else if (opts) { + // got an "options" object + exports._extend(ctx, opts); + } + // set default options + if (isUndefined(ctx.showHidden)) ctx.showHidden = false; + if (isUndefined(ctx.depth)) ctx.depth = 2; + if (isUndefined(ctx.colors)) ctx.colors = false; + if (isUndefined(ctx.customInspect)) ctx.customInspect = true; + if (ctx.colors) ctx.stylize = stylizeWithColor; + return formatValue(ctx, obj, ctx.depth); + } + exports.inspect = inspect; + +// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics + inspect.colors = { + 'bold': [1, 22], + 'italic': [3, 23], + 'underline': [4, 24], + 'inverse': [7, 27], + 'white': [37, 39], + 'grey': [90, 39], + 'black': [30, 39], + 'blue': [34, 39], + 'cyan': [36, 39], + 'green': [32, 39], + 'magenta': [35, 39], + 'red': [31, 39], + 'yellow': [33, 39] + }; + +// Don't use 'blue' not visible on cmd.exe + inspect.styles = { + 'special': 'cyan', + 'number': 'yellow', + 'boolean': 'yellow', + 'undefined': 'grey', + 'null': 'bold', + 'string': 'green', + 'date': 'magenta', + // "name": intentionally not styling + 'regexp': 'red' + }; + + function stylizeWithColor (str, styleType) { + var style = inspect.styles[styleType]; + + if (style) { + return '\u001b[' + inspect.colors[style][0] + 'm' + str + + '\u001b[' + inspect.colors[style][1] + 'm'; + } else { + return str; + } + } + + function stylizeNoColor (str, styleType) { + return str; + } + + function arrayToHash (array) { + var hash = {}; + + array.forEach(function (val, idx) { + hash[val] = true; + }); + + return hash; + } + + function formatValue (ctx, value, recurseTimes) { + // Provide a hook for user-specified inspect functions. + // Check that value is an object with an inspect function on it + if (ctx.customInspect && + value && + isFunction(value.inspect) && + // Filter out the util module, it's inspect function is special + value.inspect !== exports.inspect && + // Also filter out any prototype objects using the circular check. + !(value.constructor && value.constructor.prototype === value)) { + var ret = value.inspect(recurseTimes, ctx); + if (!isString(ret)) { + ret = formatValue(ctx, ret, recurseTimes); + } + return ret; + } + + // Primitive types cannot have properties + var primitive = formatPrimitive(ctx, value); + if (primitive) { + return primitive; + } + + // Look up the keys of the object. + var keys = Object.keys(value); + var visibleKeys = arrayToHash(keys); + + if (ctx.showHidden) { + keys = Object.getOwnPropertyNames(value); + } + + // IE doesn't make error fields non-enumerable + // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx + if (isError(value) + && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) { + return formatError(value); + } + + // Some type of object without properties can be shortcutted. + if (keys.length === 0) { + if (isFunction(value)) { + var name = value.name ? ': ' + value.name : ''; + return ctx.stylize('[Function' + name + ']', 'special'); + } + if (isRegExp(value)) { + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); + } + if (isDate(value)) { + return ctx.stylize(Date.prototype.toString.call(value), 'date'); + } + if (isError(value)) { + return formatError(value); + } + } + + var base = '', array = false, braces = ['{', '}']; + + // Make Array say that they are Array + if (isArray(value)) { + array = true; + braces = ['[', ']']; + } + + // Make functions say that they are functions + if (isFunction(value)) { + var n = value.name ? ': ' + value.name : ''; + base = ' [Function' + n + ']'; + } + + // Make RegExps say that they are RegExps + if (isRegExp(value)) { + base = ' ' + RegExp.prototype.toString.call(value); + } + + // Make dates with properties first say the date + if (isDate(value)) { + base = ' ' + Date.prototype.toUTCString.call(value); + } + + // Make error with message first say the error + if (isError(value)) { + base = ' ' + formatError(value); + } + + if (keys.length === 0 && (!array || value.length == 0)) { + return braces[0] + base + braces[1]; + } + + if (recurseTimes < 0) { + if (isRegExp(value)) { + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); + } else { + return ctx.stylize('[Object]', 'special'); + } + } + + ctx.seen.push(value); + + var output; + if (array) { + output = formatArray(ctx, value, recurseTimes, visibleKeys, keys); + } else { + output = keys.map(function (key) { + return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array); + }); + } + + ctx.seen.pop(); + + return reduceToSingleString(output, base, braces); + } + + function formatPrimitive (ctx, value) { + if (isUndefined(value)) { return ctx.stylize('undefined', 'undefined'); } + if (isString(value)) { + var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') + .replace(/'/g, "\\'") + .replace(/\\"/g, '"') + '\''; + return ctx.stylize(simple, 'string'); + } + if (isNumber(value)) { return ctx.stylize('' + value, 'number'); } + if (isBoolean(value)) { return ctx.stylize('' + value, 'boolean'); } + // For some reason typeof null is "object", so special case here. + if (isNull(value)) { return ctx.stylize('null', 'null'); } + } + + function formatError (value) { + return '[' + Error.prototype.toString.call(value) + ']'; + } + + function formatArray (ctx, value, recurseTimes, visibleKeys, keys) { + var output = []; + for (var i = 0, l = value.length; i < l; ++i) { + if (hasOwnProperty(value, String(i))) { + output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, + String(i), true)); + } else { + output.push(''); + } + } + keys.forEach(function (key) { + if (!key.match(/^\d+$/)) { + output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, + key, true)); + } + }); + return output; + } + + function formatProperty (ctx, value, recurseTimes, visibleKeys, key, array) { + var name, str, desc; + desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] }; + if (desc.get) { + if (desc.set) { + str = ctx.stylize('[Getter/Setter]', 'special'); + } else { + str = ctx.stylize('[Getter]', 'special'); + } + } else { + if (desc.set) { + str = ctx.stylize('[Setter]', 'special'); + } + } + if (!hasOwnProperty(visibleKeys, key)) { + name = '[' + key + ']'; + } + if (!str) { + if (ctx.seen.indexOf(desc.value) < 0) { + if (isNull(recurseTimes)) { + str = formatValue(ctx, desc.value, null); + } else { + str = formatValue(ctx, desc.value, recurseTimes - 1); + } + if (str.indexOf('\n') > -1) { + if (array) { + str = str.split('\n').map(function (line) { + return ' ' + line; + }).join('\n').substr(2); + } else { + str = '\n' + str.split('\n').map(function (line) { + return ' ' + line; + }).join('\n'); + } + } + } else { + str = ctx.stylize('[Circular]', 'special'); + } + } + if (isUndefined(name)) { + if (array && key.match(/^\d+$/)) { + return str; + } + name = JSON.stringify('' + key); + if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { + name = name.substr(1, name.length - 2); + name = ctx.stylize(name, 'name'); + } else { + name = name.replace(/'/g, "\\'") + .replace(/\\"/g, '"') + .replace(/(^"|"$)/g, "'"); + name = ctx.stylize(name, 'string'); + } + } + + return name + ': ' + str; + } + + function reduceToSingleString (output, base, braces) { + var numLinesEst = 0; + var length = output.reduce(function (prev, cur) { + numLinesEst++; + if (cur.indexOf('\n') >= 0) numLinesEst++; + return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1; + }, 0); + + if (length > 60) { + return braces[0] + + (base === '' ? '' : base + '\n ') + + ' ' + + output.join(',\n ') + + ' ' + + braces[1]; + } + + return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; + } + +// NOTE: These type checking functions intentionally don't use `instanceof` +// because it is fragile and can be easily faked with `Object.create()`. + function isArray (ar) { + return Array.isArray(ar); + } + exports.isArray = isArray; + + function isBoolean (arg) { + return typeof arg === 'boolean'; + } + exports.isBoolean = isBoolean; + + function isNull (arg) { + return arg === null; + } + exports.isNull = isNull; + + function isNullOrUndefined (arg) { + return arg == null; + } + exports.isNullOrUndefined = isNullOrUndefined; + + function isNumber (arg) { + return typeof arg === 'number'; + } + exports.isNumber = isNumber; + + function isString (arg) { + return typeof arg === 'string'; + } + exports.isString = isString; + + function isSymbol (arg) { + return typeof arg === 'symbol'; + } + exports.isSymbol = isSymbol; + + function isUndefined (arg) { + return arg === void 0; + } + exports.isUndefined = isUndefined; + + function isRegExp (re) { + return isObject(re) && objectToString(re) === '[object RegExp]'; + } + exports.isRegExp = isRegExp; + + function isObject (arg) { + return typeof arg === 'object' && arg !== null; + } + exports.isObject = isObject; + + function isDate (d) { + return isObject(d) && objectToString(d) === '[object Date]'; + } + exports.isDate = isDate; + + function isError (e) { + return isObject(e) && + (objectToString(e) === '[object Error]' || e instanceof Error); + } + exports.isError = isError; + + function isFunction (arg) { + return typeof arg === 'function'; + } + exports.isFunction = isFunction; + + function isPrimitive (arg) { + return arg === null || + typeof arg === 'boolean' || + typeof arg === 'number' || + typeof arg === 'string' || + typeof arg === 'symbol' || // ES6 symbol + typeof arg === 'undefined'; + } + exports.isPrimitive = isPrimitive; + + exports.isBuffer = require('./support/isBuffer'); + + function objectToString (o) { + return Object.prototype.toString.call(o); + } + + function pad (n) { + return n < 10 ? '0' + n.toString(10) : n.toString(10); + } + + var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', + 'Oct', 'Nov', 'Dec']; + +// 26 Feb 16:19:34 + function timestamp () { + var d = new Date(); + var time = [pad(d.getHours()), + pad(d.getMinutes()), + pad(d.getSeconds())].join(':'); + return [d.getDate(), months[d.getMonth()], time].join(' '); + } + +// log is just a thin wrapper to console.log that prepends a timestamp + exports.log = function () { + console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments)); + }; + +/** + * Inherit the prototype methods from one constructor into another. + * + * The Function.prototype.inherits from lang.js rewritten as a standalone + * function (not on Function.prototype). NOTE: If this file is to be loaded + * during bootstrapping this function needs to be rewritten using some native + * functions as prototype setup using normal JavaScript does not work as + * expected during bootstrapping (see mirror.js in r114903). + * + * @param {function} ctor Constructor function which needs to inherit the + * prototype. + * @param {function} superCtor Constructor function to inherit prototype from. + */ + exports.inherits = require('inherits'); + + exports._extend = function (origin, add) { + // Don't do anything if add isn't an object + if (!add || !isObject(add)) return origin; + + var keys = Object.keys(add); + var i = keys.length; + while (i--) { + origin[keys[i]] = add[keys[i]]; + } + return origin; + }; + + function hasOwnProperty (obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); + } + + }).call(this, require('_process'), typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : {}); + }, {'./support/isBuffer': 89, '_process': 70, 'inherits': 56}], + 91: [function (require, module, exports) { + module.exports = { + 'name': 'mocha', + 'version': '6.2.0', + 'homepage': 'https://mochajs.org/', + 'notifyLogo': 'https://ibin.co/4QuRuGjXvl36.png' + }; + }, {}]}, {}, [1]); diff --git a/tests/www/lib/readme.md b/tests/www/lib/readme.md new file mode 100644 index 0000000..7f34b2c --- /dev/null +++ b/tests/www/lib/readme.md @@ -0,0 +1,9 @@ +mocha.js and mocha.css + + are taken from https://github.com/Lindsay-Needs-Sleep/mocha.git#afa388dbf46c860748e4c387dd99010a922405ce + + This is a development version of 6.2.0 + + Importantly, it includes Pull Request #3952 + https://github.com/mochajs/mocha/pull/3952 + \ No newline at end of file diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 11a33d6..0be97b4 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -134,22 +134,6 @@ chrome.cast = { this.defaultActionPolicy = defaultActionPolicy || chrome.cast.DefaultActionPolicy.CREATE_SESSION; }, - /** - * Describes the receiver running an application. Normally, these objects should not be created by the client. - * @param {string} label An identifier for the receiver that is unique to the browser profile and the origin of the API client. - * @param {string} friendlyName The user given name for the receiver. - * @param {chrome.cast.Capability[]} capabilities The capabilities of the receiver, for example audio and video. - * @param {chrome.cast.Volume} volume The current volume of the receiver. - */ - Receiver: function (label, friendlyName, capabilities, volume) { - this.label = label; - this.friendlyName = friendlyName; - this.capabilities = capabilities || []; - this.volume = volume || null; - this.receiverType = chrome.cast.ReceiverType.CAST; - this.isActiveInput = null; - }, - /** * TODO: Update when the official API docs are finished * https://developers.google.com/cast/docs/reference/chrome/chrome.cast.DialRequest @@ -207,18 +191,6 @@ chrome.cast = { this.packageId = this.url = null; }, - /** - * The volume of a device or media stream. - * @param {number} level The current volume level as a value between 0.0 and 1.0. - * @param {boolean} muted Whether the receiver is muted, independent of the volume level. - */ - Volume: function (level, muted) { - this.level = level; - if (muted || muted === false) { - this.muted = !!muted; - } - }, - // media package media: { /** @@ -236,6 +208,24 @@ chrome.cast = { */ PlayerState: { IDLE: 'IDLE', PLAYING: 'PLAYING', PAUSED: 'PAUSED', BUFFERING: 'BUFFERING' }, + /** + * Possible reason why a media is idle. + * CANCELLED: A sender requested to stop playback using the STOP command. + * INTERRUPTED: A sender requested playing a different media using the LOAD command. + * FINISHED: The media playback completed. + * ERROR: The media was interrupted due to an error, this could be for example if the player could not download media due to networking errors. + */ + IdleReason: { CANCELLED: 'CANCELLED', INTERRUPTED: 'INTERRUPTED', FINISHED: 'FINISHED', ERROR: 'ERROR' }, + + /** + * Possible states of queue repeat mode. + * OFF: Items are played in order, and when the queue is completed (the last item has ended) the media session is terminated. + * ALL: The items in the queue will be played indefinitely. When the last item has ended, the first item will be played again. + * SINGLE: The current item will be repeated indefinitely. + * ALL_AND_SHUFFLE: The items in the queue will be played indefinitely. When the last item has ended, the list of items will be randomly shuffled by the receiver, and the queue will continue to play starting from the first item of the shuffled items. + */ + RepeatMode: { OFF: 'REPEAT_OFF', ALL: 'REPEAT_ALL', SINGLE: 'REPEAT_SINGLE', ALL_AND_SHUFFLE: 'REPEAT_ALL_AND_SHUFFLE' }, + /** * States of the media player after resuming. * PLAYBACK_PAUSE: Force media to pause. @@ -430,22 +420,6 @@ chrome.cast = { this.originalAirdate = this.releaseYear = this.images = this.episode = this.episodeNumber = this.season = this.seasonNumber = this.episodeTitle = this.title = this.seriesTitle = null; }, - /** - * Describes a media item. - * @param {string} contentId Identifies the content. - * @param {string} contentType MIME content type of the media. - * @property {Object} customData Custom data set by the receiver application. - * @property {number} duration Duration of the content, in seconds. - * @property {any type} metadata Describes the media content. - * @property {chrome.cast.media.StreamType} streamType The type of media stream. - */ - MediaInfo: function MediaInfo (contentId, contentType) { - this.contentId = contentId; - this.streamType = chrome.cast.media.StreamType.BUFFERED; - this.contentType = contentType; - this.customData = this.duration = this.metadata = null; - }, - /** * Possible media track types. */ @@ -456,17 +430,6 @@ chrome.cast = { */ TextTrackType: {SUBTITLES: 'SUBTITLES', CAPTIONS: 'CAPTIONS', DESCRIPTIONS: 'DESCRIPTIONS', CHAPTERS: 'CHAPTERS', METADATA: 'METADATA'}, - /** - * Describes track metadata information - * @param {number} trackId Unique identifier of the track within the context of a chrome.cast.media.MediaInfo objects - * @param {chrome.cast.media.TrackType} trackType The type of track. Value must not be null. - */ - Track: function Track (trackId, trackType) { - this.trackId = trackId; - this.type = trackType; - this.customData = this.language = this.name = this.subtype = this.trackContentId = this.trackContentType = null; - }, - /** * Possible text track edge types. */ @@ -563,7 +526,7 @@ chrome.cast.initialize = function (apiConfig, successCallback, errorCallback) { chrome.cast.requestSession = function (successCallback, errorCallback, opt_sessionRequest) { execute('requestSession', function (err, obj) { if (!err) { - successCallback(updateSession(obj)); + successCallback(createNewSession(obj)); } else { handleError(err, errorCallback); } @@ -607,6 +570,28 @@ chrome.cast.Session = function Session (sessionId, appId, displayName, appImages chrome.cast.Session.prototype = Object.create(EventEmitter.prototype); +function sessionPreCheck (sessionId) { + // if (this.status !== chrome.cast.SessionStatus.CONNECTED) { + if (!_session || _session.status !== chrome.cast.SessionStatus.CONNECTED) { + return new chrome.cast.Error( + chrome.cast.ErrorCode.INVALID_PARAMETER, 'No active session'); + } + if (sessionId !== _session.sessionId) { + return new chrome.cast.Error( + chrome.cast.ErrorCode.INVALID_PARAMETER, 'Unknown session ID'); + } + return false; +} + +chrome.cast.Session.prototype._preCheck = function (errorCallback) { + var err = sessionPreCheck(this.sessionId); + if (err) { + errorCallback && errorCallback(err); + return true; + } + return err; +}; + /** * Sets the receiver volume. * @param {number} newLevel The new volume level between 0.0 and 1.0. @@ -614,6 +599,7 @@ chrome.cast.Session.prototype = Object.create(EventEmitter.prototype); * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.setReceiverVolumeLevel = function (newLevel, successCallback, errorCallback) { + if (this._preCheck(errorCallback)) { return; } execute('setReceiverVolumeLevel', newLevel, function (err) { if (!err) { successCallback && successCallback(); @@ -630,6 +616,7 @@ chrome.cast.Session.prototype.setReceiverVolumeLevel = function (newLevel, succe * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.setReceiverMuted = function (muted, successCallback, errorCallback) { + if (this._preCheck(errorCallback)) { return; } execute('setReceiverMuted', muted, function (err) { if (!err) { successCallback && successCallback(); @@ -645,10 +632,7 @@ chrome.cast.Session.prototype.setReceiverMuted = function (muted, successCallbac * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, CHANNEL_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.stop = function (successCallback, errorCallback) { - if (this.status !== chrome.cast.SessionStatus.CONNECTED) { - errorCallback(new chrome.cast.Error(chrome.cast.Error.INVALID_PARAMETER, 'No active session', null)); - return; - } + if (this._preCheck(errorCallback)) { return; } execute('sessionStop', function (err) { if (!err) { successCallback && successCallback(); @@ -668,10 +652,7 @@ chrome.cast.Session.prototype.stop = function (successCallback, errorCallback) { * @param {function} errorCallback The possible errors are TIMEOUT, API_NOT_INITIALIZED, CHANNEL_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.leave = function (successCallback, errorCallback) { - if (this.status !== chrome.cast.SessionStatus.CONNECTED) { - errorCallback(new chrome.cast.Error(chrome.cast.Error.INVALID_PARAMETER, 'No active session', null)); - return; - } + if (this._preCheck(errorCallback)) { return; } execute('sessionLeave', function (err) { if (!err) { successCallback && successCallback(); @@ -695,6 +676,7 @@ chrome.cast.Session.prototype.leave = function (successCallback, errorCallback) * @param {[type]} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING */ chrome.cast.Session.prototype.sendMessage = function (namespace, message, successCallback, errorCallback) { + if (this._preCheck(errorCallback)) { return; } if (typeof message === 'object') { message = JSON.stringify(message); } @@ -714,43 +696,15 @@ chrome.cast.Session.prototype.sendMessage = function (namespace, message, succes * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.Session.prototype.loadMedia = function (loadRequest, successCallback, errorCallback) { + if (this._preCheck(errorCallback)) { return; } var self = this; var mediaInfo = loadRequest.media; execute('loadMedia', mediaInfo.contentId, mediaInfo.customData || {}, mediaInfo.contentType, mediaInfo.duration || 0.0, mediaInfo.streamType, loadRequest.autoplay || false, loadRequest.currentTime || 0, mediaInfo.metadata || {}, mediaInfo.textTrackSytle || {}, function (err, obj) { if (!err) { _currentMedia = new chrome.cast.media.Media(self.sessionId, obj.mediaSessionId); - _currentMedia.activeTrackIds = obj.activeTrackIds; - _currentMedia.currentItemId = obj.currentItemId; - _currentMedia.idleReason = obj.idleReason; - _currentMedia.loadingItemId = obj.loadingItemId; - _currentMedia.media = mediaInfo; - _currentMedia.media.duration = obj.media.duration; - _currentMedia.media.tracks = obj.media.tracks; - _currentMedia.media.customData = obj.media.customData || null; - _currentMedia.currentTime = obj.currentTime; - _currentMedia.playbackRate = obj.playbackRate; - _currentMedia.preloadedItemId = obj.preloadedItemId; - _currentMedia.volume = new chrome.cast.Volume(obj.volume.level, obj.volume.muted); - - _currentMedia.media.tracks = []; - - var track; - for (var i = 0; i < obj.media.tracks.length; i++) { - track = obj.media.tracks[i]; - var newTrack = new chrome.cast.media.Track(track.trackId, track.type); - newTrack.customData = track.customData || null; - newTrack.language = track.language || null; - newTrack.name = track.name || null; - newTrack.subtype = track.subtype || null; - newTrack.trackContentId = track.trackContentId || null; - newTrack.trackContentType = track.trackContentType || null; - - _currentMedia.media.tracks.push(newTrack); - } - + _currentMedia._update(obj); successCallback(_currentMedia); - } else { handleError(err, errorCallback); } @@ -818,27 +772,137 @@ chrome.cast.Session.prototype.removeMediaListener = function (listener) { }; chrome.cast.Session.prototype._update = function (obj) { - var isAlive = (obj.status !== chrome.cast.SessionStatus.STOPPED); - this.status = obj.status || this.status; - this.appId = obj.appId; - this.appImages = obj.appImages; - this.displayName = obj.displayName; + for (var attr in obj) { + if (['receiver', 'media'].indexOf(attr) === -1) { + this[attr] = obj[attr]; + } + } if (obj.receiver) { if (!this.receiver) { - this.receiver = new chrome.cast.Receiver(null, null, null, null); + this.receiver = new chrome.cast.Receiver(); } - this.receiver.friendlyName = obj.receiver.friendlyName; - this.receiver.label = obj.receiver.label; + this.receiver._update(obj.receiver); + } else { + this.receiver = null; + } - if (obj.receiver.volume) { - this.receiver.volume = new chrome.cast.Volume(obj.receiver.volume.level, obj.receiver.volume.muted); + // Empty media + this.media.splice(0, this.media.length); + if (obj.media && obj.media.length > 0) { + // refill media + var m; + for (var i = 0; i < obj.media.length; i++) { + m = new chrome.cast.media.Media(); + m._update(obj.media[i]); + this.media.push(m); + } + if (_currentMedia) { + _currentMedia._update(obj.media[0]); } } else { - this.receiver = null; + _currentMedia = null; + } +}; + +/** + * The volume of a device or media stream. + * @param {number} level The current volume level as a value between 0.0 and 1.0. + * @param {boolean} muted Whether the receiver is muted, independent of the volume level. + */ +chrome.cast.Volume = function (level, muted) { + this.level = level; + if (muted || muted === false) { + this.muted = !!muted; } +}; - this.emit('_sessionUpdated', isAlive); +chrome.cast.Volume.prototype._update = function (jsonObj) { + for (var attr in jsonObj) { + this[attr] = jsonObj[attr]; + } +}; + +/** + * Describes the receiver running an application. Normally, these objects should not be created by the client. + * @param {string} label An identifier for the receiver that is unique to the browser profile and the origin of the API client. + * @param {string} friendlyName The user given name for the receiver. + * @param {chrome.cast.Capability[]} capabilities The capabilities of the receiver, for example audio and video. + * @param {chrome.cast.Volume} volume The current volume of the receiver. + */ +chrome.cast.Receiver = function (label, friendlyName, capabilities, volume) { + this.label = label; + this.friendlyName = friendlyName; + this.capabilities = capabilities || []; + this.volume = volume || null; + this.receiverType = chrome.cast.ReceiverType.CAST; + this.isActiveInput = null; +}; + +chrome.cast.Receiver.prototype._update = function (jsonObj) { + for (var attr in jsonObj) { + if (['volume'].indexOf(attr) === -1) { + this[attr] = jsonObj[attr]; + } + } + if (jsonObj.volume) { + if (!this.volume) { + this.volume = new chrome.cast.Volume(); + } + this.volume._update(jsonObj.volume); + } +}; + +/** + * Describes track metadata information + * @param {number} trackId Unique identifier of the track within the context of a chrome.cast.media.MediaInfo objects + * @param {chrome.cast.media.TrackType} trackType The type of track. Value must not be null. + */ +chrome.cast.media.Track = function Track (trackId, trackType) { + this.trackId = trackId; + this.type = trackType; + this.customData = this.language = this.name = this.subtype = this.trackContentId = this.trackContentType = null; +}; + +chrome.cast.media.Track.prototype._update = function (jsonObj) { + for (var attr in jsonObj) { + this[attr] = jsonObj[attr]; + } +}; + +/** + * Describes a media item. + * @param {string} contentId Identifies the content. + * @param {string} contentType MIME content type of the media. + * @property {Object} customData Custom data set by the receiver application. + * @property {number} duration Duration of the content, in seconds. + * @property {any type} metadata Describes the media content. + * @property {chrome.cast.media.StreamType} streamType The type of media stream. + */ +chrome.cast.media.MediaInfo = function MediaInfo (contentId, contentType) { + this.contentId = contentId; + this.streamType = chrome.cast.media.StreamType.BUFFERED; + this.contentType = contentType; + this.customData = this.duration = this.metadata = null; +}; + +chrome.cast.media.MediaInfo.prototype._update = function (jsonObj) { + for (var attr in jsonObj) { + this[attr] = jsonObj[attr]; + } + + if (jsonObj.tracks) { + this.tracks = []; + var track, t; + for (var i = 0; i < jsonObj.tracks.length; i++) { + track = jsonObj.tracks[i]; + t = new chrome.cast.media.Track(); + t._update(track); + this.tracks.push(t); + } + } else { + this.tracks = null; + } }; /** @@ -861,7 +925,8 @@ chrome.cast.media.Media = function Media (sessionId, mediaSessionId) { this.mediaSessionId = mediaSessionId; this.currentTime = 0; this.playbackRate = 1; - this.playerState = chrome.cast.media.PlayerState.BUFFERING; + this.playerState = chrome.cast.media.PlayerState.IDLE; + this.idleReason = null; this.supportedMediaCommands = [ chrome.cast.media.MediaCommand.PAUSE, chrome.cast.media.MediaCommand.SEEK, @@ -870,11 +935,35 @@ chrome.cast.media.Media = function Media (sessionId, mediaSessionId) { ]; this.volume = new chrome.cast.Volume(1, false); this._lastUpdatedTime = Date.now(); - this.media = {}; + this.media = null; }; chrome.cast.media.Media.prototype = Object.create(EventEmitter.prototype); +function mediaPreCheck (media) { + var err = sessionPreCheck(media.sessionId); + if (err) { + return err; + } + if (!_currentMedia || + media.sessionId !== _currentMedia.sessionId || + media.playerState === chrome.cast.media.PlayerState.IDLE) { + return new chrome.cast.Error( + chrome.cast.ErrorCode.SESSION_ERROR, 'INVALID_MEDIA_SESSION_ID', + { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); + } + return false; +} + +chrome.cast.media.Media.prototype._preCheck = function (errorCallback) { + var err = mediaPreCheck(this); + if (err) { + errorCallback && errorCallback(err); + return true; + } + return err; +}; + /** * Plays the media item. * @param {chrome.cast.media.PlayRequest} playRequest The optional media play request. @@ -882,6 +971,7 @@ chrome.cast.media.Media.prototype = Object.create(EventEmitter.prototype); * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.play = function (playRequest, successCallback, errorCallback) { + if (this._preCheck(errorCallback)) { return; } execute('mediaPlay', function (err) { if (!err) { successCallback && successCallback(); @@ -898,6 +988,7 @@ chrome.cast.media.Media.prototype.play = function (playRequest, successCallback, * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.pause = function (pauseRequest, successCallback, errorCallback) { + if (this._preCheck(errorCallback)) { return; } execute('mediaPause', function (err) { if (!err) { successCallback && successCallback(); @@ -914,6 +1005,7 @@ chrome.cast.media.Media.prototype.pause = function (pauseRequest, successCallbac * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.seek = function (seekRequest, successCallback, errorCallback) { + if (this._preCheck(errorCallback)) { return; } const currentTime = Math.round(seekRequest.currentTime); const resumeState = seekRequest.resumeState || ''; @@ -933,6 +1025,7 @@ chrome.cast.media.Media.prototype.seek = function (seekRequest, successCallback, * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.stop = function (stopRequest, successCallback, errorCallback) { + if (this._preCheck(errorCallback)) { return; } execute('mediaStop', function (err) { if (!err) { successCallback && successCallback(); @@ -949,6 +1042,7 @@ chrome.cast.media.Media.prototype.stop = function (stopRequest, successCallback, * @param {function} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. */ chrome.cast.media.Media.prototype.setVolume = function (volumeRequest, successCallback, errorCallback) { + if (this._preCheck(errorCallback)) { return; } if (!volumeRequest.volume || (volumeRequest.volume.level == null && volumeRequest.volume.muted === null)) { errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.SESSION_ERROR), 'INVALID_PARAMS', { reason: 'INVALID_PARAMS', type: 'INVALID_REQUEST' }); return; @@ -995,6 +1089,7 @@ chrome.cast.media.Media.prototype.getEstimatedTime = function () { * @param {function(not-null chrome.cast.Error)} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. **/ chrome.cast.media.Media.prototype.editTracksInfo = function (editTracksInfoRequest, successCallback, errorCallback) { + if (this._preCheck(errorCallback)) { return; } var activeTracks = editTracksInfoRequest.activeTrackIds; var textTrackSytle = editTracksInfoRequest.textTrackSytle; @@ -1026,26 +1121,27 @@ chrome.cast.media.Media.prototype.removeUpdateListener = function (listener) { }; chrome.cast.media.Media.prototype._update = function (obj) { - this.currentTime = obj.currentTime || this.currentTime; - this.idleReason = obj.idleReason || this.idleReason; - this.sessionId = obj.sessionId || this.sessionId; - this.mediaSessionId = obj.mediaSessionId || this.mediaSessionId; - this.playbackRate = obj.playbackRate || this.playbackRate; - this.playerState = obj.playerState || this.playerState; - - if (obj.media && obj.media.duration) { - this.media = this.media || {}; - this.media.duration = obj.media.duration || this.media.duration; - this.media.streamType = obj.media.streamType || this.media.streamType; + for (var attr in obj) { + if (['media', 'volume'].indexOf(attr) === -1) { + this[attr] = obj[attr]; + } + } + + if (obj.media) { + if (!this.media) { + this.media = new chrome.cast.media.MediaInfo(); + } + this.media._update(obj.media); } - if (obj.volume && obj.volume.level) { - this.volume = new chrome.cast.Volume(obj.volume.level, obj.volume.muted); + if (obj.volume) { + if (!this.volume) { + this.volume = new chrome.cast.Volume(); + } + this.volume._update(obj.volume); } this._lastUpdatedTime = Date.now(); - - this.emit('_mediaUpdated', this.playerState !== 'IDLE'); }; /** @@ -1100,7 +1196,7 @@ chrome.cast.cordova = { selectRoute: function (routeId, successCallback, errorCallback) { execute('selectRoute', routeId, function (err, session) { if (!err) { - successCallback(updateSession(session)); + successCallback(createNewSession(session)); } else { handleError(err, errorCallback); } @@ -1151,18 +1247,33 @@ execute('setup', function (err, args) { * @param {function} listener The listener to add. */ SESSION_UPDATE: function (obj) { + // Should we reset the session? + if (!obj) { + _session = undefined; + _sessionListener = undefined; + _receiverListener = undefined; + return; + } if (_session) { _session._update(obj); + _session.emit('_sessionUpdated', _session.status !== chrome.cast.SessionStatus.STOPPED); } }, MEDIA_UPDATE: function (media) { + if (!media) { + _currentMedia = null; + _session.media = []; + return; + } if (!_currentMedia) { _currentMedia = new chrome.cast.media.Media(media.sessionId, media.mediaSessionId); + } else { + _currentMedia._update(media); } - _currentMedia._update(media); if (_session) { _session.media[0] = _currentMedia; } + _currentMedia.emit('_mediaUpdated', _currentMedia.playerState !== 'IDLE'); }, MEDIA_LOAD: function (media) { if (_session) { @@ -1174,8 +1285,8 @@ execute('setup', function (err, args) { } }, SESSION_LISTENER: function (javaSession) { - var session = updateSession(javaSession); - _sessionListener && _sessionListener(session); + _session = createNewSession(javaSession); + _sessionListener && _sessionListener(_session); }, RECEIVER_MESSAGE: function (namespace, message) { if (_session) { @@ -1196,53 +1307,12 @@ module.exports = chrome.cast; /** * Updates the current session with the incoming javaSession */ -function updateSession (javaSession) { - // Should we reset the sesion? - if (!javaSession) { - _session = undefined; - _sessionListener = undefined; - _receiverListener = undefined; - return; - } - _session = new chrome.cast.Session( - javaSession.sessionId, - javaSession.appId, - javaSession.displayName, - javaSession.appImages || [], - createReceiver(javaSession.receiver) - ); - _session.status = chrome.cast.SessionStatus.CONNECTED; - _session.media[0] = createMedia(javaSession.media, javaSession.sessionId); - +function createNewSession (javaSession) { + _session = new chrome.cast.Session(); + _session._update(javaSession); return _session; } -function createMedia (media, sessionId) { - if (media && media.sessionId) { - _currentMedia = new chrome.cast.media.Media(sessionId, media.mediaSessionId); - _currentMedia.currentTime = media.currentTime; - _currentMedia.playerState = media.playerState; - _currentMedia.media = media.media; - } - return _currentMedia; -} - -function createReceiver (receiver) { - if (!receiver) { - return new chrome.cast.Receiver(null, null, null, null); - } - var outReceiver = new chrome.cast.Receiver( - receiver.label, - receiver.friendlyName, - receiver.capabilities || [], - null - ); - if (receiver.volume) { - outReceiver.volume = new chrome.cast.Volume(receiver.volume.level, receiver.volume.muted); - } - return outReceiver; -} - function execute (action) { var args = [].slice.call(arguments); args.shift(); @@ -1253,7 +1323,7 @@ function execute (action) { // Reasons to not execute if (action !== 'setup' && !chrome.cast.isAvailable) { - return callback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); + return callback && callback(new chrome.cast.Error(chrome.cast.ErrorCode.API_NOT_INITIALIZED), 'The API is not initialized.', {}); } if (action !== 'setup' && action !== 'initialize' && !_initialized) { throw new Error('Not initialized. Must call chrome.cast.initialize first.'); From d25ed20a0327ad06e3fb6fa3182a159793552512 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Tue, 1 Oct 2019 09:53:56 -0600 Subject: [PATCH 060/166] Added ability to run tests on chrome to confirm that functionality matches chrome desktop behavior. --- .github/pull_request_template.md | 2 +- README.md | 14 ++- package.json | 7 +- tests/package.json | 1 - tests/plugin.xml | 2 - tests/www/chrome/cordova_stubs.js | 133 ++++++++++++++++++++++++ tests/www/chrome/host-tests.js | 34 ++++++ tests/www/chrome/tests_auto_chrome.html | 41 ++++++++ tests/www/chrome/tests_chrome.html | 27 +++++ tests/www/js/tests_manual.js | 11 ++ tests/www/lib/chai.js | 92 ++++++++++++++++ tests/www/lib/readme.md | 4 +- 12 files changed, 359 insertions(+), 9 deletions(-) create mode 100644 tests/www/chrome/cordova_stubs.js create mode 100644 tests/www/chrome/host-tests.js create mode 100644 tests/www/chrome/tests_auto_chrome.html create mode 100644 tests/www/chrome/tests_chrome.html create mode 100644 tests/www/js/tests_manual.js create mode 100644 tests/www/lib/chai.js diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index c9ce7ae..745f8e9 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -30,4 +30,4 @@ Thanks! - [ ] If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct [keyword to close issues using keywords](https://help.github.com/articles/closing-issues-using-keywords/)) - [ ] I've run `npm test` and no errors were found (run `npm style` to auto-fix errors it can) - [ ] I've run the tests (See Readme) -- [ ] I added automated test coverage as appropriate for this change \ No newline at end of file +- [ ] I added automated test coverage as appropriate for this change (See Readme Contributing section) \ No newline at end of file diff --git a/README.md b/README.md index ebdb1bc..a0060e7 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ Run `npm test` to ensure your code fits the styling. It will also find some err * If errors are found, you can try running `npm run style`, this will attempt to automatically fix the errors. -### Tests +### Tests Mobile How to run the tests: * With **admin permission** run `cordova plugin add --link /tests` @@ -106,9 +106,19 @@ How to run the tests: [Why we chose a non-standard test framework](https://github.com/jellyfin/cordova-plugin-chromecast/issues/50) +### Tests Chrome + +The auto tests also run in desktop chrome. +They use the google provided cast_sender.js. +These are particularily useful for ensuring we are following the [official Google Cast API for Chrome](https://developers.google.com/cast/docs/reference/chrome#chrome.cast) correctly. +To run the tests: +* run: `npm run host-chrome-tests []` +* Navigate to: `http://localhost:/chrome/tests_chrome.html` ## Contributing -* Make sure all tests pass ([Code Format](#code-format) and [Tests](#tests)) * Write a test for your contribution if applicable (for a bug fix, new feature, etc) + * You should test on [Chrome](#tests-chrome) first to ensure you are following [Google Cast API](https://developers.google.com/cast/docs/reference/chrome#chrome.cast) behavior correctly + * If the test does not pass on [Chrome](#tests-chrome) we should not be implementing it either (unless it is a `chrome.cast.cordova` function) +* Make sure all tests pass ([Code Format](#code-format), [Tests Mobile](#tests-mobile), and [Tests Chrome](#tests-chrome)) * Update documentation as necessary diff --git a/package.json b/package.json index 9cf9197..5d3c7de 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "name": "cordova-plugin-chromecast", "version": "1.0.0-dev", "scripts": { - "style-fix-js": "node node_modules/eslint/bin/eslint --fix src && node node_modules/eslint/bin/eslint --fix www && node node_modules/eslint/bin/eslint --fix tests/www", + "host-chrome-tests": "node tests/www/chrome/host-tests.js", + "style-fix-js": "node node_modules/eslint/bin/eslint --fix src && node node_modules/eslint/bin/eslint --fix www && node node_modules/eslint/bin/eslint --ignore-pattern tests/www/lib --fix tests/www", "test": "node node_modules/eslint/bin/eslint src && node node_modules/eslint/bin/eslint www && node node_modules/eslint/bin/eslint tests/www && node ./node_modules/java-checkstyle/bin/index.js ./src/android/ -c ./check_style.xml", "style": "npm run style-fix-js && npm run test" }, @@ -18,6 +19,8 @@ "eslint-plugin-node": "~5.0.0", "eslint-plugin-promise": "~3.5.0", "eslint-plugin-standard": "~3.0.1", - "java-checkstyle": "0.0.1" + "express": "^4.17.1", + "java-checkstyle": "0.0.1", + "path": "^0.12.7" } } diff --git a/tests/package.json b/tests/package.json index c7a11db..c1f5545 100644 --- a/tests/package.json +++ b/tests/package.json @@ -11,6 +11,5 @@ ] }, "dependencies": { - "chai": "4.2.0" } } diff --git a/tests/plugin.xml b/tests/plugin.xml index 8803222..4acfc37 100644 --- a/tests/plugin.xml +++ b/tests/plugin.xml @@ -25,6 +25,4 @@ Apache 2.0 - - diff --git a/tests/www/chrome/cordova_stubs.js b/tests/www/chrome/cordova_stubs.js new file mode 100644 index 0000000..ae5641e --- /dev/null +++ b/tests/www/chrome/cordova_stubs.js @@ -0,0 +1,133 @@ +/** + * These stub plugin specific bahaviour so we can run the auto tests on chrome + * desktop browser. + */ +(function () { + 'use strict'; + /* eslint-env mocha */ + /* global chrome */ + + window.chrome = window.chrome || {}; + chrome.cast = chrome.cast || {}; + chrome.cast.cordova = {}; + + var startJoiningButton = document.getElementById('start-session'); + var doneJoiningButton = document.getElementById('joined-session'); + var _scanning = false; + var _startRouteScanErrorCallback; + + /** + * Will actively scan for routes and send the complete list of + * active routes whenever a route change is detected. + * It is super important that client calls "stopScan", otherwise the + * battery could drain quickly. + * https://github.com/jellyfin/cordova-plugin-chromecast/issues/22#issuecomment-530773677 + * @param {function(routes)} successCallback + * @param {function(chrome.cast.Error)} successCallback + */ + chrome.cast.cordova.startRouteScan = function (successCallback, errorCallback) { + if (_scanning) { + _startRouteScanErrorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.CANCEL, + 'Started a new route scan before stopping previous one.')); + } + _startRouteScanErrorCallback = errorCallback; + _scanning = true; + var routes = []; + routes.push(new chrome.cast.cordova.Route({ + id: 'normal', + name: 'normal', + isNearbyDevice: false, + isCastGroup: false + })); + routes.push(new chrome.cast.cordova.Route({ + id: 'group', + name: 'group', + isNearbyDevice: false, + isCastGroup: true + })); + successCallback(routes); + }; + + /** + * Stops any active scanForRoutes. + * @param {function(routes)} successCallback + * @param {function(chrome.cast.Error)} successCallback + */ + chrome.cast.cordova.stopRouteScan = function (successCallback, errorCallback) { + _startRouteScanErrorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.CANCEL, + 'Scan stopped.')); + _scanning = false; + successCallback(); + }; + + /** + * Attempts to join the requested route + * @param {string} routeId + * @param {function(routes)} successCallback + * @param {function(chrome.cast.Error)} successCallback + */ + chrome.cast.cordova.selectRoute = function (routeId, successCallback, errorCallback) { + if (routeId === '') { + return errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.SESSION_ERROR, + 'Leave or stop current session before attempting to join new session.')); + } + if (routeId === 'non-existant-route-id') { + return errorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.TIMEOUT, + 'Failed to join route (' + routeId + ') after 15s and 0 tries.')); + } + + var timeout = setTimeout(function () { + console.error('Make sure to click done joining button.'); + }, 10000); + + // set up show the start join button + startJoiningButton.addEventListener('click', function joinListener () { + // hide the start joining button + startJoiningButton.style = 'display:none;'; + startJoiningButton.removeEventListener('click', joinListener); + + chrome.cast.requestSession(function (session) { + + // set up and show the done joining button + doneJoiningButton.addEventListener('click', function doneListener () { + // Hide the done joining button + doneJoiningButton.removeEventListener('click', doneListener); + doneJoiningButton.style = 'display:none;'; + + clearTimeout(timeout); + // setTimeout(function () { + successCallback(session); + // }, 1000); + }); + doneJoiningButton.style = ''; + + }, errorCallback); + }); + startJoiningButton.style = ''; + }; + + chrome.cast.cordova.Route = function (jsonRoute) { + this.id = jsonRoute.id; + this.name = jsonRoute.name; + this.isNearbyDevice = jsonRoute.isNearbyDevice; + this.isCastGroup = jsonRoute.isCastGroup; + }; + + window.cordova = window.cordova || {}; + window.cordova.exec = function (successCallback, errorCallback, plugin, fnName, args) { + if (_startRouteScanErrorCallback) { + _startRouteScanErrorCallback(new chrome.cast.Error(chrome.cast.ErrorCode.CANCEL, + 'Scan stopped because setup triggered.')); + } + successCallback(['SETUP']); + }; + + // This actually starts the tests + window['__onGCastApiAvailable'] = function (isAvailable, err) { + // If error, it is probably because we are not on chrome, so just disregard + if (isAvailable) { + mocha.run(); + } + }; + +}()); diff --git a/tests/www/chrome/host-tests.js b/tests/www/chrome/host-tests.js new file mode 100644 index 0000000..ab1f527 --- /dev/null +++ b/tests/www/chrome/host-tests.js @@ -0,0 +1,34 @@ +/** + * Starts a server which serves the content necessary to run the tests on chrome. + * + * run: + * `node ./host-tests.js []` + * + * Navigate to: + * `http://localhost:/chrome/tests_chrome.html` + */ +(function () { + 'use strict'; + + var path = require('path'); + var express = require('express'); + + var _server; + var _port = 8432; + + // process the passed arguments and configure options + if (process.argv.length >= 3) { + _port = process.argv[2]; + } + + _server = express(); + + // Add COPIES_DIR first so it is used before the ASSET_DIR + _server.use('/', express.static(path.resolve(__dirname, '../'))); + + // Start the server + _server.listen(_port, function () { + console.log('Server listening on port ', _port); + }); + +})(); diff --git a/tests/www/chrome/tests_auto_chrome.html b/tests/www/chrome/tests_auto_chrome.html new file mode 100644 index 0000000..e56276c --- /dev/null +++ b/tests/www/chrome/tests_auto_chrome.html @@ -0,0 +1,41 @@ + + + Cordova tests + + + + + + + + + + + + + +

      Auto Tests

      + + +
      + Action Required: + + +
      +
      + + + + + diff --git a/tests/www/chrome/tests_chrome.html b/tests/www/chrome/tests_chrome.html new file mode 100644 index 0000000..94c29f8 --- /dev/null +++ b/tests/www/chrome/tests_chrome.html @@ -0,0 +1,27 @@ + + + Cordova tests + + + + + + + + + +

      cordova-plugin-chromecast Tests

      + + + diff --git a/tests/www/js/tests_manual.js b/tests/www/js/tests_manual.js new file mode 100644 index 0000000..c9cc561 --- /dev/null +++ b/tests/www/js/tests_manual.js @@ -0,0 +1,11 @@ + + /* eslint-disable no-undef */ + + var interval = setInterval(function () { + if (chrome && chrome.cast && chrome.cast.isAvailable) { + clearInterval(interval); + chrome.cast.cordova.stopRouteScan(function () { + throw new Error('Just gonna throw this error for demonstration'); + }); + } + }, 500); diff --git a/tests/www/lib/chai.js b/tests/www/lib/chai.js new file mode 100644 index 0000000..efda439 --- /dev/null +++ b/tests/www/lib/chai.js @@ -0,0 +1,92 @@ +/*! + * chai + * Copyright(c) 2011-2014 Jake Luer + * MIT Licensed + */ + +var used = []; + +/*! + * Chai version + */ + +exports.version = '4.2.0'; + +/*! + * Assertion Error + */ + +exports.AssertionError = require('assertion-error'); + +/*! + * Utils for plugins (not exported) + */ + +var util = require('./chai/utils'); + +/** + * # .use(function) + * + * Provides a way to extend the internals of Chai. + * + * @param {Function} + * @returns {this} for chaining + * @api public + */ + +exports.use = function (fn) { + if (!~used.indexOf(fn)) { + fn(exports, util); + used.push(fn); + } + + return exports; +}; + +/*! + * Utility Functions + */ + +exports.util = util; + +/*! + * Configuration + */ + +var config = require('./chai/config'); +exports.config = config; + +/*! + * Primary `Assertion` prototype + */ + +var assertion = require('./chai/assertion'); +exports.use(assertion); + +/*! + * Core Assertions + */ + +var core = require('./chai/core/assertions'); +exports.use(core); + +/*! + * Expect interface + */ + +var expect = require('./chai/interface/expect'); +exports.use(expect); + +/*! + * Should interface + */ + +var should = require('./chai/interface/should'); +exports.use(should); + +/*! + * Assert interface + */ + +var assert = require('./chai/interface/assert'); +exports.use(assert); diff --git a/tests/www/lib/readme.md b/tests/www/lib/readme.md index 7f34b2c..245bf85 100644 --- a/tests/www/lib/readme.md +++ b/tests/www/lib/readme.md @@ -6,4 +6,6 @@ mocha.js and mocha.css Importantly, it includes Pull Request #3952 https://github.com/mochajs/mocha/pull/3952 - \ No newline at end of file + +chai.js + is also included because it is just easier for hosting the chrome tests \ No newline at end of file From 89ca848d343a2a5478d65bb09b76e3511c938037 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Fri, 4 Oct 2019 02:57:59 -0600 Subject: [PATCH 061/166] Fix chai.js, fix typos in readme, fix css import, rename runner since it is actually just the custom reporter --- README.md | 4 +- tests/www/html/tests.html | 2 +- tests/www/html/tests_manual.html | 2 +- ...unner.js => custom_mocha_html_reporter.js} | 0 tests/www/lib/chai.js | 10946 +++++++++++++++- 5 files changed, 10858 insertions(+), 96 deletions(-) rename tests/www/js/{runner.js => custom_mocha_html_reporter.js} (100%) diff --git a/README.md b/README.md index a0060e7..95ae2a2 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ Follow these direction to set up for plugin development: * You will need an existing cordova project or [create a new cordova project](https://cordova.apache.org/#getstarted). * With **admin permission** run: `cordova plugin add --link ` -### About the `--link` flag +#### About the `--link` flag The `--link` flag allows you to modify the native code (java/swift/obj-c) directly in the relative platform folder if desired. * This means you can work directly from Android Studio/Xcode! * Note: Be careful about adding and deleting files. These changes will be exclusive to the platform folder and will not be transferred back to your plugin folder. @@ -110,7 +110,7 @@ How to run the tests: The auto tests also run in desktop chrome. They use the google provided cast_sender.js. -These are particularily useful for ensuring we are following the [official Google Cast API for Chrome](https://developers.google.com/cast/docs/reference/chrome#chrome.cast) correctly. +These are particularly useful for ensuring we are following the [official Google Cast API for Chrome](https://developers.google.com/cast/docs/reference/chrome#chrome.cast) correctly. To run the tests: * run: `npm run host-chrome-tests []` * Navigate to: `http://localhost:/chrome/tests_chrome.html` diff --git a/tests/www/html/tests.html b/tests/www/html/tests.html index 6c56ac5..ba60ae8 100644 --- a/tests/www/html/tests.html +++ b/tests/www/html/tests.html @@ -5,7 +5,7 @@ - + diff --git a/tests/www/html/tests_manual.html b/tests/www/html/tests_manual.html index b7bd95a..0ebaae8 100644 --- a/tests/www/html/tests_manual.html +++ b/tests/www/html/tests_manual.html @@ -5,7 +5,7 @@ - + diff --git a/tests/www/js/runner.js b/tests/www/js/custom_mocha_html_reporter.js similarity index 100% rename from tests/www/js/runner.js rename to tests/www/js/custom_mocha_html_reporter.js diff --git a/tests/www/lib/chai.js b/tests/www/lib/chai.js index efda439..c53d8ab 100644 --- a/tests/www/lib/chai.js +++ b/tests/www/lib/chai.js @@ -1,92 +1,10854 @@ -/*! - * chai - * Copyright(c) 2011-2014 Jake Luer - * MIT Licensed - */ - -var used = []; - -/*! - * Chai version - */ - -exports.version = '4.2.0'; - -/*! - * Assertion Error - */ - -exports.AssertionError = require('assertion-error'); - -/*! - * Utils for plugins (not exported) - */ - -var util = require('./chai/utils'); - -/** - * # .use(function) - * - * Provides a way to extend the internals of Chai. - * - * @param {Function} - * @returns {this} for chaining - * @api public - */ - -exports.use = function (fn) { - if (!~used.indexOf(fn)) { - fn(exports, util); - used.push(fn); - } - - return exports; -}; - -/*! - * Utility Functions - */ - -exports.util = util; - -/*! - * Configuration - */ - -var config = require('./chai/config'); -exports.config = config; - -/*! - * Primary `Assertion` prototype - */ - -var assertion = require('./chai/assertion'); -exports.use(assertion); - -/*! - * Core Assertions - */ - -var core = require('./chai/core/assertions'); -exports.use(core); - -/*! - * Expect interface - */ - -var expect = require('./chai/interface/expect'); -exports.use(expect); - -/*! - * Should interface - */ - -var should = require('./chai/interface/should'); -exports.use(should); - -/*! - * Assert interface - */ - -var assert = require('./chai/interface/assert'); -exports.use(assert); +(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.chai = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i + * MIT Licensed + */ + + var used = []; + + /*! + * Chai version + */ + + exports.version = '4.2.0'; + + /*! + * Assertion Error + */ + + exports.AssertionError = require('assertion-error'); + + /*! + * Utils for plugins (not exported) + */ + + var util = require('./chai/utils'); + + /** + * # .use(function) + * + * Provides a way to extend the internals of Chai. + * + * @param {Function} + * @returns {this} for chaining + * @api public + */ + + exports.use = function (fn) { + if (!~used.indexOf(fn)) { + fn(exports, util); + used.push(fn); + } + + return exports; + }; + + /*! + * Utility Functions + */ + + exports.util = util; + + /*! + * Configuration + */ + + var config = require('./chai/config'); + exports.config = config; + + /*! + * Primary `Assertion` prototype + */ + + var assertion = require('./chai/assertion'); + exports.use(assertion); + + /*! + * Core Assertions + */ + + var core = require('./chai/core/assertions'); + exports.use(core); + + /*! + * Expect interface + */ + + var expect = require('./chai/interface/expect'); + exports.use(expect); + + /*! + * Should interface + */ + + var should = require('./chai/interface/should'); + exports.use(should); + + /*! + * Assert interface + */ + + var assert = require('./chai/interface/assert'); + exports.use(assert); + + },{"./chai/assertion":3,"./chai/config":4,"./chai/core/assertions":5,"./chai/interface/assert":6,"./chai/interface/expect":7,"./chai/interface/should":8,"./chai/utils":22,"assertion-error":33}],3:[function(require,module,exports){ + /*! + * chai + * http://chaijs.com + * Copyright(c) 2011-2014 Jake Luer + * MIT Licensed + */ + + var config = require('./config'); + + module.exports = function (_chai, util) { + /*! + * Module dependencies. + */ + + var AssertionError = _chai.AssertionError + , flag = util.flag; + + /*! + * Module export. + */ + + _chai.Assertion = Assertion; + + /*! + * Assertion Constructor + * + * Creates object for chaining. + * + * `Assertion` objects contain metadata in the form of flags. Three flags can + * be assigned during instantiation by passing arguments to this constructor: + * + * - `object`: This flag contains the target of the assertion. For example, in + * the assertion `expect(numKittens).to.equal(7);`, the `object` flag will + * contain `numKittens` so that the `equal` assertion can reference it when + * needed. + * + * - `message`: This flag contains an optional custom error message to be + * prepended to the error message that's generated by the assertion when it + * fails. + * + * - `ssfi`: This flag stands for "start stack function indicator". It + * contains a function reference that serves as the starting point for + * removing frames from the stack trace of the error that's created by the + * assertion when it fails. The goal is to provide a cleaner stack trace to + * end users by removing Chai's internal functions. Note that it only works + * in environments that support `Error.captureStackTrace`, and only when + * `Chai.config.includeStack` hasn't been set to `false`. + * + * - `lockSsfi`: This flag controls whether or not the given `ssfi` flag + * should retain its current value, even as assertions are chained off of + * this object. This is usually set to `true` when creating a new assertion + * from within another assertion. It's also temporarily set to `true` before + * an overwritten assertion gets called by the overwriting assertion. + * + * @param {Mixed} obj target of the assertion + * @param {String} msg (optional) custom error message + * @param {Function} ssfi (optional) starting point for removing stack frames + * @param {Boolean} lockSsfi (optional) whether or not the ssfi flag is locked + * @api private + */ + + function Assertion (obj, msg, ssfi, lockSsfi) { + flag(this, 'ssfi', ssfi || Assertion); + flag(this, 'lockSsfi', lockSsfi); + flag(this, 'object', obj); + flag(this, 'message', msg); + + return util.proxify(this); + } + + Object.defineProperty(Assertion, 'includeStack', { + get: function() { + console.warn('Assertion.includeStack is deprecated, use chai.config.includeStack instead.'); + return config.includeStack; + }, + set: function(value) { + console.warn('Assertion.includeStack is deprecated, use chai.config.includeStack instead.'); + config.includeStack = value; + } + }); + + Object.defineProperty(Assertion, 'showDiff', { + get: function() { + console.warn('Assertion.showDiff is deprecated, use chai.config.showDiff instead.'); + return config.showDiff; + }, + set: function(value) { + console.warn('Assertion.showDiff is deprecated, use chai.config.showDiff instead.'); + config.showDiff = value; + } + }); + + Assertion.addProperty = function (name, fn) { + util.addProperty(this.prototype, name, fn); + }; + + Assertion.addMethod = function (name, fn) { + util.addMethod(this.prototype, name, fn); + }; + + Assertion.addChainableMethod = function (name, fn, chainingBehavior) { + util.addChainableMethod(this.prototype, name, fn, chainingBehavior); + }; + + Assertion.overwriteProperty = function (name, fn) { + util.overwriteProperty(this.prototype, name, fn); + }; + + Assertion.overwriteMethod = function (name, fn) { + util.overwriteMethod(this.prototype, name, fn); + }; + + Assertion.overwriteChainableMethod = function (name, fn, chainingBehavior) { + util.overwriteChainableMethod(this.prototype, name, fn, chainingBehavior); + }; + + /** + * ### .assert(expression, message, negateMessage, expected, actual, showDiff) + * + * Executes an expression and check expectations. Throws AssertionError for reporting if test doesn't pass. + * + * @name assert + * @param {Philosophical} expression to be tested + * @param {String|Function} message or function that returns message to display if expression fails + * @param {String|Function} negatedMessage or function that returns negatedMessage to display if negated expression fails + * @param {Mixed} expected value (remember to check for negation) + * @param {Mixed} actual (optional) will default to `this.obj` + * @param {Boolean} showDiff (optional) when set to `true`, assert will display a diff in addition to the message if expression fails + * @api private + */ + + Assertion.prototype.assert = function (expr, msg, negateMsg, expected, _actual, showDiff) { + var ok = util.test(this, arguments); + if (false !== showDiff) showDiff = true; + if (undefined === expected && undefined === _actual) showDiff = false; + if (true !== config.showDiff) showDiff = false; + + if (!ok) { + msg = util.getMessage(this, arguments); + var actual = util.getActual(this, arguments); + throw new AssertionError(msg, { + actual: actual + , expected: expected + , showDiff: showDiff + }, (config.includeStack) ? this.assert : flag(this, 'ssfi')); + } + }; + + /*! + * ### ._obj + * + * Quick reference to stored `actual` value for plugin developers. + * + * @api private + */ + + Object.defineProperty(Assertion.prototype, '_obj', + { get: function () { + return flag(this, 'object'); + } + , set: function (val) { + flag(this, 'object', val); + } + }); + }; + + },{"./config":4}],4:[function(require,module,exports){ + module.exports = { + + /** + * ### config.includeStack + * + * User configurable property, influences whether stack trace + * is included in Assertion error message. Default of false + * suppresses stack trace in the error message. + * + * chai.config.includeStack = true; // enable stack on error + * + * @param {Boolean} + * @api public + */ + + includeStack: false, + + /** + * ### config.showDiff + * + * User configurable property, influences whether or not + * the `showDiff` flag should be included in the thrown + * AssertionErrors. `false` will always be `false`; `true` + * will be true when the assertion has requested a diff + * be shown. + * + * @param {Boolean} + * @api public + */ + + showDiff: true, + + /** + * ### config.truncateThreshold + * + * User configurable property, sets length threshold for actual and + * expected values in assertion errors. If this threshold is exceeded, for + * example for large data structures, the value is replaced with something + * like `[ Array(3) ]` or `{ Object (prop1, prop2) }`. + * + * Set it to zero if you want to disable truncating altogether. + * + * This is especially userful when doing assertions on arrays: having this + * set to a reasonable large value makes the failure messages readily + * inspectable. + * + * chai.config.truncateThreshold = 0; // disable truncating + * + * @param {Number} + * @api public + */ + + truncateThreshold: 40, + + /** + * ### config.useProxy + * + * User configurable property, defines if chai will use a Proxy to throw + * an error when a non-existent property is read, which protects users + * from typos when using property-based assertions. + * + * Set it to false if you want to disable this feature. + * + * chai.config.useProxy = false; // disable use of Proxy + * + * This feature is automatically disabled regardless of this config value + * in environments that don't support proxies. + * + * @param {Boolean} + * @api public + */ + + useProxy: true, + + /** + * ### config.proxyExcludedKeys + * + * User configurable property, defines which properties should be ignored + * instead of throwing an error if they do not exist on the assertion. + * This is only applied if the environment Chai is running in supports proxies and + * if the `useProxy` configuration setting is enabled. + * By default, `then` and `inspect` will not throw an error if they do not exist on the + * assertion object because the `.inspect` property is read by `util.inspect` (for example, when + * using `console.log` on the assertion object) and `.then` is necessary for promise type-checking. + * + * // By default these keys will not throw an error if they do not exist on the assertion object + * chai.config.proxyExcludedKeys = ['then', 'inspect']; + * + * @param {Array} + * @api public + */ + + proxyExcludedKeys: ['then', 'catch', 'inspect', 'toJSON'] + }; + + },{}],5:[function(require,module,exports){ + /*! + * chai + * http://chaijs.com + * Copyright(c) 2011-2014 Jake Luer + * MIT Licensed + */ + + module.exports = function (chai, _) { + var Assertion = chai.Assertion + , AssertionError = chai.AssertionError + , flag = _.flag; + + /** + * ### Language Chains + * + * The following are provided as chainable getters to improve the readability + * of your assertions. + * + * **Chains** + * + * - to + * - be + * - been + * - is + * - that + * - which + * - and + * - has + * - have + * - with + * - at + * - of + * - same + * - but + * - does + * - still + * + * @name language chains + * @namespace BDD + * @api public + */ + + [ 'to', 'be', 'been', 'is' + , 'and', 'has', 'have', 'with' + , 'that', 'which', 'at', 'of' + , 'same', 'but', 'does', 'still' ].forEach(function (chain) { + Assertion.addProperty(chain); + }); + + /** + * ### .not + * + * Negates all assertions that follow in the chain. + * + * expect(function () {}).to.not.throw(); + * expect({a: 1}).to.not.have.property('b'); + * expect([1, 2]).to.be.an('array').that.does.not.include(3); + * + * Just because you can negate any assertion with `.not` doesn't mean you + * should. With great power comes great responsibility. It's often best to + * assert that the one expected output was produced, rather than asserting + * that one of countless unexpected outputs wasn't produced. See individual + * assertions for specific guidance. + * + * expect(2).to.equal(2); // Recommended + * expect(2).to.not.equal(1); // Not recommended + * + * @name not + * @namespace BDD + * @api public + */ + + Assertion.addProperty('not', function () { + flag(this, 'negate', true); + }); + + /** + * ### .deep + * + * Causes all `.equal`, `.include`, `.members`, `.keys`, and `.property` + * assertions that follow in the chain to use deep equality instead of strict + * (`===`) equality. See the `deep-eql` project page for info on the deep + * equality algorithm: https://github.com/chaijs/deep-eql. + * + * // Target object deeply (but not strictly) equals `{a: 1}` + * expect({a: 1}).to.deep.equal({a: 1}); + * expect({a: 1}).to.not.equal({a: 1}); + * + * // Target array deeply (but not strictly) includes `{a: 1}` + * expect([{a: 1}]).to.deep.include({a: 1}); + * expect([{a: 1}]).to.not.include({a: 1}); + * + * // Target object deeply (but not strictly) includes `x: {a: 1}` + * expect({x: {a: 1}}).to.deep.include({x: {a: 1}}); + * expect({x: {a: 1}}).to.not.include({x: {a: 1}}); + * + * // Target array deeply (but not strictly) has member `{a: 1}` + * expect([{a: 1}]).to.have.deep.members([{a: 1}]); + * expect([{a: 1}]).to.not.have.members([{a: 1}]); + * + * // Target set deeply (but not strictly) has key `{a: 1}` + * expect(new Set([{a: 1}])).to.have.deep.keys([{a: 1}]); + * expect(new Set([{a: 1}])).to.not.have.keys([{a: 1}]); + * + * // Target object deeply (but not strictly) has property `x: {a: 1}` + * expect({x: {a: 1}}).to.have.deep.property('x', {a: 1}); + * expect({x: {a: 1}}).to.not.have.property('x', {a: 1}); + * + * @name deep + * @namespace BDD + * @api public + */ + + Assertion.addProperty('deep', function () { + flag(this, 'deep', true); + }); + + /** + * ### .nested + * + * Enables dot- and bracket-notation in all `.property` and `.include` + * assertions that follow in the chain. + * + * expect({a: {b: ['x', 'y']}}).to.have.nested.property('a.b[1]'); + * expect({a: {b: ['x', 'y']}}).to.nested.include({'a.b[1]': 'y'}); + * + * If `.` or `[]` are part of an actual property name, they can be escaped by + * adding two backslashes before them. + * + * expect({'.a': {'[b]': 'x'}}).to.have.nested.property('\\.a.\\[b\\]'); + * expect({'.a': {'[b]': 'x'}}).to.nested.include({'\\.a.\\[b\\]': 'x'}); + * + * `.nested` cannot be combined with `.own`. + * + * @name nested + * @namespace BDD + * @api public + */ + + Assertion.addProperty('nested', function () { + flag(this, 'nested', true); + }); + + /** + * ### .own + * + * Causes all `.property` and `.include` assertions that follow in the chain + * to ignore inherited properties. + * + * Object.prototype.b = 2; + * + * expect({a: 1}).to.have.own.property('a'); + * expect({a: 1}).to.have.property('b'); + * expect({a: 1}).to.not.have.own.property('b'); + * + * expect({a: 1}).to.own.include({a: 1}); + * expect({a: 1}).to.include({b: 2}).but.not.own.include({b: 2}); + * + * `.own` cannot be combined with `.nested`. + * + * @name own + * @namespace BDD + * @api public + */ + + Assertion.addProperty('own', function () { + flag(this, 'own', true); + }); + + /** + * ### .ordered + * + * Causes all `.members` assertions that follow in the chain to require that + * members be in the same order. + * + * expect([1, 2]).to.have.ordered.members([1, 2]) + * .but.not.have.ordered.members([2, 1]); + * + * When `.include` and `.ordered` are combined, the ordering begins at the + * start of both arrays. + * + * expect([1, 2, 3]).to.include.ordered.members([1, 2]) + * .but.not.include.ordered.members([2, 3]); + * + * @name ordered + * @namespace BDD + * @api public + */ + + Assertion.addProperty('ordered', function () { + flag(this, 'ordered', true); + }); + + /** + * ### .any + * + * Causes all `.keys` assertions that follow in the chain to only require that + * the target have at least one of the given keys. This is the opposite of + * `.all`, which requires that the target have all of the given keys. + * + * expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd'); + * + * See the `.keys` doc for guidance on when to use `.any` or `.all`. + * + * @name any + * @namespace BDD + * @api public + */ + + Assertion.addProperty('any', function () { + flag(this, 'any', true); + flag(this, 'all', false); + }); + + /** + * ### .all + * + * Causes all `.keys` assertions that follow in the chain to require that the + * target have all of the given keys. This is the opposite of `.any`, which + * only requires that the target have at least one of the given keys. + * + * expect({a: 1, b: 2}).to.have.all.keys('a', 'b'); + * + * Note that `.all` is used by default when neither `.all` nor `.any` are + * added earlier in the chain. However, it's often best to add `.all` anyway + * because it improves readability. + * + * See the `.keys` doc for guidance on when to use `.any` or `.all`. + * + * @name all + * @namespace BDD + * @api public + */ + + Assertion.addProperty('all', function () { + flag(this, 'all', true); + flag(this, 'any', false); + }); + + /** + * ### .a(type[, msg]) + * + * Asserts that the target's type is equal to the given string `type`. Types + * are case insensitive. See the `type-detect` project page for info on the + * type detection algorithm: https://github.com/chaijs/type-detect. + * + * expect('foo').to.be.a('string'); + * expect({a: 1}).to.be.an('object'); + * expect(null).to.be.a('null'); + * expect(undefined).to.be.an('undefined'); + * expect(new Error).to.be.an('error'); + * expect(Promise.resolve()).to.be.a('promise'); + * expect(new Float32Array).to.be.a('float32array'); + * expect(Symbol()).to.be.a('symbol'); + * + * `.a` supports objects that have a custom type set via `Symbol.toStringTag`. + * + * var myObj = { + * [Symbol.toStringTag]: 'myCustomType' + * }; + * + * expect(myObj).to.be.a('myCustomType').but.not.an('object'); + * + * It's often best to use `.a` to check a target's type before making more + * assertions on the same target. That way, you avoid unexpected behavior from + * any assertion that does different things based on the target's type. + * + * expect([1, 2, 3]).to.be.an('array').that.includes(2); + * expect([]).to.be.an('array').that.is.empty; + * + * Add `.not` earlier in the chain to negate `.a`. However, it's often best to + * assert that the target is the expected type, rather than asserting that it + * isn't one of many unexpected types. + * + * expect('foo').to.be.a('string'); // Recommended + * expect('foo').to.not.be.an('array'); // Not recommended + * + * `.a` accepts an optional `msg` argument which is a custom error message to + * show when the assertion fails. The message can also be given as the second + * argument to `expect`. + * + * expect(1).to.be.a('string', 'nooo why fail??'); + * expect(1, 'nooo why fail??').to.be.a('string'); + * + * `.a` can also be used as a language chain to improve the readability of + * your assertions. + * + * expect({b: 2}).to.have.a.property('b'); + * + * The alias `.an` can be used interchangeably with `.a`. + * + * @name a + * @alias an + * @param {String} type + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function an (type, msg) { + if (msg) flag(this, 'message', msg); + type = type.toLowerCase(); + var obj = flag(this, 'object') + , article = ~[ 'a', 'e', 'i', 'o', 'u' ].indexOf(type.charAt(0)) ? 'an ' : 'a '; + + this.assert( + type === _.type(obj).toLowerCase() + , 'expected #{this} to be ' + article + type + , 'expected #{this} not to be ' + article + type + ); + } + + Assertion.addChainableMethod('an', an); + Assertion.addChainableMethod('a', an); + + /** + * ### .include(val[, msg]) + * + * When the target is a string, `.include` asserts that the given string `val` + * is a substring of the target. + * + * expect('foobar').to.include('foo'); + * + * When the target is an array, `.include` asserts that the given `val` is a + * member of the target. + * + * expect([1, 2, 3]).to.include(2); + * + * When the target is an object, `.include` asserts that the given object + * `val`'s properties are a subset of the target's properties. + * + * expect({a: 1, b: 2, c: 3}).to.include({a: 1, b: 2}); + * + * When the target is a Set or WeakSet, `.include` asserts that the given `val` is a + * member of the target. SameValueZero equality algorithm is used. + * + * expect(new Set([1, 2])).to.include(2); + * + * When the target is a Map, `.include` asserts that the given `val` is one of + * the values of the target. SameValueZero equality algorithm is used. + * + * expect(new Map([['a', 1], ['b', 2]])).to.include(2); + * + * Because `.include` does different things based on the target's type, it's + * important to check the target's type before using `.include`. See the `.a` + * doc for info on testing a target's type. + * + * expect([1, 2, 3]).to.be.an('array').that.includes(2); + * + * By default, strict (`===`) equality is used to compare array members and + * object properties. Add `.deep` earlier in the chain to use deep equality + * instead (WeakSet targets are not supported). See the `deep-eql` project + * page for info on the deep equality algorithm: https://github.com/chaijs/deep-eql. + * + * // Target array deeply (but not strictly) includes `{a: 1}` + * expect([{a: 1}]).to.deep.include({a: 1}); + * expect([{a: 1}]).to.not.include({a: 1}); + * + * // Target object deeply (but not strictly) includes `x: {a: 1}` + * expect({x: {a: 1}}).to.deep.include({x: {a: 1}}); + * expect({x: {a: 1}}).to.not.include({x: {a: 1}}); + * + * By default, all of the target's properties are searched when working with + * objects. This includes properties that are inherited and/or non-enumerable. + * Add `.own` earlier in the chain to exclude the target's inherited + * properties from the search. + * + * Object.prototype.b = 2; + * + * expect({a: 1}).to.own.include({a: 1}); + * expect({a: 1}).to.include({b: 2}).but.not.own.include({b: 2}); + * + * Note that a target object is always only searched for `val`'s own + * enumerable properties. + * + * `.deep` and `.own` can be combined. + * + * expect({a: {b: 2}}).to.deep.own.include({a: {b: 2}}); + * + * Add `.nested` earlier in the chain to enable dot- and bracket-notation when + * referencing nested properties. + * + * expect({a: {b: ['x', 'y']}}).to.nested.include({'a.b[1]': 'y'}); + * + * If `.` or `[]` are part of an actual property name, they can be escaped by + * adding two backslashes before them. + * + * expect({'.a': {'[b]': 2}}).to.nested.include({'\\.a.\\[b\\]': 2}); + * + * `.deep` and `.nested` can be combined. + * + * expect({a: {b: [{c: 3}]}}).to.deep.nested.include({'a.b[0]': {c: 3}}); + * + * `.own` and `.nested` cannot be combined. + * + * Add `.not` earlier in the chain to negate `.include`. + * + * expect('foobar').to.not.include('taco'); + * expect([1, 2, 3]).to.not.include(4); + * + * However, it's dangerous to negate `.include` when the target is an object. + * The problem is that it creates uncertain expectations by asserting that the + * target object doesn't have all of `val`'s key/value pairs but may or may + * not have some of them. It's often best to identify the exact output that's + * expected, and then write an assertion that only accepts that exact output. + * + * When the target object isn't even expected to have `val`'s keys, it's + * often best to assert exactly that. + * + * expect({c: 3}).to.not.have.any.keys('a', 'b'); // Recommended + * expect({c: 3}).to.not.include({a: 1, b: 2}); // Not recommended + * + * When the target object is expected to have `val`'s keys, it's often best to + * assert that each of the properties has its expected value, rather than + * asserting that each property doesn't have one of many unexpected values. + * + * expect({a: 3, b: 4}).to.include({a: 3, b: 4}); // Recommended + * expect({a: 3, b: 4}).to.not.include({a: 1, b: 2}); // Not recommended + * + * `.include` accepts an optional `msg` argument which is a custom error + * message to show when the assertion fails. The message can also be given as + * the second argument to `expect`. + * + * expect([1, 2, 3]).to.include(4, 'nooo why fail??'); + * expect([1, 2, 3], 'nooo why fail??').to.include(4); + * + * `.include` can also be used as a language chain, causing all `.members` and + * `.keys` assertions that follow in the chain to require the target to be a + * superset of the expected set, rather than an identical set. Note that + * `.members` ignores duplicates in the subset when `.include` is added. + * + * // Target object's keys are a superset of ['a', 'b'] but not identical + * expect({a: 1, b: 2, c: 3}).to.include.all.keys('a', 'b'); + * expect({a: 1, b: 2, c: 3}).to.not.have.all.keys('a', 'b'); + * + * // Target array is a superset of [1, 2] but not identical + * expect([1, 2, 3]).to.include.members([1, 2]); + * expect([1, 2, 3]).to.not.have.members([1, 2]); + * + * // Duplicates in the subset are ignored + * expect([1, 2, 3]).to.include.members([1, 2, 2, 2]); + * + * Note that adding `.any` earlier in the chain causes the `.keys` assertion + * to ignore `.include`. + * + * // Both assertions are identical + * expect({a: 1}).to.include.any.keys('a', 'b'); + * expect({a: 1}).to.have.any.keys('a', 'b'); + * + * The aliases `.includes`, `.contain`, and `.contains` can be used + * interchangeably with `.include`. + * + * @name include + * @alias contain + * @alias includes + * @alias contains + * @param {Mixed} val + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function SameValueZero(a, b) { + return (_.isNaN(a) && _.isNaN(b)) || a === b; + } + + function includeChainingBehavior () { + flag(this, 'contains', true); + } + + function include (val, msg) { + if (msg) flag(this, 'message', msg); + + var obj = flag(this, 'object') + , objType = _.type(obj).toLowerCase() + , flagMsg = flag(this, 'message') + , negate = flag(this, 'negate') + , ssfi = flag(this, 'ssfi') + , isDeep = flag(this, 'deep') + , descriptor = isDeep ? 'deep ' : ''; + + flagMsg = flagMsg ? flagMsg + ': ' : ''; + + var included = false; + + switch (objType) { + case 'string': + included = obj.indexOf(val) !== -1; + break; + + case 'weakset': + if (isDeep) { + throw new AssertionError( + flagMsg + 'unable to use .deep.include with WeakSet', + undefined, + ssfi + ); + } + + included = obj.has(val); + break; + + case 'map': + var isEql = isDeep ? _.eql : SameValueZero; + obj.forEach(function (item) { + included = included || isEql(item, val); + }); + break; + + case 'set': + if (isDeep) { + obj.forEach(function (item) { + included = included || _.eql(item, val); + }); + } else { + included = obj.has(val); + } + break; + + case 'array': + if (isDeep) { + included = obj.some(function (item) { + return _.eql(item, val); + }) + } else { + included = obj.indexOf(val) !== -1; + } + break; + + default: + // This block is for asserting a subset of properties in an object. + // `_.expectTypes` isn't used here because `.include` should work with + // objects with a custom `@@toStringTag`. + if (val !== Object(val)) { + throw new AssertionError( + flagMsg + 'object tested must be an array, a map, an object,' + + ' a set, a string, or a weakset, but ' + objType + ' given', + undefined, + ssfi + ); + } + + var props = Object.keys(val) + , firstErr = null + , numErrs = 0; + + props.forEach(function (prop) { + var propAssertion = new Assertion(obj); + _.transferFlags(this, propAssertion, true); + flag(propAssertion, 'lockSsfi', true); + + if (!negate || props.length === 1) { + propAssertion.property(prop, val[prop]); + return; + } + + try { + propAssertion.property(prop, val[prop]); + } catch (err) { + if (!_.checkError.compatibleConstructor(err, AssertionError)) { + throw err; + } + if (firstErr === null) firstErr = err; + numErrs++; + } + }, this); + + // When validating .not.include with multiple properties, we only want + // to throw an assertion error if all of the properties are included, + // in which case we throw the first property assertion error that we + // encountered. + if (negate && props.length > 1 && numErrs === props.length) { + throw firstErr; + } + return; + } + + // Assert inclusion in collection or substring in a string. + this.assert( + included + , 'expected #{this} to ' + descriptor + 'include ' + _.inspect(val) + , 'expected #{this} to not ' + descriptor + 'include ' + _.inspect(val)); + } + + Assertion.addChainableMethod('include', include, includeChainingBehavior); + Assertion.addChainableMethod('contain', include, includeChainingBehavior); + Assertion.addChainableMethod('contains', include, includeChainingBehavior); + Assertion.addChainableMethod('includes', include, includeChainingBehavior); + + /** + * ### .ok + * + * Asserts that the target is a truthy value (considered `true` in boolean context). + * However, it's often best to assert that the target is strictly (`===`) or + * deeply equal to its expected value. + * + * expect(1).to.equal(1); // Recommended + * expect(1).to.be.ok; // Not recommended + * + * expect(true).to.be.true; // Recommended + * expect(true).to.be.ok; // Not recommended + * + * Add `.not` earlier in the chain to negate `.ok`. + * + * expect(0).to.equal(0); // Recommended + * expect(0).to.not.be.ok; // Not recommended + * + * expect(false).to.be.false; // Recommended + * expect(false).to.not.be.ok; // Not recommended + * + * expect(null).to.be.null; // Recommended + * expect(null).to.not.be.ok; // Not recommended + * + * expect(undefined).to.be.undefined; // Recommended + * expect(undefined).to.not.be.ok; // Not recommended + * + * A custom error message can be given as the second argument to `expect`. + * + * expect(false, 'nooo why fail??').to.be.ok; + * + * @name ok + * @namespace BDD + * @api public + */ + + Assertion.addProperty('ok', function () { + this.assert( + flag(this, 'object') + , 'expected #{this} to be truthy' + , 'expected #{this} to be falsy'); + }); + + /** + * ### .true + * + * Asserts that the target is strictly (`===`) equal to `true`. + * + * expect(true).to.be.true; + * + * Add `.not` earlier in the chain to negate `.true`. However, it's often best + * to assert that the target is equal to its expected value, rather than not + * equal to `true`. + * + * expect(false).to.be.false; // Recommended + * expect(false).to.not.be.true; // Not recommended + * + * expect(1).to.equal(1); // Recommended + * expect(1).to.not.be.true; // Not recommended + * + * A custom error message can be given as the second argument to `expect`. + * + * expect(false, 'nooo why fail??').to.be.true; + * + * @name true + * @namespace BDD + * @api public + */ + + Assertion.addProperty('true', function () { + this.assert( + true === flag(this, 'object') + , 'expected #{this} to be true' + , 'expected #{this} to be false' + , flag(this, 'negate') ? false : true + ); + }); + + /** + * ### .false + * + * Asserts that the target is strictly (`===`) equal to `false`. + * + * expect(false).to.be.false; + * + * Add `.not` earlier in the chain to negate `.false`. However, it's often + * best to assert that the target is equal to its expected value, rather than + * not equal to `false`. + * + * expect(true).to.be.true; // Recommended + * expect(true).to.not.be.false; // Not recommended + * + * expect(1).to.equal(1); // Recommended + * expect(1).to.not.be.false; // Not recommended + * + * A custom error message can be given as the second argument to `expect`. + * + * expect(true, 'nooo why fail??').to.be.false; + * + * @name false + * @namespace BDD + * @api public + */ + + Assertion.addProperty('false', function () { + this.assert( + false === flag(this, 'object') + , 'expected #{this} to be false' + , 'expected #{this} to be true' + , flag(this, 'negate') ? true : false + ); + }); + + /** + * ### .null + * + * Asserts that the target is strictly (`===`) equal to `null`. + * + * expect(null).to.be.null; + * + * Add `.not` earlier in the chain to negate `.null`. However, it's often best + * to assert that the target is equal to its expected value, rather than not + * equal to `null`. + * + * expect(1).to.equal(1); // Recommended + * expect(1).to.not.be.null; // Not recommended + * + * A custom error message can be given as the second argument to `expect`. + * + * expect(42, 'nooo why fail??').to.be.null; + * + * @name null + * @namespace BDD + * @api public + */ + + Assertion.addProperty('null', function () { + this.assert( + null === flag(this, 'object') + , 'expected #{this} to be null' + , 'expected #{this} not to be null' + ); + }); + + /** + * ### .undefined + * + * Asserts that the target is strictly (`===`) equal to `undefined`. + * + * expect(undefined).to.be.undefined; + * + * Add `.not` earlier in the chain to negate `.undefined`. However, it's often + * best to assert that the target is equal to its expected value, rather than + * not equal to `undefined`. + * + * expect(1).to.equal(1); // Recommended + * expect(1).to.not.be.undefined; // Not recommended + * + * A custom error message can be given as the second argument to `expect`. + * + * expect(42, 'nooo why fail??').to.be.undefined; + * + * @name undefined + * @namespace BDD + * @api public + */ + + Assertion.addProperty('undefined', function () { + this.assert( + undefined === flag(this, 'object') + , 'expected #{this} to be undefined' + , 'expected #{this} not to be undefined' + ); + }); + + /** + * ### .NaN + * + * Asserts that the target is exactly `NaN`. + * + * expect(NaN).to.be.NaN; + * + * Add `.not` earlier in the chain to negate `.NaN`. However, it's often best + * to assert that the target is equal to its expected value, rather than not + * equal to `NaN`. + * + * expect('foo').to.equal('foo'); // Recommended + * expect('foo').to.not.be.NaN; // Not recommended + * + * A custom error message can be given as the second argument to `expect`. + * + * expect(42, 'nooo why fail??').to.be.NaN; + * + * @name NaN + * @namespace BDD + * @api public + */ + + Assertion.addProperty('NaN', function () { + this.assert( + _.isNaN(flag(this, 'object')) + , 'expected #{this} to be NaN' + , 'expected #{this} not to be NaN' + ); + }); + + /** + * ### .exist + * + * Asserts that the target is not strictly (`===`) equal to either `null` or + * `undefined`. However, it's often best to assert that the target is equal to + * its expected value. + * + * expect(1).to.equal(1); // Recommended + * expect(1).to.exist; // Not recommended + * + * expect(0).to.equal(0); // Recommended + * expect(0).to.exist; // Not recommended + * + * Add `.not` earlier in the chain to negate `.exist`. + * + * expect(null).to.be.null; // Recommended + * expect(null).to.not.exist; // Not recommended + * + * expect(undefined).to.be.undefined; // Recommended + * expect(undefined).to.not.exist; // Not recommended + * + * A custom error message can be given as the second argument to `expect`. + * + * expect(null, 'nooo why fail??').to.exist; + * + * @name exist + * @namespace BDD + * @api public + */ + + Assertion.addProperty('exist', function () { + var val = flag(this, 'object'); + this.assert( + val !== null && val !== undefined + , 'expected #{this} to exist' + , 'expected #{this} to not exist' + ); + }); + + /** + * ### .empty + * + * When the target is a string or array, `.empty` asserts that the target's + * `length` property is strictly (`===`) equal to `0`. + * + * expect([]).to.be.empty; + * expect('').to.be.empty; + * + * When the target is a map or set, `.empty` asserts that the target's `size` + * property is strictly equal to `0`. + * + * expect(new Set()).to.be.empty; + * expect(new Map()).to.be.empty; + * + * When the target is a non-function object, `.empty` asserts that the target + * doesn't have any own enumerable properties. Properties with Symbol-based + * keys are excluded from the count. + * + * expect({}).to.be.empty; + * + * Because `.empty` does different things based on the target's type, it's + * important to check the target's type before using `.empty`. See the `.a` + * doc for info on testing a target's type. + * + * expect([]).to.be.an('array').that.is.empty; + * + * Add `.not` earlier in the chain to negate `.empty`. However, it's often + * best to assert that the target contains its expected number of values, + * rather than asserting that it's not empty. + * + * expect([1, 2, 3]).to.have.lengthOf(3); // Recommended + * expect([1, 2, 3]).to.not.be.empty; // Not recommended + * + * expect(new Set([1, 2, 3])).to.have.property('size', 3); // Recommended + * expect(new Set([1, 2, 3])).to.not.be.empty; // Not recommended + * + * expect(Object.keys({a: 1})).to.have.lengthOf(1); // Recommended + * expect({a: 1}).to.not.be.empty; // Not recommended + * + * A custom error message can be given as the second argument to `expect`. + * + * expect([1, 2, 3], 'nooo why fail??').to.be.empty; + * + * @name empty + * @namespace BDD + * @api public + */ + + Assertion.addProperty('empty', function () { + var val = flag(this, 'object') + , ssfi = flag(this, 'ssfi') + , flagMsg = flag(this, 'message') + , itemsCount; + + flagMsg = flagMsg ? flagMsg + ': ' : ''; + + switch (_.type(val).toLowerCase()) { + case 'array': + case 'string': + itemsCount = val.length; + break; + case 'map': + case 'set': + itemsCount = val.size; + break; + case 'weakmap': + case 'weakset': + throw new AssertionError( + flagMsg + '.empty was passed a weak collection', + undefined, + ssfi + ); + case 'function': + var msg = flagMsg + '.empty was passed a function ' + _.getName(val); + throw new AssertionError(msg.trim(), undefined, ssfi); + default: + if (val !== Object(val)) { + throw new AssertionError( + flagMsg + '.empty was passed non-string primitive ' + _.inspect(val), + undefined, + ssfi + ); + } + itemsCount = Object.keys(val).length; + } + + this.assert( + 0 === itemsCount + , 'expected #{this} to be empty' + , 'expected #{this} not to be empty' + ); + }); + + /** + * ### .arguments + * + * Asserts that the target is an `arguments` object. + * + * function test () { + * expect(arguments).to.be.arguments; + * } + * + * test(); + * + * Add `.not` earlier in the chain to negate `.arguments`. However, it's often + * best to assert which type the target is expected to be, rather than + * asserting that its not an `arguments` object. + * + * expect('foo').to.be.a('string'); // Recommended + * expect('foo').to.not.be.arguments; // Not recommended + * + * A custom error message can be given as the second argument to `expect`. + * + * expect({}, 'nooo why fail??').to.be.arguments; + * + * The alias `.Arguments` can be used interchangeably with `.arguments`. + * + * @name arguments + * @alias Arguments + * @namespace BDD + * @api public + */ + + function checkArguments () { + var obj = flag(this, 'object') + , type = _.type(obj); + this.assert( + 'Arguments' === type + , 'expected #{this} to be arguments but got ' + type + , 'expected #{this} to not be arguments' + ); + } + + Assertion.addProperty('arguments', checkArguments); + Assertion.addProperty('Arguments', checkArguments); + + /** + * ### .equal(val[, msg]) + * + * Asserts that the target is strictly (`===`) equal to the given `val`. + * + * expect(1).to.equal(1); + * expect('foo').to.equal('foo'); + * + * Add `.deep` earlier in the chain to use deep equality instead. See the + * `deep-eql` project page for info on the deep equality algorithm: + * https://github.com/chaijs/deep-eql. + * + * // Target object deeply (but not strictly) equals `{a: 1}` + * expect({a: 1}).to.deep.equal({a: 1}); + * expect({a: 1}).to.not.equal({a: 1}); + * + * // Target array deeply (but not strictly) equals `[1, 2]` + * expect([1, 2]).to.deep.equal([1, 2]); + * expect([1, 2]).to.not.equal([1, 2]); + * + * Add `.not` earlier in the chain to negate `.equal`. However, it's often + * best to assert that the target is equal to its expected value, rather than + * not equal to one of countless unexpected values. + * + * expect(1).to.equal(1); // Recommended + * expect(1).to.not.equal(2); // Not recommended + * + * `.equal` accepts an optional `msg` argument which is a custom error message + * to show when the assertion fails. The message can also be given as the + * second argument to `expect`. + * + * expect(1).to.equal(2, 'nooo why fail??'); + * expect(1, 'nooo why fail??').to.equal(2); + * + * The aliases `.equals` and `eq` can be used interchangeably with `.equal`. + * + * @name equal + * @alias equals + * @alias eq + * @param {Mixed} val + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function assertEqual (val, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object'); + if (flag(this, 'deep')) { + var prevLockSsfi = flag(this, 'lockSsfi'); + flag(this, 'lockSsfi', true); + this.eql(val); + flag(this, 'lockSsfi', prevLockSsfi); + } else { + this.assert( + val === obj + , 'expected #{this} to equal #{exp}' + , 'expected #{this} to not equal #{exp}' + , val + , this._obj + , true + ); + } + } + + Assertion.addMethod('equal', assertEqual); + Assertion.addMethod('equals', assertEqual); + Assertion.addMethod('eq', assertEqual); + + /** + * ### .eql(obj[, msg]) + * + * Asserts that the target is deeply equal to the given `obj`. See the + * `deep-eql` project page for info on the deep equality algorithm: + * https://github.com/chaijs/deep-eql. + * + * // Target object is deeply (but not strictly) equal to {a: 1} + * expect({a: 1}).to.eql({a: 1}).but.not.equal({a: 1}); + * + * // Target array is deeply (but not strictly) equal to [1, 2] + * expect([1, 2]).to.eql([1, 2]).but.not.equal([1, 2]); + * + * Add `.not` earlier in the chain to negate `.eql`. However, it's often best + * to assert that the target is deeply equal to its expected value, rather + * than not deeply equal to one of countless unexpected values. + * + * expect({a: 1}).to.eql({a: 1}); // Recommended + * expect({a: 1}).to.not.eql({b: 2}); // Not recommended + * + * `.eql` accepts an optional `msg` argument which is a custom error message + * to show when the assertion fails. The message can also be given as the + * second argument to `expect`. + * + * expect({a: 1}).to.eql({b: 2}, 'nooo why fail??'); + * expect({a: 1}, 'nooo why fail??').to.eql({b: 2}); + * + * The alias `.eqls` can be used interchangeably with `.eql`. + * + * The `.deep.equal` assertion is almost identical to `.eql` but with one + * difference: `.deep.equal` causes deep equality comparisons to also be used + * for any other assertions that follow in the chain. + * + * @name eql + * @alias eqls + * @param {Mixed} obj + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function assertEql(obj, msg) { + if (msg) flag(this, 'message', msg); + this.assert( + _.eql(obj, flag(this, 'object')) + , 'expected #{this} to deeply equal #{exp}' + , 'expected #{this} to not deeply equal #{exp}' + , obj + , this._obj + , true + ); + } + + Assertion.addMethod('eql', assertEql); + Assertion.addMethod('eqls', assertEql); + + /** + * ### .above(n[, msg]) + * + * Asserts that the target is a number or a date greater than the given number or date `n` respectively. + * However, it's often best to assert that the target is equal to its expected + * value. + * + * expect(2).to.equal(2); // Recommended + * expect(2).to.be.above(1); // Not recommended + * + * Add `.lengthOf` earlier in the chain to assert that the target's `length` + * or `size` is greater than the given number `n`. + * + * expect('foo').to.have.lengthOf(3); // Recommended + * expect('foo').to.have.lengthOf.above(2); // Not recommended + * + * expect([1, 2, 3]).to.have.lengthOf(3); // Recommended + * expect([1, 2, 3]).to.have.lengthOf.above(2); // Not recommended + * + * Add `.not` earlier in the chain to negate `.above`. + * + * expect(2).to.equal(2); // Recommended + * expect(1).to.not.be.above(2); // Not recommended + * + * `.above` accepts an optional `msg` argument which is a custom error message + * to show when the assertion fails. The message can also be given as the + * second argument to `expect`. + * + * expect(1).to.be.above(2, 'nooo why fail??'); + * expect(1, 'nooo why fail??').to.be.above(2); + * + * The aliases `.gt` and `.greaterThan` can be used interchangeably with + * `.above`. + * + * @name above + * @alias gt + * @alias greaterThan + * @param {Number} n + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function assertAbove (n, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object') + , doLength = flag(this, 'doLength') + , flagMsg = flag(this, 'message') + , msgPrefix = ((flagMsg) ? flagMsg + ': ' : '') + , ssfi = flag(this, 'ssfi') + , objType = _.type(obj).toLowerCase() + , nType = _.type(n).toLowerCase() + , errorMessage + , shouldThrow = true; + + if (doLength && objType !== 'map' && objType !== 'set') { + new Assertion(obj, flagMsg, ssfi, true).to.have.property('length'); + } + + if (!doLength && (objType === 'date' && nType !== 'date')) { + errorMessage = msgPrefix + 'the argument to above must be a date'; + } else if (nType !== 'number' && (doLength || objType === 'number')) { + errorMessage = msgPrefix + 'the argument to above must be a number'; + } else if (!doLength && (objType !== 'date' && objType !== 'number')) { + var printObj = (objType === 'string') ? "'" + obj + "'" : obj; + errorMessage = msgPrefix + 'expected ' + printObj + ' to be a number or a date'; + } else { + shouldThrow = false; + } + + if (shouldThrow) { + throw new AssertionError(errorMessage, undefined, ssfi); + } + + if (doLength) { + var descriptor = 'length' + , itemsCount; + if (objType === 'map' || objType === 'set') { + descriptor = 'size'; + itemsCount = obj.size; + } else { + itemsCount = obj.length; + } + this.assert( + itemsCount > n + , 'expected #{this} to have a ' + descriptor + ' above #{exp} but got #{act}' + , 'expected #{this} to not have a ' + descriptor + ' above #{exp}' + , n + , itemsCount + ); + } else { + this.assert( + obj > n + , 'expected #{this} to be above #{exp}' + , 'expected #{this} to be at most #{exp}' + , n + ); + } + } + + Assertion.addMethod('above', assertAbove); + Assertion.addMethod('gt', assertAbove); + Assertion.addMethod('greaterThan', assertAbove); + + /** + * ### .least(n[, msg]) + * + * Asserts that the target is a number or a date greater than or equal to the given + * number or date `n` respectively. However, it's often best to assert that the target is equal to + * its expected value. + * + * expect(2).to.equal(2); // Recommended + * expect(2).to.be.at.least(1); // Not recommended + * expect(2).to.be.at.least(2); // Not recommended + * + * Add `.lengthOf` earlier in the chain to assert that the target's `length` + * or `size` is greater than or equal to the given number `n`. + * + * expect('foo').to.have.lengthOf(3); // Recommended + * expect('foo').to.have.lengthOf.at.least(2); // Not recommended + * + * expect([1, 2, 3]).to.have.lengthOf(3); // Recommended + * expect([1, 2, 3]).to.have.lengthOf.at.least(2); // Not recommended + * + * Add `.not` earlier in the chain to negate `.least`. + * + * expect(1).to.equal(1); // Recommended + * expect(1).to.not.be.at.least(2); // Not recommended + * + * `.least` accepts an optional `msg` argument which is a custom error message + * to show when the assertion fails. The message can also be given as the + * second argument to `expect`. + * + * expect(1).to.be.at.least(2, 'nooo why fail??'); + * expect(1, 'nooo why fail??').to.be.at.least(2); + * + * The alias `.gte` can be used interchangeably with `.least`. + * + * @name least + * @alias gte + * @param {Number} n + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function assertLeast (n, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object') + , doLength = flag(this, 'doLength') + , flagMsg = flag(this, 'message') + , msgPrefix = ((flagMsg) ? flagMsg + ': ' : '') + , ssfi = flag(this, 'ssfi') + , objType = _.type(obj).toLowerCase() + , nType = _.type(n).toLowerCase() + , errorMessage + , shouldThrow = true; + + if (doLength && objType !== 'map' && objType !== 'set') { + new Assertion(obj, flagMsg, ssfi, true).to.have.property('length'); + } + + if (!doLength && (objType === 'date' && nType !== 'date')) { + errorMessage = msgPrefix + 'the argument to least must be a date'; + } else if (nType !== 'number' && (doLength || objType === 'number')) { + errorMessage = msgPrefix + 'the argument to least must be a number'; + } else if (!doLength && (objType !== 'date' && objType !== 'number')) { + var printObj = (objType === 'string') ? "'" + obj + "'" : obj; + errorMessage = msgPrefix + 'expected ' + printObj + ' to be a number or a date'; + } else { + shouldThrow = false; + } + + if (shouldThrow) { + throw new AssertionError(errorMessage, undefined, ssfi); + } + + if (doLength) { + var descriptor = 'length' + , itemsCount; + if (objType === 'map' || objType === 'set') { + descriptor = 'size'; + itemsCount = obj.size; + } else { + itemsCount = obj.length; + } + this.assert( + itemsCount >= n + , 'expected #{this} to have a ' + descriptor + ' at least #{exp} but got #{act}' + , 'expected #{this} to have a ' + descriptor + ' below #{exp}' + , n + , itemsCount + ); + } else { + this.assert( + obj >= n + , 'expected #{this} to be at least #{exp}' + , 'expected #{this} to be below #{exp}' + , n + ); + } + } + + Assertion.addMethod('least', assertLeast); + Assertion.addMethod('gte', assertLeast); + + /** + * ### .below(n[, msg]) + * + * Asserts that the target is a number or a date less than the given number or date `n` respectively. + * However, it's often best to assert that the target is equal to its expected + * value. + * + * expect(1).to.equal(1); // Recommended + * expect(1).to.be.below(2); // Not recommended + * + * Add `.lengthOf` earlier in the chain to assert that the target's `length` + * or `size` is less than the given number `n`. + * + * expect('foo').to.have.lengthOf(3); // Recommended + * expect('foo').to.have.lengthOf.below(4); // Not recommended + * + * expect([1, 2, 3]).to.have.length(3); // Recommended + * expect([1, 2, 3]).to.have.lengthOf.below(4); // Not recommended + * + * Add `.not` earlier in the chain to negate `.below`. + * + * expect(2).to.equal(2); // Recommended + * expect(2).to.not.be.below(1); // Not recommended + * + * `.below` accepts an optional `msg` argument which is a custom error message + * to show when the assertion fails. The message can also be given as the + * second argument to `expect`. + * + * expect(2).to.be.below(1, 'nooo why fail??'); + * expect(2, 'nooo why fail??').to.be.below(1); + * + * The aliases `.lt` and `.lessThan` can be used interchangeably with + * `.below`. + * + * @name below + * @alias lt + * @alias lessThan + * @param {Number} n + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function assertBelow (n, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object') + , doLength = flag(this, 'doLength') + , flagMsg = flag(this, 'message') + , msgPrefix = ((flagMsg) ? flagMsg + ': ' : '') + , ssfi = flag(this, 'ssfi') + , objType = _.type(obj).toLowerCase() + , nType = _.type(n).toLowerCase() + , errorMessage + , shouldThrow = true; + + if (doLength && objType !== 'map' && objType !== 'set') { + new Assertion(obj, flagMsg, ssfi, true).to.have.property('length'); + } + + if (!doLength && (objType === 'date' && nType !== 'date')) { + errorMessage = msgPrefix + 'the argument to below must be a date'; + } else if (nType !== 'number' && (doLength || objType === 'number')) { + errorMessage = msgPrefix + 'the argument to below must be a number'; + } else if (!doLength && (objType !== 'date' && objType !== 'number')) { + var printObj = (objType === 'string') ? "'" + obj + "'" : obj; + errorMessage = msgPrefix + 'expected ' + printObj + ' to be a number or a date'; + } else { + shouldThrow = false; + } + + if (shouldThrow) { + throw new AssertionError(errorMessage, undefined, ssfi); + } + + if (doLength) { + var descriptor = 'length' + , itemsCount; + if (objType === 'map' || objType === 'set') { + descriptor = 'size'; + itemsCount = obj.size; + } else { + itemsCount = obj.length; + } + this.assert( + itemsCount < n + , 'expected #{this} to have a ' + descriptor + ' below #{exp} but got #{act}' + , 'expected #{this} to not have a ' + descriptor + ' below #{exp}' + , n + , itemsCount + ); + } else { + this.assert( + obj < n + , 'expected #{this} to be below #{exp}' + , 'expected #{this} to be at least #{exp}' + , n + ); + } + } + + Assertion.addMethod('below', assertBelow); + Assertion.addMethod('lt', assertBelow); + Assertion.addMethod('lessThan', assertBelow); + + /** + * ### .most(n[, msg]) + * + * Asserts that the target is a number or a date less than or equal to the given number + * or date `n` respectively. However, it's often best to assert that the target is equal to its + * expected value. + * + * expect(1).to.equal(1); // Recommended + * expect(1).to.be.at.most(2); // Not recommended + * expect(1).to.be.at.most(1); // Not recommended + * + * Add `.lengthOf` earlier in the chain to assert that the target's `length` + * or `size` is less than or equal to the given number `n`. + * + * expect('foo').to.have.lengthOf(3); // Recommended + * expect('foo').to.have.lengthOf.at.most(4); // Not recommended + * + * expect([1, 2, 3]).to.have.lengthOf(3); // Recommended + * expect([1, 2, 3]).to.have.lengthOf.at.most(4); // Not recommended + * + * Add `.not` earlier in the chain to negate `.most`. + * + * expect(2).to.equal(2); // Recommended + * expect(2).to.not.be.at.most(1); // Not recommended + * + * `.most` accepts an optional `msg` argument which is a custom error message + * to show when the assertion fails. The message can also be given as the + * second argument to `expect`. + * + * expect(2).to.be.at.most(1, 'nooo why fail??'); + * expect(2, 'nooo why fail??').to.be.at.most(1); + * + * The alias `.lte` can be used interchangeably with `.most`. + * + * @name most + * @alias lte + * @param {Number} n + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function assertMost (n, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object') + , doLength = flag(this, 'doLength') + , flagMsg = flag(this, 'message') + , msgPrefix = ((flagMsg) ? flagMsg + ': ' : '') + , ssfi = flag(this, 'ssfi') + , objType = _.type(obj).toLowerCase() + , nType = _.type(n).toLowerCase() + , errorMessage + , shouldThrow = true; + + if (doLength && objType !== 'map' && objType !== 'set') { + new Assertion(obj, flagMsg, ssfi, true).to.have.property('length'); + } + + if (!doLength && (objType === 'date' && nType !== 'date')) { + errorMessage = msgPrefix + 'the argument to most must be a date'; + } else if (nType !== 'number' && (doLength || objType === 'number')) { + errorMessage = msgPrefix + 'the argument to most must be a number'; + } else if (!doLength && (objType !== 'date' && objType !== 'number')) { + var printObj = (objType === 'string') ? "'" + obj + "'" : obj; + errorMessage = msgPrefix + 'expected ' + printObj + ' to be a number or a date'; + } else { + shouldThrow = false; + } + + if (shouldThrow) { + throw new AssertionError(errorMessage, undefined, ssfi); + } + + if (doLength) { + var descriptor = 'length' + , itemsCount; + if (objType === 'map' || objType === 'set') { + descriptor = 'size'; + itemsCount = obj.size; + } else { + itemsCount = obj.length; + } + this.assert( + itemsCount <= n + , 'expected #{this} to have a ' + descriptor + ' at most #{exp} but got #{act}' + , 'expected #{this} to have a ' + descriptor + ' above #{exp}' + , n + , itemsCount + ); + } else { + this.assert( + obj <= n + , 'expected #{this} to be at most #{exp}' + , 'expected #{this} to be above #{exp}' + , n + ); + } + } + + Assertion.addMethod('most', assertMost); + Assertion.addMethod('lte', assertMost); + + /** + * ### .within(start, finish[, msg]) + * + * Asserts that the target is a number or a date greater than or equal to the given + * number or date `start`, and less than or equal to the given number or date `finish` respectively. + * However, it's often best to assert that the target is equal to its expected + * value. + * + * expect(2).to.equal(2); // Recommended + * expect(2).to.be.within(1, 3); // Not recommended + * expect(2).to.be.within(2, 3); // Not recommended + * expect(2).to.be.within(1, 2); // Not recommended + * + * Add `.lengthOf` earlier in the chain to assert that the target's `length` + * or `size` is greater than or equal to the given number `start`, and less + * than or equal to the given number `finish`. + * + * expect('foo').to.have.lengthOf(3); // Recommended + * expect('foo').to.have.lengthOf.within(2, 4); // Not recommended + * + * expect([1, 2, 3]).to.have.lengthOf(3); // Recommended + * expect([1, 2, 3]).to.have.lengthOf.within(2, 4); // Not recommended + * + * Add `.not` earlier in the chain to negate `.within`. + * + * expect(1).to.equal(1); // Recommended + * expect(1).to.not.be.within(2, 4); // Not recommended + * + * `.within` accepts an optional `msg` argument which is a custom error + * message to show when the assertion fails. The message can also be given as + * the second argument to `expect`. + * + * expect(4).to.be.within(1, 3, 'nooo why fail??'); + * expect(4, 'nooo why fail??').to.be.within(1, 3); + * + * @name within + * @param {Number} start lower bound inclusive + * @param {Number} finish upper bound inclusive + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + Assertion.addMethod('within', function (start, finish, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object') + , doLength = flag(this, 'doLength') + , flagMsg = flag(this, 'message') + , msgPrefix = ((flagMsg) ? flagMsg + ': ' : '') + , ssfi = flag(this, 'ssfi') + , objType = _.type(obj).toLowerCase() + , startType = _.type(start).toLowerCase() + , finishType = _.type(finish).toLowerCase() + , errorMessage + , shouldThrow = true + , range = (startType === 'date' && finishType === 'date') + ? start.toUTCString() + '..' + finish.toUTCString() + : start + '..' + finish; + + if (doLength && objType !== 'map' && objType !== 'set') { + new Assertion(obj, flagMsg, ssfi, true).to.have.property('length'); + } + + if (!doLength && (objType === 'date' && (startType !== 'date' || finishType !== 'date'))) { + errorMessage = msgPrefix + 'the arguments to within must be dates'; + } else if ((startType !== 'number' || finishType !== 'number') && (doLength || objType === 'number')) { + errorMessage = msgPrefix + 'the arguments to within must be numbers'; + } else if (!doLength && (objType !== 'date' && objType !== 'number')) { + var printObj = (objType === 'string') ? "'" + obj + "'" : obj; + errorMessage = msgPrefix + 'expected ' + printObj + ' to be a number or a date'; + } else { + shouldThrow = false; + } + + if (shouldThrow) { + throw new AssertionError(errorMessage, undefined, ssfi); + } + + if (doLength) { + var descriptor = 'length' + , itemsCount; + if (objType === 'map' || objType === 'set') { + descriptor = 'size'; + itemsCount = obj.size; + } else { + itemsCount = obj.length; + } + this.assert( + itemsCount >= start && itemsCount <= finish + , 'expected #{this} to have a ' + descriptor + ' within ' + range + , 'expected #{this} to not have a ' + descriptor + ' within ' + range + ); + } else { + this.assert( + obj >= start && obj <= finish + , 'expected #{this} to be within ' + range + , 'expected #{this} to not be within ' + range + ); + } + }); + + /** + * ### .instanceof(constructor[, msg]) + * + * Asserts that the target is an instance of the given `constructor`. + * + * function Cat () { } + * + * expect(new Cat()).to.be.an.instanceof(Cat); + * expect([1, 2]).to.be.an.instanceof(Array); + * + * Add `.not` earlier in the chain to negate `.instanceof`. + * + * expect({a: 1}).to.not.be.an.instanceof(Array); + * + * `.instanceof` accepts an optional `msg` argument which is a custom error + * message to show when the assertion fails. The message can also be given as + * the second argument to `expect`. + * + * expect(1).to.be.an.instanceof(Array, 'nooo why fail??'); + * expect(1, 'nooo why fail??').to.be.an.instanceof(Array); + * + * Due to limitations in ES5, `.instanceof` may not always work as expected + * when using a transpiler such as Babel or TypeScript. In particular, it may + * produce unexpected results when subclassing built-in object such as + * `Array`, `Error`, and `Map`. See your transpiler's docs for details: + * + * - ([Babel](https://babeljs.io/docs/usage/caveats/#classes)) + * - ([TypeScript](https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work)) + * + * The alias `.instanceOf` can be used interchangeably with `.instanceof`. + * + * @name instanceof + * @param {Constructor} constructor + * @param {String} msg _optional_ + * @alias instanceOf + * @namespace BDD + * @api public + */ + + function assertInstanceOf (constructor, msg) { + if (msg) flag(this, 'message', msg); + + var target = flag(this, 'object') + var ssfi = flag(this, 'ssfi'); + var flagMsg = flag(this, 'message'); + + try { + var isInstanceOf = target instanceof constructor; + } catch (err) { + if (err instanceof TypeError) { + flagMsg = flagMsg ? flagMsg + ': ' : ''; + throw new AssertionError( + flagMsg + 'The instanceof assertion needs a constructor but ' + + _.type(constructor) + ' was given.', + undefined, + ssfi + ); + } + throw err; + } + + var name = _.getName(constructor); + if (name === null) { + name = 'an unnamed constructor'; + } + + this.assert( + isInstanceOf + , 'expected #{this} to be an instance of ' + name + , 'expected #{this} to not be an instance of ' + name + ); + }; + + Assertion.addMethod('instanceof', assertInstanceOf); + Assertion.addMethod('instanceOf', assertInstanceOf); + + /** + * ### .property(name[, val[, msg]]) + * + * Asserts that the target has a property with the given key `name`. + * + * expect({a: 1}).to.have.property('a'); + * + * When `val` is provided, `.property` also asserts that the property's value + * is equal to the given `val`. + * + * expect({a: 1}).to.have.property('a', 1); + * + * By default, strict (`===`) equality is used. Add `.deep` earlier in the + * chain to use deep equality instead. See the `deep-eql` project page for + * info on the deep equality algorithm: https://github.com/chaijs/deep-eql. + * + * // Target object deeply (but not strictly) has property `x: {a: 1}` + * expect({x: {a: 1}}).to.have.deep.property('x', {a: 1}); + * expect({x: {a: 1}}).to.not.have.property('x', {a: 1}); + * + * The target's enumerable and non-enumerable properties are always included + * in the search. By default, both own and inherited properties are included. + * Add `.own` earlier in the chain to exclude inherited properties from the + * search. + * + * Object.prototype.b = 2; + * + * expect({a: 1}).to.have.own.property('a'); + * expect({a: 1}).to.have.own.property('a', 1); + * expect({a: 1}).to.have.property('b'); + * expect({a: 1}).to.not.have.own.property('b'); + * + * `.deep` and `.own` can be combined. + * + * expect({x: {a: 1}}).to.have.deep.own.property('x', {a: 1}); + * + * Add `.nested` earlier in the chain to enable dot- and bracket-notation when + * referencing nested properties. + * + * expect({a: {b: ['x', 'y']}}).to.have.nested.property('a.b[1]'); + * expect({a: {b: ['x', 'y']}}).to.have.nested.property('a.b[1]', 'y'); + * + * If `.` or `[]` are part of an actual property name, they can be escaped by + * adding two backslashes before them. + * + * expect({'.a': {'[b]': 'x'}}).to.have.nested.property('\\.a.\\[b\\]'); + * + * `.deep` and `.nested` can be combined. + * + * expect({a: {b: [{c: 3}]}}) + * .to.have.deep.nested.property('a.b[0]', {c: 3}); + * + * `.own` and `.nested` cannot be combined. + * + * Add `.not` earlier in the chain to negate `.property`. + * + * expect({a: 1}).to.not.have.property('b'); + * + * However, it's dangerous to negate `.property` when providing `val`. The + * problem is that it creates uncertain expectations by asserting that the + * target either doesn't have a property with the given key `name`, or that it + * does have a property with the given key `name` but its value isn't equal to + * the given `val`. It's often best to identify the exact output that's + * expected, and then write an assertion that only accepts that exact output. + * + * When the target isn't expected to have a property with the given key + * `name`, it's often best to assert exactly that. + * + * expect({b: 2}).to.not.have.property('a'); // Recommended + * expect({b: 2}).to.not.have.property('a', 1); // Not recommended + * + * When the target is expected to have a property with the given key `name`, + * it's often best to assert that the property has its expected value, rather + * than asserting that it doesn't have one of many unexpected values. + * + * expect({a: 3}).to.have.property('a', 3); // Recommended + * expect({a: 3}).to.not.have.property('a', 1); // Not recommended + * + * `.property` changes the target of any assertions that follow in the chain + * to be the value of the property from the original target object. + * + * expect({a: 1}).to.have.property('a').that.is.a('number'); + * + * `.property` accepts an optional `msg` argument which is a custom error + * message to show when the assertion fails. The message can also be given as + * the second argument to `expect`. When not providing `val`, only use the + * second form. + * + * // Recommended + * expect({a: 1}).to.have.property('a', 2, 'nooo why fail??'); + * expect({a: 1}, 'nooo why fail??').to.have.property('a', 2); + * expect({a: 1}, 'nooo why fail??').to.have.property('b'); + * + * // Not recommended + * expect({a: 1}).to.have.property('b', undefined, 'nooo why fail??'); + * + * The above assertion isn't the same thing as not providing `val`. Instead, + * it's asserting that the target object has a `b` property that's equal to + * `undefined`. + * + * The assertions `.ownProperty` and `.haveOwnProperty` can be used + * interchangeably with `.own.property`. + * + * @name property + * @param {String} name + * @param {Mixed} val (optional) + * @param {String} msg _optional_ + * @returns value of property for chaining + * @namespace BDD + * @api public + */ + + function assertProperty (name, val, msg) { + if (msg) flag(this, 'message', msg); + + var isNested = flag(this, 'nested') + , isOwn = flag(this, 'own') + , flagMsg = flag(this, 'message') + , obj = flag(this, 'object') + , ssfi = flag(this, 'ssfi') + , nameType = typeof name; + + flagMsg = flagMsg ? flagMsg + ': ' : ''; + + if (isNested) { + if (nameType !== 'string') { + throw new AssertionError( + flagMsg + 'the argument to property must be a string when using nested syntax', + undefined, + ssfi + ); + } + } else { + if (nameType !== 'string' && nameType !== 'number' && nameType !== 'symbol') { + throw new AssertionError( + flagMsg + 'the argument to property must be a string, number, or symbol', + undefined, + ssfi + ); + } + } + + if (isNested && isOwn) { + throw new AssertionError( + flagMsg + 'The "nested" and "own" flags cannot be combined.', + undefined, + ssfi + ); + } + + if (obj === null || obj === undefined) { + throw new AssertionError( + flagMsg + 'Target cannot be null or undefined.', + undefined, + ssfi + ); + } + + var isDeep = flag(this, 'deep') + , negate = flag(this, 'negate') + , pathInfo = isNested ? _.getPathInfo(obj, name) : null + , value = isNested ? pathInfo.value : obj[name]; + + var descriptor = ''; + if (isDeep) descriptor += 'deep '; + if (isOwn) descriptor += 'own '; + if (isNested) descriptor += 'nested '; + descriptor += 'property '; + + var hasProperty; + if (isOwn) hasProperty = Object.prototype.hasOwnProperty.call(obj, name); + else if (isNested) hasProperty = pathInfo.exists; + else hasProperty = _.hasProperty(obj, name); + + // When performing a negated assertion for both name and val, merely having + // a property with the given name isn't enough to cause the assertion to + // fail. It must both have a property with the given name, and the value of + // that property must equal the given val. Therefore, skip this assertion in + // favor of the next. + if (!negate || arguments.length === 1) { + this.assert( + hasProperty + , 'expected #{this} to have ' + descriptor + _.inspect(name) + , 'expected #{this} to not have ' + descriptor + _.inspect(name)); + } + + if (arguments.length > 1) { + this.assert( + hasProperty && (isDeep ? _.eql(val, value) : val === value) + , 'expected #{this} to have ' + descriptor + _.inspect(name) + ' of #{exp}, but got #{act}' + , 'expected #{this} to not have ' + descriptor + _.inspect(name) + ' of #{act}' + , val + , value + ); + } + + flag(this, 'object', value); + } + + Assertion.addMethod('property', assertProperty); + + function assertOwnProperty (name, value, msg) { + flag(this, 'own', true); + assertProperty.apply(this, arguments); + } + + Assertion.addMethod('ownProperty', assertOwnProperty); + Assertion.addMethod('haveOwnProperty', assertOwnProperty); + + /** + * ### .ownPropertyDescriptor(name[, descriptor[, msg]]) + * + * Asserts that the target has its own property descriptor with the given key + * `name`. Enumerable and non-enumerable properties are included in the + * search. + * + * expect({a: 1}).to.have.ownPropertyDescriptor('a'); + * + * When `descriptor` is provided, `.ownPropertyDescriptor` also asserts that + * the property's descriptor is deeply equal to the given `descriptor`. See + * the `deep-eql` project page for info on the deep equality algorithm: + * https://github.com/chaijs/deep-eql. + * + * expect({a: 1}).to.have.ownPropertyDescriptor('a', { + * configurable: true, + * enumerable: true, + * writable: true, + * value: 1, + * }); + * + * Add `.not` earlier in the chain to negate `.ownPropertyDescriptor`. + * + * expect({a: 1}).to.not.have.ownPropertyDescriptor('b'); + * + * However, it's dangerous to negate `.ownPropertyDescriptor` when providing + * a `descriptor`. The problem is that it creates uncertain expectations by + * asserting that the target either doesn't have a property descriptor with + * the given key `name`, or that it does have a property descriptor with the + * given key `name` but its not deeply equal to the given `descriptor`. It's + * often best to identify the exact output that's expected, and then write an + * assertion that only accepts that exact output. + * + * When the target isn't expected to have a property descriptor with the given + * key `name`, it's often best to assert exactly that. + * + * // Recommended + * expect({b: 2}).to.not.have.ownPropertyDescriptor('a'); + * + * // Not recommended + * expect({b: 2}).to.not.have.ownPropertyDescriptor('a', { + * configurable: true, + * enumerable: true, + * writable: true, + * value: 1, + * }); + * + * When the target is expected to have a property descriptor with the given + * key `name`, it's often best to assert that the property has its expected + * descriptor, rather than asserting that it doesn't have one of many + * unexpected descriptors. + * + * // Recommended + * expect({a: 3}).to.have.ownPropertyDescriptor('a', { + * configurable: true, + * enumerable: true, + * writable: true, + * value: 3, + * }); + * + * // Not recommended + * expect({a: 3}).to.not.have.ownPropertyDescriptor('a', { + * configurable: true, + * enumerable: true, + * writable: true, + * value: 1, + * }); + * + * `.ownPropertyDescriptor` changes the target of any assertions that follow + * in the chain to be the value of the property descriptor from the original + * target object. + * + * expect({a: 1}).to.have.ownPropertyDescriptor('a') + * .that.has.property('enumerable', true); + * + * `.ownPropertyDescriptor` accepts an optional `msg` argument which is a + * custom error message to show when the assertion fails. The message can also + * be given as the second argument to `expect`. When not providing + * `descriptor`, only use the second form. + * + * // Recommended + * expect({a: 1}).to.have.ownPropertyDescriptor('a', { + * configurable: true, + * enumerable: true, + * writable: true, + * value: 2, + * }, 'nooo why fail??'); + * + * // Recommended + * expect({a: 1}, 'nooo why fail??').to.have.ownPropertyDescriptor('a', { + * configurable: true, + * enumerable: true, + * writable: true, + * value: 2, + * }); + * + * // Recommended + * expect({a: 1}, 'nooo why fail??').to.have.ownPropertyDescriptor('b'); + * + * // Not recommended + * expect({a: 1}) + * .to.have.ownPropertyDescriptor('b', undefined, 'nooo why fail??'); + * + * The above assertion isn't the same thing as not providing `descriptor`. + * Instead, it's asserting that the target object has a `b` property + * descriptor that's deeply equal to `undefined`. + * + * The alias `.haveOwnPropertyDescriptor` can be used interchangeably with + * `.ownPropertyDescriptor`. + * + * @name ownPropertyDescriptor + * @alias haveOwnPropertyDescriptor + * @param {String} name + * @param {Object} descriptor _optional_ + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function assertOwnPropertyDescriptor (name, descriptor, msg) { + if (typeof descriptor === 'string') { + msg = descriptor; + descriptor = null; + } + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object'); + var actualDescriptor = Object.getOwnPropertyDescriptor(Object(obj), name); + if (actualDescriptor && descriptor) { + this.assert( + _.eql(descriptor, actualDescriptor) + , 'expected the own property descriptor for ' + _.inspect(name) + ' on #{this} to match ' + _.inspect(descriptor) + ', got ' + _.inspect(actualDescriptor) + , 'expected the own property descriptor for ' + _.inspect(name) + ' on #{this} to not match ' + _.inspect(descriptor) + , descriptor + , actualDescriptor + , true + ); + } else { + this.assert( + actualDescriptor + , 'expected #{this} to have an own property descriptor for ' + _.inspect(name) + , 'expected #{this} to not have an own property descriptor for ' + _.inspect(name) + ); + } + flag(this, 'object', actualDescriptor); + } + + Assertion.addMethod('ownPropertyDescriptor', assertOwnPropertyDescriptor); + Assertion.addMethod('haveOwnPropertyDescriptor', assertOwnPropertyDescriptor); + + /** + * ### .lengthOf(n[, msg]) + * + * Asserts that the target's `length` or `size` is equal to the given number + * `n`. + * + * expect([1, 2, 3]).to.have.lengthOf(3); + * expect('foo').to.have.lengthOf(3); + * expect(new Set([1, 2, 3])).to.have.lengthOf(3); + * expect(new Map([['a', 1], ['b', 2], ['c', 3]])).to.have.lengthOf(3); + * + * Add `.not` earlier in the chain to negate `.lengthOf`. However, it's often + * best to assert that the target's `length` property is equal to its expected + * value, rather than not equal to one of many unexpected values. + * + * expect('foo').to.have.lengthOf(3); // Recommended + * expect('foo').to.not.have.lengthOf(4); // Not recommended + * + * `.lengthOf` accepts an optional `msg` argument which is a custom error + * message to show when the assertion fails. The message can also be given as + * the second argument to `expect`. + * + * expect([1, 2, 3]).to.have.lengthOf(2, 'nooo why fail??'); + * expect([1, 2, 3], 'nooo why fail??').to.have.lengthOf(2); + * + * `.lengthOf` can also be used as a language chain, causing all `.above`, + * `.below`, `.least`, `.most`, and `.within` assertions that follow in the + * chain to use the target's `length` property as the target. However, it's + * often best to assert that the target's `length` property is equal to its + * expected length, rather than asserting that its `length` property falls + * within some range of values. + * + * // Recommended + * expect([1, 2, 3]).to.have.lengthOf(3); + * + * // Not recommended + * expect([1, 2, 3]).to.have.lengthOf.above(2); + * expect([1, 2, 3]).to.have.lengthOf.below(4); + * expect([1, 2, 3]).to.have.lengthOf.at.least(3); + * expect([1, 2, 3]).to.have.lengthOf.at.most(3); + * expect([1, 2, 3]).to.have.lengthOf.within(2,4); + * + * Due to a compatibility issue, the alias `.length` can't be chained directly + * off of an uninvoked method such as `.a`. Therefore, `.length` can't be used + * interchangeably with `.lengthOf` in every situation. It's recommended to + * always use `.lengthOf` instead of `.length`. + * + * expect([1, 2, 3]).to.have.a.length(3); // incompatible; throws error + * expect([1, 2, 3]).to.have.a.lengthOf(3); // passes as expected + * + * @name lengthOf + * @alias length + * @param {Number} n + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function assertLengthChain () { + flag(this, 'doLength', true); + } + + function assertLength (n, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object') + , objType = _.type(obj).toLowerCase() + , flagMsg = flag(this, 'message') + , ssfi = flag(this, 'ssfi') + , descriptor = 'length' + , itemsCount; + + switch (objType) { + case 'map': + case 'set': + descriptor = 'size'; + itemsCount = obj.size; + break; + default: + new Assertion(obj, flagMsg, ssfi, true).to.have.property('length'); + itemsCount = obj.length; + } + + this.assert( + itemsCount == n + , 'expected #{this} to have a ' + descriptor + ' of #{exp} but got #{act}' + , 'expected #{this} to not have a ' + descriptor + ' of #{act}' + , n + , itemsCount + ); + } + + Assertion.addChainableMethod('length', assertLength, assertLengthChain); + Assertion.addChainableMethod('lengthOf', assertLength, assertLengthChain); + + /** + * ### .match(re[, msg]) + * + * Asserts that the target matches the given regular expression `re`. + * + * expect('foobar').to.match(/^foo/); + * + * Add `.not` earlier in the chain to negate `.match`. + * + * expect('foobar').to.not.match(/taco/); + * + * `.match` accepts an optional `msg` argument which is a custom error message + * to show when the assertion fails. The message can also be given as the + * second argument to `expect`. + * + * expect('foobar').to.match(/taco/, 'nooo why fail??'); + * expect('foobar', 'nooo why fail??').to.match(/taco/); + * + * The alias `.matches` can be used interchangeably with `.match`. + * + * @name match + * @alias matches + * @param {RegExp} re + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + function assertMatch(re, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object'); + this.assert( + re.exec(obj) + , 'expected #{this} to match ' + re + , 'expected #{this} not to match ' + re + ); + } + + Assertion.addMethod('match', assertMatch); + Assertion.addMethod('matches', assertMatch); + + /** + * ### .string(str[, msg]) + * + * Asserts that the target string contains the given substring `str`. + * + * expect('foobar').to.have.string('bar'); + * + * Add `.not` earlier in the chain to negate `.string`. + * + * expect('foobar').to.not.have.string('taco'); + * + * `.string` accepts an optional `msg` argument which is a custom error + * message to show when the assertion fails. The message can also be given as + * the second argument to `expect`. + * + * expect('foobar').to.have.string('taco', 'nooo why fail??'); + * expect('foobar', 'nooo why fail??').to.have.string('taco'); + * + * @name string + * @param {String} str + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + Assertion.addMethod('string', function (str, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object') + , flagMsg = flag(this, 'message') + , ssfi = flag(this, 'ssfi'); + new Assertion(obj, flagMsg, ssfi, true).is.a('string'); + + this.assert( + ~obj.indexOf(str) + , 'expected #{this} to contain ' + _.inspect(str) + , 'expected #{this} to not contain ' + _.inspect(str) + ); + }); + + /** + * ### .keys(key1[, key2[, ...]]) + * + * Asserts that the target object, array, map, or set has the given keys. Only + * the target's own inherited properties are included in the search. + * + * When the target is an object or array, keys can be provided as one or more + * string arguments, a single array argument, or a single object argument. In + * the latter case, only the keys in the given object matter; the values are + * ignored. + * + * expect({a: 1, b: 2}).to.have.all.keys('a', 'b'); + * expect(['x', 'y']).to.have.all.keys(0, 1); + * + * expect({a: 1, b: 2}).to.have.all.keys(['a', 'b']); + * expect(['x', 'y']).to.have.all.keys([0, 1]); + * + * expect({a: 1, b: 2}).to.have.all.keys({a: 4, b: 5}); // ignore 4 and 5 + * expect(['x', 'y']).to.have.all.keys({0: 4, 1: 5}); // ignore 4 and 5 + * + * When the target is a map or set, each key must be provided as a separate + * argument. + * + * expect(new Map([['a', 1], ['b', 2]])).to.have.all.keys('a', 'b'); + * expect(new Set(['a', 'b'])).to.have.all.keys('a', 'b'); + * + * Because `.keys` does different things based on the target's type, it's + * important to check the target's type before using `.keys`. See the `.a` doc + * for info on testing a target's type. + * + * expect({a: 1, b: 2}).to.be.an('object').that.has.all.keys('a', 'b'); + * + * By default, strict (`===`) equality is used to compare keys of maps and + * sets. Add `.deep` earlier in the chain to use deep equality instead. See + * the `deep-eql` project page for info on the deep equality algorithm: + * https://github.com/chaijs/deep-eql. + * + * // Target set deeply (but not strictly) has key `{a: 1}` + * expect(new Set([{a: 1}])).to.have.all.deep.keys([{a: 1}]); + * expect(new Set([{a: 1}])).to.not.have.all.keys([{a: 1}]); + * + * By default, the target must have all of the given keys and no more. Add + * `.any` earlier in the chain to only require that the target have at least + * one of the given keys. Also, add `.not` earlier in the chain to negate + * `.keys`. It's often best to add `.any` when negating `.keys`, and to use + * `.all` when asserting `.keys` without negation. + * + * When negating `.keys`, `.any` is preferred because `.not.any.keys` asserts + * exactly what's expected of the output, whereas `.not.all.keys` creates + * uncertain expectations. + * + * // Recommended; asserts that target doesn't have any of the given keys + * expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd'); + * + * // Not recommended; asserts that target doesn't have all of the given + * // keys but may or may not have some of them + * expect({a: 1, b: 2}).to.not.have.all.keys('c', 'd'); + * + * When asserting `.keys` without negation, `.all` is preferred because + * `.all.keys` asserts exactly what's expected of the output, whereas + * `.any.keys` creates uncertain expectations. + * + * // Recommended; asserts that target has all the given keys + * expect({a: 1, b: 2}).to.have.all.keys('a', 'b'); + * + * // Not recommended; asserts that target has at least one of the given + * // keys but may or may not have more of them + * expect({a: 1, b: 2}).to.have.any.keys('a', 'b'); + * + * Note that `.all` is used by default when neither `.all` nor `.any` appear + * earlier in the chain. However, it's often best to add `.all` anyway because + * it improves readability. + * + * // Both assertions are identical + * expect({a: 1, b: 2}).to.have.all.keys('a', 'b'); // Recommended + * expect({a: 1, b: 2}).to.have.keys('a', 'b'); // Not recommended + * + * Add `.include` earlier in the chain to require that the target's keys be a + * superset of the expected keys, rather than identical sets. + * + * // Target object's keys are a superset of ['a', 'b'] but not identical + * expect({a: 1, b: 2, c: 3}).to.include.all.keys('a', 'b'); + * expect({a: 1, b: 2, c: 3}).to.not.have.all.keys('a', 'b'); + * + * However, if `.any` and `.include` are combined, only the `.any` takes + * effect. The `.include` is ignored in this case. + * + * // Both assertions are identical + * expect({a: 1}).to.have.any.keys('a', 'b'); + * expect({a: 1}).to.include.any.keys('a', 'b'); + * + * A custom error message can be given as the second argument to `expect`. + * + * expect({a: 1}, 'nooo why fail??').to.have.key('b'); + * + * The alias `.key` can be used interchangeably with `.keys`. + * + * @name keys + * @alias key + * @param {...String|Array|Object} keys + * @namespace BDD + * @api public + */ + + function assertKeys (keys) { + var obj = flag(this, 'object') + , objType = _.type(obj) + , keysType = _.type(keys) + , ssfi = flag(this, 'ssfi') + , isDeep = flag(this, 'deep') + , str + , deepStr = '' + , actual + , ok = true + , flagMsg = flag(this, 'message'); + + flagMsg = flagMsg ? flagMsg + ': ' : ''; + var mixedArgsMsg = flagMsg + 'when testing keys against an object or an array you must give a single Array|Object|String argument or multiple String arguments'; + + if (objType === 'Map' || objType === 'Set') { + deepStr = isDeep ? 'deeply ' : ''; + actual = []; + + // Map and Set '.keys' aren't supported in IE 11. Therefore, use .forEach. + obj.forEach(function (val, key) { actual.push(key) }); + + if (keysType !== 'Array') { + keys = Array.prototype.slice.call(arguments); + } + } else { + actual = _.getOwnEnumerableProperties(obj); + + switch (keysType) { + case 'Array': + if (arguments.length > 1) { + throw new AssertionError(mixedArgsMsg, undefined, ssfi); + } + break; + case 'Object': + if (arguments.length > 1) { + throw new AssertionError(mixedArgsMsg, undefined, ssfi); + } + keys = Object.keys(keys); + break; + default: + keys = Array.prototype.slice.call(arguments); + } + + // Only stringify non-Symbols because Symbols would become "Symbol()" + keys = keys.map(function (val) { + return typeof val === 'symbol' ? val : String(val); + }); + } + + if (!keys.length) { + throw new AssertionError(flagMsg + 'keys required', undefined, ssfi); + } + + var len = keys.length + , any = flag(this, 'any') + , all = flag(this, 'all') + , expected = keys; + + if (!any && !all) { + all = true; + } + + // Has any + if (any) { + ok = expected.some(function(expectedKey) { + return actual.some(function(actualKey) { + if (isDeep) { + return _.eql(expectedKey, actualKey); + } else { + return expectedKey === actualKey; + } + }); + }); + } + + // Has all + if (all) { + ok = expected.every(function(expectedKey) { + return actual.some(function(actualKey) { + if (isDeep) { + return _.eql(expectedKey, actualKey); + } else { + return expectedKey === actualKey; + } + }); + }); + + if (!flag(this, 'contains')) { + ok = ok && keys.length == actual.length; + } + } + + // Key string + if (len > 1) { + keys = keys.map(function(key) { + return _.inspect(key); + }); + var last = keys.pop(); + if (all) { + str = keys.join(', ') + ', and ' + last; + } + if (any) { + str = keys.join(', ') + ', or ' + last; + } + } else { + str = _.inspect(keys[0]); + } + + // Form + str = (len > 1 ? 'keys ' : 'key ') + str; + + // Have / include + str = (flag(this, 'contains') ? 'contain ' : 'have ') + str; + + // Assertion + this.assert( + ok + , 'expected #{this} to ' + deepStr + str + , 'expected #{this} to not ' + deepStr + str + , expected.slice(0).sort(_.compareByInspect) + , actual.sort(_.compareByInspect) + , true + ); + } + + Assertion.addMethod('keys', assertKeys); + Assertion.addMethod('key', assertKeys); + + /** + * ### .throw([errorLike], [errMsgMatcher], [msg]) + * + * When no arguments are provided, `.throw` invokes the target function and + * asserts that an error is thrown. + * + * var badFn = function () { throw new TypeError('Illegal salmon!'); }; + * + * expect(badFn).to.throw(); + * + * When one argument is provided, and it's an error constructor, `.throw` + * invokes the target function and asserts that an error is thrown that's an + * instance of that error constructor. + * + * var badFn = function () { throw new TypeError('Illegal salmon!'); }; + * + * expect(badFn).to.throw(TypeError); + * + * When one argument is provided, and it's an error instance, `.throw` invokes + * the target function and asserts that an error is thrown that's strictly + * (`===`) equal to that error instance. + * + * var err = new TypeError('Illegal salmon!'); + * var badFn = function () { throw err; }; + * + * expect(badFn).to.throw(err); + * + * When one argument is provided, and it's a string, `.throw` invokes the + * target function and asserts that an error is thrown with a message that + * contains that string. + * + * var badFn = function () { throw new TypeError('Illegal salmon!'); }; + * + * expect(badFn).to.throw('salmon'); + * + * When one argument is provided, and it's a regular expression, `.throw` + * invokes the target function and asserts that an error is thrown with a + * message that matches that regular expression. + * + * var badFn = function () { throw new TypeError('Illegal salmon!'); }; + * + * expect(badFn).to.throw(/salmon/); + * + * When two arguments are provided, and the first is an error instance or + * constructor, and the second is a string or regular expression, `.throw` + * invokes the function and asserts that an error is thrown that fulfills both + * conditions as described above. + * + * var err = new TypeError('Illegal salmon!'); + * var badFn = function () { throw err; }; + * + * expect(badFn).to.throw(TypeError, 'salmon'); + * expect(badFn).to.throw(TypeError, /salmon/); + * expect(badFn).to.throw(err, 'salmon'); + * expect(badFn).to.throw(err, /salmon/); + * + * Add `.not` earlier in the chain to negate `.throw`. + * + * var goodFn = function () {}; + * + * expect(goodFn).to.not.throw(); + * + * However, it's dangerous to negate `.throw` when providing any arguments. + * The problem is that it creates uncertain expectations by asserting that the + * target either doesn't throw an error, or that it throws an error but of a + * different type than the given type, or that it throws an error of the given + * type but with a message that doesn't include the given string. It's often + * best to identify the exact output that's expected, and then write an + * assertion that only accepts that exact output. + * + * When the target isn't expected to throw an error, it's often best to assert + * exactly that. + * + * var goodFn = function () {}; + * + * expect(goodFn).to.not.throw(); // Recommended + * expect(goodFn).to.not.throw(ReferenceError, 'x'); // Not recommended + * + * When the target is expected to throw an error, it's often best to assert + * that the error is of its expected type, and has a message that includes an + * expected string, rather than asserting that it doesn't have one of many + * unexpected types, and doesn't have a message that includes some string. + * + * var badFn = function () { throw new TypeError('Illegal salmon!'); }; + * + * expect(badFn).to.throw(TypeError, 'salmon'); // Recommended + * expect(badFn).to.not.throw(ReferenceError, 'x'); // Not recommended + * + * `.throw` changes the target of any assertions that follow in the chain to + * be the error object that's thrown. + * + * var err = new TypeError('Illegal salmon!'); + * err.code = 42; + * var badFn = function () { throw err; }; + * + * expect(badFn).to.throw(TypeError).with.property('code', 42); + * + * `.throw` accepts an optional `msg` argument which is a custom error message + * to show when the assertion fails. The message can also be given as the + * second argument to `expect`. When not providing two arguments, always use + * the second form. + * + * var goodFn = function () {}; + * + * expect(goodFn).to.throw(TypeError, 'x', 'nooo why fail??'); + * expect(goodFn, 'nooo why fail??').to.throw(); + * + * Due to limitations in ES5, `.throw` may not always work as expected when + * using a transpiler such as Babel or TypeScript. In particular, it may + * produce unexpected results when subclassing the built-in `Error` object and + * then passing the subclassed constructor to `.throw`. See your transpiler's + * docs for details: + * + * - ([Babel](https://babeljs.io/docs/usage/caveats/#classes)) + * - ([TypeScript](https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work)) + * + * Beware of some common mistakes when using the `throw` assertion. One common + * mistake is to accidentally invoke the function yourself instead of letting + * the `throw` assertion invoke the function for you. For example, when + * testing if a function named `fn` throws, provide `fn` instead of `fn()` as + * the target for the assertion. + * + * expect(fn).to.throw(); // Good! Tests `fn` as desired + * expect(fn()).to.throw(); // Bad! Tests result of `fn()`, not `fn` + * + * If you need to assert that your function `fn` throws when passed certain + * arguments, then wrap a call to `fn` inside of another function. + * + * expect(function () { fn(42); }).to.throw(); // Function expression + * expect(() => fn(42)).to.throw(); // ES6 arrow function + * + * Another common mistake is to provide an object method (or any stand-alone + * function that relies on `this`) as the target of the assertion. Doing so is + * problematic because the `this` context will be lost when the function is + * invoked by `.throw`; there's no way for it to know what `this` is supposed + * to be. There are two ways around this problem. One solution is to wrap the + * method or function call inside of another function. Another solution is to + * use `bind`. + * + * expect(function () { cat.meow(); }).to.throw(); // Function expression + * expect(() => cat.meow()).to.throw(); // ES6 arrow function + * expect(cat.meow.bind(cat)).to.throw(); // Bind + * + * Finally, it's worth mentioning that it's a best practice in JavaScript to + * only throw `Error` and derivatives of `Error` such as `ReferenceError`, + * `TypeError`, and user-defined objects that extend `Error`. No other type of + * value will generate a stack trace when initialized. With that said, the + * `throw` assertion does technically support any type of value being thrown, + * not just `Error` and its derivatives. + * + * The aliases `.throws` and `.Throw` can be used interchangeably with + * `.throw`. + * + * @name throw + * @alias throws + * @alias Throw + * @param {Error|ErrorConstructor} errorLike + * @param {String|RegExp} errMsgMatcher error message + * @param {String} msg _optional_ + * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types + * @returns error for chaining (null if no error) + * @namespace BDD + * @api public + */ + + function assertThrows (errorLike, errMsgMatcher, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object') + , ssfi = flag(this, 'ssfi') + , flagMsg = flag(this, 'message') + , negate = flag(this, 'negate') || false; + new Assertion(obj, flagMsg, ssfi, true).is.a('function'); + + if (errorLike instanceof RegExp || typeof errorLike === 'string') { + errMsgMatcher = errorLike; + errorLike = null; + } + + var caughtErr; + try { + obj(); + } catch (err) { + caughtErr = err; + } + + // If we have the negate flag enabled and at least one valid argument it means we do expect an error + // but we want it to match a given set of criteria + var everyArgIsUndefined = errorLike === undefined && errMsgMatcher === undefined; + + // If we've got the negate flag enabled and both args, we should only fail if both aren't compatible + // See Issue #551 and PR #683@GitHub + var everyArgIsDefined = Boolean(errorLike && errMsgMatcher); + var errorLikeFail = false; + var errMsgMatcherFail = false; + + // Checking if error was thrown + if (everyArgIsUndefined || !everyArgIsUndefined && !negate) { + // We need this to display results correctly according to their types + var errorLikeString = 'an error'; + if (errorLike instanceof Error) { + errorLikeString = '#{exp}'; + } else if (errorLike) { + errorLikeString = _.checkError.getConstructorName(errorLike); + } + + this.assert( + caughtErr + , 'expected #{this} to throw ' + errorLikeString + , 'expected #{this} to not throw an error but #{act} was thrown' + , errorLike && errorLike.toString() + , (caughtErr instanceof Error ? + caughtErr.toString() : (typeof caughtErr === 'string' ? caughtErr : caughtErr && + _.checkError.getConstructorName(caughtErr))) + ); + } + + if (errorLike && caughtErr) { + // We should compare instances only if `errorLike` is an instance of `Error` + if (errorLike instanceof Error) { + var isCompatibleInstance = _.checkError.compatibleInstance(caughtErr, errorLike); + + if (isCompatibleInstance === negate) { + // These checks were created to ensure we won't fail too soon when we've got both args and a negate + // See Issue #551 and PR #683@GitHub + if (everyArgIsDefined && negate) { + errorLikeFail = true; + } else { + this.assert( + negate + , 'expected #{this} to throw #{exp} but #{act} was thrown' + , 'expected #{this} to not throw #{exp}' + (caughtErr && !negate ? ' but #{act} was thrown' : '') + , errorLike.toString() + , caughtErr.toString() + ); + } + } + } + + var isCompatibleConstructor = _.checkError.compatibleConstructor(caughtErr, errorLike); + if (isCompatibleConstructor === negate) { + if (everyArgIsDefined && negate) { + errorLikeFail = true; + } else { + this.assert( + negate + , 'expected #{this} to throw #{exp} but #{act} was thrown' + , 'expected #{this} to not throw #{exp}' + (caughtErr ? ' but #{act} was thrown' : '') + , (errorLike instanceof Error ? errorLike.toString() : errorLike && _.checkError.getConstructorName(errorLike)) + , (caughtErr instanceof Error ? caughtErr.toString() : caughtErr && _.checkError.getConstructorName(caughtErr)) + ); + } + } + } + + if (caughtErr && errMsgMatcher !== undefined && errMsgMatcher !== null) { + // Here we check compatible messages + var placeholder = 'including'; + if (errMsgMatcher instanceof RegExp) { + placeholder = 'matching' + } + + var isCompatibleMessage = _.checkError.compatibleMessage(caughtErr, errMsgMatcher); + if (isCompatibleMessage === negate) { + if (everyArgIsDefined && negate) { + errMsgMatcherFail = true; + } else { + this.assert( + negate + , 'expected #{this} to throw error ' + placeholder + ' #{exp} but got #{act}' + , 'expected #{this} to throw error not ' + placeholder + ' #{exp}' + , errMsgMatcher + , _.checkError.getMessage(caughtErr) + ); + } + } + } + + // If both assertions failed and both should've matched we throw an error + if (errorLikeFail && errMsgMatcherFail) { + this.assert( + negate + , 'expected #{this} to throw #{exp} but #{act} was thrown' + , 'expected #{this} to not throw #{exp}' + (caughtErr ? ' but #{act} was thrown' : '') + , (errorLike instanceof Error ? errorLike.toString() : errorLike && _.checkError.getConstructorName(errorLike)) + , (caughtErr instanceof Error ? caughtErr.toString() : caughtErr && _.checkError.getConstructorName(caughtErr)) + ); + } + + flag(this, 'object', caughtErr); + }; + + Assertion.addMethod('throw', assertThrows); + Assertion.addMethod('throws', assertThrows); + Assertion.addMethod('Throw', assertThrows); + + /** + * ### .respondTo(method[, msg]) + * + * When the target is a non-function object, `.respondTo` asserts that the + * target has a method with the given name `method`. The method can be own or + * inherited, and it can be enumerable or non-enumerable. + * + * function Cat () {} + * Cat.prototype.meow = function () {}; + * + * expect(new Cat()).to.respondTo('meow'); + * + * When the target is a function, `.respondTo` asserts that the target's + * `prototype` property has a method with the given name `method`. Again, the + * method can be own or inherited, and it can be enumerable or non-enumerable. + * + * function Cat () {} + * Cat.prototype.meow = function () {}; + * + * expect(Cat).to.respondTo('meow'); + * + * Add `.itself` earlier in the chain to force `.respondTo` to treat the + * target as a non-function object, even if it's a function. Thus, it asserts + * that the target has a method with the given name `method`, rather than + * asserting that the target's `prototype` property has a method with the + * given name `method`. + * + * function Cat () {} + * Cat.prototype.meow = function () {}; + * Cat.hiss = function () {}; + * + * expect(Cat).itself.to.respondTo('hiss').but.not.respondTo('meow'); + * + * When not adding `.itself`, it's important to check the target's type before + * using `.respondTo`. See the `.a` doc for info on checking a target's type. + * + * function Cat () {} + * Cat.prototype.meow = function () {}; + * + * expect(new Cat()).to.be.an('object').that.respondsTo('meow'); + * + * Add `.not` earlier in the chain to negate `.respondTo`. + * + * function Dog () {} + * Dog.prototype.bark = function () {}; + * + * expect(new Dog()).to.not.respondTo('meow'); + * + * `.respondTo` accepts an optional `msg` argument which is a custom error + * message to show when the assertion fails. The message can also be given as + * the second argument to `expect`. + * + * expect({}).to.respondTo('meow', 'nooo why fail??'); + * expect({}, 'nooo why fail??').to.respondTo('meow'); + * + * The alias `.respondsTo` can be used interchangeably with `.respondTo`. + * + * @name respondTo + * @alias respondsTo + * @param {String} method + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function respondTo (method, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object') + , itself = flag(this, 'itself') + , context = ('function' === typeof obj && !itself) + ? obj.prototype[method] + : obj[method]; + + this.assert( + 'function' === typeof context + , 'expected #{this} to respond to ' + _.inspect(method) + , 'expected #{this} to not respond to ' + _.inspect(method) + ); + } + + Assertion.addMethod('respondTo', respondTo); + Assertion.addMethod('respondsTo', respondTo); + + /** + * ### .itself + * + * Forces all `.respondTo` assertions that follow in the chain to behave as if + * the target is a non-function object, even if it's a function. Thus, it + * causes `.respondTo` to assert that the target has a method with the given + * name, rather than asserting that the target's `prototype` property has a + * method with the given name. + * + * function Cat () {} + * Cat.prototype.meow = function () {}; + * Cat.hiss = function () {}; + * + * expect(Cat).itself.to.respondTo('hiss').but.not.respondTo('meow'); + * + * @name itself + * @namespace BDD + * @api public + */ + + Assertion.addProperty('itself', function () { + flag(this, 'itself', true); + }); + + /** + * ### .satisfy(matcher[, msg]) + * + * Invokes the given `matcher` function with the target being passed as the + * first argument, and asserts that the value returned is truthy. + * + * expect(1).to.satisfy(function(num) { + * return num > 0; + * }); + * + * Add `.not` earlier in the chain to negate `.satisfy`. + * + * expect(1).to.not.satisfy(function(num) { + * return num > 2; + * }); + * + * `.satisfy` accepts an optional `msg` argument which is a custom error + * message to show when the assertion fails. The message can also be given as + * the second argument to `expect`. + * + * expect(1).to.satisfy(function(num) { + * return num > 2; + * }, 'nooo why fail??'); + * + * expect(1, 'nooo why fail??').to.satisfy(function(num) { + * return num > 2; + * }); + * + * The alias `.satisfies` can be used interchangeably with `.satisfy`. + * + * @name satisfy + * @alias satisfies + * @param {Function} matcher + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function satisfy (matcher, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object'); + var result = matcher(obj); + this.assert( + result + , 'expected #{this} to satisfy ' + _.objDisplay(matcher) + , 'expected #{this} to not satisfy' + _.objDisplay(matcher) + , flag(this, 'negate') ? false : true + , result + ); + } + + Assertion.addMethod('satisfy', satisfy); + Assertion.addMethod('satisfies', satisfy); + + /** + * ### .closeTo(expected, delta[, msg]) + * + * Asserts that the target is a number that's within a given +/- `delta` range + * of the given number `expected`. However, it's often best to assert that the + * target is equal to its expected value. + * + * // Recommended + * expect(1.5).to.equal(1.5); + * + * // Not recommended + * expect(1.5).to.be.closeTo(1, 0.5); + * expect(1.5).to.be.closeTo(2, 0.5); + * expect(1.5).to.be.closeTo(1, 1); + * + * Add `.not` earlier in the chain to negate `.closeTo`. + * + * expect(1.5).to.equal(1.5); // Recommended + * expect(1.5).to.not.be.closeTo(3, 1); // Not recommended + * + * `.closeTo` accepts an optional `msg` argument which is a custom error + * message to show when the assertion fails. The message can also be given as + * the second argument to `expect`. + * + * expect(1.5).to.be.closeTo(3, 1, 'nooo why fail??'); + * expect(1.5, 'nooo why fail??').to.be.closeTo(3, 1); + * + * The alias `.approximately` can be used interchangeably with `.closeTo`. + * + * @name closeTo + * @alias approximately + * @param {Number} expected + * @param {Number} delta + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function closeTo(expected, delta, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object') + , flagMsg = flag(this, 'message') + , ssfi = flag(this, 'ssfi'); + + new Assertion(obj, flagMsg, ssfi, true).is.a('number'); + if (typeof expected !== 'number' || typeof delta !== 'number') { + flagMsg = flagMsg ? flagMsg + ': ' : ''; + throw new AssertionError( + flagMsg + 'the arguments to closeTo or approximately must be numbers', + undefined, + ssfi + ); + } + + this.assert( + Math.abs(obj - expected) <= delta + , 'expected #{this} to be close to ' + expected + ' +/- ' + delta + , 'expected #{this} not to be close to ' + expected + ' +/- ' + delta + ); + } + + Assertion.addMethod('closeTo', closeTo); + Assertion.addMethod('approximately', closeTo); + + // Note: Duplicates are ignored if testing for inclusion instead of sameness. + function isSubsetOf(subset, superset, cmp, contains, ordered) { + if (!contains) { + if (subset.length !== superset.length) return false; + superset = superset.slice(); + } + + return subset.every(function(elem, idx) { + if (ordered) return cmp ? cmp(elem, superset[idx]) : elem === superset[idx]; + + if (!cmp) { + var matchIdx = superset.indexOf(elem); + if (matchIdx === -1) return false; + + // Remove match from superset so not counted twice if duplicate in subset. + if (!contains) superset.splice(matchIdx, 1); + return true; + } + + return superset.some(function(elem2, matchIdx) { + if (!cmp(elem, elem2)) return false; + + // Remove match from superset so not counted twice if duplicate in subset. + if (!contains) superset.splice(matchIdx, 1); + return true; + }); + }); + } + + /** + * ### .members(set[, msg]) + * + * Asserts that the target array has the same members as the given array + * `set`. + * + * expect([1, 2, 3]).to.have.members([2, 1, 3]); + * expect([1, 2, 2]).to.have.members([2, 1, 2]); + * + * By default, members are compared using strict (`===`) equality. Add `.deep` + * earlier in the chain to use deep equality instead. See the `deep-eql` + * project page for info on the deep equality algorithm: + * https://github.com/chaijs/deep-eql. + * + * // Target array deeply (but not strictly) has member `{a: 1}` + * expect([{a: 1}]).to.have.deep.members([{a: 1}]); + * expect([{a: 1}]).to.not.have.members([{a: 1}]); + * + * By default, order doesn't matter. Add `.ordered` earlier in the chain to + * require that members appear in the same order. + * + * expect([1, 2, 3]).to.have.ordered.members([1, 2, 3]); + * expect([1, 2, 3]).to.have.members([2, 1, 3]) + * .but.not.ordered.members([2, 1, 3]); + * + * By default, both arrays must be the same size. Add `.include` earlier in + * the chain to require that the target's members be a superset of the + * expected members. Note that duplicates are ignored in the subset when + * `.include` is added. + * + * // Target array is a superset of [1, 2] but not identical + * expect([1, 2, 3]).to.include.members([1, 2]); + * expect([1, 2, 3]).to.not.have.members([1, 2]); + * + * // Duplicates in the subset are ignored + * expect([1, 2, 3]).to.include.members([1, 2, 2, 2]); + * + * `.deep`, `.ordered`, and `.include` can all be combined. However, if + * `.include` and `.ordered` are combined, the ordering begins at the start of + * both arrays. + * + * expect([{a: 1}, {b: 2}, {c: 3}]) + * .to.include.deep.ordered.members([{a: 1}, {b: 2}]) + * .but.not.include.deep.ordered.members([{b: 2}, {c: 3}]); + * + * Add `.not` earlier in the chain to negate `.members`. However, it's + * dangerous to do so. The problem is that it creates uncertain expectations + * by asserting that the target array doesn't have all of the same members as + * the given array `set` but may or may not have some of them. It's often best + * to identify the exact output that's expected, and then write an assertion + * that only accepts that exact output. + * + * expect([1, 2]).to.not.include(3).and.not.include(4); // Recommended + * expect([1, 2]).to.not.have.members([3, 4]); // Not recommended + * + * `.members` accepts an optional `msg` argument which is a custom error + * message to show when the assertion fails. The message can also be given as + * the second argument to `expect`. + * + * expect([1, 2]).to.have.members([1, 2, 3], 'nooo why fail??'); + * expect([1, 2], 'nooo why fail??').to.have.members([1, 2, 3]); + * + * @name members + * @param {Array} set + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + Assertion.addMethod('members', function (subset, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object') + , flagMsg = flag(this, 'message') + , ssfi = flag(this, 'ssfi'); + + new Assertion(obj, flagMsg, ssfi, true).to.be.an('array'); + new Assertion(subset, flagMsg, ssfi, true).to.be.an('array'); + + var contains = flag(this, 'contains'); + var ordered = flag(this, 'ordered'); + + var subject, failMsg, failNegateMsg; + + if (contains) { + subject = ordered ? 'an ordered superset' : 'a superset'; + failMsg = 'expected #{this} to be ' + subject + ' of #{exp}'; + failNegateMsg = 'expected #{this} to not be ' + subject + ' of #{exp}'; + } else { + subject = ordered ? 'ordered members' : 'members'; + failMsg = 'expected #{this} to have the same ' + subject + ' as #{exp}'; + failNegateMsg = 'expected #{this} to not have the same ' + subject + ' as #{exp}'; + } + + var cmp = flag(this, 'deep') ? _.eql : undefined; + + this.assert( + isSubsetOf(subset, obj, cmp, contains, ordered) + , failMsg + , failNegateMsg + , subset + , obj + , true + ); + }); + + /** + * ### .oneOf(list[, msg]) + * + * Asserts that the target is a member of the given array `list`. However, + * it's often best to assert that the target is equal to its expected value. + * + * expect(1).to.equal(1); // Recommended + * expect(1).to.be.oneOf([1, 2, 3]); // Not recommended + * + * Comparisons are performed using strict (`===`) equality. + * + * Add `.not` earlier in the chain to negate `.oneOf`. + * + * expect(1).to.equal(1); // Recommended + * expect(1).to.not.be.oneOf([2, 3, 4]); // Not recommended + * + * `.oneOf` accepts an optional `msg` argument which is a custom error message + * to show when the assertion fails. The message can also be given as the + * second argument to `expect`. + * + * expect(1).to.be.oneOf([2, 3, 4], 'nooo why fail??'); + * expect(1, 'nooo why fail??').to.be.oneOf([2, 3, 4]); + * + * @name oneOf + * @param {Array<*>} list + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function oneOf (list, msg) { + if (msg) flag(this, 'message', msg); + var expected = flag(this, 'object') + , flagMsg = flag(this, 'message') + , ssfi = flag(this, 'ssfi'); + new Assertion(list, flagMsg, ssfi, true).to.be.an('array'); + + this.assert( + list.indexOf(expected) > -1 + , 'expected #{this} to be one of #{exp}' + , 'expected #{this} to not be one of #{exp}' + , list + , expected + ); + } + + Assertion.addMethod('oneOf', oneOf); + + /** + * ### .change(subject[, prop[, msg]]) + * + * When one argument is provided, `.change` asserts that the given function + * `subject` returns a different value when it's invoked before the target + * function compared to when it's invoked afterward. However, it's often best + * to assert that `subject` is equal to its expected value. + * + * var dots = '' + * , addDot = function () { dots += '.'; } + * , getDots = function () { return dots; }; + * + * // Recommended + * expect(getDots()).to.equal(''); + * addDot(); + * expect(getDots()).to.equal('.'); + * + * // Not recommended + * expect(addDot).to.change(getDots); + * + * When two arguments are provided, `.change` asserts that the value of the + * given object `subject`'s `prop` property is different before invoking the + * target function compared to afterward. + * + * var myObj = {dots: ''} + * , addDot = function () { myObj.dots += '.'; }; + * + * // Recommended + * expect(myObj).to.have.property('dots', ''); + * addDot(); + * expect(myObj).to.have.property('dots', '.'); + * + * // Not recommended + * expect(addDot).to.change(myObj, 'dots'); + * + * Strict (`===`) equality is used to compare before and after values. + * + * Add `.not` earlier in the chain to negate `.change`. + * + * var dots = '' + * , noop = function () {} + * , getDots = function () { return dots; }; + * + * expect(noop).to.not.change(getDots); + * + * var myObj = {dots: ''} + * , noop = function () {}; + * + * expect(noop).to.not.change(myObj, 'dots'); + * + * `.change` accepts an optional `msg` argument which is a custom error + * message to show when the assertion fails. The message can also be given as + * the second argument to `expect`. When not providing two arguments, always + * use the second form. + * + * var myObj = {dots: ''} + * , addDot = function () { myObj.dots += '.'; }; + * + * expect(addDot).to.not.change(myObj, 'dots', 'nooo why fail??'); + * + * var dots = '' + * , addDot = function () { dots += '.'; } + * , getDots = function () { return dots; }; + * + * expect(addDot, 'nooo why fail??').to.not.change(getDots); + * + * `.change` also causes all `.by` assertions that follow in the chain to + * assert how much a numeric subject was increased or decreased by. However, + * it's dangerous to use `.change.by`. The problem is that it creates + * uncertain expectations by asserting that the subject either increases by + * the given delta, or that it decreases by the given delta. It's often best + * to identify the exact output that's expected, and then write an assertion + * that only accepts that exact output. + * + * var myObj = {val: 1} + * , addTwo = function () { myObj.val += 2; } + * , subtractTwo = function () { myObj.val -= 2; }; + * + * expect(addTwo).to.increase(myObj, 'val').by(2); // Recommended + * expect(addTwo).to.change(myObj, 'val').by(2); // Not recommended + * + * expect(subtractTwo).to.decrease(myObj, 'val').by(2); // Recommended + * expect(subtractTwo).to.change(myObj, 'val').by(2); // Not recommended + * + * The alias `.changes` can be used interchangeably with `.change`. + * + * @name change + * @alias changes + * @param {String} subject + * @param {String} prop name _optional_ + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function assertChanges (subject, prop, msg) { + if (msg) flag(this, 'message', msg); + var fn = flag(this, 'object') + , flagMsg = flag(this, 'message') + , ssfi = flag(this, 'ssfi'); + new Assertion(fn, flagMsg, ssfi, true).is.a('function'); + + var initial; + if (!prop) { + new Assertion(subject, flagMsg, ssfi, true).is.a('function'); + initial = subject(); + } else { + new Assertion(subject, flagMsg, ssfi, true).to.have.property(prop); + initial = subject[prop]; + } + + fn(); + + var final = prop === undefined || prop === null ? subject() : subject[prop]; + var msgObj = prop === undefined || prop === null ? initial : '.' + prop; + + // This gets flagged because of the .by(delta) assertion + flag(this, 'deltaMsgObj', msgObj); + flag(this, 'initialDeltaValue', initial); + flag(this, 'finalDeltaValue', final); + flag(this, 'deltaBehavior', 'change'); + flag(this, 'realDelta', final !== initial); + + this.assert( + initial !== final + , 'expected ' + msgObj + ' to change' + , 'expected ' + msgObj + ' to not change' + ); + } + + Assertion.addMethod('change', assertChanges); + Assertion.addMethod('changes', assertChanges); + + /** + * ### .increase(subject[, prop[, msg]]) + * + * When one argument is provided, `.increase` asserts that the given function + * `subject` returns a greater number when it's invoked after invoking the + * target function compared to when it's invoked beforehand. `.increase` also + * causes all `.by` assertions that follow in the chain to assert how much + * greater of a number is returned. It's often best to assert that the return + * value increased by the expected amount, rather than asserting it increased + * by any amount. + * + * var val = 1 + * , addTwo = function () { val += 2; } + * , getVal = function () { return val; }; + * + * expect(addTwo).to.increase(getVal).by(2); // Recommended + * expect(addTwo).to.increase(getVal); // Not recommended + * + * When two arguments are provided, `.increase` asserts that the value of the + * given object `subject`'s `prop` property is greater after invoking the + * target function compared to beforehand. + * + * var myObj = {val: 1} + * , addTwo = function () { myObj.val += 2; }; + * + * expect(addTwo).to.increase(myObj, 'val').by(2); // Recommended + * expect(addTwo).to.increase(myObj, 'val'); // Not recommended + * + * Add `.not` earlier in the chain to negate `.increase`. However, it's + * dangerous to do so. The problem is that it creates uncertain expectations + * by asserting that the subject either decreases, or that it stays the same. + * It's often best to identify the exact output that's expected, and then + * write an assertion that only accepts that exact output. + * + * When the subject is expected to decrease, it's often best to assert that it + * decreased by the expected amount. + * + * var myObj = {val: 1} + * , subtractTwo = function () { myObj.val -= 2; }; + * + * expect(subtractTwo).to.decrease(myObj, 'val').by(2); // Recommended + * expect(subtractTwo).to.not.increase(myObj, 'val'); // Not recommended + * + * When the subject is expected to stay the same, it's often best to assert + * exactly that. + * + * var myObj = {val: 1} + * , noop = function () {}; + * + * expect(noop).to.not.change(myObj, 'val'); // Recommended + * expect(noop).to.not.increase(myObj, 'val'); // Not recommended + * + * `.increase` accepts an optional `msg` argument which is a custom error + * message to show when the assertion fails. The message can also be given as + * the second argument to `expect`. When not providing two arguments, always + * use the second form. + * + * var myObj = {val: 1} + * , noop = function () {}; + * + * expect(noop).to.increase(myObj, 'val', 'nooo why fail??'); + * + * var val = 1 + * , noop = function () {} + * , getVal = function () { return val; }; + * + * expect(noop, 'nooo why fail??').to.increase(getVal); + * + * The alias `.increases` can be used interchangeably with `.increase`. + * + * @name increase + * @alias increases + * @param {String|Function} subject + * @param {String} prop name _optional_ + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function assertIncreases (subject, prop, msg) { + if (msg) flag(this, 'message', msg); + var fn = flag(this, 'object') + , flagMsg = flag(this, 'message') + , ssfi = flag(this, 'ssfi'); + new Assertion(fn, flagMsg, ssfi, true).is.a('function'); + + var initial; + if (!prop) { + new Assertion(subject, flagMsg, ssfi, true).is.a('function'); + initial = subject(); + } else { + new Assertion(subject, flagMsg, ssfi, true).to.have.property(prop); + initial = subject[prop]; + } + + // Make sure that the target is a number + new Assertion(initial, flagMsg, ssfi, true).is.a('number'); + + fn(); + + var final = prop === undefined || prop === null ? subject() : subject[prop]; + var msgObj = prop === undefined || prop === null ? initial : '.' + prop; + + flag(this, 'deltaMsgObj', msgObj); + flag(this, 'initialDeltaValue', initial); + flag(this, 'finalDeltaValue', final); + flag(this, 'deltaBehavior', 'increase'); + flag(this, 'realDelta', final - initial); + + this.assert( + final - initial > 0 + , 'expected ' + msgObj + ' to increase' + , 'expected ' + msgObj + ' to not increase' + ); + } + + Assertion.addMethod('increase', assertIncreases); + Assertion.addMethod('increases', assertIncreases); + + /** + * ### .decrease(subject[, prop[, msg]]) + * + * When one argument is provided, `.decrease` asserts that the given function + * `subject` returns a lesser number when it's invoked after invoking the + * target function compared to when it's invoked beforehand. `.decrease` also + * causes all `.by` assertions that follow in the chain to assert how much + * lesser of a number is returned. It's often best to assert that the return + * value decreased by the expected amount, rather than asserting it decreased + * by any amount. + * + * var val = 1 + * , subtractTwo = function () { val -= 2; } + * , getVal = function () { return val; }; + * + * expect(subtractTwo).to.decrease(getVal).by(2); // Recommended + * expect(subtractTwo).to.decrease(getVal); // Not recommended + * + * When two arguments are provided, `.decrease` asserts that the value of the + * given object `subject`'s `prop` property is lesser after invoking the + * target function compared to beforehand. + * + * var myObj = {val: 1} + * , subtractTwo = function () { myObj.val -= 2; }; + * + * expect(subtractTwo).to.decrease(myObj, 'val').by(2); // Recommended + * expect(subtractTwo).to.decrease(myObj, 'val'); // Not recommended + * + * Add `.not` earlier in the chain to negate `.decrease`. However, it's + * dangerous to do so. The problem is that it creates uncertain expectations + * by asserting that the subject either increases, or that it stays the same. + * It's often best to identify the exact output that's expected, and then + * write an assertion that only accepts that exact output. + * + * When the subject is expected to increase, it's often best to assert that it + * increased by the expected amount. + * + * var myObj = {val: 1} + * , addTwo = function () { myObj.val += 2; }; + * + * expect(addTwo).to.increase(myObj, 'val').by(2); // Recommended + * expect(addTwo).to.not.decrease(myObj, 'val'); // Not recommended + * + * When the subject is expected to stay the same, it's often best to assert + * exactly that. + * + * var myObj = {val: 1} + * , noop = function () {}; + * + * expect(noop).to.not.change(myObj, 'val'); // Recommended + * expect(noop).to.not.decrease(myObj, 'val'); // Not recommended + * + * `.decrease` accepts an optional `msg` argument which is a custom error + * message to show when the assertion fails. The message can also be given as + * the second argument to `expect`. When not providing two arguments, always + * use the second form. + * + * var myObj = {val: 1} + * , noop = function () {}; + * + * expect(noop).to.decrease(myObj, 'val', 'nooo why fail??'); + * + * var val = 1 + * , noop = function () {} + * , getVal = function () { return val; }; + * + * expect(noop, 'nooo why fail??').to.decrease(getVal); + * + * The alias `.decreases` can be used interchangeably with `.decrease`. + * + * @name decrease + * @alias decreases + * @param {String|Function} subject + * @param {String} prop name _optional_ + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function assertDecreases (subject, prop, msg) { + if (msg) flag(this, 'message', msg); + var fn = flag(this, 'object') + , flagMsg = flag(this, 'message') + , ssfi = flag(this, 'ssfi'); + new Assertion(fn, flagMsg, ssfi, true).is.a('function'); + + var initial; + if (!prop) { + new Assertion(subject, flagMsg, ssfi, true).is.a('function'); + initial = subject(); + } else { + new Assertion(subject, flagMsg, ssfi, true).to.have.property(prop); + initial = subject[prop]; + } + + // Make sure that the target is a number + new Assertion(initial, flagMsg, ssfi, true).is.a('number'); + + fn(); + + var final = prop === undefined || prop === null ? subject() : subject[prop]; + var msgObj = prop === undefined || prop === null ? initial : '.' + prop; + + flag(this, 'deltaMsgObj', msgObj); + flag(this, 'initialDeltaValue', initial); + flag(this, 'finalDeltaValue', final); + flag(this, 'deltaBehavior', 'decrease'); + flag(this, 'realDelta', initial - final); + + this.assert( + final - initial < 0 + , 'expected ' + msgObj + ' to decrease' + , 'expected ' + msgObj + ' to not decrease' + ); + } + + Assertion.addMethod('decrease', assertDecreases); + Assertion.addMethod('decreases', assertDecreases); + + /** + * ### .by(delta[, msg]) + * + * When following an `.increase` assertion in the chain, `.by` asserts that + * the subject of the `.increase` assertion increased by the given `delta`. + * + * var myObj = {val: 1} + * , addTwo = function () { myObj.val += 2; }; + * + * expect(addTwo).to.increase(myObj, 'val').by(2); + * + * When following a `.decrease` assertion in the chain, `.by` asserts that the + * subject of the `.decrease` assertion decreased by the given `delta`. + * + * var myObj = {val: 1} + * , subtractTwo = function () { myObj.val -= 2; }; + * + * expect(subtractTwo).to.decrease(myObj, 'val').by(2); + * + * When following a `.change` assertion in the chain, `.by` asserts that the + * subject of the `.change` assertion either increased or decreased by the + * given `delta`. However, it's dangerous to use `.change.by`. The problem is + * that it creates uncertain expectations. It's often best to identify the + * exact output that's expected, and then write an assertion that only accepts + * that exact output. + * + * var myObj = {val: 1} + * , addTwo = function () { myObj.val += 2; } + * , subtractTwo = function () { myObj.val -= 2; }; + * + * expect(addTwo).to.increase(myObj, 'val').by(2); // Recommended + * expect(addTwo).to.change(myObj, 'val').by(2); // Not recommended + * + * expect(subtractTwo).to.decrease(myObj, 'val').by(2); // Recommended + * expect(subtractTwo).to.change(myObj, 'val').by(2); // Not recommended + * + * Add `.not` earlier in the chain to negate `.by`. However, it's often best + * to assert that the subject changed by its expected delta, rather than + * asserting that it didn't change by one of countless unexpected deltas. + * + * var myObj = {val: 1} + * , addTwo = function () { myObj.val += 2; }; + * + * // Recommended + * expect(addTwo).to.increase(myObj, 'val').by(2); + * + * // Not recommended + * expect(addTwo).to.increase(myObj, 'val').but.not.by(3); + * + * `.by` accepts an optional `msg` argument which is a custom error message to + * show when the assertion fails. The message can also be given as the second + * argument to `expect`. + * + * var myObj = {val: 1} + * , addTwo = function () { myObj.val += 2; }; + * + * expect(addTwo).to.increase(myObj, 'val').by(3, 'nooo why fail??'); + * expect(addTwo, 'nooo why fail??').to.increase(myObj, 'val').by(3); + * + * @name by + * @param {Number} delta + * @param {String} msg _optional_ + * @namespace BDD + * @api public + */ + + function assertDelta(delta, msg) { + if (msg) flag(this, 'message', msg); + + var msgObj = flag(this, 'deltaMsgObj'); + var initial = flag(this, 'initialDeltaValue'); + var final = flag(this, 'finalDeltaValue'); + var behavior = flag(this, 'deltaBehavior'); + var realDelta = flag(this, 'realDelta'); + + var expression; + if (behavior === 'change') { + expression = Math.abs(final - initial) === Math.abs(delta); + } else { + expression = realDelta === Math.abs(delta); + } + + this.assert( + expression + , 'expected ' + msgObj + ' to ' + behavior + ' by ' + delta + , 'expected ' + msgObj + ' to not ' + behavior + ' by ' + delta + ); + } + + Assertion.addMethod('by', assertDelta); + + /** + * ### .extensible + * + * Asserts that the target is extensible, which means that new properties can + * be added to it. Primitives are never extensible. + * + * expect({a: 1}).to.be.extensible; + * + * Add `.not` earlier in the chain to negate `.extensible`. + * + * var nonExtensibleObject = Object.preventExtensions({}) + * , sealedObject = Object.seal({}) + * , frozenObject = Object.freeze({}); + * + * expect(nonExtensibleObject).to.not.be.extensible; + * expect(sealedObject).to.not.be.extensible; + * expect(frozenObject).to.not.be.extensible; + * expect(1).to.not.be.extensible; + * + * A custom error message can be given as the second argument to `expect`. + * + * expect(1, 'nooo why fail??').to.be.extensible; + * + * @name extensible + * @namespace BDD + * @api public + */ + + Assertion.addProperty('extensible', function() { + var obj = flag(this, 'object'); + + // In ES5, if the argument to this method is a primitive, then it will cause a TypeError. + // In ES6, a non-object argument will be treated as if it was a non-extensible ordinary object, simply return false. + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isExtensible + // The following provides ES6 behavior for ES5 environments. + + var isExtensible = obj === Object(obj) && Object.isExtensible(obj); + + this.assert( + isExtensible + , 'expected #{this} to be extensible' + , 'expected #{this} to not be extensible' + ); + }); + + /** + * ### .sealed + * + * Asserts that the target is sealed, which means that new properties can't be + * added to it, and its existing properties can't be reconfigured or deleted. + * However, it's possible that its existing properties can still be reassigned + * to different values. Primitives are always sealed. + * + * var sealedObject = Object.seal({}); + * var frozenObject = Object.freeze({}); + * + * expect(sealedObject).to.be.sealed; + * expect(frozenObject).to.be.sealed; + * expect(1).to.be.sealed; + * + * Add `.not` earlier in the chain to negate `.sealed`. + * + * expect({a: 1}).to.not.be.sealed; + * + * A custom error message can be given as the second argument to `expect`. + * + * expect({a: 1}, 'nooo why fail??').to.be.sealed; + * + * @name sealed + * @namespace BDD + * @api public + */ + + Assertion.addProperty('sealed', function() { + var obj = flag(this, 'object'); + + // In ES5, if the argument to this method is a primitive, then it will cause a TypeError. + // In ES6, a non-object argument will be treated as if it was a sealed ordinary object, simply return true. + // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isSealed + // The following provides ES6 behavior for ES5 environments. + + var isSealed = obj === Object(obj) ? Object.isSealed(obj) : true; + + this.assert( + isSealed + , 'expected #{this} to be sealed' + , 'expected #{this} to not be sealed' + ); + }); + + /** + * ### .frozen + * + * Asserts that the target is frozen, which means that new properties can't be + * added to it, and its existing properties can't be reassigned to different + * values, reconfigured, or deleted. Primitives are always frozen. + * + * var frozenObject = Object.freeze({}); + * + * expect(frozenObject).to.be.frozen; + * expect(1).to.be.frozen; + * + * Add `.not` earlier in the chain to negate `.frozen`. + * + * expect({a: 1}).to.not.be.frozen; + * + * A custom error message can be given as the second argument to `expect`. + * + * expect({a: 1}, 'nooo why fail??').to.be.frozen; + * + * @name frozen + * @namespace BDD + * @api public + */ + + Assertion.addProperty('frozen', function() { + var obj = flag(this, 'object'); + + // In ES5, if the argument to this method is a primitive, then it will cause a TypeError. + // In ES6, a non-object argument will be treated as if it was a frozen ordinary object, simply return true. + // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isFrozen + // The following provides ES6 behavior for ES5 environments. + + var isFrozen = obj === Object(obj) ? Object.isFrozen(obj) : true; + + this.assert( + isFrozen + , 'expected #{this} to be frozen' + , 'expected #{this} to not be frozen' + ); + }); + + /** + * ### .finite + * + * Asserts that the target is a number, and isn't `NaN` or positive/negative + * `Infinity`. + * + * expect(1).to.be.finite; + * + * Add `.not` earlier in the chain to negate `.finite`. However, it's + * dangerous to do so. The problem is that it creates uncertain expectations + * by asserting that the subject either isn't a number, or that it's `NaN`, or + * that it's positive `Infinity`, or that it's negative `Infinity`. It's often + * best to identify the exact output that's expected, and then write an + * assertion that only accepts that exact output. + * + * When the target isn't expected to be a number, it's often best to assert + * that it's the expected type, rather than asserting that it isn't one of + * many unexpected types. + * + * expect('foo').to.be.a('string'); // Recommended + * expect('foo').to.not.be.finite; // Not recommended + * + * When the target is expected to be `NaN`, it's often best to assert exactly + * that. + * + * expect(NaN).to.be.NaN; // Recommended + * expect(NaN).to.not.be.finite; // Not recommended + * + * When the target is expected to be positive infinity, it's often best to + * assert exactly that. + * + * expect(Infinity).to.equal(Infinity); // Recommended + * expect(Infinity).to.not.be.finite; // Not recommended + * + * When the target is expected to be negative infinity, it's often best to + * assert exactly that. + * + * expect(-Infinity).to.equal(-Infinity); // Recommended + * expect(-Infinity).to.not.be.finite; // Not recommended + * + * A custom error message can be given as the second argument to `expect`. + * + * expect('foo', 'nooo why fail??').to.be.finite; + * + * @name finite + * @namespace BDD + * @api public + */ + + Assertion.addProperty('finite', function(msg) { + var obj = flag(this, 'object'); + + this.assert( + typeof obj === 'number' && isFinite(obj) + , 'expected #{this} to be a finite number' + , 'expected #{this} to not be a finite number' + ); + }); + }; + + },{}],6:[function(require,module,exports){ + /*! + * chai + * Copyright(c) 2011-2014 Jake Luer + * MIT Licensed + */ + + module.exports = function (chai, util) { + /*! + * Chai dependencies. + */ + + var Assertion = chai.Assertion + , flag = util.flag; + + /*! + * Module export. + */ + + /** + * ### assert(expression, message) + * + * Write your own test expressions. + * + * assert('foo' !== 'bar', 'foo is not bar'); + * assert(Array.isArray([]), 'empty arrays are arrays'); + * + * @param {Mixed} expression to test for truthiness + * @param {String} message to display on error + * @name assert + * @namespace Assert + * @api public + */ + + var assert = chai.assert = function (express, errmsg) { + var test = new Assertion(null, null, chai.assert, true); + test.assert( + express + , errmsg + , '[ negation message unavailable ]' + ); + }; + + /** + * ### .fail([message]) + * ### .fail(actual, expected, [message], [operator]) + * + * Throw a failure. Node.js `assert` module-compatible. + * + * assert.fail(); + * assert.fail("custom error message"); + * assert.fail(1, 2); + * assert.fail(1, 2, "custom error message"); + * assert.fail(1, 2, "custom error message", ">"); + * assert.fail(1, 2, undefined, ">"); + * + * @name fail + * @param {Mixed} actual + * @param {Mixed} expected + * @param {String} message + * @param {String} operator + * @namespace Assert + * @api public + */ + + assert.fail = function (actual, expected, message, operator) { + if (arguments.length < 2) { + // Comply with Node's fail([message]) interface + + message = actual; + actual = undefined; + } + + message = message || 'assert.fail()'; + throw new chai.AssertionError(message, { + actual: actual + , expected: expected + , operator: operator + }, assert.fail); + }; + + /** + * ### .isOk(object, [message]) + * + * Asserts that `object` is truthy. + * + * assert.isOk('everything', 'everything is ok'); + * assert.isOk(false, 'this will fail'); + * + * @name isOk + * @alias ok + * @param {Mixed} object to test + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isOk = function (val, msg) { + new Assertion(val, msg, assert.isOk, true).is.ok; + }; + + /** + * ### .isNotOk(object, [message]) + * + * Asserts that `object` is falsy. + * + * assert.isNotOk('everything', 'this will fail'); + * assert.isNotOk(false, 'this will pass'); + * + * @name isNotOk + * @alias notOk + * @param {Mixed} object to test + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isNotOk = function (val, msg) { + new Assertion(val, msg, assert.isNotOk, true).is.not.ok; + }; + + /** + * ### .equal(actual, expected, [message]) + * + * Asserts non-strict equality (`==`) of `actual` and `expected`. + * + * assert.equal(3, '3', '== coerces values to strings'); + * + * @name equal + * @param {Mixed} actual + * @param {Mixed} expected + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.equal = function (act, exp, msg) { + var test = new Assertion(act, msg, assert.equal, true); + + test.assert( + exp == flag(test, 'object') + , 'expected #{this} to equal #{exp}' + , 'expected #{this} to not equal #{act}' + , exp + , act + , true + ); + }; + + /** + * ### .notEqual(actual, expected, [message]) + * + * Asserts non-strict inequality (`!=`) of `actual` and `expected`. + * + * assert.notEqual(3, 4, 'these numbers are not equal'); + * + * @name notEqual + * @param {Mixed} actual + * @param {Mixed} expected + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notEqual = function (act, exp, msg) { + var test = new Assertion(act, msg, assert.notEqual, true); + + test.assert( + exp != flag(test, 'object') + , 'expected #{this} to not equal #{exp}' + , 'expected #{this} to equal #{act}' + , exp + , act + , true + ); + }; + + /** + * ### .strictEqual(actual, expected, [message]) + * + * Asserts strict equality (`===`) of `actual` and `expected`. + * + * assert.strictEqual(true, true, 'these booleans are strictly equal'); + * + * @name strictEqual + * @param {Mixed} actual + * @param {Mixed} expected + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.strictEqual = function (act, exp, msg) { + new Assertion(act, msg, assert.strictEqual, true).to.equal(exp); + }; + + /** + * ### .notStrictEqual(actual, expected, [message]) + * + * Asserts strict inequality (`!==`) of `actual` and `expected`. + * + * assert.notStrictEqual(3, '3', 'no coercion for strict equality'); + * + * @name notStrictEqual + * @param {Mixed} actual + * @param {Mixed} expected + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notStrictEqual = function (act, exp, msg) { + new Assertion(act, msg, assert.notStrictEqual, true).to.not.equal(exp); + }; + + /** + * ### .deepEqual(actual, expected, [message]) + * + * Asserts that `actual` is deeply equal to `expected`. + * + * assert.deepEqual({ tea: 'green' }, { tea: 'green' }); + * + * @name deepEqual + * @param {Mixed} actual + * @param {Mixed} expected + * @param {String} message + * @alias deepStrictEqual + * @namespace Assert + * @api public + */ + + assert.deepEqual = assert.deepStrictEqual = function (act, exp, msg) { + new Assertion(act, msg, assert.deepEqual, true).to.eql(exp); + }; + + /** + * ### .notDeepEqual(actual, expected, [message]) + * + * Assert that `actual` is not deeply equal to `expected`. + * + * assert.notDeepEqual({ tea: 'green' }, { tea: 'jasmine' }); + * + * @name notDeepEqual + * @param {Mixed} actual + * @param {Mixed} expected + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notDeepEqual = function (act, exp, msg) { + new Assertion(act, msg, assert.notDeepEqual, true).to.not.eql(exp); + }; + + /** + * ### .isAbove(valueToCheck, valueToBeAbove, [message]) + * + * Asserts `valueToCheck` is strictly greater than (>) `valueToBeAbove`. + * + * assert.isAbove(5, 2, '5 is strictly greater than 2'); + * + * @name isAbove + * @param {Mixed} valueToCheck + * @param {Mixed} valueToBeAbove + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isAbove = function (val, abv, msg) { + new Assertion(val, msg, assert.isAbove, true).to.be.above(abv); + }; + + /** + * ### .isAtLeast(valueToCheck, valueToBeAtLeast, [message]) + * + * Asserts `valueToCheck` is greater than or equal to (>=) `valueToBeAtLeast`. + * + * assert.isAtLeast(5, 2, '5 is greater or equal to 2'); + * assert.isAtLeast(3, 3, '3 is greater or equal to 3'); + * + * @name isAtLeast + * @param {Mixed} valueToCheck + * @param {Mixed} valueToBeAtLeast + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isAtLeast = function (val, atlst, msg) { + new Assertion(val, msg, assert.isAtLeast, true).to.be.least(atlst); + }; + + /** + * ### .isBelow(valueToCheck, valueToBeBelow, [message]) + * + * Asserts `valueToCheck` is strictly less than (<) `valueToBeBelow`. + * + * assert.isBelow(3, 6, '3 is strictly less than 6'); + * + * @name isBelow + * @param {Mixed} valueToCheck + * @param {Mixed} valueToBeBelow + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isBelow = function (val, blw, msg) { + new Assertion(val, msg, assert.isBelow, true).to.be.below(blw); + }; + + /** + * ### .isAtMost(valueToCheck, valueToBeAtMost, [message]) + * + * Asserts `valueToCheck` is less than or equal to (<=) `valueToBeAtMost`. + * + * assert.isAtMost(3, 6, '3 is less than or equal to 6'); + * assert.isAtMost(4, 4, '4 is less than or equal to 4'); + * + * @name isAtMost + * @param {Mixed} valueToCheck + * @param {Mixed} valueToBeAtMost + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isAtMost = function (val, atmst, msg) { + new Assertion(val, msg, assert.isAtMost, true).to.be.most(atmst); + }; + + /** + * ### .isTrue(value, [message]) + * + * Asserts that `value` is true. + * + * var teaServed = true; + * assert.isTrue(teaServed, 'the tea has been served'); + * + * @name isTrue + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isTrue = function (val, msg) { + new Assertion(val, msg, assert.isTrue, true).is['true']; + }; + + /** + * ### .isNotTrue(value, [message]) + * + * Asserts that `value` is not true. + * + * var tea = 'tasty chai'; + * assert.isNotTrue(tea, 'great, time for tea!'); + * + * @name isNotTrue + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isNotTrue = function (val, msg) { + new Assertion(val, msg, assert.isNotTrue, true).to.not.equal(true); + }; + + /** + * ### .isFalse(value, [message]) + * + * Asserts that `value` is false. + * + * var teaServed = false; + * assert.isFalse(teaServed, 'no tea yet? hmm...'); + * + * @name isFalse + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isFalse = function (val, msg) { + new Assertion(val, msg, assert.isFalse, true).is['false']; + }; + + /** + * ### .isNotFalse(value, [message]) + * + * Asserts that `value` is not false. + * + * var tea = 'tasty chai'; + * assert.isNotFalse(tea, 'great, time for tea!'); + * + * @name isNotFalse + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isNotFalse = function (val, msg) { + new Assertion(val, msg, assert.isNotFalse, true).to.not.equal(false); + }; + + /** + * ### .isNull(value, [message]) + * + * Asserts that `value` is null. + * + * assert.isNull(err, 'there was no error'); + * + * @name isNull + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isNull = function (val, msg) { + new Assertion(val, msg, assert.isNull, true).to.equal(null); + }; + + /** + * ### .isNotNull(value, [message]) + * + * Asserts that `value` is not null. + * + * var tea = 'tasty chai'; + * assert.isNotNull(tea, 'great, time for tea!'); + * + * @name isNotNull + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isNotNull = function (val, msg) { + new Assertion(val, msg, assert.isNotNull, true).to.not.equal(null); + }; + + /** + * ### .isNaN + * + * Asserts that value is NaN. + * + * assert.isNaN(NaN, 'NaN is NaN'); + * + * @name isNaN + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isNaN = function (val, msg) { + new Assertion(val, msg, assert.isNaN, true).to.be.NaN; + }; + + /** + * ### .isNotNaN + * + * Asserts that value is not NaN. + * + * assert.isNotNaN(4, '4 is not NaN'); + * + * @name isNotNaN + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + assert.isNotNaN = function (val, msg) { + new Assertion(val, msg, assert.isNotNaN, true).not.to.be.NaN; + }; + + /** + * ### .exists + * + * Asserts that the target is neither `null` nor `undefined`. + * + * var foo = 'hi'; + * + * assert.exists(foo, 'foo is neither `null` nor `undefined`'); + * + * @name exists + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.exists = function (val, msg) { + new Assertion(val, msg, assert.exists, true).to.exist; + }; + + /** + * ### .notExists + * + * Asserts that the target is either `null` or `undefined`. + * + * var bar = null + * , baz; + * + * assert.notExists(bar); + * assert.notExists(baz, 'baz is either null or undefined'); + * + * @name notExists + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notExists = function (val, msg) { + new Assertion(val, msg, assert.notExists, true).to.not.exist; + }; + + /** + * ### .isUndefined(value, [message]) + * + * Asserts that `value` is `undefined`. + * + * var tea; + * assert.isUndefined(tea, 'no tea defined'); + * + * @name isUndefined + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isUndefined = function (val, msg) { + new Assertion(val, msg, assert.isUndefined, true).to.equal(undefined); + }; + + /** + * ### .isDefined(value, [message]) + * + * Asserts that `value` is not `undefined`. + * + * var tea = 'cup of chai'; + * assert.isDefined(tea, 'tea has been defined'); + * + * @name isDefined + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isDefined = function (val, msg) { + new Assertion(val, msg, assert.isDefined, true).to.not.equal(undefined); + }; + + /** + * ### .isFunction(value, [message]) + * + * Asserts that `value` is a function. + * + * function serveTea() { return 'cup of tea'; }; + * assert.isFunction(serveTea, 'great, we can have tea now'); + * + * @name isFunction + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isFunction = function (val, msg) { + new Assertion(val, msg, assert.isFunction, true).to.be.a('function'); + }; + + /** + * ### .isNotFunction(value, [message]) + * + * Asserts that `value` is _not_ a function. + * + * var serveTea = [ 'heat', 'pour', 'sip' ]; + * assert.isNotFunction(serveTea, 'great, we have listed the steps'); + * + * @name isNotFunction + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isNotFunction = function (val, msg) { + new Assertion(val, msg, assert.isNotFunction, true).to.not.be.a('function'); + }; + + /** + * ### .isObject(value, [message]) + * + * Asserts that `value` is an object of type 'Object' (as revealed by `Object.prototype.toString`). + * _The assertion does not match subclassed objects._ + * + * var selection = { name: 'Chai', serve: 'with spices' }; + * assert.isObject(selection, 'tea selection is an object'); + * + * @name isObject + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isObject = function (val, msg) { + new Assertion(val, msg, assert.isObject, true).to.be.a('object'); + }; + + /** + * ### .isNotObject(value, [message]) + * + * Asserts that `value` is _not_ an object of type 'Object' (as revealed by `Object.prototype.toString`). + * + * var selection = 'chai' + * assert.isNotObject(selection, 'tea selection is not an object'); + * assert.isNotObject(null, 'null is not an object'); + * + * @name isNotObject + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isNotObject = function (val, msg) { + new Assertion(val, msg, assert.isNotObject, true).to.not.be.a('object'); + }; + + /** + * ### .isArray(value, [message]) + * + * Asserts that `value` is an array. + * + * var menu = [ 'green', 'chai', 'oolong' ]; + * assert.isArray(menu, 'what kind of tea do we want?'); + * + * @name isArray + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isArray = function (val, msg) { + new Assertion(val, msg, assert.isArray, true).to.be.an('array'); + }; + + /** + * ### .isNotArray(value, [message]) + * + * Asserts that `value` is _not_ an array. + * + * var menu = 'green|chai|oolong'; + * assert.isNotArray(menu, 'what kind of tea do we want?'); + * + * @name isNotArray + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isNotArray = function (val, msg) { + new Assertion(val, msg, assert.isNotArray, true).to.not.be.an('array'); + }; + + /** + * ### .isString(value, [message]) + * + * Asserts that `value` is a string. + * + * var teaOrder = 'chai'; + * assert.isString(teaOrder, 'order placed'); + * + * @name isString + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isString = function (val, msg) { + new Assertion(val, msg, assert.isString, true).to.be.a('string'); + }; + + /** + * ### .isNotString(value, [message]) + * + * Asserts that `value` is _not_ a string. + * + * var teaOrder = 4; + * assert.isNotString(teaOrder, 'order placed'); + * + * @name isNotString + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isNotString = function (val, msg) { + new Assertion(val, msg, assert.isNotString, true).to.not.be.a('string'); + }; + + /** + * ### .isNumber(value, [message]) + * + * Asserts that `value` is a number. + * + * var cups = 2; + * assert.isNumber(cups, 'how many cups'); + * + * @name isNumber + * @param {Number} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isNumber = function (val, msg) { + new Assertion(val, msg, assert.isNumber, true).to.be.a('number'); + }; + + /** + * ### .isNotNumber(value, [message]) + * + * Asserts that `value` is _not_ a number. + * + * var cups = '2 cups please'; + * assert.isNotNumber(cups, 'how many cups'); + * + * @name isNotNumber + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isNotNumber = function (val, msg) { + new Assertion(val, msg, assert.isNotNumber, true).to.not.be.a('number'); + }; + + /** + * ### .isFinite(value, [message]) + * + * Asserts that `value` is a finite number. Unlike `.isNumber`, this will fail for `NaN` and `Infinity`. + * + * var cups = 2; + * assert.isFinite(cups, 'how many cups'); + * + * assert.isFinite(NaN); // throws + * + * @name isFinite + * @param {Number} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isFinite = function (val, msg) { + new Assertion(val, msg, assert.isFinite, true).to.be.finite; + }; + + /** + * ### .isBoolean(value, [message]) + * + * Asserts that `value` is a boolean. + * + * var teaReady = true + * , teaServed = false; + * + * assert.isBoolean(teaReady, 'is the tea ready'); + * assert.isBoolean(teaServed, 'has tea been served'); + * + * @name isBoolean + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isBoolean = function (val, msg) { + new Assertion(val, msg, assert.isBoolean, true).to.be.a('boolean'); + }; + + /** + * ### .isNotBoolean(value, [message]) + * + * Asserts that `value` is _not_ a boolean. + * + * var teaReady = 'yep' + * , teaServed = 'nope'; + * + * assert.isNotBoolean(teaReady, 'is the tea ready'); + * assert.isNotBoolean(teaServed, 'has tea been served'); + * + * @name isNotBoolean + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.isNotBoolean = function (val, msg) { + new Assertion(val, msg, assert.isNotBoolean, true).to.not.be.a('boolean'); + }; + + /** + * ### .typeOf(value, name, [message]) + * + * Asserts that `value`'s type is `name`, as determined by + * `Object.prototype.toString`. + * + * assert.typeOf({ tea: 'chai' }, 'object', 'we have an object'); + * assert.typeOf(['chai', 'jasmine'], 'array', 'we have an array'); + * assert.typeOf('tea', 'string', 'we have a string'); + * assert.typeOf(/tea/, 'regexp', 'we have a regular expression'); + * assert.typeOf(null, 'null', 'we have a null'); + * assert.typeOf(undefined, 'undefined', 'we have an undefined'); + * + * @name typeOf + * @param {Mixed} value + * @param {String} name + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.typeOf = function (val, type, msg) { + new Assertion(val, msg, assert.typeOf, true).to.be.a(type); + }; + + /** + * ### .notTypeOf(value, name, [message]) + * + * Asserts that `value`'s type is _not_ `name`, as determined by + * `Object.prototype.toString`. + * + * assert.notTypeOf('tea', 'number', 'strings are not numbers'); + * + * @name notTypeOf + * @param {Mixed} value + * @param {String} typeof name + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notTypeOf = function (val, type, msg) { + new Assertion(val, msg, assert.notTypeOf, true).to.not.be.a(type); + }; + + /** + * ### .instanceOf(object, constructor, [message]) + * + * Asserts that `value` is an instance of `constructor`. + * + * var Tea = function (name) { this.name = name; } + * , chai = new Tea('chai'); + * + * assert.instanceOf(chai, Tea, 'chai is an instance of tea'); + * + * @name instanceOf + * @param {Object} object + * @param {Constructor} constructor + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.instanceOf = function (val, type, msg) { + new Assertion(val, msg, assert.instanceOf, true).to.be.instanceOf(type); + }; + + /** + * ### .notInstanceOf(object, constructor, [message]) + * + * Asserts `value` is not an instance of `constructor`. + * + * var Tea = function (name) { this.name = name; } + * , chai = new String('chai'); + * + * assert.notInstanceOf(chai, Tea, 'chai is not an instance of tea'); + * + * @name notInstanceOf + * @param {Object} object + * @param {Constructor} constructor + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notInstanceOf = function (val, type, msg) { + new Assertion(val, msg, assert.notInstanceOf, true) + .to.not.be.instanceOf(type); + }; + + /** + * ### .include(haystack, needle, [message]) + * + * Asserts that `haystack` includes `needle`. Can be used to assert the + * inclusion of a value in an array, a substring in a string, or a subset of + * properties in an object. + * + * assert.include([1,2,3], 2, 'array contains value'); + * assert.include('foobar', 'foo', 'string contains substring'); + * assert.include({ foo: 'bar', hello: 'universe' }, { foo: 'bar' }, 'object contains property'); + * + * Strict equality (===) is used. When asserting the inclusion of a value in + * an array, the array is searched for an element that's strictly equal to the + * given value. When asserting a subset of properties in an object, the object + * is searched for the given property keys, checking that each one is present + * and strictly equal to the given property value. For instance: + * + * var obj1 = {a: 1} + * , obj2 = {b: 2}; + * assert.include([obj1, obj2], obj1); + * assert.include({foo: obj1, bar: obj2}, {foo: obj1}); + * assert.include({foo: obj1, bar: obj2}, {foo: obj1, bar: obj2}); + * + * @name include + * @param {Array|String} haystack + * @param {Mixed} needle + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.include = function (exp, inc, msg) { + new Assertion(exp, msg, assert.include, true).include(inc); + }; + + /** + * ### .notInclude(haystack, needle, [message]) + * + * Asserts that `haystack` does not include `needle`. Can be used to assert + * the absence of a value in an array, a substring in a string, or a subset of + * properties in an object. + * + * assert.notInclude([1,2,3], 4, "array doesn't contain value"); + * assert.notInclude('foobar', 'baz', "string doesn't contain substring"); + * assert.notInclude({ foo: 'bar', hello: 'universe' }, { foo: 'baz' }, 'object doesn't contain property'); + * + * Strict equality (===) is used. When asserting the absence of a value in an + * array, the array is searched to confirm the absence of an element that's + * strictly equal to the given value. When asserting a subset of properties in + * an object, the object is searched to confirm that at least one of the given + * property keys is either not present or not strictly equal to the given + * property value. For instance: + * + * var obj1 = {a: 1} + * , obj2 = {b: 2}; + * assert.notInclude([obj1, obj2], {a: 1}); + * assert.notInclude({foo: obj1, bar: obj2}, {foo: {a: 1}}); + * assert.notInclude({foo: obj1, bar: obj2}, {foo: obj1, bar: {b: 2}}); + * + * @name notInclude + * @param {Array|String} haystack + * @param {Mixed} needle + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notInclude = function (exp, inc, msg) { + new Assertion(exp, msg, assert.notInclude, true).not.include(inc); + }; + + /** + * ### .deepInclude(haystack, needle, [message]) + * + * Asserts that `haystack` includes `needle`. Can be used to assert the + * inclusion of a value in an array or a subset of properties in an object. + * Deep equality is used. + * + * var obj1 = {a: 1} + * , obj2 = {b: 2}; + * assert.deepInclude([obj1, obj2], {a: 1}); + * assert.deepInclude({foo: obj1, bar: obj2}, {foo: {a: 1}}); + * assert.deepInclude({foo: obj1, bar: obj2}, {foo: {a: 1}, bar: {b: 2}}); + * + * @name deepInclude + * @param {Array|String} haystack + * @param {Mixed} needle + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.deepInclude = function (exp, inc, msg) { + new Assertion(exp, msg, assert.deepInclude, true).deep.include(inc); + }; + + /** + * ### .notDeepInclude(haystack, needle, [message]) + * + * Asserts that `haystack` does not include `needle`. Can be used to assert + * the absence of a value in an array or a subset of properties in an object. + * Deep equality is used. + * + * var obj1 = {a: 1} + * , obj2 = {b: 2}; + * assert.notDeepInclude([obj1, obj2], {a: 9}); + * assert.notDeepInclude({foo: obj1, bar: obj2}, {foo: {a: 9}}); + * assert.notDeepInclude({foo: obj1, bar: obj2}, {foo: {a: 1}, bar: {b: 9}}); + * + * @name notDeepInclude + * @param {Array|String} haystack + * @param {Mixed} needle + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notDeepInclude = function (exp, inc, msg) { + new Assertion(exp, msg, assert.notDeepInclude, true).not.deep.include(inc); + }; + + /** + * ### .nestedInclude(haystack, needle, [message]) + * + * Asserts that 'haystack' includes 'needle'. + * Can be used to assert the inclusion of a subset of properties in an + * object. + * Enables the use of dot- and bracket-notation for referencing nested + * properties. + * '[]' and '.' in property names can be escaped using double backslashes. + * + * assert.nestedInclude({'.a': {'b': 'x'}}, {'\\.a.[b]': 'x'}); + * assert.nestedInclude({'a': {'[b]': 'x'}}, {'a.\\[b\\]': 'x'}); + * + * @name nestedInclude + * @param {Object} haystack + * @param {Object} needle + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.nestedInclude = function (exp, inc, msg) { + new Assertion(exp, msg, assert.nestedInclude, true).nested.include(inc); + }; + + /** + * ### .notNestedInclude(haystack, needle, [message]) + * + * Asserts that 'haystack' does not include 'needle'. + * Can be used to assert the absence of a subset of properties in an + * object. + * Enables the use of dot- and bracket-notation for referencing nested + * properties. + * '[]' and '.' in property names can be escaped using double backslashes. + * + * assert.notNestedInclude({'.a': {'b': 'x'}}, {'\\.a.b': 'y'}); + * assert.notNestedInclude({'a': {'[b]': 'x'}}, {'a.\\[b\\]': 'y'}); + * + * @name notNestedInclude + * @param {Object} haystack + * @param {Object} needle + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notNestedInclude = function (exp, inc, msg) { + new Assertion(exp, msg, assert.notNestedInclude, true) + .not.nested.include(inc); + }; + + /** + * ### .deepNestedInclude(haystack, needle, [message]) + * + * Asserts that 'haystack' includes 'needle'. + * Can be used to assert the inclusion of a subset of properties in an + * object while checking for deep equality. + * Enables the use of dot- and bracket-notation for referencing nested + * properties. + * '[]' and '.' in property names can be escaped using double backslashes. + * + * assert.deepNestedInclude({a: {b: [{x: 1}]}}, {'a.b[0]': {x: 1}}); + * assert.deepNestedInclude({'.a': {'[b]': {x: 1}}}, {'\\.a.\\[b\\]': {x: 1}}); + * + * @name deepNestedInclude + * @param {Object} haystack + * @param {Object} needle + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.deepNestedInclude = function(exp, inc, msg) { + new Assertion(exp, msg, assert.deepNestedInclude, true) + .deep.nested.include(inc); + }; + + /** + * ### .notDeepNestedInclude(haystack, needle, [message]) + * + * Asserts that 'haystack' does not include 'needle'. + * Can be used to assert the absence of a subset of properties in an + * object while checking for deep equality. + * Enables the use of dot- and bracket-notation for referencing nested + * properties. + * '[]' and '.' in property names can be escaped using double backslashes. + * + * assert.notDeepNestedInclude({a: {b: [{x: 1}]}}, {'a.b[0]': {y: 1}}) + * assert.notDeepNestedInclude({'.a': {'[b]': {x: 1}}}, {'\\.a.\\[b\\]': {y: 2}}); + * + * @name notDeepNestedInclude + * @param {Object} haystack + * @param {Object} needle + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notDeepNestedInclude = function(exp, inc, msg) { + new Assertion(exp, msg, assert.notDeepNestedInclude, true) + .not.deep.nested.include(inc); + }; + + /** + * ### .ownInclude(haystack, needle, [message]) + * + * Asserts that 'haystack' includes 'needle'. + * Can be used to assert the inclusion of a subset of properties in an + * object while ignoring inherited properties. + * + * assert.ownInclude({ a: 1 }, { a: 1 }); + * + * @name ownInclude + * @param {Object} haystack + * @param {Object} needle + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.ownInclude = function(exp, inc, msg) { + new Assertion(exp, msg, assert.ownInclude, true).own.include(inc); + }; + + /** + * ### .notOwnInclude(haystack, needle, [message]) + * + * Asserts that 'haystack' includes 'needle'. + * Can be used to assert the absence of a subset of properties in an + * object while ignoring inherited properties. + * + * Object.prototype.b = 2; + * + * assert.notOwnInclude({ a: 1 }, { b: 2 }); + * + * @name notOwnInclude + * @param {Object} haystack + * @param {Object} needle + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notOwnInclude = function(exp, inc, msg) { + new Assertion(exp, msg, assert.notOwnInclude, true).not.own.include(inc); + }; + + /** + * ### .deepOwnInclude(haystack, needle, [message]) + * + * Asserts that 'haystack' includes 'needle'. + * Can be used to assert the inclusion of a subset of properties in an + * object while ignoring inherited properties and checking for deep equality. + * + * assert.deepOwnInclude({a: {b: 2}}, {a: {b: 2}}); + * + * @name deepOwnInclude + * @param {Object} haystack + * @param {Object} needle + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.deepOwnInclude = function(exp, inc, msg) { + new Assertion(exp, msg, assert.deepOwnInclude, true) + .deep.own.include(inc); + }; + + /** + * ### .notDeepOwnInclude(haystack, needle, [message]) + * + * Asserts that 'haystack' includes 'needle'. + * Can be used to assert the absence of a subset of properties in an + * object while ignoring inherited properties and checking for deep equality. + * + * assert.notDeepOwnInclude({a: {b: 2}}, {a: {c: 3}}); + * + * @name notDeepOwnInclude + * @param {Object} haystack + * @param {Object} needle + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notDeepOwnInclude = function(exp, inc, msg) { + new Assertion(exp, msg, assert.notDeepOwnInclude, true) + .not.deep.own.include(inc); + }; + + /** + * ### .match(value, regexp, [message]) + * + * Asserts that `value` matches the regular expression `regexp`. + * + * assert.match('foobar', /^foo/, 'regexp matches'); + * + * @name match + * @param {Mixed} value + * @param {RegExp} regexp + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.match = function (exp, re, msg) { + new Assertion(exp, msg, assert.match, true).to.match(re); + }; + + /** + * ### .notMatch(value, regexp, [message]) + * + * Asserts that `value` does not match the regular expression `regexp`. + * + * assert.notMatch('foobar', /^foo/, 'regexp does not match'); + * + * @name notMatch + * @param {Mixed} value + * @param {RegExp} regexp + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notMatch = function (exp, re, msg) { + new Assertion(exp, msg, assert.notMatch, true).to.not.match(re); + }; + + /** + * ### .property(object, property, [message]) + * + * Asserts that `object` has a direct or inherited property named by + * `property`. + * + * assert.property({ tea: { green: 'matcha' }}, 'tea'); + * assert.property({ tea: { green: 'matcha' }}, 'toString'); + * + * @name property + * @param {Object} object + * @param {String} property + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.property = function (obj, prop, msg) { + new Assertion(obj, msg, assert.property, true).to.have.property(prop); + }; + + /** + * ### .notProperty(object, property, [message]) + * + * Asserts that `object` does _not_ have a direct or inherited property named + * by `property`. + * + * assert.notProperty({ tea: { green: 'matcha' }}, 'coffee'); + * + * @name notProperty + * @param {Object} object + * @param {String} property + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notProperty = function (obj, prop, msg) { + new Assertion(obj, msg, assert.notProperty, true) + .to.not.have.property(prop); + }; + + /** + * ### .propertyVal(object, property, value, [message]) + * + * Asserts that `object` has a direct or inherited property named by + * `property` with a value given by `value`. Uses a strict equality check + * (===). + * + * assert.propertyVal({ tea: 'is good' }, 'tea', 'is good'); + * + * @name propertyVal + * @param {Object} object + * @param {String} property + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.propertyVal = function (obj, prop, val, msg) { + new Assertion(obj, msg, assert.propertyVal, true) + .to.have.property(prop, val); + }; + + /** + * ### .notPropertyVal(object, property, value, [message]) + * + * Asserts that `object` does _not_ have a direct or inherited property named + * by `property` with value given by `value`. Uses a strict equality check + * (===). + * + * assert.notPropertyVal({ tea: 'is good' }, 'tea', 'is bad'); + * assert.notPropertyVal({ tea: 'is good' }, 'coffee', 'is good'); + * + * @name notPropertyVal + * @param {Object} object + * @param {String} property + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notPropertyVal = function (obj, prop, val, msg) { + new Assertion(obj, msg, assert.notPropertyVal, true) + .to.not.have.property(prop, val); + }; + + /** + * ### .deepPropertyVal(object, property, value, [message]) + * + * Asserts that `object` has a direct or inherited property named by + * `property` with a value given by `value`. Uses a deep equality check. + * + * assert.deepPropertyVal({ tea: { green: 'matcha' } }, 'tea', { green: 'matcha' }); + * + * @name deepPropertyVal + * @param {Object} object + * @param {String} property + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.deepPropertyVal = function (obj, prop, val, msg) { + new Assertion(obj, msg, assert.deepPropertyVal, true) + .to.have.deep.property(prop, val); + }; + + /** + * ### .notDeepPropertyVal(object, property, value, [message]) + * + * Asserts that `object` does _not_ have a direct or inherited property named + * by `property` with value given by `value`. Uses a deep equality check. + * + * assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'tea', { black: 'matcha' }); + * assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'tea', { green: 'oolong' }); + * assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'coffee', { green: 'matcha' }); + * + * @name notDeepPropertyVal + * @param {Object} object + * @param {String} property + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notDeepPropertyVal = function (obj, prop, val, msg) { + new Assertion(obj, msg, assert.notDeepPropertyVal, true) + .to.not.have.deep.property(prop, val); + }; + + /** + * ### .ownProperty(object, property, [message]) + * + * Asserts that `object` has a direct property named by `property`. Inherited + * properties aren't checked. + * + * assert.ownProperty({ tea: { green: 'matcha' }}, 'tea'); + * + * @name ownProperty + * @param {Object} object + * @param {String} property + * @param {String} message + * @api public + */ + + assert.ownProperty = function (obj, prop, msg) { + new Assertion(obj, msg, assert.ownProperty, true) + .to.have.own.property(prop); + }; + + /** + * ### .notOwnProperty(object, property, [message]) + * + * Asserts that `object` does _not_ have a direct property named by + * `property`. Inherited properties aren't checked. + * + * assert.notOwnProperty({ tea: { green: 'matcha' }}, 'coffee'); + * assert.notOwnProperty({}, 'toString'); + * + * @name notOwnProperty + * @param {Object} object + * @param {String} property + * @param {String} message + * @api public + */ + + assert.notOwnProperty = function (obj, prop, msg) { + new Assertion(obj, msg, assert.notOwnProperty, true) + .to.not.have.own.property(prop); + }; + + /** + * ### .ownPropertyVal(object, property, value, [message]) + * + * Asserts that `object` has a direct property named by `property` and a value + * equal to the provided `value`. Uses a strict equality check (===). + * Inherited properties aren't checked. + * + * assert.ownPropertyVal({ coffee: 'is good'}, 'coffee', 'is good'); + * + * @name ownPropertyVal + * @param {Object} object + * @param {String} property + * @param {Mixed} value + * @param {String} message + * @api public + */ + + assert.ownPropertyVal = function (obj, prop, value, msg) { + new Assertion(obj, msg, assert.ownPropertyVal, true) + .to.have.own.property(prop, value); + }; + + /** + * ### .notOwnPropertyVal(object, property, value, [message]) + * + * Asserts that `object` does _not_ have a direct property named by `property` + * with a value equal to the provided `value`. Uses a strict equality check + * (===). Inherited properties aren't checked. + * + * assert.notOwnPropertyVal({ tea: 'is better'}, 'tea', 'is worse'); + * assert.notOwnPropertyVal({}, 'toString', Object.prototype.toString); + * + * @name notOwnPropertyVal + * @param {Object} object + * @param {String} property + * @param {Mixed} value + * @param {String} message + * @api public + */ + + assert.notOwnPropertyVal = function (obj, prop, value, msg) { + new Assertion(obj, msg, assert.notOwnPropertyVal, true) + .to.not.have.own.property(prop, value); + }; + + /** + * ### .deepOwnPropertyVal(object, property, value, [message]) + * + * Asserts that `object` has a direct property named by `property` and a value + * equal to the provided `value`. Uses a deep equality check. Inherited + * properties aren't checked. + * + * assert.deepOwnPropertyVal({ tea: { green: 'matcha' } }, 'tea', { green: 'matcha' }); + * + * @name deepOwnPropertyVal + * @param {Object} object + * @param {String} property + * @param {Mixed} value + * @param {String} message + * @api public + */ + + assert.deepOwnPropertyVal = function (obj, prop, value, msg) { + new Assertion(obj, msg, assert.deepOwnPropertyVal, true) + .to.have.deep.own.property(prop, value); + }; + + /** + * ### .notDeepOwnPropertyVal(object, property, value, [message]) + * + * Asserts that `object` does _not_ have a direct property named by `property` + * with a value equal to the provided `value`. Uses a deep equality check. + * Inherited properties aren't checked. + * + * assert.notDeepOwnPropertyVal({ tea: { green: 'matcha' } }, 'tea', { black: 'matcha' }); + * assert.notDeepOwnPropertyVal({ tea: { green: 'matcha' } }, 'tea', { green: 'oolong' }); + * assert.notDeepOwnPropertyVal({ tea: { green: 'matcha' } }, 'coffee', { green: 'matcha' }); + * assert.notDeepOwnPropertyVal({}, 'toString', Object.prototype.toString); + * + * @name notDeepOwnPropertyVal + * @param {Object} object + * @param {String} property + * @param {Mixed} value + * @param {String} message + * @api public + */ + + assert.notDeepOwnPropertyVal = function (obj, prop, value, msg) { + new Assertion(obj, msg, assert.notDeepOwnPropertyVal, true) + .to.not.have.deep.own.property(prop, value); + }; + + /** + * ### .nestedProperty(object, property, [message]) + * + * Asserts that `object` has a direct or inherited property named by + * `property`, which can be a string using dot- and bracket-notation for + * nested reference. + * + * assert.nestedProperty({ tea: { green: 'matcha' }}, 'tea.green'); + * + * @name nestedProperty + * @param {Object} object + * @param {String} property + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.nestedProperty = function (obj, prop, msg) { + new Assertion(obj, msg, assert.nestedProperty, true) + .to.have.nested.property(prop); + }; + + /** + * ### .notNestedProperty(object, property, [message]) + * + * Asserts that `object` does _not_ have a property named by `property`, which + * can be a string using dot- and bracket-notation for nested reference. The + * property cannot exist on the object nor anywhere in its prototype chain. + * + * assert.notNestedProperty({ tea: { green: 'matcha' }}, 'tea.oolong'); + * + * @name notNestedProperty + * @param {Object} object + * @param {String} property + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notNestedProperty = function (obj, prop, msg) { + new Assertion(obj, msg, assert.notNestedProperty, true) + .to.not.have.nested.property(prop); + }; + + /** + * ### .nestedPropertyVal(object, property, value, [message]) + * + * Asserts that `object` has a property named by `property` with value given + * by `value`. `property` can use dot- and bracket-notation for nested + * reference. Uses a strict equality check (===). + * + * assert.nestedPropertyVal({ tea: { green: 'matcha' }}, 'tea.green', 'matcha'); + * + * @name nestedPropertyVal + * @param {Object} object + * @param {String} property + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.nestedPropertyVal = function (obj, prop, val, msg) { + new Assertion(obj, msg, assert.nestedPropertyVal, true) + .to.have.nested.property(prop, val); + }; + + /** + * ### .notNestedPropertyVal(object, property, value, [message]) + * + * Asserts that `object` does _not_ have a property named by `property` with + * value given by `value`. `property` can use dot- and bracket-notation for + * nested reference. Uses a strict equality check (===). + * + * assert.notNestedPropertyVal({ tea: { green: 'matcha' }}, 'tea.green', 'konacha'); + * assert.notNestedPropertyVal({ tea: { green: 'matcha' }}, 'coffee.green', 'matcha'); + * + * @name notNestedPropertyVal + * @param {Object} object + * @param {String} property + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notNestedPropertyVal = function (obj, prop, val, msg) { + new Assertion(obj, msg, assert.notNestedPropertyVal, true) + .to.not.have.nested.property(prop, val); + }; + + /** + * ### .deepNestedPropertyVal(object, property, value, [message]) + * + * Asserts that `object` has a property named by `property` with a value given + * by `value`. `property` can use dot- and bracket-notation for nested + * reference. Uses a deep equality check. + * + * assert.deepNestedPropertyVal({ tea: { green: { matcha: 'yum' } } }, 'tea.green', { matcha: 'yum' }); + * + * @name deepNestedPropertyVal + * @param {Object} object + * @param {String} property + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.deepNestedPropertyVal = function (obj, prop, val, msg) { + new Assertion(obj, msg, assert.deepNestedPropertyVal, true) + .to.have.deep.nested.property(prop, val); + }; + + /** + * ### .notDeepNestedPropertyVal(object, property, value, [message]) + * + * Asserts that `object` does _not_ have a property named by `property` with + * value given by `value`. `property` can use dot- and bracket-notation for + * nested reference. Uses a deep equality check. + * + * assert.notDeepNestedPropertyVal({ tea: { green: { matcha: 'yum' } } }, 'tea.green', { oolong: 'yum' }); + * assert.notDeepNestedPropertyVal({ tea: { green: { matcha: 'yum' } } }, 'tea.green', { matcha: 'yuck' }); + * assert.notDeepNestedPropertyVal({ tea: { green: { matcha: 'yum' } } }, 'tea.black', { matcha: 'yum' }); + * + * @name notDeepNestedPropertyVal + * @param {Object} object + * @param {String} property + * @param {Mixed} value + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notDeepNestedPropertyVal = function (obj, prop, val, msg) { + new Assertion(obj, msg, assert.notDeepNestedPropertyVal, true) + .to.not.have.deep.nested.property(prop, val); + } + + /** + * ### .lengthOf(object, length, [message]) + * + * Asserts that `object` has a `length` or `size` with the expected value. + * + * assert.lengthOf([1,2,3], 3, 'array has length of 3'); + * assert.lengthOf('foobar', 6, 'string has length of 6'); + * assert.lengthOf(new Set([1,2,3]), 3, 'set has size of 3'); + * assert.lengthOf(new Map([['a',1],['b',2],['c',3]]), 3, 'map has size of 3'); + * + * @name lengthOf + * @param {Mixed} object + * @param {Number} length + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.lengthOf = function (exp, len, msg) { + new Assertion(exp, msg, assert.lengthOf, true).to.have.lengthOf(len); + }; + + /** + * ### .hasAnyKeys(object, [keys], [message]) + * + * Asserts that `object` has at least one of the `keys` provided. + * You can also provide a single object instead of a `keys` array and its keys + * will be used as the expected set of keys. + * + * assert.hasAnyKeys({foo: 1, bar: 2, baz: 3}, ['foo', 'iDontExist', 'baz']); + * assert.hasAnyKeys({foo: 1, bar: 2, baz: 3}, {foo: 30, iDontExist: 99, baz: 1337}); + * assert.hasAnyKeys(new Map([[{foo: 1}, 'bar'], ['key', 'value']]), [{foo: 1}, 'key']); + * assert.hasAnyKeys(new Set([{foo: 'bar'}, 'anotherKey']), [{foo: 'bar'}, 'anotherKey']); + * + * @name hasAnyKeys + * @param {Mixed} object + * @param {Array|Object} keys + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.hasAnyKeys = function (obj, keys, msg) { + new Assertion(obj, msg, assert.hasAnyKeys, true).to.have.any.keys(keys); + } + + /** + * ### .hasAllKeys(object, [keys], [message]) + * + * Asserts that `object` has all and only all of the `keys` provided. + * You can also provide a single object instead of a `keys` array and its keys + * will be used as the expected set of keys. + * + * assert.hasAllKeys({foo: 1, bar: 2, baz: 3}, ['foo', 'bar', 'baz']); + * assert.hasAllKeys({foo: 1, bar: 2, baz: 3}, {foo: 30, bar: 99, baz: 1337]); + * assert.hasAllKeys(new Map([[{foo: 1}, 'bar'], ['key', 'value']]), [{foo: 1}, 'key']); + * assert.hasAllKeys(new Set([{foo: 'bar'}, 'anotherKey'], [{foo: 'bar'}, 'anotherKey']); + * + * @name hasAllKeys + * @param {Mixed} object + * @param {String[]} keys + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.hasAllKeys = function (obj, keys, msg) { + new Assertion(obj, msg, assert.hasAllKeys, true).to.have.all.keys(keys); + } + + /** + * ### .containsAllKeys(object, [keys], [message]) + * + * Asserts that `object` has all of the `keys` provided but may have more keys not listed. + * You can also provide a single object instead of a `keys` array and its keys + * will be used as the expected set of keys. + * + * assert.containsAllKeys({foo: 1, bar: 2, baz: 3}, ['foo', 'baz']); + * assert.containsAllKeys({foo: 1, bar: 2, baz: 3}, ['foo', 'bar', 'baz']); + * assert.containsAllKeys({foo: 1, bar: 2, baz: 3}, {foo: 30, baz: 1337}); + * assert.containsAllKeys({foo: 1, bar: 2, baz: 3}, {foo: 30, bar: 99, baz: 1337}); + * assert.containsAllKeys(new Map([[{foo: 1}, 'bar'], ['key', 'value']]), [{foo: 1}]); + * assert.containsAllKeys(new Map([[{foo: 1}, 'bar'], ['key', 'value']]), [{foo: 1}, 'key']); + * assert.containsAllKeys(new Set([{foo: 'bar'}, 'anotherKey'], [{foo: 'bar'}]); + * assert.containsAllKeys(new Set([{foo: 'bar'}, 'anotherKey'], [{foo: 'bar'}, 'anotherKey']); + * + * @name containsAllKeys + * @param {Mixed} object + * @param {String[]} keys + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.containsAllKeys = function (obj, keys, msg) { + new Assertion(obj, msg, assert.containsAllKeys, true) + .to.contain.all.keys(keys); + } + + /** + * ### .doesNotHaveAnyKeys(object, [keys], [message]) + * + * Asserts that `object` has none of the `keys` provided. + * You can also provide a single object instead of a `keys` array and its keys + * will be used as the expected set of keys. + * + * assert.doesNotHaveAnyKeys({foo: 1, bar: 2, baz: 3}, ['one', 'two', 'example']); + * assert.doesNotHaveAnyKeys({foo: 1, bar: 2, baz: 3}, {one: 1, two: 2, example: 'foo'}); + * assert.doesNotHaveAnyKeys(new Map([[{foo: 1}, 'bar'], ['key', 'value']]), [{one: 'two'}, 'example']); + * assert.doesNotHaveAnyKeys(new Set([{foo: 'bar'}, 'anotherKey'], [{one: 'two'}, 'example']); + * + * @name doesNotHaveAnyKeys + * @param {Mixed} object + * @param {String[]} keys + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.doesNotHaveAnyKeys = function (obj, keys, msg) { + new Assertion(obj, msg, assert.doesNotHaveAnyKeys, true) + .to.not.have.any.keys(keys); + } + + /** + * ### .doesNotHaveAllKeys(object, [keys], [message]) + * + * Asserts that `object` does not have at least one of the `keys` provided. + * You can also provide a single object instead of a `keys` array and its keys + * will be used as the expected set of keys. + * + * assert.doesNotHaveAllKeys({foo: 1, bar: 2, baz: 3}, ['one', 'two', 'example']); + * assert.doesNotHaveAllKeys({foo: 1, bar: 2, baz: 3}, {one: 1, two: 2, example: 'foo'}); + * assert.doesNotHaveAllKeys(new Map([[{foo: 1}, 'bar'], ['key', 'value']]), [{one: 'two'}, 'example']); + * assert.doesNotHaveAllKeys(new Set([{foo: 'bar'}, 'anotherKey'], [{one: 'two'}, 'example']); + * + * @name doesNotHaveAllKeys + * @param {Mixed} object + * @param {String[]} keys + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.doesNotHaveAllKeys = function (obj, keys, msg) { + new Assertion(obj, msg, assert.doesNotHaveAllKeys, true) + .to.not.have.all.keys(keys); + } + + /** + * ### .hasAnyDeepKeys(object, [keys], [message]) + * + * Asserts that `object` has at least one of the `keys` provided. + * Since Sets and Maps can have objects as keys you can use this assertion to perform + * a deep comparison. + * You can also provide a single object instead of a `keys` array and its keys + * will be used as the expected set of keys. + * + * assert.hasAnyDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [1, 2]]), {one: 'one'}); + * assert.hasAnyDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [1, 2]]), [{one: 'one'}, {two: 'two'}]); + * assert.hasAnyDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [{two: 'two'}, 'valueTwo']]), [{one: 'one'}, {two: 'two'}]); + * assert.hasAnyDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), {one: 'one'}); + * assert.hasAnyDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), [{one: 'one'}, {three: 'three'}]); + * assert.hasAnyDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), [{one: 'one'}, {two: 'two'}]); + * + * @name doesNotHaveAllKeys + * @param {Mixed} object + * @param {Array|Object} keys + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.hasAnyDeepKeys = function (obj, keys, msg) { + new Assertion(obj, msg, assert.hasAnyDeepKeys, true) + .to.have.any.deep.keys(keys); + } + + /** + * ### .hasAllDeepKeys(object, [keys], [message]) + * + * Asserts that `object` has all and only all of the `keys` provided. + * Since Sets and Maps can have objects as keys you can use this assertion to perform + * a deep comparison. + * You can also provide a single object instead of a `keys` array and its keys + * will be used as the expected set of keys. + * + * assert.hasAllDeepKeys(new Map([[{one: 'one'}, 'valueOne']]), {one: 'one'}); + * assert.hasAllDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [{two: 'two'}, 'valueTwo']]), [{one: 'one'}, {two: 'two'}]); + * assert.hasAllDeepKeys(new Set([{one: 'one'}]), {one: 'one'}); + * assert.hasAllDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), [{one: 'one'}, {two: 'two'}]); + * + * @name hasAllDeepKeys + * @param {Mixed} object + * @param {Array|Object} keys + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.hasAllDeepKeys = function (obj, keys, msg) { + new Assertion(obj, msg, assert.hasAllDeepKeys, true) + .to.have.all.deep.keys(keys); + } + + /** + * ### .containsAllDeepKeys(object, [keys], [message]) + * + * Asserts that `object` contains all of the `keys` provided. + * Since Sets and Maps can have objects as keys you can use this assertion to perform + * a deep comparison. + * You can also provide a single object instead of a `keys` array and its keys + * will be used as the expected set of keys. + * + * assert.containsAllDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [1, 2]]), {one: 'one'}); + * assert.containsAllDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [{two: 'two'}, 'valueTwo']]), [{one: 'one'}, {two: 'two'}]); + * assert.containsAllDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), {one: 'one'}); + * assert.containsAllDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), [{one: 'one'}, {two: 'two'}]); + * + * @name containsAllDeepKeys + * @param {Mixed} object + * @param {Array|Object} keys + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.containsAllDeepKeys = function (obj, keys, msg) { + new Assertion(obj, msg, assert.containsAllDeepKeys, true) + .to.contain.all.deep.keys(keys); + } + + /** + * ### .doesNotHaveAnyDeepKeys(object, [keys], [message]) + * + * Asserts that `object` has none of the `keys` provided. + * Since Sets and Maps can have objects as keys you can use this assertion to perform + * a deep comparison. + * You can also provide a single object instead of a `keys` array and its keys + * will be used as the expected set of keys. + * + * assert.doesNotHaveAnyDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [1, 2]]), {thisDoesNot: 'exist'}); + * assert.doesNotHaveAnyDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [{two: 'two'}, 'valueTwo']]), [{twenty: 'twenty'}, {fifty: 'fifty'}]); + * assert.doesNotHaveAnyDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), {twenty: 'twenty'}); + * assert.doesNotHaveAnyDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), [{twenty: 'twenty'}, {fifty: 'fifty'}]); + * + * @name doesNotHaveAnyDeepKeys + * @param {Mixed} object + * @param {Array|Object} keys + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.doesNotHaveAnyDeepKeys = function (obj, keys, msg) { + new Assertion(obj, msg, assert.doesNotHaveAnyDeepKeys, true) + .to.not.have.any.deep.keys(keys); + } + + /** + * ### .doesNotHaveAllDeepKeys(object, [keys], [message]) + * + * Asserts that `object` does not have at least one of the `keys` provided. + * Since Sets and Maps can have objects as keys you can use this assertion to perform + * a deep comparison. + * You can also provide a single object instead of a `keys` array and its keys + * will be used as the expected set of keys. + * + * assert.doesNotHaveAllDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [1, 2]]), {thisDoesNot: 'exist'}); + * assert.doesNotHaveAllDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [{two: 'two'}, 'valueTwo']]), [{twenty: 'twenty'}, {one: 'one'}]); + * assert.doesNotHaveAllDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), {twenty: 'twenty'}); + * assert.doesNotHaveAllDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), [{one: 'one'}, {fifty: 'fifty'}]); + * + * @name doesNotHaveAllDeepKeys + * @param {Mixed} object + * @param {Array|Object} keys + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.doesNotHaveAllDeepKeys = function (obj, keys, msg) { + new Assertion(obj, msg, assert.doesNotHaveAllDeepKeys, true) + .to.not.have.all.deep.keys(keys); + } + + /** + * ### .throws(fn, [errorLike/string/regexp], [string/regexp], [message]) + * + * If `errorLike` is an `Error` constructor, asserts that `fn` will throw an error that is an + * instance of `errorLike`. + * If `errorLike` is an `Error` instance, asserts that the error thrown is the same + * instance as `errorLike`. + * If `errMsgMatcher` is provided, it also asserts that the error thrown will have a + * message matching `errMsgMatcher`. + * + * assert.throws(fn, 'Error thrown must have this msg'); + * assert.throws(fn, /Error thrown must have a msg that matches this/); + * assert.throws(fn, ReferenceError); + * assert.throws(fn, errorInstance); + * assert.throws(fn, ReferenceError, 'Error thrown must be a ReferenceError and have this msg'); + * assert.throws(fn, errorInstance, 'Error thrown must be the same errorInstance and have this msg'); + * assert.throws(fn, ReferenceError, /Error thrown must be a ReferenceError and match this/); + * assert.throws(fn, errorInstance, /Error thrown must be the same errorInstance and match this/); + * + * @name throws + * @alias throw + * @alias Throw + * @param {Function} fn + * @param {ErrorConstructor|Error} errorLike + * @param {RegExp|String} errMsgMatcher + * @param {String} message + * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types + * @namespace Assert + * @api public + */ + + assert.throws = function (fn, errorLike, errMsgMatcher, msg) { + if ('string' === typeof errorLike || errorLike instanceof RegExp) { + errMsgMatcher = errorLike; + errorLike = null; + } + + var assertErr = new Assertion(fn, msg, assert.throws, true) + .to.throw(errorLike, errMsgMatcher); + return flag(assertErr, 'object'); + }; + + /** + * ### .doesNotThrow(fn, [errorLike/string/regexp], [string/regexp], [message]) + * + * If `errorLike` is an `Error` constructor, asserts that `fn` will _not_ throw an error that is an + * instance of `errorLike`. + * If `errorLike` is an `Error` instance, asserts that the error thrown is _not_ the same + * instance as `errorLike`. + * If `errMsgMatcher` is provided, it also asserts that the error thrown will _not_ have a + * message matching `errMsgMatcher`. + * + * assert.doesNotThrow(fn, 'Any Error thrown must not have this message'); + * assert.doesNotThrow(fn, /Any Error thrown must not match this/); + * assert.doesNotThrow(fn, Error); + * assert.doesNotThrow(fn, errorInstance); + * assert.doesNotThrow(fn, Error, 'Error must not have this message'); + * assert.doesNotThrow(fn, errorInstance, 'Error must not have this message'); + * assert.doesNotThrow(fn, Error, /Error must not match this/); + * assert.doesNotThrow(fn, errorInstance, /Error must not match this/); + * + * @name doesNotThrow + * @param {Function} fn + * @param {ErrorConstructor} errorLike + * @param {RegExp|String} errMsgMatcher + * @param {String} message + * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types + * @namespace Assert + * @api public + */ + + assert.doesNotThrow = function (fn, errorLike, errMsgMatcher, msg) { + if ('string' === typeof errorLike || errorLike instanceof RegExp) { + errMsgMatcher = errorLike; + errorLike = null; + } + + new Assertion(fn, msg, assert.doesNotThrow, true) + .to.not.throw(errorLike, errMsgMatcher); + }; + + /** + * ### .operator(val1, operator, val2, [message]) + * + * Compares two values using `operator`. + * + * assert.operator(1, '<', 2, 'everything is ok'); + * assert.operator(1, '>', 2, 'this will fail'); + * + * @name operator + * @param {Mixed} val1 + * @param {String} operator + * @param {Mixed} val2 + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.operator = function (val, operator, val2, msg) { + var ok; + switch(operator) { + case '==': + ok = val == val2; + break; + case '===': + ok = val === val2; + break; + case '>': + ok = val > val2; + break; + case '>=': + ok = val >= val2; + break; + case '<': + ok = val < val2; + break; + case '<=': + ok = val <= val2; + break; + case '!=': + ok = val != val2; + break; + case '!==': + ok = val !== val2; + break; + default: + msg = msg ? msg + ': ' : msg; + throw new chai.AssertionError( + msg + 'Invalid operator "' + operator + '"', + undefined, + assert.operator + ); + } + var test = new Assertion(ok, msg, assert.operator, true); + test.assert( + true === flag(test, 'object') + , 'expected ' + util.inspect(val) + ' to be ' + operator + ' ' + util.inspect(val2) + , 'expected ' + util.inspect(val) + ' to not be ' + operator + ' ' + util.inspect(val2) ); + }; + + /** + * ### .closeTo(actual, expected, delta, [message]) + * + * Asserts that the target is equal `expected`, to within a +/- `delta` range. + * + * assert.closeTo(1.5, 1, 0.5, 'numbers are close'); + * + * @name closeTo + * @param {Number} actual + * @param {Number} expected + * @param {Number} delta + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.closeTo = function (act, exp, delta, msg) { + new Assertion(act, msg, assert.closeTo, true).to.be.closeTo(exp, delta); + }; + + /** + * ### .approximately(actual, expected, delta, [message]) + * + * Asserts that the target is equal `expected`, to within a +/- `delta` range. + * + * assert.approximately(1.5, 1, 0.5, 'numbers are close'); + * + * @name approximately + * @param {Number} actual + * @param {Number} expected + * @param {Number} delta + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.approximately = function (act, exp, delta, msg) { + new Assertion(act, msg, assert.approximately, true) + .to.be.approximately(exp, delta); + }; + + /** + * ### .sameMembers(set1, set2, [message]) + * + * Asserts that `set1` and `set2` have the same members in any order. Uses a + * strict equality check (===). + * + * assert.sameMembers([ 1, 2, 3 ], [ 2, 1, 3 ], 'same members'); + * + * @name sameMembers + * @param {Array} set1 + * @param {Array} set2 + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.sameMembers = function (set1, set2, msg) { + new Assertion(set1, msg, assert.sameMembers, true) + .to.have.same.members(set2); + } + + /** + * ### .notSameMembers(set1, set2, [message]) + * + * Asserts that `set1` and `set2` don't have the same members in any order. + * Uses a strict equality check (===). + * + * assert.notSameMembers([ 1, 2, 3 ], [ 5, 1, 3 ], 'not same members'); + * + * @name notSameMembers + * @param {Array} set1 + * @param {Array} set2 + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notSameMembers = function (set1, set2, msg) { + new Assertion(set1, msg, assert.notSameMembers, true) + .to.not.have.same.members(set2); + } + + /** + * ### .sameDeepMembers(set1, set2, [message]) + * + * Asserts that `set1` and `set2` have the same members in any order. Uses a + * deep equality check. + * + * assert.sameDeepMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [{ b: 2 }, { a: 1 }, { c: 3 }], 'same deep members'); + * + * @name sameDeepMembers + * @param {Array} set1 + * @param {Array} set2 + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.sameDeepMembers = function (set1, set2, msg) { + new Assertion(set1, msg, assert.sameDeepMembers, true) + .to.have.same.deep.members(set2); + } + + /** + * ### .notSameDeepMembers(set1, set2, [message]) + * + * Asserts that `set1` and `set2` don't have the same members in any order. + * Uses a deep equality check. + * + * assert.notSameDeepMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [{ b: 2 }, { a: 1 }, { f: 5 }], 'not same deep members'); + * + * @name notSameDeepMembers + * @param {Array} set1 + * @param {Array} set2 + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notSameDeepMembers = function (set1, set2, msg) { + new Assertion(set1, msg, assert.notSameDeepMembers, true) + .to.not.have.same.deep.members(set2); + } + + /** + * ### .sameOrderedMembers(set1, set2, [message]) + * + * Asserts that `set1` and `set2` have the same members in the same order. + * Uses a strict equality check (===). + * + * assert.sameOrderedMembers([ 1, 2, 3 ], [ 1, 2, 3 ], 'same ordered members'); + * + * @name sameOrderedMembers + * @param {Array} set1 + * @param {Array} set2 + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.sameOrderedMembers = function (set1, set2, msg) { + new Assertion(set1, msg, assert.sameOrderedMembers, true) + .to.have.same.ordered.members(set2); + } + + /** + * ### .notSameOrderedMembers(set1, set2, [message]) + * + * Asserts that `set1` and `set2` don't have the same members in the same + * order. Uses a strict equality check (===). + * + * assert.notSameOrderedMembers([ 1, 2, 3 ], [ 2, 1, 3 ], 'not same ordered members'); + * + * @name notSameOrderedMembers + * @param {Array} set1 + * @param {Array} set2 + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notSameOrderedMembers = function (set1, set2, msg) { + new Assertion(set1, msg, assert.notSameOrderedMembers, true) + .to.not.have.same.ordered.members(set2); + } + + /** + * ### .sameDeepOrderedMembers(set1, set2, [message]) + * + * Asserts that `set1` and `set2` have the same members in the same order. + * Uses a deep equality check. + * + * assert.sameDeepOrderedMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { a: 1 }, { b: 2 }, { c: 3 } ], 'same deep ordered members'); + * + * @name sameDeepOrderedMembers + * @param {Array} set1 + * @param {Array} set2 + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.sameDeepOrderedMembers = function (set1, set2, msg) { + new Assertion(set1, msg, assert.sameDeepOrderedMembers, true) + .to.have.same.deep.ordered.members(set2); + } + + /** + * ### .notSameDeepOrderedMembers(set1, set2, [message]) + * + * Asserts that `set1` and `set2` don't have the same members in the same + * order. Uses a deep equality check. + * + * assert.notSameDeepOrderedMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { a: 1 }, { b: 2 }, { z: 5 } ], 'not same deep ordered members'); + * assert.notSameDeepOrderedMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { b: 2 }, { a: 1 }, { c: 3 } ], 'not same deep ordered members'); + * + * @name notSameDeepOrderedMembers + * @param {Array} set1 + * @param {Array} set2 + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notSameDeepOrderedMembers = function (set1, set2, msg) { + new Assertion(set1, msg, assert.notSameDeepOrderedMembers, true) + .to.not.have.same.deep.ordered.members(set2); + } + + /** + * ### .includeMembers(superset, subset, [message]) + * + * Asserts that `subset` is included in `superset` in any order. Uses a + * strict equality check (===). Duplicates are ignored. + * + * assert.includeMembers([ 1, 2, 3 ], [ 2, 1, 2 ], 'include members'); + * + * @name includeMembers + * @param {Array} superset + * @param {Array} subset + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.includeMembers = function (superset, subset, msg) { + new Assertion(superset, msg, assert.includeMembers, true) + .to.include.members(subset); + } + + /** + * ### .notIncludeMembers(superset, subset, [message]) + * + * Asserts that `subset` isn't included in `superset` in any order. Uses a + * strict equality check (===). Duplicates are ignored. + * + * assert.notIncludeMembers([ 1, 2, 3 ], [ 5, 1 ], 'not include members'); + * + * @name notIncludeMembers + * @param {Array} superset + * @param {Array} subset + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notIncludeMembers = function (superset, subset, msg) { + new Assertion(superset, msg, assert.notIncludeMembers, true) + .to.not.include.members(subset); + } + + /** + * ### .includeDeepMembers(superset, subset, [message]) + * + * Asserts that `subset` is included in `superset` in any order. Uses a deep + * equality check. Duplicates are ignored. + * + * assert.includeDeepMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { b: 2 }, { a: 1 }, { b: 2 } ], 'include deep members'); + * + * @name includeDeepMembers + * @param {Array} superset + * @param {Array} subset + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.includeDeepMembers = function (superset, subset, msg) { + new Assertion(superset, msg, assert.includeDeepMembers, true) + .to.include.deep.members(subset); + } + + /** + * ### .notIncludeDeepMembers(superset, subset, [message]) + * + * Asserts that `subset` isn't included in `superset` in any order. Uses a + * deep equality check. Duplicates are ignored. + * + * assert.notIncludeDeepMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { b: 2 }, { f: 5 } ], 'not include deep members'); + * + * @name notIncludeDeepMembers + * @param {Array} superset + * @param {Array} subset + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notIncludeDeepMembers = function (superset, subset, msg) { + new Assertion(superset, msg, assert.notIncludeDeepMembers, true) + .to.not.include.deep.members(subset); + } + + /** + * ### .includeOrderedMembers(superset, subset, [message]) + * + * Asserts that `subset` is included in `superset` in the same order + * beginning with the first element in `superset`. Uses a strict equality + * check (===). + * + * assert.includeOrderedMembers([ 1, 2, 3 ], [ 1, 2 ], 'include ordered members'); + * + * @name includeOrderedMembers + * @param {Array} superset + * @param {Array} subset + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.includeOrderedMembers = function (superset, subset, msg) { + new Assertion(superset, msg, assert.includeOrderedMembers, true) + .to.include.ordered.members(subset); + } + + /** + * ### .notIncludeOrderedMembers(superset, subset, [message]) + * + * Asserts that `subset` isn't included in `superset` in the same order + * beginning with the first element in `superset`. Uses a strict equality + * check (===). + * + * assert.notIncludeOrderedMembers([ 1, 2, 3 ], [ 2, 1 ], 'not include ordered members'); + * assert.notIncludeOrderedMembers([ 1, 2, 3 ], [ 2, 3 ], 'not include ordered members'); + * + * @name notIncludeOrderedMembers + * @param {Array} superset + * @param {Array} subset + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notIncludeOrderedMembers = function (superset, subset, msg) { + new Assertion(superset, msg, assert.notIncludeOrderedMembers, true) + .to.not.include.ordered.members(subset); + } + + /** + * ### .includeDeepOrderedMembers(superset, subset, [message]) + * + * Asserts that `subset` is included in `superset` in the same order + * beginning with the first element in `superset`. Uses a deep equality + * check. + * + * assert.includeDeepOrderedMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { a: 1 }, { b: 2 } ], 'include deep ordered members'); + * + * @name includeDeepOrderedMembers + * @param {Array} superset + * @param {Array} subset + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.includeDeepOrderedMembers = function (superset, subset, msg) { + new Assertion(superset, msg, assert.includeDeepOrderedMembers, true) + .to.include.deep.ordered.members(subset); + } + + /** + * ### .notIncludeDeepOrderedMembers(superset, subset, [message]) + * + * Asserts that `subset` isn't included in `superset` in the same order + * beginning with the first element in `superset`. Uses a deep equality + * check. + * + * assert.notIncludeDeepOrderedMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { a: 1 }, { f: 5 } ], 'not include deep ordered members'); + * assert.notIncludeDeepOrderedMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { b: 2 }, { a: 1 } ], 'not include deep ordered members'); + * assert.notIncludeDeepOrderedMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { b: 2 }, { c: 3 } ], 'not include deep ordered members'); + * + * @name notIncludeDeepOrderedMembers + * @param {Array} superset + * @param {Array} subset + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.notIncludeDeepOrderedMembers = function (superset, subset, msg) { + new Assertion(superset, msg, assert.notIncludeDeepOrderedMembers, true) + .to.not.include.deep.ordered.members(subset); + } + + /** + * ### .oneOf(inList, list, [message]) + * + * Asserts that non-object, non-array value `inList` appears in the flat array `list`. + * + * assert.oneOf(1, [ 2, 1 ], 'Not found in list'); + * + * @name oneOf + * @param {*} inList + * @param {Array<*>} list + * @param {String} message + * @namespace Assert + * @api public + */ + + assert.oneOf = function (inList, list, msg) { + new Assertion(inList, msg, assert.oneOf, true).to.be.oneOf(list); + } + + /** + * ### .changes(function, object, property, [message]) + * + * Asserts that a function changes the value of a property. + * + * var obj = { val: 10 }; + * var fn = function() { obj.val = 22 }; + * assert.changes(fn, obj, 'val'); + * + * @name changes + * @param {Function} modifier function + * @param {Object} object or getter function + * @param {String} property name _optional_ + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.changes = function (fn, obj, prop, msg) { + if (arguments.length === 3 && typeof obj === 'function') { + msg = prop; + prop = null; + } + + new Assertion(fn, msg, assert.changes, true).to.change(obj, prop); + } + + /** + * ### .changesBy(function, object, property, delta, [message]) + * + * Asserts that a function changes the value of a property by an amount (delta). + * + * var obj = { val: 10 }; + * var fn = function() { obj.val += 2 }; + * assert.changesBy(fn, obj, 'val', 2); + * + * @name changesBy + * @param {Function} modifier function + * @param {Object} object or getter function + * @param {String} property name _optional_ + * @param {Number} change amount (delta) + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.changesBy = function (fn, obj, prop, delta, msg) { + if (arguments.length === 4 && typeof obj === 'function') { + var tmpMsg = delta; + delta = prop; + msg = tmpMsg; + } else if (arguments.length === 3) { + delta = prop; + prop = null; + } + + new Assertion(fn, msg, assert.changesBy, true) + .to.change(obj, prop).by(delta); + } + + /** + * ### .doesNotChange(function, object, property, [message]) + * + * Asserts that a function does not change the value of a property. + * + * var obj = { val: 10 }; + * var fn = function() { console.log('foo'); }; + * assert.doesNotChange(fn, obj, 'val'); + * + * @name doesNotChange + * @param {Function} modifier function + * @param {Object} object or getter function + * @param {String} property name _optional_ + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.doesNotChange = function (fn, obj, prop, msg) { + if (arguments.length === 3 && typeof obj === 'function') { + msg = prop; + prop = null; + } + + return new Assertion(fn, msg, assert.doesNotChange, true) + .to.not.change(obj, prop); + } + + /** + * ### .changesButNotBy(function, object, property, delta, [message]) + * + * Asserts that a function does not change the value of a property or of a function's return value by an amount (delta) + * + * var obj = { val: 10 }; + * var fn = function() { obj.val += 10 }; + * assert.changesButNotBy(fn, obj, 'val', 5); + * + * @name changesButNotBy + * @param {Function} modifier function + * @param {Object} object or getter function + * @param {String} property name _optional_ + * @param {Number} change amount (delta) + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.changesButNotBy = function (fn, obj, prop, delta, msg) { + if (arguments.length === 4 && typeof obj === 'function') { + var tmpMsg = delta; + delta = prop; + msg = tmpMsg; + } else if (arguments.length === 3) { + delta = prop; + prop = null; + } + + new Assertion(fn, msg, assert.changesButNotBy, true) + .to.change(obj, prop).but.not.by(delta); + } + + /** + * ### .increases(function, object, property, [message]) + * + * Asserts that a function increases a numeric object property. + * + * var obj = { val: 10 }; + * var fn = function() { obj.val = 13 }; + * assert.increases(fn, obj, 'val'); + * + * @name increases + * @param {Function} modifier function + * @param {Object} object or getter function + * @param {String} property name _optional_ + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.increases = function (fn, obj, prop, msg) { + if (arguments.length === 3 && typeof obj === 'function') { + msg = prop; + prop = null; + } + + return new Assertion(fn, msg, assert.increases, true) + .to.increase(obj, prop); + } + + /** + * ### .increasesBy(function, object, property, delta, [message]) + * + * Asserts that a function increases a numeric object property or a function's return value by an amount (delta). + * + * var obj = { val: 10 }; + * var fn = function() { obj.val += 10 }; + * assert.increasesBy(fn, obj, 'val', 10); + * + * @name increasesBy + * @param {Function} modifier function + * @param {Object} object or getter function + * @param {String} property name _optional_ + * @param {Number} change amount (delta) + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.increasesBy = function (fn, obj, prop, delta, msg) { + if (arguments.length === 4 && typeof obj === 'function') { + var tmpMsg = delta; + delta = prop; + msg = tmpMsg; + } else if (arguments.length === 3) { + delta = prop; + prop = null; + } + + new Assertion(fn, msg, assert.increasesBy, true) + .to.increase(obj, prop).by(delta); + } + + /** + * ### .doesNotIncrease(function, object, property, [message]) + * + * Asserts that a function does not increase a numeric object property. + * + * var obj = { val: 10 }; + * var fn = function() { obj.val = 8 }; + * assert.doesNotIncrease(fn, obj, 'val'); + * + * @name doesNotIncrease + * @param {Function} modifier function + * @param {Object} object or getter function + * @param {String} property name _optional_ + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.doesNotIncrease = function (fn, obj, prop, msg) { + if (arguments.length === 3 && typeof obj === 'function') { + msg = prop; + prop = null; + } + + return new Assertion(fn, msg, assert.doesNotIncrease, true) + .to.not.increase(obj, prop); + } + + /** + * ### .increasesButNotBy(function, object, property, [message]) + * + * Asserts that a function does not increase a numeric object property or function's return value by an amount (delta). + * + * var obj = { val: 10 }; + * var fn = function() { obj.val = 15 }; + * assert.increasesButNotBy(fn, obj, 'val', 10); + * + * @name increasesButNotBy + * @param {Function} modifier function + * @param {Object} object or getter function + * @param {String} property name _optional_ + * @param {Number} change amount (delta) + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.increasesButNotBy = function (fn, obj, prop, delta, msg) { + if (arguments.length === 4 && typeof obj === 'function') { + var tmpMsg = delta; + delta = prop; + msg = tmpMsg; + } else if (arguments.length === 3) { + delta = prop; + prop = null; + } + + new Assertion(fn, msg, assert.increasesButNotBy, true) + .to.increase(obj, prop).but.not.by(delta); + } + + /** + * ### .decreases(function, object, property, [message]) + * + * Asserts that a function decreases a numeric object property. + * + * var obj = { val: 10 }; + * var fn = function() { obj.val = 5 }; + * assert.decreases(fn, obj, 'val'); + * + * @name decreases + * @param {Function} modifier function + * @param {Object} object or getter function + * @param {String} property name _optional_ + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.decreases = function (fn, obj, prop, msg) { + if (arguments.length === 3 && typeof obj === 'function') { + msg = prop; + prop = null; + } + + return new Assertion(fn, msg, assert.decreases, true) + .to.decrease(obj, prop); + } + + /** + * ### .decreasesBy(function, object, property, delta, [message]) + * + * Asserts that a function decreases a numeric object property or a function's return value by an amount (delta) + * + * var obj = { val: 10 }; + * var fn = function() { obj.val -= 5 }; + * assert.decreasesBy(fn, obj, 'val', 5); + * + * @name decreasesBy + * @param {Function} modifier function + * @param {Object} object or getter function + * @param {String} property name _optional_ + * @param {Number} change amount (delta) + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.decreasesBy = function (fn, obj, prop, delta, msg) { + if (arguments.length === 4 && typeof obj === 'function') { + var tmpMsg = delta; + delta = prop; + msg = tmpMsg; + } else if (arguments.length === 3) { + delta = prop; + prop = null; + } + + new Assertion(fn, msg, assert.decreasesBy, true) + .to.decrease(obj, prop).by(delta); + } + + /** + * ### .doesNotDecrease(function, object, property, [message]) + * + * Asserts that a function does not decreases a numeric object property. + * + * var obj = { val: 10 }; + * var fn = function() { obj.val = 15 }; + * assert.doesNotDecrease(fn, obj, 'val'); + * + * @name doesNotDecrease + * @param {Function} modifier function + * @param {Object} object or getter function + * @param {String} property name _optional_ + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.doesNotDecrease = function (fn, obj, prop, msg) { + if (arguments.length === 3 && typeof obj === 'function') { + msg = prop; + prop = null; + } + + return new Assertion(fn, msg, assert.doesNotDecrease, true) + .to.not.decrease(obj, prop); + } + + /** + * ### .doesNotDecreaseBy(function, object, property, delta, [message]) + * + * Asserts that a function does not decreases a numeric object property or a function's return value by an amount (delta) + * + * var obj = { val: 10 }; + * var fn = function() { obj.val = 5 }; + * assert.doesNotDecreaseBy(fn, obj, 'val', 1); + * + * @name doesNotDecrease + * @param {Function} modifier function + * @param {Object} object or getter function + * @param {String} property name _optional_ + * @param {Number} change amount (delta) + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.doesNotDecreaseBy = function (fn, obj, prop, delta, msg) { + if (arguments.length === 4 && typeof obj === 'function') { + var tmpMsg = delta; + delta = prop; + msg = tmpMsg; + } else if (arguments.length === 3) { + delta = prop; + prop = null; + } + + return new Assertion(fn, msg, assert.doesNotDecreaseBy, true) + .to.not.decrease(obj, prop).by(delta); + } + + /** + * ### .decreasesButNotBy(function, object, property, delta, [message]) + * + * Asserts that a function does not decreases a numeric object property or a function's return value by an amount (delta) + * + * var obj = { val: 10 }; + * var fn = function() { obj.val = 5 }; + * assert.decreasesButNotBy(fn, obj, 'val', 1); + * + * @name decreasesButNotBy + * @param {Function} modifier function + * @param {Object} object or getter function + * @param {String} property name _optional_ + * @param {Number} change amount (delta) + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.decreasesButNotBy = function (fn, obj, prop, delta, msg) { + if (arguments.length === 4 && typeof obj === 'function') { + var tmpMsg = delta; + delta = prop; + msg = tmpMsg; + } else if (arguments.length === 3) { + delta = prop; + prop = null; + } + + new Assertion(fn, msg, assert.decreasesButNotBy, true) + .to.decrease(obj, prop).but.not.by(delta); + } + + /*! + * ### .ifError(object) + * + * Asserts if value is not a false value, and throws if it is a true value. + * This is added to allow for chai to be a drop-in replacement for Node's + * assert class. + * + * var err = new Error('I am a custom error'); + * assert.ifError(err); // Rethrows err! + * + * @name ifError + * @param {Object} object + * @namespace Assert + * @api public + */ + + assert.ifError = function (val) { + if (val) { + throw(val); + } + }; + + /** + * ### .isExtensible(object) + * + * Asserts that `object` is extensible (can have new properties added to it). + * + * assert.isExtensible({}); + * + * @name isExtensible + * @alias extensible + * @param {Object} object + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.isExtensible = function (obj, msg) { + new Assertion(obj, msg, assert.isExtensible, true).to.be.extensible; + }; + + /** + * ### .isNotExtensible(object) + * + * Asserts that `object` is _not_ extensible. + * + * var nonExtensibleObject = Object.preventExtensions({}); + * var sealedObject = Object.seal({}); + * var frozenObject = Object.freeze({}); + * + * assert.isNotExtensible(nonExtensibleObject); + * assert.isNotExtensible(sealedObject); + * assert.isNotExtensible(frozenObject); + * + * @name isNotExtensible + * @alias notExtensible + * @param {Object} object + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.isNotExtensible = function (obj, msg) { + new Assertion(obj, msg, assert.isNotExtensible, true).to.not.be.extensible; + }; + + /** + * ### .isSealed(object) + * + * Asserts that `object` is sealed (cannot have new properties added to it + * and its existing properties cannot be removed). + * + * var sealedObject = Object.seal({}); + * var frozenObject = Object.seal({}); + * + * assert.isSealed(sealedObject); + * assert.isSealed(frozenObject); + * + * @name isSealed + * @alias sealed + * @param {Object} object + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.isSealed = function (obj, msg) { + new Assertion(obj, msg, assert.isSealed, true).to.be.sealed; + }; + + /** + * ### .isNotSealed(object) + * + * Asserts that `object` is _not_ sealed. + * + * assert.isNotSealed({}); + * + * @name isNotSealed + * @alias notSealed + * @param {Object} object + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.isNotSealed = function (obj, msg) { + new Assertion(obj, msg, assert.isNotSealed, true).to.not.be.sealed; + }; + + /** + * ### .isFrozen(object) + * + * Asserts that `object` is frozen (cannot have new properties added to it + * and its existing properties cannot be modified). + * + * var frozenObject = Object.freeze({}); + * assert.frozen(frozenObject); + * + * @name isFrozen + * @alias frozen + * @param {Object} object + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.isFrozen = function (obj, msg) { + new Assertion(obj, msg, assert.isFrozen, true).to.be.frozen; + }; + + /** + * ### .isNotFrozen(object) + * + * Asserts that `object` is _not_ frozen. + * + * assert.isNotFrozen({}); + * + * @name isNotFrozen + * @alias notFrozen + * @param {Object} object + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.isNotFrozen = function (obj, msg) { + new Assertion(obj, msg, assert.isNotFrozen, true).to.not.be.frozen; + }; + + /** + * ### .isEmpty(target) + * + * Asserts that the target does not contain any values. + * For arrays and strings, it checks the `length` property. + * For `Map` and `Set` instances, it checks the `size` property. + * For non-function objects, it gets the count of own + * enumerable string keys. + * + * assert.isEmpty([]); + * assert.isEmpty(''); + * assert.isEmpty(new Map); + * assert.isEmpty({}); + * + * @name isEmpty + * @alias empty + * @param {Object|Array|String|Map|Set} target + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.isEmpty = function(val, msg) { + new Assertion(val, msg, assert.isEmpty, true).to.be.empty; + }; + + /** + * ### .isNotEmpty(target) + * + * Asserts that the target contains values. + * For arrays and strings, it checks the `length` property. + * For `Map` and `Set` instances, it checks the `size` property. + * For non-function objects, it gets the count of own + * enumerable string keys. + * + * assert.isNotEmpty([1, 2]); + * assert.isNotEmpty('34'); + * assert.isNotEmpty(new Set([5, 6])); + * assert.isNotEmpty({ key: 7 }); + * + * @name isNotEmpty + * @alias notEmpty + * @param {Object|Array|String|Map|Set} target + * @param {String} message _optional_ + * @namespace Assert + * @api public + */ + + assert.isNotEmpty = function(val, msg) { + new Assertion(val, msg, assert.isNotEmpty, true).to.not.be.empty; + }; + + /*! + * Aliases. + */ + + (function alias(name, as){ + assert[as] = assert[name]; + return alias; + }) + ('isOk', 'ok') + ('isNotOk', 'notOk') + ('throws', 'throw') + ('throws', 'Throw') + ('isExtensible', 'extensible') + ('isNotExtensible', 'notExtensible') + ('isSealed', 'sealed') + ('isNotSealed', 'notSealed') + ('isFrozen', 'frozen') + ('isNotFrozen', 'notFrozen') + ('isEmpty', 'empty') + ('isNotEmpty', 'notEmpty'); + }; + + },{}],7:[function(require,module,exports){ + /*! + * chai + * Copyright(c) 2011-2014 Jake Luer + * MIT Licensed + */ + + module.exports = function (chai, util) { + chai.expect = function (val, message) { + return new chai.Assertion(val, message); + }; + + /** + * ### .fail([message]) + * ### .fail(actual, expected, [message], [operator]) + * + * Throw a failure. + * + * expect.fail(); + * expect.fail("custom error message"); + * expect.fail(1, 2); + * expect.fail(1, 2, "custom error message"); + * expect.fail(1, 2, "custom error message", ">"); + * expect.fail(1, 2, undefined, ">"); + * + * @name fail + * @param {Mixed} actual + * @param {Mixed} expected + * @param {String} message + * @param {String} operator + * @namespace BDD + * @api public + */ + + chai.expect.fail = function (actual, expected, message, operator) { + if (arguments.length < 2) { + message = actual; + actual = undefined; + } + + message = message || 'expect.fail()'; + throw new chai.AssertionError(message, { + actual: actual + , expected: expected + , operator: operator + }, chai.expect.fail); + }; + }; + + },{}],8:[function(require,module,exports){ + /*! + * chai + * Copyright(c) 2011-2014 Jake Luer + * MIT Licensed + */ + + module.exports = function (chai, util) { + var Assertion = chai.Assertion; + + function loadShould () { + // explicitly define this method as function as to have it's name to include as `ssfi` + function shouldGetter() { + if (this instanceof String + || this instanceof Number + || this instanceof Boolean + || typeof Symbol === 'function' && this instanceof Symbol) { + return new Assertion(this.valueOf(), null, shouldGetter); + } + return new Assertion(this, null, shouldGetter); + } + function shouldSetter(value) { + // See https://github.com/chaijs/chai/issues/86: this makes + // `whatever.should = someValue` actually set `someValue`, which is + // especially useful for `global.should = require('chai').should()`. + // + // Note that we have to use [[DefineProperty]] instead of [[Put]] + // since otherwise we would trigger this very setter! + Object.defineProperty(this, 'should', { + value: value, + enumerable: true, + configurable: true, + writable: true + }); + } + // modify Object.prototype to have `should` + Object.defineProperty(Object.prototype, 'should', { + set: shouldSetter + , get: shouldGetter + , configurable: true + }); + + var should = {}; + + /** + * ### .fail([message]) + * ### .fail(actual, expected, [message], [operator]) + * + * Throw a failure. + * + * should.fail(); + * should.fail("custom error message"); + * should.fail(1, 2); + * should.fail(1, 2, "custom error message"); + * should.fail(1, 2, "custom error message", ">"); + * should.fail(1, 2, undefined, ">"); + * + * + * @name fail + * @param {Mixed} actual + * @param {Mixed} expected + * @param {String} message + * @param {String} operator + * @namespace BDD + * @api public + */ + + should.fail = function (actual, expected, message, operator) { + if (arguments.length < 2) { + message = actual; + actual = undefined; + } + + message = message || 'should.fail()'; + throw new chai.AssertionError(message, { + actual: actual + , expected: expected + , operator: operator + }, should.fail); + }; + + /** + * ### .equal(actual, expected, [message]) + * + * Asserts non-strict equality (`==`) of `actual` and `expected`. + * + * should.equal(3, '3', '== coerces values to strings'); + * + * @name equal + * @param {Mixed} actual + * @param {Mixed} expected + * @param {String} message + * @namespace Should + * @api public + */ + + should.equal = function (val1, val2, msg) { + new Assertion(val1, msg).to.equal(val2); + }; + + /** + * ### .throw(function, [constructor/string/regexp], [string/regexp], [message]) + * + * Asserts that `function` will throw an error that is an instance of + * `constructor`, or alternately that it will throw an error with message + * matching `regexp`. + * + * should.throw(fn, 'function throws a reference error'); + * should.throw(fn, /function throws a reference error/); + * should.throw(fn, ReferenceError); + * should.throw(fn, ReferenceError, 'function throws a reference error'); + * should.throw(fn, ReferenceError, /function throws a reference error/); + * + * @name throw + * @alias Throw + * @param {Function} function + * @param {ErrorConstructor} constructor + * @param {RegExp} regexp + * @param {String} message + * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types + * @namespace Should + * @api public + */ + + should.Throw = function (fn, errt, errs, msg) { + new Assertion(fn, msg).to.Throw(errt, errs); + }; + + /** + * ### .exist + * + * Asserts that the target is neither `null` nor `undefined`. + * + * var foo = 'hi'; + * + * should.exist(foo, 'foo exists'); + * + * @name exist + * @namespace Should + * @api public + */ + + should.exist = function (val, msg) { + new Assertion(val, msg).to.exist; + } + + // negation + should.not = {} + + /** + * ### .not.equal(actual, expected, [message]) + * + * Asserts non-strict inequality (`!=`) of `actual` and `expected`. + * + * should.not.equal(3, 4, 'these numbers are not equal'); + * + * @name not.equal + * @param {Mixed} actual + * @param {Mixed} expected + * @param {String} message + * @namespace Should + * @api public + */ + + should.not.equal = function (val1, val2, msg) { + new Assertion(val1, msg).to.not.equal(val2); + }; + + /** + * ### .throw(function, [constructor/regexp], [message]) + * + * Asserts that `function` will _not_ throw an error that is an instance of + * `constructor`, or alternately that it will not throw an error with message + * matching `regexp`. + * + * should.not.throw(fn, Error, 'function does not throw'); + * + * @name not.throw + * @alias not.Throw + * @param {Function} function + * @param {ErrorConstructor} constructor + * @param {RegExp} regexp + * @param {String} message + * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types + * @namespace Should + * @api public + */ + + should.not.Throw = function (fn, errt, errs, msg) { + new Assertion(fn, msg).to.not.Throw(errt, errs); + }; + + /** + * ### .not.exist + * + * Asserts that the target is neither `null` nor `undefined`. + * + * var bar = null; + * + * should.not.exist(bar, 'bar does not exist'); + * + * @name not.exist + * @namespace Should + * @api public + */ + + should.not.exist = function (val, msg) { + new Assertion(val, msg).to.not.exist; + } + + should['throw'] = should['Throw']; + should.not['throw'] = should.not['Throw']; + + return should; + }; + + chai.should = loadShould; + chai.Should = loadShould; + }; + + },{}],9:[function(require,module,exports){ + /*! + * Chai - addChainingMethod utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + /*! + * Module dependencies + */ + + var addLengthGuard = require('./addLengthGuard'); + var chai = require('../../chai'); + var flag = require('./flag'); + var proxify = require('./proxify'); + var transferFlags = require('./transferFlags'); + + /*! + * Module variables + */ + + // Check whether `Object.setPrototypeOf` is supported + var canSetPrototype = typeof Object.setPrototypeOf === 'function'; + + // Without `Object.setPrototypeOf` support, this module will need to add properties to a function. + // However, some of functions' own props are not configurable and should be skipped. + var testFn = function() {}; + var excludeNames = Object.getOwnPropertyNames(testFn).filter(function(name) { + var propDesc = Object.getOwnPropertyDescriptor(testFn, name); + + // Note: PhantomJS 1.x includes `callee` as one of `testFn`'s own properties, + // but then returns `undefined` as the property descriptor for `callee`. As a + // workaround, we perform an otherwise unnecessary type-check for `propDesc`, + // and then filter it out if it's not an object as it should be. + if (typeof propDesc !== 'object') + return true; + + return !propDesc.configurable; + }); + + // Cache `Function` properties + var call = Function.prototype.call, + apply = Function.prototype.apply; + + /** + * ### .addChainableMethod(ctx, name, method, chainingBehavior) + * + * Adds a method to an object, such that the method can also be chained. + * + * utils.addChainableMethod(chai.Assertion.prototype, 'foo', function (str) { + * var obj = utils.flag(this, 'object'); + * new chai.Assertion(obj).to.be.equal(str); + * }); + * + * Can also be accessed directly from `chai.Assertion`. + * + * chai.Assertion.addChainableMethod('foo', fn, chainingBehavior); + * + * The result can then be used as both a method assertion, executing both `method` and + * `chainingBehavior`, or as a language chain, which only executes `chainingBehavior`. + * + * expect(fooStr).to.be.foo('bar'); + * expect(fooStr).to.be.foo.equal('foo'); + * + * @param {Object} ctx object to which the method is added + * @param {String} name of method to add + * @param {Function} method function to be used for `name`, when called + * @param {Function} chainingBehavior function to be called every time the property is accessed + * @namespace Utils + * @name addChainableMethod + * @api public + */ + + module.exports = function addChainableMethod(ctx, name, method, chainingBehavior) { + if (typeof chainingBehavior !== 'function') { + chainingBehavior = function () { }; + } + + var chainableBehavior = { + method: method + , chainingBehavior: chainingBehavior + }; + + // save the methods so we can overwrite them later, if we need to. + if (!ctx.__methods) { + ctx.__methods = {}; + } + ctx.__methods[name] = chainableBehavior; + + Object.defineProperty(ctx, name, + { get: function chainableMethodGetter() { + chainableBehavior.chainingBehavior.call(this); + + var chainableMethodWrapper = function () { + // Setting the `ssfi` flag to `chainableMethodWrapper` causes this + // function to be the starting point for removing implementation + // frames from the stack trace of a failed assertion. + // + // However, we only want to use this function as the starting point if + // the `lockSsfi` flag isn't set. + // + // If the `lockSsfi` flag is set, then this assertion is being + // invoked from inside of another assertion. In this case, the `ssfi` + // flag has already been set by the outer assertion. + // + // Note that overwriting a chainable method merely replaces the saved + // methods in `ctx.__methods` instead of completely replacing the + // overwritten assertion. Therefore, an overwriting assertion won't + // set the `ssfi` or `lockSsfi` flags. + if (!flag(this, 'lockSsfi')) { + flag(this, 'ssfi', chainableMethodWrapper); + } + + var result = chainableBehavior.method.apply(this, arguments); + if (result !== undefined) { + return result; + } + + var newAssertion = new chai.Assertion(); + transferFlags(this, newAssertion); + return newAssertion; + }; + + addLengthGuard(chainableMethodWrapper, name, true); + + // Use `Object.setPrototypeOf` if available + if (canSetPrototype) { + // Inherit all properties from the object by replacing the `Function` prototype + var prototype = Object.create(this); + // Restore the `call` and `apply` methods from `Function` + prototype.call = call; + prototype.apply = apply; + Object.setPrototypeOf(chainableMethodWrapper, prototype); + } + // Otherwise, redefine all properties (slow!) + else { + var asserterNames = Object.getOwnPropertyNames(ctx); + asserterNames.forEach(function (asserterName) { + if (excludeNames.indexOf(asserterName) !== -1) { + return; + } + + var pd = Object.getOwnPropertyDescriptor(ctx, asserterName); + Object.defineProperty(chainableMethodWrapper, asserterName, pd); + }); + } + + transferFlags(this, chainableMethodWrapper); + return proxify(chainableMethodWrapper); + } + , configurable: true + }); + }; + + },{"../../chai":2,"./addLengthGuard":10,"./flag":15,"./proxify":30,"./transferFlags":32}],10:[function(require,module,exports){ + var fnLengthDesc = Object.getOwnPropertyDescriptor(function () {}, 'length'); + + /*! + * Chai - addLengthGuard utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + /** + * ### .addLengthGuard(fn, assertionName, isChainable) + * + * Define `length` as a getter on the given uninvoked method assertion. The + * getter acts as a guard against chaining `length` directly off of an uninvoked + * method assertion, which is a problem because it references `function`'s + * built-in `length` property instead of Chai's `length` assertion. When the + * getter catches the user making this mistake, it throws an error with a + * helpful message. + * + * There are two ways in which this mistake can be made. The first way is by + * chaining the `length` assertion directly off of an uninvoked chainable + * method. In this case, Chai suggests that the user use `lengthOf` instead. The + * second way is by chaining the `length` assertion directly off of an uninvoked + * non-chainable method. Non-chainable methods must be invoked prior to + * chaining. In this case, Chai suggests that the user consult the docs for the + * given assertion. + * + * If the `length` property of functions is unconfigurable, then return `fn` + * without modification. + * + * Note that in ES6, the function's `length` property is configurable, so once + * support for legacy environments is dropped, Chai's `length` property can + * replace the built-in function's `length` property, and this length guard will + * no longer be necessary. In the mean time, maintaining consistency across all + * environments is the priority. + * + * @param {Function} fn + * @param {String} assertionName + * @param {Boolean} isChainable + * @namespace Utils + * @name addLengthGuard + */ + + module.exports = function addLengthGuard (fn, assertionName, isChainable) { + if (!fnLengthDesc.configurable) return fn; + + Object.defineProperty(fn, 'length', { + get: function () { + if (isChainable) { + throw Error('Invalid Chai property: ' + assertionName + '.length. Due' + + ' to a compatibility issue, "length" cannot directly follow "' + + assertionName + '". Use "' + assertionName + '.lengthOf" instead.'); + } + + throw Error('Invalid Chai property: ' + assertionName + '.length. See' + + ' docs for proper usage of "' + assertionName + '".'); + } + }); + + return fn; + }; + + },{}],11:[function(require,module,exports){ + /*! + * Chai - addMethod utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + var addLengthGuard = require('./addLengthGuard'); + var chai = require('../../chai'); + var flag = require('./flag'); + var proxify = require('./proxify'); + var transferFlags = require('./transferFlags'); + + /** + * ### .addMethod(ctx, name, method) + * + * Adds a method to the prototype of an object. + * + * utils.addMethod(chai.Assertion.prototype, 'foo', function (str) { + * var obj = utils.flag(this, 'object'); + * new chai.Assertion(obj).to.be.equal(str); + * }); + * + * Can also be accessed directly from `chai.Assertion`. + * + * chai.Assertion.addMethod('foo', fn); + * + * Then can be used as any other assertion. + * + * expect(fooStr).to.be.foo('bar'); + * + * @param {Object} ctx object to which the method is added + * @param {String} name of method to add + * @param {Function} method function to be used for name + * @namespace Utils + * @name addMethod + * @api public + */ + + module.exports = function addMethod(ctx, name, method) { + var methodWrapper = function () { + // Setting the `ssfi` flag to `methodWrapper` causes this function to be the + // starting point for removing implementation frames from the stack trace of + // a failed assertion. + // + // However, we only want to use this function as the starting point if the + // `lockSsfi` flag isn't set. + // + // If the `lockSsfi` flag is set, then either this assertion has been + // overwritten by another assertion, or this assertion is being invoked from + // inside of another assertion. In the first case, the `ssfi` flag has + // already been set by the overwriting assertion. In the second case, the + // `ssfi` flag has already been set by the outer assertion. + if (!flag(this, 'lockSsfi')) { + flag(this, 'ssfi', methodWrapper); + } + + var result = method.apply(this, arguments); + if (result !== undefined) + return result; + + var newAssertion = new chai.Assertion(); + transferFlags(this, newAssertion); + return newAssertion; + }; + + addLengthGuard(methodWrapper, name, false); + ctx[name] = proxify(methodWrapper, name); + }; + + },{"../../chai":2,"./addLengthGuard":10,"./flag":15,"./proxify":30,"./transferFlags":32}],12:[function(require,module,exports){ + /*! + * Chai - addProperty utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + var chai = require('../../chai'); + var flag = require('./flag'); + var isProxyEnabled = require('./isProxyEnabled'); + var transferFlags = require('./transferFlags'); + + /** + * ### .addProperty(ctx, name, getter) + * + * Adds a property to the prototype of an object. + * + * utils.addProperty(chai.Assertion.prototype, 'foo', function () { + * var obj = utils.flag(this, 'object'); + * new chai.Assertion(obj).to.be.instanceof(Foo); + * }); + * + * Can also be accessed directly from `chai.Assertion`. + * + * chai.Assertion.addProperty('foo', fn); + * + * Then can be used as any other assertion. + * + * expect(myFoo).to.be.foo; + * + * @param {Object} ctx object to which the property is added + * @param {String} name of property to add + * @param {Function} getter function to be used for name + * @namespace Utils + * @name addProperty + * @api public + */ + + module.exports = function addProperty(ctx, name, getter) { + getter = getter === undefined ? function () {} : getter; + + Object.defineProperty(ctx, name, + { get: function propertyGetter() { + // Setting the `ssfi` flag to `propertyGetter` causes this function to + // be the starting point for removing implementation frames from the + // stack trace of a failed assertion. + // + // However, we only want to use this function as the starting point if + // the `lockSsfi` flag isn't set and proxy protection is disabled. + // + // If the `lockSsfi` flag is set, then either this assertion has been + // overwritten by another assertion, or this assertion is being invoked + // from inside of another assertion. In the first case, the `ssfi` flag + // has already been set by the overwriting assertion. In the second + // case, the `ssfi` flag has already been set by the outer assertion. + // + // If proxy protection is enabled, then the `ssfi` flag has already been + // set by the proxy getter. + if (!isProxyEnabled() && !flag(this, 'lockSsfi')) { + flag(this, 'ssfi', propertyGetter); + } + + var result = getter.call(this); + if (result !== undefined) + return result; + + var newAssertion = new chai.Assertion(); + transferFlags(this, newAssertion); + return newAssertion; + } + , configurable: true + }); + }; + + },{"../../chai":2,"./flag":15,"./isProxyEnabled":25,"./transferFlags":32}],13:[function(require,module,exports){ + /*! + * Chai - compareByInspect utility + * Copyright(c) 2011-2016 Jake Luer + * MIT Licensed + */ + + /*! + * Module dependencies + */ + + var inspect = require('./inspect'); + + /** + * ### .compareByInspect(mixed, mixed) + * + * To be used as a compareFunction with Array.prototype.sort. Compares elements + * using inspect instead of default behavior of using toString so that Symbols + * and objects with irregular/missing toString can still be sorted without a + * TypeError. + * + * @param {Mixed} first element to compare + * @param {Mixed} second element to compare + * @returns {Number} -1 if 'a' should come before 'b'; otherwise 1 + * @name compareByInspect + * @namespace Utils + * @api public + */ + + module.exports = function compareByInspect(a, b) { + return inspect(a) < inspect(b) ? -1 : 1; + }; + + },{"./inspect":23}],14:[function(require,module,exports){ + /*! + * Chai - expectTypes utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + /** + * ### .expectTypes(obj, types) + * + * Ensures that the object being tested against is of a valid type. + * + * utils.expectTypes(this, ['array', 'object', 'string']); + * + * @param {Mixed} obj constructed Assertion + * @param {Array} type A list of allowed types for this assertion + * @namespace Utils + * @name expectTypes + * @api public + */ + + var AssertionError = require('assertion-error'); + var flag = require('./flag'); + var type = require('type-detect'); + + module.exports = function expectTypes(obj, types) { + var flagMsg = flag(obj, 'message'); + var ssfi = flag(obj, 'ssfi'); + + flagMsg = flagMsg ? flagMsg + ': ' : ''; + + obj = flag(obj, 'object'); + types = types.map(function (t) { return t.toLowerCase(); }); + types.sort(); + + // Transforms ['lorem', 'ipsum'] into 'a lorem, or an ipsum' + var str = types.map(function (t, index) { + var art = ~[ 'a', 'e', 'i', 'o', 'u' ].indexOf(t.charAt(0)) ? 'an' : 'a'; + var or = types.length > 1 && index === types.length - 1 ? 'or ' : ''; + return or + art + ' ' + t; + }).join(', '); + + var objType = type(obj).toLowerCase(); + + if (!types.some(function (expected) { return objType === expected; })) { + throw new AssertionError( + flagMsg + 'object tested must be ' + str + ', but ' + objType + ' given', + undefined, + ssfi + ); + } + }; + + },{"./flag":15,"assertion-error":33,"type-detect":38}],15:[function(require,module,exports){ + /*! + * Chai - flag utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + /** + * ### .flag(object, key, [value]) + * + * Get or set a flag value on an object. If a + * value is provided it will be set, else it will + * return the currently set value or `undefined` if + * the value is not set. + * + * utils.flag(this, 'foo', 'bar'); // setter + * utils.flag(this, 'foo'); // getter, returns `bar` + * + * @param {Object} object constructed Assertion + * @param {String} key + * @param {Mixed} value (optional) + * @namespace Utils + * @name flag + * @api private + */ + + module.exports = function flag(obj, key, value) { + var flags = obj.__flags || (obj.__flags = Object.create(null)); + if (arguments.length === 3) { + flags[key] = value; + } else { + return flags[key]; + } + }; + + },{}],16:[function(require,module,exports){ + /*! + * Chai - getActual utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + /** + * ### .getActual(object, [actual]) + * + * Returns the `actual` value for an Assertion. + * + * @param {Object} object (constructed Assertion) + * @param {Arguments} chai.Assertion.prototype.assert arguments + * @namespace Utils + * @name getActual + */ + + module.exports = function getActual(obj, args) { + return args.length > 4 ? args[4] : obj._obj; + }; + + },{}],17:[function(require,module,exports){ + /*! + * Chai - getEnumerableProperties utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + /** + * ### .getEnumerableProperties(object) + * + * This allows the retrieval of enumerable property names of an object, + * inherited or not. + * + * @param {Object} object + * @returns {Array} + * @namespace Utils + * @name getEnumerableProperties + * @api public + */ + + module.exports = function getEnumerableProperties(object) { + var result = []; + for (var name in object) { + result.push(name); + } + return result; + }; + + },{}],18:[function(require,module,exports){ + /*! + * Chai - message composition utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + /*! + * Module dependencies + */ + + var flag = require('./flag') + , getActual = require('./getActual') + , objDisplay = require('./objDisplay'); + + /** + * ### .getMessage(object, message, negateMessage) + * + * Construct the error message based on flags + * and template tags. Template tags will return + * a stringified inspection of the object referenced. + * + * Message template tags: + * - `#{this}` current asserted object + * - `#{act}` actual value + * - `#{exp}` expected value + * + * @param {Object} object (constructed Assertion) + * @param {Arguments} chai.Assertion.prototype.assert arguments + * @namespace Utils + * @name getMessage + * @api public + */ + + module.exports = function getMessage(obj, args) { + var negate = flag(obj, 'negate') + , val = flag(obj, 'object') + , expected = args[3] + , actual = getActual(obj, args) + , msg = negate ? args[2] : args[1] + , flagMsg = flag(obj, 'message'); + + if(typeof msg === "function") msg = msg(); + msg = msg || ''; + msg = msg + .replace(/#\{this\}/g, function () { return objDisplay(val); }) + .replace(/#\{act\}/g, function () { return objDisplay(actual); }) + .replace(/#\{exp\}/g, function () { return objDisplay(expected); }); + + return flagMsg ? flagMsg + ': ' + msg : msg; + }; + + },{"./flag":15,"./getActual":16,"./objDisplay":26}],19:[function(require,module,exports){ + /*! + * Chai - getOwnEnumerableProperties utility + * Copyright(c) 2011-2016 Jake Luer + * MIT Licensed + */ + + /*! + * Module dependencies + */ + + var getOwnEnumerablePropertySymbols = require('./getOwnEnumerablePropertySymbols'); + + /** + * ### .getOwnEnumerableProperties(object) + * + * This allows the retrieval of directly-owned enumerable property names and + * symbols of an object. This function is necessary because Object.keys only + * returns enumerable property names, not enumerable property symbols. + * + * @param {Object} object + * @returns {Array} + * @namespace Utils + * @name getOwnEnumerableProperties + * @api public + */ + + module.exports = function getOwnEnumerableProperties(obj) { + return Object.keys(obj).concat(getOwnEnumerablePropertySymbols(obj)); + }; + + },{"./getOwnEnumerablePropertySymbols":20}],20:[function(require,module,exports){ + /*! + * Chai - getOwnEnumerablePropertySymbols utility + * Copyright(c) 2011-2016 Jake Luer + * MIT Licensed + */ + + /** + * ### .getOwnEnumerablePropertySymbols(object) + * + * This allows the retrieval of directly-owned enumerable property symbols of an + * object. This function is necessary because Object.getOwnPropertySymbols + * returns both enumerable and non-enumerable property symbols. + * + * @param {Object} object + * @returns {Array} + * @namespace Utils + * @name getOwnEnumerablePropertySymbols + * @api public + */ + + module.exports = function getOwnEnumerablePropertySymbols(obj) { + if (typeof Object.getOwnPropertySymbols !== 'function') return []; + + return Object.getOwnPropertySymbols(obj).filter(function (sym) { + return Object.getOwnPropertyDescriptor(obj, sym).enumerable; + }); + }; + + },{}],21:[function(require,module,exports){ + /*! + * Chai - getProperties utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + /** + * ### .getProperties(object) + * + * This allows the retrieval of property names of an object, enumerable or not, + * inherited or not. + * + * @param {Object} object + * @returns {Array} + * @namespace Utils + * @name getProperties + * @api public + */ + + module.exports = function getProperties(object) { + var result = Object.getOwnPropertyNames(object); + + function addProperty(property) { + if (result.indexOf(property) === -1) { + result.push(property); + } + } + + var proto = Object.getPrototypeOf(object); + while (proto !== null) { + Object.getOwnPropertyNames(proto).forEach(addProperty); + proto = Object.getPrototypeOf(proto); + } + + return result; + }; + + },{}],22:[function(require,module,exports){ + /*! + * chai + * Copyright(c) 2011 Jake Luer + * MIT Licensed + */ + + /*! + * Dependencies that are used for multiple exports are required here only once + */ + + var pathval = require('pathval'); + + /*! + * test utility + */ + + exports.test = require('./test'); + + /*! + * type utility + */ + + exports.type = require('type-detect'); + + /*! + * expectTypes utility + */ + exports.expectTypes = require('./expectTypes'); + + /*! + * message utility + */ + + exports.getMessage = require('./getMessage'); + + /*! + * actual utility + */ + + exports.getActual = require('./getActual'); + + /*! + * Inspect util + */ + + exports.inspect = require('./inspect'); + + /*! + * Object Display util + */ + + exports.objDisplay = require('./objDisplay'); + + /*! + * Flag utility + */ + + exports.flag = require('./flag'); + + /*! + * Flag transferring utility + */ + + exports.transferFlags = require('./transferFlags'); + + /*! + * Deep equal utility + */ + + exports.eql = require('deep-eql'); + + /*! + * Deep path info + */ + + exports.getPathInfo = pathval.getPathInfo; + + /*! + * Check if a property exists + */ + + exports.hasProperty = pathval.hasProperty; + + /*! + * Function name + */ + + exports.getName = require('get-func-name'); + + /*! + * add Property + */ + + exports.addProperty = require('./addProperty'); + + /*! + * add Method + */ + + exports.addMethod = require('./addMethod'); + + /*! + * overwrite Property + */ + + exports.overwriteProperty = require('./overwriteProperty'); + + /*! + * overwrite Method + */ + + exports.overwriteMethod = require('./overwriteMethod'); + + /*! + * Add a chainable method + */ + + exports.addChainableMethod = require('./addChainableMethod'); + + /*! + * Overwrite chainable method + */ + + exports.overwriteChainableMethod = require('./overwriteChainableMethod'); + + /*! + * Compare by inspect method + */ + + exports.compareByInspect = require('./compareByInspect'); + + /*! + * Get own enumerable property symbols method + */ + + exports.getOwnEnumerablePropertySymbols = require('./getOwnEnumerablePropertySymbols'); + + /*! + * Get own enumerable properties method + */ + + exports.getOwnEnumerableProperties = require('./getOwnEnumerableProperties'); + + /*! + * Checks error against a given set of criteria + */ + + exports.checkError = require('check-error'); + + /*! + * Proxify util + */ + + exports.proxify = require('./proxify'); + + /*! + * addLengthGuard util + */ + + exports.addLengthGuard = require('./addLengthGuard'); + + /*! + * isProxyEnabled helper + */ + + exports.isProxyEnabled = require('./isProxyEnabled'); + + /*! + * isNaN method + */ + + exports.isNaN = require('./isNaN'); + + },{"./addChainableMethod":9,"./addLengthGuard":10,"./addMethod":11,"./addProperty":12,"./compareByInspect":13,"./expectTypes":14,"./flag":15,"./getActual":16,"./getMessage":18,"./getOwnEnumerableProperties":19,"./getOwnEnumerablePropertySymbols":20,"./inspect":23,"./isNaN":24,"./isProxyEnabled":25,"./objDisplay":26,"./overwriteChainableMethod":27,"./overwriteMethod":28,"./overwriteProperty":29,"./proxify":30,"./test":31,"./transferFlags":32,"check-error":34,"deep-eql":35,"get-func-name":36,"pathval":37,"type-detect":38}],23:[function(require,module,exports){ + // This is (almost) directly from Node.js utils + // https://github.com/joyent/node/blob/f8c335d0caf47f16d31413f89aa28eda3878e3aa/lib/util.js + + var getName = require('get-func-name'); + var getProperties = require('./getProperties'); + var getEnumerableProperties = require('./getEnumerableProperties'); + var config = require('../config'); + + module.exports = inspect; + + /** + * ### .inspect(obj, [showHidden], [depth], [colors]) + * + * Echoes the value of a value. Tries to print the value out + * in the best way possible given the different types. + * + * @param {Object} obj The object to print out. + * @param {Boolean} showHidden Flag that shows hidden (not enumerable) + * properties of objects. Default is false. + * @param {Number} depth Depth in which to descend in object. Default is 2. + * @param {Boolean} colors Flag to turn on ANSI escape codes to color the + * output. Default is false (no coloring). + * @namespace Utils + * @name inspect + */ + function inspect(obj, showHidden, depth, colors) { + var ctx = { + showHidden: showHidden, + seen: [], + stylize: function (str) { return str; } + }; + return formatValue(ctx, obj, (typeof depth === 'undefined' ? 2 : depth)); + } + + // Returns true if object is a DOM element. + var isDOMElement = function (object) { + if (typeof HTMLElement === 'object') { + return object instanceof HTMLElement; + } else { + return object && + typeof object === 'object' && + 'nodeType' in object && + object.nodeType === 1 && + typeof object.nodeName === 'string'; + } + }; + + function formatValue(ctx, value, recurseTimes) { + // Provide a hook for user-specified inspect functions. + // Check that value is an object with an inspect function on it + if (value && typeof value.inspect === 'function' && + // Filter out the util module, it's inspect function is special + value.inspect !== exports.inspect && + // Also filter out any prototype objects using the circular check. + !(value.constructor && value.constructor.prototype === value)) { + var ret = value.inspect(recurseTimes, ctx); + if (typeof ret !== 'string') { + ret = formatValue(ctx, ret, recurseTimes); + } + return ret; + } + + // Primitive types cannot have properties + var primitive = formatPrimitive(ctx, value); + if (primitive) { + return primitive; + } + + // If this is a DOM element, try to get the outer HTML. + if (isDOMElement(value)) { + if ('outerHTML' in value) { + return value.outerHTML; + // This value does not have an outerHTML attribute, + // it could still be an XML element + } else { + // Attempt to serialize it + try { + if (document.xmlVersion) { + var xmlSerializer = new XMLSerializer(); + return xmlSerializer.serializeToString(value); + } else { + // Firefox 11- do not support outerHTML + // It does, however, support innerHTML + // Use the following to render the element + var ns = "http://www.w3.org/1999/xhtml"; + var container = document.createElementNS(ns, '_'); + + container.appendChild(value.cloneNode(false)); + var html = container.innerHTML + .replace('><', '>' + value.innerHTML + '<'); + container.innerHTML = ''; + return html; + } + } catch (err) { + // This could be a non-native DOM implementation, + // continue with the normal flow: + // printing the element as if it is an object. + } + } + } + + // Look up the keys of the object. + var visibleKeys = getEnumerableProperties(value); + var keys = ctx.showHidden ? getProperties(value) : visibleKeys; + + var name, nameSuffix; + + // Some type of object without properties can be shortcut. + // In IE, errors have a single `stack` property, or if they are vanilla `Error`, + // a `stack` plus `description` property; ignore those for consistency. + if (keys.length === 0 || (isError(value) && ( + (keys.length === 1 && keys[0] === 'stack') || + (keys.length === 2 && keys[0] === 'description' && keys[1] === 'stack') + ))) { + if (typeof value === 'function') { + name = getName(value); + nameSuffix = name ? ': ' + name : ''; + return ctx.stylize('[Function' + nameSuffix + ']', 'special'); + } + if (isRegExp(value)) { + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); + } + if (isDate(value)) { + return ctx.stylize(Date.prototype.toUTCString.call(value), 'date'); + } + if (isError(value)) { + return formatError(value); + } + } + + var base = '' + , array = false + , typedArray = false + , braces = ['{', '}']; + + if (isTypedArray(value)) { + typedArray = true; + braces = ['[', ']']; + } + + // Make Array say that they are Array + if (isArray(value)) { + array = true; + braces = ['[', ']']; + } + + // Make functions say that they are functions + if (typeof value === 'function') { + name = getName(value); + nameSuffix = name ? ': ' + name : ''; + base = ' [Function' + nameSuffix + ']'; + } + + // Make RegExps say that they are RegExps + if (isRegExp(value)) { + base = ' ' + RegExp.prototype.toString.call(value); + } + + // Make dates with properties first say the date + if (isDate(value)) { + base = ' ' + Date.prototype.toUTCString.call(value); + } + + // Make error with message first say the error + if (isError(value)) { + return formatError(value); + } + + if (keys.length === 0 && (!array || value.length == 0)) { + return braces[0] + base + braces[1]; + } + + if (recurseTimes < 0) { + if (isRegExp(value)) { + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); + } else { + return ctx.stylize('[Object]', 'special'); + } + } + + ctx.seen.push(value); + + var output; + if (array) { + output = formatArray(ctx, value, recurseTimes, visibleKeys, keys); + } else if (typedArray) { + return formatTypedArray(value); + } else { + output = keys.map(function(key) { + return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array); + }); + } + + ctx.seen.pop(); + + return reduceToSingleString(output, base, braces); + } + + function formatPrimitive(ctx, value) { + switch (typeof value) { + case 'undefined': + return ctx.stylize('undefined', 'undefined'); + + case 'string': + var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') + .replace(/'/g, "\\'") + .replace(/\\"/g, '"') + '\''; + return ctx.stylize(simple, 'string'); + + case 'number': + if (value === 0 && (1/value) === -Infinity) { + return ctx.stylize('-0', 'number'); + } + return ctx.stylize('' + value, 'number'); + + case 'boolean': + return ctx.stylize('' + value, 'boolean'); + + case 'symbol': + return ctx.stylize(value.toString(), 'symbol'); + } + // For some reason typeof null is "object", so special case here. + if (value === null) { + return ctx.stylize('null', 'null'); + } + } + + function formatError(value) { + return '[' + Error.prototype.toString.call(value) + ']'; + } + + function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { + var output = []; + for (var i = 0, l = value.length; i < l; ++i) { + if (Object.prototype.hasOwnProperty.call(value, String(i))) { + output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, + String(i), true)); + } else { + output.push(''); + } + } + + keys.forEach(function(key) { + if (!key.match(/^\d+$/)) { + output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, + key, true)); + } + }); + return output; + } + + function formatTypedArray(value) { + var str = '[ '; + + for (var i = 0; i < value.length; ++i) { + if (str.length >= config.truncateThreshold - 7) { + str += '...'; + break; + } + str += value[i] + ', '; + } + str += ' ]'; + + // Removing trailing `, ` if the array was not truncated + if (str.indexOf(', ]') !== -1) { + str = str.replace(', ]', ' ]'); + } + + return str; + } + + function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { + var name; + var propDescriptor = Object.getOwnPropertyDescriptor(value, key); + var str; + + if (propDescriptor) { + if (propDescriptor.get) { + if (propDescriptor.set) { + str = ctx.stylize('[Getter/Setter]', 'special'); + } else { + str = ctx.stylize('[Getter]', 'special'); + } + } else { + if (propDescriptor.set) { + str = ctx.stylize('[Setter]', 'special'); + } + } + } + if (visibleKeys.indexOf(key) < 0) { + name = '[' + key + ']'; + } + if (!str) { + if (ctx.seen.indexOf(value[key]) < 0) { + if (recurseTimes === null) { + str = formatValue(ctx, value[key], null); + } else { + str = formatValue(ctx, value[key], recurseTimes - 1); + } + if (str.indexOf('\n') > -1) { + if (array) { + str = str.split('\n').map(function(line) { + return ' ' + line; + }).join('\n').substr(2); + } else { + str = '\n' + str.split('\n').map(function(line) { + return ' ' + line; + }).join('\n'); + } + } + } else { + str = ctx.stylize('[Circular]', 'special'); + } + } + if (typeof name === 'undefined') { + if (array && key.match(/^\d+$/)) { + return str; + } + name = JSON.stringify('' + key); + if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { + name = name.substr(1, name.length - 2); + name = ctx.stylize(name, 'name'); + } else { + name = name.replace(/'/g, "\\'") + .replace(/\\"/g, '"') + .replace(/(^"|"$)/g, "'"); + name = ctx.stylize(name, 'string'); + } + } + + return name + ': ' + str; + } + + function reduceToSingleString(output, base, braces) { + var length = output.reduce(function(prev, cur) { + return prev + cur.length + 1; + }, 0); + + if (length > 60) { + return braces[0] + + (base === '' ? '' : base + '\n ') + + ' ' + + output.join(',\n ') + + ' ' + + braces[1]; + } + + return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; + } + + function isTypedArray(ar) { + // Unfortunately there's no way to check if an object is a TypedArray + // We have to check if it's one of these types + return (typeof ar === 'object' && /\w+Array]$/.test(objectToString(ar))); + } + + function isArray(ar) { + return Array.isArray(ar) || + (typeof ar === 'object' && objectToString(ar) === '[object Array]'); + } + + function isRegExp(re) { + return typeof re === 'object' && objectToString(re) === '[object RegExp]'; + } + + function isDate(d) { + return typeof d === 'object' && objectToString(d) === '[object Date]'; + } + + function isError(e) { + return typeof e === 'object' && objectToString(e) === '[object Error]'; + } + + function objectToString(o) { + return Object.prototype.toString.call(o); + } + + },{"../config":4,"./getEnumerableProperties":17,"./getProperties":21,"get-func-name":36}],24:[function(require,module,exports){ + /*! + * Chai - isNaN utility + * Copyright(c) 2012-2015 Sakthipriyan Vairamani + * MIT Licensed + */ + + /** + * ### .isNaN(value) + * + * Checks if the given value is NaN or not. + * + * utils.isNaN(NaN); // true + * + * @param {Value} The value which has to be checked if it is NaN + * @name isNaN + * @api private + */ + + function isNaN(value) { + // Refer http://www.ecma-international.org/ecma-262/6.0/#sec-isnan-number + // section's NOTE. + return value !== value; + } + + // If ECMAScript 6's Number.isNaN is present, prefer that. + module.exports = Number.isNaN || isNaN; + + },{}],25:[function(require,module,exports){ + var config = require('../config'); + + /*! + * Chai - isProxyEnabled helper + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + /** + * ### .isProxyEnabled() + * + * Helper function to check if Chai's proxy protection feature is enabled. If + * proxies are unsupported or disabled via the user's Chai config, then return + * false. Otherwise, return true. + * + * @namespace Utils + * @name isProxyEnabled + */ + + module.exports = function isProxyEnabled() { + return config.useProxy && + typeof Proxy !== 'undefined' && + typeof Reflect !== 'undefined'; + }; + + },{"../config":4}],26:[function(require,module,exports){ + /*! + * Chai - flag utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + /*! + * Module dependencies + */ + + var inspect = require('./inspect'); + var config = require('../config'); + + /** + * ### .objDisplay(object) + * + * Determines if an object or an array matches + * criteria to be inspected in-line for error + * messages or should be truncated. + * + * @param {Mixed} javascript object to inspect + * @name objDisplay + * @namespace Utils + * @api public + */ + + module.exports = function objDisplay(obj) { + var str = inspect(obj) + , type = Object.prototype.toString.call(obj); + + if (config.truncateThreshold && str.length >= config.truncateThreshold) { + if (type === '[object Function]') { + return !obj.name || obj.name === '' + ? '[Function]' + : '[Function: ' + obj.name + ']'; + } else if (type === '[object Array]') { + return '[ Array(' + obj.length + ') ]'; + } else if (type === '[object Object]') { + var keys = Object.keys(obj) + , kstr = keys.length > 2 + ? keys.splice(0, 2).join(', ') + ', ...' + : keys.join(', '); + return '{ Object (' + kstr + ') }'; + } else { + return str; + } + } else { + return str; + } + }; + + },{"../config":4,"./inspect":23}],27:[function(require,module,exports){ + /*! + * Chai - overwriteChainableMethod utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + var chai = require('../../chai'); + var transferFlags = require('./transferFlags'); + + /** + * ### .overwriteChainableMethod(ctx, name, method, chainingBehavior) + * + * Overwrites an already existing chainable method + * and provides access to the previous function or + * property. Must return functions to be used for + * name. + * + * utils.overwriteChainableMethod(chai.Assertion.prototype, 'lengthOf', + * function (_super) { + * } + * , function (_super) { + * } + * ); + * + * Can also be accessed directly from `chai.Assertion`. + * + * chai.Assertion.overwriteChainableMethod('foo', fn, fn); + * + * Then can be used as any other assertion. + * + * expect(myFoo).to.have.lengthOf(3); + * expect(myFoo).to.have.lengthOf.above(3); + * + * @param {Object} ctx object whose method / property is to be overwritten + * @param {String} name of method / property to overwrite + * @param {Function} method function that returns a function to be used for name + * @param {Function} chainingBehavior function that returns a function to be used for property + * @namespace Utils + * @name overwriteChainableMethod + * @api public + */ + + module.exports = function overwriteChainableMethod(ctx, name, method, chainingBehavior) { + var chainableBehavior = ctx.__methods[name]; + + var _chainingBehavior = chainableBehavior.chainingBehavior; + chainableBehavior.chainingBehavior = function overwritingChainableMethodGetter() { + var result = chainingBehavior(_chainingBehavior).call(this); + if (result !== undefined) { + return result; + } + + var newAssertion = new chai.Assertion(); + transferFlags(this, newAssertion); + return newAssertion; + }; + + var _method = chainableBehavior.method; + chainableBehavior.method = function overwritingChainableMethodWrapper() { + var result = method(_method).apply(this, arguments); + if (result !== undefined) { + return result; + } + + var newAssertion = new chai.Assertion(); + transferFlags(this, newAssertion); + return newAssertion; + }; + }; + + },{"../../chai":2,"./transferFlags":32}],28:[function(require,module,exports){ + /*! + * Chai - overwriteMethod utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + var addLengthGuard = require('./addLengthGuard'); + var chai = require('../../chai'); + var flag = require('./flag'); + var proxify = require('./proxify'); + var transferFlags = require('./transferFlags'); + + /** + * ### .overwriteMethod(ctx, name, fn) + * + * Overwrites an already existing method and provides + * access to previous function. Must return function + * to be used for name. + * + * utils.overwriteMethod(chai.Assertion.prototype, 'equal', function (_super) { + * return function (str) { + * var obj = utils.flag(this, 'object'); + * if (obj instanceof Foo) { + * new chai.Assertion(obj.value).to.equal(str); + * } else { + * _super.apply(this, arguments); + * } + * } + * }); + * + * Can also be accessed directly from `chai.Assertion`. + * + * chai.Assertion.overwriteMethod('foo', fn); + * + * Then can be used as any other assertion. + * + * expect(myFoo).to.equal('bar'); + * + * @param {Object} ctx object whose method is to be overwritten + * @param {String} name of method to overwrite + * @param {Function} method function that returns a function to be used for name + * @namespace Utils + * @name overwriteMethod + * @api public + */ + + module.exports = function overwriteMethod(ctx, name, method) { + var _method = ctx[name] + , _super = function () { + throw new Error(name + ' is not a function'); + }; + + if (_method && 'function' === typeof _method) + _super = _method; + + var overwritingMethodWrapper = function () { + // Setting the `ssfi` flag to `overwritingMethodWrapper` causes this + // function to be the starting point for removing implementation frames from + // the stack trace of a failed assertion. + // + // However, we only want to use this function as the starting point if the + // `lockSsfi` flag isn't set. + // + // If the `lockSsfi` flag is set, then either this assertion has been + // overwritten by another assertion, or this assertion is being invoked from + // inside of another assertion. In the first case, the `ssfi` flag has + // already been set by the overwriting assertion. In the second case, the + // `ssfi` flag has already been set by the outer assertion. + if (!flag(this, 'lockSsfi')) { + flag(this, 'ssfi', overwritingMethodWrapper); + } + + // Setting the `lockSsfi` flag to `true` prevents the overwritten assertion + // from changing the `ssfi` flag. By this point, the `ssfi` flag is already + // set to the correct starting point for this assertion. + var origLockSsfi = flag(this, 'lockSsfi'); + flag(this, 'lockSsfi', true); + var result = method(_super).apply(this, arguments); + flag(this, 'lockSsfi', origLockSsfi); + + if (result !== undefined) { + return result; + } + + var newAssertion = new chai.Assertion(); + transferFlags(this, newAssertion); + return newAssertion; + } + + addLengthGuard(overwritingMethodWrapper, name, false); + ctx[name] = proxify(overwritingMethodWrapper, name); + }; + + },{"../../chai":2,"./addLengthGuard":10,"./flag":15,"./proxify":30,"./transferFlags":32}],29:[function(require,module,exports){ + /*! + * Chai - overwriteProperty utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + var chai = require('../../chai'); + var flag = require('./flag'); + var isProxyEnabled = require('./isProxyEnabled'); + var transferFlags = require('./transferFlags'); + + /** + * ### .overwriteProperty(ctx, name, fn) + * + * Overwrites an already existing property getter and provides + * access to previous value. Must return function to use as getter. + * + * utils.overwriteProperty(chai.Assertion.prototype, 'ok', function (_super) { + * return function () { + * var obj = utils.flag(this, 'object'); + * if (obj instanceof Foo) { + * new chai.Assertion(obj.name).to.equal('bar'); + * } else { + * _super.call(this); + * } + * } + * }); + * + * + * Can also be accessed directly from `chai.Assertion`. + * + * chai.Assertion.overwriteProperty('foo', fn); + * + * Then can be used as any other assertion. + * + * expect(myFoo).to.be.ok; + * + * @param {Object} ctx object whose property is to be overwritten + * @param {String} name of property to overwrite + * @param {Function} getter function that returns a getter function to be used for name + * @namespace Utils + * @name overwriteProperty + * @api public + */ + + module.exports = function overwriteProperty(ctx, name, getter) { + var _get = Object.getOwnPropertyDescriptor(ctx, name) + , _super = function () {}; + + if (_get && 'function' === typeof _get.get) + _super = _get.get + + Object.defineProperty(ctx, name, + { get: function overwritingPropertyGetter() { + // Setting the `ssfi` flag to `overwritingPropertyGetter` causes this + // function to be the starting point for removing implementation frames + // from the stack trace of a failed assertion. + // + // However, we only want to use this function as the starting point if + // the `lockSsfi` flag isn't set and proxy protection is disabled. + // + // If the `lockSsfi` flag is set, then either this assertion has been + // overwritten by another assertion, or this assertion is being invoked + // from inside of another assertion. In the first case, the `ssfi` flag + // has already been set by the overwriting assertion. In the second + // case, the `ssfi` flag has already been set by the outer assertion. + // + // If proxy protection is enabled, then the `ssfi` flag has already been + // set by the proxy getter. + if (!isProxyEnabled() && !flag(this, 'lockSsfi')) { + flag(this, 'ssfi', overwritingPropertyGetter); + } + + // Setting the `lockSsfi` flag to `true` prevents the overwritten + // assertion from changing the `ssfi` flag. By this point, the `ssfi` + // flag is already set to the correct starting point for this assertion. + var origLockSsfi = flag(this, 'lockSsfi'); + flag(this, 'lockSsfi', true); + var result = getter(_super).call(this); + flag(this, 'lockSsfi', origLockSsfi); + + if (result !== undefined) { + return result; + } + + var newAssertion = new chai.Assertion(); + transferFlags(this, newAssertion); + return newAssertion; + } + , configurable: true + }); + }; + + },{"../../chai":2,"./flag":15,"./isProxyEnabled":25,"./transferFlags":32}],30:[function(require,module,exports){ + var config = require('../config'); + var flag = require('./flag'); + var getProperties = require('./getProperties'); + var isProxyEnabled = require('./isProxyEnabled'); + + /*! + * Chai - proxify utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + /** + * ### .proxify(object) + * + * Return a proxy of given object that throws an error when a non-existent + * property is read. By default, the root cause is assumed to be a misspelled + * property, and thus an attempt is made to offer a reasonable suggestion from + * the list of existing properties. However, if a nonChainableMethodName is + * provided, then the root cause is instead a failure to invoke a non-chainable + * method prior to reading the non-existent property. + * + * If proxies are unsupported or disabled via the user's Chai config, then + * return object without modification. + * + * @param {Object} obj + * @param {String} nonChainableMethodName + * @namespace Utils + * @name proxify + */ + + var builtins = ['__flags', '__methods', '_obj', 'assert']; + + module.exports = function proxify(obj, nonChainableMethodName) { + if (!isProxyEnabled()) return obj; + + return new Proxy(obj, { + get: function proxyGetter(target, property) { + // This check is here because we should not throw errors on Symbol properties + // such as `Symbol.toStringTag`. + // The values for which an error should be thrown can be configured using + // the `config.proxyExcludedKeys` setting. + if (typeof property === 'string' && + config.proxyExcludedKeys.indexOf(property) === -1 && + !Reflect.has(target, property)) { + // Special message for invalid property access of non-chainable methods. + if (nonChainableMethodName) { + throw Error('Invalid Chai property: ' + nonChainableMethodName + '.' + + property + '. See docs for proper usage of "' + + nonChainableMethodName + '".'); + } + + // If the property is reasonably close to an existing Chai property, + // suggest that property to the user. Only suggest properties with a + // distance less than 4. + var suggestion = null; + var suggestionDistance = 4; + getProperties(target).forEach(function(prop) { + if ( + !Object.prototype.hasOwnProperty(prop) && + builtins.indexOf(prop) === -1 + ) { + var dist = stringDistanceCapped( + property, + prop, + suggestionDistance + ); + if (dist < suggestionDistance) { + suggestion = prop; + suggestionDistance = dist; + } + } + }); + + if (suggestion !== null) { + throw Error('Invalid Chai property: ' + property + + '. Did you mean "' + suggestion + '"?'); + } else { + throw Error('Invalid Chai property: ' + property); + } + } + + // Use this proxy getter as the starting point for removing implementation + // frames from the stack trace of a failed assertion. For property + // assertions, this prevents the proxy getter from showing up in the stack + // trace since it's invoked before the property getter. For method and + // chainable method assertions, this flag will end up getting changed to + // the method wrapper, which is good since this frame will no longer be in + // the stack once the method is invoked. Note that Chai builtin assertion + // properties such as `__flags` are skipped since this is only meant to + // capture the starting point of an assertion. This step is also skipped + // if the `lockSsfi` flag is set, thus indicating that this assertion is + // being called from within another assertion. In that case, the `ssfi` + // flag is already set to the outer assertion's starting point. + if (builtins.indexOf(property) === -1 && !flag(target, 'lockSsfi')) { + flag(target, 'ssfi', proxyGetter); + } + + return Reflect.get(target, property); + } + }); + }; + + /** + * # stringDistanceCapped(strA, strB, cap) + * Return the Levenshtein distance between two strings, but no more than cap. + * @param {string} strA + * @param {string} strB + * @param {number} number + * @return {number} min(string distance between strA and strB, cap) + * @api private + */ + + function stringDistanceCapped(strA, strB, cap) { + if (Math.abs(strA.length - strB.length) >= cap) { + return cap; + } + + var memo = []; + // `memo` is a two-dimensional array containing distances. + // memo[i][j] is the distance between strA.slice(0, i) and + // strB.slice(0, j). + for (var i = 0; i <= strA.length; i++) { + memo[i] = Array(strB.length + 1).fill(0); + memo[i][0] = i; + } + for (var j = 0; j < strB.length; j++) { + memo[0][j] = j; + } + + for (var i = 1; i <= strA.length; i++) { + var ch = strA.charCodeAt(i - 1); + for (var j = 1; j <= strB.length; j++) { + if (Math.abs(i - j) >= cap) { + memo[i][j] = cap; + continue; + } + memo[i][j] = Math.min( + memo[i - 1][j] + 1, + memo[i][j - 1] + 1, + memo[i - 1][j - 1] + + (ch === strB.charCodeAt(j - 1) ? 0 : 1) + ); + } + } + + return memo[strA.length][strB.length]; + } + + },{"../config":4,"./flag":15,"./getProperties":21,"./isProxyEnabled":25}],31:[function(require,module,exports){ + /*! + * Chai - test utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + /*! + * Module dependencies + */ + + var flag = require('./flag'); + + /** + * ### .test(object, expression) + * + * Test and object for expression. + * + * @param {Object} object (constructed Assertion) + * @param {Arguments} chai.Assertion.prototype.assert arguments + * @namespace Utils + * @name test + */ + + module.exports = function test(obj, args) { + var negate = flag(obj, 'negate') + , expr = args[0]; + return negate ? !expr : expr; + }; + + },{"./flag":15}],32:[function(require,module,exports){ + /*! + * Chai - transferFlags utility + * Copyright(c) 2012-2014 Jake Luer + * MIT Licensed + */ + + /** + * ### .transferFlags(assertion, object, includeAll = true) + * + * Transfer all the flags for `assertion` to `object`. If + * `includeAll` is set to `false`, then the base Chai + * assertion flags (namely `object`, `ssfi`, `lockSsfi`, + * and `message`) will not be transferred. + * + * + * var newAssertion = new Assertion(); + * utils.transferFlags(assertion, newAssertion); + * + * var anotherAssertion = new Assertion(myObj); + * utils.transferFlags(assertion, anotherAssertion, false); + * + * @param {Assertion} assertion the assertion to transfer the flags from + * @param {Object} object the object to transfer the flags to; usually a new assertion + * @param {Boolean} includeAll + * @namespace Utils + * @name transferFlags + * @api private + */ + + module.exports = function transferFlags(assertion, object, includeAll) { + var flags = assertion.__flags || (assertion.__flags = Object.create(null)); + + if (!object.__flags) { + object.__flags = Object.create(null); + } + + includeAll = arguments.length === 3 ? includeAll : true; + + for (var flag in flags) { + if (includeAll || + (flag !== 'object' && flag !== 'ssfi' && flag !== 'lockSsfi' && flag != 'message')) { + object.__flags[flag] = flags[flag]; + } + } + }; + + },{}],33:[function(require,module,exports){ + /*! + * assertion-error + * Copyright(c) 2013 Jake Luer + * MIT Licensed + */ + + /*! + * Return a function that will copy properties from + * one object to another excluding any originally + * listed. Returned function will create a new `{}`. + * + * @param {String} excluded properties ... + * @return {Function} + */ + + function exclude () { + var excludes = [].slice.call(arguments); + + function excludeProps (res, obj) { + Object.keys(obj).forEach(function (key) { + if (!~excludes.indexOf(key)) res[key] = obj[key]; + }); + } + + return function extendExclude () { + var args = [].slice.call(arguments) + , i = 0 + , res = {}; + + for (; i < args.length; i++) { + excludeProps(res, args[i]); + } + + return res; + }; + }; + + /*! + * Primary Exports + */ + + module.exports = AssertionError; + + /** + * ### AssertionError + * + * An extension of the JavaScript `Error` constructor for + * assertion and validation scenarios. + * + * @param {String} message + * @param {Object} properties to include (optional) + * @param {callee} start stack function (optional) + */ + + function AssertionError (message, _props, ssf) { + var extend = exclude('name', 'message', 'stack', 'constructor', 'toJSON') + , props = extend(_props || {}); + + // default values + this.message = message || 'Unspecified AssertionError'; + this.showDiff = false; + + // copy from properties + for (var key in props) { + this[key] = props[key]; + } + + // capture stack trace + ssf = ssf || AssertionError; + if (Error.captureStackTrace) { + Error.captureStackTrace(this, ssf); + } else { + try { + throw new Error(); + } catch(e) { + this.stack = e.stack; + } + } + } + + /*! + * Inherit from Error.prototype + */ + + AssertionError.prototype = Object.create(Error.prototype); + + /*! + * Statically set name + */ + + AssertionError.prototype.name = 'AssertionError'; + + /*! + * Ensure correct constructor + */ + + AssertionError.prototype.constructor = AssertionError; + + /** + * Allow errors to be converted to JSON for static transfer. + * + * @param {Boolean} include stack (default: `true`) + * @return {Object} object that can be `JSON.stringify` + */ + + AssertionError.prototype.toJSON = function (stack) { + var extend = exclude('constructor', 'toJSON', 'stack') + , props = extend({ name: this.name }, this); + + // include stack if exists and not turned off + if (false !== stack && this.stack) { + props.stack = this.stack; + } + + return props; + }; + + },{}],34:[function(require,module,exports){ + 'use strict'; + + /* ! + * Chai - checkError utility + * Copyright(c) 2012-2016 Jake Luer + * MIT Licensed + */ + + /** + * ### .checkError + * + * Checks that an error conforms to a given set of criteria and/or retrieves information about it. + * + * @api public + */ + + /** + * ### .compatibleInstance(thrown, errorLike) + * + * Checks if two instances are compatible (strict equal). + * Returns false if errorLike is not an instance of Error, because instances + * can only be compatible if they're both error instances. + * + * @name compatibleInstance + * @param {Error} thrown error + * @param {Error|ErrorConstructor} errorLike object to compare against + * @namespace Utils + * @api public + */ + + function compatibleInstance(thrown, errorLike) { + return errorLike instanceof Error && thrown === errorLike; + } + + /** + * ### .compatibleConstructor(thrown, errorLike) + * + * Checks if two constructors are compatible. + * This function can receive either an error constructor or + * an error instance as the `errorLike` argument. + * Constructors are compatible if they're the same or if one is + * an instance of another. + * + * @name compatibleConstructor + * @param {Error} thrown error + * @param {Error|ErrorConstructor} errorLike object to compare against + * @namespace Utils + * @api public + */ + + function compatibleConstructor(thrown, errorLike) { + if (errorLike instanceof Error) { + // If `errorLike` is an instance of any error we compare their constructors + return thrown.constructor === errorLike.constructor || thrown instanceof errorLike.constructor; + } else if (errorLike.prototype instanceof Error || errorLike === Error) { + // If `errorLike` is a constructor that inherits from Error, we compare `thrown` to `errorLike` directly + return thrown.constructor === errorLike || thrown instanceof errorLike; + } + + return false; + } + + /** + * ### .compatibleMessage(thrown, errMatcher) + * + * Checks if an error's message is compatible with a matcher (String or RegExp). + * If the message contains the String or passes the RegExp test, + * it is considered compatible. + * + * @name compatibleMessage + * @param {Error} thrown error + * @param {String|RegExp} errMatcher to look for into the message + * @namespace Utils + * @api public + */ + + function compatibleMessage(thrown, errMatcher) { + var comparisonString = typeof thrown === 'string' ? thrown : thrown.message; + if (errMatcher instanceof RegExp) { + return errMatcher.test(comparisonString); + } else if (typeof errMatcher === 'string') { + return comparisonString.indexOf(errMatcher) !== -1; // eslint-disable-line no-magic-numbers + } + + return false; + } + + /** + * ### .getFunctionName(constructorFn) + * + * Returns the name of a function. + * This also includes a polyfill function if `constructorFn.name` is not defined. + * + * @name getFunctionName + * @param {Function} constructorFn + * @namespace Utils + * @api private + */ + + var functionNameMatch = /\s*function(?:\s|\s*\/\*[^(?:*\/)]+\*\/\s*)*([^\(\/]+)/; + function getFunctionName(constructorFn) { + var name = ''; + if (typeof constructorFn.name === 'undefined') { + // Here we run a polyfill if constructorFn.name is not defined + var match = String(constructorFn).match(functionNameMatch); + if (match) { + name = match[1]; + } + } else { + name = constructorFn.name; + } + + return name; + } + + /** + * ### .getConstructorName(errorLike) + * + * Gets the constructor name for an Error instance or constructor itself. + * + * @name getConstructorName + * @param {Error|ErrorConstructor} errorLike + * @namespace Utils + * @api public + */ + + function getConstructorName(errorLike) { + var constructorName = errorLike; + if (errorLike instanceof Error) { + constructorName = getFunctionName(errorLike.constructor); + } else if (typeof errorLike === 'function') { + // If `err` is not an instance of Error it is an error constructor itself or another function. + // If we've got a common function we get its name, otherwise we may need to create a new instance + // of the error just in case it's a poorly-constructed error. Please see chaijs/chai/issues/45 to know more. + constructorName = getFunctionName(errorLike).trim() || + getFunctionName(new errorLike()); // eslint-disable-line new-cap + } + + return constructorName; + } + + /** + * ### .getMessage(errorLike) + * + * Gets the error message from an error. + * If `err` is a String itself, we return it. + * If the error has no message, we return an empty string. + * + * @name getMessage + * @param {Error|String} errorLike + * @namespace Utils + * @api public + */ + + function getMessage(errorLike) { + var msg = ''; + if (errorLike && errorLike.message) { + msg = errorLike.message; + } else if (typeof errorLike === 'string') { + msg = errorLike; + } + + return msg; + } + + module.exports = { + compatibleInstance: compatibleInstance, + compatibleConstructor: compatibleConstructor, + compatibleMessage: compatibleMessage, + getMessage: getMessage, + getConstructorName: getConstructorName, + }; + + },{}],35:[function(require,module,exports){ + 'use strict'; + /* globals Symbol: false, Uint8Array: false, WeakMap: false */ + /*! + * deep-eql + * Copyright(c) 2013 Jake Luer + * MIT Licensed + */ + + var type = require('type-detect'); + function FakeMap() { + this._key = 'chai/deep-eql__' + Math.random() + Date.now(); + } + + FakeMap.prototype = { + get: function getMap(key) { + return key[this._key]; + }, + set: function setMap(key, value) { + if (Object.isExtensible(key)) { + Object.defineProperty(key, this._key, { + value: value, + configurable: true, + }); + } + }, + }; + + var MemoizeMap = typeof WeakMap === 'function' ? WeakMap : FakeMap; + /*! + * Check to see if the MemoizeMap has recorded a result of the two operands + * + * @param {Mixed} leftHandOperand + * @param {Mixed} rightHandOperand + * @param {MemoizeMap} memoizeMap + * @returns {Boolean|null} result + */ + function memoizeCompare(leftHandOperand, rightHandOperand, memoizeMap) { + // Technically, WeakMap keys can *only* be objects, not primitives. + if (!memoizeMap || isPrimitive(leftHandOperand) || isPrimitive(rightHandOperand)) { + return null; + } + var leftHandMap = memoizeMap.get(leftHandOperand); + if (leftHandMap) { + var result = leftHandMap.get(rightHandOperand); + if (typeof result === 'boolean') { + return result; + } + } + return null; + } + + /*! + * Set the result of the equality into the MemoizeMap + * + * @param {Mixed} leftHandOperand + * @param {Mixed} rightHandOperand + * @param {MemoizeMap} memoizeMap + * @param {Boolean} result + */ + function memoizeSet(leftHandOperand, rightHandOperand, memoizeMap, result) { + // Technically, WeakMap keys can *only* be objects, not primitives. + if (!memoizeMap || isPrimitive(leftHandOperand) || isPrimitive(rightHandOperand)) { + return; + } + var leftHandMap = memoizeMap.get(leftHandOperand); + if (leftHandMap) { + leftHandMap.set(rightHandOperand, result); + } else { + leftHandMap = new MemoizeMap(); + leftHandMap.set(rightHandOperand, result); + memoizeMap.set(leftHandOperand, leftHandMap); + } + } + + /*! + * Primary Export + */ + + module.exports = deepEqual; + module.exports.MemoizeMap = MemoizeMap; + + /** + * Assert deeply nested sameValue equality between two objects of any type. + * + * @param {Mixed} leftHandOperand + * @param {Mixed} rightHandOperand + * @param {Object} [options] (optional) Additional options + * @param {Array} [options.comparator] (optional) Override default algorithm, determining custom equality. + * @param {Array} [options.memoize] (optional) Provide a custom memoization object which will cache the results of + complex objects for a speed boost. By passing `false` you can disable memoization, but this will cause circular + references to blow the stack. + * @return {Boolean} equal match + */ + function deepEqual(leftHandOperand, rightHandOperand, options) { + // If we have a comparator, we can't assume anything; so bail to its check first. + if (options && options.comparator) { + return extensiveDeepEqual(leftHandOperand, rightHandOperand, options); + } + + var simpleResult = simpleEqual(leftHandOperand, rightHandOperand); + if (simpleResult !== null) { + return simpleResult; + } + + // Deeper comparisons are pushed through to a larger function + return extensiveDeepEqual(leftHandOperand, rightHandOperand, options); + } + + /** + * Many comparisons can be canceled out early via simple equality or primitive checks. + * @param {Mixed} leftHandOperand + * @param {Mixed} rightHandOperand + * @return {Boolean|null} equal match + */ + function simpleEqual(leftHandOperand, rightHandOperand) { + // Equal references (except for Numbers) can be returned early + if (leftHandOperand === rightHandOperand) { + // Handle +-0 cases + return leftHandOperand !== 0 || 1 / leftHandOperand === 1 / rightHandOperand; + } + + // handle NaN cases + if ( + leftHandOperand !== leftHandOperand && // eslint-disable-line no-self-compare + rightHandOperand !== rightHandOperand // eslint-disable-line no-self-compare + ) { + return true; + } + + // Anything that is not an 'object', i.e. symbols, functions, booleans, numbers, + // strings, and undefined, can be compared by reference. + if (isPrimitive(leftHandOperand) || isPrimitive(rightHandOperand)) { + // Easy out b/c it would have passed the first equality check + return false; + } + return null; + } + + /*! + * The main logic of the `deepEqual` function. + * + * @param {Mixed} leftHandOperand + * @param {Mixed} rightHandOperand + * @param {Object} [options] (optional) Additional options + * @param {Array} [options.comparator] (optional) Override default algorithm, determining custom equality. + * @param {Array} [options.memoize] (optional) Provide a custom memoization object which will cache the results of + complex objects for a speed boost. By passing `false` you can disable memoization, but this will cause circular + references to blow the stack. + * @return {Boolean} equal match + */ + function extensiveDeepEqual(leftHandOperand, rightHandOperand, options) { + options = options || {}; + options.memoize = options.memoize === false ? false : options.memoize || new MemoizeMap(); + var comparator = options && options.comparator; + + // Check if a memoized result exists. + var memoizeResultLeft = memoizeCompare(leftHandOperand, rightHandOperand, options.memoize); + if (memoizeResultLeft !== null) { + return memoizeResultLeft; + } + var memoizeResultRight = memoizeCompare(rightHandOperand, leftHandOperand, options.memoize); + if (memoizeResultRight !== null) { + return memoizeResultRight; + } + + // If a comparator is present, use it. + if (comparator) { + var comparatorResult = comparator(leftHandOperand, rightHandOperand); + // Comparators may return null, in which case we want to go back to default behavior. + if (comparatorResult === false || comparatorResult === true) { + memoizeSet(leftHandOperand, rightHandOperand, options.memoize, comparatorResult); + return comparatorResult; + } + // To allow comparators to override *any* behavior, we ran them first. Since it didn't decide + // what to do, we need to make sure to return the basic tests first before we move on. + var simpleResult = simpleEqual(leftHandOperand, rightHandOperand); + if (simpleResult !== null) { + // Don't memoize this, it takes longer to set/retrieve than to just compare. + return simpleResult; + } + } + + var leftHandType = type(leftHandOperand); + if (leftHandType !== type(rightHandOperand)) { + memoizeSet(leftHandOperand, rightHandOperand, options.memoize, false); + return false; + } + + // Temporarily set the operands in the memoize object to prevent blowing the stack + memoizeSet(leftHandOperand, rightHandOperand, options.memoize, true); + + var result = extensiveDeepEqualByType(leftHandOperand, rightHandOperand, leftHandType, options); + memoizeSet(leftHandOperand, rightHandOperand, options.memoize, result); + return result; + } + + function extensiveDeepEqualByType(leftHandOperand, rightHandOperand, leftHandType, options) { + switch (leftHandType) { + case 'String': + case 'Number': + case 'Boolean': + case 'Date': + // If these types are their instance types (e.g. `new Number`) then re-deepEqual against their values + return deepEqual(leftHandOperand.valueOf(), rightHandOperand.valueOf()); + case 'Promise': + case 'Symbol': + case 'function': + case 'WeakMap': + case 'WeakSet': + case 'Error': + return leftHandOperand === rightHandOperand; + case 'Arguments': + case 'Int8Array': + case 'Uint8Array': + case 'Uint8ClampedArray': + case 'Int16Array': + case 'Uint16Array': + case 'Int32Array': + case 'Uint32Array': + case 'Float32Array': + case 'Float64Array': + case 'Array': + return iterableEqual(leftHandOperand, rightHandOperand, options); + case 'RegExp': + return regexpEqual(leftHandOperand, rightHandOperand); + case 'Generator': + return generatorEqual(leftHandOperand, rightHandOperand, options); + case 'DataView': + return iterableEqual(new Uint8Array(leftHandOperand.buffer), new Uint8Array(rightHandOperand.buffer), options); + case 'ArrayBuffer': + return iterableEqual(new Uint8Array(leftHandOperand), new Uint8Array(rightHandOperand), options); + case 'Set': + return entriesEqual(leftHandOperand, rightHandOperand, options); + case 'Map': + return entriesEqual(leftHandOperand, rightHandOperand, options); + default: + return objectEqual(leftHandOperand, rightHandOperand, options); + } + } + + /*! + * Compare two Regular Expressions for equality. + * + * @param {RegExp} leftHandOperand + * @param {RegExp} rightHandOperand + * @return {Boolean} result + */ + + function regexpEqual(leftHandOperand, rightHandOperand) { + return leftHandOperand.toString() === rightHandOperand.toString(); + } + + /*! + * Compare two Sets/Maps for equality. Faster than other equality functions. + * + * @param {Set} leftHandOperand + * @param {Set} rightHandOperand + * @param {Object} [options] (Optional) + * @return {Boolean} result + */ + + function entriesEqual(leftHandOperand, rightHandOperand, options) { + // IE11 doesn't support Set#entries or Set#@@iterator, so we need manually populate using Set#forEach + if (leftHandOperand.size !== rightHandOperand.size) { + return false; + } + if (leftHandOperand.size === 0) { + return true; + } + var leftHandItems = []; + var rightHandItems = []; + leftHandOperand.forEach(function gatherEntries(key, value) { + leftHandItems.push([ key, value ]); + }); + rightHandOperand.forEach(function gatherEntries(key, value) { + rightHandItems.push([ key, value ]); + }); + return iterableEqual(leftHandItems.sort(), rightHandItems.sort(), options); + } + + /*! + * Simple equality for flat iterable objects such as Arrays, TypedArrays or Node.js buffers. + * + * @param {Iterable} leftHandOperand + * @param {Iterable} rightHandOperand + * @param {Object} [options] (Optional) + * @return {Boolean} result + */ + + function iterableEqual(leftHandOperand, rightHandOperand, options) { + var length = leftHandOperand.length; + if (length !== rightHandOperand.length) { + return false; + } + if (length === 0) { + return true; + } + var index = -1; + while (++index < length) { + if (deepEqual(leftHandOperand[index], rightHandOperand[index], options) === false) { + return false; + } + } + return true; + } + + /*! + * Simple equality for generator objects such as those returned by generator functions. + * + * @param {Iterable} leftHandOperand + * @param {Iterable} rightHandOperand + * @param {Object} [options] (Optional) + * @return {Boolean} result + */ + + function generatorEqual(leftHandOperand, rightHandOperand, options) { + return iterableEqual(getGeneratorEntries(leftHandOperand), getGeneratorEntries(rightHandOperand), options); + } + + /*! + * Determine if the given object has an @@iterator function. + * + * @param {Object} target + * @return {Boolean} `true` if the object has an @@iterator function. + */ + function hasIteratorFunction(target) { + return typeof Symbol !== 'undefined' && + typeof target === 'object' && + typeof Symbol.iterator !== 'undefined' && + typeof target[Symbol.iterator] === 'function'; + } + + /*! + * Gets all iterator entries from the given Object. If the Object has no @@iterator function, returns an empty array. + * This will consume the iterator - which could have side effects depending on the @@iterator implementation. + * + * @param {Object} target + * @returns {Array} an array of entries from the @@iterator function + */ + function getIteratorEntries(target) { + if (hasIteratorFunction(target)) { + try { + return getGeneratorEntries(target[Symbol.iterator]()); + } catch (iteratorError) { + return []; + } + } + return []; + } + + /*! + * Gets all entries from a Generator. This will consume the generator - which could have side effects. + * + * @param {Generator} target + * @returns {Array} an array of entries from the Generator. + */ + function getGeneratorEntries(generator) { + var generatorResult = generator.next(); + var accumulator = [ generatorResult.value ]; + while (generatorResult.done === false) { + generatorResult = generator.next(); + accumulator.push(generatorResult.value); + } + return accumulator; + } + + /*! + * Gets all own and inherited enumerable keys from a target. + * + * @param {Object} target + * @returns {Array} an array of own and inherited enumerable keys from the target. + */ + function getEnumerableKeys(target) { + var keys = []; + for (var key in target) { + keys.push(key); + } + return keys; + } + + /*! + * Determines if two objects have matching values, given a set of keys. Defers to deepEqual for the equality check of + * each key. If any value of the given key is not equal, the function will return false (early). + * + * @param {Mixed} leftHandOperand + * @param {Mixed} rightHandOperand + * @param {Array} keys An array of keys to compare the values of leftHandOperand and rightHandOperand against + * @param {Object} [options] (Optional) + * @return {Boolean} result + */ + function keysEqual(leftHandOperand, rightHandOperand, keys, options) { + var length = keys.length; + if (length === 0) { + return true; + } + for (var i = 0; i < length; i += 1) { + if (deepEqual(leftHandOperand[keys[i]], rightHandOperand[keys[i]], options) === false) { + return false; + } + } + return true; + } + + /*! + * Recursively check the equality of two Objects. Once basic sameness has been established it will defer to `deepEqual` + * for each enumerable key in the object. + * + * @param {Mixed} leftHandOperand + * @param {Mixed} rightHandOperand + * @param {Object} [options] (Optional) + * @return {Boolean} result + */ + + function objectEqual(leftHandOperand, rightHandOperand, options) { + var leftHandKeys = getEnumerableKeys(leftHandOperand); + var rightHandKeys = getEnumerableKeys(rightHandOperand); + if (leftHandKeys.length && leftHandKeys.length === rightHandKeys.length) { + leftHandKeys.sort(); + rightHandKeys.sort(); + if (iterableEqual(leftHandKeys, rightHandKeys) === false) { + return false; + } + return keysEqual(leftHandOperand, rightHandOperand, leftHandKeys, options); + } + + var leftHandEntries = getIteratorEntries(leftHandOperand); + var rightHandEntries = getIteratorEntries(rightHandOperand); + if (leftHandEntries.length && leftHandEntries.length === rightHandEntries.length) { + leftHandEntries.sort(); + rightHandEntries.sort(); + return iterableEqual(leftHandEntries, rightHandEntries, options); + } + + if (leftHandKeys.length === 0 && + leftHandEntries.length === 0 && + rightHandKeys.length === 0 && + rightHandEntries.length === 0) { + return true; + } + + return false; + } + + /*! + * Returns true if the argument is a primitive. + * + * This intentionally returns true for all objects that can be compared by reference, + * including functions and symbols. + * + * @param {Mixed} value + * @return {Boolean} result + */ + function isPrimitive(value) { + return value === null || typeof value !== 'object'; + } + + },{"type-detect":38}],36:[function(require,module,exports){ + 'use strict'; + + /* ! + * Chai - getFuncName utility + * Copyright(c) 2012-2016 Jake Luer + * MIT Licensed + */ + + /** + * ### .getFuncName(constructorFn) + * + * Returns the name of a function. + * When a non-function instance is passed, returns `null`. + * This also includes a polyfill function if `aFunc.name` is not defined. + * + * @name getFuncName + * @param {Function} funct + * @namespace Utils + * @api public + */ + + var toString = Function.prototype.toString; + var functionNameMatch = /\s*function(?:\s|\s*\/\*[^(?:*\/)]+\*\/\s*)*([^\s\(\/]+)/; + function getFuncName(aFunc) { + if (typeof aFunc !== 'function') { + return null; + } + + var name = ''; + if (typeof Function.prototype.name === 'undefined' && typeof aFunc.name === 'undefined') { + // Here we run a polyfill if Function does not support the `name` property and if aFunc.name is not defined + var match = toString.call(aFunc).match(functionNameMatch); + if (match) { + name = match[1]; + } + } else { + // If we've got a `name` property we just use it + name = aFunc.name; + } + + return name; + } + + module.exports = getFuncName; + + },{}],37:[function(require,module,exports){ + 'use strict'; + + /* ! + * Chai - pathval utility + * Copyright(c) 2012-2014 Jake Luer + * @see https://github.com/logicalparadox/filtr + * MIT Licensed + */ + + /** + * ### .hasProperty(object, name) + * + * This allows checking whether an object has own + * or inherited from prototype chain named property. + * + * Basically does the same thing as the `in` + * operator but works properly with null/undefined values + * and other primitives. + * + * var obj = { + * arr: ['a', 'b', 'c'] + * , str: 'Hello' + * } + * + * The following would be the results. + * + * hasProperty(obj, 'str'); // true + * hasProperty(obj, 'constructor'); // true + * hasProperty(obj, 'bar'); // false + * + * hasProperty(obj.str, 'length'); // true + * hasProperty(obj.str, 1); // true + * hasProperty(obj.str, 5); // false + * + * hasProperty(obj.arr, 'length'); // true + * hasProperty(obj.arr, 2); // true + * hasProperty(obj.arr, 3); // false + * + * @param {Object} object + * @param {String|Symbol} name + * @returns {Boolean} whether it exists + * @namespace Utils + * @name hasProperty + * @api public + */ + + function hasProperty(obj, name) { + if (typeof obj === 'undefined' || obj === null) { + return false; + } + + // The `in` operator does not work with primitives. + return name in Object(obj); + } + + /* ! + * ## parsePath(path) + * + * Helper function used to parse string object + * paths. Use in conjunction with `internalGetPathValue`. + * + * var parsed = parsePath('myobject.property.subprop'); + * + * ### Paths: + * + * * Can be infinitely deep and nested. + * * Arrays are also valid using the formal `myobject.document[3].property`. + * * Literal dots and brackets (not delimiter) must be backslash-escaped. + * + * @param {String} path + * @returns {Object} parsed + * @api private + */ + + function parsePath(path) { + var str = path.replace(/([^\\])\[/g, '$1.['); + var parts = str.match(/(\\\.|[^.]+?)+/g); + return parts.map(function mapMatches(value) { + var regexp = /^\[(\d+)\]$/; + var mArr = regexp.exec(value); + var parsed = null; + if (mArr) { + parsed = { i: parseFloat(mArr[1]) }; + } else { + parsed = { p: value.replace(/\\([.\[\]])/g, '$1') }; + } + + return parsed; + }); + } + + /* ! + * ## internalGetPathValue(obj, parsed[, pathDepth]) + * + * Helper companion function for `.parsePath` that returns + * the value located at the parsed address. + * + * var value = getPathValue(obj, parsed); + * + * @param {Object} object to search against + * @param {Object} parsed definition from `parsePath`. + * @param {Number} depth (nesting level) of the property we want to retrieve + * @returns {Object|Undefined} value + * @api private + */ + + function internalGetPathValue(obj, parsed, pathDepth) { + var temporaryValue = obj; + var res = null; + pathDepth = (typeof pathDepth === 'undefined' ? parsed.length : pathDepth); + + for (var i = 0; i < pathDepth; i++) { + var part = parsed[i]; + if (temporaryValue) { + if (typeof part.p === 'undefined') { + temporaryValue = temporaryValue[part.i]; + } else { + temporaryValue = temporaryValue[part.p]; + } + + if (i === (pathDepth - 1)) { + res = temporaryValue; + } + } + } + + return res; + } + + /* ! + * ## internalSetPathValue(obj, value, parsed) + * + * Companion function for `parsePath` that sets + * the value located at a parsed address. + * + * internalSetPathValue(obj, 'value', parsed); + * + * @param {Object} object to search and define on + * @param {*} value to use upon set + * @param {Object} parsed definition from `parsePath` + * @api private + */ + + function internalSetPathValue(obj, val, parsed) { + var tempObj = obj; + var pathDepth = parsed.length; + var part = null; + // Here we iterate through every part of the path + for (var i = 0; i < pathDepth; i++) { + var propName = null; + var propVal = null; + part = parsed[i]; + + // If it's the last part of the path, we set the 'propName' value with the property name + if (i === (pathDepth - 1)) { + propName = typeof part.p === 'undefined' ? part.i : part.p; + // Now we set the property with the name held by 'propName' on object with the desired val + tempObj[propName] = val; + } else if (typeof part.p !== 'undefined' && tempObj[part.p]) { + tempObj = tempObj[part.p]; + } else if (typeof part.i !== 'undefined' && tempObj[part.i]) { + tempObj = tempObj[part.i]; + } else { + // If the obj doesn't have the property we create one with that name to define it + var next = parsed[i + 1]; + // Here we set the name of the property which will be defined + propName = typeof part.p === 'undefined' ? part.i : part.p; + // Here we decide if this property will be an array or a new object + propVal = typeof next.p === 'undefined' ? [] : {}; + tempObj[propName] = propVal; + tempObj = tempObj[propName]; + } + } + } + + /** + * ### .getPathInfo(object, path) + * + * This allows the retrieval of property info in an + * object given a string path. + * + * The path info consists of an object with the + * following properties: + * + * * parent - The parent object of the property referenced by `path` + * * name - The name of the final property, a number if it was an array indexer + * * value - The value of the property, if it exists, otherwise `undefined` + * * exists - Whether the property exists or not + * + * @param {Object} object + * @param {String} path + * @returns {Object} info + * @namespace Utils + * @name getPathInfo + * @api public + */ + + function getPathInfo(obj, path) { + var parsed = parsePath(path); + var last = parsed[parsed.length - 1]; + var info = { + parent: parsed.length > 1 ? internalGetPathValue(obj, parsed, parsed.length - 1) : obj, + name: last.p || last.i, + value: internalGetPathValue(obj, parsed), + }; + info.exists = hasProperty(info.parent, info.name); + + return info; + } + + /** + * ### .getPathValue(object, path) + * + * This allows the retrieval of values in an + * object given a string path. + * + * var obj = { + * prop1: { + * arr: ['a', 'b', 'c'] + * , str: 'Hello' + * } + * , prop2: { + * arr: [ { nested: 'Universe' } ] + * , str: 'Hello again!' + * } + * } + * + * The following would be the results. + * + * getPathValue(obj, 'prop1.str'); // Hello + * getPathValue(obj, 'prop1.att[2]'); // b + * getPathValue(obj, 'prop2.arr[0].nested'); // Universe + * + * @param {Object} object + * @param {String} path + * @returns {Object} value or `undefined` + * @namespace Utils + * @name getPathValue + * @api public + */ + + function getPathValue(obj, path) { + var info = getPathInfo(obj, path); + return info.value; + } + + /** + * ### .setPathValue(object, path, value) + * + * Define the value in an object at a given string path. + * + * ```js + * var obj = { + * prop1: { + * arr: ['a', 'b', 'c'] + * , str: 'Hello' + * } + * , prop2: { + * arr: [ { nested: 'Universe' } ] + * , str: 'Hello again!' + * } + * }; + * ``` + * + * The following would be acceptable. + * + * ```js + * var properties = require('tea-properties'); + * properties.set(obj, 'prop1.str', 'Hello Universe!'); + * properties.set(obj, 'prop1.arr[2]', 'B'); + * properties.set(obj, 'prop2.arr[0].nested.value', { hello: 'universe' }); + * ``` + * + * @param {Object} object + * @param {String} path + * @param {Mixed} value + * @api private + */ + + function setPathValue(obj, path, val) { + var parsed = parsePath(path); + internalSetPathValue(obj, val, parsed); + return obj; + } + + module.exports = { + hasProperty: hasProperty, + getPathInfo: getPathInfo, + getPathValue: getPathValue, + setPathValue: setPathValue, + }; + + },{}],38:[function(require,module,exports){ + (function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : + typeof define === 'function' && define.amd ? define(factory) : + (global.typeDetect = factory()); + }(this, (function () { 'use strict'; + + /* ! + * type-detect + * Copyright(c) 2013 jake luer + * MIT Licensed + */ + var promiseExists = typeof Promise === 'function'; + + /* eslint-disable no-undef */ + var globalObject = typeof self === 'object' ? self : global; // eslint-disable-line id-blacklist + + var symbolExists = typeof Symbol !== 'undefined'; + var mapExists = typeof Map !== 'undefined'; + var setExists = typeof Set !== 'undefined'; + var weakMapExists = typeof WeakMap !== 'undefined'; + var weakSetExists = typeof WeakSet !== 'undefined'; + var dataViewExists = typeof DataView !== 'undefined'; + var symbolIteratorExists = symbolExists && typeof Symbol.iterator !== 'undefined'; + var symbolToStringTagExists = symbolExists && typeof Symbol.toStringTag !== 'undefined'; + var setEntriesExists = setExists && typeof Set.prototype.entries === 'function'; + var mapEntriesExists = mapExists && typeof Map.prototype.entries === 'function'; + var setIteratorPrototype = setEntriesExists && Object.getPrototypeOf(new Set().entries()); + var mapIteratorPrototype = mapEntriesExists && Object.getPrototypeOf(new Map().entries()); + var arrayIteratorExists = symbolIteratorExists && typeof Array.prototype[Symbol.iterator] === 'function'; + var arrayIteratorPrototype = arrayIteratorExists && Object.getPrototypeOf([][Symbol.iterator]()); + var stringIteratorExists = symbolIteratorExists && typeof String.prototype[Symbol.iterator] === 'function'; + var stringIteratorPrototype = stringIteratorExists && Object.getPrototypeOf(''[Symbol.iterator]()); + var toStringLeftSliceLength = 8; + var toStringRightSliceLength = -1; + /** + * ### typeOf (obj) + * + * Uses `Object.prototype.toString` to determine the type of an object, + * normalising behaviour across engine versions & well optimised. + * + * @param {Mixed} object + * @return {String} object type + * @api public + */ + function typeDetect(obj) { + /* ! Speed optimisation + * Pre: + * string literal x 3,039,035 ops/sec ±1.62% (78 runs sampled) + * boolean literal x 1,424,138 ops/sec ±4.54% (75 runs sampled) + * number literal x 1,653,153 ops/sec ±1.91% (82 runs sampled) + * undefined x 9,978,660 ops/sec ±1.92% (75 runs sampled) + * function x 2,556,769 ops/sec ±1.73% (77 runs sampled) + * Post: + * string literal x 38,564,796 ops/sec ±1.15% (79 runs sampled) + * boolean literal x 31,148,940 ops/sec ±1.10% (79 runs sampled) + * number literal x 32,679,330 ops/sec ±1.90% (78 runs sampled) + * undefined x 32,363,368 ops/sec ±1.07% (82 runs sampled) + * function x 31,296,870 ops/sec ±0.96% (83 runs sampled) + */ + var typeofObj = typeof obj; + if (typeofObj !== 'object') { + return typeofObj; + } + + /* ! Speed optimisation + * Pre: + * null x 28,645,765 ops/sec ±1.17% (82 runs sampled) + * Post: + * null x 36,428,962 ops/sec ±1.37% (84 runs sampled) + */ + if (obj === null) { + return 'null'; + } + + /* ! Spec Conformance + * Test: `Object.prototype.toString.call(window)`` + * - Node === "[object global]" + * - Chrome === "[object global]" + * - Firefox === "[object Window]" + * - PhantomJS === "[object Window]" + * - Safari === "[object Window]" + * - IE 11 === "[object Window]" + * - IE Edge === "[object Window]" + * Test: `Object.prototype.toString.call(this)`` + * - Chrome Worker === "[object global]" + * - Firefox Worker === "[object DedicatedWorkerGlobalScope]" + * - Safari Worker === "[object DedicatedWorkerGlobalScope]" + * - IE 11 Worker === "[object WorkerGlobalScope]" + * - IE Edge Worker === "[object WorkerGlobalScope]" + */ + if (obj === globalObject) { + return 'global'; + } + + /* ! Speed optimisation + * Pre: + * array literal x 2,888,352 ops/sec ±0.67% (82 runs sampled) + * Post: + * array literal x 22,479,650 ops/sec ±0.96% (81 runs sampled) + */ + if ( + Array.isArray(obj) && + (symbolToStringTagExists === false || !(Symbol.toStringTag in obj)) + ) { + return 'Array'; + } + + // Not caching existence of `window` and related properties due to potential + // for `window` to be unset before tests in quasi-browser environments. + if (typeof window === 'object' && window !== null) { + /* ! Spec Conformance + * (https://html.spec.whatwg.org/multipage/browsers.html#location) + * WhatWG HTML$7.7.3 - The `Location` interface + * Test: `Object.prototype.toString.call(window.location)`` + * - IE <=11 === "[object Object]" + * - IE Edge <=13 === "[object Object]" + */ + if (typeof window.location === 'object' && obj === window.location) { + return 'Location'; + } + + /* ! Spec Conformance + * (https://html.spec.whatwg.org/#document) + * WhatWG HTML$3.1.1 - The `Document` object + * Note: Most browsers currently adher to the W3C DOM Level 2 spec + * (https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-26809268) + * which suggests that browsers should use HTMLTableCellElement for + * both TD and TH elements. WhatWG separates these. + * WhatWG HTML states: + * > For historical reasons, Window objects must also have a + * > writable, configurable, non-enumerable property named + * > HTMLDocument whose value is the Document interface object. + * Test: `Object.prototype.toString.call(document)`` + * - Chrome === "[object HTMLDocument]" + * - Firefox === "[object HTMLDocument]" + * - Safari === "[object HTMLDocument]" + * - IE <=10 === "[object Document]" + * - IE 11 === "[object HTMLDocument]" + * - IE Edge <=13 === "[object HTMLDocument]" + */ + if (typeof window.document === 'object' && obj === window.document) { + return 'Document'; + } + + if (typeof window.navigator === 'object') { + /* ! Spec Conformance + * (https://html.spec.whatwg.org/multipage/webappapis.html#mimetypearray) + * WhatWG HTML$8.6.1.5 - Plugins - Interface MimeTypeArray + * Test: `Object.prototype.toString.call(navigator.mimeTypes)`` + * - IE <=10 === "[object MSMimeTypesCollection]" + */ + if (typeof window.navigator.mimeTypes === 'object' && + obj === window.navigator.mimeTypes) { + return 'MimeTypeArray'; + } + + /* ! Spec Conformance + * (https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray) + * WhatWG HTML$8.6.1.5 - Plugins - Interface PluginArray + * Test: `Object.prototype.toString.call(navigator.plugins)`` + * - IE <=10 === "[object MSPluginsCollection]" + */ + if (typeof window.navigator.plugins === 'object' && + obj === window.navigator.plugins) { + return 'PluginArray'; + } + } + + if ((typeof window.HTMLElement === 'function' || + typeof window.HTMLElement === 'object') && + obj instanceof window.HTMLElement) { + /* ! Spec Conformance + * (https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray) + * WhatWG HTML$4.4.4 - The `blockquote` element - Interface `HTMLQuoteElement` + * Test: `Object.prototype.toString.call(document.createElement('blockquote'))`` + * - IE <=10 === "[object HTMLBlockElement]" + */ + if (obj.tagName === 'BLOCKQUOTE') { + return 'HTMLQuoteElement'; + } + + /* ! Spec Conformance + * (https://html.spec.whatwg.org/#htmltabledatacellelement) + * WhatWG HTML$4.9.9 - The `td` element - Interface `HTMLTableDataCellElement` + * Note: Most browsers currently adher to the W3C DOM Level 2 spec + * (https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-82915075) + * which suggests that browsers should use HTMLTableCellElement for + * both TD and TH elements. WhatWG separates these. + * Test: Object.prototype.toString.call(document.createElement('td')) + * - Chrome === "[object HTMLTableCellElement]" + * - Firefox === "[object HTMLTableCellElement]" + * - Safari === "[object HTMLTableCellElement]" + */ + if (obj.tagName === 'TD') { + return 'HTMLTableDataCellElement'; + } + + /* ! Spec Conformance + * (https://html.spec.whatwg.org/#htmltableheadercellelement) + * WhatWG HTML$4.9.9 - The `td` element - Interface `HTMLTableHeaderCellElement` + * Note: Most browsers currently adher to the W3C DOM Level 2 spec + * (https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-82915075) + * which suggests that browsers should use HTMLTableCellElement for + * both TD and TH elements. WhatWG separates these. + * Test: Object.prototype.toString.call(document.createElement('th')) + * - Chrome === "[object HTMLTableCellElement]" + * - Firefox === "[object HTMLTableCellElement]" + * - Safari === "[object HTMLTableCellElement]" + */ + if (obj.tagName === 'TH') { + return 'HTMLTableHeaderCellElement'; + } + } + } + + /* ! Speed optimisation + * Pre: + * Float64Array x 625,644 ops/sec ±1.58% (80 runs sampled) + * Float32Array x 1,279,852 ops/sec ±2.91% (77 runs sampled) + * Uint32Array x 1,178,185 ops/sec ±1.95% (83 runs sampled) + * Uint16Array x 1,008,380 ops/sec ±2.25% (80 runs sampled) + * Uint8Array x 1,128,040 ops/sec ±2.11% (81 runs sampled) + * Int32Array x 1,170,119 ops/sec ±2.88% (80 runs sampled) + * Int16Array x 1,176,348 ops/sec ±5.79% (86 runs sampled) + * Int8Array x 1,058,707 ops/sec ±4.94% (77 runs sampled) + * Uint8ClampedArray x 1,110,633 ops/sec ±4.20% (80 runs sampled) + * Post: + * Float64Array x 7,105,671 ops/sec ±13.47% (64 runs sampled) + * Float32Array x 5,887,912 ops/sec ±1.46% (82 runs sampled) + * Uint32Array x 6,491,661 ops/sec ±1.76% (79 runs sampled) + * Uint16Array x 6,559,795 ops/sec ±1.67% (82 runs sampled) + * Uint8Array x 6,463,966 ops/sec ±1.43% (85 runs sampled) + * Int32Array x 5,641,841 ops/sec ±3.49% (81 runs sampled) + * Int16Array x 6,583,511 ops/sec ±1.98% (80 runs sampled) + * Int8Array x 6,606,078 ops/sec ±1.74% (81 runs sampled) + * Uint8ClampedArray x 6,602,224 ops/sec ±1.77% (83 runs sampled) + */ + var stringTag = (symbolToStringTagExists && obj[Symbol.toStringTag]); + if (typeof stringTag === 'string') { + return stringTag; + } + + var objPrototype = Object.getPrototypeOf(obj); + /* ! Speed optimisation + * Pre: + * regex literal x 1,772,385 ops/sec ±1.85% (77 runs sampled) + * regex constructor x 2,143,634 ops/sec ±2.46% (78 runs sampled) + * Post: + * regex literal x 3,928,009 ops/sec ±0.65% (78 runs sampled) + * regex constructor x 3,931,108 ops/sec ±0.58% (84 runs sampled) + */ + if (objPrototype === RegExp.prototype) { + return 'RegExp'; + } + + /* ! Speed optimisation + * Pre: + * date x 2,130,074 ops/sec ±4.42% (68 runs sampled) + * Post: + * date x 3,953,779 ops/sec ±1.35% (77 runs sampled) + */ + if (objPrototype === Date.prototype) { + return 'Date'; + } + + /* ! Spec Conformance + * (http://www.ecma-international.org/ecma-262/6.0/index.html#sec-promise.prototype-@@tostringtag) + * ES6$25.4.5.4 - Promise.prototype[@@toStringTag] should be "Promise": + * Test: `Object.prototype.toString.call(Promise.resolve())`` + * - Chrome <=47 === "[object Object]" + * - Edge <=20 === "[object Object]" + * - Firefox 29-Latest === "[object Promise]" + * - Safari 7.1-Latest === "[object Promise]" + */ + if (promiseExists && objPrototype === Promise.prototype) { + return 'Promise'; + } + + /* ! Speed optimisation + * Pre: + * set x 2,222,186 ops/sec ±1.31% (82 runs sampled) + * Post: + * set x 4,545,879 ops/sec ±1.13% (83 runs sampled) + */ + if (setExists && objPrototype === Set.prototype) { + return 'Set'; + } + + /* ! Speed optimisation + * Pre: + * map x 2,396,842 ops/sec ±1.59% (81 runs sampled) + * Post: + * map x 4,183,945 ops/sec ±6.59% (82 runs sampled) + */ + if (mapExists && objPrototype === Map.prototype) { + return 'Map'; + } + + /* ! Speed optimisation + * Pre: + * weakset x 1,323,220 ops/sec ±2.17% (76 runs sampled) + * Post: + * weakset x 4,237,510 ops/sec ±2.01% (77 runs sampled) + */ + if (weakSetExists && objPrototype === WeakSet.prototype) { + return 'WeakSet'; + } + + /* ! Speed optimisation + * Pre: + * weakmap x 1,500,260 ops/sec ±2.02% (78 runs sampled) + * Post: + * weakmap x 3,881,384 ops/sec ±1.45% (82 runs sampled) + */ + if (weakMapExists && objPrototype === WeakMap.prototype) { + return 'WeakMap'; + } + + /* ! Spec Conformance + * (http://www.ecma-international.org/ecma-262/6.0/index.html#sec-dataview.prototype-@@tostringtag) + * ES6$24.2.4.21 - DataView.prototype[@@toStringTag] should be "DataView": + * Test: `Object.prototype.toString.call(new DataView(new ArrayBuffer(1)))`` + * - Edge <=13 === "[object Object]" + */ + if (dataViewExists && objPrototype === DataView.prototype) { + return 'DataView'; + } + + /* ! Spec Conformance + * (http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%mapiteratorprototype%-@@tostringtag) + * ES6$23.1.5.2.2 - %MapIteratorPrototype%[@@toStringTag] should be "Map Iterator": + * Test: `Object.prototype.toString.call(new Map().entries())`` + * - Edge <=13 === "[object Object]" + */ + if (mapExists && objPrototype === mapIteratorPrototype) { + return 'Map Iterator'; + } + + /* ! Spec Conformance + * (http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%setiteratorprototype%-@@tostringtag) + * ES6$23.2.5.2.2 - %SetIteratorPrototype%[@@toStringTag] should be "Set Iterator": + * Test: `Object.prototype.toString.call(new Set().entries())`` + * - Edge <=13 === "[object Object]" + */ + if (setExists && objPrototype === setIteratorPrototype) { + return 'Set Iterator'; + } + + /* ! Spec Conformance + * (http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%arrayiteratorprototype%-@@tostringtag) + * ES6$22.1.5.2.2 - %ArrayIteratorPrototype%[@@toStringTag] should be "Array Iterator": + * Test: `Object.prototype.toString.call([][Symbol.iterator]())`` + * - Edge <=13 === "[object Object]" + */ + if (arrayIteratorExists && objPrototype === arrayIteratorPrototype) { + return 'Array Iterator'; + } + + /* ! Spec Conformance + * (http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%stringiteratorprototype%-@@tostringtag) + * ES6$21.1.5.2.2 - %StringIteratorPrototype%[@@toStringTag] should be "String Iterator": + * Test: `Object.prototype.toString.call(''[Symbol.iterator]())`` + * - Edge <=13 === "[object Object]" + */ + if (stringIteratorExists && objPrototype === stringIteratorPrototype) { + return 'String Iterator'; + } + + /* ! Speed optimisation + * Pre: + * object from null x 2,424,320 ops/sec ±1.67% (76 runs sampled) + * Post: + * object from null x 5,838,000 ops/sec ±0.99% (84 runs sampled) + */ + if (objPrototype === null) { + return 'Object'; + } + + return Object + .prototype + .toString + .call(obj) + .slice(toStringLeftSliceLength, toStringRightSliceLength); + } + + return typeDetect; + + }))); + + },{}]},{},[1])(1) + }); \ No newline at end of file From f1721a94c0c96d70a534eb5a145f682c2e69aa53 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Fri, 4 Oct 2019 06:39:00 -0600 Subject: [PATCH 062/166] Fixed up Metadata, added some tests. Fix Issue #23 for Android and part of https://github.com/miloproductionsinc/cordova-plugin-chromecast/issues/3 Also some better null checking. And better images handling. Add RepeatMode to media output. Issue #36 --- package.json | 2 +- src/android/Chromecast.java | 6 +- src/android/ChromecastSession.java | 71 +++++---- src/android/ChromecastUtilities.java | 212 ++++++++++++++++++++++++--- tests/www/js/tests_auto.js | 58 +++++--- tests/www/js/utils.js | 37 ++++- www/chrome.cast.js | 44 ++++-- 7 files changed, 346 insertions(+), 84 deletions(-) diff --git a/package.json b/package.json index 5d3c7de..136168c 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "scripts": { "host-chrome-tests": "node tests/www/chrome/host-tests.js", "style-fix-js": "node node_modules/eslint/bin/eslint --fix src && node node_modules/eslint/bin/eslint --fix www && node node_modules/eslint/bin/eslint --ignore-pattern tests/www/lib --fix tests/www", - "test": "node node_modules/eslint/bin/eslint src && node node_modules/eslint/bin/eslint www && node node_modules/eslint/bin/eslint tests/www && node ./node_modules/java-checkstyle/bin/index.js ./src/android/ -c ./check_style.xml", + "test": "node node_modules/eslint/bin/eslint src && node node_modules/eslint/bin/eslint www && node node_modules/eslint/bin/eslint --ignore-pattern tests/www/lib tests/www && node ./node_modules/java-checkstyle/bin/index.js ./src/android/ -c ./check_style.xml", "style": "npm run style-fix-js && npm run test" }, "author": "", diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 0129823..3c01000 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -59,7 +59,11 @@ public void onMediaLoaded(JSONObject jsonMedia) { } @Override public void onMediaUpdate(JSONObject jsonMedia) { - sendEvent("MEDIA_UPDATE", new JSONArray().put(jsonMedia)); + JSONArray out = new JSONArray(); + if (jsonMedia != null) { + out.put(jsonMedia); + } + sendEvent("MEDIA_UPDATE", out); } @Override public void onMessageReceived(CastDevice device, String namespace, String message) { diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index ead184a..c884a6b 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -1,6 +1,7 @@ package acidhax.cordova.chromecast; import java.io.IOException; +import java.util.Iterator; import org.apache.cordova.CallbackContext; import org.json.JSONArray; @@ -224,7 +225,12 @@ public void run() { @Override public void onResult(@NonNull MediaChannelResult result) { if (result.getStatus().isSuccess()) { - callback.success(createMediaObject()); + JSONObject out = createMediaObject(); + if (out == null) { + callback.success(); + } else { + callback.success(out); + } } else { callback.error("session_error"); } @@ -235,18 +241,35 @@ public void onResult(@NonNull MediaChannelResult result) { } private MediaInfo createMediaInfo(String contentId, JSONObject customData, String contentType, long duration, String streamType, JSONObject metadata, JSONObject textTrackStyle) { - // create GENERIC MediaMetadata first and fallback to movie - MediaMetadata mediaMetadata = new MediaMetadata(); + MediaInfo.Builder mediaInfoBuilder = new MediaInfo.Builder(contentId); + + MediaMetadata mediaMetadata; try { - int metadataType = metadata.has("metadataType") ? metadata.getInt("metadataType") : MediaMetadata.MEDIA_TYPE_MOVIE; - if (metadataType == MediaMetadata.MEDIA_TYPE_GENERIC) { - mediaMetadata.putString(MediaMetadata.KEY_TITLE, (metadata.has("title")) ? metadata.getString("title") : "[Title not set]"); // TODO: What should it default to? - mediaMetadata.putString(MediaMetadata.KEY_SUBTITLE, (metadata.has("title")) ? metadata.getString("subtitle") : "[Subtitle not set]"); // TODO: What should it default to? - mediaMetadata = addImages(metadata, mediaMetadata); + int metadataType = metadata.has("metadataType") ? metadata.getInt("metadataType") : MediaMetadata.MEDIA_TYPE_GENERIC; + // Set the metadataType + mediaMetadata = new MediaMetadata(metadataType); + // Add any images + addImages(metadata, mediaMetadata); + + // Dynamically add other parameters + Iterator keys = metadata.keys(); + String key; + String value; + while (keys.hasNext()) { + key = keys.next(); + value = metadata.getString(key); + if (key.equals("metadataType") + || key.equals("images") + || key.equals("type")) { + continue; + } + key = ChromecastUtilities.getAndroidMetadataName(key); + mediaMetadata.putString(key, value); } + + mediaInfoBuilder.setMetadata(mediaMetadata); + } catch (Exception e) { - e.printStackTrace(); - mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE); } int intStreamType; @@ -262,33 +285,29 @@ private MediaInfo createMediaInfo(String contentId, JSONObject customData, Strin } TextTrackStyle trackStyle = ChromecastUtilities.parseTextTrackStyle(textTrackStyle); - MediaInfo mediaInfo = new MediaInfo.Builder(contentId) + + mediaInfoBuilder .setContentType(contentType) .setCustomData(customData) .setStreamType(intStreamType) .setStreamDuration(duration) - .setMetadata(mediaMetadata) - .setTextTrackStyle(trackStyle) - .build(); + .setTextTrackStyle(trackStyle); - return mediaInfo; + return mediaInfoBuilder.build(); } - private MediaMetadata addImages(JSONObject metadata, MediaMetadata mediaMetadata) throws JSONException { + private void addImages(JSONObject metadata, MediaMetadata mediaMetadata) throws JSONException { if (metadata.has("images")) { - JSONArray imageUrls = metadata.getJSONArray("images"); - for (int i = 0; i < imageUrls.length(); i++) { - JSONObject imageObj = imageUrls.getJSONObject(i); - String imageUrl = imageObj.has("url") ? imageObj.getString("url") : "undefined"; - if (!imageUrl.contains("http://")) { - continue; + JSONArray images = metadata.getJSONArray("images"); + for (int i = 0; i < images.length(); i++) { + JSONObject imageObj = images.getJSONObject(i); + try { + Uri imageURI = Uri.parse(imageObj.getString("url")); + mediaMetadata.addImage(new WebImage(imageURI)); + } catch (Exception e) { } - Uri imageURI = Uri.parse(imageUrl); - WebImage webImage = new WebImage(imageURI); - mediaMetadata.addImage(webImage); } } - return mediaMetadata; } /** diff --git a/src/android/ChromecastUtilities.java b/src/android/ChromecastUtilities.java index b73596c..ab2fa6f 100644 --- a/src/android/ChromecastUtilities.java +++ b/src/android/ChromecastUtilities.java @@ -4,6 +4,7 @@ import androidx.mediarouter.media.MediaRouter; +import com.google.android.gms.cast.ApplicationMetadata; import com.google.android.gms.cast.CastDevice; import com.google.android.gms.cast.MediaInfo; import com.google.android.gms.cast.MediaMetadata; @@ -18,6 +19,7 @@ import org.json.JSONObject; import java.util.List; +import java.util.Set; final class ChromecastUtilities { @@ -36,7 +38,6 @@ static String getMediaIdleReason(MediaStatus mediaStatus) { case MediaStatus.IDLE_REASON_INTERRUPTED: return "INTERRUPTED"; case MediaStatus.IDLE_REASON_NONE: - return "NONE"; default: return null; } @@ -168,6 +169,147 @@ static String getWindowType(TextTrackStyle textTrackStyle) { } } + static String getRepeatMode(MediaStatus mediaStatus) { + switch (mediaStatus.getQueueRepeatMode()) { + case MediaStatus.REPEAT_MODE_REPEAT_OFF: + return "REPEAT_OFF"; + case MediaStatus.REPEAT_MODE_REPEAT_ALL: + return "REPEAT_ALL"; + case MediaStatus.REPEAT_MODE_REPEAT_SINGLE: + return "REPEAT_SINGLE"; + case MediaStatus.REPEAT_MODE_REPEAT_ALL_AND_SHUFFLE: + return "REPEAT_ALL_AND_SHUFFLE"; + default: + return null; + } + } + + static String getAndroidMetadataName(String clientName) { + switch (clientName) { + case "albumArtist": + return MediaMetadata.KEY_ALBUM_ARTIST; + case "albumTitle": + return MediaMetadata.KEY_ALBUM_TITLE; + case "artist": + return MediaMetadata.KEY_ARTIST; + case "bookTitle": + return MediaMetadata.KEY_BOOK_TITLE; + case "broadcastDate": + return MediaMetadata.KEY_BROADCAST_DATE; + case "chapterNumber": + return MediaMetadata.KEY_CHAPTER_NUMBER; + case "chapterTitle": + return MediaMetadata.KEY_CHAPTER_TITLE; + case "composer": + return MediaMetadata.KEY_COMPOSER; + case "creationDate": + return MediaMetadata.KEY_CREATION_DATE; + case "discNumber": + return MediaMetadata.KEY_DISC_NUMBER; + case "episodeNumber": + return MediaMetadata.KEY_EPISODE_NUMBER; + case "height": + return MediaMetadata.KEY_HEIGHT; + case "locationLatitude": + return MediaMetadata.KEY_LOCATION_LATITUDE; + case "locationLongitude": + return MediaMetadata.KEY_LOCATION_LONGITUDE; + case "locationName": + return MediaMetadata.KEY_LOCATION_NAME; + case "queueItemId": + return MediaMetadata.KEY_QUEUE_ITEM_ID; + case "releaseDate": + return MediaMetadata.KEY_RELEASE_DATE; + case "seasonNumber": + return MediaMetadata.KEY_SEASON_NUMBER; + case "sectionDuration": + return MediaMetadata.KEY_SECTION_DURATION; + case "sectionStartAbsoluteTime": + return MediaMetadata.KEY_SECTION_START_ABSOLUTE_TIME; + case "sectionStartTimeInContainer": + return MediaMetadata.KEY_SECTION_START_TIME_IN_CONTAINER; + case "sectionStartTimeInMedia": + return MediaMetadata.KEY_SECTION_START_TIME_IN_MEDIA; + case "seriesTitle": + return MediaMetadata.KEY_SERIES_TITLE; + case "studio": + return MediaMetadata.KEY_STUDIO; + case "subtitle": + return MediaMetadata.KEY_SUBTITLE; + case "title": + return MediaMetadata.KEY_TITLE; + case "trackNumber": + return MediaMetadata.KEY_TRACK_NUMBER; + case "width": + return MediaMetadata.KEY_WIDTH; + default: + return clientName; + } + } + + static String getClientMetadataName(String androidName) { + switch (androidName) { + case MediaMetadata.KEY_ALBUM_ARTIST: + return "albumArtist"; + case MediaMetadata.KEY_ALBUM_TITLE: + return "albumTitle"; + case MediaMetadata.KEY_ARTIST: + return "artist"; + case MediaMetadata.KEY_BOOK_TITLE: + return "bookTitle"; + case MediaMetadata.KEY_BROADCAST_DATE: + return "broadcastDate"; + case MediaMetadata.KEY_CHAPTER_NUMBER: + return "chapterNumber"; + case MediaMetadata.KEY_CHAPTER_TITLE: + return "chapterTitle"; + case MediaMetadata.KEY_COMPOSER: + return "composer"; + case MediaMetadata.KEY_CREATION_DATE: + return "creationDate"; + case MediaMetadata.KEY_DISC_NUMBER: + return "discNumber"; + case MediaMetadata.KEY_EPISODE_NUMBER: + return "episodeNumber"; + case MediaMetadata.KEY_HEIGHT: + return "height"; + case MediaMetadata.KEY_LOCATION_LATITUDE: + return "locationLatitude"; + case MediaMetadata.KEY_LOCATION_LONGITUDE: + return "locationLongitude"; + case MediaMetadata.KEY_LOCATION_NAME: + return "locationName"; + case MediaMetadata.KEY_QUEUE_ITEM_ID: + return "queueItemId"; + case MediaMetadata.KEY_RELEASE_DATE: + return "releaseDate"; + case MediaMetadata.KEY_SEASON_NUMBER: + return "seasonNumber"; + case MediaMetadata.KEY_SECTION_DURATION: + return "sectionDuration"; + case MediaMetadata.KEY_SECTION_START_ABSOLUTE_TIME: + return "sectionStartAbsoluteTime"; + case MediaMetadata.KEY_SECTION_START_TIME_IN_CONTAINER: + return "sectionStartTimeInContainer"; + case MediaMetadata.KEY_SECTION_START_TIME_IN_MEDIA: + return "sectionStartTimeInMedia"; + case MediaMetadata.KEY_SERIES_TITLE: + return "seriesTitle"; + case MediaMetadata.KEY_STUDIO: + return "studio"; + case MediaMetadata.KEY_SUBTITLE: + return "subtitle"; + case MediaMetadata.KEY_TITLE: + return "title"; + case MediaMetadata.KEY_TRACK_NUMBER: + return "trackNumber"; + case MediaMetadata.KEY_WIDTH: + return "width"; + default: + return androidName; + } + } + static TextTrackStyle parseTextTrackStyle(JSONObject textTrackSytle) { TextTrackStyle out = new TextTrackStyle(); @@ -212,9 +354,13 @@ static JSONObject createSessionObject(CastSession session) { JSONObject out = new JSONObject(); try { - out.put("appId", session.getApplicationMetadata().getApplicationId()); - out.put("appImages", createAppImagesObject(session)); - out.put("displayName", session.getApplicationMetadata().getName()); + ApplicationMetadata metadata = session.getApplicationMetadata(); + out.put("appId", metadata.getApplicationId()); + try { + out.put("appImages", createImagesArray(metadata.getImages())); + } catch (NullPointerException e) { + } + out.put("displayName", metadata.getName()); out.put("media", createMediaArray(session)); out.put("receiver", createReceiverObject(session)); out.put("sessionId", session.getSessionId()); @@ -227,17 +373,13 @@ static JSONObject createSessionObject(CastSession session) { return out; } - private static JSONArray createAppImagesObject(CastSession session) { + private static JSONArray createImagesArray(List images) throws JSONException { JSONArray appImages = new JSONArray(); - try { - MediaMetadata metadata = session.getRemoteMediaClient().getMediaInfo().getMetadata(); - List images = metadata.getImages(); - if (images != null) { - for (WebImage o : images) { - appImages.put(o.toString()); - } - } - } catch (NullPointerException e) { + JSONObject img; + for (WebImage o : images) { + img = new JSONObject(); + img.put("url", o.getUrl().toString()); + appImages.put(img); } return appImages; } @@ -284,7 +426,10 @@ static JSONObject createMediaObject(CastSession session) { out.put("currentTime", mediaStatus.getStreamPosition() / 1000.0); out.put("customData", mediaStatus.getCustomData()); //out.put("extendedStatus",); - out.put("idleReason", ChromecastUtilities.getMediaIdleReason(mediaStatus)); + String idleReason = ChromecastUtilities.getMediaIdleReason(mediaStatus); + if (idleReason != null) { + out.put("idleReason", idleReason); + } //out.put("items", mediaStatus.getQueueItems()); //out.put("liveSeekableRange",); out.put("loadingItemId", mediaStatus.getLoadingItemId()); @@ -294,12 +439,11 @@ static JSONObject createMediaObject(CastSession session) { out.put("playerState", ChromecastUtilities.getMediaPlayerState(mediaStatus)); out.put("preloadedItemId", mediaStatus.getPreloadedItemId()); //out.put("queueData", ); - //out.put("repeatMode", mediaStatus.getQueueRepeatMode()); + out.put("repeatMode", getRepeatMode(mediaStatus)); out.put("sessionId", session.getSessionId()); //out.put("supportedMediaCommands", ); //out.put("videoInfo", ); - JSONObject volume = new JSONObject(); volume.put("level", mediaStatus.getStreamVolume()); volume.put("muted", mediaStatus.isMute()); @@ -361,26 +505,22 @@ private static JSONObject createMediaInfoObject(CastSession session) { JSONObject out = new JSONObject(); try { - MediaStatus mediaStatus = session.getRemoteMediaClient().getMediaStatus(); - MediaInfo mediaInfo = mediaStatus.getMediaInfo(); + MediaInfo mediaInfo = session.getRemoteMediaClient().getMediaInfo(); // TODO: Missing attributes are commented out. - // These are returned by the chromecast desktop SDK, we should probbaly return them too + // These are returned by the chromecast desktop SDK, we should probably return them too //out.put("breakClips",); //out.put("breaks",); out.put("contentId", mediaInfo.getContentId()); out.put("contentType", mediaInfo.getContentType()); out.put("customData", mediaInfo.getCustomData()); - //out.put("idleReason",); - //out.put("items",); out.put("duration", mediaInfo.getStreamDuration() / 1000.0); //out.put("mediaCategory",); + out.put("metadata", createMetadataObject(mediaInfo.getMetadata())); out.put("streamType", ChromecastUtilities.getMediaInfoStreamType(mediaInfo)); out.put("tracks", createMediaInfoTracks(session)); out.put("textTrackStyle", ChromecastUtilities.createTextTrackObject(mediaInfo.getTextTrackStyle())); - // TODO: Check if it's useful - //out.put("metadata", mediaInfo.getMetadata()); } catch (JSONException e) { } catch (NullPointerException e) { } @@ -388,6 +528,30 @@ private static JSONObject createMediaInfoObject(CastSession session) { return out; } + static JSONObject createMetadataObject(MediaMetadata metadata) { + JSONObject out = new JSONObject(); + try { + try { + out.put("images", createImagesArray(metadata.getImages())); + } catch (Exception e) { + } + out.put("metadataType", metadata.getMediaType()); + Set keys = metadata.keySet(); + String outKey; + for (String key : keys) { + outKey = ChromecastUtilities.getClientMetadataName(key); + if (outKey.equals("type")) { + continue; + } + out.put(outKey, metadata.getString(key)); + } + out.put("type", metadata.getMediaType()); + } catch (Exception e) { + } + + return out; + } + static JSONObject createTextTrackObject(TextTrackStyle textTrackStyle) { JSONObject out = new JSONObject(); try { diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index 26b89a4..21c9371 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -19,6 +19,7 @@ // Set the reporter mocha.setup({ ui: 'bdd', + useColors: true, reporter: window['cordova-plugin-chromecast-tests'].customHtmlReporter }); @@ -30,7 +31,7 @@ this.slow(8000); this.bail(true); - var videoUrl = 'https://archive.org/download/CosmosLaundromatFirstCycle/Cosmos%20Laundromat%20-%20First%20Cycle%20%281080p%29.mp4'; + var videoUrl = 'https://ia801302.us.archive.org/1/items/TheWater_201510/TheWater.mp4'; // callOrder constants that are re-used frequently var success = 'success'; @@ -526,21 +527,30 @@ afterEach(function () { session.removeMediaListener(mediaListener); }); - it('session.loadMedia should be able to load a remote video', function (done) { + it('session.loadMedia should be able to load a remote video and return the metadata', function (done) { var called = utils.callOrder([ { id: success, repeats: false }, { id: update, repeats: true } ], done); - session.loadMedia(new chrome.cast.media.LoadRequest( - new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4') - ), function (m) { + var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + mediaInfo.metadata = new chrome.cast.media.MovieMediaMetadata(); + mediaInfo.metadata.title = 'DaTitle'; + mediaInfo.metadata.subtitle = 'DaSubtitle'; + mediaInfo.metadata.images = [new chrome.cast.Image('https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/b60a3dda-caeb-4853-8292-52b2a0420183/d90sp0y-899e3e51-557d-4fd4-a41a-366c2d74c074.png/v1/fill/w_1024,h_576,q_80,strp/yu_yu_hakusho_group_minecraft_pixel_art_by_sel_en_ium_d90sp0y-fullview.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9NTc2IiwicGF0aCI6IlwvZlwvYjYwYTNkZGEtY2FlYi00ODUzLTgyOTItNTJiMmEwNDIwMTgzXC9kOTBzcDB5LTg5OWUzZTUxLTU1N2QtNGZkNC1hNDFhLTM2NmMyZDc0YzA3NC5wbmciLCJ3aWR0aCI6Ijw9MTAyNCJ9XV0sImF1ZCI6WyJ1cm46c2VydmljZTppbWFnZS5vcGVyYXRpb25zIl19.bO8CoNGr2_NEKrw9Yhey4txiK7Z8z_ryM25RkfC1owg')]; + mediaInfo.metadata.myMadeUpMetadata = 'DaMadeUpMetadata'; + + session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { media = m; - assert.instanceOf(media, chrome.cast.media.Media); - assert.equal(media.sessionId, session.sessionId); - assert.isFunction(media.addUpdateListener); - assert.isFunction(media.removeUpdateListener); + utils.testMediaProperties(media); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.myMadeUpMetadata, mediaInfo.metadata.myMadeUpMetadata); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.MOVIE); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.MOVIE); media.addUpdateListener(function listener (isAlive) { assert.isTrue(isAlive); + utils.testMediaProperties(media); assert.oneOf(media.playerState, [ chrome.cast.media.PlayerState.PLAYING, chrome.cast.media.PlayerState.BUFFERING]); @@ -808,7 +818,7 @@ done(); }); }); - it('session.loadMedia should be able to load a video twice in a row', function (done) { + it('session.loadMedia should be able to load a video with metadata twice in a row', function (done) { var called = utils.callOrder([ { id: success, repeats: false }, { id: update, repeats: true } @@ -821,12 +831,10 @@ new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4') ), function (m) { media = m; - assert.instanceOf(media, chrome.cast.media.Media); - assert.equal(media.sessionId, session.sessionId); - assert.isFunction(media.addUpdateListener); - assert.isFunction(media.removeUpdateListener); + utils.testMediaProperties(media); media.addUpdateListener(function listener (isAlive) { assert.isTrue(isAlive); + utils.testMediaProperties(media); assert.oneOf(media.playerState, [ chrome.cast.media.PlayerState.PLAYING, chrome.cast.media.PlayerState.BUFFERING]); @@ -840,16 +848,24 @@ assert.fail(err.code + ': ' + err.description); }); }); - session.loadMedia(new chrome.cast.media.LoadRequest( - new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4') - ), function (m) { + var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + mediaInfo.metadata = new chrome.cast.media.GenericMediaMetadata(); + mediaInfo.metadata.title = 'DaTitle'; + mediaInfo.metadata.subtitle = 'DaSubtitle'; + mediaInfo.metadata.images = [new chrome.cast.Image('https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/b60a3dda-caeb-4853-8292-52b2a0420183/d90sp0y-899e3e51-557d-4fd4-a41a-366c2d74c074.png/v1/fill/w_1024,h_576,q_80,strp/yu_yu_hakusho_group_minecraft_pixel_art_by_sel_en_ium_d90sp0y-fullview.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9NTc2IiwicGF0aCI6IlwvZlwvYjYwYTNkZGEtY2FlYi00ODUzLTgyOTItNTJiMmEwNDIwMTgzXC9kOTBzcDB5LTg5OWUzZTUxLTU1N2QtNGZkNC1hNDFhLTM2NmMyZDc0YzA3NC5wbmciLCJ3aWR0aCI6Ijw9MTAyNCJ9XV0sImF1ZCI6WyJ1cm46c2VydmljZTppbWFnZS5vcGVyYXRpb25zIl19.bO8CoNGr2_NEKrw9Yhey4txiK7Z8z_ryM25RkfC1owg')]; + mediaInfo.metadata.myMadeUpMetadata = 'DaMadeUpMetadata'; + session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { media = m; - assert.instanceOf(media, chrome.cast.media.Media); - assert.equal(media.sessionId, session.sessionId); - assert.isFunction(media.addUpdateListener); - assert.isFunction(media.removeUpdateListener); + utils.testMediaProperties(media); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.myMadeUpMetadata, mediaInfo.metadata.myMadeUpMetadata); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.GENERIC); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.GENERIC); media.addUpdateListener(function listener (isAlive) { assert.isTrue(isAlive); + utils.testMediaProperties(media); assert.oneOf(media.playerState, [ chrome.cast.media.PlayerState.PLAYING, chrome.cast.media.PlayerState.BUFFERING]); diff --git a/tests/www/js/utils.js b/tests/www/js/utils.js index 08ebd78..39af645 100644 --- a/tests/www/js/utils.js +++ b/tests/www/js/utils.js @@ -131,6 +131,7 @@ utils.testSessionProperties = function (session) { assert.instanceOf(session, chrome.cast.Session); assert.isString(session.appId); + utils.testImages(session.appImages); assert.isString(session.displayName); assert.isArray(session.media); for (var i = 0; i < session.media.length; i++) { @@ -161,7 +162,7 @@ assert.isNumber(media.currentItemId); assert.isNumber(media.currentTime); if (media.idleReason) { - assert.oneOf(utils.getObjectValues(chrome.cast.media.IdleReason), media.idleReason); + assert.oneOf(media.idleReason, utils.getObjectValues(chrome.cast.media.IdleReason)); } utils.testMediaInfoProperties(media.media); assert.isNumber(media.mediaSessionId); @@ -171,6 +172,8 @@ assert.isString(media.sessionId); assert.isArray(media.supportedMediaCommands); assert.instanceOf(media.volume, chrome.cast.Volume); + assert.isFunction(media.addUpdateListener); + assert.isFunction(media.removeUpdateListener); }; utils.testMediaInfoProperties = function (mediaInfo) { @@ -178,10 +181,42 @@ assert.isString(mediaInfo.contentId); assert.isString(mediaInfo.contentType); assert.isNumber(mediaInfo.duration); + utils.testMediaMetadata(mediaInfo.metadata); assert.isString(mediaInfo.streamType); assert.isArray(mediaInfo.tracks); }; + utils.testMediaMetadata = function (metadata) { + if (!metadata) { + return; + } + if (metadata.metadataType) { + assert.oneOf(metadata.metadataType, utils.getObjectValues(chrome.cast.media.MetadataType)); + } + if (metadata.subtitle) { + assert.isString(metadata.subtitle); + } + if (metadata.title) { + assert.isString(metadata.title); + } + utils.testImages(metadata.images); + if (metadata.type) { + assert.oneOf(metadata.type, utils.getObjectValues(chrome.cast.media.MetadataType)); + } + }; + + utils.testImages = function (images) { + if (!images) { + return; + } + assert.isArray(images); + var image; + for (var i = 0; i < images.length; i++) { + image = images[i]; + assert.isString(image.url); + } + }; + window['cordova-plugin-chromecast-tests'] = window['cordova-plugin-chromecast-tests'] || {}; window['cordova-plugin-chromecast-tests'].utils = utils; }()); diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 0be97b4..5c5e893 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -249,7 +249,7 @@ chrome.cast = { * TV_SHOW: An episode of a TV series. Used by chrome.cast.media.TvShowMediaMetadata. * @type {Object} */ - MetadataType: { GENERIC: 0, TV_SHOW: 1, MOVIE: 2, MUSIC_TRACK: 3, PHOTO: 4 }, + MetadataType: { GENERIC: 0, MOVIE: 1, TV_SHOW: 2, MUSIC_TRACK: 3, PHOTO: 4, AUDIOBOOK_CHAPTER: 5 }, /** * Possible media stream types. @@ -344,7 +344,7 @@ chrome.cast = { */ GenericMediaMetadata: function GenericMediaMetadata () { this.metadataType = this.type = chrome.cast.media.MetadataType.GENERIC; - this.releaseDate = this.releaseYear = this.images = this.subtitle = this.title = null; + this.releaseDate = this.releaseYear = this.images = this.subtitle = this.title = undefined; }, /** @@ -359,7 +359,7 @@ chrome.cast = { */ MovieMediaMetadata: function MovieMediaMetadata () { this.metadataType = this.type = chrome.cast.media.MetadataType.MOVIE; - this.releaseDate = this.releaseYear = this.images = this.subtitle = this.studio = this.title = null; + this.releaseDate = this.releaseYear = this.images = this.subtitle = this.studio = this.title = undefined; }, /** @@ -380,7 +380,7 @@ chrome.cast = { */ MusicTrackMediaMetadata: function MusicTrackMediaMetadata () { this.metadataType = this.type = chrome.cast.media.MetadataType.MUSIC_TRACK; - this.releaseDate = this.releaseYear = this.images = this.discNumber = this.trackNumber = this.artistName = this.songName = this.composer = this.artist = this.albumArtist = this.title = this.albumName = null; + this.releaseDate = this.releaseYear = this.images = this.discNumber = this.trackNumber = this.artistName = this.songName = this.composer = this.artist = this.albumArtist = this.title = this.albumName = undefined; }, /** @@ -398,7 +398,7 @@ chrome.cast = { */ PhotoMediaMetadata: function PhotoMediaMetadata () { this.metadataType = this.type = chrome.cast.media.MetadataType.PHOTO; - this.creationDateTime = this.height = this.width = this.longitude = this.latitude = this.images = this.location = this.artist = this.title = null; + this.creationDateTime = this.height = this.width = this.longitude = this.latitude = this.images = this.location = this.artist = this.title = undefined; }, /** @@ -417,7 +417,7 @@ chrome.cast = { */ TvShowMediaMetadata: function TvShowMediaMetadata () { this.metadataType = this.type = chrome.cast.media.MetadataType.TV_SHOW; - this.originalAirdate = this.releaseYear = this.images = this.episode = this.episodeNumber = this.season = this.seasonNumber = this.episodeTitle = this.title = this.seriesTitle = null; + this.originalAirdate = this.releaseYear = this.images = this.episode = this.episodeNumber = this.season = this.seasonNumber = this.episodeTitle = this.title = this.seriesTitle = undefined; }, /** @@ -772,8 +772,9 @@ chrome.cast.Session.prototype.removeMediaListener = function (listener) { }; chrome.cast.Session.prototype._update = function (obj) { + var i; for (var attr in obj) { - if (['receiver', 'media'].indexOf(attr) === -1) { + if (['receiver', 'media', 'appImages'].indexOf(attr) === -1) { this[attr] = obj[attr]; } } @@ -792,7 +793,7 @@ chrome.cast.Session.prototype._update = function (obj) { if (obj.media && obj.media.length > 0) { // refill media var m; - for (var i = 0; i < obj.media.length; i++) { + for (i = 0; i < obj.media.length; i++) { m = new chrome.cast.media.Media(); m._update(obj.media[i]); this.media.push(m); @@ -803,6 +804,16 @@ chrome.cast.Session.prototype._update = function (obj) { } else { _currentMedia = null; } + + // Empty appImages + this.appImages = this.appImages || []; + this.appImages.splice(0, this.appImages.length); + if (obj.appImages && obj.appImages.length > 0) { + // refill appImages + for (i = 0; i < obj.appImages.length; i++) { + this.appImages.push(new chrome.cast.Image(obj.appImages[i].url)); + } + } }; /** @@ -887,14 +898,17 @@ chrome.cast.media.MediaInfo = function MediaInfo (contentId, contentType) { }; chrome.cast.media.MediaInfo.prototype._update = function (jsonObj) { + var i; for (var attr in jsonObj) { - this[attr] = jsonObj[attr]; + if (['tracks', 'images'].indexOf(attr) === -1) { + this[attr] = jsonObj[attr]; + } } if (jsonObj.tracks) { this.tracks = []; var track, t; - for (var i = 0; i < jsonObj.tracks.length; i++) { + for (i = 0; i < jsonObj.tracks.length; i++) { track = jsonObj.tracks[i]; t = new chrome.cast.media.Track(); t._update(track); @@ -903,6 +917,16 @@ chrome.cast.media.MediaInfo.prototype._update = function (jsonObj) { } else { this.tracks = null; } + + // Empty images + this.images = this.images || []; + this.images.splice(0, this.images.length); + if (jsonObj.images && jsonObj.images.length > 0) { + // refill appImages + for (i = 0; i < jsonObj.images.length; i++) { + this.images.push(new chrome.cast.Image(jsonObj.images[i].url)); + } + } }; /** From 5f9e343e992b860ab18b58045c9a04759caa4288 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 6 Oct 2019 03:19:40 -0600 Subject: [PATCH 063/166] Issue #23 Fix up the metadata to handle more than strings (ChromecastSession and ChromecastUtilities). Provide better translations between desktop chrome metadata names and the android names (ChromecastUtilities). Add tests for loading audio and images. Should manually trigger media update event after media load because sometimes the MEDIA_UPDATE event fires before the client is able to get a reference to media to addUpdateListener. --- src/android/ChromecastSession.java | 121 ++++++++++++------ src/android/ChromecastUtilities.java | 100 +++++++++++++-- tests/www/js/tests_auto.js | 181 +++++++++++++++++++++------ www/chrome.cast.js | 2 + 4 files changed, 316 insertions(+), 88 deletions(-) diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index c884a6b..92fd75e 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -1,6 +1,7 @@ package acidhax.cordova.chromecast; import java.io.IOException; +import java.util.GregorianCalendar; import java.util.Iterator; import org.apache.cordova.CallbackContext; @@ -245,33 +246,95 @@ private MediaInfo createMediaInfo(String contentId, JSONObject customData, Strin MediaMetadata mediaMetadata; try { - int metadataType = metadata.has("metadataType") ? metadata.getInt("metadataType") : MediaMetadata.MEDIA_TYPE_GENERIC; - // Set the metadataType - mediaMetadata = new MediaMetadata(metadataType); - // Add any images - addImages(metadata, mediaMetadata); - - // Dynamically add other parameters - Iterator keys = metadata.keys(); - String key; - String value; - while (keys.hasNext()) { - key = keys.next(); - value = metadata.getString(key); - if (key.equals("metadataType") - || key.equals("images") - || key.equals("type")) { - continue; + mediaMetadata = new MediaMetadata(metadata.getInt("metadataType")); + } catch (JSONException e) { + mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_GENERIC); + } + // Add any images + try { + JSONArray images = metadata.getJSONArray("images"); + for (int i = 0; i < images.length(); i++) { + JSONObject imageObj = images.getJSONObject(i); + try { + Uri imageURI = Uri.parse(imageObj.getString("url")); + mediaMetadata.addImage(new WebImage(imageURI)); + } catch (Exception e) { } - key = ChromecastUtilities.getAndroidMetadataName(key); - mediaMetadata.putString(key, value); } + } catch (JSONException e) { + } - mediaInfoBuilder.setMetadata(mediaMetadata); - - } catch (Exception e) { + // Dynamically add other parameters + Iterator keys = metadata.keys(); + String key; + String convertedKey; + Object value; + while (keys.hasNext()) { + key = keys.next(); + if (key.equals("metadataType") + || key.equals("images") + || key.equals("type")) { + continue; + } + try { + value = metadata.get(key); + convertedKey = ChromecastUtilities.getAndroidMetadataName(key); + // Try to add the translated version of the key + switch (ChromecastUtilities.getMetadataType(convertedKey)) { + case "string": + mediaMetadata.putString(convertedKey, metadata.getString(key)); + break; + case "int": + mediaMetadata.putInt(convertedKey, metadata.getInt(key)); + break; + case "double": + mediaMetadata.putDouble(convertedKey, metadata.getDouble(key)); + break; + case "date": + GregorianCalendar c = new GregorianCalendar(); + if (value instanceof java.lang.Integer + || value instanceof java.lang.Long + || value instanceof java.lang.Float + || value instanceof java.lang.Double) { + c.setTimeInMillis(metadata.getLong(key)); + mediaMetadata.putDate(convertedKey, c); + } else { + String stringValue; + try { + stringValue = " value: " + metadata.getString(key); + } catch (JSONException e) { + stringValue = ""; + } + new Error("Cannot date from metadata key: " + key + stringValue + + "\n Dates must be in milliseconds from epoch UTC") + .printStackTrace(); + } + break; + case "ms": + mediaMetadata.putTimeMillis(convertedKey, metadata.getLong(key)); + break; + default: + } + // Also always add the client's version of the key because sometimes the + // MediaMetadata object removes some parameters. + // eg. If you pass metadataType == 2 == MEDIA_TYPE_TV_SHOW you will lose any + // subtitle added for "com.google.android.gms.cast.metadata.SUBTITLE", but this + // is not in-line with chrome desktop which preserves the value. + if (!key.equals(convertedKey)) { + // It is is really stubborn and if you try to add the key "subtitle" that is + // also stripped. (Hence the "cordova-plugin-chromecast_metadata_key=" prefix + convertedKey = "cordova-plugin-chromecast_metadata_key=" + key; + } + mediaMetadata.putString(convertedKey, metadata.getString(key)); + } catch (JSONException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } } + mediaInfoBuilder.setMetadata(mediaMetadata); + int intStreamType; switch (streamType) { case "buffered": @@ -296,20 +359,6 @@ private MediaInfo createMediaInfo(String contentId, JSONObject customData, Strin return mediaInfoBuilder.build(); } - private void addImages(JSONObject metadata, MediaMetadata mediaMetadata) throws JSONException { - if (metadata.has("images")) { - JSONArray images = metadata.getJSONArray("images"); - for (int i = 0; i < images.length(); i++) { - JSONObject imageObj = images.getJSONObject(i); - try { - Uri imageURI = Uri.parse(imageObj.getString("url")); - mediaMetadata.addImage(new WebImage(imageURI)); - } catch (Exception e) { - } - } - } - } - /** * Media API - Calls play on the current media. * @param callback called with success or error diff --git a/src/android/ChromecastUtilities.java b/src/android/ChromecastUtilities.java index ab2fa6f..5c16fa1 100644 --- a/src/android/ChromecastUtilities.java +++ b/src/android/ChromecastUtilities.java @@ -188,7 +188,7 @@ static String getAndroidMetadataName(String clientName) { switch (clientName) { case "albumArtist": return MediaMetadata.KEY_ALBUM_ARTIST; - case "albumTitle": + case "albumName": return MediaMetadata.KEY_ALBUM_TITLE; case "artist": return MediaMetadata.KEY_ARTIST; @@ -203,24 +203,26 @@ static String getAndroidMetadataName(String clientName) { case "composer": return MediaMetadata.KEY_COMPOSER; case "creationDate": + case "creationDateTime": return MediaMetadata.KEY_CREATION_DATE; case "discNumber": return MediaMetadata.KEY_DISC_NUMBER; - case "episodeNumber": + case "episode": return MediaMetadata.KEY_EPISODE_NUMBER; case "height": return MediaMetadata.KEY_HEIGHT; - case "locationLatitude": + case "latitude": return MediaMetadata.KEY_LOCATION_LATITUDE; - case "locationLongitude": + case "longitude": return MediaMetadata.KEY_LOCATION_LONGITUDE; case "locationName": return MediaMetadata.KEY_LOCATION_NAME; case "queueItemId": return MediaMetadata.KEY_QUEUE_ITEM_ID; case "releaseDate": + case "originalAirDate": return MediaMetadata.KEY_RELEASE_DATE; - case "seasonNumber": + case "season": return MediaMetadata.KEY_SEASON_NUMBER; case "sectionDuration": return MediaMetadata.KEY_SECTION_DURATION; @@ -252,7 +254,7 @@ static String getClientMetadataName(String androidName) { case MediaMetadata.KEY_ALBUM_ARTIST: return "albumArtist"; case MediaMetadata.KEY_ALBUM_TITLE: - return "albumTitle"; + return "albumName"; case MediaMetadata.KEY_ARTIST: return "artist"; case MediaMetadata.KEY_BOOK_TITLE: @@ -270,21 +272,21 @@ static String getClientMetadataName(String androidName) { case MediaMetadata.KEY_DISC_NUMBER: return "discNumber"; case MediaMetadata.KEY_EPISODE_NUMBER: - return "episodeNumber"; + return "episode"; case MediaMetadata.KEY_HEIGHT: return "height"; case MediaMetadata.KEY_LOCATION_LATITUDE: - return "locationLatitude"; + return "latitude"; case MediaMetadata.KEY_LOCATION_LONGITUDE: - return "locationLongitude"; + return "longitude"; case MediaMetadata.KEY_LOCATION_NAME: - return "locationName"; + return "location"; case MediaMetadata.KEY_QUEUE_ITEM_ID: return "queueItemId"; case MediaMetadata.KEY_RELEASE_DATE: return "releaseDate"; case MediaMetadata.KEY_SEASON_NUMBER: - return "seasonNumber"; + return "season"; case MediaMetadata.KEY_SECTION_DURATION: return "sectionDuration"; case MediaMetadata.KEY_SECTION_START_ABSOLUTE_TIME: @@ -310,6 +312,46 @@ static String getClientMetadataName(String androidName) { } } + static String getMetadataType(String androidName) { + switch (androidName) { + case MediaMetadata.KEY_ALBUM_ARTIST: + case MediaMetadata.KEY_ALBUM_TITLE: + case MediaMetadata.KEY_ARTIST: + case MediaMetadata.KEY_BOOK_TITLE: + case MediaMetadata.KEY_CHAPTER_NUMBER: + case MediaMetadata.KEY_CHAPTER_TITLE: + case MediaMetadata.KEY_COMPOSER: + case MediaMetadata.KEY_LOCATION_NAME: + case MediaMetadata.KEY_SERIES_TITLE: + case MediaMetadata.KEY_STUDIO: + case MediaMetadata.KEY_SUBTITLE: + case MediaMetadata.KEY_TITLE: + return "string"; // 1 in MediaMetadata + case MediaMetadata.KEY_DISC_NUMBER: + case MediaMetadata.KEY_EPISODE_NUMBER: + case MediaMetadata.KEY_HEIGHT: + case MediaMetadata.KEY_QUEUE_ITEM_ID: + case MediaMetadata.KEY_SEASON_NUMBER: + case MediaMetadata.KEY_TRACK_NUMBER: + case MediaMetadata.KEY_WIDTH: + return "int"; // 2 in MediaMetadata + case MediaMetadata.KEY_LOCATION_LATITUDE: + case MediaMetadata.KEY_LOCATION_LONGITUDE: + return "double"; // 3 in MediaMetadata + case MediaMetadata.KEY_BROADCAST_DATE: + case MediaMetadata.KEY_CREATION_DATE: + case MediaMetadata.KEY_RELEASE_DATE: + return "date"; // 4 in MediaMetadata + case MediaMetadata.KEY_SECTION_DURATION: + case MediaMetadata.KEY_SECTION_START_ABSOLUTE_TIME: + case MediaMetadata.KEY_SECTION_START_TIME_IN_CONTAINER: + case MediaMetadata.KEY_SECTION_START_TIME_IN_MEDIA: + return "ms"; // 5 in MediaMetadata + default: + return "custom"; + } + } + static TextTrackStyle parseTextTrackStyle(JSONObject textTrackSytle) { TextTrackStyle out = new TextTrackStyle(); @@ -532,21 +574,53 @@ static JSONObject createMetadataObject(MediaMetadata metadata) { JSONObject out = new JSONObject(); try { try { + // Must be in own try catch out.put("images", createImagesArray(metadata.getImages())); } catch (Exception e) { } out.put("metadataType", metadata.getMediaType()); + out.put("type", metadata.getMediaType()); + Set keys = metadata.keySet(); String outKey; + // First translate and add the Android specific keys + for (String key : keys) { + outKey = ChromecastUtilities.getClientMetadataName(key); + if (outKey.equals(key) || outKey.equals("type")) { + continue; + } + switch (ChromecastUtilities.getMetadataType(key)) { + case "string": + out.put(outKey, metadata.getString(key)); + break; + case "int": + out.put(outKey, metadata.getInt(key)); + break; + case "double": + out.put(outKey, metadata.getDouble(key)); + break; + case "date": + out.put(outKey, metadata.getDate(key).getTimeInMillis()); + break; + case "ms": + out.put(outKey, metadata.getTimeMillis(key)); + break; + default: + } + } + // Then add the non-Android specific keys ensuring we don't overwrite existing keys for (String key : keys) { outKey = ChromecastUtilities.getClientMetadataName(key); - if (outKey.equals("type")) { + if (!outKey.equals(key) || out.has(outKey) || outKey.equals("type")) { continue; } + if (outKey.startsWith("cordova-plugin-chromecast_metadata_key=")) { + outKey = outKey.substring("cordova-plugin-chromecast_metadata_key=".length()); + } out.put(outKey, metadata.getString(key)); } - out.put("type", metadata.getMediaType()); } catch (Exception e) { + e.printStackTrace(); } return out; diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index 21c9371..00c9783 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -31,7 +31,9 @@ this.slow(8000); this.bail(true); + var imageUrl = 'https://ia800705.us.archive.org/1/items/GoodHousekeeping193810/Good%20Housekeeping%201938-10.jpg'; var videoUrl = 'https://ia801302.us.archive.org/1/items/TheWater_201510/TheWater.mp4'; + var audioUrl = 'https://ia600304.us.archive.org/20/items/OTRR_Gunsmoke_Singles/Gunsmoke_52-10-03_024_Cain.mp3'; // callOrder constants that are re-used frequently var success = 'success'; @@ -527,27 +529,38 @@ afterEach(function () { session.removeMediaListener(mediaListener); }); - it('session.loadMedia should be able to load a remote video and return the metadata', function (done) { - var called = utils.callOrder([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); + it('session.loadMedia should be able to load a remote video and handle GenericMediaMetadata', function (done) { var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); - mediaInfo.metadata = new chrome.cast.media.MovieMediaMetadata(); + mediaInfo.metadata = new chrome.cast.media.GenericMediaMetadata(); mediaInfo.metadata.title = 'DaTitle'; mediaInfo.metadata.subtitle = 'DaSubtitle'; - mediaInfo.metadata.images = [new chrome.cast.Image('https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/b60a3dda-caeb-4853-8292-52b2a0420183/d90sp0y-899e3e51-557d-4fd4-a41a-366c2d74c074.png/v1/fill/w_1024,h_576,q_80,strp/yu_yu_hakusho_group_minecraft_pixel_art_by_sel_en_ium_d90sp0y-fullview.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9NTc2IiwicGF0aCI6IlwvZlwvYjYwYTNkZGEtY2FlYi00ODUzLTgyOTItNTJiMmEwNDIwMTgzXC9kOTBzcDB5LTg5OWUzZTUxLTU1N2QtNGZkNC1hNDFhLTM2NmMyZDc0YzA3NC5wbmciLCJ3aWR0aCI6Ijw9MTAyNCJ9XV0sImF1ZCI6WyJ1cm46c2VydmljZTppbWFnZS5vcGVyYXRpb25zIl19.bO8CoNGr2_NEKrw9Yhey4txiK7Z8z_ryM25RkfC1owg')]; - mediaInfo.metadata.myMadeUpMetadata = 'DaMadeUpMetadata'; - + mediaInfo.metadata.releaseDate = new Date().valueOf(); + mediaInfo.metadata.someTrueBoolean = true; + mediaInfo.metadata.someFalseBoolean = false; + mediaInfo.metadata.someSmallNumber = 15; + mediaInfo.metadata.someLargeNumber = 1234567890123456; + mediaInfo.metadata.someSmallDecimal = 15.15; + mediaInfo.metadata.someLargeDecimal = 1234567.123456789; + mediaInfo.metadata.someString = 'SomeString'; + mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { media = m; utils.testMediaProperties(media); assert.equal(media.media.metadata.title, mediaInfo.metadata.title); assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); + assert.equal(media.media.metadata.releaseDate, mediaInfo.metadata.releaseDate); + // TODO figure out how to maintain the data types for custom params on the native side + // so that we don't have to do turn each actual and expected into a string + assert.equal(media.media.metadata.someTrueBoolean + '', mediaInfo.metadata.someTrueBoolean + ''); + assert.equal(media.media.metadata.someFalseBoolean + '', mediaInfo.metadata.someFalseBoolean + ''); + assert.equal(media.media.metadata.someSmallNumber + '', mediaInfo.metadata.someSmallNumber + ''); + assert.equal(media.media.metadata.someLargeNumber + '', mediaInfo.metadata.someLargeNumber + ''); + assert.equal(media.media.metadata.someSmallDecimal + '', mediaInfo.metadata.someSmallDecimal + ''); + assert.equal(media.media.metadata.someLargeDecimal + '', mediaInfo.metadata.someLargeDecimal + ''); + assert.equal(media.media.metadata.someString, mediaInfo.metadata.someString); assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); - assert.equal(media.media.metadata.myMadeUpMetadata, mediaInfo.metadata.myMadeUpMetadata); - assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.MOVIE); - assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.MOVIE); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.GENERIC); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.GENERIC); media.addUpdateListener(function listener (isAlive) { assert.isTrue(isAlive); utils.testMediaProperties(media); @@ -556,10 +569,9 @@ chrome.cast.media.PlayerState.BUFFERING]); if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { media.removeUpdateListener(listener); - called(update); + done(); } }); - called(success); }, function (err) { assert.fail(err.code + ': ' + err.description); }); @@ -818,20 +830,61 @@ done(); }); }); - it('session.loadMedia should be able to load a video with metadata twice in a row', function (done) { - var called = utils.callOrder([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], function () { - var called = utils.callOrder([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); - session.loadMedia(new chrome.cast.media.LoadRequest( - new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4') - ), function (m) { + it('session.loadMedia should be able to load videos twice in a row and handle MovieMediaMetadata and TvShowMediaMetadata correctly', function (done) { + var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + mediaInfo.metadata = new chrome.cast.media.MovieMediaMetadata(); + mediaInfo.metadata.title = 'DaTitle'; + mediaInfo.metadata.subtitle = 'DaSubtitle'; + mediaInfo.metadata.studio = 'DaStudio'; + mediaInfo.metadata.myMadeUpMetadata = 'DaMadeUpMetadata'; + mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; + session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { + media = m; + utils.testMediaProperties(media); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); + assert.equal(media.media.metadata.studio, mediaInfo.metadata.studio); + assert.equal(media.media.metadata.myMadeUpMetadata, mediaInfo.metadata.myMadeUpMetadata); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.MOVIE); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.MOVIE); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + utils.testMediaProperties(media); + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + loadSecond(); + } + }); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + + function loadSecond () { + var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + mediaInfo.metadata = new chrome.cast.media.TvShowMediaMetadata(); + mediaInfo.metadata.title = 'DaTitle'; + mediaInfo.metadata.subtitle = 'DaSubtitle'; + mediaInfo.metadata.originalAirDate = new Date().valueOf(); + mediaInfo.metadata.episode = 15; + mediaInfo.metadata.season = 2; + mediaInfo.metadata.seriesTitle = 'DaSeries'; + mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; + session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { media = m; utils.testMediaProperties(media); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); + assert.equal(media.media.metadata.originalAirDate, mediaInfo.metadata.originalAirDate); + assert.equal(media.media.metadata.episode, mediaInfo.metadata.episode); + assert.equal(media.media.metadata.season, mediaInfo.metadata.season); + assert.equal(media.media.metadata.seriesTitle, mediaInfo.metadata.seriesTitle); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.TV_SHOW); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.TV_SHOW); media.addUpdateListener(function listener (isAlive) { assert.isTrue(isAlive); utils.testMediaProperties(media); @@ -840,29 +893,40 @@ chrome.cast.media.PlayerState.BUFFERING]); if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { media.removeUpdateListener(listener); - called(update); + done(); } }); - called(success); }, function (err) { assert.fail(err.code + ': ' + err.description); }); - }); - var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); - mediaInfo.metadata = new chrome.cast.media.GenericMediaMetadata(); + } + }); + it('session.loadMedia should be able to load remote audio and return the MusicTrackMediaMetadata', function (done) { + var mediaInfo = new chrome.cast.media.MediaInfo(audioUrl, 'audio/mpeg'); + mediaInfo.metadata = new chrome.cast.media.MusicTrackMediaMetadata(); + mediaInfo.metadata.albumArtist = 'DaAlmbumArtist'; + mediaInfo.metadata.albumName = 'DaAlbum'; + mediaInfo.metadata.artist = 'DaArtist'; + mediaInfo.metadata.composer = 'DaComposer'; mediaInfo.metadata.title = 'DaTitle'; - mediaInfo.metadata.subtitle = 'DaSubtitle'; - mediaInfo.metadata.images = [new chrome.cast.Image('https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/b60a3dda-caeb-4853-8292-52b2a0420183/d90sp0y-899e3e51-557d-4fd4-a41a-366c2d74c074.png/v1/fill/w_1024,h_576,q_80,strp/yu_yu_hakusho_group_minecraft_pixel_art_by_sel_en_ium_d90sp0y-fullview.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9NTc2IiwicGF0aCI6IlwvZlwvYjYwYTNkZGEtY2FlYi00ODUzLTgyOTItNTJiMmEwNDIwMTgzXC9kOTBzcDB5LTg5OWUzZTUxLTU1N2QtNGZkNC1hNDFhLTM2NmMyZDc0YzA3NC5wbmciLCJ3aWR0aCI6Ijw9MTAyNCJ9XV0sImF1ZCI6WyJ1cm46c2VydmljZTppbWFnZS5vcGVyYXRpb25zIl19.bO8CoNGr2_NEKrw9Yhey4txiK7Z8z_ryM25RkfC1owg')]; - mediaInfo.metadata.myMadeUpMetadata = 'DaMadeUpMetadata'; + mediaInfo.metadata.songName = 'DaSongName'; + mediaInfo.metadata.releaseDate = new Date().valueOf(); + mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; + mediaInfo.metadata.myMadeUpMetadata = 15; session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { media = m; utils.testMediaProperties(media); + assert.equal(media.media.metadata.albumArtist, mediaInfo.metadata.albumArtist); + assert.equal(media.media.metadata.albumName, mediaInfo.metadata.albumName); + assert.equal(media.media.metadata.artist, mediaInfo.metadata.artist); + assert.equal(media.media.metadata.composer, mediaInfo.metadata.composer); assert.equal(media.media.metadata.title, mediaInfo.metadata.title); - assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); + assert.equal(media.media.metadata.songName, mediaInfo.metadata.songName); + assert.equal(media.media.metadata.releaseDate, mediaInfo.metadata.releaseDate); assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); assert.equal(media.media.metadata.myMadeUpMetadata, mediaInfo.metadata.myMadeUpMetadata); - assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.GENERIC); - assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.GENERIC); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.MUSIC_TRACK); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.MUSIC_TRACK); media.addUpdateListener(function listener (isAlive) { assert.isTrue(isAlive); utils.testMediaProperties(media); @@ -871,10 +935,49 @@ chrome.cast.media.PlayerState.BUFFERING]); if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { media.removeUpdateListener(listener); - called(update); + done(); + } + }); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('session.loadMedia should be able to load remote image and return the PhotoMediaMetadata', function (done) { + var mediaInfo = new chrome.cast.media.MediaInfo(imageUrl, 'image/jpeg'); + mediaInfo.metadata = new chrome.cast.media.PhotoMediaMetadata(); + mediaInfo.metadata.title = 'DaTitle'; + mediaInfo.metadata.artist = 'DaArtist'; + mediaInfo.metadata.location = 'DaLocation'; + mediaInfo.metadata.latitude = 102.13; + mediaInfo.metadata.longitude = 101.12; + mediaInfo.metadata.height = 100; + mediaInfo.metadata.width = 100; + mediaInfo.metadata.myMadeUpMetadata = 15; + mediaInfo.metadata.creationDateTime = new Date().valueOf(); + mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; + session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { + media = m; + utils.testMediaProperties(media); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.artist, mediaInfo.metadata.artist); + assert.equal(media.media.metadata.location, mediaInfo.metadata.location); + assert.equal(media.media.metadata.latitude, mediaInfo.metadata.latitude); + assert.equal(media.media.metadata.longitude, mediaInfo.metadata.longitude); + assert.equal(media.media.metadata.height, mediaInfo.metadata.height); + assert.equal(media.media.metadata.width, mediaInfo.metadata.width); + assert.equal(media.media.metadata.myMadeUpMetadata, mediaInfo.metadata.myMadeUpMetadata); + assert.equal(media.media.metadata.creationDateTime, mediaInfo.metadata.creationDateTime); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.PHOTO); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.PHOTO); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + utils.testMediaProperties(media); + if (media.playerState === chrome.cast.media.PlayerState.PAUSED) { + media.removeUpdateListener(listener); + done(); } }); - called(success); }, function (err) { assert.fail(err.code + ': ' + err.description); }); diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 5c5e893..42e14c5 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -705,6 +705,8 @@ chrome.cast.Session.prototype.loadMedia = function (loadRequest, successCallback _currentMedia = new chrome.cast.media.Media(self.sessionId, obj.mediaSessionId); _currentMedia._update(obj); successCallback(_currentMedia); + // Also trigger the update notification + _currentMedia.emit('_mediaUpdated', _currentMedia.playerState !== 'IDLE'); } else { handleError(err, errorCallback); } From a05b187d5cc85f503e8fea37e99b5153fccf3613 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Tue, 8 Oct 2019 02:31:44 -0600 Subject: [PATCH 064/166] Include Mocha fix PR #4051 because I just tried it out and it's nice (fixes a bug with filtering by passes/failures). --- tests/www/lib/mocha.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/www/lib/mocha.js b/tests/www/lib/mocha.js index 8c8d304..7a04e60 100644 --- a/tests/www/lib/mocha.js +++ b/tests/www/lib/mocha.js @@ -3302,8 +3302,8 @@ */ function unhide () { var els = document.getElementsByClassName('suite hidden'); - for (var i = 0; i < els.length; ++i) { - els[i].className = els[i].className.replace('suite hidden', 'suite'); + while (els.length > 0) { + els[0].className = els[0].className.replace('suite hidden', 'suite'); } } From 6dd890738e78f1bc9879de0e785134e140d43b04 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 13 Oct 2019 15:09:15 -0600 Subject: [PATCH 065/166] Fix tests html, and improve utils output of callOrder functions a bit --- tests/www/chrome/tests_auto_chrome.html | 3 ++- tests/www/chrome/tests_chrome.html | 3 +++ tests/www/html/tests.html | 1 + tests/www/html/tests_auto.html | 3 ++- tests/www/html/tests_manual.html | 1 + tests/www/js/custom_mocha_html_reporter.js | 4 ++-- tests/www/js/utils.js | 13 +++++++++++++ 7 files changed, 24 insertions(+), 4 deletions(-) diff --git a/tests/www/chrome/tests_auto_chrome.html b/tests/www/chrome/tests_auto_chrome.html index e56276c..4987113 100644 --- a/tests/www/chrome/tests_auto_chrome.html +++ b/tests/www/chrome/tests_auto_chrome.html @@ -1,3 +1,4 @@ + Cordova tests @@ -10,7 +11,7 @@ - + diff --git a/tests/www/chrome/tests_chrome.html b/tests/www/chrome/tests_chrome.html index 94c29f8..fa5ae8b 100644 --- a/tests/www/chrome/tests_chrome.html +++ b/tests/www/chrome/tests_chrome.html @@ -1,3 +1,4 @@ + Cordova tests @@ -7,6 +8,8 @@ + + diff --git a/tests/www/html/tests.html b/tests/www/html/tests.html index ba60ae8..4de0339 100644 --- a/tests/www/html/tests.html +++ b/tests/www/html/tests.html @@ -1,3 +1,4 @@ + Cordova tests diff --git a/tests/www/html/tests_auto.html b/tests/www/html/tests_auto.html index b9c8476..121354d 100644 --- a/tests/www/html/tests_auto.html +++ b/tests/www/html/tests_auto.html @@ -1,3 +1,4 @@ + Cordova tests @@ -11,7 +12,7 @@ - + diff --git a/tests/www/html/tests_manual.html b/tests/www/html/tests_manual.html index 0ebaae8..0aede16 100644 --- a/tests/www/html/tests_manual.html +++ b/tests/www/html/tests_manual.html @@ -1,3 +1,4 @@ + Cordova tests diff --git a/tests/www/js/custom_mocha_html_reporter.js b/tests/www/js/custom_mocha_html_reporter.js index acc881f..110879b 100644 --- a/tests/www/js/custom_mocha_html_reporter.js +++ b/tests/www/js/custom_mocha_html_reporter.js @@ -22,7 +22,7 @@ prependPath.pop(); prependPath = prependPath.join('/') + '/'; - var lines = err.stack.split('\n'); + var lines = (err.stack || err.message || err).split('\n'); var line, filePath; for (var i = 1; i < lines.length; i++) { line = lines[i]; @@ -31,7 +31,7 @@ line = line.split('('); filePath = line[line.length - 1]; // Does the path need pre-pending? - if (filePath.indexOf(window.location.origin) === -1) { + if (filePath.indexOf('://') === -1) { // Insert the full path to the file line[line.length - 1] = prependPath + filePath; // Rejoin the line diff --git a/tests/www/js/utils.js b/tests/www/js/utils.js index 39af645..f2cb210 100644 --- a/tests/www/js/utils.js +++ b/tests/www/js/utils.js @@ -29,6 +29,11 @@ * call. */ utils.callOrder = function (calls, callback) { + var timeout = setTimeout(function () { + console.error('Did not receive all expected calls before 10s.\n' + + 'Call state (look for "called" parameter): '); + console.error(calls); + }, 10000); // Set called to 0 for (var i = 0; i < calls.length; i++) { calls[i].called = 0; @@ -76,6 +81,7 @@ } if (calls.length === expectedPos || calls[calls.length - 1].called === 1) { + clearTimeout(timeout); callback(); } }; @@ -92,6 +98,12 @@ * call. */ utils.waitForAllCalls = function (calls, callback) { + var timeout = setTimeout(function () { + console.error('Did not receive all expected calls before 10s.\n' + + 'Call state (look for "called" parameter): '); + console.error(calls); + }, 10000); + var called = []; return function (callId) { @@ -114,6 +126,7 @@ // Else, it has not been called before, so add it to called called.push(callId); if (called.length === calls.length) { + clearTimeout(timeout); callback(); } } From de16c09364425a5dbea41fd9278bc7ab4c405e88 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Wed, 16 Oct 2019 02:20:34 -0600 Subject: [PATCH 066/166] Fix crash because of NullPointerException. https://github.com/miloproductionsinc/cordova-plugin-chromecast/issues/3 --- src/android/ChromecastUtilities.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/android/ChromecastUtilities.java b/src/android/ChromecastUtilities.java index 5c16fa1..2b0f15d 100644 --- a/src/android/ChromecastUtilities.java +++ b/src/android/ChromecastUtilities.java @@ -626,7 +626,10 @@ static JSONObject createMetadataObject(MediaMetadata metadata) { return out; } - static JSONObject createTextTrackObject(TextTrackStyle textTrackStyle) { + private static JSONObject createTextTrackObject(TextTrackStyle textTrackStyle) { + if (textTrackStyle == null) { + return null; + } JSONObject out = new JSONObject(); try { out.put("backgroundColor", getHexColor(textTrackStyle.getBackgroundColor())); From 574b327445a944271f76354d646d53cda7eebef0 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Thu, 17 Oct 2019 12:47:38 -0600 Subject: [PATCH 067/166] Added base queue functions. Towards issue #36 Move some object generation methods to Utilities Disable method length rule. We are grown ups and can decide if we want to use ridiculously long methods or not. :) --- check_style.xml | 2 +- src/android/Chromecast.java | 37 ++ src/android/ChromecastSession.java | 497 ++++++++++++++++++--------- src/android/ChromecastUtilities.java | 366 ++++++++++++++++++-- tests/www/js/tests_auto.js | 240 +++++++++++++ tests/www/js/utils.js | 28 ++ www/chrome.cast.js | 97 +++++- 7 files changed, 1069 insertions(+), 198 deletions(-) diff --git a/check_style.xml b/check_style.xml index cec5dc6..632fa18 100644 --- a/check_style.xml +++ b/check_style.xml @@ -134,7 +134,7 @@ - + diff --git a/src/android/Chromecast.java b/src/android/Chromecast.java index 3c01000..8cb1fcf 100644 --- a/src/android/Chromecast.java +++ b/src/android/Chromecast.java @@ -385,6 +385,43 @@ public boolean mediaEditTracksInfo(JSONArray activeTrackIds, JSONObject textTrac return true; } + /** + * Loads a queue of media to the Chromecast. + * @param queueLoadRequest chrome.cast.media.QueueLoadRequest + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean queueLoad(JSONObject queueLoadRequest, final CallbackContext callbackContext) { + this.media.queueLoad(queueLoadRequest, callbackContext); + return true; + } + + /** + * Plays the item with itemId in the queue. + * @param itemId The ID of the item to jump to. + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean queueJumpToItem(Integer itemId, final CallbackContext callbackContext) { + this.media.queueJumpToItem(itemId, callbackContext); + return true; + } + + /** + * Plays the item with itemId in the queue. + * @param itemId The ID of the item to jump to. + * @param callbackContext called with .success or .error depending on the result + * @return true for cordova + */ + public boolean queueJumpToItem(Double itemId, final CallbackContext callbackContext) { + if (itemId - Double.valueOf(itemId).intValue() == 0) { + // Only perform the jump if the double is a whole number + return queueJumpToItem(Double.valueOf(itemId).intValue(), callbackContext); + } else { + return true; + } + } + /** * Stops the session. * @param callbackContext called with .success or .error depending on the result diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index 92fd75e..0bdfdf1 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -1,8 +1,7 @@ package acidhax.cordova.chromecast; import java.io.IOException; -import java.util.GregorianCalendar; -import java.util.Iterator; +import java.util.ArrayList; import org.apache.cordova.CallbackContext; import org.json.JSONArray; @@ -13,26 +12,24 @@ import com.google.android.gms.cast.Cast; import com.google.android.gms.cast.MediaInfo; import com.google.android.gms.cast.MediaLoadRequestData; -import com.google.android.gms.cast.MediaMetadata; +import com.google.android.gms.cast.MediaQueueItem; import com.google.android.gms.cast.MediaSeekOptions; import com.google.android.gms.cast.MediaStatus; -import com.google.android.gms.cast.TextTrackStyle; import com.google.android.gms.cast.framework.CastSession; +import com.google.android.gms.cast.framework.media.MediaQueue; import com.google.android.gms.cast.framework.media.RemoteMediaClient; import com.google.android.gms.cast.framework.media.RemoteMediaClient.MediaChannelResult; import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.common.api.Status; -import com.google.android.gms.common.images.WebImage; import android.app.Activity; -import android.net.Uri; + import androidx.annotation.NonNull; /* * All of the Chromecast session specific functions should start here. */ public class ChromecastSession { - /** The current context. */ private Activity activity; /** A registered callback that we will un-register and re-register each time the session changes. */ @@ -41,6 +38,14 @@ public class ChromecastSession { private CastSession session; /** The current session's client for controlling playback. */ private RemoteMediaClient client; + /** Indicates whether we are requesting media or not. **/ + private boolean requestingMedia = false; + /** Keeps track of the queueItems. **/ + private JSONArray queueItems; + /** Stores a callback that should be called when the queue is loaded. **/ + private Runnable queueReloadCallback; + /** Stores a callback that should be called when the queue status is updated. **/ + private Runnable queueStatusUpdatedCallback; /** * ChromecastSession constructor. @@ -68,53 +73,43 @@ public void run() { return; } session = castSession; - client = session.getRemoteMediaClient(); + client = session.getRemoteMediaClient(); if (client == null) { return; } client.registerCallback(new RemoteMediaClient.Callback() { - private String currentState = "idle"; + private int prevState = MediaStatus.PLAYER_STATE_IDLE; + private MediaInfo lastMedia; @Override public void onStatusUpdated() { MediaStatus status = client.getMediaStatus(); + if (requestingMedia + || queueStatusUpdatedCallback != null + || queueReloadCallback != null) { + return; + } + if (status != null) { - switch (status.getPlayerState()) { - case MediaStatus.PLAYER_STATE_LOADING: - case MediaStatus.PLAYER_STATE_IDLE: - if (!currentState.equals("requesting")) { - currentState = "loading"; - } - break; - default: - if (currentState.equals("loading")) { - clientListener.onMediaLoaded(createMediaObject()); - } - currentState = "loaded"; - break; + int state = status.getPlayerState(); + if (lastMedia != null + && state != prevState + && state == MediaStatus.PLAYER_STATE_LOADING) { + // It appears the queue has advanced to the next item + // So send an update to indicate the previous has finished + clientListener.onMediaUpdate(createMediaObject(MediaStatus.IDLE_REASON_FINISHED)); } + prevState = status.getPlayerState(); } + // Send update clientListener.onMediaUpdate(createMediaObject()); - } - @Override - public void onMetadataUpdated() { - clientListener.onMediaUpdate(createMediaObject()); + lastMedia = client.getMediaInfo(); } @Override public void onQueueStatusUpdated() { - clientListener.onMediaUpdate(createMediaObject()); - } - @Override - public void onPreloadStatusUpdated() { - clientListener.onMediaUpdate(createMediaObject()); - } - @Override - public void onSendingRemoteMediaRequest() { - currentState = "requesting"; - clientListener.onMediaUpdate(createMediaObject()); - } - @Override - public void onAdBreakStatusUpdated() { - clientListener.onMediaUpdate(createMediaObject()); + if (queueStatusUpdatedCallback != null) { + queueStatusUpdatedCallback.run(); + setQueueStatusUpdatedCallback(null); + } } }); session.addCastListener(new Cast.Listener() { @@ -144,6 +139,7 @@ public void onVolumeChanged() { clientListener.onSessionUpdate(createSessionObject()); } }); + setupQueue(); } }); } @@ -195,6 +191,8 @@ public void onResult(Status result) { }); } +/* ------------------------------------ MEDIA FNs ------------------------------------------- */ + /** * Loads media over the media API. * @param contentId - The URL of the content @@ -215,24 +213,25 @@ public void loadMedia(String contentId, JSONObject customData, String contentTyp } activity.runOnUiThread(new Runnable() { public void run() { - MediaInfo mediaInfo = createMediaInfo(contentId, customData, contentType, duration, streamType, metadata, textTrackStyle); + MediaInfo mediaInfo = ChromecastUtilities.createMediaInfo(contentId, customData, contentType, duration, streamType, metadata, textTrackStyle); MediaLoadRequestData loadRequest = new MediaLoadRequestData.Builder() .setMediaInfo(mediaInfo) .setAutoplay(autoPlay) .setCurrentTime((long) currentTime * 1000) .build(); + requestingMedia = true; + setQueueReloadCallback(new Runnable() { + @Override + public void run() { + callback.success(createMediaObject()); + } + }); client.load(loadRequest).setResultCallback(new ResultCallback() { @Override public void onResult(@NonNull MediaChannelResult result) { - if (result.getStatus().isSuccess()) { - JSONObject out = createMediaObject(); - if (out == null) { - callback.success(); - } else { - callback.success(out); - } - } else { + requestingMedia = false; + if (!result.getStatus().isSuccess()) { callback.error("session_error"); } } @@ -241,124 +240,6 @@ public void onResult(@NonNull MediaChannelResult result) { }); } - private MediaInfo createMediaInfo(String contentId, JSONObject customData, String contentType, long duration, String streamType, JSONObject metadata, JSONObject textTrackStyle) { - MediaInfo.Builder mediaInfoBuilder = new MediaInfo.Builder(contentId); - - MediaMetadata mediaMetadata; - try { - mediaMetadata = new MediaMetadata(metadata.getInt("metadataType")); - } catch (JSONException e) { - mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_GENERIC); - } - // Add any images - try { - JSONArray images = metadata.getJSONArray("images"); - for (int i = 0; i < images.length(); i++) { - JSONObject imageObj = images.getJSONObject(i); - try { - Uri imageURI = Uri.parse(imageObj.getString("url")); - mediaMetadata.addImage(new WebImage(imageURI)); - } catch (Exception e) { - } - } - } catch (JSONException e) { - } - - // Dynamically add other parameters - Iterator keys = metadata.keys(); - String key; - String convertedKey; - Object value; - while (keys.hasNext()) { - key = keys.next(); - if (key.equals("metadataType") - || key.equals("images") - || key.equals("type")) { - continue; - } - try { - value = metadata.get(key); - convertedKey = ChromecastUtilities.getAndroidMetadataName(key); - // Try to add the translated version of the key - switch (ChromecastUtilities.getMetadataType(convertedKey)) { - case "string": - mediaMetadata.putString(convertedKey, metadata.getString(key)); - break; - case "int": - mediaMetadata.putInt(convertedKey, metadata.getInt(key)); - break; - case "double": - mediaMetadata.putDouble(convertedKey, metadata.getDouble(key)); - break; - case "date": - GregorianCalendar c = new GregorianCalendar(); - if (value instanceof java.lang.Integer - || value instanceof java.lang.Long - || value instanceof java.lang.Float - || value instanceof java.lang.Double) { - c.setTimeInMillis(metadata.getLong(key)); - mediaMetadata.putDate(convertedKey, c); - } else { - String stringValue; - try { - stringValue = " value: " + metadata.getString(key); - } catch (JSONException e) { - stringValue = ""; - } - new Error("Cannot date from metadata key: " + key + stringValue - + "\n Dates must be in milliseconds from epoch UTC") - .printStackTrace(); - } - break; - case "ms": - mediaMetadata.putTimeMillis(convertedKey, metadata.getLong(key)); - break; - default: - } - // Also always add the client's version of the key because sometimes the - // MediaMetadata object removes some parameters. - // eg. If you pass metadataType == 2 == MEDIA_TYPE_TV_SHOW you will lose any - // subtitle added for "com.google.android.gms.cast.metadata.SUBTITLE", but this - // is not in-line with chrome desktop which preserves the value. - if (!key.equals(convertedKey)) { - // It is is really stubborn and if you try to add the key "subtitle" that is - // also stripped. (Hence the "cordova-plugin-chromecast_metadata_key=" prefix - convertedKey = "cordova-plugin-chromecast_metadata_key=" + key; - } - mediaMetadata.putString(convertedKey, metadata.getString(key)); - } catch (JSONException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } - } - - mediaInfoBuilder.setMetadata(mediaMetadata); - - int intStreamType; - switch (streamType) { - case "buffered": - intStreamType = MediaInfo.STREAM_TYPE_BUFFERED; - break; - case "live": - intStreamType = MediaInfo.STREAM_TYPE_LIVE; - break; - default: - intStreamType = MediaInfo.STREAM_TYPE_NONE; - } - - TextTrackStyle trackStyle = ChromecastUtilities.parseTextTrackStyle(textTrackStyle); - - mediaInfoBuilder - .setContentType(contentType) - .setCustomData(customData) - .setStreamType(intStreamType) - .setStreamDuration(duration) - .setTextTrackStyle(trackStyle); - - return mediaInfoBuilder.build(); - } - /** * Media API - Calls play on the current media. * @param callback called with success or error @@ -535,6 +416,268 @@ public void run() { }); } +/* ------------------------------------ QUEUE FNs ------------------------------------------- */ + + private void setQueueReloadCallback(Runnable callback) { + this.queueReloadCallback = callback; + } + + private void setQueueStatusUpdatedCallback(Runnable callback) { + this.queueStatusUpdatedCallback = callback; + } + + /** + * Sets up the objects and listeners required for queue functionality. + */ + public void setupQueue() { + MediaQueue queue = client.getMediaQueue(); + queueItems = null; + ChromecastUtilities.setQueueItems(queueItems); + queueReloadCallback = null; + // Set up the queue listener + queue.registerCallback(new MediaQueue.Callback() { + private boolean isQueueFinishedLoading = false; + private ArrayList lookingForIndexes = new ArrayList(); + + private void queueItemsPutAt(int index, JSONObject item) { + try { + queueItems.put(index, item); + } catch (JSONException e) { + e.printStackTrace(); + throw new RuntimeException("See above stack trace for error: " + e.getMessage()); + } + } + private void lookForItems(ArrayList indexes) { + synchronized (queue) { + // Merge the two arrays + lookingForIndexes.addAll(indexes); + checkItems(); + } + } + private void checkItems() { + MediaQueueItem item; + int index; + while (lookingForIndexes.size() > 0) { + index = lookingForIndexes.get(0); + item = queue.getItemAtIndex(index, true); + if (item != null) { + queueItemsPutAt(index, ChromecastUtilities.createQueueItem(item, index)); + lookingForIndexes.remove(0); + } else { + break; + } + } + if (lookingForIndexes.size() == 0) { + updateFinished(); + } + } + private void updateFinished() { + // Update the queueItems + ChromecastUtilities.setQueueItems(queueItems); + if (!isQueueFinishedLoading) { + isQueueFinishedLoading = true; + if (queueReloadCallback != null && queue.getItemCount() > 0) { + queueReloadCallback.run(); + setQueueReloadCallback(null); + } + } + clientListener.onMediaUpdate(createMediaObject()); + } + + @Override + public void itemsReloaded() { + synchronized (queue) { + isQueueFinishedLoading = false; + int itemCount = queue.getItemCount(); + if (queueReloadCallback == null) { + setQueueReloadCallback(new Runnable() { + @Override + public void run() { + // This was externally loaded + clientListener.onMediaLoaded(createMediaObject()); + } + }); + } + // init the arrays + ArrayList findIndexes = new ArrayList<>(); + queueItems = new JSONArray(); + for (int i = 0; i < itemCount; i++) { + findIndexes.add(i); + queueItems.put(null); + } + // Start loading the items + lookForItems(findIndexes); + } + } + @Override + public void itemsUpdatedAtIndexes(int[] ints) { + synchronized (queue) { + ArrayList unread = new ArrayList<>(); + for (int i : ints) { + if (queue.getItemAtIndex(i) == null) { + unread.add(i); + } + } + lookForItems(unread); + } + } + @Override + public void itemsInsertedInRange(int startIndex, int insertCount) { + synchronized (queue) { + // Make room for inserts + for (int i = 0; i < insertCount; i++) { + queueItems.put(new JSONObject()); + } + // Shift existing entries + for (int i = startIndex; i < startIndex + insertCount; i++) { + JSONObject movingObj = new JSONObject(); + try { + movingObj = queueItems.getJSONObject(i); + } catch (JSONException e) { + e.printStackTrace(); + throw new RuntimeException("Expected queueItems to contain index: " + + i + " queueItems.length: " + queueItems.length() + + "\nSee above stack trace for error: " + e.getMessage()); + } + queueItemsPutAt(i + insertCount, movingObj); + } + // Shift the lookingForIndexes + int index; + for (int i = 0; i < lookingForIndexes.size(); i++) { + index = lookingForIndexes.get(i); + if (index >= startIndex) { + lookingForIndexes.set(i, index + insertCount); + } + } + // null new entries and build indexes array to update + ArrayList updateIndexes = new ArrayList<>(); + for (int i = startIndex; i < startIndex + insertCount; i++) { + updateIndexes.add(startIndex + i); + queueItemsPutAt(startIndex + i, null); + } + // Trigger the media update + lookForItems(updateIndexes); + } + } + @Override + public void itemsRemovedAtIndexes(int[] ints) { + synchronized (queue) { + ArrayList lookingForInts = new ArrayList(); + // Remove the required indexes + int index; + for (int i : ints) { + queueItems.remove(i); + // Also, update/remove any references to look for these indexes + for (int j = 0; j < lookingForIndexes.size(); j++) { + index = lookingForIndexes.get(j); + if (index > i) { + lookingForInts.add(j, index - 1); + } else if (index < i) { + lookingForInts.add(index); + } + } + lookingForIndexes = lookingForInts; + } + // Trigger the media update + itemsUpdatedAtIndexes(new int[0]); + } + } + }); + } + + /** + * Loads a queue of media to the Chromecast. + * @param queueLoadRequest chrome.cast.media.QueueLoadRequest + * @param callback called with success or error + */ + public void queueLoad(JSONObject queueLoadRequest, CallbackContext callback) { + if (client == null || session == null) { + callback.error("session_error"); + return; + } + activity.runOnUiThread(new Runnable() { + public void run() { + try { + JSONArray qItems = queueLoadRequest.getJSONArray("items"); + MediaQueueItem[] items = new MediaQueueItem[qItems.length()]; + for (int i = 0; i < qItems.length(); i++) { + items[i] = ChromecastUtilities.createMediaQueueItem(qItems.getJSONObject(i)); + } + + int startIndex = queueLoadRequest.getInt("startIndex"); + int repeatMode = ChromecastUtilities.getAndroidRepeatMode(queueLoadRequest.getString("repeatMode")); + long playPosition = Double.valueOf(items[startIndex].getStartTime() * 1000).longValue(); + JSONObject customData = null; + try { + customData = queueLoadRequest.getJSONObject("customData"); + } catch (JSONException e) { + } + + setQueueReloadCallback(new Runnable() { + @Override + public void run() { + callback.success(createMediaObject()); + } + }); + client.queueLoad(items, startIndex, repeatMode, playPosition, customData).setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + if (!result.getStatus().isSuccess()) { + callback.error("session_error"); + setQueueReloadCallback(null); + } + } + }); + } catch (JSONException e) { + callback.error(ChromecastUtilities.createError("invalid_parameter", e.getMessage())); + } + } + }); + } + + /** + * Plays the item with itemId in the queue. + * @param itemId The ID of the item to jump to. + * @param callback called with .success or .error depending on the result + */ + public void queueJumpToItem(Integer itemId, CallbackContext callback) { + if (client == null || session == null) { + callback.error("session_error"); + return; + } + + activity.runOnUiThread(new Runnable() { + public void run() { + setQueueStatusUpdatedCallback(new Runnable() { + @Override + public void run() { + clientListener.onMediaUpdate(createMediaObject(MediaStatus.IDLE_REASON_INTERRUPTED)); + } + }); + client.queueJumpToItem(itemId, null) + .setResultCallback(new ResultCallback() { + @Override + public void onResult(@NonNull MediaChannelResult result) { + + if (result.getStatus().isSuccess()) { + callback.success(); + } else { + setQueueStatusUpdatedCallback(null); + JSONObject errorResult = result.getCustomData(); + String error = "Failed to jump to queue item with ID: " + itemId; + if (errorResult != null) { + error += "\nError details: " + errorResult; + } + callback.error(error); + } + } + }); + } + }); + } + +/* ------------------------------------ SESSION FNs ------------------------------------------- */ + /** * Sets the receiver volume level. * @param volume volume to set the receiver to @@ -579,6 +722,8 @@ public void run() { }); } +/* ------------------------------------ HELPERS ---------------------------------------------- */ + /** * Returns a resultCallback that wraps the callback and calls the onMediaUpdate listener. * @param callback client callback @@ -607,8 +752,24 @@ private JSONObject createSessionObject() { return ChromecastUtilities.createSessionObject(session); } + /** Last sent media object. **/ + private JSONObject lastMediaObject; private JSONObject createMediaObject() { - return ChromecastUtilities.createMediaObject(session); + return createMediaObject(null); + } + + private JSONObject createMediaObject(Integer idleReason) { + if (idleReason != null && lastMediaObject != null) { + try { + lastMediaObject.put("playerState", ChromecastUtilities.getMediaPlayerState(MediaStatus.PLAYER_STATE_IDLE)); + lastMediaObject.put("idleReason", ChromecastUtilities.getMediaIdleReason(idleReason)); + return lastMediaObject; + } catch (JSONException e) { + } + } + JSONObject out = ChromecastUtilities.createMediaObject(session); + lastMediaObject = out; + return out; } interface Listener extends Cast.MessageReceivedCallback { diff --git a/src/android/ChromecastUtilities.java b/src/android/ChromecastUtilities.java index 2b0f15d..dd719bd 100644 --- a/src/android/ChromecastUtilities.java +++ b/src/android/ChromecastUtilities.java @@ -1,13 +1,17 @@ package acidhax.cordova.chromecast; import android.graphics.Color; +import android.net.Uri; +import androidx.annotation.NonNull; import androidx.mediarouter.media.MediaRouter; import com.google.android.gms.cast.ApplicationMetadata; import com.google.android.gms.cast.CastDevice; import com.google.android.gms.cast.MediaInfo; import com.google.android.gms.cast.MediaMetadata; +import com.google.android.gms.cast.MediaQueueData; +import com.google.android.gms.cast.MediaQueueItem; import com.google.android.gms.cast.MediaStatus; import com.google.android.gms.cast.MediaTrack; import com.google.android.gms.cast.TextTrackStyle; @@ -18,17 +22,33 @@ import org.json.JSONException; import org.json.JSONObject; +import java.util.GregorianCalendar; +import java.util.Iterator; import java.util.List; import java.util.Set; final class ChromecastUtilities { + /** Stores a cache of the queueItems for building Media Objects. */ + private static JSONArray queueItems = null; private ChromecastUtilities() { //not called } - static String getMediaIdleReason(MediaStatus mediaStatus) { - switch (mediaStatus.getIdleReason()) { + /** + * Sets the queueItems to be returned with the media object. + * @param arr queueItems + */ + static void setQueueItems(JSONArray arr) { + // For some reason the desktop chrome behavior is that the queue items is never wiped out + // once they are in existence + if (arr == null || arr.length() > 0) { + queueItems = arr; + } + } + + static String getMediaIdleReason(int idleReason) { + switch (idleReason) { case MediaStatus.IDLE_REASON_CANCELED: return "CANCELLED"; case MediaStatus.IDLE_REASON_ERROR: @@ -43,8 +63,8 @@ static String getMediaIdleReason(MediaStatus mediaStatus) { } } - static String getMediaPlayerState(MediaStatus mediaStatus) { - switch (mediaStatus.getPlayerState()) { + static String getMediaPlayerState(int playerState) { + switch (playerState) { case MediaStatus.PLAYER_STATE_LOADING: case MediaStatus.PLAYER_STATE_BUFFERING: return "BUFFERING"; @@ -169,8 +189,8 @@ static String getWindowType(TextTrackStyle textTrackStyle) { } } - static String getRepeatMode(MediaStatus mediaStatus) { - switch (mediaStatus.getQueueRepeatMode()) { + static String getRepeatMode(int repeatMode) { + switch (repeatMode) { case MediaStatus.REPEAT_MODE_REPEAT_OFF: return "REPEAT_OFF"; case MediaStatus.REPEAT_MODE_REPEAT_ALL: @@ -184,6 +204,21 @@ static String getRepeatMode(MediaStatus mediaStatus) { } } + static int getAndroidRepeatMode(String clientRepeatMode) throws JSONException { + switch (clientRepeatMode) { + case "REPEAT_OFF": + return MediaStatus.REPEAT_MODE_REPEAT_OFF; + case "REPEAT_ALL": + return MediaStatus.REPEAT_MODE_REPEAT_ALL; + case "REPEAT_SINGLE": + return MediaStatus.REPEAT_MODE_REPEAT_SINGLE; + case "REPEAT_ALL_AND_SHUFFLE": + return MediaStatus.REPEAT_MODE_REPEAT_ALL_AND_SHUFFLE; + default: + throw new JSONException("Invalid repeat mode: " + clientRepeatMode); + } + } + static String getAndroidMetadataName(String clientName) { switch (clientName) { case "albumArtist": @@ -456,6 +491,10 @@ static JSONArray createMediaArray(CastSession session) { } static JSONObject createMediaObject(CastSession session) { + return createMediaObject(session, queueItems); + }; + + static JSONObject createMediaObject(CastSession session, JSONArray items) { JSONObject out = new JSONObject(); try { @@ -468,20 +507,21 @@ static JSONObject createMediaObject(CastSession session) { out.put("currentTime", mediaStatus.getStreamPosition() / 1000.0); out.put("customData", mediaStatus.getCustomData()); //out.put("extendedStatus",); - String idleReason = ChromecastUtilities.getMediaIdleReason(mediaStatus); + String idleReason = ChromecastUtilities.getMediaIdleReason(mediaStatus.getIdleReason()); if (idleReason != null) { out.put("idleReason", idleReason); } - //out.put("items", mediaStatus.getQueueItems()); + out.put("items", items); + out.put("isAlive", mediaStatus.getPlayerState() != MediaStatus.PLAYER_STATE_IDLE); //out.put("liveSeekableRange",); out.put("loadingItemId", mediaStatus.getLoadingItemId()); - out.put("media", createMediaInfoObject(session)); + out.put("media", createMediaInfoObject(session.getRemoteMediaClient().getMediaInfo())); out.put("mediaSessionId", 1); out.put("playbackRate", mediaStatus.getPlaybackRate()); - out.put("playerState", ChromecastUtilities.getMediaPlayerState(mediaStatus)); + out.put("playerState", ChromecastUtilities.getMediaPlayerState(mediaStatus.getPlayerState())); out.put("preloadedItemId", mediaStatus.getPreloadedItemId()); - //out.put("queueData", ); - out.put("repeatMode", getRepeatMode(mediaStatus)); + out.put("queueData", createQueueData(mediaStatus)); + out.put("repeatMode", getRepeatMode(mediaStatus.getQueueRepeatMode())); out.put("sessionId", session.getSessionId()); //out.put("supportedMediaCommands", ); //out.put("videoInfo", ); @@ -490,15 +530,7 @@ static JSONObject createMediaObject(CastSession session) { volume.put("level", mediaStatus.getStreamVolume()); volume.put("muted", mediaStatus.isMute()); out.put("volume", volume); - - long[] activeTrackIds = mediaStatus.getActiveTrackIds(); - if (activeTrackIds != null) { - JSONArray activeTracks = new JSONArray(); - for (long activeTrackId : activeTrackIds) { - activeTracks.put(activeTrackId); - } - out.put("activeTrackIds", activeTracks); - } + out.put("activeTrackIds", createActiveTrackIds(mediaStatus.getActiveTrackIds())); } catch (JSONException e) { } catch (NullPointerException e) { return null; @@ -507,13 +539,69 @@ static JSONObject createMediaObject(CastSession session) { return out; } - private static JSONArray createMediaInfoTracks(CastSession session) { + private static JSONArray createActiveTrackIds(long[] activeTrackIds) { JSONArray out = new JSONArray(); + try { + if (activeTrackIds.length == 0) { + return null; + } + for (long id : activeTrackIds) { + out.put(id); + } + } catch (NullPointerException e) { + return null; + } + return out; + } + static JSONObject createQueueData(MediaStatus status) { + JSONObject out = new JSONObject(); try { - MediaStatus mediaStatus = session.getRemoteMediaClient().getMediaStatus(); - MediaInfo mediaInfo = mediaStatus.getMediaInfo(); + MediaQueueData data = status.getQueueData(); + if (data == null) { + return null; + } + out.put("repeatMode", ChromecastUtilities.getRepeatMode(data.getRepeatMode())); + out.put("shuffle", data.getRepeatMode() == MediaStatus.REPEAT_MODE_REPEAT_ALL_AND_SHUFFLE); + out.put("startIndex", data.getStartIndex()); + } catch (JSONException e) { + e.printStackTrace(); + throw new RuntimeException("See above stack trace for error: " + e.getMessage()); + } + return out; + } + static JSONObject createQueueItem(@NonNull MediaQueueItem item, int orderId) { + JSONObject out = new JSONObject(); + try { + out.put("activeTrackIds", createActiveTrackIds(item.getActiveTrackIds())); + out.put("autoplay", item.getAutoplay()); + out.put("customData", item.getCustomData()); + out.put("itemId", item.getItemId()); + out.put("media", createMediaInfoObject(item.getMedia())); + out.put("orderId", orderId); + Double playbackDuration = item.getPlaybackDuration(); + if (Double.isInfinite(playbackDuration)) { + playbackDuration = null; + } + out.put("playbackDuration", playbackDuration); + out.put("preloadTime", item.getPreloadTime()); + Double startTime = item.getStartTime(); + if (Double.isNaN(startTime)) { + startTime = null; + } + out.put("startTime", startTime); + } catch (JSONException e) { + e.printStackTrace(); + throw new RuntimeException("See above stack trace for error: " + e.getMessage()); + } + return out; + } + + private static JSONArray createMediaInfoTracks(MediaInfo mediaInfo) { + JSONArray out = new JSONArray(); + + try { if (mediaInfo.getMediaTracks() == null) { return out; } @@ -543,12 +631,10 @@ private static JSONArray createMediaInfoTracks(CastSession session) { return out; } - private static JSONObject createMediaInfoObject(CastSession session) { + private static JSONObject createMediaInfoObject(MediaInfo mediaInfo) { JSONObject out = new JSONObject(); try { - MediaInfo mediaInfo = session.getRemoteMediaClient().getMediaInfo(); - // TODO: Missing attributes are commented out. // These are returned by the chromecast desktop SDK, we should probably return them too //out.put("breakClips",); @@ -560,7 +646,7 @@ private static JSONObject createMediaInfoObject(CastSession session) { //out.put("mediaCategory",); out.put("metadata", createMetadataObject(mediaInfo.getMetadata())); out.put("streamType", ChromecastUtilities.getMediaInfoStreamType(mediaInfo)); - out.put("tracks", createMediaInfoTracks(session)); + out.put("tracks", createMediaInfoTracks(mediaInfo)); out.put("textTrackStyle", ChromecastUtilities.createTextTrackObject(mediaInfo.getTextTrackStyle())); } catch (JSONException e) { @@ -572,6 +658,9 @@ private static JSONObject createMediaInfoObject(CastSession session) { static JSONObject createMetadataObject(MediaMetadata metadata) { JSONObject out = new JSONObject(); + if (metadata == null) { + return out; + } try { try { // Must be in own try catch @@ -685,4 +774,225 @@ static JSONObject createError(String code, String message) { } return out; } + +/* ------------------- Create NON-JSON (non-output) Objects ---------------------------------- */ + + /** + * Creates a MediaQueueItem from a JSONObject representation of a MediaQueueItem. + * @param mediaQueueItem a JSONObject representation of a MediaQueueItem + * @return a MediaQueueItem + * @throws JSONException If the input mediaQueueItem is incorrect + */ + static MediaQueueItem createMediaQueueItem(JSONObject mediaQueueItem) throws JSONException { + MediaInfo mediaInfo = createMediaInfo(mediaQueueItem.getJSONObject("media")); + MediaQueueItem.Builder builder = new MediaQueueItem.Builder(mediaInfo); + + try { + long[] activeTrackIds; + JSONArray trackIds = mediaQueueItem.getJSONArray("activeTrackIds"); + activeTrackIds = new long[trackIds.length()]; + for (int i = 0; i < trackIds.length(); i++) { + activeTrackIds[i] = trackIds.getLong(i); + } + builder.setActiveTrackIds(activeTrackIds); + } catch (JSONException e) { + } + try { + builder.setAutoplay(mediaQueueItem.getBoolean("autoplay")); + } catch (JSONException e) { + } + JSONObject customData = new JSONObject(); + try { + customData.getJSONObject("customData"); + } catch (JSONException e) { + } + try { + builder.setPlaybackDuration(mediaQueueItem.getDouble("playbackDuration")); + } catch (JSONException e) { + } + try { + builder.setPreloadTime(mediaQueueItem.getDouble("preloadTime")); + } catch (JSONException e) { + } + try { + builder.setStartTime(mediaQueueItem.getDouble("startTime")); + } catch (JSONException e) { + } + return builder.build(); + } + + static MediaInfo createMediaInfo(JSONObject mediaInfo) { + // Set defaults + String contentId = ""; + JSONObject customData = new JSONObject(); + String contentType = "unknown"; + long duration = 0; + String streamType = "unknown"; + JSONObject metadata = new JSONObject(); + JSONObject textTrackStyle = new JSONObject(); + + // Try to get the actual values + + // Try to get the actual values + try { + contentId = mediaInfo.getString("contentId"); + } catch (JSONException e) { + e.printStackTrace(); + } + try { + customData = mediaInfo.getJSONObject("customData"); + } catch (JSONException e) { + e.printStackTrace(); + } + try { + contentType = mediaInfo.getString("contentType"); + } catch (JSONException e) { + e.printStackTrace(); + } + try { + duration = mediaInfo.getLong("duration"); + } catch (JSONException e) { + e.printStackTrace(); + } + try { + streamType = mediaInfo.getString("streamType"); + } catch (JSONException e) { + e.printStackTrace(); + } + try { + metadata = mediaInfo.getJSONObject("metadata"); + } catch (JSONException e) { + e.printStackTrace(); + } + try { + textTrackStyle = mediaInfo.getJSONObject("textTrackStyle"); + } catch (JSONException e) { + e.printStackTrace(); + } + + return createMediaInfo(contentId, customData, contentType, duration, streamType, metadata, textTrackStyle); + } + + static MediaInfo createMediaInfo(String contentId, JSONObject customData, String contentType, long duration, String streamType, JSONObject metadata, JSONObject textTrackStyle) { + MediaInfo.Builder mediaInfoBuilder = new MediaInfo.Builder(contentId); + + mediaInfoBuilder.setMetadata(createMediaMetadata(metadata)); + + int intStreamType; + switch (streamType) { + case "buffered": + intStreamType = MediaInfo.STREAM_TYPE_BUFFERED; + break; + case "live": + intStreamType = MediaInfo.STREAM_TYPE_LIVE; + break; + default: + intStreamType = MediaInfo.STREAM_TYPE_NONE; + } + + TextTrackStyle trackStyle = ChromecastUtilities.parseTextTrackStyle(textTrackStyle); + + mediaInfoBuilder + .setContentType(contentType) + .setCustomData(customData) + .setStreamType(intStreamType) + .setStreamDuration(duration) + .setTextTrackStyle(trackStyle); + + return mediaInfoBuilder.build(); + } + + private static MediaMetadata createMediaMetadata(JSONObject metadata) { + + MediaMetadata mediaMetadata; + try { + mediaMetadata = new MediaMetadata(metadata.getInt("metadataType")); + } catch (JSONException e) { + mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_GENERIC); + } + // Add any images + try { + JSONArray images = metadata.getJSONArray("images"); + for (int i = 0; i < images.length(); i++) { + JSONObject imageObj = images.getJSONObject(i); + try { + Uri imageURI = Uri.parse(imageObj.getString("url")); + mediaMetadata.addImage(new WebImage(imageURI)); + } catch (Exception e) { + } + } + } catch (JSONException e) { + } + + // Dynamically add other parameters + Iterator keys = metadata.keys(); + String key; + String convertedKey; + Object value; + while (keys.hasNext()) { + key = keys.next(); + if (key.equals("metadataType") + || key.equals("images") + || key.equals("type")) { + continue; + } + try { + value = metadata.get(key); + convertedKey = ChromecastUtilities.getAndroidMetadataName(key); + // Try to add the translated version of the key + switch (ChromecastUtilities.getMetadataType(convertedKey)) { + case "string": + mediaMetadata.putString(convertedKey, metadata.getString(key)); + break; + case "int": + mediaMetadata.putInt(convertedKey, metadata.getInt(key)); + break; + case "double": + mediaMetadata.putDouble(convertedKey, metadata.getDouble(key)); + break; + case "date": + GregorianCalendar c = new GregorianCalendar(); + if (value instanceof java.lang.Integer + || value instanceof java.lang.Long + || value instanceof java.lang.Float + || value instanceof java.lang.Double) { + c.setTimeInMillis(metadata.getLong(key)); + mediaMetadata.putDate(convertedKey, c); + } else { + String stringValue; + try { + stringValue = " value: " + metadata.getString(key); + } catch (JSONException e) { + stringValue = ""; + } + new Error("Cannot date from metadata key: " + key + stringValue + + "\n Dates must be in milliseconds from epoch UTC") + .printStackTrace(); + } + break; + case "ms": + mediaMetadata.putTimeMillis(convertedKey, metadata.getLong(key)); + break; + default: + } + // Also always add the client's version of the key because sometimes the + // MediaMetadata object removes some parameters. + // eg. If you pass metadataType == 2 == MEDIA_TYPE_TV_SHOW you will lose any + // subtitle added for "com.google.android.gms.cast.metadata.SUBTITLE", but this + // is not in-line with chrome desktop which preserves the value. + if (!key.equals(convertedKey)) { + // It is is really stubborn and if you try to add the key "subtitle" that is + // also stripped. (Hence the "cordova-plugin-chromecast_metadata_key=" prefix + convertedKey = "cordova-plugin-chromecast_metadata_key=" + key; + } + mediaMetadata.putString(convertedKey, metadata.getString(key)); + } catch (JSONException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + } + return mediaMetadata; + } + } diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index 00c9783..46c6c8a 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -39,6 +39,7 @@ var success = 'success'; var update = 'update'; var stopped = 'stopped'; + var newMedia = 'newMedia'; var session; @@ -546,6 +547,7 @@ session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { media = m; utils.testMediaProperties(media); + assert.isUndefined(media.queueData); assert.equal(media.media.metadata.title, mediaInfo.metadata.title); assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); assert.equal(media.media.metadata.releaseDate, mediaInfo.metadata.releaseDate); @@ -1003,6 +1005,244 @@ assert.fail(err.code + ': ' + err.description); }); }); + describe('Queues', function () { + var videoItem; + var audioItem; + var startTime = 40; + function getCurrentItemIndex (media) { + for (var i = 0; i < media.items.length; i++) { + if (media.items[i].itemId === media.currentItemId) { + return i; + } + } + return 'Could get current item index for itemId: ' + media.currentItemId; + } + function checkItems (items) { + assert.isTrue(items[0].autoplay); + assert.equal(items[0].startTime, startTime); + assert.equal(items[0].media.contentId, videoUrl); + assert.isTrue(items[1].autoplay); + assert.equal(items[1].startTime, startTime * 2); + assert.equal(items[1].media.contentId, audioUrl); + } + before(function () { + videoItem = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + videoItem.metadata = new chrome.cast.media.TvShowMediaMetadata(); + videoItem.metadata.title = 'DaTitle'; + videoItem.metadata.subtitle = 'DaSubtitle'; + videoItem.metadata.originalAirDate = new Date().valueOf(); + videoItem.metadata.episode = 15; + videoItem.metadata.season = 2; + videoItem.metadata.seriesTitle = 'DaSeries'; + videoItem.metadata.images = [new chrome.cast.Image(imageUrl)]; + + audioItem = new chrome.cast.media.MediaInfo(audioUrl, 'audio/mpeg'); + audioItem.metadata = new chrome.cast.media.MusicTrackMediaMetadata(); + audioItem.metadata.albumArtist = 'DaAlmbumArtist'; + audioItem.metadata.albumName = 'DaAlbum'; + audioItem.metadata.artist = 'DaArtist'; + audioItem.metadata.composer = 'DaComposer'; + audioItem.metadata.title = 'DaTitle'; + audioItem.metadata.songName = 'DaSongName'; + audioItem.metadata.myMadeUpMetadata = '15'; + audioItem.metadata.releaseDate = new Date().valueOf(); + audioItem.metadata.images = [new chrome.cast.Image(imageUrl)]; + }); + it('session.queueLoad should return an error when we attempt to load an empty queue', function (done) { + session.queueLoad(new chrome.cast.media.QueueLoadRequest([]), function (m) { + assert.fail('Should not be able to load an empty queue.'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'INVALID_PARAMS'); + assert.deepEqual(err.details, { reason: 'INVALID_PARAMS', type: 'INVALID_REQUEST' }); + done(); + }); + }); + it('session.queueLoad should be able to load remote audio/video queue and return the correct Metadata', function (done) { + var item; + var queue = []; + + // Add items to the queue + item = new chrome.cast.media.QueueItem(videoItem); + item.startTime = startTime; + queue.push(item); + item = new chrome.cast.media.QueueItem(audioItem); + item.startTime = startTime * 2; + queue.push(item); + + // Create request to repeat all and start at 2nd item + var request = new chrome.cast.media.QueueLoadRequest(queue); + request.repeatMode = chrome.cast.media.RepeatMode.ALL; + request.startIndex = 1; + + session.queueLoad(request, function (m) { + media = m; + var i = getCurrentItemIndex(media); + utils.testMediaProperties(media); + assert.equal(media.currentItemId, media.items[i].itemId); + assert.equal(media.repeatMode, chrome.cast.media.RepeatMode.ALL); + assert.isObject(media.queueData); + assert.equal(media.queueData.repeatMode, request.repeatMode); + assert.isFalse(media.queueData.shuffle); + assert.equal(media.queueData.startIndex, request.startIndex); + utils.testQueueItems(media.items); + assert.equal(media.media.contentId, audioUrl); + assert.equal(media.items.length, 2); + checkItems(media.items); + assert.equal(media.items[i].media.metadata.albumArtist, audioItem.metadata.albumArtist); + assert.equal(media.items[i].media.metadata.albumName, audioItem.metadata.albumName); + assert.equal(media.items[i].media.metadata.artist, audioItem.metadata.artist); + assert.equal(media.items[i].media.metadata.composer, audioItem.metadata.composer); + assert.equal(media.items[i].media.metadata.title, audioItem.metadata.title); + assert.equal(media.items[i].media.metadata.songName, audioItem.metadata.songName); + assert.equal(media.items[i].media.metadata.releaseDate, audioItem.metadata.releaseDate); + assert.equal(media.items[i].media.metadata.images[0].url, audioItem.metadata.images[0].url); + assert.equal(media.items[i].media.metadata.myMadeUpMetadata, audioItem.metadata.myMadeUpMetadata); + assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.MUSIC_TRACK); + assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.MUSIC_TRACK); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + utils.testMediaProperties(media); + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + assert.closeTo(media.getEstimatedTime(), startTime * 2, 5); + done(); + } + }); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('Queue should start the next item automatically when previous one finishes (tests loop around of repeat_all as well)', function (done) { + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: stopped, repeats: true }, + { id: newMedia, repeats: true }, + { id: update, repeats: true } + ], done); + // Create request + var request = new chrome.cast.media.SeekRequest(); + request.currentTime = media.media.duration - 1; + + var i = getCurrentItemIndex(media); + // Listen for current media end + media.addUpdateListener(function listener (isAlive) { + if (media.playerState === chrome.cast.media.PlayerState.IDLE) { + assert.equal(media.idleReason, chrome.cast.media.IdleReason.FINISHED); + assert.isTrue(isAlive); + called(stopped); + } + if (media.currentItemId !== media.items[i].itemId) { + i = getCurrentItemIndex(media); + media.removeUpdateListener(listener); + utils.testMediaProperties(media); + assert.equal(media.repeatMode, chrome.cast.media.RepeatMode.ALL); + assert.equal(media.media.contentId, videoUrl); + utils.testQueueItems(media.items); + assert.equal(media.items.length, 2); + checkItems(media.items); + assert.equal(media.items[i].media.contentId, videoUrl); + assert.equal(media.items[i].media.metadata.title, videoItem.metadata.title); + assert.equal(media.items[i].media.metadata.subtitle, videoItem.metadata.subtitle); + assert.equal(media.items[i].media.metadata.originalAirDate, videoItem.metadata.originalAirDate); + assert.equal(media.items[i].media.metadata.episode, videoItem.metadata.episode); + assert.equal(media.items[i].media.metadata.season, videoItem.metadata.season); + assert.equal(media.items[i].media.metadata.seriesTitle, videoItem.metadata.seriesTitle); + assert.equal(media.items[i].media.metadata.images[0].url, videoItem.metadata.images[0].url); + assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.TV_SHOW); + assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.TV_SHOW); + called(newMedia); + window.m = media; + if (media.getEstimatedTime() > startTime - 5 + && media.getEstimatedTime() < startTime + 5) { + called(update); + } + } + }); + // Seek to just before the end + media.seek(request, function () { + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('media.queueJumpToItem should not call a callback for null contentId', function () { + media.queueJumpToItem(null, function () { + assert.fail('Should not be called when passing null content id to queueJumpToItem'); + }, function () { + assert.fail('Should not be called when passing null content id to queueJumpToItem'); + }); + }); + it('media.queueJumpToItem should not call a callback for unknown contentId', function () { + media.queueJumpToItem('unknown_content_id', function () { + assert.fail('Should not be called when passing unknown content id to queueJumpToItem'); + }, function () { + assert.fail('Should not be called when passing unknown content id to queueJumpToItem'); + }); + }); + it('media.queueJumpToItem should not call a callback for decimal contentId', function () { + media.queueJumpToItem(1.5, function () { + assert.fail('Should not be called when passing decimal content id to queueJumpToItem'); + }, function () { + assert.fail('Should not be called when passing decimal content id to queueJumpToItem'); + }); + }); + it('media.queueJumpToItem should jump to selected item', function (done) { + var calledAnyOrder = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + var calledOrder = utils.callOrder([ + { id: stopped, repeats: true }, + { id: newMedia, repeats: true } + ], function () { + calledAnyOrder(update); + }); + var i = getCurrentItemIndex(media); + media.addUpdateListener(function listener (isAlive) { + if (media.playerState === chrome.cast.media.PlayerState.IDLE) { + assert.equal(media.idleReason, chrome.cast.media.IdleReason.INTERRUPTED); + assert.isTrue(isAlive); + calledOrder(stopped); + } + if (media.currentItemId !== media.items[i].itemId) { + i = getCurrentItemIndex(media); + media.removeUpdateListener(listener); + utils.testMediaProperties(media); + assert.equal(media.currentItemId, media.items[i].itemId); + utils.testQueueItems(media.items); + assert.equal(media.media.contentId, audioUrl); + assert.equal(media.items.length, 2); + checkItems(media.items); + assert.equal(media.items[i].media.contentId, audioUrl); + assert.equal(media.items[i].media.metadata.albumArtist, audioItem.metadata.albumArtist); + assert.equal(media.items[i].media.metadata.albumName, audioItem.metadata.albumName); + assert.equal(media.items[i].media.metadata.artist, audioItem.metadata.artist); + assert.equal(media.items[i].media.metadata.composer, audioItem.metadata.composer); + assert.equal(media.items[i].media.metadata.title, audioItem.metadata.title); + assert.equal(media.items[i].media.metadata.songName, audioItem.metadata.songName); + assert.equal(media.items[i].media.metadata.releaseDate, audioItem.metadata.releaseDate); + assert.equal(media.items[i].media.metadata.images[0].url, audioItem.metadata.images[0].url); + assert.equal(media.items[i].media.metadata.myMadeUpMetadata, audioItem.metadata.myMadeUpMetadata); + assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.MUSIC_TRACK); + assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.MUSIC_TRACK); + assert.closeTo(media.getEstimatedTime(), startTime * 2, 5); + calledOrder(newMedia); + } + }); + // Jump + var jumpIndex = (i + 1) % media.items.length; + media.queueJumpToItem(media.items[jumpIndex].itemId, function () { + calledAnyOrder(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + }); after(function (done) { // Set up the expected calls var called = utils.waitForAllCalls([ diff --git a/tests/www/js/utils.js b/tests/www/js/utils.js index f2cb210..f0435c7 100644 --- a/tests/www/js/utils.js +++ b/tests/www/js/utils.js @@ -230,6 +230,34 @@ } }; + utils.testQueueItems = function (items) { + assert.isArray(items); + var item; + for (var i = 0; i < items.length; i++) { + item = items[i]; + assert.isBoolean(item.autoplay); + assert.isNumber(item.itemId); + utils.testQueueItemMediaInfoProperties(item.media); + assert.isNumber(item.orderId); + assert.isNumber(item.preloadTime); + assert.isNumber(item.startTime); + } + }; + + utils.testQueueItemMediaInfoProperties = function (mediaInfo) { + assert.isObject(mediaInfo); + assert.isString(mediaInfo.contentId); + assert.isString(mediaInfo.contentType); + if (mediaInfo.duration) { + assert.isNumber(mediaInfo.duration); + } + utils.testMediaMetadata(mediaInfo.metadata); + assert.isString(mediaInfo.streamType); + if (mediaInfo.tracks) { + assert.isArray(mediaInfo.tracks); + } + }; + window['cordova-plugin-chromecast-tests'] = window['cordova-plugin-chromecast-tests'] || {}; window['cordova-plugin-chromecast-tests'].utils = utils; }()); diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 42e14c5..77fe342 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -333,6 +333,35 @@ chrome.cast = { this.customData = null; }, + /** + * Represents an item in a media queue. + * @param {chrome.cast.media.MediaInfo} mediaInfo - Value must not be null. + */ + QueueItem: function (item) { + this.itemId = null; + this.media = item; + this.autoplay = !0; + this.startTime = 0; + this.playbackDuration = null; + this.preloadTime = 0; + this.customData = this.activeTrackIds = null; + }, + + /** + * A request to load and optionally start playback of a new ordered + * list of media items. + * @param {chrome.cast.media.QueueItem} items - The list of media items + * to load. Must not be null or empty. Value must not be null. + */ + QueueLoadRequest: function (items) { + this.type = 'QUEUE_LOAD'; + this.sessionId = this.requestId = null; + this.items = items; + this.startIndex = 0; + this.repeatMode = chrome.cast.media.RepeatMode.OFF; + this.customData = null; + }, + /** * A generic media description. * @property {chrome.cast.Image[]} images Content images. @@ -713,6 +742,38 @@ chrome.cast.Session.prototype.loadMedia = function (loadRequest, successCallback }); }; +/** + * Loads and optionally starts playback of a new queue of media items into a + * running receiver application. + * @param {chrome.cast.media.QueueLoadRequest} loadRequest - Request to load a + * new queue of media items. Value must not be null. + * @param {function} successCallback Invoked with the loaded Media on success. + * @param {function} errorCallback Invoked on error. The possible errors + * are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, + * SESSION_ERROR, and EXTENSION_MISSING. + */ +chrome.cast.Session.prototype.queueLoad = function (loadRequest, successCallback, errorCallback) { + if (this._preCheck(errorCallback)) { return; } + if (!loadRequest.items || loadRequest.items.length === 0) { + return errorCallback && errorCallback(new chrome.cast.Error( + chrome.cast.ErrorCode.SESSION_ERROR, 'INVALID_PARAMS', + { reason: 'INVALID_PARAMS', type: 'INVALID_REQUEST' })); + } + var self = this; + + execute('queueLoad', loadRequest, function (err, obj) { + if (!err) { + _currentMedia = new chrome.cast.media.Media(self.sessionId, obj.mediaSessionId); + _currentMedia._update(obj); + successCallback(_currentMedia); + // Also trigger the update notification + _currentMedia.emit('_mediaUpdated', _currentMedia.playerState !== 'IDLE'); + } else { + handleError(err, errorCallback); + } + }); +}; + /** * Adds a listener that is invoked when the Session has changed. * Changes to the following properties will trigger the listener: @@ -962,6 +1023,7 @@ chrome.cast.media.Media = function Media (sessionId, mediaSessionId) { this.volume = new chrome.cast.Volume(1, false); this._lastUpdatedTime = Date.now(); this.media = null; + this.queueData = undefined; }; chrome.cast.media.Media.prototype = Object.create(EventEmitter.prototype); @@ -1126,7 +1188,40 @@ chrome.cast.media.Media.prototype.editTracksInfo = function (editTracksInfoReque handleError(err, errorCallback); } }); +}; +/** + * Plays the item with itemId in the queue. + * If itemId is not found in the queue, either because it wasn't there + * originally or it was removed by another sender before calling this function, + * this function will silently return without sending a request to the + * receiver. + * + * @param {number} itemId The ID of the item to which to jump. + * Value must not be null. + * @param {function()} successCallback Invoked on success. + * @param {function(not-null chrome.cast.Error)} errorCallback Invoked on error. The possible errors are TIMEOUT, API_NOT_INITIALIZED, INVALID_PARAMETER, CHANNEL_ERROR, SESSION_ERROR, and EXTENSION_MISSING. + **/ +chrome.cast.media.Media.prototype.queueJumpToItem = function (itemId, successCallback, errorCallback) { + if (this._preCheck(errorCallback)) { return; } + var isValidItemId = false; + for (var i = 0; i < _currentMedia.items.length; i++) { + if (_currentMedia.items[i].itemId === itemId) { + isValidItemId = true; + break; + } + } + if (!isValidItemId) { + return; + } + + execute('queueJumpToItem', itemId, function (err) { + if (!err) { + successCallback && successCallback(); + } else { + handleError(err, errorCallback); + } + }); }; /** @@ -1299,7 +1394,7 @@ execute('setup', function (err, args) { if (_session) { _session.media[0] = _currentMedia; } - _currentMedia.emit('_mediaUpdated', _currentMedia.playerState !== 'IDLE'); + _currentMedia.emit('_mediaUpdated', !!media.isAlive); }, MEDIA_LOAD: function (media) { if (_session) { From c5cee81cfacfada435192fbeb73909a738832d62 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Thu, 17 Oct 2019 12:51:04 -0600 Subject: [PATCH 068/166] Improve tests styling a bit --- tests/www/css/tests.css | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/www/css/tests.css b/tests/www/css/tests.css index 3414998..3b793d7 100644 --- a/tests/www/css/tests.css +++ b/tests/www/css/tests.css @@ -1,7 +1,13 @@ +html, body { + height: 100%; +} +h1 { + margin: 0.5em; +} button { height: 3em; width: 10em; - margin-top: 1em; + margin: 0.5em; margin-bottom: 0em; } .center-horizontal { @@ -9,4 +15,7 @@ button { margin-left: auto; margin-right: auto; text-align: center; - } \ No newline at end of file +} +#mocha { + margin: 1em !important; +} \ No newline at end of file From 60e3c0146779e4deba1b65a01103c5309bf30d38 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sat, 19 Oct 2019 10:28:23 -0600 Subject: [PATCH 069/166] In tests_auto.js improve error output and move setup items to setup. --- tests/www/js/tests_auto.js | 74 +++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index 46c6c8a..e2ca4b6 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -18,19 +18,18 @@ // Set the reporter mocha.setup({ + bail: true, ui: 'bdd', useColors: true, - reporter: window['cordova-plugin-chromecast-tests'].customHtmlReporter + reporter: window['cordova-plugin-chromecast-tests'].customHtmlReporter, + slow: 8000, + timeout: 10000 }); var assert = window.chai.assert; var utils = window['cordova-plugin-chromecast-tests'].utils; describe('cordova-plugin-chromecast', function () { - this.timeout(10000); - this.slow(8000); - this.bail(true); - var imageUrl = 'https://ia800705.us.archive.org/1/items/GoodHousekeeping193810/Good%20Housekeeping%201938-10.jpg'; var videoUrl = 'https://ia801302.us.archive.org/1/items/TheWater_201510/TheWater.mp4'; var audioUrl = 'https://ia600304.us.archive.org/20/items/OTRR_Gunsmoke_Singles/Gunsmoke_52-10-03_024_Cain.mp3'; @@ -143,7 +142,7 @@ chrome.cast.initialize(apiConfig, function () { called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); @@ -188,10 +187,10 @@ chrome.cast.cordova.stopRouteScan(function () { done(); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('startRouteScan should find valid routes', function (done) { @@ -238,7 +237,7 @@ scanState = 'stopped'; called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); } }, function (err) { @@ -250,6 +249,7 @@ }); it('selectRoute should receive a TIMEOUT error if route does not exist', function (done) { this.timeout(20000); + this.slow(17000); var routeId = 'non-existant-route-id'; chrome.cast.cordova.selectRoute(routeId, function (session) { assert.fail('should not have hit the success callback'); @@ -266,7 +266,7 @@ utils.testSessionProperties(sess); done(); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('selectRoute should return error if already joined', function (done) { @@ -295,7 +295,7 @@ session.leave(function () { called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('initialize should not receive a session after session.leave', function (done) { @@ -305,7 +305,7 @@ chrome.cast.initialize(apiConfig, function () { done(); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('session.leave should give an error if session already left', function (done) { @@ -359,10 +359,10 @@ session = sess; done(); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); } }, function (err) { @@ -396,7 +396,7 @@ session.setReceiverMuted(muted, function () { called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('session.setReceiverVolumeLevel should set the volume level', function (done) { @@ -425,7 +425,7 @@ session.setReceiverVolumeLevel(requestedVolume, function () { called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('session.stop should stop the session', function (done) { @@ -444,7 +444,7 @@ session.stop(function () { called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('initialize should not receive a session after session.stop', function (done) { @@ -454,7 +454,7 @@ chrome.cast.initialize(apiConfig, function () { done(); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('session.stop should give an error if session already stopped', function (done) { @@ -512,10 +512,10 @@ session = sess; done(); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); } }, function (err) { @@ -575,7 +575,7 @@ } }); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('media.setVolume should set the volume', function (done) { @@ -613,7 +613,7 @@ assert.equal(media.volume.level, vol); called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('media.setVolume should set muted', function (done) { @@ -645,7 +645,7 @@ assert.equal(media.volume.muted, muted); called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('media.setVolume should set the volume and mute state', function (done) { @@ -686,7 +686,7 @@ assert.equal(media.volume.muted, muted); called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('media.pause should pause playback', function (done) { @@ -706,7 +706,7 @@ assert.equal(media.playerState, chrome.cast.media.PlayerState.PAUSED); called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('media.play should resume playback', function (done) { @@ -728,7 +728,7 @@ chrome.cast.media.PlayerState.BUFFERING]); called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('media.seek should skip to requested position', function (done) { @@ -750,7 +750,7 @@ assert.closeTo(media.getEstimatedTime(), request.currentTime, 1); called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('media.addUpdateListener should detect end of video', function (done) { @@ -771,7 +771,7 @@ media.seek(request, function () { called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('media.setVolume should return error when media is finished', function (done) { @@ -862,7 +862,7 @@ } }); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); function loadSecond () { @@ -899,7 +899,7 @@ } }); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); } }); @@ -941,7 +941,7 @@ } }); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('session.loadMedia should be able to load remote image and return the PhotoMediaMetadata', function (done) { @@ -981,7 +981,7 @@ } }); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('media.stop should end video playback', function (done) { @@ -1002,7 +1002,7 @@ assert.equal(media.idleReason, chrome.cast.media.IdleReason.CANCELLED); called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); describe('Queues', function () { @@ -1114,7 +1114,7 @@ } }); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('Queue should start the next item automatically when previous one finishes (tests loop around of repeat_all as well)', function (done) { @@ -1167,7 +1167,7 @@ media.seek(request, function () { called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); it('media.queueJumpToItem should not call a callback for null contentId', function () { @@ -1239,7 +1239,7 @@ media.queueJumpToItem(media.items[jumpIndex].itemId, function () { calledAnyOrder(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); }); @@ -1259,7 +1259,7 @@ session.stop(function () { called(success); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); }); From 0dbe15c11d9312493d0e8f9a98fa967dbfd6ed54 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sat, 19 Oct 2019 10:30:15 -0600 Subject: [PATCH 070/166] Added beginning of manual tests (requestSession) Towards issue #36 --- tests/www/chrome/cordova_stubs.js | 18 +- tests/www/chrome/tests_manual_chrome.html | 50 ++++ tests/www/css/tests.css | 20 +- tests/www/html/tests_manual.html | 27 +- tests/www/js/tests_manual.js | 292 +++++++++++++++++++++- 5 files changed, 396 insertions(+), 11 deletions(-) create mode 100644 tests/www/chrome/tests_manual_chrome.html diff --git a/tests/www/chrome/cordova_stubs.js b/tests/www/chrome/cordova_stubs.js index ae5641e..e61e2ee 100644 --- a/tests/www/chrome/cordova_stubs.js +++ b/tests/www/chrome/cordova_stubs.js @@ -11,6 +11,8 @@ chrome.cast = chrome.cast || {}; chrome.cast.cordova = {}; +/* -------------------------- Poly fill Cordova Functions ---------------------------------- */ + var startJoiningButton = document.getElementById('start-session'); var doneJoiningButton = document.getElementById('joined-session'); var _scanning = false; @@ -95,9 +97,7 @@ doneJoiningButton.style = 'display:none;'; clearTimeout(timeout); - // setTimeout(function () { successCallback(session); - // }, 1000); }); doneJoiningButton.style = ''; @@ -122,11 +122,23 @@ successCallback(['SETUP']); }; +/* ------------------------- Start Tests ---------------------------------- */ + // This actually starts the tests window['__onGCastApiAvailable'] = function (isAvailable, err) { // If error, it is probably because we are not on chrome, so just disregard if (isAvailable) { - mocha.run(); + var runner; + if (window['cordova-plugin-chromecast-tests'].runMocha) { + runner = window['cordova-plugin-chromecast-tests'].runMocha(); + } else { + runner = mocha.run(); + } + // This makes it so that tests actually fail in the case of + // uncaught exceptions inside promise catch blocks + window.addEventListener('unhandledrejection', function (event) { + runner.fail(runner.test, event.reason); + }); } }; diff --git a/tests/www/chrome/tests_manual_chrome.html b/tests/www/chrome/tests_manual_chrome.html new file mode 100644 index 0000000..4440183 --- /dev/null +++ b/tests/www/chrome/tests_manual_chrome.html @@ -0,0 +1,50 @@ + + + + Cordova tests + + + + + + + + + + + + + +

      Manual Tests

      +

      + + +
      +

      Action required:

      +

      Starting Tests

      + +
      + + + +
      + + + + + + diff --git a/tests/www/css/tests.css b/tests/www/css/tests.css index 3b793d7..188976b 100644 --- a/tests/www/css/tests.css +++ b/tests/www/css/tests.css @@ -6,9 +6,8 @@ h1 { } button { height: 3em; - width: 10em; + min-width: 10em; margin: 0.5em; - margin-bottom: 0em; } .center-horizontal { display: block; @@ -16,6 +15,23 @@ button { margin-right: auto; text-align: center; } +#action { + margin: 0.5em; + border-style: solid; + border-width: 1px; + background-color: bisque; +} +#action h3 { + margin: 0.5em; +} +p { + margin: 0.5em; +} +#action-button { + display: none; + margin-left: auto; + margin-right: 0.5em; +} #mocha { margin: 1em !important; } \ No newline at end of file diff --git a/tests/www/html/tests_manual.html b/tests/www/html/tests_manual.html index 0aede16..e1b464d 100644 --- a/tests/www/html/tests_manual.html +++ b/tests/www/html/tests_manual.html @@ -10,17 +10,40 @@ - + + + +

      Manual Tests

      +

      + +
      +

      Action required:

      +

      Starting Tests

      +
      +
      + + + diff --git a/tests/www/js/tests_manual.js b/tests/www/js/tests_manual.js index c9cc561..e64ad76 100644 --- a/tests/www/js/tests_manual.js +++ b/tests/www/js/tests_manual.js @@ -1,11 +1,295 @@ +/** + * The order of these tests and this.bail(true) is very important. + * + * Rather than nesting deep with describes and before's we just ensure the + * tests occur in the correct order. + * The major advantage to this is not having to repeat test code frequently + * making the suite slow. + * + */ - /* eslint-disable no-undef */ +(function () { + 'use strict'; + /* eslint-env mocha */ + /* global chrome */ + var assert = window.chai.assert; + var utils = window['cordova-plugin-chromecast-tests'].utils; + var isDesktop = window['cordova-plugin-chromecast-tests'].isDesktop || false; + var nextTestNum = getTestsPassed(); + var runningTestNum = 0; + var beforeTimeout; + var beforeTimeoutMs = 4000; + +/* ------------------- Some helper functions ------------------------------ */ + function setNextTestNum (passed) { + document.cookie = 'nextTestNum=' + passed + ';path=/'; + } + function getTestsPassed () { + var name = 'nextTestNum='; + var ca = document.cookie.split(';'); + for (var i = 0; i < ca.length; i++) { + var c = ca[i].trim(); + if (c.indexOf(name) === 0) { + try { + return parseInt(c.substring(name.length, c.length)); + } catch (err) { + } + } + } + return 0; + } + function setAction (text, btnCallback, btnText) { + document.getElementById('action-text').innerHTML = text; + var button = document.getElementById('action-button'); + if (btnCallback) { + button.style.display = 'block'; + button.onclick = btnCallback; + } else { + button.style.display = 'none'; + } + button.innerHTML = btnText || 'Done'; + } + function clearAction () { + setAction('None.'); + } + // wrap mocha functions so that we can skip previously passed tests + function wrapMochaFns () { + var origIt = it; + window.it = function (title, test) { + if (test.length < 1) { + throw new Error('test: "' + title + '" must use the "done" callback'); + } + origIt(title, function (done) { + // Should we skip this test? + if (runningTestNum < nextTestNum) { + runningTestNum++; + return done(); + } + test(function (err) { + // Test called done + if (!err) { + // If no error, increment next test num + runningTestNum++; + setNextTestNum(++nextTestNum); + } + clearAction(); + done(err); + }); + }); + }; + var origBeforeEach = beforeEach; + function runBeforeEach (name, test, shouldRunFn) { + if (test.length < 1) { + throw new Error('beforeEach: must use the "done" callback'); + } + var stack = new Error().stack.split('\n'); + var timeoutErr = stack[0] + '\nTimeout during: ' + name + '\n'; + while (stack.length > 0 && (stack[0].indexOf('tests_manual') === -1 + || stack[0].match(/^[^(]*before/i))) { + stack.splice(0, 1); + } + timeoutErr += stack.join('\n'); + origBeforeEach(function (done) { + if (!shouldRunFn()) { + // If we should not run the test + return done(); + } + beforeTimeout = setTimeout(function () { + assert.fail(timeoutErr); + }, beforeTimeoutMs); + test(function () { + clearTimeout(beforeTimeout); + done(); + }); + }); + } + window.beforeEach = function (name, test) { + if (!test) { + test = name; + name = ''; + } + name = 'beforeEach("' + name + '")'; + runBeforeEach(name, test, function () { + // If we shouldn't skip this test + return runningTestNum >= nextTestNum; + }); + }; + window.before = function (name, test) { + if (!test) { + test = name; + name = ''; + } + name = 'before("' + name + '")'; + var calledBefore = false; + runBeforeEach(name, test, function () { + if (!calledBefore && nextTestNum <= runningTestNum) { + calledBefore = true; + return true; + } + return false; + }); + }; + } + +/* ----------------------------- Setup ------------------------------------- */ + + window.addEventListener('load', function () { + document.getElementById('skipped-tests').innerHTML = 'Skipped to test: #' + + nextTestNum + ' (0-indexed)
      Click "Re-run" to run tests from the beginning'; + document.getElementById('rerun').onclick = function () { + setNextTestNum(0); + window.location.reload(); + }; + }); + + // Set the reporter + mocha.setup({ + bail: true, + ui: 'bdd', + useColors: true, + reporter: window['cordova-plugin-chromecast-tests'].customHtmlReporter, + slow: 10000, + timeout: 0 + }); + +/* ----------------------------- Tests ------------------------------------- */ + + describe('cordova-plugin-chromecast', function () { + wrapMochaFns(); + + // callOrder constants that are re-used frequently + var success = 'success'; + var stopped = 'stopped'; + + var session; + + before('Api should be available', function (done) { var interval = setInterval(function () { if (chrome && chrome.cast && chrome.cast.isAvailable) { clearInterval(interval); - chrome.cast.cordova.stopRouteScan(function () { - throw new Error('Just gonna throw this error for demonstration'); + done(); + } + }, 100); + }); + + describe('State: No session automatically discovered', function () { + before('Initialize should succeed and should not receive a session', function (done) { + var unavailable = 'unavailable'; + var available = 'available'; + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: unavailable, repeats: true }, + { id: available, repeats: true } + ], function () { + finished = true; + // Give it a moment to detect the fail condition + // of a session being discovered (so that we don't + // start running a test) + setTimeout(function () { + done(); + }, 500); + }); + var finished = false; // Need this so we stop testing after being finished + var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function sessionListener (session) { + assert.fail('should not receive a session (make sure there is no active cast session when starting the tests)'); + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }); + chrome.cast.initialize(apiConfig, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + it('chrome.cast.requestSession cancel should return error', function (done) { + setAction('1. Click "Open Dialog".
      2. Click outside of the chromecast chooser dialog to dismiss it.', function () { + chrome.cast.requestSession(function (sess) { + session = sess; + assert.fail('We should not reach here on dismiss (make sure you cancelled the dialog for this test!)'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + done(); + }); + }, 'Open Dialog'); + }); + it('chrome.cast.requestSession success should return a session', function (done) { + setAction('1. Click "Open Dialog".
      2. Select a device in the chromecast chooser dialog.', function () { + chrome.cast.requestSession(function (sess) { + session = sess; + utils.testSessionProperties(session); + done(); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }, 'Open Dialog'); + }); + it('chrome.cast.requestSession (stop casting) cancel should return error', function (done) { + setAction('1. Click "Open Dialog".
      2. Click outside of the stop casting dialog to dismiss it.', function () { + chrome.cast.requestSession(function (session) { + assert.fail('We should not reach here on dismiss (make sure you cancelled the dialog for this test!)'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + done(); }); + }, 'Open Dialog'); + }); + it('chrome.cast.requestSession (stop casting) clicking "Stop Casting" should stop the session', function (done) { + var called = utils.callOrder([ + { id: stopped, repeats: false }, + { id: success, repeats: false } + ], done); + session.addUpdateListener(function listener (isAlive) { + if (session.status === chrome.cast.SessionStatus.STOPPED) { + session.removeUpdateListener(listener); + assert.isFalse(isAlive); + called(stopped); + } + }); + setAction('1. Click "Open Dialog".
      2. Select "Stop Casting" in the stop casting dialog.' + + (isDesktop ? '
      3. Click outside of the stop casting dialog to dismiss it.' : ''), + function () { + chrome.cast.requestSession(function (session) { + assert.fail('We should not reach here on stop casting'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + called(success); + }); + }, 'Open Dialog'); + }); + after('Ensure session is stopped', function (done) { + if (!session) { + return done(); } - }, 500); + session.stop(function () { + done(); + }, function () { + done(); + }); + }); + + }); + + }); + + window['cordova-plugin-chromecast-tests'] = window['cordova-plugin-chromecast-tests'] || {}; + window['cordova-plugin-chromecast-tests'].runMocha = function () { + var runner = mocha.run(); + runner.on('suite end', function (suite) { + clearTimeout(beforeTimeout); + var passed = this.stats.passes === runner.total; + if (passed) { + setAction('All tests passed!'); + document.getElementById('action').style.backgroundColor = '#ceffc4'; + setNextTestNum(0); + } + }); + return runner; + }; + +}()); From d42462aec54536692530ca1837ca4ebe8c0bc339 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 20 Oct 2019 10:12:35 -0600 Subject: [PATCH 071/166] Added the restart app / rejoin session manual test Removed test skipping/memory for manual tests Move some functions to utils so we can use them from multiple locations --- tests/www/chrome/cordova_stubs.js | 32 +-- tests/www/chrome/tests_auto_chrome.html | 8 +- tests/www/chrome/tests_manual_chrome.html | 4 +- tests/www/html/tests_manual.html | 6 +- tests/www/js/tests_manual.js | 233 +++++++--------------- tests/www/js/utils.js | 75 +++++++ 6 files changed, 169 insertions(+), 189 deletions(-) diff --git a/tests/www/chrome/cordova_stubs.js b/tests/www/chrome/cordova_stubs.js index e61e2ee..7d65a8f 100644 --- a/tests/www/chrome/cordova_stubs.js +++ b/tests/www/chrome/cordova_stubs.js @@ -7,14 +7,14 @@ /* eslint-env mocha */ /* global chrome */ + var utils = window['cordova-plugin-chromecast-tests'].utils; + window.chrome = window.chrome || {}; chrome.cast = chrome.cast || {}; chrome.cast.cordova = {}; /* -------------------------- Poly fill Cordova Functions ---------------------------------- */ - var startJoiningButton = document.getElementById('start-session'); - var doneJoiningButton = document.getElementById('joined-session'); var _scanning = false; var _startRouteScanErrorCallback; @@ -79,31 +79,19 @@ } var timeout = setTimeout(function () { - console.error('Make sure to click done joining button.'); + console.error('Make sure to click the "Done Joining" button.'); }, 10000); - // set up show the start join button - startJoiningButton.addEventListener('click', function joinListener () { - // hide the start joining button - startJoiningButton.style = 'display:none;'; - startJoiningButton.removeEventListener('click', joinListener); - + utils.setAction('1. Click "Request Session".', function () { + utils.setAction('2. Select a device in the chromecast dialog.'); chrome.cast.requestSession(function (session) { - - // set up and show the done joining button - doneJoiningButton.addEventListener('click', function doneListener () { - // Hide the done joining button - doneJoiningButton.removeEventListener('click', doneListener); - doneJoiningButton.style = 'display:none;'; - - clearTimeout(timeout); + clearTimeout(timeout); + utils.setAction('3. Click "Done Joining" after the session has started.', function () { + utils.clearAction(); successCallback(session); - }); - doneJoiningButton.style = ''; - + }, 'Done Joining'); }, errorCallback); - }); - startJoiningButton.style = ''; + }, 'Request Session'); }; chrome.cast.cordova.Route = function (jsonRoute) { diff --git a/tests/www/chrome/tests_auto_chrome.html b/tests/www/chrome/tests_auto_chrome.html index 4987113..3963a42 100644 --- a/tests/www/chrome/tests_auto_chrome.html +++ b/tests/www/chrome/tests_auto_chrome.html @@ -29,10 +29,10 @@

      Auto Tests

      -
      - Action Required: - - +
      +

      Action required:

      +

      Starting Tests...

      +
      diff --git a/tests/www/chrome/tests_manual_chrome.html b/tests/www/chrome/tests_manual_chrome.html index 4440183..8803a20 100644 --- a/tests/www/chrome/tests_manual_chrome.html +++ b/tests/www/chrome/tests_manual_chrome.html @@ -23,7 +23,7 @@

      Manual Tests

      Back - + @@ -32,7 +32,7 @@

      Manual Tests

      Action required:

      -

      Starting Tests

      +

      Starting Tests...

      diff --git a/tests/www/html/tests_manual.html b/tests/www/html/tests_manual.html index e1b464d..2bd5ba7 100644 --- a/tests/www/html/tests_manual.html +++ b/tests/www/html/tests_manual.html @@ -19,12 +19,12 @@

      Manual Tests

      - + - + @@ -33,7 +33,7 @@

      Manual Tests

      Action required:

      -

      Starting Tests

      +

      Starting Tests...

      diff --git a/tests/www/js/tests_manual.js b/tests/www/js/tests_manual.js index e64ad76..a218139 100644 --- a/tests/www/js/tests_manual.js +++ b/tests/www/js/tests_manual.js @@ -16,134 +16,7 @@ var assert = window.chai.assert; var utils = window['cordova-plugin-chromecast-tests'].utils; var isDesktop = window['cordova-plugin-chromecast-tests'].isDesktop || false; - var nextTestNum = getTestsPassed(); - var runningTestNum = 0; - var beforeTimeout; - var beforeTimeoutMs = 4000; -/* ------------------- Some helper functions ------------------------------ */ - function setNextTestNum (passed) { - document.cookie = 'nextTestNum=' + passed + ';path=/'; - } - function getTestsPassed () { - var name = 'nextTestNum='; - var ca = document.cookie.split(';'); - for (var i = 0; i < ca.length; i++) { - var c = ca[i].trim(); - if (c.indexOf(name) === 0) { - try { - return parseInt(c.substring(name.length, c.length)); - } catch (err) { - } - } - } - return 0; - } - function setAction (text, btnCallback, btnText) { - document.getElementById('action-text').innerHTML = text; - var button = document.getElementById('action-button'); - if (btnCallback) { - button.style.display = 'block'; - button.onclick = btnCallback; - } else { - button.style.display = 'none'; - } - button.innerHTML = btnText || 'Done'; - } - function clearAction () { - setAction('None.'); - } - // wrap mocha functions so that we can skip previously passed tests - function wrapMochaFns () { - var origIt = it; - window.it = function (title, test) { - if (test.length < 1) { - throw new Error('test: "' + title + '" must use the "done" callback'); - } - origIt(title, function (done) { - // Should we skip this test? - if (runningTestNum < nextTestNum) { - runningTestNum++; - return done(); - } - test(function (err) { - // Test called done - if (!err) { - // If no error, increment next test num - runningTestNum++; - setNextTestNum(++nextTestNum); - } - clearAction(); - done(err); - }); - }); - }; - var origBeforeEach = beforeEach; - function runBeforeEach (name, test, shouldRunFn) { - if (test.length < 1) { - throw new Error('beforeEach: must use the "done" callback'); - } - var stack = new Error().stack.split('\n'); - var timeoutErr = stack[0] + '\nTimeout during: ' + name + '\n'; - while (stack.length > 0 && (stack[0].indexOf('tests_manual') === -1 - || stack[0].match(/^[^(]*before/i))) { - stack.splice(0, 1); - } - timeoutErr += stack.join('\n'); - origBeforeEach(function (done) { - if (!shouldRunFn()) { - // If we should not run the test - return done(); - } - beforeTimeout = setTimeout(function () { - assert.fail(timeoutErr); - }, beforeTimeoutMs); - test(function () { - clearTimeout(beforeTimeout); - done(); - }); - }); - } - window.beforeEach = function (name, test) { - if (!test) { - test = name; - name = ''; - } - name = 'beforeEach("' + name + '")'; - runBeforeEach(name, test, function () { - // If we shouldn't skip this test - return runningTestNum >= nextTestNum; - }); - }; - window.before = function (name, test) { - if (!test) { - test = name; - name = ''; - } - name = 'before("' + name + '")'; - var calledBefore = false; - runBeforeEach(name, test, function () { - if (!calledBefore && nextTestNum <= runningTestNum) { - calledBefore = true; - return true; - } - return false; - }); - }; - } - -/* ----------------------------- Setup ------------------------------------- */ - - window.addEventListener('load', function () { - document.getElementById('skipped-tests').innerHTML = 'Skipped to test: #' - + nextTestNum + ' (0-indexed)
      Click "Re-run" to run tests from the beginning'; - document.getElementById('rerun').onclick = function () { - setNextTestNum(0); - window.location.reload(); - }; - }); - - // Set the reporter mocha.setup({ bail: true, ui: 'bdd', @@ -153,28 +26,32 @@ timeout: 0 }); -/* ----------------------------- Tests ------------------------------------- */ - describe('cordova-plugin-chromecast', function () { - wrapMochaFns(); - // callOrder constants that are re-used frequently var success = 'success'; var stopped = 'stopped'; + var update = 'update'; var session; + var sessionListener = function (sess) { }; - before('Api should be available', function (done) { + before('Api should be available and initialize successfully', function (done) { + session = null; var interval = setInterval(function () { if (chrome && chrome.cast && chrome.cast.isAvailable) { clearInterval(interval); - done(); + initializeApi(); } }, 100); - }); - - describe('State: No session automatically discovered', function () { - before('Initialize should succeed and should not receive a session', function (done) { + function initializeApi () { + var finished = false; // Need this so we stop testing after being finished + sessionListener = function (sess) { + if (!finished) { + assert.fail('got session before "success", "unavailable", "available" sequence completed'); + } + utils.testSessionProperties(sess); + session = sess; + }; var unavailable = 'unavailable'; var available = 'available'; var called = utils.callOrder([ @@ -183,29 +60,71 @@ { id: available, repeats: true } ], function () { finished = true; - // Give it a moment to detect the fail condition - // of a session being discovered (so that we don't - // start running a test) - setTimeout(function () { - done(); - }, 500); - }); - var finished = false; // Need this so we stop testing after being finished - var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function sessionListener (session) { - assert.fail('should not receive a session (make sure there is no active cast session when starting the tests)'); - }, function receiverListener (availability) { - if (!finished) { - called(availability); - } + done(); }); + var apiConfig = new chrome.cast.ApiConfig( + new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), + sessionListener, + function receiverListener (availability) { + if (!finished) { + called(availability); + } + }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); chrome.cast.initialize(apiConfig, function () { called(success); }, function (err) { assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); + } + }); + + describe('App restart', function () { + it('Restart app with active session, should receive session on initialize', function (done) { + var interval; + var time = 15000; + interval = setInterval(function () { + time -= 500; + if (session) { + clearInterval(interval); + done(); + } + if (time < 0) { + clearInterval(interval); + assert.fail('Failed to find session for 15s after app restart. ' + + 'Make sure that a session started by this device is active during app restart.'); + } + }, 500); + utils.setAction('Situation #1 - First time you have reached the "restart app" test:
      ' + + '    1. Click "Start Session"

      ' + + 'Situation #2 - You have force killed and restarted the app:
      ' + + '    1. Wait for session discovery. (Fails if none found after 15s)
      ', function () { + clearInterval(interval); + utils.startSession(function (sess) { + session = sess; + if (isDesktop) { + utils.setAction('1. Open a new tab and visit this url.'); + } else { + utils.setAction('1. Force kill and restart the app.'); + } + }); + }, 'Start Session'); + }); + after('Ensure session is stopped', function (done) { + session.stop(function () { + session = null; + done(); + }, function () { + done(); + }); + }); + }); + + describe('State: No session automatically discovered', function () { + before(function () { + assert.notExists(session); }); it('chrome.cast.requestSession cancel should return error', function (done) { - setAction('1. Click "Open Dialog".
      2. Click outside of the chromecast chooser dialog to dismiss it.', function () { + utils.setAction('1. Click "Open Dialog".
      2. Click outside of the chromecast chooser dialog to dismiss it.', function () { chrome.cast.requestSession(function (sess) { session = sess; assert.fail('We should not reach here on dismiss (make sure you cancelled the dialog for this test!)'); @@ -217,7 +136,7 @@ }, 'Open Dialog'); }); it('chrome.cast.requestSession success should return a session', function (done) { - setAction('1. Click "Open Dialog".
      2. Select a device in the chromecast chooser dialog.', function () { + utils.setAction('1. Click "Open Dialog".
      2. Select a device in the chromecast chooser dialog.', function () { chrome.cast.requestSession(function (sess) { session = sess; utils.testSessionProperties(session); @@ -228,7 +147,7 @@ }, 'Open Dialog'); }); it('chrome.cast.requestSession (stop casting) cancel should return error', function (done) { - setAction('1. Click "Open Dialog".
      2. Click outside of the stop casting dialog to dismiss it.', function () { + utils.setAction('1. Click "Open Dialog".
      2. Click outside of the stop casting dialog to dismiss it.', function () { chrome.cast.requestSession(function (session) { assert.fail('We should not reach here on dismiss (make sure you cancelled the dialog for this test!)'); }, function (err) { @@ -250,7 +169,7 @@ called(stopped); } }); - setAction('1. Click "Open Dialog".
      2. Select "Stop Casting" in the stop casting dialog.' + utils.setAction('1. Click "Open Dialog".
      2. Select "Stop Casting" in the stop casting dialog.' + (isDesktop ? '
      3. Click outside of the stop casting dialog to dismiss it.' : ''), function () { chrome.cast.requestSession(function (session) { @@ -272,7 +191,6 @@ done(); }); }); - }); }); @@ -281,10 +199,9 @@ window['cordova-plugin-chromecast-tests'].runMocha = function () { var runner = mocha.run(); runner.on('suite end', function (suite) { - clearTimeout(beforeTimeout); var passed = this.stats.passes === runner.total; if (passed) { - setAction('All tests passed!'); + utils.setAction('All tests passed!'); document.getElementById('action').style.backgroundColor = '#ceffc4'; setNextTestNum(0); } diff --git a/tests/www/js/utils.js b/tests/www/js/utils.js index f0435c7..37d32a2 100644 --- a/tests/www/js/utils.js +++ b/tests/www/js/utils.js @@ -19,6 +19,81 @@ var utils = {}; + /** + * Displays the action information. + */ + utils.setAction = function (text, btnCallback, btnText) { + document.getElementById('action-text').innerHTML = text; + var button = document.getElementById('action-button'); + if (btnCallback) { + button.style.display = 'block'; + button.onclick = btnCallback; + } else { + button.style.display = 'none'; + } + button.innerHTML = btnText || 'Done'; + }; + + /** + * Clears the action information. + */ + utils.clearAction = function () { + utils.setAction('None.'); + }; + + /** + * Should successfully start a session on a non-nearby, non-castGroup device. + * If there is a problem with this function please ensure all the auto tests + * are passing. + */ + utils.startSession = function (callback) { + var scanState = 'running'; + var foundRoute = null; + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (scanState === 'stopped') { + assert.fail('Should not have gotten route update after scan was stopped'); + } + var route; + for (var i = 0; i < routes.length; i++) { + route = routes[i]; + assert.instanceOf(route, chrome.cast.cordova.Route); + assert.isString(route.id); + assert.isString(route.name); + assert.isBoolean(route.isNearbyDevice); + assert.isBoolean(route.isCastGroup); + if (!route.isNearbyDevice && !route.isCastGroup) { + foundRoute = route; + } + } + if (foundRoute && scanState === 'running') { + scanState = 'stopping'; + chrome.cast.cordova.stopRouteScan(function () { + scanState = 'stopped'; + utils.joinRoute(foundRoute.id, callback); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + } + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped.'); + }); + }; + /** + * Should successfully join a route. + * If there is a problem with this function please ensure all the auto tests + * are passing. + */ + utils.joinRoute = function (routeId, callback) { + chrome.cast.cordova.selectRoute(routeId, function (session) { + utils.testSessionProperties(session); + callback(session); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }; + /** * Allows you to check that a set of calls happen in a specific order. * @param {array} calls - array of expected callDetails to be receive in order From 1126487fff1c9a4f683ade0b1a26fbee8e96f872 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 20 Oct 2019 15:42:09 -0600 Subject: [PATCH 072/166] Added more manual Tests. Added all the major ones where multiple devices interact via the same session. Towards issue #36 --- src/android/ChromecastSession.java | 6 +- tests/www/chrome/cordova_stubs.js | 8 +- tests/www/chrome/tests_chrome.html | 23 +- ...tml => tests_manual_primary_1_chrome.html} | 8 +- .../chrome/tests_manual_primary_2_chrome.html | 50 +++ .../chrome/tests_manual_secondary_chrome.html | 49 +++ tests/www/html/tests.html | 21 +- ...anual.html => tests_manual_primary_1.html} | 8 +- tests/www/html/tests_manual_primary_2.html | 49 +++ tests/www/html/tests_manual_secondary.html | 48 +++ tests/www/js/tests_auto.js | 19 +- tests/www/js/tests_manual.js | 212 --------- tests/www/js/tests_manual_primary_1.js | 375 ++++++++++++++++ tests/www/js/tests_manual_primary_2.js | 118 +++++ tests/www/js/tests_manual_secondary.js | 407 ++++++++++++++++++ tests/www/js/utils.js | 23 +- 16 files changed, 1177 insertions(+), 247 deletions(-) rename tests/www/chrome/{tests_manual_chrome.html => tests_manual_primary_1_chrome.html} (89%) create mode 100644 tests/www/chrome/tests_manual_primary_2_chrome.html create mode 100644 tests/www/chrome/tests_manual_secondary_chrome.html rename tests/www/html/{tests_manual.html => tests_manual_primary_1.html} (88%) create mode 100644 tests/www/html/tests_manual_primary_2.html create mode 100644 tests/www/html/tests_manual_secondary.html delete mode 100644 tests/www/js/tests_manual.js create mode 100644 tests/www/js/tests_manual_primary_1.js create mode 100644 tests/www/js/tests_manual_primary_2.js create mode 100644 tests/www/js/tests_manual_secondary.js diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index 0bdfdf1..9c45215 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -83,6 +83,11 @@ public void run() { @Override public void onStatusUpdated() { MediaStatus status = client.getMediaStatus(); + if (status != null + && status.getPlayerState() != MediaStatus.PLAYER_STATE_IDLE + && status.getPlayerState() != MediaStatus.PLAYER_STATE_LOADING) { + lastMedia = client.getMediaInfo(); + } if (requestingMedia || queueStatusUpdatedCallback != null || queueReloadCallback != null) { @@ -102,7 +107,6 @@ public void onStatusUpdated() { } // Send update clientListener.onMediaUpdate(createMediaObject()); - lastMedia = client.getMediaInfo(); } @Override public void onQueueStatusUpdated() { diff --git a/tests/www/chrome/cordova_stubs.js b/tests/www/chrome/cordova_stubs.js index 7d65a8f..7f09260 100644 --- a/tests/www/chrome/cordova_stubs.js +++ b/tests/www/chrome/cordova_stubs.js @@ -82,16 +82,16 @@ console.error('Make sure to click the "Done Joining" button.'); }, 10000); - utils.setAction('1. Click "Request Session".', function () { + utils.setAction('1. Click "Request Session".', ' Request Session', function () { utils.setAction('2. Select a device in the chromecast dialog.'); chrome.cast.requestSession(function (session) { clearTimeout(timeout); - utils.setAction('3. Click "Done Joining" after the session has started.', function () { + utils.setAction('3. Click "Done Joining" after the session has started.', 'Done Joining', function () { utils.clearAction(); successCallback(session); - }, 'Done Joining'); + }); }, errorCallback); - }, 'Request Session'); + }); }; chrome.cast.cordova.Route = function (jsonRoute) { diff --git a/tests/www/chrome/tests_chrome.html b/tests/www/chrome/tests_chrome.html index fa5ae8b..13196fa 100644 --- a/tests/www/chrome/tests_chrome.html +++ b/tests/www/chrome/tests_chrome.html @@ -14,15 +14,32 @@

      cordova-plugin-chromecast Tests

      -
      + diff --git a/tests/www/chrome/tests_manual_chrome.html b/tests/www/chrome/tests_manual_primary_1_chrome.html similarity index 89% rename from tests/www/chrome/tests_manual_chrome.html rename to tests/www/chrome/tests_manual_primary_1_chrome.html index 8803a20..5ed894c 100644 --- a/tests/www/chrome/tests_manual_chrome.html +++ b/tests/www/chrome/tests_manual_primary_1_chrome.html @@ -15,15 +15,15 @@ -

      Manual Tests

      -

      +

      Manual Tests (Primary Device) Part 1

      +
      - + @@ -43,7 +43,7 @@

      Action required:

      window['cordova-plugin-chromecast-tests'] = window['cordova-plugin-chromecast-tests'] || {}; window['cordova-plugin-chromecast-tests'].isDesktop = true; - + diff --git a/tests/www/chrome/tests_manual_primary_2_chrome.html b/tests/www/chrome/tests_manual_primary_2_chrome.html new file mode 100644 index 0000000..9d61282 --- /dev/null +++ b/tests/www/chrome/tests_manual_primary_2_chrome.html @@ -0,0 +1,50 @@ + + + + Cordova tests + + + + + + + + + + + + + +

      Manual Tests (Primary Device) Part 2

      + +
      + +
      +

      Action required:

      +

      Starting Tests...

      + +
      + + + +
      + + + + + + diff --git a/tests/www/chrome/tests_manual_secondary_chrome.html b/tests/www/chrome/tests_manual_secondary_chrome.html new file mode 100644 index 0000000..5cdd8db --- /dev/null +++ b/tests/www/chrome/tests_manual_secondary_chrome.html @@ -0,0 +1,49 @@ + + + + Cordova tests + + + + + + + + + + + + + +

      Manual Tests (Secondary Device)

      + + +
      +

      Action required:

      +

      Preparing Secondary App...

      + +
      + + + +
      + + + + + + diff --git a/tests/www/html/tests.html b/tests/www/html/tests.html index 4de0339..0c6d1fd 100644 --- a/tests/www/html/tests.html +++ b/tests/www/html/tests.html @@ -13,14 +13,31 @@

      cordova-plugin-chromecast Tests

      diff --git a/tests/www/html/tests_manual.html b/tests/www/html/tests_manual_primary_1.html similarity index 88% rename from tests/www/html/tests_manual.html rename to tests/www/html/tests_manual_primary_1.html index 2bd5ba7..87d39f3 100644 --- a/tests/www/html/tests_manual.html +++ b/tests/www/html/tests_manual_primary_1.html @@ -16,15 +16,15 @@ -

      Manual Tests

      -

      +

      Manual Tests (Primary Device) Part 1

      +
      - + + + + + + + +

      Manual Tests (Primary Device) Part 2

      + +
      + +
      +

      Action required:

      +

      Starting Tests...

      + +
      + +
      + + + + + diff --git a/tests/www/html/tests_manual_secondary.html b/tests/www/html/tests_manual_secondary.html new file mode 100644 index 0000000..ee8ed79 --- /dev/null +++ b/tests/www/html/tests_manual_secondary.html @@ -0,0 +1,48 @@ + + + + Cordova tests + + + + + + + + + + + + + + +

      Manual Tests (Secondary Device)

      + + +
      +

      Action required:

      +

      Preparing Secondary App...

      + +
      + +
      + + + + + diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index e2ca4b6..b923c3e 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -1009,14 +1009,6 @@ var videoItem; var audioItem; var startTime = 40; - function getCurrentItemIndex (media) { - for (var i = 0; i < media.items.length; i++) { - if (media.items[i].itemId === media.currentItemId) { - return i; - } - } - return 'Could get current item index for itemId: ' + media.currentItemId; - } function checkItems (items) { assert.isTrue(items[0].autoplay); assert.equal(items[0].startTime, startTime); @@ -1078,7 +1070,7 @@ session.queueLoad(request, function (m) { media = m; - var i = getCurrentItemIndex(media); + var i = utils.getCurrentItemIndex(media); utils.testMediaProperties(media); assert.equal(media.currentItemId, media.items[i].itemId); assert.equal(media.repeatMode, chrome.cast.media.RepeatMode.ALL); @@ -1128,7 +1120,7 @@ var request = new chrome.cast.media.SeekRequest(); request.currentTime = media.media.duration - 1; - var i = getCurrentItemIndex(media); + var i = utils.getCurrentItemIndex(media); // Listen for current media end media.addUpdateListener(function listener (isAlive) { if (media.playerState === chrome.cast.media.PlayerState.IDLE) { @@ -1137,7 +1129,7 @@ called(stopped); } if (media.currentItemId !== media.items[i].itemId) { - i = getCurrentItemIndex(media); + i = utils.getCurrentItemIndex(media); media.removeUpdateListener(listener); utils.testMediaProperties(media); assert.equal(media.repeatMode, chrome.cast.media.RepeatMode.ALL); @@ -1156,7 +1148,6 @@ assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.TV_SHOW); assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.TV_SHOW); called(newMedia); - window.m = media; if (media.getEstimatedTime() > startTime - 5 && media.getEstimatedTime() < startTime + 5) { called(update); @@ -1202,7 +1193,7 @@ ], function () { calledAnyOrder(update); }); - var i = getCurrentItemIndex(media); + var i = utils.getCurrentItemIndex(media); media.addUpdateListener(function listener (isAlive) { if (media.playerState === chrome.cast.media.PlayerState.IDLE) { assert.equal(media.idleReason, chrome.cast.media.IdleReason.INTERRUPTED); @@ -1210,7 +1201,7 @@ calledOrder(stopped); } if (media.currentItemId !== media.items[i].itemId) { - i = getCurrentItemIndex(media); + i = utils.getCurrentItemIndex(media); media.removeUpdateListener(listener); utils.testMediaProperties(media); assert.equal(media.currentItemId, media.items[i].itemId); diff --git a/tests/www/js/tests_manual.js b/tests/www/js/tests_manual.js deleted file mode 100644 index a218139..0000000 --- a/tests/www/js/tests_manual.js +++ /dev/null @@ -1,212 +0,0 @@ -/** - * The order of these tests and this.bail(true) is very important. - * - * Rather than nesting deep with describes and before's we just ensure the - * tests occur in the correct order. - * The major advantage to this is not having to repeat test code frequently - * making the suite slow. - * - */ - -(function () { - 'use strict'; - /* eslint-env mocha */ - /* global chrome */ - - var assert = window.chai.assert; - var utils = window['cordova-plugin-chromecast-tests'].utils; - var isDesktop = window['cordova-plugin-chromecast-tests'].isDesktop || false; - - mocha.setup({ - bail: true, - ui: 'bdd', - useColors: true, - reporter: window['cordova-plugin-chromecast-tests'].customHtmlReporter, - slow: 10000, - timeout: 0 - }); - - describe('cordova-plugin-chromecast', function () { - // callOrder constants that are re-used frequently - var success = 'success'; - var stopped = 'stopped'; - var update = 'update'; - - var session; - var sessionListener = function (sess) { }; - - before('Api should be available and initialize successfully', function (done) { - session = null; - var interval = setInterval(function () { - if (chrome && chrome.cast && chrome.cast.isAvailable) { - clearInterval(interval); - initializeApi(); - } - }, 100); - function initializeApi () { - var finished = false; // Need this so we stop testing after being finished - sessionListener = function (sess) { - if (!finished) { - assert.fail('got session before "success", "unavailable", "available" sequence completed'); - } - utils.testSessionProperties(sess); - session = sess; - }; - var unavailable = 'unavailable'; - var available = 'available'; - var called = utils.callOrder([ - { id: success, repeats: false }, - { id: unavailable, repeats: true }, - { id: available, repeats: true } - ], function () { - finished = true; - done(); - }); - var apiConfig = new chrome.cast.ApiConfig( - new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), - sessionListener, - function receiverListener (availability) { - if (!finished) { - called(availability); - } - }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); - chrome.cast.initialize(apiConfig, function () { - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - } - }); - - describe('App restart', function () { - it('Restart app with active session, should receive session on initialize', function (done) { - var interval; - var time = 15000; - interval = setInterval(function () { - time -= 500; - if (session) { - clearInterval(interval); - done(); - } - if (time < 0) { - clearInterval(interval); - assert.fail('Failed to find session for 15s after app restart. ' - + 'Make sure that a session started by this device is active during app restart.'); - } - }, 500); - utils.setAction('Situation #1 - First time you have reached the "restart app" test:
      ' - + '    1. Click "Start Session"

      ' - + 'Situation #2 - You have force killed and restarted the app:
      ' - + '    1. Wait for session discovery. (Fails if none found after 15s)
      ', function () { - clearInterval(interval); - utils.startSession(function (sess) { - session = sess; - if (isDesktop) { - utils.setAction('1. Open a new tab and visit this url.'); - } else { - utils.setAction('1. Force kill and restart the app.'); - } - }); - }, 'Start Session'); - }); - after('Ensure session is stopped', function (done) { - session.stop(function () { - session = null; - done(); - }, function () { - done(); - }); - }); - }); - - describe('State: No session automatically discovered', function () { - before(function () { - assert.notExists(session); - }); - it('chrome.cast.requestSession cancel should return error', function (done) { - utils.setAction('1. Click "Open Dialog".
      2. Click outside of the chromecast chooser dialog to dismiss it.', function () { - chrome.cast.requestSession(function (sess) { - session = sess; - assert.fail('We should not reach here on dismiss (make sure you cancelled the dialog for this test!)'); - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); - done(); - }); - }, 'Open Dialog'); - }); - it('chrome.cast.requestSession success should return a session', function (done) { - utils.setAction('1. Click "Open Dialog".
      2. Select a device in the chromecast chooser dialog.', function () { - chrome.cast.requestSession(function (sess) { - session = sess; - utils.testSessionProperties(session); - done(); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }, 'Open Dialog'); - }); - it('chrome.cast.requestSession (stop casting) cancel should return error', function (done) { - utils.setAction('1. Click "Open Dialog".
      2. Click outside of the stop casting dialog to dismiss it.', function () { - chrome.cast.requestSession(function (session) { - assert.fail('We should not reach here on dismiss (make sure you cancelled the dialog for this test!)'); - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); - done(); - }); - }, 'Open Dialog'); - }); - it('chrome.cast.requestSession (stop casting) clicking "Stop Casting" should stop the session', function (done) { - var called = utils.callOrder([ - { id: stopped, repeats: false }, - { id: success, repeats: false } - ], done); - session.addUpdateListener(function listener (isAlive) { - if (session.status === chrome.cast.SessionStatus.STOPPED) { - session.removeUpdateListener(listener); - assert.isFalse(isAlive); - called(stopped); - } - }); - utils.setAction('1. Click "Open Dialog".
      2. Select "Stop Casting" in the stop casting dialog.' - + (isDesktop ? '
      3. Click outside of the stop casting dialog to dismiss it.' : ''), - function () { - chrome.cast.requestSession(function (session) { - assert.fail('We should not reach here on stop casting'); - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); - called(success); - }); - }, 'Open Dialog'); - }); - after('Ensure session is stopped', function (done) { - if (!session) { - return done(); - } - session.stop(function () { - done(); - }, function () { - done(); - }); - }); - }); - - }); - - window['cordova-plugin-chromecast-tests'] = window['cordova-plugin-chromecast-tests'] || {}; - window['cordova-plugin-chromecast-tests'].runMocha = function () { - var runner = mocha.run(); - runner.on('suite end', function (suite) { - var passed = this.stats.passes === runner.total; - if (passed) { - utils.setAction('All tests passed!'); - document.getElementById('action').style.backgroundColor = '#ceffc4'; - setNextTestNum(0); - } - }); - return runner; - }; - -}()); diff --git a/tests/www/js/tests_manual_primary_1.js b/tests/www/js/tests_manual_primary_1.js new file mode 100644 index 0000000..1889e41 --- /dev/null +++ b/tests/www/js/tests_manual_primary_1.js @@ -0,0 +1,375 @@ +/** + * The order of these tests and this.bail(true) is very important. + * + * Rather than nesting deep with describes and before's we just ensure the + * tests occur in the correct order. + * The major advantage to this is not having to repeat test code frequently + * making the suite slow. + * + */ + +(function () { + 'use strict'; + /* eslint-env mocha */ + /* global chrome */ + + var assert = window.chai.assert; + var utils = window['cordova-plugin-chromecast-tests'].utils; + var isDesktop = window['cordova-plugin-chromecast-tests'].isDesktop || false; + + mocha.setup({ + bail: true, + ui: 'bdd', + useColors: true, + reporter: window['cordova-plugin-chromecast-tests'].customHtmlReporter, + slow: 10000, + timeout: 180000 + }); + + describe('Manual Tests - Primary Device - Part 1', function () { + var videoUrl = 'https://ia801302.us.archive.org/1/items/TheWater_201510/TheWater.mp4'; + var audioUrl = 'https://ia600304.us.archive.org/20/items/OTRR_Gunsmoke_Singles/Gunsmoke_52-10-03_024_Cain.mp3'; + + // callOrder constants that are re-used frequently + var success = 'success'; + var stopped = 'stopped'; + var update = 'update'; + + var session; + var media; + + before('Api should be available and initialize successfully', function (done) { + session = null; + var interval = setInterval(function () { + if (chrome && chrome.cast && chrome.cast.isAvailable) { + clearInterval(interval); + initializeApi(); + } + }, 100); + function initializeApi () { + var finished = false; // Need this so we stop testing after being finished + var unavailable = 'unavailable'; + var available = 'available'; + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: unavailable, repeats: true }, + { id: available, repeats: true } + ], function () { + finished = true; + done(); + }); + var apiConfig = new chrome.cast.ApiConfig( + new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), + function (sess) { + if (!finished) { + assert.fail('got session before "success", "unavailable", "available" sequence completed'); + } + session = sess; + utils.testSessionProperties(sess); + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); + chrome.cast.initialize(apiConfig, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + } + }); + + // Must be the first test + describe('App restart', function () { + it('Restart app with active session, should receive session on initialize', function (done) { + var interval; + var time = 15000; + interval = setInterval(function () { + time -= 500; + if (session) { + clearInterval(interval); + done(); + } + if (time < 0) { + clearInterval(interval); + assert.fail('Failed to find session for 15s after app restart. ' + + 'Make sure that a session started by this device is active during app restart.'); + } + }, 500); + utils.setAction('Situation #1 - First time you have reached the "restart app" test:
      ' + + '    1. Click "Start Session"

      ' + + 'Situation #2 - You have force killed and restarted the app:
      ' + + '    1. Wait for session discovery. (Fails if none found after 15s)
      ', + 'Start Session', + function () { + clearInterval(interval); + utils.startSession(function (sess) { + session = sess; + if (isDesktop) { + utils.setAction('1. Refresh this page.'); + } else { + utils.setAction('1. Force kill and restart the app.' + + '
      *Android 4.4 does not support this feature, so just refresh the page.'); + } + }); + } + ); + }); + after('Ensure session is stopped', function (done) { + if (!session) { + return done(); + } + session.stop(function () { + done(); + }, function () { + done(); + }); + }); + }); + + describe('chrome.cast.requestSession', function () { + it('dismiss should return error', function (done) { + utils.setAction('1. Click "Open Dialog".
      2. Click outside of the chromecast chooser dialog to dismiss it.', 'Open Dialog', function () { + chrome.cast.requestSession(function (sess) { + session = sess; + assert.fail('We should not reach here on dismiss (make sure you cancelled the dialog for this test!)'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + done(); + }); + }); + }); + it('success should return a session', function (done) { + utils.setAction('1. Click "Open Dialog".
      2. Select a device in the chromecast chooser dialog.', 'Open Dialog', function () { + chrome.cast.requestSession(function (sess) { + session = sess; + utils.testSessionProperties(session); + done(); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + }); + it('(stop casting) cancel should return error', function (done) { + utils.setAction('1. Click "Open Dialog".
      2. Click outside of the stop casting dialog to dismiss it.', 'Open Dialog', function () { + chrome.cast.requestSession(function (session) { + assert.fail('We should not reach here on dismiss (make sure you cancelled the dialog for this test!)'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + done(); + }); + }); + }); + it('(stop casting) clicking "Stop Casting" should stop the session', function (done) { + var called = utils.callOrder([ + { id: stopped, repeats: false }, + { id: success, repeats: false } + ], done); + session.addUpdateListener(function listener (isAlive) { + if (session.status === chrome.cast.SessionStatus.STOPPED) { + session.removeUpdateListener(listener); + assert.isFalse(isAlive); + called(stopped); + } + }); + utils.setAction('1. Click "Open Dialog".
      2. Select "Stop Casting" in the stop casting dialog.' + + (isDesktop ? '
      3. Click outside of the stop casting dialog to dismiss it.' : ''), + 'Open Dialog', + function () { + chrome.cast.requestSession(function (session) { + assert.fail('We should not reach here on stop casting'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + called(success); + }); + } + ); + }); + after('Ensure session is stopped', function (done) { + if (!session) { + return done(); + } + session.stop(function () { + done(); + }, function () { + done(); + }); + }); + }); + + describe('External Sender Sends Commands', function () { + before(function () { + assert.equal(session.status, chrome.cast.SessionStatus.STOPPED); + }); + it('Join external session', function (done) { + if (isDesktop) { + // This is a hack because desktop chrome is incapable of + // joining a session. So we have to create the session + // from chrome first and then join from the app. + return utils.startSession(function (sess) { + session = sess; + showInstructions(done); + }); + } + // Else + showInstructions(function () { + utils.startSession(function (sess) { + session = sess; + utils.testSessionProperties(session); + done(); + }); + }); + function showInstructions (callback) { + utils.setAction('Ensure you have only 1 physical chromecast device on your network (castGroups are fine).
      ' + + '
      1. On a secondary device (or desktop chrome browser),' + + ' navigate to Manual Tests (Secondary)
      ' + + '2. Follow instructions on secondary app.', + 'Continue', + function () { + callback(); + }); + } + + }); + it('External loadMedia should trigger mediaListener', function (done) { + utils.setAction('On secondary click "Load Media"'); + var finished = false; + session.addMediaListener(function listener (m) { + if (finished) { + return; + } + utils.setAction('Tests running...'); + media = m; + var interval = setInterval(function () { + if (media.media.tracks != null && media.media.tracks !== undefined) { + clearInterval(interval); + utils.testMediaProperties(media); + finished = true; + done(); + } + }, 400); + }); + }); + it('External media stop should trigger media updateListener', function (done) { + utils.setAction('On secondary click "Stop Media"'); + media.addUpdateListener(function listener (isAlive) { + if (media.playerState === chrome.cast.media.PlayerState.IDLE) { + media.removeUpdateListener(listener); + assert.equal(media.idleReason, chrome.cast.media.IdleReason.CANCELLED); + assert.isFalse(isAlive); + done(); + } + }); + }); + it('External queueLoad should trigger mediaListener', function (done) { + utils.setAction('On secondary click "Load Queue"'); + var finished = false; + session.addMediaListener(function listener (m) { + if (finished) { + return; + } + finished = true; + media = m; + var interval = setInterval(function () { + if (media.currentItemId > -1 && media.media.tracks) { + clearInterval(interval); + finished = true; + utils.testMediaProperties(media); + var items = media.items; + var startTime = 40; + assert.isTrue(items[0].autoplay); + assert.equal(items[0].startTime, startTime); + assert.equal(items[0].media.contentId, videoUrl); + assert.isTrue(items[1].autoplay); + assert.equal(items[1].startTime, startTime * 2); + assert.equal(items[1].media.contentId, audioUrl); + done(); + } + }, 400); + }); + }); + it('Jump to different queue item should trigger media.addUpdateListener and not session.addMediaListener', function (done) { + utils.setAction('On secondary click "Queue Jump"'); + var called = utils.callOrder([ + { id: stopped, repeats: true }, + { id: update, repeats: true } + ], done); + var currentItemId = media.currentItemId; + var mediaListener = function (media) { + assert.fail('session.addMediaListener should only be called when an external sender loads media. ' + + '(We are the one loading. We are not external to ourself.'); + }; + session.addMediaListener(mediaListener); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + if (media.playerState === chrome.cast.media.PlayerState.IDLE) { + assert.oneOf(media.idleReason, + [chrome.cast.media.IdleReason.INTERRUPTED, chrome.cast.media.IdleReason.FINISHED]); + called(stopped); + } + if (media.currentItemId !== currentItemId) { + session.removeMediaListener(mediaListener); + media.removeUpdateListener(listener); + utils.testMediaProperties(media); + called(update); + } + }); + }); + it('session.leave should leave the session', function (done) { + utils.setAction('Follow instructions on secondary.', 'Leave Session', function () { + // Set up the expected calls + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], function () { + done(); + }); + var finished = false; + session.addUpdateListener(function listener (isAlive) { + if (finished) { + return; + } + assert.isTrue(isAlive); + if (session.status === chrome.cast.SessionStatus.DISCONNECTED) { + finished = true; + called(update); + } + }); + session.leave(function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + }); + after('Ensure we have left the session', function (done) { + if (!session) { + return done(); + } + session.leave(function () { + done(); + }, function () { + done(); + }); + }); + }); + + }); + + window['cordova-plugin-chromecast-tests'] = window['cordova-plugin-chromecast-tests'] || {}; + window['cordova-plugin-chromecast-tests'].runMocha = function () { + var runner = mocha.run(); + runner.on('suite end', function (suite) { + var passed = this.stats.passes === runner.total; + if (passed) { + utils.setAction('1. On secondary, click "Check Session"
      Then follow directions on secondary!'); + document.getElementById('action').style.backgroundColor = '#ceffc4'; + } + }); + return runner; + }; + +}()); diff --git a/tests/www/js/tests_manual_primary_2.js b/tests/www/js/tests_manual_primary_2.js new file mode 100644 index 0000000..9dddac2 --- /dev/null +++ b/tests/www/js/tests_manual_primary_2.js @@ -0,0 +1,118 @@ +/** + * The order of these tests and this.bail(true) is very important. + * + * Rather than nesting deep with describes and before's we just ensure the + * tests occur in the correct order. + * The major advantage to this is not having to repeat test code frequently + * making the suite slow. + * + */ + +(function () { + 'use strict'; + /* eslint-env mocha */ + /* global chrome */ + + var assert = window.chai.assert; + var utils = window['cordova-plugin-chromecast-tests'].utils; + var isDesktop = window['cordova-plugin-chromecast-tests'].isDesktop || false; + + mocha.setup({ + bail: true, + ui: 'bdd', + useColors: true, + reporter: window['cordova-plugin-chromecast-tests'].customHtmlReporter, + slow: 10000, + timeout: 180000 + }); + + describe('Manual Tests - Primary Device - Part 2', function () { + // callOrder constants that are re-used frequently + var success = 'success'; + var session; + + before('Api should be available and initialize successfully', function (done) { + session = null; + var interval = setInterval(function () { + if (chrome && chrome.cast && chrome.cast.isAvailable) { + clearInterval(interval); + done(); + } + }, 100); + }); + it('Should not receive a session on initialize', function (done) { + var finished = false; // Need this so we stop testing after being finished + var unavailable = 'unavailable'; + var available = 'available'; + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: unavailable, repeats: true }, + { id: available, repeats: true } + ], function () { + finished = true; + done(); + }); + var apiConfig = new chrome.cast.ApiConfig( + new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), + function (sess) { + session = sess; + if (!isDesktop) { + assert.fail('should not receive a session (make sure there is no active cast session when starting the tests)'); + } + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); + chrome.cast.initialize(apiConfig, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + it('Create session', function (done) { + utils.setAction('On secondary click "Start Part 2".', 'Enter Session', function () { + utils.startSession(function (sess) { + session = sess; + utils.testSessionProperties(session); + utils.setAction('On secondary click "Continue".'); + done(); + }); + }); + }); + it('External session.stop should kill this session as well', function (done) { + session.addUpdateListener(function listener (isAlive) { + if (session.status === chrome.cast.SessionStatus.STOPPED) { + assert.isFalse(isAlive); + session.removeUpdateListener(listener); + done(); + } + }); + }); + after('Ensure we have stopped the session', function (done) { + if (!session) { + return done(); + } + session.stop(function () { + done(); + }, function () { + done(); + }); + }); + + }); + + window['cordova-plugin-chromecast-tests'] = window['cordova-plugin-chromecast-tests'] || {}; + window['cordova-plugin-chromecast-tests'].runMocha = function () { + var runner = mocha.run(); + runner.on('suite end', function (suite) { + var passed = this.stats.passes === runner.total; + if (passed) { + utils.setAction('All manual tests passed! (Assuming you did Part 1 as well)'); + document.getElementById('action').style.backgroundColor = '#ceffc4'; + } + }); + return runner; + }; + +}()); diff --git a/tests/www/js/tests_manual_secondary.js b/tests/www/js/tests_manual_secondary.js new file mode 100644 index 0000000..ba7cbb0 --- /dev/null +++ b/tests/www/js/tests_manual_secondary.js @@ -0,0 +1,407 @@ +/** + * The order of these tests and this.bail(true) is very important. + * + * Rather than nesting deep with describes and before's we just ensure the + * tests occur in the correct order. + * The major advantage to this is not having to repeat test code frequently + * making the suite slow. + * + */ + +(function () { + 'use strict'; + /* eslint-env mocha */ + /* global chrome */ + + var assert = window.chai.assert; + var utils = window['cordova-plugin-chromecast-tests'].utils; + var isDesktop = window['cordova-plugin-chromecast-tests'].isDesktop || false; + + mocha.setup({ + bail: true, + ui: 'bdd', + useColors: true, + reporter: window['cordova-plugin-chromecast-tests'].customHtmlReporter, + slow: 10000, + timeout: 180000 + }); + + describe('Manual Tests - Secondary Device', function () { + var imageUrl = 'https://ia800705.us.archive.org/1/items/GoodHousekeeping193810/Good%20Housekeeping%201938-10.jpg'; + var videoUrl = 'https://ia801302.us.archive.org/1/items/TheWater_201510/TheWater.mp4'; + var audioUrl = 'https://ia600304.us.archive.org/20/items/OTRR_Gunsmoke_Singles/Gunsmoke_52-10-03_024_Cain.mp3'; + + // callOrder constants that are re-used frequently + var success = 'success'; + var stopped = 'stopped'; + var update = 'update'; + var newMedia = 'newMedia'; + + var session; + var media; + + var startTime = 40; + var videoItem; + var audioItem; + + var checkItems = function (items) { + assert.isTrue(items[0].autoplay); + assert.equal(items[0].startTime, startTime); + assert.equal(items[0].media.contentId, videoUrl); + assert.isTrue(items[1].autoplay); + assert.equal(items[1].startTime, startTime * 2); + assert.equal(items[1].media.contentId, audioUrl); + }; + + before('setup constants', function () { + videoItem = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + videoItem.metadata = new chrome.cast.media.TvShowMediaMetadata(); + videoItem.metadata.title = 'DaTitle'; + videoItem.metadata.subtitle = 'DaSubtitle'; + videoItem.metadata.originalAirDate = new Date().valueOf(); + videoItem.metadata.episode = 15; + videoItem.metadata.season = 2; + videoItem.metadata.seriesTitle = 'DaSeries'; + videoItem.metadata.images = [new chrome.cast.Image(imageUrl)]; + + audioItem = new chrome.cast.media.MediaInfo(audioUrl, 'audio/mpeg'); + audioItem.metadata = new chrome.cast.media.MusicTrackMediaMetadata(); + audioItem.metadata.albumArtist = 'DaAlmbumArtist'; + audioItem.metadata.albumName = 'DaAlbum'; + audioItem.metadata.artist = 'DaArtist'; + audioItem.metadata.composer = 'DaComposer'; + audioItem.metadata.title = 'DaTitle'; + audioItem.metadata.songName = 'DaSongName'; + audioItem.metadata.myMadeUpMetadata = '15'; + audioItem.metadata.releaseDate = new Date().valueOf(); + audioItem.metadata.images = [new chrome.cast.Image(imageUrl)]; + }); + + before('Api should be available and initialize successfully', function (done) { + session = null; + var interval = setInterval(function () { + if (chrome && chrome.cast && chrome.cast.isAvailable) { + clearInterval(interval); + initializeApi(); + } + }, 100); + function initializeApi () { + var finished = false; // Need this so we stop testing after being finished + var unavailable = 'unavailable'; + var available = 'available'; + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: unavailable, repeats: true }, + { id: available, repeats: true } + ], function () { + finished = true; + done(); + }); + var apiConfig = new chrome.cast.ApiConfig( + new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), + function (sess) { + assert.fail('should not receive a session (make sure there is no active cast session when starting the tests)'); + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); + chrome.cast.initialize(apiConfig, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + } + }); + it('Create session', function (done) { + assert.notExists(session); + utils.startSession(function (sess) { + session = sess; + session.addMediaListener(function (media) { + assert.fail('session.addMediaListener should only be called when an external sender loads media. ' + + '(We are the one loading. We are not external to ourself.'); + }); + done(); + }); + }); + it('session.loadMedia should be able to load a remote video and handle GenericMediaMetadata', function (done) { + utils.setAction('On primary click "Continue"', 'Load Media', function () { + var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + mediaInfo.metadata = new chrome.cast.media.GenericMediaMetadata(); + mediaInfo.metadata.title = 'DaTitle'; + mediaInfo.metadata.subtitle = 'DaSubtitle'; + mediaInfo.metadata.releaseDate = new Date().valueOf(); + mediaInfo.metadata.someTrueBoolean = true; + mediaInfo.metadata.someFalseBoolean = false; + mediaInfo.metadata.someSmallNumber = 15; + mediaInfo.metadata.someLargeNumber = 1234567890123456; + mediaInfo.metadata.someSmallDecimal = 15.15; + mediaInfo.metadata.someLargeDecimal = 1234567.123456789; + mediaInfo.metadata.someString = 'SomeString'; + mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; + session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { + media = m; + utils.testMediaProperties(media); + assert.isUndefined(media.queueData); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); + assert.equal(media.media.metadata.releaseDate, mediaInfo.metadata.releaseDate); + // TODO figure out how to maintain the data types for custom params on the native side + // so that we don't have to do turn each actual and expected into a string + assert.equal(media.media.metadata.someTrueBoolean + '', mediaInfo.metadata.someTrueBoolean + ''); + assert.equal(media.media.metadata.someFalseBoolean + '', mediaInfo.metadata.someFalseBoolean + ''); + assert.equal(media.media.metadata.someSmallNumber + '', mediaInfo.metadata.someSmallNumber + ''); + assert.equal(media.media.metadata.someLargeNumber + '', mediaInfo.metadata.someLargeNumber + ''); + assert.equal(media.media.metadata.someSmallDecimal + '', mediaInfo.metadata.someSmallDecimal + ''); + assert.equal(media.media.metadata.someLargeDecimal + '', mediaInfo.metadata.someLargeDecimal + ''); + assert.equal(media.media.metadata.someString, mediaInfo.metadata.someString); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.GENERIC); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.GENERIC); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + utils.testMediaProperties(media); + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + done(); + } + }); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + }); + it('media.stop should end video playback', function (done) { + utils.setAction('', 'Stop Media', function () { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], function () { + done(); + }); + media.addUpdateListener(function listener (isAlive) { + if (media.playerState === chrome.cast.media.PlayerState.IDLE) { + media.removeUpdateListener(listener); + assert.equal(media.idleReason, chrome.cast.media.IdleReason.CANCELLED); + assert.isFalse(isAlive); + called(update); + } + }); + media.stop(null, function () { + assert.equal(media.playerState, chrome.cast.media.PlayerState.IDLE); + assert.equal(media.idleReason, chrome.cast.media.IdleReason.CANCELLED); + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + }); + it('session.queueLoad should be able to load remote audio/video queue and return the correct Metadata', function (done) { + utils.setAction('', 'Load Queue', function () { + var item; + var queue = []; + + // Add items to the queue + item = new chrome.cast.media.QueueItem(videoItem); + item.startTime = startTime; + queue.push(item); + item = new chrome.cast.media.QueueItem(audioItem); + item.startTime = startTime * 2; + queue.push(item); + + // Create request to repeat all and start at 2nd item + var request = new chrome.cast.media.QueueLoadRequest(queue); + request.repeatMode = chrome.cast.media.RepeatMode.ALL; + request.startIndex = 1; + + session.queueLoad(request, function (m) { + media = m; + var i = utils.getCurrentItemIndex(media); + utils.testMediaProperties(media); + assert.equal(media.currentItemId, media.items[i].itemId); + assert.equal(media.repeatMode, chrome.cast.media.RepeatMode.ALL); + assert.isObject(media.queueData); + assert.equal(media.queueData.repeatMode, request.repeatMode); + assert.isFalse(media.queueData.shuffle); + assert.equal(media.queueData.startIndex, request.startIndex); + utils.testQueueItems(media.items); + assert.equal(media.media.contentId, audioUrl); + assert.equal(media.items.length, 2); + checkItems(media.items); + assert.equal(media.items[i].media.metadata.albumArtist, audioItem.metadata.albumArtist); + assert.equal(media.items[i].media.metadata.albumName, audioItem.metadata.albumName); + assert.equal(media.items[i].media.metadata.artist, audioItem.metadata.artist); + assert.equal(media.items[i].media.metadata.composer, audioItem.metadata.composer); + assert.equal(media.items[i].media.metadata.title, audioItem.metadata.title); + assert.equal(media.items[i].media.metadata.songName, audioItem.metadata.songName); + assert.equal(media.items[i].media.metadata.releaseDate, audioItem.metadata.releaseDate); + assert.equal(media.items[i].media.metadata.images[0].url, audioItem.metadata.images[0].url); + assert.equal(media.items[i].media.metadata.myMadeUpMetadata, audioItem.metadata.myMadeUpMetadata); + assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.MUSIC_TRACK); + assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.MUSIC_TRACK); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + utils.testMediaProperties(media); + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + assert.closeTo(media.getEstimatedTime(), startTime * 2, 5); + done(); + } + }); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + }); + it('media.queueJumpToItem should jump to selected item', function (done) { + utils.setAction('', 'Queue Jump', function () { + var calledAnyOrder = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], function () { + done(); + }); + var calledOrder = utils.callOrder([ + { id: stopped, repeats: true }, + { id: newMedia, repeats: true } + ], function () { + calledAnyOrder(update); + }); + var i = utils.getCurrentItemIndex(media); + media.addUpdateListener(function listener (isAlive) { + if (media.playerState === chrome.cast.media.PlayerState.IDLE) { + assert.oneOf(media.idleReason, + [chrome.cast.media.IdleReason.INTERRUPTED, chrome.cast.media.IdleReason.FINISHED]); + assert.isTrue(isAlive); + calledOrder(stopped); + } + if (media.currentItemId !== media.items[i].itemId && media.media.contentId === videoUrl) { + i = utils.getCurrentItemIndex(media); + media.removeUpdateListener(listener); + utils.testMediaProperties(media); + assert.equal(media.currentItemId, media.items[i].itemId); + utils.testQueueItems(media.items); + assert.equal(media.media.contentId, videoUrl); + assert.equal(media.items.length, 2); + checkItems(media.items); + assert.equal(media.items[i].media.contentId, videoUrl); + assert.equal(media.items[i].media.metadata.title, videoItem.metadata.title); + assert.equal(media.items[i].media.metadata.subtitle, videoItem.metadata.subtitle); + assert.equal(media.items[i].media.metadata.originalAirDate, videoItem.metadata.originalAirDate); + assert.equal(media.items[i].media.metadata.episode, videoItem.metadata.episode); + assert.equal(media.items[i].media.metadata.season, videoItem.metadata.season); + assert.equal(media.items[i].media.metadata.seriesTitle, videoItem.metadata.seriesTitle); + assert.equal(media.items[i].media.metadata.images[0].url, videoItem.metadata.images[0].url); + assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.TV_SHOW); + assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.TV_SHOW); + assert.closeTo(media.getEstimatedTime(), startTime, 5); + calledOrder(newMedia); + } + }); + // Jump + var jumpIndex = (i + 1) % media.items.length; + media.queueJumpToItem(media.items[jumpIndex].itemId, function () { + calledAnyOrder(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + }); + it('Primary session.leave', function (done) { + utils.setAction('On primary, click "Leave Session"', 'Check Session', function () { + assert.equal(session.status, chrome.cast.SessionStatus.CONNECTED); + done(); + }); + }); + it('Primary should not receive session on initialize', function (done) { + this.timeout(240000); + utils.setAction('On primary:
      1. Force kill and restart the app.' + + '
      2. Select Manual Tests (Primary) Part 2 from the home page to finish the manual tests.', 'Start Part 2', done); + }); + it('Secondary session.leave should cause session to end (because all senders have left)', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + session.addUpdateListener(function listener (isAlive) { + if (session.status === chrome.cast.SessionStatus.DISCONNECTED) { + assert.isTrue(isAlive); + session.removeUpdateListener(listener); + called(update); + } + }); + session.leave(function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + it('Join session', function (done) { + if (isDesktop) { + // This is a hack because desktop chrome is incapable of + // joining a session. So we have to create the session + // from chrome first and then join from the app. + utils.startSession(function (sess) { + session = sess; + utils.setAction('On primary click "Enter Session', 'Continue', done); + }); + return; + } + utils.setAction('On primary click "Enter Session', 'Continue', function () { + utils.startSession(function (sess) { + session = sess; + done(); + }); + }); + }); + it('session.stop', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + session.addUpdateListener(function listener (isAlive) { + if (session.status === chrome.cast.SessionStatus.STOPPED) { + assert.isFalse(isAlive); + session.removeUpdateListener(listener); + called(update); + } + }); + session.stop(function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + after('Ensure session is stopped', function (done) { + if (!session) { + return done(); + } + session.stop(function () { + done(); + }, function () { + done(); + }); + }); + + }); + + window['cordova-plugin-chromecast-tests'] = window['cordova-plugin-chromecast-tests'] || {}; + window['cordova-plugin-chromecast-tests'].runMocha = function () { + var runner = mocha.run(); + runner.on('suite end', function (suite) { + var passed = this.stats.passes === runner.total; + if (passed) { + utils.setAction('All Manual Tests (Secondary) passed!'); + document.getElementById('action').style.backgroundColor = '#ceffc4'; + } + }); + return runner; + }; + +}()); diff --git a/tests/www/js/utils.js b/tests/www/js/utils.js index 37d32a2..f9d0f17 100644 --- a/tests/www/js/utils.js +++ b/tests/www/js/utils.js @@ -22,12 +22,17 @@ /** * Displays the action information. */ - utils.setAction = function (text, btnCallback, btnText) { - document.getElementById('action-text').innerHTML = text; + utils.setAction = function (text, btnText, btnCallback) { + if (text || text === '') { + document.getElementById('action-text').innerHTML = text; + } var button = document.getElementById('action-button'); if (btnCallback) { button.style.display = 'block'; - button.onclick = btnCallback; + button.onclick = function () { + button.style.display = 'none'; + btnCallback(); + }; } else { button.style.display = 'none'; } @@ -94,6 +99,18 @@ }); }; + /** + * Returns the current queue item's index in the items array. + */ + utils.getCurrentItemIndex = function (media) { + for (var i = 0; i < media.items.length; i++) { + if (media.items[i].itemId === media.currentItemId) { + return i; + } + } + return 'Could get current item index for itemId: ' + media.currentItemId; + }; + /** * Allows you to check that a set of calls happen in a specific order. * @param {array} calls - array of expected callDetails to be receive in order From b357e6be6e7f1c7fe0cd9dae79dc10ac380120cd Mon Sep 17 00:00:00 2001 From: anna Date: Tue, 12 Nov 2019 11:53:40 +0800 Subject: [PATCH 073/166] finished auto tests --- plugin.xml | 14 +- src/ios/CastRequestDelegate.h | 51 ++ src/ios/CastRequestDelegate.m | 94 +++ src/ios/CastRequestDelegate.swift | 40 - src/ios/CastUtilities.h | 49 ++ src/ios/CastUtilities.m | 1172 +++++++++++++++++++++++++++++ src/ios/CastUtilities.swift | 476 ------------ src/ios/Chromecast.h | 49 ++ src/ios/Chromecast.m | 520 +++++++++++++ src/ios/Chromecast.swift | 304 -------- src/ios/ChromecastSession.h | 49 ++ src/ios/ChromecastSession.m | 438 +++++++++++ src/ios/ChromecastSession.swift | 251 ------ 13 files changed, 2430 insertions(+), 1077 deletions(-) create mode 100644 src/ios/CastRequestDelegate.h create mode 100644 src/ios/CastRequestDelegate.m delete mode 100644 src/ios/CastRequestDelegate.swift create mode 100644 src/ios/CastUtilities.h create mode 100644 src/ios/CastUtilities.m delete mode 100644 src/ios/CastUtilities.swift create mode 100644 src/ios/Chromecast.h create mode 100644 src/ios/Chromecast.m delete mode 100644 src/ios/Chromecast.swift create mode 100644 src/ios/ChromecastSession.h create mode 100644 src/ios/ChromecastSession.m delete mode 100644 src/ios/ChromecastSession.swift diff --git a/plugin.xml b/plugin.xml index 39b27c2..eb75fe5 100644 --- a/plugin.xml +++ b/plugin.xml @@ -69,11 +69,13 @@ - - - - - - + + + + + + + + diff --git a/src/ios/CastRequestDelegate.h b/src/ios/CastRequestDelegate.h new file mode 100644 index 0000000..9381ed0 --- /dev/null +++ b/src/ios/CastRequestDelegate.h @@ -0,0 +1,51 @@ +// +// CastRequestDelegate.h +// ChromeCast +// +// Created by mac on 2019/9/30. +// + +#import +#import +#import +NS_ASSUME_NONNULL_BEGIN + +@protocol CastSessionListener + +-(void)onMediaLoaded:(NSDictionary*)media; +-(void)onMediaUpdated:(NSDictionary*)media isAlive:(BOOL)isAlive; +-(void)onSessionUpdated:(NSDictionary*)session isAlive:(BOOL)isAlive; +-(void)onSessionEnd:(NSDictionary*)session; +-(void)onMessageReceived:(NSDictionary*)session namespace:(NSString*)namespace message:(NSString*)message; +@end + +@interface CastConnectionListener : NSObject +{ + void (^onMediaLoaded)(NSDictionary* media); + void (^onMediaUpdated)(NSDictionary* media,BOOL isAlive); + void (^onSessionUpdated)(NSDictionary* session, BOOL isAlive); + void (^onSessionEnd)(NSDictionary* session); + void (^onMessageReceived)(NSDictionary* session,NSString* namespace,NSString* message); +} + +@property (nonatomic, copy) void (^onReceiverAvailableUpdate)(BOOL available); +@property (nonatomic, copy) void (^onSessionRejoin)(NSDictionary* session); + +- (instancetype)initWithReceiverAvailableUpdate:(void(^)(BOOL available))onReceiverAvailableUpdate onSessionRejoin:(void(^)(NSDictionary* session))onSessionRejoin onMediaLoaded:(void(^)(NSDictionary* media))onMediaLoaded onMediaUpdated:(void(^)(NSDictionary* media, BOOL isAlive))onMediaUpdated onSessionUpdated:(void(^)(NSDictionary* session, BOOL isAlive))onSessionUpdated onSessionEnd:(void(^)(NSDictionary* session))onSessionEnd onMessageReceived:(void(^)(NSDictionary* session,NSString* namespace,NSString* message))onMessageReceived ; +@end + +@interface CastRequestDelegate : NSObject +{ + void (^didSuccess)(void); + void (^didFail)(GCKError*); + void (^didAbort)(GCKRequestAbortReason); + BOOL finished; +} + +@property (nonatomic,assign) BOOL finished; + +- (instancetype)initWithSuccess:(void(^)(void))success failure:(void(^)(GCKError*))failure abortion:(void(^)(GCKRequestAbortReason))abortion; + +@end + +NS_ASSUME_NONNULL_END diff --git a/src/ios/CastRequestDelegate.m b/src/ios/CastRequestDelegate.m new file mode 100644 index 0000000..5bc31d6 --- /dev/null +++ b/src/ios/CastRequestDelegate.m @@ -0,0 +1,94 @@ +// +// CastRequestDelegate.m +// ChromeCast +// +// Created by mac on 2019/9/30. +// + +#import "CastRequestDelegate.h" + +@implementation CastConnectionListener + +- (instancetype)initWithReceiverAvailableUpdate:(void(^)(BOOL available))onReceiverAvailableUpdate onSessionRejoin:(void(^)(NSDictionary* session))onSessionRejoin onMediaLoaded:(void(^)(NSDictionary* media))onMediaLoaded onMediaUpdated:(void(^)(NSDictionary* media, BOOL isAlive))onMediaUpdated onSessionUpdated:(void(^)(NSDictionary* session, BOOL isAlive))onSessionUpdated onSessionEnd:(void(^)(NSDictionary* session))onSessionEnd onMessageReceived:(void(^)(NSDictionary* session,NSString* namespace,NSString* message))onMessageReceived { + + self = [super init]; + if (self) { + self.onReceiverAvailableUpdate = onReceiverAvailableUpdate; + self.onSessionRejoin = onSessionRejoin; + onMediaLoaded = onMediaLoaded; + onSessionUpdated = onSessionUpdated; + onMediaUpdated = onMediaUpdated; + onSessionEnd = onSessionEnd; + onMessageReceived = onMessageReceived; + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onCastStateChanged:) name:kGCKCastStateDidChangeNotification object:nil]; + } + return self; +} + +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +- (void)onCastStateChanged:(NSNotification*)notification { + GCKCastState castState = [notification.userInfo[kGCKNotificationKeyCastState] intValue]; + if (castState == GCKCastStateNoDevicesAvailable) { + self.onReceiverAvailableUpdate(false); + } else { + self.onReceiverAvailableUpdate(true); + } +} + +- (void)onMediaUpdated:(NSDictionary *)media isAlive:(BOOL)isAlive { + onMediaUpdated(media,isAlive); +} + +- (void)onMediaLoaded:(NSDictionary *)media { + onMediaLoaded(media); +} + +- (void)onSessionUpdated:(NSDictionary *)session isAlive:(BOOL)isAlive { + onSessionUpdated(session,isAlive); +} + +- (void)onSessionEnd:(NSDictionary *)session { + onSessionEnd(session); +} + +- (void)onMessageReceived:(NSDictionary *)session namespace:(NSString *)namespace message:(NSString *)message { + onMessageReceived(session,namespace,message); +} + + +@end + +@implementation CastRequestDelegate + +- (instancetype)initWithSuccess:(void(^)(void))success failure:(void(^)(GCKError*))failure abortion:(void(^)(GCKRequestAbortReason))abortion +{ + self = [super init]; + if (self) { + didSuccess = success; + didFail = failure; + didAbort = abortion; + finished = false; + } + return self; +} + +-(void)requestDidComplete:(GCKRequest *)request{ + didSuccess(); + finished = true; +} + +-(void)request:(GCKRequest *)request didFailWithError:(GCKError *)error{ + didFail(error); + finished = true; +} + +- (void)request:(GCKRequest *)request didAbortWithReason:(GCKRequestAbortReason)abortReason { + didAbort(abortReason); + finished = true; +} +@end diff --git a/src/ios/CastRequestDelegate.swift b/src/ios/CastRequestDelegate.swift deleted file mode 100644 index bd78c42..0000000 --- a/src/ios/CastRequestDelegate.swift +++ /dev/null @@ -1,40 +0,0 @@ -import GoogleCast - -class CastRequestDelegate : NSObject, GCKRequestDelegate { - var didSuccess:()->() - var didFail:((GCKError) -> ())? - var didAbort:((GCKRequestAbortReason) -> ())? - var finished: Bool - - init(success:@escaping ()->(), - failure:((GCKError) -> ())? = nil, - abortion:((GCKRequestAbortReason) -> ())? = nil - ) { - self.didSuccess = success - self.didFail = failure - self.didAbort = abortion - self.finished = false - } - - func requestDidComplete(_ request: GCKRequest) { - self.didSuccess() - self.finished = true - } - - func request(_ request: GCKRequest, didFailWithError error: GCKError) { - self.didFail?(error) - self.finished = true - } - - func request(_ request: GCKRequest, didAbortWith abortReason: GCKRequestAbortReason) { - self.didAbort?(abortReason) - self.finished = true - } -} - -protocol CastSessionListener { - func onMediaLoaded(_ media: NSDictionary) - func onMediaUpdated(_ media: NSDictionary, isAlive: Bool) - func onSessionUpdated(_ session: NSDictionary, isAlive: Bool) - func onMessageReceived(_ session: NSDictionary, namespace: String, message: String) -} diff --git a/src/ios/CastUtilities.h b/src/ios/CastUtilities.h new file mode 100644 index 0000000..4e3f20d --- /dev/null +++ b/src/ios/CastUtilities.h @@ -0,0 +1,49 @@ +// +// CastUtilities.h +// ChromeCast +// +// Created by mac on 2019/9/30. +// + +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface CastUtilities : NSObject + ++ (GCKMediaInformation *)buildMediaInformation:(NSString *)contentUrl customData:(id )customData contentType:(NSString *)contentType duration:(double)duration streamType:(NSString *)streamType textTrackStyle:(NSDictionary *)textTrackStyle metaData:(NSDictionary *)metaData; + ++ (GCKMediaTextTrackStyle *)buildTextTrackStyle:(NSDictionary *)data; ++(GCKMediaMetadata*)buildMediaMetadata:(NSDictionary*)data; ++(NSArray*)getMetadataImages:(NSData*)imagesRaw; ++(NSDictionary*)createSessionObject:(GCKCastSession*)session; ++ (NSDictionary*)createSessionObject:(GCKCastSession *)session status:(NSString*)status; ++(NSDictionary*)createMediaObject:(GCKCastSession*)session; ++(NSDictionary*)createMediaInfoObject:(GCKMediaInformation*)mediaInfo; ++(NSArray*)createDeviceObject:(NSArray*)devices; ++(NSArray*)getMediaTracks:(NSArray*)mediaTracks; ++(NSDictionary*)getTextTrackStyle:(GCKMediaTextTrackStyle*)textTrackStyle; ++(NSString*)getEdgeType:(GCKMediaTextTrackStyleEdgeType)edgeType; ++(NSString*)getFontGenericFamily:(GCKMediaTextTrackStyleFontGenericFamily)fontGenericFamily; ++(NSString*)getFontStyle:(GCKMediaTextTrackStyleFontStyle)fontStyle; ++(NSString*)getWindowType:(GCKMediaTextTrackStyleWindowType)windowType; ++(NSString*)getTrackType:(GCKMediaTrackType)trackType; ++(NSString*)getTextTrackSubtype:(GCKMediaTextTrackSubtype)textSubtype; ++(NSString*)getIdleReason:(GCKMediaPlayerIdleReason)reason; ++(NSString*)getPlayerState:(GCKMediaPlayerState)playerState; ++(NSString*)getStreamType:(GCKMediaStreamType)streamType; ++(GCKMediaTextTrackStyleEdgeType)parseEdgeType:(NSString*)edgeType; ++(GCKMediaTextTrackStyleFontGenericFamily)parseFontGenericFamily:(NSString*)fontGenericFamily; ++(GCKMediaTextTrackStyleFontStyle)parseFontStyle:(NSString*)fontStyle; ++(GCKMediaTextTrackStyleWindowType)parseWindowType:(NSString*)windowType; ++(GCKMediaResumeState)parseResumeState:(NSString*)resumeState; ++(GCKMediaMetadataType)parseMediaMetadataType:(NSInteger)metadataType; ++(NSString*)convertDictToJsonString:(NSDictionary*)dict; ++ (NSDictionary*)createError:(NSString*)code message:(NSString*)message; ++ (GCKMediaInformation *)buildMediaInformationForQueueItem:(NSString *)contentUrl customData:(id )customData contentType:(NSString *)contentType duration:(double)duration startTime:(double)startTime streamType:(NSString *)streamType metaData:(NSDictionary *)metaData; ++ (NSDictionary *)createMediaObjectForQueue:(GCKCastSession *)session; ++ (NSDictionary *)createMediaObjectForQueueJumpToItem:(GCKCastSession *)session; +@end + +NS_ASSUME_NONNULL_END diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m new file mode 100644 index 0000000..cded9f1 --- /dev/null +++ b/src/ios/CastUtilities.m @@ -0,0 +1,1172 @@ +// +// CastUtilities.m +// ChromeCast +// +// Created by mac on 2019/9/30. +// + +#import "CastUtilities.h" + +@implementation CastUtilities + ++ (GCKMediaInformation *)buildMediaInformation:(NSString *)contentUrl customData:(id )customData contentType:(NSString *)contentType duration:(double)duration streamType:(NSString *)streamType textTrackStyle:(NSDictionary *)textTrackStyle metaData:(NSDictionary *)metaData { + NSURL* url = [NSURL URLWithString:contentUrl]; + GCKMediaInformationBuilder* mediaInfoBuilder = [[GCKMediaInformationBuilder alloc] initWithContentURL:url]; + mediaInfoBuilder.customData = customData; + mediaInfoBuilder.contentType = contentType; + mediaInfoBuilder.streamDuration = round(duration); + if ([streamType isEqualToString:@"buffered"]) { + mediaInfoBuilder.streamType = GCKMediaStreamTypeBuffered; + } else if ([streamType isEqualToString:@"live"]) { + mediaInfoBuilder.streamType = GCKMediaStreamTypeLive; + } else { + mediaInfoBuilder.streamType = GCKMediaStreamTypeNone; + } + + mediaInfoBuilder.textTrackStyle = [CastUtilities buildTextTrackStyle:textTrackStyle]; + mediaInfoBuilder.metadata = [CastUtilities buildMediaMetadata:metaData]; + return [mediaInfoBuilder build]; +} + ++ (GCKMediaInformation *)buildMediaInformationForQueueItem:(NSString *)contentUrl customData:(id )customData contentType:(NSString *)contentType duration:(double)duration startTime:(double)startTime streamType:(NSString *)streamType metaData:(NSDictionary *)metaData { + NSURL* url = [NSURL URLWithString:contentUrl]; + GCKMediaInformationBuilder* mediaInfoBuilder = [[GCKMediaInformationBuilder alloc] initWithContentURL:url]; + mediaInfoBuilder.customData = customData; + mediaInfoBuilder.contentType = contentType; + mediaInfoBuilder.streamDuration = round(duration); + //mediaInfoBuilder.startAbsoluteTime = round(startTime); + if ([streamType isEqualToString:@"buffered"]) { + mediaInfoBuilder.streamType = GCKMediaStreamTypeBuffered; + } else if ([streamType isEqualToString:@"live"]) { + mediaInfoBuilder.streamType = GCKMediaStreamTypeLive; + } else { + mediaInfoBuilder.streamType = GCKMediaStreamTypeNone; + } + + mediaInfoBuilder.metadata = [CastUtilities buildMediaMetadata:metaData]; + return [mediaInfoBuilder build]; +} + ++ (GCKMediaTextTrackStyle *)buildTextTrackStyle:(NSDictionary *)data { + NSError *error = nil; + GCKMediaTextTrackStyle* mediaTextTrackStyle = [GCKMediaTextTrackStyle createDefault]; + + if (error == nil) { + NSString* bkgColor = data[@"backgroundColor"]; + if (bkgColor != nil) { + mediaTextTrackStyle.backgroundColor = [[GCKColor alloc] initWithCSSString:bkgColor]; + + } + + NSObject* customData = data[@"customData"]; + if (bkgColor != nil) { + mediaTextTrackStyle.customData = customData; + + } + + NSString* edgeColor = data[@"edgeColor"]; + if (edgeColor != nil) { + mediaTextTrackStyle.edgeColor = [[GCKColor alloc] initWithCSSString:edgeColor]; + + } + + NSString* edgeType = data[@"edgeType"]; + if (edgeType != nil) { + mediaTextTrackStyle.edgeType = [CastUtilities parseEdgeType:edgeType]; + } + + NSString* fontFamily = data[@"fontFamily"]; + if (fontFamily != nil) { + mediaTextTrackStyle.fontFamily = fontFamily; + } + + NSString* fontGenericFamily = data[@"fontGenericFamily"]; + if (fontGenericFamily != nil) { + mediaTextTrackStyle.fontGenericFamily = [CastUtilities parseFontGenericFamily:fontGenericFamily]; + } + + CGFloat fontScale = (CGFloat)[data[@"fontScale"] floatValue]; + if (fontScale != 0) { + mediaTextTrackStyle.fontScale = fontScale; + } + + NSString* fontStyle = data[@"fontStyle"]; + if (fontFamily != nil) { + mediaTextTrackStyle.fontStyle = [CastUtilities parseFontStyle:fontStyle]; + } + NSString* foregroundColor = data[@"foregroundColor"]; + if (fontFamily != nil) { + mediaTextTrackStyle.foregroundColor = [[GCKColor alloc] initWithCSSString:foregroundColor]; + } + + NSString* windowColor = data[@"windowColor"]; + if (windowColor != nil) { + mediaTextTrackStyle.windowColor = [[GCKColor alloc] initWithCSSString:windowColor]; + } + + CGFloat wRoundedCorner = (CGFloat)[data[@"windowRoundedCornerRadius"] floatValue]; + if (wRoundedCorner != 0) { + mediaTextTrackStyle.windowRoundedCornerRadius = wRoundedCorner; + } + + NSString* windowType = data[@"windowType"]; + if (windowType != nil) { + mediaTextTrackStyle.windowType = [CastUtilities parseWindowType:windowType]; + } + } + return mediaTextTrackStyle; +} + ++(GCKMediaMetadata*)buildMediaMetadata:(NSDictionary*)data { + GCKMediaMetadata* mediaMetaData = [[GCKMediaMetadata alloc] initWithMetadataType:GCKMediaMetadataTypeGeneric]; + + if (data[@"metadataType"]) { + int metadataType = [data[@"metadataType"] intValue]; + mediaMetaData = [[GCKMediaMetadata alloc] initWithMetadataType:metadataType]; + } + NSData* imagesRaw = data[@"images"]; + if (imagesRaw != nil) { + NSArray* images = [CastUtilities getMetadataImages:imagesRaw]; + for (GCKImage* image in images) { + [mediaMetaData addImage:image]; + } + } + + NSArray* keys = data.allKeys; + for (NSString* key in keys) { + if ([key isEqualToString:@"metadataType"] || [key isEqualToString:@"images"] || [key isEqualToString:@"type"]) { + continue; + } + NSString* convertedKey = [CastUtilities getiOSMetadataName:key]; + NSString* dataType = [CastUtilities getMetadataType:convertedKey]; + if ([dataType isEqualToString:@"string"]) { + if (data[key]) { + [mediaMetaData setString:data[key] forKey:convertedKey]; + } + } + if ([dataType isEqualToString:@"int"]) { + if (data[key]) { + [mediaMetaData setInteger:[data[key] intValue] forKey:convertedKey]; + } + } + if ([dataType isEqualToString:@"double"]) { + if (data[key]) { + [mediaMetaData setDouble:[data[key] doubleValue] forKey:convertedKey]; + } + } + if ([dataType isEqualToString:@"date"]) { + if (![data[key] isKindOfClass:[NSString class]]) { + NSDate* date = [NSDate dateWithTimeIntervalSince1970:[data[key] longValue] / 1000]; + [mediaMetaData setDate:date forKey:convertedKey]; + } + } + if ([dataType isEqualToString:@"ms"]) { + if (data[key]) { + [mediaMetaData setDouble:[data[key] longValue] forKey:convertedKey]; + } + } + if (![key isEqualToString:convertedKey]) { + convertedKey = [NSString stringWithFormat:@"cordova-plugin-chromecast_metadata_key=%@",key]; + } + if ([data[key] doubleValue] != 0 && floor([data[key] doubleValue]) != [data[key] doubleValue]) { + [mediaMetaData setString:[NSString stringWithFormat:@"%@",data[key]] forKey:convertedKey]; + } else { + [mediaMetaData setString:data[key] forKey:convertedKey]; + } + } + + return mediaMetaData; +} + ++(NSString*)getMetadataType:(NSString*)iOSName { + if ([iOSName isEqualToString:kGCKMetadataKeyAlbumArtist]) { + return @"string"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyAlbumTitle]) { + return @"string"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyArtist]) { + return @"string"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyBookTitle]) { + return @"string"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyBroadcastDate]) { + return @"date"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyChapterNumber]) { + return @"string"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyChapterTitle]) { + return @"string"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyComposer]) { + return @"string"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyCreationDate]) { + return @"date"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyDiscNumber]) { + return @"int"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyEpisodeNumber]) { + return @"int"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyHeight]) { + return @"int"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyLocationLatitude]) { + return @"double"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyLocationLongitude]) { + return @"double"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyLocationName]) { + return @"string"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyQueueItemID]) { + return @"int"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyReleaseDate]) { + return @"date"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySeasonNumber]) { + return @"int"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySectionDuration]) { + return @"ms"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySectionStartAbsoluteTime]) { + return @"ms"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySectionStartTimeInContainer]) { + return @"ms"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySectionStartTimeInMedia]) { + return @"ms"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySeriesTitle]) { + return @"string"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyStudio]) { + return @"string"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySubtitle]) { + return @"string"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyTitle]) { + return @"string"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyTrackNumber]) { + return @"int"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyWidth]) { + return @"int"; + } + return iOSName; +} ++(NSString*)getiOSMetadataName:(NSString*)clientName { + if ([clientName isEqualToString:@"albumArtist"]) { + return kGCKMetadataKeyAlbumArtist; + } + if ([clientName isEqualToString:@"albumName"]) { + return kGCKMetadataKeyAlbumTitle; + } + if ([clientName isEqualToString:@"artist"]) { + return kGCKMetadataKeyArtist; + } + if ([clientName isEqualToString:@"bookTitle"]) { + return kGCKMetadataKeyBookTitle; + } + if ([clientName isEqualToString:@"broadcastDate"]) { + return kGCKMetadataKeyBroadcastDate; + } + if ([clientName isEqualToString:@"chapterNumber"]) { + return kGCKMetadataKeyChapterNumber; + } + if ([clientName isEqualToString:@"chapterTitle"]) { + return kGCKMetadataKeyChapterTitle; + } + if ([clientName isEqualToString:@"composer"]) { + return kGCKMetadataKeyComposer; + } + if ([clientName isEqualToString:@"creationDate"]) { + return kGCKMetadataKeyCreationDate; + } + if ([clientName isEqualToString:@"creationDateTime"]) { + return kGCKMetadataKeyCreationDate; + } + if ([clientName isEqualToString:@"discNumber"]) { + return kGCKMetadataKeyDiscNumber; + } + if ([clientName isEqualToString:@"episode"]) { + return kGCKMetadataKeyEpisodeNumber; + } + if ([clientName isEqualToString:@"height"]) { + return kGCKMetadataKeyHeight; + } + if ([clientName isEqualToString:@"latitude"]) { + return kGCKMetadataKeyLocationLatitude; + } + if ([clientName isEqualToString:@"longitude"]) { + return kGCKMetadataKeyLocationLongitude; + } + if ([clientName isEqualToString:@"locationName"]) { + return kGCKMetadataKeyLocationName; + } + if ([clientName isEqualToString:@"queueItemId"]) { + return kGCKMetadataKeyQueueItemID; + } + if ([clientName isEqualToString:@"releaseDate"]) { + return kGCKMetadataKeyReleaseDate; + } + if ([clientName isEqualToString:@"originalAirDate"]) { + return kGCKMetadataKeyReleaseDate; + } + if ([clientName isEqualToString:@"season"]) { + return kGCKMetadataKeySeasonNumber; + } + if ([clientName isEqualToString:@"sectionDuration"]) { + return kGCKMetadataKeySectionDuration; + } + if ([clientName isEqualToString:@"sectionStartAbsoluteTime"]) { + return kGCKMetadataKeySectionStartAbsoluteTime; + } + if ([clientName isEqualToString:@"sectionStartTimeInContainer"]) { + return kGCKMetadataKeySectionStartTimeInContainer; + } + if ([clientName isEqualToString:@"sectionStartTimeInMedia"]) { + return kGCKMetadataKeySectionStartTimeInMedia; + } + if ([clientName isEqualToString:@"seriesTitle"]) { + return kGCKMetadataKeySeriesTitle; + } + if ([clientName isEqualToString:@"studio"]) { + return kGCKMetadataKeyStudio; + } + if ([clientName isEqualToString:@"subtitle"]) { + return kGCKMetadataKeySubtitle; + } + if ([clientName isEqualToString:@"title"]) { + return kGCKMetadataKeyTitle; + } + if ([clientName isEqualToString:@"trackNumber"]) { + return kGCKMetadataKeyTrackNumber; + } + if ([clientName isEqualToString:@"width"]) { + return kGCKMetadataKeyWidth; + } + return clientName; +} + ++(NSString*)getClientMetadataName:(NSString*)iOSName { + if ([iOSName isEqualToString:kGCKMetadataKeyAlbumArtist]) { + return @"albumArtist"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyAlbumTitle]) { + return @"albumName"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyArtist]) { + return @"artist"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyBookTitle]) { + return @"bookTitle"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyBroadcastDate]) { + return @"broadcastDate"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyChapterNumber]) { + return @"chapterNumber"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyChapterTitle]) { + return @"chapterTitle"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyComposer]) { + return @"composer"; + } + + if ([iOSName isEqualToString:kGCKMetadataKeyCreationDate]) { + return @"creationDate"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyDiscNumber]) { + return @"discNumber"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyEpisodeNumber]) { + return @"episode"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyHeight]) { + return @"height"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyLocationLatitude]) { + return @"latitude"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyLocationLongitude]) { + return @"longitude"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyLocationName]) { + return @"location"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyQueueItemID]) { + return @"queueItemId"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyReleaseDate]) { + return @"releaseDate"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySeasonNumber]) { + return @"season"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySectionDuration]) { + return @"sectionDuration"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySectionStartAbsoluteTime]) { + return @"sectionStartAbsoluteTime"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySectionStartTimeInContainer]) { + return @"sectionStartTimeInContainer"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySectionStartTimeInMedia]) { + return @"sectionStartTimeInMedia"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySeriesTitle]) { + return @"seriesTitle"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyStudio]) { + return @"studio"; + } + if ([iOSName isEqualToString:kGCKMetadataKeySubtitle]) { + return @"subtitle"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyTitle]) { + return @"title"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyTrackNumber]) { + return @"trackNumber"; + } + if ([iOSName isEqualToString:kGCKMetadataKeyWidth]) { + return @"width"; + } + return iOSName; +} ++ (NSArray *)getMetadataImages:(NSArray *)imagesRaw { + NSMutableArray* images = [NSMutableArray new]; + + for (NSDictionary* dict in imagesRaw) { + NSString* urlString = dict[@"url"]; + NSURL* url = [NSURL URLWithString:urlString]; + int width = 100; + int height = 100; + if (dict[@"width"] == nil) { + width = [dict[@"width"] intValue]; + } + if (dict[@"height"] == nil) { + height = [dict[@"height"] intValue]; + } + [images addObject:[[GCKImage alloc] initWithURL:url width:width height:height]]; + } + + return images; +} + ++ (NSDictionary *)createSessionObject:(GCKCastSession *)session { + return @{ + @"appId" : session.applicationMetadata.applicationID? session.applicationMetadata.applicationID : @"", + @"media" : [CastUtilities createMediaObject:session], + @"appImages" : @{}, + @"sessionId" : session.sessionID? session.sessionID : @"", + @"displayName" : session.applicationMetadata.applicationName? session.applicationMetadata.applicationName : @"", + @"receiver" : @{ + @"friendlyName" : session.device.friendlyName? session.device.friendlyName : @"", + @"label" : session.device.uniqueID, + @"volume" : @{ + @"level" : @(session.currentDeviceVolume), + @"muted" : @(session.currentDeviceMuted) + } + }, + + }; +} + ++ (NSDictionary*)createSessionObject:(GCKCastSession *)session status:(NSString*)status { + return @{ + @"appId" : session.applicationMetadata.applicationID? session.applicationMetadata.applicationID : @"", + @"media" : [CastUtilities createMediaObject:session], + @"appImages" : @{}, + @"sessionId" : session.sessionID? session.sessionID : @"", + @"displayName" : session.applicationMetadata.applicationName? session.applicationMetadata.applicationName : @"", + @"receiver" : @{ + @"friendlyName" : session.device.friendlyName? session.device.friendlyName : @"", + @"label" : session.device.uniqueID, + @"volume" : @{ + @"level" : @(session.currentDeviceVolume), + @"muted" : @(session.currentDeviceMuted) + } + }, + @"status":status + }; +} + ++ (NSDictionary *)createMediaObjectForQueue:(GCKCastSession *)session { + if (session.remoteMediaClient == nil) { + return @{}; + } + + GCKMediaStatus* mediaStatus = session.remoteMediaClient.mediaStatus; + if (mediaStatus == nil) { + return @{}; + } + + NSMutableArray *qItems = [[NSMutableArray alloc] init]; + for (int i=0; i*)devices +{ + NSMutableArray* deviceArray = [[NSMutableArray alloc] init]; + for (int i=0; i < devices.count; i++) { + GCKDevice* device = devices[i]; + NSString* deviceName = @""; + if (device.friendlyName != nil) { + deviceName = device.friendlyName; + } else { + deviceName = device.deviceID; + } + NSMutableDictionary* deviceDict = [[NSMutableDictionary alloc] initWithDictionary:@{@"id":device.uniqueID,@"name":deviceName}]; + deviceDict[@"isNearbyDevice"] = [NSNumber numberWithBool:!device.isOnLocalNetwork]; + deviceDict[@"isCastGroup"] = [NSNumber numberWithBool:NO]; + [deviceArray addObject:[NSDictionary dictionaryWithDictionary:deviceDict]]; + } + return [NSArray arrayWithArray:deviceArray]; +} + ++ (NSArray *)getMediaTracks:(NSArray *)mediaTracks { + NSMutableArray* tracks = [NSMutableArray new]; + + if (mediaTracks == nil) { + return tracks; + } + +// for (GCKMediaTrack* mediaTrack in mediaTracks) { +// NSDictionary* track = @{ +// @"trackId": @(mediaTrack.identifier), +// @"customData": mediaTrack.customData == nil? @{} : mediaTrack.customData, +// @"language": mediaTrack.languageCode == nil? @"" : mediaTrack.languageCode, +// @"name": mediaTrack.name == nil? @"" : mediaTrack.name, +// @"subtype": [CastUtilities getTextTrackSubtype:mediaTrack.textSubtype], +// @"trackContentId": mediaTrack.contentIdentifier == nil ? @"" : mediaTrack.contentIdentifier, +// @"trackContentType": mediaTrack.contentType == nil ? @"" : mediaTrack.contentType, +// @"type": [CastUtilities getTrackType:mediaTrack.type], +// }; +// [tracks addObject:track]; +// } + return tracks; +} + ++ (NSDictionary *)getTextTrackStyle:(GCKMediaTextTrackStyle *)textTrackStyle { + if (textTrackStyle == nil) { + return @{}; + } + return @{ + @"backgroundColor": textTrackStyle.backgroundColor.CSSString, + @"customData": textTrackStyle.customData == nil? @{} : textTrackStyle.customData, + @"edgeColor": textTrackStyle.edgeColor.CSSString == nil? @"" : textTrackStyle.edgeColor.CSSString, + @"edgeType": [CastUtilities getEdgeType:textTrackStyle.edgeType], + @"fontFamily": textTrackStyle.fontFamily, + @"fontGenericFamily": [CastUtilities getFontGenericFamily:textTrackStyle.fontGenericFamily], + @"fontScale": @(textTrackStyle.fontScale), + @"fontStyle": [CastUtilities getFontStyle:textTrackStyle.fontStyle], + @"foregroundColor": textTrackStyle.foregroundColor.CSSString, + @"windowColor": textTrackStyle.windowColor.CSSString, + @"windowRoundedCornerRadius": @(textTrackStyle.windowRoundedCornerRadius), + @"windowType": [CastUtilities getWindowType:textTrackStyle.windowType] + }; +} + ++ (NSString *)getEdgeType:(GCKMediaTextTrackStyleEdgeType)edgeType { + switch (edgeType) { + case GCKMediaTextTrackStyleEdgeTypeDepressed: + return @"DEPRESSED"; + case GCKMediaTextTrackStyleEdgeTypeDropShadow: + return @"DROP_SHADOW"; + case GCKMediaTextTrackStyleEdgeTypeOutline: + return @"OUTLINE"; + case GCKMediaTextTrackStyleEdgeTypeRaised: + return @"RAISED"; + default: + return @"NONE"; + } +} + ++ (NSString *)getFontGenericFamily:(GCKMediaTextTrackStyleFontGenericFamily)fontGenericFamily { + switch (fontGenericFamily) { + case GCKMediaTextTrackStyleFontGenericFamilyCursive: + return @"CURSIVE"; + case GCKMediaTextTrackStyleFontGenericFamilyMonospacedSansSerif: + return @"MONOSPACED_SANS_SERIF"; + case GCKMediaTextTrackStyleFontGenericFamilyMonospacedSerif: + return @"MONOSPACED_SERIF"; + case GCKMediaTextTrackStyleFontGenericFamilySansSerif: + return @"SANS_SERIF"; + case GCKMediaTextTrackStyleFontGenericFamilySerif: + return @"SERIF"; + case GCKMediaTextTrackStyleFontGenericFamilySmallCapitals: + return @"SMALL_CAPITALS"; + default: + return @"SERIF"; + } +} + ++ (NSString *)getFontStyle:(GCKMediaTextTrackStyleFontStyle)fontStyle { + switch (fontStyle) { + case GCKMediaTextTrackStyleFontStyleNormal: + return @"NORMAL"; + case GCKMediaTextTrackStyleFontStyleBold: + return @"BOLD"; + case GCKMediaTextTrackStyleFontStyleBoldItalic: + return @"BOLD_ITALIC"; + case GCKMediaTextTrackStyleFontStyleItalic: + return @"ITALIC"; + default: + return @"NORMAL"; + } +} + ++ (NSString *)getWindowType:(GCKMediaTextTrackStyleWindowType)windowType { + switch (windowType) { + case GCKMediaTextTrackStyleWindowTypeNormal: + return @"NORMAL"; + case GCKMediaTextTrackStyleWindowTypeRoundedCorners: + return @"ROUNDED_CORNERS"; + default: + return @"NONE"; + } +} + ++ (NSString *)getTrackType:(GCKMediaTrackType)trackType { + switch (trackType) { + case GCKMediaTrackTypeAudio: + return @"AUDIO"; + case GCKMediaTrackTypeText: + return @"TEXT"; + case GCKMediaTrackTypeVideo: + return @"VIDEO"; + default: + return nil; + } +} + ++ (NSString *)getTextTrackSubtype:(GCKMediaTextTrackSubtype)textSubtype { + switch (textSubtype) { + case GCKMediaTextTrackSubtypeCaptions: + return @"CAPTIONS"; + case GCKMediaTextTrackSubtypeChapters: + return @"CHAPTERS"; + case GCKMediaTextTrackSubtypeDescriptions: + return @"DESCRIPTIONS"; + case GCKMediaTextTrackSubtypeMetadata: + return @"METADATA"; + case GCKMediaTextTrackSubtypeSubtitles: + return @"SUBTITLES"; + default: + return nil; + } +} + ++ (NSString *)getIdleReason:(GCKMediaPlayerIdleReason)reason { + BOOL jump = [NSUserDefaults.standardUserDefaults boolForKey:@"jump"]; + NSString *idleReason = @"FINISHED"; + if (jump) { + idleReason = @"INTERRUPTED"; + } + switch (reason) { + case GCKMediaPlayerIdleReasonCancelled: + return @"CANCELLED"; + case GCKMediaPlayerIdleReasonError: + return @"ERROR"; + case GCKMediaPlayerIdleReasonFinished: + return @"FINISHED"; + case GCKMediaPlayerIdleReasonInterrupted: + return @"INTERRUPTED"; + default: + return idleReason; + } +} + ++ (NSString *)getRepeatMode:(GCKMediaRepeatMode)repeatMode { + switch (repeatMode) { + case GCKMediaRepeatModeOff: + return @"REPEAT_OFF"; + case GCKMediaRepeatModeAll: + return @"REPEAT_ALL"; + case GCKMediaRepeatModeAllAndShuffle: + return @"REPEAT_ALL_AND_SHUFFLE"; + case GCKMediaRepeatModeSingle: + return @"REPEAT_SINGLE"; + default: + return @"REPEAT_OFF"; + } +} ++ (NSString *)getPlayerState:(GCKMediaPlayerState)playerState { + switch (playerState) { + case GCKMediaPlayerStateBuffering: + return @"BUFFERING"; + case GCKMediaPlayerStateIdle: + return @"IDLE"; + case GCKMediaPlayerStatePaused: + return @"PAUSED"; + case GCKMediaPlayerStatePlaying: + return @"PLAYING"; + default: + return @"IDLE"; + } +} + ++ (NSString *)getPlayerStateCheck:(GCKMediaPlayerState)playerState { + switch (playerState) { + case GCKMediaPlayerStateBuffering: + return @"BUFFERING"; + case GCKMediaPlayerStateIdle: + return @"IDLE"; + case GCKMediaPlayerStatePaused: + return @"PAUSED"; + case GCKMediaPlayerStatePlaying: + return @"PLAYING"; + case GCKMediaPlayerStateUnknown: + return @"IDLE"; + default: + return @"LOADING"; + } +} + ++ (NSString *)getStreamType:(GCKMediaStreamType)streamType { + switch (streamType) { + case GCKMediaStreamTypeBuffered: + return @"buffered"; + case GCKMediaStreamTypeLive: + return @"live"; + case GCKMediaStreamTypeNone: + return @"other"; + default: + return @"unknown"; + } +} + ++ (GCKMediaTextTrackStyleEdgeType)parseEdgeType:(NSString *)edgeType { + if ([edgeType isEqualToString:@"DEPRESSED"]) { + return GCKMediaTextTrackStyleEdgeTypeDepressed; + } + if ([edgeType isEqualToString:@"DROP_SHADOW"]) { + return GCKMediaTextTrackStyleEdgeTypeDropShadow; + } + if ([edgeType isEqualToString:@"OUTLINE"]) { + return GCKMediaTextTrackStyleEdgeTypeOutline; + } + if ([edgeType isEqualToString:@"RAISED"]) { + return GCKMediaTextTrackStyleEdgeTypeRaised; + } + return GCKMediaTextTrackStyleEdgeTypeNone; +} + ++ (GCKMediaTextTrackStyleFontGenericFamily)parseFontGenericFamily:(NSString *)fontGenericFamily { + if ([fontGenericFamily isEqualToString:@"CURSIVE"]) { + return GCKMediaTextTrackStyleFontGenericFamilyCursive; + } + if ([fontGenericFamily isEqualToString:@"MONOSPACED_SANS_SERIF"]) { + return GCKMediaTextTrackStyleFontGenericFamilyMonospacedSansSerif; + } + if ([fontGenericFamily isEqualToString:@"MONOSPACED_SERIF"]) { + return GCKMediaTextTrackStyleFontGenericFamilyMonospacedSerif; + } + if ([fontGenericFamily isEqualToString:@"SANS_SERIF"]) { + return GCKMediaTextTrackStyleFontGenericFamilySansSerif; + } + if ([fontGenericFamily isEqualToString:@"SERIF"]) { + return GCKMediaTextTrackStyleFontGenericFamilySerif; + } + if ([fontGenericFamily isEqualToString:@"SMALL_CAPITALS"]) { + return GCKMediaTextTrackStyleFontGenericFamilySmallCapitals; + } + return GCKMediaTextTrackStyleFontGenericFamilySerif; +} + ++ (GCKMediaTextTrackStyleFontStyle)parseFontStyle:(NSString *)fontStyle { + if ([fontStyle isEqualToString:@"NORMAL"]) { + return GCKMediaTextTrackStyleFontStyleNormal; + } + if ([fontStyle isEqualToString:@"BOLD"]) { + return GCKMediaTextTrackStyleFontStyleBold; + } + if ([fontStyle isEqualToString:@"BOLD_ITALIC"]) { + return GCKMediaTextTrackStyleFontStyleBoldItalic; + } + if ([fontStyle isEqualToString:@"ITALIC"]) { + return GCKMediaTextTrackStyleFontStyleItalic; + } + return GCKMediaTextTrackStyleFontStyleNormal; +} + ++ (GCKMediaTextTrackStyleWindowType)parseWindowType:(NSString *)windowType { + if ([windowType isEqualToString:@"NORMAL"]) { + return GCKMediaTextTrackStyleWindowTypeNormal; + } + if ([windowType isEqualToString:@"ROUNDED_CORNERS"]) { + return GCKMediaTextTrackStyleWindowTypeRoundedCorners; + } + return GCKMediaTextTrackStyleWindowTypeUnknown; +} + ++ (GCKMediaResumeState)parseResumeState:(NSString *)resumeState { + if ([resumeState isEqualToString:@"PLAYBACK_PAUSE"]) { + return GCKMediaResumeStatePause; + } + if ([resumeState isEqualToString:@"PLAYBACK_START"]) { + return GCKMediaResumeStatePlay; + } + + return GCKMediaResumeStateUnchanged; +} + ++ (GCKMediaMetadataType)parseMediaMetadataType:(NSInteger)metadataType { + switch (metadataType) { + case 0: + return GCKMediaMetadataTypeGeneric; + case 1: + return GCKMediaMetadataTypeTVShow; + case 2: + return GCKMediaMetadataTypeMovie; + case 3: + return GCKMediaMetadataTypeMusicTrack; + case 4: + return GCKMediaMetadataTypePhoto; + default: + return GCKMediaMetadataTypeGeneric; + } +} + ++ (NSString *)convertDictToJsonString:(NSDictionary *)dict { + NSError *error = nil; + NSData* json = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error]; + return [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding]; +} + ++ (NSDictionary*)createError:(NSString*)code message:(NSString*)message { + return @{@"code":code,@"description":message}; +} +@end diff --git a/src/ios/CastUtilities.swift b/src/ios/CastUtilities.swift deleted file mode 100644 index 557cd66..0000000 --- a/src/ios/CastUtilities.swift +++ /dev/null @@ -1,476 +0,0 @@ -import Foundation -import GoogleCast - -class CastUtilities { - static func buildMediaInformation(contentUrl: String, customData: Any, contentType: String, duration: Double, streamType: String, textTrackStyle: Data, metadata: Data) -> GCKMediaInformation{ - let url = URL.init(string: contentUrl)! - - let mediaInfoBuilder = GCKMediaInformationBuilder.init(contentURL: url) - mediaInfoBuilder.customData = customData - mediaInfoBuilder.contentType = contentType - mediaInfoBuilder.streamDuration = Double(duration).rounded() - - switch streamType { - case "buffered": - mediaInfoBuilder.streamType = GCKMediaStreamType.buffered - case "live": - mediaInfoBuilder.streamType = GCKMediaStreamType.live - default: - mediaInfoBuilder.streamType = GCKMediaStreamType.none - } - - mediaInfoBuilder.textTrackStyle = CastUtilities.buildTextTrackStyle(textTrackStyle) - mediaInfoBuilder.metadata = CastUtilities.buildMediaMetadata(metadata) - - - return mediaInfoBuilder.build() - } - - static func buildTextTrackStyle(_ data: Data) -> GCKMediaTextTrackStyle { - let json = try? JSONSerialization.jsonObject(with: data, options: []) - - let mediaTextTrackStyle = GCKMediaTextTrackStyle.createDefault() - - if let dict = json as? [String: Any] { - if let bkgColor = dict["backgroundColor"] as? String { - mediaTextTrackStyle.backgroundColor = GCKColor.init(cssString: bkgColor) - } - - if let customData = dict["customData"] { - mediaTextTrackStyle.customData = customData - } - - if let edgeColor = dict["edgeColor"] as? String { - mediaTextTrackStyle.edgeColor = GCKColor.init(cssString: edgeColor) - } - - if let edgeType = dict["edgeType"] as? String { - mediaTextTrackStyle.edgeType = parseEdgeType(edgeType) - } - - if let fontFamily = dict["fontFamily"] as? String { - mediaTextTrackStyle.fontFamily = fontFamily - } - - if let fontGenericFamily = dict["fontGenericFamily"] as? String { - mediaTextTrackStyle.fontGenericFamily = parseFontGenericFamily(fontGenericFamily) - } - - if let fontScale = dict["fontScale"] as? Float { - mediaTextTrackStyle.fontScale = CGFloat(fontScale) - } - - if let fontStyle = dict["fontStyle"] as? String { - mediaTextTrackStyle.fontStyle = parseFontStyle(fontStyle) - } - - if let foregroundColor = dict["foregroundColor"] as? String { - mediaTextTrackStyle.foregroundColor = GCKColor.init(cssString: foregroundColor) - } - - if let windowColor = dict["windowColor"] as? String { - mediaTextTrackStyle.windowColor = GCKColor.init(cssString: windowColor) - } - - if let wRoundedCorner = dict["windowRoundedCornerRadius"] as? Float { - mediaTextTrackStyle.windowRoundedCornerRadius = CGFloat(wRoundedCorner) - } - - if let windowType = dict["windowType"] as? String { - mediaTextTrackStyle.windowType = parseWindowType(windowType) - } - - } - - return mediaTextTrackStyle - } - - static func buildMediaMetadata(_ data: Data) -> GCKMediaMetadata { - var mediaMetadata = GCKMediaMetadata(metadataType: GCKMediaMetadataType.generic) - - let json = try? JSONSerialization.jsonObject(with: data, options: []) - - if let dict = json as? [String: Any] { - if let metadataType = dict["metadataType"] as? Int { - mediaMetadata = GCKMediaMetadata(metadataType: parseMediaMetadataType(metadataType)) - } - - if let title = dict["title"] as? String { - mediaMetadata.setString(title, forKey: kGCKMetadataKeyTitle) - } - - if let subtitle = dict["subtitle"] as? String { - mediaMetadata.setString(subtitle, forKey: kGCKMetadataKeySubtitle) - } - - if let imagesRaw = dict["images"] as? Data { - let images = getMetadataImages(imagesRaw) - - images.forEach { (i: GCKImage) in - mediaMetadata.addImage(i) - } - } - } - - return mediaMetadata - } - - static func getMetadataImages(_ imagesRaw: Data) -> [GCKImage] { - var images = [GCKImage]() - let json = try? JSONSerialization.jsonObject(with: imagesRaw, options: []) - - if let array = json as? [[String: Any]] { - array.forEach { (dict: [String : Any]) in - if let urlString = dict["url"] as? String { - let url = URL.init(string: urlString)! - let width = dict["width"] as? Int ?? 100 - let heigth = dict["height"] as? Int ?? 100 - - images.append(GCKImage(url: url, width: width, height: heigth)) - } - } - } - - return images - } - - static func createSessionObject(_ session: GCKCastSession) -> NSDictionary { - return [ - "appId": session.applicationMetadata?.applicationID ?? "", - "media": createMediaObject(session) as NSDictionary, - "appImages": [:] as NSDictionary, - "sessionId": session.sessionID ?? "", - "displayName": session.applicationMetadata?.applicationName ?? "", - "receiver": [ - "friendlyName": session.device.friendlyName ?? "", - "label": session.device.uniqueID - ] as NSDictionary, - "volume": [ - "level": session.currentDeviceVolume, - "muted": session.currentDeviceMuted - ] as NSDictionary - - ] - } - - static func createMediaObject(_ session: GCKCastSession) -> NSDictionary { - if session.remoteMediaClient == nil { - return [:] - } - - let mediaStatus = session.remoteMediaClient?.mediaStatus - - if mediaStatus == nil { - return [:] - } - - return [ - "currentItemId": mediaStatus!.currentItemID, - "currentTime": mediaStatus!.streamPosition, - "customData": mediaStatus!.customData ?? [:], - "idleReason": getIdleReason(mediaStatus!.idleReason), - "loadingItemId": mediaStatus?.loadingItemID ?? 0, - "media": createMediaInfoObject(mediaStatus!.mediaInformation ?? nil) as NSDictionary, - "mediaSessionId": mediaStatus!.mediaSessionID, - "playbackRate": mediaStatus!.playbackRate, - "playerState": getPlayerState(mediaStatus!.playerState), - "preloadedItemId": mediaStatus!.preloadedItemID, - "sessionId": session.sessionID ?? "", - "volume": [ - "level": mediaStatus!.volume, - "muted": mediaStatus!.isMuted - ] as NSDictionary, - "activeTrackIds": mediaStatus!.activeTrackIDs ?? [] - ] - } - - static func createMediaInfoObject(_ mediaInfo: GCKMediaInformation?) -> NSDictionary { - if mediaInfo == nil { - return [:] - } - - return [ - "contentId": mediaInfo!.contentID ?? "", - "contentType": mediaInfo!.contentType, - "customData": mediaInfo!.customData ?? [:], - "duration": mediaInfo!.streamDuration, - "streamType": getStreamType(mediaInfo!.streamType), - "tracks": getMediaTracks(mediaInfo!.mediaTracks) as NSArray, - "textTrackSytle": getTextTrackStyle(mediaInfo!.textTrackStyle) as NSDictionary - ] - } - - static func getMediaTracks(_ mediaTracks:[GCKMediaTrack]?) -> [NSDictionary] { - var tracks = [NSDictionary]() - - if mediaTracks == nil { - return tracks - } - - for mediaTrack in mediaTracks! { - let track = [ - "trackId": mediaTrack.identifier, - "customData": mediaTrack.customData, - "language": mediaTrack.languageCode, - "name": mediaTrack.name, - "subtype": getTextTrackSubtype(mediaTrack.textSubtype), - "trackContentId": mediaTrack.contentIdentifier, - "trackContentType": mediaTrack.contentType, - "type": getTrackType(mediaTrack.type) - ] - - tracks.append(track as NSDictionary) - } - - return tracks - } - - static func getTextTrackStyle(_ textTrackStyle: GCKMediaTextTrackStyle?) -> NSDictionary { - if textTrackStyle == nil { - return [:] - } - - return [ - "backgroundColor": textTrackStyle!.backgroundColor?.cssString(), - "customData": textTrackStyle!.customData, - "edgeColor": textTrackStyle!.edgeColor?.cssString(), - "edgeType": getEdgeType(textTrackStyle!.edgeType), - "fontFamily": textTrackStyle!.fontFamily, - "fontGenericFamily": getFontGenericFamily(textTrackStyle!.fontGenericFamily), - "fontScale": textTrackStyle!.fontScale, - "fontStyle": getFontStyle(textTrackStyle!.fontStyle), - "foregroundColor": textTrackStyle!.foregroundColor?.cssString(), - "windowColor": textTrackStyle!.windowColor?.cssString(), - "windowRoundedCornerRadius": textTrackStyle!.windowRoundedCornerRadius, - "windowType": getWindowType(textTrackStyle!.windowType) - ] - } - - static func getEdgeType(_ edgeType: GCKMediaTextTrackStyleEdgeType) -> String { - switch edgeType { - case GCKMediaTextTrackStyleEdgeType.depressed: - return "DEPRESSED" - case GCKMediaTextTrackStyleEdgeType.dropShadow: - return "DROP_SHADOW" - case GCKMediaTextTrackStyleEdgeType.outline: - return "OUTLINE" - case GCKMediaTextTrackStyleEdgeType.raised: - return "RAISED" - default: - return "NONE" - } - } - - static func getFontGenericFamily(_ fontGenericFamily: GCKMediaTextTrackStyleFontGenericFamily) -> String { - switch fontGenericFamily { - case GCKMediaTextTrackStyleFontGenericFamily.cursive: - return "CURSIVE" - case GCKMediaTextTrackStyleFontGenericFamily.monospacedSansSerif: - return "MONOSPACED_SANS_SERIF" - case GCKMediaTextTrackStyleFontGenericFamily.monospacedSerif: - return "MONOSPACED_SERIF" - case GCKMediaTextTrackStyleFontGenericFamily.sansSerif: - return "SANS_SERIF" - case GCKMediaTextTrackStyleFontGenericFamily.serif: - return "SERIF" - case GCKMediaTextTrackStyleFontGenericFamily.smallCapitals: - return "SMALL_CAPITALS" - default: - return "SERIF" - } - } - - static func getFontStyle(_ fontStyle: GCKMediaTextTrackStyleFontStyle) -> String { - switch fontStyle { - case GCKMediaTextTrackStyleFontStyle.normal: - return "NORMAL" - case GCKMediaTextTrackStyleFontStyle.bold: - return "BOLD" - case GCKMediaTextTrackStyleFontStyle.boldItalic: - return "BOLD_ITALIC" - case GCKMediaTextTrackStyleFontStyle.italic: - return "ITALIC" - default: - return "NORMAL" - } - } - - static func getWindowType(_ windowType: GCKMediaTextTrackStyleWindowType) -> String { - switch windowType { - case GCKMediaTextTrackStyleWindowType.normal: - return "NORMAL" - case GCKMediaTextTrackStyleWindowType.roundedCorners: - return "ROUNDED_CORNERS" - default: - return "NONE" - } - } - - static func getTrackType(_ trackType: GCKMediaTrackType) -> String? { - switch trackType { - case GCKMediaTrackType.audio: - return "AUDIO"; - case GCKMediaTrackType.text: - return "TEXT"; - case GCKMediaTrackType.video: - return "VIDEO"; - default: - return nil; - } - } - - static func getTextTrackSubtype(_ textSubtype: GCKMediaTextTrackSubtype) -> String? { - switch textSubtype { - case GCKMediaTextTrackSubtype.captions: - return "CAPTIONS"; - case GCKMediaTextTrackSubtype.chapters: - return "CHAPTERS"; - case GCKMediaTextTrackSubtype.descriptions: - return "DESCRIPTIONS"; - case GCKMediaTextTrackSubtype.metadata: - return "METADATA"; - case GCKMediaTextTrackSubtype.subtitles: - return "SUBTITLES"; - default: - return nil; - } - } - - static func getIdleReason(_ reason: GCKMediaPlayerIdleReason) -> String { - switch reason { - case GCKMediaPlayerIdleReason.cancelled: - return "canceled" - case GCKMediaPlayerIdleReason.error: - return "error" - case GCKMediaPlayerIdleReason.finished: - return "finished" - case GCKMediaPlayerIdleReason.interrupted: - return "interrupted" - default: - return "none" - } - } - - static func getPlayerState(_ playerState: GCKMediaPlayerState) -> String { - switch playerState { - case GCKMediaPlayerState.buffering: - return "BUFFERING" - case GCKMediaPlayerState.idle: - return "IDLE" - case GCKMediaPlayerState.paused: - return "PAUSED" - case GCKMediaPlayerState.playing: - return "PLAYING" - default: - return "UNKNOWN" - } - } - - static func getStreamType(_ streamType: GCKMediaStreamType) -> String { - switch streamType { - case GCKMediaStreamType.buffered: - return "buffered"; - case GCKMediaStreamType.live: - return "live"; - case GCKMediaStreamType.none: - return "other"; - default: - return "unknown"; - } - } - - static func parseEdgeType(_ edgeType: String) -> GCKMediaTextTrackStyleEdgeType { - switch edgeType { - case "DEPRESSED": - return GCKMediaTextTrackStyleEdgeType.depressed - case "DROP_SHADOW": - return GCKMediaTextTrackStyleEdgeType.dropShadow - case "OUTLINE": - return GCKMediaTextTrackStyleEdgeType.outline - case "RAISED": - return GCKMediaTextTrackStyleEdgeType.raised - default: - return GCKMediaTextTrackStyleEdgeType.none - } - } - - static func parseFontGenericFamily(_ fontGenericFamily: String) -> GCKMediaTextTrackStyleFontGenericFamily { - switch fontGenericFamily { - case "CURSIVE": - return GCKMediaTextTrackStyleFontGenericFamily.cursive - case "MONOSPACED_SANS_SERIF": - return GCKMediaTextTrackStyleFontGenericFamily.monospacedSansSerif - case "MONOSPACED_SERIF": - return GCKMediaTextTrackStyleFontGenericFamily.monospacedSerif - case "SANS_SERIF": - return GCKMediaTextTrackStyleFontGenericFamily.sansSerif - case "SERIF": - return GCKMediaTextTrackStyleFontGenericFamily.serif - case "SMALL_CAPITALS": - return GCKMediaTextTrackStyleFontGenericFamily.smallCapitals - default: - return GCKMediaTextTrackStyleFontGenericFamily.serif - } - } - - static func parseFontStyle(_ fontStyle: String) -> GCKMediaTextTrackStyleFontStyle { - switch fontStyle { - case "NORMAL": - return GCKMediaTextTrackStyleFontStyle.normal - case "BOLD": - return GCKMediaTextTrackStyleFontStyle.bold - case "BOLD_ITALIC": - return GCKMediaTextTrackStyleFontStyle.boldItalic - case "ITALIC": - return GCKMediaTextTrackStyleFontStyle.italic - default: - return GCKMediaTextTrackStyleFontStyle.normal - } - } - - static func parseWindowType(_ windowType: String) -> GCKMediaTextTrackStyleWindowType { - switch windowType { - case "NORMAL": - return GCKMediaTextTrackStyleWindowType.normal - case "ROUNDED_CORNERS": - return GCKMediaTextTrackStyleWindowType.roundedCorners - default: - return GCKMediaTextTrackStyleWindowType.unknown - } - } - - static func parseResumeState(_ resumeState: String) -> GCKMediaResumeState { - switch resumeState { - case "PLAYBACK_PAUSE": - return GCKMediaResumeState.pause - case "PLAYBACK_START": - return GCKMediaResumeState.play - default: - return GCKMediaResumeState.unchanged - } - } - - static func parseMediaMetadataType(_ metadataType: Int) -> GCKMediaMetadataType { - switch metadataType { - case 0: - return GCKMediaMetadataType.generic - case 1: - return GCKMediaMetadataType.tvShow - case 2: - return GCKMediaMetadataType.movie - case 3: - return GCKMediaMetadataType.musicTrack - case 4: - return GCKMediaMetadataType.photo - default: - return GCKMediaMetadataType.generic - } - } - - static func convertDictToJsonString(_ dict: NSDictionary) -> String { - let json = try? JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions.prettyPrinted) - - return String(data: json ?? Data(), encoding: String.Encoding.utf8) ?? "" - } - -} diff --git a/src/ios/Chromecast.h b/src/ios/Chromecast.h new file mode 100644 index 0000000..79aee37 --- /dev/null +++ b/src/ios/Chromecast.h @@ -0,0 +1,49 @@ +// +// Chromecast.h +// ChromeCast +// +// Created by mac on 2019/9/30. +// + +#import +#import +#import +#import "ChromecastSession.h" +#import "CastUtilities.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface Chromecast : CDVPlugin + +@property (nonatomic, strong) NSMutableArray* devicesAvailable; +@property (nonatomic, strong) ChromecastSession* currentSession; +@property (nonatomic, strong) CDVInvokedUrlCommand* eventCommand; +@property (nonatomic, strong) CDVInvokedUrlCommand* scanCommand; + +- (void)setup:(CDVInvokedUrlCommand*) command; +- (void)emitAllRoutes:(CDVInvokedUrlCommand*) command; +- (void)initialize:(CDVInvokedUrlCommand*)command; +- (BOOL)startRouteScan:(CDVInvokedUrlCommand*)command; +- (BOOL)stopRouteScan:(CDVInvokedUrlCommand*)command; +- (void)checkReceiverAvailable; +- (void)requestSession:(CDVInvokedUrlCommand*) command; +- (void)setReceiverVolumeLevel:(CDVInvokedUrlCommand*) command; +- (void)queueLoad:(CDVInvokedUrlCommand *)command; +- (void)setMediaVolume:(CDVInvokedUrlCommand*) command; +- (void)setReceiverMuted:(CDVInvokedUrlCommand*) command; +- (void)sessionStop:(CDVInvokedUrlCommand*)command; +- (void)sessionLeave:(CDVInvokedUrlCommand*) command; +- (void)loadMedia:(CDVInvokedUrlCommand*) command; +- (void)addMessageListener:(CDVInvokedUrlCommand*)command; +- (void)sendMessage:(CDVInvokedUrlCommand*) command; +- (void)mediaPlay:(CDVInvokedUrlCommand*)command; +- (void)mediaPause:(CDVInvokedUrlCommand*)command; +- (void)mediaSeek:(CDVInvokedUrlCommand*)command; +- (void)mediaStop:(CDVInvokedUrlCommand*)command; +- (void)mediaEditTracksInfo:(CDVInvokedUrlCommand*)command; +- (void)selectRoute:(CDVInvokedUrlCommand*)command; +- (void)sendEvent:(NSString*)eventName args:(NSArray*)args; +- (void)queueJumpToItem:(CDVInvokedUrlCommand *)command; +@end + +NS_ASSUME_NONNULL_END diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m new file mode 100644 index 0000000..81bd310 --- /dev/null +++ b/src/ios/Chromecast.m @@ -0,0 +1,520 @@ +// +// Chromecast.m +// ChromeCast +// +// Created by mac on 2019/9/30. +// + +#import "Chromecast.h" +#import "CastUtilities.h" + +#define IDIOM UI_USER_INTERFACE_IDIOM() +#define IPAD UIUserInterfaceIdiomPad + +@interface Chromecast() +@property (nonatomic, strong) CDVInvokedUrlCommand *sessionCommand; +@end + +@implementation Chromecast + +- (void)pluginInitialize { + [super pluginInitialize]; +// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onCastStateChanged:) name:kGCKCastStateDidChangeNotification object:nil]; +} + +- (void)sendJavascript:(NSString*)jsCommand { + [self.webViewEngine evaluateJavaScript:jsCommand completionHandler:nil]; +} + +- (void)log:(NSString*)s { + [self sendJavascript:[NSString stringWithFormat: @"console.log('Chromecast-iOS: ', %@)",s]]; +} + +- (void)setup:(CDVInvokedUrlCommand*) command { + self.eventCommand = command; + [self stopRouteScanForSetup:self.scanCommand]; + [self sendEvent:@"SETUP" args:@[]]; +} + +- (void)emitAllRoutes:(CDVInvokedUrlCommand*) command { + // No arguments. It's only implemented to satisfy plugin's JS API. + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +-(void) initialize:(CDVInvokedUrlCommand*)command { + + if (self.devicesAvailable == nil) { + self.devicesAvailable = [[NSMutableArray alloc] init]; + } + + NSString* appId = kGCKDefaultMediaReceiverApplicationID; + if (command.arguments[0] != nil) { + appId = command.arguments[0]; + } + GCKDiscoveryCriteria* criteria = [[GCKDiscoveryCriteria alloc] initWithApplicationID:appId]; + GCKCastOptions* options = [[GCKCastOptions alloc] initWithDiscoveryCriteria:criteria]; + options.physicalVolumeButtonsWillControlDeviceVolume = YES; + options.disableDiscoveryAutostart = NO; + [GCKCastContext setSharedInstanceWithOptions:options]; + [GCKCastContext.sharedInstance.discoveryManager addListener:self]; + + //For debugging purpose + GCKLogger.sharedInstance.delegate = self; +// [self log:[NSString stringWithFormat:@"API Initialized with appID %@", appId]]; + + if ([GCKCastContext sharedInstance].sessionManager.currentCastSession != nil) { + [self onSessionRejoin:[CastUtilities createSessionObject:[GCKCastContext sharedInstance].sessionManager.currentCastSession]]; + } + +// ChromecastSession *session = [[ChromecastSession alloc] init]; + [[GCKCastContext sharedInstance].sessionManager addListener:self]; + + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + [self checkReceiverAvailable]; + +} + +- (BOOL)stopRouteScanForSetup:(CDVInvokedUrlCommand*)command { + + if (self.scanCommand != nil) { + [self sendError:@"cancel" message:@"Scan stopped because setup triggered." command:self.scanCommand]; + self.scanCommand = nil; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]]; + [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } else { + [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; + if (command != nil) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]]; + [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } + } + + return YES; +} + +- (BOOL)stopRouteScan:(CDVInvokedUrlCommand*)command { + + if (self.scanCommand != nil) { + [self sendError:@"cancel" message:@"Scan stopped." command:self.scanCommand]; + self.scanCommand = nil; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]]; + [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } else { + [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; + if (command != nil) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]];  + [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } + } + + return YES; +} + +-(BOOL) startRouteScan:(CDVInvokedUrlCommand*)command { + if (self.scanCommand != nil) { + [self sendError:@"cancel" message:@"Started a new route scan before stopping previous one." command:self.scanCommand]; + } + self.scanCommand = command; + [self startRotueScanWithTimer:0 completion:nil]; + return YES; +} +- (void)startRotueScanWithTimer:(NSTimeInterval)timer completion:(void(^)(void))completion { + if (timer != 0) { + if (completion != nil) { + if ([GCKCastContext sharedInstance].discoveryManager.discoveryActive) { + [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; + [self sendReceiverData]; + } else { + [NSTimer scheduledTimerWithTimeInterval:timer repeats:NO block:^(NSTimer * _Nonnull timer) { + completion(); + }]; + + } + } + } else { + if ([GCKCastContext sharedInstance].discoveryManager.discoveryActive) { + [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; + [self sendReceiverData]; + } else { + [[GCKCastContext sharedInstance].discoveryManager startDiscovery]; + [self sendReceiverData]; + } + } +} + +- (void)checkReceiverAvailable { + if ([GCKCastContext sharedInstance].castState != GCKCastStateNoDevicesAvailable) { + [self sendEvent:@"RECEIVER_LISTENER" args:@[@(true)]]; + } else { + [self sendEvent:@"RECEIVER_LISTENER" args:@[@(false)]]; + } +} + +- (void)sendReceiverData { + if (self.scanCommand == nil) { + return; + } + GCKSessionManager* sessionManager = GCKCastContext.sharedInstance.sessionManager; +// [sessionManager startSessionWithDevice:<#(nonnull GCKDevice *)#>] + if (self.devicesAvailable.count > 0 || sessionManager.currentSession != nil) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:[CastUtilities createDeviceObject:self.devicesAvailable]]; + [pluginResult setKeepCallback:@(true)]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.scanCommand.callbackId]; + }else { + [self stopRouteScan:self.scanCommand]; + } +} +- (void)requestSession:(CDVInvokedUrlCommand*) command { + UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Cast to" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; + for (GCKDevice* device in self.devicesAvailable) { + [alert addAction:[UIAlertAction actionWithTitle:device.friendlyName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + self.currentSession = [[ChromecastSession alloc] initWithDevice:device cordovaDelegate:self.commandDelegate initialCommand:command]; + [self.currentSession add:self]; + }]]; + } + [alert addAction:[UIAlertAction actionWithTitle:@"Stop Casting" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + NSLog(@"Stop Casting"); + self.sessionCommand = command; + self.currentSession.sessionStatus = @"stopped"; + [[GCKCastContext sharedInstance].sessionManager endSession]; + + }]]; + [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { + NSLog(@"Canceld"); + [self.currentSession.remoteMediaClient stop]; + [self sendError:@"cancel" message:@"Casting is stopped." command:command]; + }]]; + if (IDIOM == IPAD) { + alert.popoverPresentationController.sourceView = self.webView; + CGRect frame = CGRectMake(self.webView.frame.size.width/2, self.webView.frame.size.height, self.webView.bounds.size.width/2, self.webView.bounds.size.height); + alert.popoverPresentationController.sourceRect = frame; + } + [self.viewController presentViewController:alert animated:YES completion:nil]; +} + +- (void)queueLoad:(CDVInvokedUrlCommand *)command { + NSDictionary *request = command.arguments[0]; + NSArray *items = request[@"items"]; + NSInteger startIndex = [request[@"startIndex"] integerValue]; + NSString *repeadModeString = request[@"repeatMode"]; + GCKMediaRepeatMode repeatMode = GCKMediaRepeatModeAll; + if ([repeadModeString isEqualToString:@"REPEAT_OFF"]) { + repeatMode = GCKMediaRepeatModeOff; + } + else if ([repeadModeString isEqualToString:@"REPEAT_ALL"]) { + repeatMode = GCKMediaRepeatModeAll; + } + else if ([repeadModeString isEqualToString:@"REPEAT_SINGLE"]) { + repeatMode = GCKMediaRepeatModeSingle; + } + else if ([repeadModeString isEqualToString:@"REPEAT_ALL_AND_SHUFFLE"]) { + repeatMode = GCKMediaRepeatModeAllAndShuffle; + } + + //GCKMediaInformation* mediaInfo = [CastUtilities buildMediaInformation:contentId customData:customData contentType:contentType duration:duration streamType:streamType textTrackStyle:textTrackStyle metaData:metadata]; + + NSMutableArray *queueItems = [[NSMutableArray alloc] init]; + + for (NSDictionary *item in items) { + GCKMediaQueueItemBuilder *queueItemBuilder = [[GCKMediaQueueItemBuilder alloc] init]; + queueItemBuilder.activeTrackIDs = item[@"activeTrackIds"]; + queueItemBuilder.autoplay = [item[@"autoplay"] boolValue]; + queueItemBuilder.customData = item[@"customData"]; + NSDictionary *media = item[@"media"]; + queueItemBuilder.startTime = [item[@"startTime"] doubleValue]; + queueItemBuilder.preloadTime = [item[@"preloadTime"] doubleValue]; + double duration = media[@"duration"] == [NSNull null] ? 0 : [media[@"duration"] doubleValue]; + + GCKMediaInformation *mediaInformation = [CastUtilities buildMediaInformationForQueueItem:media[@"contentId"] customData:media[@"customData"] contentType:media[@"contentType"] duration:duration startTime:0 streamType:media[@"streamType"] metaData:media[@"metadata"]]; + queueItemBuilder.mediaInformation = mediaInformation; + [queueItems addObject: [queueItemBuilder build]]; + } + [self.currentSession queueLoadItemsWithCommand:command queueItems:queueItems startIndex:startIndex repeatMode:repeatMode]; +} + +- (void)queueJumpToItem:(CDVInvokedUrlCommand *)command { + NSUInteger itemId = [command.arguments[0] unsignedIntegerValue]; + [self.currentSession queueJumpToItemWithCommand:command itemId:itemId]; +} + +- (void)setMediaVolume:(CDVInvokedUrlCommand*) command { + if (command.arguments[1] == [NSNull null]) { + double newLevel = 1.0; + if (command.arguments[0]) { + newLevel = [command.arguments[0] doubleValue]; + } else { + newLevel = 1.0; + } + [self.currentSession setMediaVolumeWithCommand:command newVolumeLevel:newLevel]; + } + else if (command.arguments[0] == [NSNull null]) { + BOOL muted = [command.arguments[1] boolValue]; + [self.currentSession setMediaMutedWIthCommand:command muted:muted]; + } + else { + double newLevel = 1.0; + if (command.arguments[0]) { + newLevel = [command.arguments[0] doubleValue]; + } else { + newLevel = 1.0; + } + BOOL muted = [command.arguments[1] boolValue]; + [self.currentSession setMediaMutedAndVolumeWIthCommand:command muted:muted nvewLevel:newLevel]; + } +// [self.currentSession setReceiverVolumeLevelWithCommand:command newLevel:newLevel]; +} + +- (void)setReceiverVolumeLevel:(CDVInvokedUrlCommand*) command { + double newLevel = 1.0; + if (command.arguments[0]) { + newLevel = [command.arguments[0] doubleValue]; + } else { + newLevel = 1.0; + } + [self.currentSession setReceiverVolumeLevelWithCommand:command newLevel:newLevel]; +} + +- (void)setReceiverMuted:(CDVInvokedUrlCommand*) command { + BOOL muted = NO; + if (command.arguments[0]) { + muted = [command.arguments[0] boolValue]; + } + [self.currentSession setReceiverMutedWithCommand:command muted:muted]; +} + +- (void)sessionStop:(CDVInvokedUrlCommand*)command { + self.currentSession.sessionStatus = @"stopped"; + BOOL result = [[GCKCastContext sharedInstance].sessionManager endSessionAndStopCasting:true]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:result]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +- (void)sessionLeave:(CDVInvokedUrlCommand*) command { + self.currentSession.sessionStatus = @"disconnected"; + BOOL result = [[GCKCastContext sharedInstance].sessionManager endSession]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:result]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +- (void)loadMedia:(CDVInvokedUrlCommand*) command { + NSString* contentId = command.arguments[0]; + NSObject* customData = command.arguments[1]; + NSString* contentType = command.arguments[2]; + double duration = [command.arguments[3] doubleValue]; + NSString* streamType = command.arguments[4]; + BOOL autoplay = [command.arguments[5] boolValue]; + double currentTime = [command.arguments[6] doubleValue]; + NSDictionary* metadata = command.arguments[7]; + NSDictionary* textTrackStyle = command.arguments[8]; + GCKMediaInformation* mediaInfo = [CastUtilities buildMediaInformation:contentId customData:customData contentType:contentType duration:duration streamType:streamType textTrackStyle:textTrackStyle metaData:metadata]; + [self.currentSession loadMediaWithCommand:command mediaInfo:mediaInfo autoPlay:autoplay currentTime:currentTime]; +} + +- (void)addMessageListener:(CDVInvokedUrlCommand*)command { + NSString* namespace = command.arguments[0]; + [self.currentSession createMessageChannelWithCommand:command namespace:namespace]; +} + +- (void)sendMessage:(CDVInvokedUrlCommand*) command { + NSString* namespace = command.arguments[0]; + NSString* message = command.arguments[1]; + + [self.currentSession sendMessageWithCommand:command namespace:namespace message:message]; +} + +- (void)mediaPlay:(CDVInvokedUrlCommand*)command { + [self.currentSession mediaPlayWithCommand:command]; +} + +- (void)mediaPause:(CDVInvokedUrlCommand*)command { + [self.currentSession mediaPauseWithCommand:command]; +} + +- (void)mediaSeek:(CDVInvokedUrlCommand*)command { + int currentTime = [command.arguments[0] doubleValue]; + NSString* resumeState = command.arguments[1]; + GCKMediaResumeState resumeStateObj = [CastUtilities parseResumeState:resumeState]; + [self.currentSession mediaSeekWithCommand:command position:currentTime resumeState:resumeStateObj]; +} + +- (void)mediaStop:(CDVInvokedUrlCommand*)command { + [self.currentSession mediaStopWithCommand:command]; +} + +- (void)mediaEditTracksInfo:(CDVInvokedUrlCommand*)command { + NSArray* activeTrackIds = command.arguments[0]; + NSData* textTrackStyle = command.arguments[1]; + + GCKMediaTextTrackStyle* textTrackStyleObject = [CastUtilities buildTextTrackStyle:textTrackStyle]; + [self.currentSession setActiveTracksWithCommand:command activeTrackIds:activeTrackIds textTrackStyle:textTrackStyleObject]; +} + +- (void)selectRoute:(CDVInvokedUrlCommand*)command { + NSString* routeID = command.arguments[0]; + GCKDevice* device = [[GCKCastContext sharedInstance].discoveryManager deviceWithUniqueID:routeID]; + int retries = 0; + + //testing purpose + if ([routeID isEqualToString:@""]) { + if ([GCKCastContext sharedInstance].sessionManager.currentCastSession != nil || [GCKCastContext sharedInstance].sessionManager.connectionState == GCKConnectionStateConnected) { + [self sendError:@"session_error" message:@"Leave or stop current session before attempting to join new session." command:command]; + } + } + if (device == nil) { + [self startRotueScanWithTimer:1 completion:^{ + [self sendError:@"timeout" message:[NSString stringWithFormat:@"Failed to join route (%@) after 15s and %d tries.",routeID,retries + 1] command:command]; + }]; + } else { + if ([GCKCastContext sharedInstance].sessionManager.currentCastSession != nil || [GCKCastContext sharedInstance].sessionManager.connectionState == GCKConnectionStateConnected) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: [CastUtilities createSessionObject:[GCKCastContext sharedInstance].sessionManager.currentCastSession] ]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } else { + self.currentSession = [[ChromecastSession alloc] initWithDevice:device cordovaDelegate:self.commandDelegate initialCommand:command]; + [self.currentSession add:self]; + } + } + +} + +#pragma GCKLoggerDelegate +- (void)logMessage:(NSString *)message atLevel:(GCKLoggerLevel)level fromFunction:(NSString *)function location:(NSString *)location { +// [self log:[NSString stringWithFormat:@"GCKLogger = %@, %ld, %@, %@", message,(long)level,function,location]]; +} + +#pragma GCKDiscoveryManagerListener +- (NSString*)deviceToJson:(GCKDevice*) device { + NSString* deviceName = @""; + if (device.friendlyName != nil) { + deviceName = device.friendlyName; + } else { + deviceName = device.deviceID; + } + NSDictionary* deviceJson = @{ + @"name" : deviceName, + @"id" : device.uniqueID + }; + return [CastUtilities convertDictToJsonString:deviceJson]; +} + +- (void)didInsertDevice:(GCKDevice *)device atIndex:(NSUInteger)index { + NSString* deviceName = @""; + if (device.friendlyName != nil) { + deviceName = device.friendlyName; + } else { + deviceName = device.deviceID; + } + + [self.devicesAvailable insertObject:device atIndex:index]; + [self checkReceiverAvailable]; +} + +- (void)didUpdateDevice:(GCKDevice *)device atIndex:(NSUInteger)index andMoveToIndex:(NSUInteger)newIndex { + if (self.devicesAvailable.count != 0) { + [self.devicesAvailable removeObjectAtIndex:index]; + } + [self.devicesAvailable insertObject:device atIndex:newIndex]; + [self checkReceiverAvailable]; +} + +- (void)didRemoveDevice:(GCKDevice *)device atIndex:(NSUInteger)index { + if (self.devicesAvailable.count != 0) { + [self.devicesAvailable removeObjectAtIndex:index]; + } + + [self checkReceiverAvailable]; +} + +#pragma CastSessionListener + + + +- (void)onMediaLoaded:(NSDictionary *)media { + [self sendEvent:@"MEDIA_LOAD" args:@[media]]; +} + +- (void)onMediaUpdated:(NSDictionary *)media isAlive:(BOOL)isAlive { + [self sendEvent:@"MEDIA_UPDATE" args:@[media]]; +} + + +- (void)onSessionRejoin:(NSDictionary*)session { + [self sendEvent:@"SESSION_LISTENER" args:@[session]]; +} + +- (void)onSessionUpdated:(NSDictionary *)session isAlive:(BOOL)isAlive { + [self sendEvent:@"SESSION_UPDATE" args:@[session]]; +} + +- (void)onMessageReceived:(NSDictionary *)session namespace:(NSString *)namespace message:(NSString *)message { + [self sendEvent:@"RECEIVER_MESSAGE" args:@[namespace,message]]; +} + +- (void)onSessionEnd:(NSDictionary *)session { + + [self sendEvent:@"SESSION_UPDATE" args:@[session]]; +} +- (void)onCastStateChanged:(NSNotification*)notification { + + GCKCastState castState = [notification.userInfo[kGCKNotificationKeyCastState] intValue]; + if (castState == GCKCastStateNoDevicesAvailable) { + [self sendEvent:@"RECEIVER_LISTENER" args:@[@(false)]]; + } else { + [self sendEvent:@"RECEIVER_LISTENER" args:@[@(true)]]; + } +} + +- (void)sendEvent:(NSString *)eventName args:(NSArray *)args{ + if (self.eventCommand == nil) { + return; + } + NSMutableArray* argArray = [[NSMutableArray alloc] initWithArray:@[eventName]]; + [argArray addObject:args]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:argArray]; + [pluginResult setKeepCallback:@(true)]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.eventCommand.callbackId]; +} + +- (void)sendScan:(NSArray *)args{ + if (self.scanCommand == nil) { + return; + } + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:args]; + [pluginResult setKeepCallback:@(YES)]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.scanCommand.callbackId]; +} + + +- (void)sendError:(NSString *)code message:(NSString *)message command:(CDVInvokedUrlCommand*)command{ + + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[CastUtilities createError:code message:message]]; + + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +- (void)sessionManager:(GCKSessionManager *)sessionManager didResumeCastSession:(GCKCastSession *)session { + [self onSessionRejoin:[CastUtilities createSessionObject:session]]; +} + +- (void)sessionManager:(GCKSessionManager *)sessionManager didResumeSession:(GCKSession *)session { + [self onSessionRejoin:[CastUtilities createSessionObject:session]]; +} + +- (void)sessionManager:(GCKSessionManager *)sessionManager didEndSession:(GCKSession *)session withError:(NSError *)error { + self.currentSession.currentSession = nil; + + if (error != nil) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.debugDescription]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.sessionCommand]; + } + [self.currentSession.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"stopped"] isAlive:false]; + [self sendError:@"cancel" message:@"Session is stopped." command:self.sessionCommand]; +} + + +@end diff --git a/src/ios/Chromecast.swift b/src/ios/Chromecast.swift deleted file mode 100644 index d102997..0000000 --- a/src/ios/Chromecast.swift +++ /dev/null @@ -1,304 +0,0 @@ -import GoogleCast - -@objc(Chromecast) class Chromecast : CDVPlugin { - var devicesAvailable: [GCKDevice] = [] - var currentSession: ChromecastSession? - - func sendJavascript(jsCommand: String) { - self.webViewEngine.evaluateJavaScript(jsCommand, completionHandler: nil) - } - - func log(_ s: String) { - self.sendJavascript(jsCommand: "console.log(\">>Chromecast-iOS: \(s)\")") - } - - @objc(setup:) - func setup(command: CDVInvokedUrlCommand) { - // No arguments - - let pluginResult = CDVPluginResult( - status: CDVCommandStatus_OK - ) - - self.commandDelegate!.send( - pluginResult, - callbackId: command.callbackId - ) - } - - @objc(emitAllRoutes:) - func emitAllRoutes(command: CDVInvokedUrlCommand) { - // No arguments. It's only implemented to satisfy plugin's JS API. - - let pluginResult = CDVPluginResult( - status: CDVCommandStatus_OK - ) - - self.commandDelegate!.send( - pluginResult, - callbackId: command.callbackId - ) - } - - @objc(initialize:) - func initialize(command: CDVInvokedUrlCommand) { - self.devicesAvailable = [GCKDevice]() - - let appId = command.arguments[0] as? String ?? kGCKDefaultMediaReceiverApplicationID - - let criteria = GCKDiscoveryCriteria(applicationID: appId) - let options = GCKCastOptions(discoveryCriteria: criteria) - options.physicalVolumeButtonsWillControlDeviceVolume = true - options.disableDiscoveryAutostart = false - GCKCastContext.setSharedInstanceWith(options) - - GCKCastContext.sharedInstance().discoveryManager.add(self) - - // For debugging purpose - GCKLogger.sharedInstance().delegate = self - - self.log("API Initialized with appID \(appId)") - - self.checkReceiverAvailable() - - let pluginResult = CDVPluginResult( - status: CDVCommandStatus_OK - ) - - self.commandDelegate!.send( - pluginResult, - callbackId: command.callbackId - ) - } - - @objc(checkReceiverAvailable:) - func checkReceiverAvailable() { - let sessionManager = GCKCastContext.sharedInstance().sessionManager - - if self.devicesAvailable.count > 0 || (sessionManager.currentSession != nil) { - self.sendJavascript(jsCommand: "chrome.cast._.receiverAvailable()") - } else { - self.sendJavascript(jsCommand: "chrome.cast._.receiverUnavailable()") - } - - } - - @objc(requestSession:) - func requestSession(command: CDVInvokedUrlCommand) { - let alert = UIAlertController(title: "Cast to", message: nil, preferredStyle: .actionSheet) - - for device in self.devicesAvailable { - alert.addAction( - UIAlertAction(title: device.friendlyName , style: UIAlertAction.Style.default, handler: {(_) in - self.currentSession = ChromecastSession(device, cordovaDelegate: self.commandDelegate, initialCommand: command) - self.currentSession?.add(self) - }) - ) - } - - alert.addAction( - UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil) - ) - - self.viewController?.present(alert, animated: true, completion: nil) - } - - @objc(setMediaVolume:) - func setMediaVolume(command: CDVInvokedUrlCommand) { - let newLevel = command.arguments[0] as? Double ?? 1.0 - - self.currentSession?.setReceiverVolumeLevel(command, newLevel: Float(newLevel)) - } - - @objc(setMediaMuted:) - func setMediaMuted(command: CDVInvokedUrlCommand) { - let muted = command.arguments[0] as? Bool ?? false - - self.currentSession?.setReceiverMuted(command, muted: muted) - } - - @objc(sessionStop:) - func sessionStop(command: CDVInvokedUrlCommand) { - let result = GCKCastContext.sharedInstance().sessionManager.endSessionAndStopCasting(true) - - let pluginResult = CDVPluginResult( - status: CDVCommandStatus_OK, - messageAs: result - ) - - self.commandDelegate!.send( - pluginResult, - callbackId: command.callbackId - ) - } - - @objc(sessionLeave:) - func sessionLeave(command: CDVInvokedUrlCommand) { - let result = GCKCastContext.sharedInstance().sessionManager.endSession() - - let pluginResult = CDVPluginResult( - status: CDVCommandStatus_OK, - messageAs: result - ) - - self.commandDelegate!.send( - pluginResult, - callbackId: command.callbackId - ) - } - - @objc(loadMedia:) - func loadMedia(command: CDVInvokedUrlCommand) { - let contentId = command.arguments[0] as? String ?? "" - let customData = command.arguments[1] - let contentType = command.arguments[2] as? String ?? "" - let duration = command.arguments[3] as? Double ?? 0.0 - let streamType = command.arguments[4] as? String ?? "" - let autoplay = command.arguments[5] as? Bool ?? true - let currentTime = command.arguments[6] as? Double ?? 0 - let metadata = command.arguments[7] as? Data ?? Data() - let textTrackStyle = command.arguments[8] as? Data ?? Data() - - let mediaInfo = CastUtilities.buildMediaInformation(contentUrl: contentId, customData: customData, contentType: contentType, duration: duration, streamType: streamType, textTrackStyle: textTrackStyle, metadata: metadata) - - self.currentSession?.loadMedia(command, mediaInfo: mediaInfo, autoPlay: autoplay, currentTime: currentTime) - } - - @objc(addMessageListener:) - func addMessageListener(command: CDVInvokedUrlCommand) { - let namespace = command.arguments[0] as? String ?? "" - - self.currentSession?.createMessageChannel(command, namespace: namespace) - } - - @objc(sendMessage:) - func sendMessage(command: CDVInvokedUrlCommand) { - let namespace = command.arguments[0] as? String ?? "" - let message = command.arguments[1] as? String ?? "" - - self.currentSession?.sendMessage(command, namespace: namespace, message: message) - } - - @objc(mediaPlay:) - func mediaPlay(command: CDVInvokedUrlCommand) { - self.currentSession?.mediaPlay(command) - } - - @objc(mediaPause:) - func mediaPause(command: CDVInvokedUrlCommand) { - self.currentSession?.mediaPause(command) - } - - @objc(mediaSeek:) - func mediaSeek(command: CDVInvokedUrlCommand) { - let currentTime = command.arguments[0] as? Int ?? 0 - let resumeState = command.arguments[1] as? String ?? "" - - let resumeStateObj = CastUtilities.parseResumeState(resumeState) - - self.currentSession?.mediaSeek(command, position: TimeInterval(currentTime), resumeState: resumeStateObj) - } - - @objc(mediaStop:) - func mediaStop(command: CDVInvokedUrlCommand) { - self.currentSession?.mediaStop(command) - } - - @objc(mediaEditTracksInfo:) - func mediaEditTracksInfo(command: CDVInvokedUrlCommand) { - let activeTrackIds = command.arguments[0] as? [NSNumber] ?? [NSNumber]() - let textTrackStyle = command.arguments[1] as? Data ?? Data() - - let textTrackStyleObject = CastUtilities.buildTextTrackStyle(textTrackStyle) - self.currentSession?.setActiveTracks(command, activeTrackIds: activeTrackIds, textTrackStyle: textTrackStyleObject) - } - - @objc(selectRoute:) - func selectRoute(command: CDVInvokedUrlCommand) { - let routeID = command.arguments[0] as? String ?? "" - - let device = GCKCastContext.sharedInstance().discoveryManager.device(withUniqueID: routeID) - - if device != nil { - self.currentSession = ChromecastSession(device!, cordovaDelegate: self.commandDelegate, initialCommand: command) - self.currentSession?.add(self) - } else { - let pluginResult = CDVPluginResult( - status: CDVCommandStatus_ERROR, - messageAs: "selectRoute: Invalid Device ID" - ) - self.commandDelegate!.send(pluginResult, callbackId: command.callbackId) - } - } -} - -extension Chromecast: GCKLoggerDelegate { - func logMessage(_ message: String, at level: GCKLoggerLevel, fromFunction function: String, location: String) { - self.log("GCKLogger = \(message), \(level), \(function), \(location)") - } -} - -extension Chromecast : GCKDiscoveryManagerListener { - private func deviceToJson(_ device: GCKDevice) -> String { - let deviceJson = ["name": device.friendlyName ?? device.deviceID, "id": device.uniqueID] as NSDictionary - - return CastUtilities.convertDictToJsonString(deviceJson) - } - - func didInsert(_ device: GCKDevice, at index: UInt) { - self.log("Device discovered = \(device.friendlyName ?? device.deviceID)") - self.devicesAvailable.insert(device, at: Int(index)) - - self.checkReceiverAvailable() - - // Notify JS API of new available device - self.sendJavascript(jsCommand: "chrome.cast._.routeAdded(\(self.deviceToJson(device)));") - } - - func didUpdate(_ device: GCKDevice, at index: UInt, andMoveTo newIndex: UInt) { - self.devicesAvailable.remove(at: Int(index)) - self.devicesAvailable.insert(device, at: Int(newIndex)) - - self.checkReceiverAvailable() - } - - func didRemove(_ device: GCKDevice, at index: UInt) { - self.devicesAvailable.remove(at: Int(index)) - - self.checkReceiverAvailable() - - // Notify JS API of new unavailable device - self.sendJavascript(jsCommand: "chrome.cast._.routeRemoved(\(self.deviceToJson(device)));") - } -} - -extension Chromecast : CastSessionListener { - func onMediaLoaded(_ media: NSDictionary) { - self.sendJavascript(jsCommand: "chrome.cast._.mediaLoaded(true, \(CastUtilities.convertDictToJsonString(media)));") - } - - func onMediaUpdated(_ media: NSDictionary, isAlive: Bool) { - if isAlive { - self.sendJavascript(jsCommand: "chrome.cast._.mediaUpdated(true, \(CastUtilities.convertDictToJsonString(media)));") - } else { - self.sendJavascript(jsCommand: "chrome.cast._.mediaUpdated(false, \(CastUtilities.convertDictToJsonString(media)));") - } - } - - func onSessionUpdated(_ session: NSDictionary, isAlive: Bool) { - if isAlive { - self.sendJavascript(jsCommand: "chrome.cast._.sessionUpdated(true, \(CastUtilities.convertDictToJsonString(session)));") - } else { - self.log("SESSION DESTROY!") - self.sendJavascript(jsCommand: "chrome.cast._.sessionUpdated(false, \(CastUtilities.convertDictToJsonString(session)));") - self.currentSession = nil - } - } - - func onMessageReceived(_ session: NSDictionary, namespace: String, message: String) { - let sessionId = session.value(forKey: "sessionId") as? String ?? "" - let messageFormatted = message.replacingOccurrences(of: "\\", with: "\\\\") - - sendJavascript(jsCommand: "chrome.cast._.onMessage('\(sessionId)', '\(namespace)', '\(messageFormatted)');") - } -} diff --git a/src/ios/ChromecastSession.h b/src/ios/ChromecastSession.h new file mode 100644 index 0000000..9f77b0d --- /dev/null +++ b/src/ios/ChromecastSession.h @@ -0,0 +1,49 @@ +// +// ChromecastSession.h +// ChromeCast +// +// Created by mac on 2019/9/30. +// + +#import +#import +#import +#import "CastRequestDelegate.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface ChromecastSession : NSObject + +@property (nonatomic, retain) id commandDelegate; +@property (nonatomic, retain) CDVInvokedUrlCommand* initialCommand; +@property (nonatomic, retain) GCKCastSession* currentSession; +@property (nonatomic, retain) GCKRemoteMediaClient* remoteMediaClient; +@property (nonatomic, retain) GCKCastContext* castContext; +@property (nonatomic, retain) NSMutableArray* requestDelegates; +@property (nonatomic, retain) id sessionListener; +@property (nonatomic, retain) NSMutableDictionary* genericChannels; +@property (nonatomic, retain) NSString* sessionStatus; + +- (instancetype)initWithDevice:(GCKDevice*)device cordovaDelegate:(id)cordovaDelegate initialCommand:(CDVInvokedUrlCommand*)initialCommand; +- (void)add:(id)listener; +- (void)createSession:(GCKDevice*)device; +- (CastRequestDelegate*)createGeneralRequestDelegate:(CDVInvokedUrlCommand*)command; +- (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted nvewLevel:(float)newLevel; +- (void)setMediaMutedWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted; +- (void)setMediaVolumeWithCommand:(CDVInvokedUrlCommand*)withCommand newVolumeLevel:(float)newLevel; +- (void)setReceiverVolumeLevelWithCommand:(CDVInvokedUrlCommand*)withCommand newLevel:(float)newLevel; +- (void)setReceiverMutedWithCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted; +- (void)loadMediaWithCommand:(CDVInvokedUrlCommand*)command mediaInfo:(GCKMediaInformation*)mediaInfo autoPlay:(BOOL)autoPlay currentTime : (double)currentTime; +- (void)createMessageChannelWithCommand:(CDVInvokedUrlCommand*)command namespace:(NSString*)namespace; +- (void)sendMessageWithCommand:(CDVInvokedUrlCommand*)command namespace:(NSString*)namespace message:(NSString*)message; +- (void)mediaSeekWithCommand:(CDVInvokedUrlCommand*)command position:(NSTimeInterval)position resumeState:(GCKMediaResumeState)resumeState; +- (void)mediaPlayWithCommand:(CDVInvokedUrlCommand*)command; +- (void)mediaPauseWithCommand:(CDVInvokedUrlCommand*)command; +- (void)mediaStopWithCommand:(CDVInvokedUrlCommand*)command; +- (void)setActiveTracksWithCommand:(CDVInvokedUrlCommand*)command activeTrackIds:(NSArray*)activeTrackIds textTrackStyle:(GCKMediaTextTrackStyle*)textTrackStyle; +- (void)queueLoadItemsWithCommand:(CDVInvokedUrlCommand *)command queueItems:(NSArray *)queueItems startIndex:(NSInteger)startIndex repeatMode:(GCKMediaRepeatMode)repeatMode; +- (void)queueJumpToItemWithCommand:(CDVInvokedUrlCommand *)command itemId:(NSUInteger)itemId; +- (void) checkFinishDelegates; +@end + +NS_ASSUME_NONNULL_END diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m new file mode 100644 index 0000000..bf6ae7d --- /dev/null +++ b/src/ios/ChromecastSession.m @@ -0,0 +1,438 @@ +// +// ChromecastSession.m +// ChromeCast +// +// Created by mac on 2019/9/30. +// + +#import "ChromecastSession.h" +#import "CastUtilities.h" + +@interface ChromecastSession() +{ + BOOL isRequesting; +} +@property (nonatomic, assign) BOOL isRequesting; +@end + +@implementation ChromecastSession + +- (instancetype)initWithDevice:(GCKDevice*)device cordovaDelegate:(id)cordovaDelegate initialCommand:(CDVInvokedUrlCommand*)initialCommand +{ + self = [super init]; + if (self) { + self.sessionStatus = @""; + self.commandDelegate = cordovaDelegate; + self.initialCommand = initialCommand; + self.castContext = [GCKCastContext sharedInstance]; + [self.castContext.sessionManager addListener:self]; + [self createSession:device]; + } + return self; +} + +- (void)add:(id)listener { + self.sessionListener = listener; +} + +- (void)createSession:(GCKDevice*)device { + if (device != nil) { + [self.castContext.sessionManager startSessionWithDevice:device]; + } else { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Cannot connect to selected cast device."]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.initialCommand.callbackId]; + } +} + +-(CastRequestDelegate*)createGeneralRequestDelegate:(CDVInvokedUrlCommand*)command { + [self checkFinishDelegates]; + CastRequestDelegate* delegate = [[CastRequestDelegate alloc] initWithSuccess:^{ + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:self.currentSession] isAlive:NO]; + } failure:^(GCKError * error) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } abortion:^(GCKRequestAbortReason abortReason) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + [self.requestDelegates addObject:delegate]; + return delegate; +} + +- (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted nvewLevel:(float)newLevel { + [self checkFinishDelegates]; + CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + + } failure:^(GCKError * error) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } abortion:^(GCKRequestAbortReason abortReason) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + + [self.requestDelegates addObject:requestDelegate]; + [self.remoteMediaClient setStreamMuted:muted customData:nil]; + GCKRequest* request = [self.remoteMediaClient setStreamVolume:newLevel customData:nil]; + request.delegate = requestDelegate; +} + + +- (void)setMediaMutedWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted { + [self checkFinishDelegates]; + CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + + } failure:^(GCKError * error) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } abortion:^(GCKRequestAbortReason abortReason) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + + [self.requestDelegates addObject:requestDelegate]; + GCKRequest* request = [self.remoteMediaClient setStreamMuted:muted customData:nil]; + request.delegate = requestDelegate; +} + +- (void)setMediaVolumeWithCommand:(CDVInvokedUrlCommand*)withCommand newVolumeLevel:(float)newLevel { + [self checkFinishDelegates]; + CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:withCommand.callbackId]; + + } failure:^(GCKError * error) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:withCommand.callbackId]; + } abortion:^(GCKRequestAbortReason abortReason) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:withCommand.callbackId]; + }]; + + [self.requestDelegates addObject:requestDelegate]; + GCKRequest* request = [self.remoteMediaClient setStreamVolume:newLevel customData:nil]; + request.delegate = requestDelegate; +} + +- (void)setReceiverVolumeLevelWithCommand:(CDVInvokedUrlCommand*)withCommand newLevel:(float)newLevel { + CastRequestDelegate* delegate = [self createGeneralRequestDelegate:withCommand]; + GCKRequest* request = [self.currentSession setDeviceVolume:newLevel]; + request.delegate = delegate; +} + +- (void)setReceiverMutedWithCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted { + CastRequestDelegate* delegate = [self createGeneralRequestDelegate:command]; + + GCKRequest* request = [self.currentSession setDeviceMuted:muted]; + request.delegate = delegate; +} + +- (void)loadMediaWithCommand:(CDVInvokedUrlCommand*)command mediaInfo:(GCKMediaInformation*)mediaInfo autoPlay:(BOOL)autoPlay currentTime : (double)currentTime { + [self checkFinishDelegates]; + CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ + self.isRequesting = NO; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } failure:^(GCKError * error) { + self.isRequesting = NO; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } abortion:^(GCKRequestAbortReason abortReason) { + self.isRequesting = NO; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + + [self.requestDelegates addObject:requestDelegate]; + GCKMediaLoadOptions* options = [[GCKMediaLoadOptions alloc] init]; + options.autoplay = autoPlay; + options.playPosition = currentTime; + GCKRequest* request = [self.remoteMediaClient loadMedia:mediaInfo withOptions:options]; + isRequesting = YES; + request.delegate = requestDelegate; +} + +- (void)createMessageChannelWithCommand:(CDVInvokedUrlCommand*)command namespace:(NSString*)namespace{ + GCKGenericChannel* newChannel = [[GCKGenericChannel alloc] initWithNamespace:namespace]; + newChannel.delegate = self; + self.genericChannels[namespace] = newChannel; + [self.currentSession addChannel:newChannel]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +- (void)sendMessageWithCommand:(CDVInvokedUrlCommand*)command namespace:(NSString*)namespace message:(NSString*)message { + GCKGenericChannel* channel = self.genericChannels[namespace]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[NSString stringWithFormat:@"Namespace %@ not founded",namespace]]; + + if (channel != nil) { + GCKError* error = nil; + [channel sendTextMessage:message error:&error]; + if (error != nil) { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + } else { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + } + } + + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +- (void)mediaSeekWithCommand:(CDVInvokedUrlCommand*)command position:(NSTimeInterval)position resumeState:(GCKMediaResumeState)resumeState { + [self checkFinishDelegates]; + CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + + } failure:^(GCKError * error) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } abortion:^(GCKRequestAbortReason abortReason) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + + [self.requestDelegates addObject:requestDelegate]; + + GCKMediaSeekOptions* options = [[GCKMediaSeekOptions alloc] init]; + options.interval = position; + options.resumeState = resumeState; + + GCKRequest* request = [self.remoteMediaClient seekWithOptions:options]; + request.delegate = requestDelegate; +} + +- (void)queueJumpToItemWithCommand:(CDVInvokedUrlCommand *)command itemId:(NSUInteger)itemId { + [self checkFinishDelegates]; + CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ + [NSUserDefaults.standardUserDefaults setBool:true forKey:@"jump"]; + [NSUserDefaults.standardUserDefaults synchronize]; + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + + } failure:^(GCKError * error) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } abortion:^(GCKRequestAbortReason abortReason) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + + [self.requestDelegates addObject:requestDelegate]; + + GCKRequest* request = [self.remoteMediaClient queueJumpToItemWithID:itemId]; + + request.delegate = requestDelegate; +} + +- (void)mediaPlayWithCommand:(CDVInvokedUrlCommand*)command { + [self checkFinishDelegates]; + CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + + } failure:^(GCKError * error) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } abortion:^(GCKRequestAbortReason abortReason) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + + [self.requestDelegates addObject:requestDelegate]; + + GCKRequest* request = [self.remoteMediaClient play]; + request.delegate = requestDelegate; +} + +- (void)mediaPauseWithCommand:(CDVInvokedUrlCommand*)command { + [self checkFinishDelegates]; + CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + + } failure:^(GCKError * error) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } abortion:^(GCKRequestAbortReason abortReason) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + + [self.requestDelegates addObject:requestDelegate]; + + GCKRequest* request = [self.remoteMediaClient pause]; + request.delegate = requestDelegate; +} + +- (void)mediaStopWithCommand:(CDVInvokedUrlCommand*)command { + [self checkFinishDelegates]; + CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + + } failure:^(GCKError * error) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } abortion:^(GCKRequestAbortReason abortReason) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + + [self.requestDelegates addObject:requestDelegate]; + + GCKRequest* request = [self.remoteMediaClient stop]; + request.delegate = requestDelegate; +} + +- (void)setActiveTracksWithCommand:(CDVInvokedUrlCommand*)command activeTrackIds:(NSArray*)activeTrackIds textTrackStyle:(GCKMediaTextTrackStyle*)textTrackStyle { + [self checkFinishDelegates]; + CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + + } failure:^(GCKError * error) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } abortion:^(GCKRequestAbortReason abortReason) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + + [self.requestDelegates addObject:requestDelegate]; + GCKRequest* request = [self.remoteMediaClient setActiveTrackIDs:activeTrackIds]; + request.delegate = requestDelegate; + request = [self.remoteMediaClient setTextTrackStyle:textTrackStyle]; +} + +- (void)queueLoadItemsWithCommand:(CDVInvokedUrlCommand *)command queueItems:(NSArray *)queueItems startIndex:(NSInteger)startIndex repeatMode:(GCKMediaRepeatMode)repeatMode { + CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObjectForQueue:self.currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObjectForQueue:self.currentSession]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + + } failure:^(GCKError * error) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } abortion:^(GCKRequestAbortReason abortReason) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + + [self.requestDelegates addObject:requestDelegate]; + GCKMediaQueueItem *item = queueItems[startIndex]; + GCKMediaQueueLoadOptions *options = [[GCKMediaQueueLoadOptions alloc] init]; + options.repeatMode = repeatMode; + options.startIndex = startIndex; + options.playPosition = item.startTime; + [NSUserDefaults.standardUserDefaults setBool:false forKey:@"jump"]; + [NSUserDefaults.standardUserDefaults synchronize]; + GCKRequest* request = [self.remoteMediaClient queueLoadItems:queueItems withOptions:options]; + request.delegate = requestDelegate; +} + +- (void) checkFinishDelegates{ + NSMutableArray* tempArray = [NSMutableArray new]; + for (CastRequestDelegate* delegate in self.requestDelegates) { + if (!delegate.finished ) { + [tempArray addObject:delegate]; + } + } + self.requestDelegates = tempArray; +} + +#pragma -- GCKSessionManagerListener +- (void)sessionManager:(GCKSessionManager *)sessionManager didStartCastSession:(GCKCastSession *)session { + self.currentSession = session; + self.remoteMediaClient = session.remoteMediaClient; + [self.remoteMediaClient addListener:self]; + + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: [CastUtilities createSessionObject:session] ]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.initialCommand.callbackId]; +} + +- (void)sessionManager:(GCKSessionManager *)sessionManager didEndCastSession:(GCKCastSession *)session withError:(NSError *)error { + self.currentSession = nil; + self.remoteMediaClient = nil; + + if (error != nil) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.debugDescription]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.initialCommand.callbackId]; + } + if ([self.sessionStatus isEqualToString:@""]) { + [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session] isAlive:false]; + } else { + [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:self.sessionStatus] isAlive:false]; + } + +} + +- (void)sessionManager:(GCKSessionManager *)sessionManager didResumeCastSession:(GCKCastSession *)session { + self.currentSession = session; + NSLog(@"here is the session"); +} + +- (void)sessionManager:(GCKSessionManager *)sessionManager didResumeSession:(GCKSession *)session { + GCKSession *cursession = session; + NSLog(@"session received"); +} + +#pragma -- GCKRemoteMediaClientListener +- (void)remoteMediaClient:(GCKRemoteMediaClient *)client didStartMediaSessionWithID:(NSInteger)sessionID { + NSDictionary* media = [CastUtilities createMediaObject:self.currentSession]; + if (!self.isRequesting) { +// [self.sessionListener onMediaLoaded:media]; + } +} + +- (void)remoteMediaClient:(GCKRemoteMediaClient *)client didUpdateMediaStatus:(GCKMediaStatus *)mediaStatus { + if (self.currentSession == nil) { + [self.sessionListener onMediaUpdated:@{} isAlive:false]; + return; + } + + NSDictionary* media = [CastUtilities createMediaObject:self.currentSession]; + [self.sessionListener onMediaUpdated:media isAlive:true]; +} + +- (void)remoteMediaClientDidUpdatePreloadStatus:(GCKRemoteMediaClient *)client { + [self remoteMediaClient:client didUpdateMediaStatus:nil]; +} + +- (void)remoteMediaClientDidUpdateQueue:(GCKRemoteMediaClient *)client{ + +} +- (void)remoteMediaClient:(GCKRemoteMediaClient *)client didInsertQueueItemsWithIDs:(NSArray *)queueItemIDs beforeItemWithID:(GCKMediaQueueItemID)beforeItemID { + +} + +- (void)remoteMediaClient:(GCKRemoteMediaClient *)client didReceiveQueueItems:(NSArray *)queueItems { + +} + +- (void)remoteMediaClient:(GCKRemoteMediaClient *)client didReceiveQueueItemIDs:(NSArray *)queueItemIDs { + +} + + +#pragma -- GCKGenericChannelDelegate +- (void)castChannel:(GCKGenericChannel *)channel didReceiveTextMessage:(NSString *)message withNamespace:(NSString *)protocolNamespace { + NSDictionary* currentSession = [CastUtilities createSessionObject:self.currentSession]; + [self.sessionListener onMessageReceived:currentSession namespace:protocolNamespace message:message]; +} +@end diff --git a/src/ios/ChromecastSession.swift b/src/ios/ChromecastSession.swift deleted file mode 100644 index 6614633..0000000 --- a/src/ios/ChromecastSession.swift +++ /dev/null @@ -1,251 +0,0 @@ -import GoogleCast - -@objc (ChromecastSession) class ChromecastSession : NSObject { - var commandDelegate: CDVCommandDelegate? - var initialCommand: CDVInvokedUrlCommand? - var currentSession: GCKCastSession? - var remoteMediaClient: GCKRemoteMediaClient? - var castContext: GCKCastContext? - var requestDelegates: [CastRequestDelegate] = [] - var sessionListener: CastSessionListener? - var genericChannels: [String : GCKGenericChannel] = [:] - - init(_ withDevice: GCKDevice, cordovaDelegate: CDVCommandDelegate, initialCommand: CDVInvokedUrlCommand) { - super.init() - self.commandDelegate = cordovaDelegate - self.initialCommand = initialCommand - - self.castContext = GCKCastContext.sharedInstance() - self.castContext?.sessionManager.add(self) - - self.createSession(withDevice) - } - - func add(_ listener: CastSessionListener) { - self.sessionListener = listener - } - - func createSession(_ device: GCKDevice?) { - if device != nil { - castContext?.sessionManager.startSession(with: device!) - } else { - let pluginResult = CDVPluginResult( - status: CDVCommandStatus_ERROR, - messageAs: "Cannot connect to selected cast device." - ) - - self.commandDelegate!.send(pluginResult, callbackId: self.initialCommand?.callbackId) - } - } - - func createGeneralRequestDelegate(_ command: CDVInvokedUrlCommand) -> CastRequestDelegate { - self.checkFinishedDelegates() - - let delegate = CastRequestDelegate(success: { - let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK) - self.commandDelegate!.send(pluginResult, callbackId: command.callbackId) - }, failure: {(error: GCKError) in - let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR) - self.commandDelegate!.send(pluginResult, callbackId: command.callbackId) - }, abortion: { (abortReason: GCKRequestAbortReason) in - let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR) - self.commandDelegate!.send(pluginResult, callbackId: command.callbackId) - }) - - self.requestDelegates.append(delegate) - - return delegate - } - - func setReceiverVolumeLevel(_ withCommand: CDVInvokedUrlCommand, newLevel: Float) { - let delegate = self.createGeneralRequestDelegate(withCommand) - - let request = remoteMediaClient?.setStreamVolume(newLevel) - request?.delegate = delegate - } - - func setReceiverMuted(_ withCommand: CDVInvokedUrlCommand, muted: Bool) { - let delegate = self.createGeneralRequestDelegate(withCommand) - - let request = remoteMediaClient?.setStreamMuted(muted) - request?.delegate = delegate - } - - func loadMedia(_ withCommand: CDVInvokedUrlCommand, mediaInfo: GCKMediaInformation, autoPlay: Bool, currentTime: Double) { - self.checkFinishedDelegates() - - let requestDelegate = CastRequestDelegate(success: { - let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: CastUtilities.createMediaObject(self.currentSession!) as! [String : Any]) - self.commandDelegate!.send(pluginResult, callbackId: withCommand.callbackId) - }, failure: {(error: GCKError) in - let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: error.description) - self.commandDelegate!.send(pluginResult, callbackId: withCommand.callbackId) - }, abortion: { (abortReason: GCKRequestAbortReason) in - let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: abortReason.rawValue) - self.commandDelegate!.send(pluginResult, callbackId: withCommand.callbackId) - }) - self.requestDelegates.append(requestDelegate) - - let options = GCKMediaLoadOptions.init() - options.autoplay = autoPlay - options.playPosition = currentTime - - let request = remoteMediaClient?.loadMedia(mediaInfo, with: options) - request?.delegate = requestDelegate - } - - func createMessageChannel(_ withCommand: CDVInvokedUrlCommand, namespace: String) { - let newChannel = GCKGenericChannel(namespace: namespace) - newChannel.delegate = self - - self.genericChannels.updateValue(newChannel, forKey: namespace) - self.currentSession?.add(newChannel) - - let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK) - self.commandDelegate!.send(pluginResult, callbackId: withCommand.callbackId) - } - - func sendMessage(_ withCommand: CDVInvokedUrlCommand, namespace: String, message: String) { - let channel = self.genericChannels[namespace] ?? nil - - var pluginResult = CDVPluginResult( - status: CDVCommandStatus_ERROR, - messageAs: "Namespace '\(namespace)' not fouded." - ) - - if channel != nil { - var error: GCKError? - channel?.sendTextMessage(message, error: &error) - - if error != nil { - pluginResult = CDVPluginResult( - status: CDVCommandStatus_ERROR, - messageAs: error!.description - ) - } else { - pluginResult = CDVPluginResult( - status: CDVCommandStatus_OK - ) - } - } - - self.commandDelegate!.send( - pluginResult, - callbackId: withCommand.callbackId - ) - } - - func mediaSeek(_ withCommand: CDVInvokedUrlCommand, position: TimeInterval, resumeState: GCKMediaResumeState) { - let delegate = self.createGeneralRequestDelegate(withCommand) - - let options = GCKMediaSeekOptions() - options.interval = position - options.resumeState = resumeState - - let request = remoteMediaClient?.seek(with: options) - request?.delegate = delegate - } - - - func mediaPlay(_ withCommand: CDVInvokedUrlCommand) { - let delegate = self.createGeneralRequestDelegate(withCommand) - - let request = remoteMediaClient?.play() - request?.delegate = delegate - } - - func mediaPause(_ withCommand: CDVInvokedUrlCommand) { - let delegate = self.createGeneralRequestDelegate(withCommand) - - let request = remoteMediaClient?.pause() - request?.delegate = delegate - } - - func mediaStop(_ withCommand: CDVInvokedUrlCommand) { - let delegate = self.createGeneralRequestDelegate(withCommand) - - let request = remoteMediaClient?.stop() - request?.delegate = delegate - } - - func setActiveTracks(_ withCommand: CDVInvokedUrlCommand, activeTrackIds: [NSNumber], textTrackStyle: GCKMediaTextTrackStyle) { - let delegate = self.createGeneralRequestDelegate(withCommand) - - var request = remoteMediaClient?.setActiveTrackIDs(activeTrackIds) - request?.delegate = delegate - - request = remoteMediaClient?.setTextTrackStyle(textTrackStyle) - } - - private func checkFinishedDelegates() { - self.requestDelegates = self.requestDelegates.filter({ (delegate: CastRequestDelegate) -> Bool in - return !delegate.finished - }) - } -} - -extension ChromecastSession : GCKSessionManagerListener { - func sessionManager(_ sessionManager: GCKSessionManager, didStart session: GCKCastSession) { - self.currentSession = session - self.remoteMediaClient = session.remoteMediaClient - self.remoteMediaClient?.add(self) - - let pluginResult = CDVPluginResult( - status: CDVCommandStatus_OK, - messageAs: CastUtilities.createSessionObject(session) as! [String: Any] - ) - - self.commandDelegate!.send( - pluginResult, - callbackId: self.initialCommand?.callbackId - ) - } - - func sessionManager(_ sessionManager: GCKSessionManager, didEnd session: GCKCastSession, withError error: Error?) { - self.currentSession = nil - self.remoteMediaClient = nil - - if error != nil { - let pluginResult = CDVPluginResult( - status: CDVCommandStatus_ERROR, - messageAs: error.debugDescription as String - ) - self.commandDelegate!.send( - pluginResult, - callbackId: initialCommand?.callbackId - ) - } - - self.sessionListener?.onSessionUpdated(CastUtilities.createSessionObject(session), isAlive: false) - } -} - -extension ChromecastSession : GCKRemoteMediaClientListener { - func remoteMediaClient(_ client: GCKRemoteMediaClient, didStartMediaSessionWithID sessionID: Int) { - let media = CastUtilities.createMediaObject(self.currentSession!) - - self.sessionListener?.onMediaLoaded(media) - } - - func remoteMediaClient(_ client: GCKRemoteMediaClient, didUpdate mediaStatus: GCKMediaStatus?) { - if self.currentSession == nil { - self.sessionListener?.onMediaUpdated([:], isAlive: false) - return - } - - let media = CastUtilities.createMediaObject(self.currentSession!) - self.sessionListener?.onMediaUpdated(media, isAlive: true) - } - - func remoteMediaClientDidUpdatePreloadStatus(_ client: GCKRemoteMediaClient) { - self.remoteMediaClient(client, didUpdate: nil) - } -} - -extension ChromecastSession : GCKGenericChannelDelegate { - func cast(_ channel: GCKGenericChannel, didReceiveTextMessage message: String, withNamespace protocolNamespace: String) { - let currentSession = CastUtilities.createSessionObject(self.currentSession!) - - self.sessionListener?.onMessageReceived(currentSession, namespace: protocolNamespace, message: message) - } -} From 4c406259a0a470b3414164565bb920db1435c47e Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Tue, 12 Nov 2019 15:26:51 -0700 Subject: [PATCH 074/166] Added a bit more specific instructions to manual tests --- tests/www/js/tests_manual_primary_2.js | 2 +- tests/www/js/tests_manual_secondary.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/www/js/tests_manual_primary_2.js b/tests/www/js/tests_manual_primary_2.js index 9dddac2..3975429 100644 --- a/tests/www/js/tests_manual_primary_2.js +++ b/tests/www/js/tests_manual_primary_2.js @@ -108,7 +108,7 @@ runner.on('suite end', function (suite) { var passed = this.stats.passes === runner.total; if (passed) { - utils.setAction('All manual tests passed! (Assuming you did Part 1 as well)'); + utils.setAction('All manual tests passed! [Assuming you did Part 1 as well :) ]'); document.getElementById('action').style.backgroundColor = '#ceffc4'; } }); diff --git a/tests/www/js/tests_manual_secondary.js b/tests/www/js/tests_manual_secondary.js index ba7cbb0..469413d 100644 --- a/tests/www/js/tests_manual_secondary.js +++ b/tests/www/js/tests_manual_secondary.js @@ -175,7 +175,7 @@ }); }); it('media.stop should end video playback', function (done) { - utils.setAction('', 'Stop Media', function () { + utils.setAction('Wait for instructions from primary.', 'Stop Media', function () { var called = utils.waitForAllCalls([ { id: success, repeats: false }, { id: update, repeats: true } @@ -200,7 +200,7 @@ }); }); it('session.queueLoad should be able to load remote audio/video queue and return the correct Metadata', function (done) { - utils.setAction('', 'Load Queue', function () { + utils.setAction('Wait for instructions from primary.', 'Load Queue', function () { var item; var queue = []; @@ -260,7 +260,7 @@ }); }); it('media.queueJumpToItem should jump to selected item', function (done) { - utils.setAction('', 'Queue Jump', function () { + utils.setAction('Wait for instructions from primary.', 'Queue Jump', function () { var calledAnyOrder = utils.waitForAllCalls([ { id: success, repeats: false }, { id: update, repeats: true } @@ -349,7 +349,7 @@ // from chrome first and then join from the app. utils.startSession(function (sess) { session = sess; - utils.setAction('On primary click "Enter Session', 'Continue', done); + utils.setAction('1. On primary click "Enter Session
      2. Wait for instructions from primary.', 'Continue', done); }); return; } From 3021c840a6706ee0f8e073956dbebfaa57f80c24 Mon Sep 17 00:00:00 2001 From: anna Date: Wed, 13 Nov 2019 17:59:06 +0800 Subject: [PATCH 075/166] Auto tests are fixed --- src/ios/Chromecast.m | 6 ++-- src/ios/ChromecastSession.m | 8 +++-- tests/www/js/tests_manual_secondary.js | 44 +++++++++++++------------- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 81bd310..df73b00 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -512,8 +512,10 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didEndSession:(GCKSes CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.debugDescription]; [self.commandDelegate sendPluginResult:pluginResult callbackId:self.sessionCommand]; } - [self.currentSession.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"stopped"] isAlive:false]; - [self sendError:@"cancel" message:@"Session is stopped." command:self.sessionCommand]; + if (self.currentSession.sessionStatus == @"stopped") { + [self.currentSession.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"stopped"] isAlive:false]; + [self sendError:@"cancel" message:@"Session is stopped." command:self.sessionCommand]; + } } diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index bf6ae7d..6f70904 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -382,6 +382,8 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didEndCastSession:(GC } + + - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeCastSession:(GCKCastSession *)session { self.currentSession = session; NSLog(@"here is the session"); @@ -393,12 +395,14 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeSession:(GCK } #pragma -- GCKRemoteMediaClientListener + - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didStartMediaSessionWithID:(NSInteger)sessionID { NSDictionary* media = [CastUtilities createMediaObject:self.currentSession]; - if (!self.isRequesting) { +// if (!self.isRequesting) { // [self.sessionListener onMediaLoaded:media]; - } +// } } + - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didUpdateMediaStatus:(GCKMediaStatus *)mediaStatus { if (self.currentSession == nil) { diff --git a/tests/www/js/tests_manual_secondary.js b/tests/www/js/tests_manual_secondary.js index 469413d..2ec5ce8 100644 --- a/tests/www/js/tests_manual_secondary.js +++ b/tests/www/js/tests_manual_secondary.js @@ -85,7 +85,7 @@ initializeApi(); } }, 100); - function initializeApi () { + function initializeApi() { var finished = false; // Need this so we stop testing after being finished var unavailable = 'unavailable'; var available = 'available'; @@ -101,7 +101,7 @@ new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function (sess) { assert.fail('should not receive a session (make sure there is no active cast session when starting the tests)'); - }, function receiverListener (availability) { + }, function receiverListener(availability) { if (!finished) { called(availability); } @@ -146,8 +146,8 @@ assert.equal(media.media.metadata.title, mediaInfo.metadata.title); assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); assert.equal(media.media.metadata.releaseDate, mediaInfo.metadata.releaseDate); - // TODO figure out how to maintain the data types for custom params on the native side - // so that we don't have to do turn each actual and expected into a string + // TODO figure out how to maintain the data types for custom params on the native side + // so that we don't have to do turn each actual and expected into a string assert.equal(media.media.metadata.someTrueBoolean + '', mediaInfo.metadata.someTrueBoolean + ''); assert.equal(media.media.metadata.someFalseBoolean + '', mediaInfo.metadata.someFalseBoolean + ''); assert.equal(media.media.metadata.someSmallNumber + '', mediaInfo.metadata.someSmallNumber + ''); @@ -158,7 +158,7 @@ assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.GENERIC); assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.GENERIC); - media.addUpdateListener(function listener (isAlive) { + media.addUpdateListener(function listener(isAlive) { assert.isTrue(isAlive); utils.testMediaProperties(media); assert.oneOf(media.playerState, [ @@ -177,12 +177,12 @@ it('media.stop should end video playback', function (done) { utils.setAction('Wait for instructions from primary.', 'Stop Media', function () { var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } + { id: success, repeats: false }, + { id: update, repeats: true } ], function () { done(); }); - media.addUpdateListener(function listener (isAlive) { + media.addUpdateListener(function listener(isAlive) { if (media.playerState === chrome.cast.media.PlayerState.IDLE) { media.removeUpdateListener(listener); assert.equal(media.idleReason, chrome.cast.media.IdleReason.CANCELLED); @@ -242,7 +242,7 @@ assert.equal(media.items[i].media.metadata.myMadeUpMetadata, audioItem.metadata.myMadeUpMetadata); assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.MUSIC_TRACK); assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.MUSIC_TRACK); - media.addUpdateListener(function listener (isAlive) { + media.addUpdateListener(function listener(isAlive) { assert.isTrue(isAlive); utils.testMediaProperties(media); assert.oneOf(media.playerState, [ @@ -262,19 +262,19 @@ it('media.queueJumpToItem should jump to selected item', function (done) { utils.setAction('Wait for instructions from primary.', 'Queue Jump', function () { var calledAnyOrder = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } + { id: success, repeats: false }, + { id: update, repeats: true } ], function () { done(); }); var calledOrder = utils.callOrder([ - { id: stopped, repeats: true }, - { id: newMedia, repeats: true } + { id: stopped, repeats: true }, + { id: newMedia, repeats: true } ], function () { calledAnyOrder(update); }); var i = utils.getCurrentItemIndex(media); - media.addUpdateListener(function listener (isAlive) { + media.addUpdateListener(function listener(isAlive) { if (media.playerState === chrome.cast.media.PlayerState.IDLE) { assert.oneOf(media.idleReason, [chrome.cast.media.IdleReason.INTERRUPTED, chrome.cast.media.IdleReason.FINISHED]); @@ -322,14 +322,14 @@ it('Primary should not receive session on initialize', function (done) { this.timeout(240000); utils.setAction('On primary:
      1. Force kill and restart the app.' - + '
      2. Select Manual Tests (Primary) Part 2 from the home page to finish the manual tests.', 'Start Part 2', done); + + '
      2. Select Manual Tests (Primary) Part 2 from the home page to finish the manual tests.', 'Start Part 2', done); }); it('Secondary session.leave should cause session to end (because all senders have left)', function (done) { var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } + { id: success, repeats: false }, + { id: update, repeats: true } ], done); - session.addUpdateListener(function listener (isAlive) { + session.addUpdateListener(function listener(isAlive) { if (session.status === chrome.cast.SessionStatus.DISCONNECTED) { assert.isTrue(isAlive); session.removeUpdateListener(listener); @@ -362,10 +362,10 @@ }); it('session.stop', function (done) { var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } + { id: success, repeats: false }, + { id: update, repeats: true } ], done); - session.addUpdateListener(function listener (isAlive) { + session.addUpdateListener(function listener(isAlive) { if (session.status === chrome.cast.SessionStatus.STOPPED) { assert.isFalse(isAlive); session.removeUpdateListener(listener); @@ -404,4 +404,4 @@ return runner; }; -}()); +}()); \ No newline at end of file From 47b7f66461eb44f01e30bd0745dfe0df8413fe88 Mon Sep 17 00:00:00 2001 From: anna Date: Fri, 15 Nov 2019 22:18:19 +0800 Subject: [PATCH 076/166] manual and auto tests completed --- src/ios/CastRequestDelegate.h | 6 ++- src/ios/CastRequestDelegate.m | 6 ++- src/ios/CastUtilities.m | 12 +++--- src/ios/Chromecast.m | 1 + src/ios/ChromecastSession.m | 69 ++++++++++++++++++++++------------- 5 files changed, 60 insertions(+), 34 deletions(-) diff --git a/src/ios/CastRequestDelegate.h b/src/ios/CastRequestDelegate.h index 9381ed0..43bea74 100644 --- a/src/ios/CastRequestDelegate.h +++ b/src/ios/CastRequestDelegate.h @@ -12,6 +12,7 @@ NS_ASSUME_NONNULL_BEGIN @protocol CastSessionListener +-(void)onSessionRejoin:(NSDictionary*)session; -(void)onMediaLoaded:(NSDictionary*)media; -(void)onMediaUpdated:(NSDictionary*)media isAlive:(BOOL)isAlive; -(void)onSessionUpdated:(NSDictionary*)session isAlive:(BOOL)isAlive; @@ -21,6 +22,7 @@ NS_ASSUME_NONNULL_BEGIN @interface CastConnectionListener : NSObject { + void (^onSessionRejoin)(NSDictionary* session); void (^onMediaLoaded)(NSDictionary* media); void (^onMediaUpdated)(NSDictionary* media,BOOL isAlive); void (^onSessionUpdated)(NSDictionary* session, BOOL isAlive); @@ -29,9 +31,9 @@ NS_ASSUME_NONNULL_BEGIN } @property (nonatomic, copy) void (^onReceiverAvailableUpdate)(BOOL available); -@property (nonatomic, copy) void (^onSessionRejoin)(NSDictionary* session); +//@property (nonatomic, copy) void (^onSessionRejoin)(NSDictionary* session); -- (instancetype)initWithReceiverAvailableUpdate:(void(^)(BOOL available))onReceiverAvailableUpdate onSessionRejoin:(void(^)(NSDictionary* session))onSessionRejoin onMediaLoaded:(void(^)(NSDictionary* media))onMediaLoaded onMediaUpdated:(void(^)(NSDictionary* media, BOOL isAlive))onMediaUpdated onSessionUpdated:(void(^)(NSDictionary* session, BOOL isAlive))onSessionUpdated onSessionEnd:(void(^)(NSDictionary* session))onSessionEnd onMessageReceived:(void(^)(NSDictionary* session,NSString* namespace,NSString* message))onMessageReceived ; +- (instancetype)initWithReceiverAvailableUpdate:(void(^)(BOOL available))onReceiverAvailableUpdate onSessionRejoin:(void(^)(NSDictionary* session))onSessionRejoin onMediaLoaded:(void(^)(NSDictionary* m))onMediaLoaded onMediaUpdated:(void(^)(NSDictionary* media, BOOL isAlive))onMediaUpdated onSessionUpdated:(void(^)(NSDictionary* session, BOOL isAlive))onSessionUpdated onSessionEnd:(void(^)(NSDictionary* session))onSessionEnd onMessageReceived:(void(^)(NSDictionary* session,NSString* namespace,NSString* message))onMessageReceived ; @end @interface CastRequestDelegate : NSObject diff --git a/src/ios/CastRequestDelegate.m b/src/ios/CastRequestDelegate.m index 5bc31d6..92352be 100644 --- a/src/ios/CastRequestDelegate.m +++ b/src/ios/CastRequestDelegate.m @@ -14,7 +14,7 @@ - (instancetype)initWithReceiverAvailableUpdate:(void(^)(BOOL available))onRecei self = [super init]; if (self) { self.onReceiverAvailableUpdate = onReceiverAvailableUpdate; - self.onSessionRejoin = onSessionRejoin; + onSessionRejoin = onSessionRejoin; onMediaLoaded = onMediaLoaded; onSessionUpdated = onSessionUpdated; onMediaUpdated = onMediaUpdated; @@ -56,6 +56,10 @@ - (void)onSessionEnd:(NSDictionary *)session { onSessionEnd(session); } +- (void)onSessionRejoin:(NSDictionary *)session { + onSessionRejoin(session); +} + - (void)onMessageReceived:(NSDictionary *)session namespace:(NSString *)namespace message:(NSString *)message { onMessageReceived(session,namespace,message); } diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index cded9f1..3bcbf3a 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -581,9 +581,9 @@ + (NSDictionary *)createMediaObjectForQueueJumpToItem:(GCKCastSession *)session return @{}; } - NSLog(@"stream position: %f", mediaStatus.streamPosition ); - NSLog(@"medis player state: %@", [CastUtilities getPlayerState:mediaStatus.playerState]); - NSLog(@"Idle Reason: %@", [CastUtilities getIdleReason:mediaStatus.idleReason]); +// NSLog(@"stream position: %f", mediaStatus.streamPosition ); +// NSLog(@"medis player state: %@", [CastUtilities getPlayerState:mediaStatus.playerState]); +// NSLog(@"Idle Reason: %@", [CastUtilities getIdleReason:mediaStatus.idleReason]); // if (mediaStatus.streamPosition == 0) { //// NSLog(@"medis player state: %@", [CastUtilities getPlayerState:mediaStatus.playerState]); //// NSLog(@"Idle Reason: %@", [CastUtilities getIdleReason:mediaStatus.idleReason]); @@ -650,9 +650,9 @@ + (NSDictionary *)createMediaObject:(GCKCastSession *)session { return @{}; } - NSLog(@"stream position: %f", mediaStatus.streamPosition ); - NSLog(@"medis player state: %@", [CastUtilities getPlayerState:mediaStatus.playerState]); - NSLog(@"Idle Reason: %@", [CastUtilities getIdleReason:mediaStatus.idleReason]); +// NSLog(@"stream position: %f", mediaStatus.streamPosition ); +// NSLog(@"medis player state: %@", [CastUtilities getPlayerState:mediaStatus.playerState]); +// NSLog(@"Idle Reason: %@", [CastUtilities getIdleReason:mediaStatus.idleReason]); // if (mediaStatus.streamPosition == 0) { //// NSLog(@"medis player state: %@", [CastUtilities getPlayerState:mediaStatus.playerState]); //// NSLog(@"Idle Reason: %@", [CastUtilities getIdleReason:mediaStatus.idleReason]); diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index df73b00..87ef636 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -470,6 +470,7 @@ - (void)onCastStateChanged:(NSNotification*)notification { } - (void)sendEvent:(NSString *)eventName args:(NSArray *)args{ + NSLog(@"Event Name: %@", eventName); if (self.eventCommand == nil) { return; } diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index 6f70904..188c928 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -31,6 +31,7 @@ - (instancetype)initWithDevice:(GCKDevice*)device cordovaDelegate:(id)listener { self.sessionListener = listener; } @@ -38,6 +39,7 @@ - (void)add:(id)listener { - (void)createSession:(GCKDevice*)device { if (device != nil) { [self.castContext.sessionManager startSessionWithDevice:device]; + } else { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Cannot connect to selected cast device."]; [self.commandDelegate sendPluginResult:pluginResult callbackId:self.initialCommand.callbackId]; @@ -78,6 +80,7 @@ - (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:( [self.requestDelegates addObject:requestDelegate]; [self.remoteMediaClient setStreamMuted:muted customData:nil]; + self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient setStreamVolume:newLevel customData:nil]; request.delegate = requestDelegate; } @@ -99,6 +102,7 @@ - (void)setMediaMutedWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)mute }]; [self.requestDelegates addObject:requestDelegate]; + self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient setStreamMuted:muted customData:nil]; request.delegate = requestDelegate; } @@ -119,35 +123,35 @@ - (void)setMediaVolumeWithCommand:(CDVInvokedUrlCommand*)withCommand newVolumeLe }]; [self.requestDelegates addObject:requestDelegate]; + self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient setStreamVolume:newLevel customData:nil]; request.delegate = requestDelegate; } - (void)setReceiverVolumeLevelWithCommand:(CDVInvokedUrlCommand*)withCommand newLevel:(float)newLevel { CastRequestDelegate* delegate = [self createGeneralRequestDelegate:withCommand]; + self.isRequesting = YES; GCKRequest* request = [self.currentSession setDeviceVolume:newLevel]; request.delegate = delegate; } - (void)setReceiverMutedWithCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted { CastRequestDelegate* delegate = [self createGeneralRequestDelegate:command]; - + self.isRequesting = YES; GCKRequest* request = [self.currentSession setDeviceMuted:muted]; request.delegate = delegate; } + - (void)loadMediaWithCommand:(CDVInvokedUrlCommand*)command mediaInfo:(GCKMediaInformation*)mediaInfo autoPlay:(BOOL)autoPlay currentTime : (double)currentTime { [self checkFinishDelegates]; CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - self.isRequesting = NO; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(GCKError * error) { - self.isRequesting = NO; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } abortion:^(GCKRequestAbortReason abortReason) { - self.isRequesting = NO; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; }]; @@ -156,8 +160,8 @@ - (void)loadMediaWithCommand:(CDVInvokedUrlCommand*)command mediaInfo:(GCKMediaI GCKMediaLoadOptions* options = [[GCKMediaLoadOptions alloc] init]; options.autoplay = autoPlay; options.playPosition = currentTime; + self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient loadMedia:mediaInfo withOptions:options]; - isRequesting = YES; request.delegate = requestDelegate; } @@ -170,6 +174,8 @@ - (void)createMessageChannelWithCommand:(CDVInvokedUrlCommand*)command namespace [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } + + - (void)sendMessageWithCommand:(CDVInvokedUrlCommand*)command namespace:(NSString*)namespace message:(NSString*)message { GCKGenericChannel* channel = self.genericChannels[namespace]; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[NSString stringWithFormat:@"Namespace %@ not founded",namespace]]; @@ -207,7 +213,7 @@ - (void)mediaSeekWithCommand:(CDVInvokedUrlCommand*)command position:(NSTimeInte GCKMediaSeekOptions* options = [[GCKMediaSeekOptions alloc] init]; options.interval = position; options.resumeState = resumeState; - + self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient seekWithOptions:options]; request.delegate = requestDelegate; } @@ -215,9 +221,6 @@ - (void)mediaSeekWithCommand:(CDVInvokedUrlCommand*)command position:(NSTimeInte - (void)queueJumpToItemWithCommand:(CDVInvokedUrlCommand *)command itemId:(NSUInteger)itemId { [self checkFinishDelegates]; CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [NSUserDefaults.standardUserDefaults setBool:true forKey:@"jump"]; - [NSUserDefaults.standardUserDefaults synchronize]; - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; @@ -230,9 +233,10 @@ - (void)queueJumpToItemWithCommand:(CDVInvokedUrlCommand *)command itemId:(NSUIn }]; [self.requestDelegates addObject:requestDelegate]; - + [NSUserDefaults.standardUserDefaults setBool:true forKey:@"jump"]; + [NSUserDefaults.standardUserDefaults synchronize]; + self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient queueJumpToItemWithID:itemId]; - request.delegate = requestDelegate; } @@ -252,7 +256,7 @@ - (void)mediaPlayWithCommand:(CDVInvokedUrlCommand*)command { }]; [self.requestDelegates addObject:requestDelegate]; - + self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient play]; request.delegate = requestDelegate; } @@ -273,7 +277,7 @@ - (void)mediaPauseWithCommand:(CDVInvokedUrlCommand*)command { }]; [self.requestDelegates addObject:requestDelegate]; - + self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient pause]; request.delegate = requestDelegate; } @@ -294,7 +298,7 @@ - (void)mediaStopWithCommand:(CDVInvokedUrlCommand*)command { }]; [self.requestDelegates addObject:requestDelegate]; - + self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient stop]; request.delegate = requestDelegate; } @@ -315,6 +319,7 @@ - (void)setActiveTracksWithCommand:(CDVInvokedUrlCommand*)command activeTrackIds }]; [self.requestDelegates addObject:requestDelegate]; + self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient setActiveTrackIDs:activeTrackIds]; request.delegate = requestDelegate; request = [self.remoteMediaClient setTextTrackStyle:textTrackStyle]; @@ -322,7 +327,6 @@ - (void)setActiveTracksWithCommand:(CDVInvokedUrlCommand*)command activeTrackIds - (void)queueLoadItemsWithCommand:(CDVInvokedUrlCommand *)command queueItems:(NSArray *)queueItems startIndex:(NSInteger)startIndex repeatMode:(GCKMediaRepeatMode)repeatMode { CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObjectForQueue:self.currentSession] isAlive:NO]; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObjectForQueue:self.currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; @@ -342,6 +346,7 @@ - (void)queueLoadItemsWithCommand:(CDVInvokedUrlCommand *)command queueItems:(NS options.playPosition = item.startTime; [NSUserDefaults.standardUserDefaults setBool:false forKey:@"jump"]; [NSUserDefaults.standardUserDefaults synchronize]; + self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient queueLoadItems:queueItems withOptions:options]; request.delegate = requestDelegate; } @@ -361,7 +366,6 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didStartCastSession:( self.currentSession = session; self.remoteMediaClient = session.remoteMediaClient; [self.remoteMediaClient addListener:self]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: [CastUtilities createSessionObject:session] ]; [self.commandDelegate sendPluginResult:pluginResult callbackId:self.initialCommand.callbackId]; } @@ -390,19 +394,14 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeCastSession: } - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeSession:(GCKSession *)session { - GCKSession *cursession = session; - NSLog(@"session received"); } #pragma -- GCKRemoteMediaClientListener - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didStartMediaSessionWithID:(NSInteger)sessionID { - NSDictionary* media = [CastUtilities createMediaObject:self.currentSession]; -// if (!self.isRequesting) { -// [self.sessionListener onMediaLoaded:media]; -// } } - + + - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didUpdateMediaStatus:(GCKMediaStatus *)mediaStatus { if (self.currentSession == nil) { @@ -410,8 +409,28 @@ - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didUpdateMediaStatus:(G return; } - NSDictionary* media = [CastUtilities createMediaObject:self.currentSession]; - [self.sessionListener onMediaUpdated:media isAlive:true]; + if (![[NSUserDefaults standardUserDefaults] boolForKey:@"jump"]) { + NSDictionary* media = [CastUtilities createMediaObject:self.currentSession]; + [self.sessionListener onMediaUpdated:media isAlive:true]; + if (!self.isRequesting) { + if (mediaStatus.streamPosition > 0) { + + if (mediaStatus.queueItemCount > 1) { + [self.sessionListener onMediaLoaded:[CastUtilities createMediaObjectForQueue:self.currentSession]]; + self.isRequesting = YES; + } + else { + [self.sessionListener onMediaLoaded:media]; + } + } + + } + } + else { + NSDictionary* media = [CastUtilities createMediaObject:self.currentSession]; + [self.sessionListener onMediaUpdated:media isAlive:false]; + } + } - (void)remoteMediaClientDidUpdatePreloadStatus:(GCKRemoteMediaClient *)client { From c2af2abb14fe3a05836b085f0c3955caa0448de8 Mon Sep 17 00:00:00 2001 From: anna Date: Sat, 16 Nov 2019 16:43:41 +0800 Subject: [PATCH 077/166] Auto and manual tests completed --- src/ios/Chromecast.m | 2 +- src/ios/ChromecastSession.m | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 87ef636..0c2582f 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -470,7 +470,7 @@ - (void)onCastStateChanged:(NSNotification*)notification { } - (void)sendEvent:(NSString *)eventName args:(NSArray *)args{ - NSLog(@"Event Name: %@", eventName); +// NSLog(@"Event Name: %@", eventName); if (self.eventCommand == nil) { return; } diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index 188c928..c9fb5cd 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -38,6 +38,8 @@ - (void)add:(id)listener { - (void)createSession:(GCKDevice*)device { if (device != nil) { + [NSUserDefaults.standardUserDefaults setBool:false forKey:@"jump"]; + [NSUserDefaults.standardUserDefaults synchronize]; [self.castContext.sessionManager startSessionWithDevice:device]; } else { From 84fb9f5812188556b0850feebf289df9d324fac2 Mon Sep 17 00:00:00 2001 From: anna Date: Sat, 16 Nov 2019 20:55:54 +0800 Subject: [PATCH 078/166] auto and manual tests are finished --- src/ios/Chromecast.m | 9 ++++----- src/ios/ChromecastSession.m | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 0c2582f..e8bdf77 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -507,14 +507,13 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeSession:(GCK } - (void)sessionManager:(GCKSessionManager *)sessionManager didEndSession:(GCKSession *)session withError:(NSError *)error { - self.currentSession.currentSession = nil; - + if (error != nil) { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.debugDescription]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:self.sessionCommand]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.sessionCommand.callbackId]; } - if (self.currentSession.sessionStatus == @"stopped") { - [self.currentSession.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"stopped"] isAlive:false]; + if ([self.currentSession.sessionStatus isEqual: @"stopped"]) { + [self.currentSession.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"stopped"] isAlive:true]; [self sendError:@"cancel" message:@"Session is stopped." command:self.sessionCommand]; } } diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index c9fb5cd..236f24e 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -373,17 +373,17 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didStartCastSession:( } - (void)sessionManager:(GCKSessionManager *)sessionManager didEndCastSession:(GCKCastSession *)session withError:(NSError *)error { - self.currentSession = nil; - self.remoteMediaClient = nil; +// self.currentSession = nil; +// self.remoteMediaClient = nil; if (error != nil) { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.debugDescription]; [self.commandDelegate sendPluginResult:pluginResult callbackId:self.initialCommand.callbackId]; } if ([self.sessionStatus isEqualToString:@""]) { - [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session] isAlive:false]; + [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"stopped"] isAlive:true]; } else { - [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:self.sessionStatus] isAlive:false]; + [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:self.sessionStatus] isAlive:true]; } } From 9d4d0701ba2d4a63bdcfd5c42b52c4f1d32955c0 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sat, 16 Nov 2019 18:51:50 -0700 Subject: [PATCH 079/166] Fix bug. Js failure if trying to unset media on non-existent session. --- www/chrome.cast.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/www/chrome.cast.js b/www/chrome.cast.js index 77fe342..afa2cc4 100644 --- a/www/chrome.cast.js +++ b/www/chrome.cast.js @@ -1383,7 +1383,9 @@ execute('setup', function (err, args) { MEDIA_UPDATE: function (media) { if (!media) { _currentMedia = null; - _session.media = []; + if (_session) { + _session.media = []; + } return; } if (!_currentMedia) { From ebfa96d6070566165ce89438525bb1ef267e79b8 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sat, 16 Nov 2019 21:00:26 -0700 Subject: [PATCH 080/166] Add tests that specifically test that initialize will return any active session on new page loads. (Or will not return a session that has been left (session.leave).) Keep track of test progress through page reloads and app restarts via cookies. --- .../chrome/tests_manual_primary_1_chrome.html | 4 +- .../chrome/tests_manual_primary_2_chrome.html | 4 +- tests/www/html/tests_manual_primary_1.html | 4 +- tests/www/html/tests_manual_primary_2.html | 4 +- tests/www/js/tests_manual_primary_1.js | 236 ++++++++++++++---- tests/www/js/tests_manual_primary_2.js | 161 +++++++++--- tests/www/js/tests_manual_secondary.js | 22 +- tests/www/js/utils.js | 33 +++ 8 files changed, 366 insertions(+), 102 deletions(-) diff --git a/tests/www/chrome/tests_manual_primary_1_chrome.html b/tests/www/chrome/tests_manual_primary_1_chrome.html index 5ed894c..eeeea42 100644 --- a/tests/www/chrome/tests_manual_primary_1_chrome.html +++ b/tests/www/chrome/tests_manual_primary_1_chrome.html @@ -19,12 +19,12 @@

      Manual Tests (Primary Device) Part 1

      - - diff --git a/tests/www/chrome/tests_manual_primary_2_chrome.html b/tests/www/chrome/tests_manual_primary_2_chrome.html index 9d61282..32dced1 100644 --- a/tests/www/chrome/tests_manual_primary_2_chrome.html +++ b/tests/www/chrome/tests_manual_primary_2_chrome.html @@ -19,12 +19,12 @@

      Manual Tests (Primary Device) Part 2

      - - diff --git a/tests/www/html/tests_manual_primary_1.html b/tests/www/html/tests_manual_primary_1.html index 87d39f3..5edf6a7 100644 --- a/tests/www/html/tests_manual_primary_1.html +++ b/tests/www/html/tests_manual_primary_1.html @@ -20,12 +20,12 @@

      Manual Tests (Primary Device) Part 1

      - - diff --git a/tests/www/html/tests_manual_primary_2.html b/tests/www/html/tests_manual_primary_2.html index 4712f24..5f694c4 100644 --- a/tests/www/html/tests_manual_primary_2.html +++ b/tests/www/html/tests_manual_primary_2.html @@ -20,12 +20,12 @@

      Manual Tests (Primary Device) Part 2

      - - diff --git a/tests/www/js/tests_manual_primary_1.js b/tests/www/js/tests_manual_primary_1.js index 1889e41..0fb023e 100644 --- a/tests/www/js/tests_manual_primary_1.js +++ b/tests/www/js/tests_manual_primary_1.js @@ -39,15 +39,32 @@ var media; before('Api should be available and initialize successfully', function (done) { + this.timeout(10000); + utils.setAction('Running tests...
      Please wait for instruction'); session = null; var interval = setInterval(function () { if (chrome && chrome.cast && chrome.cast.isAvailable) { clearInterval(interval); - initializeApi(); + done(); } }, 100); - function initializeApi () { + }); + + describe('App restart and reload/change page simulation', function () { + var cookieName = 'primary-p1_restart-reload'; + var runningNum = parseInt(utils.getCookie(cookieName) || '0'); + it('Create session', function (done) { + this.timeout(10000); + if (runningNum > 0) { + // Just pass the test because we need to skip ahead + return done(); + } + + // Else, initialize and create the session (Should not receive session on initialize) + utils.setAction('Initializing...'); + var finished = false; // Need this so we stop testing after being finished + var failed = false; var unavailable = 'unavailable'; var available = 'available'; var called = utils.callOrder([ @@ -56,16 +73,26 @@ { id: available, repeats: true } ], function () { finished = true; - done(); + // Initialize finished correctly, now create the session + utils.setAction('Creating session...'); + utils.startSession(function (sess) { + session = sess; + utils.testSessionProperties(sess); + utils.setCookie(cookieName, ++runningNum); + if (failed) { + // Ensure the session has stopped on failure because + // we might not hit this point until after the "after" has already run + session.stop(); + } + done(); + }); }); var apiConfig = new chrome.cast.ApiConfig( new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function (sess) { - if (!finished) { - assert.fail('got session before "success", "unavailable", "available" sequence completed'); - } + failed = true; session = sess; - utils.testSessionProperties(sess); + assert.fail('Should not receive session on initialize. We should only call this initialize when there is no existing session.'); }, function receiverListener (availability) { if (!finished) { called(availability); @@ -76,46 +103,171 @@ }, function (err) { assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); - } - }); + }); + it('Reload after session create, should receive session on initialize', function (done) { + this.timeout(10000); + var instructionNum = 1; + var testNum = 2; + assert.isAtLeast(runningNum, instructionNum, 'Should not be running this test yet'); + switch (runningNum) { + case instructionNum: + // Start the reload + utils.setAction('Reloading...'); + utils.setCookie(cookieName, ++runningNum); + window.location.reload(); + break; + case testNum: + // Test initialize since we just reloaded + utils.setAction('Testing reload after session create, should receive session...'); + var finished = false; // Need this so we stop testing after being finished + var unavailable = 'unavailable'; + var available = 'available'; + var session_listener = 'session_listener'; + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: unavailable, repeats: true }, + { id: available, repeats: true }, + { id: session_listener, repeats: false } + ], function () { + finished = true; + utils.setCookie(cookieName, ++runningNum); + done(); + }); + var apiConfig = new chrome.cast.ApiConfig( + new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), + function (sess) { + session = sess; + utils.testSessionProperties(sess); + called(session_listener); + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); + chrome.cast.initialize(apiConfig, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + break; - // Must be the first test - describe('App restart', function () { + default: + // We must be looking to run a test further down the line + return done(); + } + }); it('Restart app with active session, should receive session on initialize', function (done) { - var interval; - var time = 15000; - interval = setInterval(function () { - time -= 500; - if (session) { - clearInterval(interval); - done(); - } - if (time < 0) { - clearInterval(interval); - assert.fail('Failed to find session for 15s after app restart. ' - + 'Make sure that a session started by this device is active during app restart.'); - } - }, 500); - utils.setAction('Situation #1 - First time you have reached the "restart app" test:
      ' - + '    1. Click "Start Session"

      ' - + 'Situation #2 - You have force killed and restarted the app:
      ' - + '    1. Wait for session discovery. (Fails if none found after 15s)
      ', - 'Start Session', - function () { - clearInterval(interval); - utils.startSession(function (sess) { - session = sess; - if (isDesktop) { - utils.setAction('1. Refresh this page.'); - } else { - utils.setAction('1. Force kill and restart the app.' - + '
      *Android 4.4 does not support this feature, so just refresh the page.'); - } - }); + var instructionNum = 3; + var testNum = 4; + assert.isAtLeast(runningNum, instructionNum, 'Should not be running this test yet'); + switch (runningNum) { + case instructionNum: + // Show instructions for app restart + utils.setCookie(cookieName, testNum); + if (isDesktop) { + // If desktop, just reload the page (because restart doesn't work) + window.location.reload(); } - ); + this.timeout(0); // no timeout + utils.setAction('Force kill and restart the app, and navigate back to Manual Tests (Primary) Part 1.' + + '
      Note: Android 4.4 does not support this feature, so just refresh the page.'); + break; + case testNum: + this.timeout(10000); + // Test initialize since we just reloaded + utils.setAction('Testing initialize after app restart, should receive a session...'); + var finished = false; // Need this so we stop testing after being finished + var unavailable = 'unavailable'; + var available = 'available'; + var session_listener = 'session_listener'; + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: unavailable, repeats: true }, + { id: available, repeats: true }, + { id: session_listener, repeats: false } + ], function () { + finished = true; + utils.setCookie(cookieName, ++runningNum); + done(); + }); + var apiConfig = new chrome.cast.ApiConfig( + new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), + function (sess) { + session = sess; + utils.testSessionProperties(sess); + called(session_listener); + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); + chrome.cast.initialize(apiConfig, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + break; + + default: + // We must be looking to run a test further down the line + return done(); + } + }); + it('Reload after app restart, should receive session on initialize', function (done) { + this.timeout(10000); + var instructionNum = 5; + var testNum = 6; + assert.isAtLeast(runningNum, instructionNum, 'Should not be running this test yet'); + switch (runningNum) { + case instructionNum: + // Start the reload + utils.setAction('Reloading...'); + utils.setCookie(cookieName, ++runningNum); + window.location.reload(); + break; + case testNum: + // Test initialize since we just reloaded + utils.setAction('Testing reload after app restart, should receive a session...'); + var finished = false; // Need this so we stop testing after being finished + var unavailable = 'unavailable'; + var available = 'available'; + var session_listener = 'session_listener'; + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: unavailable, repeats: true }, + { id: available, repeats: true }, + { id: session_listener, repeats: false } + ], function () { + finished = true; + utils.setCookie(cookieName, ++runningNum); + done(); + }); + var apiConfig = new chrome.cast.ApiConfig( + new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), + function (sess) { + session = sess; + utils.testSessionProperties(sess); + called(session_listener); + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); + chrome.cast.initialize(apiConfig, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + break; + + default: + // We must be looking to run a test further down the line + return done(); + } }); after('Ensure session is stopped', function (done) { + // Reset tests + utils.setCookie(cookieName, 0); if (!session) { return done(); } diff --git a/tests/www/js/tests_manual_primary_2.js b/tests/www/js/tests_manual_primary_2.js index 3975429..c1b3558 100644 --- a/tests/www/js/tests_manual_primary_2.js +++ b/tests/www/js/tests_manual_primary_2.js @@ -32,6 +32,7 @@ var session; before('Api should be available and initialize successfully', function (done) { + this.timeout(10000); session = null; var interval = setInterval(function () { if (chrome && chrome.cast && chrome.cast.isAvailable) { @@ -40,53 +41,131 @@ } }, 100); }); - it('Should not receive a session on initialize', function (done) { - var finished = false; // Need this so we stop testing after being finished - var unavailable = 'unavailable'; - var available = 'available'; - var called = utils.callOrder([ - { id: success, repeats: false }, - { id: unavailable, repeats: true }, - { id: available, repeats: true } - ], function () { - finished = true; - done(); + describe('App restart and reload/change page simulation', function () { + var cookieName = 'primary-p2_restart-reload'; + var runningNum = parseInt(utils.getCookie(cookieName) || '0'); + it('Should not receive a session on initialize after a page change', function (done) { + this.timeout(10000); + if (runningNum > 0) { + // Just pass the test because we need to skip ahead + return done(); + } + utils.setAction('Checking for session after page load, (should not find session)...'); + var finished = false; // Need this so we stop testing after being finished + var unavailable = 'unavailable'; + var available = 'available'; + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: unavailable, repeats: true }, + { id: available, repeats: true } + ], function () { + finished = true; + // Give it an extra moment to check for the session + setTimeout(function () { + utils.setCookie(cookieName, ++runningNum); + done(); + }, 1000); + }); + var apiConfig = new chrome.cast.ApiConfig( + new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), + function (sess) { + session = sess; + if (!isDesktop) { + assert.fail('should not receive a session (make sure there is no active cast session when starting the tests)'); + } + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); + chrome.cast.initialize(apiConfig, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - var apiConfig = new chrome.cast.ApiConfig( - new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), - function (sess) { - session = sess; - if (!isDesktop) { - assert.fail('should not receive a session (make sure there is no active cast session when starting the tests)'); + it('Should not receive a session on initialize after app restart', function (done) { + var instructionNum = 1; + var testNum = 2; + assert.isAtLeast(runningNum, instructionNum, 'Should not be running this test yet'); + switch (runningNum) { + case instructionNum: + // Show instructions for app restart + utils.setCookie(cookieName, testNum); + if (isDesktop) { + // If desktop, just reload the page (because restart doesn't work) + window.location.reload(); } - }, function receiverListener (availability) { - if (!finished) { - called(availability); - } - }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); - chrome.cast.initialize(apiConfig, function () { - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + this.timeout(0); // no timeout + utils.setAction('Force kill and restart the app, and navigate back to Manual Tests (Primary) Part 2.' + + '
      Note: Android 4.4 does not support this feature, so just refresh the page.'); + break; + case testNum: + this.timeout(10000); + // Test initialize since we just reloaded + utils.setAction('Checking for session after app restart, (should not find session)...'); + var finished = false; // Need this so we stop testing after being finished + var unavailable = 'unavailable'; + var available = 'available'; + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: unavailable, repeats: true }, + { id: available, repeats: true } + ], function () { + finished = true; + // Give it an extra moment to check for the session + setTimeout(function () { + utils.setCookie(cookieName, ++runningNum); + done(); + }, 1000); + }); + var apiConfig = new chrome.cast.ApiConfig( + new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), + function (sess) { + session = sess; + if (!isDesktop) { + assert.fail('should not receive a session (make sure there is no active cast session when starting the tests)'); + } + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); + chrome.cast.initialize(apiConfig, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + break; + default: + // We must be looking to run a test further down the line + return done(); + } + }); + after(function () { + // Reset tests + utils.setCookie(cookieName, 0); }); }); - it('Create session', function (done) { - utils.setAction('On secondary click "Start Part 2".', 'Enter Session', function () { - utils.startSession(function (sess) { - session = sess; - utils.testSessionProperties(session); - utils.setAction('On secondary click "Continue".'); - done(); + describe('session interaction with secondary', function () { + it('Create session', function (done) { + utils.setAction('On secondary click "Start Part 2".', 'Enter Session', function () { + utils.startSession(function (sess) { + session = sess; + utils.testSessionProperties(session); + utils.setAction('On secondary click "Continue".'); + done(); + }); }); }); - }); - it('External session.stop should kill this session as well', function (done) { - session.addUpdateListener(function listener (isAlive) { - if (session.status === chrome.cast.SessionStatus.STOPPED) { - assert.isFalse(isAlive); - session.removeUpdateListener(listener); - done(); - } + it('External session.stop should kill this session as well', function (done) { + session.addUpdateListener(function listener (isAlive) { + if (session.status === chrome.cast.SessionStatus.STOPPED) { + assert.isFalse(isAlive); + session.removeUpdateListener(listener); + done(); + } + }); }); }); after('Ensure we have stopped the session', function (done) { diff --git a/tests/www/js/tests_manual_secondary.js b/tests/www/js/tests_manual_secondary.js index 2ec5ce8..77585ca 100644 --- a/tests/www/js/tests_manual_secondary.js +++ b/tests/www/js/tests_manual_secondary.js @@ -85,7 +85,7 @@ initializeApi(); } }, 100); - function initializeApi() { + function initializeApi () { var finished = false; // Need this so we stop testing after being finished var unavailable = 'unavailable'; var available = 'available'; @@ -101,7 +101,7 @@ new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function (sess) { assert.fail('should not receive a session (make sure there is no active cast session when starting the tests)'); - }, function receiverListener(availability) { + }, function receiverListener (availability) { if (!finished) { called(availability); } @@ -158,7 +158,7 @@ assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.GENERIC); assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.GENERIC); - media.addUpdateListener(function listener(isAlive) { + media.addUpdateListener(function listener (isAlive) { assert.isTrue(isAlive); utils.testMediaProperties(media); assert.oneOf(media.playerState, [ @@ -182,7 +182,7 @@ ], function () { done(); }); - media.addUpdateListener(function listener(isAlive) { + media.addUpdateListener(function listener (isAlive) { if (media.playerState === chrome.cast.media.PlayerState.IDLE) { media.removeUpdateListener(listener); assert.equal(media.idleReason, chrome.cast.media.IdleReason.CANCELLED); @@ -242,7 +242,7 @@ assert.equal(media.items[i].media.metadata.myMadeUpMetadata, audioItem.metadata.myMadeUpMetadata); assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.MUSIC_TRACK); assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.MUSIC_TRACK); - media.addUpdateListener(function listener(isAlive) { + media.addUpdateListener(function listener (isAlive) { assert.isTrue(isAlive); utils.testMediaProperties(media); assert.oneOf(media.playerState, [ @@ -274,7 +274,7 @@ calledAnyOrder(update); }); var i = utils.getCurrentItemIndex(media); - media.addUpdateListener(function listener(isAlive) { + media.addUpdateListener(function listener (isAlive) { if (media.playerState === chrome.cast.media.PlayerState.IDLE) { assert.oneOf(media.idleReason, [chrome.cast.media.IdleReason.INTERRUPTED, chrome.cast.media.IdleReason.FINISHED]); @@ -321,15 +321,15 @@ }); it('Primary should not receive session on initialize', function (done) { this.timeout(240000); - utils.setAction('On primary:
      1. Force kill and restart the app.' - + '
      2. Select Manual Tests (Primary) Part 2 from the home page to finish the manual tests.', 'Start Part 2', done); + utils.setAction('On primary:
      1. Click "Back".' + + '
      2. Select Manual Tests (Primary) Part 2 from the home page.', 'Start Part 2', done); }); it('Secondary session.leave should cause session to end (because all senders have left)', function (done) { var called = utils.waitForAllCalls([ { id: success, repeats: false }, { id: update, repeats: true } ], done); - session.addUpdateListener(function listener(isAlive) { + session.addUpdateListener(function listener (isAlive) { if (session.status === chrome.cast.SessionStatus.DISCONNECTED) { assert.isTrue(isAlive); session.removeUpdateListener(listener); @@ -365,7 +365,7 @@ { id: success, repeats: false }, { id: update, repeats: true } ], done); - session.addUpdateListener(function listener(isAlive) { + session.addUpdateListener(function listener (isAlive) { if (session.status === chrome.cast.SessionStatus.STOPPED) { assert.isFalse(isAlive); session.removeUpdateListener(listener); @@ -404,4 +404,4 @@ return runner; }; -}()); \ No newline at end of file +}()); diff --git a/tests/www/js/utils.js b/tests/www/js/utils.js index f9d0f17..89d7bd6 100644 --- a/tests/www/js/utils.js +++ b/tests/www/js/utils.js @@ -19,6 +19,33 @@ var utils = {}; + utils.setCookie = function (name, value) { + document.cookie = name + '=' + value + ';'; + }; + + utils.getCookie = function (name) { + name = name + '='; + var ca = document.cookie.split(';'); + for (var i = 0; i < ca.length; i++) { + var c = ca[i].trim(); + if (c.indexOf(name) === 0) { + return c.substring(name.length, c.length); + } + } + return ''; + }; + + utils.clearCookies = function () { + var cookies = document.cookie.split(';'); + + for (var i = 0; i < cookies.length; i++) { + var cookie = cookies[i]; + var eqPos = cookie.indexOf('='); + var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; + document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT'; + } + }; + /** * Displays the action information. */ @@ -350,6 +377,12 @@ } }; + document.addEventListener('DOMContentLoaded', function (event) { + // Clear test cookies on navigation away + document.getElementById('back').onclick = utils.clearCookies; + document.getElementById('rerun').onclick = utils.clearCookies; + }); + window['cordova-plugin-chromecast-tests'] = window['cordova-plugin-chromecast-tests'] || {}; window['cordova-plugin-chromecast-tests'].utils = utils; }()); From fa39558b70603c2a0e1515b29448da2e96a9fb42 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sat, 16 Nov 2019 22:01:09 -0700 Subject: [PATCH 081/166] Improve manual instructions a bit and fix forgotten closing quotes in instruction. --- tests/www/js/tests_manual_secondary.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/www/js/tests_manual_secondary.js b/tests/www/js/tests_manual_secondary.js index 77585ca..2e33e26 100644 --- a/tests/www/js/tests_manual_secondary.js +++ b/tests/www/js/tests_manual_secondary.js @@ -321,8 +321,9 @@ }); it('Primary should not receive session on initialize', function (done) { this.timeout(240000); - utils.setAction('On primary:
      1. Click "Back".' - + '
      2. Select Manual Tests (Primary) Part 2 from the home page.', 'Start Part 2', done); + utils.setAction('1. On primary, click "Back".' + + '
      2. On primary, Select Manual Tests (Primary) Part 2.' + + '
      3. Wait for instructions from primary.', 'Start Part 2', done); }); it('Secondary session.leave should cause session to end (because all senders have left)', function (done) { var called = utils.waitForAllCalls([ @@ -349,11 +350,11 @@ // from chrome first and then join from the app. utils.startSession(function (sess) { session = sess; - utils.setAction('1. On primary click "Enter Session
      2. Wait for instructions from primary.', 'Continue', done); + utils.setAction('1. On primary click "Enter Session"
      2. Wait for instructions from primary.', 'Continue', done); }); return; } - utils.setAction('On primary click "Enter Session', 'Continue', function () { + utils.setAction('On primary click "Enter Session"', 'Continue', function () { utils.startSession(function (sess) { session = sess; done(); From a8183876f9fc5a4e118ce388a2d6acc2aed03864 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 17 Nov 2019 00:01:33 -0700 Subject: [PATCH 082/166] Apparently ios deletes cookies during app restart. So switch to localstorage to persist the data through the app restart tests. --- tests/www/js/tests_manual_primary_1.js | 18 +++++++-------- tests/www/js/tests_manual_primary_2.js | 10 ++++----- tests/www/js/utils.js | 31 +++++++------------------- 3 files changed, 22 insertions(+), 37 deletions(-) diff --git a/tests/www/js/tests_manual_primary_1.js b/tests/www/js/tests_manual_primary_1.js index 0fb023e..740eb69 100644 --- a/tests/www/js/tests_manual_primary_1.js +++ b/tests/www/js/tests_manual_primary_1.js @@ -52,7 +52,7 @@ describe('App restart and reload/change page simulation', function () { var cookieName = 'primary-p1_restart-reload'; - var runningNum = parseInt(utils.getCookie(cookieName) || '0'); + var runningNum = parseInt(utils.getValue(cookieName) || '0'); it('Create session', function (done) { this.timeout(10000); if (runningNum > 0) { @@ -78,7 +78,7 @@ utils.startSession(function (sess) { session = sess; utils.testSessionProperties(sess); - utils.setCookie(cookieName, ++runningNum); + utils.storeValue(cookieName, ++runningNum); if (failed) { // Ensure the session has stopped on failure because // we might not hit this point until after the "after" has already run @@ -113,7 +113,7 @@ case instructionNum: // Start the reload utils.setAction('Reloading...'); - utils.setCookie(cookieName, ++runningNum); + utils.storeValue(cookieName, ++runningNum); window.location.reload(); break; case testNum: @@ -130,7 +130,7 @@ { id: session_listener, repeats: false } ], function () { finished = true; - utils.setCookie(cookieName, ++runningNum); + utils.storeValue(cookieName, ++runningNum); done(); }); var apiConfig = new chrome.cast.ApiConfig( @@ -163,7 +163,7 @@ switch (runningNum) { case instructionNum: // Show instructions for app restart - utils.setCookie(cookieName, testNum); + utils.storeValue(cookieName, testNum); if (isDesktop) { // If desktop, just reload the page (because restart doesn't work) window.location.reload(); @@ -187,7 +187,7 @@ { id: session_listener, repeats: false } ], function () { finished = true; - utils.setCookie(cookieName, ++runningNum); + utils.storeValue(cookieName, ++runningNum); done(); }); var apiConfig = new chrome.cast.ApiConfig( @@ -222,7 +222,7 @@ case instructionNum: // Start the reload utils.setAction('Reloading...'); - utils.setCookie(cookieName, ++runningNum); + utils.storeValue(cookieName, ++runningNum); window.location.reload(); break; case testNum: @@ -239,7 +239,7 @@ { id: session_listener, repeats: false } ], function () { finished = true; - utils.setCookie(cookieName, ++runningNum); + utils.storeValue(cookieName, ++runningNum); done(); }); var apiConfig = new chrome.cast.ApiConfig( @@ -267,7 +267,7 @@ }); after('Ensure session is stopped', function (done) { // Reset tests - utils.setCookie(cookieName, 0); + utils.storeValue(cookieName, 0); if (!session) { return done(); } diff --git a/tests/www/js/tests_manual_primary_2.js b/tests/www/js/tests_manual_primary_2.js index c1b3558..37d2558 100644 --- a/tests/www/js/tests_manual_primary_2.js +++ b/tests/www/js/tests_manual_primary_2.js @@ -43,7 +43,7 @@ }); describe('App restart and reload/change page simulation', function () { var cookieName = 'primary-p2_restart-reload'; - var runningNum = parseInt(utils.getCookie(cookieName) || '0'); + var runningNum = parseInt(utils.getValue(cookieName) || '0'); it('Should not receive a session on initialize after a page change', function (done) { this.timeout(10000); if (runningNum > 0) { @@ -62,7 +62,7 @@ finished = true; // Give it an extra moment to check for the session setTimeout(function () { - utils.setCookie(cookieName, ++runningNum); + utils.storeValue(cookieName, ++runningNum); done(); }, 1000); }); @@ -91,7 +91,7 @@ switch (runningNum) { case instructionNum: // Show instructions for app restart - utils.setCookie(cookieName, testNum); + utils.storeValue(cookieName, testNum); if (isDesktop) { // If desktop, just reload the page (because restart doesn't work) window.location.reload(); @@ -115,7 +115,7 @@ finished = true; // Give it an extra moment to check for the session setTimeout(function () { - utils.setCookie(cookieName, ++runningNum); + utils.storeValue(cookieName, ++runningNum); done(); }, 1000); }); @@ -144,7 +144,7 @@ }); after(function () { // Reset tests - utils.setCookie(cookieName, 0); + utils.storeValue(cookieName, 0); }); }); describe('session interaction with secondary', function () { diff --git a/tests/www/js/utils.js b/tests/www/js/utils.js index 89d7bd6..1e9946f 100644 --- a/tests/www/js/utils.js +++ b/tests/www/js/utils.js @@ -19,31 +19,16 @@ var utils = {}; - utils.setCookie = function (name, value) { - document.cookie = name + '=' + value + ';'; + utils.storeValue = function (name, value) { + localStorage.setItem(name, value); }; - utils.getCookie = function (name) { - name = name + '='; - var ca = document.cookie.split(';'); - for (var i = 0; i < ca.length; i++) { - var c = ca[i].trim(); - if (c.indexOf(name) === 0) { - return c.substring(name.length, c.length); - } - } - return ''; + utils.getValue = function (name) { + return localStorage.getItem(name); }; - utils.clearCookies = function () { - var cookies = document.cookie.split(';'); - - for (var i = 0; i < cookies.length; i++) { - var cookie = cookies[i]; - var eqPos = cookie.indexOf('='); - var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; - document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT'; - } + utils.clearStoredValues = function () { + localStorage.clear(); }; /** @@ -379,8 +364,8 @@ document.addEventListener('DOMContentLoaded', function (event) { // Clear test cookies on navigation away - document.getElementById('back').onclick = utils.clearCookies; - document.getElementById('rerun').onclick = utils.clearCookies; + document.getElementById('back').onclick = utils.clearStoredValues; + document.getElementById('rerun').onclick = utils.clearStoredValues; }); window['cordova-plugin-chromecast-tests'] = window['cordova-plugin-chromecast-tests'] || {}; From 679c44667aea8b31fc417e3782fd4e6c86346fe4 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 17 Nov 2019 00:04:05 -0700 Subject: [PATCH 083/166] Move the sessionRejoin event to after the initialize success callback. The order of events for initialize when a session is present should be: 1) initialize callback success 2) RECEIVER_LISTENER (false) 3) RECEIVER_LISTENER (true) 4) SESSION_LISTENER (session) --- src/ios/Chromecast.m | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index e8bdf77..c7db20c 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -63,10 +63,6 @@ -(void) initialize:(CDVInvokedUrlCommand*)command { GCKLogger.sharedInstance.delegate = self; // [self log:[NSString stringWithFormat:@"API Initialized with appID %@", appId]]; - if ([GCKCastContext sharedInstance].sessionManager.currentCastSession != nil) { - [self onSessionRejoin:[CastUtilities createSessionObject:[GCKCastContext sharedInstance].sessionManager.currentCastSession]]; - } - // ChromecastSession *session = [[ChromecastSession alloc] init]; [[GCKCastContext sharedInstance].sessionManager addListener:self]; @@ -74,6 +70,9 @@ -(void) initialize:(CDVInvokedUrlCommand*)command { [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; [self checkReceiverAvailable]; + if ([GCKCastContext sharedInstance].sessionManager.currentCastSession != nil) { + [self onSessionRejoin:[CastUtilities createSessionObject:[GCKCastContext sharedInstance].sessionManager.currentCastSession]]; + } } - (BOOL)stopRouteScanForSetup:(CDVInvokedUrlCommand*)command { From 0bce6c47aa9cbcb34bb7acacd8fd5e13fc31baa6 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 17 Nov 2019 01:52:21 -0700 Subject: [PATCH 084/166] Give slightly longer timeout to accommodate slower devices --- tests/www/js/tests_manual_primary_1.js | 10 +++++----- tests/www/js/tests_manual_primary_2.js | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/www/js/tests_manual_primary_1.js b/tests/www/js/tests_manual_primary_1.js index 740eb69..d78c074 100644 --- a/tests/www/js/tests_manual_primary_1.js +++ b/tests/www/js/tests_manual_primary_1.js @@ -39,7 +39,7 @@ var media; before('Api should be available and initialize successfully', function (done) { - this.timeout(10000); + this.timeout(15000); utils.setAction('Running tests...
      Please wait for instruction'); session = null; var interval = setInterval(function () { @@ -54,7 +54,7 @@ var cookieName = 'primary-p1_restart-reload'; var runningNum = parseInt(utils.getValue(cookieName) || '0'); it('Create session', function (done) { - this.timeout(10000); + this.timeout(15000); if (runningNum > 0) { // Just pass the test because we need to skip ahead return done(); @@ -105,7 +105,7 @@ }); }); it('Reload after session create, should receive session on initialize', function (done) { - this.timeout(10000); + this.timeout(15000); var instructionNum = 1; var testNum = 2; assert.isAtLeast(runningNum, instructionNum, 'Should not be running this test yet'); @@ -173,7 +173,7 @@ + '
      Note: Android 4.4 does not support this feature, so just refresh the page.'); break; case testNum: - this.timeout(10000); + this.timeout(15000); // Test initialize since we just reloaded utils.setAction('Testing initialize after app restart, should receive a session...'); var finished = false; // Need this so we stop testing after being finished @@ -214,7 +214,7 @@ } }); it('Reload after app restart, should receive session on initialize', function (done) { - this.timeout(10000); + this.timeout(15000); var instructionNum = 5; var testNum = 6; assert.isAtLeast(runningNum, instructionNum, 'Should not be running this test yet'); diff --git a/tests/www/js/tests_manual_primary_2.js b/tests/www/js/tests_manual_primary_2.js index 37d2558..ea8e123 100644 --- a/tests/www/js/tests_manual_primary_2.js +++ b/tests/www/js/tests_manual_primary_2.js @@ -32,7 +32,7 @@ var session; before('Api should be available and initialize successfully', function (done) { - this.timeout(10000); + this.timeout(15000); session = null; var interval = setInterval(function () { if (chrome && chrome.cast && chrome.cast.isAvailable) { @@ -45,7 +45,7 @@ var cookieName = 'primary-p2_restart-reload'; var runningNum = parseInt(utils.getValue(cookieName) || '0'); it('Should not receive a session on initialize after a page change', function (done) { - this.timeout(10000); + this.timeout(15000); if (runningNum > 0) { // Just pass the test because we need to skip ahead return done(); @@ -101,7 +101,7 @@ + '
      Note: Android 4.4 does not support this feature, so just refresh the page.'); break; case testNum: - this.timeout(10000); + this.timeout(15000); // Test initialize since we just reloaded utils.setAction('Checking for session after app restart, (should not find session)...'); var finished = false; // Need this so we stop testing after being finished From 470e308006f2c78b9d24994f94a619b4aecb122d Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 17 Nov 2019 02:06:18 -0700 Subject: [PATCH 085/166] Fix session being returned twice on app restart / session rejoin --- src/ios/Chromecast.m | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index c7db20c..9c5da03 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -502,7 +502,6 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeCastSession: } - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeSession:(GCKSession *)session { - [self onSessionRejoin:[CastUtilities createSessionObject:session]]; } - (void)sessionManager:(GCKSessionManager *)sessionManager didEndSession:(GCKSession *)session withError:(NSError *)error { From cb30f87ca10236c4a0fbe4c206dbcdf59fe15d49 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 17 Nov 2019 05:09:05 -0700 Subject: [PATCH 086/166] Fix for iOS 9. iOS 9 does not support [NSTimer scheduledTimerWithTimeInterval]. Also clean up start/stop scan and selectroute. Make it so that startScan has a hope of sending updates as available routes change. selectRoute now also actually tries for 15 seconds. --- src/ios/Chromecast.m | 139 +++++++++++++++++-------------------------- 1 file changed, 56 insertions(+), 83 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 9c5da03..7419152 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -32,7 +32,7 @@ - (void)log:(NSString*)s { - (void)setup:(CDVInvokedUrlCommand*) command { self.eventCommand = command; - [self stopRouteScanForSetup:self.scanCommand]; + [self stopRouteScanForSetup]; [self sendEvent:@"SETUP" args:@[]]; } @@ -75,44 +75,29 @@ -(void) initialize:(CDVInvokedUrlCommand*)command { } } -- (BOOL)stopRouteScanForSetup:(CDVInvokedUrlCommand*)command { - +- (void)stopRouteScanForSetup { if (self.scanCommand != nil) { [self sendError:@"cancel" message:@"Scan stopped because setup triggered." command:self.scanCommand]; self.scanCommand = nil; + } + [self stopRouteScan]; +} + +- (BOOL)stopRouteScan:(CDVInvokedUrlCommand*)command { + [self stopRouteScan]; + if (command != nil) { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]]; - [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } else { - [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; - if (command != nil) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]]; - [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } } - return YES; } -- (BOOL)stopRouteScan:(CDVInvokedUrlCommand*)command { - +- (void)stopRouteScan { if (self.scanCommand != nil) { [self sendError:@"cancel" message:@"Scan stopped." command:self.scanCommand]; self.scanCommand = nil; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]]; - [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } else { - [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; - if (command != nil) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]];  - [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } } - - return YES; + [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; } -(BOOL) startRouteScan:(CDVInvokedUrlCommand*)command { @@ -120,31 +105,18 @@ -(BOOL) startRouteScan:(CDVInvokedUrlCommand*)command { [self sendError:@"cancel" message:@"Started a new route scan before stopping previous one." command:self.scanCommand]; } self.scanCommand = command; - [self startRotueScanWithTimer:0 completion:nil]; + [self sendScanUpdate]; + [[GCKCastContext sharedInstance].discoveryManager startDiscovery]; return YES; } -- (void)startRotueScanWithTimer:(NSTimeInterval)timer completion:(void(^)(void))completion { - if (timer != 0) { - if (completion != nil) { - if ([GCKCastContext sharedInstance].discoveryManager.discoveryActive) { - [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; - [self sendReceiverData]; - } else { - [NSTimer scheduledTimerWithTimeInterval:timer repeats:NO block:^(NSTimer * _Nonnull timer) { - completion(); - }]; - - } - } - } else { - if ([GCKCastContext sharedInstance].discoveryManager.discoveryActive) { - [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; - [self sendReceiverData]; - } else { - [[GCKCastContext sharedInstance].discoveryManager startDiscovery]; - [self sendReceiverData]; - } - } + +- (void)sendScanUpdate { + if (self.scanCommand == nil) { + return; + } + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:[CastUtilities createDeviceObject:self.devicesAvailable]]; + [pluginResult setKeepCallback:@(true)]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.scanCommand.callbackId]; } - (void)checkReceiverAvailable { @@ -155,20 +127,6 @@ - (void)checkReceiverAvailable { } } -- (void)sendReceiverData { - if (self.scanCommand == nil) { - return; - } - GCKSessionManager* sessionManager = GCKCastContext.sharedInstance.sessionManager; -// [sessionManager startSessionWithDevice:<#(nonnull GCKDevice *)#>] - if (self.devicesAvailable.count > 0 || sessionManager.currentSession != nil) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:[CastUtilities createDeviceObject:self.devicesAvailable]]; - [pluginResult setKeepCallback:@(true)]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:self.scanCommand.callbackId]; - }else { - [self stopRouteScan:self.scanCommand]; - } -} - (void)requestSession:(CDVInvokedUrlCommand*) command { UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Cast to" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; for (GCKDevice* device in self.devicesAvailable) { @@ -355,30 +313,41 @@ - (void)mediaEditTracksInfo:(CDVInvokedUrlCommand*)command { } - (void)selectRoute:(CDVInvokedUrlCommand*)command { + if ([GCKCastContext sharedInstance].sessionManager.currentCastSession != nil || [GCKCastContext sharedInstance].sessionManager.connectionState == GCKConnectionStateConnected) { + [self sendError:@"session_error" message:@"Leave or stop current session before attempting to join new session." command:command]; + return; + } + NSString* routeID = command.arguments[0]; - GCKDevice* device = [[GCKCastContext sharedInstance].discoveryManager deviceWithUniqueID:routeID]; - int retries = 0; - //testing purpose - if ([routeID isEqualToString:@""]) { - if ([GCKCastContext sharedInstance].sessionManager.currentCastSession != nil || [GCKCastContext sharedInstance].sessionManager.connectionState == GCKConnectionStateConnected) { - [self sendError:@"session_error" message:@"Leave or stop current session before attempting to join new session." command:command]; - } + [self selectRouteRecursive:routeID forTime:15 command:command timesRetried:0]; +} + +// Check for a device with the routeID every 1.5 second forTime seconds +- (void)selectRouteRecursive:(NSString*)routeID forTime:(int)remainTime command:(CDVInvokedUrlCommand*)command timesRetried:(int)retries { + GCKDevice* device = [[GCKCastContext sharedInstance].discoveryManager deviceWithUniqueID:routeID]; + if (device != nil) { + self.currentSession = [[ChromecastSession alloc] initWithDevice:device cordovaDelegate:self.commandDelegate initialCommand:command]; + [self.currentSession add:self]; + return; } - if (device == nil) { - [self startRotueScanWithTimer:1 completion:^{ - [self sendError:@"timeout" message:[NSString stringWithFormat:@"Failed to join route (%@) after 15s and %d tries.",routeID,retries + 1] command:command]; - }]; - } else { - if ([GCKCastContext sharedInstance].sessionManager.currentCastSession != nil || [GCKCastContext sharedInstance].sessionManager.connectionState == GCKConnectionStateConnected) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: [CastUtilities createSessionObject:[GCKCastContext sharedInstance].sessionManager.currentCastSession] ]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } else { - self.currentSession = [[ChromecastSession alloc] initWithDevice:device cordovaDelegate:self.commandDelegate initialCommand:command]; - [self.currentSession add:self]; - } + if (remainTime <= 0) { + [self sendError:@"timeout" message:[NSString stringWithFormat:@"Failed to join route (%@) after 15s and %d tries.", routeID, retries + 1] command:command]; + return; } + remainTime -= 1; + retries += 1; + // check again in 1 second + NSMethodSignature *signature = [self methodSignatureForSelector:@selector(selectRouteRecursive:forTime:command:timesRetried:)]; + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; + [invocation setTarget:self]; + [invocation setSelector:_cmd]; + [invocation setArgument:&routeID atIndex:2]; + [invocation setArgument:&remainTime atIndex:3]; + [invocation setArgument:&command atIndex:4]; + [invocation setArgument:&retries atIndex:5]; + [NSTimer scheduledTimerWithTimeInterval:1 invocation:invocation repeats:NO]; } #pragma GCKLoggerDelegate @@ -401,8 +370,12 @@ - (NSString*)deviceToJson:(GCKDevice*) device { return [CastUtilities convertDictToJsonString:deviceJson]; } +- (void) didUpdateDeviceList { + [self sendScanUpdate]; +} + - (void)didInsertDevice:(GCKDevice *)device atIndex:(NSUInteger)index { - NSString* deviceName = @""; + NSString* deviceName = @""; if (device.friendlyName != nil) { deviceName = device.friendlyName; } else { From 52ec111f929a3c02f9d162d04ffe4d57a23c2d02 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 17 Nov 2019 05:16:37 -0700 Subject: [PATCH 087/166] Style changes. Xcode is determined to do this. --- src/ios/Chromecast.m | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 7419152..0a6b8a5 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -19,7 +19,7 @@ @implementation Chromecast - (void)pluginInitialize { [super pluginInitialize]; -// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onCastStateChanged:) name:kGCKCastStateDidChangeNotification object:nil]; + // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onCastStateChanged:) name:kGCKCastStateDidChangeNotification object:nil]; } - (void)sendJavascript:(NSString*)jsCommand { @@ -61,9 +61,9 @@ -(void) initialize:(CDVInvokedUrlCommand*)command { //For debugging purpose GCKLogger.sharedInstance.delegate = self; -// [self log:[NSString stringWithFormat:@"API Initialized with appID %@", appId]]; + // [self log:[NSString stringWithFormat:@"API Initialized with appID %@", appId]]; -// ChromecastSession *session = [[ChromecastSession alloc] init]; + // ChromecastSession *session = [[ChromecastSession alloc] init]; [[GCKCastContext sharedInstance].sessionManager addListener:self]; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]]; @@ -140,7 +140,7 @@ - (void)requestSession:(CDVInvokedUrlCommand*) command { self.sessionCommand = command; self.currentSession.sessionStatus = @"stopped"; [[GCKCastContext sharedInstance].sessionManager endSession]; - + }]]; [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"Canceld"); @@ -187,7 +187,7 @@ - (void)queueLoad:(CDVInvokedUrlCommand *)command { queueItemBuilder.startTime = [item[@"startTime"] doubleValue]; queueItemBuilder.preloadTime = [item[@"preloadTime"] doubleValue]; double duration = media[@"duration"] == [NSNull null] ? 0 : [media[@"duration"] doubleValue]; - + GCKMediaInformation *mediaInformation = [CastUtilities buildMediaInformationForQueueItem:media[@"contentId"] customData:media[@"customData"] contentType:media[@"contentType"] duration:duration startTime:0 streamType:media[@"streamType"] metaData:media[@"metadata"]]; queueItemBuilder.mediaInformation = mediaInformation; [queueItems addObject: [queueItemBuilder build]]; @@ -224,7 +224,7 @@ - (void)setMediaVolume:(CDVInvokedUrlCommand*) command { BOOL muted = [command.arguments[1] boolValue]; [self.currentSession setMediaMutedAndVolumeWIthCommand:command muted:muted nvewLevel:newLevel]; } -// [self.currentSession setReceiverVolumeLevelWithCommand:command newLevel:newLevel]; + // [self.currentSession setReceiverVolumeLevelWithCommand:command newLevel:newLevel]; } - (void)setReceiverVolumeLevel:(CDVInvokedUrlCommand*) command { @@ -352,7 +352,7 @@ - (void)selectRouteRecursive:(NSString*)routeID forTime:(int)remainTime command: #pragma GCKLoggerDelegate - (void)logMessage:(NSString *)message atLevel:(GCKLoggerLevel)level fromFunction:(NSString *)function location:(NSString *)location { -// [self log:[NSString stringWithFormat:@"GCKLogger = %@, %ld, %@, %@", message,(long)level,function,location]]; + // [self log:[NSString stringWithFormat:@"GCKLogger = %@, %ld, %@, %@", message,(long)level,function,location]]; } #pragma GCKDiscoveryManagerListener @@ -364,9 +364,9 @@ - (NSString*)deviceToJson:(GCKDevice*) device { deviceName = device.deviceID; } NSDictionary* deviceJson = @{ - @"name" : deviceName, - @"id" : device.uniqueID - }; + @"name" : deviceName, + @"id" : device.uniqueID + }; return [CastUtilities convertDictToJsonString:deviceJson]; } @@ -381,7 +381,7 @@ - (void)didInsertDevice:(GCKDevice *)device atIndex:(NSUInteger)index { } else { deviceName = device.deviceID; } - + [self.devicesAvailable insertObject:device atIndex:index]; [self checkReceiverAvailable]; } @@ -442,7 +442,7 @@ - (void)onCastStateChanged:(NSNotification*)notification { } - (void)sendEvent:(NSString *)eventName args:(NSArray *)args{ -// NSLog(@"Event Name: %@", eventName); + // NSLog(@"Event Name: %@", eventName); if (self.eventCommand == nil) { return; } @@ -478,7 +478,7 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeSession:(GCK } - (void)sessionManager:(GCKSessionManager *)sessionManager didEndSession:(GCKSession *)session withError:(NSError *)error { - + if (error != nil) { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.debugDescription]; [self.commandDelegate sendPluginResult:pluginResult callbackId:self.sessionCommand.callbackId]; @@ -489,5 +489,4 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didEndSession:(GCKSes } } - @end From 0be770c9f8a2a3cff1c3e13b27bb4d33b229b487 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 17 Nov 2019 05:24:58 -0700 Subject: [PATCH 088/166] (ios) ensure that the scan is running while attempting to selectRoute. --- src/ios/Chromecast.m | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 0a6b8a5..b08081c 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -106,10 +106,14 @@ -(BOOL) startRouteScan:(CDVInvokedUrlCommand*)command { } self.scanCommand = command; [self sendScanUpdate]; - [[GCKCastContext sharedInstance].discoveryManager startDiscovery]; + [self startRouteScan]; return YES; } +-(void) startRouteScan { + [[GCKCastContext sharedInstance].discoveryManager startDiscovery]; +} + - (void)sendScanUpdate { if (self.scanCommand == nil) { return; @@ -319,27 +323,36 @@ - (void)selectRoute:(CDVInvokedUrlCommand*)command { } NSString* routeID = command.arguments[0]; - - [self selectRouteRecursive:routeID forTime:15 command:command timesRetried:0]; + // Ensure the scan is running + [self startRouteScan]; + [self selectRouteRecursive:routeID forTime:15 command:command timesRetried:0 callback:^{ + // If there is no scanCommand that means we only started the scan for selectRoute + if (self.scanCommand == nil) { + // So we should also stop it + [self stopRouteScan]; + } + }]; } // Check for a device with the routeID every 1.5 second forTime seconds -- (void)selectRouteRecursive:(NSString*)routeID forTime:(int)remainTime command:(CDVInvokedUrlCommand*)command timesRetried:(int)retries { +- (void)selectRouteRecursive:(NSString*)routeID forTime:(int)remainTime command:(CDVInvokedUrlCommand*)command timesRetried:(int)retries callback:(void(^)(void))callback { GCKDevice* device = [[GCKCastContext sharedInstance].discoveryManager deviceWithUniqueID:routeID]; if (device != nil) { self.currentSession = [[ChromecastSession alloc] initWithDevice:device cordovaDelegate:self.commandDelegate initialCommand:command]; + callback(); [self.currentSession add:self]; return; } if (remainTime <= 0) { [self sendError:@"timeout" message:[NSString stringWithFormat:@"Failed to join route (%@) after 15s and %d tries.", routeID, retries + 1] command:command]; + callback(); return; } remainTime -= 1; retries += 1; // check again in 1 second - NSMethodSignature *signature = [self methodSignatureForSelector:@selector(selectRouteRecursive:forTime:command:timesRetried:)]; + NSMethodSignature *signature = [self methodSignatureForSelector:@selector(selectRouteRecursive:forTime:command:timesRetried:callback:)]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; [invocation setTarget:self]; [invocation setSelector:_cmd]; @@ -347,6 +360,7 @@ - (void)selectRouteRecursive:(NSString*)routeID forTime:(int)remainTime command: [invocation setArgument:&remainTime atIndex:3]; [invocation setArgument:&command atIndex:4]; [invocation setArgument:&retries atIndex:5]; + [invocation setArgument:&callback atIndex:6]; [NSTimer scheduledTimerWithTimeInterval:1 invocation:invocation repeats:NO]; } @@ -489,4 +503,5 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didEndSession:(GCKSes } } + @end From 8ac80dd0b72febc70943720d650b95a14bcb8cf7 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 17 Nov 2019 21:19:56 -0700 Subject: [PATCH 089/166] Make manual test buttons bigger for ios. --- tests/www/css/tests.css | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/www/css/tests.css b/tests/www/css/tests.css index 188976b..6edf655 100644 --- a/tests/www/css/tests.css +++ b/tests/www/css/tests.css @@ -5,9 +5,11 @@ h1 { margin: 0.5em; } button { - height: 3em; + -webkit-appearance: none; + background-color: rgb(223, 223, 223); + height: 5em; min-width: 10em; - margin: 0.5em; + margin: 1em; } .center-horizontal { display: block; From 041c1ea3f4dbc960f9f81ff577fc95199e4977ed Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 17 Nov 2019 23:55:08 -0700 Subject: [PATCH 090/166] Forgot to apply some button ids --- tests/www/chrome/tests_auto_chrome.html | 4 ++-- tests/www/chrome/tests_manual_secondary_chrome.html | 4 ++-- tests/www/html/tests_auto.html | 4 ++-- tests/www/html/tests_manual_secondary.html | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/www/chrome/tests_auto_chrome.html b/tests/www/chrome/tests_auto_chrome.html index 3963a42..6e64b09 100644 --- a/tests/www/chrome/tests_auto_chrome.html +++ b/tests/www/chrome/tests_auto_chrome.html @@ -18,12 +18,12 @@

      Auto Tests

      - - diff --git a/tests/www/chrome/tests_manual_secondary_chrome.html b/tests/www/chrome/tests_manual_secondary_chrome.html index 5cdd8db..63dc6ed 100644 --- a/tests/www/chrome/tests_manual_secondary_chrome.html +++ b/tests/www/chrome/tests_manual_secondary_chrome.html @@ -18,12 +18,12 @@

      Manual Tests (Secondary Device)

      - - diff --git a/tests/www/html/tests_auto.html b/tests/www/html/tests_auto.html index 121354d..3da9452 100644 --- a/tests/www/html/tests_auto.html +++ b/tests/www/html/tests_auto.html @@ -19,12 +19,12 @@

      Auto Tests

      - - diff --git a/tests/www/html/tests_manual_secondary.html b/tests/www/html/tests_manual_secondary.html index ee8ed79..3708dd0 100644 --- a/tests/www/html/tests_manual_secondary.html +++ b/tests/www/html/tests_manual_secondary.html @@ -19,12 +19,12 @@

      Manual Tests (Secondary Device)

      - - From 515311958c3a53b94dd64bb3dd1dce5a4641f5ed Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 17 Nov 2019 23:55:31 -0700 Subject: [PATCH 091/166] Updated readme with api description and example js --- README.md | 75 ++++++++++++++++++++++++++++++++++----- doc/example.js | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 9 deletions(-) create mode 100644 doc/example.js diff --git a/README.md b/README.md index 95ae2a2..1a41e4d 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,14 @@ cordova plugin add https://github.com/jellyfin/cordova-plugin-chromecast.git ``` +# Supports + +**Android** 4.4+ (7.x highest confirmed) (may support lower, untested) +**iOS** 9.0+ (13.2.1 highest confirmed) (may support lower, untested) + +## Quirks +* Android 4.4 (maybe 5.x and 6.x) are not able automatically rejoin/resume a chromecast session after an app restart. + # Usage This project attempts to implement the [official Google Cast API for Chrome](https://developers.google.com/cast/docs/reference/chrome#chrome.cast) within the Cordova webview. @@ -14,7 +22,7 @@ This means that you should be able to write almost identical code in cordova as We have not implemented every function in the [API](https://developers.google.com/cast/docs/reference/chrome#chrome.cast) but most of the core functions are there. If you find a function is missing we welcome [pull requests](#contributing)! Alternatively, you can file an [issue](https://github.com/jellyfin/cordova-plugin-chromecast/issues), please include a code sample of the expected functionality if possible! -The only significant difference between the [cast API](https://developers.google.com/cast/docs/reference/chrome#chrome.cast) and this plugin is the initialization. +The most significant usage difference between the [cast API](https://developers.google.com/cast/docs/reference/chrome#chrome.cast) and this plugin is the initialization. In **Chrome desktop** you would do: ```js @@ -32,8 +40,44 @@ document.addEventListener("deviceready", function () { }); ``` -## Specific to this plugin -We have added some additional methods beyond the [Chromecast API]((https://developers.google.com/cast/docs/reference/chrome#chrome.cast)). + +### Example +Here is a simple [example](doc/example.js) that loads a video, pauses it, and ends the session. + +## API +Here are the support [Chromecast API]((https://developers.google.com/cast/docs/reference/chrome#chrome.cast)) methods. Any object types required by any of these methods are also supported. (eg. chrome.cast.ApiConfig) + +[chrome.cast.initialize](https://developers.google.com/cast/docs/reference/chrome/chrome.cast#.initialize) +[chrome.cast.requestSession](https://developers.google.com/cast/docs/reference/chrome/chrome.cast#.requestSession) +[chrome.cast.setCustomReceivers](https://developers.google.com/cast/docs/reference/chrome/chrome.cast#.setCustomReceivers) +[chrome.cast.Session.setReceiverVolumeLevel](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session#setReceiverVolumeLevel) +[chrome.cast.Session.setReceiverMuted](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session#setReceiverMuted) +[chrome.cast.Session.stop](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session#stop) +[chrome.cast.Session.leave](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session#leave) +[chrome.cast.Session.sendMessage](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session#sendMessage) +[chrome.cast.Session.loadMedia](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session#loadMedia) +[chrome.cast.Session.queueLoad](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session#queueLoad) +[chrome.cast.Session.addUpdateListener](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session#addUpdateListener) +[chrome.cast.Session.removeUpdateListener](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session#removeUpdateListener) +[chrome.cast.Session.addMessageListener](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session#addMessageListener) +[chrome.cast.Session.removeMessageListener](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session#removeMessageListener) +[chrome.cast.Session.addMediaListener](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session#addMediaListener) +[chrome.cast.Session.removeMediaListener](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session#removeMediaListener) +[chrome.cast.media.Media.play](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.Media.html#play) +[chrome.cast.media.Media.pause](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.Media.html#pause) +[chrome.cast.media.Media.seek](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.Media.html#seek) +[chrome.cast.media.Media.stop](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.Media.html#stop) +[chrome.cast.media.Media.setVolume](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.Media.html#setVolume) +[chrome.cast.media.Media.supportsCommand](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.Media.html#supportsCommand) +[chrome.cast.media.Media.getEstimatedTime](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.Media.html#getEstimatedTime) +[chrome.cast.media.Media.editTracksInfo](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.Media.html#editTracksInfo) +[chrome.cast.media.Media.queueJumpToItem](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.Media.html#queueJumpToItem) +[chrome.cast.media.Media.addUpdateListener](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.Media.html#addUpdateListener) +[chrome.cast.media.Media.removeUpdateListener](https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.Media.html#removeUpdateListener) + + +### Specific to this plugin +We have added some additional methods unique to this plugin. They can all be found in the `chrome.cast.cordova` object. To make your own **custom route selector** use this: @@ -77,9 +121,12 @@ isNearbyDevice {boolean} - Is it a device only accessible via guest mode? Follow these direction to set up for plugin development: * You will need an existing cordova project or [create a new cordova project](https://cordova.apache.org/#getstarted). -* With **admin permission** run: `cordova plugin add --link ` +* Add the chromecast and chromecast tests plugins: + * `cordova plugin add --link ` + * `cordova plugin add --link /tests` + * This --link** option may require **admin permission** -#### About the `--link` flag +#### **About the `--link` flag The `--link` flag allows you to modify the native code (java/swift/obj-c) directly in the relative platform folder if desired. * This means you can work directly from Android Studio/Xcode! * Note: Be careful about adding and deleting files. These changes will be exclusive to the platform folder and will not be transferred back to your plugin folder. @@ -97,12 +144,22 @@ Run `npm test` to ensure your code fits the styling. It will also find some err * If errors are found, you can try running `npm run style`, this will attempt to automatically fix the errors. ### Tests Mobile +Requirements: +* A chromecast device How to run the tests: -* With **admin permission** run `cordova plugin add --link /tests` +* Follow [setup](#setup) * Change `config.xml`'s content tag to `` -* You must a valid chromecast on your network to run the tests. -* Run the app, let auto tests do its thing, and then follow the directions for manual tests. + +Auto tests: +* Run the app, select auto tests, let it do its thing + +Manual tests: +* This tests tricky features of chromecast such as: + * Resume casting session after page reload / app restart + * Interaction between 2 devices connected to the same session +* You will need to be able to run the tests from 2 different devices (preferred) or between a device and chrome desktop browser + * To use the chrome desktop browser see [Tests Chrome](#tests-chrome) [Why we chose a non-standard test framework](https://github.com/jellyfin/cordova-plugin-chromecast/issues/50) @@ -113,7 +170,7 @@ They use the google provided cast_sender.js. These are particularly useful for ensuring we are following the [official Google Cast API for Chrome](https://developers.google.com/cast/docs/reference/chrome#chrome.cast) correctly. To run the tests: * run: `npm run host-chrome-tests []` -* Navigate to: `http://localhost:/chrome/tests_chrome.html` +* Navigate to: `http://localhost:8432/chrome/tests_chrome.html` ## Contributing diff --git a/doc/example.js b/doc/example.js new file mode 100644 index 0000000..3b9a37e --- /dev/null +++ b/doc/example.js @@ -0,0 +1,96 @@ +document.addEventListener("deviceready", function () { + // Must wait for deviceready before using chromecast + + // File globals + var _session; + var _media; + + initialize(); + + function initialize () { + // use default app id + var appId = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID; + var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(appId), function sessionListener (session) { + // The session listener is only called under the following conditions: + // * will be called shortly chrome.cast.initialize is run + // * if the device is already connected to a cast session + // Basically, this is what allows you to re-use the same cast session + // across different pages and after app restarts + }, function receiverListener (receiverAvailable) { + // receiverAvailable is a boolean. + // True = at least one chromecast device is available + // False = No chromecast devices available + // You can use this to determine if you want to show your chromecast icon + }); + + // initialize chromecast, this must be done before using other chromecast features + chrome.cast.initialize(apiConfig, function () { + // Initialize complete + // Let's start casting + requestSession(); + }, function (err) { + // Initialize failure + }); + } + + + function requestSession () { + // This will open a native dialog that will let + // the user choose a chromecast to connect to + // (Or will let you disconnect if you are already connected) + chrome.cast.requestSession(function (session) { + // Got a session! + _session = session; + + // Load a video + loadMedia(); + }, function (err) { + // Failed, or if err is cancel, the dialog closed + }); + } + + function loadMedia () { + var videoUrl = 'https://ia801302.us.archive.org/1/items/TheWater_201510/TheWater.mp4'; + var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + + _session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (media) { + // You should see the video playing now! + // Got media! + _media = media; + + // Wait a couple seconds + setTimeout(function () { + // Lets pause the media + pauseMedia(); + }, 4000); + + }, function (err) { + // Failed (check that the video works in your browser) + }); + } + + function pauseMedia () { + _media.pause({}, function () { + // Success + + // Wait a couple seconds + setTimeout(function () { + // stop the session + stopSession(); + }, 2000) + + }, function (err) { + // Fail + }); + } + + function stopSession () { + // Also stop the session (if ) + _session.stop(function () { + // Success + }, function (err) { + // Fail + }); + } + +}); \ No newline at end of file From 1a0486771bc3307900f00907c38f0885f1252d19 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 18 Nov 2019 15:54:54 -0700 Subject: [PATCH 092/166] version update 1.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 136168c..c272b70 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-chromecast", - "version": "1.0.0-dev", + "version": "1.0.0", "scripts": { "host-chrome-tests": "node tests/www/chrome/host-tests.js", "style-fix-js": "node node_modules/eslint/bin/eslint --fix src && node node_modules/eslint/bin/eslint --fix www && node node_modules/eslint/bin/eslint --ignore-pattern tests/www/lib --fix tests/www", From 6ea9b29491edb3b2f204bb720766d7b5a8ab0505 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Tue, 19 Nov 2019 01:02:34 -0700 Subject: [PATCH 093/166] (ios) Added instructions for meeting ios app distribution requirements --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 1a41e4d..6b64b43 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,25 @@ cordova plugin add https://github.com/jellyfin/cordova-plugin-chromecast.git ``` +### Additional iOS Installation Instructions +To **distribute** an iOS app with this plugin you must add usage descriptions to your project's `config.xml`. +These strings will be used when asking the user for permission to use the microphone and bluetooth. +```xml + + + + Bluetooth is required to scan for nearby Chromecast devices with guest mode enabled. + + + + Bluetooth is required to scan for nearby Chromecast devices with guest mode enabled. + + + The microphone is required to pair with nearby Chromecast devices with guest mode enabled. + + +``` + # Supports **Android** 4.4+ (7.x highest confirmed) (may support lower, untested) From 7a0848e758695bc881b51812f0be8a8c5701d3fe Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Fri, 22 Nov 2019 20:22:06 -0700 Subject: [PATCH 094/166] Update google cast library to 4.4.6 which supports iOS 9.0+ --- README.md | 2 +- plugin.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6b64b43..b6a2efa 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ These strings will be used when asking the user for permission to use the microp # Supports **Android** 4.4+ (7.x highest confirmed) (may support lower, untested) -**iOS** 9.0+ (13.2.1 highest confirmed) (may support lower, untested) +**iOS** 9.0+ (13.2.1 highest confirmed) ## Quirks * Android 4.4 (maybe 5.x and 6.x) are not able automatically rejoin/resume a chromecast session after an app restart. diff --git a/plugin.xml b/plugin.xml index eb75fe5..0ab54f7 100644 --- a/plugin.xml +++ b/plugin.xml @@ -58,7 +58,7 @@ - + From 3d8cb98aa0b493c72cad0f4cee8a4ee7783cd16c Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 25 Nov 2019 04:48:29 -0700 Subject: [PATCH 095/166] (ios) remove unused emitAllRoutes --- src/ios/Chromecast.m | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index b08081c..2201077 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -36,12 +36,6 @@ - (void)setup:(CDVInvokedUrlCommand*) command { [self sendEvent:@"SETUP" args:@[]]; } -- (void)emitAllRoutes:(CDVInvokedUrlCommand*) command { - // No arguments. It's only implemented to satisfy plugin's JS API. - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; -} - -(void) initialize:(CDVInvokedUrlCommand*)command { if (self.devicesAvailable == nil) { From b242f8f5474fa2628291f1b82f63726fd08436c1 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 25 Nov 2019 06:34:26 -0700 Subject: [PATCH 096/166] [non-change] typo and empty lines change --- src/ios/Chromecast.m | 7 +------ src/ios/ChromecastSession.m | 9 --------- tests/www/js/tests_auto.js | 2 +- 3 files changed, 2 insertions(+), 16 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 2201077..41b1a06 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -19,7 +19,6 @@ @implementation Chromecast - (void)pluginInitialize { [super pluginInitialize]; - // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onCastStateChanged:) name:kGCKCastStateDidChangeNotification object:nil]; } - (void)sendJavascript:(NSString*)jsCommand { @@ -412,8 +411,6 @@ - (void)didRemoveDevice:(GCKDevice *)device atIndex:(NSUInteger)index { #pragma CastSessionListener - - - (void)onMediaLoaded:(NSDictionary *)media { [self sendEvent:@"MEDIA_LOAD" args:@[media]]; } @@ -422,7 +419,6 @@ - (void)onMediaUpdated:(NSDictionary *)media isAlive:(BOOL)isAlive { [self sendEvent:@"MEDIA_UPDATE" args:@[media]]; } - - (void)onSessionRejoin:(NSDictionary*)session { [self sendEvent:@"SESSION_LISTENER" args:@[session]]; } @@ -436,11 +432,10 @@ - (void)onMessageReceived:(NSDictionary *)session namespace:(NSString *)namespac } - (void)onSessionEnd:(NSDictionary *)session { - [self sendEvent:@"SESSION_UPDATE" args:@[session]]; } + - (void)onCastStateChanged:(NSNotification*)notification { - GCKCastState castState = [notification.userInfo[kGCKNotificationKeyCastState] intValue]; if (castState == GCKCastStateNoDevicesAvailable) { [self sendEvent:@"RECEIVER_LISTENER" args:@[@(false)]]; diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index 236f24e..a3b0f35 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -176,8 +176,6 @@ - (void)createMessageChannelWithCommand:(CDVInvokedUrlCommand*)command namespace [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - - - (void)sendMessageWithCommand:(CDVInvokedUrlCommand*)command namespace:(NSString*)namespace message:(NSString*)message { GCKGenericChannel* channel = self.genericChannels[namespace]; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[NSString stringWithFormat:@"Namespace %@ not founded",namespace]]; @@ -373,9 +371,6 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didStartCastSession:( } - (void)sessionManager:(GCKSessionManager *)sessionManager didEndCastSession:(GCKCastSession *)session withError:(NSError *)error { -// self.currentSession = nil; -// self.remoteMediaClient = nil; - if (error != nil) { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.debugDescription]; [self.commandDelegate sendPluginResult:pluginResult callbackId:self.initialCommand.callbackId]; @@ -388,8 +383,6 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didEndCastSession:(GC } - - - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeCastSession:(GCKCastSession *)session { self.currentSession = session; NSLog(@"here is the session"); @@ -403,8 +396,6 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeSession:(GCK - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didStartMediaSessionWithID:(NSInteger)sessionID { } - - - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didUpdateMediaStatus:(GCKMediaStatus *)mediaStatus { if (self.currentSession == nil) { [self.sessionListener onMediaUpdated:@{} isAlive:false]; diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index b923c3e..5d80acf 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -449,7 +449,7 @@ }); it('initialize should not receive a session after session.stop', function (done) { var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function sessionListener (session) { - assert.fail('should not receive a session (we did sessionLeave so we shouldnt be able to auto rejoin rejoin)'); + assert.fail('should not receive a session (we did sessionStop so we shouldnt be able to auto rejoin rejoin)'); }); chrome.cast.initialize(apiConfig, function () { done(); From 7ddc056db2b56efa25610cc5ee072aecbac1cde4 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 25 Nov 2019 06:35:23 -0700 Subject: [PATCH 097/166] (ios) we don't use sendJavascript anymore --- src/ios/Chromecast.m | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 41b1a06..548f66b 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -21,9 +21,6 @@ - (void)pluginInitialize { [super pluginInitialize]; } -- (void)sendJavascript:(NSString*)jsCommand { - [self.webViewEngine evaluateJavaScript:jsCommand completionHandler:nil]; -} - (void)log:(NSString*)s { [self sendJavascript:[NSString stringWithFormat: @"console.log('Chromecast-iOS: ', %@)",s]]; From d5d1778cabeedc794c6bde6aeb3d4d7e7942b760 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 25 Nov 2019 06:43:12 -0700 Subject: [PATCH 098/166] (ios) Move device list building to utilities. Simplify receiverListener methods. --- src/ios/CastUtilities.h | 2 +- src/ios/CastUtilities.m | 8 +++--- src/ios/Chromecast.m | 61 +++++------------------------------------ 3 files changed, 12 insertions(+), 59 deletions(-) diff --git a/src/ios/CastUtilities.h b/src/ios/CastUtilities.h index 4e3f20d..3015126 100644 --- a/src/ios/CastUtilities.h +++ b/src/ios/CastUtilities.h @@ -21,7 +21,7 @@ NS_ASSUME_NONNULL_BEGIN + (NSDictionary*)createSessionObject:(GCKCastSession *)session status:(NSString*)status; +(NSDictionary*)createMediaObject:(GCKCastSession*)session; +(NSDictionary*)createMediaInfoObject:(GCKMediaInformation*)mediaInfo; -+(NSArray*)createDeviceObject:(NSArray*)devices; ++(NSArray*)createDeviceArray; +(NSArray*)getMediaTracks:(NSArray*)mediaTracks; +(NSDictionary*)getTextTrackStyle:(GCKMediaTextTrackStyle*)textTrackStyle; +(NSString*)getEdgeType:(GCKMediaTextTrackStyleEdgeType)edgeType; diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index 3bcbf3a..0a7d870 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -837,11 +837,11 @@ + (NSArray*)createImagesArray:(NSArray*) images { return appImages; } -+(NSArray*)createDeviceObject:(NSArray*)devices -{ ++(NSArray*)createDeviceArray { NSMutableArray* deviceArray = [[NSMutableArray alloc] init]; - for (int i=0; i < devices.count; i++) { - GCKDevice* device = devices[i]; + GCKDiscoveryManager* discoveryManager = GCKCastContext.sharedInstance.discoveryManager; + for (int i = 0; i < [discoveryManager deviceCount]; i++) { + GCKDevice* device = [discoveryManager deviceAtIndex:i]; NSString* deviceName = @""; if (device.friendlyName != nil) { deviceName = device.friendlyName; diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 548f66b..b57b810 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -58,7 +58,6 @@ -(void) initialize:(CDVInvokedUrlCommand*)command { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - [self checkReceiverAvailable]; if ([GCKCastContext sharedInstance].sessionManager.currentCastSession != nil) { [self onSessionRejoin:[CastUtilities createSessionObject:[GCKCastContext sharedInstance].sessionManager.currentCastSession]]; @@ -111,14 +110,7 @@ - (void)sendScanUpdate { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:[CastUtilities createDeviceObject:self.devicesAvailable]]; [pluginResult setKeepCallback:@(true)]; [self.commandDelegate sendPluginResult:pluginResult callbackId:self.scanCommand.callbackId]; -} -- (void)checkReceiverAvailable { - if ([GCKCastContext sharedInstance].castState != GCKCastStateNoDevicesAvailable) { - [self sendEvent:@"RECEIVER_LISTENER" args:@[@(true)]]; - } else { - [self sendEvent:@"RECEIVER_LISTENER" args:@[@(false)]]; - } } - (void)requestSession:(CDVInvokedUrlCommand*) command { @@ -360,52 +352,13 @@ - (void)logMessage:(NSString *)message atLevel:(GCKLoggerLevel)level fromFunctio } #pragma GCKDiscoveryManagerListener -- (NSString*)deviceToJson:(GCKDevice*) device { - NSString* deviceName = @""; - if (device.friendlyName != nil) { - deviceName = device.friendlyName; - } else { - deviceName = device.deviceID; - } - NSDictionary* deviceJson = @{ - @"name" : deviceName, - @"id" : device.uniqueID - }; - return [CastUtilities convertDictToJsonString:deviceJson]; -} - (void) didUpdateDeviceList { + BOOL receiverAvailable = [GCKCastContext.sharedInstance.discoveryManager deviceCount] > 0 ? YES : NO; + [self sendReceiverAvailable:receiverAvailable]; [self sendScanUpdate]; } -- (void)didInsertDevice:(GCKDevice *)device atIndex:(NSUInteger)index { - NSString* deviceName = @""; - if (device.friendlyName != nil) { - deviceName = device.friendlyName; - } else { - deviceName = device.deviceID; - } - - [self.devicesAvailable insertObject:device atIndex:index]; - [self checkReceiverAvailable]; -} - -- (void)didUpdateDevice:(GCKDevice *)device atIndex:(NSUInteger)index andMoveToIndex:(NSUInteger)newIndex { - if (self.devicesAvailable.count != 0) { - [self.devicesAvailable removeObjectAtIndex:index]; - } - [self.devicesAvailable insertObject:device atIndex:newIndex]; - [self checkReceiverAvailable]; -} - -- (void)didRemoveDevice:(GCKDevice *)device atIndex:(NSUInteger)index { - if (self.devicesAvailable.count != 0) { - [self.devicesAvailable removeObjectAtIndex:index]; - } - - [self checkReceiverAvailable]; -} - #pragma CastSessionListener - (void)onMediaLoaded:(NSDictionary *)media { @@ -434,11 +387,11 @@ - (void)onSessionEnd:(NSDictionary *)session { - (void)onCastStateChanged:(NSNotification*)notification { GCKCastState castState = [notification.userInfo[kGCKNotificationKeyCastState] intValue]; - if (castState == GCKCastStateNoDevicesAvailable) { - [self sendEvent:@"RECEIVER_LISTENER" args:@[@(false)]]; - } else { - [self sendEvent:@"RECEIVER_LISTENER" args:@[@(true)]]; - } + [self sendReceiverAvailable:(castState == GCKCastStateNoDevicesAvailable)]; +} + +- (void)sendReceiverAvailable:(BOOL)available { + [self sendEvent:@"RECEIVER_LISTENER" args:@[@(available)]]; } - (void)sendEvent:(NSString *)eventName args:(NSArray *)args{ From 9cf5d02497f1318de9d6e4e7890284331a479bde Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 25 Nov 2019 07:30:45 -0700 Subject: [PATCH 099/166] (ios) media must be an array when returned in session --- src/ios/CastUtilities.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index 0a7d870..e27c16a 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -468,9 +468,14 @@ +(NSString*)getClientMetadataName:(NSString*)iOSName { } + (NSDictionary *)createSessionObject:(GCKCastSession *)session { + NSDictionary* media = [CastUtilities createMediaObject:session]; + NSMutableArray* mediaArray = [NSMutableArray new]; + if ([media count] != 0) { + [mediaArray addObject: media]; + } return @{ @"appId" : session.applicationMetadata.applicationID? session.applicationMetadata.applicationID : @"", - @"media" : [CastUtilities createMediaObject:session], + @"media" : mediaArray, @"appImages" : @{}, @"sessionId" : session.sessionID? session.sessionID : @"", @"displayName" : session.applicationMetadata.applicationName? session.applicationMetadata.applicationName : @"", From cf61f6bf8f2d9e75fd58c086a38baca712bd9f8e Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 25 Nov 2019 08:10:24 -0700 Subject: [PATCH 100/166] (ios) decrease cast utilties code duplication --- src/ios/CastUtilities.h | 2 - src/ios/CastUtilities.m | 243 +++++----------------------------------- 2 files changed, 29 insertions(+), 216 deletions(-) diff --git a/src/ios/CastUtilities.h b/src/ios/CastUtilities.h index 3015126..fc2d4a1 100644 --- a/src/ios/CastUtilities.h +++ b/src/ios/CastUtilities.h @@ -42,8 +42,6 @@ NS_ASSUME_NONNULL_BEGIN +(NSString*)convertDictToJsonString:(NSDictionary*)dict; + (NSDictionary*)createError:(NSString*)code message:(NSString*)message; + (GCKMediaInformation *)buildMediaInformationForQueueItem:(NSString *)contentUrl customData:(id )customData contentType:(NSString *)contentType duration:(double)duration startTime:(double)startTime streamType:(NSString *)streamType metaData:(NSDictionary *)metaData; -+ (NSDictionary *)createMediaObjectForQueue:(GCKCastSession *)session; -+ (NSDictionary *)createMediaObjectForQueueJumpToItem:(GCKCastSession *)session; @end NS_ASSUME_NONNULL_END diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index e27c16a..845ce14 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -510,73 +510,7 @@ + (NSDictionary*)createSessionObject:(GCKCastSession *)session status:(NSString* }; } -+ (NSDictionary *)createMediaObjectForQueue:(GCKCastSession *)session { - if (session.remoteMediaClient == nil) { - return @{}; - } - - GCKMediaStatus* mediaStatus = session.remoteMediaClient.mediaStatus; - if (mediaStatus == nil) { - return @{}; - } - - NSMutableArray *qItems = [[NSMutableArray alloc] init]; - for (int i=0; i Date: Mon, 25 Nov 2019 08:10:44 -0700 Subject: [PATCH 101/166] (ios) fix startTime and preloadTime, check for invalidTimeInterval and NaN --- src/ios/CastUtilities.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index 845ce14..c540f67 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -564,9 +564,9 @@ + (NSDictionary *)createQueueItem:(GCKMediaQueueItem *)queueItem { @"customData": (queueItem.customData == nil)? @{} : queueItem.customData, @"itemId": @(queueItem.itemID),//[NSNumber numberWithInteger:queueItem.itemID], @"orderId": @(queueItem.itemID), - @"startTime": @(queueItem.startTime),//[NSNumber numberWithDouble:0], - @"preloadTime": @(0)//[NSNumber numberWithDouble:kGCKInvalidTimeInterval]//@(queueItem.preloadTime)//[NSNumber numberWithDouble:queueItem.preloadTime] @"media": queueItem.mediaInformation ? [CastUtilities createMediaInfoObject:queueItem.mediaInformation] : @"", + @"startTime": (queueItem.startTime == kGCKInvalidTimeInterval || queueItem.startTime != queueItem.startTime) ? @(0.0) : @(queueItem.startTime), + @"preloadTime": (queueItem.preloadTime == kGCKInvalidTimeInterval || queueItem.preloadTime != queueItem.preloadTime) ? @(0.0) : @(queueItem.preloadTime) }; } From 1d67ed455c84e4208911646a70ea37fad9304e9a Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 25 Nov 2019 08:30:19 -0700 Subject: [PATCH 102/166] (ios) Let ChromecastSession handle all the rejoining logic (also change ChromecastSession initialization to happen only ). Create rety function. Store appId in user preferences. In ChromecastSession, make currentSession a private variable. --- src/ios/Chromecast.h | 2 - src/ios/Chromecast.m | 148 ++++++++++++++++++++---------------- src/ios/ChromecastSession.h | 9 +-- src/ios/ChromecastSession.m | 133 ++++++++++++++++---------------- 4 files changed, 157 insertions(+), 135 deletions(-) diff --git a/src/ios/Chromecast.h b/src/ios/Chromecast.h index 79aee37..6206a2e 100644 --- a/src/ios/Chromecast.h +++ b/src/ios/Chromecast.h @@ -21,11 +21,9 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, strong) CDVInvokedUrlCommand* scanCommand; - (void)setup:(CDVInvokedUrlCommand*) command; -- (void)emitAllRoutes:(CDVInvokedUrlCommand*) command; - (void)initialize:(CDVInvokedUrlCommand*)command; - (BOOL)startRouteScan:(CDVInvokedUrlCommand*)command; - (BOOL)stopRouteScan:(CDVInvokedUrlCommand*)command; -- (void)checkReceiverAvailable; - (void)requestSession:(CDVInvokedUrlCommand*) command; - (void)setReceiverVolumeLevel:(CDVInvokedUrlCommand*) command; - (void)queueLoad:(CDVInvokedUrlCommand *)command; diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index b57b810..475ab48 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -16,14 +16,42 @@ @interface Chromecast() @end @implementation Chromecast +NSString* appId = nil; - (void)pluginInitialize { [super pluginInitialize]; + self.currentSession = [ChromecastSession alloc]; + + NSString* applicationId = [NSUserDefaults.standardUserDefaults stringForKey:@"appId"]; + if (applicationId == nil) { + applicationId = kGCKDefaultMediaReceiverApplicationID; + } + [self setAppId:applicationId]; } +- (void)setAppId:(NSString*)applicationId { + if ([applicationId isEqualToString:appId]) { + return; + } + appId = applicationId; + [NSUserDefaults.standardUserDefaults setObject:appId forKey:@"appId"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + + GCKDiscoveryCriteria *criteria = [[GCKDiscoveryCriteria alloc] + initWithApplicationID:appId]; + GCKCastOptions *options = [[GCKCastOptions alloc] initWithDiscoveryCriteria:criteria]; + options.physicalVolumeButtonsWillControlDeviceVolume = YES; + options.disableDiscoveryAutostart = NO; + [GCKCastContext setSharedInstanceWithOptions:options]; -- (void)log:(NSString*)s { - [self sendJavascript:[NSString stringWithFormat: @"console.log('Chromecast-iOS: ', %@)",s]]; + // Enable chromecast logger. +// [GCKLogger sharedInstance].delegate = self; + + // Ensure we have only 1 listener attached + [GCKCastContext.sharedInstance.discoveryManager removeListener:self]; + [GCKCastContext.sharedInstance.discoveryManager addListener:self]; + + self.currentSession = [self.currentSession initWithListener:self cordovaDelegate:self.commandDelegate]; } - (void)setup:(CDVInvokedUrlCommand*) command { @@ -33,35 +61,33 @@ - (void)setup:(CDVInvokedUrlCommand*) command { } -(void) initialize:(CDVInvokedUrlCommand*)command { - - if (self.devicesAvailable == nil) { - self.devicesAvailable = [[NSMutableArray alloc] init]; - } - - NSString* appId = kGCKDefaultMediaReceiverApplicationID; - if (command.arguments[0] != nil) { - appId = command.arguments[0]; - } - GCKDiscoveryCriteria* criteria = [[GCKDiscoveryCriteria alloc] initWithApplicationID:appId]; - GCKCastOptions* options = [[GCKCastOptions alloc] initWithDiscoveryCriteria:criteria]; - options.physicalVolumeButtonsWillControlDeviceVolume = YES; - options.disableDiscoveryAutostart = NO; - [GCKCastContext setSharedInstanceWithOptions:options]; - [GCKCastContext.sharedInstance.discoveryManager addListener:self]; - - //For debugging purpose - GCKLogger.sharedInstance.delegate = self; - // [self log:[NSString stringWithFormat:@"API Initialized with appID %@", appId]]; - - // ChromecastSession *session = [[ChromecastSession alloc] init]; - [[GCKCastContext sharedInstance].sessionManager addListener:self]; - + [self setAppId:command.arguments[0]]; + + // Initialize success CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - if ([GCKCastContext sharedInstance].sessionManager.currentCastSession != nil) { - [self onSessionRejoin:[CastUtilities createSessionObject:[GCKCastContext sharedInstance].sessionManager.currentCastSession]]; - } + // Search for existing session + [self findAvailableReceiver:^{ + [self.currentSession tryRejoin]; + }]; +} + +- (void)findAvailableReceiver:(void(^)(void))successCallback { + // Ensure the scan is running + [self startRouteScan]; + [self retry:^BOOL{ + // Did we find any devices? + if ([GCKCastContext.sharedInstance.discoveryManager hasDiscoveredDevices]) { + [self sendReceiverAvailable:YES]; + return YES; + } + return NO; + } forTries:5 callback:^(BOOL passed){ + if (passed) { + successCallback(); + } + }]; } - (void)stopRouteScanForSetup { @@ -117,8 +143,7 @@ - (void)requestSession:(CDVInvokedUrlCommand*) command { UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Cast to" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; for (GCKDevice* device in self.devicesAvailable) { [alert addAction:[UIAlertAction actionWithTitle:device.friendlyName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { - self.currentSession = [[ChromecastSession alloc] initWithDevice:device cordovaDelegate:self.commandDelegate initialCommand:command]; - [self.currentSession add:self]; + [self.currentSession joinDevice:device cdvCommand:command]; }]]; } [alert addAction:[UIAlertAction actionWithTitle:@"Stop Casting" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { @@ -299,7 +324,10 @@ - (void)mediaEditTracksInfo:(CDVInvokedUrlCommand*)command { } - (void)selectRoute:(CDVInvokedUrlCommand*)command { - if ([GCKCastContext sharedInstance].sessionManager.currentCastSession != nil || [GCKCastContext sharedInstance].sessionManager.connectionState == GCKConnectionStateConnected) { + GCKCastSession* currentSession = [GCKCastContext sharedInstance].sessionManager.currentCastSession; + if (currentSession != nil + || [currentSession connectionState] == GCKConnectionStateConnected + || [currentSession connectionState] == GCKConnectionStateConnecting) { [self sendError:@"session_error" message:@"Leave or stop current session before attempting to join new session." command:command]; return; } @@ -307,48 +335,47 @@ - (void)selectRoute:(CDVInvokedUrlCommand*)command { NSString* routeID = command.arguments[0]; // Ensure the scan is running [self startRouteScan]; - [self selectRouteRecursive:routeID forTime:15 command:command timesRetried:0 callback:^{ - // If there is no scanCommand that means we only started the scan for selectRoute - if (self.scanCommand == nil) { - // So we should also stop it - [self stopRouteScan]; + + [self retry:^BOOL{ + GCKDevice* device = [[GCKCastContext sharedInstance].discoveryManager deviceWithUniqueID:routeID]; + if (device != nil) { + [self.currentSession joinDevice:device cdvCommand:command]; + return YES; + } + return NO; + } forTries:1 callback:^(BOOL passed) { + if (!passed) { + [self sendError:@"timeout" message:[NSString stringWithFormat:@"Failed to join route (%@) after 15s and %d tries.", routeID, 15] command:command]; } + [self stopRouteScan]; }]; } -// Check for a device with the routeID every 1.5 second forTime seconds -- (void)selectRouteRecursive:(NSString*)routeID forTime:(int)remainTime command:(CDVInvokedUrlCommand*)command timesRetried:(int)retries callback:(void(^)(void))callback { - GCKDevice* device = [[GCKCastContext sharedInstance].discoveryManager deviceWithUniqueID:routeID]; - if (device != nil) { - self.currentSession = [[ChromecastSession alloc] initWithDevice:device cordovaDelegate:self.commandDelegate initialCommand:command]; - callback(); - [self.currentSession add:self]; - return; - } - if (remainTime <= 0) { - [self sendError:@"timeout" message:[NSString stringWithFormat:@"Failed to join route (%@) after 15s and %d tries.", routeID, retries + 1] command:command]; - callback(); +// retries every 1 second forTries times +// pass -1 to forTries to try infinitely +- (void)retry:(BOOL(^)(void))condition forTries:(int)remainTries callback:(void(^)(BOOL))callback { + BOOL passed = condition(); + if (passed || remainTries == 0) { + callback(passed); return; } - remainTime -= 1; - retries += 1; + + remainTries--; // check again in 1 second - NSMethodSignature *signature = [self methodSignatureForSelector:@selector(selectRouteRecursive:forTime:command:timesRetried:callback:)]; + NSMethodSignature *signature = [self methodSignatureForSelector:@selector(retry:forTries:callback:)]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; [invocation setTarget:self]; [invocation setSelector:_cmd]; - [invocation setArgument:&routeID atIndex:2]; - [invocation setArgument:&remainTime atIndex:3]; - [invocation setArgument:&command atIndex:4]; - [invocation setArgument:&retries atIndex:5]; - [invocation setArgument:&callback atIndex:6]; + [invocation setArgument:&condition atIndex:2]; + [invocation setArgument:&remainTries atIndex:3]; + [invocation setArgument:&callback atIndex:4]; [NSTimer scheduledTimerWithTimeInterval:1 invocation:invocation repeats:NO]; } #pragma GCKLoggerDelegate - (void)logMessage:(NSString *)message atLevel:(GCKLoggerLevel)level fromFunction:(NSString *)function location:(NSString *)location { - // [self log:[NSString stringWithFormat:@"GCKLogger = %@, %ld, %@, %@", message,(long)level,function,location]]; + NSLog(@"%@", [NSString stringWithFormat:@"GCKLogger = %@, %ld, %@, %@", message,(long)level,function,location]); } #pragma GCKDiscoveryManagerListener @@ -423,13 +450,6 @@ - (void)sendError:(NSString *)code message:(NSString *)message command:(CDVInvok [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } -- (void)sessionManager:(GCKSessionManager *)sessionManager didResumeCastSession:(GCKCastSession *)session { - [self onSessionRejoin:[CastUtilities createSessionObject:session]]; -} - -- (void)sessionManager:(GCKSessionManager *)sessionManager didResumeSession:(GCKSession *)session { -} - - (void)sessionManager:(GCKSessionManager *)sessionManager didEndSession:(GCKSession *)session withError:(NSError *)error { if (error != nil) { diff --git a/src/ios/ChromecastSession.h b/src/ios/ChromecastSession.h index 9f77b0d..6c519c0 100644 --- a/src/ios/ChromecastSession.h +++ b/src/ios/ChromecastSession.h @@ -15,8 +15,7 @@ NS_ASSUME_NONNULL_BEGIN @interface ChromecastSession : NSObject @property (nonatomic, retain) id commandDelegate; -@property (nonatomic, retain) CDVInvokedUrlCommand* initialCommand; -@property (nonatomic, retain) GCKCastSession* currentSession; +@property (nonatomic, retain) GCKSessionManager* sessionManager; @property (nonatomic, retain) GCKRemoteMediaClient* remoteMediaClient; @property (nonatomic, retain) GCKCastContext* castContext; @property (nonatomic, retain) NSMutableArray* requestDelegates; @@ -24,9 +23,9 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, retain) NSMutableDictionary* genericChannels; @property (nonatomic, retain) NSString* sessionStatus; -- (instancetype)initWithDevice:(GCKDevice*)device cordovaDelegate:(id)cordovaDelegate initialCommand:(CDVInvokedUrlCommand*)initialCommand; -- (void)add:(id)listener; -- (void)createSession:(GCKDevice*)device; +- (instancetype)initWithListener:(id)listener cordovaDelegate:(id)cordovaDelegate; +- (void)tryRejoin; +- (void)joinDevice:(GCKDevice*)device cdvCommand:(CDVInvokedUrlCommand*)command; - (CastRequestDelegate*)createGeneralRequestDelegate:(CDVInvokedUrlCommand*)command; - (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted nvewLevel:(float)newLevel; - (void)setMediaMutedWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted; diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index a3b0f35..f5c55a0 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -16,44 +16,50 @@ @interface ChromecastSession() @end @implementation ChromecastSession +GCKCastSession* currentSession; +CDVInvokedUrlCommand* joinSessionCommand; -- (instancetype)initWithDevice:(GCKDevice*)device cordovaDelegate:(id)cordovaDelegate initialCommand:(CDVInvokedUrlCommand*)initialCommand +- (instancetype)initWithListener:(id)listener cordovaDelegate:(id)cordovaDelegate { self = [super init]; - if (self) { - self.sessionStatus = @""; - self.commandDelegate = cordovaDelegate; - self.initialCommand = initialCommand; - self.castContext = [GCKCastContext sharedInstance]; - [self.castContext.sessionManager addListener:self]; - [self createSession:device]; - } + self.sessionListener = listener; + self.commandDelegate = cordovaDelegate; + self.sessionStatus = @"disconnected"; + self.castContext = [GCKCastContext sharedInstance]; + self.sessionManager = self.castContext.sessionManager; + + // Ensure we are only listening once after init + [self.sessionManager removeListener:self]; + [self.sessionManager addListener:self]; + return self; } - -- (void)add:(id)listener { - self.sessionListener = listener; +- (void)setSession:(GCKCastSession*)session { + currentSession = session; + self.sessionStatus = @"connected"; } -- (void)createSession:(GCKDevice*)device { - if (device != nil) { - [NSUserDefaults.standardUserDefaults setBool:false forKey:@"jump"]; - [NSUserDefaults.standardUserDefaults synchronize]; - [self.castContext.sessionManager startSessionWithDevice:device]; - - } else { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Cannot connect to selected cast device."]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:self.initialCommand.callbackId]; +- (void)tryRejoin { + if (currentSession != nil) { + [self.sessionListener onSessionRejoin:[CastUtilities createSessionObject:currentSession status:self.sessionStatus]]; } } +- (void)joinDevice:(GCKDevice*)device cdvCommand:(CDVInvokedUrlCommand*)command { + joinSessionCommand = command; + + [NSUserDefaults.standardUserDefaults setBool:false forKey:@"jump"]; + [NSUserDefaults.standardUserDefaults synchronize]; + [self.sessionManager startSessionWithDevice:device]; +} + -(CastRequestDelegate*)createGeneralRequestDelegate:(CDVInvokedUrlCommand*)command { [self checkFinishDelegates]; CastRequestDelegate* delegate = [[CastRequestDelegate alloc] initWithSuccess:^{ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:self.currentSession] isAlive:NO]; + [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:currentSession status:self.sessionStatus] isAlive:NO]; } failure:^(GCKError * error) { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; @@ -68,8 +74,8 @@ -(CastRequestDelegate*)createGeneralRequestDelegate:(CDVInvokedUrlCommand*)comma - (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted nvewLevel:(float)newLevel { [self checkFinishDelegates]; CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(GCKError * error) { @@ -87,12 +93,11 @@ - (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:( request.delegate = requestDelegate; } - - (void)setMediaMutedWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted { [self checkFinishDelegates]; CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(GCKError * error) { @@ -112,8 +117,8 @@ - (void)setMediaMutedWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)mute - (void)setMediaVolumeWithCommand:(CDVInvokedUrlCommand*)withCommand newVolumeLevel:(float)newLevel { [self checkFinishDelegates]; CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:withCommand.callbackId]; } failure:^(GCKError * error) { @@ -133,22 +138,21 @@ - (void)setMediaVolumeWithCommand:(CDVInvokedUrlCommand*)withCommand newVolumeLe - (void)setReceiverVolumeLevelWithCommand:(CDVInvokedUrlCommand*)withCommand newLevel:(float)newLevel { CastRequestDelegate* delegate = [self createGeneralRequestDelegate:withCommand]; self.isRequesting = YES; - GCKRequest* request = [self.currentSession setDeviceVolume:newLevel]; + GCKRequest* request = [currentSession setDeviceVolume:newLevel]; request.delegate = delegate; } - (void)setReceiverMutedWithCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted { CastRequestDelegate* delegate = [self createGeneralRequestDelegate:command]; self.isRequesting = YES; - GCKRequest* request = [self.currentSession setDeviceMuted:muted]; + GCKRequest* request = [currentSession setDeviceMuted:muted]; request.delegate = delegate; } - - (void)loadMediaWithCommand:(CDVInvokedUrlCommand*)command mediaInfo:(GCKMediaInformation*)mediaInfo autoPlay:(BOOL)autoPlay currentTime : (double)currentTime { [self checkFinishDelegates]; CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(GCKError * error) { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; @@ -171,7 +175,7 @@ - (void)createMessageChannelWithCommand:(CDVInvokedUrlCommand*)command namespace GCKGenericChannel* newChannel = [[GCKGenericChannel alloc] initWithNamespace:namespace]; newChannel.delegate = self; self.genericChannels[namespace] = newChannel; - [self.currentSession addChannel:newChannel]; + [currentSession addChannel:newChannel]; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } @@ -196,8 +200,8 @@ - (void)sendMessageWithCommand:(CDVInvokedUrlCommand*)command namespace:(NSStrin - (void)mediaSeekWithCommand:(CDVInvokedUrlCommand*)command position:(NSTimeInterval)position resumeState:(GCKMediaResumeState)resumeState { [self checkFinishDelegates]; CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(GCKError * error) { @@ -221,7 +225,7 @@ - (void)mediaSeekWithCommand:(CDVInvokedUrlCommand*)command position:(NSTimeInte - (void)queueJumpToItemWithCommand:(CDVInvokedUrlCommand *)command itemId:(NSUInteger)itemId { [self checkFinishDelegates]; CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(GCKError * error) { @@ -243,8 +247,8 @@ - (void)queueJumpToItemWithCommand:(CDVInvokedUrlCommand *)command itemId:(NSUIn - (void)mediaPlayWithCommand:(CDVInvokedUrlCommand*)command { [self checkFinishDelegates]; CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(GCKError * error) { @@ -264,8 +268,8 @@ - (void)mediaPlayWithCommand:(CDVInvokedUrlCommand*)command { - (void)mediaPauseWithCommand:(CDVInvokedUrlCommand*)command { [self checkFinishDelegates]; CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(GCKError * error) { @@ -285,8 +289,8 @@ - (void)mediaPauseWithCommand:(CDVInvokedUrlCommand*)command { - (void)mediaStopWithCommand:(CDVInvokedUrlCommand*)command { [self checkFinishDelegates]; CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(GCKError * error) { @@ -306,8 +310,8 @@ - (void)mediaStopWithCommand:(CDVInvokedUrlCommand*)command { - (void)setActiveTracksWithCommand:(CDVInvokedUrlCommand*)command activeTrackIds:(NSArray*)activeTrackIds textTrackStyle:(GCKMediaTextTrackStyle*)textTrackStyle { [self checkFinishDelegates]; CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:self.currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:self.currentSession]]; + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(GCKError * error) { @@ -327,7 +331,7 @@ - (void)setActiveTracksWithCommand:(CDVInvokedUrlCommand*)command activeTrackIds - (void)queueLoadItemsWithCommand:(CDVInvokedUrlCommand *)command queueItems:(NSArray *)queueItems startIndex:(NSInteger)startIndex repeatMode:(GCKMediaRepeatMode)repeatMode { CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObjectForQueue:self.currentSession]]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(GCKError * error) { @@ -363,32 +367,33 @@ - (void) checkFinishDelegates{ #pragma -- GCKSessionManagerListener - (void)sessionManager:(GCKSessionManager *)sessionManager didStartCastSession:(GCKCastSession *)session { - self.currentSession = session; + [self setSession:session]; self.remoteMediaClient = session.remoteMediaClient; [self.remoteMediaClient addListener:self]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: [CastUtilities createSessionObject:session] ]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:self.initialCommand.callbackId]; + if (joinSessionCommand != nil) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: [CastUtilities createSessionObject:session status:self.sessionStatus] ]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:joinSessionCommand.callbackId]; + joinSessionCommand = nil; + } } - (void)sessionManager:(GCKSessionManager *)sessionManager didEndCastSession:(GCKCastSession *)session withError:(NSError *)error { - if (error != nil) { + if (error != nil && joinSessionCommand != nil) { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.debugDescription]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:self.initialCommand.callbackId]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:joinSessionCommand.callbackId]; + joinSessionCommand = nil; } if ([self.sessionStatus isEqualToString:@""]) { [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"stopped"] isAlive:true]; } else { [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:self.sessionStatus] isAlive:true]; } - + currentSession = nil; } - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeCastSession:(GCKCastSession *)session { - self.currentSession = session; - NSLog(@"here is the session"); -} - -- (void)sessionManager:(GCKSessionManager *)sessionManager didResumeSession:(GCKSession *)session { + [self setSession:session]; + [self.sessionListener onSessionRejoin:[CastUtilities createSessionObject:session status:self.sessionStatus]]; } #pragma -- GCKRemoteMediaClientListener @@ -397,20 +402,20 @@ - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didStartMediaSessionWit } - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didUpdateMediaStatus:(GCKMediaStatus *)mediaStatus { - if (self.currentSession == nil) { + if (currentSession == nil) { [self.sessionListener onMediaUpdated:@{} isAlive:false]; return; } if (![[NSUserDefaults standardUserDefaults] boolForKey:@"jump"]) { - NSDictionary* media = [CastUtilities createMediaObject:self.currentSession]; + NSDictionary* media = [CastUtilities createMediaObject:currentSession]; [self.sessionListener onMediaUpdated:media isAlive:true]; if (!self.isRequesting) { if (mediaStatus.streamPosition > 0) { if (mediaStatus.queueItemCount > 1) { - [self.sessionListener onMediaLoaded:[CastUtilities createMediaObjectForQueue:self.currentSession]]; - self.isRequesting = YES; + [self.sessionListener onMediaLoaded:[CastUtilities createMediaObject:currentSession]]; + isRequesting = YES; } else { [self.sessionListener onMediaLoaded:media]; @@ -420,7 +425,7 @@ - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didUpdateMediaStatus:(G } } else { - NSDictionary* media = [CastUtilities createMediaObject:self.currentSession]; + NSDictionary* media = [CastUtilities createMediaObject:currentSession]; [self.sessionListener onMediaUpdated:media isAlive:false]; } @@ -448,7 +453,7 @@ - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didReceiveQueueItemIDs: #pragma -- GCKGenericChannelDelegate - (void)castChannel:(GCKGenericChannel *)channel didReceiveTextMessage:(NSString *)message withNamespace:(NSString *)protocolNamespace { - NSDictionary* currentSession = [CastUtilities createSessionObject:self.currentSession]; - [self.sessionListener onMessageReceived:currentSession namespace:protocolNamespace message:message]; + NSDictionary* session = [CastUtilities createSessionObject:currentSession status:self.sessionStatus]; + [self.sessionListener onMessageReceived:session namespace:protocolNamespace message:message]; } @end From dcc954dbaa47e9cdb058e47b65ed29079ab4284e Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 25 Nov 2019 08:33:47 -0700 Subject: [PATCH 103/166] (ios) Modify the way scans keep track of whether or not to stop a scan. make scanCommand a private variable. --- src/ios/Chromecast.h | 1 - src/ios/Chromecast.m | 44 +++++++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/ios/Chromecast.h b/src/ios/Chromecast.h index 6206a2e..fe2d63b 100644 --- a/src/ios/Chromecast.h +++ b/src/ios/Chromecast.h @@ -18,7 +18,6 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, strong) NSMutableArray* devicesAvailable; @property (nonatomic, strong) ChromecastSession* currentSession; @property (nonatomic, strong) CDVInvokedUrlCommand* eventCommand; -@property (nonatomic, strong) CDVInvokedUrlCommand* scanCommand; - (void)setup:(CDVInvokedUrlCommand*) command; - (void)initialize:(CDVInvokedUrlCommand*)command; diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 475ab48..3bb0ad1 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -17,6 +17,8 @@ @interface Chromecast() @implementation Chromecast NSString* appId = nil; +CDVInvokedUrlCommand* scanCommand = nil; +int scansRunning = 0; - (void)pluginInitialize { [super pluginInitialize]; @@ -91,15 +93,21 @@ - (void)findAvailableReceiver:(void(^)(void))successCallback { } - (void)stopRouteScanForSetup { - if (self.scanCommand != nil) { - [self sendError:@"cancel" message:@"Scan stopped because setup triggered." command:self.scanCommand]; - self.scanCommand = nil; + if (scansRunning > 0) { + // Terminate all scans + scansRunning = 0; + [self sendError:@"cancel" message:@"Scan stopped because setup triggered." command:scanCommand]; + scanCommand = nil; + [self stopRouteScan]; } - [self stopRouteScan]; } - (BOOL)stopRouteScan:(CDVInvokedUrlCommand*)command { - [self stopRouteScan]; + if (scanCommand != nil) { + [self stopRouteScan]; + [self sendError:@"cancel" message:@"Scan stopped." command:scanCommand]; + scanCommand = nil; + } if (command != nil) { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; @@ -108,35 +116,37 @@ - (BOOL)stopRouteScan:(CDVInvokedUrlCommand*)command { } - (void)stopRouteScan { - if (self.scanCommand != nil) { - [self sendError:@"cancel" message:@"Scan stopped." command:self.scanCommand]; - self.scanCommand = nil; + if (--scansRunning <= 0) { + scansRunning = 0; + [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; } - [[GCKCastContext sharedInstance].discoveryManager stopDiscovery]; } -(BOOL) startRouteScan:(CDVInvokedUrlCommand*)command { - if (self.scanCommand != nil) { - [self sendError:@"cancel" message:@"Started a new route scan before stopping previous one." command:self.scanCommand]; + if (scanCommand != nil) { + [self sendError:@"cancel" message:@"Started a new route scan before stopping previous one." command:scanCommand]; + } else { + // Only start the scan if the user has not already started one + [self startRouteScan]; } - self.scanCommand = command; + scanCommand = command; [self sendScanUpdate]; - [self startRouteScan]; return YES; } -(void) startRouteScan { + scansRunning++; [[GCKCastContext sharedInstance].discoveryManager startDiscovery]; } - (void)sendScanUpdate { - if (self.scanCommand == nil) { + if (scanCommand == nil) { return; } - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:[CastUtilities createDeviceObject:self.devicesAvailable]]; - [pluginResult setKeepCallback:@(true)]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:self.scanCommand.callbackId]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:[CastUtilities createDeviceArray]]; + [pluginResult setKeepCallback:@(true)]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:scanCommand.callbackId]; } - (void)requestSession:(CDVInvokedUrlCommand*) command { From a271c9c98f06845eeb98d321eaab5c783a975459 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 25 Nov 2019 08:34:59 -0700 Subject: [PATCH 104/166] (ios) [no change] Remove sendScan (forgot to remove on commit "(ios) Move device list building to utilities.") --- src/ios/Chromecast.m | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 3bb0ad1..727b947 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -443,16 +443,6 @@ - (void)sendEvent:(NSString *)eventName args:(NSArray *)args{ [self.commandDelegate sendPluginResult:pluginResult callbackId:self.eventCommand.callbackId]; } -- (void)sendScan:(NSArray *)args{ - if (self.scanCommand == nil) { - return; - } - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:args]; - [pluginResult setKeepCallback:@(YES)]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:self.scanCommand.callbackId]; -} - - - (void)sendError:(NSString *)code message:(NSString *)message command:(CDVInvokedUrlCommand*)command{ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[CastUtilities createError:code message:message]]; From 17fc854d03c4de22dd74b1e68e45f844632fbec4 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 25 Nov 2019 08:36:42 -0700 Subject: [PATCH 105/166] (ios) [no change] Remove createSessionObject (the one with no "status" parameter) (forgot to remove on commit "(ios) Let ChromecastSession handle all the rejoining logic...") --- src/ios/CastUtilities.h | 1 - src/ios/CastUtilities.m | 24 ------------------------ 2 files changed, 25 deletions(-) diff --git a/src/ios/CastUtilities.h b/src/ios/CastUtilities.h index fc2d4a1..330bc40 100644 --- a/src/ios/CastUtilities.h +++ b/src/ios/CastUtilities.h @@ -17,7 +17,6 @@ NS_ASSUME_NONNULL_BEGIN + (GCKMediaTextTrackStyle *)buildTextTrackStyle:(NSDictionary *)data; +(GCKMediaMetadata*)buildMediaMetadata:(NSDictionary*)data; +(NSArray*)getMetadataImages:(NSData*)imagesRaw; -+(NSDictionary*)createSessionObject:(GCKCastSession*)session; + (NSDictionary*)createSessionObject:(GCKCastSession *)session status:(NSString*)status; +(NSDictionary*)createMediaObject:(GCKCastSession*)session; +(NSDictionary*)createMediaInfoObject:(GCKMediaInformation*)mediaInfo; diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index c540f67..2de44eb 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -467,30 +467,6 @@ +(NSString*)getClientMetadataName:(NSString*)iOSName { return images; } -+ (NSDictionary *)createSessionObject:(GCKCastSession *)session { - NSDictionary* media = [CastUtilities createMediaObject:session]; - NSMutableArray* mediaArray = [NSMutableArray new]; - if ([media count] != 0) { - [mediaArray addObject: media]; - } - return @{ - @"appId" : session.applicationMetadata.applicationID? session.applicationMetadata.applicationID : @"", - @"media" : mediaArray, - @"appImages" : @{}, - @"sessionId" : session.sessionID? session.sessionID : @"", - @"displayName" : session.applicationMetadata.applicationName? session.applicationMetadata.applicationName : @"", - @"receiver" : @{ - @"friendlyName" : session.device.friendlyName? session.device.friendlyName : @"", - @"label" : session.device.uniqueID, - @"volume" : @{ - @"level" : @(session.currentDeviceVolume), - @"muted" : @(session.currentDeviceMuted) - } - }, - - }; -} - + (NSDictionary*)createSessionObject:(GCKCastSession *)session status:(NSString*)status { return @{ @"appId" : session.applicationMetadata.applicationID? session.applicationMetadata.applicationID : @"", From d29c21b0aa8515fd3b37cdb060d353bad9fd8aa7 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 25 Nov 2019 08:38:05 -0700 Subject: [PATCH 106/166] (ios) create one endSession function --- src/ios/Chromecast.m | 10 ++-------- src/ios/ChromecastSession.h | 1 + src/ios/ChromecastSession.m | 11 +++++++++++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 727b947..192f039 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -267,17 +267,11 @@ - (void)setReceiverMuted:(CDVInvokedUrlCommand*) command { } - (void)sessionStop:(CDVInvokedUrlCommand*)command { - self.currentSession.sessionStatus = @"stopped"; - BOOL result = [[GCKCastContext sharedInstance].sessionManager endSessionAndStopCasting:true]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:result]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + [self.currentSession endSession:command killSession:YES]; } - (void)sessionLeave:(CDVInvokedUrlCommand*) command { - self.currentSession.sessionStatus = @"disconnected"; - BOOL result = [[GCKCastContext sharedInstance].sessionManager endSession]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:result]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + [self.currentSession endSession:command killSession:NO]; } - (void)loadMedia:(CDVInvokedUrlCommand*) command { diff --git a/src/ios/ChromecastSession.h b/src/ios/ChromecastSession.h index 6c519c0..0aaee4c 100644 --- a/src/ios/ChromecastSession.h +++ b/src/ios/ChromecastSession.h @@ -27,6 +27,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)tryRejoin; - (void)joinDevice:(GCKDevice*)device cdvCommand:(CDVInvokedUrlCommand*)command; - (CastRequestDelegate*)createGeneralRequestDelegate:(CDVInvokedUrlCommand*)command; +- (void)endSession:(CDVInvokedUrlCommand*)command killSession:(BOOL)killSession; - (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted nvewLevel:(float)newLevel; - (void)setMediaMutedWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted; - (void)setMediaVolumeWithCommand:(CDVInvokedUrlCommand*)withCommand newVolumeLevel:(float)newLevel; diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index f5c55a0..bbecbfa 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -71,6 +71,17 @@ -(CastRequestDelegate*)createGeneralRequestDelegate:(CDVInvokedUrlCommand*)comma return delegate; } +- (void)endSession:(CDVInvokedUrlCommand*)command killSession:(BOOL)killSession { + BOOL result = [[GCKCastContext sharedInstance].sessionManager endSessionAndStopCasting:killSession]; + if (killSession) { + self.sessionStatus = @"stopped"; + } else { + self.sessionStatus = @"disconnected"; + } + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:result]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + - (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted nvewLevel:(float)newLevel { [self checkFinishDelegates]; CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ From 37c3f8812e74ecdc1e230205797720137a544db8 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sat, 30 Nov 2019 17:17:32 -0700 Subject: [PATCH 107/166] (ios) try to join selected route for 5 seconds --- src/ios/Chromecast.m | 2 +- tests/www/js/tests_auto.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 192f039..ae76eb3 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -347,7 +347,7 @@ - (void)selectRoute:(CDVInvokedUrlCommand*)command { return YES; } return NO; - } forTries:1 callback:^(BOOL passed) { + } forTries:5 callback:^(BOOL passed) { if (!passed) { [self sendError:@"timeout" message:[NSString stringWithFormat:@"Failed to join route (%@) after 15s and %d tries.", routeID, 15] command:command]; } diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index 5d80acf..3911cc0 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -256,7 +256,7 @@ }, function (err) { assert.isObject(err); assert.equal(err.code, chrome.cast.ErrorCode.TIMEOUT); - assert.match(err.description, new RegExp('^Failed to join route \\(' + routeId + '\\) after 15s and [0-9]* tries\\.$')); + assert.match(err.description, new RegExp('^Failed to join route \\(' + routeId + '\\) after [0-9]+s and [0-9]+ tries\\.$')); done(); }); }); From 8feaf9959de80bc15dd23fc63b1ed6987da103a2 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 03:50:15 -0700 Subject: [PATCH 108/166] (ios) WIP remove isAlive param, it is never used in these contexts, just passed around --- src/ios/CastRequestDelegate.h | 10 +++++----- src/ios/CastRequestDelegate.m | 10 +++++----- src/ios/Chromecast.m | 6 +++--- src/ios/ChromecastSession.m | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/ios/CastRequestDelegate.h b/src/ios/CastRequestDelegate.h index 43bea74..ac0b2c0 100644 --- a/src/ios/CastRequestDelegate.h +++ b/src/ios/CastRequestDelegate.h @@ -14,8 +14,8 @@ NS_ASSUME_NONNULL_BEGIN -(void)onSessionRejoin:(NSDictionary*)session; -(void)onMediaLoaded:(NSDictionary*)media; --(void)onMediaUpdated:(NSDictionary*)media isAlive:(BOOL)isAlive; --(void)onSessionUpdated:(NSDictionary*)session isAlive:(BOOL)isAlive; +-(void)onMediaUpdated:(NSDictionary*)media; +-(void)onSessionUpdated:(NSDictionary*)session; -(void)onSessionEnd:(NSDictionary*)session; -(void)onMessageReceived:(NSDictionary*)session namespace:(NSString*)namespace message:(NSString*)message; @end @@ -24,8 +24,8 @@ NS_ASSUME_NONNULL_BEGIN { void (^onSessionRejoin)(NSDictionary* session); void (^onMediaLoaded)(NSDictionary* media); - void (^onMediaUpdated)(NSDictionary* media,BOOL isAlive); - void (^onSessionUpdated)(NSDictionary* session, BOOL isAlive); + void (^onMediaUpdated)(NSDictionary* media); + void (^onSessionUpdated)(NSDictionary* session); void (^onSessionEnd)(NSDictionary* session); void (^onMessageReceived)(NSDictionary* session,NSString* namespace,NSString* message); } @@ -33,7 +33,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, copy) void (^onReceiverAvailableUpdate)(BOOL available); //@property (nonatomic, copy) void (^onSessionRejoin)(NSDictionary* session); -- (instancetype)initWithReceiverAvailableUpdate:(void(^)(BOOL available))onReceiverAvailableUpdate onSessionRejoin:(void(^)(NSDictionary* session))onSessionRejoin onMediaLoaded:(void(^)(NSDictionary* m))onMediaLoaded onMediaUpdated:(void(^)(NSDictionary* media, BOOL isAlive))onMediaUpdated onSessionUpdated:(void(^)(NSDictionary* session, BOOL isAlive))onSessionUpdated onSessionEnd:(void(^)(NSDictionary* session))onSessionEnd onMessageReceived:(void(^)(NSDictionary* session,NSString* namespace,NSString* message))onMessageReceived ; +- (instancetype)initWithReceiverAvailableUpdate:(void(^)(BOOL available))onReceiverAvailableUpdate onSessionRejoin:(void(^)(NSDictionary* session))onSessionRejoin onMediaLoaded:(void(^)(NSDictionary* m))onMediaLoaded onMediaUpdated:(void(^)(NSDictionary* media))onMediaUpdated onSessionUpdated:(void(^)(NSDictionary* session))onSessionUpdated onSessionEnd:(void(^)(NSDictionary* session))onSessionEnd onMessageReceived:(void(^)(NSDictionary* session,NSString* namespace,NSString* message))onMessageReceived ; @end @interface CastRequestDelegate : NSObject diff --git a/src/ios/CastRequestDelegate.m b/src/ios/CastRequestDelegate.m index 92352be..e6f441b 100644 --- a/src/ios/CastRequestDelegate.m +++ b/src/ios/CastRequestDelegate.m @@ -9,7 +9,7 @@ @implementation CastConnectionListener -- (instancetype)initWithReceiverAvailableUpdate:(void(^)(BOOL available))onReceiverAvailableUpdate onSessionRejoin:(void(^)(NSDictionary* session))onSessionRejoin onMediaLoaded:(void(^)(NSDictionary* media))onMediaLoaded onMediaUpdated:(void(^)(NSDictionary* media, BOOL isAlive))onMediaUpdated onSessionUpdated:(void(^)(NSDictionary* session, BOOL isAlive))onSessionUpdated onSessionEnd:(void(^)(NSDictionary* session))onSessionEnd onMessageReceived:(void(^)(NSDictionary* session,NSString* namespace,NSString* message))onMessageReceived { +- (instancetype)initWithReceiverAvailableUpdate:(void(^)(BOOL available))onReceiverAvailableUpdate onSessionRejoin:(void(^)(NSDictionary* session))onSessionRejoin onMediaLoaded:(void(^)(NSDictionary* media))onMediaLoaded onMediaUpdated:(void(^)(NSDictionary* media))onMediaUpdated onSessionUpdated:(void(^)(NSDictionary* session))onSessionUpdated onSessionEnd:(void(^)(NSDictionary* session))onSessionEnd onMessageReceived:(void(^)(NSDictionary* session,NSString* namespace,NSString* message))onMessageReceived { self = [super init]; if (self) { @@ -40,16 +40,16 @@ - (void)onCastStateChanged:(NSNotification*)notification { } } -- (void)onMediaUpdated:(NSDictionary *)media isAlive:(BOOL)isAlive { - onMediaUpdated(media,isAlive); +- (void)onMediaUpdated:(NSDictionary *)media { + onMediaUpdated(media); } - (void)onMediaLoaded:(NSDictionary *)media { onMediaLoaded(media); } -- (void)onSessionUpdated:(NSDictionary *)session isAlive:(BOOL)isAlive { - onSessionUpdated(session,isAlive); +- (void)onSessionUpdated:(NSDictionary *)session { + onSessionUpdated(session); } - (void)onSessionEnd:(NSDictionary *)session { diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index ae76eb3..535096e 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -396,7 +396,7 @@ - (void)onMediaLoaded:(NSDictionary *)media { [self sendEvent:@"MEDIA_LOAD" args:@[media]]; } -- (void)onMediaUpdated:(NSDictionary *)media isAlive:(BOOL)isAlive { +- (void)onMediaUpdated:(NSDictionary *)media { [self sendEvent:@"MEDIA_UPDATE" args:@[media]]; } @@ -404,7 +404,7 @@ - (void)onSessionRejoin:(NSDictionary*)session { [self sendEvent:@"SESSION_LISTENER" args:@[session]]; } -- (void)onSessionUpdated:(NSDictionary *)session isAlive:(BOOL)isAlive { +- (void)onSessionUpdated:(NSDictionary *)session { [self sendEvent:@"SESSION_UPDATE" args:@[session]]; } @@ -451,7 +451,7 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didEndSession:(GCKSes [self.commandDelegate sendPluginResult:pluginResult callbackId:self.sessionCommand.callbackId]; } if ([self.currentSession.sessionStatus isEqual: @"stopped"]) { - [self.currentSession.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"stopped"] isAlive:true]; + [self.currentSession.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"stopped"]]; [self sendError:@"cancel" message:@"Session is stopped." command:self.sessionCommand]; } } diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index bbecbfa..34a18ea 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -395,9 +395,9 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didEndCastSession:(GC joinSessionCommand = nil; } if ([self.sessionStatus isEqualToString:@""]) { - [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"stopped"] isAlive:true]; + [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"stopped"]]; } else { - [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:self.sessionStatus] isAlive:true]; + [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:self.sessionStatus]]; } currentSession = nil; } From b71454575c56ae9f9e5ce6cc92fe40c954f4b10c Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 04:40:43 -0700 Subject: [PATCH 109/166] (ios) Add CDVPlugin event onReset so that we can stop scans from running when we have changed/refreshed the page --- src/ios/Chromecast.h | 1 + src/ios/Chromecast.m | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/src/ios/Chromecast.h b/src/ios/Chromecast.h index fe2d63b..63534bf 100644 --- a/src/ios/Chromecast.h +++ b/src/ios/Chromecast.h @@ -19,6 +19,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, strong) ChromecastSession* currentSession; @property (nonatomic, strong) CDVInvokedUrlCommand* eventCommand; +- (void)onReset; - (void)setup:(CDVInvokedUrlCommand*) command; - (void)initialize:(CDVInvokedUrlCommand*)command; - (BOOL)startRouteScan:(CDVInvokedUrlCommand*)command; diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 535096e..1919c4d 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -56,6 +56,13 @@ - (void)setAppId:(NSString*)applicationId { self.currentSession = [self.currentSession initWithListener:self cordovaDelegate:self.commandDelegate]; } +// Override CDVPlugin onReset +// Called when the webview navigates to a new page or refreshes +// Clean up any running process +- (void)onReset { + [self stopRouteScanForSetup]; +} + - (void)setup:(CDVInvokedUrlCommand*) command { self.eventCommand = command; [self stopRouteScanForSetup]; From 1cceb85c215c87e1a5e71dbd865c9d5563d63567 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 04:42:46 -0700 Subject: [PATCH 110/166] (ios) WIP fix requestSession bug where it was not getting the devices (because of commit "Move device list building to utilities. Simplify receiverListener methods.") --- src/ios/Chromecast.m | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 1919c4d..4dc5872 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -158,7 +158,9 @@ - (void)sendScanUpdate { - (void)requestSession:(CDVInvokedUrlCommand*) command { UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Cast to" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; - for (GCKDevice* device in self.devicesAvailable) { + GCKDiscoveryManager* discoveryManager = GCKCastContext.sharedInstance.discoveryManager; + for (int i = 0; i < [discoveryManager deviceCount]; i++) { + GCKDevice* device = [discoveryManager deviceAtIndex:i]; [alert addAction:[UIAlertAction actionWithTitle:device.friendlyName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self.currentSession joinDevice:device cdvCommand:command]; }]]; From 64035e6eebe2080dbb9d7c3852f5a500b7ffe3ce Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 15:56:26 -0700 Subject: [PATCH 111/166] (test) Ensure that chromecast is initialized in before of describe test block --- tests/www/js/tests_manual_primary_1.js | 67 +++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/tests/www/js/tests_manual_primary_1.js b/tests/www/js/tests_manual_primary_1.js index d78c074..2d2c437 100644 --- a/tests/www/js/tests_manual_primary_1.js +++ b/tests/www/js/tests_manual_primary_1.js @@ -280,6 +280,38 @@ }); describe('chrome.cast.requestSession', function () { + before('ensure initialized', function (done) { + this.timeout(15000); + utils.setAction('Initializing...'); + + var finished = false; // Need this so we stop testing after being finished + var unavailable = 'unavailable'; + var available = 'available'; + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: unavailable, repeats: true }, + { id: available, repeats: true } + ], function () { + finished = true; + done(); + }); + var apiConfig = new chrome.cast.ApiConfig( + new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), + function (sess) { + failed = true; + session = sess; + assert.fail('Should not receive session on initialize. We should only call this initialize when there is no existing session.'); + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); + chrome.cast.initialize(apiConfig, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); it('dismiss should return error', function (done) { utils.setAction('1. Click "Open Dialog".
      2. Click outside of the chromecast chooser dialog to dismiss it.', 'Open Dialog', function () { chrome.cast.requestSession(function (sess) { @@ -353,8 +385,39 @@ }); describe('External Sender Sends Commands', function () { - before(function () { - assert.equal(session.status, chrome.cast.SessionStatus.STOPPED); + before('ensure initialized', function (done) { + this.timeout(15000); + utils.setAction('Initializing...'); + + var finished = false; // Need this so we stop testing after being finished + var unavailable = 'unavailable'; + var available = 'available'; + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: unavailable, repeats: true }, + { id: available, repeats: true } + ], function () { + finished = true; + if (session) { + assert.equal(session.status, chrome.cast.SessionStatus.STOPPED); + } + done(); + }); + var apiConfig = new chrome.cast.ApiConfig( + new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), + function (sess) { + session = sess; + assert.fail('Should not receive session on initialize. We should only call this initialize when there is no existing session.'); + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); + chrome.cast.initialize(apiConfig, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); it('Join external session', function (done) { if (isDesktop) { From 0ca8d4f6e6248a6efa98b2ece0619618e31f817b Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 16:41:09 -0700 Subject: [PATCH 112/166] (ios) WIP Calculate the session's status except for the case when we are doing session.leave --- src/ios/CastUtilities.h | 1 + src/ios/CastUtilities.m | 19 ++++++++++++++++++- src/ios/ChromecastSession.h | 1 - src/ios/ChromecastSession.m | 32 ++++++++++++++++++++++---------- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/ios/CastUtilities.h b/src/ios/CastUtilities.h index 330bc40..df4dcfd 100644 --- a/src/ios/CastUtilities.h +++ b/src/ios/CastUtilities.h @@ -17,6 +17,7 @@ NS_ASSUME_NONNULL_BEGIN + (GCKMediaTextTrackStyle *)buildTextTrackStyle:(NSDictionary *)data; +(GCKMediaMetadata*)buildMediaMetadata:(NSDictionary*)data; +(NSArray*)getMetadataImages:(NSData*)imagesRaw; ++ (NSDictionary*)createSessionObject:(GCKCastSession *)session; + (NSDictionary*)createSessionObject:(GCKCastSession *)session status:(NSString*)status; +(NSDictionary*)createMediaObject:(GCKCastSession*)session; +(NSDictionary*)createMediaInfoObject:(GCKMediaInformation*)mediaInfo; diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index 2de44eb..01c801a 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -467,6 +467,10 @@ +(NSString*)getClientMetadataName:(NSString*)iOSName { return images; } ++ (NSDictionary*)createSessionObject:(GCKCastSession *)session { + return [CastUtilities createSessionObject:session status:@""]; +} + + (NSDictionary*)createSessionObject:(GCKCastSession *)session status:(NSString*)status { return @{ @"appId" : session.applicationMetadata.applicationID? session.applicationMetadata.applicationID : @"", @@ -482,7 +486,7 @@ + (NSDictionary*)createSessionObject:(GCKCastSession *)session status:(NSString* @"muted" : @(session.currentDeviceMuted) } }, - @"status":status + @"status":![status isEqual: @""]? status : [CastUtilities getConnectionStatus:session.connectionState] }; } @@ -819,6 +823,19 @@ + (NSString *)getRepeatMode:(GCKMediaRepeatMode)repeatMode { return @"REPEAT_OFF"; } } + ++ (NSString *)getConnectionStatus:(GCKConnectionState)connectionState { + switch (connectionState) { + case GCKConnectionStateConnecting: + case GCKConnectionStateConnected: + return @"connected"; + case GCKConnectionStateDisconnected: + case GCKConnectionStateDisconnecting: + default: + return @"stopped"; + } +} + + (NSString *)getPlayerState:(GCKMediaPlayerState)playerState { switch (playerState) { case GCKMediaPlayerStateBuffering: diff --git a/src/ios/ChromecastSession.h b/src/ios/ChromecastSession.h index 0aaee4c..45ae2c1 100644 --- a/src/ios/ChromecastSession.h +++ b/src/ios/ChromecastSession.h @@ -21,7 +21,6 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, retain) NSMutableArray* requestDelegates; @property (nonatomic, retain) id sessionListener; @property (nonatomic, retain) NSMutableDictionary* genericChannels; -@property (nonatomic, retain) NSString* sessionStatus; - (instancetype)initWithListener:(id)listener cordovaDelegate:(id)cordovaDelegate; - (void)tryRejoin; diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index 34a18ea..da8d5e8 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -18,13 +18,13 @@ @interface ChromecastSession() @implementation ChromecastSession GCKCastSession* currentSession; CDVInvokedUrlCommand* joinSessionCommand; +BOOL isDisconnecting = NO; - (instancetype)initWithListener:(id)listener cordovaDelegate:(id)cordovaDelegate { self = [super init]; self.sessionListener = listener; self.commandDelegate = cordovaDelegate; - self.sessionStatus = @"disconnected"; self.castContext = [GCKCastContext sharedInstance]; self.sessionManager = self.castContext.sessionManager; @@ -37,12 +37,11 @@ - (instancetype)initWithListener:(id)listener cordovaDelega - (void)setSession:(GCKCastSession*)session { currentSession = session; - self.sessionStatus = @"connected"; } - (void)tryRejoin { if (currentSession != nil) { - [self.sessionListener onSessionRejoin:[CastUtilities createSessionObject:currentSession status:self.sessionStatus]]; + [self.sessionListener onSessionRejoin:[CastUtilities createSessionObject:currentSession]]; } } @@ -382,29 +381,42 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didStartCastSession:( self.remoteMediaClient = session.remoteMediaClient; [self.remoteMediaClient addListener:self]; if (joinSessionCommand != nil) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: [CastUtilities createSessionObject:session status:self.sessionStatus] ]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: [CastUtilities createSessionObject:session] ]; [self.commandDelegate sendPluginResult:pluginResult callbackId:joinSessionCommand.callbackId]; joinSessionCommand = nil; } } - (void)sessionManager:(GCKSessionManager *)sessionManager didEndCastSession:(GCKCastSession *)session withError:(NSError *)error { + // Clear the session + currentSession = nil; + + // Did we fail on a join session command? if (error != nil && joinSessionCommand != nil) { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.debugDescription]; [self.commandDelegate sendPluginResult:pluginResult callbackId:joinSessionCommand.callbackId]; joinSessionCommand = nil; + return; } - if ([self.sessionStatus isEqualToString:@""]) { - [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"stopped"]]; + + // Else, are we just leaving the session? (leaving results in disconnected status) + if (isDisconnecting) { + // Clear is isDisconnecting + isDisconnecting = NO; + [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"disconnected"]]; } else { - [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:self.sessionStatus]]; + [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session]]; + } + + // Do we have any additional endSessionCallbacks? + if (endSessionCallback) { + endSessionCallback(YES); } - currentSession = nil; } - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeCastSession:(GCKCastSession *)session { [self setSession:session]; - [self.sessionListener onSessionRejoin:[CastUtilities createSessionObject:session status:self.sessionStatus]]; + [self.sessionListener onSessionRejoin:[CastUtilities createSessionObject:session]]; } #pragma -- GCKRemoteMediaClientListener @@ -464,7 +476,7 @@ - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didReceiveQueueItemIDs: #pragma -- GCKGenericChannelDelegate - (void)castChannel:(GCKGenericChannel *)channel didReceiveTextMessage:(NSString *)message withNamespace:(NSString *)protocolNamespace { - NSDictionary* session = [CastUtilities createSessionObject:currentSession status:self.sessionStatus]; + NSDictionary* session = [CastUtilities createSessionObject:currentSession]; [self.sessionListener onMessageReceived:session namespace:protocolNamespace message:message]; } @end From e7a38b0d70fcf6d4423a94260f574a5dc1ba37f1 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 17:01:47 -0700 Subject: [PATCH 113/166] (ios) WIP Added endSessionWithCallback to reduce code duplication by allowing requestSession's stop casting option to use the same endSesssion controls --- src/ios/Chromecast.m | 25 ++++--------------------- src/ios/ChromecastSession.h | 1 + src/ios/ChromecastSession.m | 18 +++++++++++++----- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 4dc5872..57fa026 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -12,7 +12,6 @@ #define IPAD UIUserInterfaceIdiomPad @interface Chromecast() -@property (nonatomic, strong) CDVInvokedUrlCommand *sessionCommand; @end @implementation Chromecast @@ -166,16 +165,13 @@ - (void)requestSession:(CDVInvokedUrlCommand*) command { }]]; } [alert addAction:[UIAlertAction actionWithTitle:@"Stop Casting" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { - NSLog(@"Stop Casting"); - self.sessionCommand = command; - self.currentSession.sessionStatus = @"stopped"; - [[GCKCastContext sharedInstance].sessionManager endSession]; - + [self.currentSession endSessionWithCallback:^{ + [self sendError:@"cancel" message:@"" command:command]; + } killSession:YES]; }]]; [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { - NSLog(@"Canceld"); [self.currentSession.remoteMediaClient stop]; - [self sendError:@"cancel" message:@"Casting is stopped." command:command]; + [self sendError:@"cancel" message:@"" command:command]; }]]; if (IDIOM == IPAD) { alert.popoverPresentationController.sourceView = self.webView; @@ -453,17 +449,4 @@ - (void)sendError:(NSString *)code message:(NSString *)message command:(CDVInvok [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } -- (void)sessionManager:(GCKSessionManager *)sessionManager didEndSession:(GCKSession *)session withError:(NSError *)error { - - if (error != nil) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.debugDescription]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:self.sessionCommand.callbackId]; - } - if ([self.currentSession.sessionStatus isEqual: @"stopped"]) { - [self.currentSession.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"stopped"]]; - [self sendError:@"cancel" message:@"Session is stopped." command:self.sessionCommand]; - } -} - - @end diff --git a/src/ios/ChromecastSession.h b/src/ios/ChromecastSession.h index 45ae2c1..0105973 100644 --- a/src/ios/ChromecastSession.h +++ b/src/ios/ChromecastSession.h @@ -30,6 +30,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted nvewLevel:(float)newLevel; - (void)setMediaMutedWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted; - (void)setMediaVolumeWithCommand:(CDVInvokedUrlCommand*)withCommand newVolumeLevel:(float)newLevel; +- (void)endSessionWithCallback:(void(^)(void))callback killSession:(BOOL)killSession; - (void)setReceiverVolumeLevelWithCommand:(CDVInvokedUrlCommand*)withCommand newLevel:(float)newLevel; - (void)setReceiverMutedWithCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted; - (void)loadMediaWithCommand:(CDVInvokedUrlCommand*)command mediaInfo:(GCKMediaInformation*)mediaInfo autoPlay:(BOOL)autoPlay currentTime : (double)currentTime; diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index da8d5e8..1de5734 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -71,14 +71,22 @@ -(CastRequestDelegate*)createGeneralRequestDelegate:(CDVInvokedUrlCommand*)comma } - (void)endSession:(CDVInvokedUrlCommand*)command killSession:(BOOL)killSession { - BOOL result = [[GCKCastContext sharedInstance].sessionManager endSessionAndStopCasting:killSession]; + NSLog(@"kk endSession"); + [self endSessionWithCallback:^{ + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } killSession:killSession]; +} + +- (void)endSessionWithCallback:(void(^)(void))callback killSession:(BOOL)killSession { + NSLog(@"kk endSessionWithCallback"); if (killSession) { - self.sessionStatus = @"stopped"; + [currentSession endWithAction:GCKSessionEndActionStopCasting]; } else { - self.sessionStatus = @"disconnected"; + isDisconnecting = YES; + [currentSession endWithAction:GCKSessionEndActionLeave]; } - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:result]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + callback(); } - (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted nvewLevel:(float)newLevel { From f18e5ccb655b7fcbfab1cf83dc3870ecd3669719 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 17:02:48 -0700 Subject: [PATCH 114/166] (test) WIP session.stop should happen in a specific order --- tests/www/js/tests_auto.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index 3911cc0..35f04d3 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -430,7 +430,7 @@ }); it('session.stop should stop the session', function (done) { // Set up the expected calls - var called = utils.waitForAllCalls([ + var called = utils.callOrder([ { id: success, repeats: false }, { id: update, repeats: true } ], done); From 2e35bab9a7aca10dad89bfd865e54e6b5c9727bb Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 17:04:58 -0700 Subject: [PATCH 115/166] (test) WIP Remove some unintended copy pasta --- tests/www/js/tests_auto.js | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index 35f04d3..ea0b182 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -583,13 +583,7 @@ { id: success, repeats: false }, { id: update, repeats: true } ], done); - media.addUpdateListener(function listener (isAlive) { - assert.notEqual(media.playerState, chrome.cast.media.PlayerState.IDLE); - if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { - media.removeUpdateListener(listener); - called(update); - } - }); + // Ensure we select a different volume var vol = media.volume.level; if (vol) { @@ -621,13 +615,7 @@ { id: success, repeats: false }, { id: update, repeats: true } ], done); - media.addUpdateListener(function listener (isAlive) { - assert.notEqual(media.playerState, chrome.cast.media.PlayerState.IDLE); - if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { - media.removeUpdateListener(listener); - called(update); - } - }); + var muted = true; var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, muted)); @@ -653,13 +641,7 @@ { id: success, repeats: false }, { id: update, repeats: true } ], done); - media.addUpdateListener(function listener (isAlive) { - assert.notEqual(media.playerState, chrome.cast.media.PlayerState.IDLE); - if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { - media.removeUpdateListener(listener); - called(update); - } - }); + // Ensure we select a different volume var vol = media.volume.level; if (vol) { From 95496d63b687ba21936ac4322a90c3eb435a43d8 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 17:06:48 -0700 Subject: [PATCH 116/166] (test) (ios) WIP ensure there is no idelReason when media is active --- src/ios/CastUtilities.m | 8 +++++--- tests/www/js/tests_auto.js | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index 01c801a..4dc3592 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -804,8 +804,9 @@ + (NSString *)getIdleReason:(GCKMediaPlayerIdleReason)reason { return @"FINISHED"; case GCKMediaPlayerIdleReasonInterrupted: return @"INTERRUPTED"; + case GCKMediaPlayerIdleReasonNone: default: - return idleReason; + return nil; } } @@ -838,14 +839,15 @@ + (NSString *)getConnectionStatus:(GCKConnectionState)connectionState { + (NSString *)getPlayerState:(GCKMediaPlayerState)playerState { switch (playerState) { + case GCKMediaPlayerStateLoading: case GCKMediaPlayerStateBuffering: return @"BUFFERING"; - case GCKMediaPlayerStateIdle: - return @"IDLE"; case GCKMediaPlayerStatePaused: return @"PAUSED"; case GCKMediaPlayerStatePlaying: return @"PLAYING"; + case GCKMediaPlayerStateUnknown: + case GCKMediaPlayerStateIdle: default: return @"IDLE"; } diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index ea0b182..91364da 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -563,6 +563,7 @@ assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.GENERIC); assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.GENERIC); + assert.notExists(media.idleReason); media.addUpdateListener(function listener (isAlive) { assert.isTrue(isAlive); utils.testMediaProperties(media); From 5e7e7f38c13019def26b39dd86f52cb7d205da0c Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 17:31:08 -0700 Subject: [PATCH 117/166] (ios) WIP remove isRequesting and create slighlty unified requestDelegate creators to avoid code duplication --- src/ios/ChromecastSession.h | 2 - src/ios/ChromecastSession.m | 236 +++++++++--------------------------- 2 files changed, 60 insertions(+), 178 deletions(-) diff --git a/src/ios/ChromecastSession.h b/src/ios/ChromecastSession.h index 0105973..91403da 100644 --- a/src/ios/ChromecastSession.h +++ b/src/ios/ChromecastSession.h @@ -18,14 +18,12 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, retain) GCKSessionManager* sessionManager; @property (nonatomic, retain) GCKRemoteMediaClient* remoteMediaClient; @property (nonatomic, retain) GCKCastContext* castContext; -@property (nonatomic, retain) NSMutableArray* requestDelegates; @property (nonatomic, retain) id sessionListener; @property (nonatomic, retain) NSMutableDictionary* genericChannels; - (instancetype)initWithListener:(id)listener cordovaDelegate:(id)cordovaDelegate; - (void)tryRejoin; - (void)joinDevice:(GCKDevice*)device cdvCommand:(CDVInvokedUrlCommand*)command; -- (CastRequestDelegate*)createGeneralRequestDelegate:(CDVInvokedUrlCommand*)command; - (void)endSession:(CDVInvokedUrlCommand*)command killSession:(BOOL)killSession; - (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted nvewLevel:(float)newLevel; - (void)setMediaMutedWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted; diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index 1de5734..006f169 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -8,21 +8,16 @@ #import "ChromecastSession.h" #import "CastUtilities.h" -@interface ChromecastSession() -{ - BOOL isRequesting; -} -@property (nonatomic, assign) BOOL isRequesting; -@end - @implementation ChromecastSession GCKCastSession* currentSession; CDVInvokedUrlCommand* joinSessionCommand; BOOL isDisconnecting = NO; +NSMutableArray* requestDelegates; - (instancetype)initWithListener:(id)listener cordovaDelegate:(id)cordovaDelegate { self = [super init]; + requestDelegates = [NSMutableArray new]; self.sessionListener = listener; self.commandDelegate = cordovaDelegate; self.castContext = [GCKCastContext sharedInstance]; @@ -53,20 +48,55 @@ - (void)joinDevice:(GCKDevice*)device cdvCommand:(CDVInvokedUrlCommand*)command [self.sessionManager startSessionWithDevice:device]; } --(CastRequestDelegate*)createGeneralRequestDelegate:(CDVInvokedUrlCommand*)command { - [self checkFinishDelegates]; - CastRequestDelegate* delegate = [[CastRequestDelegate alloc] initWithSuccess:^{ +-(CastRequestDelegate*)createSessionUpdateRequestDelegate:(CDVInvokedUrlCommand*)command { + return [self createRequestDelegate:command success:^{ + [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:currentSession]]; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:currentSession status:self.sessionStatus] isAlive:NO]; - } failure:^(GCKError * error) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; + } failure:nil abortion:nil]; +} + +-(CastRequestDelegate*)createMediaUpdateRequestDelegate:(CDVInvokedUrlCommand*)command { + return [self createRequestDelegate:command success:^{ + NSLog(@"%@", [NSString stringWithFormat:@"kk requestDelegate(MediaUpdate) finished"]); + [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession]]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } failure:nil abortion:nil]; +} + +-(CastRequestDelegate*)createRequestDelegate:(CDVInvokedUrlCommand*)command success:(void(^)(void))success failure:(void(^)(GCKError*))failure abortion:(void(^)(GCKRequestAbortReason))abortion { + // set up any required defaults + if (success == nil) { + success = ^{ + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }; + } + if (failure == nil) { + failure = ^(GCKError * error) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }; + } + if (abortion == nil) { + abortion = ^(GCKRequestAbortReason abortReason) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }; + } + CastRequestDelegate* delegate = [[CastRequestDelegate alloc] initWithSuccess:^{ + [self checkFinishDelegates]; + success(); + } failure:^(GCKError * error) { + [self checkFinishDelegates]; + failure(error); } abortion:^(GCKRequestAbortReason abortReason) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + [self checkFinishDelegates]; + abortion(abortReason); }]; - [self.requestDelegates addObject:delegate]; + + [requestDelegates addObject:delegate]; return delegate; } @@ -90,81 +120,27 @@ - (void)endSessionWithCallback:(void(^)(void))callback killSession:(BOOL)killSes } - (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted nvewLevel:(float)newLevel { - [self checkFinishDelegates]; - CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - - } failure:^(GCKError * error) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } abortion:^(GCKRequestAbortReason abortReason) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; - [self.requestDelegates addObject:requestDelegate]; - [self.remoteMediaClient setStreamMuted:muted customData:nil]; - self.isRequesting = YES; - GCKRequest* request = [self.remoteMediaClient setStreamVolume:newLevel customData:nil]; - request.delegate = requestDelegate; } - (void)setMediaMutedWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted { - [self checkFinishDelegates]; - CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - - } failure:^(GCKError * error) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } abortion:^(GCKRequestAbortReason abortReason) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; - [self.requestDelegates addObject:requestDelegate]; - self.isRequesting = YES; - GCKRequest* request = [self.remoteMediaClient setStreamMuted:muted customData:nil]; - request.delegate = requestDelegate; } - (void)setMediaVolumeWithCommand:(CDVInvokedUrlCommand*)withCommand newVolumeLevel:(float)newLevel { - [self checkFinishDelegates]; - CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:withCommand.callbackId]; - - } failure:^(GCKError * error) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:withCommand.callbackId]; - } abortion:^(GCKRequestAbortReason abortReason) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:withCommand.callbackId]; - }]; - [self.requestDelegates addObject:requestDelegate]; - self.isRequesting = YES; - GCKRequest* request = [self.remoteMediaClient setStreamVolume:newLevel customData:nil]; - request.delegate = requestDelegate; + GCKRequest* request = [self.remoteMediaClient setStreamVolume:newLevel customData:nil]; + request.delegate = [self createRequestDelegate:command success:setMuted failure:nil abortion:nil]; } - (void)setReceiverVolumeLevelWithCommand:(CDVInvokedUrlCommand*)withCommand newLevel:(float)newLevel { - CastRequestDelegate* delegate = [self createGeneralRequestDelegate:withCommand]; - self.isRequesting = YES; GCKRequest* request = [currentSession setDeviceVolume:newLevel]; - request.delegate = delegate; + request.delegate = [self createSessionUpdateRequestDelegate:command]; } - (void)setReceiverMutedWithCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted { - CastRequestDelegate* delegate = [self createGeneralRequestDelegate:command]; - self.isRequesting = YES; GCKRequest* request = [currentSession setDeviceMuted:muted]; - request.delegate = delegate; + request.delegate = [self createSessionUpdateRequestDelegate:command]; } - (void)loadMediaWithCommand:(CDVInvokedUrlCommand*)command mediaInfo:(GCKMediaInformation*)mediaInfo autoPlay:(BOOL)autoPlay currentTime : (double)currentTime { @@ -184,7 +160,6 @@ - (void)loadMediaWithCommand:(CDVInvokedUrlCommand*)command mediaInfo:(GCKMediaI GCKMediaLoadOptions* options = [[GCKMediaLoadOptions alloc] init]; options.autoplay = autoPlay; options.playPosition = currentTime; - self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient loadMedia:mediaInfo withOptions:options]; request.delegate = requestDelegate; } @@ -216,134 +191,44 @@ - (void)sendMessageWithCommand:(CDVInvokedUrlCommand*)command namespace:(NSStrin } - (void)mediaSeekWithCommand:(CDVInvokedUrlCommand*)command position:(NSTimeInterval)position resumeState:(GCKMediaResumeState)resumeState { - [self checkFinishDelegates]; - CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - - } failure:^(GCKError * error) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } abortion:^(GCKRequestAbortReason abortReason) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; - - [self.requestDelegates addObject:requestDelegate]; - GCKMediaSeekOptions* options = [[GCKMediaSeekOptions alloc] init]; options.interval = position; options.resumeState = resumeState; - self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient seekWithOptions:options]; - request.delegate = requestDelegate; + request.delegate = [self createMediaUpdateRequestDelegate:command]; } - (void)queueJumpToItemWithCommand:(CDVInvokedUrlCommand *)command itemId:(NSUInteger)itemId { - [self checkFinishDelegates]; - CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - - } failure:^(GCKError * error) { + GCKRequest* request = [self.remoteMediaClient queueJumpToItemWithID:itemId]; + request.delegate = [self createRequestDelegate:command success:nil failure:^(GCKError * error) { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } abortion:^(GCKRequestAbortReason abortReason) { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; }]; - - [self.requestDelegates addObject:requestDelegate]; [NSUserDefaults.standardUserDefaults setBool:true forKey:@"jump"]; [NSUserDefaults.standardUserDefaults synchronize]; - self.isRequesting = YES; - GCKRequest* request = [self.remoteMediaClient queueJumpToItemWithID:itemId]; - request.delegate = requestDelegate; } - (void)mediaPlayWithCommand:(CDVInvokedUrlCommand*)command { - [self checkFinishDelegates]; - CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - - } failure:^(GCKError * error) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } abortion:^(GCKRequestAbortReason abortReason) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; - - [self.requestDelegates addObject:requestDelegate]; - self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient play]; - request.delegate = requestDelegate; + request.delegate = [self createMediaUpdateRequestDelegate:command]; } - (void)mediaPauseWithCommand:(CDVInvokedUrlCommand*)command { - [self checkFinishDelegates]; - CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - - } failure:^(GCKError * error) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } abortion:^(GCKRequestAbortReason abortReason) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; - - [self.requestDelegates addObject:requestDelegate]; - self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient pause]; - request.delegate = requestDelegate; + request.delegate = [self createMediaUpdateRequestDelegate:command]; } - (void)mediaStopWithCommand:(CDVInvokedUrlCommand*)command { - [self checkFinishDelegates]; - CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - - } failure:^(GCKError * error) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } abortion:^(GCKRequestAbortReason abortReason) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; - - [self.requestDelegates addObject:requestDelegate]; - self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient stop]; - request.delegate = requestDelegate; + request.delegate = [self createMediaUpdateRequestDelegate:command]; } - (void)setActiveTracksWithCommand:(CDVInvokedUrlCommand*)command activeTrackIds:(NSArray*)activeTrackIds textTrackStyle:(GCKMediaTextTrackStyle*)textTrackStyle { - [self checkFinishDelegates]; - CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - [self.sessionListener onMediaUpdated:[CastUtilities createMediaObject:currentSession] isAlive:NO]; - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - - } failure:^(GCKError * error) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } abortion:^(GCKRequestAbortReason abortReason) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; - - [self.requestDelegates addObject:requestDelegate]; - self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient setActiveTrackIDs:activeTrackIds]; - request.delegate = requestDelegate; + request.delegate = [self createMediaUpdateRequestDelegate:command]; request = [self.remoteMediaClient setTextTrackStyle:textTrackStyle]; } @@ -368,19 +253,18 @@ - (void)queueLoadItemsWithCommand:(CDVInvokedUrlCommand *)command queueItems:(NS options.playPosition = item.startTime; [NSUserDefaults.standardUserDefaults setBool:false forKey:@"jump"]; [NSUserDefaults.standardUserDefaults synchronize]; - self.isRequesting = YES; GCKRequest* request = [self.remoteMediaClient queueLoadItems:queueItems withOptions:options]; request.delegate = requestDelegate; } -- (void) checkFinishDelegates{ +- (void) checkFinishDelegates { NSMutableArray* tempArray = [NSMutableArray new]; - for (CastRequestDelegate* delegate in self.requestDelegates) { + for (CastRequestDelegate* delegate in requestDelegates) { if (!delegate.finished ) { [tempArray addObject:delegate]; } } - self.requestDelegates = tempArray; + requestDelegates = tempArray; } #pragma -- GCKSessionManagerListener From 2d53b5845b459d2beae9ab3be29a77f8f41b4706 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 17:35:00 -0700 Subject: [PATCH 118/166] (ios) WIP fix/unify setMediaVolumeWithCommand so that we have just one function for simplicity and so that it does not return until both the volume and mute state have been updated --- src/ios/Chromecast.m | 25 +------------------------ src/ios/ChromecastSession.h | 4 +--- src/ios/ChromecastSession.m | 36 +++++++++++++++++++++++++++++------- 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 57fa026..410dc29 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -227,30 +227,7 @@ - (void)queueJumpToItem:(CDVInvokedUrlCommand *)command { } - (void)setMediaVolume:(CDVInvokedUrlCommand*) command { - if (command.arguments[1] == [NSNull null]) { - double newLevel = 1.0; - if (command.arguments[0]) { - newLevel = [command.arguments[0] doubleValue]; - } else { - newLevel = 1.0; - } - [self.currentSession setMediaVolumeWithCommand:command newVolumeLevel:newLevel]; - } - else if (command.arguments[0] == [NSNull null]) { - BOOL muted = [command.arguments[1] boolValue]; - [self.currentSession setMediaMutedWIthCommand:command muted:muted]; - } - else { - double newLevel = 1.0; - if (command.arguments[0]) { - newLevel = [command.arguments[0] doubleValue]; - } else { - newLevel = 1.0; - } - BOOL muted = [command.arguments[1] boolValue]; - [self.currentSession setMediaMutedAndVolumeWIthCommand:command muted:muted nvewLevel:newLevel]; - } - // [self.currentSession setReceiverVolumeLevelWithCommand:command newLevel:newLevel]; + [self.currentSession setMediaMutedAndVolumeWithCommand:command]; } - (void)setReceiverVolumeLevel:(CDVInvokedUrlCommand*) command { diff --git a/src/ios/ChromecastSession.h b/src/ios/ChromecastSession.h index 91403da..cf97261 100644 --- a/src/ios/ChromecastSession.h +++ b/src/ios/ChromecastSession.h @@ -25,10 +25,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)tryRejoin; - (void)joinDevice:(GCKDevice*)device cdvCommand:(CDVInvokedUrlCommand*)command; - (void)endSession:(CDVInvokedUrlCommand*)command killSession:(BOOL)killSession; -- (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted nvewLevel:(float)newLevel; -- (void)setMediaMutedWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted; -- (void)setMediaVolumeWithCommand:(CDVInvokedUrlCommand*)withCommand newVolumeLevel:(float)newLevel; - (void)endSessionWithCallback:(void(^)(void))callback killSession:(BOOL)killSession; +- (void)setMediaMutedAndVolumeWithCommand:(CDVInvokedUrlCommand*)command; - (void)setReceiverVolumeLevelWithCommand:(CDVInvokedUrlCommand*)withCommand newLevel:(float)newLevel; - (void)setReceiverMutedWithCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted; - (void)loadMediaWithCommand:(CDVInvokedUrlCommand*)command mediaInfo:(GCKMediaInformation*)mediaInfo autoPlay:(BOOL)autoPlay currentTime : (double)currentTime; diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index 006f169..15ad105 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -119,18 +119,40 @@ - (void)endSessionWithCallback:(void(^)(void))callback killSession:(BOOL)killSes callback(); } -- (void)setMediaMutedAndVolumeWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted nvewLevel:(float)newLevel { +- (void)setMediaMutedAndVolumeWithCommand:(CDVInvokedUrlCommand*)command { -} - -- (void)setMediaMutedWIthCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)muted { -} - -- (void)setMediaVolumeWithCommand:(CDVInvokedUrlCommand*)withCommand newVolumeLevel:(float)newLevel { + // set muted to the current state + BOOL muted = mediaStatus.isMuted; + // If we have the muted argument + if (command.arguments[1] != [NSNull null]) { + // Update muted + muted = [command.arguments[1] boolValue]; + } + + __weak ChromecastSession* weakSelf = self; + void (^setMuted)(void) = ^{ + // Now set the volume + GCKRequest* request = [weakSelf.remoteMediaClient setStreamMuted:muted customData:nil]; + request.delegate = [weakSelf createMediaUpdateRequestDelegate:command]; + }; + + // Set an invalid newLevel for default + double newLevel = -1; + // Get the newLevel argument if possible + if (command.arguments[0] != [NSNull null]) { + newLevel = [command.arguments[0] doubleValue]; + } + + if (newLevel == -1) { + // We have no newLevel, so only set muted state + setMuted(); + } else { + // We have both muted and newLevel, so set volume, then muted GCKRequest* request = [self.remoteMediaClient setStreamVolume:newLevel customData:nil]; request.delegate = [self createRequestDelegate:command success:setMuted failure:nil abortion:nil]; + } } - (void)setReceiverVolumeLevelWithCommand:(CDVInvokedUrlCommand*)withCommand newLevel:(float)newLevel { From 2c5a566bfd78680ad40180bf2006b4fc008e1353 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 20:33:54 -0700 Subject: [PATCH 119/166] (ios) WIP Add createLoadMediaRequestDelegate to unify the load request delegates. Use didReceiveQueueItemIDs to detect when new media has loaded (external and internal). Use queueFetchItemsForIDs to ensure that the media items data does exist before calling the loadMediaCallback. Remove unused remoteMediaClientListener events. --- src/ios/ChromecastSession.m | 94 ++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 43 deletions(-) diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index 15ad105..9618e7e 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -11,6 +11,7 @@ @implementation ChromecastSession GCKCastSession* currentSession; CDVInvokedUrlCommand* joinSessionCommand; +void (^loadMediaCallback)(NSString*) = nil; BOOL isDisconnecting = NO; NSMutableArray* requestDelegates; @@ -48,6 +49,28 @@ - (void)joinDevice:(GCKDevice*)device cdvCommand:(CDVInvokedUrlCommand*)command [self.sessionManager startSessionWithDevice:device]; } +-(CastRequestDelegate*)createLoadMediaRequestDelegate:(CDVInvokedUrlCommand*)command { + loadMediaCallback = ^(NSString* error) { + if (error) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } else { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } + }; + return [self createRequestDelegate:command success:^{ + } failure:^(GCKError * error) { + loadMediaCallback = nil; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } abortion:^(GCKRequestAbortReason abortReason) { + loadMediaCallback = nil; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; +} + -(CastRequestDelegate*)createSessionUpdateRequestDelegate:(CDVInvokedUrlCommand*)command { return [self createRequestDelegate:command success:^{ [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:currentSession]]; @@ -166,24 +189,11 @@ - (void)setReceiverMutedWithCommand:(CDVInvokedUrlCommand*)command muted:(BOOL)m } - (void)loadMediaWithCommand:(CDVInvokedUrlCommand*)command mediaInfo:(GCKMediaInformation*)mediaInfo autoPlay:(BOOL)autoPlay currentTime : (double)currentTime { - [self checkFinishDelegates]; - CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(GCKError * error) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } abortion:^(GCKRequestAbortReason abortReason) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; - - [self.requestDelegates addObject:requestDelegate]; GCKMediaLoadOptions* options = [[GCKMediaLoadOptions alloc] init]; options.autoplay = autoPlay; options.playPosition = currentTime; GCKRequest* request = [self.remoteMediaClient loadMedia:mediaInfo withOptions:options]; - request.delegate = requestDelegate; + request.delegate = [self createLoadMediaRequestDelegate:command]; } - (void)createMessageChannelWithCommand:(CDVInvokedUrlCommand*)command namespace:(NSString*)namespace{ @@ -255,19 +265,6 @@ - (void)setActiveTracksWithCommand:(CDVInvokedUrlCommand*)command activeTrackIds } - (void)queueLoadItemsWithCommand:(CDVInvokedUrlCommand *)command queueItems:(NSArray *)queueItems startIndex:(NSInteger)startIndex repeatMode:(GCKMediaRepeatMode)repeatMode { - CastRequestDelegate* requestDelegate = [[CastRequestDelegate alloc] initWithSuccess:^{ - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[CastUtilities createMediaObject:currentSession]]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - - } failure:^(GCKError * error) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } abortion:^(GCKRequestAbortReason abortReason) { - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; - - [self.requestDelegates addObject:requestDelegate]; GCKMediaQueueItem *item = queueItems[startIndex]; GCKMediaQueueLoadOptions *options = [[GCKMediaQueueLoadOptions alloc] init]; options.repeatMode = repeatMode; @@ -276,7 +273,7 @@ - (void)queueLoadItemsWithCommand:(CDVInvokedUrlCommand *)command queueItems:(NS [NSUserDefaults.standardUserDefaults setBool:false forKey:@"jump"]; [NSUserDefaults.standardUserDefaults synchronize]; GCKRequest* request = [self.remoteMediaClient queueLoadItems:queueItems withOptions:options]; - request.delegate = requestDelegate; + request.delegate = [self createLoadMediaRequestDelegate:command]; } - (void) checkFinishDelegates { @@ -336,6 +333,7 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeCastSession: #pragma -- GCKRemoteMediaClientListener - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didStartMediaSessionWithID:(NSInteger)sessionID { + // This is not triggered by external loads, so use didReceiveQueueItemIDs instead } - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didUpdateMediaStatus:(GCKMediaStatus *)mediaStatus { @@ -368,23 +366,33 @@ - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didUpdateMediaStatus:(G } -- (void)remoteMediaClientDidUpdatePreloadStatus:(GCKRemoteMediaClient *)client { - [self remoteMediaClient:client didUpdateMediaStatus:nil]; -} - -- (void)remoteMediaClientDidUpdateQueue:(GCKRemoteMediaClient *)client{ - -} -- (void)remoteMediaClient:(GCKRemoteMediaClient *)client didInsertQueueItemsWithIDs:(NSArray *)queueItemIDs beforeItemWithID:(GCKMediaQueueItemID)beforeItemID { - -} - -- (void)remoteMediaClient:(GCKRemoteMediaClient *)client didReceiveQueueItems:(NSArray *)queueItems { - -} - - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didReceiveQueueItemIDs:(NSArray *)queueItemIDs { + // If we do not have a loadMediaCallback that means this was an external media load + if (!loadMediaCallback) { + // So set the callback to trigger the MEDIA_LOAD event + loadMediaCallback = ^(NSString* error) { + if (!error) { + [self.sessionListener onMediaLoaded:[CastUtilities createMediaObject:currentSession]]; + } + }; + } + // When internally loading a queue the media itmes are not always available at this point, so request the items + GCKRequest* request = [self.remoteMediaClient queueFetchItemsForIDs:queueItemIDs]; + request.delegate = [self createRequestDelegate:nil success:^{ + NSLog(@"%@", [NSString stringWithFormat:@"kk qFetchItemsForIds finished: %lu", (unsigned long)currentSession.remoteMediaClient.mediaStatus.queueItemCount]); + loadMediaCallback(nil); + loadMediaCallback = nil; + NSLog(@"%@", [NSString stringWithFormat:@"kk isLoadingMedia = NO success"]); + } failure:^(GCKError * error) { + NSLog(@"%@", [NSString stringWithFormat:@"Failed to retrieve queue items with error: %@", error.description]); + loadMediaCallback = nil; + NSLog(@"%@", [NSString stringWithFormat:@"kk isLoadingMedia = NO error2"]); + } abortion:^(GCKRequestAbortReason abortReason) { + NSLog(@"%@", [NSString stringWithFormat:@"Failed to retrieve queue items with error: %ld", (long)abortReason]); + loadMediaCallback = nil; + NSLog(@"%@", [NSString stringWithFormat:@"kk isLoadingMedia = NO abour2"]); + }]; } From cdc69fe908fae0bfc3ee2b421734c1a1b8474299 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 20:58:36 -0700 Subject: [PATCH 120/166] (ios) WIP Use lastMedia to keep track of when the next item in a queue begins playing and to send a manual MEDIA_UPDATE event showing the previous media as finished --- src/ios/ChromecastSession.m | 47 +++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index 9618e7e..a0b5f91 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -11,6 +11,7 @@ @implementation ChromecastSession GCKCastSession* currentSession; CDVInvokedUrlCommand* joinSessionCommand; +NSDictionary* lastMedia = nil; void (^loadMediaCallback)(NSString*) = nil; BOOL isDisconnecting = NO; NSMutableArray* requestDelegates; @@ -337,36 +338,32 @@ - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didStartMediaSessionWit } - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didUpdateMediaStatus:(GCKMediaStatus *)mediaStatus { - if (currentSession == nil) { - [self.sessionListener onMediaUpdated:@{} isAlive:false]; - return; - } - - if (![[NSUserDefaults standardUserDefaults] boolForKey:@"jump"]) { - NSDictionary* media = [CastUtilities createMediaObject:currentSession]; - [self.sessionListener onMediaUpdated:media isAlive:true]; - if (!self.isRequesting) { - if (mediaStatus.streamPosition > 0) { - - if (mediaStatus.queueItemCount > 1) { - [self.sessionListener onMediaLoaded:[CastUtilities createMediaObject:currentSession]]; - isRequesting = YES; - } - else { - [self.sessionListener onMediaLoaded:media]; - } - } - - } - } - else { - NSDictionary* media = [CastUtilities createMediaObject:currentSession]; - [self.sessionListener onMediaUpdated:media isAlive:false]; + // The following code block is dedicated to catching when the next video in a queue loads so that we can let the user know the video ended. + + // If last media is part of the same/current mediaSession + // and it is a different media itemId than the current one + // and there is no idle reason (if there is a reason, that means the status update will probably handle the situation correctly anyways) + if (lastMedia != nil + && mediaStatus.mediaSessionID == [lastMedia gck_integerForKey:@"mediaSessionId" withDefaultValue:0] + && mediaStatus.currentItemID != [lastMedia gck_integerForKey:@"currentItemId" withDefaultValue:-1] + && mediaStatus.idleReason == GCKMediaPlayerIdleReasonNone) { + + // send out out a media update indicated the previous media has finished + NSMutableDictionary* lastMediaMutable = [lastMedia mutableCopy]; + lastMediaMutable[@"playerState"] = @"IDLE"; + lastMediaMutable[@"idleReason"] = @"FINISHED"; + [self.sessionListener onMediaUpdated:lastMediaMutable]; } + // update the last media now + lastMedia = [CastUtilities createMediaObject:currentSession]; + [self.sessionListener onMediaUpdated:lastMedia]; } - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didReceiveQueueItemIDs:(NSArray *)queueItemIDs { + // New media has been loaded, wipe any lastMedia reference + lastMedia = nil; + // If we do not have a loadMediaCallback that means this was an external media load if (!loadMediaCallback) { // So set the callback to trigger the MEDIA_LOAD event From fe3d54cc1b69cf2d8e2abbf933b10fb781df43f0 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:03:41 -0700 Subject: [PATCH 121/166] (ios) WIP Switch to using isQueueJumping to determine the reason for the queue advancing to another item --- src/ios/CastUtilities.m | 5 ----- src/ios/ChromecastSession.m | 17 ++++++++++------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index 4dc3592..b5e4c7e 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -790,11 +790,6 @@ + (NSString *)getTextTrackSubtype:(GCKMediaTextTrackSubtype)textSubtype { } + (NSString *)getIdleReason:(GCKMediaPlayerIdleReason)reason { - BOOL jump = [NSUserDefaults.standardUserDefaults boolForKey:@"jump"]; - NSString *idleReason = @"FINISHED"; - if (jump) { - idleReason = @"INTERRUPTED"; - } switch (reason) { case GCKMediaPlayerIdleReasonCancelled: return @"CANCELLED"; diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index a0b5f91..f825ad4 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -13,6 +13,7 @@ @implementation ChromecastSession CDVInvokedUrlCommand* joinSessionCommand; NSDictionary* lastMedia = nil; void (^loadMediaCallback)(NSString*) = nil; +BOOL isQueueJumping = NO; BOOL isDisconnecting = NO; NSMutableArray* requestDelegates; @@ -44,9 +45,6 @@ - (void)tryRejoin { - (void)joinDevice:(GCKDevice*)device cdvCommand:(CDVInvokedUrlCommand*)command { joinSessionCommand = command; - - [NSUserDefaults.standardUserDefaults setBool:false forKey:@"jump"]; - [NSUserDefaults.standardUserDefaults synchronize]; [self.sessionManager startSessionWithDevice:device]; } @@ -232,16 +230,17 @@ - (void)mediaSeekWithCommand:(CDVInvokedUrlCommand*)command position:(NSTimeInte } - (void)queueJumpToItemWithCommand:(CDVInvokedUrlCommand *)command itemId:(NSUInteger)itemId { + isQueueJumping = YES; GCKRequest* request = [self.remoteMediaClient queueJumpToItemWithID:itemId]; request.delegate = [self createRequestDelegate:command success:nil failure:^(GCKError * error) { + isQueueJumping = NO; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } abortion:^(GCKRequestAbortReason abortReason) { + isQueueJumping = NO; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsNSInteger:abortReason]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; }]; - [NSUserDefaults.standardUserDefaults setBool:true forKey:@"jump"]; - [NSUserDefaults.standardUserDefaults synchronize]; } - (void)mediaPlayWithCommand:(CDVInvokedUrlCommand*)command { @@ -271,8 +270,6 @@ - (void)queueLoadItemsWithCommand:(CDVInvokedUrlCommand *)command queueItems:(NS options.repeatMode = repeatMode; options.startIndex = startIndex; options.playPosition = item.startTime; - [NSUserDefaults.standardUserDefaults setBool:false forKey:@"jump"]; - [NSUserDefaults.standardUserDefaults synchronize]; GCKRequest* request = [self.remoteMediaClient queueLoadItems:queueItems withOptions:options]; request.delegate = [self createLoadMediaRequestDelegate:command]; } @@ -351,7 +348,13 @@ - (void)remoteMediaClient:(GCKRemoteMediaClient *)client didUpdateMediaStatus:(G // send out out a media update indicated the previous media has finished NSMutableDictionary* lastMediaMutable = [lastMedia mutableCopy]; lastMediaMutable[@"playerState"] = @"IDLE"; + if (isQueueJumping) { + lastMediaMutable[@"idleReason"] = @"INTERRUPTED"; + // reset isQueueJumping + isQueueJumping = NO; + } else { lastMediaMutable[@"idleReason"] = @"FINISHED"; + } [self.sessionListener onMediaUpdated:lastMediaMutable]; } From 34b4cfa070c350d79e2b7ddae0ff1711b9e3c644 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:06:58 -0700 Subject: [PATCH 122/166] (ios) WIP Allow storing multiple callbacks for when the session ends. Also don't call the endSessionCallbacks until the session has actually ended (but before the SESSION_UPDATE event). --- src/ios/ChromecastSession.m | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index f825ad4..f48e4ff 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -15,12 +15,14 @@ @implementation ChromecastSession void (^loadMediaCallback)(NSString*) = nil; BOOL isQueueJumping = NO; BOOL isDisconnecting = NO; +NSMutableArray* endSessionCallbacks; NSMutableArray* requestDelegates; - (instancetype)initWithListener:(id)listener cordovaDelegate:(id)cordovaDelegate { self = [super init]; requestDelegates = [NSMutableArray new]; + endSessionCallbacks = [NSMutableArray new]; self.sessionListener = listener; self.commandDelegate = cordovaDelegate; self.castContext = [GCKCastContext sharedInstance]; @@ -132,13 +134,13 @@ - (void)endSession:(CDVInvokedUrlCommand*)command killSession:(BOOL)killSession - (void)endSessionWithCallback:(void(^)(void))callback killSession:(BOOL)killSession { NSLog(@"kk endSessionWithCallback"); + [endSessionCallbacks addObject:callback]; if (killSession) { [currentSession endWithAction:GCKSessionEndActionStopCasting]; } else { isDisconnecting = YES; [currentSession endWithAction:GCKSessionEndActionLeave]; } - callback(); } - (void)setMediaMutedAndVolumeWithCommand:(CDVInvokedUrlCommand*)command { @@ -308,19 +310,21 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didEndCastSession:(GC return; } - // Else, are we just leaving the session? (leaving results in disconnected status) + // Call all callbacks that are waiting for session end + for (void (^endSessionCallback)(void) in endSessionCallbacks) { + endSessionCallback(); + } + // And remove the callbacks + endSessionCallbacks = [NSMutableArray new]; + + // Are we just leaving the session? (leaving results in disconnected status) if (isDisconnecting) { - // Clear is isDisconnecting + // Clear isDisconnecting isDisconnecting = NO; [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session status:@"disconnected"]]; } else { [self.sessionListener onSessionUpdated:[CastUtilities createSessionObject:session]]; } - - // Do we have any additional endSessionCallbacks? - if (endSessionCallback) { - endSessionCallback(YES); - } } - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeCastSession:(GCKCastSession *)session { From bd08dcdeb785e47072813519eced597e7b835c7c Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:11:06 -0700 Subject: [PATCH 123/166] (ios) WIP Forgot to commit these changes with their relevant commit. (Sorry, did a huge amout of fixes and then tried to make commits for each section of change, to have a slightly more informative git history. But it is difficult...) --- src/ios/CastUtilities.m | 2 +- src/ios/ChromecastSession.m | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index b5e4c7e..fe0d04f 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -552,7 +552,7 @@ + (NSDictionary *)createQueueItem:(GCKMediaQueueItem *)queueItem { + (NSDictionary*)createQueueData:(GCKMediaStatus*)mediaStatus { GCKMediaQueueData* queueData = mediaStatus.queueData; - if (queueData == [NSNull null]) { + if (queueData == nil) { return nil; } NSMutableDictionary* returnDict = [[NSMutableDictionary alloc] init]; diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index f48e4ff..a571004 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -144,8 +144,7 @@ - (void)endSessionWithCallback:(void(^)(void))callback killSession:(BOOL)killSes } - (void)setMediaMutedAndVolumeWithCommand:(CDVInvokedUrlCommand*)command { - - + GCKMediaStatus* mediaStatus = currentSession.remoteMediaClient.mediaStatus; // set muted to the current state BOOL muted = mediaStatus.isMuted; // If we have the muted argument @@ -179,7 +178,7 @@ - (void)setMediaMutedAndVolumeWithCommand:(CDVInvokedUrlCommand*)command { } } -- (void)setReceiverVolumeLevelWithCommand:(CDVInvokedUrlCommand*)withCommand newLevel:(float)newLevel { +- (void)setReceiverVolumeLevelWithCommand:(CDVInvokedUrlCommand*)command newLevel:(float)newLevel { GCKRequest* request = [currentSession setDeviceVolume:newLevel]; request.delegate = [self createSessionUpdateRequestDelegate:command]; } From 68de02e1c3830b4b5871240b43ea63e29647eb3e Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:12:07 -0700 Subject: [PATCH 124/166] (ios) WIP fix crash when textTrackStyle.backgroundColor does not exist --- src/ios/CastUtilities.m | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index fe0d04f..be12f1f 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -683,20 +683,24 @@ + (NSDictionary *)getTextTrackStyle:(GCKMediaTextTrackStyle *)textTrackStyle { if (textTrackStyle == nil) { return @{}; } - return @{ - @"backgroundColor": textTrackStyle.backgroundColor.CSSString, - @"customData": textTrackStyle.customData == nil? @{} : textTrackStyle.customData, - @"edgeColor": textTrackStyle.edgeColor.CSSString == nil? @"" : textTrackStyle.edgeColor.CSSString, - @"edgeType": [CastUtilities getEdgeType:textTrackStyle.edgeType], - @"fontFamily": textTrackStyle.fontFamily, - @"fontGenericFamily": [CastUtilities getFontGenericFamily:textTrackStyle.fontGenericFamily], - @"fontScale": @(textTrackStyle.fontScale), - @"fontStyle": [CastUtilities getFontStyle:textTrackStyle.fontStyle], - @"foregroundColor": textTrackStyle.foregroundColor.CSSString, - @"windowColor": textTrackStyle.windowColor.CSSString, - @"windowRoundedCornerRadius": @(textTrackStyle.windowRoundedCornerRadius), - @"windowType": [CastUtilities getWindowType:textTrackStyle.windowType] - }; + + NSMutableDictionary* textTrackStyleOut = [[NSMutableDictionary alloc] init]; + if (textTrackStyle.backgroundColor) { + textTrackStyleOut[@"backgroundColor"] = textTrackStyle.backgroundColor.CSSString; + } + textTrackStyleOut[@"customData"] = textTrackStyle.customData == nil? @{} : textTrackStyle.customData; + textTrackStyleOut[@"edgeColor"] = textTrackStyle.edgeColor.CSSString == nil? @"" : textTrackStyle.edgeColor.CSSString; + textTrackStyleOut[@"edgeType"] = [CastUtilities getEdgeType:textTrackStyle.edgeType]; + textTrackStyleOut[@"fontFamily"] = textTrackStyle.fontFamily; + textTrackStyleOut[@"fontGenericFamily"] = [CastUtilities getFontGenericFamily:textTrackStyle.fontGenericFamily]; + textTrackStyleOut[@"fontScale"] = @(textTrackStyle.fontScale); + textTrackStyleOut[@"fontStyle"] = [CastUtilities getFontStyle:textTrackStyle.fontStyle]; + textTrackStyleOut[@"foregroundColor"] = textTrackStyle.foregroundColor.CSSString; + textTrackStyleOut[@"windowColor"] = textTrackStyle.windowColor.CSSString; + textTrackStyleOut[@"windowRoundedCornerRadius"] = @(textTrackStyle.windowRoundedCornerRadius); + textTrackStyleOut[@"windowType"] = [CastUtilities getWindowType:textTrackStyle.windowType]; + + return textTrackStyleOut; } + (NSString *)getEdgeType:(GCKMediaTextTrackStyleEdgeType)edgeType { From aec539593cdc82a584eb1dee4942f6233f48793a Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:14:19 -0700 Subject: [PATCH 125/166] (ios) WIP Fix contentID output. For queues, only contentURL is available, but for loadMedia media, only contentID is available --- src/ios/CastUtilities.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index be12f1f..1b21038 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -569,7 +569,7 @@ + (NSDictionary *)createMediaInfoObject:(GCKMediaInformation *)mediaInfo { } return @{ - @"contentId": mediaInfo.contentURL == nil ? @"" : mediaInfo.contentURL.absoluteString, + @"contentId": mediaInfo.contentID? mediaInfo.contentID : mediaInfo.contentURL.absoluteString, @"contentType": mediaInfo.contentType, @"customData": mediaInfo.customData == nil ? @{} : mediaInfo.customData, @"duration": @(mediaInfo.streamDuration), From a89f8faeb9ac0649f2146b5575e5567d0f2cdb28 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:16:25 -0700 Subject: [PATCH 126/166] (ios) WIP bug fix, selectRoute should only return the "Leave/Stop current session before selecting" error message when a current connected session exists --- src/ios/Chromecast.m | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ios/Chromecast.m b/src/ios/Chromecast.m index 410dc29..5512939 100644 --- a/src/ios/Chromecast.m +++ b/src/ios/Chromecast.m @@ -311,9 +311,8 @@ - (void)mediaEditTracksInfo:(CDVInvokedUrlCommand*)command { - (void)selectRoute:(CDVInvokedUrlCommand*)command { GCKCastSession* currentSession = [GCKCastContext sharedInstance].sessionManager.currentCastSession; - if (currentSession != nil - || [currentSession connectionState] == GCKConnectionStateConnected - || [currentSession connectionState] == GCKConnectionStateConnecting) { + if (currentSession != nil && + (currentSession.connectionState == GCKConnectionStateConnected || currentSession.connectionState == GCKConnectionStateConnecting)) { [self sendError:@"session_error" message:@"Leave or stop current session before attempting to join new session." command:command]; return; } From 4fb7b21f49748af656e8d3a8d29e18bf86648ecf Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:18:11 -0700 Subject: [PATCH 127/166] (ios) WIP sometimes initialize -> tryRejoin can happen before didEndCastSession can update/erase the currentSession. This just provides some additional protection against that. --- src/ios/ChromecastSession.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index a571004..320441b 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -40,6 +40,8 @@ - (void)setSession:(GCKCastSession*)session { } - (void)tryRejoin { + // Make sure we are looking at the actual current session, sometimes it doesn't get removed + [self setSession:self.sessionManager.currentCastSession]; if (currentSession != nil) { [self.sessionListener onSessionRejoin:[CastUtilities createSessionObject:currentSession]]; } From 8ec8c98e6ed190fd736bd4aa5d7db2ba7a0d6cdc Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:21:04 -0700 Subject: [PATCH 128/166] (ios) WIP For some reason didResumeCastSession is called randomly even once we already have the session. (Maybe it temporarily/very quickly loses and regains connection). Whatever the reason, if we already have the session, we shouldn't trigger the SESSION_LISTENER event. (Also no point in "setSession" either, since it is the same session. --- src/ios/ChromecastSession.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index 320441b..b3b1d6d 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -329,6 +329,10 @@ - (void)sessionManager:(GCKSessionManager *)sessionManager didEndCastSession:(GC } - (void)sessionManager:(GCKSessionManager *)sessionManager didResumeCastSession:(GCKCastSession *)session { + if (currentSession && currentSession.sessionID == session.sessionID) { + // ios randomly resumes current session, don't trigger SESSION_LISTENER in this case + return; + } [self setSession:session]; [self.sessionListener onSessionRejoin:[CastUtilities createSessionObject:session]]; } From 46b795d6ba9436e12d7b3fd46fe200323d80a589 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:22:45 -0700 Subject: [PATCH 129/166] (ios) WIP detect join session fail and return an error --- src/ios/ChromecastSession.m | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ios/ChromecastSession.m b/src/ios/ChromecastSession.m index b3b1d6d..e0d791a 100644 --- a/src/ios/ChromecastSession.m +++ b/src/ios/ChromecastSession.m @@ -49,7 +49,11 @@ - (void)tryRejoin { - (void)joinDevice:(GCKDevice*)device cdvCommand:(CDVInvokedUrlCommand*)command { joinSessionCommand = command; - [self.sessionManager startSessionWithDevice:device]; + BOOL startedSuccessfully = [self.sessionManager startSessionWithDevice:device]; + if (!startedSuccessfully) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Failed to join the selected route"]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } } -(CastRequestDelegate*)createLoadMediaRequestDelegate:(CDVInvokedUrlCommand*)command { From 620b3abceeaca6318b748ae5b86e802579729c9f Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:37:31 -0700 Subject: [PATCH 130/166] (test) WIP fix test "ueue should start the next item automatically when previous one finishes" so that it does not re-use var i (causing issue if we need to hit the media listener more than once). And make it so that the current play time of the item must be close to it's start time once it is reported as playing. --- tests/www/js/tests_auto.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index 91364da..3c34306 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -1103,17 +1103,16 @@ var request = new chrome.cast.media.SeekRequest(); request.currentTime = media.media.duration - 1; - var i = utils.getCurrentItemIndex(media); // Listen for current media end + var prevId = media.currentItemId; media.addUpdateListener(function listener (isAlive) { if (media.playerState === chrome.cast.media.PlayerState.IDLE) { assert.equal(media.idleReason, chrome.cast.media.IdleReason.FINISHED); assert.isTrue(isAlive); called(stopped); } - if (media.currentItemId !== media.items[i].itemId) { - i = utils.getCurrentItemIndex(media); - media.removeUpdateListener(listener); + if (media.currentItemId !== prevId) { + var i = utils.getCurrentItemIndex(media); utils.testMediaProperties(media); assert.equal(media.repeatMode, chrome.cast.media.RepeatMode.ALL); assert.equal(media.media.contentId, videoUrl); @@ -1131,8 +1130,9 @@ assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.TV_SHOW); assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.TV_SHOW); called(newMedia); - if (media.getEstimatedTime() > startTime - 5 - && media.getEstimatedTime() < startTime + 5) { + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + assert.closeTo(media.getEstimatedTime(), startTime, 5); called(update); } } From ddfcf3c2f2eca8d491b9b56862f108380500f865 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:39:36 -0700 Subject: [PATCH 131/166] (test) WIP fix copypasta --- tests/www/js/tests_manual_primary_1.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/www/js/tests_manual_primary_1.js b/tests/www/js/tests_manual_primary_1.js index 2d2c437..c97aae9 100644 --- a/tests/www/js/tests_manual_primary_1.js +++ b/tests/www/js/tests_manual_primary_1.js @@ -298,7 +298,6 @@ var apiConfig = new chrome.cast.ApiConfig( new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function (sess) { - failed = true; session = sess; assert.fail('Should not receive session on initialize. We should only call this initialize when there is no existing session.'); }, function receiverListener (availability) { From 14e1fea40aff87dbd01386df468b4e09592bada0 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:42:14 -0700 Subject: [PATCH 132/166] (test) WIP allow requestSession's stop casting option to trigger the update listener and the success/cancel event in any order. (Potentially it can happen in either order for chrome desktop since the success/cancel event happens when the casting dialog hides. Theoretically it is possible to have the dialog hide before the session ends.) --- tests/www/js/tests_manual_primary_1.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/www/js/tests_manual_primary_1.js b/tests/www/js/tests_manual_primary_1.js index c97aae9..6892d07 100644 --- a/tests/www/js/tests_manual_primary_1.js +++ b/tests/www/js/tests_manual_primary_1.js @@ -346,8 +346,8 @@ }); }); it('(stop casting) clicking "Stop Casting" should stop the session', function (done) { - var called = utils.callOrder([ - { id: stopped, repeats: false }, + var called = utils.waitForAllCalls([ + { id: stopped, repeats: true }, { id: success, repeats: false } ], done); session.addUpdateListener(function listener (isAlive) { From 1341f9d66dc585d94cb43d743d2c01cd92e7bdae Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:43:27 -0700 Subject: [PATCH 133/166] (test) WIP auto tests should have a before which ensures that initialization has occurred so that we can use ".only" --- tests/www/js/tests_auto.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index 3c34306..f5b713b 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -146,6 +146,33 @@ }); }); + describe('post initialize functions', function () { + before('Must be initialized', function (done) { + var unavailable = 'unavailable'; + var available = 'available'; + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: unavailable, repeats: true }, + { id: available, repeats: true } + ], function () { + finished = true; + done(); + }); + var finished = false; // Need this so we stop testing after being finished + var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function sessionListener (session) { + assert.fail('should not receive a session (make sure there is no active cast session when starting the tests)'); + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }); + chrome.cast.initialize(apiConfig, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + describe('chrome.cast.cordova functions and session.leave', function () { var _route; it('should have definitions', function () { @@ -1270,6 +1297,8 @@ }); }); + }); + }); }()); From c30f6fbce66fac988811a000155154e7fe874c06 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:47:30 -0700 Subject: [PATCH 134/166] (test) WIP wrap media functions in a describe to improve ".only" use for testing just media functions --- tests/www/js/tests_auto.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index f5b713b..dbdc5f8 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -557,6 +557,7 @@ afterEach(function () { session.removeMediaListener(mediaListener); }); + describe('Media (non-queues)', function () { it('session.loadMedia should be able to load a remote video and handle GenericMediaMetadata', function (done) { var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); mediaInfo.metadata = new chrome.cast.media.GenericMediaMetadata(); @@ -1015,6 +1016,7 @@ assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); + }); describe('Queues', function () { var videoItem; var audioItem; From 123e229e94646dc039654c9f08edf812568903c0 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:52:01 -0700 Subject: [PATCH 135/166] [no change] formatting. Change indent for tests required from last 2 commits. --- tests/www/js/tests_auto.js | 2022 ++++++++++++++++++------------------ 1 file changed, 1011 insertions(+), 1011 deletions(-) diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index dbdc5f8..762a056 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -173,1055 +173,927 @@ }); }); - describe('chrome.cast.cordova functions and session.leave', function () { - var _route; - it('should have definitions', function () { - assert.exists(chrome.cast.cordova); - assert.exists(chrome.cast.cordova.startRouteScan); - assert.exists(chrome.cast.cordova.stopRouteScan); - assert.exists(chrome.cast.cordova.selectRoute); - assert.exists(chrome.cast.cordova.Route); - }); - it('startRouteScan 2nd call should result in error for first', function (done) { - var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); - var secondStarted = false; - chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { - if (secondStarted) { - assert.fail('Should not be receiving route updates here anymore.'); - } + describe('chrome.cast.cordova functions and session.leave', function () { + var _route; + it('should have definitions', function () { + assert.exists(chrome.cast.cordova); + assert.exists(chrome.cast.cordova.startRouteScan); + assert.exists(chrome.cast.cordova.stopRouteScan); + assert.exists(chrome.cast.cordova.selectRoute); + assert.exists(chrome.cast.cordova.Route); + }); + it('startRouteScan 2nd call should result in error for first', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + var secondStarted = false; chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { - // We should get updates from this scan - called(update); + if (secondStarted) { + assert.fail('Should not be receiving route updates here anymore.'); + } + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + // We should get updates from this scan + called(update); + }, function (err) { + // The only acceptable way for this scan to stop + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped.'); + }); }, function (err) { - // The only acceptable way for this scan to stop + secondStarted = true; assert.isObject(err); assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); - assert.equal(err.description, 'Scan stopped.'); + assert.equal(err.description, 'Started a new route scan before stopping previous one.'); + called(success); }); - }, function (err) { - secondStarted = true; - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); - assert.equal(err.description, 'Started a new route scan before stopping previous one.'); - called(success); }); - }); - it('stopRouteScan 2nd call should succeed', function (done) { - chrome.cast.cordova.stopRouteScan(function () { + it('stopRouteScan 2nd call should succeed', function (done) { chrome.cast.cordova.stopRouteScan(function () { - done(); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - it('startRouteScan should find valid routes', function (done) { - _route = undefined; - chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { - if (_route) { - return; // we have already found a valid route - } - var route; - for (var i = 0; i < routes.length; i++) { - route = routes[i]; - assert.instanceOf(route, chrome.cast.cordova.Route); - assert.isString(route.id); - assert.isString(route.name); - assert.isBoolean(route.isNearbyDevice); - assert.isBoolean(route.isCastGroup); - if (!route.isNearbyDevice) { - _route = route; - } - } - if (_route) { - done(); - } - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); - }); - }); - it('stopRouteScan should succeed and trigger cancel error in startRouteScan', function (done) { - var scanState = 'running'; - var called = utils.callOrder([ - { id: stopped, repeats: false }, - { id: success, repeats: false } - ], function () { - done(); - }); - chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { - if (scanState === 'stopped') { - assert.fail('Should not have gotten route update after scan was stopped'); - } - if (scanState === 'running') { - scanState = 'stopping'; chrome.cast.cordova.stopRouteScan(function () { - scanState = 'stopped'; - called(success); + done(); }, function (err) { assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); - } - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); - assert.equal(err.description, 'Scan stopped.'); - called(stopped); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - }); - it('selectRoute should receive a TIMEOUT error if route does not exist', function (done) { - this.timeout(20000); - this.slow(17000); - var routeId = 'non-existant-route-id'; - chrome.cast.cordova.selectRoute(routeId, function (session) { - assert.fail('should not have hit the success callback'); - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.TIMEOUT); - assert.match(err.description, new RegExp('^Failed to join route \\(' + routeId + '\\) after [0-9]+s and [0-9]+ tries\\.$')); - done(); + it('startRouteScan should find valid routes', function (done) { + _route = undefined; + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (_route) { + return; // we have already found a valid route + } + var route; + for (var i = 0; i < routes.length; i++) { + route = routes[i]; + assert.instanceOf(route, chrome.cast.cordova.Route); + assert.isString(route.id); + assert.isString(route.name); + assert.isBoolean(route.isNearbyDevice); + assert.isBoolean(route.isCastGroup); + if (!route.isNearbyDevice) { + _route = route; + } + } + if (_route) { + done(); + } + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + }); }); - }); - it('selectRoute should return a valid session after selecting a route', function (done) { - chrome.cast.cordova.selectRoute(_route.id, function (sess) { - session = sess; - utils.testSessionProperties(sess); - done(); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + it('stopRouteScan should succeed and trigger cancel error in startRouteScan', function (done) { + var scanState = 'running'; + var called = utils.callOrder([ + { id: stopped, repeats: false }, + { id: success, repeats: false } + ], function () { + done(); + }); + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (scanState === 'stopped') { + assert.fail('Should not have gotten route update after scan was stopped'); + } + if (scanState === 'running') { + scanState = 'stopping'; + chrome.cast.cordova.stopRouteScan(function () { + scanState = 'stopped'; + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + } + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped.'); + called(stopped); + }); }); - }); - it('selectRoute should return error if already joined', function (done) { - chrome.cast.cordova.selectRoute('', function (session) { - assert.fail('Should not be allowed to selectRoute when already in session'); - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); - assert.equal(err.description, 'Leave or stop current session before attempting to join new session.'); - done(); + it('selectRoute should receive a TIMEOUT error if route does not exist', function (done) { + this.timeout(20000); + this.slow(17000); + var routeId = 'non-existant-route-id'; + chrome.cast.cordova.selectRoute(routeId, function (session) { + assert.fail('should not have hit the success callback'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.TIMEOUT); + assert.match(err.description, new RegExp('^Failed to join route \\(' + routeId + '\\) after [0-9]+s and [0-9]+ tries\\.$')); + done(); + }); }); - }); - it('session.leave should leave the session', function (done) { - // Set up the expected calls - var called = utils.callOrder([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); - session.addUpdateListener(function listener (isAlive) { - assert.isTrue(isAlive); - if (session.status === chrome.cast.SessionStatus.DISCONNECTED) { - session.removeUpdateListener(listener); - called(update); - } + it('selectRoute should return a valid session after selecting a route', function (done) { + chrome.cast.cordova.selectRoute(_route.id, function (sess) { + session = sess; + utils.testSessionProperties(sess); + done(); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - session.leave(function () { - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + it('selectRoute should return error if already joined', function (done) { + chrome.cast.cordova.selectRoute('', function (session) { + assert.fail('Should not be allowed to selectRoute when already in session'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'Leave or stop current session before attempting to join new session.'); + done(); + }); }); - }); - it('initialize should not receive a session after session.leave', function (done) { - var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function sessionListener (session) { - assert.fail('should not receive a session (we did sessionLeave so we shouldnt be able to auto rejoin rejoin)'); + it('session.leave should leave the session', function (done) { + // Set up the expected calls + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + session.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + if (session.status === chrome.cast.SessionStatus.DISCONNECTED) { + session.removeUpdateListener(listener); + called(update); + } + }); + session.leave(function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - chrome.cast.initialize(apiConfig, function () { - done(); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + it('initialize should not receive a session after session.leave', function (done) { + var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function sessionListener (session) { + assert.fail('should not receive a session (we did sessionLeave so we shouldnt be able to auto rejoin rejoin)'); + }); + chrome.cast.initialize(apiConfig, function () { + done(); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - }); - it('session.leave should give an error if session already left', function (done) { - session.leave(function () { - assert.fail('session.leave - Should not call success'); - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.INVALID_PARAMETER); - assert.equal(err.description, 'No active session'); - done(); + it('session.leave should give an error if session already left', function (done) { + session.leave(function () { + assert.fail('session.leave - Should not call success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.INVALID_PARAMETER); + assert.equal(err.description, 'No active session'); + done(); + }); }); - }); - after(function (done) { - // Make sure we have left the session - session.leave(function () { - done(); - }, function () { - done(); + after(function (done) { + // Make sure we have left the session + session.leave(function () { + done(); + }, function () { + done(); + }); }); }); - }); - describe('chrome.cast session functions', function () { - before(function (done) { - // need to have a valid session to run these tests - session = null; - var scanState = 'running'; - var foundRoute = null; - chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { - if (scanState === 'stopped') { - assert.fail('Should not have gotten route update after scan was stopped'); - } - var route; - for (var i = 0; i < routes.length; i++) { - route = routes[i]; - assert.instanceOf(route, chrome.cast.cordova.Route); - assert.isString(route.id); - assert.isString(route.name); - assert.isBoolean(route.isNearbyDevice); - assert.isBoolean(route.isCastGroup); - if (!route.isNearbyDevice && !route.isCastGroup) { - foundRoute = route; + describe('chrome.cast session functions', function () { + before(function (done) { + // need to have a valid session to run these tests + session = null; + var scanState = 'running'; + var foundRoute = null; + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (scanState === 'stopped') { + assert.fail('Should not have gotten route update after scan was stopped'); } - } - if (foundRoute && scanState === 'running') { - scanState = 'stopping'; - chrome.cast.cordova.stopRouteScan(function () { - scanState = 'stopped'; - chrome.cast.cordova.selectRoute(foundRoute.id, function (sess) { - utils.testSessionProperties(sess); - session = sess; - done(); + var route; + for (var i = 0; i < routes.length; i++) { + route = routes[i]; + assert.instanceOf(route, chrome.cast.cordova.Route); + assert.isString(route.id); + assert.isString(route.name); + assert.isBoolean(route.isNearbyDevice); + assert.isBoolean(route.isCastGroup); + if (!route.isNearbyDevice && !route.isCastGroup) { + foundRoute = route; + } + } + if (foundRoute && scanState === 'running') { + scanState = 'stopping'; + chrome.cast.cordova.stopRouteScan(function () { + scanState = 'stopped'; + chrome.cast.cordova.selectRoute(foundRoute.id, function (sess) { + utils.testSessionProperties(sess); + session = sess; + done(); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }, function (err) { assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - } - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); - assert.equal(err.description, 'Scan stopped.'); - }); - }); - it('session.setReceiverMuted should mute the volume', function (done) { - var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], function () { - session.removeUpdateListener(listener); - done(); + } + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped.'); + }); }); + it('session.setReceiverMuted should mute the volume', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], function () { + session.removeUpdateListener(listener); + done(); + }); - // Do the opposite mute state as current - var muted = !session.receiver.volume.muted; - - function listener (isAlive) { - - assert.isTrue(isAlive); - assert.isObject(session.receiver); - assert.isObject(session.receiver.volume); - if (session.receiver.volume.muted === muted) { - called(update); - } - } - session.addUpdateListener(listener); - session.setReceiverMuted(muted, function () { - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - it('session.setReceiverVolumeLevel should set the volume level', function (done) { - var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], function () { - session.removeUpdateListener(listener); - done(); - }); + // Do the opposite mute state as current + var muted = !session.receiver.volume.muted; - // Make sure the request volume is significantly different - var requestedVolume = Math.abs(session.receiver.volume.level - 0.5); + function listener (isAlive) { - function listener (isAlive) { - assert.isTrue(isAlive); - assert.isObject(session.receiver); - assert.isObject(session.receiver.volume); - // Check that the receiver volume is approximate match - if (session.receiver.volume.level > requestedVolume - 0.1 && - session.receiver.volume.level < requestedVolume + 0.1) { - called(update); + assert.isTrue(isAlive); + assert.isObject(session.receiver); + assert.isObject(session.receiver.volume); + if (session.receiver.volume.muted === muted) { + called(update); + } } - } - session.addUpdateListener(listener); - session.setReceiverVolumeLevel(requestedVolume, function () { - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + session.addUpdateListener(listener); + session.setReceiverMuted(muted, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - }); - it('session.stop should stop the session', function (done) { - // Set up the expected calls - var called = utils.callOrder([ + it('session.setReceiverVolumeLevel should set the volume level', function (done) { + var called = utils.waitForAllCalls([ { id: success, repeats: false }, { id: update, repeats: true } - ], done); - session.addUpdateListener(function listener (isAlive) { - if (session.status === chrome.cast.SessionStatus.STOPPED) { - assert.isFalse(isAlive); + ], function () { session.removeUpdateListener(listener); - called(update); + done(); + }); + + // Make sure the request volume is significantly different + var requestedVolume = Math.abs(session.receiver.volume.level - 0.5); + + function listener (isAlive) { + assert.isTrue(isAlive); + assert.isObject(session.receiver); + assert.isObject(session.receiver.volume); + // Check that the receiver volume is approximate match + if (session.receiver.volume.level > requestedVolume - 0.1 && + session.receiver.volume.level < requestedVolume + 0.1) { + called(update); + } } + session.addUpdateListener(listener); + session.setReceiverVolumeLevel(requestedVolume, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - session.stop(function () { - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - it('initialize should not receive a session after session.stop', function (done) { - var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function sessionListener (session) { - assert.fail('should not receive a session (we did sessionStop so we shouldnt be able to auto rejoin rejoin)'); + it('session.stop should stop the session', function (done) { + // Set up the expected calls + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + session.addUpdateListener(function listener (isAlive) { + if (session.status === chrome.cast.SessionStatus.STOPPED) { + assert.isFalse(isAlive); + session.removeUpdateListener(listener); + called(update); + } + }); + session.stop(function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - chrome.cast.initialize(apiConfig, function () { - done(); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + it('initialize should not receive a session after session.stop', function (done) { + var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), function sessionListener (session) { + assert.fail('should not receive a session (we did sessionStop so we shouldnt be able to auto rejoin rejoin)'); + }); + chrome.cast.initialize(apiConfig, function () { + done(); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - }); - it('session.stop should give an error if session already stopped', function (done) { - session.stop(function () { - assert.fail('session.stop - Should not call success'); - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.INVALID_PARAMETER); - assert.equal(err.description, 'No active session'); - done(); + it('session.stop should give an error if session already stopped', function (done) { + session.stop(function () { + assert.fail('session.stop - Should not call success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.INVALID_PARAMETER); + assert.equal(err.description, 'No active session'); + done(); + }); }); - }); - after(function (done) { - // Ensure the session is stopped - session.stop(function () { - done(); - }, function () { - done(); + after(function (done) { + // Ensure the session is stopped + session.stop(function () { + done(); + }, function () { + done(); + }); }); }); - }); - describe('chrome.cast media functions', function () { - var media; - var mediaListener = function (media) { - assert.fail('session.addMediaListener should only be called when an external sender loads media'); - }; - before(function (done) { - // need to have a valid session to run these tests - session = null; - var scanState = 'running'; - var foundRoute = null; - chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { - if (scanState === 'stopped') { - assert.fail('Should not have gotten route update after scan was stopped'); - } - var route; - for (var i = 0; i < routes.length; i++) { - route = routes[i]; - assert.instanceOf(route, chrome.cast.cordova.Route); - assert.isString(route.id); - assert.isString(route.name); - assert.isBoolean(route.isNearbyDevice); - assert.isBoolean(route.isCastGroup); - if (!route.isNearbyDevice && !route.isCastGroup) { - foundRoute = route; + describe('chrome.cast media functions', function () { + var media; + var mediaListener = function (media) { + assert.fail('session.addMediaListener should only be called when an external sender loads media'); + }; + before(function (done) { + // need to have a valid session to run these tests + session = null; + var scanState = 'running'; + var foundRoute = null; + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (scanState === 'stopped') { + assert.fail('Should not have gotten route update after scan was stopped'); } - } - if (foundRoute && scanState === 'running') { - scanState = 'stopping'; - chrome.cast.cordova.stopRouteScan(function () { - scanState = 'stopped'; - chrome.cast.cordova.selectRoute(foundRoute.id, function (sess) { - utils.testSessionProperties(sess); - session = sess; - done(); + var route; + for (var i = 0; i < routes.length; i++) { + route = routes[i]; + assert.instanceOf(route, chrome.cast.cordova.Route); + assert.isString(route.id); + assert.isString(route.name); + assert.isBoolean(route.isNearbyDevice); + assert.isBoolean(route.isCastGroup); + if (!route.isNearbyDevice && !route.isCastGroup) { + foundRoute = route; + } + } + if (foundRoute && scanState === 'running') { + scanState = 'stopping'; + chrome.cast.cordova.stopRouteScan(function () { + scanState = 'stopped'; + chrome.cast.cordova.selectRoute(foundRoute.id, function (sess) { + utils.testSessionProperties(sess); + session = sess; + done(); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }, function (err) { assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); + } + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped.'); + }); + }); + beforeEach(function () { + session.addMediaListener(mediaListener); + }); + afterEach(function () { + session.removeMediaListener(mediaListener); + }); + describe('Media (non-queues)', function () { + it('session.loadMedia should be able to load a remote video and handle GenericMediaMetadata', function (done) { + var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + mediaInfo.metadata = new chrome.cast.media.GenericMediaMetadata(); + mediaInfo.metadata.title = 'DaTitle'; + mediaInfo.metadata.subtitle = 'DaSubtitle'; + mediaInfo.metadata.releaseDate = new Date().valueOf(); + mediaInfo.metadata.someTrueBoolean = true; + mediaInfo.metadata.someFalseBoolean = false; + mediaInfo.metadata.someSmallNumber = 15; + mediaInfo.metadata.someLargeNumber = 1234567890123456; + mediaInfo.metadata.someSmallDecimal = 15.15; + mediaInfo.metadata.someLargeDecimal = 1234567.123456789; + mediaInfo.metadata.someString = 'SomeString'; + mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; + session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { + media = m; + utils.testMediaProperties(media); + assert.isUndefined(media.queueData); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); + assert.equal(media.media.metadata.releaseDate, mediaInfo.metadata.releaseDate); + // TODO figure out how to maintain the data types for custom params on the native side + // so that we don't have to do turn each actual and expected into a string + assert.equal(media.media.metadata.someTrueBoolean + '', mediaInfo.metadata.someTrueBoolean + ''); + assert.equal(media.media.metadata.someFalseBoolean + '', mediaInfo.metadata.someFalseBoolean + ''); + assert.equal(media.media.metadata.someSmallNumber + '', mediaInfo.metadata.someSmallNumber + ''); + assert.equal(media.media.metadata.someLargeNumber + '', mediaInfo.metadata.someLargeNumber + ''); + assert.equal(media.media.metadata.someSmallDecimal + '', mediaInfo.metadata.someSmallDecimal + ''); + assert.equal(media.media.metadata.someLargeDecimal + '', mediaInfo.metadata.someLargeDecimal + ''); + assert.equal(media.media.metadata.someString, mediaInfo.metadata.someString); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.GENERIC); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.GENERIC); + assert.notExists(media.idleReason); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + utils.testMediaProperties(media); + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + done(); + } + }); }, function (err) { assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); - } - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); - assert.equal(err.description, 'Scan stopped.'); - }); - }); - beforeEach(function () { - session.addMediaListener(mediaListener); - }); - afterEach(function () { - session.removeMediaListener(mediaListener); - }); - describe('Media (non-queues)', function () { - it('session.loadMedia should be able to load a remote video and handle GenericMediaMetadata', function (done) { - var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); - mediaInfo.metadata = new chrome.cast.media.GenericMediaMetadata(); - mediaInfo.metadata.title = 'DaTitle'; - mediaInfo.metadata.subtitle = 'DaSubtitle'; - mediaInfo.metadata.releaseDate = new Date().valueOf(); - mediaInfo.metadata.someTrueBoolean = true; - mediaInfo.metadata.someFalseBoolean = false; - mediaInfo.metadata.someSmallNumber = 15; - mediaInfo.metadata.someLargeNumber = 1234567890123456; - mediaInfo.metadata.someSmallDecimal = 15.15; - mediaInfo.metadata.someLargeDecimal = 1234567.123456789; - mediaInfo.metadata.someString = 'SomeString'; - mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; - session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { - media = m; - utils.testMediaProperties(media); - assert.isUndefined(media.queueData); - assert.equal(media.media.metadata.title, mediaInfo.metadata.title); - assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); - assert.equal(media.media.metadata.releaseDate, mediaInfo.metadata.releaseDate); - // TODO figure out how to maintain the data types for custom params on the native side - // so that we don't have to do turn each actual and expected into a string - assert.equal(media.media.metadata.someTrueBoolean + '', mediaInfo.metadata.someTrueBoolean + ''); - assert.equal(media.media.metadata.someFalseBoolean + '', mediaInfo.metadata.someFalseBoolean + ''); - assert.equal(media.media.metadata.someSmallNumber + '', mediaInfo.metadata.someSmallNumber + ''); - assert.equal(media.media.metadata.someLargeNumber + '', mediaInfo.metadata.someLargeNumber + ''); - assert.equal(media.media.metadata.someSmallDecimal + '', mediaInfo.metadata.someSmallDecimal + ''); - assert.equal(media.media.metadata.someLargeDecimal + '', mediaInfo.metadata.someLargeDecimal + ''); - assert.equal(media.media.metadata.someString, mediaInfo.metadata.someString); - assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); - assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.GENERIC); - assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.GENERIC); - assert.notExists(media.idleReason); - media.addUpdateListener(function listener (isAlive) { - assert.isTrue(isAlive); - utils.testMediaProperties(media); - assert.oneOf(media.playerState, [ - chrome.cast.media.PlayerState.PLAYING, - chrome.cast.media.PlayerState.BUFFERING]); - if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { - media.removeUpdateListener(listener); - done(); - } }); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - it('media.setVolume should set the volume', function (done) { - var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); - - // Ensure we select a different volume - var vol = media.volume.level; - if (vol) { - vol = Math.abs(vol - 0.5); - } else { - vol = Math.random(); - } - var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(vol)); - - media.addUpdateListener(function listener (isAlive) { - assert.isTrue(isAlive); - assert.instanceOf(media.volume, chrome.cast.Volume); - if (media.volume.level === vol) { - media.removeUpdateListener(listener); - called(update); - } - }); + it('media.setVolume should set the volume', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); - media.setVolume(request, function () { - assert.instanceOf(media.volume, chrome.cast.Volume); - assert.equal(media.volume.level, vol); - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - it('media.setVolume should set muted', function (done) { - var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); - - var muted = true; - var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, muted)); - - media.addUpdateListener(function listener (isAlive) { - assert.isTrue(isAlive); - assert.instanceOf(media.volume, chrome.cast.Volume); - if (media.volume.muted === muted) { - media.removeUpdateListener(listener); - called(update); - } - }); - - media.setVolume(request, function () { - assert.instanceOf(media.volume, chrome.cast.Volume); - assert.equal(media.volume.muted, muted); - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - it('media.setVolume should set the volume and mute state', function (done) { - var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); - - // Ensure we select a different volume - var vol = media.volume.level; - if (vol) { - vol = Math.abs(vol - 0.5); - } else { - vol = Math.round(Math.random() * 100) / 100; - } - var muted = false; - var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(vol, muted)); + // Ensure we select a different volume + var vol = media.volume.level; + if (vol) { + vol = Math.abs(vol - 0.5); + } else { + vol = Math.random(); + } + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(vol)); - media.addUpdateListener(function listener (isAlive) { - assert.isTrue(isAlive); - assert.instanceOf(media.volume, chrome.cast.Volume); - if (media.volume.level === vol && - media.volume.muted === request.volume.muted) { - media.removeUpdateListener(listener); - called(update); - } - }); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + assert.instanceOf(media.volume, chrome.cast.Volume); + if (media.volume.level === vol) { + media.removeUpdateListener(listener); + called(update); + } + }); - media.setVolume(request, function () { - assert.instanceOf(media.volume, chrome.cast.Volume); - assert.equal(media.volume.level, vol); - assert.equal(media.volume.muted, muted); - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - it('media.pause should pause playback', function (done) { - var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); - media.addUpdateListener(function listener (isAlive) { - assert.isTrue(isAlive); - assert.notEqual(media.playerState, chrome.cast.media.PlayerState.IDLE); - if (media.playerState === chrome.cast.media.PlayerState.PAUSED) { - media.removeUpdateListener(listener); - called(update); - } - }); - media.pause(null, function () { - assert.equal(media.playerState, chrome.cast.media.PlayerState.PAUSED); - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - it('media.play should resume playback', function (done) { - var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); - media.addUpdateListener(function listener (isAlive) { - assert.isTrue(isAlive); - assert.notEqual(media.playerState, chrome.cast.media.PlayerState.IDLE); - if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { - media.removeUpdateListener(listener); - called(update); - } - }); - media.play(null, function () { - assert.oneOf(media.playerState, [ - chrome.cast.media.PlayerState.PLAYING, - chrome.cast.media.PlayerState.BUFFERING]); - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - it('media.seek should skip to requested position', function (done) { - var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); - var request = new chrome.cast.media.SeekRequest(); - request.currentTime = media.media.duration / 2; - media.addUpdateListener(function listener (isAlive) { - assert.isTrue(isAlive); - if (media.getEstimatedTime() > request.currentTime - 1 && - media.getEstimatedTime() < request.currentTime + 1) { - media.removeUpdateListener(listener); - called(update); - } - }); - media.seek(request, function () { - assert.closeTo(media.getEstimatedTime(), request.currentTime, 1); - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - it('media.addUpdateListener should detect end of video', function (done) { - var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); - var request = new chrome.cast.media.SeekRequest(); - request.currentTime = media.media.duration; - media.addUpdateListener(function listener (isAlive) { - if (media.playerState === chrome.cast.media.PlayerState.IDLE) { - media.removeUpdateListener(listener); - assert.equal(media.idleReason, chrome.cast.media.IdleReason.FINISHED); - assert.isFalse(isAlive); - called(update); - } - }); - media.seek(request, function () { - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - it('media.setVolume should return error when media is finished', function (done) { - var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume()); - media.setVolume(request, function () { - assert.fail('should not hit success'); - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); - assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); - assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); - done(); - }); - }); - it('media.pause should return error when media is finished', function (done) { - media.pause(null, function () { - assert.fail('should not hit success'); - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); - assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); - assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); - done(); - }); - }); - it('media.play should return error when media is finished', function (done) { - media.play(null, function () { - assert.fail('should not hit success'); - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); - assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); - assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); - done(); - }); - }); - it('media.seek should return error when media is finished', function (done) { - var request = new chrome.cast.media.SeekRequest(); - request.currentTime = media.media.duration; - media.seek(request, function () { - assert.fail('should not hit success'); - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); - assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); - assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); - done(); - }); - }); - it('media.stop should return error when media is finished', function (done) { - media.stop(null, function () { - assert.fail('should not hit success'); - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); - assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); - assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); - done(); - }); - }); - it('session.loadMedia should be able to load videos twice in a row and handle MovieMediaMetadata and TvShowMediaMetadata correctly', function (done) { - var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); - mediaInfo.metadata = new chrome.cast.media.MovieMediaMetadata(); - mediaInfo.metadata.title = 'DaTitle'; - mediaInfo.metadata.subtitle = 'DaSubtitle'; - mediaInfo.metadata.studio = 'DaStudio'; - mediaInfo.metadata.myMadeUpMetadata = 'DaMadeUpMetadata'; - mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; - session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { - media = m; - utils.testMediaProperties(media); - assert.equal(media.media.metadata.title, mediaInfo.metadata.title); - assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); - assert.equal(media.media.metadata.studio, mediaInfo.metadata.studio); - assert.equal(media.media.metadata.myMadeUpMetadata, mediaInfo.metadata.myMadeUpMetadata); - assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); - assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.MOVIE); - assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.MOVIE); - media.addUpdateListener(function listener (isAlive) { - assert.isTrue(isAlive); - utils.testMediaProperties(media); - assert.oneOf(media.playerState, [ - chrome.cast.media.PlayerState.PLAYING, - chrome.cast.media.PlayerState.BUFFERING]); - if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { - media.removeUpdateListener(listener); - loadSecond(); - } + media.setVolume(request, function () { + assert.instanceOf(media.volume, chrome.cast.Volume); + assert.equal(media.volume.level, vol); + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); + it('media.setVolume should set muted', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + + var muted = true; + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(null, muted)); - function loadSecond () { - var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); - mediaInfo.metadata = new chrome.cast.media.TvShowMediaMetadata(); - mediaInfo.metadata.title = 'DaTitle'; - mediaInfo.metadata.subtitle = 'DaSubtitle'; - mediaInfo.metadata.originalAirDate = new Date().valueOf(); - mediaInfo.metadata.episode = 15; - mediaInfo.metadata.season = 2; - mediaInfo.metadata.seriesTitle = 'DaSeries'; - mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; - session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { - media = m; - utils.testMediaProperties(media); - assert.equal(media.media.metadata.title, mediaInfo.metadata.title); - assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); - assert.equal(media.media.metadata.originalAirDate, mediaInfo.metadata.originalAirDate); - assert.equal(media.media.metadata.episode, mediaInfo.metadata.episode); - assert.equal(media.media.metadata.season, mediaInfo.metadata.season); - assert.equal(media.media.metadata.seriesTitle, mediaInfo.metadata.seriesTitle); - assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); - assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.TV_SHOW); - assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.TV_SHOW); media.addUpdateListener(function listener (isAlive) { assert.isTrue(isAlive); - utils.testMediaProperties(media); - assert.oneOf(media.playerState, [ - chrome.cast.media.PlayerState.PLAYING, - chrome.cast.media.PlayerState.BUFFERING]); - if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + assert.instanceOf(media.volume, chrome.cast.Volume); + if (media.volume.muted === muted) { media.removeUpdateListener(listener); - done(); + called(update); } }); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - } - }); - it('session.loadMedia should be able to load remote audio and return the MusicTrackMediaMetadata', function (done) { - var mediaInfo = new chrome.cast.media.MediaInfo(audioUrl, 'audio/mpeg'); - mediaInfo.metadata = new chrome.cast.media.MusicTrackMediaMetadata(); - mediaInfo.metadata.albumArtist = 'DaAlmbumArtist'; - mediaInfo.metadata.albumName = 'DaAlbum'; - mediaInfo.metadata.artist = 'DaArtist'; - mediaInfo.metadata.composer = 'DaComposer'; - mediaInfo.metadata.title = 'DaTitle'; - mediaInfo.metadata.songName = 'DaSongName'; - mediaInfo.metadata.releaseDate = new Date().valueOf(); - mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; - mediaInfo.metadata.myMadeUpMetadata = 15; - session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { - media = m; - utils.testMediaProperties(media); - assert.equal(media.media.metadata.albumArtist, mediaInfo.metadata.albumArtist); - assert.equal(media.media.metadata.albumName, mediaInfo.metadata.albumName); - assert.equal(media.media.metadata.artist, mediaInfo.metadata.artist); - assert.equal(media.media.metadata.composer, mediaInfo.metadata.composer); - assert.equal(media.media.metadata.title, mediaInfo.metadata.title); - assert.equal(media.media.metadata.songName, mediaInfo.metadata.songName); - assert.equal(media.media.metadata.releaseDate, mediaInfo.metadata.releaseDate); - assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); - assert.equal(media.media.metadata.myMadeUpMetadata, mediaInfo.metadata.myMadeUpMetadata); - assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.MUSIC_TRACK); - assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.MUSIC_TRACK); - media.addUpdateListener(function listener (isAlive) { - assert.isTrue(isAlive); - utils.testMediaProperties(media); - assert.oneOf(media.playerState, [ - chrome.cast.media.PlayerState.PLAYING, - chrome.cast.media.PlayerState.BUFFERING]); - if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { - media.removeUpdateListener(listener); - done(); - } - }); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - it('session.loadMedia should be able to load remote image and return the PhotoMediaMetadata', function (done) { - var mediaInfo = new chrome.cast.media.MediaInfo(imageUrl, 'image/jpeg'); - mediaInfo.metadata = new chrome.cast.media.PhotoMediaMetadata(); - mediaInfo.metadata.title = 'DaTitle'; - mediaInfo.metadata.artist = 'DaArtist'; - mediaInfo.metadata.location = 'DaLocation'; - mediaInfo.metadata.latitude = 102.13; - mediaInfo.metadata.longitude = 101.12; - mediaInfo.metadata.height = 100; - mediaInfo.metadata.width = 100; - mediaInfo.metadata.myMadeUpMetadata = 15; - mediaInfo.metadata.creationDateTime = new Date().valueOf(); - mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; - session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { - media = m; - utils.testMediaProperties(media); - assert.equal(media.media.metadata.title, mediaInfo.metadata.title); - assert.equal(media.media.metadata.artist, mediaInfo.metadata.artist); - assert.equal(media.media.metadata.location, mediaInfo.metadata.location); - assert.equal(media.media.metadata.latitude, mediaInfo.metadata.latitude); - assert.equal(media.media.metadata.longitude, mediaInfo.metadata.longitude); - assert.equal(media.media.metadata.height, mediaInfo.metadata.height); - assert.equal(media.media.metadata.width, mediaInfo.metadata.width); - assert.equal(media.media.metadata.myMadeUpMetadata, mediaInfo.metadata.myMadeUpMetadata); - assert.equal(media.media.metadata.creationDateTime, mediaInfo.metadata.creationDateTime); - assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); - assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.PHOTO); - assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.PHOTO); - media.addUpdateListener(function listener (isAlive) { - assert.isTrue(isAlive); - utils.testMediaProperties(media); - if (media.playerState === chrome.cast.media.PlayerState.PAUSED) { - media.removeUpdateListener(listener); - done(); - } - }); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - it('media.stop should end video playback', function (done) { - var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); - media.addUpdateListener(function listener (isAlive) { - if (media.playerState === chrome.cast.media.PlayerState.IDLE) { - media.removeUpdateListener(listener); - assert.equal(media.idleReason, chrome.cast.media.IdleReason.CANCELLED); - assert.isFalse(isAlive); - called(update); - } - }); - media.stop(null, function () { - assert.equal(media.playerState, chrome.cast.media.PlayerState.IDLE); - assert.equal(media.idleReason, chrome.cast.media.IdleReason.CANCELLED); - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - }); - describe('Queues', function () { - var videoItem; - var audioItem; - var startTime = 40; - function checkItems (items) { - assert.isTrue(items[0].autoplay); - assert.equal(items[0].startTime, startTime); - assert.equal(items[0].media.contentId, videoUrl); - assert.isTrue(items[1].autoplay); - assert.equal(items[1].startTime, startTime * 2); - assert.equal(items[1].media.contentId, audioUrl); - } - before(function () { - videoItem = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); - videoItem.metadata = new chrome.cast.media.TvShowMediaMetadata(); - videoItem.metadata.title = 'DaTitle'; - videoItem.metadata.subtitle = 'DaSubtitle'; - videoItem.metadata.originalAirDate = new Date().valueOf(); - videoItem.metadata.episode = 15; - videoItem.metadata.season = 2; - videoItem.metadata.seriesTitle = 'DaSeries'; - videoItem.metadata.images = [new chrome.cast.Image(imageUrl)]; - audioItem = new chrome.cast.media.MediaInfo(audioUrl, 'audio/mpeg'); - audioItem.metadata = new chrome.cast.media.MusicTrackMediaMetadata(); - audioItem.metadata.albumArtist = 'DaAlmbumArtist'; - audioItem.metadata.albumName = 'DaAlbum'; - audioItem.metadata.artist = 'DaArtist'; - audioItem.metadata.composer = 'DaComposer'; - audioItem.metadata.title = 'DaTitle'; - audioItem.metadata.songName = 'DaSongName'; - audioItem.metadata.myMadeUpMetadata = '15'; - audioItem.metadata.releaseDate = new Date().valueOf(); - audioItem.metadata.images = [new chrome.cast.Image(imageUrl)]; - }); - it('session.queueLoad should return an error when we attempt to load an empty queue', function (done) { - session.queueLoad(new chrome.cast.media.QueueLoadRequest([]), function (m) { - assert.fail('Should not be able to load an empty queue.'); - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); - assert.equal(err.description, 'INVALID_PARAMS'); - assert.deepEqual(err.details, { reason: 'INVALID_PARAMS', type: 'INVALID_REQUEST' }); - done(); + media.setVolume(request, function () { + assert.instanceOf(media.volume, chrome.cast.Volume); + assert.equal(media.volume.muted, muted); + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - }); - it('session.queueLoad should be able to load remote audio/video queue and return the correct Metadata', function (done) { - var item; - var queue = []; + it('media.setVolume should set the volume and mute state', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); - // Add items to the queue - item = new chrome.cast.media.QueueItem(videoItem); - item.startTime = startTime; - queue.push(item); - item = new chrome.cast.media.QueueItem(audioItem); - item.startTime = startTime * 2; - queue.push(item); + // Ensure we select a different volume + var vol = media.volume.level; + if (vol) { + vol = Math.abs(vol - 0.5); + } else { + vol = Math.round(Math.random() * 100) / 100; + } + var muted = false; + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume(vol, muted)); - // Create request to repeat all and start at 2nd item - var request = new chrome.cast.media.QueueLoadRequest(queue); - request.repeatMode = chrome.cast.media.RepeatMode.ALL; - request.startIndex = 1; + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + assert.instanceOf(media.volume, chrome.cast.Volume); + if (media.volume.level === vol && + media.volume.muted === request.volume.muted) { + media.removeUpdateListener(listener); + called(update); + } + }); - session.queueLoad(request, function (m) { - media = m; - var i = utils.getCurrentItemIndex(media); - utils.testMediaProperties(media); - assert.equal(media.currentItemId, media.items[i].itemId); - assert.equal(media.repeatMode, chrome.cast.media.RepeatMode.ALL); - assert.isObject(media.queueData); - assert.equal(media.queueData.repeatMode, request.repeatMode); - assert.isFalse(media.queueData.shuffle); - assert.equal(media.queueData.startIndex, request.startIndex); - utils.testQueueItems(media.items); - assert.equal(media.media.contentId, audioUrl); - assert.equal(media.items.length, 2); - checkItems(media.items); - assert.equal(media.items[i].media.metadata.albumArtist, audioItem.metadata.albumArtist); - assert.equal(media.items[i].media.metadata.albumName, audioItem.metadata.albumName); - assert.equal(media.items[i].media.metadata.artist, audioItem.metadata.artist); - assert.equal(media.items[i].media.metadata.composer, audioItem.metadata.composer); - assert.equal(media.items[i].media.metadata.title, audioItem.metadata.title); - assert.equal(media.items[i].media.metadata.songName, audioItem.metadata.songName); - assert.equal(media.items[i].media.metadata.releaseDate, audioItem.metadata.releaseDate); - assert.equal(media.items[i].media.metadata.images[0].url, audioItem.metadata.images[0].url); - assert.equal(media.items[i].media.metadata.myMadeUpMetadata, audioItem.metadata.myMadeUpMetadata); - assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.MUSIC_TRACK); - assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.MUSIC_TRACK); + media.setVolume(request, function () { + assert.instanceOf(media.volume, chrome.cast.Volume); + assert.equal(media.volume.level, vol); + assert.equal(media.volume.muted, muted); + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + it('media.pause should pause playback', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); media.addUpdateListener(function listener (isAlive) { assert.isTrue(isAlive); - utils.testMediaProperties(media); + assert.notEqual(media.playerState, chrome.cast.media.PlayerState.IDLE); + if (media.playerState === chrome.cast.media.PlayerState.PAUSED) { + media.removeUpdateListener(listener); + called(update); + } + }); + media.pause(null, function () { + assert.equal(media.playerState, chrome.cast.media.PlayerState.PAUSED); + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + it('media.play should resume playback', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + assert.notEqual(media.playerState, chrome.cast.media.PlayerState.IDLE); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + called(update); + } + }); + media.play(null, function () { assert.oneOf(media.playerState, [ chrome.cast.media.PlayerState.PLAYING, chrome.cast.media.PlayerState.BUFFERING]); - if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + it('media.seek should skip to requested position', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + var request = new chrome.cast.media.SeekRequest(); + request.currentTime = media.media.duration / 2; + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + if (media.getEstimatedTime() > request.currentTime - 1 && + media.getEstimatedTime() < request.currentTime + 1) { media.removeUpdateListener(listener); - assert.closeTo(media.getEstimatedTime(), startTime * 2, 5); - done(); + called(update); } }); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + media.seek(request, function () { + assert.closeTo(media.getEstimatedTime(), request.currentTime, 1); + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - }); - it('Queue should start the next item automatically when previous one finishes (tests loop around of repeat_all as well)', function (done) { - var called = utils.callOrder([ - { id: success, repeats: false }, - { id: stopped, repeats: true }, - { id: newMedia, repeats: true }, - { id: update, repeats: true } - ], done); - // Create request - var request = new chrome.cast.media.SeekRequest(); - request.currentTime = media.media.duration - 1; - - // Listen for current media end - var prevId = media.currentItemId; - media.addUpdateListener(function listener (isAlive) { - if (media.playerState === chrome.cast.media.PlayerState.IDLE) { - assert.equal(media.idleReason, chrome.cast.media.IdleReason.FINISHED); - assert.isTrue(isAlive); - called(stopped); - } - if (media.currentItemId !== prevId) { - var i = utils.getCurrentItemIndex(media); - utils.testMediaProperties(media); - assert.equal(media.repeatMode, chrome.cast.media.RepeatMode.ALL); - assert.equal(media.media.contentId, videoUrl); - utils.testQueueItems(media.items); - assert.equal(media.items.length, 2); - checkItems(media.items); - assert.equal(media.items[i].media.contentId, videoUrl); - assert.equal(media.items[i].media.metadata.title, videoItem.metadata.title); - assert.equal(media.items[i].media.metadata.subtitle, videoItem.metadata.subtitle); - assert.equal(media.items[i].media.metadata.originalAirDate, videoItem.metadata.originalAirDate); - assert.equal(media.items[i].media.metadata.episode, videoItem.metadata.episode); - assert.equal(media.items[i].media.metadata.season, videoItem.metadata.season); - assert.equal(media.items[i].media.metadata.seriesTitle, videoItem.metadata.seriesTitle); - assert.equal(media.items[i].media.metadata.images[0].url, videoItem.metadata.images[0].url); - assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.TV_SHOW); - assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.TV_SHOW); - called(newMedia); - if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + it('media.addUpdateListener should detect end of video', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + var request = new chrome.cast.media.SeekRequest(); + request.currentTime = media.media.duration; + media.addUpdateListener(function listener (isAlive) { + if (media.playerState === chrome.cast.media.PlayerState.IDLE) { media.removeUpdateListener(listener); - assert.closeTo(media.getEstimatedTime(), startTime, 5); + assert.equal(media.idleReason, chrome.cast.media.IdleReason.FINISHED); + assert.isFalse(isAlive); called(update); } + }); + media.seek(request, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + it('media.setVolume should return error when media is finished', function (done) { + var request = new chrome.cast.media.VolumeRequest(new chrome.cast.Volume()); + media.setVolume(request, function () { + assert.fail('should not hit success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); + assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); + done(); + }); + }); + it('media.pause should return error when media is finished', function (done) { + media.pause(null, function () { + assert.fail('should not hit success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); + assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); + done(); + }); + }); + it('media.play should return error when media is finished', function (done) { + media.play(null, function () { + assert.fail('should not hit success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); + assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); + done(); + }); + }); + it('media.seek should return error when media is finished', function (done) { + var request = new chrome.cast.media.SeekRequest(); + request.currentTime = media.media.duration; + media.seek(request, function () { + assert.fail('should not hit success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); + assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); + done(); + }); + }); + it('media.stop should return error when media is finished', function (done) { + media.stop(null, function () { + assert.fail('should not hit success'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'INVALID_MEDIA_SESSION_ID'); + assert.deepEqual(err.details, { reason: 'INVALID_MEDIA_SESSION_ID', type: 'INVALID_REQUEST' }); + done(); + }); + }); + it('session.loadMedia should be able to load videos twice in a row and handle MovieMediaMetadata and TvShowMediaMetadata correctly', function (done) { + var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + mediaInfo.metadata = new chrome.cast.media.MovieMediaMetadata(); + mediaInfo.metadata.title = 'DaTitle'; + mediaInfo.metadata.subtitle = 'DaSubtitle'; + mediaInfo.metadata.studio = 'DaStudio'; + mediaInfo.metadata.myMadeUpMetadata = 'DaMadeUpMetadata'; + mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; + session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { + media = m; + utils.testMediaProperties(media); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); + assert.equal(media.media.metadata.studio, mediaInfo.metadata.studio); + assert.equal(media.media.metadata.myMadeUpMetadata, mediaInfo.metadata.myMadeUpMetadata); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.MOVIE); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.MOVIE); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + utils.testMediaProperties(media); + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + loadSecond(); + } + }); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + + function loadSecond () { + var mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + mediaInfo.metadata = new chrome.cast.media.TvShowMediaMetadata(); + mediaInfo.metadata.title = 'DaTitle'; + mediaInfo.metadata.subtitle = 'DaSubtitle'; + mediaInfo.metadata.originalAirDate = new Date().valueOf(); + mediaInfo.metadata.episode = 15; + mediaInfo.metadata.season = 2; + mediaInfo.metadata.seriesTitle = 'DaSeries'; + mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; + session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { + media = m; + utils.testMediaProperties(media); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); + assert.equal(media.media.metadata.originalAirDate, mediaInfo.metadata.originalAirDate); + assert.equal(media.media.metadata.episode, mediaInfo.metadata.episode); + assert.equal(media.media.metadata.season, mediaInfo.metadata.season); + assert.equal(media.media.metadata.seriesTitle, mediaInfo.metadata.seriesTitle); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.TV_SHOW); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.TV_SHOW); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + utils.testMediaProperties(media); + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + done(); + } + }); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); } }); - // Seek to just before the end - media.seek(request, function () { - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + it('session.loadMedia should be able to load remote audio and return the MusicTrackMediaMetadata', function (done) { + var mediaInfo = new chrome.cast.media.MediaInfo(audioUrl, 'audio/mpeg'); + mediaInfo.metadata = new chrome.cast.media.MusicTrackMediaMetadata(); + mediaInfo.metadata.albumArtist = 'DaAlmbumArtist'; + mediaInfo.metadata.albumName = 'DaAlbum'; + mediaInfo.metadata.artist = 'DaArtist'; + mediaInfo.metadata.composer = 'DaComposer'; + mediaInfo.metadata.title = 'DaTitle'; + mediaInfo.metadata.songName = 'DaSongName'; + mediaInfo.metadata.releaseDate = new Date().valueOf(); + mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; + mediaInfo.metadata.myMadeUpMetadata = 15; + session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { + media = m; + utils.testMediaProperties(media); + assert.equal(media.media.metadata.albumArtist, mediaInfo.metadata.albumArtist); + assert.equal(media.media.metadata.albumName, mediaInfo.metadata.albumName); + assert.equal(media.media.metadata.artist, mediaInfo.metadata.artist); + assert.equal(media.media.metadata.composer, mediaInfo.metadata.composer); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.songName, mediaInfo.metadata.songName); + assert.equal(media.media.metadata.releaseDate, mediaInfo.metadata.releaseDate); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.myMadeUpMetadata, mediaInfo.metadata.myMadeUpMetadata); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.MUSIC_TRACK); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.MUSIC_TRACK); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + utils.testMediaProperties(media); + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + done(); + } + }); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - }); - it('media.queueJumpToItem should not call a callback for null contentId', function () { - media.queueJumpToItem(null, function () { - assert.fail('Should not be called when passing null content id to queueJumpToItem'); - }, function () { - assert.fail('Should not be called when passing null content id to queueJumpToItem'); + it('session.loadMedia should be able to load remote image and return the PhotoMediaMetadata', function (done) { + var mediaInfo = new chrome.cast.media.MediaInfo(imageUrl, 'image/jpeg'); + mediaInfo.metadata = new chrome.cast.media.PhotoMediaMetadata(); + mediaInfo.metadata.title = 'DaTitle'; + mediaInfo.metadata.artist = 'DaArtist'; + mediaInfo.metadata.location = 'DaLocation'; + mediaInfo.metadata.latitude = 102.13; + mediaInfo.metadata.longitude = 101.12; + mediaInfo.metadata.height = 100; + mediaInfo.metadata.width = 100; + mediaInfo.metadata.myMadeUpMetadata = 15; + mediaInfo.metadata.creationDateTime = new Date().valueOf(); + mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; + session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { + media = m; + utils.testMediaProperties(media); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.artist, mediaInfo.metadata.artist); + assert.equal(media.media.metadata.location, mediaInfo.metadata.location); + assert.equal(media.media.metadata.latitude, mediaInfo.metadata.latitude); + assert.equal(media.media.metadata.longitude, mediaInfo.metadata.longitude); + assert.equal(media.media.metadata.height, mediaInfo.metadata.height); + assert.equal(media.media.metadata.width, mediaInfo.metadata.width); + assert.equal(media.media.metadata.myMadeUpMetadata, mediaInfo.metadata.myMadeUpMetadata); + assert.equal(media.media.metadata.creationDateTime, mediaInfo.metadata.creationDateTime); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.PHOTO); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.PHOTO); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + utils.testMediaProperties(media); + if (media.playerState === chrome.cast.media.PlayerState.PAUSED) { + media.removeUpdateListener(listener); + done(); + } + }); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); - }); - it('media.queueJumpToItem should not call a callback for unknown contentId', function () { - media.queueJumpToItem('unknown_content_id', function () { - assert.fail('Should not be called when passing unknown content id to queueJumpToItem'); - }, function () { - assert.fail('Should not be called when passing unknown content id to queueJumpToItem'); + it('media.stop should end video playback', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + media.addUpdateListener(function listener (isAlive) { + if (media.playerState === chrome.cast.media.PlayerState.IDLE) { + media.removeUpdateListener(listener); + assert.equal(media.idleReason, chrome.cast.media.IdleReason.CANCELLED); + assert.isFalse(isAlive); + called(update); + } + }); + media.stop(null, function () { + assert.equal(media.playerState, chrome.cast.media.PlayerState.IDLE); + assert.equal(media.idleReason, chrome.cast.media.IdleReason.CANCELLED); + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); }); }); - it('media.queueJumpToItem should not call a callback for decimal contentId', function () { - media.queueJumpToItem(1.5, function () { - assert.fail('Should not be called when passing decimal content id to queueJumpToItem'); - }, function () { - assert.fail('Should not be called when passing decimal content id to queueJumpToItem'); + describe('Queues', function () { + var videoItem; + var audioItem; + var startTime = 40; + function checkItems (items) { + assert.isTrue(items[0].autoplay); + assert.equal(items[0].startTime, startTime); + assert.equal(items[0].media.contentId, videoUrl); + assert.isTrue(items[1].autoplay); + assert.equal(items[1].startTime, startTime * 2); + assert.equal(items[1].media.contentId, audioUrl); + } + before(function () { + videoItem = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + videoItem.metadata = new chrome.cast.media.TvShowMediaMetadata(); + videoItem.metadata.title = 'DaTitle'; + videoItem.metadata.subtitle = 'DaSubtitle'; + videoItem.metadata.originalAirDate = new Date().valueOf(); + videoItem.metadata.episode = 15; + videoItem.metadata.season = 2; + videoItem.metadata.seriesTitle = 'DaSeries'; + videoItem.metadata.images = [new chrome.cast.Image(imageUrl)]; + + audioItem = new chrome.cast.media.MediaInfo(audioUrl, 'audio/mpeg'); + audioItem.metadata = new chrome.cast.media.MusicTrackMediaMetadata(); + audioItem.metadata.albumArtist = 'DaAlmbumArtist'; + audioItem.metadata.albumName = 'DaAlbum'; + audioItem.metadata.artist = 'DaArtist'; + audioItem.metadata.composer = 'DaComposer'; + audioItem.metadata.title = 'DaTitle'; + audioItem.metadata.songName = 'DaSongName'; + audioItem.metadata.myMadeUpMetadata = '15'; + audioItem.metadata.releaseDate = new Date().valueOf(); + audioItem.metadata.images = [new chrome.cast.Image(imageUrl)]; }); - }); - it('media.queueJumpToItem should jump to selected item', function (done) { - var calledAnyOrder = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); - var calledOrder = utils.callOrder([ - { id: stopped, repeats: true }, - { id: newMedia, repeats: true } - ], function () { - calledAnyOrder(update); + it('session.queueLoad should return an error when we attempt to load an empty queue', function (done) { + session.queueLoad(new chrome.cast.media.QueueLoadRequest([]), function (m) { + assert.fail('Should not be able to load an empty queue.'); + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'INVALID_PARAMS'); + assert.deepEqual(err.details, { reason: 'INVALID_PARAMS', type: 'INVALID_REQUEST' }); + done(); + }); }); - var i = utils.getCurrentItemIndex(media); - media.addUpdateListener(function listener (isAlive) { - if (media.playerState === chrome.cast.media.PlayerState.IDLE) { - assert.equal(media.idleReason, chrome.cast.media.IdleReason.INTERRUPTED); - assert.isTrue(isAlive); - calledOrder(stopped); - } - if (media.currentItemId !== media.items[i].itemId) { - i = utils.getCurrentItemIndex(media); - media.removeUpdateListener(listener); + it('session.queueLoad should be able to load remote audio/video queue and return the correct Metadata', function (done) { + var item; + var queue = []; + + // Add items to the queue + item = new chrome.cast.media.QueueItem(videoItem); + item.startTime = startTime; + queue.push(item); + item = new chrome.cast.media.QueueItem(audioItem); + item.startTime = startTime * 2; + queue.push(item); + + // Create request to repeat all and start at 2nd item + var request = new chrome.cast.media.QueueLoadRequest(queue); + request.repeatMode = chrome.cast.media.RepeatMode.ALL; + request.startIndex = 1; + + session.queueLoad(request, function (m) { + media = m; + var i = utils.getCurrentItemIndex(media); utils.testMediaProperties(media); assert.equal(media.currentItemId, media.items[i].itemId); + assert.equal(media.repeatMode, chrome.cast.media.RepeatMode.ALL); + assert.isObject(media.queueData); + assert.equal(media.queueData.repeatMode, request.repeatMode); + assert.isFalse(media.queueData.shuffle); + assert.equal(media.queueData.startIndex, request.startIndex); utils.testQueueItems(media.items); assert.equal(media.media.contentId, audioUrl); assert.equal(media.items.length, 2); checkItems(media.items); - assert.equal(media.items[i].media.contentId, audioUrl); assert.equal(media.items[i].media.metadata.albumArtist, audioItem.metadata.albumArtist); assert.equal(media.items[i].media.metadata.albumName, audioItem.metadata.albumName); assert.equal(media.items[i].media.metadata.artist, audioItem.metadata.artist); @@ -1233,71 +1105,199 @@ assert.equal(media.items[i].media.metadata.myMadeUpMetadata, audioItem.metadata.myMadeUpMetadata); assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.MUSIC_TRACK); assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.MUSIC_TRACK); - assert.closeTo(media.getEstimatedTime(), startTime * 2, 5); - calledOrder(newMedia); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + utils.testMediaProperties(media); + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + assert.closeTo(media.getEstimatedTime(), startTime * 2, 5); + done(); + } + }); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + it('Queue should start the next item automatically when previous one finishes (tests loop around of repeat_all as well)', function (done) { + var called = utils.callOrder([ + { id: success, repeats: false }, + { id: stopped, repeats: true }, + { id: newMedia, repeats: true }, + { id: update, repeats: true } + ], done); + // Create request + var request = new chrome.cast.media.SeekRequest(); + request.currentTime = media.media.duration - 1; + + // Listen for current media end + var prevId = media.currentItemId; + media.addUpdateListener(function listener (isAlive) { + if (media.playerState === chrome.cast.media.PlayerState.IDLE) { + assert.equal(media.idleReason, chrome.cast.media.IdleReason.FINISHED); + assert.isTrue(isAlive); + called(stopped); + } + if (media.currentItemId !== prevId) { + var i = utils.getCurrentItemIndex(media); + utils.testMediaProperties(media); + assert.equal(media.repeatMode, chrome.cast.media.RepeatMode.ALL); + assert.equal(media.media.contentId, videoUrl); + utils.testQueueItems(media.items); + assert.equal(media.items.length, 2); + checkItems(media.items); + assert.equal(media.items[i].media.contentId, videoUrl); + assert.equal(media.items[i].media.metadata.title, videoItem.metadata.title); + assert.equal(media.items[i].media.metadata.subtitle, videoItem.metadata.subtitle); + assert.equal(media.items[i].media.metadata.originalAirDate, videoItem.metadata.originalAirDate); + assert.equal(media.items[i].media.metadata.episode, videoItem.metadata.episode); + assert.equal(media.items[i].media.metadata.season, videoItem.metadata.season); + assert.equal(media.items[i].media.metadata.seriesTitle, videoItem.metadata.seriesTitle); + assert.equal(media.items[i].media.metadata.images[0].url, videoItem.metadata.images[0].url); + assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.TV_SHOW); + assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.TV_SHOW); + called(newMedia); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + assert.closeTo(media.getEstimatedTime(), startTime, 5); + called(update); + } + } + }); + // Seek to just before the end + media.seek(request, function () { + called(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + it('media.queueJumpToItem should not call a callback for null contentId', function () { + media.queueJumpToItem(null, function () { + assert.fail('Should not be called when passing null content id to queueJumpToItem'); + }, function () { + assert.fail('Should not be called when passing null content id to queueJumpToItem'); + }); + }); + it('media.queueJumpToItem should not call a callback for unknown contentId', function () { + media.queueJumpToItem('unknown_content_id', function () { + assert.fail('Should not be called when passing unknown content id to queueJumpToItem'); + }, function () { + assert.fail('Should not be called when passing unknown content id to queueJumpToItem'); + }); + }); + it('media.queueJumpToItem should not call a callback for decimal contentId', function () { + media.queueJumpToItem(1.5, function () { + assert.fail('Should not be called when passing decimal content id to queueJumpToItem'); + }, function () { + assert.fail('Should not be called when passing decimal content id to queueJumpToItem'); + }); + }); + it('media.queueJumpToItem should jump to selected item', function (done) { + var calledAnyOrder = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + var calledOrder = utils.callOrder([ + { id: stopped, repeats: true }, + { id: newMedia, repeats: true } + ], function () { + calledAnyOrder(update); + }); + var i = utils.getCurrentItemIndex(media); + media.addUpdateListener(function listener (isAlive) { + if (media.playerState === chrome.cast.media.PlayerState.IDLE) { + assert.equal(media.idleReason, chrome.cast.media.IdleReason.INTERRUPTED); + assert.isTrue(isAlive); + calledOrder(stopped); + } + if (media.currentItemId !== media.items[i].itemId) { + i = utils.getCurrentItemIndex(media); + media.removeUpdateListener(listener); + utils.testMediaProperties(media); + assert.equal(media.currentItemId, media.items[i].itemId); + utils.testQueueItems(media.items); + assert.equal(media.media.contentId, audioUrl); + assert.equal(media.items.length, 2); + checkItems(media.items); + assert.equal(media.items[i].media.contentId, audioUrl); + assert.equal(media.items[i].media.metadata.albumArtist, audioItem.metadata.albumArtist); + assert.equal(media.items[i].media.metadata.albumName, audioItem.metadata.albumName); + assert.equal(media.items[i].media.metadata.artist, audioItem.metadata.artist); + assert.equal(media.items[i].media.metadata.composer, audioItem.metadata.composer); + assert.equal(media.items[i].media.metadata.title, audioItem.metadata.title); + assert.equal(media.items[i].media.metadata.songName, audioItem.metadata.songName); + assert.equal(media.items[i].media.metadata.releaseDate, audioItem.metadata.releaseDate); + assert.equal(media.items[i].media.metadata.images[0].url, audioItem.metadata.images[0].url); + assert.equal(media.items[i].media.metadata.myMadeUpMetadata, audioItem.metadata.myMadeUpMetadata); + assert.equal(media.items[i].media.metadata.metadataType, chrome.cast.media.MetadataType.MUSIC_TRACK); + assert.equal(media.items[i].media.metadata.type, chrome.cast.media.MetadataType.MUSIC_TRACK); + assert.closeTo(media.getEstimatedTime(), startTime * 2, 5); + calledOrder(newMedia); + } + }); + // Jump + var jumpIndex = (i + 1) % media.items.length; + media.queueJumpToItem(media.items[jumpIndex].itemId, function () { + calledAnyOrder(success); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); + }); + after(function (done) { + // Set up the expected calls + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: true } + ], done); + session.addUpdateListener(function listener (isAlive) { + if (session.status === chrome.cast.SessionStatus.STOPPED) { + assert.isFalse(isAlive); + session.removeUpdateListener(listener); + called(update); } }); - // Jump - var jumpIndex = (i + 1) % media.items.length; - media.queueJumpToItem(media.items[jumpIndex].itemId, function () { - calledAnyOrder(success); + session.stop(function () { + called(success); }, function (err) { assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); }); - after(function (done) { - // Set up the expected calls - var called = utils.waitForAllCalls([ - { id: success, repeats: false }, - { id: update, repeats: true } - ], done); - session.addUpdateListener(function listener (isAlive) { - if (session.status === chrome.cast.SessionStatus.STOPPED) { - assert.isFalse(isAlive); - session.removeUpdateListener(listener); - called(update); - } - }); - session.stop(function () { - called(success); - }, function (err) { - assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); - }); - }); - }); - describe('Tests that break the suite that must come last', function () { - // This test will prevent all future events (eg. SESSION_UPDATE) - // from being received. So run last. - it('setup should stop any existing scan', function (done) { - var setupTriggered = false; - var called = utils.callOrder([ - { id: stopped, repeats: false }, - { id: success, repeats: false } - ], done); - // Listen for cancel error - chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { - // Wait for the scan to be loaded before adding the iframe - if (!setupTriggered) { - // Manually trigger setup - setupTriggered = true; - window.cordova.exec(function (result) { - if (result[0] === 'SETUP') { - called(success); - } - }, function (err) { - assert.fail(err); - }, 'Chromecast', 'setup', []); - } - }, function (err) { - assert.isObject(err); - assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); - assert.equal(err.description, 'Scan stopped because setup triggered.'); - called(stopped); + describe('Tests that break the suite that must come last', function () { + // This test will prevent all future events (eg. SESSION_UPDATE) + // from being received. So run last. + it('setup should stop any existing scan', function (done) { + var setupTriggered = false; + var called = utils.callOrder([ + { id: stopped, repeats: false }, + { id: success, repeats: false } + ], done); + // Listen for cancel error + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + // Wait for the scan to be loaded before adding the iframe + if (!setupTriggered) { + // Manually trigger setup + setupTriggered = true; + window.cordova.exec(function (result) { + if (result[0] === 'SETUP') { + called(success); + } + }, function (err) { + assert.fail(err); + }, 'Chromecast', 'setup', []); + } + }, function (err) { + assert.isObject(err); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped because setup triggered.'); + called(stopped); + }); }); }); - }); }); From 068baa2c9326171afbc2cbd41fa6dbd204ff9270 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Sun, 1 Dec 2019 21:52:44 -0700 Subject: [PATCH 136/166] [no change] satisfy eslint and miniscule performance boost --- tests/www/js/utils.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/www/js/utils.js b/tests/www/js/utils.js index 1e9946f..4926e06 100644 --- a/tests/www/js/utils.js +++ b/tests/www/js/utils.js @@ -14,7 +14,7 @@ (function () { 'use strict'; /* eslint-env mocha */ - /* global chrome */ + /* global chrome localStorage */ var assert = window.chai.assert; var utils = {}; @@ -28,7 +28,7 @@ }; utils.clearStoredValues = function () { - localStorage.clear(); + localStorage.clear(); }; /** @@ -149,6 +149,7 @@ for (var i = 0; i < calls.length; i++) { if (calls[i].id === callId) { callDetails = calls[i]; + break; } } // Is it a valid call? @@ -215,6 +216,7 @@ for (var i = 0; i < calls.length; i++) { if (calls[i].id === callId) { callDetails = calls[i]; + break; } } // Is it a valid call? From 4e263352489015c38f9e7c38ee36ae741765ac41 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 2 Dec 2019 00:48:11 -0700 Subject: [PATCH 137/166] (ios) Have the plugin start onload. This is supposed to be required to make session resume after app restart work correctly. --- plugin.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin.xml b/plugin.xml index 0ab54f7..eb2abd7 100644 --- a/plugin.xml +++ b/plugin.xml @@ -50,6 +50,7 @@ + From 3eedaade429383d0395a9c953d10e2d90c8e02b9 Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 2 Dec 2019 00:50:06 -0700 Subject: [PATCH 138/166] (ios) Fix issue https://github.com/miloproductionsinc/cordova-plugin-chromecast/issues/5 Added tests for the issue. --- src/ios/CastUtilities.m | 37 ++++---- tests/www/js/tests_manual_primary_1.js | 118 ++++++++++++++++++++++--- 2 files changed, 129 insertions(+), 26 deletions(-) diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index 1b21038..62ada1e 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -472,22 +472,29 @@ + (NSDictionary*)createSessionObject:(GCKCastSession *)session { } + (NSDictionary*)createSessionObject:(GCKCastSession *)session status:(NSString*)status { - return @{ - @"appId" : session.applicationMetadata.applicationID? session.applicationMetadata.applicationID : @"", - @"media" : [CastUtilities createMediaObject:session], - @"appImages" : @{}, - @"sessionId" : session.sessionID? session.sessionID : @"", - @"displayName" : session.applicationMetadata.applicationName? session.applicationMetadata.applicationName : @"", - @"receiver" : @{ - @"friendlyName" : session.device.friendlyName? session.device.friendlyName : @"", - @"label" : session.device.uniqueID, - @"volume" : @{ - @"level" : @(session.currentDeviceVolume), - @"muted" : @(session.currentDeviceMuted) - } - }, - @"status":![status isEqual: @""]? status : [CastUtilities getConnectionStatus:session.connectionState] + NSMutableDictionary* sessionOut = [[NSMutableDictionary alloc] init]; + sessionOut[@"appId"] = session.applicationMetadata.applicationID? session.applicationMetadata.applicationID : @""; + sessionOut[@"appImages"] = @{}; + sessionOut[@"sessionId"] = session.sessionID? session.sessionID : @""; + sessionOut[@"displayName"] = session.applicationMetadata.applicationName? session.applicationMetadata.applicationName : @""; + sessionOut[@"receiver"] = @{ + @"friendlyName" : session.device.friendlyName? session.device.friendlyName : @"", + @"label" : session.device.uniqueID, + @"volume" : @{ + @"level" : @(session.currentDeviceVolume), + @"muted" : @(session.currentDeviceMuted) + } }; + sessionOut[@"status"] = ![status isEqual: @""]? status : [CastUtilities getConnectionStatus:session.connectionState]; + + NSMutableArray* mediaArray = [[NSMutableArray alloc] init]; + NSDictionary* mediaObj = [CastUtilities createMediaObject:session]; + if (![mediaObj isEqual: @{}]) { + [mediaArray addObject:mediaObj]; + } + sessionOut[@"media"] = mediaArray; + + return sessionOut; } + (NSDictionary *)createMediaObject:(GCKCastSession *)session { diff --git a/tests/www/js/tests_manual_primary_1.js b/tests/www/js/tests_manual_primary_1.js index 6892d07..ef0c26d 100644 --- a/tests/www/js/tests_manual_primary_1.js +++ b/tests/www/js/tests_manual_primary_1.js @@ -27,6 +27,7 @@ }); describe('Manual Tests - Primary Device - Part 1', function () { + var imageUrl = 'https://ia800705.us.archive.org/1/items/GoodHousekeeping193810/Good%20Housekeeping%201938-10.jpg'; var videoUrl = 'https://ia801302.us.archive.org/1/items/TheWater_201510/TheWater.mp4'; var audioUrl = 'https://ia600304.us.archive.org/20/items/OTRR_Gunsmoke_Singles/Gunsmoke_52-10-03_024_Cain.mp3'; @@ -53,6 +54,22 @@ describe('App restart and reload/change page simulation', function () { var cookieName = 'primary-p1_restart-reload'; var runningNum = parseInt(utils.getValue(cookieName) || '0'); + var mediaInfo; + before(function () { + mediaInfo = new chrome.cast.media.MediaInfo(videoUrl, 'video/mp4'); + mediaInfo.metadata = new chrome.cast.media.GenericMediaMetadata(); + mediaInfo.metadata.title = 'DaTitle'; + mediaInfo.metadata.subtitle = 'DaSubtitle'; + mediaInfo.metadata.releaseDate = new Date(2019, 10, 24).valueOf(); + mediaInfo.metadata.someTrueBoolean = true; + mediaInfo.metadata.someFalseBoolean = false; + mediaInfo.metadata.someSmallNumber = 15; + mediaInfo.metadata.someLargeNumber = 1234567890123456; + mediaInfo.metadata.someSmallDecimal = 15.15; + mediaInfo.metadata.someLargeDecimal = 1234567.123456789; + mediaInfo.metadata.someString = 'SomeString'; + mediaInfo.metadata.images = [new chrome.cast.Image(imageUrl)]; + }); it('Create session', function (done) { this.timeout(15000); if (runningNum > 0) { @@ -78,7 +95,6 @@ utils.startSession(function (sess) { session = sess; utils.testSessionProperties(sess); - utils.storeValue(cookieName, ++runningNum); if (failed) { // Ensure the session has stopped on failure because // we might not hit this point until after the "after" has already run @@ -104,6 +120,46 @@ assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); }); }); + it('session.loadMedia should be able to load a remote video and handle GenericMediaMetadata', function (done) { + if (runningNum > 0) { + // Just pass the test because we need to skip ahead + return done(); + } + session.loadMedia(new chrome.cast.media.LoadRequest(mediaInfo), function (m) { + media = m; + utils.testMediaProperties(media); + assert.isUndefined(media.queueData); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); + assert.equal(media.media.metadata.releaseDate, mediaInfo.metadata.releaseDate); + // TODO figure out how to maintain the data types for custom params on the native side + // so that we don't have to do turn each actual and expected into a string + assert.equal(media.media.metadata.someTrueBoolean + '', mediaInfo.metadata.someTrueBoolean + ''); + assert.equal(media.media.metadata.someFalseBoolean + '', mediaInfo.metadata.someFalseBoolean + ''); + assert.equal(media.media.metadata.someSmallNumber + '', mediaInfo.metadata.someSmallNumber + ''); + assert.equal(media.media.metadata.someLargeNumber + '', mediaInfo.metadata.someLargeNumber + ''); + assert.equal(media.media.metadata.someSmallDecimal + '', mediaInfo.metadata.someSmallDecimal + ''); + assert.equal(media.media.metadata.someLargeDecimal + '', mediaInfo.metadata.someLargeDecimal + ''); + assert.equal(media.media.metadata.someString, mediaInfo.metadata.someString); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.GENERIC); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.GENERIC); + media.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + utils.testMediaProperties(media); + assert.oneOf(media.playerState, [ + chrome.cast.media.PlayerState.PLAYING, + chrome.cast.media.PlayerState.BUFFERING]); + if (media.playerState === chrome.cast.media.PlayerState.PLAYING) { + media.removeUpdateListener(listener); + utils.storeValue(cookieName, ++runningNum); + done(); + } + }); + }, function (err) { + assert.fail('Unexpected Error: ' + err.code + ': ' + err.description); + }); + }); it('Reload after session create, should receive session on initialize', function (done) { this.timeout(15000); var instructionNum = 1; @@ -134,16 +190,36 @@ done(); }); var apiConfig = new chrome.cast.ApiConfig( - new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), - function (sess) { - session = sess; - utils.testSessionProperties(sess); - called(session_listener); - }, function receiverListener (availability) { - if (!finished) { - called(availability); - } - }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); + new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), + function (sess) { + session = sess; + utils.testSessionProperties(sess); + // Ensure the media is maintained + assert.isAbove(sess.media.length, 0); + var media = sess.media[0]; + assert.isUndefined(media.queueData); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); + assert.equal(media.media.metadata.releaseDate, mediaInfo.metadata.releaseDate); + // TODO figure out how to maintain the data types for custom params on the native side + // so that we don't have to do turn each actual and expected into a string + assert.equal(media.media.metadata.someTrueBoolean + '', mediaInfo.metadata.someTrueBoolean + ''); + assert.equal(media.media.metadata.someFalseBoolean + '', mediaInfo.metadata.someFalseBoolean + ''); + assert.equal(media.media.metadata.someSmallNumber + '', mediaInfo.metadata.someSmallNumber + ''); + assert.equal(media.media.metadata.someLargeNumber + '', mediaInfo.metadata.someLargeNumber + ''); + assert.equal(media.media.metadata.someSmallDecimal + '', mediaInfo.metadata.someSmallDecimal + ''); + assert.equal(media.media.metadata.someLargeDecimal + '', mediaInfo.metadata.someLargeDecimal + ''); + assert.equal(media.media.metadata.someString, mediaInfo.metadata.someString); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.GENERIC); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.GENERIC); + assert.equal(media.playerState, chrome.cast.media.PlayerState.PLAYING); + called(session_listener); + }, function receiverListener (availability) { + if (!finished) { + called(availability); + } + }, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED); chrome.cast.initialize(apiConfig, function () { called(success); }, function (err) { @@ -195,6 +271,26 @@ function (sess) { session = sess; utils.testSessionProperties(sess); + // // Ensure the media is maintained + assert.isAbove(sess.media.length, 0); + var media = sess.media[0]; + assert.isUndefined(media.queueData); + assert.equal(media.media.metadata.title, mediaInfo.metadata.title); + assert.equal(media.media.metadata.subtitle, mediaInfo.metadata.subtitle); + assert.equal(media.media.metadata.releaseDate, mediaInfo.metadata.releaseDate); + // TODO figure out how to maintain the data types for custom params on the native side + // so that we don't have to do turn each actual and expected into a string + assert.equal(media.media.metadata.someTrueBoolean + '', mediaInfo.metadata.someTrueBoolean + ''); + assert.equal(media.media.metadata.someFalseBoolean + '', mediaInfo.metadata.someFalseBoolean + ''); + assert.equal(media.media.metadata.someSmallNumber + '', mediaInfo.metadata.someSmallNumber + ''); + assert.equal(media.media.metadata.someLargeNumber + '', mediaInfo.metadata.someLargeNumber + ''); + assert.equal(media.media.metadata.someSmallDecimal + '', mediaInfo.metadata.someSmallDecimal + ''); + assert.equal(media.media.metadata.someLargeDecimal + '', mediaInfo.metadata.someLargeDecimal + ''); + assert.equal(media.media.metadata.someString, mediaInfo.metadata.someString); + assert.equal(media.media.metadata.images[0].url, mediaInfo.metadata.images[0].url); + assert.equal(media.media.metadata.metadataType, chrome.cast.media.MetadataType.GENERIC); + assert.equal(media.media.metadata.type, chrome.cast.media.MetadataType.GENERIC); + assert.equal(media.playerState, chrome.cast.media.PlayerState.PLAYING); called(session_listener); }, function receiverListener (availability) { if (!finished) { From f57226a9dc3cd91507ea523d4d12156d5fb82f5c Mon Sep 17 00:00:00 2001 From: Lindsay-Needs-Sleep Date: Mon, 2 Dec 2019 00:50:29 -0700 Subject: [PATCH 139/166] (ios) [no change] remove unused function --- src/ios/CastUtilities.m | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/src/ios/CastUtilities.m b/src/ios/CastUtilities.m index 62ada1e..6ac6fce 100644 --- a/src/ios/CastUtilities.m +++ b/src/ios/CastUtilities.m @@ -507,9 +507,6 @@ + (NSDictionary *)createMediaObject:(GCKCastSession *)session { return @{}; } -// NSLog(@"stream position: %f", mediaStatus.streamPosition ); - - NSMutableArray *qItems = [[NSMutableArray alloc] init]; for (int i=0; i Date: Mon, 2 Dec 2019 01:09:37 -0700 Subject: [PATCH 140/166] (test) format the test homepage to look a bit nicer --- tests/www/chrome/tests_chrome.html | 28 +++++++++++++++------------- tests/www/html/tests.html | 28 +++++++++++++++------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/tests/www/chrome/tests_chrome.html b/tests/www/chrome/tests_chrome.html index 13196fa..ce3d172 100644 --- a/tests/www/chrome/tests_chrome.html +++ b/tests/www/chrome/tests_chrome.html @@ -15,28 +15,30 @@

      cordova-plugin-chromecast Tests

      -

      Auto Tests should be run (and passing) before attempting the manual tests.

      +

      Auto Tests should be run (and passing) before attempting the manual tests.

      -

      +


      Manual Tests (Primary) Part 1 is the entry point for manual tests.
      You will require 2 devices or 1 device and a desktop chrome browser.
      - (See readme for instructions on how to run tests from the desktop chrome browser.)
      + (See readme for instructions on how to run tests from the desktop chrome browser.)

      Click Manual Tests (Primary) Part 1 and follow the directions carefully.

      - - - - - - + -

      +


      Manual Tests (Primary) Part 1 is the entry point for manual tests.
      You will require 2 devices or 1 device and a desktop chrome browser.
      - (See readme for instructions on how to run tests from the desktop chrome browser.)
      + (See readme for instructions on how to run tests from the desktop chrome browser.)

      Click Manual Tests (Primary) Part 1 and follow the directions carefully.

      - - - - - - +