mirror of
https://github.com/rive-app/rive-ios.git
synced 2026-01-18 17:11:28 +01:00
feat(ios): add support for data binding images (#9664) 8f30ede7be
Co-authored-by: David Skuza <david@rive.app>
This commit is contained in:
BIN
Tests/Assets/1x1_jpg.jpg
Normal file
BIN
Tests/Assets/1x1_jpg.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 285 B |
BIN
Tests/Assets/1x1_png.png
Normal file
BIN
Tests/Assets/1x1_png.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 116 B |
BIN
Tests/Assets/data_binding_image_test.riv
Normal file
BIN
Tests/Assets/data_binding_image_test.riv
Normal file
Binary file not shown.
@@ -10,7 +10,7 @@ import XCTest
|
||||
@testable import RiveRuntime
|
||||
|
||||
class DataBindingTests: XCTestCase {
|
||||
let file = try! RiveFile(testfileName: "data_binding_test")
|
||||
let file: RiveFile = try! RiveFile(testfileName: "data_binding_test")
|
||||
|
||||
// MARK: - RiveFile
|
||||
|
||||
@@ -442,6 +442,28 @@ class DataBindingTests: XCTestCase {
|
||||
XCTAssertNil(instance.triggerProperty(fromPath: "404"))
|
||||
}
|
||||
|
||||
// MARK: Image
|
||||
|
||||
func test_viewModelInstance_imageProperty_returnsPropertyOrNil() throws {
|
||||
let file = try RiveFile(testfileName: "data_binding_image_test")
|
||||
let instance = file.viewModelNamed("vm")!.createDefaultInstance()!
|
||||
XCTAssertNotNil(instance.imageProperty(fromPath: "img"))
|
||||
XCTAssertNil(instance.imageProperty(fromPath: "404"))
|
||||
}
|
||||
|
||||
func test_viewModelInstance_imageProperty_canSetValue() throws {
|
||||
let file = try RiveFile(testfileName: "data_binding_image_test")
|
||||
let instance = file.viewModelNamed("vm")!.createDefaultInstance()!
|
||||
let property = instance.imageProperty(fromPath: "img")!
|
||||
|
||||
let bundle = Bundle(for: type(of: self))
|
||||
let fileURL = bundle.url(forResource: "1x1_jpg", withExtension: "jpg")!
|
||||
let data = try Data(contentsOf: fileURL)
|
||||
let renderImage = RiveRenderImage(data: data)!
|
||||
property.setValue(renderImage)
|
||||
XCTAssertTrue(property.hasChanged)
|
||||
}
|
||||
|
||||
// MARK: Binding
|
||||
|
||||
func test_binding_artboard_stringProperty_updatesTextRun() throws {
|
||||
|
||||
58
Tests/RiveRenderImageTests.swift
Normal file
58
Tests/RiveRenderImageTests.swift
Normal file
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// RiveRenderImageTests.swift
|
||||
// RiveRuntimeTests
|
||||
//
|
||||
// Created by David Skuza on 5/13/25.
|
||||
// Copyright © 2025 Rive. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import RiveRuntime
|
||||
|
||||
class RiveRenderImageTests: XCTestCase {
|
||||
func test_imageFromData_withEmptyData_returnsNil() {
|
||||
// Data comprised of 0 bytes
|
||||
XCTAssertNil(RiveRenderImage(data: Data()))
|
||||
}
|
||||
|
||||
func test_imageFromData_withIncompatibleData_returnsNil() throws {
|
||||
// Data comprised of 8 bytes that do _not_ create an image
|
||||
XCTAssertNil(RiveRenderImage(data: Data([1, 2, 3, 4, 5, 6, 7, 8])))
|
||||
}
|
||||
|
||||
func test_imageFromData_withCompatibleData_returnsImage() throws {
|
||||
let bundle = Bundle(for: type(of: self))
|
||||
|
||||
// We know JPG to be a valid Rive image asset format
|
||||
var fileURL = bundle.url(forResource: "1x1_jpg", withExtension: "jpg")!
|
||||
var data = try Data(contentsOf: fileURL)
|
||||
XCTAssertNotNil(RiveRenderImage(data: data))
|
||||
|
||||
// We know PNG to be a valid Rive image asset format
|
||||
fileURL = bundle.url(forResource: "1x1_png", withExtension: "png")!
|
||||
data = try Data(contentsOf: fileURL)
|
||||
XCTAssertNotNil(RiveRenderImage(data: data))
|
||||
}
|
||||
|
||||
// MARK: - Extensions
|
||||
|
||||
func test_imageFromUIImage_withIncorrectFormat_returnsNil() throws {
|
||||
XCTAssertNil(RiveRenderImage(image: UIImage(), format: .png))
|
||||
XCTAssertNil(RiveRenderImage(image: UIImage(), format: .jpeg(compressionQuality: 80)))
|
||||
}
|
||||
|
||||
func test_imageFromUIImage_withCorrectFormat_returnsImage() throws {
|
||||
let bundle = Bundle(for: type(of: self))
|
||||
|
||||
// We know JPG to be a valid Rive image asset format
|
||||
var fileURL = bundle.url(forResource: "1x1_jpg", withExtension: "jpg")!
|
||||
var data = try Data(contentsOf: fileURL)
|
||||
var image = UIImage(data: data)!
|
||||
XCTAssertNotNil(RiveRenderImage(image: image, format: .jpeg(compressionQuality: 80)))
|
||||
|
||||
fileURL = bundle.url(forResource: "1x1_png", withExtension: "png")!
|
||||
data = try Data(contentsOf: fileURL)
|
||||
image = UIImage(data: data)!
|
||||
XCTAssertNotNil(RiveRenderImage(image: image, format: .png))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user