summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/managed/resources/managed-configuration-helper.js
blob: c3213a4dad8b93bb5057c6d8cc70c7eb3db004a9 (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
'use strict';

// These tests rely on the User Agent providing an implementation of
// Managed Configuration API
// (https://wicg.github.io/WebApiDevice/managed_config).
//
// In Chromium-based browsers this implementation is provided by a polyfill
// in order to reduce the amount of test-only code shipped to users. To enable
// these tests the browser must be run with these options:
//
//   --enable-blink-features=MojoJS,MojoJSTest
let fakeManagedConfigurationService = undefined;

async function loadChromiumResources() {
  await import('/resources/chromium/mock-managed-config.js');
}

// User Agents must provide their own implementation of `ManagedConfigTest`,
// which must contain the following this interface:
// class ManagedConfigTest {
//   /** Replaces the provided managed config with the given one. */
//   /** @param {?Object} config */
//   setManagedConfig(config);
//   /** Asynchronously waits for a new observer to be added */
//   waitTillObserverAdded();
// }

async function createManagedConfigTest() {
  if (typeof ManagedConfigTest === 'undefined') {
    if (isChromiumBased) {
      await loadChromiumResources();
    }
  }
  assert_implements(ManagedConfigTest, 'ManagedConfigTest is unavailable.');

  if (fakeManagedConfigurationService !== undefined) {
    await fakeManagedConfigurationService.initialize();
    return fakeManagedConfigurationService;
  }
  let managedConfigTest = new ManagedConfigTest();
  await managedConfigTest.initialize();
  fakeManagedConfigurationService = managedConfigTest;
  return managedConfigTest;
}

function managed_config_test(func, description) {
  promise_test(async test => {
    const managedConfigTest = await createManagedConfigTest();
    await func(test, managedConfigTest);
  }, description);
}