This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
/
app.ts
419 lines (370 loc) · 11.2 KB
/
app.ts
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import * as MRE from '@microsoft/mixed-reality-extension-sdk';
import * as MRERPC from '@microsoft/mixed-reality-extension-sdk/built/rpc';
import Menu from './menu';
import { Test, TestFactory } from './test';
import { Factories } from './tests';
import destroyActors from './utils/destroyActors';
export const SuccessColor = MRE.Color3.Green();
export const FailureColor = MRE.Color3.Red();
export const NeutralColor = MRE.Color3.Yellow();
export const BackgroundColor = new MRE.Color3(0.25, 0.25, 0.25);
/**
* Functional Test Application. Takes query arguments to the websocket connection
* @param test - Initialize menu on a particular test
* @param autostart - Start the test immediately on user join
* @param nomenu - Do not spawn the controls
*/
export class App {
public assets: MRE.AssetContainer;
private firstUser: MRE.User;
private _connectedUsers: { [id: string]: MRE.User } = {};
public testResults: { [name: string]: boolean } = {};
private activeTestName: string;
private activeTestFactory: TestFactory;
private activeTest: Test = null;
private runPromise: Promise<void> = null;
private contextLabel: MRE.Actor;
private playPauseButton: MRE.Actor;
private playPauseText: MRE.Actor;
private runnerActors: MRE.Actor[];
private sharedActors: MRE.Actor[];
private backgroundMaterial: MRE.Material;
private testRoot: MRE.Actor;
private exclusiveUser: MRE.User;
private exclusiveUserToggle: MRE.Actor;
private exclusiveUserLabel: MRE.Text;
private readonly menu = new Menu(this);
public get context() { return this._context; }
public get connectedUsers() { return this._connectedUsers; }
constructor(private _context: MRE.Context, private params: MRE.ParameterSet, private baseUrl: string) {
this.assets = new MRE.AssetContainer(_context);
this.context.onStarted(() => {
this.setupShared();
if (this.params.test === undefined) {
this.menu.show();
} else {
this.activeTestName = this.params.test as string;
this.activeTestFactory = Factories[this.activeTestName];
this.setupRunner();
}
});
this.context.onUserJoined((user) => this.userJoined(user));
this.context.onUserLeft((user) => this.userLeft(user));
this.backgroundMaterial = this.assets.createMaterial('background', {
color: BackgroundColor
});
this.menu.onSelection((name, factory, user) => {
this.menu.hide();
this.activeTestName = name;
this.activeTestFactory = factory;
this.runTest(user);
});
}
private userJoined(user: MRE.User) {
this.connectedUsers[user.id] = user;
if (!this.firstUser) {
this.firstUser = user;
if (this.params.autorun === 'true' || this.params.nomenu === 'true') {
this.runTest(user);
}
}
}
private userLeft(user: MRE.User) {
delete this.connectedUsers[user.id];
if (user === this.firstUser) {
this.firstUser = this.context.users[0] || null;
if (!this.firstUser) {
this.stopTest().catch(() => { });
}
}
}
private runTest(user: MRE.User) {
// finish setting up runner
if (!this.contextLabel) {
this.setupRunner();
}
// halt the previous test if there is one
(this.activeTest !== null ? this.stopTest() : Promise.resolve())
// start the new test, and save the stop handle
.then(() => {
if (this.playPauseButton) {
this.playPauseButton.appearance.material.color.set(1, 0, 0, 1);
this.playPauseText.text.contents = "Stop";
}
return this.runPromise = this.runTestHelper(user);
})
.then(() => {
if (this.playPauseButton) {
this.playPauseButton.appearance.material.color.set(0, 1, 0, 1);
this.playPauseText.text.contents = "Start";
}
})
// and log unexpected errors
.catch(err => console.log(err));
}
private async runTestHelper(user: MRE.User) {
this.context.rpc.send({ procName: 'functional-test:test-starting' }, this.activeTestName);
console.log(`Test starting: '${this.activeTestName}'`);
const test = this.activeTest = this.activeTestFactory(this, this.baseUrl, user);
this.setOverrideText(test.expectedResultDescription);
this.context.rpc.send({ procName: 'functional-test:test-started' }, this.activeTestName);
console.log(`Test started: '${this.activeTestName}'`);
let success: boolean;
try {
success = await test.run(this.testRoot);
if (!success) {
this.setOverrideText("Test Failed: '${testName}'", FailureColor);
}
} catch (e) {
console.log(e);
this.setOverrideText("Test " + e, FailureColor);
success = false;
}
console.log(`Test complete: '${this.activeTestName}'. Success: ${success}`);
this.context.rpc.send({ procName: 'functional-test:test-complete' }, this.activeTestName, success);
this.testResults[this.activeTestName] = success;
if (success) {
this.setOverrideText(null);
}
test.cleanup();
// Delete all actors
destroyActors(this.context.rootActors.filter(x =>
!this.runnerActors.includes(x) && !this.sharedActors.includes(x)));
}
private async stopTest() {
if (this.activeTest !== null) {
this.activeTest.stop();
await this.runPromise;
this.activeTest = null;
this.runPromise = null;
}
}
public setOverrideText(text: string, color: MRE.Color3 = NeutralColor): void {
if (text) {
this.contextLabel.text.color = color;
this.contextLabel.text.contents = text;
} else {
if (this.testResults[this.activeTestName] === true) {
this.contextLabel.text.color = SuccessColor;
} else if (this.testResults[this.activeTestName] === false) {
this.contextLabel.text.color = FailureColor;
} else {
this.contextLabel.text.color = NeutralColor;
}
this.contextLabel.text.contents = this.activeTest.expectedResultDescription;
}
}
private async toggleExclusiveUser(user: MRE.User) {
if (this.exclusiveUser || !user) {
this.exclusiveUser = null;
this.exclusiveUserLabel.contents = "Inclusive";
} else {
this.exclusiveUser = user;
this.exclusiveUserLabel.contents = `Exclusive to:\n${user.name}`;
}
const wasRunning = !!this.activeTest;
await this.stopTest();
if (this.testRoot) {
this.testRoot.destroy();
}
this.testRoot = MRE.Actor.Create(this.context, {
actor: {
name: 'testRoot',
exclusiveToUser: this.exclusiveUser && this.exclusiveUser.id || undefined
}
});
if (wasRunning) {
this.runTest(user);
}
}
private setupShared() {
// change the exclusive user
if (this.params.exclusive) {
this.exclusiveUser = this.firstUser;
}
this.exclusiveUserToggle = MRE.Actor.Create(this.context, {
actor: {
appearance: {
meshId: this.assets.createBoxMesh('button', 0.25, 0.25, 0.1).id
},
transform: {
local: {
position: { x: -0.875, y: 2.3 }
}
},
collider: {
geometry: { shape: 'auto' }
}
}
});
const label = MRE.Actor.Create(this.context, {
actor: {
parentId: this.exclusiveUserToggle.id,
transform: {
local: {
position: { x: 0.3 }
}
},
text: {
contents: this.exclusiveUser ? `Exclusive to:\n${this.exclusiveUser.name}` : "Inclusive",
height: 0.2,
anchor: MRE.TextAnchorLocation.MiddleLeft
}
}
});
this.exclusiveUserLabel = label.text;
this.exclusiveUserToggle.setBehavior(MRE.ButtonBehavior)
.onButton('released', user => this.toggleExclusiveUser(user));
const floor = MRE.Actor.Create(this.context, {
actor: {
name: 'floor',
appearance: {
meshId: this.assets.createBoxMesh('floor', 2, 0.1, 2.1 ).id,
materialId: this.backgroundMaterial.id
},
transform: {
local: {
position: { x: 0, y: -0.05, z: -1 }
}
},
collider: { geometry: { shape: 'auto' } }
}
});
const wall = MRE.Actor.Create(this.context, {
actor: {
name: 'wall',
appearance: {
meshId: this.assets.createBoxMesh('wall', 2, 2, 0.1).id,
materialId: this.backgroundMaterial.id
},
transform: {
local: {
position: { x: 0, y: 1, z: 0.1 }
}
},
collider: { geometry: { shape: 'auto' } }
}
});
this.sharedActors = [this.exclusiveUserToggle, label, wall, floor];
}
private setupRunner() {
// Main label at the top of the stage
this.contextLabel = MRE.Actor.Create(this.context, {
actor: {
name: 'contextLabel',
text: {
contents: this.activeTestName,
height: 0.2,
anchor: MRE.TextAnchorLocation.MiddleCenter,
justify: MRE.TextJustify.Center,
color: NeutralColor
},
transform: {
local: {
position: { y: 1.8 }
}
}
}
});
this.testRoot = MRE.Actor.Create(this.context, {
actor: {
name: 'testRoot',
exclusiveToUser: this.exclusiveUser && this.exclusiveUser.id || undefined
}
});
if (this.params.nomenu === 'true') {
this.runnerActors = [this.contextLabel];
return;
}
// start or stop the active test
const ppMat = this.assets.createMaterial('pp', {
color: MRE.Color3.Green().toJSON()
});
const ppMesh = this.assets.createBoxMesh('playpause', 0.7, 0.3, 0.1);
this.playPauseButton = MRE.Actor.Create(this.context, {
actor: {
name: 'playpause',
appearance: {
meshId: ppMesh.id,
materialId: ppMat.id
},
transform: {
local: {
position: { x: -0.65, y: 0.15, z: -1.95 }
}
},
collider: { geometry: { shape: 'auto' } }
}
});
this.playPauseText = MRE.Actor.Create(this.context, {
actor: {
parentId: this.playPauseButton.id,
transform: {
local: {
position: { z: -0.1 }
}
},
text: {
contents: "Start",
height: 0.15,
anchor: MRE.TextAnchorLocation.MiddleCenter,
justify: MRE.TextJustify.Center,
color: NeutralColor
}
}
});
this.playPauseButton.setBehavior(MRE.ButtonBehavior)
.onButton("released", user => {
if (this.activeTest === null) {
this.runTest(user);
} else {
this.stopTest().catch(() => { });
}
});
const menuButton = MRE.Actor.Create(this.context, {
actor: {
name: 'menu',
appearance: {
meshId: ppMesh.id
},
transform: {
local: {
position: { x: 0.65, y: 0.15, z: -1.95 }
}
},
collider: { geometry: { shape: 'auto' } }
}
});
const menuText = MRE.Actor.Create(this.context, {
actor: {
parentId: menuButton.id,
transform: {
local: {
position: { z: -0.1 }
}
},
text: {
contents: "Return",
height: 0.15,
anchor: MRE.TextAnchorLocation.MiddleCenter,
justify: MRE.TextJustify.Center,
color: MRE.Color3.Black()
}
}
});
menuButton.setBehavior(MRE.ButtonBehavior)
.onButton("released", async () => {
await this.stopTest();
[this.contextLabel, this.playPauseButton, this.playPauseText]
= this.runnerActors
= destroyActors(this.runnerActors);
this.menu.show();
});
this.runnerActors = [this.contextLabel, this.testRoot, this.playPauseButton,
this.playPauseText, menuButton, menuText];
}
}