summaryrefslogtreecommitdiffstats
path: root/layout/style/test/test_flexbox_flex_shorthand.html
blob: 24bffed949f11d8ee81be66489944d99edb3662e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=696253
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 696253</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="property_database.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=696253">Mozilla Bug 696253</a>
<div id="display">
  <div id="content">
  </div>
</div>
<pre id="test">
<script type="application/javascript;version=1.7">
"use strict";

/** Test for Bug 696253 **/
/* (Testing the 'flex' CSS shorthand property) */

// The CSS property name for the shorthand we're testing:
const gFlexPropName = "flex";

// Info from property_database.js on this property:
const gFlexPropInfo = gCSSProperties[gFlexPropName];

// The name of the property in the DOM (i.e. in elem.style):
// (NOTE: In this case it's actually the same as the CSS property name --
// "flex" -- but that's not guaranteed in general.)
const gFlexDOMName = gFlexPropInfo.domProp;

// Default values for shorthand subproperties, when they're not specified
// explicitly in a testcase.  This lets the testcases be more concise.
//
// The values here are from the flexbox spec on the 'flex' shorthand:
//   "When omitted, [flex-grow and flex-shrink are] set to '1'."
//   "When omitted [..., flex-basis's] specified value is '0%'."
let gFlexShorthandDefaults = {
    "flex-grow":   "1",
    "flex-shrink": "1",
    "flex-basis":  "0%"
};

let gFlexShorthandTestcases = [
/*
  {
    "flex":        "SPECIFIED value for flex shorthand",

    // Expected Computed Values of Subproperties
    // Semi-optional -- if unspecified, the expected value is taken
    // from gFlexShorthandDefaults.
    "flex-grow":   "EXPECTED computed value for flex-grow property",
    "flex-shrink": "EXPECTED computed value for flex-shrink property",
    "flex-basis":  "EXPECTED computed value for flex-basis property",
  },
*/

  // Initial values of subproperties:
  // --------------------------------
  // (checked by another test that uses property_database.js, too, but
  // might as well check here, too, for thoroughness).
  {
    "flex":        "",
    "flex-grow":   "0",
    "flex-shrink": "1",
    "flex-basis":  "auto",
  },
  {
    "flex":        "initial",
    "flex-grow":   "0",
    "flex-shrink": "1",
    "flex-basis":  "auto",
  },

  // Special keyword "none" --> "0 0 auto"
  // -------------------------------------
  {
    "flex":        "none",
    "flex-grow":   "0",
    "flex-shrink": "0",
    "flex-basis":  "auto",
  },

  // One Value (numeric) --> sets flex-grow
  // --------------------------------------
  {
    "flex":        "0",
    "flex-grow":   "0",
  },
  {
    "flex":        "5",
    "flex-grow":   "5",
  },
  {
    "flex":        "1000",
    "flex-grow":   "1000",
  },
  {
    "flex":        "0.0000001",
    "flex-grow":   "1e-7"
  },
  {
    "flex":        "20000000",
    "flex-grow":   "2e+7"
  },

  // One Value (length or other nonnumeric) --> sets flex-basis
  // ----------------------------------------------------------
  {
    "flex":        "0px",
    "flex-basis":  "0px",
  },
  {
    "flex":        "0%",
    "flex-basis":  "0%",
  },
  {
    "flex":        "25px",
    "flex-basis":  "25px",
  },
  {
    "flex":        "5%",
    "flex-basis":  "5%",
  },
  {
    "flex":        "auto",
    "flex-basis":  "auto",
  },
  {
    "flex":        "-moz-fit-content",
    "flex-basis":  "-moz-fit-content",
  },
  {
    "flex":        "calc(5px + 6px)",
    "flex-basis":  "11px",
  },
  {
    "flex":        "calc(15% + 30px)",
    "flex-basis":  "calc(30px + 15%)",
  },

  // Two Values (numeric) --> sets flex-grow, flex-shrink
  // ----------------------------------------------------
  {
    "flex":        "0 0",
    "flex-grow":   "0",
    "flex-shrink": "0",
  },
  {
    "flex":        "0 2",
    "flex-grow":   "0",
    "flex-shrink": "2",
  },
  {
    "flex":        "3 0",
    "flex-grow":   "3",
    "flex-shrink": "0",
  },
  {
    "flex":        "0.5000 2.03",
    "flex-grow":   "0.5",
    "flex-shrink": "2.03",
  },
  {
    "flex":        "300.0 500.0",
    "flex-grow":   "300",
    "flex-shrink": "500",
  },

  // Two Values (numeric & length-ish) --> sets flex-grow, flex-basis
  // ----------------------------------------------------------------
  {
    "flex":        "0 0px",
    "flex-grow":   "0",
    "flex-basis":  "0px",
  },
  {
    "flex":        "0 0%",
    "flex-grow":   "0",
    "flex-basis":  "0%",
  },
  {
    "flex":        "10 30px",
    "flex-grow":   "10",
    "flex-basis":  "30px",
  },
  {
    "flex":        "99px 2.3",
    "flex-grow":   "2.3",
    "flex-basis":  "99px",
  },
  {
    "flex":        "99% 6",
    "flex-grow":   "6",
    "flex-basis":  "99%",
  },
  {
    "flex":        "auto 5",
    "flex-grow":   "5",
    "flex-basis":  "auto",
  },
  {
    "flex":        "5 -moz-fit-content",
    "flex-grow":   "5",
    "flex-basis":  "-moz-fit-content",
  },
  {
    "flex":        "calc(5% + 10px) 3",
    "flex-grow":   "3",
    "flex-basis":  "calc(10px + 5%)",
  },

  // Three Values --> Sets all three subproperties
  // ---------------------------------------------
  {
    "flex":        "0 0 0",
    "flex-grow":   "0",
    "flex-shrink": "0",
    "flex-basis":  "0px",
  },
  {
    "flex":        "0.0 0.00 0px",
    "flex-grow":   "0",
    "flex-shrink": "0",
    "flex-basis":  "0px",
  },
  {
    "flex":        "0% 0 0",
    "flex-grow":   "0",
    "flex-shrink": "0",
    "flex-basis":  "0%",
  },
  {
    "flex":        "10px 3 2",
    "flex-grow":   "3",
    "flex-shrink": "2",
    "flex-basis":  "10px",
  },
];

function runFlexShorthandTest(aFlexShorthandTestcase)
{
  let content = document.getElementById("content");

  let elem = document.createElement("div");

  elem.style[gFlexDOMName] = aFlexShorthandTestcase[gFlexPropName];
  content.appendChild(elem);

  gFlexPropInfo.subproperties.forEach(function(aSubPropName) {
    var expectedVal = aSubPropName in aFlexShorthandTestcase ?
     aFlexShorthandTestcase[aSubPropName] :
     gFlexShorthandDefaults[aSubPropName];

    // Compare computed value against expected computed value (from testcase)
    is(window.getComputedStyle(elem, null).getPropertyValue(aSubPropName),
       expectedVal,
       "Computed value of subproperty \"" + aSubPropName + "\" when we set \"" +
       gFlexPropName + ": " + aFlexShorthandTestcase[gFlexPropName] + "\"");
  });

  // Clean up
  content.removeChild(elem);
}

function main() {
  gFlexShorthandTestcases.forEach(runFlexShorthandTest);
}

main();

</script>
</pre>
</body>
</html>