- engine_session_new/fetch/free: cookie e cache di sessione condivisi da documenti, sotto-risorse e fetch - engine_doc_new_session: il documento usa la sessione anche per le sotto-risorse - engine_doc_requests_json / engine_doc_deliver: le richieste accodate dagli script le esegue il guscio, che sa fare rete; il motore consegna le risposte - engine_risolvi_url: i riferimenti relativi delle fetch si risolvono sul documento Il guscio Sailfish crea una sessione per tutta la navigazione, carica i documenti attraverso di essa e serve le fetch nello stesso QTimer dei timer. La sessione è un shared_ptr perché il thread del caricamento la usa mentre la finestra potrebbe chiudersi.
212 lines
9.3 KiB
C
212 lines
9.3 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");
|
|
}
|
|
|
|
/* 3d. documento con stato: clic e timer */
|
|
{
|
|
const char *pagina =
|
|
"<html><body><div id=\"t\" style=\"width:80px;height:30px\">fermo</div>"
|
|
"<script>"
|
|
"document.getElementById('t').addEventListener('click', function () {"
|
|
" this.textContent = 'cliccato'; });"
|
|
"setTimeout(function () {"
|
|
" document.getElementById('t').style.backgroundColor = '#0000ff'; }, 100);"
|
|
"</script></body></html>";
|
|
EngineDoc *d = engine_doc_new(pagina, "", ".", NULL);
|
|
check(d != NULL, "engine_doc_new risponde");
|
|
if (d) {
|
|
EngineImage *i1 = engine_doc_render(d, 200, 120, 1.0f, 1, font, "");
|
|
check(i1 != NULL && i1->error == NULL, "engine_doc_render senza errori");
|
|
if (i1) engine_image_free(i1);
|
|
|
|
check(engine_doc_click(d, 10.0f, 10.0f) >= 1, "il clic raggiunge il gestore");
|
|
|
|
check(engine_doc_pump(d, 50) == 0, "prima della scadenza nessun timer");
|
|
check(engine_doc_pump(d, 500) == 1, "il timer scatta dopo la scadenza");
|
|
check(engine_doc_next_timer(d) < 0, "la coda dei timer è vuota");
|
|
|
|
EngineImage *i2 = engine_doc_render(d, 200, 120, 1.0f, 1, font, "");
|
|
check(i2 != NULL && i2->error == NULL, "secondo render senza errori");
|
|
if (i2) engine_image_free(i2);
|
|
engine_doc_free(d);
|
|
}
|
|
}
|
|
|
|
/* 3e. sessione e fetch: il guscio esegue, il motore consegna */
|
|
{
|
|
EngineSession *sess = engine_session_new();
|
|
check(sess != NULL, "engine_session_new risponde");
|
|
const char *pagina =
|
|
"<html><body><div id=\"t\">vuoto</div><script>"
|
|
"fetch('/dati').then(function (r) {"
|
|
" document.getElementById('t').textContent = r.text(); });"
|
|
"</script></body></html>";
|
|
EngineDoc *d = engine_doc_new_session(sess, pagina, "", ".", "http://esempio.test/pagina.html");
|
|
check(d != NULL, "engine_doc_new_session risponde");
|
|
if (d) {
|
|
char *richieste = engine_doc_requests_json(d);
|
|
check(richieste != NULL && strstr(richieste, "'/dati'") != NULL
|
|
|| (richieste != NULL && strstr(richieste, "/dati") != NULL),
|
|
"la richiesta fetch è in coda");
|
|
if (richieste) engine_string_free(richieste);
|
|
|
|
check(engine_doc_deliver(d, 1, 200, "corpo dalla rete") == 1,
|
|
"la consegna esegue il callback");
|
|
|
|
EngineImage *i = engine_doc_render(d, 200, 120, 1.0f, 1, font, "");
|
|
check(i != NULL && i->error == NULL, "render dopo la fetch");
|
|
if (i) engine_image_free(i);
|
|
engine_doc_free(d);
|
|
}
|
|
engine_session_free(sess);
|
|
}
|
|
|
|
/* 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;
|
|
}
|