[librsvg] Add tests for computing geometries of invalid/nonexistent elements



commit 1a331399c3ad6a96374e4b5355894725927d1e49
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Feb 19 09:55:11 2019 -0600

    Add tests for computing geometries of invalid/nonexistent elements

 librsvg_crate/src/lib.rs                    | 13 +++++---
 librsvg_crate/tests/intrinsic-dimensions.rs | 51 ++++++++++++++++++++++++++++-
 rsvg_internals/src/handle.rs                |  2 +-
 rsvg_internals/src/lib.rs                   |  8 ++++-
 4 files changed, 67 insertions(+), 7 deletions(-)
---
diff --git a/librsvg_crate/src/lib.rs b/librsvg_crate/src/lib.rs
index 5f2b6b59..871e815d 100644
--- a/librsvg_crate/src/lib.rs
+++ b/librsvg_crate/src/lib.rs
@@ -102,7 +102,14 @@ use glib::object::Cast;
 use rsvg_internals::{Dpi, Handle, LoadFlags};
 use url::Url;
 
-pub use rsvg_internals::{Length, LengthUnit, LoadingError, RenderingError};
+pub use rsvg_internals::{
+    DefsLookupErrorKind,
+    HrefError,
+    Length,
+    LengthUnit,
+    LoadingError,
+    RenderingError,
+};
 
 /// Full configuration for loading an [`SvgHandle`][SvgHandle].
 ///
@@ -355,9 +362,7 @@ impl<'a> CairoRenderer<'a> {
     }
 
     pub fn get_intrinsic_dimensions(&self) -> IntrinsicDimensions {
-        let d = self.handle
-            .0
-            .get_intrinsic_dimensions();
+        let d = self.handle.0.get_intrinsic_dimensions();
 
         IntrinsicDimensions {
             width: d.width.map(|l| l.to_length()),
diff --git a/librsvg_crate/tests/intrinsic-dimensions.rs b/librsvg_crate/tests/intrinsic-dimensions.rs
index d52b4d4f..188f0b40 100644
--- a/librsvg_crate/tests/intrinsic-dimensions.rs
+++ b/librsvg_crate/tests/intrinsic-dimensions.rs
@@ -6,7 +6,16 @@ extern crate librsvg;
 use gio::MemoryInputStreamExt;
 use glib::Cast;
 
-use librsvg::{IntrinsicDimensions, Length, LengthUnit, LoadOptions, SvgHandle};
+use librsvg::{
+    DefsLookupErrorKind,
+    HrefError,
+    IntrinsicDimensions,
+    Length,
+    LengthUnit,
+    LoadOptions,
+    RenderingError,
+    SvgHandle,
+};
 
 fn load_svg(input: &'static [u8]) -> SvgHandle {
     let stream = gio::MemoryInputStream::new();
@@ -103,3 +112,43 @@ fn element_geometry_with_percent_viewport() {
 
     assert_eq!((ink_r, logical_r), (rect, rect));
 }
+
+#[test]
+fn element_geometry_for_nonexistent_element() {
+    let svg = load_svg(
+        br#"<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg"; width="100%" height="100%"/>
+"#,
+    );
+
+    let renderer = svg.get_cairo_renderer();
+    match renderer.get_geometry_for_element(Some("#foo")) {
+        Err(RenderingError::InvalidId(DefsLookupErrorKind::NotFound)) => (),
+        _ => panic!(),
+    }
+}
+
+#[test]
+fn element_geometry_for_invalid_id() {
+    let svg = load_svg(
+        br#"<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg"; width="100%" height="100%"/>
+"#,
+    );
+
+    let renderer = svg.get_cairo_renderer();
+    match renderer.get_geometry_for_element(Some("foo")) {
+        Err(RenderingError::InvalidId(DefsLookupErrorKind::CannotLookupExternalReferences)) => (),
+        _ => panic!(),
+    }
+
+    match renderer.get_geometry_for_element(Some("foo.svg#foo")) {
+        Err(RenderingError::InvalidId(DefsLookupErrorKind::CannotLookupExternalReferences)) => (),
+        _ => panic!(),
+    }
+
+    match renderer.get_geometry_for_element(Some("")) {
+        Err(RenderingError::InvalidId(DefsLookupErrorKind::HrefError(HrefError::ParseError))) => (),
+        _ => panic!(),
+    }
+}
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index 61ea7b29..8e8b7742 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -388,7 +388,7 @@ impl Handle {
         &self,
         node: &RsvgNode,
     ) -> Result<(RsvgRectangle, RsvgRectangle), RenderingError> {
-        let dimensions = self.get_dimensions()?;
+        let dimensions = self.get_dimensions()?; // replace by 1,1,1,1
         let target = ImageSurface::create(cairo::Format::Rgb24, 1, 1)?;
         let cr = cairo::Context::new(&target);
         let mut draw_ctx = self.create_drawing_ctx_for_node(&cr, &dimensions, Some(node));
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 8421a3d7..6aac6100 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -43,7 +43,13 @@ pub use dpi::{rsvg_rust_set_default_dpi_x_y, Dpi};
 
 pub use drawing_ctx::RsvgRectangle;
 
-pub use error::{rsvg_rust_error_quark, LoadingError, RenderingError};
+pub use error::{
+    rsvg_rust_error_quark,
+    DefsLookupErrorKind,
+    HrefError,
+    LoadingError,
+    RenderingError,
+};
 
 pub use handle::{
     rsvg_handle_rust_close,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]