/* Ċ Ġ* ĠCopyright Ġ 2 0 1 9 ĠThe ĠAndroid ĠOpen ĠSource ĠProject Ċ Ġ* Ċ Ġ* ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ Ġ* Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ Ġ* ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ Ġ* Ċ Ġ* ĠĠĠĠĠ Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ Ġ* Ċ Ġ* ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ Ġ* Ġdistributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ Ġ* ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ Ġ* ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ Ġ* Ġlimitations Ġunder Ġthe ĠLicense . Ċ Ġ*/ Ċ Ċ package Ġandroidx . work . impl . foreground Ċ Ċ import Ġandroid . app . Notification Ċ import Ġandroid . content . Context Ċ import Ġandroid . content . pm . Service Info . FO REG ROUND _ SERVICE _ TYPE _ CONNECTED _ DEVICE Ċ import Ġandroid . content . pm . Service Info . FO REG ROUND _ SERVICE _ TYPE _ LOCATION Ċ import Ġandroid . util . Log Ċ import Ġandroidx . test . core . app . Application Provider Ċ import Ġandroidx . test . ext . junit . runners . Android JUnit 4 Ċ import Ġandroidx . test . filters . Medium Test Ċ import Ġandroidx . test . filters . Sdk Suppress Ċ import Ġandroidx . work . Configuration Ċ import Ġandroidx . work . Constraints Ċ import Ġandroidx . work . Foreground Info Ċ import Ġandroidx . work . Network Type Ċ import Ġandroidx . work . One Time Work Request Ċ import Ġandroidx . work . Work Info Ċ import Ġandroidx . work . impl . Processor Ċ import Ġandroidx . work . impl . Scheduler Ċ import Ġandroidx . work . impl . Work Database Ċ import Ġandroidx . work . impl . Work ManagerImpl Ċ import Ġandroidx . work . impl . constraints . Work Constraints Callback Ċ import Ġandroidx . work . impl . constraints . Work Constraints Tracker Ċ import Ġandroidx . work . impl . foreground . System Foreground Dispatcher . create Cancel Work Intent Ċ import Ġandroidx . work . impl . foreground . System Foreground Dispatcher . create Notify Intent Ċ import Ġandroidx . work . impl . foreground . System Foreground Dispatcher . create Start Foreground Intent Ċ import Ġandroidx . work . impl . utils . Stop Work Runnable Ċ import Ġandroidx . work . impl . utils . S ynchronous Executor Ċ import Ġandroidx . work . impl . utils . task executor . Instant Work Task Executor Ċ import Ġandroidx . work . impl . utils . task executor . Task Executor Ċ import Ġandroidx . work . worker . Test Worker Ċ import Ġorg . hamcrest . Core Matchers .` is ` Ċ import Ġorg . hamcrest . Matcher Assert . assertThat Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . mockito . Argument Matchers . any Ċ import Ġorg . mockito . Argument Matchers . eq Ċ import Ġorg . mockito . Mockito .` when ` Ċ import Ġorg . mockito . Mockito . mock Ċ import Ġorg . mockito . Mockito . spy Ċ import Ġorg . mockito . Mockito . times Ċ import Ġorg . mockito . Mockito . verify Ċ import Ġorg . mockito . Mockito . verify Zero Interactions Ċ import Ġjava . util . UUID Ċ Ċ @ RunWith ( Android JUnit 4 :: class ) Ċ @ Medium Test Ċ class ĠSystem Foreground Dispatcher Test Ġ{ ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġcontext : ĠContext ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġconfig : ĠConfiguration ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġtask Executor : ĠTask Executor ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġwork Manager : ĠWork ManagerImpl ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġwork Database : ĠWork Database ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġprocessor : ĠProcessor ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġtracker : ĠWork Constraints Tracker ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġconstraints Callback : ĠWork Constraints Callback ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġdispatcher : ĠSystem Foreground Dispatcher ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġdispatcher Callback : ĠSystem Foreground Dispatcher . Callback ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ Ġcontext Ġ= ĠApplication Provider . getApplicationContext () ĊĠĠĠĠĠĠĠ Ġconfig Ġ= ĠConfiguration . Builder () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Executor ( S ynchronous Executor ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. setMinimum Logging Level ( Log . DEBUG ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠĠĠĠĠ Ġtask Executor Ġ= ĠInstant Work Task Executor () ĊĠĠĠĠĠĠĠ Ġval Ġscheduler Ġ= Ġmock ( Scheduler :: class . java ) ĊĠĠĠĠĠĠĠ Ġwork Database Ġ= ĠWork Database . create ( context , Ġtask Executor . background Executor , Ġtrue ) ĊĠĠĠĠĠĠĠ Ġprocessor Ġ= Ġspy ( Processor ( context , Ġconfig , Ġtask Executor , Ġwork Database , ĠlistOf ( scheduler ))) ĊĠĠĠĠĠĠĠ Ġwork Manager Ġ= Ġspy ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠWork ManagerImpl ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcontext , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġconfig , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġtask Executor , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġwork Database , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf ( scheduler ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġprocessor ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġwork Database Ġ= Ġwork Manager . work Database ĊĠĠĠĠĠĠĠ Ġ// ĠInitialize ĠWork Constraints Tracker ĊĠĠĠĠĠĠĠ Ġconstraints Callback Ġ= Ġmock ( Work Constraints Callback :: class . java ) ĊĠĠĠĠĠĠĠ Ġtracker Ġ= Ġspy ( Work Constraints Tracker ( context , Ġtask Executor , Ġconstraints Callback )) ĊĠĠĠĠĠĠĠ Ġ// ĠInitialize Ġdispatcher ĊĠĠĠĠĠĠĠ Ġdispatcher Callback Ġ= Ġmock ( System Foreground Dispatcher . Callback :: class . java ) ĊĠĠĠĠĠĠĠ Ġdispatcher Ġ= Ġspy ( System Foreground Dispatcher ( context , Ġwork Manager , Ġtracker )) ĊĠĠĠĠĠĠĠ Ġdispatcher . set Callback ( dispatcher Callback ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Start Foreground _ no Interactions _ work Spec Has No Constraints () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĠOne Time Work Request . Builder ( Test Worker :: class . java ). build () ĊĠĠĠĠĠĠĠ Ġval Ġnotification Id Ġ= Ġ 1 ĊĠĠĠĠĠĠĠ Ġval Ġnotification Ġ= Ġmock ( Notification :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġmetadata Ġ= ĠFore ground Info ( notification Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġwork Database . work Spec Dao (). insert Work Spec ( request . work Spec ) ĊĠĠĠĠĠĠĠ Ġval Ġintent Ġ= Ġcreate Start Foreground Intent ( context , Ġrequest . string Id , Ġmetadata ) ĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( intent ) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. start Foreground ( eq ( notification Id ), Ġeq ( 0 ), Ġany < Notification >()) ĊĠĠĠĠĠĠĠ Ġverify Zero Interactions ( tracker ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Start Foreground _ track Constraints _ work Spec Has Constraints () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĠOne Time Work Request . Builder ( Test Worker :: class . java ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Constraints ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠConstraints . Builder (). set Required Network Type ( Network Type . CONNECTED ). build () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ). build () ĊĠĠĠĠĠĠĠ Ġwork Database . work Spec Dao (). insert Work Spec ( request . work Spec ) ĊĠĠĠĠĠĠĠ Ġval Ġnotification Id Ġ= Ġ 1 ĊĠĠĠĠĠĠĠ Ġval Ġnotification Ġ= Ġmock ( Notification :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġmetadata Ġ= ĠFore ground Info ( notification Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġintent Ġ= Ġcreate Start Foreground Intent ( context , Ġrequest . string Id , Ġmetadata ) ĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( intent ) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. start Foreground ( eq ( notification Id ), Ġeq ( 0 ), Ġany < Notification >()) ĊĠĠĠĠĠĠĠ Ġverify ( tracker , Ġtimes ( 1 )). replace ( set Of ( request . work Spec )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Stop Foreground () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// ĠThe ĠFore ground ĠService Ġnow Ġcalls Ġhandle Stop () Ġdirectly Ġwithout Ġthe Ġneed Ġfor Ġan ĊĠĠĠĠĠĠĠ Ġ// Ġadditional Ġstart Service (). ĊĠĠĠĠĠĠĠ Ġdispatcher . handle Stop () ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )). stop () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Start Foreground () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġwork Spec Id Ġ= Ġ" work Spec Id " ĊĠĠĠĠĠĠĠ Ġval Ġnotification Id Ġ= Ġ 1 ĊĠĠĠĠĠĠĠ Ġval Ġnotification Ġ= Ġmock ( Notification :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġmetadata Ġ= ĠFore ground Info ( notification Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġintent Ġ= Ġcreate Notify Intent ( context , Ġwork Spec Id , Ġmetadata ) ĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( intent ) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. start Foreground ( eq ( notification Id ), Ġeq ( 0 ), Ġany < Notification >()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Notify () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġwork Spec Id Ġ= Ġ" work Spec Id " ĊĠĠĠĠĠĠĠ Ġval Ġnotification Id Ġ= Ġ 1 ĊĠĠĠĠĠĠĠ Ġval Ġnotification Ġ= Ġmock ( Notification :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġmetadata Ġ= ĠFore ground Info ( notification Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġintent Ġ= Ġcreate Notify Intent ( context , Ġwork Spec Id , Ġmetadata ) ĊĠĠĠĠĠĠĠ Ġdispatcher . m Current Foreground Work Spec Id Ġ= Ġ" another Work Spec Id " ĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( intent ) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. notify ( eq ( notification Id ), Ġany < Notification >()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Prom ote Work Spec For Start Foreground () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġfirst Work Spec Id Ġ= Ġ" first " ĊĠĠĠĠĠĠĠ Ġval Ġfirst Id Ġ= Ġ 1 ĊĠĠĠĠĠĠĠ Ġval Ġnotification Ġ= Ġmock ( Notification :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġfirst Info Ġ= ĠFore ground Info ( first Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġfirst Intent Ġ= Ġcreate Notify Intent ( context , Ġfirst Work Spec Id , Ġfirst Info ) ĊĊĠĠĠĠĠĠĠ Ġval Ġsecond Work Spec Id Ġ= Ġ" second " ĊĠĠĠĠĠĠĠ Ġval Ġsecond Id Ġ= Ġ 2 ĊĠĠĠĠĠĠĠ Ġval Ġsecond Info Ġ= ĠFore ground Info ( second Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġsecond Intent Ġ= Ġcreate Notify Intent ( context , Ġsecond Work Spec Id , Ġsecond Info ) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( first Intent ) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Current Foreground Work Spec Id , Ġ` is `( first Work Spec Id )) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. start Foreground ( eq ( first Id ), Ġeq ( 0 ), Ġany < Notification >()) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( second Intent ) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Current Foreground Work Spec Id , Ġ` is `( first Work Spec Id )) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. notify ( eq ( second Id ), Ġany < Notification >()) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Foreground Info ById . count (), Ġ` is `( 2 )) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . on Executed ( first Work Spec Id , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. start Foreground ( eq ( second Id ), Ġeq ( 0 ), Ġany < Notification >()) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. cancel Notification ( second Id ) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Foreground Info ById . count (), Ġ` is `( 1 )) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . on Executed ( second Work Spec Id , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. cancel Notification ( second Id ) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Foreground Info ById . count (), Ġ` is `( 0 )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġpromote Work Spec For Start Foreground 2 () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġfirst Work Spec Id Ġ= Ġ" first " ĊĠĠĠĠĠĠĠ Ġval Ġfirst Id Ġ= Ġ 1 ĊĠĠĠĠĠĠĠ Ġval Ġnotification Ġ= Ġmock ( Notification :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġfirst Info Ġ= ĠFore ground Info ( first Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġfirst Intent Ġ= Ġcreate Notify Intent ( context , Ġfirst Work Spec Id , Ġfirst Info ) ĊĊĠĠĠĠĠĠĠ Ġval Ġsecond Work Spec Id Ġ= Ġ" second " ĊĠĠĠĠĠĠĠ Ġval Ġsecond Id Ġ= Ġ 2 ĊĠĠĠĠĠĠĠ Ġval Ġsecond Info Ġ= ĠFore ground Info ( second Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġsecond Intent Ġ= Ġcreate Notify Intent ( context , Ġsecond Work Spec Id , Ġsecond Info ) ĊĊĠĠĠĠĠĠĠ Ġval Ġthird Work Spec Id Ġ= Ġ" third " ĊĠĠĠĠĠĠĠ Ġval Ġthird Id Ġ= Ġ 3 ĊĠĠĠĠĠĠĠ Ġval Ġthird Info Ġ= ĠFore ground Info ( third Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġthird Intent Ġ= Ġcreate Notify Intent ( context , Ġthird Work Spec Id , Ġthird Info ) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( first Intent ) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Current Foreground Work Spec Id , Ġ` is `( first Work Spec Id )) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. start Foreground ( eq ( first Id ), Ġeq ( 0 ), Ġany < Notification >()) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( second Intent ) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Current Foreground Work Spec Id , Ġ` is `( first Work Spec Id )) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. notify ( eq ( second Id ), Ġany < Notification >()) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Foreground Info ById . count (), Ġ` is `( 2 )) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( third Intent ) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Current Foreground Work Spec Id , Ġ` is `( first Work Spec Id )) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. notify ( eq ( second Id ), Ġany < Notification >()) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Foreground Info ById . count (), Ġ` is `( 3 )) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . on Executed ( first Work Spec Id , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. start Foreground ( eq ( third Id ), Ġeq ( 0 ), Ġany < Notification >()) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. cancel Notification ( third Id ) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Foreground Info ById . count (), Ġ` is `( 2 )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġpromote Work Spec For Start Foreground 3 () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġfirst Work Spec Id Ġ= Ġ" first " ĊĠĠĠĠĠĠĠ Ġval Ġfirst Id Ġ= Ġ 1 ĊĠĠĠĠĠĠĠ Ġval Ġnotification Ġ= Ġmock ( Notification :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġfirst Info Ġ= ĠFore ground Info ( first Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġfirst Intent Ġ= Ġcreate Notify Intent ( context , Ġfirst Work Spec Id , Ġfirst Info ) ĊĊĠĠĠĠĠĠĠ Ġval Ġsecond Work Spec Id Ġ= Ġ" second " ĊĠĠĠĠĠĠĠ Ġval Ġsecond Id Ġ= Ġ 2 ĊĠĠĠĠĠĠĠ Ġval Ġsecond Info Ġ= ĠFore ground Info ( second Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġsecond Intent Ġ= Ġcreate Notify Intent ( context , Ġsecond Work Spec Id , Ġsecond Info ) ĊĊĠĠĠĠĠĠĠ Ġval Ġthird Work Spec Id Ġ= Ġ" third " ĊĠĠĠĠĠĠĠ Ġval Ġthird Id Ġ= Ġ 3 ĊĠĠĠĠĠĠĠ Ġval Ġthird Info Ġ= ĠFore ground Info ( third Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġthird Intent Ġ= Ġcreate Notify Intent ( context , Ġthird Work Spec Id , Ġthird Info ) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( first Intent ) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Current Foreground Work Spec Id , Ġ` is `( first Work Spec Id )) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. start Foreground ( eq ( first Id ), Ġeq ( 0 ), Ġany < Notification >()) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( second Intent ) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Current Foreground Work Spec Id , Ġ` is `( first Work Spec Id )) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. notify ( eq ( second Id ), Ġany < Notification >()) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Foreground Info ById . count (), Ġ` is `( 2 )) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( third Intent ) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Current Foreground Work Spec Id , Ġ` is `( first Work Spec Id )) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. notify ( eq ( second Id ), Ġany < Notification >()) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Foreground Info ById . count (), Ġ` is `( 3 )) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . on Executed ( second Work Spec Id , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. cancel Notification ( eq ( second Id )) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Foreground Info ById . count (), Ġ` is `( 2 )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġ@ Sdk Suppress ( min Sdk Version Ġ= Ġ 2 9 ) ĊĠĠĠ Ġfun Ġtest Update Notification With Different Foreground ServiceType () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġfirst Work Spec Id Ġ= Ġ" first " ĊĠĠĠĠĠĠĠ Ġval Ġfirst Id Ġ= Ġ 1 ĊĠĠĠĠĠĠĠ Ġval Ġnotification Ġ= Ġmock ( Notification :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġfirst Info Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ ĠFore ground Info ( first Id , Ġnotification , ĠFO REG ROUND _ SERVICE _ TYPE _ CONNECTED _ DEVICE ) ĊĠĠĠĠĠĠĠ Ġval Ġfirst Intent Ġ= Ġcreate Notify Intent ( context , Ġfirst Work Spec Id , Ġfirst Info ) ĊĊĠĠĠĠĠĠĠ Ġval Ġsecond Work Spec Id Ġ= Ġ" second " ĊĠĠĠĠĠĠĠ Ġval Ġsecond Id Ġ= Ġ 2 ĊĠĠĠĠĠĠĠ Ġval Ġsecond Info Ġ= ĠFore ground Info ( second Id , Ġnotification , ĠFO REG ROUND _ SERVICE _ TYPE _ LOCATION ) ĊĠĠĠĠĠĠĠ Ġval Ġsecond Intent Ġ= Ġcreate Notify Intent ( context , Ġsecond Work Spec Id , Ġsecond Info ) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( first Intent ) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. start Foreground ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġeq ( first Id ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġeq ( FO REG ROUND _ SERVICE _ TYPE _ CONNECTED _ DEVICE ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġany < Notification >() ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( second Intent ) ĊĠĠĠĠĠĠĠ ĠassertThat ( dispatcher . m Current Foreground Work Spec Id , Ġ` is `( first Work Spec Id )) ĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. notify ( eq ( second Id ), Ġany < Notification >()) ĊĊĠĠĠĠĠĠĠ Ġval Ġexpected Notification Type Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ ĠFO REG ROUND _ SERVICE _ TYPE _ LOCATION Ġor ĠFO REG ROUND _ SERVICE _ TYPE _ CONNECTED _ DEVICE ĊĊĠĠĠĠĠĠĠ Ġverify ( dispatcher Callback , Ġtimes ( 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. start Foreground ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġeq ( first Id ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġeq ( expected Notification Type ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġany < Notification >() ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Start Foreground _ track Constraints _ constraints Un Met () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĠOne Time Work Request . Builder ( Test Worker :: class . java ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Constraints ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠConstraints . Builder (). set Required Network Type ( Network Type . CONNECTED ). build () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ). build () ĊĠĠĠĠĠĠĠ Ġwork Database . work Spec Dao (). insert Work Spec ( request . work Spec ) ĊĠĠĠĠĠĠĠ Ġval Ġnotification Id Ġ= Ġ 1 ĊĠĠĠĠĠĠĠ Ġval Ġnotification Ġ= Ġmock ( Notification :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġmetadata Ġ= ĠFore ground Info ( notification Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġintent Ġ= Ġcreate Start Foreground Intent ( context , Ġrequest . string Id , Ġmetadata ) ĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( intent ) ĊĠĠĠĠĠĠĠ Ġverify ( tracker , Ġtimes ( 1 )). replace ( set Of ( request . work Spec )) ĊĠĠĠĠĠĠĠ Ġdispatcher . on All Constraints Not Met ( listOf ( request . work Spec . id )) ĊĠĠĠĠĠĠĠ Ġverify ( work Manager , Ġtimes ( 1 )). stop Foreground Work ( eq ( request . work Spec . id )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Cancel Foreground Work () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĠOne Time Work Request . Builder ( Test Worker :: class . java ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Constraints ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠConstraints . Builder (). set Required Network Type ( Network Type . CONNECTED ). build () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ). build () ĊĠĠĠĠĠĠĠ Ġwork Database . work Spec Dao (). insert Work Spec ( request . work Spec ) ĊĠĠĠĠĠĠĠ Ġval Ġnotification Id Ġ= Ġ 1 ĊĠĠĠĠĠĠĠ Ġval Ġnotification Ġ= Ġmock ( Notification :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġmetadata Ġ= ĠFore ground Info ( notification Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġintent Ġ= Ġcreate Start Foreground Intent ( context , Ġrequest . string Id , Ġmetadata ) ĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( intent ) ĊĠĠĠĠĠĠĠ Ġverify ( tracker , Ġtimes ( 1 )). replace ( set Of ( request . work Spec )) ĊĠĠĠĠĠĠĠ Ġval Ġstop Intent Ġ= Ġcreate Cancel Work Intent ( context , Ġrequest . string Id ) ĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( stop Intent ) ĊĠĠĠĠĠĠĠ Ġverify ( work Manager , Ġtimes ( 1 )). cancel Work ById ( eq ( UUID . fromString ( request . work Spec . id ))) ĊĠĠĠĠĠĠĠ ĠassertThat ( processor . has Work (), Ġ` is `( false )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Stop Foreground Work () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĠOne Time Work Request . Builder ( Test Worker :: class . java ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. setInitial State ( Work Info . State . RUNNING ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĊĠĠĠĠĠĠĠ Ġ` when `( processor . is En queued In Foreground ( eq ( request . string Id ))). thenReturn ( true ) ĊĠĠĠĠĠĠĠ Ġwork Database . work Spec Dao (). insert Work Spec ( request . work Spec ) ĊĠĠĠĠĠĠĠ Ġval Ġnotification Id Ġ= Ġ 1 ĊĠĠĠĠĠĠĠ Ġval Ġnotification Ġ= Ġmock ( Notification :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġmetadata Ġ= ĠFore ground Info ( notification Id , Ġnotification ) ĊĠĠĠĠĠĠĠ Ġval Ġintent Ġ= Ġcreate Start Foreground Intent ( context , Ġrequest . string Id , Ġmetadata ) ĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( intent ) ĊĠĠĠĠĠĠĠ Ġval Ġstop Work Runnable Ġ= ĠStop Work Runnable ( work Manager , Ġrequest . string Id , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġstop Work Runnable . run () ĊĠĠĠĠĠĠĠ Ġval Ġstate Ġ= Ġwork Database . work Spec Dao (). getState ( request . string Id ) ĊĠĠĠĠĠĠĠ ĠassertThat ( state , Ġ` is `( Work Info . State . RUNNING )) ĊĠĠĠĠĠĠĠ Ġval Ġstop And Cancel Intent Ġ= Ġcreate Cancel Work Intent ( context , Ġrequest . string Id ) ĊĠĠĠĠĠĠĠ Ġdispatcher . onStart Command ( stop And Cancel Intent ) ĊĠĠĠĠĠĠĠ Ġverify ( work Manager , Ġtimes ( 1 )). cancel Work ById ( eq ( UUID . fromString ( request . work Spec . id ))) ĊĠĠĠĠĠĠĠ ĠassertThat ( processor . has Work (), Ġ` is `( false )) ĊĠĠĠ Ġ} Ċ } Ċ