| 1 |
/* File: pencil_classifier_composer_test.c; Copyright and License: see below */ |
| 2 |
|
| 3 |
#include "pencil_classifier_composer_test.h" |
| 4 |
#include "pencil_classifier_composer.h" |
| 5 |
#include "test_fixture.h" |
| 6 |
#include "test_expect.h" |
| 7 |
#include "test_environment_assert.h" |
| 8 |
#include "test_case_result.h" |
| 9 |
|
| 10 |
static test_fixture_t * set_up(); |
| 11 |
static void tear_down( test_fixture_t *fix ); |
| 12 |
static test_case_result_t test_expand_space( test_fixture_t *fix ); |
| 13 |
static test_case_result_t test_set_envelope_box( test_fixture_t *fix ); |
| 14 |
static test_case_result_t test_set_envelope_box_too_small( test_fixture_t *fix ); |
| 15 |
|
| 16 |
test_suite_t pencil_classifier_composer_test_get_suite(void) |
| 17 |
{ |
| 18 |
test_suite_t result; |
| 19 |
test_suite_init( &result, |
| 20 |
"pencil_classifier_composer_test_get_suite", |
| 21 |
TEST_CATEGORY_UNIT | TEST_CATEGORY_CONTINUOUS | TEST_CATEGORY_COVERAGE, |
| 22 |
&set_up, |
| 23 |
&tear_down |
| 24 |
); |
| 25 |
test_suite_add_test_case( &result, "test_expand_space", &test_expand_space ); |
| 26 |
test_suite_add_test_case( &result, "test_set_envelope_box", &test_set_envelope_box ); |
| 27 |
test_suite_add_test_case( &result, "test_set_envelope_box_too_small", &test_set_envelope_box_too_small ); |
| 28 |
return result; |
| 29 |
} |
| 30 |
|
| 31 |
struct test_fixture_struct { |
| 32 |
layout_visible_classifier_t layout_vis_classifier; |
| 33 |
data_visible_classifier_t data_vis_classifier; |
| 34 |
data_profile_part_t profile; |
| 35 |
pencil_size_t pencil_size; |
| 36 |
draw_classifier_contour_t draw_classifier_contour; |
| 37 |
cairo_surface_t *surface; |
| 38 |
cairo_t *cr; |
| 39 |
geometry_rectangle_t diagram_bounds; |
| 40 |
PangoLayout *font_layout; |
| 41 |
}; |
| 42 |
typedef struct test_fixture_struct test_fixture_t; /* double declaration as reminder */ |
| 43 |
static test_fixture_t test_fixture; |
| 44 |
|
| 45 |
static test_fixture_t * set_up() |
| 46 |
{ |
| 47 |
test_fixture_t *fix = &test_fixture; |
| 48 |
/* init a pango font layout */ |
| 49 |
{ |
| 50 |
geometry_rectangle_init( &(*fix).diagram_bounds, 0.0, 0.0, 640.0, 480.0 ); |
| 51 |
(*fix).surface = cairo_image_surface_create( CAIRO_FORMAT_ARGB32, |
| 52 |
(uint32_t) geometry_rectangle_get_width( &(*fix).diagram_bounds ), |
| 53 |
(uint32_t) geometry_rectangle_get_height( &(*fix).diagram_bounds ) |
| 54 |
); |
| 55 |
TEST_ENVIRONMENT_ASSERT( CAIRO_STATUS_SUCCESS == cairo_surface_status( (*fix).surface ) ); |
| 56 |
(*fix).cr = cairo_create ( (*fix).surface ); |
| 57 |
TEST_ENVIRONMENT_ASSERT( CAIRO_STATUS_SUCCESS == cairo_status( (*fix).cr ) ); |
| 58 |
(*fix).font_layout = pango_cairo_create_layout ( (*fix).cr ); |
| 59 |
} |
| 60 |
|
| 61 |
data_profile_part_init( &(*fix).profile ); |
| 62 |
|
| 63 |
pencil_size_init( &(*fix).pencil_size, |
| 64 |
geometry_rectangle_get_width( &(*fix).diagram_bounds ), |
| 65 |
geometry_rectangle_get_height( &(*fix).diagram_bounds ) |
| 66 |
); |
| 67 |
|
| 68 |
draw_classifier_contour_init( &(*fix).draw_classifier_contour ); |
| 69 |
|
| 70 |
/* init a layout visible classifier */ |
| 71 |
{ |
| 72 |
data_classifier_t data_classifier; |
| 73 |
data_diagramelement_t data_diagele; |
| 74 |
|
| 75 |
const u8_error_t err1 |
| 76 |
= data_diagramelement_init( &data_diagele, |
| 77 |
17, /* id */ |
| 78 |
32, /* diagram_id */ |
| 79 |
99, /* classifier_id */ |
| 80 |
DATA_DIAGRAMELEMENT_FLAG_NAMED_INSTANCE, |
| 81 |
DATA_ROW_VOID, |
| 82 |
"1ded6d32-cdea-43d8-931c-9459065f8944" |
| 83 |
); |
| 84 |
TEST_ENVIRONMENT_ASSERT_EQUAL_INT( U8_ERROR_NONE, err1 ); |
| 85 |
const u8_error_t err2 |
| 86 |
= data_classifier_init( &data_classifier, |
| 87 |
99, /* id */ |
| 88 |
DATA_CLASSIFIER_TYPE_CLASS, /* main_type */ |
| 89 |
"stereotype", |
| 90 |
"classifier name", |
| 91 |
"classifier\ndescription", |
| 92 |
10000, /* x_order */ |
| 93 |
11000, /* y_order */ |
| 94 |
12000, /* list_order */ |
| 95 |
"94ad5563-3040-4f27-8e8b-d51ffc5ad4c8" |
| 96 |
); |
| 97 |
TEST_ENVIRONMENT_ASSERT_EQUAL_INT( U8_ERROR_NONE, err2 ); |
| 98 |
data_visible_classifier_init( &(*fix).data_vis_classifier, &data_classifier, &data_diagele ); |
| 99 |
layout_visible_classifier_init( &(*fix).layout_vis_classifier, &(*fix).data_vis_classifier ); |
| 100 |
|
| 101 |
data_diagramelement_destroy( &data_diagele ); |
| 102 |
data_classifier_destroy( &data_classifier ); |
| 103 |
} |
| 104 |
return fix; |
| 105 |
} |
| 106 |
|
| 107 |
static void tear_down( test_fixture_t *fix ) |
| 108 |
{ |
| 109 |
assert( fix != NULL ); |
| 110 |
/* destroy the layout visible classifier */ |
| 111 |
{ |
| 112 |
layout_visible_classifier_destroy( &(*fix).layout_vis_classifier ); |
| 113 |
data_visible_classifier_destroy( &(*fix).data_vis_classifier ); |
| 114 |
} |
| 115 |
|
| 116 |
draw_classifier_contour_destroy( &(*fix).draw_classifier_contour ); |
| 117 |
|
| 118 |
pencil_size_destroy( &(*fix).pencil_size ); |
| 119 |
|
| 120 |
data_profile_part_destroy( &(*fix).profile ); |
| 121 |
|
| 122 |
/* destroy the pango font layout */ |
| 123 |
{ |
| 124 |
g_object_unref ( (*fix).font_layout ); |
| 125 |
cairo_destroy ( (*fix).cr ); |
| 126 |
cairo_surface_finish ( (*fix).surface ); |
| 127 |
cairo_surface_destroy ( (*fix).surface ); |
| 128 |
geometry_rectangle_destroy( &(*fix).diagram_bounds ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
static test_case_result_t test_expand_space( test_fixture_t *fix ) |
| 133 |
{ |
| 134 |
assert( fix != NULL ); |
| 135 |
pencil_classifier_composer_t classifier_composer; |
| 136 |
pencil_classifier_composer_init( &classifier_composer ); |
| 137 |
|
| 138 |
const geometry_rectangle_t in_space = { .left = 100.0, .top = 90.0, .width = 420.0, .height = 104.0 }; |
| 139 |
|
| 140 |
for ( unsigned int t_idx = 0; t_idx < DATA_CLASSIFIER_TYPE_COUNT; t_idx ++ ) |
| 141 |
{ |
| 142 |
data_classifier_type_t classifier_type = DATA_CLASSIFIER_TYPE_ARRAY[ t_idx ]; |
| 143 |
data_classifier_set_main_type( data_visible_classifier_get_classifier_ptr( &(*fix).data_vis_classifier ), |
| 144 |
classifier_type |
| 145 |
); |
| 146 |
/* printf(" type: %d, %d\n", t_idx, classifier_type); */ |
| 147 |
|
| 148 |
for ( unsigned int show_children = 0; show_children <= 1; show_children ++ ) |
| 149 |
{ |
| 150 |
/* run composing method */ |
| 151 |
const pencil_error_t err |
| 152 |
= pencil_classifier_composer_expand_space( &classifier_composer, |
| 153 |
&in_space, |
| 154 |
(show_children != 0), |
| 155 |
&(*fix).profile, |
| 156 |
&(*fix).pencil_size, |
| 157 |
(*fix).font_layout, |
| 158 |
&(*fix).layout_vis_classifier |
| 159 |
); |
| 160 |
TEST_EXPECT_EQUAL_INT( PENCIL_ERROR_NONE, err ); |
| 161 |
|
| 162 |
/* check that all layouted rectangles are outside space */ |
| 163 |
const geometry_rectangle_t *const symbol |
| 164 |
= layout_visible_classifier_get_symbol_box_const( &(*fix).layout_vis_classifier ); |
| 165 |
const bool has_contour |
| 166 |
= draw_classifier_contour_has_contour( &(*fix).draw_classifier_contour, classifier_type ); |
| 167 |
if ( ! has_contour ) |
| 168 |
{ |
| 169 |
/* no intesects if fix-size-symbol */ |
| 170 |
TEST_EXPECT_EQUAL_DOUBLE( 0.0, geometry_rectangle_get_intersect_area( &in_space, symbol ) ); |
| 171 |
} |
| 172 |
TEST_EXPECT( ! geometry_rectangle_is_empty( symbol ) ); |
| 173 |
const geometry_rectangle_t *const label |
| 174 |
= layout_visible_classifier_get_label_box_const( &(*fix).layout_vis_classifier ); |
| 175 |
TEST_EXPECT_EQUAL_DOUBLE( 0.0, geometry_rectangle_get_intersect_area( &in_space, label ) ); |
| 176 |
TEST_EXPECT( ! geometry_rectangle_is_empty( label ) ); |
| 177 |
const geometry_rectangle_t *const space |
| 178 |
= layout_visible_classifier_get_space_const( &(*fix).layout_vis_classifier ); |
| 179 |
TEST_EXPECT_EQUAL_DOUBLE( 100.0, geometry_rectangle_get_left( space ) ); |
| 180 |
TEST_EXPECT_EQUAL_DOUBLE( 90.0, geometry_rectangle_get_top( space ) ); |
| 181 |
TEST_EXPECT_EQUAL_DOUBLE( 420.0, geometry_rectangle_get_width( space ) ); |
| 182 |
TEST_EXPECT_EQUAL_DOUBLE( 104.0, geometry_rectangle_get_height( space ) ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
pencil_classifier_composer_destroy( &classifier_composer ); |
| 187 |
return TEST_CASE_RESULT_OK; |
| 188 |
} |
| 189 |
|
| 190 |
static test_case_result_t test_set_envelope_box( test_fixture_t *fix ) |
| 191 |
{ |
| 192 |
assert( fix != NULL ); |
| 193 |
pencil_classifier_composer_t classifier_composer; |
| 194 |
pencil_classifier_composer_init( &classifier_composer ); |
| 195 |
|
| 196 |
const geometry_rectangle_t envelope = { .left = 50.0, .top = 40.0, .width = 460.0, .height = 180.0 }; |
| 197 |
|
| 198 |
for ( unsigned int t_idx = 0; t_idx < DATA_CLASSIFIER_TYPE_COUNT; t_idx ++ ) |
| 199 |
{ |
| 200 |
data_classifier_type_t classifier_type = DATA_CLASSIFIER_TYPE_ARRAY[ t_idx ]; |
| 201 |
data_classifier_set_main_type( data_visible_classifier_get_classifier_ptr( &(*fix).data_vis_classifier ), |
| 202 |
classifier_type |
| 203 |
); |
| 204 |
/* printf(" type: %d, %d\n", t_idx, classifier_type); */ |
| 205 |
|
| 206 |
for ( unsigned int show_children = 0; show_children <= 1; show_children ++ ) |
| 207 |
{ |
| 208 |
/* run composing method */ |
| 209 |
const pencil_error_t err |
| 210 |
= pencil_classifier_composer_set_envelope_box( &classifier_composer, |
| 211 |
&envelope, |
| 212 |
(show_children != 0), |
| 213 |
&(*fix).profile, |
| 214 |
&(*fix).pencil_size, |
| 215 |
(*fix).font_layout, |
| 216 |
&(*fix).layout_vis_classifier |
| 217 |
); |
| 218 |
TEST_EXPECT_EQUAL_INT( PENCIL_ERROR_NONE, err ); |
| 219 |
|
| 220 |
/* check that all layouted rectangles are within envelope */ |
| 221 |
const geometry_rectangle_t *const symbol |
| 222 |
= layout_visible_classifier_get_symbol_box_const( &(*fix).layout_vis_classifier ); |
| 223 |
TEST_EXPECT( geometry_rectangle_is_containing( &envelope, symbol ) ); |
| 224 |
TEST_EXPECT( ! geometry_rectangle_is_empty( symbol ) ); |
| 225 |
const geometry_rectangle_t *const label |
| 226 |
= layout_visible_classifier_get_label_box_const( &(*fix).layout_vis_classifier ); |
| 227 |
TEST_EXPECT( geometry_rectangle_is_containing( &envelope, label ) ); |
| 228 |
TEST_EXPECT( ! geometry_rectangle_is_empty( label ) ); |
| 229 |
const geometry_rectangle_t *const space |
| 230 |
= layout_visible_classifier_get_space_const( &(*fix).layout_vis_classifier ); |
| 231 |
TEST_EXPECT( geometry_rectangle_is_containing( &envelope, space ) ); |
| 232 |
TEST_EXPECT( ! geometry_rectangle_is_empty( space ) ); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
pencil_classifier_composer_destroy( &classifier_composer ); |
| 237 |
return TEST_CASE_RESULT_OK; |
| 238 |
} |
| 239 |
|
| 240 |
static test_case_result_t test_set_envelope_box_too_small( test_fixture_t *fix ) |
| 241 |
{ |
| 242 |
assert( fix != NULL ); |
| 243 |
pencil_classifier_composer_t classifier_composer; |
| 244 |
pencil_classifier_composer_init( &classifier_composer ); |
| 245 |
|
| 246 |
const geometry_rectangle_t small_envelope = { .left = 150.0, .top = 140.0, .width = 15.0, .height = 10.0 }; |
| 247 |
|
| 248 |
for ( unsigned int t_idx = 0; t_idx < DATA_CLASSIFIER_TYPE_COUNT; t_idx ++ ) |
| 249 |
{ |
| 250 |
data_classifier_type_t classifier_type = DATA_CLASSIFIER_TYPE_ARRAY[ t_idx ]; |
| 251 |
data_classifier_set_main_type( data_visible_classifier_get_classifier_ptr( &(*fix).data_vis_classifier ), |
| 252 |
classifier_type |
| 253 |
); |
| 254 |
/* printf(" type: %d, %d\n", t_idx, classifier_type); */ |
| 255 |
|
| 256 |
for ( unsigned int show_children = 0; show_children <= 1; show_children ++ ) |
| 257 |
{ |
| 258 |
/* run composing method */ |
| 259 |
const pencil_error_t err |
| 260 |
= pencil_classifier_composer_set_envelope_box( &classifier_composer, |
| 261 |
&small_envelope, |
| 262 |
(show_children != 0), |
| 263 |
&(*fix).profile, |
| 264 |
&(*fix).pencil_size, |
| 265 |
(*fix).font_layout, |
| 266 |
&(*fix).layout_vis_classifier |
| 267 |
); |
| 268 |
TEST_EXPECT_EQUAL_INT( PENCIL_ERROR_OUT_OF_BOUNDS, err ); |
| 269 |
|
| 270 |
/* check that actual_envelope rectangle is bigger and is within small_envelope */ |
| 271 |
const geometry_rectangle_t actual_envelope |
| 272 |
= layout_visible_classifier_get_envelope_box( &(*fix).layout_vis_classifier ); |
| 273 |
geometry_rectangle_trace( &small_envelope ); |
| 274 |
geometry_rectangle_trace( &actual_envelope ); |
| 275 |
TEST_EXPECT( geometry_rectangle_is_containing( &actual_envelope, &small_envelope ) ); |
| 276 |
TEST_EXPECT( ! geometry_rectangle_is_containing( &small_envelope, &actual_envelope ) ); |
| 277 |
|
| 278 |
/* check that label and space are withing symbol (unless fix-sized-symbol) */ |
| 279 |
const bool has_contour |
| 280 |
= draw_classifier_contour_has_contour( &(*fix).draw_classifier_contour, classifier_type ); |
| 281 |
if ( has_contour ) |
| 282 |
{ |
| 283 |
const geometry_rectangle_t *const symbol |
| 284 |
= layout_visible_classifier_get_symbol_box_const( &(*fix).layout_vis_classifier ); |
| 285 |
const geometry_rectangle_t *const label |
| 286 |
= layout_visible_classifier_get_label_box_const( &(*fix).layout_vis_classifier ); |
| 287 |
const geometry_rectangle_t *const space |
| 288 |
= layout_visible_classifier_get_space_const( &(*fix).layout_vis_classifier ); |
| 289 |
TEST_EXPECT( geometry_rectangle_is_containing( symbol, label ) ); |
| 290 |
TEST_EXPECT( geometry_rectangle_is_containing( symbol, space ) ); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
pencil_classifier_composer_destroy( &classifier_composer ); |
| 296 |
return TEST_CASE_RESULT_OK; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
/* |
| 301 |
* Copyright 2021-2026 Andreas Warnke |
| 302 |
* |
| 303 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 304 |
* you may not use this file except in compliance with the License. |
| 305 |
* You may obtain a copy of the License at |
| 306 |
* |
| 307 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 308 |
* |
| 309 |
* Unless required by applicable law or agreed to in writing, software |
| 310 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 311 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 312 |
* See the License for the specific language governing permissions and |
| 313 |
* limitations under the License. |
| 314 |
*/ |