Skip to content

Instantly share code, notes, and snippets.

View elihart's full-sized avatar

Eli Hart elihart

  • Airbnb
  • San Francisco
View GitHub Profile
@elihart
elihart / FragmentDirectory.kt
Created December 5, 2019 04:32
Rough example code of a system for declaring routes for Fragments and their argument types. Allows for easily loading the Fragments individually or in a new activity.. Assumes usage with MvRx, but can be used with normal Fragments and Activities with basic modification.
// The tools below can be used to easily start fragments and activities across module boundaries.
// Example usage - a "directory" is declared like this object.
// Then you can use it to:
// Create a new fragment: Search.results().newInstance(fragmentArguments)
// Create an intent for the Fragment: Search.results().newIntent(context, fragmentArguments)
// Start an intent for the Fragment: Search.results().startActivity(context, fragmentArguments)
object Search : Fragments("com.example.android.search") {
fun results() = create<SearchArguments>("SearchResultsFragment")
@elihart
elihart / gist:be43575bf7e7447f67f08467a5604740
Created December 5, 2019 04:31
Rough example code of a system for declaring accessible Fragments, and then easily loading them.
// The tools below can be used to easily start fragments and activities across module boundaries.
// Example usage - a "directory" is declared like this object.
// Then you can use it to:
// Create a new fragment: Search.results().newInstance(fragmentArguments)
// Create an intent for the Fragment: Search.results().newIntent(context, fragmentArguments)
// Start an intent for the Fragment: Search.results().startActivity(context, fragmentArguments)
object Search : Fragments("com.example.android.search") {
fun results() = create<SearchArguments>("SearchResultsFragment")
@elihart
elihart / ktest.kts
Created November 27, 2019 16:08
Kotlin script that runs `gradle clean test` on other scripts to easily build and test them (using kscript)
import java.io.BufferedReader
import java.io.File
import java.util.concurrent.TimeUnit
import kotlin.system.exitProcess
/**
* This script takes the name of another kts script as an argument, builds that script with gradle, and runs
* its tests via "gradle clean test".
*/
@elihart
elihart / MvRxCombinedMocksExample.kt
Created November 20, 2019 21:16
MvRxCombinedMocksExample
override fun provideMocks() = combineMocks(
"Marketplace" to marketplaceMocks(),
"Plus" to plusMocks(),
"Plus ProHost" to plusProHostMocks()
)
@elihart
elihart / MvrxIntegrationTestsGeneratedClass.kt
Last active November 20, 2019 21:15
MvrxIntegrationTestsGeneratedClass
@LargeTest
class MvrxIntegrationTests : MvRxIntegrationTestBase() {
@Test
fun booking_fragments_BookingFragment_Screenshots() {
runScreenshots("com.airbnb.android.booking.fragments.BookingFragment")
}
@Test
fun booking_fragments_BookingFragment_Interactions() {
runInteractionTest("com.airbnb.android.booking.fragments.BookingFragment")
@elihart
elihart / ForceClearingAndroidResourceCache.kt
Created November 20, 2019 20:58
ForceClearingAndroidResourceCache
AppCompatDrawableManager.get().onConfigurationChanged(view.context)
val resources = view.resources
val currentConfig = resources.configuration
val currentDisplayMetrics = resources.displayMetrics
resources.updateConfiguration(null, currentDisplayMetrics)
resources.updateConfiguration(currentConfig, currentDisplayMetrics)
@elihart
elihart / TestContextFailureMessageExample.kt
Created November 20, 2019 20:52
TestContextFailureMessageExample
val testContextMessage = "Error while testing 'Default State' mock for BookingFragment -> Clicking 'book_button' view on thread 'AsyncTask #1'"
throw IllegalStateException(testContextMessage, originalException)
@elihart
elihart / HowToWaitForIdleLoopers.kt
Created November 20, 2019 20:47
HowToWaitForIdleHandlers
fun waitForLoopers(loopers: List<Looper>) {
val idleDetectors = loopers.map { HandlerIdleDetector(Handler(it)) }
while (idleDetectors.any {  !it.isIdle }) {
}
}
class HandlerIdleDetector(val handler: Handler) {
var isIdle = false
init {
@elihart
elihart / AirbnbIntegrationTestExamples.kt
Created November 20, 2019 20:43
AirbnbIntegrationTestExamples
@Test
fun screenshotBookingFragment() = runScreenshots("com.airbnb.booking.BookingFragment")
@Test
fun screenshotSearchFragment() = runScreenshots("com.airbnb.search.SearchFragment")
fun runScreenshots(fragmentName: String) {
val intent = IntegrationTestActivity.intent<HappoTestActivity>(
context = InstrumentationRegistry.getInstrumentation().targetContext,
fragmentName = fragmentName
@elihart
elihart / InteractionTestActivity.kt
Created November 20, 2019 20:41
InteractionTestActivity
class InteractionTestActivity : IntegrationTestActivity() {
override fun testCurrentScreen(
mockProvider: MockedFragmentProvider,
fragment: MvRxFragment,
resetView: (onViewReset: (MvRxFragment) -> Unit) -> Unit,
finishedTestingFragment: () -> Unit
) {
ActivityInteractionTester(
activity = this,
resetViewCallback = resetView,