1 
2 /*
3  * Copyright 2006 The Android Open Source Project
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 
10 #include "SkAnimatorScript.h"
11 #include "SkAnimateBase.h"
12 #include "SkAnimateMaker.h"
13 #include "SkDisplayTypes.h"
14 #include "SkExtras.h"
15 #include "SkMemberInfo.h"
16 #include "SkParse.h"
17 
18 static const SkDisplayEnumMap gEnumMaps[] = {
19     { SkType_AddMode, "indirect|immediate" },
20     { SkType_Align, "left|center|right" },
21     { SkType_ApplyMode, "create|immediate|once" },
22     { SkType_ApplyTransition, "normal|reverse" },
23     { SkType_BitmapEncoding, "jpeg|png" },
24     { SkType_BitmapFormat, "none|A1|A8|Index8|RGB16|RGB32" },
25     { SkType_Boolean, "false|true" },
26     { SkType_Cap, "butt|round|square" },
27     { SkType_EventCode, "none|leftSoftKey|rightSoftKey|home|back|send|end|key0|key1|key2|key3|key4|key5|key6|key7|key8|key9|star|hash|up|down|left|right|OK|volUp|volDown|camera" },
28     { SkType_EventKind, "none|keyChar|keyPress|keyPressUp|mouseDown|mouseDrag|mouseMove|mouseUp|onEnd|onLoad|user" },
29     { SkType_EventMode, "deferred|immediate" },
30     { SkType_FillType, "winding|evenOdd" },
31     { SkType_FilterType, "none|bilinear" },
32     { SkType_FontStyle, "normal|bold|italic|boldItalic" },
33     { SkType_FromPathMode, "normal|angle|position" },
34     { SkType_Join, "miter|round|blunt" },
35     { SkType_MaskFilterBlurStyle, "normal|solid|outer|inner" },
36     { SkType_PathDirection, "cw|ccw" },
37     { SkType_Style, "fill|stroke|strokeAndFill" },
38     { SkType_TextBoxAlign, "start|center|end" },
39     { SkType_TextBoxMode, "oneLine|lineBreak" },
40     { SkType_TileMode, "clamp|repeat|mirror" },
41     { SkType_Xfermode, "clear|src|dst|srcOver|dstOver|srcIn|dstIn|srcOut|dstOut|"
42         "srcATop|dstATop|xor|darken|lighten" },
43 };
44 
45 static int gEnumMapCount = SK_ARRAY_COUNT(gEnumMaps);
46 
SkAnimatorScript(SkAnimateMaker & maker,SkDisplayable * working,SkDisplayTypes type)47 SkAnimatorScript::SkAnimatorScript(SkAnimateMaker& maker, SkDisplayable* working, SkDisplayTypes type)
48     : SkScriptEngine(SkScriptEngine::ToOpType(type)), fMaker(maker), fParent(nullptr), fWorking(working)
49 {
50     memberCallBack(EvalMember, (void*) this);
51     memberFunctionCallBack(EvalMemberFunction, (void*) this);
52     boxCallBack(Box, (void*) this);
53     unboxCallBack(Unbox, (void*) &maker);
54     propertyCallBack(EvalID, (void*) this); // must be first (entries are prepended, will be last), since it never fails
55     propertyCallBack(Infinity, (void*) this);
56     propertyCallBack(NaN, (void*) this);
57     functionCallBack(Eval, (void*) this);
58     functionCallBack(IsFinite, (void*) this);
59     functionCallBack(IsNaN, (void*) this);
60     if (type == SkType_ARGB) {
61         functionCallBack(EvalRGB, (void*) this);
62         propertyCallBack(EvalNamedColor, (void*) &maker.fIDs);
63     }
64     if (SkDisplayType::IsEnum(&maker, type)) {
65         // !!! for SpiderMonkey, iterate through the enum values, and map them to globals
66         const SkDisplayEnumMap& map = GetEnumValues(type);
67         propertyCallBack(EvalEnum, (void*) map.fValues);
68     }
69     for (SkExtras** extraPtr = maker.fExtras.begin(); extraPtr < maker.fExtras.end(); extraPtr++) {
70         SkExtras* extra = *extraPtr;
71         if (extra->fExtraCallBack)
72             propertyCallBack(extra->fExtraCallBack, extra->fExtraStorage);
73     }
74 }
75 
~SkAnimatorScript()76 SkAnimatorScript::~SkAnimatorScript() {
77     for (SkDisplayable** dispPtr = fTrackDisplayable.begin(); dispPtr < fTrackDisplayable.end(); dispPtr++)
78         delete *dispPtr;
79 }
80 
evaluate(const char * original,SkScriptValue * result,SkDisplayTypes type)81 bool SkAnimatorScript::evaluate(const char* original, SkScriptValue* result, SkDisplayTypes type) {
82         const char* script = original;
83         bool success = evaluateScript(&script, result);
84         if (success == false || result->fType != type) {
85             fMaker.setScriptError(*this);
86             return false;
87         }
88         return true;
89 }
90 
Box(void * user,SkScriptValue * scriptValue)91 bool SkAnimatorScript::Box(void* user, SkScriptValue* scriptValue) {
92     SkAnimatorScript* engine = (SkAnimatorScript*) user;
93     SkDisplayTypes type = scriptValue->fType;
94     SkDisplayable* displayable;
95     switch (type) {
96         case SkType_Array: {
97             SkDisplayArray* boxedValue = new SkDisplayArray(*scriptValue->fOperand.fArray);
98             displayable = boxedValue;
99             } break;
100         case SkType_Boolean: {
101             SkDisplayBoolean* boxedValue = new SkDisplayBoolean;
102             displayable = boxedValue;
103             boxedValue->value = !! scriptValue->fOperand.fS32;
104             } break;
105         case SkType_Int: {
106             SkDisplayInt* boxedValue = new SkDisplayInt;
107             displayable = boxedValue;
108             boxedValue->value = scriptValue->fOperand.fS32;
109             } break;
110         case SkType_Float: {
111             SkDisplayFloat* boxedValue = new SkDisplayFloat;
112             displayable = boxedValue;
113             boxedValue->value = scriptValue->fOperand.fScalar;
114             } break;
115         case SkType_String: {
116             SkDisplayString* boxedValue = new SkDisplayString(*scriptValue->fOperand.fString);
117             displayable = boxedValue;
118             } break;
119         case SkType_Displayable:
120             scriptValue->fOperand.fObject = scriptValue->fOperand.fDisplayable;
121             scriptValue->fType = SkType_Displayable;
122             return true;
123         default:
124             SkASSERT(0);
125             return false;
126     }
127     engine->track(displayable);
128     scriptValue->fOperand.fObject = displayable;
129     scriptValue->fType = SkType_Displayable;
130     return true;
131 }
132 
Eval(const char * function,size_t len,SkTDArray<SkScriptValue> & params,void * eng,SkScriptValue * value)133 bool SkAnimatorScript::Eval(const char* function, size_t len, SkTDArray<SkScriptValue>& params,
134         void* eng, SkScriptValue* value) {
135     if (SK_LITERAL_STR_EQUAL("eval", function, len) == false)
136         return false;
137     if (params.count() != 1)
138         return false;
139     SkAnimatorScript* host = (SkAnimatorScript*) eng;
140     SkAnimatorScript engine(host->fMaker, host->fWorking, SkScriptEngine::ToDisplayType(host->fReturnType));
141     SkScriptValue* scriptValue = params.begin();
142     bool success = true;
143     if (scriptValue->fType == SkType_String) {
144         const char* script = scriptValue->fOperand.fString->c_str();
145         success = engine.evaluateScript(&script, value);
146     } else
147         *value = *scriptValue;
148     return success;
149 }
150 
EvalEnum(const char * token,size_t len,void * callBack,SkScriptValue * value)151 bool SkAnimatorScript::EvalEnum(const char* token, size_t len, void* callBack, SkScriptValue* value) {
152     const char* tokens = (const char*) callBack;
153     value->fType = SkType_Int;
154     if (MapEnums(tokens, token, len, (int*)&value->fOperand.fS32))
155         return true;
156     return false;
157 }
158 
EvalID(const char * token,size_t len,void * user,SkScriptValue * value)159 bool SkAnimatorScript::EvalID(const char* token, size_t len, void* user, SkScriptValue* value) {
160     SkAnimatorScript* engine = (SkAnimatorScript*) user;
161     SkTDict<SkDisplayable*>* ids = &engine->fMaker.fIDs;
162     SkDisplayable* displayable;
163     bool success = ids->find(token, len, &displayable);
164     if (success == false) {
165         displayable = engine->fWorking;
166         if (SK_LITERAL_STR_EQUAL("parent", token, len)) {
167             SkDisplayable* parent = displayable->getParent();
168             if (parent == nullptr)
169                 parent = engine->fParent;
170             if (parent) {
171                 value->fOperand.fDisplayable = parent;
172                 value->fType = SkType_Displayable;
173                 return true;
174             }
175         }
176         if (displayable && EvalMember(token, len, displayable, engine, value))
177             return true;
178         value->fOperand.fString = nullptr;
179         value->fType = SkType_String;
180     } else {
181         SkDisplayable* working = engine->fWorking;
182         value->fOperand.fDisplayable = displayable;
183         value->fType = SkType_Displayable;
184         if (displayable->canContainDependents() && working && working->isAnimate()) {
185             SkAnimateBase* animator = (SkAnimateBase*) working;
186             if (animator->isDynamic()) {
187                 SkDisplayDepend* depend = (SkDisplayDepend* ) displayable;
188                 depend->addDependent(working);
189             }
190         }
191     }
192     return true;
193 }
194 
EvalNamedColor(const char * token,size_t len,void * callback,SkScriptValue * value)195 bool SkAnimatorScript::EvalNamedColor(const char* token, size_t len, void* callback, SkScriptValue* value) {
196         value->fType = SkType_Int;
197     if (SkParse::FindNamedColor(token, len, (SkColor*) &value->fOperand.fS32) != nullptr)
198         return true;
199     return false;
200 }
201 
EvalRGB(const char * function,size_t len,SkTDArray<SkScriptValue> & params,void * eng,SkScriptValue * value)202 bool SkAnimatorScript::EvalRGB(const char* function, size_t len, SkTDArray<SkScriptValue>& params,
203         void* eng, SkScriptValue* value) {
204     if (SK_LITERAL_STR_EQUAL("rgb", function, len) == false)
205         return false;
206     if (params.count() != 3)
207         return false;
208     SkScriptEngine* engine = (SkScriptEngine*) eng;
209     unsigned result = 0xFF000000;
210     int shift = 16;
211     for (SkScriptValue* valuePtr = params.begin(); valuePtr < params.end(); valuePtr++) {
212         engine->convertTo(SkType_Int, valuePtr);
213         result |= SkClampMax(valuePtr->fOperand.fS32, 255) << shift;
214         shift -= 8;
215     }
216     value->fOperand.fS32 = result;
217     value->fType = SkType_Int;
218     return true;
219 }
220 
EvalMemberCommon(SkScriptEngine * engine,const SkMemberInfo * info,SkDisplayable * displayable,SkScriptValue * value)221 bool SkAnimatorScript::EvalMemberCommon(SkScriptEngine* engine, const SkMemberInfo* info,
222         SkDisplayable* displayable, SkScriptValue* value) {
223     SkDisplayTypes original;
224     SkDisplayTypes type = original = (SkDisplayTypes) info->getType();
225     if (info->fType == SkType_Array)
226         type = SkType_Array;
227     switch (type) {
228         case SkType_ARGB:
229             type = SkType_Int;
230         case SkType_Boolean:
231         case SkType_Int:
232         case SkType_MSec:
233         case SkType_Float:
234             SkASSERT(info->getCount() == 1);
235             if (info->fType != SkType_MemberProperty && info->fType != SkType_MemberFunction)
236                 value->fOperand.fS32 = *(int32_t*) info->memberData(displayable);   // OK for SkScalar too
237             if (type == SkType_MSec) {
238                 value->fOperand.fScalar = value->fOperand.fS32 * 0.001f;
239                 type = SkType_Float;
240             }
241             break;
242         case SkType_String: {
243             SkString* displayableString;
244             if (info->fType != SkType_MemberProperty && info->fType != SkType_MemberFunction) {
245                 info->getString(displayable, &displayableString);
246                 value->fOperand.fString = new SkString(*displayableString);
247             }
248             } break;
249         case SkType_Array: {
250             SkASSERT(info->fType != SkType_MemberProperty); // !!! incomplete
251             SkTDOperandArray* displayableArray = (SkTDOperandArray*) info->memberData(displayable);
252             if (displayable->getType() == SkType_Array) {
253                 SkDisplayArray* typedArray = (SkDisplayArray*) displayable;
254                 original = typedArray->values.getType();
255             }
256             SkASSERT(original != SkType_Unknown);
257             SkTypedArray* array = value->fOperand.fArray = new SkTypedArray(original);
258             engine->track(array);
259             int count = displayableArray->count();
260             if (count > 0) {
261                 array->setCount(count);
262                 memcpy(array->begin(), displayableArray->begin(), count * sizeof(SkOperand));
263             }
264             } break;
265         default:
266             SkASSERT(0); // unimplemented
267     }
268     value->fType = type;
269     return true;
270 }
271 
EvalMember(const char * member,size_t len,void * object,void * eng,SkScriptValue * value)272 bool SkAnimatorScript::EvalMember(const char* member, size_t len, void* object, void* eng,
273         SkScriptValue* value) {
274     SkScriptEngine* engine = (SkScriptEngine*) eng;
275     SkDisplayable* displayable = (SkDisplayable*) object;
276     SkString name(member, len);
277     SkDisplayable* named = displayable->contains(name);
278     if (named) {
279         value->fOperand.fDisplayable = named;
280         value->fType = SkType_Displayable;
281         return true;
282     }
283     const SkMemberInfo* info = displayable->getMember(name.c_str());
284     if (info == nullptr)
285         return false;
286     if (info->fType == SkType_MemberProperty) {
287         if (displayable->getProperty(info->propertyIndex(), value) == false) {
288             SkASSERT(0);
289             return false;
290         }
291     }
292     return EvalMemberCommon(engine, info, displayable, value);
293 }
294 
EvalMemberFunction(const char * member,size_t len,void * object,SkTDArray<SkScriptValue> & params,void * eng,SkScriptValue * value)295 bool SkAnimatorScript::EvalMemberFunction(const char* member, size_t len, void* object,
296         SkTDArray<SkScriptValue>& params, void* eng, SkScriptValue* value) {
297     SkScriptEngine* engine = (SkScriptEngine*) eng;
298     SkDisplayable* displayable = (SkDisplayable*) object;
299     SkString name(member, len);
300     const SkMemberInfo* info = displayable->getMember(name.c_str());
301     SkASSERT(info != nullptr); /* !!! error handling unimplemented */
302     if (info->fType != SkType_MemberFunction) {
303         SkASSERT(0);
304         return false;
305     }
306     displayable->executeFunction(displayable, info->functionIndex(), params, info->getType(),
307         value);
308     return EvalMemberCommon(engine, info, displayable, value);
309 }
310 
EvaluateDisplayable(SkAnimateMaker & maker,SkDisplayable * displayable,const char * script,SkDisplayable ** result)311 bool SkAnimatorScript::EvaluateDisplayable(SkAnimateMaker& maker, SkDisplayable* displayable, const char* script, SkDisplayable** result) {
312     SkAnimatorScript engine(maker, displayable, SkType_Displayable);
313     SkScriptValue value;
314     bool success = engine.evaluate(script, &value, SkType_Displayable);
315     if (success)
316         *result = value.fOperand.fDisplayable;
317     return success;
318 }
319 
EvaluateInt(SkAnimateMaker & maker,SkDisplayable * displayable,const char * script,int32_t * result)320 bool SkAnimatorScript::EvaluateInt(SkAnimateMaker& maker, SkDisplayable* displayable, const char* script, int32_t* result) {
321     SkAnimatorScript engine(maker, displayable, SkType_Int);
322     SkScriptValue value;
323     bool success = engine.evaluate(script, &value, SkType_Int);
324     if (success)
325         *result = value.fOperand.fS32;
326     return success;
327 }
328 
EvaluateFloat(SkAnimateMaker & maker,SkDisplayable * displayable,const char * script,SkScalar * result)329 bool SkAnimatorScript::EvaluateFloat(SkAnimateMaker& maker, SkDisplayable* displayable, const char* script, SkScalar* result) {
330     SkAnimatorScript engine(maker, displayable, SkType_Float);
331     SkScriptValue value;
332     bool success = engine.evaluate(script, &value, SkType_Float);
333     if (success)
334         *result = value.fOperand.fScalar;
335     return success;
336 }
337 
EvaluateString(SkAnimateMaker & maker,SkDisplayable * displayable,const char * script,SkString * result)338 bool SkAnimatorScript::EvaluateString(SkAnimateMaker& maker, SkDisplayable* displayable, const char* script, SkString* result) {
339     SkAnimatorScript engine(maker, displayable, SkType_String);
340     SkScriptValue value;
341     bool success = engine.evaluate(script, &value, SkType_String);
342     if (success)
343         result->set(*(value.fOperand.fString));
344   return success;
345 }
346 
EvaluateString(SkAnimateMaker & maker,SkDisplayable * displayable,SkDisplayable * parent,const char * script,SkString * result)347 bool SkAnimatorScript::EvaluateString(SkAnimateMaker& maker, SkDisplayable* displayable, SkDisplayable* parent, const char* script, SkString* result) {
348     SkAnimatorScript engine(maker, displayable, SkType_String);
349     engine.fParent = parent;
350     SkScriptValue value;
351     bool success = engine.evaluate(script, &value, SkType_String);
352     if (success)
353         result->set(*(value.fOperand.fString));
354   return success;
355 }
356 
GetEnumValues(SkDisplayTypes type)357 const SkDisplayEnumMap& SkAnimatorScript::GetEnumValues(SkDisplayTypes type) {
358     int index = SkTSearch<SkDisplayTypes>(&gEnumMaps[0].fType, gEnumMapCount, type,
359         sizeof(SkDisplayEnumMap));
360     SkASSERT(index >= 0);
361     return gEnumMaps[index];
362 }
363 
Infinity(const char * token,size_t len,void * user,SkScriptValue * value)364 bool SkAnimatorScript::Infinity(const char* token, size_t len, void* user, SkScriptValue* value) {
365     if (SK_LITERAL_STR_EQUAL("Infinity", token, len) == false)
366         return false;
367     value->fType = SkType_Float;
368     value->fOperand.fScalar = SK_ScalarInfinity;
369     return true;
370 }
371 
IsFinite(const char * function,size_t len,SkTDArray<SkScriptValue> & params,void * eng,SkScriptValue * value)372 bool SkAnimatorScript::IsFinite(const char* function, size_t len, SkTDArray<SkScriptValue>& params,
373         void* eng, SkScriptValue* value) {
374     if (SK_LITERAL_STR_EQUAL(function, "isFinite", len) == false)
375         return false;
376     if (params.count() != 1)
377         return false;
378     SkScriptValue* scriptValue = params.begin();
379     SkDisplayTypes type = scriptValue->fType;
380     SkScalar scalar = scriptValue->fOperand.fScalar;
381     value->fType = SkType_Int;
382     value->fOperand.fS32 = type == SkType_Float ? SkScalarIsNaN(scalar) == false &&
383         SkScalarAbs(scalar) != SK_ScalarInfinity    : type == SkType_Int;
384     return true;
385 }
386 
IsNaN(const char * function,size_t len,SkTDArray<SkScriptValue> & params,void * eng,SkScriptValue * value)387 bool SkAnimatorScript::IsNaN(const char* function, size_t len, SkTDArray<SkScriptValue>& params,
388         void* eng, SkScriptValue* value) {
389     if (SK_LITERAL_STR_EQUAL("isNaN", function, len) == false)
390         return false;
391     if (params.count() != 1)
392         return false;
393     SkScriptValue* scriptValue = params.begin();
394     value->fType = SkType_Int;
395     value->fOperand.fS32 = scriptValue->fType == SkType_Float ? SkScalarIsNaN(scriptValue->fOperand.fScalar) : 0;
396     return true;
397 }
398 
MapEnums(const char * ptr,const char * match,size_t len,int * value)399 bool SkAnimatorScript::MapEnums(const char* ptr, const char* match, size_t len, int* value) {
400     int index = 0;
401     bool more = true;
402     do {
403         const char* last = strchr(ptr, '|');
404         if (last == nullptr) {
405             last = &ptr[strlen(ptr)];
406             more = false;
407         }
408         size_t length = last - ptr;
409         if (len == length && strncmp(ptr, match, length) == 0) {
410             *value = index;
411             return true;
412         }
413         index++;
414         ptr = last + 1;
415     } while (more);
416     return false;
417 }
418 
NaN(const char * token,size_t len,void * user,SkScriptValue * value)419 bool SkAnimatorScript::NaN(const char* token, size_t len, void* user, SkScriptValue* value) {
420     if (SK_LITERAL_STR_EQUAL("NaN", token, len) == false)
421         return false;
422     value->fType = SkType_Float;
423     value->fOperand.fScalar = SK_ScalarNaN;
424     return true;
425 }
426 
427 #if 0
428 bool SkAnimatorScript::ObjectToString(void* object, void* user, SkScriptValue* value) {
429     SkTDict<SkDisplayable*>* ids = (SkTDict<SkDisplayable*>*) user;
430     SkDisplayable* displayable = (SkDisplayable*) object;
431     const char* key;
432     bool success = ids->findKey(displayable, &key);
433     if (success == false)
434         return false;
435     value->fOperand.fString =   new SkString(key);
436     value->fType = SkType_String;
437     return true;
438 }
439 #endif
440 
Unbox(void * m,SkScriptValue * scriptValue)441 bool SkAnimatorScript::Unbox(void* m, SkScriptValue* scriptValue) {
442     SkAnimateMaker* maker = (SkAnimateMaker*) m;
443     SkASSERT((unsigned) scriptValue->fType == (unsigned) SkType_Displayable);
444     SkDisplayable* displayable = (SkDisplayable*) scriptValue->fOperand.fObject;
445     SkDisplayTypes type = displayable->getType();
446     switch (displayable->getType()) {
447         case SkType_Array: {
448             SkDisplayArray* boxedValue = (SkDisplayArray*) displayable;
449             scriptValue->fOperand.fArray = &boxedValue->values;
450             } break;
451         case SkType_Boolean: {
452             SkDisplayBoolean* boxedValue = (SkDisplayBoolean*) displayable;
453             scriptValue->fOperand.fS32 = boxedValue->value;
454             } break;
455         case SkType_Int: {
456             SkDisplayInt* boxedValue = (SkDisplayInt*) displayable;
457             scriptValue->fOperand.fS32 = boxedValue->value;
458             } break;
459         case SkType_Float: {
460             SkDisplayFloat* boxedValue = (SkDisplayFloat*) displayable;
461             scriptValue->fOperand.fScalar = boxedValue->value;
462             } break;
463         case SkType_String: {
464             SkDisplayString* boxedValue = (SkDisplayString*) displayable;
465             scriptValue->fOperand.fString = new SkString(boxedValue->value);
466             } break;
467         default: {
468             const char* id = nullptr;
469             SkDEBUGCODE(bool success = ) maker->findKey(displayable, &id);
470             SkASSERT(success);
471             scriptValue->fOperand.fString = new SkString(id);
472             type = SkType_String;
473         }
474     }
475     scriptValue->fType = type;
476     return true;
477 }
478 
479 #if defined SK_SUPPORT_UNITTEST
480 
481 #include "SkAnimator.h"
482 
483 static const char scriptTestSetup[]  =
484 "<screenplay>\n"
485     "<text id='label' text='defg'/>\n"
486     "<add id='addLabel' use='label'/>\n"
487     "<text id='text1' text='test'/>\n"
488     "<apply scope='addLabel'>\n"
489         "<set target='label' field='text' to='#script:text1.text'/>\n"
490     "</apply>\n"
491     "<apply>\n"
492         "<paint id='labelPaint'>\n"
493             "<emboss id='emboss' direction='[1,1,1]'  />\n"
494         "</paint>\n"
495         "<animate id='animation' field='direction' target='emboss' from='[1,1,1]' to='[-1,1,1]' dur='1'/>\n"
496         "<set lval='direction[0]' target='emboss' to='-1' />\n"
497     "</apply>\n"
498     "<color id='testColor' color='0 ? rgb(0,0,0) : rgb(255,255,255)' />\n"
499     "<color id='xColor' color='rgb(12,34,56)' />\n"
500     "<array id='emptyArray' />\n"
501     "<array id='intArray' values='[1, 4, 6]' />\n"
502     "<int id='idx' value='2' />\n"
503     "<int id='idy' value='2' />\n"
504     "<string id='alpha' value='abc' />\n"
505     "<rect id='testRect' left='Math.cos(0)' top='2' right='12' bottom='5' />\n"
506     "<event id='evt'>\n"
507         "<input name='x' />\n"
508         "<apply scope='idy'>\n"
509             "<set field='value' to='evt.x.int' />\n"
510         "</apply>\n"
511     "</event>\n"
512 "</screenplay>";
513 
514 #define DEFAULT_ANSWER   , 0
515 
516 static const SkScriptNAnswer scriptTests[]  = {
517     { "label.text.length == 4", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
518 //  { "labelPaint.measureText(label.text) > 0 ? labelPaint.measureText(label.text)+10 : 40", SkType_Float, 0, SkIntToScalar(0x23)  },
519     {   "Number.POSITIVE_INFINITY >= Number.MAX_VALUE ? 1 : 0", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
520     {   "Infinity >= Number.MAX_VALUE ? 1 : 0", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
521     {   "Number.NEGATIVE_INFINITY <= -Number.MAX_VALUE ? 1 : 0", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
522     {   "Number.MIN_VALUE > 0 ? 1 : 0", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
523     {   "isNaN(Number.NaN)", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
524     {   "isNaN(NaN)", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
525     {   "Math.sin(0)", SkType_Float, 0, SkIntToScalar(0) DEFAULT_ANSWER },
526     {   "alpha+alpha", SkType_String, 0, 0, "abcabc" },
527     {   "intArray[4]", SkType_Unknown DEFAULT_ANSWER DEFAULT_ANSWER DEFAULT_ANSWER },
528     {   "emptyArray[4]", SkType_Unknown DEFAULT_ANSWER DEFAULT_ANSWER DEFAULT_ANSWER },
529     {   "idx", SkType_Int, 2 DEFAULT_ANSWER DEFAULT_ANSWER },
530     {   "intArray.length", SkType_Int, 3 DEFAULT_ANSWER DEFAULT_ANSWER },
531     {   "intArray.values[0]", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
532     {   "intArray[0]", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
533     {   "idx.value", SkType_Int, 2 DEFAULT_ANSWER DEFAULT_ANSWER },
534     {   "alpha.value", SkType_String, 0, 0, "abc" },
535     {   "alpha", SkType_String, 0, 0, "abc" },
536     {   "alpha.value+alpha.value", SkType_String, 0, 0, "abcabc" },
537     {   "alpha+idx", SkType_String, 0, 0, "abc2" },
538     {   "idx+alpha", SkType_String, 0, 0, "2abc" },
539     {   "intArray[idx]", SkType_Int, 6 DEFAULT_ANSWER DEFAULT_ANSWER },
540     {   "alpha.slice(1,2)", SkType_String, 0, 0, "b" },
541     {   "alpha.value.slice(1,2)", SkType_String, 0, 0, "b" },
542     {   "testRect.left+2", SkType_Float, 0, SkIntToScalar(3) DEFAULT_ANSWER },
543     {   "0 ? Math.sin(0) : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
544     {   "0 ? intArray[0] : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
545     {   "0 ? intArray.values[0] : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
546     {   "0 ? idx : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
547     {   "0 ? idx.value : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
548     {   "0 ? alpha.slice(1,2) : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
549     {   "0 ? alpha.value.slice(1,2) : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
550     { "idy", SkType_Int, 3 DEFAULT_ANSWER DEFAULT_ANSWER }
551 };
552 
553 #define SkScriptNAnswer_testCount   SK_ARRAY_COUNT(scriptTests)
554 
UnitTest()555 void SkAnimatorScript::UnitTest() {
556 #if defined(SK_SUPPORT_UNITTEST)
557     SkAnimator animator;
558     SkASSERT(animator.decodeMemory(scriptTestSetup, sizeof(scriptTestSetup)-1));
559     SkEvent evt;
560     evt.setString("id", "evt");
561     evt.setS32("x", 3);
562     animator.doUserEvent(evt);
563     // set up animator with memory script above, then run value tests
564     for (unsigned index = 0; index < SkScriptNAnswer_testCount; index++) {
565         SkAnimatorScript engine(*animator.fMaker, nullptr, scriptTests[index].fType);
566         SkScriptValue value;
567         const char* script = scriptTests[index].fScript;
568         bool success = engine.evaluateScript(&script, &value);
569         if (success == false) {
570             SkDebugf("script failed: %s\n", scriptTests[index].fScript);
571             SkASSERT(scriptTests[index].fType == SkType_Unknown);
572             continue;
573         }
574         SkASSERT(value.fType == scriptTests[index].fType);
575         SkScalar error;
576         switch (value.fType) {
577             case SkType_Int:
578                 SkASSERT(value.fOperand.fS32 == scriptTests[index].fIntAnswer);
579                 break;
580             case SkType_Float:
581                 error = SkScalarAbs(value.fOperand.fScalar - scriptTests[index].fScalarAnswer);
582                 SkASSERT(error < SK_Scalar1 / 10000);
583                 break;
584             case SkType_String:
585                 SkASSERT(strcmp(value.fOperand.fString->c_str(), scriptTests[index].fStringAnswer) == 0);
586                 break;
587             default:
588                 SkASSERT(0);
589         }
590     }
591 #endif
592 }
593 
594 #endif
595