extends TestCase ## A10: every menu reachable and navigable, key rebinding through the menu, and the ## program's flow from the main menu into a practice match, the pause menu, the ## results and back. const PATH: String = "user://test_menus_settings.cfg" var menus: Menus var tree: SceneTree func before_each() -> void: super.before_each() tree = Engine.get_main_loop() as SceneTree menus = Menus.new() menus.use_settings(GameSettings.new(), PATH) tree.root.add_child(menus) func after_each() -> void: super.after_each() if is_instance_valid(menus): menus.free() DirAccess.remove_absolute(ProjectSettings.globalize_path(PATH)) func _press(screen: String, text: String) -> void: var buttons: Dictionary = menus.buttons_of(screen) assert_true(buttons.has(text), "%s has a '%s' button (has %s)" % [screen, text, buttons.keys()]) if buttons.has(text): (buttons[text] as Button).pressed.emit() func _key(keycode: Key) -> InputEventKey: var key: InputEventKey = InputEventKey.new() key.physical_keycode = keycode key.keycode = keycode key.pressed = true return key func test_every_screen_is_reachable_from_the_main_menu_and_back() -> void: menus.show_screen("main", false) assert_eq(menus.current, "main") for pair: Array in [["Practice against bots", "practice"], ["Join a server", "join"], ["Settings", "settings"]]: _press("main", pair[0]) assert_eq(menus.current, pair[1]) assert_true((menus.screens[pair[1]] as Control).visible) assert_false((menus.screens["main"] as Control).visible, "one screen at a time") menus.back() assert_eq(menus.current, "main", "back from %s" % pair[1]) var quit: Array[bool] = [false] menus.quit_requested.connect(func() -> void: quit[0] = true) _press("main", "Quit") assert_true(quit[0]) func test_every_screen_takes_keyboard_focus_and_escape_goes_back() -> void: for screen: String in Menus.SCREENS: menus.show_screen(screen, false) await tree.process_frame var focused: Control = menus.get_viewport().gui_get_focus_owner() assert_not_null(focused, "%s focuses something" % screen) if focused != null: assert_true(menus.screens[screen].is_ancestor_of(focused), "%s's focus is on its own screen" % screen) menus.show_screen("main", false) _press("main", "Settings") await tree.process_frame var cancel: InputEventAction = InputEventAction.new() cancel.action = "ui_cancel" cancel.pressed = true menus._unhandled_input(cancel) assert_eq(menus.current, "main", "Escape went back") func test_focus_moves_through_the_main_menu_with_the_arrow_keys() -> void: menus.show_screen("main", false) await tree.process_frame var seen: Array[String] = [] var focused: Control = menus.get_viewport().gui_get_focus_owner() for _step: int in 4: assert_not_null(focused) if focused == null: return seen.append((focused as Button).text) var next: Control = focused.find_valid_focus_neighbor(SIDE_BOTTOM) if next == null: break next.grab_focus() focused = next assert_eq(seen, ["Practice against bots", "Join a server", "Settings", "Quit"] as Array[String]) func test_rebinding_in_the_menu_detects_conflicts() -> void: menus.show_screen("settings", false) menus.listen_for("jump") assert_eq(menus.listening_for, "jump") menus._unhandled_input(_key(KEY_W)) assert_eq(menus.listening_for, "", "stopped listening") assert_eq(menus.settings.bindings.code_of("jump"), KEY_SPACE, "W is forward's: refused") menus.listen_for("jump") menus._unhandled_input(_key(KEY_F)) assert_eq(menus.settings.bindings.code_of("jump"), KEY_F) assert_eq(GameSettings.load_from(PATH).bindings.code_of("jump"), KEY_F, "saved at once") menus.listen_for("jump") menus._unhandled_input(_key(KEY_ESCAPE)) assert_eq(menus.settings.bindings.code_of("jump"), KEY_F, "Escape left it") assert_eq(menus.current, "settings", "and stayed on the screen") _press("settings", "Put every key back") assert_eq(menus.settings.bindings.code_of("jump"), KEY_SPACE) func test_settings_controls_change_and_save_the_settings() -> void: menus.show_screen("main", false) _press("main", "Settings") menus._sensitivity.value = 2.0 menus._invert.button_pressed = true menus._quality.select(0) menus._quality.item_selected.emit(0) (menus._volumes["music"] as HSlider).value = 0.25 menus._max_fps.select(2) menus._max_fps.item_selected.emit(2) menus._print_pass.button_pressed = false menus.back() var saved: GameSettings = GameSettings.load_from(PATH) assert_near(saved.mouse.mouse_sensitivity, 2.0, 0.0001) assert_true(saved.mouse.invert_y) assert_eq(saved.quality, 0) assert_near(saved.music_volume, 0.25, 0.0001) assert_eq(saved.max_fps, GameSettings.FRAME_CAPS[2]) assert_false(saved.print_pass) # And the next time the screen opens, it shows them. var reopened: Menus = Menus.new() reopened.use_settings(saved, PATH) assert_near(reopened._sensitivity.value, 2.0, 0.0001) assert_eq(reopened._quality.selected, 0) reopened.free() func test_practice_and_join_say_what_was_asked() -> void: var asked: Array = [] menus.practice_requested.connect(func(bots: int, difficulty: int) -> void: asked.append(["practice", bots, difficulty])) menus.join_requested.connect(func(host: String, port: int, player_name: String) -> void: asked.append(["join", host, port, player_name])) menus.show_screen("practice", false) menus._bots.value = 5 menus._difficulty.select(1) _press("practice", "Set out") assert_eq(asked.pop_back(), ["practice", 5, 0]) menus.show_screen("join", false) menus._name.text = "Wren" menus._address.text = "not a port:x" _press("join", "Join this address") assert_true(asked.is_empty(), "a bad address isn't joined") assert_true(menus._join_note.text.contains("isn't an address")) menus._address.text = "127.0.0.1:24999" _press("join", "Join this address") assert_eq(asked.pop_back(), ["join", "127.0.0.1", 24999, "Wren"]) assert_eq(GameSettings.load_from(PATH).last_server, "127.0.0.1:24999") # A found server, chosen from the list. menus.show_servers([ {"name": "Old", "players": 9, "max_players": 24, "address": "192.0.2.9", "port": 24650, "source": "lan", "compatible": false}, {"name": "Harbour", "players": 3, "max_players": 24, "address": "127.0.0.1", "port": 24651, "source": "lan", "compatible": true}, ] as Array[Dictionary]) menus._servers.select(0) _press("join", "Join the chosen server") assert_true(asked.is_empty(), "another version can't be joined") menus._servers.select(1) _press("join", "Join the chosen server") assert_eq(asked.pop_back(), ["join", "127.0.0.1", 24651, "Wren"]) func test_results_say_where_you_placed() -> void: menus.show_results(1, 24, 3, ["Ada drowned"] as Array[String]) assert_eq(menus.current, "results") assert_eq(menus._results_title.text, "Last on dry ground") assert_true(menus._results_body.text.contains("Eliminations: 3")) menus.show_results(7, 24, 0, [] as Array[String]) assert_eq(menus._results_title.text, "Placed 7 of 24") func test_the_shell_goes_from_the_menu_to_a_match_and_back() -> void: menus.free() var shell: AppShell = AppShell.new(GameSettings.new(), PATH) shell.grab_mouse = false tree.root.add_child(shell) assert_eq(shell.menus.current, "main") shell.menus.settings.practice_bots = 3 _press_in(shell.menus, "main", "Practice against bots") _press_in(shell.menus, "practice", "Set out") assert_not_null(shell.session, "a match began") assert_not_null(shell.session.local, "offline") assert_false(shell.menus.visible, "the menus are out of the way") for _frame: int in 30: shell.session.pump(1.0 / 30.0) shell.watch_match() assert_true(shell.session.game.world != null, "the world arrived") # Escape: the pause menu, and the player's keys stop. shell.session.pause_requested.emit() assert_eq(shell.menus.current, "pause") assert_true(shell.session.paused) _press_in(shell.menus, "pause", "Settings") assert_eq(shell.menus.current, "settings") shell.menus.back() assert_eq(shell.menus.current, "pause", "settings goes back to the pause menu") _press_in(shell.menus, "pause", "Back to the match") assert_false(shell.session.paused) assert_false(shell.menus.visible) # Leaving: back to the main menu with no match. shell.pause() _press_in(shell.menus, "pause", "Leave the match") assert_null(shell.session) assert_eq(shell.menus.current, "main") shell.free() func test_the_shell_shows_results_when_a_match_you_played_ends() -> void: menus.free() var shell: AppShell = AppShell.new(GameSettings.new(), PATH) shell.grab_mouse = false tree.root.add_child(shell) var rules: MatchRules = MatchRules.new() rules.max_players = 2 rules.min_players = 1 rules.lobby_ticks = 2 rules.countdown_ticks = 2 rules.results_ticks = 600 var practice: ClientSession = ClientSession.new() practice.play_offline(4, 1, rules) shell.adopt(practice) var game: ClientGame = practice.game var server: Match = practice.local.loop.match_flow var frames: int = 0 while not game.playing() and frames < 400: practice.pump(1.0 / 30.0) shell.watch_match() frames += 1 assert_true(game.playing(), "in the match") # The bot falls to this player, and the match ends with them on top. var bot_handle: int = -1 for handle: int in server.participants: if handle != LocalServer.HANDLE: bot_handle = handle var bot: SimEntity = server.sim.entities.find(int(server.entity_of[bot_handle])) bot.health = 0 bot.last_attacker = int(server.entity_of[LocalServer.HANDLE]) bot.last_cause = SimEntity.CAUSE_WEAPON while game.match_phase != Match.RESULTS and frames < 800: practice.pump(1.0 / 30.0) shell.watch_match() frames += 1 assert_eq(game.match_phase, Match.RESULTS) assert_eq(shell.menus.current, "results") assert_eq(shell.menus._results_title.text, "Last on dry ground") assert_true(shell.menus._results_body.text.contains("Eliminations: 1"), shell.menus._results_body.text) _press_in(shell.menus, "results", "Back to the main menu") assert_eq(shell.menus.current, "main") shell.free() func _press_in(on: Menus, screen: String, text: String) -> void: var buttons: Dictionary = on.buttons_of(screen) assert_true(buttons.has(text), "%s has a '%s' button (has %s)" % [screen, text, buttons.keys()]) if buttons.has(text): (buttons[text] as Button).pressed.emit() func test_menu_buttons_knock_when_pointed_at_and_pressed() -> void: var sounds: MenuSounds = MenuSounds.new() tree.root.add_child(sounds) sounds.attach(menus) var main: Dictionary = menus.buttons_of("main") var first: Button = main.values()[0] first.mouse_entered.emit() assert_eq(sounds.heard.back(), "ui_hover") var later: Button = Button.new() later.text = "Back" menus.add_child(later) later.pressed.emit() assert_eq(sounds.heard.back(), "ui_back", "a button made later is hooked too") sounds.free() later.free() SoundBank.clear() func test_main_menu_shows_the_version_and_commit() -> void: var build: Label = menus.find_child("Build", true, false) as Label assert_not_null(build, "the main menu has a build line") if build != null: assert_eq(build.text, "Version " + BuildInfo.describe())