Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 249 lines (225 sloc) 8.111 kb
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 249
(ns seesaw.test.bind
  (:refer-clojure :exclude [some])
  (:require [seesaw.core :as ssc])
  (:use seesaw.bind)
  (:use [lazytest.describe :only (describe it testing)]
        [lazytest.expect :only (expect)]))

(describe bind
  (it "returns a composite bindable"
    (satisfies? Bindable (bind (atom 0) (atom 1) (atom 2) (atom 3))))

  (it "can chain bindables together"
    (let [a (atom 1)
          b (atom nil)]
      (bind a (transform + 5) b)
      (reset! a 5)
      (expect (= 10 @b))))
  
  (it "can chain bindables, including composites, together"
    (let [a (atom 1)
          b (atom nil)]
      (bind a (bind (transform + 5) (transform * 2) (atom nil)) b)
      (reset! a 2)
      (expect (= 14 @b))))

  (it "should sync the enabled? property of a widget with an atom"
    (let [v (atom true)
          b (ssc/button)]
      (bind (property b :enabled?) v)
      (ssc/config! b :enabled? false)
      (expect (not @v))
      (reset! v true)))

  (it "should sync the enabled? property of a widget with an atom"
    (let [v (atom true)
          b (ssc/button)]
      (bind (property b :enabled?) v)
      (ssc/config! b :enabled? false)
      (expect (not @v))))

  (it "should sync an atom to the enabled? property of a widget"
    (let [v (atom true)
          b (ssc/button)]
      (bind v (property b :enabled?))
      (reset! v false)
      (expect (not (.isEnabled b)))))

  (testing "with a BoundedRangeModel"
    (it "Updates an atom when the model changes"
      (let [a (atom -1)
            m (javax.swing.DefaultBoundedRangeModel. 50 0 2 100)]
        (bind m a)
        (.setValue m 51)
        (expect (= 51 @a))))
    (it "Updates the model when the atom changes"
      (let [a (atom -1)
            m (javax.swing.DefaultBoundedRangeModel. 50 0 2 100)]
        (bind a m)
        (reset! a 99)
        (expect (= 99 (.getValue m))))))

  (testing "given a text field"
    (it "should update an atom when the underlying document changes"
      (let [a (atom nil)
            t (ssc/text "initial")]
        (bind (.getDocument t) a)
        (ssc/text! t "foo")
        (expect (= "foo" @a))))

    (it "should update the underlying document when the atom changes"
      (let [a (atom "initial")
            t (ssc/text "")]
        (bind a (.getDocument t))
        (reset! a "foo")
        (expect (= "foo" (ssc/text t))))))

  (testing "given a slider"
    (it "should sync the value of the atom with the slider value, if slider value changed"
      (let [v (atom 15)
            sl (ssc/slider :value @v)]
        (bind (.getModel sl) v)
        (.setValue sl 20)
        (expect (= @v 20))))
    (it "should sync the value of the slider with the atom value, if atom value changed"
      (let [v (atom 15)
            sl (ssc/slider :value @v)]
        (bind v (.getModel sl))
        (reset! v 20)
        (expect (= (.getValue sl) 20)))))

  (testing "given an agent"

    (it "should pass along changes to the agent's value"
      (let [start (agent nil)
            end (atom nil)]
        (bind start end)
        (send start (constantly :called))
        (await start)
        (expect (= :called @start))
        (expect (= :called @end))))

    (it "should throw an exception if you try to notify an agent"
      (let [start (atom nil)]
        (bind start (agent nil))
        (expect (try
                  (reset! start 99)
                  false
                  ; In Clojure 1.3, the exception propagates correctly
                  (catch IllegalStateException e
                    true)
                  ; Unfortunately, in Clojure 1.2, IllegalStateException gets wrapped by reset!
                  (catch RuntimeException e
                    (= IllegalStateException (class (.getCause e))))))))))

(describe tee
  (it "creates a tee junction in a bind"
    (let [start (atom 0)
          end1 (atom 0)
          end2 (atom 0)]
      (bind start (tee (bind (transform * 2) end1)
                       (bind (transform * 4) end2)))
      (reset! start 5)
      (expect (= 10 @end1))
      (expect (= 20 @end2)))))

(describe funnel
  (it "create a funnel in a bind which listens to multiple source and produces a vector of values"
    (let [a (atom 0)
          b (atom 1)
          f (funnel a b)
          end (atom nil)]
      (bind f end)
      (reset! a 5)
      (expect (= [5 nil] @end))
      (reset! b 6)
      (expect (= [5 6] @end)))))

(describe some
  (it "doesn't pass along falsey values returned by the predicate"
    (let [start (atom :foo)
          end (atom :bar)]
      (bind start (some (constantly nil)) end)
      (reset! start :something)
      (expect (= :bar @end))))
  (it "doesn't passes along result of predicate when it returns truthy"
    (let [start (atom :foo)
          end (atom :bar)]
      (bind start (some (constantly :yum)) end)
      (reset! start :something)
      (expect (= :yum @end)))))

(describe selection
  (it "sends out selection changes on a widget"
    (let [lb (ssc/listbox :model [:a :b :c])
          output (atom nil)]
      (bind (selection lb) output)
      (ssc/selection! lb :b)
      (expect (= :b @output))))
  (it "receives selection changes on a widget"
    (let [input (atom nil)
          lb (ssc/listbox :model [:a :b :c])]
      (bind input (selection lb))
      (reset! input :b)
      (expect (= :b (ssc/selection lb))))))

(describe to-bindable
  (it "returns arg if it's already bindable"
    (let [a (atom nil)]
      (expect (= a (to-bindable a)))))
  (it "converts a text component to its document"
    (let [t (ssc/text)]
      (expect (= (.getDocument t) (to-bindable t)))))
  (it "converts a slider to its model"
    (let [s (ssc/slider)]
      (expect (= (.getModel s) (to-bindable s))))))

(describe b-swap!
  (it "acts like swap! passing the old value, new value, and additional args to a function"
    (let [start (atom nil)
          target (atom [])
          end (atom nil)]
      (bind start
            (b-swap! target conj)
            end)
      (reset! start 1)
      (reset! start 2)
      (reset! start 3)
      (expect (= [1 2 3] @target))
      (expect (= @end @target)))))

(describe b-send
  (it "acts like send passing the old value, new value, and additional args to a function"
    (let [start (atom nil)
          target (agent [])]
      (bind start
            (b-send target conj) )
      (reset! start 1)
      (reset! start 2)
      (reset! start 3)
      (await target)
      (expect (= [1 2 3] @target)))))

(describe b-send-off
  (it "acts like sendoff passing the old value, new value, and additional args to a function"
    (let [start (atom nil)
          target (agent [])]
      (bind start
            (b-send-off target conj) )
      (reset! start 1)
      (reset! start 2)
      (reset! start 3)
      (await target)
      (expect (= [1 2 3] @target)))))

(describe notify-later
  (it "passes incoming values to the swing thread with invoke-later"
    (let [start (atom nil)
          end (atom nil)
          p (promise)]
      (bind start
            (notify-later)
            (transform (fn [v] {:value v :edt? (javax.swing.SwingUtilities/isEventDispatchThread)}))
            end)
      (subscribe end (fn [v] (deliver p :got-it)))
      (reset! start 99)
      (expect (= :got-it @p))
      (expect (= {:value 99 :edt? true} @end)))))

(describe notify-soon
  (it "passes incoming values to the swing thread with invoke-soon"
    (let [start (atom nil)
          end (atom nil)]
      (bind start
            (notify-soon)
            (transform (fn [v] {:value v :edt? (javax.swing.SwingUtilities/isEventDispatchThread)}))
            end)
      (ssc/invoke-now (reset! start 99))
      (expect (= {:value 99 :edt? true} @end)))))

(describe notify-now
  (it "passes incoming values to the swing thread with invoke-now"
    (let [start (atom nil)
          end (atom nil)]
      (bind start
            (notify-now)
            (transform (fn [v] {:value v :edt? (javax.swing.SwingUtilities/isEventDispatchThread)}))
            end)
      (reset! start 99)
      (expect (= {:value 99 :edt? true} @end)))))
Something went wrong with that request. Please try again.