Skip to content

Commit

Permalink
Used fixed time (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahsmartin authored Feb 4, 2025
1 parent 694bec0 commit a90510d
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 47 deletions.
11 changes: 1 addition & 10 deletions Examples/DemoApp/DemoModule/ConversationMessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion Examples/DemoApp/DemoModule/DateView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct DateView: View {
let currentDate = Date.current
let currentDate = Date()

var body: some View {
Text(currentDate, style: .date)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import XCTest

final class DemoWatchAccessibilityPreviewTest: AccessibilityPreviewTest {

override func getApp() -> XCUIApplication {
override class func getApp() -> XCUIApplication {
return XCUIApplication()
}
}
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -59,5 +60,6 @@ let package = Package(
.testTarget(
name: "SnapshotPreviewsTests",
dependencies: ["SnapshotPreviewsCore"]),
]
],
cxxLanguageStandard: .cxx11
)
8 changes: 4 additions & 4 deletions Sources/SnapshotPreviewsCore/ExpandingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public final class ExpandingViewController: UIHostingController<EmergeModifierVi
rootView.supportsExpansion
}

private let HeightExpansionTimeLimitInSeconds: Double = 30
private let HeightExpansionTimeLimitInSeconds: UInt64 = 30

private var didCall = false
var previousHeight: CGFloat?

var heightAnchor: NSLayoutConstraint?
private var widthAnchor: NSLayoutConstraint?

private var startTime: Date?
private var startTime: UInt64?
private var timer: Timer?

public var expansionSettled: ((EmergeRenderingMode?, Float?, Bool?, Bool?, Error?) -> Void)? {
Expand Down Expand Up @@ -110,11 +110,11 @@ public final class ExpandingViewController: UIHostingController<EmergeModifierVi
print("Timer already exists")
return
}
startTime = Date()
startTime = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW)
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
guard let self,
let start = startTime,
Date().timeIntervalSince(start) >= 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,
Expand Down
29 changes: 0 additions & 29 deletions Sources/SnapshottingTestsObjc/EMGInvocationCreator.m

This file was deleted.

72 changes: 72 additions & 0 deletions Sources/SnapshottingTestsObjc/EMGInvocationCreator.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// EMGInvocationCreator.m
//
//
// Created by Noah Martin on 8/9/24.
//

#import <Foundation/Foundation.h>

@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 <sys/time.h>
#import <mach/thread_status.h>
#import <mach/vm_types.h>
#import <SimpleDebugger.h>

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<void(bool removeBreak)> 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<NSString *, NSString *> *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

0 comments on commit a90510d

Please sign in to comment.