This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface NoteDetailsContract { | |
interface View { | |
void showNote(String note); | |
void close(); | |
} | |
interface UserActions { | |
void deleteNoteClick(long noteId); | |
void updateNoteClick(long noteId, String updatedNote); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(AndroidJUnit4::class) | |
class MainViewTests { | |
val mockUserAction = mock(MainContract.UserAction::class.java) | |
@get:Rule | |
val activityTestRule = object : ActivityTestRule<MainActivity>(MainActivity::class.java, true, true) { | |
override fun beforeActivityLaunched() { | |
super.beforeActivityLaunched() | |
val myApp = InstrumentationRegistry.getTargetContext().applicationContext as MyApp | |
myApp.dispatchingActivityInjector = createFakeFragmentInjector<MainFragment> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified F : Fragment> createFakeFragmentInjector(crossinline block : F.() -> Unit) | |
: DispatchingAndroidInjector<Any> { | |
val myApp = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as MyApp | |
val originalDispatchingActivityInjector = myApp.dispatchingActivityInjector | |
var originalFragmentInjector: AndroidInjector<Any>? = null | |
val fragmentInjector = AndroidInjector<Fragment> { fragment -> | |
originalFragmentInjector?.inject(fragment) | |
if (fragment is F) { | |
fragment.block() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyApp : Application(), HasAndroidInjector { | |
@Inject lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Any> | |
override fun androidInjector() = dispatchingActivityInjector | |
override fun onCreate() { | |
super.onCreate() | |
DaggerAppComponent.builder().application(this).build().inject(this) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the fragment that we want to inject | |
@Module | |
class InjectFragmentModule | |
@Module | |
abstract class InjectFragmentBuilder { | |
@ContributesAndroidInjector(modules = [InjectFragmentModule::class]) | |
internal abstract fun bindInjectFragment(): InjectFragment | |
} | |
// application level |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T> lambdaMock(): T = mock(T::class.java) | |
class Presenter { | |
val titleLiveData = MutableLiveData<String>() | |
fun observeTitleChanges(lifecycle: Lifecycle, observer: (String) -> Unit) { | |
titleLiveData.observe({ lifecycle }) { title -> | |
title?.let(observer) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Presenter { | |
val titleLiveData = MutableLiveData<String>() | |
fun showTitle(title: String) { | |
titleLiveData.postValue(title) | |
} | |
} | |
class PresenterTests { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun getFirstImage (firstImageUrl: String, secondImageUrl: String, imageFetcher: ImageFetcher) { | |
val images = mutableListOf<Bitmap>() | |
val firstJob = launch { | |
images.add(imageFetcher.getImageFromUrl(firstImageUrl)) | |
} | |
val secondJob = launch(CommonPool) { | |
images.add(imageFetcher.getImageFromUrl(secondImageUrl)) | |
} | |
select<Unit> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun fetch2Images (firstImageUrl: String, secondImageUrl: String, imageFetcher: ImageFetcher) { | |
val images = mutableListOf<Bitmap>() | |
val firstJob = launch { | |
images.add(imageFetcher.getImageFromUrl(firstImageUrl)) | |
} | |
val secondJob = launch(CommonPool) { | |
images.add(imageFetcher.getImageFromUrl(secondImageUrl)) | |
} | |
firstJob.join() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun fetchImage(key: String, storage: Storage, imageFetcher: ImageFetcher) : Bitmap { | |
val url = async {storage.getUrlForKey(key)}.await() | |
val image = async { imageFetcher.getImageFromUrl(url) } | |
println("this might be run after the async have started") | |
return image.await() | |
} |