| 1 | // vtest build: !docker-ubuntu-musl // needs GL/gl.h |
| 2 | import os |
| 3 | import gg |
| 4 | |
| 5 | const vroot = @VEXEROOT |
| 6 | const background_path = os.join_path(vroot, 'examples/flappylearning/assets/img/background.png') |
| 7 | |
| 8 | fn test_get_cached_image_idx_bounds_checking() { |
| 9 | mut ctx := gg.new_context(width: 100) |
| 10 | assert ctx.get_cached_image_by_idx(-1).ok == false |
| 11 | assert ctx.get_cached_image_by_idx(1).ok == false |
| 12 | } |
| 13 | |
| 14 | fn test_remove_cached_image_remove_and_get() { |
| 15 | mut ctx := gg.new_context(width: 100) |
| 16 | image := gg.Image{ |
| 17 | ok: true |
| 18 | } |
| 19 | idx := ctx.cache_image(image) |
| 20 | assert ctx.get_cached_image_by_idx(idx).ok == true |
| 21 | ctx.remove_cached_image_by_idx(idx) |
| 22 | assert ctx.get_cached_image_by_idx(idx).ok == false |
| 23 | } |
| 24 | |
| 25 | fn test_remove_cached_images_by_id_keeps_other_ids_valid() { |
| 26 | mut ctx := gg.new_context(width: 100) |
| 27 | background_bytes := os.read_bytes(background_path)! |
| 28 | prev := ctx.create_image_from_byte_array(background_bytes)! |
| 29 | curr := ctx.create_image_from_byte_array(background_bytes)! |
| 30 | assert prev.id == 0 |
| 31 | assert curr.id == 1 |
| 32 | assert ctx.get_cached_image_by_idx(prev.id).ok == true |
| 33 | assert ctx.get_cached_image_by_idx(curr.id).ok == true |
| 34 | ctx.remove_cached_image_by_idx(prev.id) |
| 35 | ctx.remove_cached_image_by_idx(curr.id) |
| 36 | assert ctx.get_cached_image_by_idx(prev.id).ok == false |
| 37 | assert ctx.get_cached_image_by_idx(curr.id).ok == false |
| 38 | } |
| 39 | |
| 40 | fn test_new_context_sets_borderless_window_flag() { |
| 41 | ctx := gg.new_context( |
| 42 | width: 100 |
| 43 | borderless_window: true |
| 44 | ) |
| 45 | assert ctx.window.borderless_window == true |
| 46 | } |
| 47 | |
| 48 | fn test_new_context_sets_texture_filter() { |
| 49 | ctx := gg.new_context( |
| 50 | width: 100 |
| 51 | texture_filter: .nearest |
| 52 | ) |
| 53 | assert ctx.config.texture_filter == .nearest |
| 54 | } |
| 55 | |
| 56 | fn test_create_image_from_byte_array_loads_rgba_pixels() { |
| 57 | mut ctx := gg.new_context(width: 100) |
| 58 | background_bytes := os.read_bytes(background_path)! |
| 59 | img := ctx.create_image_from_byte_array(background_bytes)! |
| 60 | assert img.width > 0 |
| 61 | assert img.height > 0 |
| 62 | assert img.nr_channels == 4 |
| 63 | assert !isnil(img.data) |
| 64 | assert img.texture_filter == .linear |
| 65 | assert ctx.get_cached_image_by_idx(img.id).nr_channels == 4 |
| 66 | } |
| 67 | |
| 68 | fn test_create_image_from_byte_array_uses_context_texture_filter() { |
| 69 | mut ctx := gg.new_context( |
| 70 | width: 100 |
| 71 | texture_filter: .nearest |
| 72 | ) |
| 73 | background_bytes := os.read_bytes(background_path)! |
| 74 | img := ctx.create_image_from_byte_array(background_bytes)! |
| 75 | assert img.texture_filter == .nearest |
| 76 | assert ctx.get_cached_image_by_idx(img.id).texture_filter == .nearest |
| 77 | } |
| 78 | |
| 79 | fn test_create_image_from_byte_array_with_filter_overrides_context_default() { |
| 80 | mut ctx := gg.new_context(width: 100) |
| 81 | background_bytes := os.read_bytes(background_path)! |
| 82 | img := ctx.create_image_from_byte_array_with_filter(background_bytes, .nearest)! |
| 83 | assert img.texture_filter == .nearest |
| 84 | assert ctx.get_cached_image_by_idx(img.id).texture_filter == .nearest |
| 85 | } |
| 86 | |