tests/vector-sprite-sheet.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #undef G_DISABLE_ASSERT | ||
| 2 | |||
| 3 | #include <shumate/shumate.h> | ||
| 4 | #include "shumate/shumate-vector-sprite-sheet.h" | ||
| 5 | |||
| 6 | static void | ||
| 7 | 2 | test_vector_sprite_sheet (void) | |
| 8 | { | ||
| 9 | 4 | g_autoptr(GError) error = NULL; | |
| 10 |
1/2✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 2 times.
|
2 | g_autoptr(ShumateVectorSpriteSheet) sprites = NULL; |
| 11 |
1/2✓ Branch 28 → 29 taken 2 times.
✗ Branch 28 → 30 not taken.
|
2 | g_autoptr(GdkTexture) texture = NULL; |
| 12 |
1/2✓ Branch 26 → 27 taken 2 times.
✗ Branch 26 → 28 not taken.
|
2 | g_autoptr(GBytes) json_bytes = NULL; |
| 13 |
1/2✓ Branch 24 → 25 taken 2 times.
✗ Branch 24 → 26 not taken.
|
2 | g_autoptr(ShumateVectorSprite) sprite = NULL; |
| 14 | |||
| 15 | 2 | json_bytes = g_resources_lookup_data ("/org/gnome/shumate/Tests/sprites.json", G_RESOURCE_LOOKUP_FLAGS_NONE, &error); | |
| 16 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 2 times.
|
2 | g_assert_no_error (error); |
| 17 | 2 | texture = gdk_texture_new_from_resource ("/org/gnome/shumate/Tests/sprites.png"); | |
| 18 | |||
| 19 | 2 | sprites = shumate_vector_sprite_sheet_new (); | |
| 20 | |||
| 21 | 2 | shumate_vector_sprite_sheet_add_page (sprites, texture, g_bytes_get_data (json_bytes, NULL), 1, &error); | |
| 22 |
1/2✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 2 times.
|
2 | g_assert_no_error (error); |
| 23 |
1/2✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 2 times.
|
2 | g_assert_nonnull (sprites); |
| 24 | |||
| 25 | 2 | sprite = shumate_vector_sprite_sheet_get_sprite (sprites, "sprite", 1); | |
| 26 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 2 times.
|
2 | g_assert_true (SHUMATE_IS_VECTOR_SPRITE (sprite)); |
| 27 |
1/2✓ Branch 17 → 18 taken 2 times.
✗ Branch 17 → 19 not taken.
|
2 | g_clear_object (&sprite); |
| 28 | |||
| 29 | 2 | sprite = shumate_vector_sprite_sheet_get_sprite (sprites, "not-a-sprite", 1); | |
| 30 |
1/2✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 2 times.
|
2 | g_assert_null (sprite); |
| 31 |
2/4✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 2 times.
✓ Branch 24 → 25 taken 2 times.
✗ Branch 24 → 26 not taken.
|
2 | g_clear_object (&sprite); |
| 32 | 2 | } | |
| 33 | |||
| 34 | typedef struct { | ||
| 35 | char *expected_name; | ||
| 36 | gboolean called; | ||
| 37 | gboolean return_null; | ||
| 38 | } FallbackData; | ||
| 39 | |||
| 40 | static ShumateVectorSprite * | ||
| 41 | 208 | fallback_func (ShumateVectorSpriteSheet *sprite_sheet, | |
| 42 | const char *name, | ||
| 43 | double scale_factor, | ||
| 44 | gpointer user_data) | ||
| 45 | { | ||
| 46 | 208 | FallbackData *data = user_data; | |
| 47 | 416 | g_autoptr(GdkPaintable) paintable = NULL; | |
| 48 | |||
| 49 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 208 times.
|
208 | g_assert_cmpstr (name, ==, data->expected_name); |
| 50 | 208 | data->called = TRUE; | |
| 51 | |||
| 52 | /* Test reentrancy */ | ||
| 53 | 208 | g_object_unref (shumate_vector_sprite_sheet_get_sprite (sprite_sheet, "sprite", 1)); | |
| 54 | |||
| 55 |
2/2✓ Branch 7 → 8 taken 206 times.
✓ Branch 7 → 12 taken 2 times.
|
208 | if (data->return_null) |
| 56 | return NULL; | ||
| 57 | else | ||
| 58 | { | ||
| 59 | 206 | paintable = GDK_PAINTABLE (gdk_texture_new_from_resource ("/org/gnome/shumate/Tests/sprites.png")); | |
| 60 | 206 | return shumate_vector_sprite_new (paintable); | |
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | static void | ||
| 66 | 2 | test_vector_sprite_sheet_fallback (void) | |
| 67 | { | ||
| 68 | 4 | g_autoptr(GError) error = NULL; | |
| 69 |
1/2✗ Branch 77 → 78 not taken.
✓ Branch 77 → 79 taken 2 times.
|
2 | g_autoptr(ShumateVectorSpriteSheet) sprites = NULL; |
| 70 |
1/2✓ Branch 75 → 76 taken 2 times.
✗ Branch 75 → 77 not taken.
|
2 | g_autoptr(GdkTexture) texture = NULL; |
| 71 |
1/2✓ Branch 73 → 74 taken 2 times.
✗ Branch 73 → 75 not taken.
|
2 | g_autoptr(GBytes) json_bytes = NULL; |
| 72 |
1/2✓ Branch 71 → 72 taken 2 times.
✗ Branch 71 → 73 not taken.
|
2 | g_autoptr(ShumateVectorSprite) sprite = NULL; |
| 73 | 2 | FallbackData *user_data; | |
| 74 | |||
| 75 | 2 | json_bytes = g_resources_lookup_data ("/org/gnome/shumate/Tests/sprites.json", G_RESOURCE_LOOKUP_FLAGS_NONE, &error); | |
| 76 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 2 times.
|
2 | g_assert_no_error (error); |
| 77 | 2 | texture = gdk_texture_new_from_resource ("/org/gnome/shumate/Tests/sprites.png"); | |
| 78 | |||
| 79 | 2 | sprites = shumate_vector_sprite_sheet_new (); | |
| 80 | 2 | shumate_vector_sprite_sheet_add_page (sprites, texture, g_bytes_get_data (json_bytes, NULL), 1, &error); | |
| 81 |
1/2✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 2 times.
|
2 | g_assert_no_error (error); |
| 82 |
1/2✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 2 times.
|
2 | g_assert_nonnull (sprites); |
| 83 | |||
| 84 | 2 | user_data = g_new0 (FallbackData, 1); | |
| 85 | 2 | shumate_vector_sprite_sheet_set_fallback (sprites, fallback_func, user_data, g_free); | |
| 86 | |||
| 87 | /* the fallback function should not be called for sprites in the sheet */ | ||
| 88 | 2 | sprite = shumate_vector_sprite_sheet_get_sprite (sprites, "sprite", 1); | |
| 89 |
1/2✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 2 times.
|
2 | g_assert_true (SHUMATE_IS_VECTOR_SPRITE (sprite)); |
| 90 |
1/2✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 2 times.
|
2 | g_assert_false (user_data->called); |
| 91 |
1/2✓ Branch 21 → 22 taken 2 times.
✗ Branch 21 → 23 not taken.
|
2 | g_clear_object (&sprite); |
| 92 | |||
| 93 | /* the fallback function should provide a sprite */ | ||
| 94 | 2 | user_data->expected_name = "not-a-sprite"; | |
| 95 | 2 | sprite = shumate_vector_sprite_sheet_get_sprite (sprites, "not-a-sprite", 1); | |
| 96 |
1/2✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 2 times.
|
2 | g_assert_true (SHUMATE_IS_VECTOR_SPRITE (sprite)); |
| 97 |
1/2✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 2 times.
|
2 | g_assert_true (user_data->called); |
| 98 | |||
| 99 |
1/2✓ Branch 29 → 30 taken 2 times.
✗ Branch 29 → 31 not taken.
|
2 | g_clear_object (&sprite); |
| 100 | 2 | user_data->called = FALSE; | |
| 101 | |||
| 102 | /* should be cached */ | ||
| 103 | 2 | sprite = shumate_vector_sprite_sheet_get_sprite (sprites, "not-a-sprite", 1); | |
| 104 |
1/2✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 2 times.
|
2 | g_assert_true (SHUMATE_IS_VECTOR_SPRITE (sprite)); |
| 105 |
1/2✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 2 times.
|
2 | g_assert_false (user_data->called); |
| 106 |
1/2✓ Branch 37 → 38 taken 2 times.
✗ Branch 37 → 39 not taken.
|
2 | g_clear_object (&sprite); |
| 107 | |||
| 108 | /* test the case when the fallback function returns NULL */ | ||
| 109 | 2 | user_data->expected_name = "not-a-sprite-2"; | |
| 110 | 2 | user_data->return_null = TRUE; | |
| 111 | 2 | sprite = shumate_vector_sprite_sheet_get_sprite (sprites, "not-a-sprite-2", 1); | |
| 112 |
1/2✗ Branch 40 → 41 not taken.
✓ Branch 40 → 42 taken 2 times.
|
2 | g_assert_null (sprite); |
| 113 |
1/2✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 2 times.
|
2 | g_assert_true (user_data->called); |
| 114 | |||
| 115 | 2 | user_data->called = FALSE; | |
| 116 | |||
| 117 | /* NULL responses should also be cached */ | ||
| 118 | 2 | sprite = shumate_vector_sprite_sheet_get_sprite (sprites, "not-a-sprite-2", 1); | |
| 119 |
1/2✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 2 times.
|
2 | g_assert_null (sprite); |
| 120 |
1/2✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 2 times.
|
2 | g_assert_false (user_data->called); |
| 121 | |||
| 122 | /* test cache purging */ | ||
| 123 | 2 | user_data->expected_name = "cached-sprite"; | |
| 124 | 2 | user_data->return_null = FALSE; | |
| 125 | 2 | sprite = shumate_vector_sprite_sheet_get_sprite (sprites, "cached-sprite", 1); | |
| 126 |
1/2✗ Branch 51 → 52 not taken.
✓ Branch 51 → 53 taken 2 times.
|
2 | g_assert_true (SHUMATE_IS_VECTOR_SPRITE (sprite)); |
| 127 |
1/2✓ Branch 53 → 54 taken 2 times.
✗ Branch 53 → 55 not taken.
|
2 | g_clear_object (&sprite); |
| 128 |
2/2✓ Branch 62 → 56 taken 200 times.
✓ Branch 62 → 63 taken 2 times.
|
202 | for (int i = 0; i < 100; i ++) |
| 129 | { | ||
| 130 | 200 | g_autoptr(ShumateVectorSprite) filler_sprite = NULL; | |
| 131 |
1/2✓ Branch 59 → 60 taken 200 times.
✗ Branch 59 → 61 not taken.
|
400 | g_autofree char *name = g_strdup_printf ("filler-sprite-%d", i); |
| 132 | 200 | user_data->expected_name = name; | |
| 133 | 200 | filler_sprite = shumate_vector_sprite_sheet_get_sprite (sprites, name, 1); | |
| 134 | } | ||
| 135 | 2 | user_data->called = FALSE; | |
| 136 | |||
| 137 | /* should not be cached anymore */ | ||
| 138 | 2 | user_data->expected_name = "cached-sprite"; | |
| 139 | 2 | sprite = shumate_vector_sprite_sheet_get_sprite (sprites, "cached-sprite", 1); | |
| 140 |
1/2✗ Branch 65 → 66 not taken.
✓ Branch 65 → 67 taken 2 times.
|
2 | g_assert_true (SHUMATE_IS_VECTOR_SPRITE (sprite)); |
| 141 |
1/2✗ Branch 67 → 68 not taken.
✓ Branch 67 → 69 taken 2 times.
|
2 | g_assert_true (user_data->called); |
| 142 |
2/4✓ Branch 69 → 70 taken 2 times.
✗ Branch 69 → 71 not taken.
✓ Branch 71 → 72 taken 2 times.
✗ Branch 71 → 73 not taken.
|
2 | g_clear_object (&sprite); |
| 143 | 2 | } | |
| 144 | |||
| 145 | void | ||
| 146 | 2 | test_scale_factor (void) | |
| 147 | { | ||
| 148 | 4 | g_autoptr(GError) error = NULL; | |
| 149 |
1/2✗ Branch 75 → 76 not taken.
✓ Branch 75 → 77 taken 2 times.
|
2 | g_autoptr(ShumateVectorSpriteSheet) sprites = NULL; |
| 150 |
1/2✓ Branch 73 → 74 taken 2 times.
✗ Branch 73 → 75 not taken.
|
2 | g_autoptr(GdkTexture) texture = NULL; |
| 151 |
1/2✓ Branch 71 → 72 taken 2 times.
✗ Branch 71 → 73 not taken.
|
2 | g_autoptr(GBytes) json_bytes = NULL; |
| 152 |
1/2✓ Branch 69 → 70 taken 2 times.
✗ Branch 69 → 71 not taken.
|
2 | g_autoptr(GdkTexture) texture_2x = NULL; |
| 153 |
1/2✓ Branch 67 → 68 taken 2 times.
✗ Branch 67 → 69 not taken.
|
2 | g_autoptr(GBytes) json_bytes_2x = NULL; |
| 154 | 2 | ShumateVectorSprite *sprite; | |
| 155 |
1/2✓ Branch 65 → 66 taken 2 times.
✗ Branch 65 → 67 not taken.
|
2 | g_autoptr(ShumateVectorSprite) new_sprite = NULL; |
| 156 | |||
| 157 | 2 | sprites = shumate_vector_sprite_sheet_new (); | |
| 158 | |||
| 159 | 2 | json_bytes = g_resources_lookup_data ("/org/gnome/shumate/Tests/sprites.json", G_RESOURCE_LOOKUP_FLAGS_NONE, &error); | |
| 160 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 2 times.
|
2 | g_assert_no_error (error); |
| 161 | 2 | texture = gdk_texture_new_from_resource ("/org/gnome/shumate/Tests/sprites.png"); | |
| 162 | 2 | shumate_vector_sprite_sheet_add_page (sprites, texture, g_bytes_get_data (json_bytes, NULL), 1, &error); | |
| 163 |
1/2✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 2 times.
|
2 | g_assert_no_error (error); |
| 164 | |||
| 165 | 2 | json_bytes_2x = g_resources_lookup_data ("/org/gnome/shumate/Tests/sprites@2x.json", G_RESOURCE_LOOKUP_FLAGS_NONE, &error); | |
| 166 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 2 times.
|
2 | g_assert_no_error (error); |
| 167 | 2 | texture_2x = gdk_texture_new_from_resource ("/org/gnome/shumate/Tests/sprites@2x.png"); | |
| 168 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 2 times.
|
2 | g_assert_no_error (error); |
| 169 | 2 | shumate_vector_sprite_sheet_add_page (sprites, texture_2x, g_bytes_get_data (json_bytes_2x, NULL), 2, &error); | |
| 170 |
1/2✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 2 times.
|
2 | g_assert_no_error (error); |
| 171 | |||
| 172 | 2 | sprite = shumate_vector_sprite_sheet_get_sprite (sprites, "sprite", 1); | |
| 173 |
1/2✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 2 times.
|
2 | g_assert_true (SHUMATE_IS_VECTOR_SPRITE (sprite)); |
| 174 |
1/2✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 2 times.
|
2 | g_assert_cmpint (shumate_vector_sprite_get_scale_factor (sprite), ==, 1); |
| 175 |
1/2✗ Branch 29 → 30 not taken.
✓ Branch 29 → 31 taken 2 times.
|
2 | g_assert_cmpint (shumate_vector_sprite_get_width (sprite), ==, 16); |
| 176 |
1/2✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 2 times.
|
2 | g_assert_cmpfloat (gdk_paintable_get_intrinsic_width (shumate_vector_sprite_get_source_paintable (sprite)), ==, 16); |
| 177 |
1/2✓ Branch 35 → 36 taken 2 times.
✗ Branch 35 → 37 not taken.
|
2 | g_clear_object (&sprite); |
| 178 | |||
| 179 | 2 | sprite = shumate_vector_sprite_sheet_get_sprite (sprites, "sprite", 2); | |
| 180 |
1/2✗ Branch 39 → 40 not taken.
✓ Branch 39 → 41 taken 2 times.
|
2 | g_assert_true (SHUMATE_IS_VECTOR_SPRITE (sprite)); |
| 181 |
1/2✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 2 times.
|
2 | g_assert_cmpint (shumate_vector_sprite_get_scale_factor (sprite), ==, 2); |
| 182 |
1/2✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 2 times.
|
2 | g_assert_cmpint (shumate_vector_sprite_get_width (sprite), ==, 16); |
| 183 |
1/2✗ Branch 49 → 50 not taken.
✓ Branch 49 → 51 taken 2 times.
|
2 | g_assert_cmpfloat (gdk_paintable_get_intrinsic_width (shumate_vector_sprite_get_source_paintable (sprite)), ==, 32); |
| 184 |
1/2✓ Branch 51 → 52 taken 2 times.
✗ Branch 51 → 53 not taken.
|
2 | g_clear_object (&sprite); |
| 185 | |||
| 186 | 2 | sprite = shumate_vector_sprite_sheet_get_sprite (sprites, "sprite", 3); | |
| 187 |
1/2✗ Branch 55 → 56 not taken.
✓ Branch 55 → 57 taken 2 times.
|
2 | g_assert_cmpint (shumate_vector_sprite_get_scale_factor (sprite), ==, 2); |
| 188 |
1/2✓ Branch 57 → 58 taken 2 times.
✗ Branch 57 → 59 not taken.
|
2 | g_clear_object (&sprite); |
| 189 | |||
| 190 | 2 | sprite = shumate_vector_sprite_sheet_get_sprite (sprites, "sprite", 0.5); | |
| 191 |
1/2✗ Branch 61 → 62 not taken.
✓ Branch 61 → 63 taken 2 times.
|
2 | g_assert_cmpint (shumate_vector_sprite_get_scale_factor (sprite), ==, 1); |
| 192 |
2/4✓ Branch 63 → 64 taken 2 times.
✗ Branch 63 → 65 not taken.
✓ Branch 65 → 66 taken 2 times.
✗ Branch 65 → 67 not taken.
|
2 | g_clear_object (&sprite); |
| 193 | 2 | } | |
| 194 | |||
| 195 | |||
| 196 | int | ||
| 197 | 2 | main (int argc, char *argv[]) | |
| 198 | { | ||
| 199 | 2 | g_test_init (&argc, &argv, NULL); | |
| 200 | |||
| 201 | 2 | g_test_add_func ("/vector-sprite-sheet/sprites", test_vector_sprite_sheet); | |
| 202 | 2 | g_test_add_func ("/vector-sprite-sheet/fallback", test_vector_sprite_sheet_fallback); | |
| 203 | 2 | g_test_add_func ("/vector-sprite-sheet/scale-factor", test_scale_factor); | |
| 204 | |||
| 205 | 2 | return g_test_run (); | |
| 206 | } | ||
| 207 |