Skip to content

Instantly share code, notes, and snippets.

@emmano
emmano / LooperDependent.java
Created April 20, 2020 21:55
Sample class that has a dependency on Looper
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 April 20, 2020 21:36
Portion of SandboxTestRunner that reloads tests using reflection and custom ClassLoader
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 March 23, 2020 18:19
Testing Fragments that depend on a Activity.
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 March 16, 2020 03:08
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>()
class TimerFlow {
@ExperimentalCoroutinesApi
suspend fun timer(
pollingDelay: Long,
coroutineDispatcher: CoroutineDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
) = flow {
while (true) {
delay(pollingDelay)
emit(Unit)
}
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()
private val adapter by adapter()
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 ->
private val adapter by adapter<ComicCellBinding, ComicModel>(BR.comicModel, R.layout.comic_cell){binding, model-> //do something}
private val adapter by adapter<ComicModel>(BR.comicModel, R.layout.comic_cell){model-> //do something on click}