Skip to content

Commit

Permalink
Merge branch '1581-Sqlcipher'
Browse files Browse the repository at this point in the history
  • Loading branch information
mtotschnig committed Oct 31, 2024
2 parents 0567acd + 05cc5c0 commit 0da5fd5
Show file tree
Hide file tree
Showing 66 changed files with 929 additions and 283 deletions.
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ securityCrypto = "1.1.0-alpha06"
simpledialogfragments = "09a642bc42"
slf4jApi = "2.0.13"
snakeyaml = "2.2"
sqlcipher = "4.5.4"
sqlcipher = "4.6.1"
taptargetview = "1.13.3"
tesseract4androidOpenmp = "4.7.0"
mlkitTextRecognition = "16.0.1"
Expand All @@ -112,7 +112,7 @@ accompanist-themeadapter-material3 = { module = "com.google.accompanist:accompan
acra-core = { module = "ch.acra:acra-core", version.ref = "acraVersion" }
acra-dialog = { module = "ch.acra:acra-dialog", version.ref = "acraVersion" }
acra-mail = { module = "ch.acra:acra-mail", version.ref = "acraVersion" }
android-database-sqlcipher = { module = "net.zetetic:android-database-sqlcipher", version.ref = "sqlcipher" }
android-database-sqlcipher = { module = "net.zetetic:sqlcipher-android", version.ref = "sqlcipher" }
android-image-cropper = { module = "com.github.mtotschnig:Android-Image-Cropper", version.ref = "androidImageCropper" }
android-state = { module = "com.evernote:android-state", version.ref = "evernote" }
android-state-processor = { module = "com.evernote:android-state-processor", version.ref = "evernote" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.totschnig.myexpenses.db2.getUuidForAccount
import org.totschnig.myexpenses.provider.DatabaseConstants
import org.totschnig.myexpenses.testutils.BaseUiTest
import org.totschnig.myexpenses.testutils.Espresso.wait
import org.totschnig.myexpenses.testutils.cleanup
import org.totschnig.myexpenses.testutils.withListSize

class AccountEditTest : BaseUiTest<AccountEdit>() {
Expand All @@ -34,6 +35,9 @@ class AccountEditTest : BaseUiTest<AccountEdit>() {
clickFab()
assertFinishing()
assertThat(repository.findAnyOpenByLabel(LABEL)).isNotNull
cleanup {
deleteAccount(LABEL)
}
}

@Test
Expand All @@ -47,6 +51,9 @@ class AccountEditTest : BaseUiTest<AccountEdit>() {
testScenario = ActivityScenario.launchActivityForResult(i)
clickFab()
assertThat(repository.getUuidForAccount(id)).isEqualTo(uuid)
cleanup {
deleteAccount(LABEL)
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withId
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.totschnig.myexpenses.R
Expand All @@ -20,11 +21,14 @@ import org.totschnig.myexpenses.compose.TEST_TAG_FILTER_CARD
import org.totschnig.myexpenses.compose.TEST_TAG_LIST
import org.totschnig.myexpenses.compose.TEST_TAG_PART_LIST
import org.totschnig.myexpenses.db2.archive
import org.totschnig.myexpenses.db2.deleteAccount
import org.totschnig.myexpenses.db2.deleteCategory
import org.totschnig.myexpenses.model.CurrencyUnit
import org.totschnig.myexpenses.model.Money
import org.totschnig.myexpenses.model.Transaction
import org.totschnig.myexpenses.model2.Account
import org.totschnig.myexpenses.testutils.BaseMyExpensesTest
import org.totschnig.myexpenses.testutils.cleanup
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
Expand Down Expand Up @@ -55,6 +59,13 @@ class ArchiveDetailWithSearchTest : BaseMyExpensesTest() {
repository.archive(account.id, date to date)
}

@After
fun clearDb() {
cleanup {
repository.deleteAccount(account.id)
}
}

@Test
fun showDetailWithSearch() {
launch(account.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import org.totschnig.myexpenses.delegate.TransactionDelegate
import org.totschnig.myexpenses.model.Template
import org.totschnig.myexpenses.model2.Account
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_ACCOUNTID
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_ROWID
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_TITLE
import org.totschnig.myexpenses.provider.TransactionProvider.TEMPLATES_URI
import org.totschnig.myexpenses.testutils.BaseComposeTest
import org.totschnig.myexpenses.testutils.withIdAndParent
import org.totschnig.myexpenses.testutils.withOperationType
Expand Down Expand Up @@ -104,16 +107,25 @@ abstract class BaseExpenseEditTest : BaseComposeTest<TestExpenseEdit>() {
}

protected fun assertTemplate(
templateId: Long,
expectedAccount: Long,
expectedAmount: Long,
templateTitle: String = TEMPLATE_TITLE,
expectedTags: List<String> = emptyList()
) {
val templateId = contentResolver.query(
TEMPLATES_URI,
arrayOf(KEY_ROWID),
"$KEY_TITLE = ?",
arrayOf(templateTitle),
null
)!!.use {
it.moveToFirst()
it.getLong(0)
}
val (transaction, tags) = Template.getInstanceFromDbWithTags(contentResolver, templateId)!!
with(transaction as Template) {
assertThat(title).isEqualTo(templateTitle)
assertThat(amount.amountMinor).isEqualTo(expectedAmount)
assertThat(title).isEqualTo(templateTitle)
assertThat(accountId).isEqualTo(expectedAccount)
}
assertThat(tags.map { it.label }).containsExactlyElementsIn(expectedTags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withText
import org.hamcrest.CoreMatchers.allOf
import org.hamcrest.CoreMatchers.containsString
import org.junit.After
import org.junit.Test
import org.totschnig.myexpenses.activity.BudgetActivity
import org.totschnig.myexpenses.compose.*
import org.totschnig.myexpenses.db2.deleteAccount
import org.totschnig.myexpenses.db2.deleteCategory
import org.totschnig.myexpenses.model.Grouping
import org.totschnig.myexpenses.model.Money
import org.totschnig.myexpenses.model.Transaction
Expand All @@ -28,19 +31,25 @@ import org.totschnig.myexpenses.provider.filter.CategoryCriterion
import org.totschnig.myexpenses.provider.filter.FilterPersistence
import org.totschnig.myexpenses.test.R
import org.totschnig.myexpenses.testutils.BaseComposeTest
import org.totschnig.myexpenses.testutils.cleanup
import org.totschnig.myexpenses.viewmodel.BudgetViewModel
import timber.log.Timber
import java.time.LocalDate

class BudgetActivityTest : BaseComposeTest<BudgetActivity>() {

lateinit var account: Account
var catIgnore: Long = 0
var mainCat1: Long = 0
var mainCat2: Long = 0

fun setup(withFilter: Boolean) {
val account = buildAccount("Account")
account = buildAccount("Account")
//we write first a category to force category's id to be different from account id
//in order to verify fix for https://github.com/mtotschnig/MyExpenses/issues/1189
writeCategory("ignore")
val mainCat1 = writeCategory("A", null)
val mainCat2 = writeCategory("B", null)
catIgnore = writeCategory("ignore")
mainCat1 = writeCategory("A", null)
mainCat2 = writeCategory("B", null)
val op0 = Transaction.getNewInstance(account.id, homeCurrency)
op0.amount = Money(homeCurrency, -12300L)
op0.catId = mainCat1
Expand All @@ -65,6 +74,16 @@ class BudgetActivityTest : BaseComposeTest<BudgetActivity>() {
})
}

@After
fun clearDb() {
cleanup {
repository.deleteAccount(account.id)
repository.deleteCategory(catIgnore)
repository.deleteCategory(mainCat1)
repository.deleteCategory(mainCat2)
}
}

private fun createBudget(account: Account) = ContentUris.parseId(
repository.contentResolver.insert(
TransactionProvider.BUDGETS_URI,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import org.totschnig.myexpenses.activity.BudgetEdit
import org.totschnig.myexpenses.activity.ManageCategories
import org.totschnig.myexpenses.activity.ManageParties
import org.totschnig.myexpenses.activity.ManageTags
import org.totschnig.myexpenses.db2.deleteAccount
import org.totschnig.myexpenses.db2.deleteCategory
import org.totschnig.myexpenses.model2.Account
import org.totschnig.myexpenses.testutils.BaseUiTest
import org.totschnig.myexpenses.testutils.cleanup
import org.totschnig.myexpenses.testutils.withAccount
import org.totschnig.myexpenses.viewmodel.data.AccountMinimal
import kotlin.reflect.KClass
Expand All @@ -37,17 +41,24 @@ import kotlin.test.Test

class BudgetEditTest : BaseUiTest<BudgetEdit>() {

lateinit var account1: Account
lateinit var account2: Account

@Before
fun fixture() {
buildAccount("Test account 1", 0)
buildAccount("Test account 2", 0)
account1 = buildAccount("Test account 1", 0)
account2 = buildAccount("Test account 2", 0)
testScenario = ActivityScenario.launch(Intent(targetContext, BudgetEdit::class.java))
Intents.init()
}

@After
fun tearDown() {
Intents.release()
cleanup {
repository.deleteAccount(account1.id)
repository.deleteAccount(account2.id)
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.adevinta.android.barista.internal.matcher.HelperMatchers
import com.google.common.truth.Truth.assertThat
import org.hamcrest.CoreMatchers.containsString
import org.hamcrest.Matchers
import org.junit.After
import org.junit.Test
import org.totschnig.myexpenses.R
import org.totschnig.myexpenses.activity.Action
Expand All @@ -37,14 +38,20 @@ import org.totschnig.myexpenses.compose.TEST_TAG_EDIT_TEXT
import org.totschnig.myexpenses.compose.TEST_TAG_POSITIVE_BUTTON
import org.totschnig.myexpenses.contract.TransactionsContract.Transactions
import org.totschnig.myexpenses.db2.FLAG_NEUTRAL
import org.totschnig.myexpenses.db2.deleteAccount
import org.totschnig.myexpenses.db2.deleteBudget
import org.totschnig.myexpenses.db2.deleteCategory
import org.totschnig.myexpenses.db2.deleteTemplate
import org.totschnig.myexpenses.model.CurrencyUnit
import org.totschnig.myexpenses.model.Grouping
import org.totschnig.myexpenses.model.Money
import org.totschnig.myexpenses.model.Template
import org.totschnig.myexpenses.model.Transaction
import org.totschnig.myexpenses.preference.PrefKey
import org.totschnig.myexpenses.provider.DatabaseConstants
import org.totschnig.myexpenses.provider.TransactionProvider
import org.totschnig.myexpenses.testutils.BaseComposeTest
import org.totschnig.myexpenses.testutils.cleanup
import org.totschnig.myexpenses.viewmodel.CategoryViewModel
import org.totschnig.myexpenses.viewmodel.data.Budget
import java.time.LocalDate
Expand All @@ -55,14 +62,24 @@ class CategoriesCabTest : BaseComposeTest<ManageCategories>() {
private lateinit var account: org.totschnig.myexpenses.model2.Account
private var categoryId: Long = 0
private var origListSize = 0
private var controlCategory: Long = 0

private fun baseFixture() {
account = buildAccount("Test account 1")
categoryId = writeCategory(label = "TestCategory")
writeCategory(label = "Control Category")
controlCategory = writeCategory(label = "Control Category")
origListSize = repository.count(TransactionProvider.CATEGORIES_URI)
}

@After
fun clearDb() {
cleanup {
repository.deleteAccount(account.id)
repository.deleteCategory(categoryId)
repository.deleteCategory(controlCategory)
}
}

private fun launch() =
ActivityScenario.launch<ManageCategories>(
Intent(targetContext, ManageCategories::class.java).also {
Expand All @@ -73,18 +90,18 @@ class CategoriesCabTest : BaseComposeTest<ManageCategories>() {
testScenario = it
}

private fun fixtureWithMappedTransaction() {
private fun fixtureWithMappedTransaction(): Long {
baseFixture()
with(Transaction.getNewInstance(account.id, homeCurrency)) {
return with(Transaction.getNewInstance(account.id, homeCurrency)) {
amount = Money(homeCurrency, -1200L)
catId = categoryId
save(contentResolver)
ContentUris.parseId(save(contentResolver)!!)
}
}

private fun fixtureWithMappedTemplate() {
private fun fixtureWithMappedTemplate(): Long {
baseFixture()
with(
return with(
Template(
contentResolver,
account.id,
Expand All @@ -95,11 +112,11 @@ class CategoriesCabTest : BaseComposeTest<ManageCategories>() {
) {
amount = Money(CurrencyUnit(Currency.getInstance("USD")), -1200L)
catId = categoryId
save(contentResolver)
ContentUris.parseId(save(contentResolver)!!)
}
}

private fun fixtureWithMappedBudget() {
private fun fixtureWithMappedBudget(): Long {
baseFixture()
val budget = Budget(
0L,
Expand All @@ -121,6 +138,7 @@ class CategoriesCabTest : BaseComposeTest<ManageCategories>() {
)!!
)
setCategoryBudget(budgetId, categoryId, 50000)
return budgetId
}

private fun setCategoryBudget(
Expand Down Expand Up @@ -157,12 +175,16 @@ class CategoriesCabTest : BaseComposeTest<ManageCategories>() {
assertTextAtPosition("Control Category", 0)
listNode.assert(hasRowCount(1))
}
cleanup {
prefHandler.remove(PrefKey.SORT_ORDER_CATEGORIES)
}
}

@Test
fun shouldNotDeleteCategoryMappedToTransaction() {
fixtureWithMappedTransaction()
val transactionId = fixtureWithMappedTransaction()
launch().use {
assertTextAtPosition("TestCategory", 0)
callDelete()
onView(withId(com.google.android.material.R.id.snackbar_text))
.check(
Expand All @@ -176,11 +198,14 @@ class CategoriesCabTest : BaseComposeTest<ManageCategories>() {
)
assertThat(repository.count(TransactionProvider.CATEGORIES_URI)).isEqualTo(origListSize)
}
cleanup {
repository.deleteTransaction(transactionId)
}
}

@Test
fun shouldNotDeleteCategoryMappedToTemplate() {
fixtureWithMappedTemplate()
val templateId = fixtureWithMappedTemplate()
launch().use {
callDelete()
onView(withId(com.google.android.material.R.id.snackbar_text))
Expand All @@ -195,11 +220,14 @@ class CategoriesCabTest : BaseComposeTest<ManageCategories>() {
)
assertThat(repository.count(TransactionProvider.CATEGORIES_URI)).isEqualTo(origListSize)
}
cleanup {
repository.deleteTemplate(templateId)
}
}

@Test
fun shouldNotDeleteCategoryMappedToBudget() {
fixtureWithMappedBudget()
val budgetId = fixtureWithMappedBudget()
launch().use {
callDelete(false)
onView(withText(containsString(getString(R.string.warning_delete_category_with_budget)))).check(
Expand All @@ -208,6 +236,9 @@ class CategoriesCabTest : BaseComposeTest<ManageCategories>() {
onView(withText(R.string.response_no)).perform(click())
assertThat(repository.count(TransactionProvider.CATEGORIES_URI)).isEqualTo(origListSize)
}
cleanup {
repository.deleteBudget(budgetId)
}
}

private fun callDelete(withConfirmation: Boolean = true, position: Int = 0) {
Expand Down
Loading

0 comments on commit 0da5fd5

Please sign in to comment.