diff --git a/Examples/DemoApp/DemoModule/ConversationMessageView.swift b/Examples/DemoApp/DemoModule/ConversationMessageView.swift index a460bb83..2c731d71 100644 --- a/Examples/DemoApp/DemoModule/ConversationMessageView.swift +++ b/Examples/DemoApp/DemoModule/ConversationMessageView.swift @@ -59,22 +59,13 @@ struct ConversationCellView_Previews: PreviewProvider { ConversationCellView(conversation: Conversation( contactName: "John Doe", messagePreview: "Hey, how are you?", - timestamp: Date.current, + timestamp: Date(), unreadCount: 3 )) .previewLayout(.sizeThatFits) } } -extension Date { - static var current: Date { - if ProcessInfo.processInfo.isPreviews { - return Date(timeIntervalSince1970: 682070400) - } - return Date() - } -} - extension ProcessInfo { var isPreviews: Bool { self.environment["EMERGE_IS_RUNNING_FOR_SNAPSHOTS"] == "1" || self.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" diff --git a/Examples/DemoApp/DemoModule/DateView.swift b/Examples/DemoApp/DemoModule/DateView.swift index 1e10df1c..3a002d0a 100644 --- a/Examples/DemoApp/DemoModule/DateView.swift +++ b/Examples/DemoApp/DemoModule/DateView.swift @@ -8,7 +8,7 @@ import SwiftUI struct DateView: View { - let currentDate = Date.current + let currentDate = Date() var body: some View { Text(currentDate, style: .date) diff --git a/Examples/DemoApp/DemoWatchTests/DemoWatchAccessibilityPreviewTest.swift b/Examples/DemoApp/DemoWatchTests/DemoWatchAccessibilityPreviewTest.swift index 77bc6cdd..4568cd58 100644 --- a/Examples/DemoApp/DemoWatchTests/DemoWatchAccessibilityPreviewTest.swift +++ b/Examples/DemoApp/DemoWatchTests/DemoWatchAccessibilityPreviewTest.swift @@ -11,7 +11,7 @@ import XCTest final class DemoWatchAccessibilityPreviewTest: AccessibilityPreviewTest { - override func getApp() -> XCUIApplication { + override class func getApp() -> XCUIApplication { return XCUIApplication() } } diff --git a/Package.swift b/Package.swift index 63cda1eb..d2e4a4bc 100644 --- a/Package.swift +++ b/Package.swift @@ -37,12 +37,13 @@ let package = Package( dependencies: [ .package(url: "https://github.com/swhitty/FlyingFox.git", exact: "0.16.0"), .package(url: "https://github.com/EmergeTools/AccessibilitySnapshot.git", exact: "1.0.2"), + .package(url: "https://github.com/EmergeTools/SimpleDebugger.git", branch: "main"), ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products from dependencies. // Target that provides the XCTest - .target(name: "SnapshottingTestsObjc"), + .target(name: "SnapshottingTestsObjc", dependencies: [.product(name: "SimpleDebugger", package: "SimpleDebugger", condition: .when(platforms: [.iOS, .macOS, .macCatalyst]))]), .target(name: "SnapshottingTests", dependencies: ["SnapshotPreviewsCore", "SnapshottingTestsObjc"]), .target(name: "SnapshotSharedModels"), // Core functionality @@ -59,5 +60,6 @@ let package = Package( .testTarget( name: "SnapshotPreviewsTests", dependencies: ["SnapshotPreviewsCore"]), - ] + ], + cxxLanguageStandard: .cxx11 ) diff --git a/Sources/SnapshotPreviewsCore/ExpandingViewController.swift b/Sources/SnapshotPreviewsCore/ExpandingViewController.swift index 40c7e93e..8f8110cf 100644 --- a/Sources/SnapshotPreviewsCore/ExpandingViewController.swift +++ b/Sources/SnapshotPreviewsCore/ExpandingViewController.swift @@ -20,7 +20,7 @@ public final class ExpandingViewController: UIHostingController Void)? { @@ -110,11 +110,11 @@ public final class ExpandingViewController: UIHostingController= HeightExpansionTimeLimitInSeconds else { + clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) - start >= (HeightExpansionTimeLimitInSeconds * 1_000_000_000) else { return } let timeoutError = RenderingError.expandingViewTimeout(CGSize(width: UIScreen.main.bounds.size.width, diff --git a/Sources/SnapshottingTestsObjc/EMGInvocationCreator.m b/Sources/SnapshottingTestsObjc/EMGInvocationCreator.m deleted file mode 100644 index c31de152..00000000 --- a/Sources/SnapshottingTestsObjc/EMGInvocationCreator.m +++ /dev/null @@ -1,29 +0,0 @@ -// -// EMGInvocationCreator.m -// -// -// Created by Noah Martin on 8/9/24. -// - -#import - -@interface EMGInvocationCreator: NSObject - -+ (NSInvocation *)create:(NSString *)selectorName; - -@end - -@implementation EMGInvocationCreator - -+ (NSInvocation *)create:(NSString *)selectorName { - NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[NSMethodSignature signatureWithObjCTypes:"v@:"]]; - invocation.selector = NSSelectorFromString(selectorName); - return invocation; -} - -+ (void)load { - id previewBaseTest = NSClassFromString(@"EMGPreviewBaseTest"); - [previewBaseTest performSelector:@selector(swizzle:) withObject:[self class]]; -} - -@end diff --git a/Sources/SnapshottingTestsObjc/EMGInvocationCreator.mm b/Sources/SnapshottingTestsObjc/EMGInvocationCreator.mm new file mode 100644 index 00000000..fba331e1 --- /dev/null +++ b/Sources/SnapshottingTestsObjc/EMGInvocationCreator.mm @@ -0,0 +1,72 @@ +// +// EMGInvocationCreator.m +// +// +// Created by Noah Martin on 8/9/24. +// + +#import + +@interface EMGInvocationCreator: NSObject + ++ (NSInvocation *)create:(NSString *)selectorName; + +@end + +@implementation EMGInvocationCreator + ++ (NSInvocation *)create:(NSString *)selectorName { + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[NSMethodSignature signatureWithObjCTypes:"v@:"]]; + invocation.selector = NSSelectorFromString(selectorName); + return invocation; +} + +#if TARGET_OS_TV || TARGET_OS_WATCH || TARGET_OS_VISION || !(defined(__arm64__) || defined(__aarch64__)) + #define EMG_ENABLE_FIX_TIME 0 +#else + #define EMG_ENABLE_FIX_TIME 1 +#endif + +#if EMG_ENABLE_FIX_TIME + +#import +#import +#import +#import + +SimpleDebugger *handler; + +int gettimeofday_new(struct timeval *t, void *a) { + t->tv_sec = 1723532400; + t->tv_usec = 0; + return 0; +} + +void callback(mach_port_t thread, arm_thread_state64_t state, std::function a) { + state.__pc = (__uint64_t) &gettimeofday_new; + thread_set_state(thread, ARM_THREAD_STATE64, (thread_state_t) &state, ARM_THREAD_STATE64_COUNT); + a(false); +} + +#endif + ++ (void)hookTime { +#if EMG_ENABLE_FIX_TIME + vm_address_t a = (vm_address_t) &gettimeofday; + handler = new SimpleDebugger(); + handler->setBreakpoint(a); + handler->setExceptionCallback(&callback); + handler->startDebugging(); +#endif +} + ++ (void)load { + NSDictionary *env = [[NSProcessInfo processInfo] environment]; + if ([[env objectForKey:@"EMERGE_SHOULD_FIX_TIME"] isEqualToString:@"1"]) { + [self hookTime]; + } + id previewBaseTest = NSClassFromString(@"EMGPreviewBaseTest"); + [previewBaseTest performSelector:@selector(swizzle:) withObject:[self class]]; +} + +@end