Bug 1845311 - [Part 2] Use ChromeUtils.defineLazyGetter in more places r=arai,webcomp...
[gecko.git] / netwerk / test / unit / head_webtransport.js
blob99432e950d2e1b42aef0de742d4d29cbd6371e2a
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 /* import-globals-from head_cookies.js */
9 let WebTransportListener = function () {};
11 WebTransportListener.prototype = {
12   onSessionReady(sessionId) {
13     info("SessionId " + sessionId);
14     this.ready();
15   },
16   onSessionClosed(errorCode, reason) {
17     info("Error: " + errorCode + " reason: " + reason);
18     if (this.closed) {
19       this.closed();
20     }
21   },
22   onIncomingBidirectionalStreamAvailable(stream) {
23     info("got incoming bidirectional stream");
24     this.streamAvailable(stream);
25   },
26   onIncomingUnidirectionalStreamAvailable(stream) {
27     info("got incoming unidirectional stream");
28     this.streamAvailable(stream);
29   },
30   onDatagramReceived(data) {
31     info("got datagram");
32     if (this.onDatagram) {
33       this.onDatagram(data);
34     }
35   },
36   onMaxDatagramSize(size) {
37     info("max datagram size: " + size);
38     if (this.onMaxDatagramSize) {
39       this.onMaxDatagramSize(size);
40     }
41   },
42   onOutgoingDatagramOutCome(id, outcome) {
43     if (this.onDatagramOutcome) {
44       this.onDatagramOutcome({ id, outcome });
45     }
46   },
48   QueryInterface: ChromeUtils.generateQI(["WebTransportSessionEventListener"]),
51 function WebTransportStreamCallback() {}
53 WebTransportStreamCallback.prototype = {
54   QueryInterface: ChromeUtils.generateQI(["nsIWebTransportStreamCallback"]),
56   onBidirectionalStreamReady(aStream) {
57     Assert.ok(aStream != null);
58     this.finish(aStream);
59   },
60   onUnidirectionalStreamReady(aStream) {
61     Assert.ok(aStream != null);
62     this.finish(aStream);
63   },
64   onError(aError) {
65     this.finish(aError);
66   },
69 function StreamStatsCallback() {}
71 StreamStatsCallback.prototype = {
72   QueryInterface: ChromeUtils.generateQI([
73     "nsIWebTransportStreamStatsCallback",
74   ]),
76   onSendStatsAvailable(aStats) {
77     Assert.ok(aStats != null);
78     this.finish(aStats);
79   },
80   onReceiveStatsAvailable(aStats) {
81     Assert.ok(aStats != null);
82     this.finish(aStats);
83   },
86 function inputStreamReader() {}
88 inputStreamReader.prototype = {
89   QueryInterface: ChromeUtils.generateQI(["nsIInputStreamCallback"]),
91   onInputStreamReady(input) {
92     let data = NetUtil.readInputStreamToString(input, input.available());
93     this.finish(data);
94   },
97 function streamCreatePromise(transport, bidi) {
98   return new Promise(resolve => {
99     let listener = new WebTransportStreamCallback().QueryInterface(
100       Ci.nsIWebTransportStreamCallback
101     );
102     listener.finish = resolve;
104     if (bidi) {
105       transport.createOutgoingBidirectionalStream(listener);
106     } else {
107       transport.createOutgoingUnidirectionalStream(listener);
108     }
109   });
112 function sendStreamStatsPromise(stream) {
113   return new Promise(resolve => {
114     let listener = new StreamStatsCallback().QueryInterface(
115       Ci.nsIWebTransportStreamStatsCallback
116     );
117     listener.finish = resolve;
119     stream.QueryInterface(Ci.nsIWebTransportSendStream);
120     stream.getSendStreamStats(listener);
121   });
124 function receiveStreamStatsPromise(stream) {
125   return new Promise(resolve => {
126     let listener = new StreamStatsCallback().QueryInterface(
127       Ci.nsIWebTransportStreamStatsCallback
128     );
129     listener.finish = resolve;
131     stream.QueryInterface(Ci.nsIWebTransportReceiveStream);
132     stream.getReceiveStreamStats(listener);
133   });