summaryrefslogtreecommitdiff
path: root/src/test/java/simulator/VSMainTest.java
blob: d6cb141bb5c5da90b23cd764c14d547e5e91e747 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package simulator;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;

import java.awt.Component;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import prefs.VSDefaultPrefs;
import prefs.VSPrefs;

public class VSMainTest {
    @AfterEach
    void resetHooks() {
        VSMain.resetTestHooks();
    }

    @Test
    void resolveStartupSimulationFileReturnsNullForMissingArgs() {
        assertNull(VSMain.resolveStartupSimulationFile(null));
        assertNull(VSMain.resolveStartupSimulationFile(new String[0]));
        assertNull(VSMain.resolveStartupSimulationFile(new String[] {""}));
        assertNull(VSMain.resolveStartupSimulationFile(new String[] {"   "}));
    }

    @Test
    void resolveStartupSimulationFileUsesFirstArgument() {
        assertEquals("saved-simulations/raft.dat",
                     VSMain.resolveStartupSimulationFile(
                         new String[] {"saved-simulations/raft.dat"}));
        assertEquals("saved-simulations/raft.dat",
                     VSMain.resolveStartupSimulationFile(
                         new String[] {"  saved-simulations/raft.dat  ",
                                       "ignored"}));
    }

    @Test
    void runOnEventDispatchThreadExecutesOnSwingEdt() {
        AtomicBoolean ranOnEdt = new AtomicBoolean(false);

        VSMain.runOnEventDispatchThread(new Runnable() {
            public void run() {
                ranOnEdt.set(javax.swing.SwingUtilities
                             .isEventDispatchThread());
            }
        });

        assertTrue(ranOnEdt.get());
    }

    @Test
    void launchSimulatorFrameCreatesAndStartsOnSwingEdt() {
        VSPrefs prefs = VSDefaultPrefs.init();
        AtomicBoolean createdOnEdt = new AtomicBoolean(false);
        AtomicBoolean openedOnEdt = new AtomicBoolean(false);
        AtomicReference<String> openedFilename = new AtomicReference<String>();
        VSSimulatorFrame frame = mock(VSSimulatorFrame.class);
        doAnswer(invocation -> {
            openedOnEdt.set(javax.swing.SwingUtilities
                            .isEventDispatchThread());
            openedFilename.set(invocation.getArgument(0, String.class));
            return null;
        }).when(frame).openAndStartSimulator("saved-simulations/raft.dat");

        VSSimulatorFrame launchedFrame = VSMain.launchSimulatorFrame(
            prefs, null, "saved-simulations/raft.dat",
            new VSMain.SimulatorFrameFactory() {
                public VSSimulatorFrame create(VSPrefs framePrefs,
                                               Component relativeTo) {
                    createdOnEdt.set(javax.swing.SwingUtilities
                                     .isEventDispatchThread());
                    return frame;
                }
            });

        assertTrue(createdOnEdt.get());
        assertTrue(openedOnEdt.get());
        assertNotNull(launchedFrame);
        assertSame(frame, launchedFrame);
        assertEquals("saved-simulations/raft.dat", openedFilename.get());
    }

    @Test
    void mainRoutesStartupReplayThroughEdt() {
        AtomicInteger splashCalls = new AtomicInteger(0);
        AtomicInteger delayCalls = new AtomicInteger(0);
        AtomicBoolean createdOnEdt = new AtomicBoolean(false);
        AtomicBoolean openedOnEdt = new AtomicBoolean(false);
        AtomicReference<String> openedFilename = new AtomicReference<String>();
        VSSimulatorFrame frame = mock(VSSimulatorFrame.class);

        VSMain.splashScreenLauncher = new VSMain.SplashScreenLauncher() {
            public void show() {
                splashCalls.incrementAndGet();
            }
        };
        VSMain.startupDelay = new VSMain.StartupDelay() {
            public void pause() {
                delayCalls.incrementAndGet();
            }
        };
        VSMain.simulatorFrameFactory = new VSMain.SimulatorFrameFactory() {
            public VSSimulatorFrame create(VSPrefs framePrefs,
                                           Component relativeTo) {
                createdOnEdt.set(javax.swing.SwingUtilities
                                 .isEventDispatchThread());
                return frame;
            }
        };

        doAnswer(invocation -> {
            openedOnEdt.set(javax.swing.SwingUtilities
                            .isEventDispatchThread());
            openedFilename.set(invocation.getArgument(0, String.class));
            return null;
        }).when(frame).openAndStartSimulator("saved-simulations/raft.dat");

        VSMain.main(new String[] {"saved-simulations/raft.dat"});

        assertEquals(1, splashCalls.get());
        assertEquals(1, delayCalls.get());
        assertTrue(createdOnEdt.get());
        assertTrue(openedOnEdt.get());
        assertEquals("saved-simulations/raft.dat", openedFilename.get());
    }
}