From f49f91abfafff9ae03c4d3656b4279a8b3343038 Mon Sep 17 00:00:00 2001 From: LucaHaverty Date: Tue, 13 Aug 2024 16:41:56 -0700 Subject: [PATCH 01/11] Input system tests --- fission/src/test/InputSystem.test.ts | 55 ++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 fission/src/test/InputSystem.test.ts diff --git a/fission/src/test/InputSystem.test.ts b/fission/src/test/InputSystem.test.ts new file mode 100644 index 0000000000..768ec5b930 --- /dev/null +++ b/fission/src/test/InputSystem.test.ts @@ -0,0 +1,55 @@ +import { test, describe, assert, expect } from "vitest" +import InputSystem from "@/systems/input/InputSystem" +import InputSchemeManager from "@/systems/input/InputSchemeManager" +import DefaultInputs from "@/systems/input/DefaultInputs" + +describe("Input scheme manager checks", () => { + test("Available schemes", () => { + assert(InputSchemeManager.availableInputSchemes[0].schemeName == DefaultInputs.ernie().schemeName) + assert(InputSchemeManager.defaultInputSchemes.length >= 1) + + const startingLength = InputSchemeManager.availableInputSchemes.length + InputSchemeManager.addCustomScheme(DefaultInputs.newBlankScheme) + + expect(InputSchemeManager.availableInputSchemes.length).toBe(startingLength + 1) + }) + test("Add a custom scheme", () => { + const startingLength = InputSchemeManager.availableInputSchemes.length + InputSchemeManager.addCustomScheme(DefaultInputs.newBlankScheme) + + assert((InputSchemeManager.availableInputSchemes.length = startingLength + 1)) + }) + test("Get random names", () => { + const names: string[] = [] + for (let i = 0; i < 20; i++) { + const name = InputSchemeManager.randomAvailableName + expect(names.includes(name)).toBe(false) + assert(name != undefined) + expect(name.length).toBeGreaterThan(0) + + const scheme = DefaultInputs.newBlankScheme + scheme.schemeName = name + + InputSchemeManager.addCustomScheme(scheme) + + names.push(name) + } + }) +}) + +describe("Input system checks", () => { + new InputSystem() + + test("Brain map exists", () => { + assert(InputSystem.brainIndexSchemeMap != undefined) + }) + + test("Inputs are zero", () => { + expect(InputSystem.getInput("arcadeDrive", 0)).toBe(0) + expect(InputSystem.getGamepadAxis(0)).toBe(0) + expect(InputSystem.getInput("randomInputThatDoesNotExist", 1273)).toBe(0) + expect(InputSystem.isKeyPressed("keyA")).toBe(false) + expect(InputSystem.isKeyPressed("ajhsekff")).toBe(false) + expect(InputSystem.isGamepadButtonPressed(1)).toBe(false) + }) +}) From fcac0cbd1b6ce3db07c11b18c8aa4c2f0237bf00 Mon Sep 17 00:00:00 2001 From: LucaHaverty Date: Thu, 15 Aug 2024 10:51:51 -0700 Subject: [PATCH 02/11] Input system, prefs system, select menu, and button unit tests --- fission/package.json | 3 + fission/src/systems/input/InputSystem.ts | 2 +- fission/src/test/InputSystem.test.ts | 42 +++++++--- fission/src/test/PreferencesSystem.test.ts | 34 ++++++++ fission/src/test/ui/Button.test.tsx | 29 +++++++ fission/src/test/ui/SelectMenu.test.tsx | 81 +++++++++++++++++++ fission/src/ui/components/Button.tsx | 15 +++- fission/src/ui/components/Checkbox.tsx | 1 + fission/src/ui/components/SelectMenu.tsx | 7 +- .../src/ui/components/StyledComponents.tsx | 14 ++-- 10 files changed, 209 insertions(+), 19 deletions(-) create mode 100644 fission/src/test/PreferencesSystem.test.ts create mode 100644 fission/src/test/ui/Button.test.tsx create mode 100644 fission/src/test/ui/SelectMenu.test.tsx diff --git a/fission/package.json b/fission/package.json index 6e1ae08b2b..e305e855a6 100644 --- a/fission/package.json +++ b/fission/package.json @@ -45,6 +45,9 @@ "@emotion/react": "^11.11.3", "@emotion/styled": "^11.11.0", "@mui/material": "^5.15.6", + "@testing-library/dom": "^10.4.0", + "@testing-library/react": "^16.0.0", + "@testing-library/user-event": "^14.5.2", "@types/node": "^20.4.4", "@types/pako": "^2.0.3", "@types/react": "^18.2.47", diff --git a/fission/src/systems/input/InputSystem.ts b/fission/src/systems/input/InputSystem.ts index d56aadd692..3be39b564a 100644 --- a/fission/src/systems/input/InputSystem.ts +++ b/fission/src/systems/input/InputSystem.ts @@ -241,7 +241,7 @@ class InputSystem extends WorldSystem { } // Returns true if two modifier states are identical - private static compareModifiers(state1: ModifierState, state2: ModifierState): boolean { + public static compareModifiers(state1: ModifierState, state2: ModifierState): boolean { if (!state1 || !state2) return false return ( diff --git a/fission/src/test/InputSystem.test.ts b/fission/src/test/InputSystem.test.ts index 768ec5b930..fc63f5e829 100644 --- a/fission/src/test/InputSystem.test.ts +++ b/fission/src/test/InputSystem.test.ts @@ -1,10 +1,10 @@ import { test, describe, assert, expect } from "vitest" -import InputSystem from "@/systems/input/InputSystem" +import InputSystem, { EmptyModifierState, ModifierState } from "@/systems/input/InputSystem" import InputSchemeManager from "@/systems/input/InputSchemeManager" import DefaultInputs from "@/systems/input/DefaultInputs" -describe("Input scheme manager checks", () => { - test("Available schemes", () => { +describe("Input Scheme Manager Checks", () => { + test("Available Schemes", () => { assert(InputSchemeManager.availableInputSchemes[0].schemeName == DefaultInputs.ernie().schemeName) assert(InputSchemeManager.defaultInputSchemes.length >= 1) @@ -13,13 +13,13 @@ describe("Input scheme manager checks", () => { expect(InputSchemeManager.availableInputSchemes.length).toBe(startingLength + 1) }) - test("Add a custom scheme", () => { + test("Add a Custom Scheme", () => { const startingLength = InputSchemeManager.availableInputSchemes.length InputSchemeManager.addCustomScheme(DefaultInputs.newBlankScheme) assert((InputSchemeManager.availableInputSchemes.length = startingLength + 1)) }) - test("Get random names", () => { + test("Get Random Names", () => { const names: string[] = [] for (let i = 0; i < 20; i++) { const name = InputSchemeManager.randomAvailableName @@ -37,14 +37,14 @@ describe("Input scheme manager checks", () => { }) }) -describe("Input system checks", () => { - new InputSystem() +describe("Input System Checks", () => { + const inputSystem = new InputSystem() - test("Brain map exists", () => { + test("Brain Map Exists?", () => { assert(InputSystem.brainIndexSchemeMap != undefined) }) - test("Inputs are zero", () => { + test("Inputs are Zero", () => { expect(InputSystem.getInput("arcadeDrive", 0)).toBe(0) expect(InputSystem.getGamepadAxis(0)).toBe(0) expect(InputSystem.getInput("randomInputThatDoesNotExist", 1273)).toBe(0) @@ -52,4 +52,28 @@ describe("Input system checks", () => { expect(InputSystem.isKeyPressed("ajhsekff")).toBe(false) expect(InputSystem.isGamepadButtonPressed(1)).toBe(false) }) + + test("Modifier State Comparison", () => { + const allFalse: ModifierState = { + alt: false, + ctrl: false, + shift: false, + meta: false, + } + + const differentState: ModifierState = { + alt: false, + ctrl: true, + shift: false, + meta: true, + } + + inputSystem.Update(-1) + + expect(InputSystem.compareModifiers(allFalse, EmptyModifierState)).toBe(true) + expect(InputSystem.compareModifiers(allFalse, InputSystem.currentModifierState)).toBe(true) + expect(InputSystem.compareModifiers(differentState, InputSystem.currentModifierState)).toBe(false) + expect(InputSystem.compareModifiers(differentState, differentState)).toBe(true) + expect(InputSystem.compareModifiers(differentState, allFalse)).toBe(false) + }) }) diff --git a/fission/src/test/PreferencesSystem.test.ts b/fission/src/test/PreferencesSystem.test.ts new file mode 100644 index 0000000000..34f8d73661 --- /dev/null +++ b/fission/src/test/PreferencesSystem.test.ts @@ -0,0 +1,34 @@ +import PreferencesSystem from "@/systems/preferences/PreferencesSystem" +import { test, describe, expect } from "vitest" + +describe("Preferences System", () => { + test("Settings without saving", () => { + PreferencesSystem.setGlobalPreference("ZoomSensitivity", 15) + PreferencesSystem.setGlobalPreference("RenderSceneTags", true) + PreferencesSystem.setGlobalPreference("RenderScoreboard", false) + + expect(PreferencesSystem.getGlobalPreference("ZoomSensitivity")).toBe(15) + expect(PreferencesSystem.getGlobalPreference("RenderSceneTags")).toBe(true) + expect(PreferencesSystem.getGlobalPreference("RenderScoreboard")).toBe(false) + }) + test("Reset to default if undefined", () => { + PreferencesSystem.setGlobalPreference("ZoomSensitivity", undefined) + PreferencesSystem.setGlobalPreference("RenderSceneTags", undefined) + PreferencesSystem.setGlobalPreference("RenderScoreboard", undefined) + + expect(PreferencesSystem.getGlobalPreference("ZoomSensitivity")).toBe(15) + expect(PreferencesSystem.getGlobalPreference("RenderSceneTags")).toBe(true) + expect(PreferencesSystem.getGlobalPreference("RenderScoreboard")).toBe(true) + }) + test("Settings then saving", () => { + PreferencesSystem.setGlobalPreference("ZoomSensitivity", 13) + PreferencesSystem.setGlobalPreference("RenderSceneTags", true) + PreferencesSystem.setGlobalPreference("RenderScoreboard", false) + + PreferencesSystem.savePreferences() + + expect(PreferencesSystem.getGlobalPreference("ZoomSensitivity")).toBe(13) + expect(PreferencesSystem.getGlobalPreference("RenderSceneTags")).toBe(true) + expect(PreferencesSystem.getGlobalPreference("RenderScoreboard")).toBe(false) + }) +}) diff --git a/fission/src/test/ui/Button.test.tsx b/fission/src/test/ui/Button.test.tsx new file mode 100644 index 0000000000..6c831f313b --- /dev/null +++ b/fission/src/test/ui/Button.test.tsx @@ -0,0 +1,29 @@ +import { render, fireEvent, getByText } from "@testing-library/react" +import { assert, describe, expect, test } from "vitest" +import Button from "@/ui/components/Button" + +describe("Button", () => { + test("Click Enabled Button", () => { + let buttonClicked = false + let container = render(