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

import{ManagedConfigurationObserverRemote, ManagedConfigurationService, ManagedConfigurationServiceReceiver} from '/gen/third_party/blink/public/mojom/device/device.mojom.m.js';


self.ManagedConfigTest = (() => {
  // Class that mocks ManagedConfigurationService interface defined in
  // https://source.chromium.org/chromium/chromium/src/third_party/blink/public/mojom/device/device.mojom
  class MockManagedConfig {
    constructor() {
      this.receiver_ = new ManagedConfigurationServiceReceiver(this);
      this.interceptor_ = new MojoInterfaceInterceptor(
          ManagedConfigurationService.$interfaceName);
      this.interceptor_.oninterfacerequest = e =>
          this.receiver_.$.bindHandle(e.handle);
      this.interceptor_.start();
      this.subscription_ = null;
      this.reset();
    }

    reset() {
      this.configuration_ = null;
      this.onObserverAdd_ = null;
    }

    async getManagedConfiguration(keys) {
      if (this.configuration_ === null) {
        return {};
      }

      return {
        configurations: Object.keys(this.configuration_)
                            .filter(key => keys.includes(key))
                            .reduce(
                                (obj, key) => {
                                  obj[key] =
                                      JSON.stringify(this.configuration_[key]);
                                  return obj;
                                },
                                {})
      };
    }

    subscribeToManagedConfiguration(remote) {
      this.subscription_ = remote;
      if (this.onObserverAdd_ !== null) {
        this.onObserverAdd_();
      }
    }

    setManagedConfig(value) {
      this.configuration_ = value;
      if (this.subscription_ !== null) {
        this.subscription_.onConfigurationChanged();
      }
    }
  }

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

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

    initialize() {
      if (testInternal.mockManagedConfig !== null) {
        testInternal.mockManagedConfig.reset();
        return;
      }

      testInternal.mockManagedConfig = new MockManagedConfig;
      testInternal.initialized = true;
    }

    setManagedConfig(config) {
      testInternal.mockManagedConfig.setManagedConfig(config);
    }

    async nextObserverAdded() {
      await new Promise(resolve => {
        testInternal.mockManagedConfig.onObserverAdd_ = resolve;
      });
    }
  }

  return ManagedConfigTestChromium;
})();