瀏覽代碼

Apply options only on first controller (#2734)

Guy Carmeli 6 年之前
父節點
當前提交
0d7005a586
No account linked to committer's email address

+ 15
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/Options.java 查看文件

@@ -81,4 +81,19 @@ public class Options implements DEFAULT_VALUES {
81 81
         bottomTabsOptions = new BottomTabsOptions();
82 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 查看文件

@@ -59,7 +59,7 @@ public class BottomTabsController extends ParentController implements AHBottomNa
59 59
         int tabIndex = findTabContainingComponent(childComponent);
60 60
         if (tabIndex >= 0) new BottomTabOptionsPresenter(bottomTabs).present(options, tabIndex);
61 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 查看文件

@@ -39,7 +39,7 @@ public class StackController extends ParentController <StackLayout> {
39 39
         super.applyOptions(options, component);
40 40
         stackLayout.applyOptions(this.options, component);
41 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 查看文件

@@ -70,8 +70,10 @@ public class TopTabsController extends ParentController<TopTabsViewPager> implem
70 70
     @Override
71 71
     public void applyOptions(Options options, ReactComponent childComponent) {
72 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 查看文件

@@ -183,9 +183,20 @@ public class OptionsTest extends BaseTest {
183 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 查看文件

@@ -7,9 +7,12 @@ import com.reactnativenavigation.BaseTest;
7 7
 import com.reactnativenavigation.mocks.MockPromise;
8 8
 import com.reactnativenavigation.mocks.SimpleViewController;
9 9
 import com.reactnativenavigation.parse.Options;
10
+import com.reactnativenavigation.parse.Text;
11
+import com.reactnativenavigation.views.ReactComponent;
10 12
 
11 13
 import org.assertj.core.api.iterable.Extractor;
12 14
 import org.junit.Test;
15
+import org.mockito.ArgumentCaptor;
13 16
 
14 17
 import javax.annotation.Nullable;
15 18
 
@@ -338,6 +341,25 @@ public class StackControllerTest extends BaseTest {
338 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 363
     private void assertContainsOnlyId(String... ids) {
342 364
         assertThat(uut.size()).isEqualTo(ids.length);
343 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 查看文件

@@ -5,6 +5,7 @@ import android.support.annotation.NonNull;
5 5
 import android.view.ViewGroup;
6 6
 
7 7
 import com.reactnativenavigation.BaseTest;
8
+import com.reactnativenavigation.mocks.MockPromise;
8 9
 import com.reactnativenavigation.mocks.TestComponentViewCreator;
9 10
 import com.reactnativenavigation.mocks.TestReactView;
10 11
 import com.reactnativenavigation.parse.Options;
@@ -15,13 +16,14 @@ import com.reactnativenavigation.views.ReactComponent;
15 16
 import com.reactnativenavigation.views.TopTabsLayoutCreator;
16 17
 import com.reactnativenavigation.views.TopTabsViewPager;
17 18
 
18
-import org.assertj.core.api.Assertions;
19 19
 import org.junit.Test;
20
+import org.mockito.ArgumentCaptor;
20 21
 import org.mockito.Mockito;
21 22
 
22 23
 import java.util.ArrayList;
23 24
 import java.util.List;
24 25
 
26
+import static org.assertj.core.api.Assertions.assertThat;
25 27
 import static org.mockito.ArgumentMatchers.any;
26 28
 import static org.mockito.ArgumentMatchers.eq;
27 29
 import static org.mockito.Mockito.spy;
@@ -38,14 +40,15 @@ public class TopTabsViewControllerTest extends BaseTest {
38 40
     private List<Options> tabOptions = new ArrayList<>(SIZE);
39 41
     private final Options options = new Options();
40 42
     private TopTabsViewPager topTabsLayout;
43
+    private Activity activity;
41 44
 
42 45
     @Override
43 46
     public void beforeEach() {
44 47
         super.beforeEach();
45 48
 
46
-        final Activity activity = newActivity();
49
+        activity = newActivity();
47 50
         tabOptions = createOptions();
48
-        tabControllers = createTabsControllers(activity);
51
+        tabControllers = createTabsControllers(activity, tabOptions);
49 52
 
50 53
         topTabsLayout = spy(new TopTabsViewPager(activity, tabControllers, new TopTabsAdapter(tabControllers)));
51 54
         TopTabsLayoutCreator layoutCreator = Mockito.mock(TopTabsLayoutCreator.class);
@@ -54,6 +57,7 @@ public class TopTabsViewControllerTest extends BaseTest {
54 57
         tabControllers.forEach(viewController -> viewController.setParentController(uut));
55 58
 
56 59
         parentController = spy(new StackController(activity, "stackId", new Options()));
60
+        parentController.push(uut, new MockPromise());
57 61
         uut.setParentController(parentController);
58 62
     }
59 63
 
@@ -69,7 +73,7 @@ public class TopTabsViewControllerTest extends BaseTest {
69 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 77
         List<ViewController> tabControllers = new ArrayList<>(SIZE);
74 78
         for (int i = 0; i < SIZE; i++) {
75 79
             ComponentViewController viewController = new ComponentViewController(
@@ -165,17 +169,17 @@ public class TopTabsViewControllerTest extends BaseTest {
165 169
         uut.onViewAppeared();
166 170
         ReactComponent currentTab = tabView(0);
167 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 174
         uut.switchToTab(1);
171 175
         currentTab = tabView(1);
172 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 179
         uut.switchToTab(0);
176 180
         currentTab = tabView(0);
177 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 185
     private TestReactView getActualTabView(int index) {
@@ -193,6 +197,16 @@ public class TopTabsViewControllerTest extends BaseTest {
193 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 210
     private IReactView tab(TopTabsViewPager topTabs, final int index) {
197 211
         return (IReactView) ((ViewGroup) topTabs.getChildAt(index)).getChildAt(0);
198 212
     }