package Ġdev . j yl ha . station . ui . stations Ċ Ċ import Ġandroid . location . Location Ċ import Ġcom . dropbox . android . external . store 4 . Response Origin Ċ import Ġcom . dropbox . android . external . store 4 . Store Response Ċ import Ġdev . j yl ha . station . data . location . Location Service Ċ import Ġdev . j yl ha . station . data . settings . Settings Repository Ċ import Ġdev . j yl ha . station . data . stations . Station Name Mapper Ċ import Ġdev . j yl ha . station . data . stations . Station Repository Ċ import Ġdev . j yl ha . station . model . Station Ċ import Ġdev . j yl ha . station . testutil . Main Coroutine Scope Rule Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġkotlinx . coroutines . channels . Channel Ċ import Ġkotlinx . coroutines . flow . consume As Flow Ċ import Ġkotlinx . coroutines . flow . flow Of Ċ import Ġkotlinx . coroutines . test . run Blocking Test Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . mockito . Mock Ċ import Ġorg . mockito . Mockito . mock Ċ import Ġorg . mockito . Mockito . verify Ċ import Ġorg . mockito . junit . Mockito JUnit Runner Ċ import Ġorg . mockito . Mockito .` when ` Ġas Ġwhen Called Ċ Ċ @ Opt In ( Experimental Cor outines Api :: class ) Ċ @ RunWith ( Mockito JUnit Runner :: class ) Ċ class ĠSt ations ViewModel Test Ġ{ ĊĊĠĠĠ Ġ@ get : Rule Ġval Ġcoroutine Rule : ĠMain Coroutine Scope Rule Ġ= ĠMain Coroutine Scope Rule () ĊĊĠĠĠ Ġ@ Mock Ġprivate Ġlateinit Ġvar Ġstation Repository : ĠStation Repository ĊĠĠĠ Ġ@ Mock Ġprivate Ġlateinit Ġvar Ġsettings Repository : ĠSettings Repository ĊĠĠĠ Ġ@ Mock Ġprivate Ġlateinit Ġvar Ġlocation Service : ĠLocation Service ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar ĠviewModel : ĠSt ations ViewModel ĊĊĠĠĠ Ġprivate Ġval Ġtest Mapper Ġ= ĠStation Name Mapper Ġ{ Ġstation Code Ġ-> ĊĠĠĠĠĠĠĠ Ġwhen Ġ( station Code ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠ 1 Ġ-> Ġ" Hel sink i " ĊĠĠĠĠĠĠĠĠĠĠĠ Ġelse Ġ-> Ġnull ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠĠĠ Ġprivate Ġval Ġtest St ations Ġ= ĠlistOf ( ĊĠĠĠĠĠĠĠ ĠStation (" Hel sink i ", Ġ" HK I ", Ġ 1 , Ġ 5 0 . 0 , Ġ 5 0 . 0 ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġ@ Before Ġfun Ġsetup () Ġ= Ġcoroutine Rule . run Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġwhen Called ( station Repository . get Station Name Mapper ()). thenReturn ( test Mapper ) ĊĠĠĠĠĠĠĠ Ġwhen Called ( station Repository . fetch St ations ()). thenReturn ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġflow Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠStore Response . Data ( test St ations , Ġorigin Ġ= ĠResponse Origin . Cache ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġwhen Called ( settings Repository . recent St ations ()). thenReturn ( flow Of ( emptyList ())) ĊĊĠĠĠĠĠĠĠ ĠviewModel Ġ= ĠSt ations ViewModel ( station Repository , Ġsettings Repository , Ġlocation Service ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test Ġfun Ġ` initialize Ġview Ġmodel `() Ġ= Ġcoroutine Rule . run Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġexpected Ġ= ĠSt ations ViewState ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġstations Ġ= Ġtest St ations , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġrecent St ations Ġ= Ġempty List (), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġname Mapper Ġ= Ġtest Mapper , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠisLoading St ations Ġ= Ġfalse , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠisLoading Name Mapper Ġ= Ġfalse , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġis Re loading St ations Ġ= Ġfalse , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġselect Nearest Ġ= Ġfalse , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġis Fetching Location Ġ= Ġfalse , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlongitude Ġ= Ġnull , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlatitude Ġ= Ġnull , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġnearest Station Ġ= Ġnull , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠerrorMessage Ġ= Ġnull ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġresult Ġ= ĠviewModel . state . value ĊĠĠĠĠĠĠĠ ĠassertThat ( result ). isEqualTo ( expected ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test Ġfun Ġ` select Ġnearest Ġstation `() Ġ= Ġcoroutine Rule . run Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġlocation Channel Ġ= ĠChannel < Location >() ĊĠĠĠĠĠĠĠ Ġwhen Called ( location Service . current Location ()). thenReturn ( location Channel . consume As Flow ()) ĊĠĠĠĠĠĠĠ ĠviewModel . setSelection Mode ( select Nearest Station Ġ= Ġtrue ) ĊĊĠĠĠĠĠĠĠ Ġwith ( viewModel . state . value ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( select Nearest ). isTrue () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( is Fetching Location ). isTrue () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( latitude ). isNull () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( longitude ). isNull () ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġlocation Ġ= Ġmock ( Location :: class . java ) ĊĠĠĠĠĠĠĠ Ġwhen Called ( location . latitude ). thenReturn ( 6 0 . 0 ) ĊĠĠĠĠĠĠĠ Ġwhen Called ( location . longitude ). thenReturn ( 7 0 . 0 ) ĊĠĠĠĠĠĠĠ Ġlocation Channel . send ( location ) ĊĊĠĠĠĠĠĠĠ Ġwith ( viewModel . state . value ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( is Fetching Location ). isFalse () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( latitude ). isEqualTo ( 6 0 . 0 ) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( longitude ). isEqualTo ( 7 0 . 0 ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test Ġfun Ġ` set Ġselected Ġstation `() Ġ= Ġcoroutine Rule . run Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġstation Ġ= Ġtest St ations . first () ĊĠĠĠĠĠĠĠ ĠviewModel . station Selected ( station ) ĊĠĠĠĠĠĠĠ Ġverify ( settings Repository ). set Station ( station . code ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test Ġfun Ġ` change Ġmode Ġback Ġto Ġselecting Ġstation Ġfrom Ġlist `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġlocation Channel Ġ= ĠChannel < Location >() ĊĠĠĠĠĠĠĠ Ġwhen Called ( location Service . current Location ()). thenReturn ( location Channel . consume As Flow ()) ĊĠĠĠĠĠĠĠ ĠviewModel . setSelection Mode ( select Nearest Station Ġ= Ġtrue ) ĊĊĠĠĠĠĠĠĠ Ġwith ( viewModel . state . value ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( select Nearest ). isTrue () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( is Fetching Location ). isTrue () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( latitude ). isNull () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( longitude ). isNull () ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ ĠviewModel . setSelection Mode ( select Nearest Station Ġ= Ġfalse ) ĊĊĠĠĠĠĠĠĠ Ġwith ( viewModel . state . value ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( select Nearest ). isFalse () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( is Fetching Location ). isFalse () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ } Ċ