| 1 |
// Copyright 2014 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.style.transformTest'); |
| 16 |
goog.setTestOnly('goog.style.transformTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.style'); |
| 21 |
goog.require('goog.style.transform'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
goog.require('goog.userAgent'); |
| 24 |
goog.require('goog.userAgent.product.isVersion'); |
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Floating point equality tolerance. |
| 29 |
* @const {number} |
| 30 |
*/ |
| 31 |
var EPSILON = .0001; |
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* Element being transformed. |
| 36 |
* @type {!Element} |
| 37 |
*/ |
| 38 |
var element; |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Sets a transform translation and asserts the translation was applied. |
| 43 |
* @param {number} x The horizontal translation |
| 44 |
* @param {number} y The vertical translation |
| 45 |
*/ |
| 46 |
var setAndAssertTranslation = function(x, y) { |
| 47 |
if (goog.userAgent.GECKO || |
| 48 |
goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(10)) { |
| 49 |
// Mozilla and <IE10 do not support CSSMatrix. |
| 50 |
return; |
| 51 |
} |
| 52 |
var success = goog.style.transform.setTranslation(element, x, y); |
| 53 |
if (!goog.style.transform.isSupported()) { |
| 54 |
assertFalse(success); |
| 55 |
} else { |
| 56 |
assertTrue(success); |
| 57 |
var translation = goog.style.transform.getTranslation(element); |
| 58 |
assertEquals(x, translation.x); |
| 59 |
assertEquals(y, translation.y); |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* Sets a transform translation and asserts the translation was applied. |
| 66 |
* @param {number} x The horizontal scale |
| 67 |
* @param {number} y The vertical scale |
| 68 |
* @param {number} z The depth scale |
| 69 |
*/ |
| 70 |
var setAndAssertScale = function(x, y, z) { |
| 71 |
if (goog.userAgent.GECKO || |
| 72 |
goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(10)) { |
| 73 |
// Mozilla and <IE10 do not support CSSMatrix. |
| 74 |
return; |
| 75 |
} |
| 76 |
var success = goog.style.transform.setScale(element, x, y, z); |
| 77 |
if (!goog.style.transform.isSupported()) { |
| 78 |
assertFalse(success); |
| 79 |
} else { |
| 80 |
assertTrue(success); |
| 81 |
var scale = goog.style.transform.getScale(element); |
| 82 |
assertEquals(x, scale.x); |
| 83 |
assertEquals(y, scale.y); |
| 84 |
if (goog.style.transform.is3dSupported()) { |
| 85 |
assertEquals(z, scale.z); |
| 86 |
} |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* Sets a transform rotation and asserts the translation was applied. |
| 93 |
* @param {number|function(number):boolean} expectedDegrees |
| 94 |
* The expected resulting rotation in degrees, or a function to evaluate |
| 95 |
* the resulting rotation. |
| 96 |
* @param {string=} opt_transform The plaintext CSS transform value. |
| 97 |
*/ |
| 98 |
var setAndAssertRotation = function(expectedDegrees, opt_transform) { |
| 99 |
if (goog.userAgent.GECKO || |
| 100 |
goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(10)) { |
| 101 |
// Mozilla and <IE10 do not support CSSMatrix. |
| 102 |
return; |
| 103 |
} |
| 104 |
if (opt_transform) { |
| 105 |
goog.style.setStyle( |
| 106 |
element, goog.style.transform.getTransformProperty_(), opt_transform); |
| 107 |
} else { |
| 108 |
var success = |
| 109 |
goog.style.transform.setRotation(element, Number(expectedDegrees)); |
| 110 |
if (!goog.style.transform.isSupported()) { |
| 111 |
assertFalse(success); |
| 112 |
return; |
| 113 |
} else { |
| 114 |
assertTrue(success); |
| 115 |
} |
| 116 |
} |
| 117 |
var rotation = goog.style.transform.getRotation(element); |
| 118 |
if (expectedDegrees instanceof Function) { |
| 119 |
assertTrue('Incorrect rotation: ' + rotation, expectedDegrees(rotation)); |
| 120 |
} else { |
| 121 |
assertRoughlyEquals(expectedDegrees, rotation, EPSILON); |
| 122 |
} |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
function setUp() { |
| 127 |
element = goog.dom.createElement(goog.dom.TagName.DIV); |
| 128 |
goog.dom.appendChild(goog.dom.getDocument().body, element); |
| 129 |
} |
| 130 |
|
| 131 |
function tearDown() { |
| 132 |
goog.dom.removeNode(element); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
function testIsSupported() { |
| 137 |
if (goog.userAgent.IE && !goog.userAgent.product.isVersion(9)) { |
| 138 |
assertFalse(goog.style.transform.isSupported()); |
| 139 |
} else { |
| 140 |
assertTrue(goog.style.transform.isSupported()); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
function testIs3dSupported() { |
| 146 |
if (goog.userAgent.GECKO && !goog.userAgent.product.isVersion(10) || |
| 147 |
(goog.userAgent.IE && !goog.userAgent.product.isVersion(10))) { |
| 148 |
assertFalse(goog.style.transform.is3dSupported()); |
| 149 |
} else { |
| 150 |
assertTrue(goog.style.transform.is3dSupported()); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
function testTranslateX() { |
| 155 |
setAndAssertTranslation(10, 0); |
| 156 |
} |
| 157 |
|
| 158 |
function testTranslateY() { |
| 159 |
setAndAssertTranslation(0, 10); |
| 160 |
} |
| 161 |
|
| 162 |
function testTranslateXY() { |
| 163 |
setAndAssertTranslation(10, 20); |
| 164 |
} |
| 165 |
|
| 166 |
function testScaleX() { |
| 167 |
setAndAssertScale(5, 1, 1); |
| 168 |
} |
| 169 |
|
| 170 |
function testScaleY() { |
| 171 |
setAndAssertScale(1, 3, 1); |
| 172 |
} |
| 173 |
|
| 174 |
function testScaleZ() { |
| 175 |
setAndAssertScale(1, 1, 8); |
| 176 |
} |
| 177 |
|
| 178 |
function testScale() { |
| 179 |
setAndAssertScale(2, 2, 2); |
| 180 |
} |
| 181 |
|
| 182 |
function testRotatePositive() { |
| 183 |
setAndAssertRotation(90); |
| 184 |
} |
| 185 |
|
| 186 |
function testRotateNegative() { |
| 187 |
setAndAssertRotation(-90); |
| 188 |
} |
| 189 |
|
| 190 |
function testGetRotationWhenScaledUp() { |
| 191 |
setAndAssertRotation(90, 'scale(5) rotate3d(0,0,1,90deg)'); |
| 192 |
} |
| 193 |
|
| 194 |
function testGetRotationWhenScaledDown() { |
| 195 |
setAndAssertRotation(90, 'scale(.5) rotate3d(0,0,1,90deg)'); |
| 196 |
} |
| 197 |
|
| 198 |
function testGetRotationWithSkew() { |
| 199 |
setAndAssertRotation(0, 'skew(30deg, 30deg)'); |
| 200 |
// NOTE: Non-zero rotations are not well-defined with a skew, but the lower |
| 201 |
// and upper bounds are. So check that the rotation is within these bounds. |
| 202 |
setAndAssertRotation(function(x) { |
| 203 |
return (x > 0 && x < 30); |
| 204 |
}, 'skew(0, 30deg)'); |
| 205 |
setAndAssertRotation(function(x) { |
| 206 |
return (x < 0 && x > -30); |
| 207 |
}, 'skew(30deg, 0)'); |
| 208 |
} |