Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 116 lines (107 sloc) 4.436 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
; Copyright (c) Dave Ray, 2011. All rights reserved.

; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this
; distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.

(ns seesaw.test.tree
  (:use seesaw.tree)
  (:use [seesaw.core :only [listen]])
  (:use [lazytest.describe :only (describe it testing given)]
        [lazytest.expect :only (expect)]))

(defn- tree-listener
  "Dummy TreeModelListener that calls handler with the received event."
  [handler]
  (reify javax.swing.event.TreeModelListener
    (treeNodesChanged [this e] (handler e))
    (treeNodesInserted [this e] (handler e))
    (treeNodesRemoved [this e] (handler e))
    (treeStructureChanged [this e] (handler e))))

(describe simple-tree-model
  (given [branch? (fn [node] (= node "dir"))
        children (fn [node] (when (= node "dir") [1 2 3]))
        m (simple-tree-model branch? children "dir")]
    (it "should create a read-only tree model from branch? and children functions"
      (instance? javax.swing.tree.TreeModel m))
    (it "should return the root"
      (= "dir" (.getRoot m)))
    (it "should return isLeaf"
      (expect (.isLeaf m "file"))
      (expect (not (.isLeaf m "dir"))))
    (it "should return the child count"
      (expect (= 0 (.getChildCount m "file")))
      (expect (= 3 (.getChildCount m "dir"))))
    (it "should return a child by index"
      (= [1 2 3] (map #(.getChild m "dir" %) (range 3))))
    (it "should retrieve the index of a child"
      (= [0 1 2] (map #(.getIndexOfChild m "dir" %) [1 2 3])))
    (it "should allow a listener to be added"
      (let [called (atom nil)]
        (.addTreeModelListener m (tree-listener #(reset! called %)))
        (node-changed m [(.getRoot m)])
        (expect @called)))
    (it "should allow a listener to be removed"
      (let [called-a (atom nil)
            called-b (atom nil)
            listener-a (tree-listener #(reset! called-a %))
            listener-b (tree-listener #(reset! called-b %))]
        (.addTreeModelListener m listener-a)
        (.addTreeModelListener m listener-b)
        (.removeTreeModelListener m listener-a)
        (node-changed m [(.getRoot m)])
        (expect (not @called-a))
        (expect @called-b)))))

(defn- make-test-model []
  (simple-tree-model #(.isDirectory %) #(.listFiles %) (java.io.File. ".")))

(describe fire-event
  (it "fires nodes-changed events"
    (let [m (make-test-model)
          e (atom nil)
          root (.getRoot m)]
      (listen m :tree-nodes-changed #(reset! e %))
      (nodes-changed m [root] (map #(.getChild m root %) (range 4)))
      (expect (not (nil? @e)))))
  (it "fires node-changed events"
    (let [m (make-test-model)
          e (atom nil)
          root (.getRoot m)]
      (listen m :tree-nodes-changed #(reset! e %))
      (node-changed m [root])
      (expect (not (nil? @e)))))
  (it "fires nodes-inserted events"
    (let [m (make-test-model)
          e (atom nil)
          root (.getRoot m)]
      (listen m :tree-nodes-inserted #(reset! e %))
      (nodes-inserted m [root] (map #(.getChild m root %) (range 4)))
      (expect (not (nil? @e)))))
  (it "fires node-inserted events"
    (let [m (make-test-model)
          e (atom nil)
          root (.getRoot m)]
      (listen m :tree-nodes-inserted #(reset! e %))
      (node-inserted m [root])
      (expect (not (nil? @e)))))

  (it "fires node-structure-changed events"
    (let [m (make-test-model)
          e (atom nil)
          root (.getRoot m)]
      (listen m :tree-structure-changed #(reset! e %))
      (node-structure-changed m [root])
      (expect (not (nil? @e)))))

  (it "fires nodes-removed events"
    (let [m (make-test-model)
          e (atom nil)
          root (.getRoot m)]
      (listen m :tree-nodes-removed #(reset! e %))
      (nodes-removed m [root] (range 1 4) (map #(.getChild m root %) (range 1 4)))
      (expect (not (nil? @e)))))

  (it "fires node-removed events"
    (let [m (make-test-model)
          e (atom nil)
          root (.getRoot m)]
      (listen m :tree-nodes-removed #(reset! e %))
      (node-removed m [root] 2 (.getChild m root 2))
      (expect (not (nil? @e))))))
Something went wrong with that request. Please try again.