summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resources/chromium/mock-subapps.js
blob: b81936713b17db941ef380d4019cecfbe563caf6 (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
'use strict';

import {SubAppsService, SubAppsServiceReceiver, SubAppsServiceResultCode} from '/gen/third_party/blink/public/mojom/subapps/sub_apps_service.mojom.m.js';

self.SubAppsServiceTest = (() => {
  // Class that mocks SubAppsService interface defined in /third_party/blink/public/mojom/subapps/sub_apps_service.mojom

  class MockSubAppsService {
    constructor() {
      this.interceptor_ =
        new MojoInterfaceInterceptor(SubAppsService.$interfaceName);
      this.receiver_ = new SubAppsServiceReceiver(this);
      this.interceptor_.oninterfacerequest =
        e => this.receiver_.$.bindHandle(e.handle);
      this.interceptor_.start();
    }

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

    add(sub_apps) {
      return Promise.resolve({
        result: testInternal.addCallReturnValue,
      });
    }

    list() {
      return Promise.resolve({
        result: {
          resultCode: testInternal.serviceResultCode,
          subAppsList: testInternal.listCallReturnValue,
        }
      });
    }

    remove() {
      return Promise.resolve({
        result: testInternal.removeCallReturnValue,
      });
    }
  }

  let testInternal = {
    initialized: false,
    mockSubAppsService: null,
    serviceResultCode: 0,
    addCallReturnValue: [],
    listCallReturnValue: [],
    removeCallReturnValue: [],
  }

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

    initialize(service_result_code, add_call_return_value, list_call_return_value, remove_call_return_value) {
      if (!testInternal.initialized) {
        testInternal = {
          mockSubAppsService: new MockSubAppsService(),
          initialized: true,
          serviceResultCode: service_result_code,
          addCallReturnValue: add_call_return_value,
          listCallReturnValue: list_call_return_value,
          removeCallReturnValue: remove_call_return_value,
        };
      };
    }

    async reset() {
      if (testInternal.initialized) {
        testInternal.mockSubAppsService.reset();
        testInternal = {
          mockSubAppsService: null,
          initialized: false,
          serviceResultCode: 0,
          addCallReturnValue: [],
          listCallReturnValue: [],
          removeCallReturnValue: [],
        };
        await new Promise(resolve => setTimeout(resolve, 0));
      }
    }
  }

  return SubAppsServiceTestChromium;
})();