package Ġtech . ula . utils Ċ Ċ import Ġandroid . app . Download Manager Ċ import Ġandroid . database . Cursor Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . verify Ċ import Ġorg . junit . Assert .* Ġ// Ġk tl int - disable Ġno - wildcard - imports Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . rules . Temporary Folder Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . mockito . Argument Matchers Ċ import Ġorg . mockito . Mock Ċ import Ġorg . mockito . Mockito .* Ġ// Ġk tl int - disable Ġno - wildcard - imports Ċ import Ġorg . mockito . junit . Mockito JUnit Runner Ċ import Ġtech . ula . model . entities . Asset Ċ import Ġjava . io . File Ċ import Ġkotlin . text . Charsets . UTF _ 8 Ċ Ċ @ RunWith ( Mockito JUnit Runner :: class ) Ċ class ĠDownload Utility Test Ġ{ ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġtemp Folder Ġ= ĠTemporary Folder () ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġlateinit Ġvar Ġdownload Manager : ĠDownload Manager ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġlateinit Ġvar Ġtimestamp Preferences : ĠTimestamp Preferences ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġlateinit Ġvar Ġdownload Manager Wrapper : ĠDownload Manager Wrapper ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġlateinit Ġvar Ġrequest Return : ĠDownload Manager . Request ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġlateinit Ġvar Ġquery Return : ĠDownload Manager . Query ĊĊĠĠĠ Ġ@ Mock ĊĠĠĠ Ġlateinit Ġvar Ġcursor : ĠCursor ĊĊĠĠĠ Ġlateinit Ġvar Ġdownload Directory : ĠFile ĊĊĠĠĠ Ġlateinit Ġvar Ġasset 1 : ĠAsset ĊĠĠĠ Ġlateinit Ġvar Ġasset 2 : ĠAsset ĊĠĠĠ Ġlateinit Ġvar Ġasset List : ĠList < Asset > ĊĠĠĠ Ġlateinit Ġvar Ġdownload Utility : ĠDownload Utility ĊĊĠĠĠ Ġval Ġbranch Ġ= Ġ" master " ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun Ġsetup () Ġ{ ĊĠĠĠĠĠĠĠ Ġdownload Directory Ġ= Ġtemp Folder . new Folder (" downloads ") ĊĠĠĠĠĠĠĠ Ġ` when `( download Manager Wrapper . get Downloads Directory ()). thenReturn ( download Directory ) ĊĠĠĠĠĠĠĠ Ġdownload Utility Ġ= ĠDownload Utility ( download Manager , Ġtimestamp Preferences , Ġdownload Manager Wrapper , Ġapplication Files Dir Ġ= Ġtemp Folder . root ) ĊĊĠĠĠĠĠĠĠ Ġasset 1 Ġ= ĠAsset (" name 1 ", Ġ" dist Type 1 ", Ġ" arch Type 1 ", Ġ 0 ) ĊĠĠĠĠĠĠĠ Ġasset 2 Ġ= ĠAsset (" name 2 ", Ġ" dist Type 2 ", Ġ" arch Type 2 ", Ġ 0 ) ĊĠĠĠĠĠĠĠ Ġasset List Ġ= ĠlistOf ( asset 1 , Ġasset 2 ) ĊĊĠĠĠĠĠĠĠ Ġval Ġurl 1 Ġ= Ġget Download Url ( asset 1 . distribution Type , Ġasset 1 . architecture Type , Ġasset 1 . name ) ĊĠĠĠĠĠĠĠ Ġval Ġdestination 1 Ġ= Ġasset 1 . concat en ated Name ĊĠĠĠĠĠĠĠ Ġ` when `( download Manager Wrapper . generate Download Request ( url 1 , Ġdestination 1 )). thenReturn ( request Return ) ĊĊĠĠĠĠĠĠĠ Ġval Ġurl 2 Ġ= Ġget Download Url ( asset 2 . distribution Type , Ġasset 2 . architecture Type , Ġasset 2 . name ) ĊĠĠĠĠĠĠĠ Ġval Ġdestination 2 Ġ= Ġasset 2 . concat en ated Name ĊĠĠĠĠĠĠĠ Ġ` when `( download Manager Wrapper . generate Download Request ( url 2 , Ġdestination 2 )). thenReturn ( request Return ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġget Download Url ( dist Type : ĠString , Ġarch Type : ĠString , Ġname : ĠString ): ĠString Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġ" https :// github . com / Cy ph erp unk Arm ory / User L And - Assets -$ dist Type / raw /$ branch / assets /$ arch Type /$ name " ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġen queues Download () Ġ{ ĊĠĠĠĠĠĠĠ Ġdownload Utility . download Requirements ( asset List ) ĊĊĠĠĠĠĠĠĠ Ġverify ( download Manager , Ġtimes ( 2 )). enqueue ( any ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġdeletes Previous Downloads () Ġ{ ĊĠĠĠĠĠĠĠ Ġtemp Folder . new Folder (" dist Type 1 ") ĊĠĠĠĠĠĠĠ Ġtemp Folder . new Folder (" dist Type 2 ") ĊĠĠĠĠĠĠĠ Ġval Ġasset 1 File Ġ= ĠFile ("${ temp Folder . root . path }/ dist Type 1 / name 1 ") ĊĠĠĠĠĠĠĠ Ġval Ġasset 2 File Ġ= ĠFile ("${ temp Folder . root . path }/ dist Type 2 / name 2 ") ĊĠĠĠĠĠĠĠ Ġasset 1 File . createNew File () ĊĠĠĠĠĠĠĠ Ġasset 2 File . createNew File () ĊĠĠĠĠĠĠĠ ĠassertTrue ( asset 1 File . exists ()) ĊĠĠĠĠĠĠĠ ĠassertTrue ( asset 2 File . exists ()) ĊĊĠĠĠĠĠĠĠ Ġval Ġasset 1 Downloads File Ġ= ĠFile ("${ download Directory . path }/${ asset 1 . concat en ated Name }") ĊĠĠĠĠĠĠĠ Ġval Ġasset 2 Downloads File Ġ= ĠFile ("${ download Directory . path }/${ asset 2 . concat en ated Name }") ĊĠĠĠĠĠĠĠ Ġasset 1 Downloads File . createNew File () ĊĠĠĠĠĠĠĠ Ġasset 2 Downloads File . createNew File () ĊĠĠĠĠĠĠĠ ĠassertTrue ( asset 1 Downloads File . exists ()) ĊĠĠĠĠĠĠĠ ĠassertTrue ( asset 2 Downloads File . exists ()) ĊĊĠĠĠĠĠĠĠ Ġdownload Utility . download Requirements ( asset List ) ĊĠĠĠĠĠĠĠ Ġverify ( download Manager , Ġtimes ( 2 )). enqueue ( any ()) ĊĊĠĠĠĠĠĠĠ ĠassertFalse ( asset 1 File . exists ()) ĊĠĠĠĠĠĠĠ ĠassertFalse ( asset 2 File . exists ()) ĊĠĠĠĠĠĠĠ ĠassertFalse ( asset 1 Downloads File . exists ()) ĊĠĠĠĠĠĠĠ ĠassertFalse ( asset 2 Downloads File . exists ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġsets Timestamp When Title Is Re levant () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġid Ġ= Ġ 1 L ĊĠĠĠĠĠĠĠ Ġ` when `( download Manager Wrapper . generate Query ( id )). thenReturn ( query Return ) ĊĠĠĠĠĠĠĠ Ġ` when `( download Manager Wrapper . generate Cursor ( download Manager , Ġquery Return )). thenReturn ( cursor ) ĊĠĠĠĠĠĠĠ Ġ` when `( download Manager Wrapper . get Download Title ( cursor )). thenReturn ( asset 1 . concat en ated Name ) ĊĊĠĠĠĠĠĠĠ Ġdownload Utility . set Timestamp For Downloaded File ( id ) ĊĊĠĠĠĠĠĠĠ Ġverify ( timestamp Preferences ). set Saved Timestamp For File To Now ( asset 1 . concat en ated Name ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġignores Ir relevant Downloads () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġid Ġ= Ġ 1 L ĊĠĠĠĠĠĠĠ Ġval Ġtitle Name Ġ= Ġ" not user land " ĊĠĠĠĠĠĠĠ Ġ` when `( download Manager Wrapper . generate Query ( id )). thenReturn ( query Return ) ĊĠĠĠĠĠĠĠ Ġ` when `( download Manager Wrapper . generate Cursor ( download Manager , Ġquery Return )). thenReturn ( cursor ) ĊĠĠĠĠĠĠĠ Ġ` when `( download Manager Wrapper . get Download Title ( cursor )). thenReturn ( title Name ) ĊĊĠĠĠĠĠĠĠ Ġdownload Utility . set Timestamp For Downloaded File ( id ) ĊĊĠĠĠĠĠĠĠ Ġverify ( timestamp Preferences , Ġnever ()). set Saved Timestamp For File To Now ( Argument Matchers . any String ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġmoves Assets To Correct Location And Updates Permissions () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġasset 1 Downloads File Ġ= ĠFile ("${ download Directory . path }/${ asset 1 . concat en ated Name }") ĊĠĠĠĠĠĠĠ Ġval Ġasset 2 Downloads File Ġ= ĠFile ("${ download Directory . path }/${ asset 2 . concat en ated Name }") ĊĠĠĠĠĠĠĠ Ġasset 1 Downloads File . createNew File () ĊĠĠĠĠĠĠĠ Ġasset 2 Downloads File . createNew File () ĊĊĠĠĠĠĠĠĠ Ġval Ġasset 1 File Ġ= ĠFile ("${ temp Folder . root . path }/ dist Type 1 / name 1 ") ĊĠĠĠĠĠĠĠ Ġval Ġasset 2 File Ġ= ĠFile ("${ temp Folder . root . path }/ dist Type 2 / name 2 ") ĊĠĠĠĠĠĠĠ ĠassertFalse ( asset 1 File . exists ()) ĊĠĠĠĠĠĠĠ ĠassertFalse ( asset 2 File . exists ()) ĊĊĠĠĠĠĠĠĠ Ġdownload Utility . move Assets To Correct Local Directory () ĊĊĠĠĠĠĠĠĠ ĠassertFalse ( asset 1 Downloads File . exists ()) ĊĠĠĠĠĠĠĠ ĠassertFalse ( asset 2 Downloads File . exists ()) ĊĠĠĠĠĠĠĠ ĠassertTrue ( asset 1 File . exists ()) ĊĠĠĠĠĠĠĠ ĠassertTrue ( asset 2 File . exists ()) ĊĊĠĠĠĠĠĠĠ Ġvar Ġoutput Ġ= Ġ"" ĊĠĠĠĠĠĠĠ Ġval Ġproc 1 Ġ= ĠRuntime . get Runtime (). exec (" ls Ġ- l Ġ${ asset 1 File . path }") ĊĊĠĠĠĠĠĠĠ Ġproc 1 . inputStream . buffered Reader ( UTF _ 8 ). forEach Line Ġ{ Ġoutput Ġ+= Ġit Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġpermissions 1 Ġ= Ġoutput . substring ( 0 , Ġ 1 0 ) ĊĠĠĠĠĠĠĠ ĠassertTrue ( permissions 1 Ġ== Ġ"- rw x rw x rw x ") ĊĊĠĠĠĠĠĠĠ Ġoutput Ġ= Ġ"" ĊĠĠĠĠĠĠĠ Ġval Ġproc 2 Ġ= ĠRuntime . get Runtime (). exec (" ls Ġ- l Ġ${ asset 2 File . path }") ĊĊĠĠĠĠĠĠĠ Ġproc 2 . inputStream . buffered Reader ( UTF _ 8 ). forEach Line Ġ{ Ġoutput Ġ+= Ġit Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġpermissions 2 Ġ= Ġoutput . substring ( 0 , Ġ 1 0 ) ĊĠĠĠĠĠĠĠ ĠassertTrue ( permissions 2 Ġ== Ġ"- rw x rw x rw x ") ĊĠĠĠ Ġ} Ċ }