OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * @fileoverview A standard set of tests for using a single property in an |
| 3 * inline StylePropertyMap |
| 4 * (https://www.w3.org/TR/css-typed-om-1/#the-stylepropertymap). |
| 5 * |
| 6 * Create a config object containing { |
| 7 * validKeywords: array of strings, |
| 8 * validObjects: array of CSSStyleValue instances that are valid for the |
| 9 * property, |
| 10 * supportsMultiple: boolean; whether the property supports a list of |
| 11 * properties, |
| 12 * invalidObjects: array of CSSStyleValue instances that are invalid for the |
| 13 * property |
| 14 * } |
| 15 * |
| 16 * Then, call runInlineStylePropertyMapTests to test the behavior of an inline |
| 17 * StylePropertyMap for one CSS property for an element. |
| 18 * |
| 19 * Note that CSS-wide keywords, and non-CSSStyleValue invalid types do not need |
| 20 * to be specified. |
| 21 * |
| 22 * If necessary, you can continue to define non-standard tests in your html |
| 23 * file as usual. |
| 24 */ |
| 25 |
| 26 function runInlineStylePropertyMapTests(config) { |
| 27 let element = document.createElement('div'); |
| 28 let validKeywords = config.validKeywords.concat([ |
| 29 // CSS-wide keywords |
| 30 'initial', |
| 31 'inherit', |
| 32 'unset' |
| 33 ]); |
| 34 let validObjects = config.validObjects; |
| 35 let invalidObjects = config.invalidObjects.concat([ |
| 36 // No properties should take these values |
| 37 null, |
| 38 undefined, |
| 39 true, |
| 40 false, |
| 41 1, |
| 42 'hello', |
| 43 {}, |
| 44 new CSSKeywordValue('notAKeyword') |
| 45 ]); |
| 46 let validObject = validObjects.length ? |
| 47 validObjects[0] : new CSSKeywordValue(validKeywords[0]); |
| 48 |
| 49 let styleMap = element.styleMap; |
| 50 runSetterTests( |
| 51 config.property, validKeywords, validObjects, invalidObjects, element); |
| 52 runGetterTests(config.property, validKeywords, validObjects, element); |
| 53 runGetAllTests( |
| 54 config.property, validObject, element, config.supportsMultiple); |
| 55 runDeletionTests(config.property, validObject, element); |
| 56 runGetPropertiesTests(config.property, validObject, element); |
| 57 if (config.supportsMultiple) { |
| 58 runSequenceSetterTests( |
| 59 config.property, validObject, invalidObjects[0], element); |
| 60 runAppendTests( |
| 61 config.property, validObject, invalidObjects[0], element); |
| 62 } else { |
| 63 runMultipleValuesNotSupportedTests( |
| 64 config.property, validObject, element); |
| 65 } |
| 66 } |
| 67 |
| 68 function runSetterTests( |
| 69 propertyName, validKeywords, validObjects, invalidObjects, element) { |
| 70 for (let keyword of validKeywords) { |
| 71 test(function() { |
| 72 element.style = ''; |
| 73 element.styleMap.set(propertyName, new CSSKeywordValue(keyword)); |
| 74 assert_equals(element.style[propertyName], keyword); |
| 75 }, 'Setting ' + propertyName + ' to ' + keyword); |
| 76 } |
| 77 for (let validObject of validObjects) { |
| 78 test(function() { |
| 79 element.style = ''; |
| 80 |
| 81 element.styleMap.set(propertyName, validObject); |
| 82 assert_equals(element.style[propertyName], validObject.cssText); |
| 83 }, 'Setting ' + propertyName + ' to ' + validObject.constructor.name + |
| 84 ' with value ' + validObject.cssText); |
| 85 } |
| 86 |
| 87 // Negative tests |
| 88 for (let invalidObject of invalidObjects) { |
| 89 let name = invalidObject instanceof CSSStyleValue ? |
| 90 invalidObject.constructor.name : invalidObject; |
| 91 test(function() { |
| 92 assert_throws(new TypeError(), function() { |
| 93 element.styleMap.set(propertyName, invalidObject); |
| 94 }); |
| 95 }, 'Setting ' + propertyName + ' to invalid value ' + name + ' throws'); |
| 96 } |
| 97 } |
| 98 |
| 99 function runGetterTests( |
| 100 propertyName, validKeywords, validObjects, element) { |
| 101 for (let keyword of validKeywords) { |
| 102 test(function() { |
| 103 element.style[propertyName] = keyword; |
| 104 |
| 105 let result = element.styleMap.get(propertyName); |
| 106 assert_true(result instanceof CSSKeywordValue, |
| 107 'result instanceof CSSKeywordValue:'); |
| 108 assert_equals(result.cssText, keyword); |
| 109 }, 'Getting ' + propertyName + ' when it is set to ' + keyword); |
| 110 } |
| 111 for (let validObject of validObjects) { |
| 112 test(function() { |
| 113 element.style[propertyName] = validObject.cssText; |
| 114 |
| 115 let result = element.styleMap.get(propertyName); |
| 116 assert_equals(result.constructor.name, validObject.constructor.name, |
| 117 'typeof result'); |
| 118 assert_equals(result.cssText, validObject.cssText); |
| 119 }, 'Getting ' + propertyName + ' with a ' + validObject.constructor.name + |
| 120 ' whose value is ' + validObject.cssText); |
| 121 } |
| 122 } |
| 123 |
| 124 function runSequenceSetterTests( |
| 125 propertyName, validObject, invalidObject, element) { |
| 126 test(function() { |
| 127 element.style = ''; |
| 128 |
| 129 element.styleMap.set(propertyName, [validObject, validObject]); |
| 130 assert_equals( |
| 131 element.style[propertyName], validObject.cssText + ', ' + |
| 132 validObject.cssText); |
| 133 }, 'Set ' + propertyName + ' to a sequence'); |
| 134 |
| 135 test(function() { |
| 136 let sequence = [validObject, invalidObject]; |
| 137 assert_throws(new TypeError(), function() { |
| 138 element.styleMap.set(propertyName, sequence); |
| 139 }); |
| 140 }, 'Set ' + propertyName + ' to a sequence containing an invalid type'); |
| 141 } |
| 142 |
| 143 function runAppendTests( |
| 144 propertyName, validObject, invalidObject, element) { |
| 145 test(function() { |
| 146 element.style = ''; |
| 147 |
| 148 element.styleMap.append(propertyName, validObject); |
| 149 assert_equals(element.style[propertyName], validObject.cssText); |
| 150 |
| 151 element.styleMap.append(propertyName, validObject); |
| 152 assert_equals( |
| 153 element.style[propertyName], validObject.cssText + ', ' + |
| 154 validObject.cssText); |
| 155 }, 'Appending a ' + validObject.constructor.name + ' to ' + propertyName); |
| 156 |
| 157 test(function() { |
| 158 element.style = ''; |
| 159 |
| 160 element.styleMap.append(propertyName, [validObject, validObject]); |
| 161 assert_equals( |
| 162 element.style[propertyName], validObject.cssText + ', ' + |
| 163 validObject.cssText); |
| 164 }, 'Append a sequence to ' + propertyName); |
| 165 |
| 166 // Negative tests |
| 167 test(function() { |
| 168 assert_throws(new TypeError(), function() { |
| 169 element.styleMap.append(propertyName, invalidObject); |
| 170 }); |
| 171 }, 'Appending an invalid value to ' + propertyName); |
| 172 |
| 173 test(function() { |
| 174 let sequence = [validObject, invalidObject]; |
| 175 assert_throws(new TypeError(), function() { |
| 176 element.styleMap.append(propertyName, sequence); |
| 177 }); |
| 178 }, 'Append a sequence containing an invalid value to ' + propertyName); |
| 179 } |
| 180 |
| 181 function runGetAllTests( |
| 182 propertyName, validObject, element, supportsMultiple) { |
| 183 test(function() { |
| 184 element.style = ''; |
| 185 assert_array_equals(element.styleMap.getAll(propertyName), []); |
| 186 |
| 187 element.style[propertyName] = validObject.cssText; |
| 188 let result = element.styleMap.getAll(propertyName); |
| 189 assert_equals(result.length, 1, |
| 190 'Expected getAll to retrieve an array containing a ' + |
| 191 'single CSSStyleValue'); |
| 192 assert_equals(result[0].constructor.name, validObject.constructor.name, |
| 193 'Returned type is incorrect:'); |
| 194 assert_equals(result[0].cssText, validObject.cssText); |
| 195 }, 'getAll for single-valued ' + propertyName); |
| 196 |
| 197 if (supportsMultiple) { |
| 198 test(function() { |
| 199 element.style = ''; |
| 200 element.styleMap.set(propertyName, [validObject, validObject]); |
| 201 let result = element.styleMap.getAll(propertyName); |
| 202 assert_equals(result.length, 2, |
| 203 'Expected getAll to return an array containing two instances ' + |
| 204 'of CSSStyleValue'); |
| 205 assert_true(result[0] instanceof CSSStyleValue, |
| 206 'Expected first result to be an instance of CSSStyleValue'); |
| 207 assert_true(result[1] instanceof CSSStyleValue, |
| 208 'Expected second result to be an instance of CSSStyleValue'); |
| 209 assert_equals(result[0].constructor.name, validObject.constructor.name); |
| 210 assert_equals(result[1].constructor.name, validObject.constructor.name); |
| 211 assert_equals(result[0].cssText, validObject.cssText); |
| 212 assert_equals(result[1].cssText, validObject.cssText); |
| 213 }, 'getAll for list-valued ' + propertyName); |
| 214 } |
| 215 } |
| 216 |
| 217 function runDeletionTests(propertyName, validObject, element) { |
| 218 test(function() { |
| 219 element.style[propertyName] = validObject.cssText; |
| 220 |
| 221 assert_not_equals(element.styleMap.get(propertyName), null); |
| 222 |
| 223 element.styleMap.delete(propertyName); |
| 224 assert_equals(element.style[propertyName], ''); |
| 225 assert_equals(element.styleMap.get(propertyName), null); |
| 226 }, 'Delete ' + propertyName + ' removes the value form the styleMap'); |
| 227 } |
| 228 |
| 229 function runGetPropertiesTests(propertyName, validObject, element) { |
| 230 test(function() { |
| 231 element.style = ''; |
| 232 assert_array_equals(element.styleMap.getProperties(), []); |
| 233 |
| 234 element.styleMap.set(propertyName, validObject); |
| 235 |
| 236 assert_array_equals(element.styleMap.getProperties(), [propertyName]); |
| 237 }, propertyName + ' shows up in getProperties'); |
| 238 } |
| 239 |
| 240 function runMultipleValuesNotSupportedTests( |
| 241 propertyName, validObject, element) { |
| 242 test(function() { |
| 243 element.style = ''; |
| 244 assert_throws(new TypeError(), function() { |
| 245 element.styleMap.set(propertyName, [validObject, validObject]); |
| 246 }); |
| 247 }, 'Setting ' + propertyName + ' to a sequence throws'); |
| 248 |
| 249 test(function() { |
| 250 element.style = ''; |
| 251 assert_throws(new TypeError(), function() { |
| 252 element.styleMap.append(propertyName, validObject); |
| 253 }); |
| 254 }, 'Appending to ' + propertyName + ' throws'); |
| 255 } |
| 256 |
OLD | NEW |