package dev.privacyllc.period.launcher

import android.content.ComponentName
import android.content.Context
import android.content.pm.PackageManager
import androidx.test.core.app.ApplicationProvider
import dev.privacyllc.period.MainActivity
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34])
class LauncherAliasSwitcherTest {

    private val context: Context = ApplicationProvider.getApplicationContext()
    private val switcher = AndroidLauncherAliasSwitcher(context)
    private val pm = context.packageManager
    private val packageName = context.packageName
    private val aliasPackage = MainActivity::class.java.name.substringBeforeLast('.')
    private val standard = ComponentName(packageName, "$aliasPackage.PeriodLauncherAlias")
    private val incognito = ComponentName(packageName, "$aliasPackage.IncognitoLauncherAlias")

    @Test fun `enabling incognito turns on only the neutral alias`() {
        switcher.setIncognitoEnabled(true)

        assertEquals(PackageManager.COMPONENT_ENABLED_STATE_DISABLED, pm.getComponentEnabledSetting(standard))
        assertEquals(PackageManager.COMPONENT_ENABLED_STATE_ENABLED, pm.getComponentEnabledSetting(incognito))
    }

    @Test fun `disabling incognito restores only the normal alias`() {
        switcher.setIncognitoEnabled(false)

        assertEquals(PackageManager.COMPONENT_ENABLED_STATE_ENABLED, pm.getComponentEnabledSetting(standard))
        assertEquals(PackageManager.COMPONENT_ENABLED_STATE_DISABLED, pm.getComponentEnabledSetting(incognito))
    }
}
