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;
#[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]);
}
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
}
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()
}
#[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"
);
}
#[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");
}