summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/subapps/resources/subapps-helpers.js
blob: 38b8d1146603147667df405ef012358ae423fde4 (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
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
// This mock provides a way to intercept renderer <-> browser mojo messages for
// navigator.subApps.* calls eliminating the need for an actual browser.
//
// In Chromium-based browsers this implementation is provided by a polyfill
// in order to reduce the amount of test-only code shipped to users.

'use strict';

let mockSubAppsService = null;

// TODO(crbug.com/1408101): Figure out how to export SubAppsServiceResult (and
// get rid of this).
const Status = {
  SUCCESS: 0,
  FAILURE: 1,
};

async function createMockSubAppsService(service_result_code, add_call_return_value, list_call_return_value, remove_call_return_value) {
  if (typeof SubAppsServiceTest === 'undefined') {
    // Load test-only API helpers.
    const script = document.createElement('script');
    script.src = '/resources/test-only-api.js';
    script.async = false;
    const p = new Promise((resolve, reject) => {
      script.onload = () => { resolve(); };
      script.onerror = e => { reject(e); };
    })
    document.head.appendChild(script);
    await p;

    if (isChromiumBased) {
      // Chrome setup.
      await import('/resources/chromium/mock-subapps.js');
    } else {
      throw new Error('Unsupported browser.');
    }
  }
  assert_implements(SubAppsServiceTest, 'SubAppsServiceTest is not loaded properly.');

  if (mockSubAppsService === null) {
    mockSubAppsService = new SubAppsServiceTest();
    mockSubAppsService.initialize(service_result_code, add_call_return_value, list_call_return_value, remove_call_return_value);
  }
}

function subapps_test(func, description) {
  promise_test(async test => {
    test.add_cleanup(async () => {
      await mockSubAppsService.reset();
      mockSubAppsService = null;
    });
    await createMockSubAppsService(Status.SUCCESS, [], [], []);
    await func(test, mockSubAppsService);
  }, description);
}

async function subapps_add_expect_reject_with_result(t, add_call_params, mocked_response, expected_results) {
  t.add_cleanup(async () => {
    await mockSubAppsService.reset();
    mockSubAppsService = null;
  });

  await createMockSubAppsService(Status.FAILURE, mocked_response, [], []);
  await navigator.subApps.add(add_call_params).then(
    result => {
      assert_unreached("Should have rejected: ", result);
    },
    error => {
      for (const app_id in expected_results) {
        assert_own_property(error, app_id, "Return results are missing entry for subapp.")
        assert_equals(error[app_id], expected_results[app_id], "Return results are not as expected.")
      }
    });
}

async function subapps_add_expect_success_with_result(t, add_call_params, mocked_response, expected_results) {
  t.add_cleanup(async () => {
    await mockSubAppsService.reset();
    mockSubAppsService = null;
  });

  await createMockSubAppsService(Status.SUCCESS, mocked_response, [], []);
  await navigator.subApps.add(add_call_params).then(result => {
    for (const app_id in expected_results) {
      assert_own_property(result, app_id, "Return results are missing entry for subapp.")
      assert_equals(result[app_id], expected_results[app_id], "Return results are not as expected.")
    }
  });
}

async function subapps_remove_expect_reject_with_result(t, remove_call_params, mocked_response, expected_results) {
  t.add_cleanup(async () => {
    await mockSubAppsService.reset();
    mockSubAppsService = null;
  });

  await createMockSubAppsService(Status.FAILURE, [], [], mocked_response);
  await navigator.subApps.remove(remove_call_params).then(
    result => {
      assert_unreached("Should have rejected: ", result);
    },
    error => {
      for (const app_id in expected_results) {
        assert_own_property(error, app_id, "Return results are missing entry for subapp.")
        assert_equals(error[app_id], expected_results[app_id], "Return results are not as expected.")
      }
    });
}

async function subapps_remove_expect_success_with_result(t, remove_call_params, mocked_response, expected_results) {
  t.add_cleanup(async () => {
    await mockSubAppsService.reset();
    mockSubAppsService = null;
  });

  await createMockSubAppsService(Status.SUCCESS, [], [], mocked_response);
  await navigator.subApps.remove(remove_call_params).then(result => {
    for (const app_id in expected_results) {
      assert_own_property(result, app_id, "Return results are missing entry for subapp.")
      assert_equals(result[app_id], expected_results[app_id], "Return results are not as expected.")
    }
  });
}