Skip to content

Commit

Permalink
more tests for textures and tap actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxxfrazer committed Dec 20, 2023
1 parent 6cdb6d2 commit 9afec10
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Sources/RealityUI/RUITexture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public struct RUITexture {
return try await self.generateTexture(systemName: systemName, config: config)
}
fileprivate static func generateCGImage(
systemName: String, config: UIImage.SymbolConfiguration
systemName: String, config: UIImage.SymbolConfiguration?
) throws -> CGImage {
#if canImport(UIKit)
guard let symbolImage = UIImage(
Expand All @@ -65,7 +65,7 @@ public struct RUITexture {
/// - config: Image SymbolConfiguration for the SF Symbol Image.
/// - Returns: A new `TextureResource`.
public static func generateTexture(
systemName: String, config: UIImage.SymbolConfiguration
systemName: String, config: UIImage.SymbolConfiguration? = nil
) async throws -> TextureResource {
let cgImage = try self.generateCGImage(systemName: systemName, config: config)
return try await TextureResource.generate(
Expand Down
2 changes: 1 addition & 1 deletion Sources/RealityUI/RealityUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ import Combine
self.installedGestures[arView]?.append(dragGesture)
}

fileprivate func tapActionChecker(_ arView: ARView, _ tapInView: CGPoint) {
internal func tapActionChecker(_ arView: ARView, _ tapInView: CGPoint) {
if let ccHit = arView.hitTest(tapInView, mask: RealityUI.tapGestureMask).first,
let comp = ccHit.entity.components.get(RUITapComponent.self) {
// if the element has RUIComponent, and it has `ruiEnabled` set to false
Expand Down
40 changes: 40 additions & 0 deletions Tests/RealityUITests/RUITextureTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// RUITextureTests.swift
//
//
// Created by Max Cobb on 29/01/2023.
//

import XCTest
import RealityKit
@testable import RealityUI

@available(iOS 15.0, macOS 12, *)
final class RUITextureTests: XCTestCase {


override func setUpWithError() throws {
}

override func tearDownWithError() throws {
}

func testBasicTexture() async throws {
let tex = try await RUITexture.generateTexture(systemName: "pencil", pointSize: 20)
let xcMainThread = XCTestExpectation(description: "main async called")
DispatchQueue.main.async {
XCTAssertEqual(tex.width, 48, accuracy: 1)
XCTAssertEqual(tex.height, 48, accuracy: 1)
xcMainThread.fulfill()
}
await fulfillment(of: [xcMainThread])
}

func testInvalidTexture() async throws {
do {
_ = try await RUITexture.generateTexture(systemName: "|nv@中id")
} catch let err as RUITexture.TextureError {
XCTAssertEqual(err, .invalidSystemName)
}
}
}
21 changes: 21 additions & 0 deletions Tests/RealityUITests/RealityUIUtilityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,25 @@ final class RealityUIUtilityTests: XCTestCase {
XCTAssertEqual(newHasRUI.components.get(RUIComponent.self)?.ruiEnabled, newHasRUI.rui.ruiEnabled)
XCTAssertFalse(newHasRUI.rui.ruiEnabled)
}

func testTapActions() {
let arView = ARView(frame: .init(origin: .zero, size: CGSize(width: 200, height: 200)))
let anch = AnchorEntity(world: .zero)
arView.scene.addAnchor(anch)
let ent = Entity()
ent.components.set(CollisionComponent(shapes: [.generateSphere(radius: 0.5)]))
anch.addChild(ent)

let exception = XCTestExpectation(description: "tap action called")
ent.components.set(RUITapComponent(action: { _, _ in exception.fulfill() }))
RealityUI.shared.tapActionChecker(arView, CGPoint(x: 100, y: 100))
wait(for: [exception])

ent.components.set(RUIComponent(isEnabled: false))
let nonException = XCTestExpectation(description: "tap action not called")
nonException.isInverted = true
ent.components.set(RUITapComponent(action: { _, _ in nonException.fulfill() }))
RealityUI.shared.tapActionChecker(arView, CGPoint(x: 100, y: 100))
wait(for: [nonException], timeout: 0.05)
}
}

0 comments on commit 9afec10

Please sign in to comment.