|
| 1 | +// |
| 2 | +// MParticleSceneDelegateTests.swift |
| 3 | +// mParticle-Apple-SDK |
| 4 | +// |
| 5 | +// Created by Brandon Stalnaker on 11/13/25. |
| 6 | +// |
| 7 | + |
| 8 | +#if MPARTICLE_LOCATION_DISABLE |
| 9 | +import mParticle_Apple_SDK_NoLocation |
| 10 | +#else |
| 11 | +import mParticle_Apple_SDK |
| 12 | +#endif |
| 13 | +import XCTest |
| 14 | + |
| 15 | +@available(iOS 13.0, *) |
| 16 | +final class MParticleSceneDelegateTests: MParticleTestBase { |
| 17 | + |
| 18 | + // MARK: - Properties |
| 19 | + |
| 20 | + var testURL: URL! |
| 21 | + var testUserActivity: NSUserActivity! |
| 22 | + |
| 23 | + override func setUp() { |
| 24 | + super.setUp() |
| 25 | + testURL = URL(string: "myapp://test/path?param=value")! |
| 26 | + testUserActivity = NSUserActivity(activityType: "com.test.activity") |
| 27 | + testUserActivity.title = "Test Activity" |
| 28 | + testUserActivity.userInfo = ["key": "value"] |
| 29 | + |
| 30 | + // The implementation calls [MParticle sharedInstance], so we need to set the mock on the shared instance |
| 31 | + MParticle.sharedInstance().appNotificationHandler = appNotificationHandler |
| 32 | + |
| 33 | + // Reset mock state for each test |
| 34 | + appNotificationHandler.continueUserActivityCalled = false |
| 35 | + appNotificationHandler.continueUserActivityUserActivityParam = nil |
| 36 | + appNotificationHandler.continueUserActivityRestorationHandlerParam = nil |
| 37 | + appNotificationHandler.openURLWithOptionsCalled = false |
| 38 | + appNotificationHandler.openURLWithOptionsURLParam = nil |
| 39 | + appNotificationHandler.openURLWithOptionsOptionsParam = nil |
| 40 | + } |
| 41 | + |
| 42 | + // MARK: - Method Existence Tests |
| 43 | + |
| 44 | + func test_handleURLContext_methodExists() { |
| 45 | + // Verify the method exists with correct signature |
| 46 | + XCTAssertTrue(mparticle.responds(to: #selector(mparticle.handleURLContext(_:)))) |
| 47 | + } |
| 48 | + |
| 49 | + func test_handleUserActivity_methodExists() { |
| 50 | + // Verify the method exists with correct signature |
| 51 | + XCTAssertTrue(mparticle.responds(to: #selector(mparticle.handleUserActivity(_:)))) |
| 52 | + } |
| 53 | + |
| 54 | + // MARK: - handleURLContext Tests |
| 55 | + // Note: Testing with mock URLContext is complex due to system class limitations |
| 56 | + // These tests focus on method availability and basic functionality |
| 57 | + |
| 58 | + func test_handleURLContext_availabilityCheck() { |
| 59 | + // Verify the method is only available on iOS 13+ |
| 60 | + if #available(iOS 13.0, *) { |
| 61 | + XCTAssertTrue(mparticle.responds(to: #selector(mparticle.handleURLContext(_:)))) |
| 62 | + } else { |
| 63 | + XCTAssertFalse(mparticle.responds(to: #selector(mparticle.handleURLContext(_:)))) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // MARK: - handleUserActivity Tests |
| 68 | + |
| 69 | + func test_handleUserActivity_invokesAppNotificationHandler() { |
| 70 | + // Act |
| 71 | + mparticle.handleUserActivity(testUserActivity) |
| 72 | + |
| 73 | + // Assert - handleUserActivity directly calls the app notification handler |
| 74 | + XCTAssertTrue(appNotificationHandler.continueUserActivityCalled) |
| 75 | + XCTAssertEqual(appNotificationHandler.continueUserActivityUserActivityParam, testUserActivity) |
| 76 | + XCTAssertNotNil(appNotificationHandler.continueUserActivityRestorationHandlerParam) |
| 77 | + } |
| 78 | + |
| 79 | + func test_handleUserActivity_withWebBrowsingActivity() { |
| 80 | + // Arrange |
| 81 | + let webActivity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb) |
| 82 | + webActivity.title = "Web Page" |
| 83 | + webActivity.webpageURL = URL(string: "https://example.com/page") |
| 84 | + |
| 85 | + // Act |
| 86 | + mparticle.handleUserActivity(webActivity) |
| 87 | + |
| 88 | + // Assert - Direct call to app notification handler |
| 89 | + XCTAssertTrue(appNotificationHandler.continueUserActivityCalled) |
| 90 | + XCTAssertEqual(appNotificationHandler.continueUserActivityUserActivityParam, webActivity) |
| 91 | + } |
| 92 | + |
| 93 | + func test_handleUserActivity_restorationHandlerIsEmpty() { |
| 94 | + // Act |
| 95 | + mparticle.handleUserActivity(testUserActivity) |
| 96 | + |
| 97 | + // Assert |
| 98 | + XCTAssertTrue(appNotificationHandler.continueUserActivityCalled) |
| 99 | + |
| 100 | + // Verify the restoration handler is provided and safe to call |
| 101 | + let restorationHandler = appNotificationHandler.continueUserActivityRestorationHandlerParam |
| 102 | + XCTAssertNotNil(restorationHandler) |
| 103 | + |
| 104 | + // Test that calling the restoration handler doesn't crash |
| 105 | + XCTAssertNoThrow(restorationHandler?(nil)) |
| 106 | + XCTAssertNoThrow(restorationHandler?([])) |
| 107 | + } |
| 108 | + |
| 109 | + // MARK: - iOS Version Availability Tests |
| 110 | + |
| 111 | + func test_handleUserActivity_alwaysAvailable() { |
| 112 | + // handleUserActivity should be available on all iOS versions |
| 113 | + XCTAssertTrue(mparticle.responds(to: #selector(mparticle.handleUserActivity(_:)))) |
| 114 | + } |
| 115 | + |
| 116 | + // MARK: - Integration Tests |
| 117 | + |
| 118 | + func test_bothMethods_exist() { |
| 119 | + // Verify both SceneDelegate support methods exist |
| 120 | + XCTAssertTrue(mparticle.responds(to: #selector(mparticle.handleURLContext(_:)))) |
| 121 | + XCTAssertTrue(mparticle.responds(to: #selector(mparticle.handleUserActivity(_:)))) |
| 122 | + } |
| 123 | +} |
0 commit comments