layout/style/test/test_flexbox_layout.html

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=666041
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <meta charset="utf-8">
michael@0 8 <title>Test for Bug 666041</title>
michael@0 9 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 10 <script type="text/javascript" src="flexbox_layout_testcases.js"></script>
michael@0 11 <script type="text/javascript" src="property_database.js"></script>
michael@0 12 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
michael@0 13 </head>
michael@0 14 <body>
michael@0 15 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=666041">Mozilla Bug 666041</a>
michael@0 16 <div id="display">
michael@0 17 <div id="content">
michael@0 18 </div>
michael@0 19 </div>
michael@0 20 <pre id="test">
michael@0 21 <script type="application/javascript;version=1.7">
michael@0 22 "use strict";
michael@0 23
michael@0 24 /** Test for Bug 666041 **/
michael@0 25
michael@0 26 /* Flexbox Layout Tests
michael@0 27 * --------------------
michael@0 28 * This mochitest exercises our implementation of the flexbox layout algorithm
michael@0 29 * by creating a flex container, inserting some flexible children, and then
michael@0 30 * verifying that the computed width of those children is what we expect.
michael@0 31 *
michael@0 32 * See flexbox_layout_testcases.js for the actual testcases & testcase format.
michael@0 33 */
michael@0 34
michael@0 35 function getComputedStyleWrapper(elem, prop)
michael@0 36 {
michael@0 37 return window.getComputedStyle(elem, null).getPropertyValue(prop);
michael@0 38 }
michael@0 39
michael@0 40 function setPossiblyAliasedProperty(aElem, aPropertyName, aPropertyValue,
michael@0 41 aPropertyMapping)
michael@0 42 {
michael@0 43 let actualPropertyName = (aPropertyName in aPropertyMapping ?
michael@0 44 aPropertyMapping[aPropertyName] : aPropertyName);
michael@0 45
michael@0 46 if (!gCSSProperties[actualPropertyName]) {
michael@0 47 ok(false, "Bug in test: property '" + actualPropertyName +
michael@0 48 "' doesn't exist in gCSSProperties");
michael@0 49 } else {
michael@0 50 let domPropertyName = gCSSProperties[actualPropertyName].domProp;
michael@0 51 aElem.style[domPropertyName] = aPropertyValue;
michael@0 52 }
michael@0 53 }
michael@0 54
michael@0 55 // Helper function to strip "px" off the end of a string
michael@0 56 // (so that we can compare two lengths using "isfuzzy()" with an epsilon)
michael@0 57 function stripPx(aLengthInPx)
michael@0 58 {
michael@0 59 let pxOffset = aLengthInPx.length - 2; // subtract off length of "px"
michael@0 60
michael@0 61 // Sanity-check the arg:
michael@0 62 ok(pxOffset > 0 && aLengthInPx.substr(pxOffset) == "px",
michael@0 63 "expecting value with 'px' units");
michael@0 64
michael@0 65 return aLengthInPx.substr(0, pxOffset);
michael@0 66 }
michael@0 67
michael@0 68 // The main test function.
michael@0 69 // aFlexboxTestcase is an entry from the list in flexbox_layout_testcases.js
michael@0 70 function testFlexboxTestcase(aFlexboxTestcase, aFlexDirection, aPropertyMapping)
michael@0 71 {
michael@0 72 let content = document.getElementById("content");
michael@0 73
michael@0 74 // Create flex container
michael@0 75 let flexContainer = document.createElement("div");
michael@0 76 flexContainer.style.display = "flex";
michael@0 77 flexContainer.style.flexDirection = aFlexDirection;
michael@0 78 setPossiblyAliasedProperty(flexContainer, "_main-size",
michael@0 79 gDefaultFlexContainerSize,
michael@0 80 aPropertyMapping);
michael@0 81
michael@0 82 // Apply testcase's customizations for flex container (if any).
michael@0 83 if (aFlexboxTestcase.container_properties) {
michael@0 84 for (let propName in aFlexboxTestcase.container_properties) {
michael@0 85 let propValue = aFlexboxTestcase.container_properties[propName];
michael@0 86 setPossiblyAliasedProperty(flexContainer, propName, propValue,
michael@0 87 aPropertyMapping);
michael@0 88 }
michael@0 89 }
michael@0 90
michael@0 91 // Create & append flex items
michael@0 92 aFlexboxTestcase.items.forEach(function(aChildSpec) {
michael@0 93 // Create an element for our item
michael@0 94 let child = document.createElement("div");
michael@0 95
michael@0 96 // Set all the specified properties on our item
michael@0 97 for (let propName in aChildSpec) {
michael@0 98 // aChildSpec[propName] is either a specified value,
michael@0 99 // or an array of [specifiedValue, computedValue]
michael@0 100 let specifiedValue = Array.isArray(aChildSpec[propName]) ?
michael@0 101 aChildSpec[propName][0] :
michael@0 102 aChildSpec[propName];
michael@0 103
michael@0 104 // SANITY CHECK:
michael@0 105 if (Array.isArray(aChildSpec[propName])) {
michael@0 106 ok(aChildSpec[propName].length >= 2 &&
michael@0 107 aChildSpec[propName].length <= 3,
michael@0 108 "unexpected number of elements in array within child spec");
michael@0 109 }
michael@0 110
michael@0 111 if (specifiedValue !== null) {
michael@0 112 setPossiblyAliasedProperty(child, propName, specifiedValue,
michael@0 113 aPropertyMapping);
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 // Append the item to the flex container
michael@0 118 flexContainer.appendChild(child);
michael@0 119 });
michael@0 120
michael@0 121 // Append the flex container
michael@0 122 content.appendChild(flexContainer);
michael@0 123
michael@0 124 // NOW: Test the computed style on the flex items
michael@0 125 let child = flexContainer.firstChild;
michael@0 126 for (let i = 0; i < aFlexboxTestcase.items.length; i++) {
michael@0 127 if (!child) { // sanity
michael@0 128 ok(false, "should have created a child for each child-spec");
michael@0 129 }
michael@0 130
michael@0 131 let childSpec = aFlexboxTestcase.items[i];
michael@0 132 for (let propName in childSpec) {
michael@0 133 if (Array.isArray(childSpec[propName])) {
michael@0 134 let expectedVal = childSpec[propName][1];
michael@0 135 let actualPropName = (propName in aPropertyMapping ?
michael@0 136 aPropertyMapping[propName] : propName);
michael@0 137 let actualVal = getComputedStyleWrapper(child, actualPropName);
michael@0 138 let message = "computed value of '" + actualPropName +
michael@0 139 "' should match expected";
michael@0 140
michael@0 141 if (childSpec[propName].length > 2) {
michael@0 142 // 3rd entry in array is epsilon
michael@0 143 // Need to strip off "px" units in order to use epsilon:
michael@0 144 let actualValNoPx = stripPx(actualVal);
michael@0 145 let expectedValNoPx = stripPx(expectedVal);
michael@0 146 isfuzzy(actualValNoPx, expectedValNoPx,
michael@0 147 childSpec[propName][2], message);
michael@0 148 } else {
michael@0 149 is(actualVal, expectedVal, message);
michael@0 150 }
michael@0 151 }
michael@0 152 }
michael@0 153
michael@0 154 child = child.nextSibling;
michael@0 155 }
michael@0 156
michael@0 157 // Clean up: drop the flex container.
michael@0 158 content.removeChild(flexContainer);
michael@0 159 }
michael@0 160
michael@0 161 function main()
michael@0 162 {
michael@0 163 gFlexboxTestcases.forEach(
michael@0 164 function(aTestcase) {
michael@0 165 testFlexboxTestcase(aTestcase, "",
michael@0 166 gRowPropertyMapping);
michael@0 167 testFlexboxTestcase(aTestcase, "row",
michael@0 168 gRowPropertyMapping);
michael@0 169 testFlexboxTestcase(aTestcase, "row-reverse",
michael@0 170 gRowReversePropertyMapping);
michael@0 171 testFlexboxTestcase(aTestcase, "column",
michael@0 172 gColumnPropertyMapping);
michael@0 173 testFlexboxTestcase(aTestcase, "column-reverse",
michael@0 174 gColumnReversePropertyMapping);
michael@0 175 }
michael@0 176 );
michael@0 177 }
michael@0 178
michael@0 179 main();
michael@0 180
michael@0 181 </script>
michael@0 182 </pre>
michael@0 183 </body>
michael@0 184 </html>

mercurial