-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathUnitTest.js
290 lines (240 loc) · 8.37 KB
/
UnitTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/****************************************************************************
http://www.cocos2d-html5.org
http://www.cocos2d-iphone.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
var unitTestSceneIdx = -1;
//------------------------------------------------------------------
//
// UnitTestBase
//
//------------------------------------------------------------------
var UnitTestBase = BaseTestLayer.extend({
_title:"",
_subtitle:"",
onRestartCallback:function (sender) {
var s = new UnitTestScene();
s.addChild(restartUnitTest());
director.replaceScene(s);
},
onNextCallback:function (sender) {
var s = new UnitTestScene();
s.addChild(nextUnitTest());
director.replaceScene(s);
},
onBackCallback:function (sender) {
var s = new UnitTestScene();
s.addChild(previousUnitTest());
director.replaceScene(s);
},
// automation
numberOfPendingTests:function() {
return ( (arrayOfUnitTest.length-1) - unitTestSceneIdx );
},
getTestNumber:function() {
return unitTestSceneIdx;
}
});
//------------------------------------------------------------------
//
// RectUnitTest
//
//------------------------------------------------------------------
var RectUnitTest = UnitTestBase.extend({
_title:"Rect Unit Test",
_subtitle:"See console for possible errors",
onEnter:function () {
this._super();
this.runTest();
},
runTest:function () {
var ret = [];
var rectA;
var rectB;
var rectC;
var point;
var r;
this.log("Test 1: rectIntersectsRect 1");
rectA = cc.rect(0,0,5,10);
rectB = cc.rect(4,9,5,10);
r = cc.rectIntersectsRect(rectA, rectB);
if(!r)
throw "Fail rectIntersectsRect 1";
ret.push(r);
this.log("Test 2: rectIntersectsRect 2");
rectA = cc.rect(0,0,5,10);
rectB = cc.rect(40,90,5,10);
r = cc.rectIntersectsRect(rectA, rectB);
if(r)
throw "Fail rectIntersectsRect 2";
ret.push(r);
this.log("Test 3: rectIntersection");
rectA = cc.rect(0,0,5,10);
rectB = cc.rect(4,9,5,10);
rectC = cc.rectIntersection(rectA, rectB);
r = cc.rectEqualToRect(rectC, cc.rect(4,9,1,1) );
if(!r)
throw "Fail rectIntersection";
ret.push(r);
this.log("Test 4: rectUnion");
rectA = cc.rect(0,0,5,10);
rectB = cc.rect(4,9,5,10);
rectC = cc.rectUnion(rectA, rectB);
r = cc.rectEqualToRect(rectC, cc.rect(0,0,9,19) );
if(!r)
throw "Fail rectUnion";
ret.push(r);
this.log("Test 5: rectContainsPoint 1");
rectA = cc.rect(0,0,5,10);
point = cc.p(1,1);
r = cc.rectContainsPoint(rectA, point);
if(!r)
throw "Fail rectContainsPoint 1";
ret.push(r);
this.log("Test 6: rectContainsPoint 2");
rectA = cc.rect(0,0,5,10);
point = cc.p(1,-1);
r = cc.rectContainsPoint(rectA, point);
if(r)
throw "Fail rectContainsPoint 2";
ret.push(r);
this.log("Test 7: rect property x");
rectA = cc.rect(1,2,3,4);
if( rectA.x != 1)
throw "Fail rect property x";
ret.push(rectA.x);
this.log("Test 8: rect property y");
rectA = cc.rect(1,2,3,4);
if( rectA.y != 2)
throw "Fail rect property y";
ret.push(rectA.y);
this.log("Test 9: rect property width");
rectA = cc.rect(1,2,3,4);
if( rectA.width != 3)
throw "Fail rect property width";
ret.push(rectA.width);
this.log("Test 10: rect property height");
rectA = cc.rect(1,2,3,4);
if( rectA.height != 4)
throw "Fail rect property height";
ret.push(rectA.height);
this.log("Test 11: getBoundingBox()");
var node = cc.Node.create();
node.setContentSize( cc.size(99,101) );
var bb = node.getBoundingBox();
if (!bb.size)
throw "Fail getBoundingBox().size";
if (!bb.origin)
throw "Fail getBoundingBox().origin";
if( bb.size.height != 101 || bb.size.width != 99)
throw "Fail getBoundingBox().size values";
if (bb.origin.x != node.getPositionX() || bb.origin.y != node.getPositionY())
throw "Fail getBoundingBox().origin values";
ret.push( bb.height );
ret.push( bb.width );
return ret;
},
//
// Automation
//
testDuration:0.1,
getExpectedResult:function() {
var ret = [true,false,true,true,true,false,1,2,3,4,101,99];
return JSON.stringify(ret);
},
getCurrentResult:function() {
var ret = this.runTest();
return JSON.stringify(ret);
}
});
//------------------------------------------------------------------
//
// DictionaryToFromTest
//
//------------------------------------------------------------------
var DictionaryToFromTest = UnitTestBase.extend({
_title:"Dictionary To/From Test",
_subtitle:"Sends and receives a dictionary to JSB",
ctor:function() {
this._super();
this.runTest();
},
runTest:function() {
var frameCache = cc.SpriteFrameCache.getInstance();
frameCache.addSpriteFrames(s_grossiniPlist);
// Purge previously loaded animation
var animCache = cc.AnimationCache.getInstance();
animCache.addAnimations(s_animations2Plist);
var normal = animCache.getAnimation("dance_1");
var frame = normal.getFrames()[0];
var dict = frame.getUserInfo();
this.log( JSON.stringify(dict) );
frame.setUserInfo( {
"array":[1,2,3,"hello world"],
"bool0":0, // false XXX
"bool1":1, // true XXX
"dict":{"key1":"value1", "key2":2},
"number":42,
"string":"hello!"
});
dict = frame.getUserInfo();
this.log(JSON.stringify(dict));
return dict;
},
//
// Automation
//
testDuration:0.1,
getExpectedResult:function() {
var ret = this.sortObject( {"array":[1,2,3,"hello world"],"bool0":0,"bool1":1,"dict":{"key1":"value1","key2":2},"number":42,"string":"hello!"} );
return JSON.stringify(ret);
},
getCurrentResult:function() {
var ret = this.sortObject( this.runTest() );
return JSON.stringify(ret);
}
});
var UnitTestScene = TestScene.extend({
runThisTest:function () {
unitTestSceneIdx = -1;
var layer = nextUnitTest();
this.addChild(layer);
director.replaceScene(this);
}
});
//
// Flow control
//
var arrayOfUnitTest = [
RectUnitTest,
DictionaryToFromTest
];
var nextUnitTest = function () {
unitTestSceneIdx++;
unitTestSceneIdx = unitTestSceneIdx % arrayOfUnitTest.length;
return new arrayOfUnitTest[unitTestSceneIdx]();
};
var previousUnitTest = function () {
unitTestSceneIdx--;
if (unitTestSceneIdx < 0)
unitTestSceneIdx += arrayOfUnitTest.length;
return new arrayOfUnitTest[unitTestSceneIdx]();
};
var restartUnitTest = function () {
return new arrayOfUnitTest[unitTestSceneIdx]();
};