package Ġcom . exp one a . sdk . telemetry Ċ Ċ import Ġandroid . content . Context Ċ import Ġandroidx . test . core . app . Application Provider Ċ import Ġcom . exp one a . sdk . telemetry . model . Crash Log Ċ import Ġcom . exp one a . sdk . telemetry . storage . File Telemetry Storage Ċ import Ġcom . exp one a . sdk . testutil . Exp one a SDK Test Ċ import Ġio . mock k . every Ċ import Ġio . mock k . mock k Constructor Ċ import Ġjava . io . File Ċ import Ġjava . util . Date Ċ import Ġkotlin . test . assertEquals Ċ import Ġkotlin . test . assertFalse Ċ import Ġkotlin . test . assertTrue Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . ro bo lectric . Ro bo lectric TestRunner Ċ Ċ @ RunWith ( Ro bo lectric TestRunner :: class ) Ċ internal Ġclass ĠFile Telemetry Storage Test Ġ: ĠExp one a SDK Test () Ġ{ ĊĠĠĠ Ġfun Ġget Mock Crash Log (): ĠCrash Log Ġ= ĠCrash Log ( Exception (" B oom ! "), Ġtrue , ĠDate (), ĠDate (), Ġ" mock - run - id ") ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` should Ġreturn Ġempty Ġresult Ġwithout Ġstored Ġcrash Ġlogs `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġstorage Ġ= ĠFile Telemetry Storage ( Application Provider . getApplicationContext ()) ĊĠĠĠĠĠĠĠ ĠassertEquals ( emptyList (), Ġstorage . getAll Crash Logs ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` should Ġnot Ġfail Ġdeleting Ġcrash Ġlog Ġthat 's Ġnot Ġsaved `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġstorage Ġ= ĠFile Telemetry Storage ( Application Provider . getApplicationContext ()) ĊĠĠĠĠĠĠĠ Ġstorage . delete Crash Log ( getMock Crash Log ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` should Ġsave - get - delete Ġcrash Ġlog `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġstorage Ġ= ĠFile Telemetry Storage ( Application Provider . getApplicationContext ()) ĊĠĠĠĠĠĠĠ Ġval Ġcrash Log Ġ= Ġget Mock Crash Log () ĊĠĠĠĠĠĠĠ Ġstorage . save Crash Log ( crash Log ) ĊĠĠĠĠĠĠĠ Ġval Ġcrash Logs Ġ= Ġstorage . getAll Crash Logs () ĊĠĠĠĠĠĠĠ ĠassertEquals ( 1 , Ġcrash Logs . size ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( crash Log , Ġcrash Logs [ 0 ]) ĊĠĠĠĠĠĠĠ Ġstorage . delete Crash Log ( crash Log ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( emptyList (), Ġstorage . getAll Crash Logs ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` should Ġsort Ġcrash Ġlogs `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġstorage Ġ= ĠFile Telemetry Storage ( Application Provider . getApplicationContext ()) ĊĠĠĠĠĠĠĠ Ġval Ġcrash Log 1 Ġ= Ġget Mock Crash Log () ĊĠĠĠĠĠĠĠ ĠThread . sleep ( 5 ) ĊĠĠĠĠĠĠĠ Ġval Ġcrash Log 2 Ġ= Ġget Mock Crash Log () ĊĠĠĠĠĠĠĠ ĠThread . sleep ( 5 ) ĊĠĠĠĠĠĠĠ Ġval Ġcrash Log 3 Ġ= Ġget Mock Crash Log () ĊĠĠĠĠĠĠĠ Ġstorage . save Crash Log ( crash Log 2 ) ĊĠĠĠĠĠĠĠ Ġstorage . save Crash Log ( crash Log 3 ) ĊĠĠĠĠĠĠĠ Ġstorage . save Crash Log ( crash Log 1 ) ĊĠĠĠĠĠĠĠ Ġval Ġcrash Logs Ġ= Ġstorage . getAll Crash Logs () ĊĠĠĠĠĠĠĠ ĠassertEquals ( 3 , Ġcrash Logs . size ) ĊĠĠĠĠĠĠĠ ĠassertTrue ( crash Logs [ 0 ]. timestamp MS Ġ< Ġcrash Logs [ 1 ]. timestamp MS ) ĊĠĠĠĠĠĠĠ ĠassertTrue ( crash Logs [ 1 ]. timestamp MS Ġ< Ġcrash Logs [ 2 ]. timestamp MS ) ĊĠĠĠĠĠĠĠ Ġstorage . delete Crash Log ( crash Log 1 ) ĊĠĠĠĠĠĠĠ Ġstorage . delete Crash Log ( crash Log 2 ) ĊĠĠĠĠĠĠĠ Ġstorage . delete Crash Log ( crash Log 3 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` should Ġnot Ġcrash Ġwhen Ġunable Ġto Ġcreate Ġstorage Ġfolder `() Ġ{ ĊĠĠĠĠĠĠĠ Ġmock k Constructor ( File Telemetry Storage :: class ) Ġ// ĠI 'm Ġnot Ġable Ġto Ġmock Ġfile , ĠI 'll Ġmock Ġthis Ġmethod ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġany Construct ed < File Telemetry Storage >() [" get Logs Directory "] () Ġ} Ġreturns Ġnull ĊĊĠĠĠĠĠĠĠ Ġval Ġstorage Ġ= ĠFile Telemetry Storage ( Application Provider . getApplicationContext ()) ĊĠĠĠĠĠĠĠ Ġstorage . save Crash Log ( getMock Crash Log ()) ĊĠĠĠĠĠĠĠ Ġstorage . delete Crash Log ( getMock Crash Log ()) ĊĠĠĠĠĠĠĠ ĠassertEquals ( array ListOf (), Ġstorage . getAll Crash Logs ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` should Ġnot Ġcrash Ġwhen Ġunable Ġto Ġcreating Ġstorage Ġfolder Ġthrows `() Ġ{ ĊĠĠĠĠĠĠĠ Ġmock k Constructor ( File Telemetry Storage :: class ) Ġ// ĠI 'm Ġnot Ġable Ġto Ġmock Ġfile , ĠI 'll Ġmock Ġthis Ġmethod ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġany Construct ed < File Telemetry Storage >() [" get Logs Directory "] () Ġ} Ġthrows ĠRuntimeException () ĊĊĠĠĠĠĠĠĠ Ġval Ġstorage Ġ= ĠFile Telemetry Storage ( Application Provider . getApplicationContext ()) ĊĠĠĠĠĠĠĠ Ġstorage . save Crash Log ( getMock Crash Log ()) ĊĠĠĠĠĠĠĠ Ġstorage . delete Crash Log ( getMock Crash Log ()) ĊĠĠĠĠĠĠĠ ĠassertEquals ( array ListOf (), Ġstorage . getAll Crash Logs ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` should Ġfilter Ġcrash Ġlogs Ġwhen Ġloading Ġfiles `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġstorage Ġ= ĠFile Telemetry Storage ( Application Provider . getApplicationContext ()) ĊĠĠĠĠĠĠĠ Ġstorage . save Crash Log ( getMock Crash Log ()) ĊĠĠĠĠĠĠĠ Ġval Ġmy Dir Ġ= ĠFile ( Application Provider . getApplicationContext < Context >(). cacheDir , ĠFile Telemetry Storage . DIRECTORY ) ĊĠĠĠĠĠĠĠ ĠFile ( my Dir , Ġ" some _ other _ file "). write Text (" mock Ġdata ") ĊĠĠĠĠĠĠĠ ĠassertEquals ( 1 , Ġstorage . getAll Crash Logs (). size ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` should Ġskip Ġand Ġdelete Ġfiles Ġwith Ġcorrupt Ġdata Ġwhen Ġloading Ġfiles `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġstorage Ġ= ĠFile Telemetry Storage ( Application Provider . getApplicationContext ()) ĊĠĠĠĠĠĠĠ Ġval Ġcrash Log 1 Ġ= Ġget Mock Crash Log () ĊĠĠĠĠĠĠĠ Ġstorage . save Crash Log ( crash Log 1 ) ĊĠĠĠĠĠĠĠ Ġval Ġcrash Log 2 Ġ= Ġget Mock Crash Log () ĊĠĠĠĠĠĠĠ Ġstorage . save Crash Log ( crash Log 2 ) ĊĠĠĠĠĠĠĠ Ġval Ġmy Dir Ġ= ĠFile ( Application Provider . getApplicationContext < Context >(). cacheDir , ĠFile Telemetry Storage . DIRECTORY ) ĊĠĠĠĠĠĠĠ ĠFile ( my Dir , Ġstorage . get FileName ( crash Log 2 )). write Text ("{ Ġcorrupt Data :") ĊĠĠĠĠĠĠĠ ĠassertEquals ( 1 , Ġstorage . getAll Crash Logs (). size ) ĊĠĠĠĠĠĠĠ ĠassertFalse ( File ( my Dir , Ġstorage . get FileName ( crash Log 2 )). exists ()) ĊĠĠĠ Ġ} Ċ } Ċ