瀏覽代碼

Don't apply default options in mergeOptions

Guy Carmeli 6 年之前
父節點
當前提交
6092900e17

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/parse/TopBarOptions.java 查看文件

@@ -95,7 +95,7 @@ public class TopBarOptions {
95 95
         validate();
96 96
     }
97 97
 
98
-    private void validate() {
98
+    public void validate() {
99 99
         if (title.component.hasValue() && (title.text.hasValue() || subtitle.text.hasValue())) {
100 100
             if (BuildConfig.DEBUG) Log.w("RNN", "A screen can't use both text and component - clearing text.");
101 101
             title.text = new NullText();

+ 2
- 3
lib/android/app/src/main/java/com/reactnativenavigation/presentation/BottomTabsOptionsPresenter.java 查看文件

@@ -60,10 +60,9 @@ public class BottomTabsOptionsPresenter {
60 60
     }
61 61
 
62 62
     public void mergeChildOptions(Options options, Component child) {
63
-        Options withDefault = options.copy().withDefaultOptions(defaultOptions);
64
-        mergeBottomTabsOptions(withDefault.bottomTabsOptions, withDefault.animations);
63
+        mergeBottomTabsOptions(options.bottomTabsOptions, options.animations);
65 64
         int tabIndex = bottomTabFinder.findByComponent(child);
66
-        mergeDrawBehind(withDefault.bottomTabsOptions, tabIndex);
65
+        mergeDrawBehind(options.bottomTabsOptions, tabIndex);
67 66
     }
68 67
 
69 68
     private void mergeBottomTabsOptions(BottomTabsOptions options, AnimationsOptions animations) {

+ 5
- 6
lib/android/app/src/main/java/com/reactnativenavigation/presentation/StackOptionsPresenter.java 查看文件

@@ -156,12 +156,11 @@ public class StackOptionsPresenter {
156 156
     }
157 157
 
158 158
     public void mergeChildOptions(Options options, Component child) {
159
-        Options withDefaultOptions = options.copy().withDefaultOptions(defaultOptions);
160
-        mergeOrientation(withDefaultOptions.layout.orientation);
161
-        mergeButtons(withDefaultOptions.topBar.buttons);
162
-        mergeTopBarOptions(withDefaultOptions.topBar, withDefaultOptions.animations, child);
163
-        mergeTopTabsOptions(withDefaultOptions.topTabs);
164
-        mergeTopTabOptions(withDefaultOptions.topTabOptions);
159
+        mergeOrientation(options.layout.orientation);
160
+        mergeButtons(options.topBar.buttons);
161
+        mergeTopBarOptions(options.topBar, options.animations, child);
162
+        mergeTopTabsOptions(options.topTabs);
163
+        mergeTopTabOptions(options.topTabOptions);
165 164
     }
166 165
 
167 166
     private void mergeOrientation(OrientationOptions orientationOptions) {

+ 54
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/BottomTabsOptionsPresenterTest.java 查看文件

@@ -0,0 +1,54 @@
1
+package com.reactnativenavigation.viewcontrollers;
2
+
3
+import android.app.Activity;
4
+
5
+import com.reactnativenavigation.BaseTest;
6
+import com.reactnativenavigation.mocks.SimpleViewController;
7
+import com.reactnativenavigation.parse.Options;
8
+import com.reactnativenavigation.parse.params.Bool;
9
+import com.reactnativenavigation.parse.params.Color;
10
+import com.reactnativenavigation.presentation.BottomTabsOptionsPresenter;
11
+import com.reactnativenavigation.viewcontrollers.bottomtabs.TabSelector;
12
+import com.reactnativenavigation.views.BottomTabs;
13
+import com.reactnativenavigation.views.Component;
14
+
15
+import org.junit.Test;
16
+import org.mockito.Mockito;
17
+
18
+import java.util.Arrays;
19
+import java.util.List;
20
+
21
+import static org.mockito.Mockito.spy;
22
+import static org.mockito.Mockito.verify;
23
+import static org.mockito.Mockito.verifyNoMoreInteractions;
24
+
25
+public class BottomTabsOptionsPresenterTest extends BaseTest {
26
+    private List<ViewController> tabs;
27
+    private BottomTabsOptionsPresenter uut;
28
+    private BottomTabs bottomTabs;
29
+
30
+    @Override
31
+    public void beforeEach() {
32
+        Activity activity = newActivity();
33
+        ChildControllersRegistry childRegistry = new ChildControllersRegistry();
34
+        ViewController child1 = spy(new SimpleViewController(activity, childRegistry, "child1", new Options()));
35
+        ViewController child2 = spy(new SimpleViewController(activity, childRegistry, "child2", new Options()));
36
+        tabs = Arrays.asList(child1, child2);
37
+        uut = new BottomTabsOptionsPresenter(tabs, new Options());
38
+        bottomTabs = Mockito.mock(BottomTabs.class);
39
+        uut.bindView(bottomTabs, Mockito.mock(TabSelector.class));
40
+    }
41
+
42
+    @Test
43
+    public void mergeChildOptions_onlyDeclaredOptionsAreApplied() { // default options are not applies on merge
44
+        Options defaultOptions = new Options();
45
+        defaultOptions.bottomTabsOptions.visible = new Bool(false);
46
+        uut.setDefaultOptions(defaultOptions);
47
+
48
+        Options options = new Options();
49
+        options.bottomTabsOptions.backgroundColor = new Color(10);
50
+        uut.mergeChildOptions(options, (Component) tabs.get(0).getView());
51
+        verify(bottomTabs).setBackgroundColor(options.bottomTabsOptions.backgroundColor.get());
52
+        verifyNoMoreInteractions(bottomTabs);
53
+    }
54
+}

+ 14
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackOptionsPresenterTest.java 查看文件

@@ -104,6 +104,7 @@ public class StackOptionsPresenterTest extends BaseTest {
104 104
         options.topBar.visible = new Bool(false);
105 105
         options.topBar.drawBehind = new Bool(false);
106 106
         options.topBar.hideOnScroll = new Bool(false);
107
+        options.topBar.validate();
107 108
         uut.mergeChildOptions(options, child);
108 109
 
109 110
         assertTopBarOptions(options, 1);
@@ -156,6 +157,19 @@ public class StackOptionsPresenterTest extends BaseTest {
156 157
         verify(topBar).hide();
157 158
     }
158 159
 
160
+    @Test
161
+    public void mergeOptions_defaultOptionsAreNotApplied() {
162
+        Options defaultOptions = new Options();
163
+        defaultOptions.topBar.background.color = new Color(10);
164
+        uut.setDefaultOptions(defaultOptions);
165
+
166
+        Options childOptions = new Options();
167
+        childOptions.topBar.title.text = new Text("someText");
168
+        uut.mergeChildOptions(childOptions, child);
169
+
170
+        verify(topBar, times(0)).setBackgroundColor(anyInt());
171
+    }
172
+
159 173
     private void assertTopBarOptions(Options options, int t) {
160 174
         if (options.topBar.title.component.hasValue()) {
161 175
             verify(topBar, times(0)).setTitle(any());