summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/subapps/resources/subapps-helpers.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/subapps/resources/subapps-helpers.js
parentInitial commit. (diff)
downloadfirefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz
firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/subapps/resources/subapps-helpers.js')
-rw-r--r--testing/web-platform/tests/subapps/resources/subapps-helpers.js99
1 files changed, 99 insertions, 0 deletions
diff --git a/testing/web-platform/tests/subapps/resources/subapps-helpers.js b/testing/web-platform/tests/subapps/resources/subapps-helpers.js
new file mode 100644
index 0000000000..e923db01f3
--- /dev/null
+++ b/testing/web-platform/tests/subapps/resources/subapps-helpers.js
@@ -0,0 +1,99 @@
+// 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;
+
+const Status = {
+ SUCCESS: 0,
+ FAILURE: 1,
+};
+
+const AddCallResultCode = {
+ SUCCESS_NEW_INSTALL: 0,
+ SUCCESS_ALREADY_INSTALLED: 1,
+ USER_INSTALL_DECLINED: 2,
+ EXPECTED_APP_ID_CHECK_FAILED: 3,
+ PARENT_APP_UNINSTALLED: 4,
+ INSTALL_URL_INVALID: 5,
+ NOT_VALID_MANIFEST_FOR_WEB_APP: 6,
+ FAILURE: 7,
+}
+
+async function createMockSubAppsService(service_result_code, add_call_return_value, list_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);
+ }
+}
+
+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.");
+ })
+ .catch(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_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.")
+ }
+ })
+}