summaryrefslogtreecommitdiffstats
path: root/test/wpt/tests/resources/chromium/mock-direct-sockets.js
blob: 6d557f7a01530664d619c1568f1482bfd121e4b7 (plain)
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
'use strict';

import {DirectSocketsService, DirectSocketsServiceReceiver} from '/gen/third_party/blink/public/mojom/direct_sockets/direct_sockets.mojom.m.js';

self.DirectSocketsServiceTest = (() => {
  // Class that mocks DirectSocketsService interface defined in
  // https://source.chromium.org/chromium/chromium/src/third_party/blink/public/mojom/direct_sockets/direct_sockets.mojom
  class MockDirectSocketsService {
    constructor() {
      this.interceptor_ = new MojoInterfaceInterceptor(DirectSocketsService.$interfaceName);
      this.receiver_ = new DirectSocketsServiceReceiver(this);
      this.interceptor_.oninterfacerequest = e =>
          this.receiver_.$.bindHandle(e.handle);
      this.interceptor_.start();
    }

    reset() {
      this.receiver_.$.close();
      this.interceptor_.stop();
    }

    openTCPSocket(
      options,
      receiver,
      observer) {
      return Promise.resolve({
        // return result = net:Error::NOT_IMPLEMENTED (code -11)
        result: -11
      });
    }

    openConnectedUDPSocket(
      options,
      receiver,
      listener) {
      return Promise.resolve({
        // return result = net:Error::NOT_IMPLEMENTED (code -11)
        result: -11
      });
    }

    openBoundUDPSocket(
      options,
      receiver,
      listener) {
      return Promise.resolve({
        // return result = net:Error::NOT_IMPLEMENTED (code -11)
        result: -11
      });
    }

    openTCPServerSocket(
      options,
      receiver) {
      return Promise.resolve({
        // return result = net:Error::NOT_IMPLEMENTED (code -11)
        result: -11
      });
    }
  }

  let testInternal = {
    initialized: false,
    mockDirectSocketsService: null
  }

  class DirectSocketsServiceTestChromium {
    constructor() {
      Object.freeze(this);  // Make it immutable.
    }

    initialize() {
      if (!testInternal.initialized) {
        testInternal = {
          mockDirectSocketsService: new MockDirectSocketsService(),
          initialized: true
        };
      }
    }

    async reset() {
      if (testInternal.initialized) {
        testInternal.mockDirectSocketsService.reset();
        testInternal = {
          mockDirectSocketsService: null,
          initialized: false
        };
        await new Promise(resolve => setTimeout(resolve, 0));
      }
    }
  }

  return DirectSocketsServiceTestChromium;
})();