package Ġg q . kir man ak . meal ient . data . recipes . impl Ċ Ċ import Ġandroidx . paging .* Ċ import Ġandroidx . paging . Load Type .* Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġg q . kir man ak . meal ient . data . network . Network Error . Unauthorized Ċ import Ġg q . kir man ak . meal ient . data . recipes . db . Recipe Storage Ċ import Ġg q . kir man ak . meal ient . data . recipes . db . entity . Recipe Summary Entity Ċ import Ġg q . kir man ak . meal ient . data . recipes . network . Recipe DataSource Ċ import Ġg q . kir man ak . meal ient . test . Recipe Impl TestData . TEST _ RE CI PE _ SUM MAR IES Ċ import Ġio . mock k . Mock K Annotations Ċ import Ġio . mock k . co Every Ċ import Ġio . mock k . co Verify Ċ import Ġio . mock k . impl . annotations . Mock K Ċ import Ġio . mock k . verify Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġkotlinx . coroutines . test . run Test Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ Ċ @ Experimental Cor outines Api Ċ @ Opt In ( Experimental Paging Api :: class ) Ċ class ĠRe cipes Remote Mediator Test Ġ{ ĊĠĠĠ Ġprivate Ġval Ġpaging Config Ġ= ĠP aging Config ( ĊĠĠĠĠĠĠĠ ĠpageSize Ġ= Ġ 2 , ĊĠĠĠĠĠĠĠ Ġprefetch Distance Ġ= Ġ 5 , ĊĠĠĠĠĠĠĠ Ġenable Place holders Ġ= Ġfalse ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġlateinit Ġvar Ġsubject : ĠRe cipes Remote Mediator ĊĊĠĠĠ Ġ@ Mock K ( relax Unit Fun Ġ= Ġtrue ) ĊĠĠĠ Ġlateinit Ġvar Ġstorage : ĠRecipe Storage ĊĊĠĠĠ Ġ@ Mock K ĊĠĠĠ Ġlateinit Ġvar ĠdataSource : ĠRecipe DataSource ĊĊĠĠĠ Ġ@ Mock K ( relax Unit Fun Ġ= Ġtrue ) ĊĠĠĠ Ġlateinit Ġvar Ġpaging Source Factory : ĠInvalid ating Paging Source Factory < Int , ĠRecipe Summary Entity > ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ ĠMock K Annotations . init ( this ) ĊĠĠĠĠĠĠĠ Ġsubject Ġ= ĠRe cipes Remote Mediator ( storage , ĠdataSource , Ġpaging Source Factory ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġfirst Ġload Ġwith Ġrefresh Ġsuccessful Ġthen Ġresult Ġsuccess `() Ġ= Ġrun Test Ġ{ ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ ĠdataSource . request Recipes ( eq ( 0 ), Ġeq ( 6 )) Ġ} Ġreturns ĠTEST _ RE CI PE _ SUM MAR IES ĊĠĠĠĠĠĠĠ Ġval Ġresult Ġ= Ġsubject . load ( REFRESH , Ġpaging State ()) ĊĠĠĠĠĠĠĠ ĠassertThat ( result ). isInstanceOf ( Remote Mediator . Mediator Result . Success :: class . java ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġfirst Ġload Ġwith Ġrefresh Ġsuccessful Ġthen Ġend Ġis Ġreached `() Ġ= Ġrun Test Ġ{ ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ ĠdataSource . request Recipes ( eq ( 0 ), Ġeq ( 6 )) Ġ} Ġreturns ĠTEST _ RE CI PE _ SUM MAR IES ĊĠĠĠĠĠĠĠ Ġval Ġresult Ġ= Ġsubject . load ( REFRESH , Ġpaging State ()) ĊĠĠĠĠĠĠĠ ĠassertThat (( result Ġas ĠRemote Mediator . Mediator Result . Success ). end Of Pagination Reached ). isTrue () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġfirst Ġload Ġwith Ġrefresh Ġsuccessful Ġthen Ġinvalidate Ġcalled `() Ġ= Ġrun Test Ġ{ ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ ĠdataSource . request Recipes ( any (), Ġany ()) Ġ} Ġreturns ĠTEST _ RE CI PE _ SUM MAR IES ĊĠĠĠĠĠĠĠ Ġsubject . load ( REFRESH , Ġpaging State ()) ĊĠĠĠĠĠĠĠ Ġverify Ġ{ Ġpaging Source Factory . invalidate () Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġfirst Ġload Ġwith Ġrefresh Ġsuccessful Ġthen Ġrecipes Ġstored `() Ġ= Ġrun Test Ġ{ ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ ĠdataSource . request Recipes ( eq ( 0 ), Ġeq ( 6 )) Ġ} Ġreturns ĠTEST _ RE CI PE _ SUM MAR IES ĊĠĠĠĠĠĠĠ Ġsubject . load ( REFRESH , Ġpaging State ()) ĊĠĠĠĠĠĠĠ Ġco Verify Ġ{ Ġstorage . refresh All ( eq ( TEST _ RE CI PE _ SUM MAR IES )) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġload Ġstate Ġprepend Ġthen Ġsuccess `() Ġ= Ġrun Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġresult Ġ= Ġsubject . load ( PRE PEND , Ġpaging State ()) ĊĠĠĠĠĠĠĠ ĠassertThat ( result ). isInstanceOf ( Remote Mediator . Mediator Result . Success :: class . java ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġload Ġstate Ġprepend Ġthen Ġend Ġis Ġreached `() Ġ= Ġrun Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġresult Ġ= Ġsubject . load ( PRE PEND , Ġpaging State ()) ĊĠĠĠĠĠĠĠ ĠassertThat (( result Ġas ĠRemote Mediator . Mediator Result . Success ). end Of Pagination Reached ). isTrue () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġload Ġsuccessful Ġthen Ġlast Request End Ġupdated `() Ġ= Ġrun Test Ġ{ ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ ĠdataSource . request Recipes ( eq ( 0 ), Ġeq ( 6 )) Ġ} Ġreturns ĠTEST _ RE CI PE _ SUM MAR IES ĊĠĠĠĠĠĠĠ Ġsubject . load ( REFRESH , Ġpaging State ()) ĊĠĠĠĠĠĠĠ Ġval Ġactual Ġ= Ġsubject . last Request End ĊĠĠĠĠĠĠĠ ĠassertThat ( actual ). isEqualTo ( 2 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġload Ġfails Ġthen Ġlast Request End Ġstill Ġ 0 `() Ġ= Ġrun Test Ġ{ ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ ĠdataSource . request Recipes ( eq ( 0 ), Ġeq ( 6 )) Ġ} Ġthrows ĠUnauthorized ( RuntimeException ()) ĊĠĠĠĠĠĠĠ Ġsubject . load ( REFRESH , Ġpaging State ()) ĊĠĠĠĠĠĠĠ Ġval Ġactual Ġ= Ġsubject . last Request End ĊĠĠĠĠĠĠĠ ĠassertThat ( actual ). isEqualTo ( 0 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġload Ġfails Ġthen Ġresult Ġis Ġerror `() Ġ= Ġrun Test Ġ{ ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ ĠdataSource . request Recipes ( eq ( 0 ), Ġeq ( 6 )) Ġ} Ġthrows ĠUnauthorized ( RuntimeException ()) ĊĠĠĠĠĠĠĠ Ġval Ġactual Ġ= Ġsubject . load ( REFRESH , Ġpaging State ()) ĊĠĠĠĠĠĠĠ ĠassertThat ( actual ). isInstanceOf ( Remote Mediator . Mediator Result . Error :: class . java ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġrefresh Ġthen Ġrequest Ġparams Ġcorrect `() Ġ= Ġrun Test Ġ{ ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ ĠdataSource . request Recipes ( any (), Ġany ()) Ġ} Ġthrows ĠUnauthorized ( RuntimeException ()) ĊĠĠĠĠĠĠĠ Ġsubject . load ( REFRESH , Ġpaging State ()) ĊĠĠĠĠĠĠĠ Ġco Verify Ġ{ ĠdataSource . request Recipes ( eq ( 0 ), Ġeq ( 6 )) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġappend Ġthen Ġrequest Ġparams Ġcorrect `() Ġ= Ġrun Test Ġ{ ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ ĠdataSource . request Recipes ( any (), Ġany ()) Ġ} Ġreturns ĠTEST _ RE CI PE _ SUM MAR IES ĊĠĠĠĠĠĠĠ Ġsubject . load ( REFRESH , Ġpaging State ()) ĊĠĠĠĠĠĠĠ Ġsubject . load ( APPEND , Ġpaging State ()) ĊĠĠĠĠĠĠĠ Ġco Verify Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠdataSource . request Recipes ( eq ( 0 ), Ġeq ( 6 )) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠdataSource . request Recipes ( eq ( 2 ), Ġeq ( 2 )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġappend Ġfails Ġthen Ġrecipes Ġaren 't Ġremoved `() Ġ= Ġrun Test Ġ{ ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ ĠdataSource . request Recipes ( any (), Ġany ()) Ġ} Ġreturns ĠTEST _ RE CI PE _ SUM MAR IES ĊĠĠĠĠĠĠĠ Ġsubject . load ( REFRESH , Ġpaging State ()) ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ ĠdataSource . request Recipes ( any (), Ġany ()) Ġ} Ġthrows ĠUnauthorized ( RuntimeException ()) ĊĠĠĠĠĠĠĠ Ġsubject . load ( APPEND , Ġpaging State ()) ĊĠĠĠĠĠĠĠ Ġco Verify Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġstorage . refresh All ( TEST _ RE CI PE _ SUM MAR IES ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġpaging State ( ĊĠĠĠĠĠĠĠ Ġpages : ĠList < Paging Source . Load Result . Page < Int , ĠRecipe Summary Entity >> Ġ= Ġempty List (), ĊĠĠĠĠĠĠĠ Ġanchor Position : ĠInt ? Ġ= Ġnull ĊĠĠĠ Ġ): ĠP aging State < Int , ĠRecipe Summary Entity > Ġ= ĠP aging State ( ĊĠĠĠĠĠĠĠ Ġpages Ġ= Ġpages , ĊĠĠĠĠĠĠĠ Ġanchor Position Ġ= Ġanchor Position , ĊĠĠĠĠĠĠĠ Ġconfig Ġ= Ġpaging Config , ĊĠĠĠĠĠĠĠ Ġleading Placeholder Count Ġ= Ġ 0 ĊĠĠĠ Ġ) Ċ }