keyscore/tests/gui-shots.bjo

246 lines
13 KiB
Text
Raw Permalink Normal View History

;; 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.
;; - Copies ZYK,Å, edits a row of the copy, swaps two keys on its board,
;; and checks that bad rows and edits of a built-in layout are rejected.
;; Saves editing.png, then removes the copy.
;; - 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 (keyscore score))
(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))
;; The chosen layout's mean score.
(: (-> Model double))
(defun (mean-total m) (mean-of m (.chosen m) (fun (s) (record-ref s total))))
;; The rows of the layout named `n`.
(: (-> Model string (Vec string)))
(defun (rows-named m n)
(match (find (fun (l) (= (layout-name-of l) n)) (.layouts m))
((Some l) (record-ref l rows))
(None (panic! (str "no layout is named " n)))))
;; The rows of the chosen layout.
(: (-> Model (Vec string)))
(defun (chosen-rows m) (record-ref (vec-ref (.layouts m) (.chosen m)) rows))
(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))
;; What the saved layouts file holds, written back at the end.
(def layouts-before (if (file-exists? layouts-file) (Some (file-read-text layouts-file)) None))
(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"))
;; The built-in layouts and those saved before the test.
(def base (vec-length (.layouts (app-model app))))
(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)) (+ base 1)) (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))
(: 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)) (+ base 2)) (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))) (+ base 2)))
(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)) (+ base 1)) (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)) base) (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"))
;; --- editing
(choose-named "ZYK,Å")
(app-dispatch! app (SetRow 0 "YZK,Å XCHPW"))
(when (not (wait-for (fun (m) (string-starts-with? (.status m) "A built-in layout cannot be changed"))))
(panic! "an edit of a built-in layout was not rejected"))
(when (not (= (vec-ref (chosen-rows (app-model app)) 0) "ZYK,Å XCHPW"))
(panic! "a built-in layout was changed"))
(app-dispatch! app CopyLayout)
(when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) (+ base 1)) (scored? m)
(= (layout-name-of (vec-ref (.layouts m) (.chosen m))) "ZYK,Å copy")))))
(panic! (str "the layout was not copied: " (.status (app-model app)))))
(def before-edit (mean-total (app-model app)))
(app-dispatch! app (SetRow 0 "YZK,Å XCHPW"))
(when (not (wait-for (fun (m) (and (scored? m) (= (vec-ref (chosen-rows m) 0) "YZK,Å XCHPW")
(not (= (mean-total m) before-edit))))))
(panic! (str "a row edit was not applied and scored: " (.status (app-model app)))))
(when (not (= (vec-ref (rows-named (app-model app) "ZYK,Å") 0) "ZYK,Å XCHPW"))
(panic! "the edit changed the original layout"))
;; A character on two keys, and one on no key.
(app-dispatch! app (SetRow 1 "IEDAA VTRNS"))
(when (not (wait-for (fun (m) (string-starts-with? (.status m) "On two keys: a"))))
(panic! (str "a character on two keys was not rejected: " (.status (app-model app)))))
(when (not (and (= (vec-ref (chosen-rows (app-model app)) 1) "IEDAÄ VTRNS")
(= (vec-ref (.rows-text (app-model app)) 1) "IEDAA VTRNS")))
(panic! "a rejected row changed the layout, or was not kept in its field"))
(app-dispatch! app (SetRow 1 "IEDAÄ VTRNS"))
(app-dispatch! app (SetRow 2 "ÖUFO."))
(when (not (wait-for (fun (m) (string-starts-with? (.status m) "On no key: "))))
(panic! (str "a missing character was not rejected: " (.status (app-model app)))))
(app-dispatch! app (SetRow 2 "ÖUFO. Q GMLBJ"))
;; Q and W are keys 0 and 1 of `board`: Y and Z change places again.
(app-dispatch! app (SwapKeys 0 1))
(when (not (wait-for (fun (m) (and (scored? m) (= (vec-ref (chosen-rows m) 0) "ZYK,Å XCHPW")
(= (vec-ref (.rows-text m) 0) "ZYK,Å XCHPW")))))
(panic! (str "a key swap was not applied: " (vec-ref (chosen-rows (app-model app)) 0))))
(app-dispatch! app (SetRow 0 "YZK,Å XCHPW"))
(ignore (wait-for scored?))
(app-dispatch! app (Tab 2))
(app-run-pending! app)
(save-shot (cast Control frame) 1500 900 (path-combine folder "editing.png"))
(app-dispatch! app RemoveLayout)
(when (not (wait-for (fun (m) (and (= (vec-length (.layouts m)) base) (scored? m)))))
(panic! "the copy was not removed"))
(match layouts-before
((Some text) (file-write-text layouts-file text))
(None (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)