package Ġgithub . inform ram iz . testing and ri od applications . statistics Ċ Ċ import Ġandroidx . arch . core . executor . testing . Instant Task Executor Rule Ċ import Ġgithub . inform ram iz . testing and ri od applications . data . Task Ċ import Ġgithub . inform ram iz . testing and ri od applications . data . source . Fake Tasks Repository Ċ import Ġgithub . inform ram iz . testing and ri od applications . util . Main Coroutine Rule Ċ import Ġgithub . inform ram iz . testing and ri od applications . util . get Or Await Value Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġorg . hamcrest . Core Matchers .` is ` Ċ import Ġorg . junit . Assert .* Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ Ċ /** Ċ Ġ* ĠCreated Ġby ĠRam iz ĠR aja Ġon Ġ 0 3 / 0 7 / 2 0 2 0 . Ċ Ġ*/ Ċ @ Experimental Cor outines Api Ċ class ĠStatistics ViewModel Test Ġ{ ĊĠĠĠ Ġ// ĠExec utes Ġeach Ġtask Ġsynchronous ly Ġusing ĠArchitecture ĠComponents . ĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġinstance Task Executor Rule Ġ= ĠInstant Task Executor Rule () ĊĊĠĠĠ Ġ// Ġset ĠTesting Coroutine Dispatcher Ġas ĠDispatcher . Main ĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġmain Coroutine Rule Ġ= ĠMain Coroutine Rule () ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġfake Tasks Repository : ĠFake Tasks Repository ĊĊĠĠĠ Ġ// ĠG IV EN : Ġsubject Ġunder Ġtest ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġstatistics ViewModel : ĠStatistics ViewModel ĊĠĠĠ Ġprivate Ġval Ġactive Task Ġ= ĠTask (" title 1 ", Ġ" description 1 ") ĊĠĠĠ Ġprivate Ġval Ġcompleted Task Ġ= ĠTask (" title 2 ", Ġ" description 2 ", Ġis Completed Ġ= Ġtrue ) ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun Ġsetup ViewModel () Ġ{ ĊĠĠĠĠĠĠĠ Ġfake Tasks Repository Ġ= ĠFake Tasks Repository () ĊĠĠĠĠĠĠĠ Ġstatistics ViewModel Ġ= ĠStatistics ViewModel ( fake Tasks Repository ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Stats _ zero Active Zero Complete () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// WHEN : ĠThere Ġare Ġno Ġtasks ĊĊĠĠĠĠĠĠĠ Ġ// THEN : Ġactive Ġtasks Ġ= Ġ 0 , Ġcompleted Ġtask Ġ= Ġ 0 ĊĠĠĠĠĠĠĠ Ġval Ġactive Tasks Ġ= Ġstatistics ViewModel . active Tasks Percent . get Or Await Value () ĊĠĠĠĠĠĠĠ Ġval Ġcompleted Tasks Ġ= Ġstatistics ViewModel . completed Tasks Percent . get Or Await Value () ĊĠĠĠĠĠĠĠ ĠassertThat ( active Tasks , Ġ` is `( 0 f )) ĊĠĠĠĠĠĠĠ ĠassertThat ( completed Tasks , Ġ` is `( 0 f )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Stats _ h undred Active Zero Complete () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// WHEN : ĠThere Ġare Ġare Ġ 1 0 0 % Ġactive Ġtasks , Ġ 0 % Ġcompleted Ġtasks ĊĠĠĠĠĠĠĠ Ġfake Tasks Repository . add Tasks ( active Task ) ĊĊĠĠĠĠĠĠĠ Ġ// THEN : Ġactive Ġtasks Ġ= Ġ 1 0 0 , Ġcompleted Ġtask Ġ= Ġ 0 ĊĠĠĠĠĠĠĠ Ġval Ġactive Tasks Ġ= Ġstatistics ViewModel . active Tasks Percent . get Or Await Value () ĊĠĠĠĠĠĠĠ Ġval Ġcompleted Tasks Ġ= Ġstatistics ViewModel . completed Tasks Percent . get Or Await Value () ĊĠĠĠĠĠĠĠ ĠassertThat ( active Tasks , Ġ` is `( 1 0 0 f )) ĊĠĠĠĠĠĠĠ ĠassertThat ( completed Tasks , Ġ` is `( 0 f )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Stats _ zero Active H undred Complete () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// WHEN : ĠThere Ġare Ġare Ġ 0 % Ġactive Ġtasks , Ġ 1 0 0 % Ġcompleted Ġtasks ĊĠĠĠĠĠĠĠ Ġfake Tasks Repository . add Tasks ( completed Task ) ĊĊĠĠĠĠĠĠĠ Ġ// THEN : Ġactive Ġtasks Ġ= Ġ 0 , Ġcompleted Ġtask Ġ= Ġ 1 0 0 ĊĠĠĠĠĠĠĠ Ġval Ġactive Tasks Ġ= Ġstatistics ViewModel . active Tasks Percent . get Or Await Value () ĊĠĠĠĠĠĠĠ Ġval Ġcompleted Tasks Ġ= Ġstatistics ViewModel . completed Tasks Percent . get Or Await Value () ĊĠĠĠĠĠĠĠ ĠassertThat ( active Tasks , Ġ` is `( 0 f )) ĊĠĠĠĠĠĠĠ ĠassertThat ( completed Tasks , Ġ` is `( 1 0 0 f )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Stats _ f ifty Active F ifty Complete () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// WHEN : ĠThere Ġare Ġare Ġ 1 0 0 % Ġactive Ġtasks , Ġ 0 % Ġcompleted Ġtasks ĊĠĠĠĠĠĠĠ Ġfake Tasks Repository . add Tasks ( active Task ) ĊĠĠĠĠĠĠĠ Ġfake Tasks Repository . add Tasks ( completed Task ) ĊĊĠĠĠĠĠĠĠ Ġ// THEN : Ġactive Ġtasks Ġ= Ġ 5 0 , Ġcompleted Ġtask Ġ= Ġ 5 0 ĊĠĠĠĠĠĠĠ Ġval Ġactive Tasks Ġ= Ġstatistics ViewModel . active Tasks Percent . get Or Await Value () ĊĠĠĠĠĠĠĠ Ġval Ġcompleted Tasks Ġ= Ġstatistics ViewModel . completed Tasks Percent . get Or Await Value () ĊĠĠĠĠĠĠĠ ĠassertThat ( active Tasks , Ġ` is `( 5 0 f )) ĊĠĠĠĠĠĠĠ ĠassertThat ( completed Tasks , Ġ` is `( 5 0 f )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġrefresh Tasks _ loading () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// we Ġwant Ġto Ġtest Ġthat Ġloading Ġstatus Ġchanges Ġin Ġfollowing Ġways ĊĠĠĠĠĠĠĠ Ġ// 1 . ĠLoading Ġ= Ġtrue , Ġwhen Ġdata Ġis Ġloading ĊĠĠĠĠĠĠĠ Ġ// Ġ 2 . ĠLoading Ġ= Ġfalse , Ġwhen Ġdata Ġis Ġloaded . ĊĠĠĠĠĠĠĠ Ġ// so Ġwe Ġwant Ġto Ġcheck Ġthe Ġasynchronous Ġlogic Ġhere Ġso Ġwe Ġare Ġnot Ġgoing Ġto Ġuse Ġrun Block Test ĊĠĠĠĠĠĠĠ Ġ// instead Ġwe Ġare Ġgoing Ġto Ġuse Ġpause / resume Ġfor Ġdispatcher ĊĊĠĠĠĠĠĠĠ Ġ// as Ġsoon Ġas Ġa Ġcoroutine Ġtries Ġto Ġlaunch , Ġpause Ġit Ġimmediately Ġbefore Ġexecuting Ġthe Ġcoroutine ĊĠĠĠĠĠĠĠ Ġmain Coroutine Rule . pause Dispatcher () ĊĠĠĠĠĠĠĠ Ġ// WHEN : ĠTasks Ġare Ġref reshed ĊĠĠĠĠĠĠĠ Ġstatistics ViewModel . refresh () Ġ// this Ġwill Ġbe Ġpaused Ġat ĠviewModel Scope . launch Ġ{} Ġline ĊĊĠĠĠĠĠĠĠ Ġ// THEN : ĊĠĠĠĠĠĠĠ Ġ// 1 . ĠWhen Ġdata Ġis Ġloading , Ġloading Ġstatus Ġshould Ġbe Ġtrue ĊĠĠĠĠĠĠĠ ĠassertThat ( statistics ViewModel . data Loading . get Or Await Value (), Ġ` is `( true )) ĊĠĠĠĠĠĠĠ Ġ// now Ġresume Ġthe Ġdispatcher Ġso Ġthat Ġdata Ġis Ġloaded ĊĠĠĠĠĠĠĠ Ġmain Coroutine Rule . resume Dispatcher () ĊĠĠĠĠĠĠĠ Ġ// 2 . ĠWhen Ġdata Ġis Ġloaded , Ġloading Ġstatus Ġshould Ġbe Ġfalse ĊĠĠĠĠĠĠĠ ĠassertThat ( statistics ViewModel . data Loading . get Or Await Value (), Ġ` is `( false )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġrefresh Tasks _ when Tasks Not Available _ display Error () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// WHEN : Ġtasks Ġare Ġnot Ġavailable Ġfor Ġno Ġinternet Ġor Ġany Ġother Ġreason ĊĠĠĠĠĠĠĠ Ġfake Tasks Repository . set Return Error ( true ) ĊĠĠĠĠĠĠĠ Ġstatistics ViewModel . refresh () ĊĊĠĠĠĠĠĠĠ Ġ// THEN : Ġerror Ġshould Ġbe Ġset Ġand Ġtasks Ġempty Ġstatus Ġshould Ġset ĊĠĠĠĠĠĠĠ ĠassertThat ( statistics ViewModel . error . get Or Await Value (), Ġ` is `( true )) ĊĠĠĠĠĠĠĠ ĠassertThat ( statistics ViewModel . empty . get Or Await Value (), Ġ` is `( true )) ĊĊĠĠĠĠĠĠĠ Ġ// reset Ġthe Ġerror Ġto Ġfalse ĊĠĠĠĠĠĠĠ Ġfake Tasks Repository . set Return Error ( false ) ĊĠĠĠ Ġ} Ċ } Ċ