File size: 6,915 Bytes
30c32c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const tap = require('tap');
const path = require('path');
const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer;
const makeTestStorage = require('../fixtures/make-test-storage');
const VirtualMachine = require('../../src/virtual-machine');

let vm;
let projectChanged;

tap.beforeEach(() => {
    const projectUri = path.resolve(__dirname, '../fixtures/default.sb2');
    const project = readFileToBuffer(projectUri);

    vm = new VirtualMachine();

    vm.runtime.addListener('PROJECT_CHANGED', () => {
        projectChanged = true;
    });

    vm.attachStorage(makeTestStorage());
    return vm.loadProject(project).then(() => {
        // The test in project_load_changed_state.js tests
        // that loading a project does not emit a project changed
        // event. This setup tries to be agnostic of whether that
        // test is passing or failing.
        projectChanged = false;
    });
});

tap.tearDown(() => process.nextTick(process.exit));

const test = tap.test;

test('Adding a sprite (from sprite2) should emit a project changed event', t => {
    const sprite2Uri = path.resolve(__dirname, '../fixtures/cat.sprite2');
    const sprite2 = readFileToBuffer(sprite2Uri);

    vm.addSprite(sprite2).then(() => {
        t.equal(projectChanged, true);
        t.end();
    });
});

test('Adding a sprite (from sprite3) should emit a project changed event', t => {
    const sprite3Uri = path.resolve(__dirname, '../fixtures/cat.sprite3');
    const sprite3 = readFileToBuffer(sprite3Uri);

    vm.addSprite(sprite3).then(() => {
        t.equal(projectChanged, true);
        t.end();
    });
});

test('Adding a costume should emit a project changed event', t => {
    const newCostume = {
        name: 'costume1',
        baseLayerID: 0,
        baseLayerMD5: 'f9a1c175dbe2e5dee472858dd30d16bb.svg',
        bitmapResolution: 1,
        rotationCenterX: 47,
        rotationCenterY: 55
    };

    vm.addCostume('f9a1c175dbe2e5dee472858dd30d16bb.svg', newCostume).then(() => {
        t.equal(projectChanged, true);
        t.end();
    });
});

test('Adding a costume from library should emit a project changed event', t => {
    const newCostume = {
        name: 'costume1',
        baseLayerID: 0,
        baseLayerMD5: 'f9a1c175dbe2e5dee472858dd30d16bb.svg',
        bitmapResolution: 1,
        rotationCenterX: 47,
        rotationCenterY: 55
    };

    vm.addCostumeFromLibrary('f9a1c175dbe2e5dee472858dd30d16bb.svg', newCostume).then(() => {
        t.equal(projectChanged, true);
        t.end();
    });
});

test('Adding a backdrop should emit a project changed event', t => {
    const newCostume = {
        name: 'costume1',
        baseLayerID: 0,
        baseLayerMD5: 'f9a1c175dbe2e5dee472858dd30d16bb.svg',
        bitmapResolution: 1,
        rotationCenterX: 47,
        rotationCenterY: 55
    };

    vm.addBackdrop('f9a1c175dbe2e5dee472858dd30d16bb.svg', newCostume).then(() => {
        t.equal(projectChanged, true);
        t.end();
    });
});

test('Adding a sound should emit a project changed event', t => {
    const newSound = {
        soundName: 'meow',
        soundID: 0,
        md5: '83c36d806dc92327b9e7049a565c6bff.wav',
        sampleCount: 18688,
        rate: 22050
    };

    vm.addSound(newSound).then(() => {
        t.equal(projectChanged, true);
        t.end();
    });
});

test('Deleting a sprite should emit a project changed event', t => {
    const spriteId = vm.editingTarget.id;

    vm.deleteSprite(spriteId);
    t.equal(projectChanged, true);
    t.end();
});

test('Deleting a costume should emit a project changed event', t => {
    vm.deleteCostume(0);

    t.equal(projectChanged, true);
    t.end();
});

test('Deleting a sound should emit a project changed event', t => {
    vm.deleteSound(0);

    t.equal(projectChanged, true);
    t.end();
});

test('Reordering a sprite should emit a project changed event', t => {
    const sprite3Uri = path.resolve(__dirname, '../fixtures/cat.sprite3');
    const sprite3 = readFileToBuffer(sprite3Uri);

    // Add a new sprite so we have 2 to reorder
    vm.addSprite(sprite3).then(() => {
        // Reset the project changed flag to ignore change from adding new sprite
        projectChanged = false;
        t.equal(vm.runtime.targets.filter(target => !target.isStage).length, 2);
        vm.reorderTarget(2, 1);
        t.equal(projectChanged, true);
        t.end();
    });
});

test('Reordering a costume should emit a project changed event', t => {
    t.equal(vm.editingTarget.sprite.costumes.length, 2);
    const spriteId = vm.editingTarget.id;
    const reordered = vm.reorderCostume(spriteId, 1, 0);
    t.equal(reordered, true);
    t.equal(projectChanged, true);
    t.end();
});

test('Reordering a sound should emit a project changed event', t => {
    const spriteId = vm.editingTarget.id;
    const newSound = {
        soundName: 'meow',
        soundID: 0,
        md5: '83c36d806dc92327b9e7049a565c6bff.wav',
        sampleCount: 18688,
        rate: 22050
    };
    vm.addSound(newSound).then(() => {
        // Reset the project changed flag to ignore change from adding new sound
        projectChanged = false;
        t.equal(vm.editingTarget.sprite.sounds.length, 2);
        const reordered = vm.reorderSound(spriteId, 1, 0);
        t.equal(reordered, true);
        t.equal(projectChanged, true);
        t.end();
    });
});

test('Renaming a sprite should emit a project changed event', t => {
    const spriteId = vm.editingTarget.id;
    vm.renameSprite(spriteId, 'My Sprite');
    t.equal(projectChanged, true);
    t.end();
});

test('Renaming a costume should emit a project changed event', t => {
    vm.renameCostume(0, 'My Costume');
    t.equal(projectChanged, true);
    t.end();
});

test('Renaming a sound should emit a project changed event', t => {
    vm.renameSound(0, 'My Sound');

    t.equal(projectChanged, true);
    t.end();
});

test('Changing sprite info should emit a project changed event', t => {
    const newSpritePosition = {
        x: 10,
        y: 100
    };

    vm.postSpriteInfo(newSpritePosition);
    t.equal(projectChanged, true);
    projectChanged = false;

    const newSpriteDirection = {
        direction: -30
    };

    vm.postSpriteInfo(newSpriteDirection);
    t.equal(projectChanged, true);
    projectChanged = false;

    t.end();

});

test('Editing a vector costume should emit a project changed event', t => {
    const mockSvg = 'svg';
    const mockRotationX = -13;
    const mockRotationY = 25;

    vm.updateSvg(0, mockSvg, mockRotationX, mockRotationY);
    t.equal(projectChanged, true);
    t.end();
});

test('Editing a sound should emit a project changed event', t => {
    const mockSoundBuffer = [];
    const mockSoundEncoding = [];

    vm.updateSoundBuffer(0, mockSoundBuffer, mockSoundEncoding);
    t.equal(projectChanged, true);
    t.end();
});