package com.dtourolle.jellytau

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * The screen-wake decision, isolated from the Activity window it is applied to.
 *
 * TRACES: UR-003 | DR-202 | UT-199
 */
class ScreenWakeStateTest {

    @Test
    fun `starts released`() {
        assertFalse(ScreenWakeState().keepScreenOn)
    }

    @Test
    fun `native video playing holds the screen on`() {
        val state = ScreenWakeState()
        state.updateNative(playing = true, isVideo = true)
        assertTrue(state.keepScreenOn)
    }

    @Test
    fun `pausing native video releases the screen`() {
        val state = ScreenWakeState()
        state.updateNative(playing = true, isVideo = true)
        state.updateNative(playing = false, isVideo = true)
        assertFalse(state.keepScreenOn)
    }

    /** Music with the screen off is the whole point of the audio path. */
    @Test
    fun `native audio playing does not hold the screen on`() {
        val state = ScreenWakeState()
        state.updateNative(playing = true, isVideo = false)
        assertFalse(state.keepScreenOn)
    }

    @Test
    fun `webview video playing holds the screen on`() {
        val state = ScreenWakeState()
        state.updateHtml5(active = true, playing = true)
        assertTrue(state.keepScreenOn)
    }

    @Test
    fun `webview video paused releases the screen`() {
        val state = ScreenWakeState()
        state.updateHtml5(active = true, playing = true)
        state.updateHtml5(active = true, playing = false)
        assertFalse(state.keepScreenOn)
    }

    /** The element going away must release even if it never reported a pause. */
    @Test
    fun `webview video going inactive while playing releases the screen`() {
        val state = ScreenWakeState()
        state.updateHtml5(active = true, playing = true)
        state.updateHtml5(active = false, playing = true)
        assertFalse(state.keepScreenOn)
    }

    /** The two rendering paths are independent holders; either one is enough. */
    @Test
    fun `one path releasing does not release while the other still plays`() {
        val state = ScreenWakeState()
        state.updateNative(playing = true, isVideo = true)
        state.updateHtml5(active = true, playing = true)
        state.updateHtml5(active = false, playing = false)
        assertTrue(state.keepScreenOn)
        state.updateNative(playing = false, isVideo = true)
        assertFalse(state.keepScreenOn)
    }

    @Test
    fun `teardown releases both paths`() {
        val state = ScreenWakeState()
        state.updateNative(playing = true, isVideo = true)
        state.updateHtml5(active = true, playing = true)
        state.reset()
        assertFalse(state.keepScreenOn)
    }
}
