From f8ad5514af548871af80b57797c0a91294f5cc37 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Mon, 11 Jan 2016 09:16:55 +0100 Subject: [PATCH] AnimationState unit test. Pharan, y u so silly?! --- .../spine-libgdx-tests/assets/test/test.json | 18 ++ .../spine-libgdx-tests/assets/test/test.spine | Bin 0 -> 392 bytes .../spine/AnimationStateTest.java | 216 ++++++++++++++++++ 3 files changed, 234 insertions(+) create mode 100644 spine-libgdx/spine-libgdx-tests/assets/test/test.json create mode 100644 spine-libgdx/spine-libgdx-tests/assets/test/test.spine create mode 100644 spine-libgdx/spine-libgdx-tests/src/com/esotericsoftware/spine/AnimationStateTest.java diff --git a/spine-libgdx/spine-libgdx-tests/assets/test/test.json b/spine-libgdx/spine-libgdx-tests/assets/test/test.json new file mode 100644 index 000000000..872fb7692 --- /dev/null +++ b/spine-libgdx/spine-libgdx-tests/assets/test/test.json @@ -0,0 +1,18 @@ +{ +"skeleton": { "hash": "NG9aJneROk8CsMAugcCOiMeXbGA", "spine": "Dev", "width": 0, "height": 0 }, +"bones": [ + { "name": "root" } +], +"events": { + "event": {} +}, +"animations": { + "events": { + "events": [ + { "time": 0, "name": "event", "string": "0" }, + { "time": 0.4666, "name": "event", "string": "14" }, + { "time": 1, "name": "event", "string": "30" } + ] + } +} +} \ No newline at end of file diff --git a/spine-libgdx/spine-libgdx-tests/assets/test/test.spine b/spine-libgdx/spine-libgdx-tests/assets/test/test.spine new file mode 100644 index 0000000000000000000000000000000000000000..13f3b53f4e7f8fea52f91deb10dab03e8e7a899d GIT binary patch literal 392 zcmV;30eAkbk-bU-K@i91zq_}0xl8WyArcUypajx{h^;1mHa05p0fcZx6myynv5a|* zyo7}>U}fi9q!S66UT5ztR1g~%c4p^h?l;4K6mkj$vMXp1(St)hiF6r|2yZT< zZhx=+Dsk&=LJA^Od*|UZC|!pKvPyX8qsZkRVpI_!IW+-humg02W=r3aVCvfdP25z7J7jXS9 z0ZxAbS}cam8(BrHNdJwB5}IXHELj?PZHnxeZOrFCW)mfh$K&@b0;FOnx=}QfNr#N7 m^^I{&m@KL&kYl9F;FV<$S#Ba({eIY+F;QeVcKFWo75D(WjIjLx literal 0 HcmV?d00001 diff --git a/spine-libgdx/spine-libgdx-tests/src/com/esotericsoftware/spine/AnimationStateTest.java b/spine-libgdx/spine-libgdx-tests/src/com/esotericsoftware/spine/AnimationStateTest.java new file mode 100644 index 000000000..cbfd514fb --- /dev/null +++ b/spine-libgdx/spine-libgdx-tests/src/com/esotericsoftware/spine/AnimationStateTest.java @@ -0,0 +1,216 @@ +/****************************************************************************** + * Spine Runtimes Software License + * Version 2.3 + * + * Copyright (c) 2013-2015, Esoteric Software + * All rights reserved. + * + * You are granted a perpetual, non-exclusive, non-sublicensable and + * non-transferable license to use, install, execute and perform the Spine + * Runtimes Software (the "Software") and derivative works solely for personal + * or internal use. Without the written permission of Esoteric Software (see + * Section 2 of the Spine Software License Agreement), you may not (a) modify, + * translate, adapt or otherwise create derivative works, improvements of the + * Software or develop new applications using the Software or (b) remove, + * delete, alter or obscure any trademarks or any copyright, trademark, patent + * or other intellectual property or proprietary rights notices on or in the + * Software, including any copy thereof. Redistributions in binary or source + * form must include this license and terms. + * + * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *****************************************************************************/ + +package com.esotericsoftware.spine; + +import com.badlogic.gdx.Files.FileType; +import com.badlogic.gdx.backends.lwjgl.LwjglFileHandle; +import com.badlogic.gdx.utils.Array; +import com.esotericsoftware.spine.AnimationState.AnimationStateListener; +import com.esotericsoftware.spine.attachments.AttachmentLoader; +import com.esotericsoftware.spine.attachments.BoundingBoxAttachment; +import com.esotericsoftware.spine.attachments.MeshAttachment; +import com.esotericsoftware.spine.attachments.RegionAttachment; +import com.esotericsoftware.spine.attachments.SkinnedMeshAttachment; + +public class AnimationStateTest { + final SkeletonJson json = new SkeletonJson(new AttachmentLoader() { + public SkinnedMeshAttachment newSkinnedMeshAttachment (Skin skin, String name, String path) { + return null; + } + + public RegionAttachment newRegionAttachment (Skin skin, String name, String path) { + return null; + } + + public MeshAttachment newMeshAttachment (Skin skin, String name, String path) { + return null; + } + + public BoundingBoxAttachment newBoundingBoxAttachment (Skin skin, String name) { + return null; + } + }); + + AnimationStateListener stateListener = new AnimationStateListener() { + public void start (int trackIndex) { + actual.add(new Result("start", null)); + } + + public void event (int trackIndex, Event event) { + actual.add(new Result("event", event.getString())); + } + + public void complete (int trackIndex, int loopCount) { + actual.add(new Result("complete", null)); + } + + public void end (int trackIndex) { + actual.add(new Result("end", null)); + } + }; + + final SkeletonData skeletonData; + final AnimationStateData stateData; + final Array actual = new Array(); + + public AnimationStateTest () { + skeletonData = json.readSkeletonData(new LwjglFileHandle("test/test.json", FileType.Internal)); + stateData = new AnimationStateData(skeletonData); + + AnimationState state; + + state = newState(); + state.setAnimation(0, "events", false); + test(state, 1 / 60f, 1000, // + new Result("start", null), // + new Result("event", "0"), // + new Result("event", "14"), // + new Result("event", "30"), // + new Result("complete", null), // + new Result("end", null) // + ); + + state = newState(); + state.setAnimation(0, "events", false); + test(state, 30, 1000, // + new Result("start", null), // + new Result("event", "0"), // + new Result("event", "14"), // + new Result("event", "30"), // + new Result("complete", null), // + new Result("end", null) // + ); + + state = newState(); + state.setAnimation(0, "events", false); + test(state, 1, 1.01f, // + new Result("start", null), // + new Result("event", "0"), // + new Result("event", "14"), // + new Result("event", "30"), // + new Result("complete", null), // + new Result("end", null) // + ); + + state = newState(); + state.setAnimation(0, "events", false); + state.addAnimation(0, "events", false, 0); + test(state, 0.1f, 3f, // + new Result("start", null), // + new Result("event", "0"), // + new Result("event", "14"), // + new Result("event", "30"), // + new Result("complete", null), // + new Result("end", null), // + new Result("start", null), // + new Result("event", "0"), // + new Result("event", "14"), // + new Result("event", "30"), // + new Result("complete", null), // + new Result("end", null) // + ); + } + + private AnimationState newState () { + AnimationState state = new AnimationState(stateData); + state.addListener(stateListener); + return state; + } + + private void test (AnimationState state, float incr, float endTime, Result... expectedArray) { + Array expected = new Array(expectedArray); + + Skeleton skeleton = new Skeleton(skeletonData); + + for (int i = 0; i < endTime; i++) { + skeleton.update(incr); + state.update(incr); + state.apply(skeleton); + } + + if (expected.equals(actual)) { + actual.clear(); + return; + } + int i = 0; + for (int n = expected.size; i < n; i++) { + System.out.print(expected.get(i) + " == " + (i < actual.size ? actual.get(i) : "")); + if (i >= actual.size || !actual.get(i).equals(expected.get(i))) + System.out.println(" <- FAIL"); + else + System.out.println(); + } + for (int n = actual.size; i < n; i++) + System.out.print(" == " + actual.get(i) + " <- FAIL"); + System.exit(0); + } + + static public class Result { + String eventName; + String payload; + + public Result (String eventName, String payload) { + this.eventName = eventName; + this.payload = payload; + } + + public int hashCode () { + final int prime = 31; + int result = 1; + result = prime * result + ((eventName == null) ? 0 : eventName.hashCode()); + result = prime * result + ((payload == null) ? 0 : payload.hashCode()); + return result; + } + + public boolean equals (Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + Result other = (Result)obj; + if (eventName == null) { + if (other.eventName != null) return false; + } else if (!eventName.equals(other.eventName)) return false; + if (payload == null) { + if (other.payload != null) return false; + } else if (!payload.equals(other.payload)) return false; + return true; + } + + public String toString () { + return "[" + eventName + ", " + payload + "]"; + } + } + + static public void main (String[] args) throws Exception { + new AnimationStateTest(); + } +}