summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 01:55:22 -0300
committerGabriel Schneider <[email protected]>2026-11-10 01:55:32 -0300
commit75db3e84e24ee75626074d93927261249b646386 (patch)
treef794df2c86801e23191f4c8ef08d40106bd22d85 /test
parent428898ae93543ab47f977b2f6f287a6bb9287625 (diff)
downloadpardes-75db3e84e24ee75626074d93927261249b646386.tar.gz
pardes-75db3e84e24ee75626074d93927261249b646386.zip
macos: the AppKit trackpad adapter for the SDL window (upstream a41360e3's Trackpad.zig, macos_trackpad.{h,m} and their test as they were), its gui.zig hooks on today's names (pointAt, view().jumps and active, one setFontPx for a key's step and a pinch) and the build's shim per gui module with macos-trackpad-test registered once after the loop, all gated to macOS so a Linux touchpad's pinch and the goldens stay as they were
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'test')
-rw-r--r--test/macos_trackpad.m165
1 files changed, 165 insertions, 0 deletions
diff --git a/test/macos_trackpad.m b/test/macos_trackpad.m
new file mode 100644
index 00000000..03c9d311
--- /dev/null
+++ b/test/macos_trackpad.m
@@ -0,0 +1,165 @@
+/* Exercise the real AppKit adapter without a window or a physical trackpad.
+ * Only SDL's event queue and window lookup are replaced with test doubles. */
+#import <Cocoa/Cocoa.h>
+#include "../src/gui/macos_trackpad.m"
+#include <assert.h>
+
+static id native_window;
+static SDL_Event queued[32];
+static unsigned count;
+SDL_PropertiesID SDL_GetWindowProperties(SDL_Window *window) { (void)window; return 1; }
+void *SDL_GetPointerProperty(SDL_PropertiesID props, const char *name, void *fallback)
+{ (void)props; (void)name; (void)fallback; return (__bridge void *)native_window; }
+Uint32 SDL_RegisterEvents(int n) { (void)n; return SDL_EVENT_USER; }
+Uint64 SDL_GetTicksNS(void) { return 100; }
+SDL_WindowID SDL_GetWindowID(SDL_Window *window) { (void)window; return 42; }
+bool SDL_PushEvent(SDL_Event *event) { assert(count < 32); queued[count++] = *event; return true; }
+
+@interface FakeNativeWindow : NSObject
+@property NSView *contentView;
+@end
+@implementation FakeNativeWindow
+@end
+
+@interface HapticSpy : NSObject <NSHapticFeedbackPerformer>
+@property NSUInteger pulses;
+@end
+@implementation HapticSpy
+- (void)performFeedbackPattern:(NSHapticFeedbackPattern)pattern performanceTime:(NSHapticFeedbackPerformanceTime)time
+{
+ assert(pattern == NSHapticFeedbackPatternAlignment);
+ assert(time == NSHapticFeedbackPerformanceTimeNow);
+ self.pulses++;
+}
+@end
+static HapticSpy *haptic_spy;
+static id fake_performer(id receiver, SEL selector)
+{ (void)receiver; (void)selector; return haptic_spy; }
+static BOOL swipe_enabled(id receiver, SEL selector)
+{ (void)receiver; (void)selector; return YES; }
+
+@interface TrackpadEvent : NSEvent
+@property NSUInteger fingerCount;
+@property NSEventPhase testPhase;
+@property BOOL precise;
+@property BOOL horizontal, cancelled;
+@property CGFloat progress, discreteDirection;
+@end
+@implementation TrackpadEvent
+- (NSPoint)locationInWindow { return NSMakePoint(25, 40); }
+- (NSEventPhase)phase { return self.testPhase; }
+- (BOOL)hasPreciseScrollingDeltas { return self.precise; }
+- (NSEventPhase)momentumPhase { return NSEventPhaseNone; }
+- (NSEventModifierFlags)modifierFlags { return 0; }
+- (CGFloat)deltaX { return self.discreteDirection; }
+- (CGFloat)scrollingDeltaX { return self.horizontal ? 20 : 3; }
+- (CGFloat)scrollingDeltaY { return self.horizontal ? 1 : 20; }
+- (void)trackSwipeEventWithOptions:(NSEventSwipeTrackingOptions)options
+ dampenAmountThresholdMin:(CGFloat)min max:(CGFloat)max
+ usingHandler:(void (^)(CGFloat, NSEventPhase, BOOL, BOOL *))handler
+{
+ (void)options; assert(min == -1 && max == 1);
+ BOOL stop = NO;
+ handler(0.13, NSEventPhaseChanged, NO, &stop);
+ handler(0.18, NSEventPhaseChanged, NO, &stop);
+ if (self.cancelled) handler(self.progress, NSEventPhaseCancelled, NO, &stop);
+ else handler(self.progress, NSEventPhaseChanged, NO, &stop);
+ if (!stop) handler(0, NSEventPhaseEnded, NO, &stop);
+ if (!stop) handler(1, NSEventPhaseNone, YES, &stop);
+}
+- (NSInteger)stage { return 2; }
+- (float)rotation { return 12; }
+- (CGFloat)magnification { return 0.25; }
+- (NSSet<NSTouch *> *)touchesMatchingPhase:(NSTouchPhase)phase inView:(NSView *)view
+{
+ (void)phase; (void)view;
+ NSMutableSet *result = [NSMutableSet set];
+ for (NSUInteger i = 0; i < self.fingerCount; ++i) [result addObject:[NSObject new]];
+ return (NSSet<NSTouch *> *)result;
+}
+@end
+
+int main(void)
+{
+ @autoreleasepool {
+ haptic_spy = [HapticSpy new];
+ Method performer = class_getClassMethod([NSHapticFeedbackManager class], @selector(defaultPerformer));
+ IMP original = method_setImplementation(performer, (IMP)fake_performer);
+ FakeNativeWindow *window = [FakeNativeWindow new];
+ NSView *view = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 400, 300)];
+ window.contentView = view;
+ native_window = window;
+ ui_macos_trackpad_attach((SDL_Window *)1);
+ assert([NSStringFromClass(view.class) isEqualToString:@"PardesSDLTrackpadView"]);
+ assert(view.wantsRestingTouches);
+ TrackpadEvent *event = [TrackpadEvent new];
+ event.fingerCount = 3;
+ [view touchesBeganWithEvent:event];
+ UITrackpadEvent *packet = (UITrackpadEvent *)&queued[0];
+ assert(packet->kind == UI_TRACKPAD_FINGERS && packet->fingers == 3);
+ assert(packet->windowID == 42 && packet->x == 25 && packet->y == 260);
+ event.precise = YES;
+ event.testPhase = NSEventPhaseBegan;
+ [view scrollWheel:event];
+ packet = (UITrackpadEvent *)&queued[1];
+ assert(packet->kind == UI_TRACKPAD_SCROLL && packet->a == 3 && packet->b == 20 && packet->phase == 1);
+ // System momentum has no finger phase. It must keep reaching SDL.
+ event.testPhase = NSEventPhaseNone;
+ [view scrollWheel:event];
+ assert(((UITrackpadEvent *)&queued[2])->kind == UI_TRACKPAD_SCROLL);
+ [view pressureChangeWithEvent:event];
+ assert(((UITrackpadEvent *)&queued[3])->kind == UI_TRACKPAD_PRESSURE);
+ assert(((UITrackpadEvent *)&queued[3])->a == 2);
+ [view rotateWithEvent:event];
+ assert(((UITrackpadEvent *)&queued[4])->kind == UI_TRACKPAD_ROTATE);
+ assert(((UITrackpadEvent *)&queued[4])->a == 12);
+ [view magnifyWithEvent:event];
+ assert(((UITrackpadEvent *)&queued[5])->kind == UI_TRACKPAD_PINCH);
+ assert(((UITrackpadEvent *)&queued[5])->a == 1.25);
+ event.discreteDirection = -1;
+ [view swipeWithEvent:event];
+ assert(((UITrackpadEvent *)&queued[6])->kind == UI_TRACKPAD_HISTORY);
+ assert(((UITrackpadEvent *)&queued[6])->a == -1);
+ Method preference = class_getClassMethod([NSEvent class], @selector(isSwipeTrackingFromScrollEventsEnabled));
+ IMP old_preference = method_setImplementation(preference, (IMP)swipe_enabled);
+ event.horizontal = YES;
+ event.progress = 0.25;
+ event.testPhase = NSEventPhaseBegan;
+ [view scrollWheel:event];
+ assert(count == 8);
+ assert(haptic_spy.pulses == 1); // one early cue despite multiple progress events
+ assert(((UITrackpadEvent *)&queued[7])->kind == UI_TRACKPAD_HISTORY);
+ assert(((UITrackpadEvent *)&queued[7])->a == 1);
+ event.cancelled = YES;
+ event.progress = 1; // even later completion cannot undo cancellation
+ [view scrollWheel:event];
+ assert(count == 8); // a cancelled swipe never navigates
+ event.cancelled = NO;
+ event.progress = 0.20;
+ [view scrollWheel:event];
+ assert(count == 8); // below threshold never commits on release/animation
+ event.horizontal = NO;
+ [view scrollWheel:event];
+ event.horizontal = YES;
+ event.testPhase = NSEventPhaseChanged;
+ [view scrollWheel:event];
+ assert(((UITrackpadEvent *)&queued[9])->kind == UI_TRACKPAD_SCROLL);
+ event.testPhase = NSEventPhaseBegan;
+ event.progress = -0.25;
+ [view scrollWheel:event];
+ assert(count == 11);
+ assert(((UITrackpadEvent *)&queued[10])->a == -1);
+ event.testPhase = NSEventPhaseChanged;
+ [view scrollWheel:event];
+ event.testPhase = NSEventPhaseNone;
+ [view scrollWheel:event];
+ assert(count == 11); // remaining movement/momentum cannot jump twice
+ method_setImplementation(preference, old_preference);
+ NSUInteger before = haptic_spy.pulses;
+ ui_macos_trackpad_haptic();
+ method_setImplementation(performer, original);
+ assert(haptic_spy.pulses == before + 1);
+ puts("PASS native SDL trackpad adapter: touches, momentum, pressure, rotation, pinch, history/cancellation, haptics");
+ }
+ return 0;
+}