Browse Source

Apply options only on first controller (#2734)

Guy Carmeli 6 years ago
parent
commit
0d7005a586
No account linked to committer's email address

+ 15
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/Options.java View File

81
         bottomTabsOptions = new BottomTabsOptions();
81
         bottomTabsOptions = new BottomTabsOptions();
82
         return this;
82
         return this;
83
     }
83
     }
84
+
85
+    public Options clearTopTabOptions() {
86
+        topTabOptions = new TopTabOptions();
87
+        return this;
88
+    }
89
+
90
+    public Options clearTopTabsOptions() {
91
+        topTabsOptions = new TopTabsOptions();
92
+        return this;
93
+    }
94
+
95
+    public Options clearBottomTabOptions() {
96
+        bottomTabOptions = new BottomTabOptions();
97
+        return this;
98
+    }
84
 }
99
 }

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/BottomTabsController.java View File

59
         int tabIndex = findTabContainingComponent(childComponent);
59
         int tabIndex = findTabContainingComponent(childComponent);
60
         if (tabIndex >= 0) new BottomTabOptionsPresenter(bottomTabs).present(options, tabIndex);
60
         if (tabIndex >= 0) new BottomTabOptionsPresenter(bottomTabs).present(options, tabIndex);
61
         applyOnParentController(parentController ->
61
         applyOnParentController(parentController ->
62
-                ((ParentController) parentController).applyOptions(this.options.copy().clearBottomTabsOptions(), childComponent)
62
+                ((ParentController) parentController).applyOptions(this.options.copy().clearBottomTabsOptions().clearBottomTabOptions(), childComponent)
63
         );
63
         );
64
     }
64
     }
65
 
65
 

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java View File

39
         super.applyOptions(options, component);
39
         super.applyOptions(options, component);
40
         stackLayout.applyOptions(this.options, component);
40
         stackLayout.applyOptions(this.options, component);
41
         applyOnParentController(parentController ->
41
         applyOnParentController(parentController ->
42
-                ((ParentController) parentController).applyOptions(this.options, component)
42
+                ((ParentController) parentController).applyOptions(this.options.copy().clearTopBarOptions(), component)
43
         );
43
         );
44
     }
44
     }
45
 
45
 

+ 4
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/toptabs/TopTabsController.java View File

70
     @Override
70
     @Override
71
     public void applyOptions(Options options, ReactComponent childComponent) {
71
     public void applyOptions(Options options, ReactComponent childComponent) {
72
         super.applyOptions(options, childComponent);
72
         super.applyOptions(options, childComponent);
73
-        applyOnParentController(parentController ->
74
-                ((ParentController) parentController).applyOptions(this.options, childComponent)
73
+        applyOnParentController(parentController -> {
74
+                Options opt = this.options.copy();
75
+                ((ParentController) parentController).applyOptions(opt.clearTopTabOptions().clearTopTabsOptions(), childComponent);
76
+            }
75
         );
77
         );
76
     }
78
     }
77
 
79
 

+ 16
- 5
lib/android/app/src/test/java/com/reactnativenavigation/parse/OptionsTest.java View File

183
         assertThat(uut.bottomTabsOptions.color.hasValue()).isFalse();
183
         assertThat(uut.bottomTabsOptions.color.hasValue()).isFalse();
184
     }
184
     }
185
 
185
 
186
-//    topBarOptions.mergeWithDefault(other.topBarOptions);
187
-//        topTabOptions.mergeWithDefault(other.topTabOptions);
188
-//        topTabsOptions.mergeWithDefault(other.topTabsOptions);
189
-//        bottomTabOptions.mergeWithDefault(other.bottomTabOptions);
190
-//        bottomTabsOptions.mergeWithDefault(other.bottomTabsOptions);
186
+    @Test
187
+    public void clear_topTabsOptions() throws Exception {
188
+        Options uut = new Options();
189
+        uut.topTabsOptions.fontSize = new Number(666);
190
+        uut.clearTopTabsOptions();
191
+        assertThat(uut.topTabsOptions.fontSize.hasValue()).isFalse();
192
+    }
193
+
194
+
195
+    @Test
196
+    public void clear_topTabOptions() throws Exception {
197
+        Options uut = new Options();
198
+        uut.topTabOptions.title = new Text("some title");
199
+        uut.clearTopTabOptions();
200
+        assertThat(uut.topTabOptions.title.hasValue()).isFalse();
201
+    }
191
 }
202
 }

+ 22
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java View File

7
 import com.reactnativenavigation.mocks.MockPromise;
7
 import com.reactnativenavigation.mocks.MockPromise;
8
 import com.reactnativenavigation.mocks.SimpleViewController;
8
 import com.reactnativenavigation.mocks.SimpleViewController;
9
 import com.reactnativenavigation.parse.Options;
9
 import com.reactnativenavigation.parse.Options;
10
+import com.reactnativenavigation.parse.Text;
11
+import com.reactnativenavigation.views.ReactComponent;
10
 
12
 
11
 import org.assertj.core.api.iterable.Extractor;
13
 import org.assertj.core.api.iterable.Extractor;
12
 import org.junit.Test;
14
 import org.junit.Test;
15
+import org.mockito.ArgumentCaptor;
13
 
16
 
14
 import javax.annotation.Nullable;
17
 import javax.annotation.Nullable;
15
 
18
 
338
         assertThat(parent.getView().getChildAt(1)).isEqualTo(uut.getView());
341
         assertThat(parent.getView().getChildAt(1)).isEqualTo(uut.getView());
339
     }
342
     }
340
 
343
 
344
+    @Test
345
+    public void applyOptions_applyOnlyOnFirstStack() throws Exception {
346
+        StackController parent = spy(new StackController(activity, "someStack", new Options()));
347
+        parent.ensureViewIsCreated();
348
+        parent.push(uut, new MockPromise());
349
+
350
+        Options childOptions = new Options();
351
+        childOptions.topBarOptions.title = new Text("Something");
352
+        child1.options = childOptions;
353
+        uut.push(child1, new MockPromise());
354
+        child1.ensureViewIsCreated();
355
+        child1.onViewAppeared();
356
+
357
+        ArgumentCaptor<Options> optionsCaptor = ArgumentCaptor.forClass(Options.class);
358
+        ArgumentCaptor<ReactComponent> viewCaptor = ArgumentCaptor.forClass(ReactComponent.class);
359
+        verify(parent, times(1)).applyOptions(optionsCaptor.capture(), viewCaptor.capture());
360
+        assertThat(optionsCaptor.getValue().topBarOptions.title.hasValue()).isFalse();
361
+    }
362
+
341
     private void assertContainsOnlyId(String... ids) {
363
     private void assertContainsOnlyId(String... ids) {
342
         assertThat(uut.size()).isEqualTo(ids.length);
364
         assertThat(uut.size()).isEqualTo(ids.length);
343
         assertThat(uut.getChildControllers()).extracting((Extractor<ViewController, String>) ViewController::getId).containsOnly(ids);
365
         assertThat(uut.getChildControllers()).extracting((Extractor<ViewController, String>) ViewController::getId).containsOnly(ids);

+ 21
- 7
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/TopTabsViewControllerTest.java View File

5
 import android.view.ViewGroup;
5
 import android.view.ViewGroup;
6
 
6
 
7
 import com.reactnativenavigation.BaseTest;
7
 import com.reactnativenavigation.BaseTest;
8
+import com.reactnativenavigation.mocks.MockPromise;
8
 import com.reactnativenavigation.mocks.TestComponentViewCreator;
9
 import com.reactnativenavigation.mocks.TestComponentViewCreator;
9
 import com.reactnativenavigation.mocks.TestReactView;
10
 import com.reactnativenavigation.mocks.TestReactView;
10
 import com.reactnativenavigation.parse.Options;
11
 import com.reactnativenavigation.parse.Options;
15
 import com.reactnativenavigation.views.TopTabsLayoutCreator;
16
 import com.reactnativenavigation.views.TopTabsLayoutCreator;
16
 import com.reactnativenavigation.views.TopTabsViewPager;
17
 import com.reactnativenavigation.views.TopTabsViewPager;
17
 
18
 
18
-import org.assertj.core.api.Assertions;
19
 import org.junit.Test;
19
 import org.junit.Test;
20
+import org.mockito.ArgumentCaptor;
20
 import org.mockito.Mockito;
21
 import org.mockito.Mockito;
21
 
22
 
22
 import java.util.ArrayList;
23
 import java.util.ArrayList;
23
 import java.util.List;
24
 import java.util.List;
24
 
25
 
26
+import static org.assertj.core.api.Assertions.assertThat;
25
 import static org.mockito.ArgumentMatchers.any;
27
 import static org.mockito.ArgumentMatchers.any;
26
 import static org.mockito.ArgumentMatchers.eq;
28
 import static org.mockito.ArgumentMatchers.eq;
27
 import static org.mockito.Mockito.spy;
29
 import static org.mockito.Mockito.spy;
38
     private List<Options> tabOptions = new ArrayList<>(SIZE);
40
     private List<Options> tabOptions = new ArrayList<>(SIZE);
39
     private final Options options = new Options();
41
     private final Options options = new Options();
40
     private TopTabsViewPager topTabsLayout;
42
     private TopTabsViewPager topTabsLayout;
43
+    private Activity activity;
41
 
44
 
42
     @Override
45
     @Override
43
     public void beforeEach() {
46
     public void beforeEach() {
44
         super.beforeEach();
47
         super.beforeEach();
45
 
48
 
46
-        final Activity activity = newActivity();
49
+        activity = newActivity();
47
         tabOptions = createOptions();
50
         tabOptions = createOptions();
48
-        tabControllers = createTabsControllers(activity);
51
+        tabControllers = createTabsControllers(activity, tabOptions);
49
 
52
 
50
         topTabsLayout = spy(new TopTabsViewPager(activity, tabControllers, new TopTabsAdapter(tabControllers)));
53
         topTabsLayout = spy(new TopTabsViewPager(activity, tabControllers, new TopTabsAdapter(tabControllers)));
51
         TopTabsLayoutCreator layoutCreator = Mockito.mock(TopTabsLayoutCreator.class);
54
         TopTabsLayoutCreator layoutCreator = Mockito.mock(TopTabsLayoutCreator.class);
54
         tabControllers.forEach(viewController -> viewController.setParentController(uut));
57
         tabControllers.forEach(viewController -> viewController.setParentController(uut));
55
 
58
 
56
         parentController = spy(new StackController(activity, "stackId", new Options()));
59
         parentController = spy(new StackController(activity, "stackId", new Options()));
60
+        parentController.push(uut, new MockPromise());
57
         uut.setParentController(parentController);
61
         uut.setParentController(parentController);
58
     }
62
     }
59
 
63
 
69
         return result;
73
         return result;
70
     }
74
     }
71
 
75
 
72
-    private List<ViewController> createTabsControllers(Activity activity) {
76
+    private List<ViewController> createTabsControllers(Activity activity, List<Options> tabOptions) {
73
         List<ViewController> tabControllers = new ArrayList<>(SIZE);
77
         List<ViewController> tabControllers = new ArrayList<>(SIZE);
74
         for (int i = 0; i < SIZE; i++) {
78
         for (int i = 0; i < SIZE; i++) {
75
             ComponentViewController viewController = new ComponentViewController(
79
             ComponentViewController viewController = new ComponentViewController(
165
         uut.onViewAppeared();
169
         uut.onViewAppeared();
166
         ReactComponent currentTab = tabView(0);
170
         ReactComponent currentTab = tabView(0);
167
         verify(uut, times(1)).applyOptions(any(Options.class), eq(currentTab));
171
         verify(uut, times(1)).applyOptions(any(Options.class), eq(currentTab));
168
-        Assertions.assertThat(uut.options.topBarOptions.title.get()).isEqualTo(createTabTopBarTitle(0));
172
+        assertThat(uut.options.topBarOptions.title.get()).isEqualTo(createTabTopBarTitle(0));
169
 
173
 
170
         uut.switchToTab(1);
174
         uut.switchToTab(1);
171
         currentTab = tabView(1);
175
         currentTab = tabView(1);
172
         verify(uut, times(1)).applyOptions(any(Options.class), eq(currentTab));
176
         verify(uut, times(1)).applyOptions(any(Options.class), eq(currentTab));
173
-        Assertions.assertThat(uut.options.topBarOptions.title.get()).isEqualTo(createTabTopBarTitle(1));
177
+        assertThat(uut.options.topBarOptions.title.get()).isEqualTo(createTabTopBarTitle(1));
174
 
178
 
175
         uut.switchToTab(0);
179
         uut.switchToTab(0);
176
         currentTab = tabView(0);
180
         currentTab = tabView(0);
177
         verify(uut, times(2)).applyOptions(any(Options.class), eq(currentTab));
181
         verify(uut, times(2)).applyOptions(any(Options.class), eq(currentTab));
178
-        Assertions.assertThat(uut.options.topBarOptions.title.get()).isEqualTo(createTabTopBarTitle(0));
182
+        assertThat(uut.options.topBarOptions.title.get()).isEqualTo(createTabTopBarTitle(0));
179
     }
183
     }
180
 
184
 
181
     private TestReactView getActualTabView(int index) {
185
     private TestReactView getActualTabView(int index) {
193
         verify(topTabsLayout, times(1)).applyOptions(any(Options.class));
197
         verify(topTabsLayout, times(1)).applyOptions(any(Options.class));
194
     }
198
     }
195
 
199
 
200
+    @Test
201
+    public void applyOptions_applyOnlyOnFirstTopTabs() throws Exception {
202
+        tabOptions.get(0).topTabOptions.title = new Text("tab title");
203
+        tabControllers.get(0).onViewAppeared();
204
+        ArgumentCaptor<Options> optionsCaptor = ArgumentCaptor.forClass(Options.class);
205
+        ArgumentCaptor<ReactComponent> viewCaptor = ArgumentCaptor.forClass(ReactComponent.class);
206
+        verify(parentController, times(1)).applyOptions(optionsCaptor.capture(), viewCaptor.capture());
207
+        assertThat(optionsCaptor.getValue().topTabOptions.title.hasValue()).isFalse();
208
+    }
209
+
196
     private IReactView tab(TopTabsViewPager topTabs, final int index) {
210
     private IReactView tab(TopTabsViewPager topTabs, final int index) {
197
         return (IReactView) ((ViewGroup) topTabs.getChildAt(index)).getChildAt(0);
211
         return (IReactView) ((ViewGroup) topTabs.getChildAt(index)).getChildAt(0);
198
     }
212
     }