From 1306431e7c3883e80f29707eeb327693ccf5e306 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 8 Feb 2017 19:53:32 -0800 Subject: [PATCH 1/2] Add PhantomJS test support and CharMeasure tests Fixes #534 --- gulpfile.js | 5 +++ package.json | 1 + src/Interfaces.ts | 6 +++ src/utils/CharMeasure.phantom.ts | 65 ++++++++++++++++++++++++++++++++ src/xterm.js | 3 ++ test-harness.html | 22 +++++++++++ 6 files changed, 102 insertions(+) create mode 100644 src/utils/CharMeasure.phantom.ts create mode 100644 test-harness.html diff --git a/gulpfile.js b/gulpfile.js index 144b8831..2aaf21ac 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -3,6 +3,7 @@ const buffer = require('vinyl-buffer'); const fs = require('fs-extra'); const gulp = require('gulp'); const merge = require('merge-stream'); +const mochaPhantom = require('gulp-mocha-phantomjs'); const sorcery = require('sorcery'); const source = require('vinyl-source-stream'); const sourcemaps = require('gulp-sourcemaps'); @@ -70,6 +71,10 @@ gulp.task('browserify', ['tsc'], function() { return merge(bundleStream, copyAddons, copyStylesheets); }); +gulp.task('test-phantom', function () { + return gulp.src('test-harness.html') + .pipe(mochaPhantom()); +}); /** * Use `sorcery` to resolve the source map chain and point back to the TypeScript files. diff --git a/package.json b/package.json index 3e8a8d55..8e766d2f 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "glob": "^7.0.5", "gulp": "^3.9.1", "gulp-cli": "^1.2.2", + "gulp-mocha-phantomjs": "^0.12.0", "gulp-sourcemaps": "1.9.1", "gulp-typescript": "^3.1.3", "jsdoc": "3.4.3", diff --git a/src/Interfaces.ts b/src/Interfaces.ts index c876d256..92094c95 100644 --- a/src/Interfaces.ts +++ b/src/Interfaces.ts @@ -44,6 +44,12 @@ export interface ITerminal { emit(event: string, data: any); } +export interface ICharMeasure { + width: number; + height: number; + measure(): void; +} + interface ICircularList { length: number; maxLength: number; diff --git a/src/utils/CharMeasure.phantom.ts b/src/utils/CharMeasure.phantom.ts new file mode 100644 index 00000000..f8173dc6 --- /dev/null +++ b/src/utils/CharMeasure.phantom.ts @@ -0,0 +1,65 @@ +/** + * @license MIT + */ +import { ICharMeasure, ITerminal } from '../Interfaces'; + +declare var assert: Chai.Assert; +declare var Terminal: ITerminal; + +// Do not describe tests unless in PhantomJS environment +if (typeof Terminal !== 'undefined') { + + const CharMeasure = (Terminal).CharMeasure; + + describe('CharMeasure', () => { + const parentElement = document.createElement('div'); + let charMeasure: ICharMeasure; + + beforeEach(() => { + charMeasure = new CharMeasure(parentElement); + document.querySelector('#xterm').appendChild(parentElement); + }); + + afterEach(() => { + if (parentElement && parentElement.parentElement) { + parentElement.parentElement.removeChild(parentElement); + } + }); + + describe('measure', () => { + it('should be performed async on first call', done => { + assert.equal(charMeasure.width, null); + charMeasure.measure(); + assert.equal(charMeasure.width, null); + setTimeout(() => { + assert.isTrue(charMeasure.width > 0); + done(); + }, 0); + }); + + it('should be performed sync on successive calls', done => { + charMeasure.measure(); + setTimeout(() => { + const firstWidth = charMeasure.width; + parentElement.style.fontSize = '2em'; + charMeasure.measure(); + assert.equal(charMeasure.width, firstWidth * 2); + done(); + }, 0); + }); + + it('should NOT do a measure when the parent is hidden', done => { + charMeasure.measure(); + setTimeout(() => { + const firstWidth = charMeasure.width; + parentElement.style.display = 'none'; + parentElement.style.fontSize = '2em'; + charMeasure.measure(); + assert.equal(charMeasure.width, firstWidth); + done(); + }, 0); + }); + }); + }); + +} diff --git a/src/xterm.js b/src/xterm.js index 314a76d0..f76c01b5 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -2237,6 +2237,9 @@ function keys(obj) { Terminal.EventEmitter = EventEmitter; Terminal.inherits = inherits; +// Expose for Phantom.JS tests +Terminal.CharMeasure = CharMeasure; + /** * Adds an event listener to the terminal. * diff --git a/test-harness.html b/test-harness.html new file mode 100644 index 00000000..8c5f0b30 --- /dev/null +++ b/test-harness.html @@ -0,0 +1,22 @@ + + + + + + + +
+
+ + + + + + + + From e8adf8ad2b080ca6e9baff3a4371648cec74300e Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 8 Feb 2017 20:06:14 -0800 Subject: [PATCH 2/2] Use gulp-mocha and attach run both test type in npm test --- gulpfile.js | 14 ++++++++++---- package.json | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 2aaf21ac..f961b699 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -3,7 +3,8 @@ const buffer = require('vinyl-buffer'); const fs = require('fs-extra'); const gulp = require('gulp'); const merge = require('merge-stream'); -const mochaPhantom = require('gulp-mocha-phantomjs'); +const mocha = require('gulp-mocha'); +const mochaPhantomJs = require('gulp-mocha-phantomjs'); const sorcery = require('sorcery'); const source = require('vinyl-source-stream'); const sourcemaps = require('gulp-sourcemaps'); @@ -71,9 +72,14 @@ gulp.task('browserify', ['tsc'], function() { return merge(bundleStream, copyAddons, copyStylesheets); }); -gulp.task('test-phantom', function () { +gulp.task('test-mocha', function () { + return gulp.src(['lib/*test.js', 'lib/**/*test.js'], {read: false}) + .pipe(mocha()) +}); + +gulp.task('test-mocha-phantomjs', function () { return gulp.src('test-harness.html') - .pipe(mochaPhantom()); + .pipe(mochaPhantomJs()); }); /** @@ -88,5 +94,5 @@ gulp.task('sorcery', ['browserify'], function () { }); gulp.task('build', ['sorcery']); - +gulp.task('test', ['test-mocha', 'test-mocha-phantomjs']); gulp.task('default', ['build']); diff --git a/package.json b/package.json index 8e766d2f..c95de73b 100644 --- a/package.json +++ b/package.json @@ -47,12 +47,12 @@ "glob": "^7.0.5", "gulp": "^3.9.1", "gulp-cli": "^1.2.2", + "gulp-mocha": "^3.0.1", "gulp-mocha-phantomjs": "^0.12.0", "gulp-sourcemaps": "1.9.1", "gulp-typescript": "^3.1.3", "jsdoc": "3.4.3", "merge-stream": "^1.0.1", - "mocha": "2.5.3", "node-pty": "^0.4.1", "nodemon": "1.10.2", "sleep": "^3.0.1", @@ -68,7 +68,7 @@ "start": "node demo/app", "dev": "nodemon -e js,ts --watch src --watch demo --exec npm start", "lint": "tslint src/*.ts src/**/*.ts", - "test": "mocha --recursive ./lib", + "test": "gulp test", "build:docs": "jsdoc -c jsdoc.json", "build": "gulp build", "prepublish": "npm run build"