Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/facett

facett-map/tests/zoom_feel_is_one_number.rs

//! **The wheel feels the same in every pane** — lane ST, gfx3d-S10.
//!
//! `MapView::zoom_by_scroll` divided scroll pixels by a function-local
//! `const PX_PER_ZOOM_LEVEL: f32 = 120.0`, and korp's 3D wheel handler
//! (`map_views.rs`) divided by `50.0` — under a comment claiming *"a zoom LEVEL per
//! notch is the same feel the flat pane has"*. It was **2.4× faster per notch**: two
//! panes of one application responding differently to the same gesture, while the code
//! said they did not. The clamp bounds were a second copy too, written out at both
//! sites.
//!
//! The three numbers are `pub const` now, so korp reads them instead of holding its own.
//! This asserts they are what the widget actually applies — a `pub const` nothing reads
//! is just a third copy.

use facett_map::layer::{LatLon, MapView, MAX_ZOOM, MIN_ZOOM, PX_PER_ZOOM_LEVEL};

fn view(zoom: f32) -> MapView {
    MapView::new(LatLon::new(59.33, 18.07), zoom, (1600.0, 1000.0))
}

/// **One `PX_PER_ZOOM_LEVEL` of scroll moves exactly one zoom level.** That is the
/// sentence the constant's name makes, asserted against the widget rather than assumed.
#[test]
fn one_constants_worth_of_scroll_is_exactly_one_zoom_level() {
    let mut v = view(10.0);
    v.zoom_by_scroll(PX_PER_ZOOM_LEVEL);
    assert!(
        (v.zoom - 11.0).abs() < 1e-5,
        "{PX_PER_ZOOM_LEVEL} px of scroll must be one level: 10.0 → {}",
        v.zoom
    );

    // …and half of it is half a level, so the relationship is linear and not a step.
    let mut v = view(10.0);
    v.zoom_by_scroll(PX_PER_ZOOM_LEVEL * 0.5);
    assert!((v.zoom - 10.5).abs() < 1e-5, "half a notch is half a level: {}", v.zoom);

    // Negative scroll zooms OUT by the same measure — a wheel that is not symmetric is
    // a wheel that drifts on every up-down pair.
    let mut v = view(10.0);
    v.zoom_by_scroll(-PX_PER_ZOOM_LEVEL);
    assert!((v.zoom - 9.0).abs() < 1e-5, "one notch out is one level: {}", v.zoom);
}

/// **The exported clamp bounds are the ones the widget applies.** Both were written out
/// as literals at korp's call site as well, so this is the assertion that makes the
/// single copy real.
#[test]
fn the_exported_clamp_bounds_are_the_ones_the_widget_applies() {
    // Far past the deep end: it stops AT `MAX_ZOOM`, not near it.
    let mut v = view(MAX_ZOOM - 0.5);
    v.zoom_by_scroll(PX_PER_ZOOM_LEVEL * 100.0);
    assert!((v.zoom - MAX_ZOOM).abs() < 1e-5, "clamped to MAX_ZOOM: {}", v.zoom);

    // …and at the shallow end.
    let mut v = view(MIN_ZOOM + 0.5);
    v.zoom_by_scroll(-PX_PER_ZOOM_LEVEL * 100.0);
    assert!((v.zoom - MIN_ZOOM).abs() < 1e-5, "clamped to MIN_ZOOM: {}", v.zoom);

    // The premise: the bounds are a real range, so the clamps above are not both
    // landing on the same number.
    const _: () = assert!(MAX_ZOOM > MIN_ZOOM + 1.0, "MAX_ZOOM must sit more than one level above MIN_ZOOM");
}

/// **The measurement that names the defect**, kept so nobody re-derives it: `50.0` — the
/// divisor korp used — is not `PX_PER_ZOOM_LEVEL`, and the ratio is the 2.4× the two
/// panes differed by.
#[test]
fn the_divisor_korp_used_was_a_different_number() {
    const KORP_OLD_DIVISOR: f32 = 50.0;
    assert!(
        (PX_PER_ZOOM_LEVEL - KORP_OLD_DIVISOR).abs() > 1.0,
        "if these ever agree this row is moot: {PX_PER_ZOOM_LEVEL} vs {KORP_OLD_DIVISOR}"
    );
    let ratio = PX_PER_ZOOM_LEVEL / KORP_OLD_DIVISOR;
    assert!(
        (ratio - 2.4).abs() < 0.05,
        "the measured drift was 2.4× faster per notch in korp's 3D pane, got {ratio:.2}×"
    );
}