mln-rs-runtime 0.0.23

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
//! Layers that change after they drew: a property that comes to depend on
//! the zoom, a new place in the style, and a new zoom on a tile the map keeps.

use mln_engine::TileOperation;
use mln_engine::camera::CameraOptions;
use mln_engine::map::MapEvent;
use mln_runtime::{RuntimeEventKind, RuntimeOptions};
use serde_json::json;

use crate::support::still::{StillMap, StillOptions, center_pixel, fixtures_root, ll};
use crate::support::wait;

/// A layer whose color comes to depend on the zoom draws the color of the
/// zoom that the map moves to after.
#[test]
fn a_layer_updated_to_depend_on_the_zoom_follows_it() {
    let map = StillMap::still();
    map.load_json(
        r#"{"version": 8, "sources": {},
            "layers": [{"id": "background", "type": "background", "paint": {"background-color": "red"}}]}"#,
    );
    map.jump(CameraOptions { zoom: Some(0.0), ..Default::default() });
    assert_eq!(center_pixel(&map.render()), [255, 0, 0, 255]);

    let color = json!(["interpolate", ["linear"], ["zoom"], 0, "red", 14, "lime"]);
    wait(map.map.set_style_layer_property("background", "background-color", color.to_string()));
    assert_eq!(center_pixel(&map.render()), [255, 0, 0, 255]);

    map.jump(CameraOptions { zoom: Some(14.0), ..Default::default() });
    assert_eq!(center_pixel(&map.render()), [0, 255, 0, 255]);
}

/// A still map over the streets fixtures, around them: their water as a
/// fill and as a wide line, in `order`, on a background.
fn streets_map(order: [&str; 2]) -> StillMap {
    let map = StillMap::new(StillOptions {
        runtime_options: RuntimeOptions {
            asset_path: Some(fixtures_root().join("test/fixtures/api/assets")),
            ..RuntimeOptions::default()
        },
        ..StillOptions::default()
    });
    let water = |id: &str| {
        let paint = match id {
            "fill" => json!({"fill-color": "blue"}),
            _ => json!({"line-color": "red", "line-width": 12}),
        };
        json!({"id": id, "type": id, "source": "mapbox", "source-layer": "water", "paint": paint})
    };
    let style = json!({
        "version": 8,
        "sources": {"mapbox": {"type": "vector", "tiles": ["asset://streets/{z}-{x}-{y}.vector.pbf"]}},
        "layers": [{"id": "background", "type": "background", "paint": {"background-color": "white"}},
            water(order[0]), water(order[1])],
    });
    map.load_json(&style.to_string());
    map.jump(CameraOptions { center: Some(ll(37.8, -122.5)), zoom: Some(10.0), ..Default::default() });
    map
}

/// How many tiles the map started to parse after the first `from` of its
/// events.
fn parses_since(map: &StillMap, from: usize) -> usize {
    map.events.events()[from..]
        .iter()
        .filter(|event| {
            matches!(event.kind, RuntimeEventKind::Map(MapEvent::TileAction { op: TileOperation::StartParse, .. }))
        })
        .count()
}

/// A moved layer draws in its new place without laying its tiles out again,
/// as a style with the layers in that order draws them.
#[test]
fn a_moved_layer_draws_in_its_new_place_without_a_relayout() {
    let map = streets_map(["fill", "line"]);
    let before = map.render();
    assert!(parses_since(&map, 0) > 0);

    let from = map.events.events().len();
    wait(map.map.move_style_layer("line", Some("fill".to_owned())));
    let moved = map.render();
    assert_eq!(0, parses_since(&map, from), "moving a layer laid its tiles out again");
    assert!(before != moved, "the moved layer draws where it did");
    assert!(
        streets_map(["line", "fill"]).render() == moved,
        "the moved layer draws otherwise than a layer in its place"
    );
}

/// A dashed line whose dash array steps at a zoom between two draws of the
/// same tile shows the dashes of the zoom it draws at, as a map that starts
/// at that zoom does.
#[test]
fn a_line_shows_the_dashes_of_its_zoom_on_a_kept_tile() {
    let style = r#"{
      "version": 8,
      "sources": {"line": {"type": "geojson", "data": {"type": "LineString", "coordinates": [[-40, 0], [40, 0]]}}},
      "layers": [
        {"id": "background", "type": "background", "paint": {"background-color": "white"}},
        {"id": "line", "type": "line", "source": "line", "paint": {
          "line-width": 8,
          "line-dasharray": ["step", ["zoom"], ["literal", [1, 1]], 2.5, ["literal", [4, 1]]]
        }}
      ]
    }"#;
    let render_at = |map: &StillMap, zoom: f64| {
        map.jump(CameraOptions { center: Some(ll(0.0, 0.0)), zoom: Some(zoom), ..Default::default() });
        map.render()
    };

    let zoomed = StillMap::still();
    zoomed.load_json(style);
    render_at(&zoomed, 2.2);
    let zoomed = render_at(&zoomed, 2.8);

    let fresh = StillMap::still();
    fresh.load_json(style);
    let fresh = render_at(&fresh, 2.8);

    assert!(zoomed.data == fresh.data, "the zoomed map draws other dashes than a map that starts at its zoom");
}