Mosaic: motore di rendering proprio, gusci desktop e Sailfish
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.
This commit is contained in:
commit
577682ae22
173 files changed
+23685
No files matched your search
@@ -0,0 +1,179 @@
|
||||
//! Sotto-risorse dalla rete (M2.6), provate sul serio: un server locale serve la
|
||||
//! pagina, il foglio di stile e l'immagine separatamente, e il test guarda i
|
||||
//! pixel che ne risultano. Niente internet: la CI resta deterministica.
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use std::net::TcpListener;
|
||||
use std::path::PathBuf;
|
||||
use std::thread;
|
||||
|
||||
use engine::{render_with_form, FormState, RenderOptions};
|
||||
|
||||
const PAGINA: &str = concat!(
|
||||
"<!doctype html><html><head><meta charset=\"utf-8\">",
|
||||
"<link rel=\"stylesheet\" href=\"stile.css\">",
|
||||
"</head><body><h1>Titolo</h1><p>testo</p>",
|
||||
"<img src=\"img/quadrato.png\" width=\"40\" height=\"40\">",
|
||||
"</body></html>"
|
||||
);
|
||||
|
||||
const STILE: &str = "body{margin:0;padding:4px}h1{color:#cc0000;font-size:24px;margin:0}\
|
||||
p{margin:0;font-size:12px}";
|
||||
|
||||
fn font() -> PathBuf {
|
||||
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
p.push("cases/mono.ttf");
|
||||
p
|
||||
}
|
||||
|
||||
fn immagine() -> Vec<u8> {
|
||||
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
p.push("cases/quadrato-blu.png");
|
||||
std::fs::read(p).expect("quadrato di prova")
|
||||
}
|
||||
|
||||
/// Server usa-e-getta: risponde con l'elenco dato e chiude. `404` per il resto.
|
||||
fn server(risposte: Vec<(String, String, Vec<u8>)>) -> u16 {
|
||||
let ascolto = TcpListener::bind("127.0.0.1:0").expect("porta libera");
|
||||
let porta = ascolto.local_addr().unwrap().port();
|
||||
thread::spawn(move || {
|
||||
for flusso in ascolto.incoming().flatten() {
|
||||
let mut flusso = flusso;
|
||||
let mut buf = [0u8; 4096];
|
||||
let n = flusso.read(&mut buf).unwrap_or(0);
|
||||
let richiesta = String::from_utf8_lossy(&buf[..n]).to_string();
|
||||
let percorso = richiesta
|
||||
.split_whitespace()
|
||||
.nth(1)
|
||||
.unwrap_or("/")
|
||||
.to_string();
|
||||
let trovato = risposte.iter().find(|(p, _, _)| *p == percorso);
|
||||
let (codice, tipo, corpo) = match trovato {
|
||||
Some((_, tipo, corpo)) => ("200 OK", tipo.clone(), corpo.clone()),
|
||||
None => (
|
||||
"404 Not Found",
|
||||
"text/plain".to_string(),
|
||||
b"non c'e'".to_vec(),
|
||||
),
|
||||
};
|
||||
let testa = format!(
|
||||
"HTTP/1.1 {}\r\nContent-Type: {}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
|
||||
codice,
|
||||
tipo,
|
||||
corpo.len()
|
||||
);
|
||||
let _ = flusso.write_all(testa.as_bytes());
|
||||
let _ = flusso.write_all(&corpo);
|
||||
}
|
||||
});
|
||||
porta
|
||||
}
|
||||
|
||||
fn opzioni(porta: u16) -> RenderOptions {
|
||||
RenderOptions {
|
||||
viewport: (300, 200),
|
||||
dpi_scale: 1.0,
|
||||
font_path: font(),
|
||||
auto_height: true,
|
||||
base_dir: PathBuf::from("."),
|
||||
base_url: Some(format!("http://127.0.0.1:{}/pagina.html", porta)),
|
||||
}
|
||||
}
|
||||
|
||||
fn conta(res: &engine::RenderResult, rgb: [u8; 3]) -> usize {
|
||||
res.rgba
|
||||
.chunks_exact(4)
|
||||
.filter(|p| p[0] == rgb[0] && p[1] == rgb[1] && p[2] == rgb[2])
|
||||
.count()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stile_esterno_e_immagine_arrivano_dalla_rete() {
|
||||
let porta = server(vec![
|
||||
(
|
||||
"/pagina.html".to_string(),
|
||||
"text/html".to_string(),
|
||||
PAGINA.as_bytes().to_vec(),
|
||||
),
|
||||
(
|
||||
"/stile.css".to_string(),
|
||||
"text/css".to_string(),
|
||||
STILE.as_bytes().to_vec(),
|
||||
),
|
||||
(
|
||||
"/img/quadrato.png".to_string(),
|
||||
"image/png".to_string(),
|
||||
immagine(),
|
||||
),
|
||||
]);
|
||||
let res = render_with_form(PAGINA, "", &opzioni(porta), &FormState::default()).expect("render");
|
||||
|
||||
assert!(
|
||||
res.resource_errors.is_empty(),
|
||||
"sotto-risorse fallite: {:?}",
|
||||
res.resource_errors
|
||||
);
|
||||
// il colore del titolo viene dal foglio esterno
|
||||
assert!(
|
||||
conta(&res, [0xcc, 0x00, 0x00]) > 50,
|
||||
"il foglio esterno non è stato applicato"
|
||||
);
|
||||
// l'immagine: 40x40 px tutti blu
|
||||
assert!(
|
||||
conta(&res, [0x00, 0x00, 0xff]) >= 40 * 40,
|
||||
"l'immagine non è stata scaricata e disegnata"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn risorsa_mancante_e_un_errore_riportato_non_un_render_rotto() {
|
||||
let html = "<html><head><link rel=\"stylesheet\" href=\"manca.css\"></head>\
|
||||
<body><p>testo comunque impaginato</p></body></html>";
|
||||
let porta = server(vec![(
|
||||
"/pagina.html".to_string(),
|
||||
"text/html".to_string(),
|
||||
html.as_bytes().to_vec(),
|
||||
)]);
|
||||
let res = render_with_form(html, "", &opzioni(porta), &FormState::default())
|
||||
.expect("il render non deve fallire per una sotto-risorsa mancante");
|
||||
assert_eq!(res.resource_errors.len(), 1, "{:?}", res.resource_errors);
|
||||
assert!(
|
||||
res.resource_errors[0].contains("404"),
|
||||
"{:?}",
|
||||
res.resource_errors
|
||||
);
|
||||
assert!(res.layout.rect.h > 0.0, "la pagina è comunque impaginata");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn documenti_locali_non_escono_in_rete() {
|
||||
// senza base_url nessuna richiesta: la pagina resta quella di sempre
|
||||
let opts = RenderOptions {
|
||||
viewport: (200, 120),
|
||||
dpi_scale: 1.0,
|
||||
font_path: font(),
|
||||
auto_height: true,
|
||||
base_dir: PathBuf::from("."),
|
||||
base_url: None,
|
||||
};
|
||||
let html = "<html><head><link rel=\"stylesheet\" href=\"http://127.0.0.1:1/x.css\">\
|
||||
</head><body><p>solo locale</p></body></html>";
|
||||
let res = render_with_form(html, "", &opts, &FormState::default()).expect("render");
|
||||
assert!(res.resource_errors.is_empty(), "{:?}", res.resource_errors);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn risoluzione_dei_riferimenti() {
|
||||
use engine::resource::risolvi_url;
|
||||
assert_eq!(
|
||||
risolvi_url(Some("https://a.test/d/p.html"), "../i/a.png").as_deref(),
|
||||
Some("https://a.test/i/a.png")
|
||||
);
|
||||
assert_eq!(
|
||||
risolvi_url(Some("https://a.test/d/p.html"), "s.css").as_deref(),
|
||||
Some("https://a.test/d/s.css")
|
||||
);
|
||||
assert_eq!(risolvi_url(None, "s.css"), None);
|
||||
// il client HTTP accetta solo http/https: gli altri schemi non si risolvono
|
||||
assert_eq!(risolvi_url(Some("ftp://a.test/"), "s.css"), None);
|
||||
}
|
||||
Reference in new issue
Block a user