Files
rive-ios/Tests/RiveViewModelTest.swift
dskuza 2787cd9c6d Add support for nested text runs on iOS
This adds support for getting / setting nested text runs on iOS, via the exposed lower-level API. Tests have been added for: testing that setting the text run works at the artboard level, and at the view model level, and additionally that the parent text run will be returned if the path is empty.

Diffs=
bbec5cbbc Add support for nested text runs on iOS (#8108)

Co-authored-by: David Skuza <david@rive.app>
2024-09-10 14:20:35 +00:00

72 lines
2.6 KiB
Swift

//
// RiveViewModelTest.swift
// RiveRuntimeTests
//
// Created by Maxwell Talbot on 14/02/2023.
// Copyright © 2023 Rive. All rights reserved.
//
import XCTest
import RiveRuntime
class RiveViewModelTest: XCTestCase {
// This test reproduces a previous production error
// Having an Animation state without an animation caused advancing a state machine past it
// to fail
func testLoadFileWithEmptyAnimationState() throws {
let file = try RiveFile(testfileName: "empty_animation_state")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, autoPlay: false)
let view = viewModel.createRiveView()
view.advance(delta: 0.1)
}
func testChangingTextRun_updatesText_andAdvances() throws {
let file = try RiveFile(testfileName: "testtext")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, autoPlay: false)
let delegate = PlayerDelegate()
let view = viewModel.createRiveView()
view.playerDelegate = delegate
XCTAssertEqual(viewModel.getTextRunValue("MyRun"), "Hello there")
try viewModel.setTextRunValue("MyRun", textValue: "Hello test")
XCTAssertEqual(viewModel.getTextRunValue("MyRun"), "Hello test")
XCTAssertTrue(delegate.didAdvance)
}
func testChangingNestedTextRun_updatesText_andAdvances() throws {
let file = try RiveFile(testfileName: "nested_text_run")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, autoPlay: false)
let view = viewModel.createRiveView()
let delegate = PlayerDelegate()
view.playerDelegate = delegate
XCTAssertEqual(viewModel.getTextRunValue("text", path: "Nested/Two-Deep"), "Text")
try viewModel.setTextRunValue("text", path: "Nested/Two-Deep", textValue: "Hello test")
XCTAssertEqual(viewModel.getTextRunValue("text", path: "Nested/Two-Deep"), "Hello test")
XCTAssertTrue(delegate.didAdvance)
}
}
private extension RiveViewModelTest {
class PlayerDelegate: NSObject, RivePlayerDelegate {
var didAdvance = false
func player(playedWithModel riveModel: RiveRuntime.RiveModel?) { }
func player(pausedWithModel riveModel: RiveRuntime.RiveModel?) { }
func player(loopedWithModel riveModel: RiveRuntime.RiveModel?, type: Int) { }
func player(stoppedWithModel riveModel: RiveRuntime.RiveModel?) { }
func player(didAdvanceby seconds: Double, riveModel: RiveModel?) {
didAdvance = true
}
}
}