Code

fix include
[inkscape.git] / src / sp-gradient-test.h
2 #ifndef SEEN_SP_GRADIENT_TEST_H
3 #define SEEN_SP_GRADIENT_TEST_H
5 #include "document-using-test.h"
8 #include "sp-gradient.h"
9 #include "svg/svg.h"
10 #include "xml/repr.h"
11 #include <2geom/transforms.h>
12 #include "helper/geom.h"
14 class SPGradientTest : public DocumentUsingTest
15 {
16 public:
17     SPDocument* _doc;
19     SPGradientTest() :
20         _doc(0)
21     {
22     }
24     virtual ~SPGradientTest()
25     {
26         if ( _doc )
27         {
28             sp_document_unref( _doc );
29         }
30     }
32     static void createSuiteSubclass( SPGradientTest *& dst )
33     {
34         SPGradient *gr = static_cast<SPGradient *>(g_object_new(SP_TYPE_GRADIENT, NULL));
35         if ( gr ) {
36             UTEST_ASSERT(gr->gradientTransform.isIdentity());
37             UTEST_ASSERT(gr->gradientTransform == Geom::identity());
38             g_object_unref(gr);
40             dst = new SPGradientTest();
41         }
42     }
44     static SPGradientTest *createSuite()
45     {
46         return Inkscape::createSuiteAndDocument<SPGradientTest>( createSuiteSubclass );
47     }
49     static void destroySuite( SPGradientTest *suite ) { delete suite; }
51 // -------------------------------------------------------------------------
52 // -------------------------------------------------------------------------
54     void testSetGradientTransform()
55     {
56         SPGradient *gr = static_cast<SPGradient *>(g_object_new(SP_TYPE_GRADIENT, NULL));
57         SP_OBJECT(gr)->document = _doc;
59         sp_object_set(SP_OBJECT(gr), SP_ATTR_GRADIENTTRANSFORM, "translate(5, 8)");
60         TS_ASSERT_EQUALS( gr->gradientTransform, Geom::Matrix(Geom::Translate(5, 8)) );
62         sp_object_set(SP_OBJECT(gr), SP_ATTR_GRADIENTTRANSFORM, "");
63         TS_ASSERT_EQUALS( gr->gradientTransform, Geom::identity() );
65         sp_object_set(SP_OBJECT(gr), SP_ATTR_GRADIENTTRANSFORM, "rotate(90)");
66         TS_ASSERT_EQUALS( gr->gradientTransform, Geom::Matrix(rotate_degrees(90)) );
68         g_object_unref(gr);
69     }
72     void testWrite()
73     {
74         SPGradient *gr = static_cast<SPGradient *>(g_object_new(SP_TYPE_GRADIENT, NULL));
75         SP_OBJECT(gr)->document = _doc;
77         sp_object_set(SP_OBJECT(gr), SP_ATTR_GRADIENTTRANSFORM, "matrix(0, 1, -1, 0, 0, 0)");
78         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(_doc);
79         Inkscape::XML::Node *repr = xml_doc->createElement("svg:radialGradient");
80         SP_OBJECT(gr)->updateRepr(repr, SP_OBJECT_WRITE_ALL);
81         {
82             gchar const *tr = repr->attribute("gradientTransform");
83             Geom::Matrix svd;
84             bool const valid = sp_svg_transform_read(tr, &svd);
85             TS_ASSERT( valid );
86             TS_ASSERT_EQUALS( svd, Geom::Matrix(rotate_degrees(90)) );
87         }
89         g_object_unref(gr);
90     }
93     void testGetG2dGetGs2dSetGs2d()
94     {
95         SPGradient *gr = static_cast<SPGradient *>(g_object_new(SP_TYPE_GRADIENT, NULL));
96         SP_OBJECT(gr)->document = _doc;
97         Geom::Matrix const grXform(2, 1,
98                                  1, 3,
99                                  4, 6);
100         gr->gradientTransform = grXform;
101         Geom::Rect const unit_rect(Geom::Point(0, 0), Geom::Point(1, 1));
102         {
103             Geom::Matrix const g2d(sp_gradient_get_g2d_matrix(gr, Geom::identity(), unit_rect));
104             Geom::Matrix const gs2d(sp_gradient_get_gs2d_matrix(gr, Geom::identity(), unit_rect));
105             TS_ASSERT_EQUALS( g2d, Geom::identity() );
106             TS_ASSERT( Geom::matrix_equalp(gs2d, gr->gradientTransform * g2d, 1e-12) );
108             sp_gradient_set_gs2d_matrix(gr, Geom::identity(), unit_rect, gs2d);
109             TS_ASSERT( Geom::matrix_equalp(gr->gradientTransform, grXform, 1e-12) );
110         }
112         gr->gradientTransform = grXform;
113         Geom::Matrix const funny(2, 3,
114                                4, 5,
115                                6, 7);
116         {
117             Geom::Matrix const g2d(sp_gradient_get_g2d_matrix(gr, funny, unit_rect));
118             Geom::Matrix const gs2d(sp_gradient_get_gs2d_matrix(gr, funny, unit_rect));
119             TS_ASSERT_EQUALS( g2d, funny );
120             TS_ASSERT( Geom::matrix_equalp(gs2d, gr->gradientTransform * g2d, 1e-12) );
122             sp_gradient_set_gs2d_matrix(gr, funny, unit_rect, gs2d);
123             TS_ASSERT( Geom::matrix_equalp(gr->gradientTransform, grXform, 1e-12) );
124         }
126         gr->gradientTransform = grXform;
127         Geom::Rect const larger_rect(Geom::Point(5, 6), Geom::Point(8, 10));
128         {
129             Geom::Matrix const g2d(sp_gradient_get_g2d_matrix(gr, funny, larger_rect));
130             Geom::Matrix const gs2d(sp_gradient_get_gs2d_matrix(gr, funny, larger_rect));
131             TS_ASSERT_EQUALS( g2d, Geom::Matrix(3, 0,
132                                               0, 4,
133                                               5, 6) * funny );
134             TS_ASSERT( Geom::matrix_equalp(gs2d, gr->gradientTransform * g2d, 1e-12) );
136             sp_gradient_set_gs2d_matrix(gr, funny, larger_rect, gs2d);
137             TS_ASSERT( Geom::matrix_equalp(gr->gradientTransform, grXform, 1e-12) );
139             sp_object_set(SP_OBJECT(gr), SP_ATTR_GRADIENTUNITS, "userSpaceOnUse");
140             Geom::Matrix const user_g2d(sp_gradient_get_g2d_matrix(gr, funny, larger_rect));
141             Geom::Matrix const user_gs2d(sp_gradient_get_gs2d_matrix(gr, funny, larger_rect));
142             TS_ASSERT_EQUALS( user_g2d, funny );
143             TS_ASSERT( Geom::matrix_equalp(user_gs2d, gr->gradientTransform * user_g2d, 1e-12) );
144         }
145         g_object_unref(gr);
146     }
148 };
151 #endif // SEEN_SP_GRADIENT_TEST_H
153 /*
154   Local Variables:
155   mode:c++
156   c-file-style:"stroustrup"
157   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
158   indent-tabs-mode:nil
159   fill-column:99
160   End:
161 */
162 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :