package Ġorg . chr js . rating dial ok sample Ċ Ċ import Ġandroid . content . SharedPreferences Ċ import Ġandroidx . test . Instrumentation Registry Ċ import Ġandroidx . test . espress o . Esp ress o . on View Ċ import Ġandroidx . test . espress o . action . View Actions . click Ċ import Ġandroidx . test . espress o . assertion . View Assertions . does NotExist Ċ import Ġandroidx . test . espress o . assertion . View Assertions . matches Ċ import Ġandroidx . test . espress o . matcher . View Matchers .* Ċ import Ġandroidx . test . rule . Activity Test Rule Ċ import Ġandroidx . test . runner . Android JUnit 4 Ċ import Ġandroid . text . format . Date Utils Ċ import Ġandroid . view . View Ċ import Ġorg . hamcrest . Matchers . all Of Ċ import Ġorg . hamcrest . Matchers . not Ċ import Ġorg . junit . Assert .* Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġjava . util .* ĊĊ Ċ @ Suppress (" Red undant Visibility Modifier ") Ċ @ RunWith ( Android JUnit 4 :: class ) Ċ public Ġclass ĠMainActivity Ui Test Ġ{ ĊĊĠĠĠ Ġ@ Rule ĊĠĠĠ Ġ@ Jvm Field ĊĠĠĠ Ġvar Ġactivity Test Rule Ġ= ĠActivity Test Rule < MainActivity >( MainActivity :: class . java , Ġtrue , Ġfalse ) ĊĊĠĠĠ Ġprivate Ġval Ġpreferences : ĠSharedPreferences Ġby Ġlazy Ġ{ ĊĠĠĠĠĠĠĠ ĠInstrument ation Registry . getTarget Context (). get SharedPreferences (" rating Dial ok ", Ġ 0 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġval Ġpreferences Editor : ĠSharedPreferences . Editor Ġby Ġlazy Ġ{ ĊĠĠĠĠĠĠĠ Ġpreferences . edit () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġprivate Ġconst Ġval ĠKEY _ USER _ HAS _ R ATED Ġ= Ġ" RD _ KEY _ USER _ HAS _ R ATED " ĊĠĠĠĠĠĠĠ Ġprivate Ġconst Ġval ĠKEY _ NE VER _ REM IND _ AG AIN Ġ= Ġ" RD _ KEY _ NE VER _ REM IND _ AG AIN " ĊĠĠĠĠĠĠĠ Ġprivate Ġconst Ġval ĠKEY _ FIRST _ START _ DATE Ġ= Ġ" RD _ KEY _ FIRST _ START _ DATE " ĊĠĠĠĠĠĠĠ Ġprivate Ġconst Ġval ĠKEY _ LA UNCH _ COUNT Ġ= Ġ" RD _ KEY _ LA UNCH _ COUNT " ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġclear Shared Prefs () Ġ{ ĊĠĠĠĠĠĠĠ Ġpreferences Editor . clear (). apply () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġset Conditions Are Met () Ġ{ ĊĠĠĠĠĠĠĠ Ġpreferences Editor . put Int ( KEY _ LA UNCH _ COUNT , Ġ 1 0 ). apply () ĊĠĠĠĠĠĠĠ Ġval Ġcal Ġ= ĠCalendar . getInstance () ĊĠĠĠĠĠĠĠ Ġcal . add ( Calendar . DAY _ OF _ YEAR , Ġ- 1 0 ) ĊĠĠĠĠĠĠĠ Ġpreferences Editor . put Long ( KEY _ FIRST _ START _ DATE , Ġcal . time In Millis ). apply () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġreset Should Reset All The Data () Ġ{ ĊĠĠĠĠĠĠĠ Ġpreferences Editor . put Int ( KEY _ LA UNCH _ COUNT , Ġ 2 ) ĊĠĠĠĠĠĠĠ Ġval Ġcal Ġ= ĠCalendar . getInstance () ĊĠĠĠĠĠĠĠ Ġcal . add ( Calendar . DAY _ OF _ YEAR , Ġ- 3 ) ĊĠĠĠĠĠĠĠ Ġpreferences Editor . put Long ( KEY _ FIRST _ START _ DATE , Ġcal . time In Millis ) ĊĠĠĠĠĠĠĠ Ġpreferences Editor . put Boolean ( KEY _ USER _ HAS _ R ATED , Ġtrue ) ĊĠĠĠĠĠĠĠ Ġpreferences Editor . put Boolean ( KEY _ NE VER _ REM IND _ AG AIN , Ġtrue ) ĊĠĠĠĠĠĠĠ Ġpreferences Editor . apply () ĊĊĠĠĠĠĠĠĠ Ġactivity Test Rule . launch Activity ( null ) ĊĊĠĠĠĠĠĠĠ Ġon View ( with Id ( R . id . button Reset )). perform ( click ()) ĊĊĠĠĠĠĠĠĠ ĠassertFalse ( preferences . getBoolean ( KEY _ USER _ HAS _ R ATED , Ġtrue )) ĊĠĠĠĠĠĠĠ ĠassertFalse ( preferences . getBoolean ( KEY _ NE VER _ REM IND _ AG AIN , Ġtrue )) ĊĠĠĠĠĠĠĠ ĠassertEquals ( preferences . getLong ( KEY _ FIRST _ START _ DATE , Ġ 1 2 3 L ), Ġ 0 L ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( preferences . getInt ( KEY _ LA UNCH _ COUNT , Ġ 1 2 3 ), Ġ 0 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġuser Set Rem ind Never Again ShouldBe Saved () Ġ{ ĊĠĠĠĠĠĠĠ Ġclear Shared Prefs () ĊĠĠĠĠĠĠĠ Ġset Conditions Are Met () ĊĊĠĠĠĠĠĠĠ Ġactivity Test Rule . launch Activity ( null ) ĊĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 2 ), Ġwith Text ( R . string . rem ind Never ))). perform ( click ()) ĊĊĠĠĠĠĠĠĠ ĠassertTrue ( preferences . getBoolean ( KEY _ NE VER _ REM IND _ AG AIN , Ġfalse )) ĊĊĠĠĠĠĠĠĠ Ġ// Verify Ġaction Ġcallback ĊĠĠĠĠĠĠĠ ĠassertTrue ( activity Test Rule . activity . rem ind Never Clicked ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġuser Set Rem ind Later ShouldBe Saved () Ġ{ ĊĠĠĠĠĠĠĠ Ġclear Shared Prefs () ĊĠĠĠĠĠĠĠ Ġset Conditions Are Met () ĊĊĠĠĠĠĠĠĠ Ġactivity Test Rule . launch Activity ( null ) ĊĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 3 ), Ġwith Text ( R . string . rem ind Later ))). perform ( click ()) ĊĊĠĠĠĠĠĠĠ Ġval Ġlaunch Count Ġ= Ġpreferences . getInt ( KEY _ LA UNCH _ COUNT , Ġ 1 0 0 ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( launch Count , Ġ 0 ) ĊĊĠĠĠĠĠĠĠ Ġval Ġfirst Launch Date Ġ= Ġpreferences . getLong ( KEY _ FIRST _ START _ DATE , Ġ 1 0 0 ) ĊĠĠĠĠĠĠĠ ĠassertTrue ( Date Utils . is Today ( first Launch Date )) ĊĊĠĠĠĠĠĠĠ Ġ// Verify Ġaction Ġcallback ĊĠĠĠĠĠĠĠ ĠassertTrue ( activity Test Rule . activity . rem ind Later Clicked ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġdialog Should Popup When Requirements Are Met () Ġ{ ĊĠĠĠĠĠĠĠ Ġclear Shared Prefs () ĊĊĠĠĠĠĠĠĠ Ġset Conditions Are Met () ĊĊĠĠĠĠĠĠĠ Ġactivity Test Rule . launch Activity ( null ) ĊĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 1 ), Ġwith Text ( R . string . rate Now ))). check ( matches ( is Displayed ())) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġdialog Should Not Popup When User Already R ated () Ġ{ ĊĠĠĠĠĠĠĠ Ġclear Shared Prefs () ĊĊĠĠĠĠĠĠĠ Ġset Conditions Are Met () ĊĠĠĠĠĠĠĠ Ġpreferences Editor . put Boolean ( KEY _ USER _ HAS _ R ATED , Ġtrue ). commit () ĊĊĠĠĠĠĠĠĠ Ġactivity Test Rule . launch Activity ( null ) ĊĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 1 ), Ġwith Text ( R . string . rate Now ))). check ( does NotExist ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġdialog Should Not Popup When User Set Never Rem ind Again () Ġ{ ĊĠĠĠĠĠĠĠ Ġclear Shared Prefs () ĊĊĠĠĠĠĠĠĠ Ġset Conditions Are Met () ĊĠĠĠĠĠĠĠ Ġpreferences Editor . put Boolean ( KEY _ NE VER _ REM IND _ AG AIN , Ġtrue ). commit () ĊĊĠĠĠĠĠĠĠ Ġactivity Test Rule . launch Activity ( null ) ĊĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 1 ), Ġwith Text ( R . string . rate Now ))). check ( does NotExist ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġdialog Should Not Popup When Requirements Are Not Met () Ġ{ ĊĠĠĠĠĠĠĠ Ġclear Shared Prefs () ĊĊĠĠĠĠĠĠĠ Ġpreferences Editor . put Int ( KEY _ LA UNCH _ COUNT , Ġ 1 ). commit () ĊĠĠĠĠĠĠĠ Ġval Ġcal Ġ= ĠCalendar . getInstance () ĊĠĠĠĠĠĠĠ Ġcal . add ( Calendar . DAY _ OF _ YEAR , Ġ- 1 ) ĊĠĠĠĠĠĠĠ Ġpreferences Editor . put Long ( KEY _ FIRST _ START _ DATE , Ġcal . time In Millis ). commit () ĊĊĠĠĠĠĠĠĠ Ġactivity Test Rule . launch Activity ( null ) ĊĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 1 ), Ġwith Text ( R . string . rate Now ))). check ( does NotExist ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġbutton Visibility Should Adjust () Ġ{ ĊĠĠĠĠĠĠĠ Ġclear Shared Prefs () ĊĊĠĠĠĠĠĠĠ Ġactivity Test Rule . launch Activity ( null ) ĊĠĠĠĠĠĠĠ Ġ// All ĠButtons Ġare Ġvisible ĊĠĠĠĠĠĠĠ Ġval Ġbutton Default Ġ= Ġon View ( all Of < View >( with Id ( R . id . button Default Dialog ), Ġis Displayed ())) ĊĠĠĠĠĠĠĠ Ġbutton Default . perform ( click ()) ĊĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 1 ), Ġwith Text ( R . string . rate Now ), Ġis Displayed ())). check ( matches ( is Displayed ())) ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 2 ), Ġwith Text ( R . string . rem ind Never ), Ġis Displayed ())). check ( matches ( is Displayed ())) ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 3 ), Ġwith Text ( R . string . rem ind Later ), Ġis Displayed ())). perform ( click ()) ĊĊĠĠĠĠĠĠĠ Ġ// No Ġrem ind Ġnever Ġbutton ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( R . id . button No Rem ind Never ), Ġis Displayed ())). perform ( click ()) ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 1 ), Ġwith Text ( R . string . rate Now ), Ġis Displayed ())). check ( matches ( is Displayed ())) ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 2 ))). check ( matches ( not ( is Displayed ()))) ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 3 ), Ġwith Text ( R . string . rem ind Later ), Ġis Displayed ())) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. check ( matches ( is Displayed ())). perform ( click ()) ĊĊĠĠĠĠĠĠĠ Ġ// No Ġrem ind Ġlater Ġbutton ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( R . id . button No Rem ind Later ), Ġis Displayed ())). perform ( click ()) ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 1 ), Ġwith Text ( R . string . rate Now ), Ġis Displayed ())). check ( matches ( is Displayed ())) ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 3 ))). check ( matches ( not ( is Displayed ()))) ĊĠĠĠĠĠĠĠ Ġon View ( all Of < View >( with Id ( android . R . id . button 2 ), Ġwith Text ( R . string . rem ind Never ), Ġis Displayed ())) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. check ( matches ( is Displayed ())). perform ( click ()) ĊĠĠĠ Ġ} Ċ }