Skip to content

Commit

Permalink
Make SyncRepoTest less convoluted
Browse files Browse the repository at this point in the history
I hadn't realized that Kotlin interfaces can have methods with bodies...

Not sure about the status of GitRepoTest.
  • Loading branch information
amberin committed Oct 6, 2024
1 parent 3e279f7 commit ca91362
Show file tree
Hide file tree
Showing 6 changed files with 460 additions and 901 deletions.
159 changes: 30 additions & 129 deletions app/src/androidTest/java/com/orgzly/android/repos/DocumentRepoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@ import com.orgzly.R
import com.orgzly.android.OrgzlyTest
import com.orgzly.android.db.entity.Repo
import com.orgzly.android.espresso.util.EspressoUtils
import com.orgzly.android.repos.SyncRepoTest.Companion.repoDirName
import com.orgzly.android.repos.SyncRepoTest.Companion.treeDocumentFileExtraSegment
import com.orgzly.android.ui.repos.ReposActivity
import com.orgzly.android.util.MiscUtils
import org.hamcrest.core.AllOf
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import java.io.IOException
import kotlin.io.path.Path

class DocumentRepoTest : SyncRepoTest, OrgzlyTest() {

private lateinit var documentTreeSegment: String
private lateinit var repo: Repo
private lateinit var syncRepo: SyncRepo
private lateinit var repoDirectory: DocumentFile
private lateinit var mSyncRepo: SyncRepo
private var treeDocumentFileUrl = if (Build.VERSION.SDK_INT < 30) {
"content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2F$repoDirName"
} else {
"content://com.android.externalstorage.documents/tree/primary%3A$repoDirName"
}

@Before
override fun setUp() {
Expand All @@ -45,138 +51,33 @@ class DocumentRepoTest : SyncRepoTest, OrgzlyTest() {
}
}

@Test
override fun testGetBooks_singleOrgFile() {
SyncRepoTest.testGetBooks_singleOrgFile(repoDirectory, syncRepo)
}

@Test
override fun testGetBooks_singleFileInSubfolderWhenEnabled() {
SyncRepoTest.testGetBooks_singleFileInSubfolderWhenEnabled(repoDirectory, syncRepo)
}

@Test
override fun testGetBooks_singleFileInSubfolderWhenDisabled() {
SyncRepoTest.testGetBooks_singleFileInSubfolderWhenDisabled(repoDirectory, syncRepo)
}

@Test
override fun testGetBooks_allFilesAreIgnored() {
SyncRepoTest.testGetBooks_allFilesAreIgnored(repoDirectory, syncRepo)
}

@Test
override fun testGetBooks_specificFileInSubfolderIsIgnored() {
SyncRepoTest.testGetBooks_specificFileInSubfolderIsIgnored(repoDirectory, syncRepo)
}

@Test
override fun testGetBooks_specificFileIsUnignored() {
SyncRepoTest.testGetBooks_specificFileIsUnignored(repoDirectory, syncRepo)
}

@Test
override fun testGetBooks_ignoredExtensions() {
SyncRepoTest.testGetBooks_ignoredExtensions(repoDirectory, syncRepo)
}

@Test
override fun testStoreBook_expectedUri() {
SyncRepoTest.testStoreBook_expectedUri(syncRepo)
}

@Test
override fun testStoreBook_producesSameUriAsRetrieveBookWithSubfolder() {
SyncRepoTest.testStoreBook_producesSameUriAsRetrieveBookWithSubfolder(syncRepo)
}

@Test
override fun testStoreBook_producesSameUriAsRetrieveBookWithoutSubfolder() {
SyncRepoTest.testStoreBook_producesSameUriAsRetrieveBookWithoutSubfolder(syncRepo)
}

@Test
override fun testStoreBook_producesSameUriAsGetBooks() {
SyncRepoTest.testStoreBook_producesSameUriAsGetBooks(repoDirectory, syncRepo)
}

@Test
override fun testStoreBook_inSubfolder() {
SyncRepoTest.testStoreBook_inSubfolder(repoDirectory, syncRepo)
}

@Test(expected = IOException::class)
override fun testStoreBook_inSubfolderWhenDisabled() {
SyncRepoTest.testStoreBook_inSubfolderWhenDisabled(syncRepo)
}

@Test
override fun testRenameBook_expectedUri() {
SyncRepoTest.testRenameBook_expectedUri(syncRepo)
}

@Test(expected = IOException::class)
override fun testRenameBook_repoFileAlreadyExists() {
SyncRepoTest.testRenameBook_repoFileAlreadyExists(repoDirectory, syncRepo)
}

@Test
override fun testRenameBook_fromRootToSubfolderWhenEnabled() {
SyncRepoTest.testRenameBook_fromRootToSubfolderWhenEnabled(syncRepo)
}

@Test(expected = IOException::class)
override fun testRenameBook_fromRootToSubfolderWhenDisabled() {
SyncRepoTest.testRenameBook_fromRootToSubfolderWhenDisabled(syncRepo)
}

@Test
override fun testRenameBook_fromSubfolderToRoot() {
SyncRepoTest.testRenameBook_fromSubfolderToRoot(syncRepo)
}

@Test
override fun testRenameBook_newSubfolderSameLeafName() {
SyncRepoTest.testRenameBook_newSubfolderSameLeafName(syncRepo)
}

@Test
override fun testRenameBook_newSubfolderAndLeafName() {
SyncRepoTest.testRenameBook_newSubfolderAndLeafName(syncRepo)
}
override var syncRepo: SyncRepo
get() = mSyncRepo
set(value) {}
override val repoManipulationPoint: Any
get() = repoDirectory

@Test
override fun testRenameBook_sameSubfolderNewLeafName() {
SyncRepoTest.testRenameBook_sameSubfolderNewLeafName(syncRepo)
override fun writeFileToRepo(content: String, repoRelativePath: String): String {
val targetPath = Path(repoRelativePath)
var expectedRookUri = mSyncRepo.uri.toString() + treeDocumentFileExtraSegment + Uri.encode(repoRelativePath)
var targetDir = repoDirectory
if (repoRelativePath.contains("/")) {
targetDir = targetDir.createDirectory(targetPath.parent.toString())!!
expectedRookUri = mSyncRepo.uri.toString() + treeDocumentFileExtraSegment + Uri.encode(repoRelativePath)
}
MiscUtils.writeStringToDocumentFile(content, targetPath.fileName.toString(), targetDir.uri)
return expectedRookUri
}

private fun setupDocumentRepo(extraDir: String? = null) {
val repoDirName = SyncRepoTest.repoDirName
documentTreeSegment = if (Build.VERSION.SDK_INT < 30) {
"/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2F$repoDirName%2F"
} else {
"/document/primary%3A$repoDirName%2F"
}
var treeDocumentFileUrl = if (Build.VERSION.SDK_INT < 30) {
"content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2F$repoDirName"
} else {
"content://com.android.externalstorage.documents/tree/primary%3A$repoDirName"
}
if (extraDir != null) {
treeDocumentFileUrl = "$treeDocumentFileUrl%2F" + Uri.encode(extraDir)
}
private fun setupDocumentRepo() {
repoDirectory = DocumentFile.fromTreeUri(context, treeDocumentFileUrl.toUri())!!
repo = if (!repoDirectory.exists()) {
if (extraDir != null) {
setupDocumentRepoInUi(extraDir)
} else {
setupDocumentRepoInUi(repoDirName)
}
setupDocumentRepoInUi()
dataRepository.getRepos()[0]
} else {
testUtils.setupRepo(RepoType.DOCUMENT, treeDocumentFileUrl)
}
syncRepo = testUtils.repoInstance(RepoType.DOCUMENT, repo.url, repo.id)
mSyncRepo = testUtils.repoInstance(RepoType.DOCUMENT, repo.url, repo.id)
Assert.assertEquals(treeDocumentFileUrl, repo.url)
}

Expand All @@ -186,7 +87,7 @@ class DocumentRepoTest : SyncRepoTest, OrgzlyTest() {
* a different repo URL, making some tests fail. If you are running locally, you must work
* around this by wiping the device's data between test suite runs.
*/
private fun setupDocumentRepoInUi(repoDirName: String) {
private fun setupDocumentRepoInUi() {
ActivityScenario.launch(ReposActivity::class.java).use {
Espresso.onView(ViewMatchers.withId(R.id.activity_repos_directory))
.perform(ViewActions.click())
Expand Down Expand Up @@ -220,4 +121,4 @@ class DocumentRepoTest : SyncRepoTest, OrgzlyTest() {
.perform(ViewActions.click())
}
}
}
}
138 changes: 25 additions & 113 deletions app/src/test/java/com/orgzly/android/repos/DropboxRepoTest.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
package com.orgzly.android.repos

import android.net.Uri
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.orgzly.BuildConfig
import com.orgzly.android.db.entity.Repo
import com.orgzly.android.prefs.AppPreferences
import com.orgzly.android.repos.SyncRepoTest.Companion.repoDirName
import com.orgzly.android.util.MiscUtils
import org.json.JSONObject
import org.junit.After
import org.junit.Assume.assumeTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.io.IOException
import java.util.UUID

@RunWith(AndroidJUnit4::class)
class DropboxRepoTest : SyncRepoTest {

private lateinit var syncRepo: SyncRepo
private lateinit var client: DropboxClient
private lateinit var mSyncRepo: SyncRepo

override var syncRepo: SyncRepo
get() = mSyncRepo
set(value) {}

override val repoManipulationPoint: Any
get() = client

override fun writeFileToRepo(content: String, repoRelativePath: String): String {
val tmpFile = kotlin.io.path.createTempFile().toFile()
MiscUtils.writeStringToFile(content, tmpFile)
val expectedRookUri = mSyncRepo.uri.toString() + "/" + Uri.encode(repoRelativePath, "/")
client.upload(tmpFile, mSyncRepo.uri, repoRelativePath)
tmpFile.delete()
return expectedRookUri
}

@Before
fun setup() {
Expand All @@ -33,123 +50,18 @@ class DropboxRepoTest : SyncRepoTest {
ApplicationProvider.getApplicationContext(),
mockSerializedDbxCredential.toString()
)
val repo = Repo(0, RepoType.DROPBOX, "dropbox:/${SyncRepoTest.repoDirName}/" + UUID.randomUUID().toString())
val repo = Repo(0, RepoType.DROPBOX, "dropbox:/$repoDirName/" + UUID.randomUUID().toString())
val repoPropsMap = HashMap<String, String>()
val repoWithProps = RepoWithProps(repo, repoPropsMap)
syncRepo = DropboxRepo(repoWithProps, ApplicationProvider.getApplicationContext())
mSyncRepo = DropboxRepo(repoWithProps, ApplicationProvider.getApplicationContext())
client = DropboxClient(ApplicationProvider.getApplicationContext(), repo.id)
}

@After
fun tearDown() {
if (this::syncRepo.isInitialized) {
val dropboxRepo = syncRepo as DropboxRepo
dropboxRepo.deleteDirectory(syncRepo.uri)
if (this::mSyncRepo.isInitialized) {
val dropboxRepo = mSyncRepo as DropboxRepo
dropboxRepo.deleteDirectory(mSyncRepo.uri)
}
}

@Test
override fun testGetBooks_singleOrgFile() {
SyncRepoTest.testGetBooks_singleOrgFile(client, syncRepo)
}

@Test
override fun testGetBooks_singleFileInSubfolderWhenEnabled() {
SyncRepoTest.testGetBooks_singleFileInSubfolderWhenEnabled(client, syncRepo)
}

@Test
override fun testGetBooks_singleFileInSubfolderWhenDisabled() {
SyncRepoTest.testGetBooks_singleFileInSubfolderWhenDisabled(client, syncRepo)
}

@Test
override fun testGetBooks_allFilesAreIgnored() {
SyncRepoTest.testGetBooks_allFilesAreIgnored(client, syncRepo)
}

@Test
override fun testGetBooks_specificFileInSubfolderIsIgnored() {
SyncRepoTest.testGetBooks_specificFileInSubfolderIsIgnored(client, syncRepo)
}

@Test
override fun testGetBooks_specificFileIsUnignored() {
SyncRepoTest.testGetBooks_specificFileIsUnignored(client, syncRepo)
}

@Test
override fun testGetBooks_ignoredExtensions() {
SyncRepoTest.testGetBooks_ignoredExtensions(client, syncRepo)
}

@Test
override fun testStoreBook_expectedUri() {
SyncRepoTest.testStoreBook_expectedUri(syncRepo)
}

@Test
override fun testStoreBook_producesSameUriAsRetrieveBookWithSubfolder() {
SyncRepoTest.testStoreBook_producesSameUriAsRetrieveBookWithSubfolder(syncRepo)
}

@Test
override fun testStoreBook_producesSameUriAsRetrieveBookWithoutSubfolder() {
SyncRepoTest.testStoreBook_producesSameUriAsRetrieveBookWithoutSubfolder(syncRepo)
}

@Test
override fun testStoreBook_producesSameUriAsGetBooks() {
SyncRepoTest.testStoreBook_producesSameUriAsGetBooks(client, syncRepo)
}

@Test
override fun testStoreBook_inSubfolder() {
SyncRepoTest.testStoreBook_inSubfolder(client, syncRepo)
}

@Test(expected = IOException::class)
override fun testStoreBook_inSubfolderWhenDisabled() {
SyncRepoTest.testStoreBook_inSubfolderWhenDisabled(syncRepo)
}

@Test
override fun testRenameBook_expectedUri() {
SyncRepoTest.testRenameBook_expectedUri(syncRepo)
}

@Test(expected = IOException::class)
override fun testRenameBook_repoFileAlreadyExists() {
SyncRepoTest.testRenameBook_repoFileAlreadyExists(client, syncRepo)
}

@Test
override fun testRenameBook_fromRootToSubfolderWhenEnabled() {
SyncRepoTest.testRenameBook_fromRootToSubfolderWhenEnabled(syncRepo)
}

@Test(expected = IOException::class)
override fun testRenameBook_fromRootToSubfolderWhenDisabled() {
SyncRepoTest.testRenameBook_fromRootToSubfolderWhenDisabled(syncRepo)
}

@Test
override fun testRenameBook_fromSubfolderToRoot() {
SyncRepoTest.testRenameBook_fromSubfolderToRoot(syncRepo)
}

@Test
override fun testRenameBook_newSubfolderSameLeafName() {
SyncRepoTest.testRenameBook_newSubfolderSameLeafName(syncRepo)
}

@Test
override fun testRenameBook_newSubfolderAndLeafName() {
SyncRepoTest.testRenameBook_newSubfolderAndLeafName(syncRepo)
}

@Test
override fun testRenameBook_sameSubfolderNewLeafName() {
SyncRepoTest.testRenameBook_sameSubfolderNewLeafName(syncRepo)
}
}
Loading

0 comments on commit ca91362

Please sign in to comment.