1+ package com .reactnativenavigation .viewcontrollers .child ;
2+
3+ import androidx .test .ext .junit .rules .ActivityScenarioRule ;
4+ import androidx .test .ext .junit .runners .AndroidJUnit4 ;
5+ import androidx .test .platform .app .InstrumentationRegistry ;
6+ import androidx .test .core .app .ActivityScenario ;
7+
8+ import com .reactnativenavigation .BaseAndroidTest ;
9+ import com .reactnativenavigation .TestActivity ;
10+ import com .reactnativenavigation .mocks .SimpleViewController ;
11+ import com .reactnativenavigation .options .Options ;
12+ import com .reactnativenavigation .viewcontrollers .viewcontroller .Presenter ;
13+ import com .reactnativenavigation .viewcontrollers .parent .ParentController ;
14+
15+ import org .junit .Before ;
16+ import org .junit .Rule ;
17+ import org .junit .Test ;
18+ import org .junit .runner .RunWith ;
19+ import org .mockito .Mockito ;
20+
21+ import static org .mockito .ArgumentMatchers .any ;
22+ import static org .mockito .Mockito .spy ;
23+ import static org .mockito .Mockito .times ;
24+ import static org .mockito .Mockito .verify ;
25+
26+ @ RunWith (AndroidJUnit4 .class )
27+ public class ChildControllerTest extends BaseAndroidTest {
28+ private ChildController <?> uut ;
29+ private ChildControllersRegistry childRegistry ;
30+ private Presenter presenter ;
31+ private Options resolvedOptions = new Options ();
32+
33+ @ Rule
34+ public ActivityScenarioRule <TestActivity > rule = new ActivityScenarioRule <>(TestActivity .class );
35+
36+ @ Before
37+ public void beforeEach () {
38+ rule .getScenario ().onActivity (activity -> {
39+ childRegistry = spy (new ChildControllersRegistry ());
40+ presenter = Mockito .mock (Presenter .class );
41+ uut = new SimpleViewController (activity , childRegistry , "childId" , presenter , new Options ()) {
42+ @ Override
43+ public Options resolveCurrentOptions () {
44+ return resolvedOptions ;
45+ }
46+ };
47+ ParentController <?> parent = Mockito .mock (ParentController .class );
48+ Mockito .when (parent .resolveChildOptions (uut )).thenReturn (Options .EMPTY );
49+ uut .setParentController (parent );
50+ });
51+ }
52+
53+ @ Test
54+ public void onViewAppeared () {
55+ InstrumentationRegistry .getInstrumentation ().runOnMainSync (() -> uut .onViewWillAppear ());
56+ verify (childRegistry , times (1 )).onViewAppeared (uut );
57+ }
58+
59+ @ Test
60+ public void onViewDisappear () {
61+ InstrumentationRegistry .getInstrumentation ().runOnMainSync (() -> {
62+ uut .onViewWillAppear ();
63+ uut .onViewDisappear ();
64+ });
65+ verify (childRegistry , times (1 )).onViewDisappear (uut );
66+ }
67+
68+ @ Test
69+ public void mergeOptions () {
70+ try (ActivityScenario <TestActivity > scenario = ActivityScenario .launch (TestActivity .class )) {
71+ scenario .onActivity (activity -> {
72+ activity .setContentView (uut .getView ());
73+ Options options = new Options ();
74+ uut .mergeOptions (options );
75+ verify (presenter ).mergeOptions (uut , options );
76+ });
77+ }
78+ }
79+
80+ @ Test
81+ public void mergeOptions_emptyOptionsAreIgnored () {
82+ InstrumentationRegistry .getInstrumentation ().runOnMainSync (() -> {
83+ uut .mergeOptions (Options .EMPTY );
84+ verify (presenter , times (0 )).mergeOptions (any (), any ());
85+ });
86+ }
87+
88+ @ Test
89+ public void mergeOptions_mergeWithParentViewController () {
90+ InstrumentationRegistry .getInstrumentation ().runOnMainSync (() -> {
91+ Options options = new Options ();
92+ uut .mergeOptions (options );
93+ verify (uut .getParentController ()).mergeChildOptions (options , uut );
94+ });
95+ }
96+
97+ @ Test
98+ public void destroy () {
99+ InstrumentationRegistry .getInstrumentation ().runOnMainSync (() -> uut .destroy ());
100+ verify (childRegistry ).onChildDestroyed (uut );
101+ }
102+ }
0 commit comments