package Ġcom . example . mul ats ch ak tracker Ċ Ċ import Ġandroid . content . Context Ċ import Ġandroidx . appcompat . app . AppCompatActivity Ċ import Ġandroidx . test . core . app . Activity Scenario Ċ import Ġandroidx . test . core . app . Application Provider Ċ import Ġandroidx . test . espress o . Esp ress o . on View Ċ import Ġandroidx . test . espress o . action . View Actions . click Ċ import Ġandroidx . test . espress o . assertion . View Assertions . matches Ċ import Ġandroidx . test . espress o . matcher . Root Matchers Ċ import Ġandroidx . test . espress o . matcher . View Matchers .* Ċ import Ġandroidx . test . ext . junit . runners . Android JUnit 4 Ċ import Ġjunit . framework . TestCase Ċ import Ġorg . junit . After Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ Ċ val ĠTEST USER Ġ= Ġ" New User " Ċ Ċ @ RunWith ( Android JUnit 4 :: class ) Ċ class ĠDelete User Test Ġ: ĠTestCase () Ġ{ ĊĠĠĠ Ġlateinit Ġvar Ġscenario : ĠActivity Scenario < MainActivity > ĊĠĠĠ Ġlateinit Ġvar Ġapp Context : ĠContext ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġpublic Ġoverride Ġfun ĠsetUp (){ ĊĠĠĠĠĠĠĠ Ġsuper . setUp () ĊĠĠĠĠĠĠĠ Ġapp Context Ġ= ĠApplication Provider . getApplicationContext () ĊĠĠĠĠĠĠĠ Ġval Ġuser Repo Ġ= ĠUser Repository ( app Context ) ĊĠĠĠĠĠĠĠ Ġuser Repo . reset Database () ĊĠĠĠĠĠĠĠ Ġuser Repo . createUser ( User Object ( TEST USER )) ĊĠĠĠĠĠĠĠ Ġval Ġpreferences Ġ= Ġapp Context . get SharedPreferences ( PREFER ENC ENAME , ĠAppCompatActivity . MODE _ PRIVATE ) ĊĠĠĠĠĠĠĠ Ġpreferences . edit (). putString ( LAST USER , ĠTEST USER ). commit () ĊĠĠĠĠĠĠĠ Ġscenario Ġ= ĠActivity Scenario . launch ( MainActivity :: class . java ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ After ĊĠĠĠ Ġpublic Ġoverride Ġfun ĠtearDown (){ ĊĠĠĠĠĠĠĠ Ġsuper . te ar Down () ĊĠĠĠĠĠĠĠ Ġscenario . close () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġdelete Button Present (){ ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . navigation _ Options )). perform ( click ()) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . Delete User Button )). check ( matches ( is Displayed ())) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġcheck Confirm Dialog (){ ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . navigation _ Options )). perform ( click ()) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . Delete User Button )). perform ( click ()) ĊĠĠĠĠĠĠĠ Ġon View ( with Text ( R . string . delete _ user _ button )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. in Root ( Root Matchers . is Dialog ()) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. check ( matches ( is Displayed ())) ĊĠĠĠĠĠĠĠ Ġon View ( with Text ( R . string . alert _ delete _ user )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. in Root ( Root Matchers . is Dialog ()) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. check ( matches ( is Displayed ())) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ( expected Ġ= ĠException :: class ) ĊĠĠĠ Ġfun Ġcheck Confirm Dialog Click Yes (){ ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . navigation _ Options )). perform ( click ()) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . Delete User Button )). perform ( click ()) ĊĠĠĠĠĠĠĠ Ġon View ( with Text ( R . string . yes )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. in Root ( Root Matchers . is Dialog ()) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. check ( matches ( is Displayed ())) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. perform ( click ()) ĊĠĠĠĠĠĠĠ Ġval Ġuser Repo Ġ= ĠUser Repository ( app Context ) ĊĠĠĠĠĠĠĠ Ġval Ġuser Ġ= Ġuser Repo . getUser ( TEST USER ) ĊĠĠĠĠĠĠĠ Ġassert ( false ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġcheck Confirm Dialog Click Cancel (){ ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . navigation _ Options )). perform ( click ()) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . Delete User Button )). perform ( click ()) ĊĠĠĠĠĠĠĠ Ġon View ( with Text ( R . string . cancel )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. in Root ( Root Matchers . is Dialog ()) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. check ( matches ( is Displayed ())) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. perform ( click ()) ĊĠĠĠĠĠĠĠ Ġval Ġuser Repo Ġ= ĠUser Repository ( app Context ) ĊĠĠĠĠĠĠĠ Ġval Ġuser Ġ= Ġuser Repo . getUser ( TEST USER ) ĊĠĠĠĠĠĠĠ Ġassert ( user . id Ġ> Ġ 0 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġcheck Confirm Dialog Navigate (){ ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . navigation _ Options )). perform ( click ()) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . Delete User Button )). perform ( click ()) ĊĠĠĠĠĠĠĠ Ġon View ( with Text ( R . string . yes )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. in Root ( Root Matchers . is Dialog ()) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. check ( matches ( is Displayed ())) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. perform ( click ()) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . Submit UserName Button )). check ( matches ( is Displayed ())) ĊĠĠĠ Ġ} Ċ } Ċ