From 618c0fce1eab401a7d15debdd8fe54b3e7053d4c Mon Sep 17 00:00:00 2001 From: Evan Hahn <69474926+EvanHahn-Signal@users.noreply.github.com> Date: Wed, 4 Nov 2020 11:44:32 -0600 Subject: [PATCH] test-electron: wait to run tests until protos are ready --- libtextsecure/protobufs.js | 60 +++++++++++++++++++++++------------ libtextsecure/test/index.html | 6 ++-- test/index.html | 12 ++++--- ts/textsecure.d.ts | 4 ++- 4 files changed, 54 insertions(+), 28 deletions(-) diff --git a/libtextsecure/protobufs.js b/libtextsecure/protobufs.js index ee16ebd04..1c0a910a2 100644 --- a/libtextsecure/protobufs.js +++ b/libtextsecure/protobufs.js @@ -5,11 +5,40 @@ // eslint-disable-next-line func-names (function() { - window.textsecure = window.textsecure || {}; - window.textsecure.protobuf = {}; + const FILES_TO_LOAD = [ + 'SignalService.proto', + 'SignalStorage.proto', + 'SubProtocol.proto', + 'DeviceMessages.proto', + 'Stickers.proto', - function loadProtoBufs(filename) { - return dcodeIO.ProtoBuf.loadProtoFile( + // Just for encrypting device names + 'DeviceName.proto', + + // Metadata-specific protos + 'UnidentifiedDelivery.proto', + + // Groups + 'Groups.proto', + ]; + + let remainingFilesToLoad = FILES_TO_LOAD.length; + const hasFinishedLoading = () => remainingFilesToLoad <= 0; + let onLoadCallbacks = []; + + window.textsecure = window.textsecure || {}; + window.textsecure.protobuf = { + onLoad: callback => { + if (hasFinishedLoading()) { + setTimeout(callback, 0); + } else { + onLoadCallbacks.push(callback); + } + }, + }; + + FILES_TO_LOAD.forEach(filename => { + dcodeIO.ProtoBuf.loadProtoFile( { root: window.PROTO_ROOT, file: filename }, (error, result) => { if (error) { @@ -29,22 +58,13 @@ for (const protoName in protos) { textsecure.protobuf[protoName] = protos[protoName]; } + + remainingFilesToLoad -= 1; + if (hasFinishedLoading()) { + onLoadCallbacks.forEach(callback => callback()); + onLoadCallbacks = []; + } } ); - } - - loadProtoBufs('SignalService.proto'); - loadProtoBufs('SignalStorage.proto'); - loadProtoBufs('SubProtocol.proto'); - loadProtoBufs('DeviceMessages.proto'); - loadProtoBufs('Stickers.proto'); - - // Just for encrypting device names - loadProtoBufs('DeviceName.proto'); - - // Metadata-specific protos - loadProtoBufs('UnidentifiedDelivery.proto'); - - // Groups - loadProtoBufs('Groups.proto'); + }); })(); diff --git a/libtextsecure/test/index.html b/libtextsecure/test/index.html index fe1260873..0f6b58901 100644 --- a/libtextsecure/test/index.html +++ b/libtextsecure/test/index.html @@ -52,8 +52,10 @@ diff --git a/test/index.html b/test/index.html index 0b69fd374..b3883d2ab 100644 --- a/test/index.html +++ b/test/index.html @@ -397,13 +397,15 @@ diff --git a/ts/textsecure.d.ts b/ts/textsecure.d.ts index 65ca1ee84..6af49700d 100644 --- a/ts/textsecure.d.ts +++ b/ts/textsecure.d.ts @@ -212,7 +212,9 @@ type SubProtocolProtobufTypes = { WebSocketResponseMessage: typeof WebSocketResponseMessageClass; }; -type ProtobufCollectionType = DeviceMessagesProtobufTypes & +type ProtobufCollectionType = { + onLoad: (callback: () => unknown) => void; +} & DeviceMessagesProtobufTypes & DeviceNameProtobufTypes & GroupsProtobufTypes & SignalServiceProtobufTypes &