2018-01-19 03:59:58 +08:00
/ *
Distributed under both the W3C Test Suite License [ 1 ] and the W3C
3 - clause BSD License [ 2 ] . To contribute to a W3C Test Suite , see the
policies and contribution forms [ 3 ] .
[ 1 ] http : //www.w3.org/Consortium/Legal/2008/04-testsuite-license
[ 2 ] http : //www.w3.org/Consortium/Legal/2008/03-bsd-license
[ 3 ] http : //www.w3.org/2004/10/27-testcases
* /
//
// Helper Functions for NavigationTiming W3C tests
//
2018-04-29 12:11:55 +02:00
var performanceNamespace = self . performance ;
2018-01-19 03:59:58 +08:00
var timingAttributes = [
'connectEnd' ,
'connectStart' ,
'domComplete' ,
'domContentLoadedEventEnd' ,
'domContentLoadedEventStart' ,
'domInteractive' ,
'domLoading' ,
'domainLookupEnd' ,
'domainLookupStart' ,
'fetchStart' ,
'loadEventEnd' ,
'loadEventStart' ,
'navigationStart' ,
'redirectEnd' ,
'redirectStart' ,
'requestStart' ,
'responseEnd' ,
'responseStart' ,
'unloadEventEnd' ,
'unloadEventStart'
] ;
var namespace _check = false ;
//
// All test() functions in the WebPerf test suite should use wp_test() instead.
//
// wp_test() validates the window.performance namespace exists prior to running tests and
// immediately shows a single failure if it does not.
//
function wp _test ( func , msg , properties )
{
// only run the namespace check once
if ( ! namespace _check )
{
namespace _check = true ;
if ( performanceNamespace === undefined || performanceNamespace == null )
{
// show a single error that window.performance is undefined
test ( function ( ) { assert _true ( performanceNamespace !== undefined && performanceNamespace != null , "window.performance is defined and not null" ) ; } , "window.performance is defined and not null." , { author : "W3C http://www.w3.org/" , help : "http://www.w3.org/TR/navigation-timing/#sec-window.performance-attribute" , assert : "The window.performance attribute provides a hosting area for performance related attributes. " } ) ;
}
}
test ( func , msg , properties ) ;
}
function test _namespace ( child _name , skip _root )
{
if ( skip _root === undefined ) {
var msg = 'window.performance is defined' ;
wp _test ( function ( ) { assert _true ( performanceNamespace !== undefined , msg ) ; } , msg , { author : "W3C http://www.w3.org/" , help : "http://www.w3.org/TR/navigation-timing/#sec-window.performance-attribute" , assert : "The window.performance attribute provides a hosting area for performance related attributes. " } ) ;
}
if ( child _name !== undefined ) {
var msg2 = 'window.performance.' + child _name + ' is defined' ;
wp _test ( function ( ) { assert _true ( performanceNamespace [ child _name ] !== undefined , msg2 ) ; } , msg2 , { author : "W3C http://www.w3.org/" , help : "http://www.w3.org/TR/navigation-timing/#sec-window.performance-attribute" , assert : "The window.performance attribute provides a hosting area for performance related attributes. " } ) ;
}
}
function test _attribute _exists ( parent _name , attribute _name , properties )
{
var msg = 'window.performance.' + parent _name + '.' + attribute _name + ' is defined.' ;
wp _test ( function ( ) { assert _true ( performanceNamespace [ parent _name ] [ attribute _name ] !== undefined , msg ) ; } , msg , properties ) ;
}
function test _enum ( parent _name , enum _name , value , properties )
{
var msg = 'window.performance.' + parent _name + '.' + enum _name + ' is defined.' ;
wp _test ( function ( ) { assert _true ( performanceNamespace [ parent _name ] [ enum _name ] !== undefined , msg ) ; } , msg , properties ) ;
msg = 'window.performance.' + parent _name + '.' + enum _name + ' = ' + value ;
wp _test ( function ( ) { assert _equals ( performanceNamespace [ parent _name ] [ enum _name ] , value , msg ) ; } , msg , properties ) ;
}
function test _timing _order ( attribute _name , greater _than _attribute , properties )
{
// ensure it's not 0 first
var msg = "window.performance.timing." + attribute _name + " > 0" ;
wp _test ( function ( ) { assert _true ( performanceNamespace . timing [ attribute _name ] > 0 , msg ) ; } , msg , properties ) ;
// ensure it's in the right order
msg = "window.performance.timing." + attribute _name + " >= window.performance.timing." + greater _than _attribute ;
wp _test ( function ( ) { assert _true ( performanceNamespace . timing [ attribute _name ] >= performanceNamespace . timing [ greater _than _attribute ] , msg ) ; } , msg , properties ) ;
}
function test _timing _greater _than ( attribute _name , greater _than , properties )
{
var msg = "window.performance.timing." + attribute _name + " > " + greater _than ;
test _greater _than ( performanceNamespace . timing [ attribute _name ] , greater _than , msg , properties ) ;
}
function test _timing _equals ( attribute _name , equals , msg , properties )
{
var test _msg = msg || "window.performance.timing." + attribute _name + " == " + equals ;
test _equals ( performanceNamespace . timing [ attribute _name ] , equals , test _msg , properties ) ;
}
//
// Non-test related helper functions
//
function sleep _milliseconds ( n )
{
var start = new Date ( ) . getTime ( ) ;
while ( true ) {
if ( ( new Date ( ) . getTime ( ) - start ) >= n ) break ;
}
}
//
// Common helper functions
//
function test _true ( value , msg , properties )
{
wp _test ( function ( ) { assert _true ( value , msg ) ; } , msg , properties ) ;
}
function test _equals ( value , equals , msg , properties )
{
wp _test ( function ( ) { assert _equals ( value , equals , msg ) ; } , msg , properties ) ;
}
function test _greater _than ( value , greater _than , msg , properties )
{
wp _test ( function ( ) { assert _true ( value > greater _than , msg ) ; } , msg , properties ) ;
}
function test _greater _or _equals ( value , greater _than , msg , properties )
{
wp _test ( function ( ) { assert _true ( value >= greater _than , msg ) ; } , msg , properties ) ;
}
function test _not _equals ( value , notequals , msg , properties )
{
wp _test ( function ( ) { assert _true ( value !== notequals , msg ) ; } , msg , properties ) ;
}