browser_fontinspector_editor-letter-spacing-conversion.js (1896B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 /* global getPropertyValue */ 4 5 "use strict"; 6 7 // Unit test for math behind conversion of units for letter-spacing. 8 9 const TEST_URI = ` 10 <style type='text/css'> 11 body { 12 /* Set root font-size to equivalent of 32px (2*16px) */ 13 font-size: 200%; 14 } 15 div { 16 letter-spacing: 1em; 17 } 18 </style> 19 <div>LETTER SPACING</div> 20 `; 21 22 add_task(async function () { 23 const URI = "data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI); 24 const { inspector, view } = await openFontInspectorForURL(URI); 25 const viewDoc = view.document; 26 const property = "letter-spacing"; 27 const UNITS = { 28 px: 32, 29 pt: 24, 30 rem: 2, 31 em: 1, 32 }; 33 34 await selectNode("div", inspector); 35 36 info("Check that font editor shows letter-spacing value in original units"); 37 const letterSpacing = getPropertyValue(viewDoc, property); 38 is( 39 letterSpacing.value + letterSpacing.unit, 40 "1em", 41 "Original letter spacing is 1em" 42 ); 43 44 // Starting value and unit for conversion. 45 let prevValue = letterSpacing.value; 46 let prevUnit = letterSpacing.unit; 47 48 for (const unit in UNITS) { 49 const value = UNITS[unit]; 50 51 info(`Convert letter-spacing from ${prevValue}${prevUnit} to ${unit}`); 52 const convertedValue = await view.convertUnits( 53 property, 54 prevValue, 55 prevUnit, 56 unit 57 ); 58 is( 59 convertedValue, 60 value, 61 `Converting to ${unit} returns transformed value.` 62 ); 63 64 // Store current unit and value to use in conversion on the next iteration. 65 prevUnit = unit; 66 prevValue = value; 67 } 68 69 info(`Check that conversion to fake unit returns 1-to-1 mapping`); 70 const valueToFakeUnit = await view.convertUnits(property, 1, "px", "fake"); 71 is(valueToFakeUnit, 1, `Converting to fake unit returns same value.`); 72 });