Skip to content

Commit

Permalink
Tests for arrow interface
Browse files Browse the repository at this point in the history
  • Loading branch information
johngray1965 committed Feb 6, 2024
1 parent b27ceeb commit 9496ae9
Show file tree
Hide file tree
Showing 12 changed files with 740 additions and 40 deletions.
83 changes: 83 additions & 0 deletions PdfiumAndroid/arrow/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@



plugins {
alias(libs.plugins.androidLibrary)
alias(libs.plugins.kotlinAndroid)
`maven-publish`
}

android {
Expand Down Expand Up @@ -39,4 +43,83 @@ dependencies {
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(libs.truth)
androidTestImplementation(libs.kotlinx.coroutines.test)
androidTestImplementation(libs.androidx.core.testing)
androidTestImplementation(libs.arrow.fx.coroutines)
}

fun isReleaseBuild(): Boolean = !findProject("VERSION_NAME").toString().contains("SNAPSHOT")

fun getReleaseRepositoryUrl(): String {
return if (rootProject.hasProperty("RELEASE_REPOSITORY_URL")) {
rootProject.properties["RELEASE_REPOSITORY_URL"] as String
} else {
"https://oss.sonatype.org/service/local/staging/deploy/maven2/"
}
}

fun getSnapshotRepositoryUrl(): String {
return if (rootProject.hasProperty("SNAPSHOT_REPOSITORY_URL")) {
rootProject.properties["SNAPSHOT_REPOSITORY_URL"] as String
} else {
"https://oss.sonatype.org/content/repositories/snapshots/"
}
}

fun getRepositoryUrl(): String {
return if (isReleaseBuild()) getReleaseRepositoryUrl() else getSnapshotRepositoryUrl()
}

fun getRepositoryUsername(): String {
return if (rootProject.hasProperty("NEXUS_USERNAME")) rootProject.properties["NEXUS_USERNAME"] as String else ""
}

fun getRepositoryPassword(): String {
return if (rootProject.hasProperty("NEXUS_PASSWORD")) rootProject.properties["NEXUS_PASSWORD"] as String else ""
}

publishing {
publications {
create<MavenPublication>("maven") {
groupId = "io.legere"
artifactId = "pdfiumandroid-arrow"
version = rootProject.properties["VERSION_NAME"] as String

pom {
name.set("PdfiumAndroid.Arrow")
description.set("Arrow support for PdfiumAndroid")
url.set(rootProject.properties["POM_URL"] as String)
licenses {
license {
name.set(rootProject.properties["POM_LICENCE_NAME"] as String)
url.set(rootProject.properties["POM_LICENCE_URL"] as String)
}
}
developers {
developer {
id.set(rootProject.properties["POM_DEVELOPER_ID"] as String)
name.set(rootProject.properties["POM_DEVELOPER_NAME"] as String)
}
}
scm {
connection.set(rootProject.properties["POM_SCM_CONNECTION"] as String)
developerConnection.set(rootProject.properties["POM_SCM_DEV_CONNECTION"] as String)
url.set(rootProject.properties["POM_SCM_URL"] as String)
}
}
afterEvaluate {
from(components["release"])
}
}
}
repositories {
maven {
url = uri(getRepositoryUrl())
credentials {
username = getRepositoryUsername()
password = getRepositoryPassword()
}
}
}
}
4 changes: 4 additions & 0 deletions PdfiumAndroid/arrow/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POM_NAME=PdfiumAndroid
POM_ARTIFACT_ID=pdfiumandroid
POM_PACKAGING=aar

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.legere.pdfiumandroid.arrow

import android.graphics.RectF
import android.util.Log
import androidx.test.platform.app.InstrumentationRegistry

@Suppress("unused")
open class BasePDFTest {

// set to true to skip tests that are not implemented yet
// set to false to force unimplemented tests to fail
val notImplementedAssetValue = false

val noResultRect = RectF(-1f, -1f, -1f, -1f)

fun getPdfBytes(filename: String): ByteArray? {
val appContext = InstrumentationRegistry.getInstrumentation().context
val assetManager = appContext.assets
try {
val input = assetManager.open(filename)
return input.readBytes()
} catch (e: Exception) {
Log.e(BasePDFTest::class.simpleName, "Ugh", e)
}
assetManager.close()
return null
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package io.legere.pdfiumandroid.arrow

import androidx.test.ext.junit.runners.AndroidJUnit4
import arrow.core.raise.either
import com.google.common.truth.Truth
import io.legere.pdfiumandroid.PdfWriteCallback
import junit.framework.TestCase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class PdfDocumentKtFTest : BasePDFTest() {

private lateinit var pdfDocument: PdfDocumentKtF
private var pdfBytes: ByteArray? = null

@Before
fun setUp() = runBlocking {
pdfBytes = getPdfBytes("f01.pdf")

TestCase.assertNotNull(pdfBytes)

pdfDocument = PdfiumCoreKtF(Dispatchers.Unconfined).newDocument(pdfBytes).getOrNull()!!
}

@After
fun tearDown() = runTest {
pdfDocument.close()
}

@Test
fun getPageCount() = runTest {
either {
val pageCount = pdfDocument.getPageCount().bind()

assert(pageCount == 4) { "Page count should be 4" }
}
}

@Test
fun openPage() = runTest {
either {
val page = pdfDocument.openPage(0).bind()

TestCase.assertNotNull(page)
}
}

@Test
fun openPages() = runTest {
either {
val page = pdfDocument.openPages(0, 3).bind()

assert(page.size == 4) { "Page count should be 4" }
}
}

@Test
fun getDocumentMeta() = runTest {
either {
val meta = pdfDocument.getDocumentMeta().bind()

TestCase.assertNotNull(meta)
}
}

@Test
fun getTableOfContents() = runTest {
either {
// I don't think this test document has a table of contents
val toc = pdfDocument.getTableOfContents().bind()

TestCase.assertNotNull(toc)
Truth.assertThat(toc.size).isEqualTo(0)
}
}

@Test
fun openTextPage() = runTest {
either {
val page = pdfDocument.openPage(0).bind()
val textPage = page.openTextPage().bind()
TestCase.assertNotNull(textPage)
}
}

@Test
fun openTextPages() = runTest {
either {
val textPages = pdfDocument.openTextPages(0, 3).bind()
Truth.assertThat(textPages.size).isEqualTo(4)
}
}

@Test
fun saveAsCopy() = runTest {
pdfDocument.saveAsCopy(object : PdfWriteCallback {
override fun WriteBlock(data: ByteArray?): Int {
// Truth.assertThat(data?.size).isEqualTo(pdfBytes?.size)
// Truth.assertThat(data).isEqualTo(pdfBytes)
return data?.size ?: 0
}
})
}

fun close() = runTest {
either {
var documentAfterClose: PdfDocumentKtF?
PdfiumCoreKtF(Dispatchers.Unconfined).newDocument(pdfBytes).bind().use {
documentAfterClose = it
}
documentAfterClose?.openPage(0)
}.mapLeft {
Truth.assertThat(it).isInstanceOf(PdfiumKtFErrors.AlreadyClosed::class.java)
}
}
}
Loading

0 comments on commit 9496ae9

Please sign in to comment.