browser_device_custom.js (7470B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Test adding and removing custom devices via the modal. 7 8 const L10N = new LocalizationHelper( 9 "devtools/client/locales/device.properties", 10 true 11 ); 12 13 const device = { 14 name: "Test Device", 15 width: 400, 16 height: 570, 17 pixelRatio: 1.5, 18 userAgent: "Mozilla/5.0 (Mobile; rv:39.0) Gecko/39.0 Firefox/39.0", 19 touch: true, 20 }; 21 22 const unicodeDevice = { 23 name: "\u00B6\u00C7\u00DA\u00E7\u0126", 24 width: 400, 25 height: 570, 26 pixelRatio: 1.5, 27 userAgent: "Mozilla/5.0 (Mobile; rv:39.0) Gecko/39.0 Firefox/39.0", 28 touch: true, 29 }; 30 31 const TEST_URL = "data:text/html;charset=utf-8,"; 32 33 addRDMTask( 34 TEST_URL, 35 async function ({ ui }) { 36 const { toolWindow } = ui; 37 const { document } = toolWindow; 38 39 await openDeviceModal(ui); 40 41 is( 42 getCustomHeaderEl(document), 43 null, 44 "There's no Custom header when we don't have custom devices" 45 ); 46 47 info("Reveal device adder form, check that defaults match the viewport"); 48 const adderShow = document.getElementById("device-add-button"); 49 adderShow.click(); 50 testDeviceAdder(ui, { 51 name: "Custom Device", 52 width: 320, 53 height: 480, 54 pixelRatio: window.devicePixelRatio, 55 userAgent: navigator.userAgent, 56 touch: false, 57 }); 58 59 info("Fill out device adder form and save"); 60 await addDeviceInModal(ui, device); 61 62 info("Verify device defaults to enabled in modal"); 63 const deviceCb = [ 64 ...document.querySelectorAll(".device-input-checkbox"), 65 ].find(cb => { 66 return cb.value == device.name; 67 }); 68 ok(deviceCb, "Custom device checkbox added to modal"); 69 ok(deviceCb.checked, "Custom device enabled"); 70 71 const customHeaderEl = getCustomHeaderEl(document); 72 ok(customHeaderEl, "There's a Custom header when add a custom devices"); 73 is( 74 customHeaderEl.textContent, 75 L10N.getStr(`device.custom`), 76 "The custom header has the expected text" 77 ); 78 79 document.getElementById("device-close-button").click(); 80 81 info("Look for custom device in device selector"); 82 const deviceSelector = document.getElementById("device-selector"); 83 await testMenuItems(toolWindow, deviceSelector, items => { 84 const menuItem = findMenuItem(items, device.name); 85 ok(menuItem, "Custom device menu item added to device selector"); 86 }); 87 }, 88 { waitForDeviceList: true } 89 ); 90 91 addRDMTask( 92 TEST_URL, 93 async function ({ ui }) { 94 const { toolWindow } = ui; 95 const { store, document } = toolWindow; 96 97 info("Select existing device from the selector"); 98 await selectDevice(ui, "Test Device"); 99 100 await openDeviceModal(ui); 101 102 info( 103 "Reveal device adder form, check that defaults are based on selected device" 104 ); 105 const adderShow = document.getElementById("device-add-button"); 106 adderShow.click(); 107 testDeviceAdder( 108 ui, 109 Object.assign({}, device, { 110 name: "Test Device (Custom)", 111 }) 112 ); 113 114 info("Remove previously added custom device"); 115 // Close the form since custom device buttons are only shown when form is not open. 116 const cancelButton = document.getElementById("device-form-cancel"); 117 cancelButton.click(); 118 119 const deviceRemoveButton = document.querySelector(".device-remove-button"); 120 const removed = Promise.all([ 121 waitUntilState(store, state => !state.devices.custom.length), 122 once(ui, "device-association-removed"), 123 ]); 124 deviceRemoveButton.click(); 125 await removed; 126 127 info("Close the form before submitting."); 128 document.getElementById("device-close-button").click(); 129 130 info("Ensure custom device was removed from device selector"); 131 await waitUntilState(store, state => state.viewports[0].device == ""); 132 const deviceSelectorTitle = document.querySelector("#device-selector"); 133 is( 134 deviceSelectorTitle.textContent, 135 "Responsive", 136 "Device selector reset to no device" 137 ); 138 139 info("Look for custom device in device selector"); 140 const deviceSelector = document.getElementById("device-selector"); 141 await testMenuItems(toolWindow, deviceSelector, menuItems => { 142 const menuItem = findMenuItem(menuItems, device.name); 143 ok(!menuItem, "Custom device option removed from device selector"); 144 }); 145 146 info("Ensure device properties like UA have been reset"); 147 await testUserAgent(ui, navigator.userAgent); 148 }, 149 { waitForDeviceList: true } 150 ); 151 152 addRDMTask( 153 TEST_URL, 154 async function ({ ui }) { 155 const { toolWindow } = ui; 156 const { document } = toolWindow; 157 158 await openDeviceModal(ui); 159 160 info("Reveal device adder form"); 161 const adderShow = document.querySelector("#device-add-button"); 162 adderShow.click(); 163 164 info( 165 "Fill out device adder form by setting details to unicode device and save" 166 ); 167 await addDeviceInModal(ui, unicodeDevice); 168 169 info("Verify unicode device defaults to enabled in modal"); 170 const deviceCb = [ 171 ...document.querySelectorAll(".device-input-checkbox"), 172 ].find(cb => { 173 return cb.value == unicodeDevice.name; 174 }); 175 ok(deviceCb, "Custom unicode device checkbox added to modal"); 176 ok(deviceCb.checked, "Custom unicode device enabled"); 177 document.getElementById("device-close-button").click(); 178 179 info("Look for custom unicode device in device selector"); 180 const deviceSelector = document.getElementById("device-selector"); 181 await testMenuItems(toolWindow, deviceSelector, items => { 182 const menuItem = findMenuItem(items, unicodeDevice.name); 183 ok(menuItem, "Custom unicode device option added to device selector"); 184 }); 185 }, 186 { waitForDeviceList: true } 187 ); 188 189 addRDMTask( 190 TEST_URL, 191 async function ({ ui }) { 192 const { toolWindow } = ui; 193 const { document } = toolWindow; 194 195 // Check if the unicode custom device is present in the list of device options since 196 // we want to ensure that unicode device names are not forgotten after restarting RDM 197 // see bug 1379687 198 info("Look for custom unicode device in device selector"); 199 const deviceSelector = document.getElementById("device-selector"); 200 await testMenuItems(toolWindow, deviceSelector, items => { 201 const menuItem = findMenuItem(items, unicodeDevice.name); 202 ok(menuItem, "Custom unicode device option present in device selector"); 203 }); 204 }, 205 { waitForDeviceList: true } 206 ); 207 208 function testDeviceAdder(ui, expected) { 209 const { document } = ui.toolWindow; 210 211 const nameInput = document.querySelector("#device-form-name input"); 212 const [widthInput, heightInput] = document.querySelectorAll( 213 "#device-form-size input" 214 ); 215 const pixelRatioInput = document.querySelector( 216 "#device-form-pixel-ratio input" 217 ); 218 const userAgentInput = document.querySelector( 219 "#device-form-user-agent input" 220 ); 221 const touchInput = document.querySelector("#device-form-touch input"); 222 223 is(nameInput.value, expected.name, "Device name matches"); 224 is(parseInt(widthInput.value, 10), expected.width, "Width matches"); 225 is(parseInt(heightInput.value, 10), expected.height, "Height matches"); 226 is( 227 parseFloat(pixelRatioInput.value), 228 expected.pixelRatio, 229 "devicePixelRatio matches" 230 ); 231 is(userAgentInput.value, expected.userAgent, "User agent matches"); 232 is(touchInput.checked, expected.touch, "Touch matches"); 233 } 234 235 function getCustomHeaderEl(doc) { 236 return doc.querySelector(`.device-type-custom .device-header`); 237 }