Permalink
Browse files

Move ViewFinder protocol to a public namespace.

  • Loading branch information...
1 parent e3120eb commit 2a5c2004385f692685e8db7d6cddef32b1c49c19 @sattvik committed Mar 14, 2011
@@ -1,18 +0,0 @@
-; Copyright © 2011 Sattvik Software & Technology Resources, Ltd. Co.
-; All rights reserved.
-;
-; This program and the accompanying materials are made available under the
-; terms of the Eclipse Public License v1.0 which accompanies this distribution,
-; and is available at <http://www.eclipse.org/legal/epl-v10.html>.
-;
-; 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 neko.-protocols.view-finder
- "Home of the ViewFinder protocol and related utilities."
- {:author "Daniel Solano Gómez"})
-
-(defprotocol ViewFinder
- "Protocol for types that support finding child views by an ID."
- (-find-view [finder id]))
@@ -15,7 +15,8 @@
(:import android.app.Activity
android.view.View)
(:require [neko.context :as context])
- (:use [neko.-protocols resolvable view-finder]
+ (:use neko.find-view
+ neko.-protocols.resolvable
neko.-utils))
(def
@@ -41,31 +42,6 @@
(and (bound? #'*activity*)
(activity? *activity*)))
-(extend-type Activity
- ViewFinder
- (-find-view [activity id]
- (.findViewById activity (resolve-id id activity))))
-
-(defn find-view
- "Finds a view that identified by the given ID. Returns the view if found or
- nil otherwise. The id-or-name argument may either be the integer ID or a
- keyword name.
-
- If processed within a with-activity form, the activity argument may be
- omitted."
- ([id-or-name]
- {:pre [(resolvable? id-or-name)
- (has-*activity*?)]
- :post [(or (nil? %)
- (instance? View %))]}
- (-find-view *activity* id-or-name))
- ([activity id-or-name]
- {:pre [(activity? activity)
- (resolvable? id-or-name)]
- :post [(or (nil? %)
- (instance? View %))]}
- (-find-view activity id-or-name)))
-
(defn set-content-view!
"Sets the content for the activity. The view may be one of:
@@ -0,0 +1,91 @@
+; Copyright © 2011 Sattvik Software & Technology Resources, Ltd. Co.
+; All rights reserved.
+;
+; This program and the accompanying materials are made available under the
+; terms of the Eclipse Public License v1.0 which accompanies this distribution,
+; and is available at <http://www.eclipse.org/legal/epl-v10.html>.
+;
+; 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 neko.find-view
+ "Home of the ViewFinder protocol, which should simplify and unify use of the
+ findViewById method introduced in various Android classes. To use the
+ protocol, you need an object that supports findViewById, and these are:
+
+ + activities
+ + dialogs
+ + views
+ + windows
+
+ Given one of these objects, you can (find-view obj id). The id argument can
+ be an integer ID, as in the original method, or it can be a keyword that will
+ be resolved like:
+
+ (neko.context/resolve-resource context :id id)
+
+ For example, instead of calling:
+
+ (.findViewById activity R$id/my_view)
+
+ You can now use:
+
+ (find-view activity :my_view)
+
+ In addition, if within a with-activity form, you can leave out the activity
+ argument, simplifying the above call to:
+
+ (find-view :my_view)"
+ {:author "Daniel Solano Gómez"}
+ (:use neko.activity
+ neko.-protocols.resolvable))
+
+(defn- nil-or-view?
+ [x]
+ (or (nil? x)
+ (instance? android.view.View x)))
+
+(defprotocol ViewFinder
+ "Protocol for finding child views by an ID."
+ (find-view [id] [finder id]
+ "The two-arg version is the general version used outside of any context.
+ The one-arg version is designed for use within a (with-activity)
+ context."))
+
+(extend-protocol ViewFinder
+ android.view.Window
+ (find-view [window id]
+ {:pre [(resolvable? id)]
+ :post [(nil-or-view? %)]}
+ (.findViewById window (resolve-id id (.getContext window))))
+
+ android.app.Activity
+ (find-view [activity id]
+ {:pre [(resolvable? id)]
+ :post [(nil-or-view? %)]}
+ (.findViewById activity (resolve-id id activity)))
+
+ android.view.View
+ (find-view [view id]
+ {:pre [(resolvable? id)]
+ :post [(nil-or-view? %)]}
+ (.findViewById view (resolve-id id (.getContext view))))
+
+ android.app.Dialog
+ (find-view [dialog id]
+ {:pre [(resolvable? id)]
+ :post [(nil-or-view? %)]}
+ (.findViewById dialog (resolve-id id (.getContext dialog))))
+
+ Integer
+ (find-view [id]
+ {:pre [(has-*activity*?)]
+ :post [(nil-or-view? %)]}
+ (find-view *activity* id))
+
+ clojure.lang.Keyword
+ (find-view [id]
+ {:pre [(has-*activity*?)]
+ :post [(nil-or-view? %)]}
+ (find-view *activity* id)))
@@ -17,7 +17,6 @@
:constructors {[] [Class]}
:extends android.test.ActivityUnitTestCase
:methods [[testWithActivity [] void]
- [testFindView [] void]
[testSetContentViewWithID [] void]
[testSetContentViewWithIDNoContext [] void]
[testSetContentViewWithName [] void]
@@ -28,13 +27,13 @@
[testRequestWindowFeaturesOneFeature [] void]
[testRequestWindowFeaturesManyFeatures [] void]
[testRequestWindowFeaturesManyFeaturesInContext [] void]
- [testRequestWindowFeaturesBadFeatures [] void]
- ])
+ [testRequestWindowFeaturesBadFeatures [] void]])
(:import android.view.Window
com.sattvik.neko.test_app.R$id)
(:require [com.sattvik.neko.test_app.TestActivity :as test-activity])
(:use [neko activity
[context :only [*context*]]
+ find-view
test-utils]
neko.test-utils
junit.assert))
@@ -54,19 +53,6 @@
(is-not (bound? #'*context*))
(is-not (bound? #'*activity*))))
-(defn -testFindView
- "Tests the find-view function."
- [this]
- (let [activity (start-activity this)
- output-view (.findViewById activity R$id/output)]
- ; keyword
- (is-same output-view (find-view activity :output))
- ; id
- (is-same output-view (find-view activity R$id/output))
- (with-activity activity
- (is-same output-view (find-view :output))
- (is-same output-view (find-view R$id/output)))))
-
(defn -testSetContentViewWithID
"Tests the set-content-view! function using an ID within a with-activiity
context."
@@ -0,0 +1,151 @@
+; Copyright © 2011 Sattvik Software & Technology Resources, Ltd. Co.
+; All rights reserved.
+;
+; This program and the accompanying materials are made available under the
+; terms of the Eclipse Public License v1.0 which accompanies this distribution,
+; and is available at <http://www.eclipse.org/legal/epl-v10.html>.
+;
+; 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 neko.find-view-test
+ "Tests for the ViewFinder protocol."
+ {:author "Daniel Solano Gómez"}
+ (:gen-class :main false
+ :init init
+ :constructors {[] [Class]}
+ :extends android.test.ActivityUnitTestCase
+ :methods [[testActivity [] void]
+ [testWindow [] void]
+ [testView [] void]
+ [testDialog [] void]
+ [testOneArg [] void]])
+ (:import [com.sattvik.neko.test_app R$id R$layout])
+ (:use [neko activity
+ find-view
+ test-utils]
+ junit.assert))
+
+(defn -init []
+ [[com.sattvik.neko.test_app.TestActivity] nil])
+
+(defn -testActivity
+ "Tests the ViewFinder protocol with an Activity."
+ [this]
+ (let [activity (start-activity this)
+ output-view (.findViewById activity R$id/output)]
+ ; keyword
+ (is-same output-view (find-view activity :output))
+ ; id
+ (is-same output-view (find-view activity R$id/output))
+ ; views not available
+ (is-nil (find-view activity 0))
+ (is-nil (find-view activity :android/extractArea))
+ ; check one-arg function fails
+ (does-throw IllegalArgumentException
+ (find-view activity))
+ ; fails with bad arguments
+ (does-throw AssertionError
+ (find-view activity 'neko))
+ (does-throw AssertionError
+ (find-view activity "neko"))
+ (does-throw AssertionError
+ (find-view activity []))))
+
+(defn -testWindow
+ "Tests the ViewFinder protocol with a Window."
+ [this]
+ (let [window (.getWindow (start-activity this))
+ output-view (.findViewById window R$id/output)]
+ ; keyword
+ (is-same output-view (find-view window :output))
+ ; id
+ (is-same output-view (find-view window R$id/output))
+ ; views not available
+ (is-nil (find-view window 0))
+ (is-nil (find-view window :android/extractArea))
+ ; check one-arg function fails
+ (does-throw IllegalArgumentException
+ (find-view window))
+ ; fails with bad arguments
+ (does-throw AssertionError
+ (find-view window 'neko))
+ (does-throw AssertionError
+ (find-view window "neko"))
+ (does-throw AssertionError
+ (find-view window []))))
+
+(defn -testView
+ "Tests the ViewFinder protocol with a View."
+ [this]
+ (let [view (.. (start-activity this) getWindow getDecorView)
+ output-view (.findViewById view R$id/output)]
+ ; keyword
+ (is-same output-view (find-view view :output))
+ ; id
+ (is-same output-view (find-view view R$id/output))
+ ; views not available
+ (is-nil (find-view view 0))
+ (is-nil (find-view view :android/extractArea))
+ ; check one-arg function fails
+ (does-throw IllegalArgumentException
+ (find-view view))
+ ; fails with bad arguments
+ (does-throw AssertionError
+ (find-view view 'neko))
+ (does-throw AssertionError
+ (find-view view "neko"))
+ (does-throw AssertionError
+ (find-view view []))))
+
+(defn -testDialog
+ "Tests the ViewFinder protocol with a Dialog."
+ [this]
+ (let [activity (start-activity this)
+ custom-view (.. activity getLayoutInflater (inflate R$layout/main nil))
+ dialog (.create (doto (android.app.AlertDialog$Builder. activity)
+ (.setView custom-view)))
+ output-view (.findViewById dialog R$id/output)]
+ ; keyword
+ (is-same output-view (find-view dialog :output))
+ ; id
+ (is-same output-view (find-view dialog R$id/output))
+ ; views not available
+ (is-nil (find-view dialog 0))
+ (is-nil (find-view dialog :android/extractArea))
+ ; check one-arg function fails
+ (does-throw IllegalArgumentException
+ (find-view dialog))
+ ; fails with bad arguments
+ (does-throw AssertionError
+ (find-view dialog 'neko))
+ (does-throw AssertionError
+ (find-view dialog "neko"))
+ (does-throw AssertionError
+ (find-view dialog []))))
+
+(defn -testOneArg
+ "Tests the one-argument version of the protocol."
+ [this]
+ ; does not work on illegal types
+ (does-throw IllegalArgumentException
+ (find-view 'neko))
+ (does-throw IllegalArgumentException
+ (find-view "neko"))
+ (does-throw IllegalArgumentException
+ (find-view []))
+ ; fails outside of a valid context
+ (does-throw AssertionError
+ (find-view :output))
+ (does-throw AssertionError
+ (find-view R$id/output))
+ (with-activity (start-activity this)
+ (let [output-view (.findViewById *activity* R$id/output)]
+ ; keyword
+ (is-same output-view (find-view :output))
+ ; id
+ (is-same output-view (find-view R$id/output))
+ ; views not available
+ (is-nil (find-view 0))
+ (is-nil (find-view :android/extractArea)))))

0 comments on commit 2a5c200

Please sign in to comment.