|
| 1 | +#if canImport(Testing) |
| 2 | + |
| 3 | +import Testing |
| 4 | +import Foundation |
| 5 | +#if canImport(CoreGraphics) |
| 6 | +import CoreGraphics |
| 7 | +#endif |
| 8 | + |
| 9 | +import GeoJSONKit |
| 10 | +@testable import GeoDrawer |
| 11 | +@testable import GeoProjector |
| 12 | + |
| 13 | +struct GeoDrawerSVGZoomTests { |
| 14 | + |
| 15 | + @Test func testSVGWithoutZoom() throws { |
| 16 | + let drawer = GeoDrawer( |
| 17 | + size: .init(width: 400, height: 200), |
| 18 | + projection: Projections.Equirectangular() |
| 19 | + ) |
| 20 | + |
| 21 | + let line = GeoJSON.LineString(positions: [ |
| 22 | + .init(latitude: 40, longitude: -120), // West coast US |
| 23 | + .init(latitude: 40, longitude: -80) // East coast US |
| 24 | + ]) |
| 25 | + |
| 26 | + let content: [GeoDrawer.Content] = [ |
| 27 | + .line(line, stroke: testColor(red: 1, green: 0, blue: 0), strokeWidth: 2) |
| 28 | + ] |
| 29 | + |
| 30 | + let svg = drawer.drawSVG(content) |
| 31 | + |
| 32 | + // Just verify the basic structure and that it contains the expected line |
| 33 | + #expect(svg.contains("<svg")) |
| 34 | + #expect(svg.contains("<path")) |
| 35 | + #expect(svg.contains("stroke=\"#FF0000\"")) |
| 36 | + #expect(svg.contains("d=\"M 66.7 55.6 L 111.1 55.6\"")) |
| 37 | + } |
| 38 | + |
| 39 | + @Test func testSVGWithZoom() throws { |
| 40 | + // Create a bounding box for North America |
| 41 | + let northAmericaBbox = GeoJSON.BoundingBox(positions: [ |
| 42 | + .init(latitude: 10.0, longitude: -170.0), |
| 43 | + .init(latitude: 70.0, longitude: -50.0) |
| 44 | + ]) |
| 45 | + |
| 46 | + let drawer = GeoDrawer( |
| 47 | + size: .init(width: 400, height: 200), |
| 48 | + projection: Projections.Equirectangular(), |
| 49 | + zoomTo: northAmericaBbox |
| 50 | + ) |
| 51 | + |
| 52 | + let line = GeoJSON.LineString(positions: [ |
| 53 | + .init(latitude: 40, longitude: -120), // West coast US |
| 54 | + .init(latitude: 40, longitude: -80) // East coast US |
| 55 | + ]) |
| 56 | + |
| 57 | + let content: [GeoDrawer.Content] = [ |
| 58 | + .line(line, stroke: testColor(red: 1, green: 0, blue: 0), strokeWidth: 2) |
| 59 | + ] |
| 60 | + |
| 61 | + let svg = drawer.drawSVG(content) |
| 62 | + |
| 63 | + // With zoom to North America, the line should span a much larger portion of the width |
| 64 | + // The exact coordinates will depend on the zoom calculation, but it should be noticeably different |
| 65 | + // print("SVG with zoom: \(svg)") |
| 66 | + |
| 67 | + // For now, just check that we get a valid SVG with a path element |
| 68 | + #expect(svg.contains("<svg")) |
| 69 | + #expect(svg.contains("<path")) |
| 70 | + #expect(svg.contains("stroke=\"#FF0000\"")) |
| 71 | + } |
| 72 | + |
| 73 | + @Test func testZoomActuallyChangesOutput() throws { |
| 74 | + let projection = Projections.Equirectangular() |
| 75 | + let size = Size(width: 400, height: 200) |
| 76 | + |
| 77 | + // Create a bounding box for North America |
| 78 | + let northAmericaBbox = GeoJSON.BoundingBox(positions: [ |
| 79 | + .init(latitude: 10.0, longitude: -170.0), |
| 80 | + .init(latitude: 70.0, longitude: -50.0) |
| 81 | + ]) |
| 82 | + |
| 83 | + let drawerNoZoom = GeoDrawer(size: size, projection: projection) |
| 84 | + let drawerWithZoom = GeoDrawer(size: size, projection: projection, zoomTo: northAmericaBbox) |
| 85 | + |
| 86 | + let line = GeoJSON.LineString(positions: [ |
| 87 | + .init(latitude: 40, longitude: -120), // West coast US |
| 88 | + .init(latitude: 40, longitude: -80) // East coast US |
| 89 | + ]) |
| 90 | + |
| 91 | + let content: [GeoDrawer.Content] = [ |
| 92 | + .line(line, stroke: testColor(red: 1, green: 0, blue: 0), strokeWidth: 2) |
| 93 | + ] |
| 94 | + |
| 95 | + let svgNoZoom = drawerNoZoom.drawSVG(content) |
| 96 | + let svgWithZoom = drawerWithZoom.drawSVG(content) |
| 97 | + |
| 98 | + // print("SVG without zoom: \(svgNoZoom)") |
| 99 | + // print("SVG with zoom: \(svgWithZoom)") |
| 100 | + |
| 101 | + // The SVGs should be different if zoom is working correctly |
| 102 | + #expect(svgNoZoom != svgWithZoom, "SVG output should be different when zoom is applied") |
| 103 | + } |
| 104 | + |
| 105 | + @Test func testMapBackgroundWithZoom() throws { |
| 106 | + let northAmericaBbox = GeoJSON.BoundingBox(positions: [ |
| 107 | + .init(latitude: 10.0, longitude: -170.0), |
| 108 | + .init(latitude: 70.0, longitude: -50.0) |
| 109 | + ]) |
| 110 | + |
| 111 | + let drawer = GeoDrawer( |
| 112 | + size: .init(width: 400, height: 200), |
| 113 | + projection: Projections.Equirectangular(), |
| 114 | + zoomTo: northAmericaBbox |
| 115 | + ) |
| 116 | + |
| 117 | + let svg = drawer.drawSVG( |
| 118 | + [], |
| 119 | + mapBackground: testColor(red: 0.8, green: 0.8, blue: 1.0), |
| 120 | + mapOutline: testColor(red: 0, green: 0, blue: 0) |
| 121 | + ) |
| 122 | + |
| 123 | + // The map background should respect zoom settings |
| 124 | + #expect(svg.contains("<svg")) |
| 125 | + #expect(svg.contains("<rect")) |
| 126 | + #expect(svg.contains("fill=\"#CCCCFF\"")) |
| 127 | + #expect(svg.contains("stroke=\"#000000\"")) |
| 128 | + |
| 129 | + // print("Map background with zoom: \(svg)") |
| 130 | + } |
| 131 | + |
| 132 | + // MARK: - Helper Methods |
| 133 | + |
| 134 | + private func testColor(red: Double, green: Double, blue: Double, alpha: Double = 1.0) |
| 135 | + -> GeoDrawer.Color |
| 136 | + { |
| 137 | + #if canImport(CoreGraphics) |
| 138 | + return CGColor(red: red, green: green, blue: blue, alpha: alpha) |
| 139 | + #else |
| 140 | + return GeoDrawer.Color(red: red, green: green, blue: blue, alpha: alpha) |
| 141 | + #endif |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +#endif // canImport(Testing) |
0 commit comments