Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering tests #784

Merged
merged 5 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions druid-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ glib-sys = { version = "0.9.0", optional = true }
gtk-sys = { version = "0.9.0", optional = true }
xcb = { version = "0.9.0", features = ["thread", "xlib_xcb", "randr"], optional = true }

[dev-dependencies]
piet-common = {version = "0.0.12", features = ["png"]}

[target.'cfg(target_os="windows")'.dependencies]
wio = "0.2"

Expand Down
2 changes: 2 additions & 0 deletions druid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ console_log = "0.1.2"

[dev-dependencies]
float-cmp = { version = "0.6.0", default-features = false }
tempfile = "3.1.0"
piet-common = {version = "0.0.12", features = ["png"]}
118 changes: 95 additions & 23 deletions druid/src/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
// limitations under the License.

//! Tools and infrastructure for testing widgets.

use std::path::Path;

use crate::core::{BaseState, CommandQueue};
use crate::piet::{BitmapTarget, Device, Piet};
use crate::piet::{BitmapTarget, Device, Error, ImageFormat, Piet};
use crate::*;

pub(crate) const DEFAULT_SIZE: Size = Size::new(400., 400.);
Expand Down Expand Up @@ -57,37 +60,106 @@ struct Inner<T> {
/// A way to clean up resources when our target goes out of scope.
// the inner type is an option so that we can take ownership in `drop` even
// though self is `& mut`.
struct TargetGuard<'a>(Option<BitmapTarget<'a>>);
pub struct TargetGuard<'a>(Option<BitmapTarget<'a>>);

impl<'a> TargetGuard<'a> {
/// Turns the TargetGuard into a array of pixels
#[allow(dead_code)]
pub fn into_raw(mut self) -> Vec<u8> {
let raw_target = self.0.take().unwrap();
let raw_pixels: Vec<u8> = raw_target.into_raw_pixels(ImageFormat::RgbaPremul).unwrap();
raw_pixels
}

/// Saves the TargetGuard into a png
#[allow(dead_code)]
pub fn into_png<P: AsRef<Path>>(mut self, path: P) -> Result<(), Error> {
let raw_target = self.0.take().unwrap();
raw_target.save_to_file(path)
}
}

impl<T: Data> Harness<'_, T> {
/// Create a new `Harness` with the given data and a root widget,
/// and provide that harness to the passed in function.
///
/// For lifetime reasons™, we cannot just make a harness. It's complicated.
/// I tried my best.
pub fn create(data: T, root: impl Widget<T> + 'static, mut f: impl FnMut(&mut Harness<T>)) {
///
/// This function is a subset of [create_with_render](struct.Harness.html#create_with_render)
pub fn create_simple(
data: T,
root: impl Widget<T> + 'static,
harness_closure: impl FnMut(&mut Harness<T>),
) {
Self::create_with_render(data, root, DEFAULT_SIZE, harness_closure, |_target| {})
}

/// Create a new `Harness` with the given data and a root widget,
/// and provide that harness to the `harness_closure` callback and then the
/// render_context to the `render_context_closure` callback.
///
/// For lifetime reasons™, we cannot just make a harness. It's complicated.
/// I tried my best.
///
/// The with_render version of `create` also has a callback that can be used
/// to save or inspect the painted widget
///
/// # Usage
///
/// The create functions are used to test a widget. The function takes a `root` widget
/// and a data structure and uses them to create a `Harness`. The Harness can then be interacted
/// with via the `harness_closure` callback. The the final render of
/// the widget can be inspected with the `render_context_closure` callback.
///
/// # Arguments
///
/// * `data` - A structure that matches the type of the widget and that will be
/// passed to the `harness_closure` callback via the `Harness` structure.
///
/// * `root` - The widget under test
///
/// * `shape` - The shape of the render_context in the `Harness` structure
///
/// * `harness_closure` - A closure used to interact with the widget under test through the
/// `Harness` structure.
///
/// * `render_context_closure` - A closure used to inspect the final render_context via the `TargetGuard` structure.
///
pub fn create_with_render(
data: T,
root: impl Widget<T> + 'static,
window_size: Size,
mut harness_closure: impl FnMut(&mut Harness<T>),
mut render_context_closure: impl FnMut(TargetGuard),
) {
let mut device = Device::new().expect("harness failed to get device");
let target = device.bitmap_target(400, 400, 2.).expect("bitmap_target");
let target = device
.bitmap_target(window_size.width as usize, window_size.height as usize, 1.0)
.expect("bitmap_target");
let mut target = TargetGuard(Some(target));
let piet = target.0.as_mut().unwrap().render_context();

let desc = WindowDesc::new(|| root);
let window = Window::new(WindowId::next(), Default::default(), desc);

let inner = Inner {
data,
env: theme::init(),
window,
cmds: Default::default(),
};

let mut harness = Harness {
piet,
inner,
window_size: DEFAULT_SIZE,
};
f(&mut harness);
harness.piet.finish().expect("piet finish failed");
{
let piet = target.0.as_mut().unwrap().render_context();

let desc = WindowDesc::new(|| root);
let window = Window::new(WindowId::next(), Default::default(), desc);

let inner = Inner {
data,
env: theme::init(),
window,
cmds: Default::default(),
};

let mut harness = Harness {
piet,
inner,
window_size,
};
harness_closure(&mut harness);
fishrockz marked this conversation as resolved.
Show resolved Hide resolved
harness.piet.finish().expect("piet finish failed");
}
render_context_closure(target)
}

/// Set the size without sending a resize event; intended to be used
Expand Down
8 changes: 4 additions & 4 deletions druid/src/tests/layout_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn simple_layout() {
.with_id(id_1)
.center();

Harness::create(true, widget, |harness| {
Harness::create_simple(true, widget, |harness| {
harness.send_initial_events();
harness.just_layout();
let state = harness.get_state(id_1);
Expand Down Expand Up @@ -64,7 +64,7 @@ fn row_column() {
1.0,
);

Harness::create((), widget, |harness| {
Harness::create_simple((), widget, |harness| {
harness.send_initial_events();
harness.just_layout();
let state1 = harness.get_state(id1);
Expand Down Expand Up @@ -95,7 +95,7 @@ fn simple_paint_rect() {
.background(Color::BLACK)
.center();

Harness::create((), widget, |harness| {
Harness::create_simple((), widget, |harness| {
harness.send_initial_events();
harness.just_layout();

Expand Down Expand Up @@ -170,7 +170,7 @@ fn flex_paint_rect_overflow() {
.padding(10.)
.center();

Harness::create((), widget, |harness| {
Harness::create_simple((), widget, |harness| {
harness.set_initial_size(Size::new(300., 300.));
harness.send_initial_events();
harness.just_layout();
Expand Down
Loading