Skip to content

Instantly share code, notes, and snippets.

class SharedPreferencesManagerImpl(val context: Context) : SharedPreferencesManager {
private val notEncryptedPreferencesName = "not_encrypted_preferences_filename"
private val encryptedPreferencesName = "encrypted_preferences_filename"
private var prefs: SharedPreferences
init {
val nonEncryptedPreferences: SharedPreferences = context.getSharedPreferences(notEncryptedPreferencesName, Context.MODE_PRIVATE)
@oaviad
oaviad / SharedPreferencesManager.kt
Last active May 17, 2022 12:28
Shared Preferences Interface
interface SharedPreferencesManager {
fun <T : Any?> set(key: String, value: T)
fun getString(key: String, defaultValue: String?): String?
fun getInt(key: String, defaultValue: Int): Int
fun getBoolean(key: String, defaultValue: Boolean): Boolean
dependencies {
...
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}
@LargeTest
@RunWith(AndroidJUnit4::class)
class NotificationTest {
@get:Rule
val rule = activityScenarioRule<MainActivity>()
val device = UiDevice.getInstance(getInstrumentation())
@Test
@oaviad
oaviad / NoConstraintsViewActionExample.kt
Last active April 21, 2020 13:17
NoConstraintsViewAction Example
// Implementation of custum viewAction for click() Action
val noConstraintsClickAction = object : ViewAction {
override fun getDescription(): String {
return "noConstraintsClickAction"
}
override fun getConstraints(): Matcher<View> {
return isEnabled() // no constraints
}
@oaviad
oaviad / ActivityScenarioRuleExample.kt
Last active April 20, 2020 13:23
ActivityScenarioRule Example
@get:Rule
val rule = activityScenarioRule<MyActivity>()
@Test void myTest() {
val scenario = rule.getScenario();
// Your test code goes here.
}
@oaviad
oaviad / getCurrentActivity.kt
Last active April 26, 2020 11:07
Get Current Activity - Espresso
// How to get your current Activity
fun getCurrentActivity(): Activity? {
var currentActivity: Activity? = null
getInstrumentation().runOnMainSync { run { currentActivity = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED).elementAtOrNull(0) } }
return currentActivity
}
// Example: how to use it
assertThat(getCurrentActivity(), instanceOf(MyActivity::class.java))
@oaviad
oaviad / build.gradle
Created April 20, 2020 09:52
Android Test Orchestrator - Gradle File
android {
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// The following argument makes the Android Test Orchestrator run its
// "pm clear" command after each test invocation. This command ensures
// that the app's state is completely cleared between tests.
testInstrumentationRunnerArguments clearPackageData: 'true'
}
@oaviad
oaviad / UnitTestJunit5.kt
Last active February 18, 2020 08:35
Unit Test with Junit5
@Test
fun `when calling loadInbox, then update inboxLiveData with empty messages list`() {
val messages = emptyList<MessageEntity>()
runBlocking {
whenever(messagesRepository.getMessages(any(), any())).thenReturn(messages)
inboxViewModel.loadInbox().join()
getValue(inboxViewModel.inboxLiveData).let {
assertThat(it).isEqualTo(messages)
@oaviad
oaviad / RoomDatabase.kt
Last active May 4, 2021 03:39
room database
@Entity(tableName = "inbox", indices = [Index(value = ["uid"], unique = true)])
data class Message @JvmOverloads constructor(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = _id) val id: Long?,
@ColumnInfo(name = timestamp) val timestamp: Long
@ColumnInfo(name = uid) val uid: Long,
@ColumnInfo(name = is_read, defaultValue = "0") val isRead: Int = 0
@ColumnInfo(name = text) val text: String?
)
@Database(entities = arrayOf(Message::class), version = 2)