@@ -9,7 +9,8 @@ import * as color from "tns-core-modules/color";
99
1010import { SelectorCore } from "tns-core-modules/ui/styling/css-selector" ;
1111
12- //import * as styling from "ui/styling";
12+ const DELTA = 1 ;
13+ const SCALE_DELTA = 0.001 ;
1314
1415function createAnimationFromCSS ( css : string , name : string ) : keyframeAnimation . KeyframeAnimationInfo {
1516 let scope = new styleScope . StyleScope ( ) ;
@@ -113,72 +114,179 @@ export function test_ReadKeyframe() {
113114 TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . property , "backgroundColor" , "Keyframe declarations are not correct" ) ;
114115}
115116
117+ export function test_ReadTransformAllSet ( ) {
118+ const css = ".test { animation-name: test; } @keyframes test { to { transform: rotate(10) scaleX(5) translate(100, 200); } }" ;
119+ const animation = createAnimationFromCSS ( css , "test" ) ;
120+ const { rotate, scale, translate } = getTransformsValues ( animation . keyframes [ 0 ] . declarations ) ;
121+
122+ TKUnit . assertAreClose ( rotate , 10 , DELTA ) ;
123+
124+ TKUnit . assertAreClose ( scale . x , 5 , SCALE_DELTA ) ;
125+ TKUnit . assertAreClose ( scale . y , 1 , SCALE_DELTA ) ;
126+
127+ TKUnit . assertAreClose ( translate . x , 100 , DELTA ) ;
128+ TKUnit . assertAreClose ( translate . y , 200 , DELTA ) ;
129+ }
130+
131+ export function test_ReadTransformNone ( ) {
132+ const css = ".test { animation-name: test; } @keyframes test { to { transform: none; } }" ;
133+ const animation = createAnimationFromCSS ( css , "test" ) ;
134+ const { rotate, scale, translate } = getTransformsValues ( animation . keyframes [ 0 ] . declarations ) ;
135+
136+ TKUnit . assertEqual ( rotate , 0 ) ;
137+
138+ TKUnit . assertEqual ( scale . x , 1 ) ;
139+ TKUnit . assertEqual ( scale . y , 1 ) ;
140+
141+ TKUnit . assert ( translate . x === 0 ) ;
142+ TKUnit . assert ( translate . y === 0 ) ;
143+ }
144+
116145export function test_ReadScale ( ) {
117- let animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: scaleX(5),scaleY(10); } }" , "test" ) ;
118- let scale = animation . keyframes [ 0 ] . declarations [ 0 ] . value ;
119- TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . property , "scale" ) ;
120- TKUnit . assert ( scale . x === 5 && scale . y === 10 ) ;
121- animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: scale(-5, 12.3pt); } }" , "test" ) ;
122- scale = animation . keyframes [ 0 ] . declarations [ 0 ] . value ;
123- TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . property , "scale" ) ;
124- TKUnit . assert ( scale . x === - 5 && scale . y === 12.3 ) ;
125- animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: scaleY(10); } }" , "test" ) ;
126- scale = animation . keyframes [ 0 ] . declarations [ 0 ] . value ;
127- TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . property , "scale" ) ;
128- TKUnit . assert ( scale . x === 1 && scale . y === 10 ) ;
129- animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: scale3d(10, 20, 30); } }" , "test" ) ;
130- scale = animation . keyframes [ 0 ] . declarations [ 0 ] . value ;
131- TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . property , "scale" ) ;
132- TKUnit . assert ( scale . x === 10 && scale . y === 20 ) ;
146+ const animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: scale(-5, 12.3pt); } }" , "test" ) ;
147+ const { scale, rotate } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
148+
149+ TKUnit . assertEqual ( scale . property , "scale" ) ;
150+ TKUnit . assertAreClose ( scale . value . x , - 5 , DELTA ) ;
151+ TKUnit . assertAreClose ( scale . value . y , 12.3 , DELTA ) ;
152+ }
153+
154+ export function test_ReadScaleSingle ( ) {
155+ const animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: scale(2); } }" , "test" ) ;
156+ const { scale } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
157+
158+ TKUnit . assertEqual ( scale . property , "scale" ) ;
159+ TKUnit . assertAreClose ( scale . value . x , 2 , DELTA ) ;
160+ TKUnit . assertAreClose ( scale . value . y , 2 , DELTA ) ;
161+ }
162+
163+ export function test_ReadScaleXY ( ) {
164+ const css = ".test { animation-name: test; } @keyframes test { to { transform: scaleX(5) scaleY(10); } }" ;
165+ const animation = createAnimationFromCSS ( css , "test" ) ;
166+ const { scale } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
167+
168+ TKUnit . assertEqual ( scale . property , "scale" ) ;
169+ TKUnit . assertAreClose ( scale . value . x , 5 , SCALE_DELTA ) ;
170+ TKUnit . assertAreClose ( scale . value . y , 10 , SCALE_DELTA ) ;
171+ }
172+
173+ export function test_ReadScaleX ( ) {
174+ const css = ".test { animation-name: test; } @keyframes test { to { transform: scaleX(12.5); } }" ;
175+ const animation = createAnimationFromCSS ( css , "test" ) ;
176+ const { scale } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
177+
178+ TKUnit . assertEqual ( scale . property , "scale" ) ;
179+ TKUnit . assertAreClose ( scale . value . x , 12.5 , SCALE_DELTA ) ;
180+ // y defaults to 1
181+ TKUnit . assertAreClose ( scale . value . y , 1 , SCALE_DELTA ) ;
182+ }
183+
184+ export function test_ReadScaleY ( ) {
185+ const css = ".test { animation-name: test; } @keyframes test { to { transform: scaleY(10); } }" ;
186+ const animation = createAnimationFromCSS ( css , "test" ) ;
187+ const { scale } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
188+
189+ TKUnit . assertEqual ( scale . property , "scale" ) ;
190+ TKUnit . assertAreClose ( scale . value . y , 10 , SCALE_DELTA ) ;
191+ // x defaults to 1
192+ TKUnit . assertAreClose ( scale . value . x , 1 , SCALE_DELTA ) ;
193+ }
194+
195+ export function test_ReadScale3d ( ) {
196+ const css = ".test { animation-name: test; } @keyframes test { to { transform: scale3d(10, 20, 30); } }" ;
197+ const animation = createAnimationFromCSS ( css , "test" ) ;
198+ const { scale } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
199+
200+ TKUnit . assertEqual ( scale . property , "scale" ) ;
201+ TKUnit . assertAreClose ( scale . value . x , 10 , SCALE_DELTA ) ;
202+ TKUnit . assertAreClose ( scale . value . y , 20 , SCALE_DELTA ) ;
133203}
134204
135205export function test_ReadTranslate ( ) {
136- let animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: translateX(5),translateY(10); } }" , "test" ) ;
137- let translate = animation . keyframes [ 0 ] . declarations [ 0 ] . value ;
138- TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . property , "translate" ) ;
139- TKUnit . assert ( translate . x === 5 && translate . y === 10 ) ;
140- animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: translate(-5, 12.3pt); } }" , "test" ) ;
141- translate = animation . keyframes [ 0 ] . declarations [ 0 ] . value ;
142- TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . property , "translate" ) ;
143- TKUnit . assert ( translate . x === - 5 && translate . y === 12.3 ) ;
144- animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: translateX(10); } }" , "test" ) ;
145- translate = animation . keyframes [ 0 ] . declarations [ 0 ] . value ;
146- TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . property , "translate" ) ;
147- TKUnit . assert ( translate . x === 10 && translate . y === 0 ) ;
148- animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: translate3d(10, 20, 30); } }" , "test" ) ;
149- translate = animation . keyframes [ 0 ] . declarations [ 0 ] . value ;
150- TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . property , "translate" ) ;
151- TKUnit . assert ( translate . x === 10 && translate . y === 20 ) ;
206+ const animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: translate(100, 20); } }" , "test" ) ;
207+ const { translate } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
208+
209+ TKUnit . assertEqual ( translate . property , "translate" ) ;
210+ TKUnit . assertAreClose ( translate . value . x , 100 , DELTA ) ;
211+ TKUnit . assertAreClose ( translate . value . y , 20 , DELTA ) ;
212+ }
213+
214+ export function test_ReadTranslateSingle ( ) {
215+ const animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: translate(30); } }" , "test" ) ;
216+ const { translate, rotate } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
217+
218+ TKUnit . assertEqual ( translate . property , "translate" ) ;
219+ TKUnit . assertAreClose ( translate . value . x , 30 , DELTA ) ;
220+ TKUnit . assertAreClose ( translate . value . y , 30 , DELTA ) ;
221+ }
222+
223+ export function test_ReadTranslateXY ( ) {
224+ const css = ".test { animation-name: test; } @keyframes test { to { transform: translateX(5) translateY(10); } }" ;
225+ const animation = createAnimationFromCSS ( css , "test" ) ;
226+ const { translate } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
227+
228+ TKUnit . assertEqual ( translate . property , "translate" ) ;
229+ TKUnit . assertAreClose ( translate . value . x , 5 , DELTA ) ;
230+ TKUnit . assertAreClose ( translate . value . y , 10 , DELTA ) ;
231+ }
232+
233+ export function test_ReadTranslateX ( ) {
234+ const css = ".test { animation-name: test; } @keyframes test { to { transform: translateX(12.5); } }" ;
235+ const animation = createAnimationFromCSS ( css , "test" ) ;
236+ const { translate } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
237+
238+ TKUnit . assertEqual ( translate . property , "translate" ) ;
239+ TKUnit . assertAreClose ( translate . value . x , 12.5 , DELTA ) ;
240+ // y defaults to 0
241+ TKUnit . assertAreClose ( translate . value . y , 0 , DELTA ) ;
242+ }
243+
244+ export function test_ReadTranslateY ( ) {
245+ const css = ".test { animation-name: test; } @keyframes test { to { transform: translateY(10); } }" ;
246+ const animation = createAnimationFromCSS ( css , "test" ) ;
247+ const { translate } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
248+
249+ TKUnit . assertEqual ( translate . property , "translate" ) ;
250+ TKUnit . assertAreClose ( translate . value . y , 10 , DELTA ) ;
251+ // x defaults to 0
252+ TKUnit . assertAreClose ( translate . value . x , 0 , DELTA ) ;
253+ }
254+
255+ export function test_ReadTranslate3d ( ) {
256+ const css = ".test { animation-name: test; } @keyframes test { to { transform: translate3d(10, 20, 30); } }" ;
257+ const animation = createAnimationFromCSS ( css , "test" ) ;
258+ const { translate } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
259+
260+ TKUnit . assertEqual ( translate . property , "translate" ) ;
261+ TKUnit . assertAreClose ( translate . value . x , 10 , DELTA ) ;
262+ TKUnit . assertAreClose ( translate . value . y , 20 , DELTA ) ;
152263}
153264
154265export function test_ReadRotate ( ) {
155- let animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: rotate(5); } }" , "test" ) ;
156- TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . property , "rotate" ) ;
157- TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . value , 5 ) ;
158- animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: rotate(45deg); } }" , "test" ) ;
159- TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . property , "rotate" ) ;
160- TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . value , 45 ) ;
161- animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: rotate(0.7853981634rad); } }" , "test" ) ;
162- TKUnit . assertEqual ( animation . keyframes [ 0 ] . declarations [ 0 ] . property , "rotate" ) ;
163- TKUnit . assertTrue ( animation . keyframes [ 0 ] . declarations [ 0 ] . value - 45 < 0.1 ) ;
164- }
165-
166- export function test_ReadTransform ( ) {
167- let css = ".test { animation-name: test; } @keyframes test { to { transform: rotate(10),scaleX(5),translate(2,4); } }" ;
168- let animation = createAnimationFromCSS ( css , "test" ) ;
169- let rotate = animation . keyframes [ 0 ] . declarations [ 0 ] . value ;
170- let scale = animation . keyframes [ 0 ] . declarations [ 1 ] . value ;
171- let translate = animation . keyframes [ 0 ] . declarations [ 2 ] . value ;
172- TKUnit . assertEqual ( rotate , 10 ) ;
173- TKUnit . assert ( scale . x === 5 && scale . y === 1 ) ;
174- TKUnit . assert ( translate . x === 2 && translate . y === 4 ) ;
175- animation = createAnimationFromCSS ( ".test { animation-name: test; } @keyframes test { to { transform: none; } }" , "test" ) ;
176- rotate = animation . keyframes [ 0 ] . declarations [ 0 ] . value ;
177- scale = animation . keyframes [ 0 ] . declarations [ 1 ] . value ;
178- translate = animation . keyframes [ 0 ] . declarations [ 2 ] . value ;
179- TKUnit . assertEqual ( rotate , 0 ) ;
180- TKUnit . assert ( scale . x === 1 && scale . y === 1 ) ;
181- TKUnit . assert ( translate . x === 0 && translate . y === 0 ) ;
266+ const css = ".test { animation-name: test; } @keyframes test { to { transform: rotate(5); } }" ;
267+ const animation = createAnimationFromCSS ( css , "test" ) ;
268+ const { rotate } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
269+
270+ TKUnit . assertEqual ( rotate . property , "rotate" ) ;
271+ TKUnit . assertAreClose ( rotate . value , 5 , DELTA ) ;
272+ }
273+
274+ export function test_ReadRotateDeg ( ) {
275+ const css = ".test { animation-name: test; } @keyframes test { to { transform: rotate(45deg); } }" ;
276+ const animation = createAnimationFromCSS ( css , "test" ) ;
277+ const { rotate } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
278+
279+ TKUnit . assertEqual ( rotate . property , "rotate" ) ;
280+ TKUnit . assertAreClose ( rotate . value , 45 , DELTA ) ;
281+ }
282+
283+ export function test_ReadRotateRad ( ) {
284+ const css = ".test { animation-name: test; } @keyframes test { to { transform: rotate(0.7853981634rad); } }" ;
285+ const animation = createAnimationFromCSS ( css , "test" ) ;
286+ const { rotate } = getTransforms ( animation . keyframes [ 0 ] . declarations ) ;
287+
288+ TKUnit . assertEqual ( rotate . property , "rotate" ) ;
289+ TKUnit . assertAreClose ( rotate . value , 45 , DELTA ) ;
182290}
183291
184292export function test_ReadAnimationWithUnsortedKeyframes ( ) {
@@ -311,3 +419,15 @@ export function test_AnimationCurveInKeyframes() {
311419 TKUnit . assertEqual ( realAnimation . animations [ 1 ] . curve , enums . AnimationCurve . linear ) ;
312420 TKUnit . assertEqual ( realAnimation . animations [ 2 ] . curve , enums . AnimationCurve . easeIn ) ;
313421}
422+
423+ function getTransformsValues ( declarations ) {
424+ return Object . assign ( { } ,
425+ ...( < any > Object ) . entries ( getTransforms ( declarations ) )
426+ . map ( ( [ k , v ] ) => ( { [ k ] : v . value } ) )
427+ ) ;
428+ }
429+
430+ function getTransforms ( declarations ) {
431+ const [ translate , rotate , scale ] = [ ...declarations ] ;
432+ return { translate, rotate, scale } ;
433+ }
0 commit comments