Review of unit test suites
[mediawiki.git] / tests / qunit / suites / resources / jquery / jquery.colorUtil.js
blob01965a2f6cda01ac69994c3ab64b7deee891ff6c
1 module( 'jquery.colorUtil.js' );
3 test( '-- Initial check', function() {
4         expect(1);
5         ok( $.colorUtil, '$.colorUtil defined' );
6 });
8 test( 'getRGB', function() {
9         expect(18);
11         strictEqual( $.colorUtil.getRGB(), undefined, 'No arguments' );
12         strictEqual( $.colorUtil.getRGB( '' ), undefined, 'Empty string' );
13         deepEqual( $.colorUtil.getRGB( [0, 100, 255] ), [0, 100, 255], 'Parse array of rgb values' );
14         deepEqual( $.colorUtil.getRGB( 'rgb(0,100,255)' ), [0, 100, 255], 'Parse simple rgb string' );
15         deepEqual( $.colorUtil.getRGB( 'rgb(0, 100, 255)' ), [0, 100, 255], 'Parse simple rgb string with spaces' );
16         deepEqual( $.colorUtil.getRGB( 'rgb(0%,20%,40%)' ), [0, 51, 102], 'Parse rgb string with percentages' );
17         deepEqual( $.colorUtil.getRGB( 'rgb(0%, 20%, 40%)' ), [0, 51, 102], 'Parse rgb string with percentages and spaces' );
18         deepEqual( $.colorUtil.getRGB( '#f2ddee' ), [242, 221, 238], 'Hex string: 6 char lowercase' );
19         deepEqual( $.colorUtil.getRGB( '#f2DDEE' ), [242, 221, 238], 'Hex string: 6 char uppercase' );
20         deepEqual( $.colorUtil.getRGB( '#f2DdEe' ), [242, 221, 238], 'Hex string: 6 char mixed' );
21         deepEqual( $.colorUtil.getRGB( '#eee' ), [238, 238, 238], 'Hex string: 3 char lowercase' );
22         deepEqual( $.colorUtil.getRGB( '#EEE' ), [238, 238, 238], 'Hex string: 3 char uppercase' );
23         deepEqual( $.colorUtil.getRGB( '#eEe' ), [238, 238, 238], 'Hex string: 3 char mixed' );
24         deepEqual( $.colorUtil.getRGB( 'rgba(0, 0, 0, 0)' ), [255, 255, 255], 'Zero rgba for Safari 3; Transparent (whitespace)' );
26         // Perhaps this is a bug in colorUtil, but it is the current behaviour so, let's keep
27         // track of it, so we will know in case it would ever change.
28         strictEqual( $.colorUtil.getRGB( 'rgba(0,0,0,0)' ), undefined, 'Zero rgba without whitespace' );
30         deepEqual( $.colorUtil.getRGB( 'lightGreen' ), [144, 238, 144], 'Color names (lightGreen)' );
31         deepEqual( $.colorUtil.getRGB( 'lightGreen' ), [144, 238, 144], 'Color names (transparent)' );
32         strictEqual( $.colorUtil.getRGB( 'mediaWiki' ), undefined, 'Inexisting color name' );
33 });
35 test( 'rgbToHsl', function() {
36         expect(1);
38         var hsl = $.colorUtil.rgbToHsl( 144, 238, 144 );
40         // Cross-browser differences in decimals...
41         // Round to two decimals so they can be more reliably checked.
42         var dualDecimals = function(a,b){
43                 return Math.round(a*100)/100;
44         };
45         // Re-create the rgbToHsl return array items, limited to two decimals.
46         var ret = [dualDecimals(hsl[0]), dualDecimals(hsl[1]), dualDecimals(hsl[2])];
48         deepEqual( ret, [0.33, 0.73, 0.75], 'rgb(144, 238, 144): hsl(0.33, 0.73, 0.75)' );
49 });
51 test( 'hslToRgb', function() {
52         expect(1);
54         var rgb = $.colorUtil.hslToRgb( 0.3, 0.7, 0.8 );
56         // Cross-browser differences in decimals...
57         // Re-create the hslToRgb return array items, rounded to whole numbers.
58         var ret = [Math.round(rgb[0]), Math.round(rgb[1]), Math.round(rgb[2])];
60         deepEqual( ret ,[183, 240, 168], 'hsl(0.3, 0.7, 0.8): rgb(183, 240, 168)' );
61 });
63 test( 'getColorBrightness', function() {
64         expect(2);
66         var a = $.colorUtil.getColorBrightness( 'red', +0.1 );
67         equal( a, 'rgb(255,50,50)', 'Start with named color "red", brighten 10%' );
69         var b = $.colorUtil.getColorBrightness( 'rgb(200,50,50)', -0.2 );
70         equal( b, 'rgb(118,29,29)', 'Start with rgb string "rgb(200,50,50)", darken 20%' );
71 });