Skip to content

Instantly share code, notes, and snippets.

@oaviad
oaviad / viewmodelLivedataExample.kt
Last active April 7, 2020 10:11
viewmodel & livedata
// yeah, still written Java
public class InboxActivity extends Activity {
private InboxViewModel viewModel;
@Override
public View onCreate(Bundle savedInstanceState) {
viewModel = new ViewModelProvider(this).get(InboxViewModel.class);
viewModel.getInboxLiveData().observe(this, this::handleInboxData);
viewModel.loadInbox();
@oaviad
oaviad / GetInboxFromServer.kt
Last active February 25, 2020 14:05
Write async code in a sequential manner
val retrofitClient = RetrofitClient.getInstance()
private suspend fun getInbox(): GetInboxResponse? = suspendCoroutine {
val call = retrofitClient.getInbox()
call.enqueue(object : Callback<GetInboxResponse> {
override fun onResponse(call: Call<GetInboxResponse>, response: Response<GetInboxResponse>) {
it.resume(response.body())
}
@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)
@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 / 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 / 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 / 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 / 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
}
@LargeTest
@RunWith(AndroidJUnit4::class)
class NotificationTest {
@get:Rule
val rule = activityScenarioRule<MainActivity>()
val device = UiDevice.getInstance(getInstrumentation())
@Test
dependencies {
...
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}