Skip to content

Commit

Permalink
Merge pull request #6448 from grzesiek2010/COLLECT-6447
Browse files Browse the repository at this point in the history
Include DynamicPreloadExtra in formDef only if a form contains search or pulldata
  • Loading branch information
seadowg authored Oct 15, 2024
2 parents 63050fd + b2f5b95 commit e828193
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.odk.collect.android.dynamicpreload

import org.javarosa.core.model.FormDef
import org.javarosa.core.model.QuestionDef
import org.javarosa.core.util.externalizable.ExtUtil
import org.javarosa.core.util.externalizable.Externalizable
import org.javarosa.core.util.externalizable.PrototypeFactory
import org.javarosa.xform.parse.IXFormParserFactory
Expand Down Expand Up @@ -50,22 +49,14 @@ class DynamicPreloadParseProcessor :
}

override fun processFormDef(formDef: FormDef) {
formDef.extras.put(DynamicPreloadExtra(containsPullData || containsSearch))
if (containsPullData || containsSearch) {
formDef.extras.put(DynamicPreloadExtra())
}
}
}

class DynamicPreloadExtra(usesDynamicPreload: Boolean) : Externalizable {

constructor() : this(false)

var usesDynamicPreload = usesDynamicPreload
private set
class DynamicPreloadExtra : Externalizable {
override fun readExternal(`in`: DataInputStream?, pf: PrototypeFactory?) = Unit

override fun readExternal(`in`: DataInputStream?, pf: PrototypeFactory?) {
usesDynamicPreload = ExtUtil.read(`in`, Boolean::class.javaObjectType) as Boolean
}

override fun writeExternal(out: DataOutputStream?) {
ExtUtil.write(out, usesDynamicPreload)
}
override fun writeExternal(out: DataOutputStream?) = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object ExternalDataUseCases {
isCancelled: Supplier<Boolean>,
progressReporter: Consumer<Function<Resources, String>>
) {
if (form.extras.get(DynamicPreloadExtra::class.java)?.usesDynamicPreload == false) {
if (form.extras.get(DynamicPreloadExtra::class.java) == null) {
return
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package org.odk.collect.android.dynamicpreload

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.instanceOf
import org.javarosa.core.util.externalizable.ExtUtil
import org.junit.Test

class DynamicPreloadExtraTest {

@Test
fun `can be externalized`() {
val extra = DynamicPreloadExtra(true)
val extra = DynamicPreloadExtra()

val external = ExtUtil.serialize(extra)
val deserialized =
ExtUtil.deserialize(external, DynamicPreloadExtra::class.java) as DynamicPreloadExtra

assertThat(deserialized.usesDynamicPreload, equalTo(true))
assertThat(deserialized, instanceOf(DynamicPreloadExtra::class.java))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.odk.collect.android.dynamicpreload

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.instanceOf
import org.javarosa.core.model.FormDef
import org.javarosa.core.model.QuestionDef
import org.javarosa.xpath.expr.XPathExpression
Expand All @@ -14,50 +15,50 @@ class DynamicPreloadParseProcessorTest {
private val processor = DynamicPreloadParseProcessor()

@Test
fun `usesDynamicPreload is false when xpath does not contain pulldata`() {
fun `DynamicPreloadExtra is null when xpath does not contain pulldata`() {
val formDef = FormDef()

processor.processXPath(createNonPullDataExpression())
processor.processFormDef(formDef)
assertThat(
formDef.extras.get(DynamicPreloadExtra::class.java).usesDynamicPreload,
equalTo(false)
formDef.extras.get(DynamicPreloadExtra::class.java),
equalTo(null)
)
}

@Test
fun `usesDynamicPreload is true when xpath does contain pulldata`() {
fun `DynamicPreloadExtra is not null when xpath does contain pulldata`() {
val formDef = FormDef()

processor.processXPath(createPullDataExpression())
processor.processFormDef(formDef)
assertThat(
formDef.extras.get(DynamicPreloadExtra::class.java).usesDynamicPreload,
equalTo(true)
formDef.extras.get(DynamicPreloadExtra::class.java),
instanceOf(DynamicPreloadExtra::class.java)
)
}

@Test
fun `usesDynamicPreload is false when question appearance does not contain search`() {
fun `DynamicPreloadExtra is null when question appearance does not contain search`() {
val formDef = FormDef()

processor.processQuestion(createQuestion(appearance = "minimal"))
processor.processFormDef(formDef)
assertThat(
formDef.extras.get(DynamicPreloadExtra::class.java).usesDynamicPreload,
equalTo(false)
formDef.extras.get(DynamicPreloadExtra::class.java),
equalTo(null)
)
}

@Test
fun `usesDynamicPreload is true when question appearance does contain search`() {
fun `DynamicPreloadExtra is not null when question appearance does contain search`() {
val formDef = FormDef()

processor.processQuestion(createQuestion(appearance = "search('fruits')"))
processor.processFormDef(formDef)
assertThat(
formDef.extras.get(DynamicPreloadExtra::class.java).usesDynamicPreload,
equalTo(true)
formDef.extras.get(DynamicPreloadExtra::class.java),
instanceOf(DynamicPreloadExtra::class.java)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ class ExternalDataUseCasesTest {

@Test
fun `create() does nothing if the form doesn't use dynamic preload`() {
val form = FormDef().also {
it.extras.put(DynamicPreloadExtra(false))
}
val form = FormDef()

val mediaDir = TempFiles.createTempDir().also {
File(it, "items.csv").writeText("name_key,name\nmango,Mango")
Expand All @@ -27,8 +25,23 @@ class ExternalDataUseCasesTest {
}

@Test
fun `create() works if the FormDef does not have a DynamicPreloadExtra()`() {
fun `create() does not create a db file if the FormDef does not have a DynamicPreloadExtra`() {
val form = FormDef()

val mediaDir = TempFiles.createTempDir().also {
File(it, "items.csv").writeText("name_key,name\nmango,Mango")
}

ExternalDataUseCases.create(form, mediaDir, { false }, {})
assertThat(mediaDir.listFiles().size, equalTo(1))
}

@Test
fun `create() creates a db file if the FormDef has a DynamicPreloadExtra`() {
val form = FormDef().also {
it.extras.put(DynamicPreloadExtra())
}

val mediaDir = TempFiles.createTempDir().also {
File(it, "items.csv").writeText("name_key,name\nmango,Mango")
}
Expand All @@ -40,7 +53,7 @@ class ExternalDataUseCasesTest {
@Test
fun `create() leaves original CSV in place`() {
val form = FormDef().also {
it.extras.put(DynamicPreloadExtra(true))
it.extras.put(DynamicPreloadExtra())
}

val mediaDir = TempFiles.createTempDir()
Expand Down

0 comments on commit e828193

Please sign in to comment.