subapps-helpers.js (4488B)
1 // This mock provides a way to intercept renderer <-> browser mojo messages for 2 // navigator.subApps.* calls eliminating the need for an actual browser. 3 // 4 // In Chromium-based browsers this implementation is provided by a polyfill 5 // in order to reduce the amount of test-only code shipped to users. 6 7 'use strict'; 8 9 let mockSubAppsService = null; 10 11 // TODO(crbug.com/1408101): Figure out how to export SubAppsServiceResult (and 12 // get rid of this). 13 const Status = { 14 SUCCESS: 0, 15 FAILURE: 1, 16 }; 17 18 async function createMockSubAppsService(service_result_code, add_call_return_value, list_call_return_value, remove_call_return_value) { 19 if (typeof SubAppsServiceTest === 'undefined') { 20 // Load test-only API helpers. 21 const script = document.createElement('script'); 22 script.src = '/resources/test-only-api.js'; 23 script.async = false; 24 const p = new Promise((resolve, reject) => { 25 script.onload = () => { resolve(); }; 26 script.onerror = e => { reject(e); }; 27 }) 28 document.head.appendChild(script); 29 await p; 30 31 if (isChromiumBased) { 32 // Chrome setup. 33 await import('/resources/chromium/mock-subapps.js'); 34 } else { 35 throw new Error('Unsupported browser.'); 36 } 37 } 38 assert_implements(SubAppsServiceTest, 'SubAppsServiceTest is not loaded properly.'); 39 40 if (mockSubAppsService === null) { 41 mockSubAppsService = new SubAppsServiceTest(); 42 mockSubAppsService.initialize(service_result_code, add_call_return_value, list_call_return_value, remove_call_return_value); 43 } 44 } 45 46 function subapps_test(func, description) { 47 promise_test(async test => { 48 test.add_cleanup(async () => { 49 await mockSubAppsService.reset(); 50 mockSubAppsService = null; 51 }); 52 await createMockSubAppsService(Status.SUCCESS, [], [], []); 53 await func(test, mockSubAppsService); 54 }, description); 55 } 56 57 async function subapps_add_expect_reject_with_result(t, add_call_params, mocked_response, expected_results) { 58 t.add_cleanup(async () => { 59 await mockSubAppsService.reset(); 60 mockSubAppsService = null; 61 }); 62 63 await createMockSubAppsService(Status.FAILURE, mocked_response, [], []); 64 await navigator.subApps.add(add_call_params).then( 65 result => { 66 assert_unreached("Should have rejected: ", result); 67 }, 68 error => { 69 for (const app_id in expected_results) { 70 assert_own_property(error, app_id, "Return results are missing entry for subapp.") 71 assert_equals(error[app_id], expected_results[app_id], "Return results are not as expected.") 72 } 73 }); 74 } 75 76 async function subapps_add_expect_success_with_result(t, add_call_params, mocked_response, expected_results) { 77 t.add_cleanup(async () => { 78 await mockSubAppsService.reset(); 79 mockSubAppsService = null; 80 }); 81 82 await createMockSubAppsService(Status.SUCCESS, mocked_response, [], []); 83 await navigator.subApps.add(add_call_params).then(result => { 84 for (const app_id in expected_results) { 85 assert_own_property(result, app_id, "Return results are missing entry for subapp.") 86 assert_equals(result[app_id], expected_results[app_id], "Return results are not as expected.") 87 } 88 }); 89 } 90 91 async function subapps_remove_expect_reject_with_result(t, remove_call_params, mocked_response, expected_results) { 92 t.add_cleanup(async () => { 93 await mockSubAppsService.reset(); 94 mockSubAppsService = null; 95 }); 96 97 await createMockSubAppsService(Status.FAILURE, [], [], mocked_response); 98 await navigator.subApps.remove(remove_call_params).then( 99 result => { 100 assert_unreached("Should have rejected: ", result); 101 }, 102 error => { 103 for (const app_id in expected_results) { 104 assert_own_property(error, app_id, "Return results are missing entry for subapp.") 105 assert_equals(error[app_id], expected_results[app_id], "Return results are not as expected.") 106 } 107 }); 108 } 109 110 async function subapps_remove_expect_success_with_result(t, remove_call_params, mocked_response, expected_results) { 111 t.add_cleanup(async () => { 112 await mockSubAppsService.reset(); 113 mockSubAppsService = null; 114 }); 115 116 await createMockSubAppsService(Status.SUCCESS, [], [], mocked_response); 117 await navigator.subApps.remove(remove_call_params).then(result => { 118 for (const app_id in expected_results) { 119 assert_own_property(result, app_id, "Return results are missing entry for subapp.") 120 assert_equals(result[app_id], expected_results[app_id], "Return results are not as expected.") 121 } 122 }); 123 }