package Ġcom . example . mul ats ch ak tracker ĊĊ Ċ import Ġandroid . content . Context Ċ import Ġandroid . view . View Ċ import Ġandroid . widget . TextView Ċ import Ġandroidx . appcompat . app . AppCompatActivity Ċ import Ġandroidx . recyclerview . widget . RecyclerView Ċ 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 . Esp ress o . press Back Ċ import Ġandroidx . test . espress o . No Matching View Exception Ċ import Ġandroidx . test . espress o . Ui Controller Ċ import Ġandroidx . test . espress o . View Action Ċ import Ġandroidx . test . espress o . View Interaction Ċ import Ġandroidx . test . espress o . action . View Actions . click Ċ import Ġandroidx . test . espress o . action . View Actions . long Click Ċ import Ġandroidx . test . espress o . assertion . View Assertions . matches Ċ import Ġandroidx . test . espress o . contrib . RecyclerView Actions Ċ import Ġandroidx . test . espress o . matcher . View Matchers .* Ċ import Ġandroidx . test . ext . junit . runners . Android JUnit 4 Ċ import Ġjunit . framework . Assertion Failed Error Ċ import Ġjunit . framework . TestCase Ċ import Ġorg . hamcrest . Matcher Ċ import Ġorg . junit . After Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ Ċ @ RunWith ( Android JUnit 4 :: class ) Ċ class ĠHistory Fragment Test : TestCase () Ġ{ ĊĠĠĠ Ġlateinit Ġvar Ġuser Repo Ġ: ĠUser Repository ĊĠĠĠ Ġlateinit Ġvar Ġgame Repo Ġ: ĠGame Repository ĊĠĠĠ Ġlateinit Ġvar Ġscenario Ġ: ĠActivity Scenario < MainActivity > ĊĠĠĠ Ġval ĠuserName Ġ= Ġ" New User " ĊĠĠĠ Ġval Ġplayer Name Ġ= Ġ" Player Name " ĊĠĠĠ Ġ//@ get : Rule ĊĠĠĠ Ġ// var Ġactivity Rule : ĠActivity Scenario Rule < MainActivity > ĊĠĠĠ Ġ// ĠĠĠĠĠĠĠ Ġ= ĠActivity Scenario Rule ( MainActivity :: class . java ) ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġpublic Ġoverride Ġfun ĠsetUp (){ ĊĠĠĠĠĠĠĠ Ġsuper . setUp () ĊĠĠĠĠĠĠĠ Ġval Ġ Ġapp Context : ĠContext Ġ= ĠApplication Provider . getApplicationContext () ĊĊĠĠĠĠĠĠĠ Ġuser Repo Ġ= ĠUser Repository ( app Context ) ĊĠĠĠĠĠĠĠ Ġuser Repo . reset Database () ĊĊĠĠĠĠĠĠĠ Ġval ĠuserID Ġ= Ġuser Repo . createUser ( User Object ( userName )) ĊĠĠĠĠĠĠĠ Ġgame Repo Ġ= ĠGame Repository ( app Context ) ĊĠĠĠĠĠĠĠ Ġval ĠgameObject Ġ= ĠGameObject ( player Name , player Name , player Name , player Name ) ĊĠĠĠĠĠĠĠ ĠgameObject . finished Ġ= Ġ 1 ĊĠĠĠĠĠĠĠ Ġval Ġgame Id Ġ= Ġgame Repo . create Game ( gameObject , userID ) ĊĠĠĠĠĠĠĠ ĠgameObject . finished Ġ= Ġ 0 ĊĠĠĠĠĠĠĠ Ġgame Repo . create Game ( gameObject , userID ) ĊĠĠĠĠĠĠĠ Ġval Ġround Object Ġ= ĠRound Object ( 2 , Ġ 0 , Ġ- 1 Ġ, 3 , 0 Ġ, 0 ) ĊĠĠĠĠĠĠĠ Ġgame Repo . enter New Round ( round Object , Ġgame Id ) ĊĊĠĠĠĠĠĠĠ Ġval Ġpreferences Ġ= Ġapp Context . get SharedPreferences ( PREFER ENC ENAME , ĠAppCompatActivity . MODE _ PRIVATE ) ĊĠĠĠĠĠĠĠ Ġpreferences . edit (). putString ( LAST USER , ĠuserName ). commit () ĊĠĠĠĠĠĠĠ Ġscenario Ġ= ĠActivity Scenario . launch ( MainActivity :: class . java ) ĊĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . navigation _ History )). perform ( click ()) ĊĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ After ĊĠĠĠ Ġpublic Ġoverride Ġfun ĠtearDown () Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . te ar Down () ĊĠĠĠĠĠĠĠ Ġscenario . close () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġfinished Game Is Present In History Fragment (){ ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . game _ text view )). check ( matches ( is Displayed ())) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġcorrect Game Id Displayed In History Fragment (){ ĊĠĠĠĠĠĠĠ ĠassertEquals ( getText ( on View ( with Id ( R . id . game _ text view ))), " Game Ġ 1 ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġcorrect Player Names Are Displayed In History Fragment (){ ĊĠĠĠĠĠĠĠ ĠassertEquals ( getText ( on View ( with Id ( R . id . game _ text view _ players ))), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ" Player Name , ĠPlayer Name , ĠPlayer Name , ĠPlayer Name " ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġclick On Game Open s Game Activity (){ ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . history RecyclerView )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. perform ( RecyclerView Actions . action On Item At Position < RecyclerView . ViewHolder >( 0 , Ġclick ())) ĊĠĠĠĠĠĠĠ ĠassertEquals ( player Name , ĠgetText ( on View ( with Id ( R . id . textView Player 1 )))) ĊĠĠĠĠĠĠĠ ĠassertEquals ( player Name , ĠgetText ( on View ( with Id ( R . id . textView Player 2 )))) ĊĠĠĠĠĠĠĠ ĠassertEquals ( player Name , ĠgetText ( on View ( with Id ( R . id . textView Player 3 )))) ĊĠĠĠĠĠĠĠ ĠassertEquals ( player Name , ĠgetText ( on View ( with Id ( R . id . textView Player 4 )))) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġedit History Detail Fragment (){ ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . history RecyclerView )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. perform ( RecyclerView Actions . action On Item At Position < RecyclerView . ViewHolder >( 0 , Ġclick ())) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( 1 2 0 0 0 )). perform ( long Click ()) ĊĠĠĠĠĠĠĠ ĠThread . sleep ( 3 0 0 ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( player Name , ĠgetText ( on View ( with Id ( R . id . textView Player 1 )))) ĊĠĠĠĠĠĠĠ ĠassertEquals ( player Name , ĠgetText ( on View ( with Id ( R . id . textView Player 2 )))) ĊĠĠĠĠĠĠĠ ĠassertEquals ( player Name , ĠgetText ( on View ( with Id ( R . id . textView Player 3 )))) ĊĠĠĠĠĠĠĠ ĠassertEquals ( player Name , ĠgetText ( on View ( with Id ( R . id . textView Player 4 )))) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ( expected Ġ= ĠNo Matching View Exception :: class ) ĊĠĠĠ Ġfun Ġclose History Detail Fragment (){ ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . history RecyclerView )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. perform ( RecyclerView Actions . action On Item At Position < RecyclerView . ViewHolder >( 0 , Ġclick ())) ĊĠĠĠĠĠĠĠ Ġpress Back () ĊĠĠĠĠĠĠĠ ĠThread . sleep ( 2 0 0 ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( getText ( on View ( with Id ( R . id . game _ text view _ players ))), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ" Player Name , ĠPlayer Name , ĠPlayer Name , ĠPlayer Name " ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . Start New Game Activity Button )). check ( matches ( is Displayed ())) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ( expected Ġ= ĠAssertion Failed Error :: class ) ĊĠĠĠ Ġfun Ġcheck Add Round Button History Detail Fragment (){ ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . history RecyclerView )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. perform ( RecyclerView Actions . action On Item At Position < RecyclerView . ViewHolder >( 0 , Ġclick ())) ĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . Add Round Button )). check ( matches ( is Displayed ())) ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġ// helper Ġfunction Ġfor Ġcomparing Ġ 2 Ġstrings Ġfrom Ġtext boxes ĊĠĠĠ Ġfun ĠgetText ( matcher : ĠView Interaction ): ĠString Ġ{ ĊĠĠĠĠĠĠĠ Ġvar Ġtext Ġ= ĠString () ĊĠĠĠĠĠĠĠ Ġmatcher . perform ( object Ġ: ĠView Action Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun Ġget Constraints (): ĠMatcher < View > Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġis AssignableFrom ( TextView :: class . java ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun ĠgetDescription (): ĠString Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġ" Text Ġof Ġthe Ġview " ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun Ġperform ( ui Controller : ĠUi Controller , Ġview : ĠView ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġtv Ġ= Ġview Ġas ĠTextView ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġtext Ġ= Ġtv . text . toString () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ}) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġtext ĊĠĠĠ Ġ} ĊĊĊ Ċ } Ċ