Skip to content

Commit

Permalink
Port more tests, fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Aug 17, 2024
1 parent e16aacf commit 48d56f6
Show file tree
Hide file tree
Showing 19 changed files with 779 additions and 48 deletions.
25 changes: 1 addition & 24 deletions client/src/main/java/util/DialogUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import screen.LoadingDialog;

import java.awt.*;

import static utils.InjectedThings.logger;

@Deprecated()
public class DialogUtil
{
private static LoadingDialog loadingDialog = new LoadingDialog();
private static boolean shownConnectionLost = false;

public static void showInfo(String infoText)
Expand Down Expand Up @@ -106,23 +102,4 @@ public static void showConnectionLost()
shownConnectionLost = true;
}
}

public static void showLoadingDialog(String text)
{
logger.info("loaderShown", text);
loadingDialog.showDialog(text);
}
public static void dismissLoadingDialog()
{
loadingDialog.dismissDialog();
}

public static void showCustomMessage(Object message) {
JOptionPane.showMessageDialog(
null,
message,
"Information",
JOptionPane.INFORMATION_MESSAGE
);
}
}
164 changes: 164 additions & 0 deletions client/src/main/kotlin/util/DialogUtilNew.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package util

import java.awt.Component
import java.io.File
import javax.swing.JFileChooser
import javax.swing.JOptionPane
import javax.swing.SwingUtilities
import screen.LoadingDialog
import screen.ScreenCache
import utils.InjectedThings.logger

object DialogUtilNew {
private var loadingDialog: LoadingDialog? = null

fun showInfo(infoText: String, parent: Component = ScreenCache.getMainScreen()) {
logDialogShown("Info", "Information", infoText)
JOptionPane.showMessageDialog(
parent,
infoText,
"Information",
JOptionPane.INFORMATION_MESSAGE
)
logDialogClosed("Info", null)
}

fun showCustomMessage(message: Any, parent: Component = ScreenCache.getMainScreen()) {
logDialogShown("CustomInfo", "Information", "?")
JOptionPane.showMessageDialog(
parent,
message,
"Information",
JOptionPane.INFORMATION_MESSAGE
)
logDialogClosed("CustomInfo", null)
}

fun showError(errorText: String, parent: Component? = ScreenCache.getMainScreen()) {
dismissLoadingDialog()

logDialogShown("Error", "Error", errorText)
JOptionPane.showMessageDialog(parent, errorText, "Error", JOptionPane.ERROR_MESSAGE)
logDialogClosed("Error", null)
}

fun showErrorLater(errorText: String) {
SwingUtilities.invokeLater { showError(errorText) }
}

@JvmOverloads
fun showQuestion(
message: String,
allowCancel: Boolean = false,
parent: Component = ScreenCache.getMainScreen()
): Int {
logDialogShown("Question", "Question", message)
val option =
if (allowCancel) JOptionPane.YES_NO_CANCEL_OPTION else JOptionPane.YES_NO_OPTION
val selection =
JOptionPane.showConfirmDialog(
parent,
message,
"Question",
option,
JOptionPane.QUESTION_MESSAGE
)
logDialogClosed("Question", selection)
return selection
}

fun showLoadingDialog(text: String) {
logDialogShown("Loading", "", text)
loadingDialog = LoadingDialog()
loadingDialog?.showDialog(text)
}

fun dismissLoadingDialog() {
val wasVisible = loadingDialog?.isVisible ?: false
loadingDialog?.dismissDialog()
if (wasVisible) {
logDialogClosed("Loading", null)
}
}

fun showOption(title: String, message: String, options: List<String>): String? {
logDialogShown("Option", title, message)
val typedArray = options.toTypedArray()
val selection =
JOptionPane.showOptionDialog(
null,
message,
title,
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
typedArray,
options.first()
)
val selectionStr = if (selection > -1) typedArray[selection] else null

logDialogClosed("Option", selectionStr)
return selectionStr
}

fun <K> showInput(
title: String,
message: String,
options: Array<K>? = null,
defaultOption: K? = null
): K? {
logDialogShown("Input", title, message)
val selection =
JOptionPane.showInputDialog(
null,
message,
title,
JOptionPane.PLAIN_MESSAGE,
null,
options,
defaultOption
) as K?

logDialogClosed("Input", selection)
return selection
}

fun chooseDirectory(parent: Component?): File? {
logDialogShown("File selector", "", "")
val fc = JFileChooser()
fc.fileSelectionMode = JFileChooser.DIRECTORIES_ONLY
val option = fc.showDialog(parent, "Select")
if (option != JFileChooser.APPROVE_OPTION) {
return null
}

val file = fc.selectedFile
logDialogClosed("File selector", file?.absolutePath)
return file
}

private fun logDialogShown(type: String, title: String, message: String) {
logger.info(
"dialogShown",
"$type dialog shown: $message",
"dialogType" to type,
"dialogTitle" to title,
"dialogMessage" to message
)
}

private fun logDialogClosed(type: String, selection: Any?) {
var message = "$type dialog closed"
selection?.let { message += " - selected ${translateOption(it)}" }

logger.info("dialogClosed", message, "dialogType" to type, "dialogSelection" to selection)
}

private fun translateOption(option: Any?) =
when (option) {
JOptionPane.YES_OPTION -> "Yes"
JOptionPane.NO_OPTION -> "No"
JOptionPane.CANCEL_OPTION -> "Cancel"
else -> option
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import javax.swing.JOptionPane
import javax.swing.JPanel
import kong.unirest.Unirest
import kong.unirest.json.JSONObject
import utils.InjectedThings.logger
import kotlin.system.exitProcess
import utils.InjectedThings.logger

/**
* Automatically check for and download updates using the Github API
Expand All @@ -34,7 +34,7 @@ object UpdateManager {

fun queryLatestReleaseJson(repositoryUrl: String): JSONObject? {
try {
DialogUtil.showLoadingDialog("Checking for updates...")
DialogUtilNew.showLoadingDialog("Checking for updates...")

val response = Unirest.get("$repositoryUrl/releases/latest").asJson()
if (response.status != 200) {
Expand All @@ -43,17 +43,17 @@ object UpdateManager {
"Received non-success HTTP status: ${response.status} - ${response.statusText}",
"responseBody" to response.body,
)
DialogUtil.showError("Failed to check for updates (unable to connect).")
DialogUtilNew.showError("Failed to check for updates (unable to connect).")
return null
}

return response.body.`object`
} catch (t: Throwable) {
logger.error("updateError", "Caught $t checking for updates", t)
DialogUtil.showError("Failed to check for updates (unable to connect).")
DialogUtilNew.showError("Failed to check for updates (unable to connect).")
return null
} finally {
DialogUtil.dismissLoadingDialog()
DialogUtilNew.dismissLoadingDialog()
}
}

Expand All @@ -73,7 +73,7 @@ object UpdateManager {
}

val answer =
DialogUtil.showQuestion(
DialogUtilNew.showQuestion(
"An update is available (${metadata.version}). Would you like to download it now?",
false
)
Expand All @@ -91,7 +91,7 @@ object UpdateManager {
panel.add(lblOne, BorderLayout.NORTH)
panel.add(linkLabel, BorderLayout.SOUTH)

DialogUtil.showCustomMessage(panel)
DialogUtilNew.showCustomMessage(panel)
}

fun parseUpdateMetadata(responseJson: JSONObject): UpdateMetadata? {
Expand Down Expand Up @@ -126,7 +126,7 @@ object UpdateManager {

val msg =
"Failed to launch update.bat - call the following manually to perform the update: \n\n$manualCommand"
DialogUtil.showError(msg)
DialogUtilNew.showError(msg)
return
}

Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions client/src/main/resources/update/update.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@echo off

REM %1 = noOfBytes
REM %2 = version number
REM %3 = filename
REM %4 = assetId

echo Performing download of %1 bytes (Version %2)

curl -LJO -H "Accept: application/octet-stream" https://api.github.com/repos/alyssaburlton/Dartzee/releases/assets/%4

ren Dartzee.jar Dartzee_OLD.jar
ren %3 Dartzee.jar
del Dartzee_OLD.jar

start javaw -Xms256m -Xmx512m -jar Dartzee.jar justUpdated trueLaunch
exit
94 changes: 94 additions & 0 deletions client/src/test/kotlin/bean/HyperlinkAdaptorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package bean

import com.github.alyssaburlton.swingtest.makeMouseEvent
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.shouldBe
import io.mockk.mockk
import io.mockk.verifySequence
import java.awt.Cursor
import java.awt.event.MouseEvent
import javax.swing.JButton
import javax.swing.JPanel
import main.kotlin.testCore.AbstractTest
import org.junit.jupiter.api.Test

private val mouseEventOverLink = makeMouseEvent(JButton())
private val mouseEventNotOverLink = makeMouseEvent(JButton())

class HyperlinkAdaptorTest : AbstractTest() {
@Test
fun `Should not accept a non-component listener`() {
shouldThrow<ClassCastException> { HyperlinkAdaptor(NonComponentHyperlinkListener()) }
}

@Test
fun `Should respond to mouse clicks`() {
val listener = mockk<TestHyperlinkListener>(relaxed = true)

val adaptor = HyperlinkAdaptor(listener)
adaptor.mouseClicked(mouseEventOverLink)
adaptor.mouseClicked(mouseEventNotOverLink)

verifySequence {
listener.linkClicked(mouseEventOverLink)
listener.linkClicked(mouseEventNotOverLink)
}
}

@Test
fun `Should change the cursor on mouse movement`() {
val listener = TestHyperlinkListener()
val adaptor = HyperlinkAdaptor(listener)

adaptor.mouseMoved(mouseEventNotOverLink)
listener.cursor shouldBe Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)

adaptor.mouseMoved(mouseEventOverLink)
listener.cursor shouldBe Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)

adaptor.mouseMoved(mouseEventNotOverLink)
listener.cursor shouldBe Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)

adaptor.mouseEntered(mouseEventNotOverLink)
listener.cursor shouldBe Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)

adaptor.mouseEntered(mouseEventOverLink)
listener.cursor shouldBe Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)
}

@Test
fun `Should revert the cursor on mouseExit`() {
val listener = TestHyperlinkListener()
val adaptor = HyperlinkAdaptor(listener)

adaptor.mouseMoved(mouseEventOverLink)
listener.cursor shouldBe Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)

adaptor.mouseExited(null)
listener.cursor shouldBe Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)
}

@Test
fun `Should revert the cursor on mouseDragged`() {
val listener = TestHyperlinkListener()
val adaptor = HyperlinkAdaptor(listener)

adaptor.mouseMoved(mouseEventOverLink)
listener.cursor shouldBe Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)

adaptor.mouseDragged(null)
listener.cursor shouldBe Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)
}
}

private class TestHyperlinkListener : JPanel(), IHyperlinkListener {
override fun isOverHyperlink(arg0: MouseEvent) = arg0 === mouseEventOverLink

override fun linkClicked(arg0: MouseEvent) {}
}

private class NonComponentHyperlinkListener : IHyperlinkListener {
override fun isOverHyperlink(arg0: MouseEvent) = false

override fun linkClicked(arg0: MouseEvent) {}
}
Loading

0 comments on commit 48d56f6

Please sign in to comment.