Skip to content

Commit 22cd067

Browse files
fishrockzcmyr
authored andcommitted
Add basic svg transform tests
1 parent 50fe187 commit 22cd067

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

druid/src/widget/svg.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,120 @@ fn color_from_usvg(paint: &usvg::Paint, opacity: usvg::Opacity) -> Color {
238238
}
239239
}
240240
}
241+
242+
#[cfg(test)]
243+
mod tests {
244+
use super::*;
245+
246+
#[test]
247+
fn translate() {
248+
use crate::tests::harness::Harness;
249+
250+
let svg_data = SvgData::from_str(
251+
"<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2 2'>
252+
<g>
253+
<g>
254+
<rect width='1' height='1'/>
255+
</g>
256+
</g>
257+
<g transform=\"translate(1, 1)\">
258+
<g>
259+
<rect width='1' height='1'/>
260+
</g>
261+
</g>
262+
</svg>",
263+
)
264+
.unwrap();
265+
266+
let svg_widget = Svg::new(svg_data);
267+
268+
Harness::create_with_render(
269+
true,
270+
svg_widget,
271+
Size::new(400., 600.),
272+
|harness| {
273+
harness.send_initial_events();
274+
harness.just_layout();
275+
harness.paint();
276+
},
277+
|target| {
278+
let raw_pixels = target.into_raw();
279+
assert_eq!(raw_pixels.len(), 400 * 600 * 4);
280+
281+
// Being a tall widget with a square image the top and bottom rows will be
282+
// the padding color and the middle rows will not have any padding.
283+
284+
// Check that the middle row 400 pix wide is 200 black then 200 white.
285+
let expecting: Vec<u8> = [
286+
vec![41, 41, 41, 255].repeat(200),
287+
vec![0, 0, 0, 255].repeat(200),
288+
]
289+
.concat();
290+
assert_eq!(raw_pixels[400 * 300 * 4..400 * 301 * 4], expecting[..]);
291+
292+
// Check that all of the last 100 rows are all the background color.
293+
let expecting: Vec<u8> = vec![41, 41, 41, 255].repeat(400 * 100);
294+
assert_eq!(
295+
raw_pixels[400 * 600 * 4 - 4 * 400 * 100..400 * 600 * 4],
296+
expecting[..]
297+
);
298+
},
299+
)
300+
}
301+
302+
#[test]
303+
fn scale() {
304+
use crate::tests::harness::Harness;
305+
306+
let svg_data = SvgData::from_str(
307+
"<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2 2'>
308+
<g>
309+
<g>
310+
<rect width='1' height='1'/>
311+
</g>
312+
</g>
313+
<g transform=\"translate(1, 1)\">
314+
<g transform=\"scale(1, 2)\">
315+
<rect width='1' height='0.5'/>
316+
</g>
317+
</g>
318+
</svg>",
319+
)
320+
.unwrap();
321+
322+
let svg_widget = Svg::new(svg_data);
323+
324+
Harness::create_with_render(
325+
true,
326+
svg_widget,
327+
Size::new(400., 600.),
328+
|harness| {
329+
harness.send_initial_events();
330+
harness.just_layout();
331+
harness.paint();
332+
},
333+
|target| {
334+
let raw_pixels = target.into_raw();
335+
assert_eq!(raw_pixels.len(), 400 * 600 * 4);
336+
337+
// Being a tall widget with a square image the top and bottom rows will be
338+
// the padding color and the middle rows will not have any padding.
339+
340+
// Check that the middle row 400 pix wide is 200 black then 200 white.
341+
let expecting: Vec<u8> = [
342+
vec![41, 41, 41, 255].repeat(200),
343+
vec![0, 0, 0, 255].repeat(200),
344+
]
345+
.concat();
346+
assert_eq!(raw_pixels[400 * 300 * 4..400 * 301 * 4], expecting[..]);
347+
348+
// Check that all of the last 100 rows are all the background color.
349+
let expecting: Vec<u8> = vec![41, 41, 41, 255].repeat(400 * 100);
350+
assert_eq!(
351+
raw_pixels[400 * 600 * 4 - 4 * 400 * 100..400 * 600 * 4],
352+
expecting[..]
353+
);
354+
},
355+
)
356+
}
357+
}

0 commit comments

Comments
 (0)