2022-07-22 05:32:08 +08:00
import {
Cartesian2 ,
defaultValue ,
defined ,
DeveloperError ,
FeatureDetection ,
PrimitiveType ,
Buffer ,
BufferUsage ,
ClearCommand ,
DrawCommand ,
ShaderProgram ,
VertexArray ,
2022-11-02 03:39:57 +08:00
Math as CesiumMath ,
} from "@cesium/engine" ;
2020-04-17 08:31:36 +08:00
import equals from "./equals.js" ;
2022-07-22 05:32:08 +08:00
2020-04-17 08:31:36 +08:00
function createMissingFunctionMessageFunction (
item ,
actualPrototype ,
expectedInterfacePrototype
) {
return function ( ) {
2022-02-03 04:40:21 +08:00
return ` Expected function ' ${ item } ' to exist on ${ actualPrototype . constructor . name } because it should implement interface ${ expectedInterfacePrototype . constructor . name } . ` ;
2020-04-17 08:31:36 +08:00
} ;
}
2023-01-13 00:07:41 +08:00
function makeAsyncThrowFunction ( debug , Type , name ) {
if ( debug ) {
return function ( util ) {
return {
compare : function ( actualPromise , message ) {
// based on the built-in Jasmine toBeRejectedWithError async-matcher
if ( ! defined ( actualPromise ) || ! defined ( actualPromise . then ) ) {
throw new Error ( "Expected function to be called on a promise." ) ;
}
return actualPromise
. then ( ( ) => {
return {
pass : false ,
message :
"Expected a promise to be rejected but it was resolved." ,
} ;
} )
. catch ( ( e ) => {
let result = e instanceof Type || e . name === name ;
if ( defined ( message ) ) {
result = result && util . equals ( e . message , message ) ;
}
return {
pass : result ,
message : result
? ` Expected a promise to be rejected with ${ name } . `
: ` Expected a promise to be rejected with ${
defined ( message ) ? ` ${ name } : ${ message } ` : name
} , but it was rejected with $ { e } ` ,
} ;
} ) ;
} ,
} ;
} ;
}
return function ( ) {
return {
compare : function ( actualPromise ) {
return Promise . resolve ( actualPromise )
2023-01-21 04:20:38 +08:00
. then ( ( ) => {
2023-01-13 00:07:41 +08:00
return { pass : true } ;
} )
2023-01-21 04:20:38 +08:00
. catch ( ( e ) => {
// Ignore any error
2023-01-13 00:07:41 +08:00
return { pass : true } ;
} ) ;
} ,
negativeCompare : function ( actualPromise ) {
return Promise . resolve ( actualPromise )
2023-01-21 04:20:38 +08:00
. then ( ( ) => {
2023-01-13 00:07:41 +08:00
return { pass : true } ;
} )
2023-01-21 04:20:38 +08:00
. catch ( ( e ) => {
// Ignore any error
2023-01-13 00:07:41 +08:00
return { pass : true } ;
} ) ;
} ,
} ;
} ;
}
2020-04-17 08:31:36 +08:00
function makeThrowFunction ( debug , Type , name ) {
if ( debug ) {
2022-04-06 01:35:51 +08:00
return function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected ) {
// based on the built-in Jasmine toThrow matcher
2022-01-22 00:26:25 +08:00
let result = false ;
let exception ;
2020-04-17 08:31:36 +08:00
if ( typeof actual !== "function" ) {
throw new Error ( "Actual is not a function" ) ;
}
try {
actual ( ) ;
} catch ( e ) {
exception = e ;
}
if ( exception ) {
2022-04-13 22:24:27 +08:00
result = exception instanceof Type || exception . name === name ;
2020-04-17 08:31:36 +08:00
}
2022-01-22 00:26:25 +08:00
let message ;
2020-04-17 08:31:36 +08:00
if ( result ) {
message = [
2022-02-03 04:40:21 +08:00
` Expected function not to throw ${ name } , but it threw ` ,
2020-04-17 08:31:36 +08:00
exception . message || exception ,
] . join ( " " ) ;
} else {
2022-02-03 04:40:21 +08:00
message = ` Expected function to throw ${ name } . ` ;
2020-04-17 08:31:36 +08:00
}
return {
pass : result ,
message : message ,
} ;
} ,
} ;
} ;
}
return function ( ) {
return {
compare : function ( actual , expected ) {
return { pass : true } ;
} ,
negativeCompare : function ( actual , expected ) {
return { pass : true } ;
} ,
} ;
} ;
}
function createDefaultMatchers ( debug ) {
return {
2022-04-06 01:35:51 +08:00
toBeBetween : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , lower , upper ) {
if ( lower > upper ) {
2022-01-22 00:26:25 +08:00
const tmp = upper ;
2020-04-17 08:31:36 +08:00
upper = lower ;
lower = tmp ;
}
return { pass : actual >= lower && actual <= upper } ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toStartWith : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected ) {
return { pass : actual . slice ( 0 , expected . length ) === expected } ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toEndWith : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected ) {
return { pass : actual . slice ( - expected . length ) === expected } ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toEqual : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected ) {
return {
2022-04-06 01:35:51 +08:00
pass : equals ( util , actual , expected ) ,
2020-04-17 08:31:36 +08:00
} ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toEqualEpsilon : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected , epsilon ) {
function equalityTester ( a , b ) {
2022-04-06 01:35:51 +08:00
a = typedArrayToArray ( a ) ;
b = typedArrayToArray ( b ) ;
2020-04-17 08:31:36 +08:00
if ( Array . isArray ( a ) && Array . isArray ( b ) ) {
if ( a . length !== b . length ) {
return false ;
}
2022-01-22 00:26:25 +08:00
for ( let i = 0 ; i < a . length ; ++ i ) {
2020-04-17 08:31:36 +08:00
if ( ! equalityTester ( a [ i ] , b [ i ] ) ) {
return false ;
2015-02-17 13:12:00 +08:00
}
2020-04-17 08:31:36 +08:00
}
2012-07-26 03:38:41 +08:00
2020-04-17 08:31:36 +08:00
return true ;
2017-12-30 22:29:07 +08:00
}
2016-12-13 22:07:08 +08:00
2022-01-22 00:26:25 +08:00
let to _run ;
2020-04-17 08:31:36 +08:00
if ( defined ( a ) ) {
if ( typeof a . equalsEpsilon === "function" ) {
return a . equalsEpsilon ( b , epsilon ) ;
} else if ( a instanceof Object ) {
// Check if the current object has a static function named 'equalsEpsilon'
to _run = Object . getPrototypeOf ( a ) . constructor . equalsEpsilon ;
if ( typeof to _run === "function" ) {
return to _run ( a , b , epsilon ) ;
}
}
2016-12-13 22:07:08 +08:00
}
2018-10-02 02:38:15 +08:00
2020-04-17 08:31:36 +08:00
if ( defined ( b ) ) {
if ( typeof b . equalsEpsilon === "function" ) {
return b . equalsEpsilon ( a , epsilon ) ;
} else if ( b instanceof Object ) {
// Check if the current object has a static function named 'equalsEpsilon'
to _run = Object . getPrototypeOf ( b ) . constructor . equalsEpsilon ;
if ( typeof to _run === "function" ) {
return to _run ( b , a , epsilon ) ;
}
}
}
2018-10-02 02:38:15 +08:00
2020-04-17 08:31:36 +08:00
if ( typeof a === "number" || typeof b === "number" ) {
return Math . abs ( a - b ) <= epsilon ;
}
2016-12-13 19:34:59 +08:00
2022-04-06 03:45:21 +08:00
if ( defined ( a ) && defined ( b ) ) {
const keys = Object . keys ( a ) ;
for ( let i = 0 ; i < keys . length ; i ++ ) {
if ( ! b . hasOwnProperty ( keys [ i ] ) ) {
return false ;
}
const aVal = a [ keys [ i ] ] ;
const bVal = b [ keys [ i ] ] ;
if ( ! equalityTester ( aVal , bVal ) ) {
return false ;
}
}
return true ;
}
2022-04-06 01:35:51 +08:00
return equals ( util , a , b ) ;
2020-04-17 08:31:36 +08:00
}
2022-04-06 01:35:51 +08:00
const result = equalityTester ( actual , expected ) ;
2020-04-17 08:31:36 +08:00
return { pass : result } ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toConformToInterface : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expectedInterface ) {
// All function properties on the prototype should also exist on the actual's prototype.
2022-01-22 00:26:25 +08:00
const actualPrototype = actual . prototype ;
const expectedInterfacePrototype = expectedInterface . prototype ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
for ( const item in expectedInterfacePrototype ) {
2020-04-17 08:31:36 +08:00
if (
expectedInterfacePrototype . hasOwnProperty ( item ) &&
typeof expectedInterfacePrototype [ item ] === "function" &&
! actualPrototype . hasOwnProperty ( item )
) {
return {
pass : false ,
message : createMissingFunctionMessageFunction (
item ,
actualPrototype ,
expectedInterfacePrototype
) ,
} ;
}
}
return { pass : true } ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toRender : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected ) {
2022-04-06 01:35:51 +08:00
return renderEquals ( util , actual , expected , true ) ;
2020-04-17 08:31:36 +08:00
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
notToRender : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected ) {
2022-04-06 01:35:51 +08:00
return renderEquals ( util , actual , expected , false ) ;
2020-04-17 08:31:36 +08:00
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toRenderAndCall : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected ) {
2022-01-22 00:26:25 +08:00
const actualRgba = renderAndReadPixels ( actual ) ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
const webglStub = ! ! window . webglStub ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
// The callback may have expectations that fail, which still makes the
// spec fail, as we desired, even though this matcher sets pass to true.
2022-01-22 00:26:25 +08:00
const callback = expected ;
2020-04-17 08:31:36 +08:00
callback ( actualRgba ) ;
}
return {
pass : true ,
} ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toRenderPixelCountAndCall : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected ) {
2022-01-22 00:26:25 +08:00
const actualRgba = renderAndReadPixels ( actual ) ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
const webglStub = ! ! window . webglStub ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
// The callback may have expectations that fail, which still makes the
// spec fail, as we desired, even though this matcher sets pass to true.
2022-01-22 00:26:25 +08:00
const callback = expected ;
2020-04-17 08:31:36 +08:00
callback ( countRenderedPixels ( actualRgba ) ) ;
}
return {
pass : true ,
} ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toPickPrimitive : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected , x , y , width , height ) {
return pickPrimitiveEquals ( actual , expected , x , y , width , height ) ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
notToPick : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , x , y , width , height ) {
return pickPrimitiveEquals ( actual , undefined , x , y , width , height ) ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toDrillPickPrimitive : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected , x , y , width , height ) {
return drillPickPrimitiveEquals ( actual , 1 , x , y , width , height ) ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
notToDrillPick : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , x , y , width , height ) {
return drillPickPrimitiveEquals ( actual , 0 , x , y , width , height ) ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toPickAndCall : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
2020-07-02 05:39:25 +08:00
compare : function ( actual , expected , args ) {
2022-01-22 00:26:25 +08:00
const scene = actual ;
const result = scene . pick ( defaultValue ( args , new Cartesian2 ( 0 , 0 ) ) ) ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
const webglStub = ! ! window . webglStub ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
// The callback may have expectations that fail, which still makes the
// spec fail, as we desired, even though this matcher sets pass to true.
2022-01-22 00:26:25 +08:00
const callback = expected ;
2020-04-17 08:31:36 +08:00
callback ( result ) ;
}
return {
pass : true ,
} ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toDrillPickAndCall : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected , limit ) {
2022-01-22 00:26:25 +08:00
const scene = actual ;
const pickedObjects = scene . drillPick ( new Cartesian2 ( 0 , 0 ) , limit ) ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
const webglStub = ! ! window . webglStub ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
// The callback may have expectations that fail, which still makes the
// spec fail, as we desired, even though this matcher sets pass to true.
2022-01-22 00:26:25 +08:00
const callback = expected ;
2020-04-17 08:31:36 +08:00
callback ( pickedObjects ) ;
}
return {
pass : true ,
} ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toPickFromRayAndCall : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected , ray , objectsToExclude , width ) {
2022-01-22 00:26:25 +08:00
const scene = actual ;
const result = scene . pickFromRay ( ray , objectsToExclude , width ) ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
const webglStub = ! ! window . webglStub ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
// The callback may have expectations that fail, which still makes the
// spec fail, as we desired, even though this matcher sets pass to true.
2022-01-22 00:26:25 +08:00
const callback = expected ;
2020-04-17 08:31:36 +08:00
callback ( result ) ;
}
return {
pass : true ,
} ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toDrillPickFromRayAndCall : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function (
actual ,
expected ,
ray ,
limit ,
objectsToExclude ,
width
) {
2022-01-22 00:26:25 +08:00
const scene = actual ;
const results = scene . drillPickFromRay (
2020-04-17 08:31:36 +08:00
ray ,
limit ,
objectsToExclude ,
width
) ;
2022-01-22 00:26:25 +08:00
const webglStub = ! ! window . webglStub ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
// The callback may have expectations that fail, which still makes the
// spec fail, as we desired, even though this matcher sets pass to true.
2022-01-22 00:26:25 +08:00
const callback = expected ;
2020-04-17 08:31:36 +08:00
callback ( results ) ;
}
return {
pass : true ,
} ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toSampleHeightAndCall : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function (
actual ,
expected ,
position ,
objectsToExclude ,
width
) {
2022-01-22 00:26:25 +08:00
const scene = actual ;
const results = scene . sampleHeight ( position , objectsToExclude , width ) ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
const webglStub = ! ! window . webglStub ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
// The callback may have expectations that fail, which still makes the
// spec fail, as we desired, even though this matcher sets pass to true.
2022-01-22 00:26:25 +08:00
const callback = expected ;
2020-04-17 08:31:36 +08:00
callback ( results ) ;
}
return {
pass : true ,
} ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toClampToHeightAndCall : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function (
actual ,
expected ,
cartesian ,
objectsToExclude ,
width
) {
2022-01-22 00:26:25 +08:00
const scene = actual ;
const results = scene . clampToHeight (
cartesian ,
objectsToExclude ,
width
) ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
const webglStub = ! ! window . webglStub ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
// The callback may have expectations that fail, which still makes the
// spec fail, as we desired, even though this matcher sets pass to true.
2022-01-22 00:26:25 +08:00
const callback = expected ;
2020-04-17 08:31:36 +08:00
callback ( results ) ;
}
return {
pass : true ,
} ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toPickPositionAndCall : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected , x , y ) {
2022-01-22 00:26:25 +08:00
const scene = actual ;
const canvas = scene . canvas ;
2020-04-17 08:31:36 +08:00
x = defaultValue ( x , canvas . clientWidth / 2 ) ;
y = defaultValue ( y , canvas . clientHeight / 2 ) ;
2022-01-22 00:26:25 +08:00
const result = scene . pickPosition ( new Cartesian2 ( x , y ) ) ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
const webglStub = ! ! window . webglStub ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
// The callback may have expectations that fail, which still makes the
// spec fail, as we desired, even though this matcher sets pass to true.
2022-01-22 00:26:25 +08:00
const callback = expected ;
2020-04-17 08:31:36 +08:00
callback ( result ) ;
}
return {
pass : true ,
} ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toReadPixels : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected ) {
2022-01-22 00:26:25 +08:00
let context ;
let framebuffer ;
let epsilon = 0 ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
const options = actual ;
2020-04-17 08:31:36 +08:00
if ( defined ( options . context ) ) {
// options were passed to to a framebuffer
context = options . context ;
framebuffer = options . framebuffer ;
epsilon = defaultValue ( options . epsilon , epsilon ) ;
} else {
context = options ;
}
2022-01-22 00:26:25 +08:00
const rgba = context . readPixels ( {
2020-04-17 08:31:36 +08:00
framebuffer : framebuffer ,
} ) ;
2022-01-22 00:26:25 +08:00
let pass = true ;
let message ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
const webglStub = ! ! window . webglStub ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
if (
! CesiumMath . equalsEpsilon ( rgba [ 0 ] , expected [ 0 ] , 0 , epsilon ) ||
! CesiumMath . equalsEpsilon ( rgba [ 1 ] , expected [ 1 ] , 0 , epsilon ) ||
! CesiumMath . equalsEpsilon ( rgba [ 2 ] , expected [ 2 ] , 0 , epsilon ) ||
! CesiumMath . equalsEpsilon ( rgba [ 3 ] , expected [ 3 ] , 0 , epsilon )
) {
pass = false ;
if ( epsilon === 0 ) {
2022-02-03 04:40:21 +08:00
message = ` Expected context to render ${ expected } , but rendered: ${ rgba } ` ;
2020-04-17 08:31:36 +08:00
} else {
2022-02-03 04:40:21 +08:00
message = ` Expected context to render ${ expected } with epsilon = ${ epsilon } , but rendered: ${ rgba } ` ;
2020-04-17 08:31:36 +08:00
}
}
}
return {
pass : pass ,
message : message ,
} ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
notToReadPixels : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected ) {
2022-01-22 00:26:25 +08:00
const context = actual ;
const rgba = context . readPixels ( ) ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
let pass = true ;
let message ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
const webglStub = ! ! window . webglStub ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
if (
rgba [ 0 ] === expected [ 0 ] &&
rgba [ 1 ] === expected [ 1 ] &&
rgba [ 2 ] === expected [ 2 ] &&
rgba [ 3 ] === expected [ 3 ]
) {
pass = false ;
2022-02-03 04:40:21 +08:00
message = ` Expected context not to render ${ expected } , but rendered: ${ rgba } ` ;
2020-04-17 08:31:36 +08:00
}
}
return {
pass : pass ,
message : message ,
} ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
contextToRenderAndCall : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected ) {
2022-01-22 00:26:25 +08:00
const actualRgba = contextRenderAndReadPixels ( actual ) . color ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
const webglStub = ! ! window . webglStub ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
// The callback may have expectations that fail, which still makes the
// spec fail, as we desired, even though this matcher sets pass to true.
2022-01-22 00:26:25 +08:00
const callback = expected ;
2020-04-17 08:31:36 +08:00
callback ( actualRgba ) ;
}
return {
pass : true ,
} ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
contextToRender : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected ) {
return expectContextToRender ( actual , expected , true ) ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
notContextToRender : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual , expected ) {
return expectContextToRender ( actual , expected , false ) ;
} ,
} ;
} ,
2022-04-06 01:35:51 +08:00
toBeImageOrImageBitmap : function ( util ) {
2020-04-17 08:31:36 +08:00
return {
compare : function ( actual ) {
if ( typeof createImageBitmap !== "function" ) {
2016-12-13 19:34:59 +08:00
return {
2020-04-17 08:31:36 +08:00
pass : actual instanceof Image ,
2016-12-13 19:34:59 +08:00
} ;
2020-04-17 08:31:36 +08:00
}
return {
pass : actual instanceof ImageBitmap || actual instanceof Image ,
} ;
} ,
} ;
} ,
2022-03-22 06:05:52 +08:00
2020-04-17 08:31:36 +08:00
toThrowDeveloperError : makeThrowFunction (
debug ,
DeveloperError ,
"DeveloperError"
) ,
} ;
}
2023-01-13 00:07:41 +08:00
function createDefaultAsyncMatchers ( debug ) {
return {
toBeRejectedWithDeveloperError : makeAsyncThrowFunction (
debug ,
DeveloperError ,
"DeveloperError"
) ,
} ;
}
2020-04-17 08:31:36 +08:00
function countRenderedPixels ( rgba ) {
2022-01-22 00:26:25 +08:00
const pixelCount = rgba . length / 4 ;
let count = 0 ;
for ( let i = 0 ; i < pixelCount ; i ++ ) {
const index = i * 4 ;
2020-04-17 08:31:36 +08:00
if (
rgba [ index ] !== 0 ||
rgba [ index + 1 ] !== 0 ||
rgba [ index + 2 ] !== 0 ||
rgba [ index + 3 ] !== 255
) {
count ++ ;
2016-12-13 19:34:59 +08:00
}
2020-04-17 08:31:36 +08:00
}
return count ;
}
2016-12-13 19:34:59 +08:00
2020-04-17 08:31:36 +08:00
function renderAndReadPixels ( options ) {
2022-01-22 00:26:25 +08:00
let scene ;
2016-12-15 20:59:11 +08:00
2020-04-17 08:31:36 +08:00
if ( defined ( options . scene ) ) {
// options were passed to render the scene at a given time or prime shadow map
scene = options . scene ;
2022-01-22 00:26:25 +08:00
const time = options . time ;
2016-12-15 20:59:11 +08:00
2020-04-17 08:31:36 +08:00
scene . initializeFrame ( ) ;
if ( defined ( options . primeShadowMap ) ) {
scene . render ( time ) ; // Computes shadow near/far for next frame
2016-12-15 20:59:11 +08:00
}
2020-04-17 08:31:36 +08:00
scene . render ( time ) ;
} else {
scene = options ;
scene . initializeFrame ( ) ;
scene . render ( ) ;
}
return scene . context . readPixels ( ) ;
}
function isTypedArray ( o ) {
return FeatureDetection . typedArrayTypes . some ( function ( type ) {
return o instanceof type ;
} ) ;
}
function typedArrayToArray ( array ) {
if ( isTypedArray ( array ) ) {
return Array . prototype . slice . call ( array , 0 ) ;
}
return array ;
}
2022-04-06 01:35:51 +08:00
function renderEquals ( util , actual , expected , expectEqual ) {
2022-01-22 00:26:25 +08:00
const actualRgba = renderAndReadPixels ( actual ) ;
2020-04-17 08:31:36 +08:00
// When the WebGL stub is used, all WebGL function calls are noops so
// the expectation is not verified. This allows running all the WebGL
// tests, to exercise as much Cesium code as possible, even if the system
// doesn't have a WebGL implementation or a reliable one.
if ( ! ! window . webglStub ) {
return {
pass : true ,
} ;
}
2022-04-06 01:35:51 +08:00
const eq = equals ( util , actualRgba , expected ) ;
2022-01-22 00:26:25 +08:00
const pass = expectEqual ? eq : ! eq ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
let message ;
2020-04-17 08:31:36 +08:00
if ( ! pass ) {
2022-02-03 04:40:21 +08:00
message = ` Expected ${
expectEqual ? "" : "not "
} to render [ $ { typedArrayToArray (
expected
) } ] , but actually rendered [ $ { typedArrayToArray ( actualRgba ) } ] . ` ;
2020-04-17 08:31:36 +08:00
}
return {
pass : pass ,
message : message ,
} ;
}
function pickPrimitiveEquals ( actual , expected , x , y , width , height ) {
2022-01-22 00:26:25 +08:00
const scene = actual ;
const windowPosition = new Cartesian2 ( x , y ) ;
const result = scene . pick ( windowPosition , width , height ) ;
2020-04-17 08:31:36 +08:00
if ( ! ! window . webglStub ) {
return {
pass : true ,
} ;
}
2022-01-22 00:26:25 +08:00
let pass = true ;
let message ;
2020-04-17 08:31:36 +08:00
if ( defined ( expected ) ) {
pass = result . primitive === expected ;
} else {
pass = ! defined ( result ) ;
}
if ( ! pass ) {
2022-02-03 04:40:21 +08:00
message = ` Expected to pick ${ expected } , but picked: ${ result } ` ;
2020-04-17 08:31:36 +08:00
}
return {
pass : pass ,
message : message ,
} ;
}
function drillPickPrimitiveEquals ( actual , expected , x , y , width , height ) {
2022-01-22 00:26:25 +08:00
const scene = actual ;
const windowPosition = new Cartesian2 ( x , y ) ;
const result = scene . drillPick ( windowPosition , undefined , width , height ) ;
2020-04-17 08:31:36 +08:00
if ( ! ! window . webglStub ) {
return {
pass : true ,
} ;
}
2022-01-22 00:26:25 +08:00
let pass = true ;
let message ;
2020-04-17 08:31:36 +08:00
if ( defined ( expected ) ) {
pass = result . length === expected ;
} else {
pass = ! defined ( result ) ;
}
if ( ! pass ) {
2022-02-03 04:40:21 +08:00
message = ` Expected to pick ${ expected } , but picked: ${ result } ` ;
2020-04-17 08:31:36 +08:00
}
return {
pass : pass ,
message : message ,
} ;
}
function contextRenderAndReadPixels ( options ) {
2022-01-22 00:26:25 +08:00
const context = options . context ;
let vs = options . vertexShader ;
const fs = options . fragmentShader ;
let sp = options . shaderProgram ;
const uniformMap = options . uniformMap ;
const modelMatrix = options . modelMatrix ;
const depth = defaultValue ( options . depth , 0.0 ) ;
const clear = defaultValue ( options . clear , true ) ;
let clearColor ;
2020-04-17 08:31:36 +08:00
if ( ! defined ( context ) ) {
throw new DeveloperError ( "options.context is required." ) ;
}
if ( ! defined ( fs ) && ! defined ( sp ) ) {
throw new DeveloperError (
"options.fragmentShader or options.shaderProgram is required."
) ;
}
if ( defined ( fs ) && defined ( sp ) ) {
throw new DeveloperError (
"Both options.fragmentShader and options.shaderProgram can not be used at the same time."
) ;
}
if ( defined ( vs ) && defined ( sp ) ) {
throw new DeveloperError (
"Both options.vertexShader and options.shaderProgram can not be used at the same time."
) ;
}
if ( ! defined ( sp ) ) {
if ( ! defined ( vs ) ) {
vs =
2022-11-18 03:30:39 +08:00
"in vec4 position; void main() { gl_PointSize = 1.0; gl_Position = position; }" ;
2018-08-15 11:25:10 +08:00
}
2020-04-17 08:31:36 +08:00
sp = ShaderProgram . fromCache ( {
context : context ,
vertexShaderSource : vs ,
fragmentShaderSource : fs ,
attributeLocations : {
position : 0 ,
} ,
} ) ;
}
2022-01-22 00:26:25 +08:00
let va = new VertexArray ( {
2020-04-17 08:31:36 +08:00
context : context ,
attributes : [
{
index : 0 ,
vertexBuffer : Buffer . createVertexBuffer ( {
context : context ,
typedArray : new Float32Array ( [ 0.0 , 0.0 , depth , 1.0 ] ) ,
usage : BufferUsage . STATIC _DRAW ,
} ) ,
componentsPerAttribute : 4 ,
} ,
] ,
} ) ;
if ( clear ) {
ClearCommand . ALL . execute ( context ) ;
clearColor = context . readPixels ( ) ;
}
2022-01-22 00:26:25 +08:00
const command = new DrawCommand ( {
2020-04-17 08:31:36 +08:00
primitiveType : PrimitiveType . POINTS ,
shaderProgram : sp ,
vertexArray : va ,
uniformMap : uniformMap ,
modelMatrix : modelMatrix ,
} ) ;
command . execute ( context ) ;
2022-01-22 00:26:25 +08:00
const rgba = context . readPixels ( ) ;
2020-04-17 08:31:36 +08:00
sp = sp . destroy ( ) ;
va = va . destroy ( ) ;
return {
color : rgba ,
clearColor : clearColor ,
} ;
}
function expectContextToRender ( actual , expected , expectEqual ) {
2022-01-22 00:26:25 +08:00
const options = actual ;
const context = options . context ;
const clear = defaultValue ( options . clear , true ) ;
const epsilon = defaultValue ( options . epsilon , 0 ) ;
2020-04-17 08:31:36 +08:00
if ( ! defined ( expected ) ) {
expected = [ 255 , 255 , 255 , 255 ] ;
}
2022-01-22 00:26:25 +08:00
const webglStub = ! ! window . webglStub ;
2020-04-17 08:31:36 +08:00
2022-01-22 00:26:25 +08:00
const output = contextRenderAndReadPixels ( options ) ;
2020-04-17 08:31:36 +08:00
if ( clear ) {
2022-01-22 00:26:25 +08:00
const clearedRgba = output . clearColor ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
2022-01-22 00:26:25 +08:00
const expectedAlpha = context . options . webgl . alpha ? 0 : 255 ;
2020-04-17 08:31:36 +08:00
if (
clearedRgba [ 0 ] !== 0 ||
clearedRgba [ 1 ] !== 0 ||
clearedRgba [ 2 ] !== 0 ||
clearedRgba [ 3 ] !== expectedAlpha
) {
2018-10-30 00:33:52 +08:00
return {
2020-04-17 08:31:36 +08:00
pass : false ,
2022-02-03 04:40:21 +08:00
message : ` After clearing the framebuffer, expected context to render [0, 0, 0, ${ expectedAlpha } ], but rendered: ${ clearedRgba } ` ,
2018-10-30 00:33:52 +08:00
} ;
2020-04-17 08:31:36 +08:00
}
2018-10-26 09:36:03 +08:00
}
2020-04-17 08:31:36 +08:00
}
2022-01-22 00:26:25 +08:00
const rgba = output . color ;
2020-04-17 08:31:36 +08:00
if ( ! webglStub ) {
if ( expectEqual ) {
if (
! CesiumMath . equalsEpsilon ( rgba [ 0 ] , expected [ 0 ] , 0 , epsilon ) ||
! CesiumMath . equalsEpsilon ( rgba [ 1 ] , expected [ 1 ] , 0 , epsilon ) ||
! CesiumMath . equalsEpsilon ( rgba [ 2 ] , expected [ 2 ] , 0 , epsilon ) ||
! CesiumMath . equalsEpsilon ( rgba [ 3 ] , expected [ 3 ] , 0 , epsilon )
) {
2016-12-19 20:34:29 +08:00
return {
2020-04-17 08:31:36 +08:00
pass : false ,
2022-02-03 04:40:21 +08:00
message : ` Expected context to render ${ expected } , but rendered: ${ rgba } ` ,
2013-11-27 08:51:46 +08:00
} ;
2020-04-17 08:31:36 +08:00
}
} else if (
CesiumMath . equalsEpsilon ( rgba [ 0 ] , expected [ 0 ] , 0 , epsilon ) &&
CesiumMath . equalsEpsilon ( rgba [ 1 ] , expected [ 1 ] , 0 , epsilon ) &&
CesiumMath . equalsEpsilon ( rgba [ 2 ] , expected [ 2 ] , 0 , epsilon ) &&
CesiumMath . equalsEpsilon ( rgba [ 3 ] , expected [ 3 ] , 0 , epsilon )
) {
return {
pass : false ,
2022-02-03 04:40:21 +08:00
message : ` Expected context not to render ${ expected } , but rendered: ${ rgba } ` ,
2020-04-17 08:31:36 +08:00
} ;
Migrate Cesium to ES6 Modules
See https://github.com/AnalyticalGraphicsInc/cesium/pull/8224 for details.
eslint
There are a handful of new .eslintrc.json files, mostly to identify the files that are still AMD modules (Sandcastle/Workers). These are needed because you can't change the parser type with a comment directive (since the parser is the thing reading the file). We can finally detect unusued modules! So those have all been cleaned up as well.
requirejs -> rollup & clean-css
requirejs, almond, and karma-requirejs have all been removed. We now use rollup for building and minifying (via uglify) JS code and clean-css for css. These changes are fairly straight-forward and just involve calling rollup instead of requirejs in the build process.
Overall build time is significantly faster. CI is ~11 minutes compared to ~17 in master. Running makeZipFile on my machine takes 69 seconds compared to 112 seconds in master. There's probably plenty of room for additional optimization here too.
We wrote an published a small npm module, rollup-plugin-strip-pragma, for stripping the requirejs pragmas we use out of the release builds. This is maintained in the Tools/rollup-plugin-strip-pragma directory.
As for what we produce. The built version of Cesium is now a UMD module. So it should work anywhere that hasn't made the jump to ES6 yet. For users that were already using the "legacy" combined/minified approach, nothing changes.
One awesome thing about roll-up is that it compiles all of the workers at once and automatically detects shared codes and generates separate bundles under the hood. This means the size of our worker modules shrink dramatically and Cesium itself will load them much faster. The total minified/gzipped size of all workers in master is 2.6 MB compared to 225 KB in this branch! This should be most noticeable on demos like Geometry & Appearances which load lots of workers for the various geometry typs.
roll-up is also used to build Cesium Viewer, which is now an ES6 app.
We use clean-css via gulp and it is also a straightforward change from requirejs that requires no special mention.
Workers
While the spec allows for ES6 Web Workers, no browser actually supports them yet. That means we needed a way to get our workers into non-ES6 form. Thankfully, roll-up can generate AMD modules, which means we now have a build step to compile our Worker source code back into AMD and use the existing TaskProcessor to load and execute them. This build step is part of the standard build task and is called createWorkers. During development, these "built" workers are un-optimized so you can still debug them and read the code.
Since there is a build step, that means if you are changing code that affects a worker, you need to re-run build, or you can use the build-watch task to do it automatically.
The ES6 versions of Worker code has moved into Source/WorkersES6 and we build the workers into their "old home" of Source/Workers. cesiumWorkerBootstrapper and transferTypedArrayTest which were already non-AMD ES5 scripts remain living in the Workers directory.
Surprisingly little was changed about TaskProcessor or the worker system in general, especially considering that I thought this would be one of the major hurdles.
ThirdParty
A lot of our ThirdParty either already had a hand-written wrapper for AMD (which I updated to ES6) or had UMD which created problems when importing the same code in both Node and the browser. I basically had to update the wrapper of every third-party library to fix these problems. In some cases I updated the library version itself (Autolinker, topojson). Nothing to be too concerned about, but future clean-up would be using npm versions of these libraries and auto-generating the wrappers as needed so we don't hand-edit things.
Sandcastle
Sandcastle is eternal and manages to live another day in it's ancient requirejs/dojo 1.x form. Sandcastle now automatically uses the ES6 version of Cesium if it is available and fallsback to the ES5 unminified version if it is now. The built version of Sandcastle always uses CesiumUnminified, just like master. This means Sandcastle still works in IE11 if you run the combine step first (or use the relase zip)
Removed Cesium usage from Sandcastle proper, since it wasn't really needed
Generate a VERSION propertyin the gallery index since Cesium is no longer being included.
Remove requirejs from Sandcastle bucket
Update bucket to use the built version of Cesium if it is available by fallbackto the ES6 version during development.
Standalone.html was also updated
There's a bit of room for further clean-up here, but I think this gets us into master. I did not rename bucket-requirejs.html because I'm pretty sure it would break previously shared demos. We can put in some backwards compatible code later on if we want. (But I'd rather just see a full Sandcastle rewrite).
Specs
Specs are now all ES6, except for TestWorkers, which remain standard JS worker modules. This means you can no longer run the unbuilt unit tests in IE11. No changes for Chrome and Firefox.
Since the specs use ES6 modules and built Cesium is an ES5 UMD, I added a build-specs build step which generates a combined ES5 version of the specs which rely on Cesium as a global variable. We then inject these files into jasmine instead of the standard specs and everything works exactly as it did before. SpecRunner.html has been updated to inject the correct version of the script depending on the build/release query parameters.
The Specs must always use Cesium by importing Source/Cesium.js, this is so we can replace it with the built Cesium as describe above.
There's a bunch of room for clean-up here, such as unifying our two copies of jasmine into a single helper file, but I didn't want to start doing that clean-up as part of this already overly big PR. The important thing is that we can still test the built version and still test on IE/Edge as needed.
I also found and fixed two bugs that were causing failing unit tests, one in BingMapsImageryProviderSpec.js (which was overwriting createImage andnot setting it back) and ShadowVolumeAppearance.js (which had a module level caching bug). I think these may have been the cause of random CI failures in master as well, but only time will tell.
For coverage, we had to switch to karma-coverage-istanbul-instrumenter for native ES6 support, but that's it.
Finally, I updated appveryor to build Cesium and run the built tests under IE. We still don't fail the build for IE, but we should probably fix that if we want to keep it going.
NodeJS
When NODE_ENV is production, we now require in the minified CesiumJS directly, which works great because it's now a UMD module. Otherwise, we use the excellant esmpackage to load individual modules, it was a fairly straightforward swap from our old requirejs usage. We could probably drop esm too if we don't care about debugging or if we provie source maps at some point.
2019-10-03 23:51:23 +08:00
}
2020-04-17 08:31:36 +08:00
}
return {
pass : true ,
} ;
}
function addDefaultMatchers ( debug ) {
return function ( ) {
this . addMatchers ( createDefaultMatchers ( debug ) ) ;
2023-01-13 00:07:41 +08:00
this . addAsyncMatchers ( createDefaultAsyncMatchers ( debug ) ) ;
2020-04-17 08:31:36 +08:00
} ;
}
Migrate Cesium to ES6 Modules
See https://github.com/AnalyticalGraphicsInc/cesium/pull/8224 for details.
eslint
There are a handful of new .eslintrc.json files, mostly to identify the files that are still AMD modules (Sandcastle/Workers). These are needed because you can't change the parser type with a comment directive (since the parser is the thing reading the file). We can finally detect unusued modules! So those have all been cleaned up as well.
requirejs -> rollup & clean-css
requirejs, almond, and karma-requirejs have all been removed. We now use rollup for building and minifying (via uglify) JS code and clean-css for css. These changes are fairly straight-forward and just involve calling rollup instead of requirejs in the build process.
Overall build time is significantly faster. CI is ~11 minutes compared to ~17 in master. Running makeZipFile on my machine takes 69 seconds compared to 112 seconds in master. There's probably plenty of room for additional optimization here too.
We wrote an published a small npm module, rollup-plugin-strip-pragma, for stripping the requirejs pragmas we use out of the release builds. This is maintained in the Tools/rollup-plugin-strip-pragma directory.
As for what we produce. The built version of Cesium is now a UMD module. So it should work anywhere that hasn't made the jump to ES6 yet. For users that were already using the "legacy" combined/minified approach, nothing changes.
One awesome thing about roll-up is that it compiles all of the workers at once and automatically detects shared codes and generates separate bundles under the hood. This means the size of our worker modules shrink dramatically and Cesium itself will load them much faster. The total minified/gzipped size of all workers in master is 2.6 MB compared to 225 KB in this branch! This should be most noticeable on demos like Geometry & Appearances which load lots of workers for the various geometry typs.
roll-up is also used to build Cesium Viewer, which is now an ES6 app.
We use clean-css via gulp and it is also a straightforward change from requirejs that requires no special mention.
Workers
While the spec allows for ES6 Web Workers, no browser actually supports them yet. That means we needed a way to get our workers into non-ES6 form. Thankfully, roll-up can generate AMD modules, which means we now have a build step to compile our Worker source code back into AMD and use the existing TaskProcessor to load and execute them. This build step is part of the standard build task and is called createWorkers. During development, these "built" workers are un-optimized so you can still debug them and read the code.
Since there is a build step, that means if you are changing code that affects a worker, you need to re-run build, or you can use the build-watch task to do it automatically.
The ES6 versions of Worker code has moved into Source/WorkersES6 and we build the workers into their "old home" of Source/Workers. cesiumWorkerBootstrapper and transferTypedArrayTest which were already non-AMD ES5 scripts remain living in the Workers directory.
Surprisingly little was changed about TaskProcessor or the worker system in general, especially considering that I thought this would be one of the major hurdles.
ThirdParty
A lot of our ThirdParty either already had a hand-written wrapper for AMD (which I updated to ES6) or had UMD which created problems when importing the same code in both Node and the browser. I basically had to update the wrapper of every third-party library to fix these problems. In some cases I updated the library version itself (Autolinker, topojson). Nothing to be too concerned about, but future clean-up would be using npm versions of these libraries and auto-generating the wrappers as needed so we don't hand-edit things.
Sandcastle
Sandcastle is eternal and manages to live another day in it's ancient requirejs/dojo 1.x form. Sandcastle now automatically uses the ES6 version of Cesium if it is available and fallsback to the ES5 unminified version if it is now. The built version of Sandcastle always uses CesiumUnminified, just like master. This means Sandcastle still works in IE11 if you run the combine step first (or use the relase zip)
Removed Cesium usage from Sandcastle proper, since it wasn't really needed
Generate a VERSION propertyin the gallery index since Cesium is no longer being included.
Remove requirejs from Sandcastle bucket
Update bucket to use the built version of Cesium if it is available by fallbackto the ES6 version during development.
Standalone.html was also updated
There's a bit of room for further clean-up here, but I think this gets us into master. I did not rename bucket-requirejs.html because I'm pretty sure it would break previously shared demos. We can put in some backwards compatible code later on if we want. (But I'd rather just see a full Sandcastle rewrite).
Specs
Specs are now all ES6, except for TestWorkers, which remain standard JS worker modules. This means you can no longer run the unbuilt unit tests in IE11. No changes for Chrome and Firefox.
Since the specs use ES6 modules and built Cesium is an ES5 UMD, I added a build-specs build step which generates a combined ES5 version of the specs which rely on Cesium as a global variable. We then inject these files into jasmine instead of the standard specs and everything works exactly as it did before. SpecRunner.html has been updated to inject the correct version of the script depending on the build/release query parameters.
The Specs must always use Cesium by importing Source/Cesium.js, this is so we can replace it with the built Cesium as describe above.
There's a bunch of room for clean-up here, such as unifying our two copies of jasmine into a single helper file, but I didn't want to start doing that clean-up as part of this already overly big PR. The important thing is that we can still test the built version and still test on IE/Edge as needed.
I also found and fixed two bugs that were causing failing unit tests, one in BingMapsImageryProviderSpec.js (which was overwriting createImage andnot setting it back) and ShadowVolumeAppearance.js (which had a module level caching bug). I think these may have been the cause of random CI failures in master as well, but only time will tell.
For coverage, we had to switch to karma-coverage-istanbul-instrumenter for native ES6 support, but that's it.
Finally, I updated appveryor to build Cesium and run the built tests under IE. We still don't fail the build for IE, but we should probably fix that if we want to keep it going.
NodeJS
When NODE_ENV is production, we now require in the minified CesiumJS directly, which works great because it's now a UMD module. Otherwise, we use the excellant esmpackage to load individual modules, it was a fairly straightforward swap from our old requirejs usage. We could probably drop esm too if we don't care about debugging or if we provie source maps at some point.
2019-10-03 23:51:23 +08:00
export default addDefaultMatchers ;