/* Ċ Ġ* ĠCopyright Ġ 2 0 2 0 ĠGoogle ĠLLC . Ċ Ġ* Ċ Ġ* ĠThis Ġcode Ġmay Ġonly Ġbe Ġused Ġunder Ġthe ĠBSD Ġstyle Ġlicense Ġfound Ġat Ċ Ġ* Ġhttp :// polymer . github . io / LICENSE . txt Ċ Ġ* Ċ Ġ* ĠCode Ġdistributed Ġby ĠGoogle Ġas Ġpart Ġof Ġthis Ġproject Ġis Ġalso Ġsubject Ġto Ġan Ġadditional ĠIP Ġrights Ċ Ġ* Ġgrant Ġfound Ġat Ċ Ġ* Ġhttp :// polymer . github . io / PAT ENTS . txt Ċ Ġ*/ Ċ Ċ package Ġar cs . android . storage . service Ċ Ċ import Ġandroidx . test . ext . junit . runners . Android JUnit 4 Ċ import Ġar cs . android . cr dt . Parcelable Cr dt Type Ċ import Ġar cs . android . storage . service . testing . Fake Message Callback Ċ import Ġar cs . android . storage . service . testing . Fake Result Callback Ċ import Ġar cs . android . storage . service . testing . Fake Storage Channel Callback Ċ import Ġar cs . android . storage . to Parcel ByteArray Ċ import Ġar cs . core . data . Count Type Ċ import Ġar cs . core . storage . Direct Store Ċ import Ġar cs . core . storage . Fixed Driver Factory Ċ import Ġar cs . core . storage . Storage Key Ċ import Ġar cs . core . storage . Storage Key Manager Ċ import Ġar cs . core . storage . Store Options Ċ import Ġar cs . core . storage . Unt yped Proxy Message Ċ import Ġar cs . core . storage . keys . Ram Disk Storage Key Ċ import Ġar cs . core . storage . testutil . Mock Driver Provider Ċ import Ġar cs . core . storage . testutil . test Write Back Provider Ċ import Ġar cs . core . util . statistics . Transaction Statistics Impl Ċ import Ġar cs . flags . Build Flag Disabled Error Ċ import Ġar cs . flags . Build Flags Ċ import Ġar cs . flags . testing . Build Flags Rule Ċ import Ġar cs . jvm . util . Jvm Time Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġjava . lang . IllegalStateException Ċ import Ġkotlin . test . assert Fails With Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġkotlinx . coroutines . test . Test Coroutine Scope Ċ import Ġkotlinx . coroutines . test . run Blocking Test Ċ import Ġorg . junit . After Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ Ċ @ RunWith ( Android JUnit 4 :: class ) Ċ @ Opt In ( Experimental Cor outines Api :: class ) Ċ class ĠStorage Service Ng Impl Test Ġ{ ĊĠ Ġ@ get : Rule ĊĠ Ġval Ġbuild Flags Rule Ġ= ĠBuild Flags Rule . create () ĊĊĠ Ġprivate Ġval Ġdriver Factory Ġ= ĠFixed Driver Factory ( Mock Driver Provider ()) ĊĠ Ġprivate Ġval Ġon Proxy Message Callback : Ġsuspend Ġ( Storage Key , ĠUnt yped Proxy Message ) Ġ-> ĠUnit Ġ= ĊĠĠĠ Ġ{ Ġ_: ĠStorage Key , Ġ_: ĠUnt yped Proxy Message Ġ-> Ġ} ĊĠ Ġprivate Ġval Ġmessage Callback Ġ= ĠFake Message Callback () ĊĠ Ġprivate Ġval Ġchannel Callback Ġ= ĠFake Storage Channel Callback () ĊĊĠ Ġprivate Ġval Ġscope Ġ= ĠTest Coroutine Scope () ĊĠ Ġprivate Ġlateinit Ġvar Ġstorage Service : ĠStorage Service Ng Impl ĊĊĠ Ġ@ Before ĊĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠ ĠBuild Flags . STORAGE _ SERVICE _ NG Ġ= Ġtrue ĊĊĠĠĠ ĠStorage Key Manager . GLOBAL _ INSTANCE . add Parser ( Ram Disk Storage Key ) ĊĠĠĠ Ġstorage Service Ġ= ĠStorage Service Ng Impl ( ĊĠĠĠĠĠ Ġscope , ĊĠĠĠĠĠ ĠTransaction Statistics Impl ( Jvm Time ), ĊĠĠĠĠĠ Ġdriver Factory , ĊĠĠĠĠĠ Ġ:: test Write Back Provider , ĊĠĠĠĠĠ Ġnull , ĊĠĠĠĠĠ Ġon Proxy Message Callback ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ After ĊĠ Ġfun ĠtearDown () Ġ{ ĊĠĠĠ Ġscope . cleanup Test Cor outines () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġrequires Build Flag () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ ĠBuild Flags . STORAGE _ SERVICE _ NG Ġ= Ġfalse ĊĠĠĠ Ġassert Fails With < Build Flag Disabled Error > Ġ{ ĊĠĠĠĠĠ ĠStorage Service Ng Impl ( ĊĠĠĠĠĠĠĠ Ġscope , ĊĠĠĠĠĠĠĠ ĠTransaction Statistics Impl ( Jvm Time ), ĊĠĠĠĠĠĠĠ Ġdriver Factory , ĊĠĠĠĠĠĠĠ Ġ:: test Write Back Provider , ĊĠĠĠĠĠĠĠ Ġnull , ĊĠĠĠĠĠĠĠ Ġon Proxy Message Callback ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġopen Storage Channel _ inv okes On Create Callback _ and Creates Channel () Ġ= Ġscope . run Blocking Test Ġ{ ĊĠĠĠ Ġval Ġencoded Store Options Ġ= Ġcreate Encoded Store Options ( D UMMY _ STORAGE _ KEY ) ĊĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options , Ġchannel Callback , Ġmessage Callback ) ĊĊĠĠĠ Ġval Ġchannel Ġ= Ġchannel Callback . waitFor On Create () ĊĊĠĠĠ ĠassertThat ( channel ). isInstanceOf ( Storage Channel Impl :: class . java ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġopening Two Channels _ for Same Store _ results In Two Distinct Channels () Ġ= Ġscope . run Blocking Test Ġ{ ĊĠĠĠ Ġval Ġencoded Store Options Ġ= Ġcreate Encoded Store Options ( D UMMY _ STORAGE _ KEY ) ĊĊĠĠĠ Ġval Ġchannel Callback 1 Ġ= ĠFake Storage Channel Callback () ĊĠĠĠ Ġval Ġchannel Callback 2 Ġ= ĠFake Storage Channel Callback () ĊĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options , Ġchannel Callback 1 , Ġmessage Callback ) ĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options , Ġchannel Callback 2 , Ġmessage Callback ) ĊĊĠĠĠ Ġval Ġchannel 1 Ġ= Ġchannel Callback 1 . waitFor On Create () Ġas ĠStorage Channel Impl ĊĠĠĠ Ġval Ġchannel 2 Ġ= Ġchannel Callback 2 . waitFor On Create () Ġas ĠStorage Channel Impl ĊĠĠĠ ĠassertThat ( channel 2 ). isNot Same Instance As ( channel 1 ) ĊĠĠĠ ĠassertThat ( storage Service . channel Count For Active Storage Keys ()) ĊĠĠĠĠĠ Ġ. contains Exactly ( D UMMY _ STORAGE _ KEY , Ġ 2 ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġopen Storage Channel _ creates AS tore () Ġ= Ġscope . run Blocking Test Ġ{ ĊĠĠĠ Ġval Ġstorage Key Ġ= ĠRam Disk Storage Key (" store 1 ") ĊĠĠĠ Ġval Ġencoded Store Options Ġ= Ġcreate Encoded Store Options ( storage Key ) ĊĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options , Ġchannel Callback , Ġmessage Callback ) ĊĠĠĠ Ġchannel Callback . waitFor On Create () Ġas ĠStorage Channel Impl ĊĊĠĠĠ ĠassertThat ( storage Service . active Storage Keys ()). contains Exactly ( storage Key ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġopening Two Channels _ for Different Stores _ creates Two Stores () Ġ= Ġscope . run Blocking Test Ġ{ ĊĠĠĠ Ġ// ĠDefine Ġthe Ġstore Ġoptions Ġfor Ġtwo Ġdifferent Ġstores . ĊĠĠĠ Ġval Ġstorage Key 1 Ġ= ĠRam Disk Storage Key (" store 1 ") ĊĠĠĠ Ġval Ġstorage Key 2 Ġ= ĠRam Disk Storage Key (" store 2 ") ĊĠĠĠ Ġval Ġencoded Store Options 1 Ġ= Ġcreate Encoded Store Options ( storage Key 1 ) ĊĠĠĠ Ġval Ġencoded Store Options 2 Ġ= Ġcreate Encoded Store Options ( storage Key 2 ) ĊĠĠĠ Ġval Ġchannel Callback 1 Ġ= ĠFake Storage Channel Callback () ĊĠĠĠ Ġval Ġchannel Callback 2 Ġ= ĠFake Storage Channel Callback () ĊĊĠĠĠ Ġ// ĠOpen Ġtwo Ġstorage Ġchannels Ġfor Ġtwo Ġdistinct Ġstores ĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options 1 , Ġchannel Callback 1 , Ġmessage Callback ) ĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options 2 , Ġchannel Callback 2 , Ġmessage Callback ) ĊĊĠĠĠ Ġ// Ġcheck Ġtwo Ġstores Ġhave Ġbeen Ġcreated ĊĠĠĠ Ġval Ġchannel 1 Ġ= Ġchannel Callback 1 . waitFor On Create () Ġas ĠStorage Channel Impl ĊĠĠĠ Ġval Ġchannel 2 Ġ= Ġchannel Callback 2 . waitFor On Create () Ġas ĠStorage Channel Impl ĊĊĠĠĠ ĠassertThat ( storage Service . active Storage Keys ()). contains Exactly ( storage Key 1 , Ġstorage Key 2 ) ĊĊĠĠĠ Ġ// ĠCheck Ġthe Ġchannels Ġare Ġcommunic ating Ġwith Ġthe Ġexpected Ġstores . ĊĠĠĠ ĠassertThat ( channel 1 . store ). isNot Same Instance As ( channel 2 . store ) ĊĠĠĠ ĠassertThat ( channel 1 . store . storage Key ). isEqualTo ( storage Key 1 ) ĊĠĠĠ ĠassertThat ( channel 2 . store . storage Key ). isEqualTo ( storage Key 2 ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġopening Two Channels _ for Same Store _ only Creates One Store () Ġ= Ġscope . run Blocking Test Ġ{ ĊĠĠĠ Ġval Ġstorage Key Ġ= ĠRam Disk Storage Key (" store 1 ") ĊĠĠĠ Ġval Ġencoded Store Options Ġ= Ġcreate Encoded Store Options ( storage Key ) ĊĠĠĠ Ġval Ġchannel Callback 1 Ġ= ĠFake Storage Channel Callback () ĊĠĠĠ Ġval Ġchannel Callback 2 Ġ= ĠFake Storage Channel Callback () ĊĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options , Ġchannel Callback 1 , Ġmessage Callback ) ĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options , Ġchannel Callback 2 , Ġmessage Callback ) ĊĊĠĠĠ Ġval Ġchannel 1 Ġ= Ġchannel Callback 1 . waitFor On Create () Ġas ĠStorage Channel Impl ĊĠĠĠ Ġval Ġchannel 2 Ġ= Ġchannel Callback 2 . waitFor On Create () Ġas ĠStorage Channel Impl ĊĊĠĠĠ ĠassertThat ( storage Service . active Storage Keys ()). contains Exactly ( storage Key ) ĊĠĠĠ ĠassertThat ( channel 1 . store ). is Same Instance As ( channel 2 . store ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġclosing All Channels _ for Store _ clo ses Store () Ġ= Ġscope . run Blocking Test Ġ{ ĊĠĠĠ Ġval Ġstorage Key Ġ= ĠRam Disk Storage Key (" store 1 ") ĊĠĠĠ Ġval Ġencoded Store Options Ġ= Ġcreate Encoded Store Options ( storage Key ) ĊĠĠĠ Ġval Ġchannel Callback 1 Ġ= ĠFake Storage Channel Callback () ĊĠĠĠ Ġval Ġchannel Callback 2 Ġ= ĠFake Storage Channel Callback () ĊĠĠĠ Ġval Ġresult Callback 1 Ġ= ĠFake Result Callback () ĊĠĠĠ Ġval Ġresult Callback 2 Ġ= ĠFake Result Callback () ĊĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options , Ġchannel Callback 1 , Ġmessage Callback ) ĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options , Ġchannel Callback 2 , Ġmessage Callback ) ĊĊĠĠĠ Ġval Ġchannel 1 Ġ= Ġchannel Callback 1 . waitFor On Create () Ġas ĠStorage Channel Impl ĊĠĠĠ Ġval Ġchannel 2 Ġ= Ġchannel Callback 2 . waitFor On Create () Ġas ĠStorage Channel Impl ĊĊĠĠĠ Ġval Ġstore Ġ= Ġchannel 1 . store Ġas ĠDirect Store ĊĊĠĠĠ ĠassertThat ( storage Service . active Storage Keys ()). contains Exactly ( storage Key ) ĊĠĠĠ ĠassertThat ( storage Service . channel Count For Active Storage Keys ()). contains Exactly ( storage Key , Ġ 2 ) ĊĊĠĠĠ Ġchannel 1 . close ( result Callback 1 ) ĊĠĠĠ Ġresult Callback 1 . waitFor Result () ĊĊĠĠĠ ĠassertThat ( storage Service . active Storage Keys ()). contains Exactly ( storage Key ) ĊĠĠĠ ĠassertThat ( storage Service . channel Count For Active Storage Keys ()). contains Exactly ( storage Key , Ġ 1 ) ĊĠĠĠ ĠassertThat ( store . closed ). isFalse () ĊĊĠĠĠ Ġchannel 2 . close ( result Callback 2 ) ĊĠĠĠ Ġresult Callback 2 . waitFor Result () ĊĊĠĠĠ ĠassertThat ( storage Service . active Storage Keys ()). isEmpty () ĊĠĠĠ ĠassertThat ( storage Service . channel Count For Active Storage Keys ()). isEmpty () ĊĠĠĠ ĠassertThat ( store . closed ). isTrue () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġfails When Closing AC losed Store () Ġ= Ġscope . run Blocking Test Ġ{ ĊĠĠĠ Ġval Ġencoded Store Options Ġ= Ġcreate Encoded Store Options ( D UMMY _ STORAGE _ KEY ) ĊĠĠĠ Ġval Ġresult Callback Ġ= ĠFake Result Callback () ĊĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options , Ġchannel Callback , Ġmessage Callback ) ĊĠĠĠ Ġval Ġchannel Ġ= Ġchannel Callback . waitFor On Create () Ġas ĠStorage Channel Impl ĊĠĠĠ Ġchannel . close ( result Callback ) ĊĊĠĠĠ Ġassert Fails With < IllegalStateException >( ĊĠĠĠĠĠ Ġ" There Ġis Ġnot Ġstore Ġwith Ġstorage Ġkey Ġ$ D UMMY _ STORAGE _ KEY ." ĊĠĠĠ Ġ) Ġ{ ĊĠĠĠĠĠ Ġchannel . close () ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġreconnect To AC losed Store () Ġ= Ġscope . run Blocking Test Ġ{ ĊĠĠĠ Ġval Ġencoded Store Options Ġ= Ġcreate Encoded Store Options ( D UMMY _ STORAGE _ KEY ) ĊĠĠĠ Ġval Ġchannel Callback 1 Ġ= ĠFake Storage Channel Callback () ĊĠĠĠ Ġval Ġchannel Callback 2 Ġ= ĠFake Storage Channel Callback () ĊĠĠĠ Ġval Ġchannel Callback 3 Ġ= ĠFake Storage Channel Callback () ĊĠĠĠ Ġval Ġresult Callback 1 Ġ= ĠFake Result Callback () ĊĠĠĠ Ġval Ġresult Callback 2 Ġ= ĠFake Result Callback () ĊĠĠĠ Ġval Ġresult Callback 3 Ġ= ĠFake Result Callback () ĊĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options , Ġchannel Callback 1 , Ġmessage Callback ) ĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options , Ġchannel Callback 2 , Ġmessage Callback ) ĊĊĠĠĠ Ġval Ġchannel 1 Ġ= Ġchannel Callback 1 . waitFor On Create () Ġas ĠStorage Channel Impl ĊĠĠĠ Ġval Ġchannel 2 Ġ= Ġchannel Callback 2 . waitFor On Create () Ġas ĠStorage Channel Impl ĊĊĠĠĠ Ġval Ġstore Ġ= Ġchannel 1 . store Ġas ĠDirect Store ĊĊĠĠĠ Ġchannel 1 . close ( result Callback 1 ) ĊĠĠĠ Ġchannel 2 . close ( result Callback 2 ) ĊĠĠĠ Ġresult Callback 1 . waitFor Result () ĊĠĠĠ Ġresult Callback 2 . waitFor Result () ĊĊĠĠĠ ĠassertThat ( storage Service . active Storage Keys ()). isEmpty () ĊĠĠĠ ĠassertThat ( storage Service . channel Count For Active Storage Keys ()). isEmpty () ĊĠĠĠ ĠassertThat ( store . storage Key ). isEqualTo ( D UMMY _ STORAGE _ KEY ) ĊĠĠĠ ĠassertThat ( store . closed ). isTrue () ĊĊĠĠĠ Ġstorage Service . open Storage Channel ( encoded Store Options , Ġchannel Callback 3 , Ġmessage Callback ) ĊĠĠĠ Ġval Ġchannel 3 Ġ= Ġchannel Callback 3 . waitFor On Create () Ġas ĠStorage Channel Impl ĊĊĠĠĠ Ġval Ġnew Store Ġ= Ġchannel 3 . store Ġas ĠDirect Store ĊĊĠĠĠ ĠassertThat ( storage Service . active Storage Keys ()). contains Exactly ( D UMMY _ STORAGE _ KEY ) ĊĠĠĠ ĠassertThat ( storage Service . channel Count For Active Storage Keys ()) ĊĠĠĠĠĠ Ġ. contains Exactly ( D UMMY _ STORAGE _ KEY , Ġ 1 ) ĊĠĠĠ ĠassertThat ( new Store . storage Key ). isEqualTo ( D UMMY _ STORAGE _ KEY ) ĊĠĠĠ ĠassertThat ( new Store . closed ). isFalse () ĊĊĠĠĠ Ġchannel 3 . close ( result Callback 3 ) ĊĠĠĠ Ġresult Callback 3 . waitFor Result () ĊĊĠĠĠ ĠassertThat ( storage Service . active Storage Keys ()). isEmpty () ĊĠĠĠ ĠassertThat ( storage Service . channel Count For Active Storage Keys ()). isEmpty () ĊĠĠĠ ĠassertThat ( new Store . closed ). isTrue () ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġcreate Encoded Store Options ( storage Key : ĠStorage Key ): ĠByteArray Ġ{ ĊĠĠĠ Ġreturn ĠStore Options ( storage Key , ĠCount Type ()). to Parcel ByteArray ( Parcelable Cr dt Type . Count ) ĊĠ Ġ} ĊĊĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠ Ġval ĠD UMMY _ STORAGE _ KEY Ġ= ĠRam Disk Storage Key (" my Count ") ĊĠ Ġ} Ċ } Ċ