browser_device_modal_submit.js (6709B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Test submitting display device changes on the device modal 7 const { getDevices } = require("resource://devtools/client/shared/devices.js"); 8 9 const addedDevice = { 10 name: "Fake Phone RDM Test", 11 width: 320, 12 height: 570, 13 pixelRatio: 1.5, 14 userAgent: "Mozilla/5.0 (Mobile; rv:39.0) Gecko/39.0 Firefox/39.0", 15 touch: true, 16 firefoxOS: false, 17 os: "custom", 18 featured: true, 19 }; 20 21 const TEST_URL = "data:text/html;charset=utf-8,"; 22 23 addRDMTask( 24 TEST_URL, 25 async function ({ ui }) { 26 const { toolWindow } = ui; 27 const { document, store } = toolWindow; 28 const deviceSelector = document.getElementById("device-selector"); 29 30 await openDeviceModal(ui); 31 32 info( 33 "Checking displayed device checkboxes are checked in the device modal." 34 ); 35 const checkedCbs = document.querySelectorAll( 36 ".device-input-checkbox:checked" 37 ); 38 const remoteList = await getDevices(); 39 40 const featuredCount = getNumberOfFeaturedDevices(remoteList); 41 is( 42 featuredCount, 43 checkedCbs.length, 44 "Got expected number of displayed devices." 45 ); 46 47 for (const cb of checkedCbs) { 48 ok( 49 Object.keys(remoteList).filter(type => remoteList[type][cb.value]), 50 cb.value + " is correctly checked." 51 ); 52 } 53 54 // Tests where the user adds a non-featured device 55 info("Check the first unchecked device and submit new device list."); 56 const uncheckedCb = document.querySelector( 57 ".device-input-checkbox:not(:checked)" 58 ); 59 const value = uncheckedCb.value; 60 uncheckedCb.click(); 61 document.getElementById("device-close-button").click(); 62 63 ok( 64 !store.getState().devices.isModalOpen, 65 "The device modal is closed on submit." 66 ); 67 68 info("Checking that the new device is added to the user preference list."); 69 let preferredDevices = _loadPreferredDevices(); 70 ok(preferredDevices.added.has(value), value + " in user added list."); 71 72 info("Checking new device is added to the device selector."); 73 await testMenuItems(toolWindow, deviceSelector, menuItems => { 74 is( 75 menuItems.length - 1, 76 featuredCount + 1, 77 "Got expected number of devices in device selector." 78 ); 79 80 const menuItem = findMenuItem(menuItems, value); 81 ok(menuItem, value + " added to the device selector."); 82 }); 83 84 info("Reopen device modal and check new device is correctly checked"); 85 await openDeviceModal(ui); 86 87 const previouslyClickedCb = [ 88 ...document.querySelectorAll(".device-input-checkbox"), 89 ].find(cb => cb.value === value); 90 ok(previouslyClickedCb.checked, value + " is checked in the device modal."); 91 92 // Tests where the user removes a featured device 93 info("Uncheck the first checked device different than the previous one"); 94 const checkboxes = [...document.querySelectorAll(".device-input-checkbox")]; 95 const checkedCb = checkboxes.find(cb => { 96 if (!cb.checked || cb.value == value) { 97 return false; 98 } 99 // In the list, we have devices with similar names (e.g. "Galaxy Note 20" and "Galaxy Note 20 Ultra") 100 // Given how some test helpers are using `includes` to check device names, we might 101 // get positive result for "Galaxy Note 20" although it would actually match "Galaxy Note 20 Ultra". 102 // To prevent such issue without modifying existing helpers, we're excluding any 103 // item whose name is part of another device. 104 return !checkboxes.some( 105 innerCb => 106 innerCb.value !== cb.value && innerCb.value.includes(cb.value) 107 ); 108 }); 109 const checkedVal = checkedCb.value; 110 checkedCb.click(); 111 document.getElementById("device-close-button").click(); 112 113 info("Checking that the device is removed from the user preference list."); 114 preferredDevices = _loadPreferredDevices(); 115 ok( 116 preferredDevices.removed.has(checkedVal), 117 checkedVal + " in removed list" 118 ); 119 120 info("Checking that the device is not in the device selector."); 121 await testMenuItems(toolWindow, deviceSelector, menuItems => { 122 is( 123 menuItems.length - 1, 124 featuredCount, 125 "Got expected number of devices in device selector." 126 ); 127 128 const menuItem = findMenuItem(menuItems, checkedVal); 129 ok(!menuItem, checkedVal + " removed from the device selector."); 130 }); 131 132 info("Reopen device modal and check device is correctly unchecked"); 133 await openDeviceModal(ui); 134 ok( 135 [...document.querySelectorAll(".device-input-checkbox")].filter( 136 cb => !cb.checked && cb.value === checkedVal 137 )[0], 138 checkedVal + " is unchecked in the device modal." 139 ); 140 141 // Let's add a dummy device to simulate featured flag changes for next test 142 addDeviceForTest(addedDevice); 143 }, 144 { waitForDeviceList: true } 145 ); 146 147 addRDMTask( 148 TEST_URL, 149 async function ({ ui }) { 150 const { toolWindow } = ui; 151 const { document } = toolWindow; 152 153 await openDeviceModal(ui); 154 155 const remoteList = await getDevices(); 156 const featuredCount = getNumberOfFeaturedDevices(remoteList); 157 const preferredDevices = _loadPreferredDevices(); 158 159 // Tests to prove that reloading the RDM didn't break our device list 160 info("Checking new featured device appears in the device selector."); 161 const deviceSelector = document.getElementById("device-selector"); 162 await testMenuItems(toolWindow, deviceSelector, items => { 163 is( 164 items.length - 1, 165 featuredCount - 166 preferredDevices.removed.size + 167 preferredDevices.added.size, 168 "Got expected number of devices in device selector." 169 ); 170 171 const added = findMenuItem(items, addedDevice.name); 172 ok(added, "Dummy device added to the device selector."); 173 174 for (const name of preferredDevices.added.keys()) { 175 const menuItem = findMenuItem(items, name); 176 ok(menuItem, "Device added by user still in the device selector."); 177 } 178 179 for (const name of preferredDevices.removed.keys()) { 180 const menuItem = findMenuItem(items, name); 181 ok(!menuItem, "Device removed by user not in the device selector."); 182 } 183 }); 184 }, 185 { waitForDeviceList: true } 186 ); 187 188 /** 189 * Returns the number of featured devices 190 * 191 * @param {Map} devicesByType: Map of devices, keyed by type (as returned by getDevices) 192 * @returns {Integer} 193 */ 194 function getNumberOfFeaturedDevices(devicesByType) { 195 let count = 0; 196 const devices = [...devicesByType.values()].flat(); 197 for (const device of devices) { 198 if (device.featured && device.os != "fxos") { 199 count++; 200 } 201 } 202 return count; 203 }