DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (4126098473d5)

VCS Links

Line Code
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
<!doctype html>
<meta charset=utf-8>
<title>RTCPeerConnection.prototype.ondatachannel</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="RTCPeerConnection-helper.js"></script>
<script>
'use strict';

// Test is based on the following revision:
// https://rawgit.com/w3c/webrtc-pc/1cc5bfc3ff18741033d804c4a71f7891242fb5b3/webrtc.html

// The following helper functions are called from RTCPeerConnection-helper.js:
// exchangeIceCandidates
// doSignalingHandshake
// createDataChannelPair

/*
  6.2.  RTCDataChannel
    When an underlying data transport is to be announced (the other peer created a channel with
    negotiated unset or set to false), the user agent of the peer that did not initiate the
    creation process MUST queue a task to run the following steps:
      2. Let channel be a newly created RTCDataChannel object.
      7. Set channel's [[ReadyState]] to open (but do not fire the open event, yet).
      8. Fire a datachannel event named datachannel with channel at the RTCPeerConnection object.

  6.3.  RTCDataChannelEvent
    Firing a datachannel event named e with an RTCDataChannel channel means that an event with the
    name e, which does not bubble (except where otherwise stated) and is not cancelable (except
    where otherwise stated), and which uses the RTCDataChannelEvent interface with the channel
    attribute set to channel, MUST be created and dispatched at the given target.

    interface RTCDataChannelEvent : Event {
      readonly attribute RTCDataChannel channel;
    };
 */
promise_test(async (t) => {
  const resolver = new Resolver();
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());

  let eventCount = 0;

  pc2.ondatachannel = t.step_func((event) => {
    eventCount++;
    assert_equals(eventCount, 1,
      'Expect data channel event to fire exactly once');

    assert_true(event instanceof RTCDataChannelEvent,
      'Expect event to be instance of RTCDataChannelEvent');

    assert_equals(event.bubbles, false);
    assert_equals(event.cancelable, false);

    const dc = event.channel;
    assert_true(dc instanceof RTCDataChannel,
      'Expect channel to be instance of RTCDataChannel');

    // The channel should be in the 'open' state already.
    // See: https://github.com/w3c/webrtc-pc/pull/1851
    assert_equals(dc.readyState, 'open',
      'Expect channel ready state to be open');

    resolver.resolve();
  });

  pc1.createDataChannel('fire-me!');

  exchangeIceCandidates(pc1, pc2);
  await doSignalingHandshake(pc1, pc2);

  await resolver;
}, 'Data channel event should fire when new data channel is announced to the remote peer');

/*
  Since the channel should be in the 'open' state when dispatching via the 'datachannel' event,
  we should be able to send data in the event handler.
 */
promise_test(async (t) => {
  const resolver = new Resolver();
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());

  const message = 'meow meow!';

  pc2.ondatachannel = t.step_func((event) => {
    const dc2 = event.channel;
    dc2.send(message);
  });

  const dc1 = pc1.createDataChannel('fire-me!');
  dc1.onmessage = t.step_func((event) => {
    assert_equals(event.data, message,
      'Received data should be equal to sent data');

    resolver.resolve();
  });

  exchangeIceCandidates(pc1, pc2);
  await doSignalingHandshake(pc1, pc2);

  await resolver;
}, 'Should be able to send data in a datachannel event handler');

/*
  6.2.  RTCDataChannel
    When an underlying data transport is to be announced (the other peer created a channel with
    negotiated unset or set to false), the user agent of the peer that did not initiate the
    creation process MUST queue a task to run the following steps:
      8. Fire a datachannel event named datachannel with channel at the RTCPeerConnection object.
      9. If the channel's [[ReadyState]] is still open, announce the data channel as open.
 */
promise_test(async (t) => {
  const resolver = new Resolver();
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());

  pc2.ondatachannel = t.step_func((event) => {
    const dc = event.channel;
    dc.onopen = t.step_func(() => {
      assert_unreached('Open event should not fire');
    });

    // This should prevent triggering the 'open' event
    dc.close();

    // Wait a bit to ensure the 'open' event does NOT fire
    t.step_timeout(() => resolver.resolve(), 500);
  });

  pc1.createDataChannel('fire-me!');

  exchangeIceCandidates(pc1, pc2);
  await doSignalingHandshake(pc1, pc2);

  await resolver;
}, 'Open event should not be raised when closing the channel in the datachannel event');

// Added this test as a result of the discussion in
// https://github.com/w3c/webrtc-pc/pull/1851#discussion_r185976747
promise_test(async (t) => {
  const resolver = new Resolver();
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());

  pc2.ondatachannel = t.step_func((event) => {
    const dc = event.channel;
    dc.onopen = t.step_func((event) => {
      resolver.resolve();
    });

    // This should NOT prevent triggering the 'open' event since it enqueues at least two tasks
    t.step_timeout(() => {
      t.step_timeout(() => {
        dc.close()
      }, 1);
    }, 1);
  });

  pc1.createDataChannel('fire-me!');

  exchangeIceCandidates(pc1, pc2);
  await doSignalingHandshake(pc1, pc2);

  await resolver;
}, 'Open event should be raised when closing the channel in the datachannel event after ' +
  'enqueuing a task');


/*
  Combination of the two tests above (send and close).
 */
promise_test(async (t) => {
  const resolver = new Resolver();
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());

  const message = 'meow meow!';

  pc2.ondatachannel = t.step_func((event) => {
    const dc2 = event.channel;
    dc2.onopen = t.step_func(() => {
      assert_unreached('Open event should not fire');
    });

    // This should send but still prevent triggering the 'open' event
    dc2.send(message);
    dc2.close();
  });

  const dc1 = pc1.createDataChannel('fire-me!');
  dc1.onmessage = t.step_func((event) => {
    assert_equals(event.data, message,
      'Received data should be equal to sent data');

    resolver.resolve();
  });

  exchangeIceCandidates(pc1, pc2);
  await doSignalingHandshake(pc1, pc2);

  await resolver;
}, 'Open event should not be raised when sending and immediately closing the channel in the ' +
  'datachannel event');

/*
  6.2.  RTCDataChannel

    interface RTCDataChannel : EventTarget {
      readonly attribute USVString           label;
      readonly attribute boolean             ordered;
      readonly attribute unsigned short?     maxPacketLifeTime;
      readonly attribute unsigned short?     maxRetransmits;
      readonly attribute USVString           protocol;
      readonly attribute boolean             negotiated;
      readonly attribute unsigned short?     id;
      readonly attribute RTCPriorityType     priority;
      readonly attribute RTCDataChannelState readyState;
      ...
    };

    When an underlying data transport is to be announced (the other peer created a channel with
    negotiated unset or set to false), the user agent of the peer that did not initiate the
    creation process MUST queue a task to run the following steps:
      2. Let channel be a newly created RTCDataChannel object.
      3. Let configuration be an information bundle received from the other peer as a part of the
         process to establish the underlying data transport described by the WebRTC DataChannel
         Protocol specification [RTCWEB-DATA-PROTOCOL].
      4. Initialize channel's [[DataChannelLabel]], [[Ordered]], [[MaxPacketLifeTime]],
         [[MaxRetransmits]], [[DataChannelProtocol]], and [[DataChannelId]] internal slots to the
         corresponding values in configuration.
      5. Initialize channel's [[Negotiated]] internal slot to false.
      6. Initialize channel's [[DataChannelPriority]] internal slot based on the integer priority
         value in configuration, [...]
      7. Set channel's [[ReadyState]] slot to connecting.
      8. Fire a datachannel event named datachannel with channel at the RTCPeerConnection object.

    Note: More exhaustive tests are defined in RTCDataChannel-dcep
 */

promise_test(async (t) => {
  const resolver = new Resolver();
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());

  const dc1 = pc1.createDataChannel('test', {
    ordered: false,
    maxRetransmits: 1,
    protocol: 'custom',
    priority: 'high'
  });

  assert_equals(dc1.label, 'test');
  assert_equals(dc1.ordered, false);
  assert_equals(dc1.maxPacketLifeTime, null);
  assert_equals(dc1.maxRetransmits, 1);
  assert_equals(dc1.protocol, 'custom');
  assert_equals(dc1.negotiated, false);
  assert_equals(dc1.priority, 'high');

  pc2.ondatachannel = t.step_func((event) => {
    const dc2 = event.channel;
    assert_true(dc2 instanceof RTCDataChannel,
      'Expect channel to be instance of RTCDataChannel');

    assert_equals(dc2.label, 'test');
    assert_equals(dc2.ordered, false);
    assert_equals(dc2.maxPacketLifeTime, null);
    assert_equals(dc2.maxRetransmits, 1);
    assert_equals(dc2.protocol, 'custom');
    assert_equals(dc2.negotiated, false);
    assert_equals(dc2.id, dc1.id);
    assert_equals(dc2.priority, 'high');

    resolver.resolve();
  });

  exchangeIceCandidates(pc1, pc2);
  await doSignalingHandshake(pc1, pc2);

  await resolver;
}, 'In-band negotiated channel created on remote peer should match the same configuration as local ' +
  'peer');

promise_test(async (t) => {
  const resolver = new Resolver();
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());

  const dc1 = pc1.createDataChannel('');

  assert_equals(dc1.label, '');
  assert_equals(dc1.ordered, true);
  assert_equals(dc1.maxPacketLifeTime, null);
  assert_equals(dc1.maxRetransmits, null);
  assert_equals(dc1.protocol, '');
  assert_equals(dc1.negotiated, false);
  assert_equals(dc1.priority, 'low');

  pc2.ondatachannel = t.step_func((event) => {
    const dc2 = event.channel;
    assert_true(dc2 instanceof RTCDataChannel,
      'Expect channel to be instance of RTCDataChannel');

    assert_equals(dc2.label, '');
    assert_equals(dc2.ordered, true);
    assert_equals(dc2.maxPacketLifeTime, null);
    assert_equals(dc2.maxRetransmits, null);
    assert_equals(dc2.protocol, '');
    assert_equals(dc2.negotiated, false);
    assert_equals(dc2.id, dc1.id);
    assert_equals(dc2.priority, 'low');

    resolver.resolve();
  });

  exchangeIceCandidates(pc1, pc2);
  await doSignalingHandshake(pc1, pc2);

  await resolver;
}, 'In-band negotiated channel created on remote peer should match the same (default) ' +
  'configuration as local peer');

/*
  6.2.  RTCDataChannel
    Dictionary RTCDataChannelInit Members
      negotiated
        The default value of false tells the user agent to announce the
        channel in-band and instruct the other peer to dispatch a corresponding
        RTCDataChannel object. If set to true, it is up to the application
        to negotiate the channel and create a RTCDataChannel object with the
        same id at the other peer.
 */
promise_test(async (t) => {
  const resolver = new Resolver();
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  t.add_cleanup(() => pc2.close());

  pc2.ondatachannel = t.unreached_func('datachannel event should not be fired');

  pc1.createDataChannel('test', {
    negotiated: true,
    id: 42
  });

  exchangeIceCandidates(pc1, pc2);
  await doSignalingHandshake(pc1, pc2);

  // Wait a bit to ensure the 'datachannel' event does NOT fire
  t.step_timeout(() => resolver.resolve(), 500);
  await resolver;
}, 'Negotiated channel should not fire datachannel event on remote peer');

/*
  Non-testable
  6.2.  RTCDataChannel
    When an underlying data transport is to be announced
      1.  If the associated RTCPeerConnection object's [[isClosed]] slot
          is true, abort these steps.

  The above step is not testable because to reach it we would have to
  close the peer connection just between receiving the in-band negotiated data
  channel via DCEP and firing the datachannel event.
 */
</script>