package Ġcom . pix ite . android . billing x Ċ Ċ import Ġandroid . app . Activity Ċ import Ġandroid . app . Application Ċ import Ġandroid . content . Broadcast Receiver Ċ import Ġandroid . content . Context Ċ import Ġandroid . content . Intent Filter Ċ import Ġcom . android . billing client . api . Billing Client . Billing Response Ċ import Ġcom . android . billing client . api . Billing Client . Feature Type Ċ import Ġcom . android . billing client . api . Billing Client . Sku Type Ċ import Ġcom . android . billing client . api . Billing Client State Listener Ċ import Ġcom . android . billing client . api . Internal P urchases Result Ċ import Ġcom . android . billing client . api . Purchase Ċ import Ġcom . android . billing client . api . Purchase History Response Listener Ċ import Ġcom . android . billing client . api . P urchases Updated Listener Ċ import Ġcom . android . billing client . api . Sku Details Ċ import Ġcom . android . billing client . api . Sku Details Params Ċ import Ġcom . android . billing client . api . Sku Details Response Listener Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġcom . n ha ar man . mockito _ kotlin . any Ċ import Ġcom . n ha ar man . mockito _ kotlin . do Answer Ċ import Ġcom . n ha ar man . mockito _ kotlin . do Return Ċ import Ġcom . n ha ar man . mockito _ kotlin . eq Ċ import Ġcom . n ha ar man . mockito _ kotlin . mock Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġjava . util . Date Ċ import Ġjava . util . concurrent . Executor Ċ Ċ class ĠDebug Billing Client Test Ġ{ ĊĊĠ Ġlateinit Ġvar Ġapplication : ĠApplication ĊĠ Ġlateinit Ġvar Ġactivity : ĠActivity ĊĠ Ġlateinit Ġvar Ġstore : ĠBilling Store ĊĠ Ġlateinit Ġvar Ġpurch ases Updated Listener : ĠFake P urchases Updated Listener ĊĠ Ġlateinit Ġvar Ġclient : ĠDebug Billing Client ĊĠ Ġprivate Ġval Ġlocal Broadcast Interactor Ġ= Ġobject Ġ: ĠLocal Broadcast Interactor Ġ{ ĊĠĠĠ Ġoverride Ġfun Ġregister Receiver ( context : ĠContext , Ġbroadcast Receiver : ĠBroadcast Receiver , Ġintent Filter : ĠIntent Filter ) Ġ{ ĊĠĠĠĠĠ Ġ// Ġno Ġop ĊĠĠĠ Ġ} ĊĠĠĠ Ġoverride Ġfun Ġunregister Receiver ( context : ĠContext , Ġbroadcast Receiver : ĠBroadcast Receiver ) Ġ{ ĊĠĠĠĠĠ Ġ// Ġno Ġop ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġval Ġempty State Listener Ġ= Ġobject Ġ: ĠBilling Client State Listener Ġ{ ĊĠĠĠ Ġoverride Ġfun Ġon Billing Service Disconnected () Ġ{} ĊĠĠĠ Ġoverride Ġfun Ġon Billing Setup Finished ( response Code : ĠInt ) Ġ{} ĊĠ Ġ} ĊĠ Ġprivate Ġval Ġsubs Purchase 1 Ġ= ĠPurchase ("{ \" orderId \":\" foo - 1 2 3 4 \",\" packageName \ ":" Ġ+ ĊĠĠĠĠĠ Ġ"\" com . foo . package \",\" productId \":\" com . foo . package . sku \",\" auto Renew ing \": true ,\" pur " Ġ+ ĊĠĠĠĠĠ Ġ" ch ase Time \":\" ${ Date (). time }\ ", \" token \":\" 1 2 3 4 5 6 7 8 9 0 \" }", ĊĠĠĠĠĠ Ġ" debug - signature - com . foo . package . sku - subs ") ĊĠ Ġprivate Ġval Ġsubs Purchase 2 Ġ= ĠPurchase ("{ \" orderId \":\" bar - 1 2 3 4 \",\" packageName \": \"" Ġ+ ĊĠĠĠĠĠ Ġ" com . bar . package \",\" productId \":\" com . bar . package . sku \",\" auto Renew ing \": false ,\" pur cha " Ġ+ ĊĠĠĠĠĠ Ġ" se Time \":\" ${ Date (). time }\ ", \" purchase Token \":\" 0 9 8 7 6 5 4 3 2 1 \" }", ĊĠĠĠĠĠ Ġ" debug - signature - com . bar . package . sku - subs ") ĊĠ Ġprivate Ġval Ġin app Purchase 1 Ġ= ĠPurchase ("{ \" orderId \":\" foo - 1 2 3 4 \",\" packageName \ ":" Ġ+ ĊĠĠĠĠĠ Ġ"\" com . foo . package \",\" productId \":\" com . foo . package . sku \",\" auto Renew ing \": true ,\" pur " Ġ+ ĊĠĠĠĠĠ Ġ" ch ase Time \":\" ${ Date (). time }\ ", \" token \":\" 1 2 3 4 5 6 7 8 9 0 \" }", ĊĠĠĠĠĠ Ġ" debug - signature - com . foo . package . sku - in app ") ĊĠ Ġprivate Ġval Ġin app Purchase 2 Ġ= ĠPurchase ("{ \" orderId \":\" bar - 1 2 3 4 \",\" packageName \": \"" Ġ+ ĊĠĠĠĠĠ Ġ" com . bar . package \",\" productId \":\" com . bar . package . sku \",\" auto Renew ing \": false ,\" pur cha " Ġ+ ĊĠĠĠĠĠ Ġ" se Time \":\" ${ Date (). time }\ ", \" purchase Token \":\" 0 9 8 7 6 5 4 3 2 1 \" }", ĊĠĠĠĠĠ Ġ" debug - signature - com . bar . package . sku - in app ") ĊĊĠ Ġprivate Ġval Ġsubs 1 Json Ġ= Ġ"{\" productId \":\" com . foo . package . sku \",\" type \": \"" Ġ+ ĊĠĠĠĠĠ Ġ" subs \",\" price \":\" \$ 4 . 9 9 \",\" price _ amount _ micro s \":\" 4 9 9 0 0 0 0 \",\" price _ currency _ code \": \"" Ġ+ ĊĠĠĠĠĠ Ġ" USD \",\" title \":\" Foo \",\" description \":\" So Ġmuch ĠFoo \",\" subscription Period \":\" P 1 W \",\" f " Ġ+ ĊĠĠĠĠĠ Ġ" ree Trial Period \":\" P 1 W \" }" ĊĠ Ġprivate Ġval Ġsubs 2 Json Ġ= Ġ"{\" productId \":\" com . bar . package . sku \",\" type \": \"" Ġ+ ĊĠĠĠĠĠ Ġ" subs \",\" price \":\" \$ 9 . 9 9 \",\" price _ amount _ micro s \":\" 9 9 9 0 0 0 0 \",\" price _ currency _ code \": \"" Ġ+ ĊĠĠĠĠĠ Ġ" USD \",\" title \":\" Bar \",\" description \":\" So Ġmuch ĠBar \",\" subscription Period \":\" P 1 M \" }" ĊĠ Ġprivate Ġval Ġin app 1 Json Ġ= Ġ"{\" productId \":\" com . baz . package . sku \",\" type \": \"" Ġ+ ĊĠĠĠĠĠ Ġ" in app \",\" price \":\" \$ 1 4 . 9 9 \",\" price _ amount _ micro s \":\" 1 4 9 9 0 0 0 0 \",\" price _ currency _ code \ ":" Ġ+ ĊĠĠĠĠĠ Ġ"\" USD \",\" title \":\" B az \",\" description \":\" So Ġmuch ĠB az \" }" ĊĠ Ġprivate Ġval Ġsubs 1 Ġ= ĠSk u Details ( subs 1 Json ) ĊĠ Ġprivate Ġval Ġsubs 2 Ġ= ĠSk u Details ( subs 2 Json ) ĊĠ Ġprivate Ġval Ġin app 1 Ġ= ĠSk u Details ( in app 1 Json ) ĊĊĠ Ġ@ Before Ġfun Ġsetup () Ġ{ ĊĠĠĠ Ġapplication Ġ= Ġmock () ĊĠĠĠ Ġactivity Ġ= Ġmock Ġ{ ĊĠĠĠĠĠ Ġon Ġ{ Ġapplication Context Ġ} Ġdo Return Ġapplication ĊĠĠĠ Ġ} ĊĠĠĠ Ġstore Ġ= Ġmock Ġ{ ĊĠĠĠĠĠ Ġon Ġ{ Ġget P urchases ( eq ( Sku Type . IN APP )) Ġ} Ġdo Return ĠInternal P urchases Result ( Billing Response . OK , ĠlistOf ( in app Purchase 1 , Ġin app Purchase 2 )) ĊĠĠĠĠĠ Ġon Ġ{ Ġget P urchases ( eq ( Sku Type . SUB S )) Ġ} Ġdo Return ĠInternal P urchases Result ( Billing Response . OK , ĠlistOf ( subs Purchase 1 , Ġsubs Purchase 2 )) ĊĠĠĠĠĠ Ġon Ġ{ Ġget Sku Details ( any ()) Ġ} Ġdo Answer Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġparams Ġ= Ġit . arguments . first () Ġas ĠSk u Details Params ĊĠĠĠĠĠĠĠ Ġwhen Ġ( params . sku Type ) Ġ{ ĊĠĠĠĠĠĠĠĠĠ ĠSk u Type . SUB S Ġ-> ĠlistOf ( subs 1 , Ġsubs 2 ) ĊĠĠĠĠĠĠĠĠĠ ĠSk u Type . IN APP Ġ-> ĠlistOf ( in app 1 ) ĊĠĠĠĠĠĠĠĠĠ Ġelse Ġ-> Ġthrow ĠIllegalArgumentException (" Unknown Ġsku Type : Ġ${ params . sku Type }") ĊĠĠĠĠĠĠĠ Ġ}. filter Ġ{ Ġit . sku Ġin Ġparams . sk us List Ġ} ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠĠĠ Ġpurch ases Updated Listener Ġ= ĠFake P urchases Updated Listener () ĊĠĠĠ Ġclient Ġ= ĠDebug Billing Client ( activity , Ġpurch ases Updated Listener , ĊĠĠĠĠĠĠĠ ĠExecutor Ġ{ Ġcommand Ġ-> Ġcommand ?. run () Ġ}, Ġstore , Ġlocal Broadcast Interactor ) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġmethods Fail Without Started Connection () Ġ{ ĊĠĠĠ ĠassertThat ( client . is Ready ). isFalse () ĊĠĠĠ ĠassertThat ( client . is Feature Supported ( Feature Type . SUB SCRIPTION S )) ĊĠĠĠĠĠĠĠ Ġ. isEqualTo ( Billing Response . SERVICE _ DIS CONNECTED ) ĊĊĠĠĠ Ġ// Ġconnect ĊĠĠĠ Ġclient . start Connection ( empty State Listener ) ĊĊĠĠĠ ĠassertThat ( client . is Ready ). isTrue () ĊĠĠĠ ĠassertThat ( client . is Feature Supported ( Feature Type . SUB SCRIPTION S )) ĊĠĠĠĠĠĠĠ Ġ. isEqualTo ( Billing Response . OK ) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġquery P urchases Returns Disconnected () Ġ{ ĊĠĠĠ Ġval Ġresponse Ġ= Ġclient . query P urchases (" com . foo ") ĊĠĠĠ ĠassertThat ( response . response Code ). isEqualTo ( Billing Response . SERVICE _ DIS CONNECTED ) ĊĠĠĠ ĠassertThat ( response . p urchases List ). isNull () ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġquery P urchases Returns Saved Subscriptions () Ġ{ ĊĠĠĠ Ġclient . start Connection ( empty State Listener ) ĊĊĠĠĠ Ġval Ġresponse Ġ= Ġclient . query P urchases ( Sku Type . SUB S ) ĊĠĠĠ ĠassertThat ( response . response Code ). isEqualTo ( Billing Response . OK ) ĊĠĠĠ ĠassertThat ( response . p urchases List [ 0 ]). isEqualTo ( subs Purchase 1 ) ĊĠĠĠ ĠassertThat ( response . p urchases List [ 1 ]). isEqualTo ( subs Purchase 2 ) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġquery P urchases Returns Saved In App P urchases () Ġ{ ĊĠĠĠ Ġclient . start Connection ( empty State Listener ) ĊĊĠĠĠ Ġval Ġresponse Ġ= Ġclient . query P urchases ( Sku Type . IN APP ) ĊĠĠĠ ĠassertThat ( response . response Code ). isEqualTo ( Billing Response . OK ) ĊĠĠĠ ĠassertThat ( response . p urchases List [ 0 ]). isEqualTo ( in app Purchase 1 ) ĊĠĠĠ ĠassertThat ( response . p urchases List [ 1 ]). isEqualTo ( in app Purchase 2 ) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġquery Sku Details Async Returns Disconnected () Ġ{ ĊĠĠĠ Ġval Ġlistener Ġ= ĠFake Sku Details Response Listener () ĊĠĠĠ Ġclient . query Sku Details Async ( Sku Details Params . newBuilder () ĊĠĠĠĠĠĠĠ Ġ. setType ( Sku Type . SUB S ). set Sk us List ( listOf ()). build (), Ġlistener ) ĊĊĠĠĠ ĠassertThat ( listener . response Code ). isEqualTo ( Billing Response . SERVICE _ DIS CONNECTED ) ĊĠĠĠ ĠassertThat ( listener . sku Details List ). isNull () ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġquery Sku Details Async Returns Saved Subscriptions () Ġ{ ĊĠĠĠ Ġval Ġlistener Ġ= ĠFake Sku Details Response Listener () ĊĠĠĠ Ġclient . start Connection ( empty State Listener ) ĊĠĠĠ Ġclient . query Sku Details Async ( ĊĠĠĠĠĠĠĠ ĠSk u Details Params . newBuilder () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. setType ( Sku Type . SUB S ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Sk us List ( listOf ( subs 1 . sku , Ġsubs 2 . sku )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. build (), ĊĠĠĠĠĠĠĠ Ġlistener ) ĊĊĠĠĠ ĠassertThat ( listener . response Code ). isEqualTo ( Billing Response . OK ) ĊĠĠĠ ĠassertThat ( listener . sku Details List ). contains Exactly Elements In ( listOf ( subs 1 , Ġsubs 2 )) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġquery Sku Details Async Returns Saved In App Products () Ġ{ ĊĠĠĠ Ġval Ġlistener Ġ= ĠFake Sku Details Response Listener () ĊĠĠĠ Ġclient . start Connection ( empty State Listener ) ĊĠĠĠ Ġclient . query Sku Details Async ( ĊĠĠĠĠĠĠĠ ĠSk u Details Params . newBuilder () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. setType ( Sku Type . IN APP ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Sk us List ( listOf ( in app 1 . sku )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. build (), ĊĠĠĠĠĠĠĠ Ġlistener ) ĊĊĠĠĠ ĠassertThat ( listener . response Code ). isEqualTo ( Billing Response . OK ) ĊĠĠĠ ĠassertThat ( listener . sku Details List ). contains Exactly Elements In ( listOf ( in app 1 )) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġquery Purchase History Async Returns Disconnected () Ġ{ ĊĠĠĠ Ġval Ġlistener Ġ= ĠFake Purchase History Response Listener () ĊĠĠĠ Ġclient . query Purchase History Async ( Sku Type . SUB S , Ġlistener ) ĊĠĠĠ ĠassertThat ( listener . response Code ). isEqualTo ( Billing Response . SERVICE _ DIS CONNECTED ) ĊĠĠĠ ĠassertThat ( listener . p urchases List ). isNull () ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġquery Purchase History Async Returns Saved Subscriptions () Ġ{ ĊĠĠĠ Ġclient . start Connection ( empty State Listener ) ĊĊĠĠĠ Ġval Ġlistener Ġ= ĠFake Purchase History Response Listener () ĊĠĠĠ Ġclient . query Purchase History Async ( Sku Type . SUB S , Ġlistener ) ĊĠĠĠ ĠassertThat ( listener . response Code ). isEqualTo ( Billing Response . OK ) ĊĠĠĠ ĠassertThat ( listener . p urchases List ). contains Exactly Elements In ( listOf ( subs Purchase 1 , Ġsubs Purchase 2 )) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġquery Purchase History Async Returns Saved In App Products () Ġ{ ĊĠĠĠ Ġclient . start Connection ( empty State Listener ) ĊĊĠĠĠ Ġval Ġlistener Ġ= ĠFake Purchase History Response Listener () ĊĠĠĠ Ġclient . query Purchase History Async ( Sku Type . IN APP , Ġlistener ) ĊĠĠĠ ĠassertThat ( listener . response Code ). isEqualTo ( Billing Response . OK ) ĊĠĠĠ ĠassertThat ( listener . p urchases List ). contains Exactly Elements In ( listOf ( in app Purchase 1 , Ġin app Purchase 2 )) ĊĠ Ġ} ĊĊĠ Ġclass ĠFake Sku Details Response Listener Ġ: ĠSk u Details Response Listener Ġ{ ĊĠĠĠ Ġvar Ġresponse Code : ĠInt ? Ġ= Ġnull ĊĠĠĠ Ġvar Ġsku Details List : ĠList < Sku Details >? Ġ= Ġnull ĊĠĠĠ Ġoverride Ġfun Ġon Sku Details Response ( response Code : ĠInt , Ġsku Details List : ĠMutable List < Sku Details > ?) Ġ{ ĊĠĠĠĠĠ Ġthis . response Code Ġ= Ġresponse Code ĊĠĠĠĠĠ Ġthis . sku Details List Ġ= Ġsku Details List ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġclass ĠFake Purchase History Response Listener Ġ: ĠPurchase History Response Listener Ġ{ ĊĠĠĠ Ġvar Ġresponse Code : ĠInt ? Ġ= Ġnull ĊĠĠĠ Ġvar Ġpurch ases List : ĠMutable List < Purchase >? Ġ= Ġnull ĊĠĠĠ Ġoverride Ġfun Ġon Purchase History Response ( response Code : ĠInt , Ġpurch ases List : ĠMutable List < Purchase > ?) Ġ{ ĊĠĠĠĠĠ Ġthis . response Code Ġ= Ġresponse Code ĊĠĠĠĠĠ Ġthis . p urchases List Ġ= Ġpurch ases List ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġclass ĠFake P urchases Updated Listener Ġ: ĠP urchases Updated Listener Ġ{ ĊĠĠĠ Ġvar Ġresponse Code : ĠInt ? Ġ= Ġnull ĊĠĠĠ Ġvar Ġpurch ases List : ĠMutable List < Purchase >? Ġ= Ġnull ĊĠĠĠ Ġoverride Ġfun Ġon P urchases Updated ( response Code : ĠInt , Ġpurch ases : ĠMutable List < Purchase > ?) Ġ{ ĊĠĠĠĠĠ Ġthis . response Code Ġ= Ġresponse Code ĊĠĠĠĠĠ Ġthis . p urchases List Ġ= Ġpurch ases ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ }