Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added load animations tests #30

Merged
merged 1 commit into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RiveRuntime.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
04BE5413264943BB00427B39 /* util.mm in Sources */ = {isa = PBXBuildFile; fileRef = 04BE5412264943BB00427B39 /* util.mm */; };
04BE5418264A818F00427B39 /* RiveAnimationConfigurationsTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = 04BE5417264A818F00427B39 /* RiveAnimationConfigurationsTest.mm */; };
04BE541A264A823000427B39 /* animationconfigurations.riv in Resources */ = {isa = PBXBuildFile; fileRef = 04BE5419264A823000427B39 /* animationconfigurations.riv */; };
04BE541C264A90D600427B39 /* RiveAnimationLoadTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = 04BE541B264A90D600427B39 /* RiveAnimationLoadTest.mm */; };
C9002A20263C76080011556B /* RiveView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9002A1F263C76080011556B /* RiveView.swift */; };
C9161A81263CBCBC007749A1 /* RiveRuntime.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C9C73ED124FC478800EF9516 /* RiveRuntime.framework */; };
C9601F2B250C25930032AA07 /* RiveRenderer.mm in Sources */ = {isa = PBXBuildFile; fileRef = C9601F2A250C25930032AA07 /* RiveRenderer.mm */; };
Expand Down Expand Up @@ -334,6 +335,7 @@
04BE541526497D4100427B39 /* util.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = util.h; sourceTree = "<group>"; };
04BE5417264A818F00427B39 /* RiveAnimationConfigurationsTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = RiveAnimationConfigurationsTest.mm; sourceTree = "<group>"; };
04BE5419264A823000427B39 /* animationconfigurations.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = animationconfigurations.riv; sourceTree = "<group>"; };
04BE541B264A90D600427B39 /* RiveAnimationLoadTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = RiveAnimationLoadTest.mm; sourceTree = "<group>"; };
C9002A1F263C76080011556B /* RiveView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RiveView.swift; sourceTree = "<group>"; };
C9601F29250C25830032AA07 /* RiveRenderer.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = RiveRenderer.hpp; sourceTree = "<group>"; };
C9601F2A250C25930032AA07 /* RiveRenderer.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = RiveRenderer.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -920,6 +922,7 @@
04BE5412264943BB00427B39 /* util.mm */,
04BE541526497D4100427B39 /* util.h */,
04BE5417264A818F00427B39 /* RiveAnimationConfigurationsTest.mm */,
04BE541B264A90D600427B39 /* RiveAnimationLoadTest.mm */,
);
path = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1773,6 +1776,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
04BE541C264A90D600427B39 /* RiveAnimationLoadTest.mm in Sources */,
C9C73EE024FC478900EF9516 /* RiveRuntimeTests.mm in Sources */,
04BE5413264943BB00427B39 /* util.mm in Sources */,
04BE54112649434900427B39 /* RiveFileLoadTest.mm in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Source/Renderer/Rive.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ typedef NS_ENUM(NSInteger, Alignment) {
- (CGRect)bounds;

- (NSInteger)animationCount;
- (NSArray *)animationNames;
- (RiveLinearAnimation *)firstAnimation;
- (RiveLinearAnimation *)animationFromIndex:(NSInteger)index;
- (RiveLinearAnimation *)animationFromName:(NSString *)name;
Expand Down
24 changes: 21 additions & 3 deletions Source/Renderer/Rive.mm
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,19 @@ - (NSInteger)animationCount {

// Returns the first animation in the artboard, or null if it has none
- (RiveLinearAnimation *)firstAnimation {
return [[RiveLinearAnimation alloc] initWithAnimation:_artboard->firstAnimation()];
rive::LinearAnimation *animation = _artboard->firstAnimation();
if (animation == nullptr) {
@throw [[RiveException alloc] initWithName:@"NoAnimations" reason:@"No Animations Found." userInfo:nil];
}
else {
return [[RiveLinearAnimation alloc] initWithAnimation:animation];
}

}

- (RiveLinearAnimation *)animationFromIndex:(NSInteger)index {
if (index < 0 || index >= [self animationCount]) {
return NULL;
@throw [[RiveException alloc] initWithName:@"NoAnimationFound" reason:[NSString stringWithFormat: @"No Animation Found at index %ld.", index] userInfo:nil];
}
return [[RiveLinearAnimation alloc] initWithAnimation: _artboard->animation(index)];
}
Expand All @@ -270,11 +277,21 @@ - (RiveLinearAnimation *)animationFromName:(NSString *)name {
std::string stdName = std::string([name UTF8String]);
rive::LinearAnimation *animation = _artboard->animation(stdName);
if (animation == nullptr) {
return NULL;
@throw [[RiveException alloc] initWithName:@"NoAnimationFound" reason:[NSString stringWithFormat: @"No Animation Found with name %@.", name] userInfo:nil];
}
return [[RiveLinearAnimation alloc] initWithAnimation: animation];
}

- (NSArray *)animationNames{
NSMutableArray *animationNames = [NSMutableArray array];

for (NSUInteger i=0; i<[self animationCount]; i++){
[animationNames addObject:[[self animationFromIndex: i] name]];
}
return animationNames;

}

// Returns the number of state machines in the artboard
- (NSInteger)stateMachineCount {
return _artboard->stateMachineCount();
Expand Down Expand Up @@ -329,6 +346,7 @@ @implementation RiveLinearAnimation {
const rive::LinearAnimation *animation;
}


- (instancetype)initWithAnimation:(const rive::LinearAnimation *) riveAnimation {
if (self = [super init]) {
animation = riveAnimation;
Expand Down
122 changes: 122 additions & 0 deletions Tests/RiveAnimationLoadTest.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//
// RiveAnimationLoadTest.m
// RiveRuntimeTests
//
// Created by Maxwell Talbot on 11/05/2021.
// Copyright © 2021 Rive. All rights reserved.
//



#import <XCTest/XCTest.h>
#import "Rive.h"
#import "util.h"

@interface RiveanimationLoadTest : XCTestCase

@end

@implementation RiveanimationLoadTest

/*
* Test first Animation
*/
- (void)testAnimationFirstAnimation {
RiveFile* file = [Util loadTestFile:@"multipleartboards"];
RiveArtboard* artboard = [file artboardFromName:@"artboard1"];

RiveLinearAnimation* animation = [artboard firstAnimation];
RiveLinearAnimation* animationByIndex = [artboard animationFromIndex:0];
RiveLinearAnimation* animationByName = [artboard animationFromName:@"artboard1animation1"];

XCTAssertTrue([animation.name isEqualToString:animationByIndex.name]);
XCTAssertTrue([animation.name isEqualToString:animationByName.name]);

NSArray *target = [NSArray arrayWithObjects:@"artboard1animation1", nil];
XCTAssertTrue([[artboard animationNames] isEqualToArray: target]);
}

/*
* Test second Animation
*/
- (void)testAnimationSecondAnimation {
RiveFile* file = [Util loadTestFile:@"multipleartboards"];
RiveArtboard* artboard = [file artboardFromName:@"artboard2"];

RiveLinearAnimation* animation = [artboard firstAnimation];
RiveLinearAnimation* animationByIndex = [artboard animationFromIndex:0];
RiveLinearAnimation* animationByName = [artboard animationFromName:@"artboard2animation1"];

XCTAssertTrue([animation.name isEqualToString:animationByIndex.name]);
XCTAssertTrue([animation.name isEqualToString:animationByName.name]);


RiveLinearAnimation* animation2ByIndex = [artboard animationFromIndex:1];
RiveLinearAnimation* animation2ByName = [artboard animationFromName:@"artboard2animation2"];

XCTAssertTrue([animation2ByIndex.name isEqualToString:animation2ByName.name]);


NSArray *target = [NSArray arrayWithObjects:@"artboard2animation1", @"artboard2animation2", nil];
XCTAssertTrue([[artboard animationNames] isEqualToArray: target]);
}

/*
* Test no animations
*/
- (void)testArtboardHasNoAnimations {
RiveFile* file = [Util loadTestFile:@"noanimation"];
RiveArtboard* artboard = [file artboard];

XCTAssertEqual([artboard animationCount], 0);

NSArray *target = [NSArray array];
XCTAssertTrue([[artboard animationNames] isEqualToArray: target]);
}

/*
* Test access nothing
*/
- (void)testArtboardAnimationDoesntExist {
RiveFile* file = [Util loadTestFile:@"noanimation"];
RiveArtboard* artboard = [file artboard];


XCTAssertThrowsSpecificNamed(
[artboard firstAnimation],
RiveException,
@"NoAnimations"
);
}

/*
* Test access index doesnt exist
*/
- (void)testArtboardAnimationAtIndexDoesntExist {
RiveFile* file = [Util loadTestFile:@"noanimation"];
RiveArtboard* artboard = [file artboard];


XCTAssertThrowsSpecificNamed(
[artboard animationFromIndex:0],
RiveException,
@"NoAnimationFound"
);
}

/*
* Test access name doesnt exist
*/
- (void)testArtboardAnimationWithNameDoesntExist {
RiveFile* file = [Util loadTestFile:@"noanimation"];
RiveArtboard* artboard = [file artboard];


XCTAssertThrowsSpecificNamed(
[artboard animationFromName:@"boo"],
RiveException,
@"NoAnimationFound"
);
}

@end