package Ġcom . mil ky head . app . android trie search sample . ui Ċ Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġcom . mil ky head . app . android trie search sample . Coroutine Dispatch ers Rule Ċ import Ġcom . mil ky head . app . android trie search sample . domain . model . Person Ċ import Ġcom . mil ky head . app . android trie search sample . domain . repository . Person Loader Repository Ċ import Ġcom . mil ky head . app . android trie search sample . domain . repository . Person Repository Ċ import Ġcom . mil ky head . app . android trie search sample . domain . use case .* Ċ import Ġcom . mil ky head . app . android trie search sample . ui . event . Pers ons Event Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġkotlinx . coroutines . test . run Blocking Test Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . mockito . Mockito Ċ import Ġorg . mockito . Mockito .` when ` Ċ import Ġorg . mockito . kotlin . any Ċ import Ġorg . mockito . kotlin . times Ċ import Ġorg . mockito . kotlin . verify Ċ Ċ @ Experimental Cor outines Api Ċ class ĠPers ons ViewModel Test Ġ{ ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġperson Loader Repository : ĠPerson Loader Repository ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġperson Repository : ĠPerson Repository ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġload Pers ons UseCase : ĠLoad Pers ons UseCase ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġclear All UseCase : ĠClear All UseCase ĊĠĠĠ Ġprivate Ġlateinit Ġvar ĠgetAll Person UseCase : ĠGetAll Person UseCase ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġinsert Pers ons UseCase : ĠInsert Pers ons UseCase ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġsearch Person UseCase : ĠSearch Person UseCase ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġpersons ViewModel : ĠPers ons ViewModel ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġcoroutine Rule Ġ= ĠCor outine Dispatch ers Rule () ĊĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ Ġperson Loader Repository Ġ= ĠMockito . mock ( Person Loader Repository :: class . java ) ĊĠĠĠĠĠĠĠ Ġperson Repository Ġ= ĠMockito . mock ( Person Repository :: class . java ) ĊĊĠĠĠĠĠĠĠ Ġload Pers ons UseCase Ġ= ĠLoad Pers ons UseCase ( person Loader Repository ) ĊĠĠĠĠĠĠĠ Ġclear All UseCase Ġ= ĠClear All UseCase ( person Repository ) ĊĠĠĠĠĠĠĠ ĠgetAll Person UseCase Ġ= ĠGetAll Person UseCase ( person Repository ) ĊĠĠĠĠĠĠĠ Ġinsert Pers ons UseCase Ġ= ĠInsert Pers ons UseCase ( person Repository ) ĊĠĠĠĠĠĠĠ Ġsearch Person UseCase Ġ= ĠSearch Person UseCase ( person Repository ) ĊĊĠĠĠĠĠĠĠ Ġpersons ViewModel Ġ= ĠPers ons ViewModel ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġload Pers ons UseCase , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinsert Pers ons UseCase , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠgetAll Person UseCase , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsearch Person UseCase , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġclear All UseCase , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcoroutine Rule . dispatcher ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` load Ġperson Ġon Ġstart Ġview Ġmodel Ġmust Ġsucceed ` (): ĠUnit Ġ= Ġcoroutine Rule . run Blocking Test Ġ{ ĊĊĠĠĠĠĠĠĠ Ġval Ġdata Ġ= Ġarray ListOf ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠPerson (" T ito ", Ġ" K ling ", Ġ 2 6 ), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠPerson (" Kar ina ", Ġ" Gold ner ", Ġ 2 1 ), ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġ` when `( person Loader Repository . load ()). thenReturn ( data ) ĊĊĠĠĠĠĠĠĠ Ġ` when `( person Repository . insert ( any ())). thenReturn ( true ) ĊĊĠĠĠĠĠĠĠ Ġ` when `( person Repository . getAll ()). thenReturn ( data ) ĊĊĠĠĠĠĠĠĠ Ġpersons ViewModel . onStart () ĊĊĠĠĠĠĠĠĠ Ġverify ( person Loader Repository , Ġtimes ( 1 )). load () ĊĠĠĠĠĠĠĠ Ġverify ( person Repository , Ġtimes ( 1 )). insert ( data ) ĊĠĠĠĠĠĠĠ Ġverify ( person Repository , Ġtimes ( 1 )). getAll () ĊĊĠĠĠĠĠĠĠ ĠassertThat ( persons ViewModel . state . value . persons . size ). isEqualTo ( 2 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( persons ViewModel . state . value . loading ). isFalse () ĊĠĠĠĠĠĠĠ ĠassertThat ( persons ViewModel . state . value . empty View ). isFalse () ĊĠĠĠĠĠĠĠ ĠassertThat ( persons ViewModel . state . value . retry ). isFalse () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` search Ġperson Ġmust Ġupdate Ġstate Ġsucceed ` (): ĠUnit Ġ= Ġcoroutine Rule . run Blocking Test Ġ{ ĊĊĠĠĠĠĠĠĠ Ġ` when `( person Repository . search ( any ())). thenReturn ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġarray ListOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠPerson (" T ito ", Ġ" K ling ", Ġ 2 6 ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġpersons ViewModel . event ( Pers ons Event . Search (" Ti ")) ĊĊĠĠĠĠĠĠĠ Ġverify ( person Repository , Ġtimes ( 1 )). search (" Ti ") ĊĊĠĠĠĠĠĠĠ ĠassertThat ( persons ViewModel . state . value . persons . size ). isEqualTo ( 1 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( persons ViewModel . state . value . persons [ 0 ]). isEqualTo ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠPerson (" T ito ", Ġ" K ling ", Ġ 2 6 ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ ĠassertThat ( persons ViewModel . state . value . loading ). isFalse () ĊĠĠĠĠĠĠĠ ĠassertThat ( persons ViewModel . state . value . empty View ). isFalse () ĊĠĠĠĠĠĠĠ ĠassertThat ( persons ViewModel . state . value . retry ). isFalse () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` clear Ġall Ġdata Ġwhen Ġview Ġmodel Ġstop ` (): ĠUnit Ġ= Ġcoroutine Rule . run Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġpersons ViewModel . on Stop () ĊĠĠĠĠĠĠĠ Ġverify ( person Repository , Ġtimes ( 1 )). clear () ĊĠĠĠ Ġ} Ċ }