@@ -7,55 +7,94 @@ const chromeless = new Chromeless();
7
7
const indexHTML = path . resolve ( __dirname , 'index.html' ) ;
8
8
const testDir = ( ...args ) => path . resolve ( __dirname , 'pick-tests' , ...args ) ;
9
9
10
- const runFile = ( file , script ) =>
10
+ const runFile = ( file , action , script ) =>
11
11
// start each test by going to the index.html, and loading the scratch file
12
12
chromeless . goto ( `file://${ indexHTML } ` )
13
13
. setFileInput ( '#file' , testDir ( file ) )
14
14
// the index.html handler for file input will add a #loaded element when it
15
15
// finishes.
16
16
. wait ( '#loaded' )
17
- . evaluate ( script )
17
+ . evaluate ( `function () {return ( ${ script } )( ${ action } );}` )
18
18
;
19
19
20
20
// immediately invoked async function to let us wait for each test to finish before starting the next.
21
21
( async ( ) => {
22
22
23
- await test ( 'pick tests' , async t => {
24
-
25
- const results = await runFile ( 'test-mouse-touch.sb2' , ( ) => {
26
- vm . greenFlag ( ) ;
27
- const sendResults = [ ] ;
28
-
29
- const idToTargetName = id => {
30
- const target = vm . runtime . targets . find ( tar => tar . drawableID === id ) ;
31
- if ( ! target ) {
32
- return `[Unknown drawableID: ${ id } ]` ;
33
- }
34
- return target . sprite . name ;
35
- } ;
36
- const sprite = vm . runtime . targets . find ( target => target . sprite . name === 'Sprite1' ) ;
37
-
38
- sendResults . push ( [ 'center' , idToTargetName ( render . pick ( 240 , 180 ) ) ] ) ;
39
- sendResults . push ( [ 'left' , idToTargetName ( render . pick ( 200 , 180 ) ) ] ) ;
40
- sendResults . push ( [ 'over' , render . drawableTouching ( sprite . drawableID , 240 , 180 ) ] ) ;
41
- sprite . setVisible ( false ) ;
42
- sendResults . push ( [ 'hidden sprite pick center' , idToTargetName ( render . pick ( 240 , 180 ) ) ] ) ;
43
- sendResults . push ( [ 'hidden over' , render . drawableTouching ( sprite . drawableID , 240 , 180 ) ] ) ;
44
- return sendResults ;
23
+ async function testOperation ( name , action , expect ) {
24
+ await test ( name , async t => {
25
+
26
+ const results = await runFile ( 'test-mouse-touch.sb2' , action , action => {
27
+ vm . greenFlag ( ) ;
28
+ const sendResults = [ ] ;
29
+
30
+ const idToTargetName = id => {
31
+ const target = vm . runtime . targets . find ( tar => tar . drawableID === id ) ;
32
+ if ( ! target ) {
33
+ return `[Unknown drawableID: ${ id } ]` ;
34
+ }
35
+ return target . sprite . name ;
36
+ } ;
37
+ const sprite = vm . runtime . targets . find ( target => target . sprite . name === 'Sprite1' ) ;
38
+
39
+ action ( {
40
+ sendResults,
41
+ idToTargetName,
42
+ render,
43
+ sprite
44
+ } ) ;
45
+ return sendResults ;
46
+ } ) ;
47
+
48
+ t . plan ( expect . length ) ;
49
+ for ( let x = 0 ; x < expect . length ; x ++ ) {
50
+ t . deepEqual ( results [ x ] , expect [ x ] , expect [ x ] [ 0 ] ) ;
51
+ }
52
+ t . end ( ) ;
45
53
} ) ;
46
- const expect = [
47
- [ 'center' , 'Sprite1' ] ,
48
- [ 'left' , 'Stage' ] ,
49
- [ 'over' , true ] ,
50
- [ 'hidden sprite pick center' , 'Stage' ] ,
51
- [ 'hidden over' , true ]
52
- ] ;
53
- t . plan ( expect . length ) ;
54
- for ( let x = 0 ; x < expect . length ; x ++ ) {
55
- t . deepEqual ( results [ x ] , expect [ x ] , expect [ x ] [ 0 ] ) ;
54
+ }
55
+
56
+ const tests = [
57
+ {
58
+ name : 'pick Sprite1' ,
59
+ action : ( { sendResults, render, idToTargetName} ) => {
60
+ sendResults . push ( [ 'center' , idToTargetName ( render . pick ( 240 , 180 ) ) ] ) ;
61
+ } ,
62
+ expect : [ [ 'center' , 'Sprite1' ] ]
63
+ } ,
64
+ {
65
+ name : 'pick Stage' ,
66
+ action : ( { sendResults, render, idToTargetName} ) => {
67
+ sendResults . push ( [ 'left' , idToTargetName ( render . pick ( 200 , 180 ) ) ] ) ;
68
+ } ,
69
+ expect : [ [ 'left' , 'Stage' ] ]
70
+ } ,
71
+ {
72
+ name : 'touching Sprite1' ,
73
+ action : ( { sprite, sendResults, render, idToTargetName} ) => {
74
+ sendResults . push ( [ 'over' , render . drawableTouching ( sprite . drawableID , 240 , 180 ) ] ) ;
75
+ } ,
76
+ expect : [ [ 'over' , true ] ]
77
+ } ,
78
+ {
79
+ name : 'pick Stage through hidden Sprite1' ,
80
+ action : ( { sprite, sendResults, render, idToTargetName} ) => {
81
+ sprite . setVisible ( false ) ;
82
+ sendResults . push ( [ 'hidden sprite pick center' , idToTargetName ( render . pick ( 240 , 180 ) ) ] ) ;
83
+ } ,
84
+ expect : [ [ 'hidden sprite pick center' , 'Stage' ] ]
85
+ } ,
86
+ {
87
+ name : 'touching hidden Sprite1' ,
88
+ action : ( { sprite, sendResults, render, idToTargetName} ) => {
89
+ sprite . setVisible ( false ) ;
90
+ sendResults . push ( [ 'hidden over' , render . drawableTouching ( sprite . drawableID , 240 , 180 ) ] ) ;
91
+ } ,
92
+ expect : [ [ 'hidden over' , true ] ]
56
93
}
57
- t . end ( ) ;
58
- } ) ;
94
+ ] ;
95
+ for ( const { name, action, expect} of tests ) {
96
+ await testOperation ( name , action , expect ) ;
97
+ }
59
98
60
99
// close the browser window we used
61
100
await chromeless . end ( ) ;
0 commit comments