package Ġcom . sensor berg . file preferences Ċ Ċ import Ġandroid . content . SharedPreferences Ċ import Ġcom . sensor berg . execution er . Execution er . POOL Ċ import Ġcom . sensor berg . execution er . Execution er . run On Ċ import Ġorg . junit . Assert .* Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġjava . io . File Ċ import Ġjava . util .* Ċ import Ġjava . util . concurrent . Count DownLatch Ċ import Ġjava . util . concurrent . TimeUnit Ċ Ċ /** Ċ Ġ* ĠThis Ġfile Ġhas Ġa Ġcouple Ġmore Ġtests Ġon Ġtop Ġof ĠAndroid ĠC TS , Ċ Ġ* ĠThose Ġtests Ġwere Ġcreated Ġto Ġcomplete Ġcoverage Ċ Ġ*/ Ċ class ĠFile Preferences Extra Test Ġ{ ĊĊ ĉ private Ġlateinit Ġvar Ġprefs : ĠSharedPreferences Ċ ĉ private Ġlateinit Ġvar Ġprefs File : ĠFile Ċ ĉ private Ġval Ġrandom Ġ= ĠRandom () ĊĊ ĉ @ Before Ċ ĉ fun Ġsetup () Ġ{ Ċĉ ĉ prefs File Ġ= ĠFile . create TempFile (" prefs -${ UUID . randomUUID () }", Ġ". json ") Ċĉ ĉ prefs File . delete () Ċĉ ĉ prefs Ġ= ĠFile Preferences . create ( prefs File ) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġtest _ string _ set () Ġ{ Ċĉ ĉ val Ġkey Ġ= Ġ" test _ set " Ċĉ ĉ val Ġvalue Ġ= Ġmutable Set Of < String >() Ċĉ ĉ for Ġ( i Ġin Ġ 0 Ġuntil Ġ 1 0 ) Ġvalue . add (" string -${ UUID . randomUUID () }") Ċĉ ĉ assertNull ( prefs . getString Set ( key , Ġnull )) Ċĉ ĉ prefs . edit (). putString Set ( key , Ġvalue ). commit () Ċĉ ĉ assertEquals ( value , Ġprefs . getString Set ( key , Ġnull )) Ċĉ ĉ prefs . edit (). putString Set ( key , Ġnull ). commit () Ċĉ ĉ assertNull ( prefs . getString Set ( key , Ġnull )) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġtest _ long () Ġ{ Ċĉ ĉ val Ġkey Ġ= Ġ" test _ set " Ċĉ ĉ val Ġvalue Ġ= Ġrandom . next Long () Ċĉ ĉ assertEquals ( Long . MIN _ VALUE , Ġprefs . getLong ( key , ĠLong . MIN _ VALUE )) Ċĉ ĉ prefs . edit (). put Long ( key , Ġvalue ). commit () Ċĉ ĉ assertEquals ( value , Ġprefs . getLong ( key , ĠLong . MIN _ VALUE )) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġtest _ int () Ġ{ Ċĉ ĉ val Ġkey Ġ= Ġ" test _ set " Ċĉ ĉ val Ġvalue Ġ= Ġrandom . nextInt ( Int . MAX _ VALUE ) Ċĉ ĉ assertEquals ( Int . MIN _ VALUE , Ġprefs . getInt ( key , ĠInt . MIN _ VALUE )) Ċĉ ĉ prefs . edit (). put Int ( key , Ġvalue ). commit () Ċĉ ĉ assertEquals ( value , Ġprefs . getInt ( key , ĠInt . MIN _ VALUE )) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġtest _ float () Ġ{ Ċĉ ĉ val Ġkey Ġ= Ġ" test _ set " Ċĉ ĉ val Ġvalue Ġ= Ġrandom . next Float () Ċĉ ĉ assertEquals ( Float . NaN , Ġprefs . get Float ( key , ĠFloat . NaN )) Ċĉ ĉ prefs . edit (). put Float ( key , Ġvalue ). commit () Ċĉ ĉ assertEquals ( value , Ġprefs . get Float ( key , ĠFloat . NaN )) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġtest _ listener () Ġ{ Ċĉ ĉ val ĠwaitFor It Ġ= ĠCount DownLatch ( 1 ) Ċĉ ĉ val Ġkey Ġ= Ġ" test _ set " Ċĉ ĉ val Ġvalue Ġ= ĠUUID . randomUUID (). toString () Ċĉ ĉ prefs . register On Shared Preference ChangeListener Ġ{ Ġshared Preferences , Ġreceived Key Ġ-> Ċĉĉ ĉ assertEquals ( key , Ġreceived Key ) Ċĉĉ ĉ assertEquals ( value , Ġshared Preferences . getString ( received Key , Ġnull )) Ċĉĉ ĉ waitFor It . count Down () Ċĉ ĉ } Ċĉ ĉ prefs . edit (). putString ( key , Ġvalue ). commit () Ċĉ ĉ assertTrue ( waitFor It . await ( 1 , ĠTimeUnit . SECONDS )) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġeditor _ clear _ apply _ does nt _ freeze () Ġ{ Ċĉ ĉ val ĠwaitFor It Ġ= ĠCount DownLatch ( 1 ) Ċĉ ĉ run On ( POOL ) Ġ{ Ċĉĉ ĉ prefs . edit (). putString (" key ", Ġ" value "). commit () Ċĉĉ ĉ prefs . edit (). clear (). commit () Ċĉĉ ĉ prefs . edit (). clear (). commit () Ċĉĉ ĉ waitFor It . count Down () Ċĉ ĉ } Ċĉ ĉ assertTrue ( waitFor It . await ( 1 0 0 , ĠTimeUnit . MILLISECONDS )) Ċ ĉ } Ċ }