Emberveil source

tests/WorldMapCheck.java

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;

public final class WorldMapCheck {
    public static void main(String[] args) throws Exception {
        EventQueue.invokeAndWait(() -> {
            try {
                BufferedImage image = EmberveilWorldMap.loadImage();
                if (image.getWidth()!=3328 || image.getHeight()!=2816) throw new AssertionError("Map resource dimensions");
                EmberveilWorldMap.MapCanvas map = new EmberveilWorldMap.MapCanvas(image);
                map.setSize(1000, 700);
                if(!new Point(2221,1627).equals(EmberveilWorldMap.mapPoint(new EmberveilWorldMap.Position(3222,3218,0),image))) throw new AssertionError("Lumbridge calibration");
                if(!new Point(1995,1729).equals(EmberveilWorldMap.mapPoint(new EmberveilWorldMap.Position(3109,3167,0),image))) throw new AssertionError("Tower calibration");
                if(EmberveilWorldMap.mapPoint(new EmberveilWorldMap.Position(3222,9618,0),image)!=null) throw new AssertionError("Dungeon marker must be hidden");
                if(EmberveilWorldMap.mapPoint(new EmberveilWorldMap.Position(0,0,0),image)!=null) throw new AssertionError("Outside map marker must be hidden");
                if(EmberveilWorldMap.mapPoint(null,image)!=null) throw new AssertionError("Logged out marker must be hidden");
                EmberveilWorldMap.updatePosition(true,3222,3218,0);
                map.findMe();
                Point location=EmberveilWorldMap.mapPoint(new EmberveilWorldMap.Position(3222,3218,0),image);
                if(Math.abs(map.offsetX+location.x*map.scale-500)>1 || Math.abs(map.offsetY+location.y*map.scale-350)>1) throw new AssertionError("Find me did not centre player");
                BufferedImage markerPreview=new BufferedImage(1000,700,BufferedImage.TYPE_INT_RGB);
                Graphics2D markerGraphics=markerPreview.createGraphics(); map.paint(markerGraphics); markerGraphics.dispose();
                if((markerPreview.getRGB(500,350)&0xffffff)!=0xffd21e) throw new AssertionError("Marker missing from centre");
                ImageIO.write(markerPreview,"png",new File("runtime/worldmap-player.png"));
                EmberveilWorldMap.updatePosition(true,3109,3167,0);
                map.findMe();
                markerGraphics=markerPreview.createGraphics(); map.paint(markerGraphics); markerGraphics.dispose();
                if((markerPreview.getRGB(500,350)&0xffffff)!=0xffd21e) throw new AssertionError("Marker failed to follow changed location");
                if(EmberveilWorldMap.mapPoint(new EmberveilWorldMap.Position(3222,3218,1),image)==null) throw new AssertionError("Upper floor projection missing");
                EmberveilWorldMap.updatePosition(false,0,0,0);
                markerGraphics=markerPreview.createGraphics(); map.paint(markerGraphics); markerGraphics.dispose();
                if((markerPreview.getRGB(500,350)&0xffffff)==0xffd21e) throw new AssertionError("Logged out marker remains");
                map.fit();
                long start=System.nanoTime();
                for (int i=0; i<1000; i++) {
                    map.zoom(i%2==0 ? 2 : 0.1, i%1000, i%700);
                    if (!Double.isFinite(map.offsetX) || !Double.isFinite(map.offsetY)) throw new AssertionError("Invalid pan position");
                }
                System.out.println("1,000 zoom changes: " + (System.nanoTime()-start)/1000000 + " ms");
                map.fit();
                if (image.getWidth()*map.scale>1001 || image.getHeight()*map.scale>701) throw new AssertionError("Overview clips map");
                map.zoom(1, 500, 350);
                double oldX=map.offsetX;
                map.dispatchEvent(new MouseEvent(map, MouseEvent.MOUSE_PRESSED, 1, 0, 500, 350, 1, false, MouseEvent.BUTTON1));
                map.dispatchEvent(new MouseEvent(map, MouseEvent.MOUSE_DRAGGED, 2, MouseEvent.BUTTON1_DOWN_MASK, 550, 350, 0, false, MouseEvent.NOBUTTON));
                if (Math.abs(map.offsetX-oldX-50)>0.01) throw new AssertionError("Drag did not pan");
                map.zoom(100, 500, 350);
                if (map.scale!=2) throw new AssertionError("Upper zoom bound");
                map.zoom(0.001, 500, 350);
                if (map.scale!=0.1) throw new AssertionError("Lower zoom bound");
                map.fit();
                BufferedImage preview = new BufferedImage(1000, 700, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics=preview.createGraphics();
                map.paint(graphics);
                graphics.dispose();
                ImageIO.write(preview, "png", new File("runtime/worldmap-preview.png"));
                map.zoom(1,500,350);
                graphics=preview.createGraphics(); map.paint(graphics); graphics.dispose();
                ImageIO.write(preview,"png",new File("runtime/worldmap-detail.png"));
                EmberveilWorldMap.open(null);
            } catch (Exception ex) { throw new RuntimeException(ex); }
        });
        for(int cycle=0;cycle<20;cycle++) {
            EmberveilWorldMap.open(null);
            EmberveilWorldMap.open(null);
            EventQueue.invokeAndWait(() -> {
                int visible=0;
                for(Window w:Window.getWindows()) if(w instanceof JDialog && w.isShowing()) { visible++; w.dispose(); }
                if(visible!=1) throw new AssertionError("Repeated open created duplicate windows: "+visible);
            });
        }
        System.out.println("PASS: 20 reopen/close cycles; repeated opening reuses one window");
        EmberveilWorldMap.open(null);
        EventQueue.invokeAndWait(() -> {
            int count=0;
            for (Window window : Window.getWindows()) {
                if (window instanceof JDialog && window.isShowing()) {
                    count++;
                    if (!((JDialog)window).getTitle().contains("World Map")) throw new AssertionError("Map failed to open");
                    try {
                        BufferedImage full = new BufferedImage(window.getWidth(), window.getHeight(), BufferedImage.TYPE_INT_RGB);
                        Graphics2D g=full.createGraphics();
                        window.paint(g);
                        g.dispose();
                        ImageIO.write(full, "png", new File("runtime/worldmap-window.png"));
                        window.setSize(720,480);
                        window.validate();
                        BufferedImage small = new BufferedImage(720,480,BufferedImage.TYPE_INT_RGB);
                        g=small.createGraphics(); window.paint(g); g.dispose();
                        ImageIO.write(small,"png",new File("runtime/worldmap-small.png"));
                    } catch(Exception ex) { throw new RuntimeException(ex); }
                    window.dispose();
                }
            }
            if (count!=1) throw new AssertionError("Expected one map dialog: " + count);
        });
        System.out.println("PASS: bundled map, overview, drag, zoom bounds, painting and window open/close");
    }
}