FullScreenFeatureTest.kt 3.74 KB
Newer Older
1
2
3
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
6
7
8
9
10

package mozilla.components.feature.session

import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
import mozilla.components.concept.engine.EngineSession
import mozilla.components.support.test.mock
11
import mozilla.components.support.test.whenever
12
13
14
15
16
17
18
19
20
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mockito.never
import org.mockito.Mockito.spy
import org.mockito.Mockito.verify

class FullScreenFeatureTest {
21

22
23
24
25
26
27
28
29
30
31
    private val sessionManager: SessionManager = mock()
    private val selectedSession: Session = mock()
    private val useCases = spy(SessionUseCases(sessionManager))

    @Test
    fun `start without a sessionId`() {
        val fullscreenFeature = spy(FullScreenFeature(sessionManager, useCases) {})

        fullscreenFeature.start()

32
        verify(fullscreenFeature).observeIdOrSelected(null)
33
34
35
36
37
38
39
        verify(sessionManager, never()).findSessionById(anyString())
        verify(fullscreenFeature).observeSelected()
    }

    @Test
    fun `start with a sessionId`() {
        val fullscreenFeature = spy(FullScreenFeature(sessionManager, useCases, "abc") {})
40
        whenever(sessionManager.findSessionById(anyString())).thenReturn(selectedSession)
41
42
43

        fullscreenFeature.start()

44
        verify(fullscreenFeature).observeIdOrSelected(anyString())
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        verify(sessionManager).findSessionById(anyString())
        verify(fullscreenFeature).observeFixed(selectedSession)
    }

    @Test
    fun `invoke listener when fullscreen observer invoked`() {
        var fullscreenChanged = false
        val fullscreenFeature = spy(FullScreenFeature(sessionManager, useCases) { b -> fullscreenChanged = b })

        fullscreenFeature.onFullScreenChanged(mock(), true)

        assertTrue(fullscreenChanged)
    }

    @Test
    fun `onBackPressed returns false with no activeSession`() {
        val fullscreenFeature = spy(FullScreenFeature(sessionManager, useCases) {})

        assertFalse(fullscreenFeature.onBackPressed())
    }

    @Test
    fun `onBackPressed returns false with activeSession but not in fullscreen mode`() {
        val fullscreenFeature = spy(FullScreenFeatureTest(sessionManager, useCases) {})
        val activeSession: Session = mock()

71
72
        whenever(fullscreenFeature.activeSession).thenReturn(activeSession)
        whenever(activeSession.fullScreenMode).thenReturn(false)
73
74
75
76
77
78
79
80
81
82

        assertFalse(fullscreenFeature.onBackPressed())
    }

    @Test
    fun `onBackPressed returns true with activeSession and fullscreen mode`() {
        val activeSession: Session = mock()
        val engineSession: EngineSession = mock()
        val fullscreenFeature = spy(FullScreenFeatureTest(sessionManager, useCases) {})

83
84
85
86
        whenever(sessionManager.getOrCreateEngineSession(activeSession)).thenReturn(engineSession)
        whenever(fullscreenFeature.activeSession).thenReturn(activeSession)
        whenever(activeSession.fullScreenMode).thenReturn(true)
        whenever(useCases.exitFullscreen).thenReturn(mock())
87
88
89
90
91
92
93
94
95
96

        assertTrue(fullscreenFeature.onBackPressed())
        verify(useCases.exitFullscreen).invoke(activeSession)
    }

    private class FullScreenFeatureTest(
        sessionManager: SessionManager,
        sessionUseCases: SessionUseCases,
        sessionId: String? = null,
        fullScreenChanged: (Boolean) -> Unit
97
    ) : FullScreenFeature(sessionManager, sessionUseCases, sessionId, fullScreenChanged = fullScreenChanged) {
98
99
100
        public override var activeSession: Session? = null
    }
}