Rest of unit tests in uhabits-core

pull/2020/head
Dharanish 1 year ago
parent e65ed032a9
commit bad674dd36

@ -1,37 +0,0 @@
{
"version": 3,
"artifactType": {
"type": "APK",
"kind": "Directory"
},
"applicationId": "org.isoron.uhabits",
"variantName": "release",
"elements": [
{
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 20202,
"versionName": "2.2.2",
"outputFile": "uhabits-android-release.apk"
}
],
"elementType": "File",
"baselineProfiles": [
{
"minApi": 28,
"maxApi": 30,
"baselineProfiles": [
"baselineProfiles/1/uhabits-android-release.dm"
]
},
{
"minApi": 31,
"maxApi": 2147483647,
"baselineProfiles": [
"baselineProfiles/0/uhabits-android-release.dm"
]
}
],
"minSdkVersionForDexing": 28
}

@ -18,6 +18,7 @@
*/
package org.isoron.uhabits.core.test
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.HabitGroup
import org.isoron.uhabits.core.models.HabitGroupList
import org.isoron.uhabits.core.models.HabitList
@ -32,10 +33,12 @@ class HabitGroupFixtures(private val modelFactory: ModelFactory, private val hab
fun createEmptyHabitGroup(
name: String = "Exercise",
color: PaletteColor = PaletteColor(3),
position: Int = 0
position: Int = 0,
id: Long = 0L
): HabitGroup {
val hgr = modelFactory.buildHabitGroup()
hgr.name = name
hgr.id = id
hgr.question = "Did you exercise today?"
hgr.color = color
hgr.position = position
@ -47,11 +50,15 @@ class HabitGroupFixtures(private val modelFactory: ModelFactory, private val hab
name: String = "Exercise",
color: PaletteColor = PaletteColor(3),
position: Int = 0,
numHabits: Int = 1
numHabits: Int = 1,
id: Long = 0L
): HabitGroup {
val hgr = createEmptyHabitGroup(name, color, position)
val hgr = createEmptyHabitGroup(name, color, position, id)
for (i in 1..numHabits) {
hgr.habitList.add(habitFixtures.createEmptyHabit())
val h = habitFixtures.createEmptyHabit()
h.id = id + i
addGroupId(h, hgr)
hgr.habitList.add(h)
}
saveIfSQLite(hgr)
return hgr
@ -62,11 +69,15 @@ class HabitGroupFixtures(private val modelFactory: ModelFactory, private val hab
color: PaletteColor = PaletteColor(3),
position: Int = 0,
targetType: NumericalHabitType = NumericalHabitType.AT_LEAST,
numHabits: Int = 1
numHabits: Int = 1,
id: Long = 0L
): HabitGroup {
val hgr = createEmptyHabitGroup(name, color, position)
val hgr = createEmptyHabitGroup(name, color, position, id)
for (i in 1..numHabits) {
hgr.habitList.add(habitFixtures.createEmptyNumericalHabit(targetType))
val h = habitFixtures.createEmptyNumericalHabit(targetType)
h.id = id + i
addGroupId(h, hgr)
hgr.habitList.add(h)
}
saveIfSQLite(hgr)
return hgr
@ -76,11 +87,15 @@ class HabitGroupFixtures(private val modelFactory: ModelFactory, private val hab
name: String = "Exercise",
color: PaletteColor = PaletteColor(3),
position: Int = 0,
numHabits: Int = 1
numHabits: Int = 1,
id: Long = 0L
): HabitGroup {
val hgr = createEmptyHabitGroup(name, color, position)
val hgr = createEmptyHabitGroup(name, color, position, id)
for (i in 1..numHabits) {
hgr.habitList.add(habitFixtures.createNumericalHabit())
val h = habitFixtures.createNumericalHabit()
h.id = id + i
addGroupId(h, hgr)
hgr.habitList.add(h)
}
saveIfSQLite(hgr)
return hgr
@ -90,11 +105,15 @@ class HabitGroupFixtures(private val modelFactory: ModelFactory, private val hab
name: String = "Exercise",
color: PaletteColor = PaletteColor(3),
position: Int = 0,
numHabits: Int = 1
numHabits: Int = 1,
id: Long = 0L
): HabitGroup {
val hgr = createEmptyHabitGroup(name, color, position)
val hgr = createEmptyHabitGroup(name, color, position, id)
for (i in 1..numHabits) {
hgr.habitList.add(habitFixtures.createLongHabit())
val h = habitFixtures.createLongHabit()
h.id = id + i
addGroupId(h, hgr)
hgr.habitList.add(h)
}
saveIfSQLite(hgr)
return hgr
@ -104,11 +123,15 @@ class HabitGroupFixtures(private val modelFactory: ModelFactory, private val hab
name: String = "Exercise",
color: PaletteColor = PaletteColor(3),
position: Int = 0,
numHabits: Int = 1
numHabits: Int = 1,
id: Long = 0L
): HabitGroup {
val hgr = createEmptyHabitGroup(name, color, position)
val hgr = createEmptyHabitGroup(name, color, position, id)
for (i in 1..numHabits) {
hgr.habitList.add(habitFixtures.createShortHabit())
val h = habitFixtures.createShortHabit()
h.id = id + i
addGroupId(h, hgr)
hgr.habitList.add(h)
}
saveIfSQLite(hgr)
return hgr
@ -118,4 +141,10 @@ class HabitGroupFixtures(private val modelFactory: ModelFactory, private val hab
if (hgr.habitList !is SQLiteHabitList) return
habitGroupList.add(hgr)
}
private fun addGroupId(h: Habit, hgr: HabitGroup) {
h.groupId = hgr.id
h.group = hgr
h.groupUUID = hgr.uuid
}
}

@ -481,7 +481,7 @@ class HabitCardListCache @Inject constructor(
if (type == HABIT_GROUP) return
// Workaround for https://github.com/iSoron/uhabits/issues/968
val checkedToPosition = if (toPosition > positionTypes.size) {
val checkedToPosition = if (toPosition >= positionTypes.size) {
logger.error("performMove: $toPosition for habit is strictly higher than ${habits.size}")
positionTypes.size - 1
} else {
@ -684,14 +684,14 @@ class HabitCardListCache @Inject constructor(
val id = habitGroup.id
val prevIdx = newData.positionIndices[position]
val habitList = newData.subHabits[prevIdx]
val idx = if (data.positionIndices.size > position) {
val idx = if (position < data.positionIndices.size) {
data.positionIndices[position]
} else {
data.habitGroups.size
}
data.habitGroups.add(idx, habitGroup)
data.subHabits.add(prevIdx, habitList)
data.subHabits.add(idx, habitList)
data.scores[id] = newData.scores[id]!!
for (h in habitList) {
data.scores[h.id] = newData.scores[h.id]!!

@ -27,6 +27,7 @@ import org.isoron.uhabits.core.commands.DeleteHabitGroupsCommand
import org.isoron.uhabits.core.commands.DeleteHabitsCommand
import org.isoron.uhabits.core.commands.RefreshParentGroupCommand
import org.isoron.uhabits.core.commands.RemoveFromGroupCommand
import org.isoron.uhabits.core.commands.UnarchiveHabitGroupsCommand
import org.isoron.uhabits.core.commands.UnarchiveHabitsCommand
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.HabitGroup
@ -134,6 +135,7 @@ class ListHabitsSelectionMenuBehavior @Inject constructor(
fun onUnarchiveHabits() {
commandRunner.run(UnarchiveHabitsCommand(habitList, adapter.getSelectedHabits()))
commandRunner.run(UnarchiveHabitGroupsCommand(habitGroupList, adapter.getSelectedHabitGroups()))
for (habit in adapter.getSelectedHabits()) {
commandRunner.run(RefreshParentGroupCommand(habit, habitGroupList))
}

@ -43,7 +43,7 @@ create table Habits (
uuid text,
group_id integer,
group_uuid text,
foreign key(group_id)
foreign key (group_id)
references HabitGroups(id)
on update cascade
on delete cascade

@ -0,0 +1,47 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.commands
import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.HabitGroup
import org.junit.Before
import org.junit.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class ArchiveHabitGroupsCommandTest : BaseUnitTest() {
private lateinit var command: ArchiveHabitGroupsCommand
private lateinit var hgr: HabitGroup
@Before
@Throws(Exception::class)
override fun setUp() {
super.setUp()
hgr = groupFixtures.createGroupWithShortHabits()
habitGroupList.add(hgr)
command = ArchiveHabitGroupsCommand(habitGroupList, listOf(hgr))
}
@Test
fun testExecute() {
assertFalse(hgr.isArchived)
command.run()
assertTrue(hgr.isArchived)
}
}

@ -0,0 +1,66 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.commands
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.HabitGroup
import org.isoron.uhabits.core.models.PaletteColor
import org.junit.Before
import org.junit.Test
import java.util.LinkedList
class ChangeHabitGroupColorCommandTest : BaseUnitTest() {
private lateinit var command: ChangeHabitGroupColorCommand
private lateinit var selected: LinkedList<HabitGroup>
@Before
@Throws(Exception::class)
override fun setUp() {
super.setUp()
selected = LinkedList()
for (i in 0..2) {
val hgr = groupFixtures.createGroupWithShortHabits()
hgr.color = PaletteColor(i + 1)
selected.add(hgr)
habitGroupList.add(hgr)
}
command = ChangeHabitGroupColorCommand(habitGroupList, selected, PaletteColor(0))
}
@Test
fun testExecute() {
checkOriginalColors()
command.run()
checkNewColors()
}
private fun checkNewColors() {
for (hgr in selected) {
assertThat(hgr.color, equalTo(PaletteColor(0)))
}
}
private fun checkOriginalColors() {
var k = 0
for (hgr in selected)
assertThat(hgr.color, equalTo(PaletteColor(++k)))
}
}

@ -0,0 +1,53 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.commands
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.HabitGroup
import org.isoron.uhabits.core.models.Reminder
import org.isoron.uhabits.core.models.WeekdayList
import org.junit.Before
import org.junit.Test
import kotlin.test.assertTrue
class CreateHabitGroupCommandTest : BaseUnitTest() {
private lateinit var command: CreateHabitGroupCommand
private lateinit var model: HabitGroup
@Before
@Throws(Exception::class)
override fun setUp() {
super.setUp()
model = groupFixtures.createEmptyHabitGroup()
model.name = "New habit group"
model.reminder = Reminder(8, 30, WeekdayList.EVERY_DAY)
command = CreateHabitGroupCommand(modelFactory, habitGroupList, model)
}
@Test
fun testExecute() {
assertTrue(habitGroupList.isEmpty)
command.run()
assertThat(habitGroupList.size(), equalTo(1))
val hgr = habitGroupList.getByPosition(0)
assertThat(hgr.name, equalTo(model.name))
}
}

@ -0,0 +1,60 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.commands
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.HabitGroup
import org.junit.Before
import org.junit.Test
import java.util.*
class DeleteHabitGroupsCommandTest : BaseUnitTest() {
private lateinit var command: DeleteHabitGroupsCommand
private lateinit var selected: LinkedList<HabitGroup>
@Before
@Throws(Exception::class)
override fun setUp() {
super.setUp()
selected = LinkedList()
// Habits that should be deleted
for (i in 0..2) {
val hgr = groupFixtures.createGroupWithShortHabits()
habitGroupList.add(hgr)
selected.add(hgr)
}
// Extra habit that should not be deleted
val extraHgr = groupFixtures.createGroupWithShortHabits()
extraHgr.name = "extra"
habitGroupList.add(extraHgr)
command = DeleteHabitGroupsCommand(habitGroupList, selected)
}
@Test
fun testExecute() {
assertThat(habitGroupList.size(), equalTo(4))
command.run()
assertThat(habitGroupList.size(), equalTo(1))
assertThat(habitGroupList.getByPosition(0).name, equalTo("extra"))
}
}

@ -0,0 +1,60 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.commands
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.HabitGroup
import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.core.utils.DateUtils.Companion.getTodayWithOffset
import org.junit.Before
import org.junit.Test
class EditHabitGroupCommandTest : BaseUnitTest() {
private lateinit var command: EditHabitGroupCommand
private lateinit var hgr: HabitGroup
private lateinit var modified: HabitGroup
private lateinit var today: Timestamp
@Before
@Throws(Exception::class)
override fun setUp() {
super.setUp()
hgr = groupFixtures.createGroupWithShortHabits()
hgr.name = "original"
hgr.recompute()
habitGroupList.add(hgr)
modified = groupFixtures.createEmptyHabitGroup()
modified.copyFrom(hgr)
modified.name = "modified"
habitGroupList.add(modified)
today = getTodayWithOffset()
}
@Test
fun testExecute() {
command = EditHabitGroupCommand(habitGroupList, hgr.id!!, modified)
val originalScore = hgr.scores[today].value
assertThat(hgr.name, equalTo("original"))
command.run()
assertThat(hgr.name, equalTo("modified"))
assertThat(hgr.scores[today].value, equalTo(originalScore))
}
}

@ -0,0 +1,48 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.commands
import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.HabitGroup
import org.junit.Before
import org.junit.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class UnarchiveHabitGroupsCommandTest : BaseUnitTest() {
private lateinit var command: UnarchiveHabitGroupsCommand
private lateinit var hgr: HabitGroup
@Before
@Throws(Exception::class)
override fun setUp() {
super.setUp()
hgr = groupFixtures.createGroupWithShortHabits()
hgr.isArchived = true
habitGroupList.add(hgr)
command = UnarchiveHabitGroupsCommand(habitGroupList, listOf(hgr))
}
@Test
fun testExecuteUndoRedo() {
assertTrue(hgr.isArchived)
command.run()
assertFalse(hgr.isArchived)
}
}

@ -31,6 +31,8 @@ import org.isoron.uhabits.core.models.ModelObservable
import org.isoron.uhabits.core.models.Reminder
import org.isoron.uhabits.core.models.WeekdayList
import org.isoron.uhabits.core.models.sqlite.records.HabitGroupRecord
import org.isoron.uhabits.core.models.sqlite.records.HabitRecord
import org.isoron.uhabits.core.test.HabitFixtures
import org.isoron.uhabits.core.test.HabitGroupFixtures
import org.junit.Assert.assertThrows
import org.junit.Test
@ -40,6 +42,7 @@ import kotlin.test.assertNull
class SQLiteHabitGroupListTest : BaseUnitTest() {
private lateinit var repository: Repository<HabitGroupRecord>
private lateinit var habitRepository: Repository<HabitRecord>
private var listener: ModelObservable.Listener = mock()
private lateinit var habitGroupsArray: ArrayList<HabitGroup>
private lateinit var activeHabitGroups: HabitGroupList
@ -52,7 +55,9 @@ class SQLiteHabitGroupListTest : BaseUnitTest() {
modelFactory = SQLModelFactory(db)
habitGroupList = SQLiteHabitGroupList(modelFactory)
groupFixtures = HabitGroupFixtures(modelFactory, habitList, habitGroupList)
fixtures = HabitFixtures(modelFactory, habitList)
repository = Repository(HabitGroupRecord::class.java, db)
habitRepository = Repository(HabitRecord::class.java, db)
habitGroupsArray = ArrayList()
for (i in 0..9) {
val hgr = groupFixtures.createEmptyHabitGroup()
@ -185,4 +190,26 @@ class SQLiteHabitGroupListTest : BaseUnitTest() {
val record4 = repository.find(4L)!!
assertThat(record4.position, equalTo(2))
}
@Test
fun testAdd_Habit() {
val habit = fixtures.createEmptyHabit()
val hgr = habitGroupList.getById(1)!!
hgr.habitList.add(habit)
assertThat(habit.id!!, equalTo(11L))
val record = habitRepository.find(11L)
assertThat(record!!.name, equalTo(habit.name))
}
@Test
fun testRemoveHabit() {
val habit = fixtures.createEmptyHabit()
val hgr = habitGroupList.getById(1)!!
hgr.habitList.add(habit)
hgr.habitList.remove(habit)
assertThat(hgr.habitList.indexOf(habit), equalTo(-1))
val record = habitRepository.find(11L)
assertNull(record)
}
}

@ -48,7 +48,6 @@ class SQLiteHabitListTest : BaseUnitTest() {
override fun setUp() {
super.setUp()
val db: Database = buildMemoryDatabase()
db.execute("PRAGMA foreign_keys = OFF;")
modelFactory = SQLModelFactory(db)
habitList = SQLiteHabitList(modelFactory)
fixtures = HabitFixtures(modelFactory, habitList)

@ -22,30 +22,46 @@ import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.commands.CreateRepetitionCommand
import org.isoron.uhabits.core.commands.DeleteHabitGroupsCommand
import org.isoron.uhabits.core.commands.DeleteHabitsCommand
import org.isoron.uhabits.core.models.Entry
import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.core.utils.DateUtils.Companion.getToday
import org.junit.Test
import org.mockito.kotlin.mock
import org.mockito.kotlin.reset
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoMoreInteractions
class HabitCardListCacheTest : BaseUnitTest() {
private lateinit var cache: HabitCardListCache
private lateinit var listener: HabitCardListCache.Listener
var today = getToday()
lateinit var today: Timestamp
@Throws(Exception::class)
override fun setUp() {
super.setUp()
today = getToday()
habitList.removeAll()
for (i in 0..9) {
if (i == 3) habitList.add(fixtures.createLongHabit()) else habitList.add(fixtures.createShortHabit())
}
habitGroupList.removeAll()
for (i in 0..2) {
habitGroupList.add(groupFixtures.createEmptyHabitGroup(id = 10L + i.toLong()))
}
cache = HabitCardListCache(habitList, habitGroupList, commandRunner, taskRunner, mock())
cache.setCheckmarkCount(10)
cache.refreshAllHabits()
val hgr = habitGroupList.getByPosition(2)
val h = fixtures.createShortHabit()
hgr.habitList.add(h)
h.id = 13L
h.groupId = hgr.id
h.group = hgr
h.groupUUID = hgr.uuid
cache.refreshAllHabits()
cache.onAttached()
listener = mock()
cache.setListener(listener)
@ -56,7 +72,7 @@ class HabitCardListCacheTest : BaseUnitTest() {
}
@Test
fun testCommandListener_all() {
fun testCommandListener_delete_habit() {
assertThat(cache.habitCount, equalTo(10))
val h = habitList.getByPosition(0)
commandRunner.run(
@ -67,6 +83,30 @@ class HabitCardListCacheTest : BaseUnitTest() {
assertThat(cache.habitCount, equalTo(9))
}
@Test
fun testCommandListener_delete_hgr() {
assertThat(cache.habitGroupCount, equalTo(3))
assertThat(cache.subHabitCount, equalTo(1))
val hgr = habitGroupList.getByPosition(0)
commandRunner.run(
DeleteHabitGroupsCommand(habitGroupList, listOf(hgr))
)
verify(listener).onItemRemoved(10)
verify(listener).onRefreshFinished()
assertThat(cache.habitGroupCount, equalTo(2))
assertThat(cache.subHabitCount, equalTo(1))
val hgr2 = habitGroupList.getByPosition(1)
commandRunner.run(
DeleteHabitGroupsCommand(habitGroupList, listOf(hgr2))
)
verify(listener).onItemRemoved(11)
verify(listener).onItemRemoved(12)
verify(listener, times(2)).onRefreshFinished()
assertThat(cache.habitGroupCount, equalTo(1))
assertThat(cache.subHabitCount, equalTo(0))
}
@Test
fun testCommandListener_single() {
val h2 = habitList.getByPosition(2)
@ -76,6 +116,16 @@ class HabitCardListCacheTest : BaseUnitTest() {
verifyNoMoreInteractions(listener)
}
@Test
fun testCommandListener_single_sub_habit() {
val hgr2 = habitGroupList.getByPosition(2)
val h2 = hgr2.habitList.getByPosition(0)
commandRunner.run(CreateRepetitionCommand(hgr2.habitList, h2, today, Entry.NO, ""))
verify(listener).onItemChanged(13)
verify(listener).onRefreshFinished()
verifyNoMoreInteractions(listener)
}
@Test
fun testGet() {
assertThat(cache.habitCount, equalTo(10))
@ -92,15 +142,39 @@ class HabitCardListCacheTest : BaseUnitTest() {
assertThat(actualCheckmarks, equalTo(expectedCheckmarks))
}
@Test
fun testGetGroup() {
assertThat(cache.habitGroupCount, equalTo(3))
val hgr = habitGroupList.getByPosition(2)
val score = hgr.scores[today].value
assertThat(cache.getHabitGroupByPosition(12), equalTo(hgr))
assertThat(cache.getScore(hgr.id!!), equalTo(score))
val h = hgr.habitList.getByPosition(0)
val score2 = h.scores[today].value
assertThat(cache.getHabitByPosition(13), equalTo(h))
assertThat(cache.getScore(h.id!!), equalTo(score2))
val actualCheckmarks = cache.getCheckmarks(h.id!!)
val expectedCheckmarks = h
.computedEntries
.getByInterval(today.minus(9), today)
.map { it.value }.toIntArray()
assertThat(actualCheckmarks, equalTo(expectedCheckmarks))
}
@Test
fun testRemoval() {
removeHabitAt(0)
removeHabitAt(3)
removeHabitGroupAt(0)
cache.refreshAllHabits()
verify(listener).onItemRemoved(0)
verify(listener).onItemRemoved(3)
verify(listener).onItemRemoved(8)
verify(listener).onRefreshFinished()
assertThat(cache.habitCount, equalTo(8))
assertThat(cache.habitGroupCount, equalTo(2))
}
@Test
@ -123,6 +197,21 @@ class HabitCardListCacheTest : BaseUnitTest() {
verifyNoMoreInteractions(listener)
}
@Test
fun testReorder_onCache_Groups() {
val hgr10 = cache.getHabitGroupByPosition(10)
val hgr11 = cache.getHabitGroupByPosition(11)
val hgr12 = cache.getHabitGroupByPosition(12)
val h13 = cache.getHabitByPosition(13)
cache.reorder(10, 12)
assertThat(cache.getHabitGroupByPosition(10), equalTo(hgr11))
assertThat(cache.getHabitGroupByPosition(11), equalTo(hgr12))
assertThat(cache.getHabitGroupByPosition(13), equalTo(hgr10))
assertThat(cache.getHabitByPosition(12), equalTo(h13))
verify(listener).onItemMoved(10, 12)
verifyNoMoreInteractions(listener)
}
@Test
fun testReorder_onList() {
val h2 = habitList.getByPosition(2)
@ -145,8 +234,34 @@ class HabitCardListCacheTest : BaseUnitTest() {
verifyNoMoreInteractions(listener)
}
@Test
fun testReorder_onList_Groups() {
val hgr10 = habitGroupList.getByPosition(0)
val hgr11 = habitGroupList.getByPosition(1)
val hgr12 = habitGroupList.getByPosition(2)
val h13 = hgr12.habitList.getByPosition(0)
assertThat(cache.getHabitGroupByPosition(10), equalTo(hgr10))
assertThat(cache.getHabitGroupByPosition(12), equalTo(hgr12))
reset(listener)
habitGroupList.reorder(hgr10, hgr12)
cache.refreshAllHabits()
assertThat(cache.getHabitGroupByPosition(10), equalTo(hgr11))
assertThat(cache.getHabitGroupByPosition(11), equalTo(hgr12))
assertThat(cache.getHabitByPosition(12), equalTo(h13))
assertThat(cache.getHabitGroupByPosition(13), equalTo(hgr10))
verify(listener).onItemMoved(11, 10)
verify(listener).onItemMoved(12, 11)
verify(listener).onRefreshFinished()
verifyNoMoreInteractions(listener)
}
private fun removeHabitAt(position: Int) {
val h = habitList.getByPosition(position)
habitList.remove(h)
}
private fun removeHabitGroupAt(position: Int) {
val hgr = habitGroupList.getByPosition(position)
habitGroupList.remove(hgr)
}
}

@ -24,6 +24,8 @@ import org.hamcrest.core.IsEqual.equalTo
import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.Entry
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.HabitGroup
import org.isoron.uhabits.core.models.PaletteColor
import org.isoron.uhabits.core.preferences.Preferences
import org.isoron.uhabits.core.utils.DateUtils.Companion.getToday
import org.isoron.uhabits.core.utils.DateUtils.Companion.getTodayWithOffset
@ -51,8 +53,11 @@ class ListHabitsBehaviorTest : BaseUnitTest() {
private val screen: ListHabitsBehavior.Screen = mock()
private lateinit var habit1: Habit
private lateinit var habit2: Habit
private lateinit var hgr1: HabitGroup
private lateinit var hgr2: HabitGroup
var picker: KArgumentCaptor<ListHabitsBehavior.NumberPickerCallback> = argumentCaptor()
var picker2: KArgumentCaptor<ListHabitsBehavior.CheckMarkDialogCallback> = argumentCaptor()
private val bugReporter: ListHabitsBehavior.BugReporter = mock()
@ -62,9 +67,14 @@ class ListHabitsBehaviorTest : BaseUnitTest() {
super.setUp()
habit1 = fixtures.createShortHabit()
habit2 = fixtures.createNumericalHabit()
hgr1 = groupFixtures.createGroupWithShortHabits(id = 2L)
hgr2 = groupFixtures.createGroupWithNumericalHabits(id = 4L)
habitList.add(habit1)
habitList.add(habit2)
habitGroupList.add(hgr1)
habitGroupList.add(hgr2)
clearInvocations(habitList)
clearInvocations(habitGroupList)
behavior = ListHabitsBehavior(
habitList,
habitGroupList,
@ -90,6 +100,20 @@ class ListHabitsBehaviorTest : BaseUnitTest() {
assertThat(habit2.computedEntries.get(today).value, equalTo(100000))
}
@Test
fun testOnEditBoolean() {
behavior.onEdit(habit1, getToday())
verify(screen).showCheckmarkPopup(
eq(2),
eq(""),
eq(PaletteColor(8)),
picker2.capture()
)
picker2.lastValue.onNotesSaved(2, "", 0f, 0f)
val today = getTodayWithOffset()
assertThat(habit1.computedEntries.get(today).value, equalTo(2))
}
@Test
@Throws(Exception::class)
fun testOnExportCSV() {
@ -118,6 +142,12 @@ class ListHabitsBehaviorTest : BaseUnitTest() {
verify(screen).showHabitScreen(habit1)
}
@Test
fun testOnHabitGroupClick() {
behavior.onClickHabitGroup(hgr1)
verify(screen).showHabitGroupScreen(hgr1)
}
@Test
fun testOnHabitReorder() {
val from = habit1
@ -126,6 +156,14 @@ class ListHabitsBehaviorTest : BaseUnitTest() {
verify(habitList).reorder(from, to)
}
@Test
fun testOnHabitGroupReorder() {
val from = hgr1
val to = hgr2
behavior.onReorderHabitGroup(from, to)
verify(habitGroupList).reorder(from, to)
}
@Test
fun testOnRepairDB() {
behavior.onRepairDB()

@ -176,4 +176,10 @@ class ListHabitsMenuBehaviorTest : BaseUnitTest() {
verify(themeSwitcher).toggleNightMode()
verify(screen).applyTheme()
}
@Test
fun testOnCreateSubHabit() {
behavior.onCreateHabit(0L)
verify(screen).showSelectHabitTypeDialog(0L)
}
}

@ -22,6 +22,7 @@ import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.HabitGroup
import org.isoron.uhabits.core.models.PaletteColor
import org.isoron.uhabits.core.ui.callbacks.OnColorPickedCallback
import org.isoron.uhabits.core.ui.callbacks.OnConfirmedCallback
@ -45,6 +46,10 @@ class ListHabitsSelectionMenuBehaviorTest : BaseUnitTest() {
private lateinit var habit2: Habit
private lateinit var habit3: Habit
private lateinit var hgr1: HabitGroup
private lateinit var hgr2: HabitGroup
private lateinit var hgr3: HabitGroup
private val colorPickerCallback: KArgumentCaptor<OnColorPickedCallback> = argumentCaptor()
private val deleteCallback: KArgumentCaptor<OnConfirmedCallback> = argumentCaptor()
@ -53,8 +58,10 @@ class ListHabitsSelectionMenuBehaviorTest : BaseUnitTest() {
@Throws(Exception::class)
fun canArchive() {
whenever(adapter.getSelectedHabits()).thenReturn(listOf(habit1, habit2))
whenever(adapter.getSelectedHabitGroups()).thenReturn(listOf(hgr1, hgr3))
assertFalse(behavior.canArchive())
whenever(adapter.getSelectedHabits()).thenReturn(listOf(habit2, habit3))
whenever(adapter.getSelectedHabitGroups()).thenReturn(listOf(hgr1, hgr2))
assertTrue(behavior.canArchive())
}
@ -67,12 +74,23 @@ class ListHabitsSelectionMenuBehaviorTest : BaseUnitTest() {
assertFalse(behavior.canEdit())
}
@Test
@Throws(Exception::class)
fun canEditGroups() {
whenever(adapter.getSelectedHabitGroups()).thenReturn(listOf(hgr1))
assertTrue(behavior.canEdit())
whenever(adapter.getSelectedHabitGroups()).thenReturn(listOf(hgr1, hgr2))
assertFalse(behavior.canEdit())
}
@Test
@Throws(Exception::class)
fun canUnarchive() {
whenever(adapter.getSelectedHabits()).thenReturn(listOf(habit1, habit2))
whenever(adapter.getSelectedHabitGroups()).thenReturn(listOf(hgr1, hgr2))
assertFalse(behavior.canUnarchive())
whenever(adapter.getSelectedHabits()).thenReturn(listOf(habit1))
whenever(adapter.getSelectedHabitGroups()).thenReturn(listOf(hgr3))
assertTrue(behavior.canUnarchive())
}
@ -85,17 +103,30 @@ class ListHabitsSelectionMenuBehaviorTest : BaseUnitTest() {
assertTrue(habit2.isArchived)
}
@Test
@Throws(Exception::class)
fun onArchiveHabitGroups() {
assertFalse(hgr2.isArchived)
whenever(adapter.getSelectedHabitGroups()).thenReturn(listOf(hgr2))
behavior.onArchiveHabits()
assertTrue(hgr2.isArchived)
}
@Test
@Throws(Exception::class)
fun onChangeColor() {
assertThat(habit1.color, equalTo(PaletteColor(8)))
assertThat(habit2.color, equalTo(PaletteColor(8)))
assertThat(hgr1.color, equalTo(PaletteColor(3)))
assertThat(hgr2.color, equalTo(PaletteColor(3)))
whenever(adapter.getSelectedHabits()).thenReturn(listOf(habit1, habit2))
whenever(adapter.getSelectedHabitGroups()).thenReturn(listOf(hgr1, hgr2))
behavior.onChangeColor()
verify(screen)
.showColorPicker(eq(PaletteColor(8)), colorPickerCallback.capture())
colorPickerCallback.lastValue.onColorPicked(PaletteColor(30))
assertThat(habit1.color, equalTo(PaletteColor(30)))
assertThat(hgr1.color, equalTo(PaletteColor(30)))
}
@Test
@ -110,6 +141,21 @@ class ListHabitsSelectionMenuBehaviorTest : BaseUnitTest() {
assertNull(habitList.getById(id))
}
@Test
@Throws(Exception::class)
fun onDeleteHabitGroups() {
val id = hgr2.id!!
val hgr = habitGroupList.getById(id)!!
val hId = hgr.habitList.getByPosition(0).id!!
habitGroupList.getHabitByID(hId)!!
whenever(adapter.getSelectedHabitGroups()).thenReturn(listOf(hgr2))
behavior.onDeleteHabits()
verify(screen).showDeleteConfirmationScreen(deleteCallback.capture(), eq(1))
deleteCallback.lastValue.onConfirmed()
assertNull(habitGroupList.getById(id))
assertNull(habitGroupList.getHabitByID(hId))
}
@Test
@Throws(Exception::class)
fun onEditHabits() {
@ -119,13 +165,56 @@ class ListHabitsSelectionMenuBehaviorTest : BaseUnitTest() {
verify(screen).showEditHabitsScreen(selected)
}
@Test
@Throws(Exception::class)
fun onEditHabitGroup() {
val selected: List<HabitGroup> = listOf(hgr1)
whenever(adapter.getSelectedHabitGroups()).thenReturn(selected)
behavior.onEditHabits()
verify(screen).showEditHabitGroupScreen(selected)
}
@Test
@Throws(Exception::class)
fun onUnarchiveHabits() {
assertTrue(habit1.isArchived)
assertTrue(hgr3.isArchived)
whenever(adapter.getSelectedHabits()).thenReturn(listOf(habit1))
whenever(adapter.getSelectedHabitGroups()).thenReturn(listOf(hgr3))
behavior.onUnarchiveHabits()
assertFalse(habit1.isArchived)
assertFalse(hgr3.isArchived)
}
@Test
@Throws(Exception::class)
fun testSelectionType() {
val subHabit = hgr2.habitList.getByPosition(0)
whenever(adapter.getSelectedHabits()).thenReturn(listOf(habit1, habit2, subHabit))
assertTrue(behavior.areHabits())
assertFalse(behavior.areSubHabits())
whenever(adapter.getSelectedHabits()).thenReturn(listOf(subHabit))
assertTrue(behavior.areSubHabits())
}
@Test
@Throws(Exception::class)
fun testAddToGroup() {
assertFalse(habit1.isSubHabit())
val selected = listOf(habit1)
whenever(adapter.getSelectedHabits()).thenReturn(selected)
behavior.onAddToGroup()
verify(screen).showHabitGroupPickerDialog(selected)
}
@Test
@Throws(Exception::class)
fun testRemoveFromGroup() {
val subHabit = hgr2.habitList.getByPosition(0)
assertTrue(subHabit.isSubHabit())
whenever(adapter.getSelectedHabits()).thenReturn(listOf(subHabit))
behavior.onRemoveFromGroup()
assertFalse(subHabit.isSubHabit())
}
@Throws(Exception::class)
@ -138,6 +227,15 @@ class ListHabitsSelectionMenuBehaviorTest : BaseUnitTest() {
habitList.add(habit1)
habitList.add(habit2)
habitList.add(habit3)
hgr1 = groupFixtures.createEmptyHabitGroup(id = 4L)
hgr2 = groupFixtures.createGroupWithShortHabits(id = 5L)
hgr3 = groupFixtures.createGroupWithShortHabits(id = 7L)
hgr3.isArchived = true
habitGroupList.add(hgr1)
habitGroupList.add(hgr2)
habitGroupList.add(hgr3)
behavior = ListHabitsSelectionMenuBehavior(
habitList,
habitGroupList,

@ -0,0 +1,50 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.ui.screens.habits.show
import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.HabitGroup
import org.junit.Test
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
class ShowHabitGroupMenuPresenterTest : BaseUnitTest() {
private lateinit var screen: ShowHabitGroupMenuPresenter.Screen
private lateinit var hgr: HabitGroup
private lateinit var menu: ShowHabitGroupMenuPresenter
@Throws(Exception::class)
override fun setUp() {
super.setUp()
screen = mock()
hgr = groupFixtures.createGroupWithShortHabits()
menu = ShowHabitGroupMenuPresenter(
commandRunner,
hgr,
habitGroupList,
screen
)
}
@Test
fun testOnEditHabit() {
menu.onEditHabitGroup()
verify(screen).showEditHabitGroupScreen(hgr)
}
}
Loading…
Cancel
Save