|
| 1 | +from concurrent.futures import Future |
| 2 | +from playwright.sync_api import Page, Position |
| 3 | +from IPython.display import display |
| 4 | + |
| 5 | +from ipyleaflet import ( |
| 6 | + Map, |
| 7 | + DrawControl, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +def future_trait_change(widget, attribute, timeout=2000): |
| 12 | + future = Future() |
| 13 | + |
| 14 | + def on_change(change): |
| 15 | + value = change["new"] |
| 16 | + future.set_result(value) |
| 17 | + |
| 18 | + widget.observe(on_change, attribute) |
| 19 | + return future |
| 20 | + |
| 21 | + |
| 22 | +def test_draw_basics(solara_test, page_session: Page, assert_solara_snapshot): |
| 23 | + # this all runs in process, which only works with solara |
| 24 | + # also, this test is only with pure ipywidgets |
| 25 | + center = [34.6252978589571, -77.34580993652344] |
| 26 | + zoom = 10 |
| 27 | + |
| 28 | + m = Map(center=center, zoom=zoom) |
| 29 | + |
| 30 | + dc = DrawControl( |
| 31 | + marker={"shapeOptions": {"color": "#0000FF"}}, |
| 32 | + rectangle={"shapeOptions": {"color": "#00FF00"}}, |
| 33 | + circle={"shapeOptions": {"color": "#FF0000"}}, |
| 34 | + circlemarker={}, |
| 35 | + ) |
| 36 | + m.add(dc) |
| 37 | + |
| 38 | + display(m) |
| 39 | + |
| 40 | + last_draw_future = future_trait_change(dc, "last_draw") |
| 41 | + |
| 42 | + # click around to create a rectangle |
| 43 | + leaflet = page_session.locator(".leaflet-container") |
| 44 | + leaflet.locator(".leaflet-draw-draw-polygon").click() |
| 45 | + leaflet.wait_for() |
| 46 | + x_offset = 200 |
| 47 | + leaflet.click(position=Position(x=x_offset + 100, y=100)) |
| 48 | + page_session.wait_for_timeout(100) # too fast, and it removes to wrong point |
| 49 | + leaflet.click(position=Position(x=x_offset + 200, y=100)) |
| 50 | + page_session.wait_for_timeout(100) |
| 51 | + leaflet.click(position=Position(x=x_offset + 300, y=150)) # lets remove this |
| 52 | + page_session.wait_for_timeout(100) |
| 53 | + leaflet.locator("text=Delete last point").click() |
| 54 | + leaflet.click(position=Position(x=x_offset + 200, y=200)) |
| 55 | + page_session.wait_for_timeout(100) |
| 56 | + leaflet.click(position=Position(x=x_offset + 100, y=200)) |
| 57 | + leaflet.locator("text=Finish").click() |
| 58 | + |
| 59 | + # solara runs in a different thread, so we can block the pytest thread |
| 60 | + last_draw = last_draw_future.result(timeout=1) |
| 61 | + assert last_draw["type"] == "Feature" |
| 62 | + geometry = last_draw["geometry"] |
| 63 | + coordinates = geometry["coordinates"] |
| 64 | + assert len(coordinates) == 1 |
| 65 | + assert len(coordinates[0]) == 5 |
| 66 | + assert len(coordinates[0][0]) == 2 |
| 67 | + assert coordinates[0][0] == coordinates[0][-1] |
| 68 | + assert coordinates[0][0] != coordinates[0][1] |
0 commit comments