Crates.io | bevy-autoplay |
lib.rs | bevy-autoplay |
version | 0.1.0 |
source | src |
created_at | 2024-06-09 00:25:59.500339 |
updated_at | 2024-06-09 00:25:59.500339 |
description | Automated integration testing based on recorded play-testing sessions |
homepage | |
repository | https://github.com/tobyselway/bevy-autoplay/ |
max_upload_size | |
id | 1266095 |
size | 128,050 |
Automated integration testing based on recorded play-testing sessions.
use bevy::{input::InputSystem, prelude::*};
use bevy_autoplay::{AutoplayPlugin, AutoplayState, AutoplaySystem, SaveToFile};
fn main() {
App::new()
.add_plugins((DefaultPlugins, AutoplayPlugin)) // Register the AutoplayPlugin
.add_systems(
PreUpdate,
toggle_record.before(AutoplaySystem).after(InputSystem),
)
.add_systems(OnExit(AutoplayState::Recording), after_recording)
.run();
}
// After recording finished, save the session to a file
fn after_recording(mut ev_save: EventWriter<SaveToFile>) {
ev_save.send(SaveToFile("tests/sessions/my_session.gsi".into()));
}
// When F12 pressed, start/stop recording
fn toggle_record(
mut keyboard_input: ResMut<ButtonInput<KeyCode>>,
autoplay_state: Res<State<AutoplayState>>,
mut next_autoplay_state: ResMut<NextState<AutoplayState>>,
) {
// Clear in order to avoid the F12 keypress passing through to the recording
keyboard_input.clear_just_released(KeyCode::F12);
if !keyboard_input.clear_just_pressed(KeyCode::F12) {
return;
}
// If stopped then record, and vice-versa
if *autoplay_state.get() == AutoplayState::Stopped {
next_autoplay_state.set(AutoplayState::Recording);
} else {
next_autoplay_state.set(AutoplayState::Stopped);
}
}
use bevy::{
app::{App, Update},
prelude::*,
};
use bevy_autoplay::testing::{AutoplayTestPlugin, TestResult};
#[test]
fn player_must_press_f_key() {
fn f_pressed(mut result: EventWriter<TestResult>, keyboard_input: Res<ButtonInput<KeyCode>>) {
if keyboard_input.just_pressed(KeyCode::KeyF) { // When F key pressed
result.send(TestResult::Success); // End test with success
}
}
App::new()
// Load recorded session
.add_plugins(AutoplayTestPlugin("tests/sessions/press_f.gsi".into()))
.add_systems(Update, f_pressed)
.run();
}
Licensed under either of
at your option.