michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkAnimatorScript.h" michael@0: #include "SkAnimateBase.h" michael@0: #include "SkAnimateMaker.h" michael@0: #include "SkDisplayTypes.h" michael@0: #include "SkExtras.h" michael@0: #include "SkMemberInfo.h" michael@0: #include "SkParse.h" michael@0: michael@0: static const SkDisplayEnumMap gEnumMaps[] = { michael@0: { SkType_AddMode, "indirect|immediate" }, michael@0: { SkType_Align, "left|center|right" }, michael@0: { SkType_ApplyMode, "create|immediate|once" }, michael@0: { SkType_ApplyTransition, "normal|reverse" }, michael@0: { SkType_BitmapEncoding, "jpeg|png" }, michael@0: { SkType_BitmapFormat, "none|A1|A8|Index8|RGB16|RGB32" }, michael@0: { SkType_Boolean, "false|true" }, michael@0: { SkType_Cap, "butt|round|square" }, michael@0: { 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" }, michael@0: { SkType_EventKind, "none|keyChar|keyPress|keyPressUp|mouseDown|mouseDrag|mouseMove|mouseUp|onEnd|onLoad|user" }, michael@0: { SkType_EventMode, "deferred|immediate" }, michael@0: { SkType_FillType, "winding|evenOdd" }, michael@0: { SkType_FilterType, "none|bilinear" }, michael@0: { SkType_FontStyle, "normal|bold|italic|boldItalic" }, michael@0: { SkType_FromPathMode, "normal|angle|position" }, michael@0: { SkType_Join, "miter|round|blunt" }, michael@0: { SkType_MaskFilterBlurStyle, "normal|solid|outer|inner" }, michael@0: { SkType_PathDirection, "cw|ccw" }, michael@0: { SkType_Style, "fill|stroke|strokeAndFill" }, michael@0: { SkType_TextBoxAlign, "start|center|end" }, michael@0: { SkType_TextBoxMode, "oneLine|lineBreak" }, michael@0: { SkType_TileMode, "clamp|repeat|mirror" }, michael@0: { SkType_Xfermode, "clear|src|dst|srcOver|dstOver|srcIn|dstIn|srcOut|dstOut|" michael@0: "srcATop|dstATop|xor|darken|lighten" }, michael@0: }; michael@0: michael@0: static int gEnumMapCount = SK_ARRAY_COUNT(gEnumMaps); michael@0: michael@0: SkAnimatorScript::SkAnimatorScript(SkAnimateMaker& maker, SkDisplayable* working, SkDisplayTypes type) michael@0: : SkScriptEngine(SkScriptEngine::ToOpType(type)), fMaker(maker), fParent(NULL), fWorking(working) michael@0: { michael@0: memberCallBack(EvalMember, (void*) this); michael@0: memberFunctionCallBack(EvalMemberFunction, (void*) this); michael@0: boxCallBack(Box, (void*) this); michael@0: unboxCallBack(Unbox, (void*) &maker); michael@0: propertyCallBack(EvalID, (void*) this); // must be first (entries are prepended, will be last), since it never fails michael@0: propertyCallBack(Infinity, (void*) this); michael@0: propertyCallBack(NaN, (void*) this); michael@0: functionCallBack(Eval, (void*) this); michael@0: functionCallBack(IsFinite, (void*) this); michael@0: functionCallBack(IsNaN, (void*) this); michael@0: if (type == SkType_ARGB) { michael@0: functionCallBack(EvalRGB, (void*) this); michael@0: propertyCallBack(EvalNamedColor, (void*) &maker.fIDs); michael@0: } michael@0: if (SkDisplayType::IsEnum(&maker, type)) { michael@0: // !!! for SpiderMonkey, iterate through the enum values, and map them to globals michael@0: const SkDisplayEnumMap& map = GetEnumValues(type); michael@0: propertyCallBack(EvalEnum, (void*) map.fValues); michael@0: } michael@0: for (SkExtras** extraPtr = maker.fExtras.begin(); extraPtr < maker.fExtras.end(); extraPtr++) { michael@0: SkExtras* extra = *extraPtr; michael@0: if (extra->fExtraCallBack) michael@0: propertyCallBack(extra->fExtraCallBack, extra->fExtraStorage); michael@0: } michael@0: } michael@0: michael@0: SkAnimatorScript::~SkAnimatorScript() { michael@0: for (SkDisplayable** dispPtr = fTrackDisplayable.begin(); dispPtr < fTrackDisplayable.end(); dispPtr++) michael@0: delete *dispPtr; michael@0: } michael@0: michael@0: bool SkAnimatorScript::evaluate(const char* original, SkScriptValue* result, SkDisplayTypes type) { michael@0: const char* script = original; michael@0: bool success = evaluateScript(&script, result); michael@0: if (success == false || result->fType != type) { michael@0: fMaker.setScriptError(*this); michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SkAnimatorScript::Box(void* user, SkScriptValue* scriptValue) { michael@0: SkAnimatorScript* engine = (SkAnimatorScript*) user; michael@0: SkDisplayTypes type = scriptValue->fType; michael@0: SkDisplayable* displayable; michael@0: switch (type) { michael@0: case SkType_Array: { michael@0: SkDisplayArray* boxedValue = new SkDisplayArray(*scriptValue->fOperand.fArray); michael@0: displayable = boxedValue; michael@0: } break; michael@0: case SkType_Boolean: { michael@0: SkDisplayBoolean* boxedValue = new SkDisplayBoolean; michael@0: displayable = boxedValue; michael@0: boxedValue->value = !! scriptValue->fOperand.fS32; michael@0: } break; michael@0: case SkType_Int: { michael@0: SkDisplayInt* boxedValue = new SkDisplayInt; michael@0: displayable = boxedValue; michael@0: boxedValue->value = scriptValue->fOperand.fS32; michael@0: } break; michael@0: case SkType_Float: { michael@0: SkDisplayFloat* boxedValue = new SkDisplayFloat; michael@0: displayable = boxedValue; michael@0: boxedValue->value = scriptValue->fOperand.fScalar; michael@0: } break; michael@0: case SkType_String: { michael@0: SkDisplayString* boxedValue = new SkDisplayString(*scriptValue->fOperand.fString); michael@0: displayable = boxedValue; michael@0: } break; michael@0: case SkType_Displayable: michael@0: scriptValue->fOperand.fObject = scriptValue->fOperand.fDisplayable; michael@0: scriptValue->fType = SkType_Displayable; michael@0: return true; michael@0: default: michael@0: SkASSERT(0); michael@0: return false; michael@0: } michael@0: engine->track(displayable); michael@0: scriptValue->fOperand.fObject = displayable; michael@0: scriptValue->fType = SkType_Displayable; michael@0: return true; michael@0: } michael@0: michael@0: bool SkAnimatorScript::Eval(const char* function, size_t len, SkTDArray& params, michael@0: void* eng, SkScriptValue* value) { michael@0: if (SK_LITERAL_STR_EQUAL("eval", function, len) == false) michael@0: return false; michael@0: if (params.count() != 1) michael@0: return false; michael@0: SkAnimatorScript* host = (SkAnimatorScript*) eng; michael@0: SkAnimatorScript engine(host->fMaker, host->fWorking, SkScriptEngine::ToDisplayType(host->fReturnType)); michael@0: SkScriptValue* scriptValue = params.begin(); michael@0: bool success = true; michael@0: if (scriptValue->fType == SkType_String) { michael@0: const char* script = scriptValue->fOperand.fString->c_str(); michael@0: success = engine.evaluateScript(&script, value); michael@0: } else michael@0: *value = *scriptValue; michael@0: return success; michael@0: } michael@0: michael@0: bool SkAnimatorScript::EvalEnum(const char* token, size_t len, void* callBack, SkScriptValue* value) { michael@0: const char* tokens = (const char*) callBack; michael@0: value->fType = SkType_Int; michael@0: if (MapEnums(tokens, token, len, (int*)&value->fOperand.fS32)) michael@0: return true; michael@0: return false; michael@0: } michael@0: michael@0: bool SkAnimatorScript::EvalID(const char* token, size_t len, void* user, SkScriptValue* value) { michael@0: SkAnimatorScript* engine = (SkAnimatorScript*) user; michael@0: SkTDict* ids = &engine->fMaker.fIDs; michael@0: SkDisplayable* displayable; michael@0: bool success = ids->find(token, len, &displayable); michael@0: if (success == false) { michael@0: displayable = engine->fWorking; michael@0: if (SK_LITERAL_STR_EQUAL("parent", token, len)) { michael@0: SkDisplayable* parent = displayable->getParent(); michael@0: if (parent == NULL) michael@0: parent = engine->fParent; michael@0: if (parent) { michael@0: value->fOperand.fDisplayable = parent; michael@0: value->fType = SkType_Displayable; michael@0: return true; michael@0: } michael@0: } michael@0: if (displayable && EvalMember(token, len, displayable, engine, value)) michael@0: return true; michael@0: value->fOperand.fString = NULL; michael@0: value->fType = SkType_String; michael@0: } else { michael@0: SkDisplayable* working = engine->fWorking; michael@0: value->fOperand.fDisplayable = displayable; michael@0: value->fType = SkType_Displayable; michael@0: if (displayable->canContainDependents() && working && working->isAnimate()) { michael@0: SkAnimateBase* animator = (SkAnimateBase*) working; michael@0: if (animator->isDynamic()) { michael@0: SkDisplayDepend* depend = (SkDisplayDepend* ) displayable; michael@0: depend->addDependent(working); michael@0: } michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SkAnimatorScript::EvalNamedColor(const char* token, size_t len, void* callback, SkScriptValue* value) { michael@0: value->fType = SkType_Int; michael@0: if (SkParse::FindNamedColor(token, len, (SkColor*) &value->fOperand.fS32) != NULL) michael@0: return true; michael@0: return false; michael@0: } michael@0: michael@0: bool SkAnimatorScript::EvalRGB(const char* function, size_t len, SkTDArray& params, michael@0: void* eng, SkScriptValue* value) { michael@0: if (SK_LITERAL_STR_EQUAL("rgb", function, len) == false) michael@0: return false; michael@0: if (params.count() != 3) michael@0: return false; michael@0: SkScriptEngine* engine = (SkScriptEngine*) eng; michael@0: unsigned result = 0xFF000000; michael@0: int shift = 16; michael@0: for (SkScriptValue* valuePtr = params.begin(); valuePtr < params.end(); valuePtr++) { michael@0: engine->convertTo(SkType_Int, valuePtr); michael@0: result |= SkClampMax(valuePtr->fOperand.fS32, 255) << shift; michael@0: shift -= 8; michael@0: } michael@0: value->fOperand.fS32 = result; michael@0: value->fType = SkType_Int; michael@0: return true; michael@0: } michael@0: michael@0: bool SkAnimatorScript::EvalMemberCommon(SkScriptEngine* engine, const SkMemberInfo* info, michael@0: SkDisplayable* displayable, SkScriptValue* value) { michael@0: SkDisplayTypes original; michael@0: SkDisplayTypes type = original = (SkDisplayTypes) info->getType(); michael@0: if (info->fType == SkType_Array) michael@0: type = SkType_Array; michael@0: switch (type) { michael@0: case SkType_ARGB: michael@0: type = SkType_Int; michael@0: case SkType_Boolean: michael@0: case SkType_Int: michael@0: case SkType_MSec: michael@0: case SkType_Float: michael@0: SkASSERT(info->getCount() == 1); michael@0: if (info->fType != SkType_MemberProperty && info->fType != SkType_MemberFunction) michael@0: value->fOperand.fS32 = *(int32_t*) info->memberData(displayable); // OK for SkScalar too michael@0: if (type == SkType_MSec) { michael@0: value->fOperand.fScalar = SkScalarDiv((SkScalar) value->fOperand.fS32, 1000); // dividing two ints is the same as dividing two scalars michael@0: type = SkType_Float; michael@0: } michael@0: break; michael@0: case SkType_String: { michael@0: SkString* displayableString; michael@0: if (info->fType != SkType_MemberProperty && info->fType != SkType_MemberFunction) { michael@0: info->getString(displayable, &displayableString); michael@0: value->fOperand.fString = new SkString(*displayableString); michael@0: } michael@0: } break; michael@0: case SkType_Array: { michael@0: SkASSERT(info->fType != SkType_MemberProperty); // !!! incomplete michael@0: SkTDOperandArray* displayableArray = (SkTDOperandArray*) info->memberData(displayable); michael@0: if (displayable->getType() == SkType_Array) { michael@0: SkDisplayArray* typedArray = (SkDisplayArray*) displayable; michael@0: original = typedArray->values.getType(); michael@0: } michael@0: SkASSERT(original != SkType_Unknown); michael@0: SkTypedArray* array = value->fOperand.fArray = new SkTypedArray(original); michael@0: engine->track(array); michael@0: int count = displayableArray->count(); michael@0: if (count > 0) { michael@0: array->setCount(count); michael@0: memcpy(array->begin(), displayableArray->begin(), count * sizeof(SkOperand)); michael@0: } michael@0: } break; michael@0: default: michael@0: SkASSERT(0); // unimplemented michael@0: } michael@0: value->fType = type; michael@0: return true; michael@0: } michael@0: michael@0: bool SkAnimatorScript::EvalMember(const char* member, size_t len, void* object, void* eng, michael@0: SkScriptValue* value) { michael@0: SkScriptEngine* engine = (SkScriptEngine*) eng; michael@0: SkDisplayable* displayable = (SkDisplayable*) object; michael@0: SkString name(member, len); michael@0: SkDisplayable* named = displayable->contains(name); michael@0: if (named) { michael@0: value->fOperand.fDisplayable = named; michael@0: value->fType = SkType_Displayable; michael@0: return true; michael@0: } michael@0: const SkMemberInfo* info = displayable->getMember(name.c_str()); michael@0: if (info == NULL) michael@0: return false; michael@0: if (info->fType == SkType_MemberProperty) { michael@0: if (displayable->getProperty(info->propertyIndex(), value) == false) { michael@0: SkASSERT(0); michael@0: return false; michael@0: } michael@0: } michael@0: return EvalMemberCommon(engine, info, displayable, value); michael@0: } michael@0: michael@0: bool SkAnimatorScript::EvalMemberFunction(const char* member, size_t len, void* object, michael@0: SkTDArray& params, void* eng, SkScriptValue* value) { michael@0: SkScriptEngine* engine = (SkScriptEngine*) eng; michael@0: SkDisplayable* displayable = (SkDisplayable*) object; michael@0: SkString name(member, len); michael@0: const SkMemberInfo* info = displayable->getMember(name.c_str()); michael@0: SkASSERT(info != NULL); /* !!! error handling unimplemented */ michael@0: if (info->fType != SkType_MemberFunction) { michael@0: SkASSERT(0); michael@0: return false; michael@0: } michael@0: displayable->executeFunction(displayable, info->functionIndex(), params, info->getType(), michael@0: value); michael@0: return EvalMemberCommon(engine, info, displayable, value); michael@0: } michael@0: michael@0: bool SkAnimatorScript::EvaluateDisplayable(SkAnimateMaker& maker, SkDisplayable* displayable, const char* script, SkDisplayable** result) { michael@0: SkAnimatorScript engine(maker, displayable, SkType_Displayable); michael@0: SkScriptValue value; michael@0: bool success = engine.evaluate(script, &value, SkType_Displayable); michael@0: if (success) michael@0: *result = value.fOperand.fDisplayable; michael@0: return success; michael@0: } michael@0: michael@0: bool SkAnimatorScript::EvaluateInt(SkAnimateMaker& maker, SkDisplayable* displayable, const char* script, int32_t* result) { michael@0: SkAnimatorScript engine(maker, displayable, SkType_Int); michael@0: SkScriptValue value; michael@0: bool success = engine.evaluate(script, &value, SkType_Int); michael@0: if (success) michael@0: *result = value.fOperand.fS32; michael@0: return success; michael@0: } michael@0: michael@0: bool SkAnimatorScript::EvaluateFloat(SkAnimateMaker& maker, SkDisplayable* displayable, const char* script, SkScalar* result) { michael@0: SkAnimatorScript engine(maker, displayable, SkType_Float); michael@0: SkScriptValue value; michael@0: bool success = engine.evaluate(script, &value, SkType_Float); michael@0: if (success) michael@0: *result = value.fOperand.fScalar; michael@0: return success; michael@0: } michael@0: michael@0: bool SkAnimatorScript::EvaluateString(SkAnimateMaker& maker, SkDisplayable* displayable, const char* script, SkString* result) { michael@0: SkAnimatorScript engine(maker, displayable, SkType_String); michael@0: SkScriptValue value; michael@0: bool success = engine.evaluate(script, &value, SkType_String); michael@0: if (success) michael@0: result->set(*(value.fOperand.fString)); michael@0: return success; michael@0: } michael@0: michael@0: bool SkAnimatorScript::EvaluateString(SkAnimateMaker& maker, SkDisplayable* displayable, SkDisplayable* parent, const char* script, SkString* result) { michael@0: SkAnimatorScript engine(maker, displayable, SkType_String); michael@0: engine.fParent = parent; michael@0: SkScriptValue value; michael@0: bool success = engine.evaluate(script, &value, SkType_String); michael@0: if (success) michael@0: result->set(*(value.fOperand.fString)); michael@0: return success; michael@0: } michael@0: michael@0: const SkDisplayEnumMap& SkAnimatorScript::GetEnumValues(SkDisplayTypes type) { michael@0: int index = SkTSearch(&gEnumMaps[0].fType, gEnumMapCount, type, michael@0: sizeof(SkDisplayEnumMap)); michael@0: SkASSERT(index >= 0); michael@0: return gEnumMaps[index]; michael@0: } michael@0: michael@0: bool SkAnimatorScript::Infinity(const char* token, size_t len, void* user, SkScriptValue* value) { michael@0: if (SK_LITERAL_STR_EQUAL("Infinity", token, len) == false) michael@0: return false; michael@0: value->fType = SkType_Float; michael@0: value->fOperand.fScalar = SK_ScalarInfinity; michael@0: return true; michael@0: } michael@0: michael@0: bool SkAnimatorScript::IsFinite(const char* function, size_t len, SkTDArray& params, michael@0: void* eng, SkScriptValue* value) { michael@0: if (SK_LITERAL_STR_EQUAL(function, "isFinite", len) == false) michael@0: return false; michael@0: if (params.count() != 1) michael@0: return false; michael@0: SkScriptValue* scriptValue = params.begin(); michael@0: SkDisplayTypes type = scriptValue->fType; michael@0: SkScalar scalar = scriptValue->fOperand.fScalar; michael@0: value->fType = SkType_Int; michael@0: value->fOperand.fS32 = type == SkType_Float ? SkScalarIsNaN(scalar) == false && michael@0: SkScalarAbs(scalar) != SK_ScalarInfinity : type == SkType_Int; michael@0: return true; michael@0: } michael@0: michael@0: bool SkAnimatorScript::IsNaN(const char* function, size_t len, SkTDArray& params, michael@0: void* eng, SkScriptValue* value) { michael@0: if (SK_LITERAL_STR_EQUAL("isNaN", function, len) == false) michael@0: return false; michael@0: if (params.count() != 1) michael@0: return false; michael@0: SkScriptValue* scriptValue = params.begin(); michael@0: value->fType = SkType_Int; michael@0: value->fOperand.fS32 = scriptValue->fType == SkType_Float ? SkScalarIsNaN(scriptValue->fOperand.fScalar) : 0; michael@0: return true; michael@0: } michael@0: michael@0: bool SkAnimatorScript::MapEnums(const char* ptr, const char* match, size_t len, int* value) { michael@0: int index = 0; michael@0: bool more = true; michael@0: do { michael@0: const char* last = strchr(ptr, '|'); michael@0: if (last == NULL) { michael@0: last = &ptr[strlen(ptr)]; michael@0: more = false; michael@0: } michael@0: size_t length = last - ptr; michael@0: if (len == length && strncmp(ptr, match, length) == 0) { michael@0: *value = index; michael@0: return true; michael@0: } michael@0: index++; michael@0: ptr = last + 1; michael@0: } while (more); michael@0: return false; michael@0: } michael@0: michael@0: bool SkAnimatorScript::NaN(const char* token, size_t len, void* user, SkScriptValue* value) { michael@0: if (SK_LITERAL_STR_EQUAL("NaN", token, len) == false) michael@0: return false; michael@0: value->fType = SkType_Float; michael@0: value->fOperand.fScalar = SK_ScalarNaN; michael@0: return true; michael@0: } michael@0: michael@0: #if 0 michael@0: bool SkAnimatorScript::ObjectToString(void* object, void* user, SkScriptValue* value) { michael@0: SkTDict* ids = (SkTDict*) user; michael@0: SkDisplayable* displayable = (SkDisplayable*) object; michael@0: const char* key; michael@0: bool success = ids->findKey(displayable, &key); michael@0: if (success == false) michael@0: return false; michael@0: value->fOperand.fString = new SkString(key); michael@0: value->fType = SkType_String; michael@0: return true; michael@0: } michael@0: #endif michael@0: michael@0: bool SkAnimatorScript::Unbox(void* m, SkScriptValue* scriptValue) { michael@0: SkAnimateMaker* maker = (SkAnimateMaker*) m; michael@0: SkASSERT((unsigned) scriptValue->fType == (unsigned) SkType_Displayable); michael@0: SkDisplayable* displayable = (SkDisplayable*) scriptValue->fOperand.fObject; michael@0: SkDisplayTypes type = displayable->getType(); michael@0: switch (displayable->getType()) { michael@0: case SkType_Array: { michael@0: SkDisplayArray* boxedValue = (SkDisplayArray*) displayable; michael@0: scriptValue->fOperand.fArray = &boxedValue->values; michael@0: } break; michael@0: case SkType_Boolean: { michael@0: SkDisplayBoolean* boxedValue = (SkDisplayBoolean*) displayable; michael@0: scriptValue->fOperand.fS32 = boxedValue->value; michael@0: } break; michael@0: case SkType_Int: { michael@0: SkDisplayInt* boxedValue = (SkDisplayInt*) displayable; michael@0: scriptValue->fOperand.fS32 = boxedValue->value; michael@0: } break; michael@0: case SkType_Float: { michael@0: SkDisplayFloat* boxedValue = (SkDisplayFloat*) displayable; michael@0: scriptValue->fOperand.fScalar = boxedValue->value; michael@0: } break; michael@0: case SkType_String: { michael@0: SkDisplayString* boxedValue = (SkDisplayString*) displayable; michael@0: scriptValue->fOperand.fString = SkNEW_ARGS(SkString, (boxedValue->value)); michael@0: } break; michael@0: default: { michael@0: const char* id = NULL; michael@0: SkDEBUGCODE(bool success = ) maker->findKey(displayable, &id); michael@0: SkASSERT(success); michael@0: scriptValue->fOperand.fString = SkNEW_ARGS(SkString, (id)); michael@0: type = SkType_String; michael@0: } michael@0: } michael@0: scriptValue->fType = type; michael@0: return true; michael@0: } michael@0: michael@0: #if defined SK_SUPPORT_UNITTEST michael@0: michael@0: #include "SkAnimator.h" michael@0: michael@0: static const char scriptTestSetup[] = michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: "\n" michael@0: ""; michael@0: michael@0: #define DEFAULT_ANSWER , 0 michael@0: michael@0: static const SkScriptNAnswer scriptTests[] = { michael@0: { "label.text.length == 4", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: // { "labelPaint.measureText(label.text) > 0 ? labelPaint.measureText(label.text)+10 : 40", SkType_Float, 0, SkIntToScalar(0x23) }, michael@0: { "Number.POSITIVE_INFINITY >= Number.MAX_VALUE ? 1 : 0", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "Infinity >= Number.MAX_VALUE ? 1 : 0", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "Number.NEGATIVE_INFINITY <= -Number.MAX_VALUE ? 1 : 0", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "Number.MIN_VALUE > 0 ? 1 : 0", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "isNaN(Number.NaN)", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "isNaN(NaN)", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "Math.sin(0)", SkType_Float, 0, SkIntToScalar(0) DEFAULT_ANSWER }, michael@0: { "alpha+alpha", SkType_String, 0, 0, "abcabc" }, michael@0: { "intArray[4]", SkType_Unknown DEFAULT_ANSWER DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "emptyArray[4]", SkType_Unknown DEFAULT_ANSWER DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "idx", SkType_Int, 2 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "intArray.length", SkType_Int, 3 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "intArray.values[0]", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "intArray[0]", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "idx.value", SkType_Int, 2 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "alpha.value", SkType_String, 0, 0, "abc" }, michael@0: { "alpha", SkType_String, 0, 0, "abc" }, michael@0: { "alpha.value+alpha.value", SkType_String, 0, 0, "abcabc" }, michael@0: { "alpha+idx", SkType_String, 0, 0, "abc2" }, michael@0: { "idx+alpha", SkType_String, 0, 0, "2abc" }, michael@0: { "intArray[idx]", SkType_Int, 6 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "alpha.slice(1,2)", SkType_String, 0, 0, "b" }, michael@0: { "alpha.value.slice(1,2)", SkType_String, 0, 0, "b" }, michael@0: { "testRect.left+2", SkType_Float, 0, SkIntToScalar(3) DEFAULT_ANSWER }, michael@0: { "0 ? Math.sin(0) : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "0 ? intArray[0] : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "0 ? intArray.values[0] : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "0 ? idx : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "0 ? idx.value : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "0 ? alpha.slice(1,2) : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "0 ? alpha.value.slice(1,2) : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER }, michael@0: { "idy", SkType_Int, 3 DEFAULT_ANSWER DEFAULT_ANSWER } michael@0: }; michael@0: michael@0: #define SkScriptNAnswer_testCount SK_ARRAY_COUNT(scriptTests) michael@0: michael@0: void SkAnimatorScript::UnitTest() { michael@0: #if defined(SK_SUPPORT_UNITTEST) michael@0: SkAnimator animator; michael@0: SkASSERT(animator.decodeMemory(scriptTestSetup, sizeof(scriptTestSetup)-1)); michael@0: SkEvent evt; michael@0: evt.setString("id", "evt"); michael@0: evt.setS32("x", 3); michael@0: animator.doUserEvent(evt); michael@0: // set up animator with memory script above, then run value tests michael@0: for (unsigned index = 0; index < SkScriptNAnswer_testCount; index++) { michael@0: SkAnimatorScript engine(*animator.fMaker, NULL, scriptTests[index].fType); michael@0: SkScriptValue value; michael@0: const char* script = scriptTests[index].fScript; michael@0: bool success = engine.evaluateScript(&script, &value); michael@0: if (success == false) { michael@0: SkDebugf("script failed: %s\n", scriptTests[index].fScript); michael@0: SkASSERT(scriptTests[index].fType == SkType_Unknown); michael@0: continue; michael@0: } michael@0: SkASSERT(value.fType == scriptTests[index].fType); michael@0: SkScalar error; michael@0: switch (value.fType) { michael@0: case SkType_Int: michael@0: SkASSERT(value.fOperand.fS32 == scriptTests[index].fIntAnswer); michael@0: break; michael@0: case SkType_Float: michael@0: error = SkScalarAbs(value.fOperand.fScalar - scriptTests[index].fScalarAnswer); michael@0: SkASSERT(error < SK_Scalar1 / 10000); michael@0: break; michael@0: case SkType_String: michael@0: SkASSERT(strcmp(value.fOperand.fString->c_str(), scriptTests[index].fStringAnswer) == 0); michael@0: break; michael@0: default: michael@0: SkASSERT(0); michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: #endif