Skip to content

Commit

Permalink
Added test cases for testing this functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
MohitMaliDeveloper authored and MohitMaliFtechiz committed Sep 9, 2024
1 parent c1de637 commit 8f2372b
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ abstract class CoreReaderFragment :
) {
super.onViewCreated(view, savedInstanceState)
setupMenu()
donationDialogHandler?.showDonationDialogCallBack(this)
donationDialogHandler?.setDonationDialogCallBack(this)
val activity = requireActivity() as AppCompatActivity?
activity?.let {
WebView(it).destroy() // Workaround for buggy webViews see #710
Expand Down Expand Up @@ -1224,7 +1224,7 @@ abstract class CoreReaderFragment :
unRegisterReadAloudService()
storagePermissionForNotesLauncher?.unregister()
storagePermissionForNotesLauncher = null
donationDialogHandler?.showDonationDialogCallBack(null)
donationDialogHandler?.setDonationDialogCallBack(null)
donationDialogHandler = null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DonationDialogHandler @Inject constructor(

private var showDonationDialogCallback: ShowDonationDialogCallback? = null

fun showDonationDialogCallBack(showDonationDialogCallback: ShowDonationDialogCallback?) {
fun setDonationDialogCallBack(showDonationDialogCallback: ShowDonationDialogCallback?) {
this.showDonationDialogCallback = showDonationDialogCallback
}

Expand All @@ -61,18 +61,18 @@ class DonationDialogHandler @Inject constructor(
return timeDifference >= THREE_DAYS_IN_MILLISECONDS
}

private fun isZimFilesAvailableInLibrary(): Boolean =
fun isZimFilesAvailableInLibrary(): Boolean =
if (activity.isCustomApp()) true else newBookDao.getBooks().isNotEmpty()

fun updateLastDonationPopupShownTime() {
sharedPreferenceUtil.lastDonationPopupShownInMilliSeconds = System.currentTimeMillis()
}

Check warning on line 69 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/DonationDialogHandler.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/DonationDialogHandler.kt#L68-L69

Added lines #L68 - L69 were not covered by tests

fun donateLater() {
sharedPreferenceUtil.laterClickedMilliSeconds = System.currentTimeMillis()
fun donateLater(currentMillis: Long = System.currentTimeMillis()) {
sharedPreferenceUtil.laterClickedMilliSeconds = currentMillis
}

Check warning on line 73 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/DonationDialogHandler.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/utils/DonationDialogHandler.kt#L71-L73

Added lines #L71 - L73 were not covered by tests

private fun resetDonateLater() {
fun resetDonateLater() {
sharedPreferenceUtil.laterClickedMilliSeconds = 0L
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Kiwix Android
* Copyright (c) 2024 Kiwix <android.kiwix.org>
* This program 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.
*
* This program 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.kiwix.kiwixmobile.core.utils

import android.app.Activity
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.kiwix.kiwixmobile.core.dao.NewBookDao
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions

class DonationDialogHandlerTest {
private lateinit var activity: Activity
private lateinit var sharedPreferenceUtil: SharedPreferenceUtil
private lateinit var newBookDao: NewBookDao
private lateinit var donationDialogHandler: DonationDialogHandler
private lateinit var showDonationDialogCallback: DonationDialogHandler.ShowDonationDialogCallback

@BeforeEach
fun setup() {
activity = mockk(relaxed = true)
sharedPreferenceUtil = mockk(relaxed = true)
newBookDao = mockk(relaxed = true)
showDonationDialogCallback = mockk(relaxed = true)
donationDialogHandler = DonationDialogHandler(activity, sharedPreferenceUtil, newBookDao)
donationDialogHandler.setDonationDialogCallBack(showDonationDialogCallback)
}

@Test
fun `test should show initial popup`() {
every { sharedPreferenceUtil.lastDonationPopupShownInMilliSeconds } returns 0L
every { newBookDao.getBooks() } returns listOf(mockk())
donationDialogHandler.attemptToShowDonationPopup()
verify { showDonationDialogCallback.showDonationDialog() }
}

@Test
fun `test should not show popup when time difference is less than 3 months`() {
val currentMilliSeconds = System.currentTimeMillis()
every {
sharedPreferenceUtil.lastDonationPopupShownInMilliSeconds
} returns currentMilliSeconds - (THREE_MONTHS_IN_MILLISECONDS / 2)
every { newBookDao.getBooks() } returns listOf(mockk())
donationDialogHandler.attemptToShowDonationPopup()
verify(exactly = 0) { showDonationDialogCallback.showDonationDialog() }
}

@Test
fun `test should show popup when time difference is more than 3 months`() {
val currentMilliSeconds = System.currentTimeMillis()
every {
sharedPreferenceUtil.lastDonationPopupShownInMilliSeconds
} returns currentMilliSeconds - (THREE_MONTHS_IN_MILLISECONDS + 1000)
every { newBookDao.getBooks() } returns listOf(mockk())
donationDialogHandler.attemptToShowDonationPopup()
verify { showDonationDialogCallback.showDonationDialog() }
}

@Test
fun `test donate later saves the correct time`() {
val currentMilliSeconds = System.currentTimeMillis()
every { sharedPreferenceUtil.laterClickedMilliSeconds = any() } just Runs
donationDialogHandler.donateLater(currentMilliSeconds)
verify { sharedPreferenceUtil.laterClickedMilliSeconds = currentMilliSeconds }
}

@Test
fun `test reset donate later sets value to zero`() {
every { sharedPreferenceUtil.laterClickedMilliSeconds = 0L } just Runs
donationDialogHandler.resetDonateLater()
verify { sharedPreferenceUtil.laterClickedMilliSeconds = 0L }
}

@Test
fun `test isZimFilesAvailableInLibrary returns true when isCustomApp is true`() {
with(mockk<ActivityExtensions>()) {
every { activity.packageName } returns "org.kiwix.kiwixcustom"
every { activity.isCustomApp() } returns true
val result = donationDialogHandler.isZimFilesAvailableInLibrary()
assertTrue(result)
}
}

@Test
fun `test isZimFilesAvailableInLibrary returns false when no books and isCustomApp is false`() {
with(mockk<ActivityExtensions>()) {
every { activity.packageName } returns "org.kiwix.kiwixmobile"
every { activity.isCustomApp() } returns false
every { newBookDao.getBooks() } returns emptyList()
val result = donationDialogHandler.isZimFilesAvailableInLibrary()
assertFalse(result)
}
}

@Test
fun `isZimFilesAvailableInLibrary returns true when books available and isCustomApp is false`() {
with(mockk<ActivityExtensions>()) {
every { activity.packageName } returns "org.kiwix.kiwixmobile"
every { activity.isCustomApp() } returns false
every { newBookDao.getBooks() } returns listOf(mockk())
val result = donationDialogHandler.isZimFilesAvailableInLibrary()
assertTrue(result)
}
}
}

0 comments on commit 8f2372b

Please sign in to comment.