diff --git a/gulpfile.js b/gulpfile.js index 4f7c45a..643503e 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -129,6 +129,12 @@ gulp.task("test-minified", ["libjass.min.js"], function (callback) { }); }); +gulp.task("test-browser", ["libjass.min.js"], function (callback) { + npm.load(function () { + npm.commands["run-script"](["test-browser"], callback); + }); +}); + gulp.task("test", ["test-lib", "test-minified"]); gulp.task("demo", ["libjass.js"], function () { @@ -143,7 +149,7 @@ gulp.task("doc", ["libjass.js"], function () { .pipe(gulp.dest("../libjass-gh-pages/")); }); -gulp.task("dist", ["clean", "default", "test", "demo", "doc"], function () { +gulp.task("dist", ["clean", "default", "test", "test-browser", "demo", "doc"], function () { var inputFiles = [ "./README.md", "./CHANGELOG.md", "./LICENSE", "./lib/libjass.js", "./lib/libjass.js.map", diff --git a/package.json b/package.json index 244be67..a218cc5 100644 --- a/package.json +++ b/package.json @@ -19,12 +19,14 @@ "prepublish": "gulp clean default", "test": "gulp test", "test-lib": "intern-client config=./tests/intern reporters=pretty", - "test-minified": "intern-client config=./tests/intern reporters=pretty minified=true" + "test-minified": "intern-client config=./tests/intern reporters=pretty minified=true", + "test-browser": "intern-runner config=./tests/intern" }, "devDependencies": { "gulp": "3.x", "intern": "2.x", "npm": "2.x", + "pngjs": "0.4.0", "typescript": "1.5.0-alpha", "uglify-js": "2.x >=2.4.16", "vinyl": "latest" diff --git a/tests/functional/kfx/kfx-1.png b/tests/functional/kfx/kfx-1.png new file mode 100644 index 0000000..064c1d4 Binary files /dev/null and b/tests/functional/kfx/kfx-1.png differ diff --git a/tests/functional/kfx/kfx-2.png b/tests/functional/kfx/kfx-2.png new file mode 100644 index 0000000..e053b57 Binary files /dev/null and b/tests/functional/kfx/kfx-2.png differ diff --git a/tests/functional/kfx/kfx-3.png b/tests/functional/kfx/kfx-3.png new file mode 100644 index 0000000..e053b57 Binary files /dev/null and b/tests/functional/kfx/kfx-3.png differ diff --git a/tests/functional/kfx/kfx-4.png b/tests/functional/kfx/kfx-4.png new file mode 100644 index 0000000..c623d8c Binary files /dev/null and b/tests/functional/kfx/kfx-4.png differ diff --git a/tests/functional/kfx/kfx-5.png b/tests/functional/kfx/kfx-5.png new file mode 100644 index 0000000..a7b93ec Binary files /dev/null and b/tests/functional/kfx/kfx-5.png differ diff --git a/tests/functional/kfx/kfx-6.png b/tests/functional/kfx/kfx-6.png new file mode 100644 index 0000000..a7b93ec Binary files /dev/null and b/tests/functional/kfx/kfx-6.png differ diff --git a/tests/functional/kfx/kfx.ass b/tests/functional/kfx/kfx.ass new file mode 100644 index 0000000..2bb9c7a --- /dev/null +++ b/tests/functional/kfx/kfx.ass @@ -0,0 +1,18 @@ +[Script Info] +; Script generated by Aegisub 3.2.2 +; http://www.aegisub.org/ +Title: Default Aegisub file +ScriptType: v4.00+ +WrapStyle: 0 +ScaledBorderAndShadow: yes +YCbCr Matrix: None +PlayResX: 256 +PlayResY: 144 + +[V4+ Styles] +Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding +Style: Default,Arial,36,&H00FFFFFF,&H000000FF,&H00000000,&H96000000,0,0,0,0,100,100,0,0,1,2,1.2,2,15,15,15,1 + +[Events] +Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text +Dialogue: 0,0:00:01.00,0:00:07.00,Default,,0,0,0,,{\k100}{\k200}a{\k100}bc{\k200}d diff --git a/tests/functional/kfx/kfx.js b/tests/functional/kfx/kfx.js new file mode 100644 index 0000000..dce303f --- /dev/null +++ b/tests/functional/kfx/kfx.js @@ -0,0 +1,35 @@ +/** + * libjass + * + * https://github.com/Arnavion/libjass + * + * Copyright 2013 Arnav Singh + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +define(["intern!tdd", "require", "tests/support/test-page"], function (tdd, require, TestPage) { + tdd.suite("WebDriver", function () { + tdd.test("Basic", function () { + var testPage = new TestPage(this.remote, require.toUrl("tests/support/browser-test-page.html"), "/tests/functional/kfx/kfx.ass", 256, 144); + return testPage + .prepare() + .then(function (testPage) { return testPage.seekAndCompareScreenshot(1.5, require.toUrl("./kfx-1.png")); }) + .then(function (testPage) { return testPage.seekAndCompareScreenshot(2.5, require.toUrl("./kfx-2.png")); }) + .then(function (testPage) { return testPage.seekAndCompareScreenshot(3.5, require.toUrl("./kfx-3.png")); }) + .then(function (testPage) { return testPage.seekAndCompareScreenshot(4.5, require.toUrl("./kfx-4.png")); }) + .then(function (testPage) { return testPage.seekAndCompareScreenshot(5.5, require.toUrl("./kfx-5.png")); }) + .then(function (testPage) { return testPage.seekAndCompareScreenshot(6.5, require.toUrl("./kfx-6.png")); }); + }); + }); +}); diff --git a/tests/intern.js b/tests/intern.js index 3d50f06..634a6bb 100644 --- a/tests/intern.js +++ b/tests/intern.js @@ -28,7 +28,14 @@ define(["intern"], function (intern) { "tests/unit/tags", "tests/unit/webworker" ], + functionalSuites: [ + "tests/functional/kfx/kfx" + ], excludeInstrumentation: /^(?:tests|node_modules)[/\\]/, + tunnel: "NullTunnel", + environments: [{ + browserName: "chrome", + }], }; if (intern.args.minified === "true") { diff --git a/tests/support/browser-test-page.css b/tests/support/browser-test-page.css new file mode 100644 index 0000000..0247eac --- /dev/null +++ b/tests/support/browser-test-page.css @@ -0,0 +1,25 @@ +/** + * libjass + * + * https://github.com/Arnavion/libjass + * + * Copyright 2013 Arnav Singh + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +body { + margin: 0; + padding: 0; + box-sizing: border-box; +} diff --git a/tests/support/browser-test-page.html b/tests/support/browser-test-page.html new file mode 100644 index 0000000..6619a9e --- /dev/null +++ b/tests/support/browser-test-page.html @@ -0,0 +1,12 @@ + + + + >2012 >Streaman Animu + + + + + +
+ + diff --git a/tests/support/test-page.js b/tests/support/test-page.js new file mode 100644 index 0000000..929b1d9 --- /dev/null +++ b/tests/support/test-page.js @@ -0,0 +1,113 @@ +/** + * libjass + * + * https://github.com/Arnavion/libjass + * + * Copyright 2013 Arnav Singh + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +define([ + "intern/dojo/node!fs", + "intern/dojo/node!path", + "intern/dojo/node!pngjs", + "intern/chai!assert", + "intern/dojo/node!intern/node_modules/leadfoot/helpers/pollUntil", + "lib/libjass" +], function (fs, path, pngjs, assert, pollUntil, libjass) { + function TestPage(remote, pageUrl, assUrl, targetWidth, targetHeight) { + this._remote = remote; + this._pageUrl = pageUrl; + this._assUrl = assUrl; + this._targetWidth = targetWidth; + this._targetHeight = targetHeight; + } + + TestPage.prototype.prepare = function () { + var _this = this; + + this._remote.session.setExecuteAsyncTimeout(10000); + + return this._remote + .setWindowSize(1280, 720) + .get(this._pageUrl) + .then(pollUntil('return (document.readyState === "complete") ? true : null;'), 100) + .executeAsync(function (assUrl, callback) { + window.clock = new libjass.renderers.ManualClock(); + var libjassSubsWrapper = document.querySelector(".libjass-wrapper"); + + libjass.ASS.fromUrl(assUrl).then(function (ass) { + libjassSubsWrapper.style.width = ass.properties.resolutionX + "px"; + libjassSubsWrapper.style.height = ass.properties.resolutionY + "px"; + + var renderer = new libjass.renderers.WebRenderer(ass, clock, libjassSubsWrapper); + renderer.addEventListener("ready", function () { + renderer.resize(ass.properties.resolutionX, ass.properties.resolutionY); + clock.pause(); + callback(); + }); + }); + }, [this._assUrl]) + .then(function () { return _this; }); + }; + + TestPage.prototype.seekAndCompareScreenshot = function (time, filename) { + var _this = this; + + return this._remote + .execute(function (time) { clock.seek(time); }, [time]) + .takeScreenshot() + .then(function (buffer) { + return new libjass.Promise(function (resolve, reject) { + new pngjs.PNG().parse(buffer, function (error, input) { + if (error !== null) { + reject(error); + return; + } + + var output = new pngjs.PNG({ width: _this._targetWidth, height: _this._targetHeight }); + input.bitblt(output, 0, 0, _this._targetWidth, _this._targetHeight, 0, 0); + resolve(output); + }); + }); + }) + .then(function (screenshot) { + return new libjass.Promise(function (resolve, reject) { + var baselineBuffer = fs.readFileSync(filename); + new pngjs.PNG().parse(baselineBuffer, function (error, baseline) { + if (error !== null) { + reject(error); + return; + } + + try { + assert.equal(0, screenshot.data.compare(baseline.data), filename + " does not match!"); + resolve(); + } + catch (ex) { + reject(ex); + } + }); + }).catch(function (err) { + return new libjass.Promise(function (resolve, reject) { + var newFilename = filename.substr(0, filename.length - path.extname(filename).length) + "-new" + path.extname(filename); + screenshot.pack().pipe(fs.createWriteStream(newFilename)).once("error", reject).on("finish", function () { reject(err); }); + }); + }); + }) + .then(function () { return _this; }); + }; + + return TestPage; +});