import {describe, expect, test} from 'vitest'; import {LngLat} from '../geo/lng_lat.ts'; import {LngLatBounds} from '../geo/lng_lat_bounds.ts'; import {createCameraViewState} from './camera_view_state.ts'; import {createCameraContext} from './camera_context.ts'; import {projectCamera} from './project_camera.ts'; import {normalizeCenter, resize, setCenter, setMaxBounds, setZoom} from './camera_updates.ts'; import {CRSWorldCoordinateHelper} from '../geo/projection/crs.ts'; import {createRotatedCrs, createTestCamera} from '../util/test/util.ts'; import type {CameraViewState} from './camera_view_state.ts'; import type {CameraContext} from './camera_context.ts'; function createSizedState(context: CameraContext): CameraViewState { const view = resize(createCameraViewState({renderWorldCopies: true}), context, 200, 200); return setZoom(view, context, 10); } describe('setMaxBounds', () => { test('constrains latitude while leaving longitude unbounded', () => { const context = createCameraContext(); let view = createSizedState(context); view = setMaxBounds(view, context, new LngLatBounds([-Infinity, -50], [Infinity, 65])); view = setCenter(view, context, new LngLat(180, 80)); expect(view.center.lng).toBe(180); expect(view.center.lat).toBeLessThan(65); expect(view.lngRange).toBeNull(); expect(view.latRange).toEqual([-50, 65]); }); test('leaves longitude unbounded when the longitude bounds span 360 degrees', () => { const context = createCameraContext(); let view = createSizedState(context); view = setMaxBounds(view, context, new LngLatBounds([-180, -50], [180, 65])); view = setCenter(view, context, new LngLat(180, 80)); expect(view.zoom).toBe(10); expect(view.center.lng).toBe(180); expect(view.center.lat).toBeLessThan(65); expect(view.lngRange).toEqual([-180, 180]); }); test('rejects one-sided infinite longitude bounds', () => { const context = createCameraContext(); const view = createCameraViewState(); expect(() => setMaxBounds(view, context, new LngLatBounds([-Infinity, -50], [30, 65]))).toThrow( 'Longitude maxBounds values must either be finite or span from -Infinity to Infinity' ); expect(() => setMaxBounds(view, context, new LngLatBounds([-30, -50], [Infinity, 65]))).toThrow( 'Longitude maxBounds values must either be finite or span from -Infinity to Infinity' ); }); test('can constrain longitude without further restricting latitude', () => { const context = createCameraContext(); let view = createSizedState(context); view = setMaxBounds(view, context, new LngLatBounds([-30, -90], [30, 90])); view = setCenter(view, context, new LngLat(80, 80)); expect(view.center.lng).toBeLessThan(30); expect(view.center.lat).toBe(80); }); test('clears bounds when both axes span their valid ranges', () => { const context = createCameraContext(); let view = createCameraViewState(); view = setMaxBounds(view, context, new LngLatBounds([-30, -50], [30, 65])); view = setMaxBounds(view, context, new LngLatBounds([-Infinity, -90], [Infinity, 90])); expect(view).toEqual(createCameraViewState()); }); }); test('derives no projection matrices until the camera has a width and a height', () => { const context = createCameraContext(); const view = createCameraViewState(); expect(projectCamera(resize(view, context, 0, 480, false), context).projectionMatrix).toBeUndefined(); expect(projectCamera(resize(view, context, 640, 0, false), context).projectionMatrix).toBeUndefined(); expect(projectCamera(resize(view, context, 640, 480, false), context).projectionMatrix).toBeDefined(); }); test('normalizeCenter extends the target past the antimeridian when that path is shorter in a wrapping world', () => { const {state: view, context} = createTestCamera({renderWorldCopies: true, width: 200, height: 200, center: [170, 0]}); const target = new LngLat(-170, 0); normalizeCenter(view, context.worldCoordinateHelper, target); expect(target.lng).toBe(190); }); test('normalizeCenter leaves the target alone in a world that does not wrap', () => { const {state: view, context} = createTestCamera({renderWorldCopies: true, width: 200, height: 200, zoom: 4, center: [140, 0]}, {worldCoordinateHelper: new CRSWorldCoordinateHelper(createRotatedCrs())}); const target = new LngLat(-140, 0); normalizeCenter(view, context.worldCoordinateHelper, target); expect(target.lng).toBe(-140); });