Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a very basic Swing unit test #300

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
run: chmod +x gradlew
- name: Install docs dependencies
run: pip3 install -r docs/requirements.txt -U
- name: Install Xvfb server for UI tests
run: sudo apt install xvfb
- name: Read version from gradle.properties
id: read_version
uses: christian-draeger/read-properties@1.0.0
Expand All @@ -33,8 +35,12 @@ jobs:
# Compile the code, run unit tests, upload code coverage results.
- name: Compile Java code
run: ./gradlew compileJava
- name: Start Xvfb
run: /usr/bin/Xvfb -ac :42 &
- name: Run unit tests
run: ./gradlew check
run: DISPLAY=42 ./gradlew check
- name: Kill VNC
run: vncserver -kill :42
- name: Codecov
uses: codecov/codecov-action@v1.0.5
with:
Expand Down
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ dependencies {
compileOnly("com.google.gwt:gwt-user:2.8.2")
implementation("javax.help:javahelp:2.0.05")
testImplementation("junit:junit:4.12")
testCompile("org.assertj:assertj-core:3.11.1")
testCompile("org.assertj:assertj-swing:3.9.2")
}

application {
Expand Down
94 changes: 47 additions & 47 deletions src/main/java/org/edumips64/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@

public class Main {

// Elements that are public for unit tests.
public JFrame mainFrame;

private CPU cpu;
// The last created CPU Worker. Necessary for the Stop menu item.
private CPUSwingWorker cpuWorker;
Expand All @@ -67,7 +70,6 @@ public class Main {
private ConfigStore configStore;
private JFileChooser jfc;

private JFrame mainFrame;
private JMenuItem open;
private JMenuItem reset;
private JMenuItem exit;
Expand Down Expand Up @@ -195,53 +197,7 @@ public static void main(String args[]) {
System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tm%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n");

Main.showVersion();

// Creating the main JFrame
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
mm.mainFrame = new JFrame();
mm.mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Maximizing the application
Insets screenInsets = mm.mainFrame.getToolkit().getScreenInsets(mm.mainFrame.getGraphicsConfiguration());
Rectangle screenSize = mm.mainFrame.getGraphicsConfiguration().getBounds();
Rectangle maxBounds = new Rectangle(screenInsets.left + screenSize.x,
screenInsets.top + screenSize.y,
screenSize.x + screenSize.width - screenInsets.right - screenInsets.left,
screenSize.y + screenSize.height - screenInsets.bottom - screenInsets.top);
mm.mainFrame.setMaximizedBounds(maxBounds);
mm.mainFrame.setBounds(maxBounds);

mm.mainFrame.setLocation(0, 0);
mm.init();
mm.mainFrame.setTitle("EduMIPS64 v. " + MetaInfo.VERSION + " - " + CurrentLocale.getString("PROSIM"));
mm.mainFrame.setVisible(true);
mm.mainFrame.setExtendedState(mm.mainFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
// Auto-minimze the log window and the I/O window
try {
mm.ioFrame.setIcon(true);
} catch (java.beans.PropertyVetoException ignored) {}

// Tile windows once the window is maximized.
mm.mainFrame.addWindowStateListener(new WindowStateListener() {
// Keep track of whether maximization was already done, to prevent unwanted tiling of windows when the main
// window is maximized again.
private boolean alreadyMaximized = false;

public void windowStateChanged(WindowEvent event) {
if (alreadyMaximized) {
return;
}
boolean isMaximized = (event.getNewState() & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH;
boolean wasMaximized = (event.getOldState() & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH;

if (isMaximized && !wasMaximized) {
mm.tileWindows();
alreadyMaximized = true;
}
}
});

log.info("Simulator started");

if (parsedArgs.filename != null) {
mm.resetSimulator(false);
Expand Down Expand Up @@ -283,8 +239,13 @@ private void setFileChooserFont(Component[] components) {
}

public void init() {
// Creating the main JFrame
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

log.info("Simulator started");
LocalFileUtils lfu = new LocalFileUtils();

configStore = new JavaPrefsConfigStore(ConfigStore.defaults);
Expand Down Expand Up @@ -453,6 +414,45 @@ public void internalFrameDeiconified(InternalFrameEvent e) {
codeSupplier = () -> this.code;

changeShownMenuItems(cpu.getStatus());

// Maximizing the application
Insets screenInsets = mainFrame.getToolkit().getScreenInsets(mainFrame.getGraphicsConfiguration());
Rectangle screenSize = mainFrame.getGraphicsConfiguration().getBounds();
Rectangle maxBounds = new Rectangle(screenInsets.left + screenSize.x,
screenInsets.top + screenSize.y,
screenSize.x + screenSize.width - screenInsets.right - screenInsets.left,
screenSize.y + screenSize.height - screenInsets.bottom - screenInsets.top);
mainFrame.setMaximizedBounds(maxBounds);
mainFrame.setBounds(maxBounds);

mainFrame.setLocation(0, 0);
mainFrame.setTitle("EduMIPS64 v. " + MetaInfo.VERSION + " - " + CurrentLocale.getString("PROSIM"));
mainFrame.setVisible(true);
mainFrame.setExtendedState(mainFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
// Auto-minimze the log window and the I/O window
try {
ioFrame.setIcon(true);
} catch (java.beans.PropertyVetoException ignored) {}

// Tile windows once the window is maximized.
mainFrame.addWindowStateListener(new WindowStateListener() {
// Keep track of whether maximization was already done, to prevent unwanted tiling of windows when the main
// window is maximized again.
private boolean alreadyMaximized = false;

public void windowStateChanged(WindowEvent event) {
if (alreadyMaximized) {
return;
}
boolean isMaximized = (event.getNewState() & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH;
boolean wasMaximized = (event.getOldState() & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH;

if (isMaximized && !wasMaximized) {
tileWindows();
alreadyMaximized = true;
}
}
});
}

/** Changes the status of running menu items.
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/org/edumips64/MainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* MainTest.java
*
* Tests for the Main class of EduMIPS64.
*
* (c) 2020 Andrea Spadaccini
*
* This file is part of the EduMIPS64 project, and is released under the GNU
* General Public License.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.edumips64;

import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.fixture.FrameFixture;

@RunWith(JUnit4.class)
public class MainTest {
private FrameFixture window;

@Before
public void setUp() {
Main main = GuiActionRunner.execute(() -> new Main());
main.init();
window = new FrameFixture(main.mainFrame);
window.show();
}

@Test
public void shouldBePossibleToExit() {
window.menuItemWithPath("File", "Exit").click();
}

@After
public void tearDown() {
window.cleanUp();
}
}