From 7186f72cbeda14bc2465cc05eec2ed97f19f170e Mon Sep 17 00:00:00 2001 From: lucasmerlin Date: Tue, 7 Jan 2025 13:26:57 +0100 Subject: [PATCH] Add a test for comboboxes (#5574) * [x] I have followed the instructions in the PR template --- crates/egui_kittest/tests/regression_tests.rs | 53 ++++++++++++++++++- .../tests/snapshots/combobox_closed.png | 3 ++ .../tests/snapshots/combobox_opened.png | 3 ++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 crates/egui_kittest/tests/snapshots/combobox_closed.png create mode 100644 crates/egui_kittest/tests/snapshots/combobox_opened.png diff --git a/crates/egui_kittest/tests/regression_tests.rs b/crates/egui_kittest/tests/regression_tests.rs index 32b412e6..8567e10e 100644 --- a/crates/egui_kittest/tests/regression_tests.rs +++ b/crates/egui_kittest/tests/regression_tests.rs @@ -1,4 +1,5 @@ -use egui::{Button, Image, Vec2, Widget}; +use egui::accesskit::Role; +use egui::{Button, ComboBox, Image, Vec2, Widget}; use egui_kittest::{kittest::Queryable, Harness}; #[test] @@ -43,3 +44,53 @@ fn image_failed() { #[cfg(all(feature = "wgpu", feature = "snapshot"))] harness.snapshot("image_snapshots"); } + +#[test] +fn test_combobox() { + let items = ["Item 1", "Item 2", "Item 3"]; + let mut harness = Harness::builder() + .with_size(Vec2::new(300.0, 200.0)) + .build_ui_state( + |ui, selected| { + ComboBox::new("combobox", "Select Something").show_index( + ui, + selected, + items.len(), + |idx| *items.get(idx).expect("Invalid index"), + ); + }, + 0, + ); + + harness.run(); + + let mut results = vec![]; + + #[cfg(all(feature = "wgpu", feature = "snapshot"))] + results.push(harness.try_snapshot("combobox_closed")); + + let combobox = harness.get_by_role_and_label(Role::ComboBox, "Select Something"); + combobox.click(); + + harness.run(); + + #[cfg(all(feature = "wgpu", feature = "snapshot"))] + results.push(harness.try_snapshot("combobox_opened")); + + let item_2 = harness.get_by_role_and_label(Role::Button, "Item 2"); + // Node::click doesn't close the popup, so we use simulate_click + item_2.simulate_click(); + + harness.run(); + + assert_eq!(harness.state(), &1); + + // Popup should be closed now + assert!(harness.query_by_label("Item 2").is_none()); + + for result in results { + if let Err(err) = result { + panic!("{}", err); + } + } +} diff --git a/crates/egui_kittest/tests/snapshots/combobox_closed.png b/crates/egui_kittest/tests/snapshots/combobox_closed.png new file mode 100644 index 00000000..18a9d0bc --- /dev/null +++ b/crates/egui_kittest/tests/snapshots/combobox_closed.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:202091634d4483949cb1c5c4c5ec02faa23f4d19e7e833aba135887b77e3188d +size 4485 diff --git a/crates/egui_kittest/tests/snapshots/combobox_opened.png b/crates/egui_kittest/tests/snapshots/combobox_opened.png new file mode 100644 index 00000000..201fc999 --- /dev/null +++ b/crates/egui_kittest/tests/snapshots/combobox_opened.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0a61ecf294d930ebbee9837611d7a75381e690348f448b1c0c8264b27f44ceb3 +size 7535