1+ // declare vars (preventing JSHint messages)
2+ var test = test || function ( ) { } ,
3+ QUnit = QUnit || { } ,
4+ _html2canvas = _html2canvas || { } ;
5+
6+
17module ( "CSS" ) ;
2- $ ( function ( ) {
8+ $ ( function ( ) {
9+
10+ var propsToTest = { } ,
11+ numDivs = { } ;
12+
313
414
5- var propsToTest = [ "borderTopWidth" , "borderRightWidth" , "borderBottomWidth" , "borderLeftWidth" ] ,
6- numDivs = $ ( '#borders div' ) . length ;
15+ propsToTest [ 'border-width' ] = [ "borderTopWidth" , "borderRightWidth" , "borderBottomWidth" , "borderLeftWidth" ] ,
16+ numDivs [ 'border-width' ] = $ ( '#borders div' ) . length ;
717 /*
818 expecting = [
919 // #1
@@ -56,10 +66,10 @@ $(function() {
5666 ];
5767 */
5868
59- test ( 'border-width' , propsToTest . length * numDivs , function ( ) {
69+ test ( 'border-width' , propsToTest [ 'border-width' ] . length * numDivs [ 'border-width' ] , function ( ) {
6070
6171 $ ( '#borders div' ) . each ( function ( i , el ) {
62- $ . each ( propsToTest , function ( s , prop ) {
72+ $ . each ( propsToTest [ 'border-width' ] , function ( s , prop ) {
6373 var expect = $ ( el ) . css ( prop ) ;
6474
6575 // older IE's don't necessarily return px even with jQuery
@@ -76,32 +86,28 @@ $(function() {
7686 } ) ;
7787 } ) ;
7888
79- var propsToTest2 = [ "paddingTop" , "paddingRight" , "paddingBottom" , "paddingLeft" ] ,
80- numDivs2 = $ ( '#padding div' ) . length ;
89+ propsToTest [ 'padding width' ] = [ "paddingTop" , "paddingRight" , "paddingBottom" , "paddingLeft" ] ;
90+ numDivs [ 'padding width' ] = $ ( '#padding div' ) . length ;
8191
82- test ( 'padding width' , propsToTest2 . length * numDivs2 * 2 , function ( ) {
92+ test ( 'padding width' , propsToTest [ 'padding width' ] . length * numDivs [ 'padding width' ] * 2 , function ( ) {
8393
8494 $ ( '#padding div' ) . each ( function ( i , el ) {
85- $ . each ( propsToTest2 , function ( s , prop ) {
95+ $ . each ( propsToTest [ 'padding width' ] , function ( s , prop ) {
8696 var isPx = _html2canvas . Util . getCSS ( el , prop ) . indexOf ( "px" ) ;
8797 QUnit . notEqual ( isPx , - 1 , "div #" + ( i + 1 ) + " property " + prop + " is in pixels" ) ;
8898 QUnit . equal ( _html2canvas . Util . getCSS ( el , prop ) , $ ( el ) . css ( prop ) , "div #" + ( i + 1 ) + " property " + prop + " equals " + $ ( el ) . css ( prop ) ) ;
8999 } ) ;
90100
91101 } ) ;
92- } ) ;
93-
102+ } ) ;
94103
95-
96-
97-
98- var propsToTest3 = [ "backgroundPosition" ] ,
99- numDivs3 = $ ( '#backgroundPosition div' ) . length ;
104+ propsToTest [ 'background-position' ] = [ "backgroundPosition" ] ;
105+ numDivs [ 'background-position' ] = $ ( '#backgroundPosition div' ) . length ;
100106
101- test ( 'background-position' , propsToTest3 . length * numDivs3 * 2 , function ( ) {
107+ test ( 'background-position' , propsToTest [ 'background-position' ] . length * numDivs [ 'background-position' ] * 2 , function ( ) {
102108
103109 $ ( '#backgroundPosition div' ) . each ( function ( i , el ) {
104- $ . each ( propsToTest3 , function ( s , prop ) {
110+ $ . each ( propsToTest [ 'background-position' ] , function ( s , prop ) {
105111 var img = new Image ( ) ;
106112 img . width = 50 ;
107113 img . height = 50 ;
@@ -113,7 +119,7 @@ $(function() {
113119 if ( window . getComputedStyle ) {
114120 split = $ ( el ) . css ( prop ) . split ( " " ) ;
115121 } else {
116- split = [ $ ( el ) . css ( prop + "X" ) , $ ( el ) . css ( prop + "Y" ) ]
122+ split = [ $ ( el ) . css ( prop + "X" ) , $ ( el ) . css ( prop + "Y" ) ] ;
117123 }
118124
119125 var testEl = $ ( '<div />' ) . css ( {
@@ -139,4 +145,45 @@ $(function() {
139145 } ) ;
140146
141147// TODO add backgroundPosition % tests
148+
149+ propsToTest [ 'background-gradient' ] = [ "backgroundImage" ] ;
150+ numDivs [ 'background-gradient' ] = $ ( '#backgroundGradients div' ) . length ;
151+
152+ test ( 'background-gradient' , propsToTest [ 'background-gradient' ] . length * numDivs [ 'background-gradient' ] , function ( ) {
153+
154+ $ ( '#backgroundGradients div' ) . each ( function ( i , el ) {
155+ $ . each ( propsToTest [ 'background-gradient' ] , function ( s , prop ) {
156+ var src , img , canvas , ctx , id , data , len , red , green , blue , overallColor = 0 ;
157+
158+ src = _html2canvas . Util . getCSS ( el , prop ) ,
159+ img = _html2canvas . Generate . Gradient ( src , {
160+ width : 50 ,
161+ height : 50
162+ } ) ;
163+
164+ canvas = document . createElement ( 'canvas' ) ;
165+ canvas . width = 50 ;
166+ canvas . height = 50 ;
167+ ctx = canvas . getContext ( '2d' ) ;
168+ ctx . drawImage ( img , 0 , 0 ) ;
169+ id = ctx . getImageData ( 0 , 0 , 50 , 50 ) ;
170+ data = id . data ;
171+ len = data . length ;
172+
173+ //console.log(img);
174+
175+ for ( var i = 0 ; i < len ; i += 4 ) {
176+ red = data [ i ] ; // red
177+ green = data [ i + 1 ] ; // green
178+ blue = data [ i + 2 ] ; // blue
179+ // i+3 is alpha (the fourth element)
180+
181+ overallColor += ( red + green + blue ) / 3 ;
182+ }
183+ overallColor /= ( len / 4 ) ;
184+
185+ QUnit . notEqual ( overallColor , 255 , 'No Background Gradient - CSS was ' + src ) ;
186+ } ) ;
187+ } ) ;
188+ } ) ;
142189} ) ;
0 commit comments