Skip to content

Commit

Permalink
chore: refactor ChooseAlignmentDialog into com.jkuester package (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuester authored Feb 12, 2025
1 parent 8c1a90d commit ec0e630
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 42 deletions.
13 changes: 9 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,17 @@ kover {
excludes {
packages(
"com.sduduzog.slimlauncher",
"dagger.hilt",
"hilt_aggregated_deps",
"com.jkuester.unlauncher.datastore.proto",
"dagger.hilt.internal.aggregatedroot.codegen",
"hilt_aggregated_deps",
)
annotatedBy(
"javax.annotation.processing.Generated",
"dagger.internal.DaggerGenerated",
)
classes(
"*Hilt_*",
)
annotatedBy("dagger.internal.DaggerGenerated")
annotatedBy("javax.annotations.Generated")
}
}
verify {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jkuester.unlauncher.dialog

import android.app.AlertDialog
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import com.jkuester.unlauncher.datasource.CorePreferencesRepository
import com.jkuester.unlauncher.datasource.setAlignmentFormat
import com.jkuester.unlauncher.datastore.proto.AlignmentFormat
import com.sduduzog.slimlauncher.R
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class AlignmentFormatDialog : DialogFragment() {
@Inject
lateinit var corePreferencesRepo: CorePreferencesRepository

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = AlertDialog
.Builder(context)
.setTitle(R.string.choose_alignment_dialog_title)
.setSingleChoiceItems(
R.array.alignment_format_array,
corePreferencesRepo.get().alignmentFormat.number,
this::selectAlignment
)
.create()

private fun selectAlignment(dialogInterface: DialogInterface, i: Int) = dialogInterface
.dismiss()
.also { corePreferencesRepo.updateAsync(setAlignmentFormat(AlignmentFormat.forNumber(i))) }
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import androidx.core.content.edit
import androidx.navigation.Navigation
import com.jkuester.unlauncher.datasource.CorePreferencesRepository
import com.jkuester.unlauncher.datasource.setKeepDeviceWallpaper
import com.jkuester.unlauncher.dialog.AlignmentFormatDialog
import com.sduduzog.slimlauncher.R
import com.sduduzog.slimlauncher.databinding.OptionsFragmentBinding
import com.sduduzog.slimlauncher.ui.dialogs.ChangeThemeDialog
import com.sduduzog.slimlauncher.ui.dialogs.ChooseAlignmentDialog
import com.sduduzog.slimlauncher.ui.dialogs.ChooseClockTypeDialog
import com.sduduzog.slimlauncher.ui.dialogs.ChooseTimeFormatDialog
import com.sduduzog.slimlauncher.utils.BaseFragment
Expand Down Expand Up @@ -64,8 +64,7 @@ class OptionsFragment : BaseFragment() {
chooseClockTypeDialog.showNow(childFragmentManager, "CLOCK_TYPE_CHOOSER")
}
optionsFragment.optionsFragmentChooseAlignment.setOnClickListener {
val chooseAlignmentDialog = ChooseAlignmentDialog.getInstance()
chooseAlignmentDialog.showNow(childFragmentManager, "ALIGNMENT_CHOOSER")
AlignmentFormatDialog().showNow(childFragmentManager, "ALIGNMENT_CHOOSER")
}
optionsFragment.optionsFragmentToggleStatusBar.setOnClickListener {
val settings = requireContext().getSharedPreferences(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.jkuester.unlauncher.dialog

import android.app.AlertDialog
import android.content.DialogInterface
import com.jkuester.unlauncher.datasource.CorePreferencesRepository
import com.jkuester.unlauncher.datasource.setAlignmentFormat
import com.jkuester.unlauncher.datastore.proto.AlignmentFormat
import com.sduduzog.slimlauncher.R
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import io.mockk.justRun
import io.mockk.mockk
import io.mockk.mockkConstructor
import io.mockk.mockkStatic
import io.mockk.slot
import io.mockk.verify
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@MockKExtension.CheckUnnecessaryStub
// @MockKExtension.ConfirmVerification Weird bug between mockk/kotlin/Java is causing this to fail
@ExtendWith(MockKExtension::class)
class AlignmentFormatDialogTest {
@MockK
lateinit var corePrefsRepo: CorePreferencesRepository
@MockK
lateinit var alertDialog: AlertDialog

@Test
fun onCreateDialog() {
mockkConstructor(AlertDialog.Builder::class)
val selectAlignmentSlot = slot<DialogInterface.OnClickListener>()
every { anyConstructed<AlertDialog.Builder>().setTitle(any(Int::class)) } answers
{ self as AlertDialog.Builder }
every {
anyConstructed<AlertDialog.Builder>().setSingleChoiceItems(
any(Int::class),
any(Int::class),
capture(selectAlignmentSlot)
)
} answers { self as AlertDialog.Builder }
every { anyConstructed<AlertDialog.Builder>().create() } returns alertDialog
every { corePrefsRepo.get().alignmentFormat.number } returns 0
justRun { alertDialog.dismiss() }
every { corePrefsRepo.updateAsync(any()) } returns mockk()
mockkStatic(::setAlignmentFormat)
every { setAlignmentFormat(any()) } returns mockk()

val dialogFragment = AlignmentFormatDialog()
.apply { corePreferencesRepo = corePrefsRepo }
val result = dialogFragment.onCreateDialog(null)

result shouldBe alertDialog
verify(exactly = 1) { anyConstructed<AlertDialog.Builder>().setTitle(R.string.choose_alignment_dialog_title) }
verify(exactly = 1) {
anyConstructed<AlertDialog.Builder>().setSingleChoiceItems(
R.array.alignment_format_array,
0,
selectAlignmentSlot.captured
)
}
verify(exactly = 1) { anyConstructed<AlertDialog.Builder>().create() }
verify(exactly = 1) { corePrefsRepo.get().alignmentFormat.number }

// Trigger the listener
selectAlignmentSlot.captured.onClick(alertDialog, 0)

verify(exactly = 1) { alertDialog.dismiss() }
verify(exactly = 1) { corePrefsRepo.updateAsync(any()) }
verify(exactly = 1) { setAlignmentFormat(AlignmentFormat.forNumber(0)) }
}
}

0 comments on commit ec0e630

Please sign in to comment.