package Ġco il . lifecycle Ċ Ċ import Ġandroidx . lifecycle . Lifecycle Ċ import Ġandroidx . lifecycle . Lifecycle Observer Ċ import Ġco il . util . await Started Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġkotlinx . coroutines . launch Ċ import Ġkotlinx . coroutines . test . Test Dispatcher Ċ import Ġkotlinx . coroutines . test . Un conf ined Test Dispatcher Ċ import Ġkotlinx . coroutines . test . run Test Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . ro bo lectric . Ro bo lectric TestRunner Ċ import Ġkotlin . test . assertEquals Ċ import Ġkotlin . test . assertFalse Ċ import Ġkotlin . test . assertTrue Ċ import Ġkotlin . test . fail Ċ Ċ @ Opt In ( Experimental Cor outines Api :: class ) Ċ @ RunWith ( Ro bo lectric TestRunner :: class ) Ċ class ĠL ifec yc les Test Ġ{ ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġtest Dispatcher : ĠTest Dispatcher ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġlifecycle : ĠFake Lifecycle Registry ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun Ġbefore () Ġ{ ĊĠĠĠĠĠĠĠ Ġtest Dispatcher Ġ= ĠUn conf ined Test Dispatcher () ĊĠĠĠĠĠĠĠ Ġlifecycle Ġ= ĠFake Lifecycle Registry () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` does Ġnot Ġobserve Ġif Ġalready Ġstarted `() Ġ= Ġrun Test ( test Dispatcher ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġlifecycle Ġ= Ġobject Ġ: ĠLifecycle () Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun ĠgetCurrent State () Ġ= ĠState . STARTED ĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun Ġadd Observer ( observer : ĠLifecycle Observer ) Ġ= Ġfail (" Should Ġnot Ġobserve .") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun Ġremove Observer ( observer : ĠLifecycle Observer ) Ġ= Ġfail (" Should Ġnot Ġobserve .") ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġlifecycle . await Started () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` dis patches Ġafter Ġstart Ġevent `() Ġ= Ġrun Test ( test Dispatcher ) Ġ{ ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 , Ġlifecycle . observer Count ) ĊĊĠĠĠĠĠĠĠ Ġval Ġjob Ġ= Ġlaunch Ġ{ Ġlifecycle . await Started () Ġ} ĊĊĠĠĠĠĠĠĠ ĠassertFalse ( job . is Completed ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( 1 , Ġlifecycle . observer Count ) ĊĊĠĠĠĠĠĠĠ Ġlifecycle . handle Lifecycle Event ( Lifecycle . Event . ON _ START ) ĊĊĠĠĠĠĠĠĠ ĠassertTrue ( job . is Completed ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 , Ġlifecycle . observer Count ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` observer Ġis Ġremoved Ġif Ġcancelled `() Ġ= Ġrun Test ( test Dispatcher ) Ġ{ ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 , Ġlifecycle . observer Count ) ĊĊĠĠĠĠĠĠĠ Ġval Ġjob Ġ= Ġlaunch Ġ{ Ġlifecycle . await Started () Ġ} ĊĊĠĠĠĠĠĠĠ ĠassertFalse ( job . is Completed ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( 1 , Ġlifecycle . observer Count ) ĊĊĠĠĠĠĠĠĠ Ġjob . cancel () ĊĊĠĠĠĠĠĠĠ ĠassertTrue ( job . is Completed ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 , Ġlifecycle . observer Count ) ĊĠĠĠ Ġ} Ċ } Ċ