Primo commit del progetto: motore Rust con parser HTML, cascata CSS, layout, raster CPU, formulari, bidi, flex, tabelle, JavaScript (Boa) con eventi e costruzione di nodi, rete HTTP/HTTPS con TLS, sotto-risorse, invio dei formulari; guscio grafico per Sailfish (QML/Silica) e per desktop (minifb, distribuito come AppImage), con 11 documenti di specifica e 91 casi di reftest.
153 lines
6.6 KiB
C
153 lines
6.6 KiB
C
/* Verifica del confine C: compila contro engine.h e linka libengine.a.
|
|
* Se questo programma passa, il contratto dell'API è rispettato. */
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "engine.h"
|
|
|
|
static int fails = 0;
|
|
|
|
static void check(int cond, const char *what) {
|
|
printf("%s %s\n", cond ? "ok " : "FAIL", what);
|
|
if (!cond) fails++;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
const char *font = argc > 1 ? argv[1] : "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf";
|
|
|
|
printf("versione: %s\n", engine_version());
|
|
|
|
/* 1. rettangolo: il pixel interno deve essere del colore dichiarato */
|
|
const char *html =
|
|
"<div style=\"width:40px;height:20px;background:#ff0000\"></div>";
|
|
EngineImage *img = engine_render(html, "", 100, 60, 1.0f, 0, font, "");
|
|
check(img != NULL, "engine_render restituisce un'immagine");
|
|
if (img) {
|
|
check(img->error == NULL, "nessun errore");
|
|
check(img->width == 100 && img->height == 60, "dimensioni del buffer");
|
|
if (img->pixels) {
|
|
/* body margin 8: il div parte a (8,8) */
|
|
size_t idx = (size_t)((10 * img->width) + 10) * 4;
|
|
check(img->pixels[idx] == 255 && img->pixels[idx + 1] == 0 &&
|
|
img->pixels[idx + 2] == 0,
|
|
"pixel (10,10) rosso");
|
|
size_t idx2 = (size_t)((10 * img->width) + 80) * 4;
|
|
check(img->pixels[idx2] == 255 && img->pixels[idx2 + 1] == 255,
|
|
"pixel (80,10) bianco");
|
|
}
|
|
engine_image_free(img);
|
|
}
|
|
|
|
/* 2. testo: glyph rasterizzati presenti nel buffer */
|
|
const char *html2 = "<p style=\"font-size:24px\">Prova</p>";
|
|
EngineImage *img2 = engine_render(html2, "", 200, 100, 1.0f, 0, font, "");
|
|
if (img2 && img2->pixels) {
|
|
int dark = 0;
|
|
for (size_t i = 0; i < img2->len; i += 4) {
|
|
if (img2->pixels[i] < 100 && img2->pixels[i + 1] < 100) dark++;
|
|
}
|
|
check(dark > 50, "testo rasterizzato (pixel scuri)");
|
|
engine_image_free(img2);
|
|
} else {
|
|
check(0, "immagine di testo prodotta");
|
|
}
|
|
|
|
/* 3. errore: font inesistente non deve crashare, deve riportare un errore */
|
|
EngineImage *img3 = engine_render("<p>x</p>", "", 50, 50, 1.0f, 0, "/non/esiste.ttf", "");
|
|
check(img3 != NULL, "errore restituito come immagine");
|
|
if (img3) {
|
|
check(img3->error != NULL, "messaggio di errore presente");
|
|
check(img3->pixels == NULL, "nessun pixel in caso di errore");
|
|
printf(" messaggio: %s\n", img3->error ? img3->error : "(null)");
|
|
engine_image_free(img3);
|
|
}
|
|
|
|
/* 3b. stato dei moduli: l'elenco aperto deve arrivare al motore */
|
|
{
|
|
const char *form_html =
|
|
"<p style=\"margin:0\"><select><option>uno</option>"
|
|
"<option selected>due</option></select></p>";
|
|
const char *stato = "{\"open_select\":\"html>body:0>p:0>select:0\","
|
|
"\"select_highlight\":1}";
|
|
EngineImage *imgf = engine_render_form(form_html, "", 200, 120, 1.0f, 0,
|
|
font, "", stato);
|
|
check(imgf != NULL, "engine_render_form risponde");
|
|
if (imgf) {
|
|
check(imgf->error == NULL, "nessun errore con lo stato dei moduli");
|
|
check(imgf->controls != NULL, "elenco dei controlli presente");
|
|
if (imgf->controls) {
|
|
/* con il select aperto il guscio deve ricevere il rettangolo
|
|
* dell'elenco: senza, il menù non si può disegnare né toccare */
|
|
check(strstr(imgf->controls, "\"popup\"") != NULL &&
|
|
strstr(imgf->controls, "\"uno\"") != NULL,
|
|
"lo stato open_select arriva al motore (popup + opzioni)");
|
|
printf(" controlli: %.120s...\n", imgf->controls);
|
|
}
|
|
engine_image_free(imgf);
|
|
}
|
|
}
|
|
|
|
/* 3c. rete: un indirizzo impossibile deve dare errore, non un crash */
|
|
{
|
|
int stato = 0;
|
|
char *errore = NULL;
|
|
char *testo = engine_fetch("ftp://esempio.test/", &stato, &errore);
|
|
check(testo == NULL, "schema non supportato: nessun testo");
|
|
check(errore != NULL, "schema non supportato: messaggio presente");
|
|
if (errore) {
|
|
check(strstr(errore, "schema") != NULL, "messaggio sulla schema");
|
|
engine_string_free(errore);
|
|
}
|
|
check(stato == 0, "nessun codice HTTP senza risposta");
|
|
}
|
|
|
|
/* 4. viewport nullo */
|
|
EngineImage *img4 = engine_render("<p>x</p>", "", 0, 50, 1.0f, 0, font, "");
|
|
check(img4 && img4->error != NULL, "viewport nullo segnalato");
|
|
engine_image_free(img4);
|
|
engine_image_free(NULL); /* idempotente */
|
|
|
|
/* 5. scala 2: la geometria raddoppia (viewport CSS 100, div 40x20 a (8,8)) */
|
|
EngineImage *img5 = engine_render(html, "", 200, 120, 2.0f, 0, font, "");
|
|
if (img5 && img5->pixels && img5->width == 200) {
|
|
size_t a = (size_t)((20 * img5->width) + 20) * 4;
|
|
size_t b = (size_t)((20 * img5->width) + 110) * 4;
|
|
check(img5->pixels[a] == 255 && img5->pixels[a + 1] == 0, "scala 2: pixel interno rosso");
|
|
check(img5->pixels[b] == 255 && img5->pixels[b + 1] == 255, "scala 2: fuori dal box bianco");
|
|
engine_image_free(img5);
|
|
} else {
|
|
check(0, "immagine a scala 2 prodotta");
|
|
}
|
|
|
|
/* 6. auto_height: l'immagine è alta quanto il documento */
|
|
const char *tall =
|
|
"<div style=\"height:300px;background:#00ff00\"></div>";
|
|
EngineImage *img6 = engine_render(tall, "", 100, 40, 1.0f, 1, font, "");
|
|
if (img6 && img6->pixels) {
|
|
check(img6->height > 300, "auto_height: immagine più alta del viewport richiesto");
|
|
engine_image_free(img6);
|
|
} else {
|
|
check(0, "immagine con auto_height prodotta");
|
|
}
|
|
|
|
/* 7. immagine da data URI: un PNG 1x1 rosso scalato a 20x20 */
|
|
const char *html_img =
|
|
"<img src=\"data:image/png;base64,"
|
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC"
|
|
"\" width=\"20\" height=\"20\">";
|
|
EngineImage *img7 = engine_render(html_img, "", 100, 100, 1.0f, 0, font, "");
|
|
if (img7 && img7->pixels) {
|
|
/* body margin 8: l'immagine parte a (8,8) */
|
|
size_t i7 = (size_t)((12 * img7->width) + 12) * 4;
|
|
check(img7->pixels[i7] > 200 && img7->pixels[i7 + 1] < 60 && img7->pixels[i7 + 2] < 60,
|
|
"immagine disegnata (rosso)");
|
|
engine_image_free(img7);
|
|
} else {
|
|
check(0, "immagine con data URI prodotta");
|
|
}
|
|
|
|
printf("\n%s\n", fails == 0 ? "confine C: tutto ok" : "confine C: FALLITO");
|
|
return fails == 0 ? 0 : 1;
|
|
}
|