Files
tinyblocks/tests/a11y_test.c
T
mguinhosandClaude Opus 5.5 8f046833e7 Acessibilidade: arrastar e soltar como ação; harness inteiro pela árvore
tb_a11y_drop(origem, destino) e tb_a11y_drop_at(origem, destino, x, y):
o editor encaixa pelo papel do destino (campo, embaixo de um bloco,
dentro de uma boca, sobre a paleta para apagar, aba sobre aba, área
sobre área) pelo mesmo caminho do mouse (pointer_motion, extraído do
tratamento de entrada); origem ou destino fora da vista rolam antes.

A árvore passa a cobrir o explorador de arquivos (trilha, locais,
arquivos, nome, opções, confirmação de substituir), a aba de imagem
(ferramentas, folha, rodapé, cores, paleta, camadas), a dica (Alt),
as bordas entre áreas, as alças e os botões de destacar. O retângulo do
palco é a vista dele; avisos repetidos dizem quantas vezes no valor.

O harness não acha mais nada por texto desenhado nem por coordenada
fixa: categorias, blocos, campos, menus, diálogos, saída e avisos vêm
da árvore. Só pintar na folha e soltar em lugar livre usam posição, e
relativa a um componente.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 07:41:12 -03:00

138 lines
6.8 KiB
C

/* Teste da tinyblocks-a11y: arvore acessivel, busca, ids, eventos e acoes */
#include <stdio.h>
#include <string.h>
#include <tb_a11y.h>
static int fails = 0;
#define CHECK(cond, msg) do { if (cond) printf("ok %s\n", msg); else { printf("FAIL %s\n", msg); fails++; } } while (0)
static int performed = -1;
static int dropped_on = -1;
static float drop_x = -1;
static bool perform(void* user, const TinyBlocks_A11yTree* t, int node, const TinyBlocks_A11yRequest* req) {
(void)user; (void)t;
if (req->action == TB_A11Y_ACT_DRAG) { dropped_on = req->target; drop_x = req->at ? req->x : -1; return true; }
if (req->action != TB_A11Y_ACT_ACTIVATE) return false;
performed = node;
return true;
}
static int bridge_events = 0;
static void on_event(void* user, const TinyBlocks_A11yTree* t, const TinyBlocks_A11yEvent* ev) {
(void)user; (void)t; (void)ev;
bridge_events++;
}
/* Um quadro de uma interface pequena; `sel` = aba selecionada, `edit` = campo com foco */
static void build(TinyBlocks_A11yTree* t, int sel, bool edit, const char* value, bool dialog) {
tb_a11y_begin(t, "App", 800, 600);
tb_a11y_push(t, TB_A11Y_TOOLBAR, "Menu", 0, 0, 800, 40);
int b = tb_a11y_add(t, TB_A11Y_BUTTON, "Abrir", 10, 5, 60, 30);
tb_a11y_set_actions(t, b, TB_A11Y_ACT_ACTIVATE);
tb_a11y_add(t, TB_A11Y_BUTTON, "Salvar", 80, 5, 60, 30);
tb_a11y_pop(t);
tb_a11y_push(t, TB_A11Y_TAB_LIST, "Abas", 0, 40, 800, 30);
for (int i = 0; i < 3; i++) {
char nm[16];
snprintf(nm, sizeof(nm), "Aba %d", i + 1);
int a = tb_a11y_add(t, TB_A11Y_TAB, nm, 10 + i * 100, 40, 90, 30);
tb_a11y_set_states(t, a, i == sel ? TB_A11Y_SELECTED : 0);
}
tb_a11y_pop(t);
int blk = tb_a11y_push(t, TB_A11Y_BLOCK, value, 100, 100, 200, 40); /* nome muda com o valor */
tb_a11y_set_key(t, blk, "Control.Repeat");
tb_a11y_set_point(t, blk, 108, 120);
int f = tb_a11y_add(t, TB_A11Y_TEXT_FIELD, "vezes", 150, 108, 30, 24);
tb_a11y_set_value(t, f, value);
tb_a11y_set_states(t, f, TB_A11Y_EDITABLE | (edit ? TB_A11Y_FOCUSED | TB_A11Y_EDITING : 0));
tb_a11y_pop(t);
if (dialog) {
tb_a11y_push(t, TB_A11Y_DIALOG, "Configurações", 100, 100, 600, 400);
tb_a11y_add(t, TB_A11Y_BUTTON, "Voltar", 100, 100, 80, 40);
tb_a11y_pop(t);
}
tb_a11y_end(t);
}
static bool has_event(TinyBlocks_A11yTree* t, TinyBlocks_A11yEventKind k) {
for (int i = 0; i < tb_a11y_event_count(t); i++) if (tb_a11y_event(t, i)->kind == k) return true;
return false;
}
int main(void) {
TinyBlocks_A11yTree* t = tb_a11y_tree_new();
tb_a11y_set_perform(t, perform, NULL);
build(t, 0, false, "repita (10) vezes", false);
CHECK(tb_a11y_count(t) == 10, "árvore com a raiz e 9 componentes");
int menu = tb_a11y_find(t, 0, TB_A11Y_TOOLBAR, "Menu", 0);
int abrir = tb_a11y_find(t, menu, TB_A11Y_BUTTON, "Abrir", 0);
CHECK(menu > 0 && abrir > menu && tb_a11y_node(t, abrir)->parent == menu, "busca por papel + nome, dentro do pai");
CHECK(tb_a11y_find(t, menu, TB_A11Y_TAB, NULL, 0) < 0, "a busca fica na subárvore");
CHECK(tb_a11y_find(t, 0, TB_A11Y_TAB, NULL, 2) >= 0 && tb_a11y_find(t, 0, TB_A11Y_TAB, NULL, 3) < 0,
"n-ésimo componente");
CHECK(tb_a11y_find_part(t, 0, TB_A11Y_BLOCK, "repita", 0) >= 0, "busca por parte do nome");
CHECK(tb_a11y_event_count(t) == 0, "primeiro quadro não gera eventos");
char buf[256];
CHECK(!strcmp(tb_a11y_path(t, abrir, buf, sizeof(buf)), "Menu > Abrir"), "caminho \"Menu > Abrir\"");
int aba1 = tb_a11y_find(t, 0, TB_A11Y_TAB, "Aba 1", 0);
CHECK(!strcmp(tb_a11y_describe(t, aba1, buf, sizeof(buf)), "Aba 1, aba, selecionado"), "frase do narrador com estado");
float x, y;
int blk = tb_a11y_find(t, 0, TB_A11Y_BLOCK, NULL, 0);
tb_a11y_point(t, blk, &x, &y);
CHECK(x == 108 && y == 120, "ponto de ação próprio");
tb_a11y_center(t, abrir, &x, &y);
CHECK(x == 40 && y == 20, "centro do retângulo");
CHECK(tb_a11y_do_action(t, abrir, TB_A11Y_ACT_ACTIVATE) && performed == abrir, "ação executada por quem desenha");
int salvar = tb_a11y_find(t, 0, TB_A11Y_BUTTON, "Salvar", 0);
CHECK(!tb_a11y_do_action(t, salvar, TB_A11Y_ACT_ACTIVATE), "ação não declarada é recusada");
int fld = tb_a11y_find(t, 0, TB_A11Y_TEXT_FIELD, "vezes", 0);
tb_a11y_set_actions(t, abrir, TB_A11Y_ACT_ACTIVATE | TB_A11Y_ACT_DRAG);
CHECK(!tb_a11y_drop(t, abrir, fld), "soltar exige DROP no destino");
tb_a11y_set_actions(t, fld, TB_A11Y_ACT_DROP);
CHECK(tb_a11y_drop(t, abrir, fld) && dropped_on == fld && drop_x < 0, "arrastar e soltar sobre o destino");
CHECK(tb_a11y_drop_at(t, abrir, fld, 12, 3) && drop_x == 12, "soltar numa posição do destino");
CHECK(!tb_a11y_drop(t, salvar, fld), "soltar exige DRAG na origem");
uint32_t id_aba2 = tb_a11y_node(t, tb_a11y_find(t, 0, TB_A11Y_TAB, "Aba 2", 0))->id;
uint32_t id_blk = tb_a11y_node(t, blk)->id;
TinyBlocks_A11yBridge br = { "teste", on_event, NULL, NULL };
tb_a11y_set_bridge(t, &br);
/* quadro 2: outra aba, campo editando com valor novo, dialogo aberto */
build(t, 1, true, "repita (25) vezes", true);
int aba2 = tb_a11y_find(t, 0, TB_A11Y_TAB, "Aba 2", 0);
blk = tb_a11y_find(t, 0, TB_A11Y_BLOCK, NULL, 0);
CHECK(tb_a11y_node(t, aba2)->id == id_aba2, "id estável entre quadros");
CHECK(tb_a11y_node(t, blk)->id == id_blk, "set_key: o id não muda quando o nome muda");
CHECK(has_event(t, TB_A11Y_EV_STATE), "evento de estado (seleção mudou)");
CHECK(has_event(t, TB_A11Y_EV_FOCUS), "evento de foco (campo editando)");
CHECK(tb_a11y_focus(t) == tb_a11y_find(t, 0, TB_A11Y_TEXT_FIELD, "vezes", 0), "foco = campo editando");
CHECK(has_event(t, TB_A11Y_EV_VALUE) && has_event(t, TB_A11Y_EV_NAME), "eventos de valor e de nome");
CHECK(has_event(t, TB_A11Y_EV_SHOWN), "evento: diálogo apareceu");
CHECK(bridge_events == tb_a11y_event_count(t), "a ponte recebe todos os eventos");
/* quadro 3: igual ao 2 -> nada muda */
build(t, 1, true, "repita (25) vezes", true);
CHECK(tb_a11y_event_count(t) == 0, "quadro igual não gera eventos");
tb_a11y_announce(t, "ignorado fora de begin/end"); /* o begin seguinte limpa */
tb_a11y_begin(t, "App", 800, 600);
tb_a11y_announce(t, "Projeto salvo");
tb_a11y_end(t);
CHECK(tb_a11y_event_count(t) == 1 && tb_a11y_event(t, 0)->kind == TB_A11Y_EV_ANNOUNCE &&
!strcmp(tb_a11y_event(t, 0)->text, "Projeto salvo"), "aviso para ler em voz alta");
/* robustez: arvore NULL e indices invalidos */
CHECK(tb_a11y_push(NULL, TB_A11Y_BUTTON, "x", 0, 0, 1, 1) == -1 && tb_a11y_node(t, 999) == NULL &&
tb_a11y_find(t, 999, TB_A11Y_NONE, NULL, 0) == -1, "NULL e índices inválidos não quebram");
tb_a11y_tree_free(t);
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
return fails != 0;
}