// ĠCopyright Ġ 2 0 1 8 - 2 0 2 0 ĠTwitter , ĠInc . Ċ // ĠLicensed Ġunder Ġthe ĠMo Pub ĠSDK ĠLicense ĠAgreement Ċ // Ġhttp :// www . m op ub . com / legal / sdk - license - agreement / Ċ Ċ package Ġcom . m op ub . network Ċ Ċ import Ġandroid . app . Activity Ċ import Ġandroid . webkit . Web Settings Ċ Ċ import Ġcom . m op ub . common . test . support . Sdk TestRunner Ċ Ċ import Ġorg . junit . After Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . power mock . api . mockito . Power Mockito Ċ import Ġorg . power mock . core . class loader . annotations . Power Mock Ignore Ċ import Ġorg . power mock . core . class loader . annotations . Prepare For Test Ċ import Ġorg . power mock . modules . junit 4 . rule . Power Mock Rule Ċ import Ġorg . ro bo lectric . Ro bo lectric Ċ Ċ import Ġjava . util . concurrent . Count DownLatch Ċ import Ġjava . util . concurrent . TimeUnit Ċ Ċ import Ġorg . fest . assertions . api . Assertions . assertThat Ċ import Ġorg . power mock . api . mockito . Power Mockito . mock Static Ċ Ċ @ Power Mock Ignore (" org . mockito .* ", Ġ" org . ro bo lectric .* ", Ġ" android .* ", Ġ" org . json .* ") Ċ @ Prepare For Test ( Web Settings :: class ) Ċ @ RunWith ( Sdk TestRunner :: class ) Ċ class ĠNetwork ing Test Ġ{ ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġcontext : ĠActivity ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġrule Ġ= ĠPower Mock Rule () ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ Ġcontext Ġ= ĠRo bo lectric . build Activity ( Activity :: class . java ). create (). get () ĊĠĠĠĠĠĠĠ Ġmock Static ( Web Settings :: class . java ) ĊĊĠĠĠĠĠĠĠ ĠPower Mockito .` when `( Web Settings . getDefault UserAgent ( context )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. thenReturn (" some Ġandroid Ġuser Ġagent ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ After ĊĠĠĠ Ġfun ĠtearDown () Ġ{ ĊĠĠĠĠĠĠĠ ĠNetwork ing . clear ForTesting () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun ĠgetUser Agent _ uses Cached UserAgent () Ġ{ ĊĠĠĠĠĠĠĠ ĠNetwork ing . set UserAgent ForTesting (" some Ġcached Ġuser Ġagent ") ĊĠĠĠĠĠĠĠ Ġval Ġuser Agent Ġ= ĠNetwork ing . getUser Agent ( context ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( userAgent ). isEqualTo (" some Ġcached Ġuser Ġagent ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun ĠgetUser Agent _ should Include Android () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġuser Agent Ġ= ĠNetwork ing . getUser Agent ( context ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( userAgent ). isEqualTo (" some Ġandroid Ġuser Ġagent ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġ@ Throws ( InterruptedException :: class ) ĊĠĠĠ Ġfun ĠgetUser Agent _ when On AB ack ground Thread _ should Return Http Agent () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġuser Agent Ġ= ĠarrayOf Null s < String >( 1 ) ĊĠĠĠĠĠĠĠ Ġval Ġl atch Ġ= ĠCount DownLatch ( 1 ) ĊĠĠĠĠĠĠĠ Ġobject Ġ: ĠThread () Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun Ġrun () Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġuser Agent [ 0 ] Ġ= ĠNetwork ing . getUser Agent ( context ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġl atch . count Down () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ}. start () ĊĊĠĠĠĠĠĠĠ Ġl atch . await ( 5 0 0 , ĠTimeUnit . MILLISECONDS ) ĊĠĠĠĠĠĠĠ Ġ// ĠRo bo lectric 's Ġdefault Ġhttp Ġagent Ġis Ġnull Ġwhich Ġgets Ġre written Ġto Ġan Ġempty ĠString . ĊĠĠĠĠĠĠĠ ĠassertThat ( userAgent [ 0 ]). isEqualTo ("") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Cached UserAgent _ uses Cached UserAgent () Ġ{ ĊĠĠĠĠĠĠĠ ĠNetwork ing . set UserAgent ForTesting (" some Ġcached Ġuser Ġagent ") ĊĠĠĠĠĠĠĠ Ġval Ġuser Agent Ġ= ĠNetwork ing . cached UserAgent ĊĊĠĠĠĠĠĠĠ ĠassertThat ( userAgent ). isEqualTo (" some Ġcached Ġuser Ġagent ") ĊĠĠĠ Ġ} Ċ } Ċ