-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathStackControllerTest.kt
More file actions
1176 lines (1057 loc) · 44.2 KB
/
StackControllerTest.kt
File metadata and controls
1176 lines (1057 loc) · 44.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.reactnativenavigation.viewcontrollers.stack
import android.animation.AnimatorSet
import android.app.Activity
import android.content.Context
import android.view.View
import android.widget.FrameLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import org.mockito.kotlin.*
import com.reactnativenavigation.BaseTest
import com.reactnativenavigation.TestUtils
import com.reactnativenavigation.mocks.*
import com.reactnativenavigation.mocks.SimpleViewController.SimpleView
import com.reactnativenavigation.options.Options
import com.reactnativenavigation.options.StackAnimationOptions
import com.reactnativenavigation.options.params.Bool
import com.reactnativenavigation.options.params.Text
import com.reactnativenavigation.react.CommandListenerAdapter
import com.reactnativenavigation.react.events.EventEmitter
import com.reactnativenavigation.utils.*
import com.reactnativenavigation.viewcontrollers.child.ChildControllersRegistry
import com.reactnativenavigation.viewcontrollers.parent.ParentController
import com.reactnativenavigation.viewcontrollers.stack.topbar.TopBarAppearanceAnimator
import com.reactnativenavigation.viewcontrollers.stack.topbar.TopBarController
import com.reactnativenavigation.viewcontrollers.stack.topbar.button.BackButtonHelper
import com.reactnativenavigation.viewcontrollers.stack.topbar.button.IconResolver
import com.reactnativenavigation.viewcontrollers.statusbar.StatusBarPresenter
import com.reactnativenavigation.viewcontrollers.viewcontroller.ViewController
import com.reactnativenavigation.views.stack.StackBehaviour
import com.reactnativenavigation.views.stack.StackLayout
import com.reactnativenavigation.views.stack.topbar.ScrollDIsabledBehavior
import com.reactnativenavigation.views.stack.topbar.TopBar
import org.assertj.core.api.Java6Assertions.assertThat
import org.assertj.core.api.iterable.Extractor
import org.json.JSONObject
import org.junit.Ignore
import org.junit.Test
import org.robolectric.Robolectric
import org.robolectric.shadows.ShadowLooper
import java.util.*
import kotlin.test.fail
class StackControllerTest : BaseTest() {
private lateinit var activity: Activity
private lateinit var childRegistry: ChildControllersRegistry
private lateinit var child1: ViewController<*>
private lateinit var child1a: ViewController<*>
private lateinit var child2: ViewController<*>
private lateinit var child3: ViewController<*>
private var child3View: SimpleView? = null
private lateinit var child4: ViewController<*>
private lateinit var animator: StackAnimator
private lateinit var topBarAnimator: TopBarAppearanceAnimator
private lateinit var topBarController: TopBarController
private lateinit var presenter: StackPresenter
private lateinit var backButtonHelper: BackButtonHelper
private lateinit var eventEmitter: EventEmitter
private lateinit var uut: StackController
override fun beforeEach() {
super.beforeEach()
eventEmitter = mock()
backButtonHelper = spy(BackButtonHelper())
activity = newActivity()
StatusBarPresenter.init(activity)
SystemUiUtils.saveStatusBarHeight(63)
animator = spy(StackAnimator(activity))
childRegistry = ChildControllersRegistry()
topBarAnimator = TopBarAppearanceAnimator()
topBarController = createTopBarController(topBarAnimator)
presenter = createStackPresenter()
createChildren()
uut = createStackBuilder("stack", ArrayList()).build()
activity.setContentView(uut.view)
}
private fun createChildren() {
child1 = spy(SimpleViewController(activity, childRegistry, "child1", Options()))
child1a = spy(SimpleViewController(activity, childRegistry, "child1", Options()))
child2 = spy(SimpleViewController(activity, childRegistry, "child2", Options()))
child3 = spy(object : SimpleViewController(activity, childRegistry, "child3", Options()) {
override fun createView(): SimpleView {
return child3View ?: super.createView()
}
})
child4 = spy(SimpleViewController(activity, childRegistry, "child4", Options()))
}
@Test
fun isAViewController() {
assertThat(uut).isInstanceOf(ViewController::class.java)
}
@Test
fun childrenMustBeUniqueById() {
try {
val uut: StackController = recreateStack(listOf(child1, child2, child1))
fail("Stack should not have duplicate ids!")
} catch (e: IllegalArgumentException) {
assertThat(e.message).contains(child1.id)
}
}
@Test
fun childrenAreAssignedParent() {
val uut: StackController = recreateStack(listOf(child1, child2))
for (child in uut.childControllers) {
assertThat(child.parentController == uut).isTrue()
}
}
@Test
fun constructor_backButtonIsAddedToChild() {
recreateStack(listOf(child1, child2, child3))
assertThat(child2.options.topBar.buttons.back.visible[false]).isTrue()
assertThat(child3.options.topBar.buttons.back.visible[false]).isTrue()
}
@Test
fun createView_currentChildIsAdded() {
val uut: StackController = recreateStack(listOf(child1, child2, child3, child4))
assertThat(uut.childControllers.size).isEqualTo(4)
assertThat(uut.view.childCount).isEqualTo(2)
assertThat(uut.view.getChildAt(0)).isEqualTo(child4.view)
}
@Test
fun createView_topBarScrollIsDisabled() {
val behavior = (uut.topBar.layoutParams as CoordinatorLayout.LayoutParams).behavior
assertThat(behavior is ScrollDIsabledBehavior).isTrue()
}
@Test
fun holdsAStackOfViewControllers() {
assertThat(uut.isEmpty).isTrue()
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
uut.push(child3, CommandListenerAdapter())
assertThat(uut.peek()).isEqualTo(child3)
assertContainsOnlyId(child1.id, child2.id, child3.id)
}
@Test
fun isRendered_falseIfStackIsEmpty() {
assertThat(uut.size()).isZero()
assertThat(uut.isRendered).isFalse()
}
@Test
fun isRendered() {
disablePushAnimation(child1)
uut.push(child1, CommandListenerAdapter())
assertThat(uut.isRendered).isTrue()
child1.setWaitForRender(Bool(true))
assertThat(uut.isRendered).isFalse()
child1.view.addView(View(activity))
assertThat(uut.isRendered).isTrue()
whenever(presenter.isRendered(child1.view)).then { false }
assertThat(uut.isRendered).isFalse()
}
@Test
fun push() {
assertThat(uut.isEmpty).isTrue()
val listener = spy(CommandListenerAdapter())
uut.push(child1, listener)
assertContainsOnlyId(child1.id)
assertThat((child1.view.layoutParams as CoordinatorLayout.LayoutParams).behavior).isInstanceOf(StackBehaviour::class.java)
verify(listener).onSuccess(child1.id)
}
@Test
fun push_backButtonIsNotAddedIfScreenContainsLeftButton() {
disablePushAnimation(child1, child2)
uut.push(child1, CommandListenerAdapter())
child2.options.topBar.buttons.left = ArrayList(setOf(TitleBarHelper.iconButton("someButton", "icon.png")))
uut.push(child2, CommandListenerAdapter())
ShadowLooper.idleMainLooper()
assertThat(topBarController.leftButtonCount).isOne()
verify(topBarController.view, never()).setBackButton(any())
}
@Test
fun push_backButtonIsNotAddedIfScreenClearsLeftButton() {
child1.options.topBar.buttons.left = ArrayList()
uut.push(child1, CommandListenerAdapter())
verify(child1, never()).mergeOptions(any())
}
@Test
fun setRoot_pushDuringSetRootAnimationShouldNotCrash() {
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
idleMainLooper()
uut.setRoot(listOf(child1), CommandListenerAdapter())
uut.push(child3, CommandListenerAdapter())
assertThat(uut.currentChild).isEqualTo(child3)
}
@Test
fun push_backButtonAddedBeforeChildViewIsCreated() {
disablePopAnimation(child1, child2)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
val inOrder = inOrder(backButtonHelper, child2)
inOrder.verify(backButtonHelper)!!.addToPushedChild(child2)
inOrder.verify(child2)!!.parentController = uut
inOrder.verify(child2, atLeastOnce())!!.view // creates view
}
@Test
fun push_waitForRender() {
disablePushAnimation(child1)
uut.push(child1, CommandListenerAdapter())
assertThat(child1.view.parent).isEqualTo(uut.view)
child2.options.animations.push.waitForRender = Bool(true)
uut.push(child2, CommandListenerAdapter())
// Both children are attached
assertThat(child1.view.parent).isEqualTo(uut.view)
assertThat(child2.view.parent).isEqualTo(uut.view)
assertThat(child2.isViewShown).isFalse()
verify(child2, never()).onViewWillAppear()
child2.view.addView(View(activity))
ShadowLooper.idleMainLooper()
verify(child2).onViewWillAppear()
assertThat(child2.isViewShown).isTrue()
animator.endPushAnimation(child2)
assertThat(child1.view.parent).isNull()
}
@Test
fun push_backPressedDuringPushAnimationDestroysPushedScreenImmediately() {
backPressedDuringPushAnimation(false)
}
@Test
@Ignore
fun push_backPressedDuringPushAnimationDestroysPushedScreenImmediatelyWaitForRender() {
backPressedDuringPushAnimation(true)
}
private fun backPressedDuringPushAnimation(waitForRender: Boolean) {
disablePushAnimation(child1)
uut.push(child1, CommandListenerAdapter())
val pushListener = spy(CommandListenerAdapter())
child2.options.animations.push.waitForRender = Bool(waitForRender)
uut.push(child2, pushListener)
// both children are attached
assertThat(child1.view.parent).isEqualTo(uut.view)
assertThat(child2.view.parent).isEqualTo(uut.view)
val backListener = spy(CommandListenerAdapter())
uut.handleBack(backListener)
assertThat(uut.size()).isOne()
assertThat(child1.view.parent).isEqualTo(uut.view)
assertThat(child2.isDestroyed).isTrue()
val inOrder = inOrder(pushListener, backListener)
inOrder.verify(pushListener).onSuccess(any())
inOrder.verify(backListener).onSuccess(any())
}
@Test
fun push_rejectIfStackContainsChildWithId() {
disablePushAnimation(child1)
uut.push(child1, CommandListenerAdapter())
assertThat(uut.size()).isEqualTo(1)
val listener = spy(CommandListenerAdapter())
uut.push(child1a, listener)
verify(listener).onError(any())
assertThat(uut.size()).isEqualTo(1)
}
@Test
fun push_onViewDidAppearInvokedOnPushedScreen() {
disablePushAnimation(child1, child2)
uut.push(child1, CommandListenerAdapter()) // Initialize stack with a child
uut.push(child2, CommandListenerAdapter())
idleMainLooper()
verify(child2).onViewDidAppear()
}
@Test
fun `push - should make push when stack is not yet created`() {
val child11 = spy(child1)
val child21 = spy(child2)
disablePushAnimation(child11, child21)
uut.view = null
uut.push(child11, mock())
assertThat(uut.size()).isEqualTo(1)
uut.push(child21, mock())
assertThat(uut.size()).isEqualTo(2)
verify(child11, never()).view
verify(child21, never()).view
}
@Test
fun animateSetRoot() {
disablePushAnimation(child1, child2, child3)
assertThat(uut.isEmpty).isTrue()
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
uut.setRoot(listOf(child3), object : CommandListenerAdapter() {
override fun onSuccess(childId: String) {
assertContainsOnlyId(child3.id)
}
})
}
@Test
fun setRoot_singleChild() {
activity.setContentView(uut.view)
disablePushAnimation(child1, child2, child3)
assertThat(uut.isEmpty).isTrue()
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
ShadowLooper.idleMainLooper()
assertThat(uut.topBar.navigationIcon).isNotNull()
uut.setRoot(listOf(child3), object : CommandListenerAdapter() {
override fun onSuccess(childId: String) {
assertContainsOnlyId(child3.id)
ShadowLooper.idleMainLooper()
assertThat(uut.topBar.navigationIcon).isNull()
}
})
}
@Test
fun setRoot_multipleChildren() {
Robolectric.getForegroundThreadScheduler().pause()
activity.setContentView(uut.view)
disablePushAnimation(child1, child2, child3, child4)
disablePopAnimation(child4)
assertThat(uut.isEmpty).isTrue()
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
ShadowLooper.idleMainLooper()
assertThat(uut.topBar.navigationIcon).isNotNull()
uut.setRoot(listOf(child3, child4), object : CommandListenerAdapter() {
override fun onSuccess(childId: String) {
assertContainsOnlyId(child3.id, child4.id)
assertThat(child4.isViewShown).isTrue()
assertThat(child3.isViewShown).isFalse()
assertThat(uut.currentChild).isEqualTo(child4)
uut.pop(Options.EMPTY, CommandListenerAdapter())
ShadowLooper.idleMainLooper()
assertThat(uut.topBar.leftButtonBar.navigationIcon).isNull()
assertThat(uut.currentChild).isEqualTo(child3)
}
})
}
@Test
fun setRoot_backButtonIsAddedToAllChildren() {
Robolectric.getForegroundThreadScheduler().pause()
activity.setContentView(uut.view)
disablePushAnimation(child1, child2)
uut.setRoot(listOf(child1, child2), CommandListenerAdapter())
assertThat(child1.options.topBar.buttons.back.visible[false]).isFalse()
assertThat(child2.options.topBar.buttons.back.visible[false]).isTrue()
}
@Test
fun setRoot_doesNotCrashWhenCalledInQuickSuccession() {
disablePushAnimation(child1)
uut.setRoot(listOf(child1), CommandListenerAdapter())
uut.setRoot(listOf(child2), CommandListenerAdapter())
uut.setRoot(listOf(child3), CommandListenerAdapter())
animator.endPushAnimation(child2)
animator.endPushAnimation(child3)
assertContainsOnlyId(child3.id)
}
@Test
fun setRoot_doesNotCrashWhenCalledWithSameId() {
disablePushAnimation(child1, child1a)
uut.setRoot(listOf(child1), CommandListenerAdapter())
uut.setRoot(listOf(child1a), CommandListenerAdapter())
assertContainsOnlyId(child1a.id)
}
@Test
fun setRoot_topScreenIsStartedThenTheRest() {
disablePushAnimation(child1, child2, child3)
child3View = spy(SimpleView(activity))
uut.setRoot(listOf(child1, child2, child3), CommandListenerAdapter())
ShadowLooper.idleMainLooper()
val inOrder = inOrder(child3View!!, child2, child1)
inOrder.verify(child3View)!!.start()
inOrder.verify(child2)!!.start()
inOrder.verify(child1)!!.start()
}
@Test
fun setRoot_onViewDidAppearIsInvokedOnAppearingChild() {
disablePushAnimation(child1)
uut.setRoot(listOf(child1), CommandListenerAdapter())
verify(child1).onViewDidAppear()
}
@Test
fun `setRoot - should change stack children, no view creation, when stack is not yet created`() {
val spyChild = spy(child1)
val spyChild2 = spy(child2)
uut.view = null
uut.setRoot(listOf(spyChild), CommandListenerAdapter())
verify(spyChild, never()).view
verify(spyChild).parentController = uut
assertThat(uut.size()).isEqualTo(1)
val listOf = listOf(spyChild, spyChild2)
uut.setRoot(listOf, CommandListenerAdapter())
verify(spyChild, never()).view
verify(spyChild2, never()).view
verify(spyChild2).parentController = uut
assertThat(uut.size()).isEqualTo(2)
assertThat(uut.childControllers).isEqualTo(listOf)
}
@Test
fun setRoot_onViewDidAppearIsInvokedBeforePreviousRootIsDestroyed() {
disablePushAnimation(child1, child2, child3)
uut.push(child1, CommandListenerAdapter())
uut.setRoot(listOf(child2, child3), CommandListenerAdapter())
ShadowLooper.idleMainLooper()
val inOrder = inOrder(child2, child3, child1)
inOrder.verify(child3)!!.onViewDidAppear()
inOrder.verify(child1)!!.onViewDisappear()
verify(child2, never()).onViewDidAppear()
}
@Test
fun pop() {
disablePushAnimation(child1, child2)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, object : CommandListenerAdapter() {
override fun onSuccess(childId: String) {
assertContainsOnlyId(child2.id, child1.id)
uut.pop(Options.EMPTY, CommandListenerAdapter())
assertContainsOnlyId(child1.id)
}
})
}
@Test
fun `pop - should make pop when stack is not yet created`() {
val child11 = spy(child1)
val child21 = spy(child2)
disablePushAnimation(child11, child21)
uut.view = null
uut.push(child11, mock())
uut.push(child21, mock())
uut.pop(Options.EMPTY, mock())
assertThat(uut.size()).isEqualTo(1)
verify(child11, never()).view
verify(child21, never()).view
}
@Test
fun pop_screenCurrentlyBeingPushedIsPopped() {
disablePushAnimation(child1, child2)
uut.push(child1, mock())
uut.push(child2, mock())
uut.push(child3, mock())
uut.pop(Options.EMPTY, mock())
assertThat(uut.size()).isEqualTo(2)
assertContainsOnlyId(child1.id, child2.id)
}
@Test
fun pop_appliesOptionsAfterPop() {
disablePushAnimation(child1, child2)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
uut.pop(Options.EMPTY, CommandListenerAdapter())
dispatchOnGlobalLayout(child1.view)
verify(presenter).applyChildOptions(any(), eq(uut), eq(child1))
}
@Test
fun pop_popEventIsEmitted() {
disablePushAnimation(child1, child2)
disablePopAnimation(child2)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
uut.pop(Options.EMPTY, CommandListenerAdapter())
verify(eventEmitter).emitScreenPoppedEvent(child2.id)
}
@Test
fun popToRoot_popEventIsEmitted() {
disablePushAnimation(child1, child2, child3)
disablePopAnimation(child2, child3)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
uut.push(child3, CommandListenerAdapter())
uut.pop(Options.EMPTY, CommandListenerAdapter())
verify(eventEmitter).emitScreenPoppedEvent(child3.id)
verifyNoMoreInteractions(eventEmitter)
}
@Test
fun stackOperations() {
assertThat(uut.peek()).isNull()
assertThat(uut.size()).isZero()
assertThat(uut.isEmpty).isTrue()
uut.push(child1, CommandListenerAdapter())
assertThat(uut.peek()).isEqualTo(child1)
assertThat(uut.size()).isEqualTo(1)
assertThat(uut.isEmpty).isFalse()
}
@Test
fun onChildDestroyed() {
uut.onChildDestroyed(child2)
verify(presenter).onChildDestroyed(child2)
}
@Test
fun handleBack_PopsUnlessSingleChild() {
assertThat(uut.isEmpty).isTrue()
assertThat(uut.handleBack(CommandListenerAdapter())).isFalse()
uut.push(child1, CommandListenerAdapter())
assertThat(uut.size()).isEqualTo(1)
assertThat(uut.handleBack(CommandListenerAdapter())).isFalse()
uut.push(child2, object : CommandListenerAdapter() {
override fun onSuccess(childId: String) {
assertThat(uut.size()).isEqualTo(2)
assertThat(uut.handleBack(CommandListenerAdapter())).isTrue()
assertThat(uut.size()).isEqualTo(1)
assertThat(uut.handleBack(CommandListenerAdapter())).isFalse()
}
})
}
@Test
fun pop_doesNothingWhenZeroOrOneChild() {
assertThat(uut.isEmpty).isTrue()
uut.pop(Options.EMPTY, CommandListenerAdapter())
assertThat(uut.isEmpty).isTrue()
uut.push(child1, CommandListenerAdapter())
uut.pop(Options.EMPTY, CommandListenerAdapter())
assertContainsOnlyId(child1.id)
}
@Test
fun pop_animationOptionsAreMergedCorrectlyToDisappearingChild() {
disablePushAnimation(child1, child2)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
val mergeOptions = Options()
mergeOptions.animations.pop.content = createEnterExitAnimation(duration = 123)
uut.pop(mergeOptions, CommandListenerAdapter())
val captor = argumentCaptor<Options>()
verify(animator).pop(any(), any(), captor.capture(), any(), any())
val animator = captor.firstValue.animations.pop.content.exit
.getAnimation(mockView(activity))
assertThat((animator as AnimatorSet).childAnimations.first().duration).isEqualTo(123)
}
@Test
fun pop_animationOptionsAreMergedCorrectlyToDisappearingChildWithDefaultOptions() {
disablePushAnimation(child1, child2)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
val defaultOptions = Options()
defaultOptions.animations.pop.content = createEnterExitAnimation(duration = 123)
uut.setDefaultOptions(defaultOptions)
uut.pop(Options.EMPTY, CommandListenerAdapter())
val captor = argumentCaptor<Options>()
verify(animator).pop(any(), any(), captor.capture(), any(), any())
val animator = captor.firstValue.animations.pop.content.exit
.getAnimation(mockView(activity))
assertThat((animator as AnimatorSet).childAnimations.first().duration).isEqualTo(123)
}
@Test
fun canPopWhenSizeIsMoreThanOne() {
assertThat(uut.isEmpty).isTrue()
assertThat(uut.canPop()).isFalse()
uut.push(child1, CommandListenerAdapter())
assertContainsOnlyId(child1.id)
assertThat(uut.canPop()).isFalse()
uut.push(child2, CommandListenerAdapter())
assertContainsOnlyId(child1.id, child2.id)
assertThat(uut.canPop()).isTrue()
}
@Test
fun push_addsToViewTree() {
assertNotChildOf(uut.view, child1.view)
uut.push(child1, CommandListenerAdapter())
assertIsChild(uut.view, child1.view)
}
@Test
fun push_removesPreviousFromTree() {
disablePushAnimation(child1, child2)
assertNotChildOf(uut.view, child1.view)
uut.push(child1, CommandListenerAdapter())
assertIsChild(uut.view, child1.view)
idleMainLooper()
uut.push(child2, CommandListenerAdapter())
idleMainLooper()
assertIsChild(uut.view, child2)
assertNotChildOf(uut.view, child1)
}
@Test
fun push_assignsRefToSelfOnPushedController() {
assertThat(child1.parentController).isNull()
uut.push(child1, CommandListenerAdapter())
assertThat(child1.parentController).isEqualTo(uut)
val anotherNavController = recreateStack("another")
anotherNavController.ensureViewIsCreated()
anotherNavController.push(child2, CommandListenerAdapter())
assertThat(child2.parentController).isEqualTo(anotherNavController)
}
@Test
fun push_doesNotAnimateTopBarIfScreenIsPushedWithoutAnimation() {
uut.ensureViewIsCreated()
child1.ensureViewIsCreated()
child1.options.topBar.visible = Bool(false)
child1.options.topBar.animate = Bool(false)
disablePushAnimation(child1, child2)
uut.push(child1, CommandListenerAdapter())
child1.onViewWillAppear()
assertThat(uut.topBar.visibility).isEqualTo(View.GONE)
uut.push(child2, CommandListenerAdapter())
child2.onViewWillAppear()
verify(topBarController, never()).showAnimate(any(), any())
assertThat(uut.topBar.visibility).isEqualTo(View.VISIBLE)
verify(topBarController.view).resetViewProperties()
}
@Test
fun push_animatesAndClearsPreviousAnimationValues() {
uut.ensureViewIsCreated()
child1.options.topBar.visible = Bool(false)
child1.options.topBar.animate = Bool(false)
child1.options.animations.push.enabled = Bool(false)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
verify(topBarController.view).resetViewProperties()
}
@Test
fun pop_replacesViewWithPrevious() {
disablePushAnimation(child1, child2)
disablePopAnimation(child2)
val child2View: View = child2.view
val child1View: View = child1.view
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
idleMainLooper()
assertIsChild(uut.view, child2View)
assertNotChildOf(uut.view, child1View)
uut.pop(Options.EMPTY, CommandListenerAdapter())
assertNotChildOf(uut.view, child2View)
assertIsChild(uut.view, child1View)
}
@Test
fun popTo_PopsTopUntilControllerIsNewTop() {
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
uut.push(child3, object : CommandListenerAdapter() {
override fun onSuccess(childId: String) {
assertThat(uut.size()).isEqualTo(3)
assertThat(uut.peek()).isEqualTo(child3)
uut.popTo(child1, Options.EMPTY, CommandListenerAdapter())
assertThat(uut.size()).isEqualTo(1)
assertThat(uut.peek()).isEqualTo(child1)
}
})
}
@Test
fun popTo_optionsAreMergedOnTopChild() {
disablePushAnimation(child1, child2)
uut.push(child1, CommandListenerAdapter())
val mergeOptions = Options()
uut.popTo(child2, mergeOptions, CommandListenerAdapter())
uut.popTo(child1, mergeOptions, CommandListenerAdapter())
verify(child1, never()).mergeOptions(mergeOptions)
uut.push(child2, CommandListenerAdapter())
uut.popTo(child1, mergeOptions, CommandListenerAdapter())
verify(child2).mergeOptions(mergeOptions)
}
@Test
fun popTo_NotAChildOfThisStack_DoesNothing() {
uut.push(child1, CommandListenerAdapter())
uut.push(child3, CommandListenerAdapter())
assertThat(uut.size()).isEqualTo(2)
uut.popTo(child2, Options.EMPTY, CommandListenerAdapter())
assertThat(uut.size()).isEqualTo(2)
}
@Test
fun popTo_animatesTopController() {
disablePushAnimation(child1, child2, child3, child4)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
uut.push(child3, CommandListenerAdapter())
uut.push(child4, CommandListenerAdapter())
idleMainLooper()
uut.popTo(child2, Options.EMPTY, CommandListenerAdapter())
verify(animator, never()).pop(any(), eq(child1), any(), any(), any())
verify(animator, never()).pop(any(), eq(child2), any(), any(), any())
verify(animator, never()).pop(any(), eq(child3), any(), any(), any())
verify(animator).pop(any(), eq(child4), any(), any(), any())
}
@Test
fun popTo_pushAnimationIsCancelled() {
disablePushAnimation(child1, child2)
uut.push(child1, mock())
uut.push(child2, mock())
uut.push(child3, mock())
idleMainLooper()
uut.popTo(child1, Options.EMPTY, mock())
animator.endPushAnimation(child3)
assertContainsOnlyId(child1.id)
}
@Test
fun popToRoot_PopsEverythingAboveFirstController() {
child1.options.animations.push.enabled = Bool(false)
child2.options.animations.push.enabled = Bool(false)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
uut.push(child3, object : CommandListenerAdapter() {
override fun onSuccess(childId: String) {
assertThat(uut.size()).isEqualTo(3)
assertThat(uut.peek()).isEqualTo(child3)
uut.popToRoot(Options.EMPTY, object : CommandListenerAdapter() {
override fun onSuccess(childId: String) {
assertThat(uut.size()).isEqualTo(1)
assertThat(uut.peek()).isEqualTo(child1)
}
})
}
})
}
@Test
fun popToRoot_onlyTopChildIsAnimated() {
disablePushAnimation(child1, child2, child3)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
uut.push(child3, CommandListenerAdapter())
idleMainLooper()
uut.popToRoot(Options.EMPTY, object : CommandListenerAdapter() {
override fun onSuccess(childId: String) {
verify(animator).pop(eq(child1), eq(child3), any(), any(), any())
}
})
}
@Test
fun popToRoot_topChildrenAreDestroyed() {
child1.options.animations.push.enabled = Bool(false)
child2.options.animations.push.enabled = Bool(false)
child3.options.animations.push.enabled = Bool(false)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
uut.push(child3, CommandListenerAdapter())
idleMainLooper()
uut.popToRoot(Options.EMPTY, object : CommandListenerAdapter() {
override fun onSuccess(childId: String) {
verify(child1, never()).destroy()
verify(child2).destroy()
verify(child3).destroy()
}
})
}
@Test
fun popToRoot_EmptyStackDoesNothing() {
assertThat(uut.isEmpty).isTrue()
val listener = spy(CommandListenerAdapter())
uut.popToRoot(Options.EMPTY, listener)
assertThat(uut.isEmpty).isTrue()
verify(listener).onSuccess("")
}
@Test
fun popToRoot_optionsAreMergedOnTopChild() {
disablePushAnimation(child1, child2)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
val mergeOptions = Options()
uut.popToRoot(mergeOptions, CommandListenerAdapter())
verify(child2).mergeOptions(mergeOptions)
verify(child1, never()).mergeOptions(mergeOptions)
}
@Test
fun popToRoot_screenPushedBeforePopAnimationCompletesIsPopped() {
disablePushAnimation(child1, child2)
uut.push(child1, mock())
uut.push(child2, mock())
uut.push(child3, mock())
idleMainLooper()
uut.popToRoot(Options.EMPTY, mock())
animator.endPushAnimation(child3)
assertContainsOnlyId(child1.id)
}
@Test
fun findControllerById_ReturnsSelfOrChildrenById() {
assertThat(uut.findController("123")).isNull()
assertThat(uut.findController(uut.id)).isEqualTo(uut)
uut.push(child1, CommandListenerAdapter())
assertThat(uut.findController(child1.id)).isEqualTo(child1)
}
@Test
fun findControllerById_Deeply() {
val stack = recreateStack("another")
stack.ensureViewIsCreated()
stack.push(child2, CommandListenerAdapter())
uut.push(stack, CommandListenerAdapter())
assertThat(uut.findController(child2.id)).isEqualTo(child2)
}
@Test
fun pop_callsDestroyOnPoppedChild() {
child2 = spy(child2)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
verify(child2, never()).destroy()
uut.pop(Options.EMPTY, CommandListenerAdapter())
verify(child2).destroy()
}
@Test
fun pop_callWillDisappear() {
disablePushAnimation(child1, child2)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
uut.pop(Options.EMPTY, CommandListenerAdapter())
verify(child2).onViewWillDisappear()
}
@Test
fun pop_callDidAppear() {
disablePushAnimation(child1, child2)
disablePopAnimation(child2)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
uut.pop(Options.EMPTY, CommandListenerAdapter())
verify(child1).onViewDidAppear()
}
@Test
fun pop_animatesTopBar() {
uut.ensureViewIsCreated()
disablePushAnimation(child1, child2)
child1.options.topBar.visible = Bool(false)
uut.push(child1, CommandListenerAdapter())
child1.onViewWillAppear()
assertThat(uut.topBar.visibility).isEqualTo(View.GONE)
uut.push(child2, CommandListenerAdapter())
child2.onViewWillAppear()
assertThat(uut.topBar.visibility).isEqualTo(View.VISIBLE)
uut.pop(Options.EMPTY, CommandListenerAdapter())
assertThat(topBarAnimator.isAnimatingHide()).isTrue()
}
@Test
fun pop_doesNotAnimateTopBarIfScreenIsPushedWithoutAnimation() {
disablePushAnimation(child1)
child1.options.topBar.visible = Bool(false)
child1.options.topBar.animate = Bool(false)
assertThat(uut.topBar.visibility).isEqualTo(View.VISIBLE)
uut.push(child1, CommandListenerAdapter())
child1.onViewWillAppear()
assertThat(topBarAnimator.isAnimatingHide()).isFalse()
assertThat(uut.topBar.visibility).isEqualTo(View.GONE)
}
@Test
fun popTo_CallsDestroyOnPoppedChild() {
child1 = spy(child1)
child2 = spy(child2)
child3 = spy(child3)
uut.push(child1, CommandListenerAdapter())
uut.push(child2, CommandListenerAdapter())
uut.push(child3, object : CommandListenerAdapter() {
override fun onSuccess(childId: String) {
verify(child2, never()).destroy()
verify(child3, never()).destroy()
uut.popTo(child1, Options.EMPTY, object : CommandListenerAdapter() {
override fun onSuccess(childId: String) {
verify(child2).destroy()
verify(child3).destroy()
}
})
}
})
}
@Test
fun stackCanBePushed() {
val parent = recreateStack("someStack")
parent.ensureViewIsCreated()
parent.push(uut, CommandListenerAdapter())
uut.onViewWillAppear()
assertThat(parent.view.getChildAt(0)).isEqualTo(uut.view)
}
@Test
fun applyOptions_applyOnlyOnFirstStack() {
val parent = spy(recreateStack("someStack"))
parent.ensureViewIsCreated()
parent.push(uut, CommandListenerAdapter())
val childOptions = Options()
childOptions.topBar.title.text = Text("Something")
child1.options = childOptions
uut.push(child1, CommandListenerAdapter())
child1.ensureViewIsCreated()
child1.onViewWillAppear()
val optionsCaptor = argumentCaptor<Options>()
val viewCaptor = argumentCaptor<ViewController<*>>()
verify(parent).applyChildOptions(optionsCaptor.capture(), viewCaptor.capture())
assertThat(optionsCaptor.firstValue.topBar.title.text.hasValue()).isFalse()
}
@Test
fun applyOptions_topTabsAreNotVisibleIfNoTabsAreDefined() {
uut.ensureViewIsCreated()
uut.push(child1, CommandListenerAdapter())
child1.ensureViewIsCreated()
child1.onViewWillAppear()
assertThat(ViewHelper.isVisible(uut.topBar.topTabs)).isFalse()
}
@Test
fun buttonPressInvokedOnCurrentStack() {
uut.ensureViewIsCreated()
uut.push(child1, CommandListenerAdapter())
uut.sendOnNavigationButtonPressed("btn1")
verify(child1).sendOnNavigationButtonPressed("btn1")
}
@Test
fun mergeChildOptions_updatesViewWithNewOptions() {
val uut = spy(TestUtils.newStackController(activity)
.setId("stack")
.build())
val optionsToMerge = Options()
val vc = mock<ViewController<*>>()
uut.mergeChildOptions(optionsToMerge, vc)
verify(uut).mergeChildOptions(optionsToMerge, vc)
}
@Test
fun mergeOptions_doesNotMergeOptionsIfViewIsNotVisible() {
uut.mergeOptions(Options.EMPTY)
verify(presenter, never()).mergeOptions(any(), any(), any())
}
@Test
fun mergeChildOptions_updatesParentControllerWithNewOptions() {
val uut = TestUtils.newStackController(activity)
.setId("stack")
.build()
val parentController = mock<ParentController<*>>()
uut.parentController = parentController
uut.ensureViewIsCreated()
val optionsToMerge = Options()
optionsToMerge.topBar.testId = Text("topBarID")
optionsToMerge.bottomTabsOptions.testId = Text("bottomTabsID")
val vc = mock<ViewController<*>>()
uut.mergeChildOptions(optionsToMerge, vc)
val captor = argumentCaptor<Options>()
verify(parentController).mergeChildOptions(captor.capture(), eq(vc))
assertThat(captor.firstValue.topBar.testId.hasValue()).isFalse()
assertThat(captor.firstValue.bottomTabsOptions.testId.get()).isEqualTo(optionsToMerge.bottomTabsOptions.testId.get())
}
@Test
fun mergeChildOptions_StackRelatedOptionsAreCleared() {
uut.ensureViewIsCreated()
val parentController = mock<ParentController<*>>()