nandi/jolt-nativepublic Fork 0
2271a9188bb358e15c020f96d9923f906e495c56
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Fire :on-activate with nothing, wrap a row, and break a word that cannot fit 2271a91 · on 2271a9188bb358e15c020f96d9923f906e495c56 · nandi · 9d ago
tests.clj · 248 lines · 10.5 KBClojure Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
(ns glimmer-jvui.tests
  "Every check here runs headless: no window, no SDL, no font, no display.

   jvui takes its measurer as a function, so the whole stack — glimmer's
   reconciler, this backend's walk, jvui's layout and event routing — can be
   driven with a stub that says eight pixels a character. `jolt test`."
  (:require [glimmer.ratom :as ra]
            [jvui.widgets :as w]
            [glimmer.core :as gui]
            [glimmer-jvui.core :as jv]
            [jvui.core :as c]))

(def ^:private failures (atom 0))

(defn- check! [ok? msg]
  (when-not ok?
    (swap! failures inc)
    (println "  FAIL:" msg)))

(defn- ctx []
  (c/context {:size [400 300]
              :measure (fn [s _] [(* 8.0 (count s)) 16.0])
              :line-height (fn [_] 16.0)}))

(defn- walk [n] (cons n (mapcat walk (:children @n))))
(defn- tagged [root tag] (first (filter #(= tag (:tag @%)) (walk root))))
(defn- labels [root]
  (map #(str (:label (:props @%)))
       (filter #(= :label (:tag @%)) (walk root))))

(defn- centre
  "The middle of the rectangle jvui gave this node on the last walk."
  [n]
  (let [[x y w h] (:rect @n)] [(+ x (/ w 2.0)) (+ y (/ h 2.0))]))

(defn- click-at [x y]
  [{:kind :motion :x x :y y}
   {:kind :mouse-down :x x :y y :button 1 :clicks 1}
   {:kind :mouse-up :x x :y y :button 1}])

;; --- the reconciler reaches the backend --------------------------------------

(def ^:private clicks (ra/atom 0))
(def ^:private on? (ra/atom false))

(defn- app []
  [:card {}
   [:title {:label "Counter"}]
   [:label {:label (str "Count: " (ra/deref clicks))}]
   [:button {:label "+1" :kind :primary :on-click #(ra/swap! clicks inc)}]
   [:checkbox {:label "loud" :checked (ra/deref on?)
               :on-change #(ra/reset! on? %)}]])

(defn- check-tree! []
  (let [root (jv/root-node)]
    (gui/mount root :page [app])
    (check! (some? (tagged root :card)) "the reconciler built a card")
    (check! (some? (tagged root :button)) "and a button")
    (check! (some #{"Count: 0"} (labels root))
            "and a label that read the ratom")))

(defn- check-walk-places-widgets! []
  (let [root (jv/root-node) cx (ctx)]
    (gui/mount root :page [app])
    (jv/render-once root cx)
    (let [placed (vals (:data @cx))
          card (first (filter #(and (:rect %) (> (first (:min-size % [0 0])) 100))
                              placed))]
      (check! (some? card) "the walk placed a container with a rectangle")
      (check! (every? (fn [[_ _ w h]] (and (>= w 0) (>= h 0)))
                      (keep :rect placed))
              "and no rectangle came out negative"))))

;; --- a click goes all the way round ------------------------------------------

(defn- check-click-fires-the-handler! []
  (let [root (jv/root-node) cx (ctx)]
    (ra/reset! clicks 0)
    (gui/mount root :page [app])
    (jv/render-once root cx)
    (jv/render-once root cx)
    (let [before (ra/deref clicks)
          [bx by] (centre (tagged root :button))]
      (jv/render-once root cx (click-at bx by))
      (check! (= (inc before) (ra/deref clicks))
              (str "the click reached :on-click (" before " -> "
                   (ra/deref clicks) ")"))
      (jv/render-once root cx)
      (check! (some #{(str "Count: " (ra/deref clicks))} (labels root))
              "and the reactive re-render reached the tree"))))

(defn- check-press-alone-is-not-a-click! []
  (let [root (jv/root-node) cx (ctx)]
    (ra/reset! clicks 0)
    (gui/mount root :page [app])
    (jv/render-once root cx)
    (jv/render-once root cx)
    (let [[bx by] (centre (tagged root :button))]
      (jv/render-once root cx [{:kind :motion :x bx :y by}
                               {:kind :mouse-down :x bx :y by :button 1 :clicks 1}]))
    (check! (zero? (ra/deref clicks)) "a press alone must not fire the handler")
    (jv/render-once root cx [{:kind :motion :x 390 :y 290}
                             {:kind :mouse-up :x 390 :y 290 :button 1}])
    (check! (zero? (ra/deref clicks)) "and releasing off it must not either")))

;; --- identity follows the node, not its index --------------------------------

(defn- check-reorder-keeps-identity! []
  ;; The reason every node carries a serial as its jvui key. Render a list,
  ;; reorder it, and the ids the walk hands out must travel with the nodes.
  (let [root (jv/root-node) cx (ctx)
        items (ra/atom [:a :b :c])
        list-app (fn []
                   (into [:vbox {}]
                         (for [k (ra/deref items)]
                           ^{:key k} [:button {:label (name k)}])))
        ids (fn [] (into {} (map (fn [n] [(:label (:props @n)) (:key @n)])
                                 (filter #(= :button (:tag @%)) (walk root)))))]
    (gui/mount root :page [list-app])
    (jv/render-once root cx)
    (let [before (ids)]
      (ra/reset! items [:c :a :b])
      (jv/render-once root cx)
      (let [after (ids)]
        (check! (= 3 (count after)) "the list still has three buttons")
        (check! (= (get before "a") (get after "a"))
                "a keyed node keeps its jvui key across a reorder")
        (check! (= (get before "c") (get after "c"))
                "including the one that moved to the front")))))

;; --- the vocabulary ----------------------------------------------------------

(defn- check-unknown-tag-is-a-container! []
  (let [root (jv/root-node) cx (ctx)]
    (gui/mount root :page [(fn [] [:flerb {} [:label {:label "inside"}]])])
    (jv/render-once root cx)
    (check! (some #{"inside"} (labels root))
            "an unknown tag shows its contents rather than raising")))

(defn- check-entry-round-trips! []
  (let [root (jv/root-node) cx (ctx)
        text (ra/atom "")]
    (gui/mount root :page
               [(fn [] [:entry {:value (ra/deref text)
                                :on-change #(ra/reset! text %)}])])
    (jv/render-once root cx)
    (let [[ex ey] (centre (tagged root :entry))]
      (jv/render-once root cx (click-at ex ey)))       ; take focus
    (jv/render-once root cx [{:kind :text :text "hi"}])
    (check! (= "hi" (ra/deref text))
            (str "typed text reached :on-change: " (pr-str (ra/deref text))))))

(defn- check-enter-sends! []
  ;; Enter is the one key a field must not swallow. frq sends its message
  ;; on it, so a compose box that accepted text and never reported Enter
  ;; would take a message and have no way to say it was finished — typing
  ;; works, sending does not, and nothing looks broken.
  (let [root (jv/root-node) cx (ctx)
        text (ra/atom "hello")
        sent (ra/atom nil)]
    (gui/mount root :page
               [(fn [] [:entry {:value (ra/deref text)
                                :on-change #(ra/reset! text %)
                                ;; A THUNK, which is what frq's handlers
                                ;; are: s/send-draft! takes no arguments
                                ;; and the text is already the caller's
                                ;; from :on-change.
                                :on-activate #(ra/reset! sent (ra/deref text))}])])
    (jv/render-once root cx)
    (let [[ex ey] (centre (tagged root :entry))]
      (jv/render-once root cx (click-at ex ey)))       ; take focus
    (jv/render-once root cx [{:kind :key-down :key :return}])
    (check! (= "hello" (ra/deref sent))
            (str "Enter reached :on-activate: " (pr-str (ra/deref sent))))))

(defn- label-node [root text]
  (first (filter #(and (= :label (:tag @%)) (= text (str (:label (:props @%)))))
                 (walk root))))

(defn- check-scroll-sticks! []
  ;; A chat that does not follow new messages is the difference between a
  ;; window you read and one you drag.
  ;;
  ;; Asserted on GEOMETRY and not on which labels exist: every line is in
  ;; the tree whether or not it is on screen, so `labels` cannot tell a
  ;; stuck list from a pinned one. What tells them apart is where the last
  ;; line was PUT — inside the viewport, or far below it.
  (let [root (jv/root-node) cx (ctx)
        n (ra/atom 3)]
    (gui/mount root :page
               [(fn [] [:scroll {:height 60 :scroll-key "chat" :stick-to-bottom true}
                        (into [:vbox {}]
                              (for [i (range (ra/deref n))]
                                [:label {:key i :label (str "line " i)}]))])])
    (dotimes [_ 3] (jv/render-once root cx))
    (ra/reset! n 40)
    (dotimes [_ 3] (jv/render-once root cx))
    ;; Forty lines of sixteen in a sixty-tall viewport leaves a long way
    ;; to scroll. A stuck list is at the far end of it; a pinned one is
    ;; still at zero.
    (let [off (w/scroll-offset "chat")]
      (check! (and off (> off 100.0))
              (str "a stuck list followed its content: offset=" off))
      ;; And it must STOP following once the reader has moved, or they can
      ;; never read anything but the newest line.
      ;; The mouse has to be OVER the list: a wheel is delivered to
      ;; whatever is under the pointer, and the pointer only moves on a
      ;; motion event.
      (jv/render-once root cx [{:kind :motion :x 20 :y 30}])
      (dotimes [_ 4]
        (jv/render-once root cx [{:kind :motion :x 20 :y 30}
                                 {:kind :wheel :x 20 :y 30 :dy 3}]))
      (let [off2 (w/scroll-offset "chat")]
        (check! (and off2 (< off2 off))
                (str "and let go when the reader scrolled up: " off " -> " off2))))))

(defn- check-checkbox-round-trips! []
  (let [root (jv/root-node) cx (ctx)]
    (ra/reset! on? false)
    (gui/mount root :page [app])
    (jv/render-once root cx)
    (jv/render-once root cx)
    (let [[bx by] (centre (tagged root :checkbox))]
      (jv/render-once root cx (click-at bx by)))
    (check! (true? (ra/deref on?))
            (str "the checkbox reached :on-change: " (ra/deref on?)))))

;; --- runner ------------------------------------------------------------------

(def ^:private checks
  [["the reconciler builds a tree"  check-tree!]
   ["the walk places widgets"       check-walk-places-widgets!]
   ["a click fires the handler"     check-click-fires-the-handler!]
   ["a press alone does not"        check-press-alone-is-not-a-click!]
   ["a reorder keeps identity"      check-reorder-keeps-identity!]
   ["an unknown tag is a container" check-unknown-tag-is-a-container!]
   ["a list sticks to the end"     check-scroll-sticks!]
   ["Enter sends"                  check-enter-sends!]
   ["an entry round-trips"          check-entry-round-trips!]
   ["a checkbox round-trips"        check-checkbox-round-trips!]])

(defn -main [& _]
  (doseq [[name f] checks]
    (println "-" name)
    (f))
  (if (zero? @failures)
    (println "\nall" (count checks) "checks passed")
    (do (println "\n" @failures "failed") (System/exit 1))))