1 #include <fstream>
2 #include <boost/test/unit_test.hpp>
3 #include <boost/test/test_tools.hpp>
4 #include <boost/test/floating_point_comparison.hpp>
5 using namespace boost::unit_test;
6 #include <cairomm/surface.h>
7 using namespace Cairo;
8
9 static unsigned int test_slot_called = 0;
test_slot(const unsigned char *,unsigned int)10 ErrorStatus test_slot(const unsigned char* /*data*/, unsigned int /*len*/)
11 {
12 test_slot_called++;
13 return CAIRO_STATUS_SUCCESS;
14 }
15
test_write_to_png_stream()16 void test_write_to_png_stream()
17 {
18 auto surface = ImageSurface::create(FORMAT_ARGB32, 1, 1);
19 surface->write_to_png_stream(sigc::ptr_fun(test_slot));
20 BOOST_CHECK(test_slot_called > 0);
21 }
22
test_pdf_constructor_slot()23 void test_pdf_constructor_slot()
24 {
25 test_slot_called = nullptr;
26 auto pdf = PdfSurface::create_for_stream(sigc::ptr_fun(&test_slot), 1, 1);
27 pdf->show_page();
28 pdf->finish();
29 BOOST_CHECK(test_slot_called > 0);
30 }
31
test_ps_constructor_slot()32 void test_ps_constructor_slot()
33 {
34 test_slot_called = 0;
35 auto ps = PsSurface::create_for_stream(sigc::ptr_fun(&test_slot), 1, 1);
36 ps->show_page();
37 ps->finish();
38 BOOST_CHECK(test_slot_called > 0);
39 }
40
test_svg_constructor_slot()41 void test_svg_constructor_slot()
42 {
43 test_slot_called = 0;
44 auto svg = SvgSurface::create_for_stream(sigc::ptr_fun(&test_slot), 1, 1);
45 svg->show_page();
46 svg->finish();
47 BOOST_CHECK(test_slot_called > 0);
48 }
49
50 static std::ifstream png_file;
51 unsigned int test_read_func_called = 0;
test_read_func(unsigned char * data,unsigned int len)52 static ErrorStatus test_read_func(unsigned char* data, unsigned int len)
53 {
54 ++test_read_func_called;
55 if (png_file.read(reinterpret_cast<char*>(data), len))
56 return CAIRO_STATUS_SUCCESS;
57 return CAIRO_STATUS_READ_ERROR;
58 }
59
60 unsigned int c_test_read_func_called = 0;
c_test_read_func(void *,unsigned char * data,unsigned int len)61 static cairo_status_t c_test_read_func(void* /*closure*/, unsigned char* data, unsigned int len)
62 {
63 ++c_test_read_func_called;
64 if (png_file.read(reinterpret_cast<char*>(data), len))
65 return CAIRO_STATUS_SUCCESS;
66 return CAIRO_STATUS_READ_ERROR;
67 }
68
test_create_from_png()69 void test_create_from_png()
70 {
71 RefPtr<ImageSurface> surface;
72 // try the sigc::slot version
73 png_file.open(PNG_STREAM_FILE);
74 surface = ImageSurface::create_from_png_stream(sigc::ptr_fun(&test_read_func));
75 png_file.close();
76 BOOST_CHECK(test_read_func_called > 0);
77
78 // now try the raw C function (deprecated) version
79 png_file.open(PNG_STREAM_FILE);
80 surface = ImageSurface::create_from_png(&c_test_read_func, NULL);
81 png_file.close();
82 BOOST_CHECK(c_test_read_func_called > 0);
83 }
84
test_ps_eps()85 void test_ps_eps()
86 {
87 auto ps = PsSurface::create("test.ps", 1, 1);
88 // check the initial value
89 bool result = ps->get_eps();
90 // set it to the opposite value
91 ps->set_eps(!result);
92 // verify
93 BOOST_CHECK_EQUAL(ps->get_eps(), !result);
94 }
95
test_content()96 void test_content()
97 {
98 auto surface = ImageSurface::create(FORMAT_ARGB32, 1, 1);
99 BOOST_CHECK_EQUAL(surface->get_content(), CONTENT_COLOR_ALPHA);
100 auto similar = Surface::create(surface, CONTENT_ALPHA, 1, 1);
101 BOOST_REQUIRE(similar);
102 BOOST_CHECK_EQUAL(similar->get_content(), CONTENT_ALPHA);
103 }
104
test_fallback_resolution()105 void test_fallback_resolution()
106 {
107 auto surface = ImageSurface::create(FORMAT_ARGB32, 1, 1);
108 double x, y;
109 surface->get_fallback_resolution(x, y);
110 const double new_x = 94, new_y = 123;
111 surface->set_fallback_resolution(new_x, new_y);
112 surface->get_fallback_resolution(x, y);
113 BOOST_CHECK_EQUAL(x, new_x);
114 BOOST_CHECK_EQUAL(y, new_y);
115 }
116
test_show_text_glyphs()117 void test_show_text_glyphs()
118 {
119 // image surface doesn't support show_text_glyphs
120 auto surf = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, 10, 10); \
121 BOOST_CHECK(!surf->has_show_text_glyphs());
122 // but pdf surface should
123 surf = Cairo::PdfSurface::create("test.pdf", 10.0, 10.0);
124 BOOST_CHECK(surf->has_show_text_glyphs());
125 }
126
127
128 test_suite*
init_unit_test_suite(int argc,char * argv[])129 init_unit_test_suite(int argc, char* argv[])
130 {
131 // compile even with -Werror
132 if (argc && argv) {}
133
134 test_suite* test= BOOST_TEST_SUITE( "Cairo::Surface Tests" );
135
136 test->add (BOOST_TEST_CASE (&test_write_to_png_stream));
137 test->add (BOOST_TEST_CASE (&test_pdf_constructor_slot));
138 test->add (BOOST_TEST_CASE (&test_ps_constructor_slot));
139 test->add (BOOST_TEST_CASE (&test_svg_constructor_slot));
140 test->add (BOOST_TEST_CASE (&test_create_from_png));
141 test->add (BOOST_TEST_CASE (&test_ps_eps));
142 test->add (BOOST_TEST_CASE (&test_content));
143 test->add (BOOST_TEST_CASE (&test_show_text_glyphs));
144
145 return test;
146 }
147