@@ -2,24 +2,36 @@ defineSuite([
2
2
'Core/Resource' ,
3
3
'Core/defaultValue' ,
4
4
'Core/DefaultProxy' ,
5
+ 'Core/FeatureDetection' ,
5
6
'Core/queryToObject' ,
6
7
'Core/Request' ,
7
8
'Core/RequestErrorEvent' ,
8
9
'Core/RequestScheduler' ,
10
+ 'Core/TrustedServers' ,
11
+ 'Specs/createCanvas' ,
9
12
'ThirdParty/Uri' ,
10
13
'ThirdParty/when'
11
14
] , function (
12
15
Resource ,
13
16
defaultValue ,
14
17
DefaultProxy ,
18
+ FeatureDetection ,
15
19
queryToObject ,
16
20
Request ,
17
21
RequestErrorEvent ,
18
22
RequestScheduler ,
23
+ TrustedServers ,
24
+ createCanvas ,
19
25
Uri ,
20
26
when ) {
21
27
'use strict' ;
22
28
29
+ var dataUri = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIW2Nk+M/wHwAEBgIA5agATwAAAABJRU5ErkJggg==' ;
30
+
31
+ beforeAll ( function ( ) {
32
+ return FeatureDetection . supportsImageBitmapOptions ( ) ;
33
+ } ) ;
34
+
23
35
it ( 'Constructor sets correct properties' , function ( ) {
24
36
var proxy = new DefaultProxy ( '/proxy/' ) ;
25
37
var request = new Request ( ) ;
@@ -1122,26 +1134,141 @@ defineSuite([
1122
1134
} ) ;
1123
1135
} ) ;
1124
1136
1125
- describe ( 'fetchImage' , function ( ) {
1137
+ it ( 'can load an image preferring blob' , function ( ) {
1138
+ return Resource . fetchImage ( './Data/Images/Green.png' , true ) . then ( function ( loadedImage ) {
1139
+ expect ( loadedImage . width ) . toEqual ( 1 ) ;
1140
+ expect ( loadedImage . height ) . toEqual ( 1 ) ;
1141
+ } ) ;
1142
+ } ) ;
1126
1143
1127
- var dataUri = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIW2Nk+M/wHwAEBgIA5agATwAAAABJRU5ErkJggg==' ;
1144
+ it ( 'can load an image from a data URI' , function ( ) {
1145
+ return Resource . fetchImage ( dataUri ) . then ( function ( loadedImage ) {
1146
+ expect ( loadedImage . width ) . toEqual ( 1 ) ;
1147
+ expect ( loadedImage . height ) . toEqual ( 1 ) ;
1148
+ } ) ;
1149
+ } ) ;
1128
1150
1129
- it ( 'can load an image' , function ( ) {
1151
+ describe ( 'fetchImage with ImageBitmap' , function ( ) {
1152
+ if ( ! FeatureDetection . supportsCreateImageBitmap ( ) ) {
1153
+ return ;
1154
+ }
1155
+
1156
+ var canvas ;
1157
+ beforeAll ( function ( ) {
1158
+ canvas = createCanvas ( 1 , 2 ) ;
1159
+ } ) ;
1160
+
1161
+ afterAll ( function ( ) {
1162
+ document . body . removeChild ( canvas ) ;
1163
+ } ) ;
1164
+
1165
+ function getColorAtPixel ( image , x , y ) {
1166
+ var context = canvas . getContext ( '2d' ) ;
1167
+ context . drawImage ( image , 0 , 0 , image . width , image . height ) ;
1168
+ var imageData = context . getImageData ( 0 , 0 , 1 , 1 ) ;
1169
+ return [ imageData . data [ 0 ] , imageData . data [ 1 ] , imageData . data [ 2 ] , imageData . data [ 3 ] ] ;
1170
+ }
1171
+
1172
+ it ( 'can load and decode an image' , function ( ) {
1130
1173
return Resource . fetchImage ( './Data/Images/Green.png' ) . then ( function ( loadedImage ) {
1131
1174
expect ( loadedImage . width ) . toEqual ( 1 ) ;
1132
1175
expect ( loadedImage . height ) . toEqual ( 1 ) ;
1176
+ expect ( loadedImage instanceof ImageBitmap ) ;
1133
1177
} ) ;
1134
1178
} ) ;
1135
1179
1136
- it ( 'can load an image preferring blob' , function ( ) {
1137
- return Resource . fetchImage ( './Data/Images/Green.png' , true ) . then ( function ( loadedImage ) {
1180
+ it ( 'does not call createImageBitmap when ImageBitmapOptions support is not ready' , function ( ) {
1181
+ spyOn ( FeatureDetection , 'supportsImageBitmapOptionsSync' ) . and . returnValue ( undefined ) ;
1182
+ spyOn ( window , 'createImageBitmap' ) . and . callThrough ( ) ;
1183
+
1184
+ return Resource . fetchImage ( './Data/Images/Green.png' ) . then ( function ( loadedImage ) {
1138
1185
expect ( loadedImage . width ) . toEqual ( 1 ) ;
1139
1186
expect ( loadedImage . height ) . toEqual ( 1 ) ;
1187
+ expect ( window . createImageBitmap ) . not . toHaveBeenCalled ( ) ;
1188
+ } ) ;
1189
+ } ) ;
1190
+
1191
+ it ( 'correctly flips image when ImageBitmapOptions are supported' , function ( ) {
1192
+ return Resource . fetchImage ( {
1193
+ url : './Data/Images/BlueOverRed.png' ,
1194
+ flipImage : true
1195
+ } ) . then ( function ( loadedImage ) {
1196
+ if ( FeatureDetection . supportsImageBitmapOptionsSync ( ) ) {
1197
+ expect ( getColorAtPixel ( loadedImage , 0 , 0 ) ) . toEqual ( [ 255 , 0 , 0 , 255 ] ) ;
1198
+ } else {
1199
+ expect ( getColorAtPixel ( loadedImage , 0 , 0 ) ) . toEqual ( [ 0 , 0 , 255 , 255 ] ) ;
1200
+ }
1201
+ } ) ;
1202
+ } ) ;
1203
+
1204
+ it ( 'correctly loads image without flip when ImageBitmapOptions are supported' , function ( ) {
1205
+ return Resource . fetchImage ( {
1206
+ url : './Data/Images/BlueOverRed.png' ,
1207
+ flipImage : false
1208
+ } ) . then ( function ( loadedImage ) {
1209
+ if ( FeatureDetection . supportsImageBitmapOptionsSync ( ) ) {
1210
+ expect ( getColorAtPixel ( loadedImage , 0 , 0 ) ) . toEqual ( [ 0 , 0 , 255 , 255 ] ) ;
1211
+ } else {
1212
+ expect ( getColorAtPixel ( loadedImage , 0 , 0 ) ) . toEqual ( [ 0 , 0 , 255 , 255 ] ) ;
1213
+ }
1140
1214
} ) ;
1141
1215
} ) ;
1142
1216
1143
- it ( 'can load an image from a data URI' , function ( ) {
1144
- return Resource . fetchImage ( dataUri ) . then ( function ( loadedImage ) {
1217
+ it ( 'does not pass options when ImageBitmapOptions are not supported' , function ( ) {
1218
+ spyOn ( FeatureDetection , 'supportsImageBitmapOptionsSync' ) . and . returnValue ( false ) ;
1219
+ spyOn ( window , 'createImageBitmap' ) . and . callThrough ( ) ;
1220
+
1221
+ return Resource . fetchImage ( './Data/Images/Green.png' ) . then ( function ( loadedImage ) {
1222
+ expect ( window . createImageBitmap ) . toHaveBeenCalledWith ( new Blob ( ) ) ;
1223
+ } ) ;
1224
+ } ) ;
1225
+
1226
+ it ( 'rejects the promise when the image errors' , function ( ) {
1227
+ return Resource . fetchImage ( 'http://example.invalid/testuri.png' )
1228
+ . then ( function ( ) {
1229
+ fail ( 'expected promise to reject' ) ;
1230
+ } )
1231
+ . otherwise ( function ( error ) {
1232
+ expect ( error ) . toBeInstanceOf ( TypeError ) ;
1233
+ } ) ;
1234
+ } ) ;
1235
+
1236
+ it ( 'does not set credentials for untrusted cross-origin images using fetch API' , function ( ) {
1237
+ var fetchSpy = spyOn ( window , 'fetch' ) . and . callThrough ( ) ;
1238
+ var url = './Data/Images/Green.png' ;
1239
+
1240
+ return Resource . fetchImage ( url )
1241
+ . then ( function ( ) {
1242
+ expect ( fetchSpy ) . toHaveBeenCalledWith ( url , {
1243
+ credentials : 'same-origin'
1244
+ } ) ;
1245
+ } ) ;
1246
+ } ) ;
1247
+
1248
+ it ( 'sets credentials for trusted cross-origin images using fetch API' , function ( ) {
1249
+ var fetchSpy = spyOn ( window , 'fetch' ) . and . callThrough ( ) ;
1250
+ spyOn ( TrustedServers , 'contains' ) . and . returnValue ( true ) ;
1251
+ var url = 'http://example.invalid/testuri.png' ;
1252
+
1253
+ return Resource . fetchImage ( url )
1254
+ . otherwise ( function ( ) {
1255
+ expect ( fetchSpy ) . toHaveBeenCalledWith ( url , {
1256
+ credentials : 'include'
1257
+ } ) ;
1258
+ } ) ;
1259
+ } ) ;
1260
+ } ) ;
1261
+
1262
+ describe ( 'fetchImage without ImageBitmap' , function ( ) {
1263
+ beforeAll ( function ( ) {
1264
+ // Force it to use the Image constructor since these specs all test
1265
+ // specific functionality of this code path. For example, the crossOrigin
1266
+ // restriction does not apply to images loaded with ImageBitmap.
1267
+ spyOn ( FeatureDetection , 'supportsCreateImageBitmap' ) . and . returnValue ( false ) ;
1268
+ } ) ;
1269
+
1270
+ it ( 'can load an image' , function ( ) {
1271
+ return Resource . fetchImage ( './Data/Images/Green.png' ) . then ( function ( loadedImage ) {
1145
1272
expect ( loadedImage . width ) . toEqual ( 1 ) ;
1146
1273
expect ( loadedImage . height ) . toEqual ( 1 ) ;
1147
1274
} ) ;
0 commit comments