From 537e751abe736628e730571e86415584be70574d Mon Sep 17 00:00:00 2001 From: Moriel Schottlender Date: Tue, 24 Jun 2014 13:15:48 -0400 Subject: [PATCH] MWImageModel unit tests Change-Id: I87d1383b383e2a7cd6cb38ed51710cc36a59f63e --- VisualEditor.hooks.php | 1 + modules/ve-mw/dm/nodes/ve.dm.MWImageNode.js | 2 +- .../ve-mw/test/dm/ve.dm.MWImageModel.test.js | 230 ++++++++++++++++++ 3 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 modules/ve-mw/test/dm/ve.dm.MWImageModel.test.js diff --git a/VisualEditor.hooks.php b/VisualEditor.hooks.php index 1ebfdfaddd..9db225efa6 100644 --- a/VisualEditor.hooks.php +++ b/VisualEditor.hooks.php @@ -571,6 +571,7 @@ class VisualEditorHooks { 'lib/ve/modules/ve/test/dm/lineardata/ve.dm.MetaLinearData.test.js', 'modules/ve-mw/test/dm/ve.dm.mwExample.js', 'modules/ve-mw/test/dm/ve.dm.Converter.test.js', + 'modules/ve-mw/test/dm/ve.dm.MWImageModel.test.js', // VisualEditor ContentEditable Tests 'lib/ve/modules/ve/test/ce/ve.ce.test.js', 'lib/ve/modules/ve/test/ce/ve.ce.Document.test.js', diff --git a/modules/ve-mw/dm/nodes/ve.dm.MWImageNode.js b/modules/ve-mw/dm/nodes/ve.dm.MWImageNode.js index 152a378060..83201dc86f 100644 --- a/modules/ve-mw/dm/nodes/ve.dm.MWImageNode.js +++ b/modules/ve-mw/dm/nodes/ve.dm.MWImageNode.js @@ -166,7 +166,7 @@ ve.dm.MWImageNode.prototype.getScalable = function () { ve.dm.MWImageNode.prototype.getScalablePromise = function () { // On the first call set off an async call to update the scalable's // original dimensions from the API. - if ( !this.scalablePromise ) { + if ( ve.init.target && !this.scalablePromise ) { this.scalablePromise = ve.init.target.constructor.static.apiRequest( { 'action': 'query', diff --git a/modules/ve-mw/test/dm/ve.dm.MWImageModel.test.js b/modules/ve-mw/test/dm/ve.dm.MWImageModel.test.js new file mode 100644 index 0000000000..d5f6660d79 --- /dev/null +++ b/modules/ve-mw/test/dm/ve.dm.MWImageModel.test.js @@ -0,0 +1,230 @@ +/*! + * VisualEditor DataModel MWImageModel tests. + * + * @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt + * @license The MIT License (MIT); see LICENSE.txt + */ + +QUnit.module( 've.dm.MWImageModel' ); + +/* Tests */ + +QUnit.test( 'Create and manipulate image nodes', function ( assert ) { + var i, imageType, imageModel, value, result, expected, expectedAlignment, method, dir, + expect = 0, + imageNode = {}, + images = { + 'mwInlineImage': { + 'dir': 'ltr', + 'attrs': { + 'type': 'frameless', + 'href': './File:Foo.jpg', + 'src': 'http://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Foo.jpg/229px-Foo.jpg', + 'resource': './File:Foo.jpg', + 'align': 'left', + 'width': '100', + 'height': '150', + 'mediaType': 'BITMAP', + 'defaultSize': true + }, + 'tests': [ + { + 'methods': { + 'setType': 'thumb', + 'setAlignment': 'default' + }, + 'results': { + 'expect': { + 'getImageNodeType': 'mwBlockImage', + 'isBorderable': false + }, + 'position': 'oppositeToText' + } + } + ] + }, + 'mwBlockImage': { + 'dir': 'rtl', + 'attrs': { + 'href': './File:Foo.jpg', + 'src': 'http://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Foo.jpg/229px-Foo.jpg', + 'resource': './File:Foo.jpg', + 'type': 'thumb', + 'align': 'default', + 'width': '100', + 'height': '150', + 'mediaType': 'DRAWING', + 'defaultSize': false + }, + 'tests': [ + { + 'methods': { + 'setType': 'frameless', + 'setAlignment': 'none' + }, + 'results': { + 'expect': { + 'getImageNodeType': 'mwInlineImage', + 'getAlignment': 'none' + }, + 'position': 'none' + } + }, + { + 'methods': { + 'setType': 'none', + 'setAlignment': 'default', + 'toggleBorder': true + }, + 'results': { + 'expect': { + 'getImageNodeType': 'mwInlineImage', + 'isBorderable': true, + 'isDefaultSize': false + }, + 'position': 'none' + } + }, + { + 'methods': { + 'setType': 'frame', + 'setAlignment': 'default', + 'toggleDefaultSize': true + }, + 'results': { + 'expect': { + 'getImageNodeType': 'mwBlockImage', + 'isBorderable': false, + 'isDefaultSize': true + }, + 'position': 'oppositeToText' + } + }, + { + 'methods': { + 'setType': 'frame', + 'setAlignment': 'right' + }, + 'results': { + 'expect': { + 'getImageNodeType': 'mwBlockImage' + }, + 'position': 'withText' + } + } + ] + } + }; + + for ( imageType in images ) { + // Type test + expect++; + for ( i = 0; i < images[imageType].tests.length; i++ ) { + // Method tests + expect += Object.keys( images[imageType].tests[i].results.expect ).length; + if ( images[imageType].tests[i].results.position ) { + // Position test + expect++; + } + } + } + + QUnit.expect( expect ); + + for ( imageType in images ) { + // Create the node + imageNode[imageType] = new ve.dm.MWImageModel.static.createImageNode( + images[imageType].attrs, + imageType + ); + + // Verify node type + assert.strictEqual( + imageNode[imageType].type, + imageType, + 'createImageNode node type: ' + imageType + ); + + // Run tests + for ( i = 0; i < images[imageType].tests.length; i++ ) { + dir = images[imageType].dir; + + // Start from original details + imageModel = new ve.dm.MWImageModel.static.newFromImageNode( imageNode[imageType], dir ); + + // Run attributes + for ( method in images[imageType].tests[i].methods ) { + value = images[imageType].tests[i].methods[method]; + imageModel[method]( value ); + } + + // Check result + for ( method in images[imageType].tests[i].results.expect ) { + result = imageModel[method](); + expected = images[imageType].tests[i].results.expect[method]; + assert.deepEqual( + result, + expected, + method + ); + } + + // Run language-specific tests + if ( images[imageType].tests[i].results.position ) { + // Definition: + // * withText (document direction; 'left' for ltr, 'right' for rtl) + // * oppositeToText (opposite the document direction; 'right' for ltr, 'left' for rtl) + // * none (no alignment set) + switch ( images[imageType].tests[i].results.position ) { + case 'withText': + expectedAlignment = dir === 'ltr' ? 'left' : 'right'; + break; + case 'oppositeToText': + expectedAlignment = dir === 'ltr' ? 'right' : 'left'; + break; + case 'none': + expectedAlignment = 'none'; + break; + } + + assert.equal( + imageModel.getAlignment(), + expectedAlignment, + 'getAlignment' + ); + } + } + } +} ); + +// TODO: ve.dm.MWImageModel#updateImageNode +// TODO: ve.dm.MWImageModel#insertImageNode +// TODO: ve.dm.MWImageModel#getUpdatedAttributes +// TODO: ve.dm.MWImageModel#onScalableDefaultSizeChange +// TODO: ve.dm.MWImageModel#setMediaNode +// TODO: ve.dm.MWImageModel#getMediaNode +// TODO: ve.dm.MWImageModel#hasBorder +// TODO: ve.dm.MWImageModel#isAligned +// TODO: ve.dm.MWImageModel#isDefaultAligned +// TODO: ve.dm.MWImageModel#getAltText +// TODO: ve.dm.MWImageModel#getType +// TODO: ve.dm.MWImageModel#getSizeType +// TODO: ve.dm.MWImageModel#getMediaType +// TODO: ve.dm.MWImageModel#getVerticalAlignment +// TODO: ve.dm.MWImageModel#getScalable +// TODO: ve.dm.MWImageModel#getCurrentDimensions +// TODO: ve.dm.MWImageModel#getCaptionDocument +// TODO: ve.dm.MWImageModel#toggleBorderable +// TODO: ve.dm.MWImageModel#cacheOriginalImageAttributes +// TODO: ve.dm.MWImageModel#getOriginalImageAttributes +// TODO: ve.dm.MWImageModel#setCurrentDimensions +// TODO: ve.dm.MWImageModel#setAltText +// TODO: ve.dm.MWImageModel#resetDefaultDimensions +// TODO: ve.dm.MWImageModel#getDefaultDimensions +// TODO: ve.dm.MWImageModel#setSizeType +// TODO: ve.dm.MWImageModel#setVerticalAlignment +// TODO: ve.dm.MWImageModel#getDefaultDir +// TODO: ve.dm.MWImageModel#getDir +// TODO: ve.dm.MWImageModel#setDir +// TODO: ve.dm.MWImageModel#setScalable +// TODO: ve.dm.MWImageModel#setCaptionDocument