Skip to content

Commit

Permalink
Tests: unit tests refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
f1ames committed Feb 5, 2018
1 parent bc3293f commit d6ab2f2
Showing 1 changed file with 163 additions and 136 deletions.
299 changes: 163 additions & 136 deletions tests/plugins/button/buttonicon.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,119 @@
/* bender-tags: editor */
/* bender-ckeditor-plugins: button,toolbar */

var editorCounter = 0,
originalBasePath = null,
isDev = CKEDITOR.version === '%VERSION%',
isHidpi;

function resolutionStandard() {
CKEDITOR.env.hidpi = false;
}

function resolutionHidpi() {
CKEDITOR.env.hidpi = true;
}

function assertIcon( editorConfig, btnName, iconPath ) {
editorCounter++;
bender.editorBot.create( {
name: 'editor' + editorCounter,
config: editorConfig
}, function( bot ) {
var btn = bot.editor.ui.get( btnName ),
btnEl = CKEDITOR.document.getById( btn._.id ),
btnCss = CKEDITOR.tools.parseCssText( btnEl.findOne( '.cke_button_icon' ).getAttribute( 'style' ), true );
( function() {
'use strict';

var editorCounter = 0,
originalBasePath = null,
isHidpi,
tests;

function createIconTests( testsObj, tests ) {
CKEDITOR.tools.array.forEach( tests, function( test ) {
testsObj[ test.name ] = function() {
CKEDITOR.env.hidpi = test.name.indexOf( 'hidpi' ) !== -1;
assertIcon( test.config, test.button, test.iconPath, test.iconName );
};
} );
}

assert.isMatching( iconPath, btnCss[ 'background-image' ] );
} );
}
function assertIcon( editorConfig, btnName, iconPath, iconName ) {
editorCounter++;
bender.editorBot.create( {
name: 'editor' + editorCounter,
config: editorConfig
}, function( bot ) {
var btn = bot.editor.ui.get( btnName ),
btnEl = CKEDITOR.document.getById( btn._.id ),
btnCss = CKEDITOR.tools.parseCssText( btnEl.findOne( '.cke_button_icon' ).getAttribute( 'style' ), true );

if ( iconPath === undefined ) {
// Standard icon should be checked.
if ( CKEDITOR.version === '%VERSION%' ) {
var iconFileName = ( iconName || btnName ).toLowerCase(),
hidpi = CKEDITOR.env.hidpi ? 'hidpi\\/' : '';

iconPath = new RegExp( 'plugins\\/' + iconFileName + '\\/icons\\/' + hidpi + iconFileName + '\\.png', 'gi' );
} else {
// In build version all standard icons are inside 'icons.png' sprite.
iconPath = CKEDITOR.env.hidpi ? /plugins\/icons_hidpi\.png/gi : /plugins\/icons\.png/gi;
}
}

bender.test( {
init: function() {
isHidpi = CKEDITOR.env.hidpi;
},
assert.isMatching( iconPath, btnCss[ 'background-image' ] );
} );
}

tearDown: function() {
// Restore global values modified by tests.
CKEDITOR.env.hidpi = isHidpi;
if ( originalBasePath ) {
CKEDITOR.basePath = originalBasePath;
originalBasePath = null;
tests = {
init: function() {
isHidpi = CKEDITOR.env.hidpi;
},

tearDown: function() {
// Restore global values modified by tests.
CKEDITOR.env.hidpi = isHidpi;
if ( originalBasePath ) {
CKEDITOR.basePath = originalBasePath;
originalBasePath = null;
}
}
},

'test default button icon': function() {
resolutionStandard();
assertIcon(
{ extraPlugins: 'link' },
'Link',
isDev ? /plugins\/link\/icons\/link\.png/gi : /plugins\/icons\.png/gi
);
},

'test default button icon (hidpi)': function() {
resolutionHidpi();
assertIcon(
{ extraPlugins: 'find' },
'Find',
isDev ? /plugins\/find\/icons\/hidpi\/find\.png/gi : /plugins\/icons\.png/gi
);
},

'test button icon from different plugin': function() {
resolutionStandard();
assertIcon( {
};

createIconTests( tests, [
{
name: 'test default button icon',
button: 'Link',
config: {
extraPlugins: 'link'
}
}, {
name: 'test default button icon (hidpi)',
button: 'Find',
config: {
extraPlugins: 'find'
}
}, {
name: 'test overwriting default button icon',
button: 'Replace',
iconPath: /tests\/_assets\/sample_icon\.png/gi,
config: {
extraPlugins: 'find',
toolbar: [ [ 'Replace' ] ],
on: {
pluginsLoaded: function( evt ) {
var editor = evt.editor;
editor.ui.addButton( 'Replace', {
label: editor.lang.find.replace,
command: 'replace',
icon: 'tests/_assets/sample_icon.png'
} );
}
}
}
}, {
name: 'test overwriting default button icon (hidpi)',
button: 'Replace',
iconPath: /tests\/_assets\/sample_icon\.hidpi\.png/gi,
config: {
extraPlugins: 'find',
toolbar: [ [ 'Replace' ] ],
on: {
pluginsLoaded: function( evt ) {
var editor = evt.editor;
editor.ui.addButton( 'Replace', {
label: editor.lang.find.replace,
command: 'replace',
iconHiDpi: 'tests/_assets/sample_icon.hidpi.png'
} );
}
}
}
}, {
name: 'test button icon from different plugin',
button: 'custom_btn1',
iconName: 'about',
config: {
extraPlugins: 'about',
toolbar: [ [ 'custom_btn1' ] ],
on: {
Expand All @@ -72,15 +123,12 @@ bender.test( {
} );
}
}
},
'custom_btn1',
isDev ? /plugins\/about\/icons\/about\.png/gi : /plugins\/icons\.png/gi
);
},

'test button icon from different plugin (hidpi)': function() {
resolutionHidpi();
assertIcon( {
}
}, {
name: 'test button icon from different plugin (hidpi)',
button: 'custom_btn2',
iconName: 'blockquote',
config: {
extraPlugins: 'blockquote',
toolbar: [ [ 'custom_btn2' ] ],
on: {
Expand All @@ -90,15 +138,12 @@ bender.test( {
} );
}
}
},
'custom_btn2',
isDev ? /plugins\/blockquote\/icons\/hidpi\/blockquote\.png/gi : /plugins\/icons\.png/gi
);
},

'test custom button icon': function() {
resolutionStandard();
assertIcon( {
}
}, {
name: 'test custom button icon',
button: 'custom_btn3',
iconPath: /tests\/_assets\/sample_icon\.png/gi,
config: {
toolbar: [ [ 'custom_btn3' ] ],
on: {
pluginsLoaded: function( evt ) {
Expand All @@ -107,15 +152,12 @@ bender.test( {
} );
}
}
},
'custom_btn3',
/tests\/_assets\/sample_icon\.png/gi
);
},

'test custom button icon (hidpi)': function() {
resolutionHidpi();
assertIcon( {
}
}, {
name: 'test custom button icon (hidpi)',
button: 'custom_btn4',
iconPath: /tests\/_assets\/sample_icon\.hidpi\.png/gi,
config: {
toolbar: [ [ 'custom_btn4' ] ],
on: {
pluginsLoaded: function( evt ) {
Expand All @@ -124,15 +166,12 @@ bender.test( {
} );
}
}
},
'custom_btn4',
/tests\/_assets\/sample_icon\.hidpi\.png/gi
);
},

'test custom button icon-only (hidpi)': function() {
resolutionHidpi();
assertIcon( {
}
}, {
name: 'test custom button icon-only (hidpi)',
button: 'custom_btn5',
iconPath: /tests\/_assets\/sample_icon\.png/gi,
config: {
toolbar: [ [ 'custom_btn5' ] ],
on: {
pluginsLoaded: function( evt ) {
Expand All @@ -141,15 +180,12 @@ bender.test( {
} );
}
}
},
'custom_btn5',
/tests\/_assets\/sample_icon\.png/gi
);
},

'test custom button icon with different basepath': function() {
resolutionStandard();
assertIcon( {
}
}, {
name: 'test custom button icon with different basepath',
button: 'custom_btn6',
iconPath: /different\/basepath\/assets\/icons\.sample\.png/gi,
config: {
toolbar: [ [ 'custom_btn6' ] ],
on: {
pluginsLoaded: function( evt ) {
Expand All @@ -160,15 +196,12 @@ bender.test( {
} );
}
}
},
'custom_btn6',
/different\/basepath\/assets\/icons\.sample\.png/gi
);
},

'test custom button icon with different basepath (hidpi)': function() {
resolutionHidpi();
assertIcon( {
}
}, {
name: 'test custom button icon with different basepath (hidpi)',
button: 'custom_btn7',
iconPath: /different\/basepath\/assets\/hidpi\/icons\.sample\.png/gi,
config: {
toolbar: [ [ 'custom_btn7' ] ],
on: {
pluginsLoaded: function( evt ) {
Expand All @@ -179,15 +212,12 @@ bender.test( {
} );
}
}
},
'custom_btn7',
/different\/basepath\/assets\/hidpi\/icons\.sample\.png/gi
);
},

'test custom button icon with different basepath trailing slash': function() {
resolutionStandard();
assertIcon( {
}
}, {
name: 'test custom button icon with different basepath trailing slash',
button: 'custom_btn8',
iconPath: /['|(]\/assets\/icons\.sample\.png/gi,
config: {
toolbar: [ [ 'custom_btn8' ] ],
on: {
pluginsLoaded: function( evt ) {
Expand All @@ -198,15 +228,12 @@ bender.test( {
} );
}
}
},
'custom_btn8',
/['|(]\/assets\/icons\.sample\.png/gi
);
},

'test custom button icon with different basepath trailing slash (hidpi)': function() {
resolutionHidpi();
assertIcon( {
}
}, {
name: 'test custom button icon with different basepath trailing slash (hidpi)',
button: 'custom_btn9',
iconPath: /['|(]\/assets\/hidpi\/icons\.sample\.png/gi,
config: {
toolbar: [ [ 'custom_btn9' ] ],
on: {
pluginsLoaded: function( evt ) {
Expand All @@ -217,9 +244,9 @@ bender.test( {
} );
}
}
},
'custom_btn9',
/['|(]\/assets\/hidpi\/icons\.sample\.png/gi
);
}
} );
}
}
] );

bender.test( tests );
} )();

0 comments on commit d6ab2f2

Please sign in to comment.