-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add snapshot integration tests using egui_kittest (#1)
Builds the app, creates a snapshot, compares with the expected one. Snapshots are disabled on Win/Ubuntu runners on GH actions that don't support wgpu.
- Loading branch information
1 parent
96302d5
commit c7ed234
Showing
15 changed files
with
310 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tests/snapshots/*png filter=lfs diff=lfs merge=lfs -text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
/target | ||
/assets | ||
|
||
/tests/snapshots/*new.png | ||
/tests/snapshots/*diff.png |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
mod kittest_common; | ||
|
||
use eframe::egui; | ||
use env_logger; | ||
|
||
use kittest_common::*; | ||
use maps::app::{AppOptions, AppState, TintOptions}; | ||
|
||
#[test] | ||
fn main() { | ||
env_logger::init(); | ||
|
||
let app_state = AppState::init( | ||
vec![ | ||
_load_meta_with_fake_path(TEST_META_0), | ||
_load_meta_with_fake_path(TEST_META_1), | ||
], | ||
AppOptions { | ||
tint_settings: TintOptions { | ||
tint_for_all: egui::Color32::from_rgba_unmultiplied(255, 255, 255, 127), | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}, | ||
) | ||
.expect("Failed to initialize AppState"); | ||
|
||
snapshot_full_app(app_state, "aligned_view", egui::Vec2::new(1000., 750.)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
mod kittest_common; | ||
|
||
use eframe::egui; | ||
|
||
use kittest_common::*; | ||
use maps::app::{AppOptions, AppState}; | ||
|
||
#[test] | ||
fn empty_start() { | ||
env_logger::init(); | ||
|
||
let app_state = AppState::init( | ||
vec![], | ||
AppOptions { | ||
..Default::default() | ||
}, | ||
) | ||
.expect("Failed to initialize AppState"); | ||
|
||
snapshot_full_app(app_state, "empty_start", egui::Vec2::new(1000., 750.)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::path::PathBuf; | ||
|
||
use eframe::egui; | ||
use eframe::egui::Context; | ||
use egui_kittest::Harness; | ||
|
||
use maps::app::AppState; | ||
use maps::meta::Meta; | ||
|
||
// Expects that cargo test is run from the root of the repository. | ||
#[allow(dead_code)] | ||
pub const TEST_META_0: &str = "data/dummy_maps/dummy_map_lores.yaml"; | ||
#[allow(dead_code)] | ||
pub const TEST_META_1: &str = "data/dummy_maps/dummy_map_rot.yaml"; | ||
|
||
const PIXELS_PER_POINT: f32 = 1.; | ||
|
||
/// Spins up the full app state UI. | ||
/// Does a snapshot diff test unless the "kittest_snapshots" feature is disabled. | ||
/// To create/update baseline snapshots, run: UPDATE_SNAPSHOTS=1 cargo test | ||
pub fn snapshot_full_app(mut app_state: AppState, test_name: &str, size: egui::Vec2) { | ||
let app_closure = |ctx: &Context| { | ||
// This is copypasta from AppState::update(). | ||
// TODO: refactor this once egui_kittest can run eframe::App. | ||
// Waiting for this to be released: | ||
// https://github.com/emilk/egui/commit/46b58e5bcca0bb34861b5671958be872419bee90 | ||
egui::CentralPanel::default().show(ctx, |ui| { | ||
app_state.error_modal(ui); | ||
app_state.quit_modal(ui); | ||
app_state.handle_key_shortcuts(ui); | ||
|
||
app_state.header_panel(ui); | ||
app_state.menu_panel(ui); | ||
app_state.footer_panel(ui); | ||
app_state.settings_panel(ui); | ||
app_state.central_panel(ui); | ||
|
||
app_state.info_window(ui); | ||
}); | ||
|
||
if ctx.input(|i| i.viewport().close_requested()) { | ||
if app_state.status.unsaved_changes { | ||
ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose); | ||
app_state.status.quit_modal_active = true; | ||
} | ||
} | ||
}; | ||
|
||
let mut harness = Harness::builder() | ||
.with_size(size) | ||
.with_pixels_per_point(PIXELS_PER_POINT) | ||
.build(app_closure); | ||
harness.run(); | ||
|
||
#[cfg(feature = "kittest_snapshots")] | ||
harness.wgpu_snapshot(test_name); | ||
|
||
#[cfg(not(feature = "kittest_snapshots"))] | ||
println!( | ||
"Snapshot diff test for {} skipped. \ | ||
Enable the 'kittest_snapshots' feature to run it.", | ||
test_name | ||
); | ||
} | ||
|
||
/// Load the metadata with faked absolute YAML path. | ||
/// Allows to have runner-agnostic snapshots when paths are shown in the UI. | ||
pub fn _load_meta_with_fake_path(meta_path: &str) -> Meta { | ||
let mut meta = Meta::load_from_file(&PathBuf::from(meta_path)).expect("Failed to load map"); | ||
let fake_parent = PathBuf::from("/fake_path_for_testing/"); | ||
meta.yaml_path = fake_parent.join(meta.yaml_path.file_name().unwrap()); | ||
meta | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.