manual-helper.js (3222B)
1 // Internal function. Returns [instruction, list] DOM elements. 2 function setupManualShareTestCommon() { 3 const div = document.createElement('div'); 4 document.body.appendChild(div); 5 6 const instruction = document.createElement('div'); 7 instruction.id = 'instruction'; 8 div.appendChild(instruction); 9 10 const shareButton = document.createElement('input'); 11 shareButton.id = 'share_button'; 12 shareButton.value = 'Share button'; 13 shareButton.type = 'button'; 14 div.appendChild(shareButton); 15 16 let heading = document.createElement('h2'); 17 heading.innerText = 'Instructions:'; 18 instruction.appendChild(heading); 19 let list = document.createElement('ol'); 20 instruction.appendChild(list); 21 let item = document.createElement('li'); 22 list.appendChild(item); 23 item.innerText = 'Click the Share button.'; 24 25 return [instruction, list]; 26 } 27 28 // Sets up the page for running manual tests. Automatically creates the 29 // instructions (based on the parameters) and the share button. 30 function setupManualShareTest(expected_share_data) { 31 const {title, text, url, files} = expected_share_data; 32 let [instruction, list] = setupManualShareTestCommon(); 33 let item = document.createElement('li'); 34 list.appendChild(item); 35 item.innerText = 'Choose a valid share target.'; 36 37 heading = document.createElement('h2'); 38 heading.innerText = 'Pass the test iff the target app received:'; 39 instruction.appendChild(heading); 40 41 list = document.createElement('ul'); 42 instruction.appendChild(list); 43 44 item = document.createElement('li'); 45 list.appendChild(item); 46 item.innerText = `title = "${title}"`; 47 item = document.createElement('li'); 48 list.appendChild(item); 49 item.innerText = `text = "${text}"`; 50 item = document.createElement('li'); 51 list.appendChild(item); 52 item.innerText = `url = "${url}"`; 53 if (files) { 54 item = document.createElement('li'); 55 list.appendChild(item); 56 item.innerText = `files = ${files.length} file(s)`; 57 for (let file of files) { 58 const div = document.createElement('div'); 59 if (file.type.startsWith('text/')) { 60 const reader = new FileReader(); 61 reader.onload = () => { 62 div.textContent = reader.result; 63 }; 64 reader.readAsText(file); 65 } else if (file.type.startsWith('image/')) { 66 const image = document.createElement('img'); 67 image.src = URL.createObjectURL(file); 68 image.alt = file.name; 69 div.appendChild(image); 70 } 71 item.appendChild(div); 72 } 73 } 74 } 75 76 function setupManualShareTestRequiringCancellation() { 77 const [instruction, list] = setupManualShareTestCommon(); 78 const item = document.createElement('li'); 79 list.appendChild(item); 80 item.innerText = 'Cancel the share dialog.'; 81 } 82 83 // Returns a promise. When the user clicks the button, calls 84 // |click_handler| and resolves the promise with the result. 85 function callWhenButtonClicked(click_handler) { 86 return new Promise((resolve, reject) => { 87 document.querySelector('#share_button').onclick = () => { 88 try { 89 const result = click_handler(); 90 resolve(result); 91 } catch (e) { 92 reject(e); 93 } 94 }; 95 }); 96 } 97 98 function getAbsoluteUrl(url) { 99 return new URL(url, document.baseURI).toString(); 100 }