package Ġcom . sensor berg . observable Ċ Ċ import Ġorg . junit . Assert .* Ċ import Ġorg . junit . Test Ċ import Ġjava . util . concurrent . Count DownLatch Ċ import Ġjava . util . concurrent . TimeUnit Ċ import Ġjava . util . concurrent . atomic . Atomic Boolean Ċ import Ġjava . util . concurrent . atomic . Atomic Reference Ċ Ċ class ĠObservable Data Test Ġ{ ĊĊ ĉ comp anion Ġobject Ġ{ Ċĉ ĉ internal Ġconst Ġval ĠTIME OUT Ġ= Ġ 2 0 L Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġall _ ob servers _ receive _ values () Ġ{ Ċĉ ĉ val Ġtested Ġ= ĠMutable Observable Data < String >() Ċĉ ĉ val Ġwait Ġ= ĠCount DownLatch ( 3 ) Ċĉ ĉ repeat ( 3 ) Ġ{ Ċĉĉ ĉ tested . observe Ġ{ Ġwait . count Down () Ġ} Ċĉ ĉ } Ċĉ ĉ tested . value Ġ= Ġ" true " Ċĉ ĉ assertTrue ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġall _ ob servers _ receive _ values _ tw ice () Ġ{ Ċĉ ĉ val Ġtested Ġ= ĠMutable Observable Data < String >() Ċĉ ĉ var Ġwait Ġ= ĠCount DownLatch ( 3 ) Ċĉ ĉ repeat ( 3 ) Ġ{ Ċĉĉ ĉ tested . observe Ġ{ Ċĉĉĉ ĉ if Ġ( wait . count Ġ> Ġ 0 ) Ġ{ Ċĉĉĉĉ ĉ wait . count Down () Ċĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉ ĉ wait . count Down () Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉ ĉ } Ċĉ ĉ tested . value Ġ= Ġ" true " Ċĉ ĉ assertTrue ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) Ċĉ ĉ wait Ġ= ĠCount DownLatch ( 3 ) Ċĉ ĉ tested . value Ġ= Ġ" false " Ċĉ ĉ assertTrue ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġif _ set _ value _ is _ not _ called _ observer _ does nt _ get _ called () Ġ{ Ċĉ ĉ val Ġtested Ġ= ĠMutable Observable Data < String >() Ċĉ ĉ val Ġwait Ġ= ĠCount DownLatch ( 1 ) Ċĉ ĉ tested . observe Ġ{ Ġwait . count Down () Ġ} Ċĉ ĉ assertFalse ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġobserver _ immediate ly _ re ceiv es _ value _ already _ stored () Ġ{ Ċĉ ĉ val Ġtested Ġ= ĠMutable Observable Data < String >() Ċĉ ĉ var Ġwait Ġ= ĠCount DownLatch ( 1 ) Ċĉ ĉ tested . observe Ġ{ Ġwait . count Down () Ġ} Ċĉ ĉ tested . value Ġ= Ġ" true " Ċĉ ĉ assertTrue ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) ĊĊĉ ĉ val Ġreceived Ġ= ĠAtomic Reference < String >() Ċĉ ĉ wait Ġ= ĠCount DownLatch ( 1 ) Ċĉ ĉ tested . observe Ġ{ Ċĉĉ ĉ received . set ( it ) Ċĉĉ ĉ wait . count Down () Ċĉ ĉ } Ċĉ ĉ assertTrue ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) Ċĉ ĉ assertEquals (" true ", Ġreceived . get ()) ĊĊ ĉ } ĊĊ ĉ @ Test Ġfun Ġset _ value _ tw ice _ observer _ only _ receive _ last _ value () Ġ{ Ċĉ ĉ val Ġtested Ġ= ĠMutable Observable Data < String >() Ċĉ ĉ val Ġreceived Ġ= ĠAtomic Reference < String >() Ċĉ ĉ val Ġwait Ġ= ĠCount DownLatch ( 1 ) Ċĉ ĉ tested . observe Ġ{ Ċĉĉ ĉ received . set ( it ) Ċĉĉ ĉ wait . count Down () Ċĉ ĉ } Ċĉ ĉ tested . value Ġ= Ġ" true " Ċĉ ĉ tested . value Ġ= Ġ" false " Ċĉ ĉ wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS ) Ċĉ ĉ assertEquals (" false ", Ġreceived . get ()) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġset _ value _ tw ice _ observer _ only _ receive _ once () Ġ{ Ċĉ ĉ val Ġtested Ġ= ĠMutable Observable Data < String >() Ċĉ ĉ val Ġwait Ġ= ĠCount DownLatch ( 2 ) Ċĉ ĉ tested . value Ġ= Ġ" true " Ċĉ ĉ tested . value Ġ= Ġ" false " Ċĉ ĉ tested . observe Ġ{ Ġwait . count Down () Ġ} Ċĉ ĉ assertFalse ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġon _ active _ inactive _ are _ proper ly _ called () Ġ{ Ċĉ ĉ val Ġwait Ġ= ĠCount DownLatch ( 2 ) Ċĉ ĉ val Ġactive Ġ= ĠAtomic Boolean ( false ) Ċĉ ĉ val Ġinactive Ġ= ĠAtomic Boolean ( false ) Ċĉ ĉ val Ġtested Ġ= Ġobject Ġ: ĠMutable Observable Data < String >() Ġ{ Ċĉĉ ĉ override Ġfun Ġon Active () Ġ{ Ċĉĉĉ ĉ active . set ( true ) Ċĉĉĉ ĉ wait . count Down () Ċĉĉ ĉ } ĊĊĉĉ ĉ override Ġfun Ġon Inactive () Ġ{ Ċĉĉĉ ĉ inactive . set ( true ) Ċĉĉĉ ĉ wait . count Down () Ċĉĉ ĉ } Ċĉ ĉ } ĊĊĉ ĉ val Ġobserver : ĠObserver < String > Ġ= Ġ{} Ċĉ ĉ tested . observe ( observer ) Ċĉ ĉ tested . value Ġ= Ġ" true " Ċĉ ĉ tested . remove Observer ( observer ) Ċĉ ĉ assertTrue ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) Ċĉ ĉ assertTrue ( active . get ()) Ċĉ ĉ assertTrue ( inactive . get ()) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġob servers _ are _ called _ on _ separate _ thread () Ġ{ Ċĉ ĉ val Ġwait Ġ= ĠCount DownLatch ( 1 ) Ċĉ ĉ val Ġtested Ġ= ĠMutable Observable Data < String >() Ċĉ ĉ val Ġtest Thread Ġ= ĠThread . currentThread () Ċĉ ĉ val Ġobserver Thread Ġ= ĠAtomic Reference < Thread >() Ċĉ ĉ tested . observe Ġ{ Ċĉĉ ĉ wait . count Down () Ċĉĉ ĉ observer Thread . set ( Thread . currentThread ()) Ċĉ ĉ } Ċĉ ĉ tested . value Ġ= Ġ" true " Ċĉ ĉ assertTrue ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) Ċĉ ĉ assertNot Equals ( test Thread , Ġobserver Thread . get ()) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġget _ value _ returns _ value _ after _ execution _ on _ background _ thread () Ġ{ Ċĉ ĉ val Ġwait Ġ= ĠCount DownLatch ( 1 ) Ċĉ ĉ val Ġtested Ġ= ĠMutable Observable Data < String >() Ċĉ ĉ tested . observe Ġ{ Ġwait . count Down () Ġ} Ċĉ ĉ tested . value Ġ= Ġ" true " Ċĉ ĉ assertTrue ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) Ċĉ ĉ assertEquals (" true ", Ġtested . value ) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġhas _ observer _ values _ are _ correct () Ġ{ Ċĉ ĉ var Ġwait Ġ= ĠCount DownLatch ( 1 ) Ċĉ ĉ val Ġtested Ġ= Ġobject Ġ: ĠMutable Observable Data < String >() Ġ{ Ċĉĉ ĉ override Ġfun Ġon Active () Ġ{ Ċĉĉĉ ĉ wait . count Down () Ċĉĉ ĉ } ĊĊĉĉ ĉ override Ġfun Ġon Inactive () Ġ{ Ċĉĉĉ ĉ wait . count Down () Ċĉĉ ĉ } Ċĉ ĉ } Ċĉ ĉ val Ġobserver : ĠObserver < String > Ġ= Ġ{ Ġwait . count Down () Ġ} ĊĊĉ ĉ assertFalse ( tested . has Observer s ()) Ċĉ ĉ tested . observe ( observer ) Ċĉ ĉ assertTrue ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) Ċĉ ĉ assertTrue ( tested . has Observer s ()) ĊĊĉ ĉ wait Ġ= ĠCount DownLatch ( 1 ) Ċĉ ĉ tested . remove Observer ( observer ) Ċĉ ĉ assertTrue ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) Ċĉ ĉ assertFalse ( tested . has Observer s ()) Ċ ĉ } ĊĊ ĉ @ Test Ġfun Ġvalue _ changes _ inside _ on _ active _ gets _ dis patched _ and _ previous _ one _ does nt () Ġ{ Ċĉ ĉ val Ġwait Ġ= ĠCount DownLatch ( 2 ) Ċĉ ĉ val Ġreceived Ġ= ĠAtomic Reference < String >() Ċĉ ĉ val Ġtested Ġ= Ġobject Ġ: ĠMutable Observable Data < String >() Ġ{ Ċĉĉ ĉ override Ġfun Ġon Active () Ġ{ Ċĉĉĉ ĉ if Ġ( has Observer s ()) Ġ{ Ċĉĉĉĉ ĉ this . value Ġ= Ġ" false " Ċĉĉĉ ĉ } Ċĉĉĉ ĉ wait . count Down () Ċĉĉ ĉ } Ċĉ ĉ } Ċĉ ĉ tested . value Ġ= Ġ" true " Ċĉ ĉ tested . observe Ġ{ Ċĉĉ ĉ received . set ( it ) Ċĉĉ ĉ wait . count Down () Ċĉ ĉ } Ċĉ ĉ assertTrue ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) Ċĉ ĉ assertEquals (" false ", Ġreceived . get ()) Ċ ĉ } ĊĊ ĉ @ Test Ġfun ĠgetValue _ matches _ value _ received _ on _ observer () Ġ{ Ċĉ ĉ val Ġtested Ġ= ĠMutable Observable Data < String >() Ċĉ ĉ val Ġfirst Ġ= ĠCount DownLatch ( 1 ) ĊĊĉ ĉ // Ġadd Ġsome Ġvalue Ċĉ ĉ tested . value Ġ= Ġ" true " Ċĉ ĉ tested . observe Ġ{ Ġfirst . count Down () Ġ} Ċĉ ĉ assertTrue ( first . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) ĊĊĉ ĉ // Ġchange Ġvalue Ċĉ ĉ tested . value Ġ= Ġ" false " Ċĉ ĉ val Ġwait Ġ= ĠCount DownLatch ( 1 ) Ċĉ ĉ val Ġreceived 1 Ġ= ĠAtomic Reference < String >() Ċĉ ĉ val Ġreceived 2 Ġ= ĠAtomic Reference < String >() Ċĉ ĉ val Ġobserver : ĠObserver < String > Ġ= Ġ{ Ċĉĉ ĉ received 1 . set ( it ) Ċĉĉ ĉ received 2 . set ( tested . value ) Ċĉĉ ĉ assertEquals ( it , Ġtested . value ) Ċĉĉ ĉ wait . count Down () Ċĉ ĉ } Ċĉ ĉ tested . observe ( observer ) ĊĊĉ ĉ assertTrue ( wait . await ( TIMEOUT , ĠTimeUnit . MILLISECONDS )) Ċĉ ĉ assertEquals (" false ", Ġreceived 1 . get ()) Ċĉ ĉ assertEquals (" false ", Ġreceived 2 . get ()) Ċĉ ĉ assertEquals (" false ", Ġtested . value ) ĊĊ ĉ } Ċ }