/* Ċ Ġ* ĠCopyright Ġ 2 0 2 2 ĠThe ĠMatrix . org ĠFoundation ĠC . I . C . Ċ Ġ* Ċ Ġ* Ġ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 Ġorg . matrix . android . sdk . internal . session . room . aggregation . poll Ċ Ċ import Ġio . mock k . every Ċ import Ġio . mock k . mock k Ċ import Ġio . realm . Realm List Ċ import Ġio . realm . Realm Model Ċ import Ġio . realm . Realm Query Ċ import Ġorg . am sh ove . k lu ent . shouldBe False Ċ import Ġorg . am sh ove . k lu ent . shouldBe True Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġorg . matrix . android . sdk . api . session . Session Ċ import Ġorg . matrix . android . sdk . api . session . get Room Ċ import Ġorg . matrix . android . sdk . api . session . room . Room Ċ import Ġorg . matrix . android . sdk . api . session . room . getTime line Event Ċ import Ġorg . matrix . android . sdk . api . session . room . power levels . Power Levels Helper Ċ import Ġorg . matrix . android . sdk . internal . database . model . Event Annotations Summary Entity Ċ import Ġorg . matrix . android . sdk . internal . database . model . Event Annotations Summary Entity Fields Ċ import Ġorg . matrix . android . sdk . internal . database . model . Poll Response Ag gregated Summary Entity Ċ import Ġorg . matrix . android . sdk . internal . session . room . aggregation . poll . Poll Events TestData . AN _ EVENT _ ID Ċ import Ġorg . matrix . android . sdk . internal . session . room . aggregation . poll . Poll Events TestData . AN _ INVALID _ POLL _ RESPONSE _ EVENT Ċ import Ġorg . matrix . android . sdk . internal . session . room . aggregation . poll . Poll Events TestData . A _ BRO KEN _ POLL _ REPLACE _ EVENT Ċ import Ġorg . matrix . android . sdk . internal . session . room . aggregation . poll . Poll Events TestData . A _ POLL _ END _ EVENT Ċ import Ġorg . matrix . android . sdk . internal . session . room . aggregation . poll . Poll Events TestData . A _ POLL _ REFERENCE _ EVENT Ċ import Ġorg . matrix . android . sdk . internal . session . room . aggregation . poll . Poll Events TestData . A _ POLL _ REPLACE _ EVENT Ċ import Ġorg . matrix . android . sdk . internal . session . room . aggregation . poll . Poll Events TestData . A _ POLL _ RESPONSE _ EVENT Ċ import Ġorg . matrix . android . sdk . internal . session . room . aggregation . poll . Poll Events TestData . A _ POLL _ RESPONSE _ EVENT _ WITH _ A _ WRONG _ REFERENCE Ċ import Ġorg . matrix . android . sdk . internal . session . room . aggregation . poll . Poll Events TestData . A _ POLL _ START _ EVENT Ċ import Ġorg . matrix . android . sdk . internal . session . room . aggregation . poll . Poll Events TestData . A _ ROOM _ ID Ċ import Ġorg . matrix . android . sdk . internal . session . room . aggregation . poll . Poll Events TestData . A _ TIME LINE _ EVENT Ċ import Ġorg . matrix . android . sdk . internal . session . room . aggregation . poll . Poll Events TestData . A _ USER _ ID _ 1 Ċ import Ġorg . matrix . android . sdk . test . f akes . Fake Realm Ċ Ċ class ĠPoll Aggregation Processor Test Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġpoll Aggregation Processor : ĠPoll Aggregation Processor Ġ= ĠDefault Poll Aggregation Processor () ĊĠĠĠ Ġprivate Ġval Ġrealm Ġ= ĠFake Realm () ĊĠĠĠ Ġprivate Ġval Ġsession Ġ= Ġmock k < Session >() ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun Ġsetup () Ġ{ ĊĠĠĠĠĠĠĠ Ġmock Event Annotations Summary Entity () ĊĠĠĠĠĠĠĠ Ġmock Room ( A _ ROOM _ ID , ĠAN _ EVENT _ ID ) ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġsession . my UserId Ġ} Ġreturns ĠA _ USER _ ID _ 1 ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` given Ġa Ġpoll Ġstart Ġevent , Ġwhen Ġprocessing , Ġthen Ġis Ġignored Ġand Ġreturns Ġfalse `() Ġ{ ĊĠĠĠĠĠĠĠ Ġpoll Aggregation Processor . handle Poll Start Event ( realm . instance , ĠA _ POLL _ START _ EVENT ). shouldBe False () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` given Ġa Ġpoll Ġstart Ġevent Ġwith Ġa Ġreference , Ġwhen Ġprocessing , Ġthen Ġis Ġignored Ġand Ġreturns Ġfalse `() Ġ{ ĊĠĠĠĠĠĠĠ Ġpoll Aggregation Processor . handle Poll Start Event ( realm . instance , ĠA _ POLL _ REFERENCE _ EVENT ). shouldBe False () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` given Ġa Ġpoll Ġstart Ġevent Ġwith Ġa Ġreplace Ġrelation Ġbut Ġwithout Ġa Ġtarget Ġevent Ġid , Ġwhen Ġprocessing , Ġthen Ġis Ġignored Ġand Ġreturns Ġfalse `() Ġ{ ĊĠĠĠĠĠĠĠ Ġpoll Aggregation Processor . handle Poll Start Event ( realm . instance , ĠA _ BRO KEN _ POLL _ REPLACE _ EVENT ). shouldBe False () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` given Ġa Ġpoll Ġstart Ġevent Ġwith Ġa Ġreplace , Ġwhen Ġprocessing , Ġthen Ġis Ġprocessed Ġand Ġreturns Ġtrue `() Ġ{ ĊĠĠĠĠĠĠĠ Ġpoll Aggregation Processor . handle Poll Start Event ( realm . instance , ĠA _ POLL _ REPLACE _ EVENT ). shouldBe True () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` given Ġa Ġpoll Ġresponse Ġevent Ġwith Ġa Ġbroken Ġreference , Ġwhen Ġprocessing , Ġthen Ġis Ġignored Ġand Ġreturns Ġfalse `() Ġ{ ĊĠĠĠĠĠĠĠ Ġpoll Aggregation Processor . handle Poll Response Event ( session , Ġrealm . instance , ĠA _ POLL _ RESPONSE _ EVENT _ WITH _ A _ WRONG _ REFERENCE ). shouldBe False () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` given Ġa Ġpoll Ġresponse Ġevent Ġwith Ġa Ġreference , Ġwhen Ġprocessing , Ġthen Ġis Ġprocessed Ġand Ġreturns Ġtrue `() Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġrealm . instance . createObject ( Poll Response Ag gregated Summary Entity :: class . java ) Ġ} Ġreturns ĠPoll Response Ag gregated Summary Entity () ĊĠĠĠĠĠĠĠ Ġpoll Aggregation Processor . handle Poll Response Event ( session , Ġrealm . instance , ĠA _ POLL _ RESPONSE _ EVENT ). shouldBe True () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` given Ġa Ġpoll Ġresponse Ġevent Ġafter Ġpoll Ġis Ġclosed , Ġwhen Ġprocessing , Ġthen Ġis Ġignored Ġand Ġreturns Ġfalse `() Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġrealm . instance . createObject ( Poll Response Ag gregated Summary Entity :: class . java ) Ġ} Ġreturns ĠPoll Response Ag gregated Summary Entity (). apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġclosed Time Ġ= Ġ( A _ POLL _ RESPONSE _ EVENT . origin Server Ts Ġ?: Ġ 0 ) Ġ- Ġ 1 ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġpoll Aggregation Processor . handle Poll Response Event ( session , Ġrealm . instance , ĠA _ POLL _ RESPONSE _ EVENT ). shouldBe False () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` given Ġa Ġpoll Ġresponse Ġevent Ġwhich Ġis Ġalready Ġprocessed , Ġwhen Ġprocessing , Ġthen Ġis Ġignored Ġand Ġreturns Ġfalse `() Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġrealm . instance . createObject ( Poll Response Ag gregated Summary Entity :: class . java ) Ġ} Ġreturns ĠPoll Response Ag gregated Summary Entity (). apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsource Events Ġ= ĠRealm List ( A _ POLL _ RESPONSE _ EVENT . eventId ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġpoll Aggregation Processor . handle Poll Response Event ( session , Ġrealm . instance , ĠA _ POLL _ RESPONSE _ EVENT ). shouldBe False () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` given Ġa Ġpoll Ġresponse Ġevent Ġwhich Ġis Ġnot Ġone Ġof Ġthe Ġoptions , Ġwhen Ġprocessing , Ġthen Ġis Ġignored Ġand Ġreturns Ġfalse `() Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġrealm . instance . createObject ( Poll Response Ag gregated Summary Entity :: class . java ) Ġ} Ġreturns ĠPoll Response Ag gregated Summary Entity () ĊĠĠĠĠĠĠĠ Ġpoll Aggregation Processor . handle Poll Response Event ( session , Ġrealm . instance , ĠAN _ INVALID _ POLL _ RESPONSE _ EVENT ). shouldBe False () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` given Ġa Ġpoll Ġend Ġevent , Ġwhen Ġprocessing , Ġthen Ġis Ġprocessed Ġand Ġreturn Ġtrue `() Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġrealm . instance . createObject ( Poll Response Ag gregated Summary Entity :: class . java ) Ġ} Ġreturns ĠPoll Response Ag gregated Summary Entity () ĊĠĠĠĠĠĠĠ Ġval Ġpower Levels Helper Ġ= Ġmock Red action Power Levels ( A _ USER _ ID _ 1 , Ġtrue ) ĊĠĠĠĠĠĠĠ Ġpoll Aggregation Processor . handle Poll End Event ( session , Ġpower Levels Helper , Ġrealm . instance , ĠA _ POLL _ END _ EVENT ). shouldBe True () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` given Ġa Ġpoll Ġend Ġevent Ġfor Ġmy Ġown Ġpoll Ġwithout Ġenough Ġred action Ġpower Ġlevel , Ġwhen Ġprocessing , Ġthen Ġis Ġprocessed Ġand Ġreturns Ġtrue `() Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġrealm . instance . createObject ( Poll Response Ag gregated Summary Entity :: class . java ) Ġ} Ġreturns ĠPoll Response Ag gregated Summary Entity () ĊĠĠĠĠĠĠĠ Ġval Ġpower Levels Helper Ġ= Ġmock Red action Power Levels ( A _ USER _ ID _ 1 , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġpoll Aggregation Processor . handle Poll End Event ( session , Ġpower Levels Helper , Ġrealm . instance , ĠA _ POLL _ END _ EVENT ). shouldBe True () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` given Ġa Ġpoll Ġend Ġevent Ġwithout Ġenough Ġred action Ġpower Ġlevel , Ġwhen Ġis Ġprocessed , Ġthen Ġis Ġignored Ġand Ġreturn Ġfalse `() Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġrealm . instance . createObject ( Poll Response Ag gregated Summary Entity :: class . java ) Ġ} Ġreturns ĠPoll Response Ag gregated Summary Entity () ĊĠĠĠĠĠĠĠ Ġval Ġpower Levels Helper Ġ= Ġmock Red action Power Levels (" another - sender - id ", Ġfalse ) ĊĠĠĠĠĠĠĠ Ġval Ġevent Ġ= ĠA _ POLL _ END _ EVENT . copy ( sender Id Ġ= Ġ" another - sender - id ") ĊĠĠĠĠĠĠĠ Ġpoll Aggregation Processor . handle Poll End Event ( session , Ġpower Levels Helper , Ġrealm . instance , Ġevent ). shouldBe False () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġinline Ġfun Ġ< re ified ĠT Ġ: ĠRealm Model > ĠRealm Query < T >. given EqualTo ( fieldName : ĠString , Ġvalue : ĠString , Ġresult : ĠRealm Query < T >) Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ ĠequalTo ( fieldName , Ġvalue ) Ġ} Ġreturns Ġresult ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġmock Event Annotations Summary Entity () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġquery Result Ġ= Ġrealm . given Where Returns ( result Ġ= ĠEvent Annotations Summary Entity ()) ĊĠĠĠĠĠĠĠ Ġquery Result . given EqualTo ( Event Annotations Summary Entity Fields . ROOM _ ID , ĠA _ POLL _ REPLACE _ EVENT . roomId !! , Ġquery Result ) ĊĠĠĠĠĠĠĠ Ġquery Result . given EqualTo ( Event Annotations Summary Entity Fields . EVENT _ ID , ĠA _ POLL _ REPLACE _ EVENT . eventId !! , Ġquery Result ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġmock Room ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġroom Id : ĠString , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġevent Id : ĠString ĊĠĠĠ Ġ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġroom Ġ= Ġmock k < Room >() ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġsession . get Room ( roomId ) Ġ} Ġreturns Ġroom ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġroom . getTime line Event ( eventId ) Ġ} Ġreturns ĠA _ TIME LINE _ EVENT ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġmock Red action Power Levels ( userId : ĠString , Ġis Ab le To Red act : ĠBoolean ): ĠPower Levels Helper Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġpower Levels Helper Ġ= Ġmock k < Power Levels Helper >() ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġpower Levels Helper . is User Ab le To Red act ( userId ) Ġ} Ġreturns Ġis Ab le To Red act ĊĠĠĠĠĠĠĠ Ġreturn Ġpower Levels Helper ĊĠĠĠ Ġ} Ċ } Ċ