Skip to content

Commit

Permalink
Implement Unit test for W3WMLKitImageDataSource and OcrScanManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Duy Phạm authored and Duy Phạm committed Nov 6, 2024
1 parent 11548b0 commit 6ebcfd6
Show file tree
Hide file tree
Showing 12 changed files with 512 additions and 652 deletions.
6 changes: 5 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ jobs:
paths:
- ~/.gradle
key: jars2-{{ checksum "build.gradle.kts" }}-{{ checksum "lib/build.gradle.kts" }}

- android/create-avd:
avd-name: testDevice
system-image: system-images;android-30;google_apis;x86
system-image: system-images;android-30;google_apis;x86_64
install: true
additional-args: --device pixel_4

- android/start-emulator:
avd-name: testDevice
no-window: true
restore-gradle-cache-prefix: v2a

- android/run-tests:
test-command: ./gradlew lib:connectedDebugAndroidTest
max-tries: 3
Expand Down
9 changes: 6 additions & 3 deletions lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,14 @@ dependencies {
implementation("com.google.android.gms:play-services-base:18.5.0")

// Test dependencies
androidTestImplementation("androidx.test:runner:1.6.1")
androidTestUtil("androidx.test:orchestrator:1.5.0")
androidTestImplementation("androidx.test:runner:1.6.2")
androidTestUtil("androidx.test:orchestrator:1.5.1")
androidTestImplementation("androidx.test.ext:junit:1.2.1")
androidTestImplementation("io.mockk:mockk-android:1.13.3")
androidTestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.0")
androidTestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.1")
androidTestImplementation("org.junit.jupiter:junit-jupiter-api:5.9.3")
androidTestImplementation("org.junit.jupiter:junit-jupiter-params:5.9.3")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.3")

androidTestImplementation("com.google.mlkit:text-recognition:16.0.1")
androidTestImplementation("com.google.mlkit:text-recognition-chinese:16.0.1")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package com.what3words.ocr.components

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.what3words.core.types.common.W3WError
import com.what3words.core.types.domain.W3WAddress
import com.what3words.core.types.domain.W3WCountry
import com.what3words.core.types.domain.W3WSuggestion
import com.what3words.core.types.image.W3WImage
import com.what3words.core.types.language.W3WRFC5646Language
import com.what3words.ocr.components.fake.FakeImageDataSource
import com.what3words.ocr.components.fake.FakeTextDataSource
import com.what3words.ocr.components.ui.OcrScanManager
import com.what3words.ocr.components.ui.OcrScannerState
import io.mockk.every
import io.mockk.mockk
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertFalse
import junit.framework.TestCase.assertNotNull
import junit.framework.TestCase.assertNull
import junit.framework.TestCase.assertTrue
import junit.framework.TestCase.fail
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class OcrScanManagerTest {
private lateinit var ocrScanManager: OcrScanManager
private lateinit var fakeImageDataSource: FakeImageDataSource
private lateinit var fakeTextDataSource: FakeTextDataSource

@Before
fun setup() {
fakeImageDataSource = FakeImageDataSource()
fakeTextDataSource = FakeTextDataSource()
ocrScanManager = OcrScanManager(fakeImageDataSource, fakeTextDataSource)
}

@Test
fun testGetReadySuccess() {
var isReady = false
var error: W3WError? = null

fakeImageDataSource.setShouldSimulateStartError(false)

val onReady = {
isReady = true
}

val onError: ((W3WError) -> Unit) = {
error = it
}

ocrScanManager.getReady(onReady, onError)

assertTrue(isReady)
assertNull(error)
}

@Test
fun testGetReadyFailed() {
var isReady = false
var error: W3WError? = null

fakeImageDataSource.setShouldSimulateStartError(true)

val onReady = {
isReady = true
}

val onError: ((W3WError) -> Unit) = {
error = it
}

ocrScanManager.getReady(onReady, onError)

assertFalse(isReady)
assertNotNull(error)
}

@Test
fun testScanImage() = runTest {
fakeImageDataSource.setScanResults(listOf("index.home.raft"))
fakeTextDataSource.setSuggested3was(
listOf(
W3WSuggestion(
w3wAddress = W3WAddress(
"index.home.raft",
null,
null,
W3WRFC5646Language.EN_GB,
W3WCountry("en"),
"Bayswater, London"
),
rank = 0,
distanceToFocus = null
)
)
)

val mockImage: W3WImage = mockk()
every { mockImage.bitmap } returns mockk(relaxed = true)

var foundSuggestions: List<W3WSuggestion>? = null
var completed = false

ocrScanManager.getReady(
onReady = {

},
onError = {
fail("Error when ocrScanManager getReady")
}
)
ocrScanManager.scanImage(
mockImage,
onError = {
println("Error: $it")
fail("Unexpected error")
},
onFound = { foundSuggestions = it },
onCompleted = { completed = true }
)

// Assert the results
assertNotNull(foundSuggestions)
assertTrue(completed)
}

@Test
fun testOcrScannerState() = runTest {
// Initial state
assertEquals(OcrScannerState.State.Idle, ocrScanManager.ocrScannerState.value.state)
assertTrue(ocrScanManager.ocrScannerState.value.foundItems.isEmpty())

// Simulate scanning
val mockImage: W3WImage = mockk()
every { mockImage.bitmap } returns mockk(relaxed = true)

fakeImageDataSource.setScanResults(listOf("index.home.raft"))
fakeTextDataSource.setSuggested3was(
listOf(
W3WSuggestion(
w3wAddress = W3WAddress(
"index.home.raft",
null,
null,
W3WRFC5646Language.EN_GB,
W3WCountry("en"),
"Bayswater, London"
),
rank = 0,
distanceToFocus = null
)
)
)

ocrScanManager.getReady(
onReady = {

},
onError = {
fail("Error when ocrScanManager getReady")
}
)
ocrScanManager.scanImage(
mockImage,
onError = {
fail("Unexpected error")
},
onFound = {

},
onCompleted = {

}
)

// Verify the final state
assertEquals(OcrScannerState.State.Found, ocrScanManager.ocrScannerState.value.state)
assertEquals(1, ocrScanManager.ocrScannerState.value.foundItems.size)
assertEquals(
"index.home.raft",
ocrScanManager.ocrScannerState.value.foundItems[0].w3wAddress.words
)
}
}
Loading

0 comments on commit 6ebcfd6

Please sign in to comment.