/* Ċ Ġ* ĠCopyright Ġ( C ) Ġ 2 0 2 0 Ġ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 Ġcom . android . tools . idea . progress Ċ Ċ import Ġcom . android . io . C ancellable File Io Ċ import Ġcom . android . test utils . file . Delegating FileSystem Provider Ċ import Ġcom . android . test utils . file . create InMemory FileSystem Ċ import Ġcom . android . test utils . file . some Root Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġcom . intellij . openapi . application . Application Manager Ċ import Ġcom . intellij . openapi . application . ex . Application Ex Ċ import Ġcom . intellij . openapi . progress . util . Progress Indicator Utils Ċ import Ġcom . intellij . test Framework . Application Rule Ċ import Ġorg . junit . Assert . fail Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġjava . nio . file . FileSystem Ċ import Ġjava . nio . file . Link Option Ċ import Ġjava . nio . file . Path Ċ import Ġjava . nio . file . attribute . Basic File Attributes Ċ import Ġjava . util . concurrent . Count DownLatch Ċ import Ġjava . util . concurrent . Semaphore Ċ import Ġjava . util . concurrent . TimeUnit Ċ import Ġjava . util . concurrent . atomic . Atomic Integer Ċ Ċ /** Ċ Ġ* ĠTest Ġfor Ġ[ Studio Progress Manager Adapter ] Ġand Ġ[ C ancellable File Io ]. Ċ Ġ*/ Ċ class ĠStudio Progress Manager Adapter Test Ġ{ ĊĠ Ġ@ get : Rule ĊĠ Ġval Ġrule Ġ= ĠApplication Rule () ĊĊĠ Ġprivate Ġval Ġfile System Provider Ġ= ĠMock FileSystem Provider ( create InMemory FileSystem ()) ĊĠ Ġprivate Ġval Ġfile System Ġ= Ġfile System Provider . file System ĊĊĠ Ġ/** ĠChecks Ġcancellation Ġof ĠI / O Ġoperations Ġby Ġa Ġpending Ġwrite Ġaction . Ġ*/ ĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Io Cancellation () Ġ{ ĊĠĠĠ Ġval Ġio Operation Count Ġ= ĠAtomicInteger () ĊĠĠĠ Ġval Ġread Action Started Ġ= ĠCount DownLatch ( 1 ) ĊĊĠĠĠ Ġ// ĠStart Ġa Ġread Ġaction Ġon Ġa Ġbackground Ġthread . ĊĠĠĠ ĠApplication Manager . getApplication (). execute On P ooled Thread Ġ{ ĊĠĠĠĠĠ ĠProgress Indicator Utils . run In Read Action With Write Action Priority Ġ{ ĊĠĠĠĠĠĠĠ Ġread Action Started . count Down () ĊĠĠĠĠĠĠĠ Ġfor Ġ( i Ġin Ġ 1 .. 1 0 0 ) Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġval Ġfile Ġ= Ġfile System . some Root . resolve (" dir / file $ i ") ĊĠĠĠĠĠĠĠĠĠ ĠC ancellable File Io . is Regular File ( file ) ĊĠĠĠĠĠĠĠĠĠ Ġio Operation Count . set ( i ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// ĠWait Ġuntil Ġread Ġaction Ġstarts . ĊĠĠĠ Ġif Ġ(! read Action Started . await ( 2 , ĠTimeUnit . SECONDS )) Ġ{ ĊĠĠĠĠĠ Ġfail (" Unable Ġto Ġstart Ġa Ġread Ġaction , Ġprobably Ġa Ġwrite Ġaction Ġwas Ġstill Ġrunning Ġwhen Ġthis Ġtest Ġstarted ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġwrite Action Completed Ġ= ĠCount DownLatch ( 1 ) ĊĠĠĠ Ġ// ĠStart Ġa Ġwrite Ġaction Ġasynchronously . ĊĠĠĠ ĠApplication Manager . getApplication (). invoke Later Ġ{ ĊĠĠĠĠĠ ĠApplication Manager . getApplication (). run Write Action ( write Action Completed :: count Down ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// ĠWait Ġuntil Ġthe Ġwrite Ġaction Ġis Ġready Ġto Ġrun . ĊĠĠĠ Ġwhile Ġ(!( Application Manager . getApplication () Ġas ĠApplication Ex ). is Write Action Pending Ġ&& Ġwrite Action Completed . count Ġ> Ġ 0 ) Ġ{ ĊĠĠĠĠĠ Ġwrite Action Completed . await ( 1 0 , ĠTimeUnit . MILLISECONDS ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// ĠAllow Ġone ĠI / O Ġoperation Ġevery Ġ 1 0 0 Ġmilliseconds . ĊĠĠĠ Ġfor Ġ( i Ġin Ġ 1 .. 1 0 0 ) Ġ{ ĊĠĠĠĠĠ Ġwrite Action Completed . await ( 1 0 , ĠTimeUnit . MILLISECONDS ) ĊĠĠĠĠĠ Ġfile System Provider . proceed () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// ĠThe Ġread Ġaction Ġshould Ġbe Ġcancelled Ġafter Ġthe Ġfirst ĠI / O Ġoperation . ĊĠĠĠ ĠassertThat ( io Operation Count . get ()). is At Most ( 1 ) ĊĊĠĠĠ Ġ// ĠMake Ġsure Ġthat Ġthe Ġwrite Ġaction Ġis Ġnot Ġleft Ġbehind Ġwhen Ġthe Ġtest Ġtermin ates . ĊĠĠĠ ĠApplication Manager . getApplication (). run Read Action Ġ{} ĊĠ Ġ} ĊĊĠ Ġprivate Ġclass ĠMock FileSystem Provider ( file System : ĠFileSystem ) Ġ: ĠDeleg ating FileSystem Provider ( file System ) Ġ{ ĊĠĠĠ Ġprivate Ġval Ġsemaphore Ġ= ĠSem aphore ( Integer . MAX _ VALUE ). apply Ġ{ Ġdrain Permit s () Ġ} ĊĊĠĠĠ Ġ/** ĠAllows Ġthe Ġgiven Ġnumber Ġof ĠI / O Ġoperations Ġto Ġbe Ġexecuted . Ġ*/ ĊĠĠĠ Ġfun Ġproceed ( numberOf Operations : ĠInt Ġ= Ġ 1 ) Ġ{ ĊĠĠĠĠĠ Ġsemaphore . release ( numberOf Operations ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġ< A Ġ: ĠBasic File Attributes > Ġread Attributes ( path : ĠPath , Ġtype : ĠClass < A >, Ġvar arg Ġoptions : ĠLink Option ): ĠA Ġ{ ĊĠĠĠĠĠ Ġsemaphore . acquire () ĊĠĠĠĠĠ Ġreturn Ġsuper . read Attributes ( path , Ġtype , Ġ* options ) ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ }