;; Pictures of the GUI's tabs, drawn with no screen. ;; ;; bjo run tests/gui-shots.bjo [folder] ;; ;; - Starts (keyscore gui) headless, waits until the texts are read and the ;; layouts scored, and saves rules.png, key-costs.png, layout.png and ;; search.png in `folder`, the current one unless given. ;; - Changes a number first, and checks that the scores follow. ;; - Then runs a short search, keeps a result, and saves searched.png. ;; - Saves a preset, changes a number, and loads the preset back. Saves ;; presets.png, then deletes the preset. ;; - Saves a search result and the kept result as layouts, saves ;; layout-saved.png, and removes both again. ;; - Leaves no presets folder or saved-layouts.txt that was not there before. (import (std prelude)) (import (std interop)) (import (bjogui app)) (import (keyscore gui)) (import (keyscore layouts)) (import (keyscore params)) (import (keyscore saved)) (import/class (Object (: System.Object)) (Window (: Avalonia.Controls.Window (-> Window))) (ContentControl (: Avalonia.Controls.ContentControl)) (Control (: Avalonia.Controls.Control)) (Layoutable (: Avalonia.Layout.Layoutable)) (Visual (: Avalonia.Visual)) (Size (: Avalonia.Size (-> double double Size))) (Rect (: Avalonia.Rect (-> double double double double Rect))) (PixelSize (: Avalonia.PixelSize (-> int int PixelSize))) (Vector (: Avalonia.Vector (-> double double Vector))) (RenderBitmap (: Avalonia.Media.Imaging.RenderTargetBitmap (-> PixelSize Vector RenderBitmap))) (Bitmap (: Avalonia.Media.Imaging.Bitmap)) (Application (: Avalonia.Application)) (Styles (: Avalonia.Styling.Styles))) (import/extern (measure! (: Avalonia.Layout.Layoutable.Measure (-> Layoutable Size void))) (arrange! (: Avalonia.Layout.Layoutable.Arrange (-> Layoutable Rect void))) (render-into! (: Avalonia.Media.Imaging.RenderTargetBitmap.Render (-> RenderBitmap Visual void))) (save-png! (: Avalonia.Media.Imaging.Bitmap.Save (-> Bitmap string (System.Nullable int) void))) (sleep-ms (: System.Threading.Thread.Sleep (-> int void) #:blocking)) (current-app (: Avalonia.Application.Current #:get)) (content! (: Avalonia.Controls.ContentControl.Content (-> ContentControl Object void) #:set)) (show! (: Avalonia.Controls.Window.Show (-> Window void))) (app-styles (: Avalonia.Application.Styles (-> Application Styles) #:get)) (styles-add! (: Avalonia.Styling.Styles.Add (-> Styles IStyle void)))) ;; What `control` shows, laid out at `w` by `h`, saved to `path` at twice the ;; pixels. (: (-> Control int int string void)) (defun (save-shot control w h path) (measure! (cast Layoutable control) (Size. (cast double w) (cast double h))) (arrange! (cast Layoutable control) (Rect. 0.0 0.0 (cast double w) (cast double h))) (def bitmap (RenderBitmap. (PixelSize. (* 2 w) (* 2 h)) (Vector. 192.0 192.0))) (render-into! bitmap (cast Visual control)) (save-png! (cast Bitmap bitmap) path (: (clr-default) (System.Nullable int)))) ;; The name of `l`. (: (-> Layout string)) (defun (layout-name-of l) (record-ref l name)) (defun (main args) (def folder (if (> (vec-length args) 0) (vec-ref args 0) ".")) (start-headless!) ;; With no window there is no theme, and a control with a template draws nothing. (styles-add! (app-styles (cast Application current-app)) (fluent-theme)) (def app (start-app initial gui-update view (fun (c) unit) #:subscriptions subscriptions)) ;; Runs what the fibers posted until `done?` holds, for two minutes at most. (: (-> (-> Model bool) bool)) (defun (wait-for done?) (let go ((tries 0)) (app-run-pending! app) (cond ((done? (app-model app)) #t) ((> tries 12000) #f) (else (sleep-ms 10) (go (+ tries 1)))))) (: scored? (-> Model bool)) (def scored? (fun (m) (= (vec-length (.scores m)) (vec-length (.layouts m))))) (when (not (wait-for scored?)) (panic! "the layouts were not scored")) (def before (.generation (app-model app))) (app-dispatch! app (SetParam 3 60.0)) (when (not (wait-for (fun (m) (and (> (.generation m) before) (scored? m))))) (panic! "the scores did not follow a change")) ;; 3 is the key weight, after the three texts' weights. (app-dispatch! app (SetParam 3 2.0)) (ignore (wait-for scored?)) ;; In a window, so that the theme gives the controls their templates. (def frame (Window.)) (content! (cast ContentControl frame) (cast Object (app-control app))) (show! frame) (loop (:for (Tuple t name) (list (Tuple 0 "rules") (Tuple 1 "key-costs") (Tuple 2 "layout") (Tuple 3 "search"))) (:do (app-dispatch! app (Tab t)) (app-run-pending! app) (save-shot (cast Control frame) 1500 900 (path-combine folder (str name ".png"))))) ;; A short search, and its results. (app-dispatch! app (SetRuns 2.0)) (app-dispatch! app (SetSteps 20000.0)) (app-dispatch! app Search) (when (not (wait-for (fun (m) (and (not (.searching m)) (= (vec-length (.results m)) 2))))) (panic! (str "the search did not finish: " (.status (app-model app))))) (app-dispatch! app (Keep 0)) (when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) 17) (scored? m))))) (panic! "a kept layout was not scored")) (app-run-pending! app) (save-shot (cast Control frame) 1500 900 (path-combine folder "searched.png")) ;; --- presets (def had-presets (directory-exists? presets-folder)) (def had-layouts (file-exists? layouts-file)) (: weight (-> Model double)) (def weight (fun (m) (record-ref (.params m) key-weight))) (app-dispatch! app (SetPresetName "gui-test")) (app-dispatch! app SavePreset) (when (not (wait-for (fun (m) (vec-contains? (.presets m) "gui-test")))) (panic! (str "the preset was not saved: " (.status (app-model app))))) (app-dispatch! app (SetParam 3 7.0)) (when (not (wait-for (fun (m) (= (weight m) 7.0)))) (panic! "the key weight did not change")) (app-dispatch! app (LoadPreset "gui-test")) (when (not (wait-for (fun (m) (and (= (weight m) 2.0) (scored? m))))) (panic! (str "the preset was not loaded: " (.status (app-model app))))) (app-dispatch! app (Tab 0)) (app-run-pending! app) (save-shot (cast Control frame) 1500 900 (path-combine folder "presets.png")) (app-dispatch! app (DeletePreset "gui-test")) (when (not (wait-for (fun (m) (not (vec-contains? (.presets m) "gui-test"))))) (panic! "the preset was not deleted")) ;; --- saved layouts (: names (-> Model (Vec string))) (def names (fun (m) (vec-map layout-name-of (.layouts m)))) (: choose-named (-> string void)) (defun (choose-named n) (def m (app-model app)) (def r (ranking m)) (match (find (fun (pos) (= (layout-name-of (vec-ref (.layouts m) (vec-ref r pos))) n)) (range 0 (vec-length r))) ((Some pos) (app-dispatch! app (Choose pos))) (None (panic! (str "no layout is named " n))))) (app-dispatch! app (SetLayoutName "gui test")) (app-dispatch! app (SaveResult 1)) (when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) 18) (scored? m) (= (.status m) (str "Saved the layout gui test to " layouts-file ".")))))) (panic! (str "a result was not saved: " (.status (app-model app))))) (when (not (string-contains? (file-read-text layouts-file) "gui test\t")) (panic! "the saved layout is not in the file")) ;; The kept result, saved under a name, takes the place of "found 1". (choose-named "found 1") (app-dispatch! app (SetLayoutName "gui kept")) (app-dispatch! app SaveLayout) (when (not (wait-for (fun (m) (and (scored? m) (vec-contains? (.saved m) "gui kept"))))) (panic! (str "the kept layout was not saved: " (.status (app-model app))))) (when (or (not (= (vec-length (.layouts (app-model app))) 18)) (vec-contains? (names (app-model app)) "found 1")) (panic! "the kept layout was not renamed")) (when (not (wait-for (fun (m) (string-contains? (.status m) "Saved the layout gui kept")))) (panic! "the file was not written")) (app-dispatch! app (Tab 2)) (app-run-pending! app) (save-shot (cast Control frame) 1500 900 (path-combine folder "layout-saved.png")) (app-dispatch! app RemoveLayout) (when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) 17) (scored? m))))) (panic! "the kept layout was not removed")) (choose-named "gui test") (app-dispatch! app RemoveLayout) (when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) 16) (scored? m) (string-contains? (.status m) "Removed gui test from"))))) (panic! (str "the layouts were not removed: " (.status (app-model app))))) (when (string-contains? (file-read-text layouts-file) "gui ") (panic! "a removed layout is still in the file")) (when (not had-layouts) (file-delete layouts-file)) (when (and (not had-presets) (= (vec-length (directory-files presets-folder)) 0)) (directory-delete presets-folder)) (println "gui shots ok") 0)