Skip to content

Commit

Permalink
Add unit-test for SolidExportedDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
bailuk committed Jul 15, 2024
1 parent e1e1e69 commit 01ed65a
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 4 deletions.
4 changes: 4 additions & 0 deletions aat-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ dependencies {
val mapsForgeVersion: String by project


// Android support only Junit 4
// https://mvnrepository.com/artifact/junit/junit
testImplementation("junit:junit:4.12")

// MapsForge Android
implementation("org.mapsforge:mapsforge-map-android:$mapsForgeVersion")
implementation("com.caverock:androidsvg:1.4")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class SolidExportedDocument(storage: StorageInterface) : SolidString(storage, KE
const val LIMIT_MILLIS = 30 * 1000 // 1/2 minute
}

override fun setValueFromString(string: String) {
override fun setValue(string: String) {
time.setValue(System.currentTimeMillis())
super.setValueFromString(string)
super.setValue(string)
}

fun setDocument(file: Foc) {
setValueFromString(file.path);
setValue(file.path);
}

fun isExportAllowed(path: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ch.bailu.aat.preferences

import ch.bailu.aat_lib.preferences.OnPreferencesChanged
import ch.bailu.aat_lib.preferences.StorageInterface

class MockStorage : StorageInterface {
var mockIntValue: Int = 0
var mockStringValue = ""
var mockLongValue: Long = 0L


override fun readString(key: String): String {
return mockStringValue
}

override fun writeString(key: String, value: String) {
mockStringValue = value
}

override fun readInteger(key: String): Int {
return mockIntValue
}

override fun writeInteger(key: String, value: Int) {
mockIntValue = value
}

override fun writeIntegerForce(key: String, value: Int) {
TODO("Not yet implemented")
}

override fun readLong(key: String): Long {
return mockLongValue
}

override fun writeLong(key: String, value: Long) {
mockLongValue = value
}

override fun register(onPreferencesChanged: OnPreferencesChanged) {
TODO("Not yet implemented")
}

override fun unregister(onPreferencesChanged: OnPreferencesChanged) {
TODO("Not yet implemented")
}

override fun isDefaultString(s: String): Boolean {
return s.isEmpty()
}

override fun getDefaultString(): String {
return ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package ch.bailu.aat.preferences

import ch.bailu.foc.FocFile
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertTrue
import org.junit.Test


class SolidExportedDocumentTest {

@Test
fun testSetValueFromString() {
val mockStorage = MockStorage()

val exportedDocument = SolidExportedDocument(mockStorage)

assertEquals(false, exportedDocument.isExportAllowed(testFile1))

exportedDocument.setValueFromString(testFile1)

assertEquals(true, exportedDocument.isExportAllowed(testFile1))
assertEquals(false, exportedDocument.isExportAllowed(testFileEmpty))
assertEquals(false, exportedDocument.isExportAllowed(testFile2))

assertTrue(mockStorage.mockLongValue > 0)
mockStorage.mockLongValue -= LIMIT_MILLIS

assertEquals(false, exportedDocument.isExportAllowed(testFile1))
}


@Test
fun testSetValue() {
val mockStorage = MockStorage()

val exportedDocument = SolidExportedDocument(mockStorage)

assertEquals(false, exportedDocument.isExportAllowed(testFile1))

exportedDocument.setValue(testFile1)

assertEquals(true, exportedDocument.isExportAllowed(testFile1))
assertEquals(false, exportedDocument.isExportAllowed(testFileEmpty))
assertEquals(false, exportedDocument.isExportAllowed(testFile2))

assertTrue(mockStorage.mockLongValue > 0)
mockStorage.mockLongValue -= LIMIT_MILLIS

assertEquals(false, exportedDocument.isExportAllowed(testFile1))
}

@Test
fun testSetDocument() {
val mockStorage = MockStorage()

val exportedDocument = SolidExportedDocument(mockStorage)

assertEquals(false, exportedDocument.isExportAllowed(testFile1))

exportedDocument.setDocument(FocFile(testFile1))

assertEquals(true, exportedDocument.isExportAllowed(testFile1))
assertEquals(false, exportedDocument.isExportAllowed(testFileEmpty))
assertEquals(false, exportedDocument.isExportAllowed(testFile2))

assertTrue(mockStorage.mockLongValue > 0)
mockStorage.mockLongValue -= LIMIT_MILLIS

assertEquals(false, exportedDocument.isExportAllowed(testFile1))
}


@Test
fun testEmpty() {
val mockStorage = MockStorage()

val exportedDocument = SolidExportedDocument(mockStorage)

mockStorage.mockLongValue = System.currentTimeMillis() + LIMIT_MILLIS

assertEquals(false, exportedDocument.isExportAllowed(testFileEmpty))

exportedDocument.setValueFromString(testFileEmpty)

assertEquals(true, exportedDocument.isExportAllowed(testFileEmpty))
assertEquals(false, exportedDocument.isExportAllowed(testFile1))
assertEquals(false, exportedDocument.isExportAllowed(testFile2))

assertTrue(mockStorage.mockLongValue > 0)
mockStorage.mockLongValue -= LIMIT_MILLIS

assertEquals(false, exportedDocument.isExportAllowed(testFileEmpty))
}

companion object {
const val testFile1 = "/storage/path/file.gpx"
const val testFile2 = "/storage/path/file1.gpx"
const val testFileEmpty = ""
const val LIMIT_MILLIS = 30 * 1000 // 1/2 minute
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ open class SolidString(private val storage: StorageInterface, private val key: S
setValue(string)
}

fun setValue(v: String) {
open fun setValue(v: String) {
getStorage().writeString(key, v)
}

Expand Down

0 comments on commit 01ed65a

Please sign in to comment.