Skip to content

Commit

Permalink
Adds FakeVocableSharedPreferences, Adds tests for SelectionModeViewMo…
Browse files Browse the repository at this point in the history
…delTest
  • Loading branch information
IanCrossCD committed Nov 16, 2023
1 parent db8c83b commit e3f1c33
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.willowtree.vocable.settings.selectionmode

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.willowtree.vocable.MainDispatcherRule
import com.willowtree.vocable.getOrAwaitValue
import com.willowtree.vocable.utils.FakeVocableSharedPreferences
import com.willowtree.vocable.utils.IVocableSharedPreferences
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test


class SelectionModeViewModelTest {

@get:Rule
val mainDispatcherRule = MainDispatcherRule()

@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()

private fun createViewModel(sharedPreferences: IVocableSharedPreferences): SelectionModeViewModel {
return SelectionModeViewModel(sharedPreferences)
}

private fun createSharedPrefs(headTrackingEnabled: Boolean): IVocableSharedPreferences {
return FakeVocableSharedPreferences(headTrackingEnabled = headTrackingEnabled)
}

@Test
fun `permission state Requested on init if head tracking E=enabled`() = runTest {

val viewModel = createViewModel(createSharedPrefs(headTrackingEnabled = true))

assert(viewModel.headTrackingPermissionState.getOrAwaitValue() is HeadTrackingPermissionState.PermissionRequested)
}

@Test
fun `permission state Disabled on init if head tracking enabled`() = runTest {

val viewModel = createViewModel(createSharedPrefs(headTrackingEnabled = false))

assert(viewModel.headTrackingPermissionState.getOrAwaitValue() is HeadTrackingPermissionState.Disabled)
}

@Test
fun `requestHeadTracking() sets permission state to PermissionRequested`() = runTest {

// Setting false so its not Requested on init
val viewModel = createViewModel(createSharedPrefs(headTrackingEnabled = false))

assert(viewModel.headTrackingPermissionState.getOrAwaitValue() is HeadTrackingPermissionState.Disabled)

viewModel.requestHeadTracking()

assert(viewModel.headTrackingPermissionState.getOrAwaitValue() is HeadTrackingPermissionState.PermissionRequested)
}

@Test
fun `enableHeadTracking() sets permission state to Enabled`() = runTest {

val sharedPreference = createSharedPrefs(headTrackingEnabled = false)
assert(!sharedPreference.getHeadTrackingEnabled())

// Setting false so its not Requested on init
val viewModel = createViewModel(sharedPreference)

assert(viewModel.headTrackingPermissionState.getOrAwaitValue() is HeadTrackingPermissionState.Disabled)

viewModel.enableHeadTracking()

assert(viewModel.headTrackingPermissionState.getOrAwaitValue() is HeadTrackingPermissionState.Enabled)
// Check shared preferences is updated
assert(sharedPreference.getHeadTrackingEnabled())
}

@Test
fun `disableHeadTracking() sets permission state to Disabled`() = runTest {

val sharedPreference = createSharedPrefs(headTrackingEnabled = true)
assert(sharedPreference.getHeadTrackingEnabled())

// Setting true so its not Disabled on init
val viewModel = createViewModel(sharedPreference)

assert(viewModel.headTrackingPermissionState.getOrAwaitValue() is HeadTrackingPermissionState.PermissionRequested)

viewModel.disableHeadTracking()

assert(viewModel.headTrackingPermissionState.getOrAwaitValue() is HeadTrackingPermissionState.Disabled)
// Check shared preferences is updated
assert(!sharedPreference.getHeadTrackingEnabled())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.willowtree.vocable.utils

import android.content.SharedPreferences


class FakeVocableSharedPreferences(
private var mySayings: List<String> = listOf(),
private var dwellTime: Long = 0,
private var sensitivity: Float = 0f,
private var headTrackingEnabled: Boolean = false,
private var firstTime: Boolean = false
) : IVocableSharedPreferences {

override fun registerOnSharedPreferenceChangeListener(vararg listeners: SharedPreferences.OnSharedPreferenceChangeListener) {
// no-op currently
}

override fun unregisterOnSharedPreferenceChangeListener(vararg listeners: SharedPreferences.OnSharedPreferenceChangeListener) {
// no-op currently
}

override fun getMySayings(): List<String> {
return mySayings
}

override fun setMySayings(mySayings: Set<String>) {
this.mySayings = mySayings.toList()
}

override fun getDwellTime(): Long {
return dwellTime
}

override fun setDwellTime(time: Long) {
dwellTime = time
}

override fun getSensitivity(): Float {
return sensitivity
}

override fun setSensitivity(sensitivity: Float) {
this.sensitivity = sensitivity
}

override fun setHeadTrackingEnabled(enabled: Boolean) {
headTrackingEnabled = enabled
}

override fun getHeadTrackingEnabled(): Boolean {
return headTrackingEnabled
}

override fun setFirstTime() {
firstTime = true
}

override fun getFirstTime(): Boolean {
return firstTime
}
}

0 comments on commit e3f1c33

Please sign in to comment.