MODULE test_demos ; (* m2-GTK4 test - the m2 gtk-demo registry (demos/). Checks that the registry is populated and that every demo can be built and torn down. Requires a display. *) FROM GtkApplication IMPORT gtk_application_new; FROM Gio IMPORT g_application_run, g_application_quit; FROM GtkDemo IMPORT Count, TitleOf, KeywordsOf, BuildByTitle; FROM GtkUtils IMPORT Connect; FROM SYSTEM IMPORT ADDRESS, ADR; FROM libc IMPORT printf; CONST AppId = "org.example.m2gtk4.TestDemos"; VAR app: ADDRESS; failures: INTEGER; PROCEDURE StrEqual (a, b: ARRAY OF CHAR) : BOOLEAN; VAR i: CARDINAL; BEGIN i := 0; WHILE (a[i] # 0C) AND (b[i] # 0C) DO IF a[i] # b[i] THEN RETURN FALSE END; INC(i) END; RETURN a[i] = b[i] END StrEqual; PROCEDURE Check (cond: BOOLEAN; msg: ARRAY OF CHAR); BEGIN IF cond THEN printf(" ok - %s\n", msg) ELSE printf(" NOT OK - %s\n", msg); INC(failures) END END Check; (* build a demo once (creates + shows its window), then toggle it off. *) PROCEDURE BuildAndClose (title: ARRAY OF CHAR); VAR w: ADDRESS; BEGIN w := BuildByTitle(title, NIL); Check(w # NIL, title); IF w # NIL THEN w := BuildByTitle(title, NIL) END END BuildAndClose; PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS); VAR t, kw: ARRAY [0..127] OF CHAR; BEGIN failures := 0; Check(Count() = 5, "registry reports 5 demos"); TitleOf(0, t); Check(StrEqual(t, "Links"), "demo 0 is 'Links'"); KeywordsOf(0, kw); Check(NOT StrEqual(kw, ""), "demo 0 has search keywords"); Check(BuildByTitle("NoSuchDemo", NIL) = NIL, "unknown demo yields NIL"); BuildAndClose("Links"); BuildAndClose("List Box"); BuildAndClose("Flow Box"); BuildAndClose("Expander"); BuildAndClose("CSS Basics"); IF failures = 0 THEN printf("test_demos: PASS\n") ELSE printf("test_demos: FAIL (%d)\n", failures) END; g_application_quit(application) END OnActivate; VAR rc: INTEGER; BEGIN failures := 1; (* set to 0 by OnActivate; stays 1 if it never runs *) app := gtk_application_new(AppId, 0); IF app = NIL THEN printf("test_demos: FAIL (no app)\n"); HALT(1) END; Connect(app, "activate", ADR(OnActivate), NIL); rc := g_application_run(app, 0, NIL); IF failures = 0 THEN HALT(0) ELSE HALT(1) END END test_demos.