1 #include <cxxtest/TestSuite.h>
2 #include <cassert>
3 #include <cstdlib>
5 #include "prefs-utils.h"
6 #include "svg/svg-color.h"
7 #include "svg/svg-icc-color.h"
9 class SVGColorTest : public CxxTest::TestSuite
10 {
11 struct simpleIccCase {
12 unsigned numEntries;
13 bool shouldPass;
14 char const* name;
15 char const* str;
16 };
18 public:
19 void check_rgb24(unsigned const rgb24)
20 {
21 char css[8];
22 prefs_set_int_attribute("options.svgoutput", "usenamedcolors", 0);
23 sp_svg_write_color(css, sizeof(css), rgb24 << 8);
24 TS_ASSERT_EQUALS(sp_svg_read_color(css, 0xff),
25 rgb24 << 8);
26 prefs_set_int_attribute("options.svgoutput", "usenamedcolors", 1);
27 sp_svg_write_color(css, sizeof(css), rgb24 << 8);
28 TS_ASSERT_EQUALS(sp_svg_read_color(css, 0xff),
29 rgb24 << 8);
30 }
32 // createSuite and destroySuite get us per-suite setup and teardown
33 // without us having to worry about static initialization order, etc.
34 static SVGColorTest *createSuite() { return new SVGColorTest(); }
35 static void destroySuite( SVGColorTest *suite ) { delete suite; }
37 void testWrite()
38 {
39 unsigned const components[] = {0, 0x80, 0xff, 0xc0, 0x77};
40 unsigned const nc = G_N_ELEMENTS(components);
41 for (unsigned i = nc*nc*nc; i--;) {
42 unsigned tmp = i;
43 unsigned rgb24 = 0;
44 for (unsigned c = 0; c < 3; ++c) {
45 unsigned const component = components[tmp % nc];
46 rgb24 = (rgb24 << 8) | component;
47 tmp /= nc;
48 }
49 assert( tmp == 0 );
50 check_rgb24(rgb24);
51 }
53 /* And a few completely random ones. */
54 using std::rand;
55 for (unsigned i = 500; i--;) { /* Arbitrary number of iterations. */
56 unsigned const rgb24 = (rand() >> 4) & 0xffffff;
57 check_rgb24(rgb24);
58 }
59 }
61 void testReadColor()
62 {
63 gchar const* val[] = {"#f0f", "#ff00ff", "rgb(255,0,255)", "fuchsia"};
64 size_t const n = sizeof(val)/sizeof(*val);
65 for(size_t i=0; i<n; i++) {
66 gchar const* end = 0;
67 guint32 result = sp_svg_read_color( val[i], &end, 0x3 );
68 TS_ASSERT_EQUALS( result, 0xff00ff00 );
69 TS_ASSERT_LESS_THAN( val[i], end );
70 }
71 }
73 void testIccColor()
74 {
75 simpleIccCase cases[] = {
76 {1, true, "named", "icc-color(named, 3)"},
77 {0, false, "", "foodle"},
78 {1, true, "a", "icc-color(a, 3)"},
79 {4, true, "named", "icc-color(named, 3, 0, 0.1, 2.5)"},
80 {0, false, "", "icc-color(named, 3"},
81 {0, false, "", "icc-color(space named, 3)"},
82 {0, false, "", "icc-color(tab\tnamed, 3)"},
83 {0, false, "", "icc-color(0name, 3)"},
84 {0, false, "", "icc-color(-name, 3)"},
85 {1, true, "positive", "icc-color(positive, +3)"},
86 {1, true, "negative", "icc-color(negative, -3)"},
87 {1, true, "positive", "icc-color(positive, +0.1)"},
88 {1, true, "negative", "icc-color(negative, -0.1)"},
89 {0, false, "", "icc-color(named, value)"},
90 };
92 for ( size_t i = 0; i < G_N_ELEMENTS(cases); i++ ) {
93 SVGICCColor tmp;
94 gchar const* str = cases[i].str;
95 gchar const* result = 0;
97 std::string testDescr( cases[i].str );
99 bool parseRet = sp_svg_read_icc_color( str, &result, &tmp );
100 TSM_ASSERT_EQUALS( testDescr, parseRet, cases[i].shouldPass );
101 TSM_ASSERT_EQUALS( testDescr, tmp.colors.size(), cases[i].numEntries );
102 if ( cases[i].shouldPass ) {
103 TSM_ASSERT_DIFFERS( testDescr, str, result );
104 TSM_ASSERT_EQUALS( testDescr, tmp.colorProfile, std::string(cases[i].name) );
105 } else {
106 TSM_ASSERT_EQUALS( testDescr, str, result );
107 TSM_ASSERT( testDescr, tmp.colorProfile.empty() );
108 }
109 }
110 }
112 };
114 /*
115 Local Variables:
116 mode:c++
117 c-file-style:"stroustrup"
118 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
119 indent-tabs-mode:nil
120 fill-column:99
121 End:
122 */
123 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :