-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathModalStackTest.java
More file actions
368 lines (319 loc) · 14.3 KB
/
ModalStackTest.java
File metadata and controls
368 lines (319 loc) · 14.3 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
package com.reactnativenavigation.viewcontrollers.modal;
import android.app.Activity;
import android.widget.FrameLayout;
import com.reactnativenavigation.BaseTest;
import com.reactnativenavigation.TestUtils;
import com.reactnativenavigation.mocks.SimpleViewController;
import com.reactnativenavigation.options.Options;
import com.reactnativenavigation.react.CommandListener;
import com.reactnativenavigation.react.CommandListenerAdapter;
import com.reactnativenavigation.react.events.EventEmitter;
import com.reactnativenavigation.viewcontrollers.child.ChildControllersRegistry;
import com.reactnativenavigation.viewcontrollers.stack.StackController;
import com.reactnativenavigation.viewcontrollers.viewcontroller.ViewController;
import com.reactnativenavigation.options.TransitionAnimationOptions;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import java.util.EmptyStackException;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.assertj.core.api.Java6Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
public class ModalStackTest extends BaseTest {
private static final String MODAL_ID_1 = "modalId1";
private static final String MODAL_ID_2 = "modalId2";
private static final String MODAL_ID_3 = "modalId3";
private static final String MODAL_ID_4 = "modalId4";
private ModalStack uut;
private ViewController<?> modal1;
private ViewController<?> modal2;
private ViewController<?> modal3;
private ViewController<?> modal4;
private StackController stack;
private Activity activity;
private ChildControllersRegistry childRegistry;
private ModalPresenter presenter;
private ModalAnimator animator;
private ViewController<?> root;
private EventEmitter emitter;
@Override
public void beforeEach() {
super.beforeEach();
activity = newActivity();
childRegistry = new ChildControllersRegistry();
root = new SimpleViewController(activity, childRegistry, "root", new Options());
FrameLayout rootLayout = new FrameLayout(activity);
CoordinatorLayout modalsLayout = new CoordinatorLayout(activity);
FrameLayout contentLayout = new FrameLayout(activity);
contentLayout.addView(rootLayout);
contentLayout.addView(modalsLayout);
activity.setContentView(contentLayout);
animator = spy(new ModalAnimatorMock(activity));
presenter = spy(new ModalPresenter(animator));
uut = new ModalStack(activity, presenter);
uut.setModalsLayout(modalsLayout);
uut.setRootLayout(rootLayout);
emitter = Mockito.mock(EventEmitter.class);
uut.setEventEmitter(emitter);
modal1 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_1, new Options()));
modal2 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_2, new Options()));
modal3 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_3, new Options()));
modal4 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_4, new Options()));
stack = TestUtils.newStackController(activity)
.setChildren(modal4)
.build();
}
@Test
public void showModal_DidAppearEventShouldBeCallled(){
CommandListener listener = spy(new CommandListenerAdapter());
uut.showModal(modal1, root, listener);
verify(listener).onSuccess(modal1.getId());
idleMainLooper();
verify(modal1).onViewDidAppear();
}
@Test
public void modalRefIsSaved() {
disableShowModalAnimation(modal1);
CommandListener listener = spy(new CommandListenerAdapter());
uut.showModal(modal1, root, listener);
verify(listener, times(1)).onSuccess(modal1.getId());
assertThat(findModal(MODAL_ID_1)).isNotNull();
}
@Test
public void showModal() {
CommandListener listener = spy(new CommandListenerAdapter());
uut.showModal(modal1, root, listener);
idleMainLooper();
verify(listener).onSuccess(modal1.getId());
verify(modal1).onViewDidAppear();
assertThat(uut.size()).isOne();
verify(presenter).showModal(eq(modal1), eq(root), any());
assertThat(findModal(MODAL_ID_1)).isNotNull();
}
@Test
public void showModal_canShowModalBeforeRootIsSet() {
CommandListener listener = spy(new CommandListenerAdapter());
uut.showModal(modal1, null, listener);
verify(listener).onSuccess(modal1.getId());
}
@Test
public void dismissModal() {
uut.showModal(modal1, root, new CommandListenerAdapter());
CommandListener listener = spy(new CommandListenerAdapter());
uut.dismissModal(modal1.getId(), root, listener);
assertThat(findModal(modal1.getId())).isNull();
verify(presenter).dismissModal(eq(modal1), eq(root), eq(root), any());
verify(listener).onSuccess(modal1.getId());
}
@Test
public void dismissModal_listenerAndEmitterAreInvokedWithRootViewControllerId() {
uut.showModal(stack, root, new CommandListenerAdapter());
CommandListener listener = spy(new CommandListenerAdapter());
uut.dismissModal(modal4.getId(), root, listener);
verify(listener).onSuccess(stack.getId());
verify(emitter).emitModalDismissed(stack.getId(), modal4.getCurrentComponentName(), 1);
}
@SuppressWarnings("Convert2Lambda")
@Test
public void dismissModal_rejectIfModalNotFound() {
CommandListener listener = spy(new CommandListenerAdapter());
Runnable onModalWillDismiss = spy(new Runnable() {
@Override
public void run() {
}
});
uut.dismissModal(MODAL_ID_1, root, listener);
verify(onModalWillDismiss, times(0)).run();
verify(listener, times(1)).onError(anyString());
verifyNoMoreInteractions(listener);
}
@Test
public void dismissModal_dismissDeepModal() {
disableShowModalAnimation(modal1, modal2, modal3);
disableDismissModalAnimation(modal1, modal2, modal3);
uut.showModal(modal1, root, new CommandListenerAdapter());
uut.showModal(modal2, root, new CommandListenerAdapter());
uut.showModal(modal3, root, new CommandListenerAdapter());
assertThat(root.getView().getParent()).isNull();
uut.dismissModal(modal1.getId(), root, new CommandListenerAdapter());
assertThat(root.getView().getParent()).isNull();
uut.dismissModal(modal3.getId(), root, new CommandListenerAdapter());
uut.dismissModal(modal2.getId(), root, new CommandListenerAdapter());
assertThat(root.getView().getParent()).isNotNull();
assertThat(root.getView().isShown()).isTrue();
}
@Test
public void dismissAllModals() {
uut.showModal(modal1, root, new CommandListenerAdapter());
uut.showModal(modal2, root, new CommandListenerAdapter());
CommandListener listener = spy(new CommandListenerAdapter() {
@Override
public void onSuccess(String childId) {
assertThat(findModal(modal1.getId())).isNull();
assertThat(findModal(modal2.getId())).isNull();
assertThat(uut.isEmpty()).isTrue();
}
});
uut.dismissAllModals(root, Options.EMPTY, listener);
verify(listener, times(1)).onSuccess(anyString());
verifyNoMoreInteractions(listener);
}
@Test
public void dismissAllModal_resolvesPromiseSuccessfullyWhenCalledBeforeRootIsSet() {
CommandListenerAdapter spy = spy(new CommandListenerAdapter());
uut.dismissAllModals(null, Options.EMPTY, spy);
verify(spy).onSuccess("");
}
@Test
public void dismissAllModals_resolveSuccessfullyIfEmpty() {
CommandListener spy = spy(new CommandListenerAdapter());
uut.dismissAllModals(root, Options.EMPTY, spy);
verify(spy, times(1)).onSuccess(root.getId());
}
@Test
public void dismissAllModals_optionsAreMergedOnTopModal() {
uut.showModal(modal1, root, new CommandListenerAdapter());
uut.showModal(modal2, root, new CommandListenerAdapter());
uut.showModal(modal3, root, new CommandListenerAdapter());
Options mergeOptions = new Options();
uut.dismissAllModals(root, mergeOptions, new CommandListenerAdapter());
verify(modal3).mergeOptions(mergeOptions);
verify(modal1, times(0)).mergeOptions(mergeOptions);
verify(modal2, times(0)).mergeOptions(mergeOptions);
}
@Test
public void dismissAllModals_onlyTopModalIsAnimated() {
modal2 = spy(modal2);
Options defaultOptions = new Options();
uut.setDefaultOptions(defaultOptions);
Options resolvedOptions = new Options();
when(modal2.resolveCurrentOptions(defaultOptions)).then(invocation -> resolvedOptions);
uut.showModal(modal1, root, new CommandListenerAdapter());
uut.showModal(modal2, root, new CommandListenerAdapter());
CommandListener listener = spy(new CommandListenerAdapter());
uut.dismissAllModals(root, Options.EMPTY, listener);
verify(presenter).dismissModal(eq(modal2), eq(root), eq(root), any());
verify(listener).onSuccess(modal2.getId());
verify(animator, never()).dismiss(eq(modal1), eq(modal2), eq(modal1.options.animations.dismissModal), any());
verify(animator).dismiss(eq(root), eq(modal2), eq(resolvedOptions.animations.dismissModal), any());
assertThat(uut.size()).isEqualTo(0);
}
@Test
public void dismissAllModals_bottomModalsAreDestroyed() {
disableModalAnimations(modal1, modal2);
uut.showModal(modal1, root, new CommandListenerAdapter());
idleMainLooper();
uut.showModal(modal2, root, new CommandListenerAdapter());
uut.dismissAllModals(root, Options.EMPTY, new CommandListenerAdapter());
verify(modal1).destroy();
verify(modal1).onViewDisappear();
assertThat(uut.size()).isEqualTo(0);
}
@Test
public void isEmpty() {
assertThat(uut.isEmpty()).isTrue();
uut.showModal(modal1, root, new CommandListenerAdapter());
assertThat(uut.isEmpty()).isFalse();
uut.dismissAllModals(root, Options.EMPTY, new CommandListenerAdapter());
assertThat(uut.isEmpty()).isTrue();
}
@Test
public void peek() {
assertThat(uut.isEmpty()).isTrue();
assertThatThrownBy(() -> uut.peek()).isInstanceOf(EmptyStackException.class);
uut.showModal(modal1, root, new CommandListenerAdapter() {
@Override
public void onSuccess(String childId) {
assertThat(uut.peek()).isEqualTo(modal1);
}
});
}
@Test
public void onDismiss_onViewAppearedInvokedOnPreviousModal() {
disableShowModalAnimation(modal1, modal2);
uut.showModal(modal1, root, new CommandListenerAdapter());
idleMainLooper();
uut.showModal(modal2, root, new CommandListenerAdapter());
idleMainLooper();
uut.dismissModal(modal2.getId(), root, new CommandListenerAdapter());
idleMainLooper();
verify(modal1, times(2)).onViewWillAppear();
}
@Test
public void onDismiss_dismissModalInTheMiddleOfStack() {
disableShowModalAnimation(modal1, modal2, modal3);
disableDismissModalAnimation(modal1, modal2, modal3);
uut.showModal(modal1, root, new CommandListenerAdapter());
uut.showModal(modal2, root, new CommandListenerAdapter());
idleMainLooper();
uut.showModal(modal3, root, new CommandListenerAdapter());
uut.dismissModal(modal2.getId(), root, new CommandListenerAdapter());
idleMainLooper();
assertThat(uut.size()).isEqualTo(2);
verify(modal2).onViewDisappear();
verify(modal2).destroy();
assertThat(modal1.getView().getParent()).isNull();
}
@Test
public void handleBack_doesNothingIfModalStackIsEmpty() {
assertThat(uut.isEmpty()).isTrue();
assertThat(uut.handleBack(new CommandListenerAdapter(), root)).isFalse();
}
@Test
public void handleBack_dismissModal() {
disableDismissModalAnimation(modal1);
uut.showModal(modal1, root, new CommandListenerAdapter());
idleMainLooper();
assertThat(uut.handleBack(new CommandListenerAdapter(), root)).isTrue();
verify(modal1).onViewDisappear();
}
@Test
public void handleBack_ViewControllerTakesPrecedenceOverModal() {
ViewController<?> backHandlingModal = spy(new SimpleViewController(activity, childRegistry, "stack", new Options()){
@Override
public boolean handleBack(CommandListener listener) {
return true;
}
});
uut.showModal(backHandlingModal, root, new CommandListenerAdapter());
root.getView().getViewTreeObserver().dispatchOnGlobalLayout();
assertThat(uut.handleBack(new CommandListenerAdapter(), any())).isTrue();
verify(backHandlingModal, times(1)).handleBack(any());
verify(backHandlingModal, times(0)).onViewDisappear();
}
@Test
public void setDefaultOptions() {
Options defaultOptions = new Options();
uut.setDefaultOptions(defaultOptions);
verify(presenter).setDefaultOptions(defaultOptions);
}
@Test
public void destroy() {
showModalsWithoutAnimation(modal1, modal2);
uut.destroy();
verify(modal1).destroy();
verify(modal2).destroy();
}
private ViewController<?> findModal(String id) {
return uut.findControllerById(id);
}
private void showModalsWithoutAnimation(ViewController<?>... modals) {
for (ViewController<?> modal : modals) {
showModalWithoutAnimation(modal);
}
}
private void showModalWithoutAnimation(ViewController<?> modal) {
disableShowModalAnimation(modal);
uut.showModal(modal, root, new CommandListenerAdapter());
}
}