#define _POSIX_C_SOURCE 200809L /* Harness headless do editor: dirige Editor.Scene com um bloco de eventos * falso e inspeciona os comandos de desenho produzidos por on_draw. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static TinyBlocks* rt; static TinyBlocks_Block *ev, *scene; static TinyBlocks_DrawCmd cmds[TB_DRAW_CMD_CAP]; static uint32_t ncmd; static int fails = 0; static int g_mods = 0; static void mods(int m) { g_mods = m; } static void eval_ev(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; self->dirty = false; } static void draw(void) { TinyBlocks_RenderableBlock* rb = NULL; if (scene->type->on_draw(scene, rt, &rb) && rb) { ncmd = rb->count; memcpy(cmds, rb->cmds, ncmd * sizeof(cmds[0])); } tb_renderable_release_all(); } static float g_scroll_dy = 0.f; static void frame(float mx, float my, int ld, int lu, int rd, int sc, const char* text) { tb_block_set_output(ev, GFX_IN_MOUSE_X, tb_value_real(mx)); tb_block_set_output(ev, GFX_IN_MOUSE_Y, tb_value_real(my)); tb_block_set_output(ev, GFX_IN_MOUSE_L_DOWN, tb_value_integer(ld)); tb_block_set_output(ev, GFX_IN_MOUSE_L_UP, tb_value_integer(lu)); tb_block_set_output(ev, GFX_IN_MOUSE_R_DOWN, tb_value_integer(rd)); tb_block_set_output(ev, GFX_IN_KEY, tb_value_integer(sc)); tb_block_set_output(ev, GFX_IN_KEY_MOD, tb_value_integer(g_mods)); tb_block_set_output(ev, GFX_IN_MOD_STATE, tb_value_integer(g_mods)); tb_block_set_output(ev, GFX_IN_TEXT, tb_value_text(text ? text : "")); tb_block_set_output(ev, GFX_IN_DROP_FILE, tb_value_text("")); tb_block_set_output(ev, GFX_IN_SCROLL_DX, tb_value_real(0)); tb_block_set_output(ev, GFX_IN_SCROLL_DY, tb_value_real(g_scroll_dy)); tb_block_set_output(ev, GFX_IN_WIN_W, tb_value_integer(1280)); tb_block_set_output(ev, GFX_IN_WIN_H, tb_value_integer(720)); tb_block_eval(scene, rt); draw(); g_scroll_dy = 0.f; } /* Roda do mouse (usada para o zoom na aba de imagem) */ static void scroll(float mx, float my, float dy) { g_scroll_dy = dy; frame(mx, my, 0, 0, 0, 0, NULL); } enum { R_ANY, R_CAT, R_FLY, R_WS, R_STAGE }; static bool in_region(float x, int r) { switch (r) { case R_CAT: return x < 68; case R_FLY: return x >= 68 && x < 318; case R_WS: return x >= 318 && x < 920; case R_STAGE: return x >= 920; default: return true; } } /* Centro (aprox.) do n-ésimo texto igual a s na região */ static bool find(const char* s, int region, int nth, float* x, float* y) { for (uint32_t i = 0; i < ncmd; i++) { TinyBlocks_DrawCmd* c = &cmds[i]; if (c->kind != TB_DRAW_TEXT || strcmp(c->text, s) != 0 || !in_region(c->x, region)) continue; if (nth-- > 0) continue; *x = c->x + 3; *y = c->y - c->font_size*0.36f; return true; } return false; } /* Bloco "+" na area de trabalho (nao o "+" da barra de abas) */ static bool block_plus(float* x, float* y, float min_y) { for (int k = 0; find("+", R_WS, k, x, y); k++) if (*y > min_y) return true; printf("FAIL não achei o bloco \"+\"\n"); fails++; return false; } /* Texto s na mesma linha de (ax, ay), a direita de ax */ static bool find_near(const char* s, float ax, float ay, float* x, float* y) { for (int k = 0; find(s, R_WS, k, x, y); k++) if (*y > ay - 6 && *y < ay + 6 && *x > ax) return true; return false; } static bool stage_has(const char* s) { for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && cmds[i].x >= 920 && strstr(cmds[i].text, s)) return true; return false; } /* Mensagens de status: palco/saída ou avisos flutuantes (toasts), em qualquer lugar */ static bool any_has(const char* s) { for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && strstr(cmds[i].text, s)) return true; return false; } static bool ws_has(const char* s) { for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && in_region(cmds[i].x, R_WS) && strcmp(cmds[i].text, s) == 0) return true; return false; } static int ws_count(const char* s) { int n = 0; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && in_region(cmds[i].x, R_WS) && strcmp(cmds[i].text, s) == 0) n++; return n; } static bool find_tabbar(const char* s, float* x, float* y) { for (uint32_t i = 0; i < ncmd; i++) { TinyBlocks_DrawCmd* c = &cmds[i]; if (c->kind != TB_DRAW_TEXT || strcmp(c->text, s) != 0) continue; if (c->y < 40 || c->y > 86) continue; *x = c->x + 3; *y = c->y - c->font_size*0.36f; return true; } return false; } static bool find_sub(const char* sub, float* x, float* y) { for (uint32_t i = 0; i < ncmd; i++) { TinyBlocks_DrawCmd* c = &cmds[i]; if (c->kind != TB_DRAW_TEXT || !strstr(c->text, sub)) continue; *x = c->x + 3; *y = c->y - c->font_size*0.36f; return true; } return false; } /* Conta pixels nao-brancos da ultima imagem desenhada no workspace */ /* Largura da maior imagem do workspace (a folha de desenho) */ static float img_draw_w(void) { float best = 0, w = 0; for (uint32_t i = 0; i < ncmd; i++) { if (cmds[i].kind != TB_DRAW_IMAGE || !cmds[i].image || !in_region(cmds[i].x, R_WS)) continue; float a = cmds[i].w * cmds[i].h; if (a > best) { best = a; w = cmds[i].w; } } return w; } static int img_nonwhite(void) { /* A folha de desenho e a MAIOR imagem do workspace (miniaturas sao pequenas) */ const TinyBlocks_Image* im = NULL; float best = 0; for (uint32_t i = 0; i < ncmd; i++) { if (cmds[i].kind != TB_DRAW_IMAGE || !cmds[i].image || !in_region(cmds[i].x, R_WS)) continue; float a = cmds[i].w * cmds[i].h; if (a < 20000.f) continue; /* ignora miniaturas */ if (a > best) { best = a; im = cmds[i].image; } } if (!im) return -1; int W = tb_image_width(im), H = tb_image_height(im), n = 0; for (int y = 0; y < H; y++) for (int x = 0; x < W; x++) { uint8_t r, g, b, a; if (tb_image_get_pixel(im, x, y, &r, &g, &b, &a) && a > 0 && (r < 240 || g < 240 || b < 240)) n++; } return n; } static bool any_substr(const char* s) { for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && strstr(cmds[i].text, s)) return true; return false; } static bool ws_substr(const char* s) { for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && in_region(cmds[i].x, R_WS) && strstr(cmds[i].text, s)) return true; return false; } static void dump_ws(void) { for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && in_region(cmds[i].x, R_WS)) printf(" ws| %s (%.0f,%.0f)\n", cmds[i].text, cmds[i].x, cmds[i].y); } static void dump_stage(void) { for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && cmds[i].x >= 920 && cmds[i].y > 130) printf(" | %s\n", cmds[i].text); } #define CHECK(cond, msg) do { if (cond) printf("ok %s\n", msg); else { printf("FAIL %s\n", msg); fails++; dump_stage(); } } while (0) static void click(float x, float y) { frame(x, y, 1, 0, 0, 0, NULL); frame(x, y, 0, 1, 0, 0, NULL); } static void drag(float x0, float y0, float x1, float y1) { frame(x0, y0, 1, 0, 0, 0, NULL); frame(x0 + 8, y0 + 8, 0, 0, 0, 0, NULL); frame(x1, y1, 0, 0, 0, 0, NULL); frame(x1, y1, 0, 1, 0, 0, NULL); } static void type(const char* s) { frame(0, 0, 0, 0, 0, 0, s); } static void key(int sc, float x, float y) { frame(x, y, 0, 0, 0, sc, NULL); } static void flag(void) { click(920 + 30, 44 + 22); } static bool need(const char* s, int region, int nth, float* x, float* y) { if (find(s, region, nth, x, y)) return true; printf("FAIL não achei \"%s\"\n", s); fails++; return false; } int main(void) { /* configurações isoladas: o harness não lê nem grava o ~/.config real */ setenv("XDG_CONFIG_HOME", "/tmp/tb_harness_cfg", 1); unsetenv("TINYBLOCKS_SETTINGS"); remove("/tmp/tb_harness_cfg/tinyblocks/settings.csv"); tinyblocks_init(&rt, NULL); tb_graphics_set_backend(tb_graphics_null_backend()); tb_library_register(rt, tb_events_get_library()); tb_library_register(rt, tb_control_get_library()); tb_library_register(rt, tb_errors_get_library()); tb_library_register(rt, tb_operators_get_library()); tb_library_register(rt, tb_variables_get_library()); tb_library_register(rt, tb_appearance_get_library()); tb_library_register(rt, tb_pen_get_library()); tb_library_register(rt, tb_curves_get_library()); tb_library_register(rt, tb_base64_get_library()); tb_library_register(rt, tb_ppm_get_library()); tb_library_register(rt, tb_input_get_library()); tb_library_register(rt, tb_graphics_get_library()); tb_library_register(rt, tb_editor_get_library()); (void)eval_ev; ev = tb_block_create(rt, tb_type_block_find(rt, "Graphics.Input")); scene = tb_block_create(rt, tb_type_block_find(rt, "Editor.Scene")); tb_block_set_default(scene, 0, tb_value_block(ev)); float x, y, x2, y2; frame(0, 0, 0, 0, 0, 0, NULL); printf("comandos de desenho no 1º frame: %u (cap %d)\n", ncmd, TB_DRAW_CMD_CAP); CHECK(ncmd > 100 && ncmd < TB_DRAW_CMD_CAP, "frame inicial cabe no RenderableBlock"); /* 1. Script de exemplo */ flag(); CHECK(stage_has("x vale = 1") && stage_has("x vale = 5") && !stage_has("x vale = 6"), "bandeira roda o script de exemplo (x vale = 1..5)"); /* 2. Arrastar "escreva" do flyout para uma área livre e clicar nele */ if (need("Aparência", R_CAT, 0, &x, &y)) click(x, y); if (need("escreva", R_FLY, 0, &x, &y)) drag(x, y, 600, 560); CHECK(find("escreva", R_WS, 1, &x, &y), "bloco do flyout aparece no workspace"); if (need("escreva", R_WS, 1, &x, &y)) click(x, y); CHECK(stage_has("Olá!"), "clique no bloco solto executa (Olá!)"); /* 3. Encaixar o "escreva Olá!" logo abaixo do chapéu */ float hx, hy; if (need("quando a bandeira for clicado", R_WS, 0, &hx, &hy) && need("escreva", R_WS, 1, &x, &y)) { /* hat: rótulo em (bx+10, by+18+18); fundo em by+54 - alvo: topo do bloco em by+54 */ float bx = hx - 3 - 10, by = hy - 18 - 18; float gx = x - 3 - 10, gy = y - 18; /* canto do bloco arrastado */ drag(x, y, x + (bx - gx), y + (by + 54 - gy) + 6); } flag(); CHECK(stage_has("Olá!") && stage_has("x vale = 5"), "encaixe sob o chapéu: script roda os dois"); float oy1 = 0, oy2 = 0; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && cmds[i].x >= 920) { if (!strcmp(cmds[i].text, "Olá!") && !oy1) oy1 = cmds[i].y; if (!strcmp(cmds[i].text, "x vale = 1") && !oy2) oy2 = cmds[i].y; } CHECK(oy1 > 0 && oy2 > oy1, "ordem de execução: Olá! antes de x vale"); /* 4. Repórter "+": solto, editar campos, clicar - balão */ if (need("Operadores", R_CAT, 0, &x, &y)) click(x, y); if (need("+", R_FLY, 0, &x, &y)) drag(x, y, 640, 470); if (need("+", R_WS, 0, &x, &y)) { click(x - 22, y); type("2"); key(TB_KEY_RETURN, 0, 0); if (need("+", R_WS, 0, &x, &y)) { click(x + 22, y); type("3"); key(TB_KEY_RETURN, 0, 0); } if (need("+", R_WS, 0, &x, &y)) { click(x, y); click(x, y); } /* clique duplo */ } CHECK(ws_has("5"), "clique duplo no repórter mostra balão com 5 (2+3)"); /* 5. Encaixar o repórter (2+3) no campo de "mude x para (0)" */ if (need("para", R_WS, 0, &x2, &y2) && need("+", R_WS, 0, &x, &y)) { float zx, zy; int k = 0; while (find("0", R_WS, k, &zx, &zy) && !(zx > x2 && zy > y2 - 4 && zy < y2 + 4)) k++; float tgt_left = zx - 3 - 10, rep_left = x - 3 - 43; drag(x, y, x + (tgt_left - rep_left), y + (zy - y)); } flag(); CHECK(stage_has("x vale = 10") && stage_has("x vale = 6") && !stage_has("x vale = 5"), "repórter encaixado: x começa em 5 (x vale = 6..10)"); /* 6. Delete sobre o "escreva Olá!" apaga só ele; o resto sobe */ if (need("escreva", R_WS, 0, &x, &y)) { float ox, oy; find("Olá!", R_WS, 0, &ox, &oy); if (oy < y) find("escreva", R_WS, 1, &x, &y); /* o do "Olá!" é o primeiro de cima */ key(TB_KEY_DELETE, x, y); } flag(); CHECK(!stage_has("Olá!") && stage_has("x vale = 10"), "Delete remove o bloco e mantém o resto do script"); /* 7. Arrastar de volta para a paleta apaga */ if (need("Aparência", R_CAT, 0, &x, &y)) click(x, y); if (need("escreva", R_FLY, 0, &x, &y)) drag(x, y, 700, 600); int before = 0; while (find("escreva", R_WS, before, &x2, &y2)) before++; float lx = 0, ly = 0; for (int k = 0; find("escreva", R_WS, k, &x2, &y2); k++) if (y2 > ly) { lx = x2; ly = y2; } drag(lx, ly, 150, 400); int after = 0; while (find("escreva", R_WS, after, &x2, &y2)) after++; CHECK(after == before - 1, "soltar bloco sobre a paleta apaga"); /* 8. Bloco composto "dobro(n)" criado pelo diálogo; corpo: devolva n + 1 */ if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); if (need("Criar um Bloco", R_FLY, 0, &x, &y)) click(x, y); if (need("nome do bloco", R_WS, 0, &x, &y)) click(x, y); type("dobro"); if (need("número ou texto", R_WS, 0, &x, &y)) click(x, y); type("n"); key(TB_KEY_RETURN, 0, 0); /* OK no diálogo */ /* como no Scratch: o "defina" aparece na própria aba, com o parâmetro ao lado */ CHECK(ws_has("defina") && ws_count("n") == 1, "Criar um Bloco põe \"defina dobro\" na mesma aba (sem parâmetros soltos)"); CHECK(ws_has("quando a bandeira for clicado"), "sem abrir aba nova: os scripts continuam ali"); hx = 0; hy = 0; need("defina", R_WS, 0, &hx, &hy); { float nx, ny; CHECK(find_near("dobro", hx, hy, &nx, &ny) && find_near("n", nx, ny, &nx, &ny), "o protótipo \"dobro (n)\" aparece dentro do defina"); } if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); if (need("devolva", R_FLY, 0, &x, &y)) drag(x, y, hx, hy + 44); /* embaixo do "defina" */ if (need("Operadores", R_CAT, 0, &x, &y)) click(x, y); if (need("+", R_FLY, 0, &x, &y)) drag(x, y, hx + 300, hy + 110); /* encaixa o parâmetro n no slot esquerdo do "+" */ { float px, py, zx, zy; /* o parâmetro sai arrastando de dentro do "defina" */ float dx0, dy0; if (find_near("dobro", hx, hy, &dx0, &dy0) && find_near("n", dx0, dy0, &px, &py) && block_plus(&x, &y, hy + 20)) { int k = 0; bool got = false; while (find("0", R_WS, k++, &zx, &zy)) if (zy > y - 4 && zy < y + 4 && zx < x) { got = true; break; } if (!got) { printf("FAIL slot esquerdo do + (+ em %.0f,%.0f)\n", x, y); fails++; } else drag(px, py, zx - 3, zy); } CHECK(ws_count("n") == 2, "arrastar o parâmetro do defina cria o reporter \"n\""); } /* digita 1 no slot direito do "+" */ if (block_plus(&x, &y, hy + 20)) { float zx, zy; int k = 0; while (find("0", R_WS, k++, &zx, &zy)) if (zy > y - 4 && zy < y + 4 && zx > x) { click(zx, zy); type("1"); key(TB_KEY_RETURN, 0, 0); break; } } if (block_plus(&x, &y, hy + 20) && need("=", R_WS, 0, &x2, &y2)) { float rep_left = x - 3 - 10; /* o reporter "n" (o outro "n" esta no defina) */ { float px, py; for (int k = 0; find("n", R_WS, k, &px, &py); k++) if (py > hy + 10) { rep_left = px - 3 - 10; break; } } drag(x, y, x + ((x2 + 12) - rep_left), y + (y2 - y)); } if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); CHECK(find("dobro", R_FLY, 0, &x, &y), "bloco dobro aparece em Meus Blocos"); if (need("dobro", R_FLY, 0, &x, &y)) drag(x, y, hx + 330, hy - 30); { float sx, sy; float dx2 = 0, dy2 = 0; for (int k = 0; find("dobro", R_WS, k, &x, &y); k++) if (y < hy) { dx2 = x; dy2 = y; } if (dx2 > 0 && find_near("n", dx2, dy2, &sx, &sy)) { click(sx, sy); key(TB_KEY_BACKSPACE, 0, 0); type("21"); key(TB_KEY_RETURN, 0, 0); } } { int k = 0; float bx = 0, by = 0; while (find("dobro", R_WS, k, &x, &y)) { if (y < hy) { bx = x; by = y; } k++; } /* a chamada, não o "defina" */ if (bx > 0) { click(bx, by); click(bx, by); } } CHECK(ws_has("22"), "bloco composto dobro(21) = 22"); if (!ws_has("22")) { for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind==TB_DRAW_TEXT && in_region(cmds[i].x,R_WS)) printf(" ws: %s (%.0f,%.0f)\n", cmds[i].text, cmds[i].x, cmds[i].y); } /* 9. Laço infinito é interrompido */ if (need("Controle", R_CAT, 0, &x, &y)) click(x, y); if (need("repita até que", R_FLY, 0, &x, &y)) drag(x, y, 700, 300); if (need("repita até que", R_WS, 0, &x, &y)) click(x, y); /* laço sem fim roda quadro a quadro (estilo Scratch): não trava nem aborta */ for (int i = 0; i < 5; i++) frame(0, 0, 0, 0, 0, 0, NULL); CHECK(!stage_has("limite de passos"), "laço sem fim roda sem travar (sem aviso de limite)"); click(1280 - 360 + 76, 44 + 22); /* botão vermelho */ frame(0, 0, 0, 0, 0, 0, NULL); /* 10. Caneta: desenhar no palco embutido */ int circles_before = 0; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_FILL_CIRCLE && cmds[i].x >= 920 && cmds[i].y > 90 && cmds[i].y < 340) circles_before++; if (need("Caneta", R_CAT, 0, &x, &y)) click(x, y); if (need("desenhe círculo em x", R_FLY, 0, &x, &y)) drag(x, y, 600, 650); if (need("desenhe círculo em x", R_WS, 0, &x, &y)) click(x, y); int circles = 0; float cx = 0, cy = 0, cr = 0; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_FILL_CIRCLE && cmds[i].x >= 920 && cmds[i].y > 90 && cmds[i].y < 340) { circles++; cx = cmds[i].x; cy = cmds[i].y; cr = cmds[i].param; } printf(" círculo no palco: (%.1f, %.1f) r=%.1f\n", cx, cy, cr); CHECK(circles == circles_before + 1 && fabsf(cx - (928 + 172)) < 2 && fabsf(cy - (44 + 44 + 1 + 128)) < 2, "bloco da caneta desenha círculo centrado no palco embutido"); if (need("Caneta", R_CAT, 0, &x, &y)) click(x, y); if (need("apague tudo", R_FLY, 0, &x, &y)) drag(x, y, 600, 700); if (need("apague tudo", R_WS, 0, &x, &y)) click(x, y); circles = 0; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_FILL_CIRCLE && cmds[i].x >= 920 && cmds[i].y > 90 && cmds[i].y < 340) circles++; CHECK(circles == circles_before, "\"apague tudo\" limpa o palco"); /* 11. Destacar o palco numa janela própria e recolher de volta */ { click(1280 - 50 + 20, 44 + 22); frame(0, 0, 0, 0, 0, 0, NULL); /* a área do palco sai da janela principal; fica um botão "Palco" no menu */ bool no_menu = false, no_painel = false; float cx = 0, cy = 0; for (uint32_t i = 0; i < ncmd; i++) { if (cmds[i].kind != TB_DRAW_TEXT || strcmp(cmds[i].text, "Palco") != 0) continue; if (cmds[i].y < 44) { no_menu = true; cx = cmds[i].x + 3; cy = 22; } else no_painel = true; } CHECK(no_menu && !no_painel, "palco destacado abre janela própria (botão \"Palco\" no menu)"); if (no_menu) click(cx, cy); /* recolhe no próximo quadro */ frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("Palco", R_ANY, 0, &x, &y) && x > 920 && y > 44, "palco recolhido volta ao painel"); } /* 12. Salvar e abrir projeto (.json) via tinyblocks-blueprint */ const char* proj = "/tmp/tb_harness_proj.json"; remove(proj); if (need("Salvar", R_FLY, 0, &x, &y)) click(x, y); for (int i = 0; i < 16; i++) key(TB_KEY_BACKSPACE, 0, 0); type(proj); key(TB_KEY_RETURN, 0, 0); CHECK(any_has("Projeto salvo"), "salvar projeto escreve o .json"); { /* aviso flutuante (toast) sobre a área de scripts, não no menu */ bool flutua = false; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && strstr(cmds[i].text, "Projeto salvo") && cmds[i].x > 318 && cmds[i].x < 920 && cmds[i].y > 400) flutua = true; CHECK(flutua, "mensagem vira aviso flutuante no canto da área de scripts"); /* salvar de novo: a mesma mensagem vira contador (x2) */ if (need("Salvar", R_FLY, 0, &x, &y)) click(x, y); for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); type(proj); key(TB_KEY_RETURN, 0, 0); if (find("Substituir", R_ANY, 0, &x, &y)) click(x, y); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(any_has("(x2)"), "mensagem repetida vira contador (x2)"); } if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y); for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); type(proj); key(TB_KEY_RETURN, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(any_has("Projeto aberto"), "abrir projeto le o .json"); CHECK(find("quando a bandeira for clicado", R_WS, 0, &x, &y), "estado restaurado apos abrir (chapéu de exemplo de volta)"); remove(proj); /* 13. Abrir um projeto escrito a mao (aba distinta) */ const char* openjson = "/tmp/tb_harness_open.json"; FILE* pf = fopen(openjson, "wb"); if (pf) { fputs("{\"format\":\"tinyblocks-project\",\"version\":1,\"cur_tab\":0," "\"tabs\":[{\"name\":\"Aberto!\",\"composite\":false,\"comp_id\":-1,\"color\":0," "\"blocks\":[" "{\"i\":0,\"type\":\"Events.WhenRun\",\"x\":40,\"y\":40,\"next\":1}," "{\"i\":1,\"type\":\"IO.Print\",\"x\":40,\"y\":100,\"in_text\":[\"Oi do arquivo\",\"\"]}," "{\"i\":2,\"type\":\"Stage.Circle\",\"x\":40,\"y\":180,\"in_text\":[\"0\",\"0\",\"80\"]}" "],\"top\":[0,2]}]," "\"user_blocks\":[],\"variables\":[{\"name\":\"x\",\"kind\":0,\"num\":7,\"text\":\"\"}]}", pf); fclose(pf); } if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y); for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); type(openjson); key(TB_KEY_RETURN, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("Aberto!", R_ANY, 0, &x, &y), "abrir projeto de arquivo externo (aba Aberto!)"); CHECK(find("Oi do arquivo", R_WS, 0, &x, &y), "bloco do arquivo externo aparece no workspace"); remove(openjson); /* 13b. Explorador de arquivos (tinyblocks-editor-files) */ { const char* fdir = "/tmp/tb_harness_fdlg"; char fp[256]; mkdir(fdir, 0755); snprintf(fp, sizeof(fp), "%s/sub", fdir); mkdir(fp, 0755); snprintf(fp, sizeof(fp), "%s/nota.md", fdir); FILE* tf = fopen(fp, "wb"); if (tf) { fputs("oi", tf); fclose(tf); } snprintf(fp, sizeof(fp), "%s/proj.json", fdir); tf = fopen(fp, "wb"); if (tf) { fputs("{\"format\":\"tinyblocks-project\",\"version\":1,\"cur_tab\":0," "\"tabs\":[{\"name\":\"Explorado\",\"composite\":false,\"comp_id\":-1,\"color\":0," "\"blocks\":[{\"i\":0,\"type\":\"Events.WhenRun\",\"x\":40,\"y\":40}]," "\"top\":[0]}],\"user_blocks\":[],\"variables\":[]}", tf); fclose(tf); } if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y); CHECK(find("Abrir projeto", R_ANY, 0, &x, &y), "Abrir mostra o explorador de arquivos"); type(fdir); key(TB_KEY_RETURN, 0, 0); CHECK(find("proj.json", R_ANY, 0, &x, &y) && find("sub", R_ANY, 0, &x, &y) && !find("nota.md", R_ANY, 0, &x, &y), "explorador lista pastas + .json e filtra o resto"); if (need("proj.json", R_ANY, 0, &x, &y)) { click(x, y); click(x, y); } frame(0, 0, 0, 0, 0, 0, NULL); CHECK(any_has("Projeto aberto") && find("Explorado", R_ANY, 0, &x, &y), "duplo clique no explorador abre o projeto"); if (need("Salvar", R_FLY, 0, &x, &y)) click(x, y); CHECK(find("Salvar projeto", R_ANY, 0, &x, &y), "Salvar mostra o explorador de arquivos"); for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); type("proj"); key(TB_KEY_RETURN, 0, 0); CHECK(find("Substituir", R_ANY, 0, &x, &y), "salvar sobre arquivo existente pede confirmacao"); if (need("Substituir", R_ANY, 0, &x, &y)) click(x, y); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(any_has("Projeto salvo") && !find("Salvar projeto", R_ANY, 0, &x, &y), "Substituir grava o .json (extensao completada) e fecha"); if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y); key(TB_KEY_ESCAPE, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(!find("Abrir projeto", R_ANY, 0, &x, &y), "Esc fecha o explorador"); remove(fp); snprintf(fp, sizeof(fp), "%s/nota.md", fdir); remove(fp); snprintf(fp, sizeof(fp), "%s/sub", fdir); rmdir(fp); rmdir(fdir); } /* 13c. "desenhe [lista de pontos]" (Caneta) + tipo do valor no balao */ { const char* dj = "/tmp/tb_harness_draw.json"; FILE* df = fopen(dj, "wb"); if (df) { fputs("{\"format\":\"tinyblocks-project\",\"version\":1,\"cur_tab\":0," "\"tabs\":[{\"name\":\"Desenho\",\"composite\":false,\"comp_id\":-1,\"color\":0," "\"blocks\":[" "{\"i\":0,\"type\":\"Events.WhenRun\",\"x\":40,\"y\":40,\"next\":1}," "{\"i\":1,\"type\":\"Stage.Draw\",\"x\":40,\"y\":100,\"parent\":0,\"prel\":0," "\"args\":[2,-1,-1],\"in_text\":[\"\",\"0\",\"0\"]}," "{\"i\":2,\"type\":\"Curves.Line\",\"x\":0,\"y\":0,\"parent\":1,\"prel\":3," "\"in_text\":[\"-100\",\"0\",\"100\",\"50\",\"10\"]}," "{\"i\":3,\"type\":\"Curves.Line\",\"x\":40,\"y\":220," "\"in_text\":[\"0\",\"0\",\"100\",\"0\",\"20\"]}" "],\"top\":[0,3]}],\"user_blocks\":[],\"variables\":[]}", df); fclose(df); } if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y); for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); type(dj); key(TB_KEY_RETURN, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("Desenho", R_ANY, 0, &x, &y), "projeto com 'desenhe' aberto"); int before = 0, after = 0; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_STROKE_LINE && cmds[i].x >= 920) before++; flag(); frame(0, 0, 0, 0, 0, 0, NULL); for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_STROKE_LINE && cmds[i].x >= 920) after++; CHECK(after >= before + 10, "desenhe (reta com 10 passos) traca 10 segmentos no palco"); /* repórter solto (o encaixado roda a pilha inteira, como no Scratch) */ if (need("reta de x", R_WS, 1, &x, &y)) { click(x, y); click(x, y); } frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("lista de pontos", R_WS, 0, &x, &y), "balao mostra o tipo 'lista de pontos'"); remove(dj); } /* 13d. "sempre": uma volta por quadro quando desenha; para no botão vermelho */ { const char* fj = "/tmp/tb_harness_forever.json"; FILE* ff = fopen(fj, "wb"); if (ff) { fputs("{\"format\":\"tinyblocks-project\",\"version\":1,\"cur_tab\":0," "\"tabs\":[{\"name\":\"Sempre\",\"composite\":false,\"comp_id\":-1,\"color\":0," "\"blocks\":[" "{\"i\":0,\"type\":\"Events.WhenRun\",\"x\":40,\"y\":40,\"next\":1}," "{\"i\":1,\"type\":\"Control.Forever\",\"x\":40,\"y\":100,\"parent\":0,\"prel\":0,\"sub\":[2,-1]}," "{\"i\":2,\"type\":\"IO.Print\",\"x\":0,\"y\":0,\"parent\":1,\"prel\":1,\"next\":3," "\"in_text\":[\"volta\",\"\"]}," "{\"i\":3,\"type\":\"Stage.Circle\",\"x\":0,\"y\":0,\"parent\":2,\"prel\":0," "\"in_text\":[\"0\",\"0\",\"30\"]}" "],\"top\":[0]}],\"user_blocks\":[],\"variables\":[]}", ff); fclose(ff); } if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y); for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); type(fj); key(TB_KEY_RETURN, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("sempre", R_WS, 0, &x, &y), "bloco 'sempre' aparece no projeto"); flag(); for (int i = 0; i < 6; i++) frame(0, 0, 0, 0, 0, 0, NULL); int voltas = 0; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && cmds[i].x >= 920 && strstr(cmds[i].text, "volta")) voltas++; CHECK(voltas >= 4 && voltas <= 12, "'sempre' com caneta anima: ~uma volta por quadro"); click(1280 - 360 + 76, 44 + 22); /* botão vermelho */ for (int i = 0; i < 6; i++) frame(0, 0, 0, 0, 0, 0, NULL); int depois = 0; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && cmds[i].x >= 920 && strstr(cmds[i].text, "volta")) depois++; CHECK(depois == 0, "botão vermelho para o 'sempre' (e limpa a saída)"); remove(fj); } /* 13e. Sensores: posição do mouse no palco e última tecla, num "sempre" */ { const char* sj = "/tmp/tb_harness_sensores.json"; FILE* sf = fopen(sj, "wb"); if (sf) { fputs("{\"format\":\"tinyblocks-project\",\"version\":1,\"cur_tab\":0," "\"tabs\":[{\"name\":\"Sensores\",\"composite\":false,\"comp_id\":-1,\"color\":0," "\"blocks\":[" "{\"i\":0,\"type\":\"Events.WhenRun\",\"x\":40,\"y\":40,\"next\":1}," "{\"i\":1,\"type\":\"Control.Forever\",\"x\":40,\"y\":100,\"parent\":0,\"prel\":0,\"sub\":[2,-1]}," "{\"i\":2,\"type\":\"Stage.Clear\",\"x\":0,\"y\":0,\"parent\":1,\"prel\":1,\"next\":3}," "{\"i\":3,\"type\":\"IO.Print\",\"x\":0,\"y\":0,\"parent\":2,\"prel\":0,\"args\":[4,5]}," "{\"i\":4,\"type\":\"Input.MouseX\",\"x\":0,\"y\":0,\"parent\":3,\"prel\":3}," "{\"i\":5,\"type\":\"Input.LastKey\",\"x\":0,\"y\":0,\"parent\":3,\"prel\":4}" "],\"top\":[0]}],\"user_blocks\":[],\"variables\":[]}", sf); fclose(sf); } if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y); for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); type(sj); key(TB_KEY_RETURN, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); flag(); /* vista do palco no painel: origem (929, 89), 342 px = 480 do palco; x do palco 100 -> 929 + 171 + 100 * 342/480 */ float px = 929.f + 171.f + 100.f * 342.f / 480.f, py = 89.f + 100.f; for (int i = 0; i < 3; i++) frame(px, py, 0, 0, 0, 0, NULL); CHECK(stage_has("100"), "posição x do mouse sobre o palco = 100"); if (!stage_has("100")) for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && cmds[i].x >= 920) printf(" palco: '%s'\n", cmds[i].text); frame(px, py, 0, 0, 0, TB_KEY_SPACE, NULL); for (int i = 0; i < 3; i++) frame(px, py, 0, 0, 0, 0, NULL); CHECK(stage_has("espaço"), "última tecla = espaço"); click(1280 - 360 + 76, 44 + 22); /* botão vermelho */ frame(0, 0, 0, 0, 0, 0, NULL); remove(sj); } /* 14. Exportar pelo menu de contexto do botao Exportar */ if (need("Exportar", R_FLY, 0, &x, &y)) click(x, y); CHECK(find("Exportar JSON", R_FLY, 0, &x, &y) && find("Exportar XML", R_FLY, 0, &x, &y) && find("Exportar CSV", R_FLY, 0, &x, &y), "botao Exportar abre menu com JSON/XML/CSV"); const char* expf = "/tmp/tb_harness_export.xml"; remove(expf); if (need("Exportar XML", R_FLY, 0, &x, &y)) click(x, y); for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); type(expf); key(TB_KEY_RETURN, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(any_has("Exportado xml"), "exportar XML pelo menu escreve o arquivo"); remove(expf); /* 15. Regressao: arrastar um bloco da Caneta para o chapeu */ if (need("Caneta", R_CAT, 0, &x, &y)) click(x, y); frame(0, 0, 0, 0, 0, 0, NULL); int clear_before = 0; { float a, b; for (int k = 0; find("apague tudo", R_WS, k, &a, &b); k++) clear_before++; } if (need("apague tudo", R_FLY, 0, &x, &y)) { float hx, hy; if (need("quando a bandeira for clicado", R_WS, 0, &hx, &hy)) { float bx = hx - 3 - 10, by = hy - 18 - 18; float gx = x - 3 - 10, gy = y - 18; drag(x, y, x + (bx - gx), y + (by + 54 - gy) + 6); } } frame(0, 0, 0, 0, 0, 0, NULL); int clear_after = 0; { float a, b; for (int k = 0; find("apague tudo", R_WS, k, &a, &b); k++) clear_after++; } CHECK(clear_after == clear_before + 1, "arrastar bloco da Caneta para o chapeu"); /* 16. Gestor de libraries (tinyblocks-libman), UI desenhada pelo editor */ CHECK(!find("Blueprint", R_CAT, 0, &x, &y), "categoria Blueprint ainda nao existe"); if (need("Bibliotecas", R_WS, 0, &x, &y)) click(x, y); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("Escolha uma Extensão", R_ANY, 0, &x, &y), "abre a tela do libman"); CHECK(find("Buscar", R_ANY, 0, &x, &y), "libman mostra o campo de busca"); type("blueprint"); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("tinyblocks-blueprint", R_ANY, 0, &x, &y), "busca filtra por nome"); if (need("tinyblocks-blueprint", R_ANY, 0, &x, &y)) click(x, y); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(!find("Escolha uma Extensão", R_ANY, 0, &x, &y), "libman fecha apos carregar"); CHECK(find("Blueprint", R_CAT, 0, &x, &y), "library carregada aparece na paleta"); /* 17. Botao de extensoes no fim do painel (estilo Scratch) */ frame(0, 0, 0, 0, 0, 0, NULL); click(34, 720 - 28); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("Escolha uma Extensão", R_ANY, 0, &x, &y), "botao de extensoes no painel abre o libman"); key(TB_KEY_ESCAPE, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); /* 18. Tela de Configuracoes (settings da tinyblocks-environ) */ remove("build/settings_test.csv"); setenv("TINYBLOCKS_SETTINGS", "build/settings_test.csv", 1); click(1231, 22); /* botao Config (WIN_W=1280) */ frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("Configurações", R_ANY, 0, &x, &y), "botao Config abre a tela de configuracoes"); CHECK(find("Detectar caminho de TINYBLOCKS_SETTINGS", R_ANY, 0, &x, &y), "mostra o toggle da variavel de ambiente"); CHECK(find("autor", R_ANY, 0, &x, &y), "listas as settings do store (autor)"); click(50, 84); /* liga o toggle de deteccao pelo env */ frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("build/settings_test.csv", R_ANY, 0, &x, &y), "toggle passa a usar o caminho de TINYBLOCKS_SETTINGS"); click(200, 194); /* seleciona a linha 'autor' */ type("maria"); key(TB_KEY_RETURN, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); click(922, 136); /* botao Salvar (CSV no arquivo de config) */ frame(0, 0, 0, 0, 0, 0, NULL); { FILE* sf = fopen("build/settings_test.csv", "rb"); char sbuf[512] = ""; if (sf) { size_t sn = fread(sbuf, 1, sizeof(sbuf)-1, sf); sbuf[sn] = 0; fclose(sf); } CHECK(strstr(sbuf, "# arquivo_config,build/settings_test.csv") != NULL, "CSV inclui o caminho do arquivo de configuracao"); CHECK(strstr(sbuf, "chave,valor") != NULL, "settings salvas em CSV (cabecalho)"); CHECK(strstr(sbuf, "autor,maria") != NULL, "valor editado gravado no CSV"); remove("build/settings_test.csv"); } key(TB_KEY_ESCAPE, 0, 0); /* fecha a tela */ frame(0, 0, 0, 0, 0, 0, NULL); CHECK(!find("Configurações", R_ANY, 0, &x, &y), "Esc fecha as configuracoes"); unsetenv("TINYBLOCKS_SETTINGS"); /* 19. Previa de resultado: curva (lista de pontos) em balão, clique duplo */ if (need("Curvas", R_CAT, 0, &x, &y)) click(x, y); frame(0, 0, 0, 0, 0, 0, NULL); if (need("círculo em (", R_FLY, 0, &x, &y)) drag(x, y, 620, 500); frame(0, 0, 0, 0, 0, 0, NULL); if (need("círculo em (", R_WS, 0, &x, &y)) { click(x, y); click(x, y); } frame(0, 0, 0, 0, 0, 0, NULL); { int lines = 0; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_STROKE_LINE && in_region(cmds[i].x, R_WS)) lines++; CHECK(lines >= 20, "clique duplo na curva desenha a polilinha no balão"); } CHECK(ws_has("37 pontos"), "balão mostra a contagem de pontos da curva"); /* 20. Previa de imagem: PPM.New em balão desenha TB_DRAW_IMAGE */ if (need("PPM", R_CAT, 0, &x, &y)) click(x, y); frame(0, 0, 0, 0, 0, 0, NULL); if (need("nova imagem(", R_FLY, 0, &x, &y)) drag(x, y, 640, 520); frame(0, 0, 0, 0, 0, 0, NULL); if (need("nova imagem(", R_WS, 0, &x, &y)) { click(x, y); click(x, y); } frame(0, 0, 0, 0, 0, 0, NULL); { int imgs = 0; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_IMAGE) imgs++; CHECK(imgs >= 1, "clique duplo numa imagem desenha TB_DRAW_IMAGE no balão"); } CHECK(ws_has("32x32"), "balão mostra as dimensões da imagem"); /* 21. Tooltip de metadados: Alt + hover sobre um bloco (Stage.Clear) */ if (need("Caneta", R_CAT, 0, &x, &y)) click(x, y); frame(0, 0, 0, 0, 0, 0, NULL); if (need("apague tudo", R_FLY, 0, &x, &y)) drag(x, y, 520, 430); frame(0, 0, 0, 0, 0, 0, NULL); { float bx = 0, by = 0; if (find("apague tudo", R_WS, 0, &bx, &by)) { mods(TB_MOD_ALT); frame(bx, by, 0, 0, 0, 0, NULL); CHECK(any_substr("eval_clear"), "Alt+hover mostra o tooltip com a funcao C"); CHECK(any_substr("tb_pen.c"), "tooltip mostra o arquivo-fonte"); CHECK(any_substr("Limpa o palco"), "tooltip mostra a descricao (doc)"); /* clica na previa do codigo -> abre aba de codigo na linha */ float px = 0, py = 0; if (find_sub("tb_stage_clear", &px, &py)) { mods(0); click(px, py); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(any_substr("tb_pen.c"), "clique na previa abre aba de codigo do arquivo"); CHECK(ws_substr("eval_clear"), "aba de codigo mostra o codigo na linha da funcao"); } else { mods(0); CHECK(false, "achei a previa do codigo no tooltip"); } } else { CHECK(false, "achei o bloco 'apague tudo' no workspace"); } } /* 22. Botao "+" -> nova aba: Editor de Codigo */ key(TB_KEY_ESCAPE, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); { float px = 0, py = 0; if (find_tabbar("+", &px, &py)) click(px, py); else { CHECK(false, "achei o botao +"); } frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("Editor de Código", R_ANY, 0, &x, &y) && find("Novo bloco", R_ANY, 0, &x, &y) && find("Blueprint", R_ANY, 0, &x, &y), "menu '+' lista Blueprint, Novo bloco, Imagem e Código"); CHECK(find("Edição de Imagem", R_ANY, 0, &x, &y), "menu '+' oferece Edição de Imagem"); if (need("Editor de Código", R_ANY, 0, &x, &y)) click(x, y); frame(0, 0, 0, 0, 0, 0, NULL); type("int x = 42;"); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(ws_has("int"), "aba de codigo realca 'int' (palavra-chave)"); CHECK(ws_has("42"), "aba de codigo realca o numero 42"); CHECK(ws_has(";"), "aba de codigo realca a pontuacao"); CHECK(!any_has(""), "aba de codigo nao mostra a marcacao "); } /* 23. Botao "+" -> nova aba: Edição de Imagem */ key(TB_KEY_ESCAPE, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); { float px = 0, py = 0; if (find_tabbar("+", &px, &py)) click(px, py); else { CHECK(false, "achei o botao +"); } frame(0, 0, 0, 0, 0, 0, NULL); if (need("Edição de Imagem", R_ANY, 0, &x, &y)) click(x, y); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("Camadas", R_WS, 0, &x, &y), "aba de imagem mostra a lista de camadas"); CHECK(find("Salvar", R_WS, 0, &x, &y), "aba de imagem mostra o botao Salvar"); CHECK(find("Desfazer", R_WS, 0, &x, &y), "aba de imagem mostra o botao Desfazer"); CHECK(find("Cores", R_WS, 0, &x, &y), "aba de imagem mostra a coluna de cores"); CHECK(find("Paleta", R_WS, 0, &x, &y), "aba de imagem mostra a paleta rapida"); CHECK(x > 700, "cores ficam na lateral direita"); { int imgs = 0; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_IMAGE && in_region(cmds[i].x, R_WS)) imgs++; CHECK(imgs >= 1, "aba de imagem desenha a folha de desenho"); } } /* 24. Pintar na aba de imagem (ferramenta pincel) */ { int before = img_nonwhite(); CHECK(before == 0, "folha de desenho comeca em branco"); click(345, 192); /* botao da ferramenta pincel */ frame(0, 0, 0, 0, 0, 0, NULL); drag(500, 300, 560, 400); /* pinta um traco no canvas */ frame(0, 0, 0, 0, 0, 0, NULL); int after = img_nonwhite(); CHECK(after > 0, "pincel pinta pixels na folha de desenho"); click(421, 703); /* botao Desfazer */ frame(0, 0, 0, 0, 0, 0, NULL); CHECK(img_nonwhite() == 0, "Desfazer volta a folha em branco"); click(345, 292); /* ferramenta retangulo */ frame(0, 0, 0, 0, 0, 0, NULL); drag(560, 280, 680, 360); frame(0, 0, 0, 0, 0, 0, NULL); int rect_px = img_nonwhite(); CHECK(rect_px > 100, "ferramenta retangulo desenha o contorno"); click(345, 342); /* ferramenta elipse */ frame(0, 0, 0, 0, 0, 0, NULL); drag(620, 290, 690, 390); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(img_nonwhite() > rect_px, "ferramenta elipse desenha o contorno"); click(345, 392); /* ferramenta preencher */ frame(0, 0, 0, 0, 0, 0, NULL); click(620, 320); /* dentro do retangulo */ frame(0, 0, 0, 0, 0, 0, NULL); click(520, 400); /* fora das formas */ frame(0, 0, 0, 0, 0, 0, NULL); int fill_px = img_nonwhite(); CHECK(fill_px > rect_px * 3, "ferramenta preencher alaga a area branca"); click(345, 492); /* conta-gotas */ frame(0, 0, 0, 0, 0, 0, NULL); click(520, 400); frame(0, 0, 0, 0, 0, 0, NULL); { bool found = false; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && strstr(cmds[i].text, "Cor do pixel")) found = true; CHECK(found, "conta-gotas informa a cor capturada"); } /* 24b. Escolher uma cor na paleta lateral e desenhar com ela */ { click(803, 277); /* segundo quadradinho da paleta (branco) */ frame(0, 0, 0, 0, 0, 0, NULL); bool dark = false; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && strstr(cmds[i].text, "#FFFFFF")) dark = true; CHECK(dark, "paleta lateral troca a cor da ferramenta"); click(345, 142); /* selecionar */ frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("Desfazer", R_WS, 0, &x, &y), "ferramenta selecionar continua disponivel"); } CHECK(find("Fundo", R_WS, 0, &x, &y), "lista de camadas mostra a camada Fundo"); CHECK(find("+ Raster", R_WS, 0, &x, &y), "botao de nova camada raster"); CHECK(find("+ Vetor", R_WS, 0, &x, &y), "botao de nova camada vetorial"); CHECK(find("Fundo", R_WS, 0, &x, &y), "achei a camada Fundo"); click(x - 29, y + 6); /* olho da camada Fundo: esconde */ frame(0, 0, 0, 0, 0, 0, NULL); /* sem camada visivel nao ha imagem nenhuma na folha (=-1) */ CHECK(img_nonwhite() <= 0, "camada pode ser escondida"); click(x - 29, y + 6); /* olho: mostra de novo */ frame(0, 0, 0, 0, 0, 0, NULL); CHECK(img_nonwhite() > 0, "camada pode ser mostrada de novo"); } /* 22. Áreas estilo Blender: redimensionar arrastando a borda, trocar de lugar pela alça */ { frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("Blocos", R_ANY, 0, &x, &y) && x < 100, "paleta tem cabeçalho \"Blocos\" com alça"); /* borda paleta | scripts em x = 318: arrasta 100 px para a direita */ drag(318, 400, 418, 400); frame(0, 0, 0, 0, 0, 0, NULL); bool largo = false; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_SET_CLIP && cmds[i].x == 68 && cmds[i].w > 340) largo = true; CHECK(largo, "arrastar a borda alarga a paleta (418 px)"); float px0 = 0, py0 = 0; CHECK(find("Palco", R_ANY, 0, &px0, &py0) && px0 > 920, "palco continua na direita"); /* alça do palco (ao lado do botão de destacar) -> solta sobre os scripts */ drag(1280 - 50 - 18 - 8 + 9, 44 + 22, 640, 400); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("Palco", R_ANY, 0, &x, &y) && x > 418 && x < 900, "alça troca o palco com a área de scripts"); /* e de volta: a alça do palco agora está no meio da tela */ drag(418 + (1280 - 360 - 418) - 50 - 18 - 8 + 9, 44 + 22, 1100, 200); /* scripts, em cima à direita */ frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("Palco", R_ANY, 0, &x, &y) && x > 920, "trocar de novo devolve o palco à direita"); /* layout salvo nas configurações (arquivo isolado do harness) */ CHECK(!strcmp(tb_environ_get("editor.layout"), "H0:418(p,H1:360(s,V0:310(t,o)))"), "layout salvo em editor.layout (paleta com 418 px)"); FILE* cf = fopen("/tmp/tb_harness_cfg/tinyblocks/settings.csv", "rb"); char cbuf[1024] = ""; if (cf) { size_t n = fread(cbuf, 1, sizeof(cbuf) - 1, cf); cbuf[n] = 0; fclose(cf); } CHECK(strstr(cbuf, "editor.layout") != NULL, "layout gravado no arquivo de configurações"); } /* 23. Abas: arrastar na barra reordena (como no navegador) */ { frame(0, 0, 0, 0, 0, 0, NULL); /* rótulos das abas na barra (y 44..80), em ordem de x */ char nomes[8][64]; float xs[8]; int n = 0; for (uint32_t i = 0; i < ncmd && n < 8; i++) { if (cmds[i].kind != TB_DRAW_TEXT || cmds[i].y < 50 || cmds[i].y > 80) continue; if (cmds[i].x < 318 || cmds[i].x > 900 || !strcmp(cmds[i].text, "=") || !strcmp(cmds[i].text, "+")) continue; snprintf(nomes[n], 64, "%s", cmds[i].text); xs[n] = cmds[i].x; n++; } CHECK(n >= 2, "há pelo menos duas abas para reordenar"); if (n >= 2) { char primeira[64]; snprintf(primeira, 64, "%s", nomes[0]); float sx = xs[0] + 10; drag(sx, 62, sx + 150 + 20, 62); /* passa do meio da vizinha */ frame(0, 0, 0, 0, 0, 0, NULL); float nx = 0, ny = 0; bool achou = false; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && !strcmp(cmds[i].text, primeira) && cmds[i].y > 50 && cmds[i].y < 80) { nx = cmds[i].x; ny = cmds[i].y; achou = true; } (void)ny; CHECK(achou && nx > xs[1] - 20, "arrastar a aba para a direita troca a ordem com a vizinha"); /* arrastar a aba para longe da barra destaca numa janela própria */ drag(nx + 10, 62, nx + 10, 400); frame(0, 0, 0, 0, 0, 0, NULL); bool na_barra = false, no_menu = false; float mxp = 0; for (uint32_t i = 0; i < ncmd; i++) { if (cmds[i].kind != TB_DRAW_TEXT || strcmp(cmds[i].text, primeira) != 0) continue; if (cmds[i].y > 50 && cmds[i].y < 80) na_barra = true; if (cmds[i].y < 44) { no_menu = true; mxp = cmds[i].x + 3; } } CHECK(!na_barra && no_menu, "aba arrastada para fora vira janela (botão no menu)"); if (no_menu) click(mxp, 22); /* volta para a barra */ frame(0, 0, 0, 0, 0, 0, NULL); na_barra = false; for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind == TB_DRAW_TEXT && !strcmp(cmds[i].text, primeira) && cmds[i].y > 50 && cmds[i].y < 80) na_barra = true; CHECK(na_barra, "botão no menu traz a aba de volta para a barra"); } } /* 24. Notação do fórum (scratchblocks): Abrir .txt cria aba; Exportar gera o texto */ { const char* src = "/tmp/tb_harness_sb.txt"; const char* dst = "/tmp/tb_harness_sb_out.txt"; const char* script = "when flag clicked\n" "set [x v] to (0)\n" "repeat (3)\n" " change [x v] by (1)\n" "end\n" "say (x)\n"; FILE* f = fopen(src, "wb"); if (f) { fputs(script, f); fclose(f); } remove(dst); frame(0, 0, 0, 0, 0, 0, NULL); if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y); for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); type(src); key(TB_KEY_RETURN, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(any_has("Importado: 1 script"), "Abrir um .txt em scratchblocks cria aba com o script"); CHECK(any_has("de tb_harness_sb.txt"), "aba nova recebe o nome do arquivo"); CHECK(find("repita", R_WS, 0, &x, &y) || ws_has("repita"), "blocos importados aparecem (repita)"); if (need("Exportar", R_FLY, 0, &x, &y)) click(x, y); if (need("Exportar scratchblocks", R_ANY, 0, &x, &y)) click(x, y); frame(0, 0, 0, 0, 0, 0, NULL); /* prévia: mostra o texto antes de exportar */ CHECK(find("when flag clicked", R_ANY, 0, &x, &y) && find("Copiar", R_ANY, 0, &x, &y), "Exportar scratchblocks abre a prévia com o texto"); if (need("Copiar", R_ANY, 0, &x, &y)) click(x, y); char* clip = tb_graphics_get_clipboard(); CHECK(clip && strstr(clip, "[scratchblocks]\nwhen flag clicked\n") && strstr(clip, "[/scratchblocks]"), "Copiar põe o texto com [scratchblocks] na área de transferência"); free(clip); if (need("Salvar...", R_ANY, 0, &x, &y)) click(x, y); for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); type(dst); key(TB_KEY_RETURN, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); char got[512] = ""; f = fopen(dst, "rb"); if (f) { size_t n = fread(got, 1, sizeof(got) - 1, f); got[n] = 0; fclose(f); } CHECK(!strcmp(got, script), "Exportar scratchblocks gera o mesmo texto"); if (strcmp(got, script)) printf("---- exportado:\n%s----\n", got); /* colar do fórum: a área de transferência vira uma aba */ tb_graphics_set_clipboard("[scratchblocks]\nwhen flag clicked\nerase all\n[/scratchblocks]\n"); if (need("Exportar", R_FLY, 0, &x, &y)) click(x, y); if (need("Colar scratchblocks", R_ANY, 0, &x, &y)) click(x, y); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(any_has("Importado: 1 script(s) de Colado"), "Colar scratchblocks importa da área de transferência"); remove(src); remove(dst); } printf("comandos de desenho no último frame: %u\n", ncmd); /* 26. Erros: tente / se der erro / finalmente, resultados e erro sem tratamento */ { const char* pj = "/tmp/tb_harness_try.json"; FILE* f = fopen(pj, "wb"); if (f) { fputs("{\"format\":\"tinyblocks-project\",\"version\":2,\"cur_tab\":0,\"tabs\":[{\"name\":\"Erros\",\"composite\":false,\"comp_id\":-1,\"color\":0,\"blocks\":[" "{\"i\":0,\"type\":\"Events.WhenRun\",\"x\":40,\"y\":40,\"next\":1}," "{\"i\":1,\"type\":\"Errors.Try\",\"parent\":0,\"prel\":0,\"next\":7,\"sub\":[2,3,5]}," "{\"i\":2,\"type\":\"Errors.Raise\",\"parent\":1,\"prel\":1,\"next\":6,\"in_text\":[\"deu ruim\"]}," "{\"i\":6,\"type\":\"IO.Print\",\"parent\":2,\"prel\":0,\"in_text\":[\"nao roda\",\"\"]}," "{\"i\":3,\"type\":\"IO.Print\",\"parent\":1,\"prel\":2,\"args\":[-1,4],\"in_text\":[\"pegou\",\"\"]}," "{\"i\":4,\"type\":\"Errors.Caught\",\"parent\":3,\"prel\":5}," "{\"i\":5,\"type\":\"IO.Print\",\"parent\":1,\"prel\":3,\"in_text\":[\"no finalmente\",\"\"]}," "{\"i\":7,\"type\":\"IO.Print\",\"parent\":1,\"prel\":0,\"next\":8,\"args\":[-1,9],\"in_text\":[\"resultado\",\"\"]}," "{\"i\":9,\"type\":\"Errors.UnwrapOr\",\"parent\":7,\"prel\":5,\"args\":[10,-1],\"in_text\":[\"\",\"77\"]}," "{\"i\":10,\"type\":\"Errors.Attempt\",\"parent\":9,\"prel\":4,\"args\":[11]}," "{\"i\":11,\"type\":\"Errors.Unwrap\",\"parent\":10,\"prel\":4,\"args\":[12]}," "{\"i\":12,\"type\":\"Errors.Err\",\"parent\":11,\"prel\":4,\"in_text\":[\"x\"]}," "{\"i\":8,\"type\":\"Errors.Raise\",\"parent\":7,\"prel\":0,\"next\":13,\"in_text\":[\"sem tente\"]}," "{\"i\":13,\"type\":\"IO.Print\",\"parent\":8,\"prel\":0,\"in_text\":[\"nunca\",\"\"]}" "],\"top\":[0]}],\"user_blocks\":[],\"variables\":[]}", f); fclose(f); } if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y); for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); type(pj); key(TB_KEY_RETURN, 0, 0); if (find("Descartar", R_ANY, 0, &x, &y)) click(x, y); frame(0, 0, 0, 0, 0, 0, NULL); CHECK(ws_has("tente") && ws_has("se der erro") && ws_has("finalmente"), "bloco tente / se der erro / finalmente com três bocas"); flag(); for (int i = 0; i < 3; i++) frame(0, 0, 0, 0, 0, 0, NULL); CHECK(stage_has("deu ruim") && !stage_has("nao roda"), "erro pula o resto do tente e vai para o se der erro"); CHECK(stage_has("no finalmente"), "finalmente roda depois"); CHECK(stage_has("77"), "tente obter + desembrulhe + valor de ... ou: erro vira valor (Result)"); CHECK(stage_has("[ERRO] sem tente") && !stage_has("nunca"), "erro sem tente para o script e aparece no palco"); remove(pj); } /* 25. Grupos (como no Unreal): agrupar a pilha e mover tudo pelo título */ { float hx0, hy0; frame(0, 0, 0, 0, 0, 0, NULL); if (need("quando a bandeira for clicado", R_WS, 0, &hx0, &hy0)) { drag(hx0, hy0, hx0 + 40, hy0 + 160); /* longe da barra de abas */ need("quando a bandeira for clicado", R_WS, 0, &hx0, &hy0); frame(hx0, hy0, 0, 0, 1, 0, NULL); /* botão direito na pilha */ frame(hx0, hy0, 0, 0, 0, 0, NULL); if (need("Agrupar esta pilha", R_ANY, 0, &x, &y)) click(x, y); for (int i = 0; i < 12; i++) key(TB_KEY_BACKSPACE, 0, 0); type("Principal"); key(TB_KEY_RETURN, 0, 0); CHECK(find("Principal", R_WS, 0, &x, &y), "Agrupar esta pilha cria o grupo com título"); float tx = x, ty = y; drag(tx, ty, tx + 120, ty + 70); /* arrasta pelo título */ float nx, ny; CHECK(find("quando a bandeira for clicado", R_WS, 0, &nx, &ny) && fabsf(nx - (hx0 + 120)) < 2 && fabsf(ny - (hy0 + 70)) < 2, "arrastar o título do grupo move a pilha junto"); CHECK(find("Principal", R_WS, 0, &x, &y) && fabsf(x - (tx + 120)) < 2, "o grupo vai junto com a pilha"); /* salvar e abrir de novo mantém o grupo */ const char* gp = "/tmp/tb_harness_grp.json"; remove(gp); if (need("Salvar", R_FLY, 0, &x, &y)) click(x, y); for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); type(gp); key(TB_KEY_RETURN, 0, 0); FILE* f = fopen(gp, "rb"); char buf[65536] = ""; if (f) { size_t n = fread(buf, 1, sizeof(buf) - 1, f); buf[n] = 0; fclose(f); } CHECK(strstr(buf, "\"groups\"") && strstr(buf, "Principal"), "projeto salvo guarda os grupos"); } } /* 27. Dois blocos definidos na mesma aba: os dois aparecem em Meus Blocos */ { const char* nomes[2] = { "render", "draw button" }; for (int k = 0; k < 2; k++) { if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); if (need("Criar um Bloco", R_FLY, 0, &x, &y)) click(x, y); if (need("nome do bloco", R_WS, 0, &x, &y)) click(x, y); type(nomes[k]); key(TB_KEY_RETURN, 0, 0); } if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); CHECK(find("render", R_FLY, 0, &x, &y) && find("draw button", R_FLY, 0, &x, &y), "dois blocos definidos aparecem na paleta"); } tinyblocks_deinit(&rt); printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails); return fails != 0; }