]> git.proxmox.com Git - mirror_xterm.js.git/commitdiff
Merge pull request #535 from Tyriar/534_phantomjs
authorDaniel Imms <tyriar@tyriar.com>
Wed, 15 Feb 2017 21:29:50 +0000 (13:29 -0800)
committerGitHub <noreply@github.com>
Wed, 15 Feb 2017 21:29:50 +0000 (13:29 -0800)
Add PhantomJS test support and CharMeasure tests

gulpfile.js
package.json
src/Interfaces.ts
src/utils/CharMeasure.phantom.ts [new file with mode: 0644]
src/xterm.js
test-harness.html [new file with mode: 0644]

index ff4a74382c2b1d31394e9208d166285aa885b6e6..8aa86b115979fd9dc81f72009e243e6baf33807e 100644 (file)
@@ -3,6 +3,8 @@ const buffer = require('vinyl-buffer');
 const fs = require('fs-extra');
 const gulp = require('gulp');
 const merge = require('merge-stream');
+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');
@@ -68,6 +70,15 @@ gulp.task('browserify', ['tsc'], function() {
   return merge(bundleStream, copyAddons, copyStylesheets);
 });
 
+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(mochaPhantomJs());
+});
 
 /**
  * Use `sorcery` to resolve the source map chain and point back to the TypeScript files.
@@ -81,5 +92,5 @@ gulp.task('sorcery', ['browserify'], function () {
 });
 
 gulp.task('build', ['sorcery']);
-
+gulp.task('test', ['test-mocha', 'test-mocha-phantomjs']);
 gulp.task('default', ['build']);
index c6e6f429a0a845e6a2e312840bf1bc64f714719b..06a7492b97e4cd5e231da20e2a93c12447beda3c 100644 (file)
     "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",
@@ -66,7 +67,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"
index c876d256b8f244aaeb844d88ff188de2f753ecbc..92094c9553a820042d8a16bb0d7d8e5292adab45 100644 (file)
@@ -44,6 +44,12 @@ export interface ITerminal {
   emit(event: string, data: any);
 }
 
+export interface ICharMeasure {
+  width: number;
+  height: number;
+  measure(): void;
+}
+
 interface ICircularList<T> {
   length: number;
   maxLength: number;
diff --git a/src/utils/CharMeasure.phantom.ts b/src/utils/CharMeasure.phantom.ts
new file mode 100644 (file)
index 0000000..f8173dc
--- /dev/null
@@ -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 = (<any>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);
+      });
+    });
+  });
+
+}
index 314a76d0eb8822b3228af575022982e81f95d962..f76c01b5941482833ad7e2d58849d12ddeabbee2 100644 (file)
@@ -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 (file)
index 0000000..8c5f0b3
--- /dev/null
@@ -0,0 +1,22 @@
+<html>
+  <head>
+    <meta charset="utf-8">
+    <!-- encoding must be set for mocha's special characters to render properly -->
+    <link rel="stylesheet" href="node_modules/mocha/mocha.css" />
+  </head>
+  <body>
+    <div id="mocha"></div>
+    <div id="xterm"></div>
+    <script src="node_modules/mocha/mocha.js"></script>
+    <script src="node_modules/chai/chai.js"></script>
+    <script>
+      mocha.ui('bdd')
+      assert = chai.assert
+    </script>
+    <script src="build/xterm.js"></script>
+    <script src="lib/utils/CharMeasure.phantom.js"></script>
+    <script>
+      mocha.run()
+    </script>
+  </body>
+</html>