package Ġcom . kir ak ish ou . photo exchange . helper . database . repository Ċ Ċ import Ġandroidx . room . Room Ċ import Ġandroid . content . Context Ċ import Ġandroidx . test . Instrumentation Registry Ċ import Ġandroidx . test . runner . Android JUnit 4 Ċ import Ġcom . kir ak ish ou . photo exchange . helper . concurrency . coroutines . Dispatch ers Provider Ċ import Ġcom . kir ak ish ou . photo exchange . helper . concurrency . coroutines . Mock Dispatch ers Ċ import Ġcom . kir ak ish ou . photo exchange . helper . database . My Database Ċ import Ġcom . kir ak ish ou . photo exchange . helper . util .* Ċ import Ġcom . kir ak ish ou . photo exchange . helper . database . source . local . Taken Photos Local Source Ċ import Ġcom . kir ak ish ou . photo exchange . helper . database . source . local . TempFile Local Source Ċ import Ġcom . kir ak ish ou . photo exchange . helper . extension . mb Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . wh enever Ċ import Ġkotlinx . coroutines . run Blocking Ċ import Ġorg . junit . After Ċ import Ġorg . junit . Assert .* Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . mockito . Mockito Ċ import Ġjava . io . File Ċ Ċ /** Ċ Ġ* ĠCreated Ġby Ġk ir ak ish ou Ġon Ġ 3 / 1 0 / 2 0 1 8 . Ċ Ġ*/ ĊĊ Ċ @ RunWith ( Android JUnit 4 :: class ) Ċ class ĠT aken Photos Repository Tests Ġ{ ĊĊĠ Ġlateinit Ġvar Ġapp Context : ĠContext ĊĠ Ġlateinit Ġvar Ġtarget Context : ĠContext ĊĠ Ġlateinit Ġvar Ġdatabase : ĠMy Database ĊĠ Ġlateinit Ġvar Ġtime Utils : ĠTime Utils ĊĠ Ġlateinit Ġvar Ġtemp Files Dir : ĠString ĊĠ Ġlateinit Ġvar Ġfile Utils : ĠFileUtils ĊĠ Ġlateinit Ġvar Ġdispatch ers Provider : ĠDispatch ers Provider ĊĊĠ Ġlateinit Ġvar Ġtaken Photos Local Source : ĠT aken Photos Local Source ĊĠ Ġlateinit Ġvar Ġtemp Files Local Source : ĠTemp File Local Source ĊĠ Ġlateinit Ġvar Ġtaken Photos Repository : ĠT aken Photos Repository ĊĊĠ Ġ@ Before ĊĠ Ġfun Ġinit () Ġ{ ĊĠĠĠ Ġapp Context Ġ= ĠInstrument ation Registry . getContext () ĊĠĠĠ Ġtarget Context Ġ= ĠInstrument ation Registry . getTarget Context () ĊĠĠĠ Ġdatabase Ġ= ĠRoom . in Memory Database Builder ( app Context , ĠMy Database :: class . java ). build () ĊĠĠĠ Ġtime Utils Ġ= ĠMockito . spy ( Time Utils :: class . java ) ĊĠĠĠ Ġfile Utils Ġ= ĠMockito . spy ( FileUtils :: class . java ) ĊĠĠĠ Ġtemp Files Dir Ġ= Ġtarget Context . get Dir (" test _ temp _ files ", ĠContext . MODE _ PRIVATE ). absolute Path ĊĠĠĠ Ġdispatch ers Provider Ġ= ĠMock Dispatch ers () ĊĊĠĠĠ Ġtaken Photos Local Source Ġ= ĠMockito . spy ( Taken Photos Local Source ( database , Ġtime Utils )) ĊĠĠĠ Ġtemp Files Local Source Ġ= ĠMockito . spy ( ĊĠĠĠĠĠ ĠTemp File Local Source ( ĊĠĠĠĠĠĠĠ Ġdatabase , ĊĠĠĠĠĠĠĠ Ġtemp Files Dir , ĊĠĠĠĠĠĠĠ Ġtime Utils , ĊĠĠĠĠĠĠĠ Ġfile Utils , ĊĠĠĠĠĠĠĠĠ 5 0 . mb (), ĊĠĠĠĠĠĠĠĠ 5 0 L , ĊĠĠĠĠĠĠĠĠ 7 ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġtaken Photos Repository Ġ= ĠT aken Photos Repository ( ĊĠĠĠĠĠ Ġtime Utils , ĊĠĠĠĠĠ Ġdatabase , ĊĠĠĠĠĠ Ġtaken Photos Local Source , ĊĠĠĠĠĠ Ġtemp Files Local Source ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ After ĊĠ Ġfun ĠtearDown () Ġ{ ĊĠĠĠ ĠFileUtils Impl (). delete All Files ( File ( temp Files Dir )) ĊĊĠĠĠ Ġdatabase . close () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġshould _ save _ taken _ photo _ should _ be _ able _ to _ find _ photo _ file _ by _ id () Ġ{ ĊĠĠĠ Ġrun Blocking Ġ{ ĊĠĠĠĠĠ Ġval Ġphoto File Ġ= Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġval Ġtaken Photo Ġ= Ġtaken Photos Repository . save Taken Photo ( photo File ) ĊĠĠĠĠĠ Ġval Ġtemp File Ġ= Ġtaken Photos Repository . find TempFile ( taken Photo !!. id ) ĊĊĠĠĠĠĠ ĠassertEquals ( false , Ġtemp File . isEmpty ()) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġshould _ delete _ photo _ file _ from _ disk _ when _ could _ not _ save _ temp _ file _ info _ in _ the _ database () Ġ{ ĊĠĠĠ Ġrun Blocking Ġ{ ĊĠĠĠĠĠ Ġval Ġtemp File Ġ= Ġtemp Files Local Source . create () ĊĠĠĠĠĠ ĠMockito .` when `( time Utils . getTime Fast ()). thenReturn ( 4 4 4 L ) ĊĠĠĠĠĠ ĠMockito .` when `( temp Files Local Source . update Taken Photo Id ( temp File , Ġ 1 )). thenReturn (- 1 ) ĊĊĠĠĠĠĠ Ġval Ġtaken Photo Ġ= Ġtaken Photos Repository . save Taken Photo ( temp File ) ĊĊĠĠĠĠĠ ĠassertEquals ( true , Ġtemp File . file Exists ()) ĊĠĠĠĠĠ ĠassertNull ( taken Photo ) ĊĊĠĠĠĠĠ Ġval Ġdeleted Files Ġ= Ġtemp Files Local Source . find Deleted Old ( Long . MAX _ VALUE ) ĊĠĠĠĠĠ ĠassertEquals ( 1 , Ġdeleted Files . size ) ĊĊĠĠĠĠĠ Ġtemp Files Local Source . delete Old ( Long . MAX _ VALUE ) ĊĠĠĠĠĠ ĠassertEquals ( true , Ġtemp Files Local Source . find Deleted Old ( Long . MAX _ VALUE ). isEmpty ()) ĊĠĠĠĠĠ ĠassertEquals ( false , Ġtemp File . as File (). exists ()) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġshould _ create _ files _ on _ disk () Ġ{ ĊĠĠĠ Ġrun Blocking Ġ{ ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĊĠĠĠĠĠ ĠassertEquals ( 4 , Ġtemp Files Local Source . findAll (). size ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġshould _ update _ entity _ deleted _ on _ field _ by _ id () Ġ{ ĊĠĠĠ Ġrun Blocking Ġ{ ĊĠĠĠĠĠ ĠMockito .` when `( time Utils . getTime Fast ()). thenReturn ( 1 2 3 L ) ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 1 ) ĊĠĠĠĠĠ Ġval Ġmarked Entity Ġ= Ġtemp Files Local Source . findAll (). first () ĊĊĠĠĠĠĠ ĠassertEquals ( 1 2 3 L , Ġmarked Entity . deleted On ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġshould _ find _ old _ files () Ġ{ ĊĠĠĠ Ġval Ġfirst Photo Deletion Time Ġ= Ġ 1 0 0 L ĊĠĠĠ Ġval Ġsecond Photo Deletion Time Ġ= Ġ 1 1 0 L ĊĠĠĠ Ġval Ġthird Photo Deletion Time Ġ= Ġ 1 6 0 L ĊĠĠĠ Ġval Ġfourth Photo Deletion Time Ġ= Ġ 1 9 0 L ĊĠĠĠ Ġval Ġdelete Before Time Ġ= Ġ 1 5 0 L ĊĊĠĠĠ ĠMockito .` when `( time Utils . getTime Fast ()). thenReturn ( ĊĠĠĠĠĠ Ġfirst Photo Deletion Time , ĊĠĠĠĠĠ Ġsecond Photo Deletion Time , ĊĠĠĠĠĠ Ġthird Photo Deletion Time , ĊĠĠĠĠĠ Ġfourth Photo Deletion Time ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġrun Blocking Ġ{ ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 1 ) ĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 2 ) ĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 3 ) ĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 4 ) ĊĊĠĠĠĠĠ Ġval Ġold Files Ġ= Ġtemp Files Local Source . find Deleted Old ( delete Before Time ) ĊĊĠĠĠĠĠ ĠassertEquals ( 2 , Ġold Files . size ) ĊĠĠĠĠĠ ĠassertEquals ( 1 , Ġold Files [ 0 ]. id !! ) ĊĠĠĠĠĠ ĠassertEquals ( 2 , Ġold Files [ 1 ]. id !! ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġshould _ delete _ old _ files () Ġ{ ĊĠĠĠ Ġval Ġfirst Photo Deletion Time Ġ= Ġ 1 0 0 L ĊĠĠĠ Ġval Ġsecond Photo Deletion Time Ġ= Ġ 1 1 0 L ĊĠĠĠ Ġval Ġthird Photo Deletion Time Ġ= Ġ 1 6 0 L ĊĠĠĠ Ġval Ġfourth Photo Deletion Time Ġ= Ġ 1 9 0 L ĊĠĠĠ Ġval Ġdelete Before Time Ġ= Ġ 1 5 0 L ĊĊĠĠĠ ĠMockito .` when `( time Utils . getTime Fast ()). thenReturn ( ĊĠĠĠĠĠ Ġfirst Photo Deletion Time , ĊĠĠĠĠĠ Ġsecond Photo Deletion Time , ĊĠĠĠĠĠ Ġthird Photo Deletion Time , ĊĠĠĠĠĠ Ġfourth Photo Deletion Time ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġrun Blocking Ġ{ ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 1 ) ĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 2 ) ĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 3 ) ĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 4 ) ĊĊĠĠĠĠĠ Ġtemp Files Local Source . delete Old ( delete Before Time ) ĊĊĠĠĠĠĠ ĠassertEquals ( 2 , Ġtemp Files Local Source . findAll (). size ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġshould _ remove _ empty _ temp _ files () Ġ{ ĊĠĠĠ Ġrun Blocking Ġ{ ĊĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠ Ġtemp Files Local Source . create () ĊĊĠĠĠĠĠ ĠassertEquals ( 4 , Ġtemp Files Local Source . findAll (). size ) ĊĠĠĠĠĠ Ġtemp Files Local Source . delete Empty Temp Files () ĊĊĠĠĠĠĠ ĠassertEquals ( 0 , Ġtemp Files Local Source . findAll (). size ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġshould _ find _ four _ ol dest _ deleted _ photos _ and _ delete _ them () Ġ{ ĊĠĠĠ ĠMockito .` when `( time Utils . getTime Fast ()). thenReturn ( ĊĠĠĠĠĠĠ 1 0 0 , Ġ 1 1 0 , Ġ 1 2 0 , Ġ 1 3 0 , Ġ 2 5 0 , Ġ 2 6 0 , Ġ 2 7 0 , Ġ 2 8 0 ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġrun Blocking Ġ{ ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp Files Local Source . create () ĊĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 1 ) ĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 2 ) ĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 3 ) ĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 4 ) ĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 5 ) ĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 6 ) ĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 7 ) ĊĠĠĠĠĠ Ġtemp Files Local Source . mark Deleted ById ( 8 ) ĊĊĠĠĠĠĠ Ġval Ġoldest Deleted Files Ġ= Ġtemp Files Local Source . find Ol dest ( 4 ) ĊĊĠĠĠĠĠ ĠassertEquals ( 4 , Ġoldest Deleted Files . size ) ĊĠĠĠĠĠ ĠassertEquals ( 1 0 0 , Ġoldest Deleted Files [ 0 ]. deleted On ) ĊĠĠĠĠĠ ĠassertEquals ( 1 1 0 , Ġoldest Deleted Files [ 1 ]. deleted On ) ĊĠĠĠĠĠ ĠassertEquals ( 1 2 0 , Ġoldest Deleted Files [ 2 ]. deleted On ) ĊĠĠĠĠĠ ĠassertEquals ( 1 3 0 , Ġoldest Deleted Files [ 3 ]. deleted On ) ĊĊĠĠĠĠĠ Ġtemp Files Local Source . delete Many ( ol dest Deleted Files ) ĊĊĠĠĠĠĠ Ġval Ġrest Of Files Ġ= Ġtemp Files Local Source . findAll () ĊĠĠĠĠĠ ĠassertEquals ( 2 5 0 , Ġrest Of Files [ 0 ]. deleted On ) ĊĠĠĠĠĠ ĠassertEquals ( 2 6 0 , Ġrest Of Files [ 1 ]. deleted On ) ĊĠĠĠĠĠ ĠassertEquals ( 2 7 0 , Ġrest Of Files [ 2 ]. deleted On ) ĊĠĠĠĠĠ ĠassertEquals ( 2 8 0 , Ġrest Of Files [ 3 ]. deleted On ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġshould _ correct ly _ calculate _ size _ of _ all _ files _ in _ a _ directory () Ġ{ ĊĠĠĠ Ġrun Blocking Ġ{ ĊĠĠĠĠĠ Ġval Ġtemp File 1 Ġ= Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp File 1 . as File (). write Text (" 1 2 3 4 5 6 7 8 9 0 ") ĊĊĠĠĠĠĠ Ġval Ġtemp File 2 Ġ= Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp File 2 . as File (). write Text (" 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 ") ĊĊĠĠĠĠĠ Ġval Ġtemp File 3 Ġ= Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp File 3 . as File (). write Text (" 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 ") ĊĊĠĠĠĠĠ Ġval Ġtemp File 4 Ġ= Ġtemp Files Local Source . create () ĊĠĠĠĠĠ Ġtemp File 4 . as File (). write Text (" 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 ") ĊĊĠĠĠĠĠ ĠassertEquals ( 1 0 0 , ĠFileUtils Impl (). calculate Total Directory Size ( File ( temp Files Dir ))) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest _ cleanup () Ġ{ ĊĠĠĠ Ġrun Blocking Ġ{ ĊĠĠĠĠĠ Ġwhenever ( time Utils . getTime Fast ()). thenReturn ( 1 0 0 , Ġ 1 0 1 , Ġ 1 0 2 , Ġ 1 0 3 , Ġ 2 0 0 , Ġ 2 0 1 , Ġ 2 0 2 , Ġ 2 0 3 , Ġ 4 4 4 ) ĊĊĠĠĠĠĠ Ġtaken Photos Repository . save Taken Photo ( temp Files Local Source . create ()) ĊĠĠĠĠĠ Ġtaken Photos Repository . save Taken Photo ( temp Files Local Source . create ()) ĊĠĠĠĠĠ Ġtaken Photos Repository . save Taken Photo ( temp Files Local Source . create ()) ĊĠĠĠĠĠ Ġtaken Photos Repository . save Taken Photo ( temp Files Local Source . create ()) ĊĊĠĠĠĠĠ Ġtaken Photos Repository . cleanup () ĊĊĠĠĠĠĠ ĠassertTrue ( taken Photos Local Source . findAll (). isEmpty ()) ĊĠĠĠĠĠ ĠassertTrue ( temp Files Local Source . findAll (). isEmpty ()) ĊĠĠĠĠĠ ĠassertTrue ( File ( temp Files Dir ). list (). isEmpty ()) ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } ĊĊĊĊĊĊĊĊĊĊĊĊĊĊĊĊ ĊĊĊĊĊĊĊĊ