|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Brayan Oliveira <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU General Public License as published by the Free Software |
| 6 | + * Foundation; either version 3 of the License, or (at your option) any later |
| 7 | + * version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | + * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License along with |
| 14 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | +package com.ichi2.anki.ui.windows.reviewer |
| 17 | + |
| 18 | +import android.content.SharedPreferences |
| 19 | +import android.content.res.Resources |
| 20 | +import androidx.core.content.edit |
| 21 | +import com.github.ivanshafran.sharedpreferencesmock.SPMockBuilder |
| 22 | +import com.ichi2.anki.R |
| 23 | +import com.ichi2.anki.preferences.reviewer.ReviewerMenuRepository |
| 24 | +import com.ichi2.anki.preferences.reviewer.ViewerAction |
| 25 | +import com.ichi2.anki.settings.PrefsRepository |
| 26 | +import io.mockk.every |
| 27 | +import io.mockk.mockk |
| 28 | +import org.junit.Assert.assertEquals |
| 29 | +import org.junit.Assert.assertFalse |
| 30 | +import org.junit.Assert.assertTrue |
| 31 | +import org.junit.Test |
| 32 | +import java.net.ServerSocket |
| 33 | + |
| 34 | +class StudyScreenRepositoryTest { |
| 35 | + private val sharedPrefs: SharedPreferences = SPMockBuilder().createSharedPreferences() |
| 36 | + private val menuRepository = ReviewerMenuRepository(sharedPrefs) |
| 37 | + private val prefs: PrefsRepository |
| 38 | + |
| 39 | + init { |
| 40 | + val mockResources = mockk<Resources>() |
| 41 | + every { mockResources.getString(any()) } answers { invocation.args[0].toString() } |
| 42 | + prefs = PrefsRepository(sharedPrefs, mockResources) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `isMarkShownInToolbar and isFlagShownInToolbar are true when toolbar is enabled and action is set to ALWAYS`() { |
| 47 | + menuRepository.setDisplayTypeActions( |
| 48 | + alwaysShowActions = |
| 49 | + listOf( |
| 50 | + ViewerAction.MARK, |
| 51 | + ViewerAction.FLAG_MENU, |
| 52 | + ), |
| 53 | + menuOnlyActions = listOf(), |
| 54 | + disabledActions = listOf(), |
| 55 | + ) |
| 56 | + val repository = StudyScreenRepository(prefs) |
| 57 | + |
| 58 | + assertTrue(repository.isMarkShownInToolbar) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `isMarkShownInToolbar and isFlagShownInToolbar are false when action is NOT in ALWAYS list`() { |
| 63 | + menuRepository.setDisplayTypeActions( |
| 64 | + alwaysShowActions = listOf(), |
| 65 | + menuOnlyActions = |
| 66 | + listOf( |
| 67 | + ViewerAction.MARK, |
| 68 | + ViewerAction.FLAG_MENU, |
| 69 | + ), |
| 70 | + disabledActions = listOf(), |
| 71 | + ) |
| 72 | + val repository = StudyScreenRepository(prefs) |
| 73 | + |
| 74 | + assertFalse(repository.isMarkShownInToolbar) |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun `isMarkShownInToolbar and isFlagShownInToolbar are false when toolbar is completely hidden`() { |
| 79 | + menuRepository.setDisplayTypeActions( |
| 80 | + alwaysShowActions = |
| 81 | + listOf( |
| 82 | + ViewerAction.MARK, |
| 83 | + ViewerAction.FLAG_MENU, |
| 84 | + ), |
| 85 | + menuOnlyActions = listOf(), |
| 86 | + disabledActions = listOf(), |
| 87 | + ) |
| 88 | + sharedPrefs.edit { |
| 89 | + putString(R.string.reviewer_toolbar_position_key.toString(), R.string.reviewer_toolbar_value_none.toString()) |
| 90 | + } |
| 91 | + val repository = StudyScreenRepository(prefs) |
| 92 | + |
| 93 | + assertFalse(repository.isMarkShownInToolbar) |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun `getServerPort returns 0 when useFixedPortInReviewer is false`() { |
| 98 | + prefs.useFixedPortInReviewer = false |
| 99 | + |
| 100 | + val repository = StudyScreenRepository(prefs) |
| 101 | + val port = repository.getServerPort() |
| 102 | + |
| 103 | + assertEquals(0, port) |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun `getServerPort returns valid port when configured and available`() { |
| 108 | + prefs.useFixedPortInReviewer = true |
| 109 | + prefs.reviewerPort = 0 |
| 110 | + |
| 111 | + val repository = StudyScreenRepository(prefs) |
| 112 | + val port = repository.getServerPort() |
| 113 | + |
| 114 | + assertTrue(port > 0) |
| 115 | + assertEquals(prefs.reviewerPort, port) |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + fun `getServerPort returns 0 when specific port is already in use`() { |
| 120 | + val blockerSocket = ServerSocket(0) |
| 121 | + val busyPort = blockerSocket.localPort |
| 122 | + |
| 123 | + try { |
| 124 | + prefs.useFixedPortInReviewer = true |
| 125 | + prefs.reviewerPort = busyPort |
| 126 | + |
| 127 | + val repository = StudyScreenRepository(prefs) |
| 128 | + val resultPort = repository.getServerPort() |
| 129 | + |
| 130 | + assertEquals(0, resultPort) |
| 131 | + } finally { |
| 132 | + blockerSocket.close() |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments