/* 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 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 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(0)); 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(); } 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; } 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; } static bool any_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; } 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 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 */ 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) { 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_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_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("numérica ou texto", R_WS, 0, &x, &y)) click(x, y); type("n"); key(TB_KEY_RETURN, 0, 0); /* OK no diálogo */ CHECK(ws_has("parâmetro"), "diálogo cria o bloco composto e o corpo com o parâmetro"); if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); if (need("devolva", R_FLY, 0, &x, &y)) drag(x, y, 450, 300); if (need("Operadores", R_CAT, 0, &x, &y)) click(x, y); if (need("+", R_FLY, 0, &x, &y)) drag(x, y, 470, 450); /* encaixa o parâmetro n no slot esquerdo do "+" */ { float px, py, zx, zy; if (need("parâmetro", R_WS, 0, &px, &py) && need("+", R_WS, 0, &x, &y)) { 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 +\n"); fails++; } else drag(px, py, px + ((zx - 3 - 10) - (px - 3 - 10)), zy); } } /* digita 1 no slot direito do "+" */ if (need("+", R_WS, 0, &x, &y)) { 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 (need("+", R_WS, 0, &x, &y) && need("=", R_WS, 0, &x2, &y2)) { float rep_left = x - 3 - 10; { float px, py; if (find("parâmetro", R_WS, 0, &px, &py)) rep_left = px - 3 - 10 - 10; } drag(x, y, x + ((x2 + 12) - rep_left), y + (y2 - y)); } if (need("Salvar", R_WS, 0, &x, &y)) click(x, y); CHECK(find("dobro", R_ANY, 0, &x, &y), "bloco composto salvo"); if (need("Código", R_ANY, 0, &x, &y)) click(x, y); if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); if (need("dobro", R_FLY, 0, &x, &y)) drag(x, y, 650, 640); { float sx, sy; if (find("n", R_WS, 0, &sx, &sy)) { click(sx, sy); key(TB_KEY_BACKSPACE, 0, 0); type("21"); key(TB_KEY_RETURN, 0, 0); } } if (need("dobro", R_WS, 0, &x, &y)) { click(x, y); click(x, y); } 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); CHECK(stage_has("limite de passos"), "laço infinito é abortado com aviso"); /* 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); CHECK(any_has("Palco aberto em outra janela"), "palco destacado abre janela própria"); click(1280 - 50 + 20, 44 + 22); CHECK(!any_has("Palco aberto em outra janela"), "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"); 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); /* 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 Codigo", R_ANY, 0, &x, &y), "menu '+' lista as opcoes de nova aba"); CHECK(find("Edicao de Imagem", R_ANY, 0, &x, &y), "menu '+' oferece Edicao de Imagem"); if (need("Editor de Codigo", 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"); } /* 23. Botao "+" -> nova aba: Edicao 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("Edicao 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"); } printf("comandos de desenho no último frame: %u\n", ncmd); tinyblocks_deinit(&rt); printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails); return fails != 0; }