Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/test/java/prog/MainActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package prog;

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

import javax.swing.JButton;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

class MainActionTest {
@BeforeAll
static void headless() {
System.setProperty("java.awt.headless", "true");
// a real display would block on the warning dialog
assumeTrue(GraphicsEnvironment.isHeadless());
}

@Test
void everyActionWithoutFileShowsWarning() {
Main main = new Main();
main.createContentPane();
assertNull(main.getOpenedFile());
for (JButton b : new JButton[]{main.getHuffmanCompressButton(), main.getHuffmanDecompressButton(),
main.getLzwCompressButton(), main.getLzwDecompressButton()}) {
ActionEvent ev = new ActionEvent(b, ActionEvent.ACTION_PERFORMED, "");
assertThrows(HeadlessException.class, () -> main.actionPerformed(ev));
}
// unknown source is ignored
main.actionPerformed(new ActionEvent(new Object(), ActionEvent.ACTION_PERFORMED, ""));
}
}
96 changes: 96 additions & 0 deletions src/test/java/prog/handler/HandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package prog.handler;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import prog.util.Constants;

import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

/** Handlers end with a JOptionPane, which throws HeadlessException in headless mode (as in CI). */
class HandlerTest {
private static final String CONTENT = "hello hello hello compression world";

@TempDir
Path tempDir;

@BeforeAll
static void headless() {
System.setProperty("java.awt.headless", "true");
// a real display would block on the dialogs
assumeTrue(GraphicsEnvironment.isHeadless());
}

private File input() throws Exception {
Path p = tempDir.resolve("in.txt");
Files.writeString(p, CONTENT);
return p.toFile();
}

@Test
void huffmanRoundTripRunsBeforeDialog() throws Exception {
File in = input();
assertThrows(HeadlessException.class, () -> new CompressionHandler().compressWithHuffman(in));
File compressed = new File(in.getPath() + Constants.HUFFMAN_FILE_EXTENSION);
assertTrue(compressed.exists());

Files.delete(in.toPath());
assertThrows(HeadlessException.class, () -> new DecompressionHandler().decompressHuffman(compressed));
assertEquals(CONTENT, Files.readString(in.toPath()));
}

@Test
void lzwRoundTripRunsBeforeDialog() throws Exception {
File in = input();
assertThrows(HeadlessException.class, () -> new CompressionHandler().compressWithLZW(in));
File compressed = new File(in.getPath() + Constants.LZW_FILE_EXTENSION);
assertTrue(compressed.exists());

Files.delete(in.toPath());
assertThrows(HeadlessException.class, () -> new DecompressionHandler().decompressLZW(compressed));
assertEquals(CONTENT, Files.readString(in.toPath()));
}

@Test
void missingFileHitsErrorPath() {
File missing = tempDir.resolve("missing.txt").toFile();
File missingHuff = tempDir.resolve("missing.txt" + Constants.HUFFMAN_FILE_EXTENSION).toFile();
File missingLzw = tempDir.resolve("missing.txt" + Constants.LZW_FILE_EXTENSION).toFile();
assertThrows(HeadlessException.class, () -> new CompressionHandler().compressWithHuffman(missing));
assertThrows(HeadlessException.class, () -> new CompressionHandler().compressWithLZW(missing));
assertThrows(HeadlessException.class, () -> new DecompressionHandler().decompressHuffman(missingHuff));
assertThrows(HeadlessException.class, () -> new DecompressionHandler().decompressLZW(missingLzw));
}

@Test
void compressionRatio() {
assertEquals(75.0, new CompressionHandler().calculateCompressionRatio(100, 25), 1e-9);
}

@Test
void fileOperationHandler() {
FileOperationHandler h = new FileOperationHandler();
assertFalse(h.isFileSelected());
File f = new File("x.txt");
h.setSelectedFile(f);
h.setOutputFile(f);
assertTrue(h.isFileSelected());
assertSame(f, h.getSelectedFile());
assertSame(f, h.getOutputFile());

assertEquals("512 B", h.formatFileSize(512));
assertEquals(String.format("%.2f KB", 1.5), h.formatFileSize(1536));
assertEquals(String.format("%.2f MB", 2.0), h.formatFileSize(2L * 1024 * 1024));
assertEquals(String.format("%.2f GB", 3.0), h.formatFileSize(3L * 1024 * 1024 * 1024));

assertThrows(HeadlessException.class, h::browseForFile);
assertThrows(HeadlessException.class, h::showNoFileSelectedWarning);
}
}
36 changes: 36 additions & 0 deletions src/test/java/prog/ui/ButtonFactoryEffectsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package prog.ui;

import org.junit.jupiter.api.Test;

import javax.swing.JButton;
import javax.swing.border.Border;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

class ButtonFactoryEffectsTest {
@Test
void hoverAndPressEffects() {
JButton b = ButtonFactory.createStyledButton("Go", Color.BLUE, e -> {});
// Swing adds its own listeners; pick the one ButtonFactory added
MouseListener l = Arrays.stream(b.getMouseListeners())
.filter(x -> x.getClass().getName().startsWith(ButtonFactory.class.getName()))
.findFirst().orElseThrow();
MouseEvent ev = new MouseEvent(b, MouseEvent.MOUSE_ENTERED, 0, 0, 0, 0, 0, false);

l.mouseEntered(ev);
assertEquals(Color.BLUE.brighter(), b.getBackground());
l.mouseExited(ev);
assertEquals(Color.BLUE, b.getBackground());

Border before = b.getBorder();
l.mousePressed(ev);
assertNotSame(before, b.getBorder());
Border pressed = b.getBorder();
l.mouseReleased(ev);
assertNotSame(pressed, b.getBorder());
}
}
48 changes: 48 additions & 0 deletions src/test/java/prog/ui/MainWindowMenuTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package prog.ui;

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

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

class MainWindowMenuTest {
@BeforeAll
static void headless() {
System.setProperty("java.awt.headless", "true");
// a real display would block on the Help dialogs
assumeTrue(GraphicsEnvironment.isHeadless());
}

@Test
void menuBarHasFileAndHelp() {
AtomicBoolean opened = new AtomicBoolean();
JMenuBar bar = MainWindow.createMenuBar(e -> opened.set(true));
assertEquals(2, bar.getMenuCount());

JMenu file = bar.getMenu(0);
assertEquals("File", file.getText());
assertEquals("Open", file.getItem(0).getText());
assertEquals("Exit", file.getItem(1).getText());
file.getItem(0).doClick();
assertTrue(opened.get());

JMenu help = bar.getMenu(1);
assertEquals("How To", help.getItem(0).getText());
assertEquals("About", help.getItem(1).getText());
// dialogs throw in headless mode, after building their message
assertThrows(HeadlessException.class, () -> help.getItem(0).doClick());
assertThrows(HeadlessException.class, () -> help.getItem(1).doClick());
}

@Test
void createAndShowGuiNeedsDisplay() {
assertThrows(HeadlessException.class, () -> MainWindow.createAndShowGUI(new javax.swing.JPanel(), e -> {}));
}
}
Loading