Skip to content

Instantly share code, notes, and snippets.

@emmano
emmano / LooperDependent.java
Created Apr 20, 2020
Sample class that has a dependency on Looper
View LooperDependent.java
public class LooperDependent {
static final Handler HANDLER =
new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
throw new AssertionError("Unknown handler message received: " + msg.what);
}
};
public void someMethodToVerify() {}
}
@emmano
emmano / SandboxTestRunner.java
Created Apr 20, 2020
Portion of SandboxTestRunner that reloads tests using reflection and custom ClassLoader
View SandboxTestRunner.java
sandbox.runOnMainThread(() -> {
ClassLoader priorContextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(sandbox.getRobolectricClassLoader());
Class bootstrappedTestClass =
sandbox.bootstrappedClass(getTestClass().getJavaClass());
HelperTestRunner helperTestRunner = getHelperTestRunner(bootstrappedTestClass);
helperTestRunner.frameworkMethod = method;
final Method bootstrappedMethod;
@emmano
emmano / onFragment.kt
Created Mar 23, 2020
Testing Fragments that depend on a Activity.
View onFragment.kt
abstract val destination: Int
inline fun <reified F : Fragment> onFragment(crossinline block: F.() -> Unit) {
val intent = Intent(
ApplicationProvider.getApplicationContext<Application>(),
MainActivity::class.java
).apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK }
val scenario = ActivityScenario.launch<MainActivity>(intent)
scenario.onActivity {
Shadows.shadowOf(Looper.getMainLooper()).idle()
@emmano
emmano / launchInActivity.kt
Created Mar 16, 2020
launchInActivity.kt
View launchInActivity.kt
inline fun <reified F : Fragment, reified A : AppCompatActivity> launchInActivity(
crossinline fragmentFactory: () -> F,
state: Lifecycle.State = Lifecycle.State.RESUMED,
layoutId: Int = android.R.id.content,
isTestOnlyActivity: Boolean = false
) : ActivityScenario<A> {
//Manually register test only Activity since manifest merging is broken for test resources https://github.com/robolectric/robolectric/issues/4725
if(isTestOnlyActivity) {
val appContext = ApplicationProvider.getApplicationContext<Application>()
View TimerFlow.kt
class TimerFlow {
@ExperimentalCoroutinesApi
suspend fun timer(
pollingDelay: Long,
coroutineDispatcher: CoroutineDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
) = flow {
while (true) {
delay(pollingDelay)
emit(Unit)
}
View AdapterTest.kt
class TestFragmentTest: RobolectricTest() {
@Test
fun `test adapter dsl`() {
val scenario = launchFragmentInContainer<TestFragment>()
scenario.onFragment {
val recyclerView = it.requireView().findViewById<RecyclerView>(R.id.recycler)
val headerViewHolder =
recyclerView.findViewHolderForLayoutPosition(0) ?: throw IllegalStateException()
View ComplexAdapter.kt
fun TestFragment.adapter() = adapter<Model> {
val TYPE_HEADER = 1
val TYPE_CONTENT = 2
onCreateViewHolder { parent, viewType ->
when (viewType) {
TYPE_HEADER -> holder<HeaderBinding, Header>(parent, R.layout.header, BR.header) {
onBind { binding, header ->
View SimpleAdapterWithBinding.kt
private val adapter by adapter<ComicCellBinding, ComicModel>(BR.comicModel, R.layout.comic_cell){binding, model-> //do something}
View SimpleAdapterWithClickListener.kt
private val adapter by adapter<ComicModel>(BR.comicModel, R.layout.comic_cell){model-> //do something on click}