-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds FakeVocableSharedPreferences, Adds tests for SelectionModeViewMo…
…delTest
- Loading branch information
1 parent
75e92cb
commit eedd2fa
Showing
2 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
...src/test/java/com/willowtree/vocable/settings/selectionmode/SelectionModeViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
app/src/test/java/com/willowtree/vocable/utils/FakeVocableSharedPreferences.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |