forked from CreateJS/EaselJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UtilityMethodsSpec.js
190 lines (151 loc) · 5.51 KB
/
UtilityMethodsSpec.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
describe("Utlity Methods", function () {
beforeEach(function () {
createjs.Ticker.reset();
createjs.Ticker.init();
});
it("indexOf()", function () {
var arr = [1, 2, 3, 4, 5];
expect(createjs.indexOf(arr, 3)).toBe(2);
});
describe("Ticker", function () {
it("createjs.Ticker.addEventListener(tick) evt.time", function (done) {
setTimeout(function () {
var tick = function (evt) {
expect(evt.time).toBeInRange(createjs.Ticker.getTime(), 2);
done();
createjs.Ticker.removeEventListener("tick", tick);
}
createjs.Ticker.addEventListener("tick", tick);
}, 1000);
});
it("createjs.Ticker.addEventListener(tick) evt.delta", function (done) {
setTimeout(function () {
var tick = function (evt) {
expect(evt.delta).toBeInRange(createjs.Ticker.getInterval(), 5);
done();
createjs.Ticker.removeEventListener("tick", tick);
}
createjs.Ticker.addEventListener("tick", tick);
}, 1000);
});
it("createjs.Ticker.addEventListener(tick) evt.runTime", function (done) {
setTimeout(function () {
var tick = function (evt) {
expect(evt.runTime).toBeInRange(createjs.Ticker.getTime() | 0, 1);
done();
createjs.Ticker.removeEventListener("tick", tick);
}
createjs.Ticker.addEventListener("tick", tick);
}, 1000);
});
it("setPaused", function (done) {
createjs.Ticker.setPaused(true);
// Should be paused
expect(createjs.Ticker.getPaused()).toBe(true);
// tick event should also also be paused.
var func = function (evt) {
expect(evt.paused).toBe(true);
createjs.Ticker.removeEventListener("tick", func);
done();
};
createjs.Ticker.addEventListener("tick", func);
});
it("getFPS", function () {
createjs.Ticker.setInterval(40);
expect(createjs.Ticker.getFPS()).toBe(25);
expect(createjs.Ticker.getInterval()).toBe(40);
});
it("setFPS", function (done) {
createjs.Ticker.setFPS(40);
setTimeout(function () {
expect(createjs.Ticker.getTime() | 0).toBeInRange(40 * 2, 3);
done();
}, 40 * 2);
});
it("getMeasuredFPS", function (done) {
setTimeout(function () {
expect(createjs.Ticker.getMeasuredFPS()).toBeInRange(createjs.Ticker.getFPS(), 5);
done();
}, 1000);
});
it("getMeasuredTickTime", function () {
expect(createjs.Ticker.getMeasuredTickTime() | 0).not.toBe(null);
});
it("getTicks", function (done) {
setTimeout(function () {
expect(createjs.Ticker.getTicks()).toBeInRange(createjs.Ticker.getFPS(), 5);
done();
}, 1000);
});
it("should advance by 1 seconds - getTime()", function (done) {
setTimeout(function () {
expect(createjs.Ticker.getTime()).toBeInRange(1000, 5);
done();
}, 1000);
});
it("getEventTime", function (done) {
setTimeout(function () {
var expected = (createjs.Ticker.getInterval() * createjs.Ticker.getFPS()) - createjs.Ticker.getInterval();
// Firefox needs the higest range (on average)
expect(createjs.Ticker.getEventTime() | 0).toBeInRange(expected, 40);
done();
}, 1000);
});
});
describe("SpriteSheetBuilder", function () {
beforeEach(function () {
var circle = new createjs.Shape();
circle.graphics.beginFill("#F00").drawCircle(0, 0, 30).beginFill("#C00").drawCircle(0, 0, 10);
circle.setTransform(0, 0);
var square = new createjs.Container();
var squareBg = square.addChild(new createjs.Shape());
squareBg.graphics.beginFill("#00F").drawRect(0, 0, 80, 80);
var squareFld = square.addChild(new createjs.Text("1", "bold 72px Arial", "#9BF"));
squareFld.textBaseline = "top";
squareFld.textAlign = "center";
squareFld.x = 40;
square.bounds = new createjs.Rectangle(0, 0, 80, 80);
// create the sprite sheet builder:
this.builder = new createjs.SpriteSheetBuilder();
var index = this.builder.addFrame(circle, new createjs.Rectangle(-30, -30, 60, 60));
this.builder.addAnimation("circle", index);
var frames = [];
for (var i = 0; i < 5; i++) {
index = this.builder.addFrame(square, null, 1, function (target, data) {
squareFld.text = data;
}, i);
frames.push(index);
}
this.builder.addAnimation("square", [1], true);
this.builder.addAnimation("square2", [2], true);
});
it("should build synchronously", function (done) {
var spriteSheet = this.builder.build();
var circle2 = new createjs.Sprite(spriteSheet, "circle");
this.stage.addChild(circle2).set({x: 40, y: 120});
var circle3 = new createjs.Sprite(spriteSheet, "circle");
this.stage.addChild(circle3).set({x: 120, y: 120});
var square2 = new createjs.Sprite(spriteSheet, "square");
this.stage.addChild(square2);
var square3 = new createjs.Sprite(spriteSheet, "square2");
this.stage.addChild(square3).set({x: 80});
this.compareBaseLine("assets/SpriteSheetBuilder.png", done, expect, 0.0075);
});
it("should build async", function (done) {
var _this = this;
this.builder.addEventListener("complete", function () {
var spriteSheet = _this.builder.spriteSheet;
var circle2 = new createjs.Sprite(spriteSheet, "circle");
_this.stage.addChild(circle2).set({x: 40, y: 120});
var circle3 = new createjs.Sprite(spriteSheet, "circle");
_this.stage.addChild(circle3).set({x: 120, y: 120});
var square2 = new createjs.Sprite(spriteSheet, "square");
_this.stage.addChild(square2);
var square3 = new createjs.Sprite(spriteSheet, "square2");
_this.stage.addChild(square3).set({x: 80});
_this.compareBaseLine("assets/SpriteSheetBuilder.png", done, expect, 0.0075);
});
this.builder.buildAsync();
});
});
});