Browse Source

fix android unittests

Daniel Zlotin 6 years ago
parent
commit
74f377fca7

+ 61
- 57
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ComponentViewController.java View File

13
     public interface ReactViewCreator {
13
     public interface ReactViewCreator {
14
 
14
 
15
         IReactView create(Activity activity, String componentId, String componentName);
15
         IReactView create(Activity activity, String componentId, String componentName);
16
-	}
16
+    }
17
 
17
 
18
     public interface IReactView {
18
     public interface IReactView {
19
 
19
 
23
 
23
 
24
         void destroy();
24
         void destroy();
25
 
25
 
26
-		void sendComponentStart();
26
+        void sendComponentStart();
27
 
27
 
28
-		void sendComponentStop();
28
+        void sendComponentStop();
29
 
29
 
30
         void sendOnNavigationButtonPressed(String buttonId);
30
         void sendOnNavigationButtonPressed(String buttonId);
31
     }
31
     }
32
 
32
 
33
-	private final String componentName;
34
-
35
-	private final ReactViewCreator viewCreator;
36
-	private NavigationOptions options;
37
-	private ReactComponent component;
38
-
39
-	public ComponentViewController(final Activity activity,
40
-								   final String id,
41
-								   final String componentName,
42
-								   final ReactViewCreator viewCreator,
43
-								   final NavigationOptions initialNavigationOptions) {
44
-		super(activity, id);
45
-		this.componentName = componentName;
46
-		this.viewCreator = viewCreator;
47
-		this.options = initialNavigationOptions;
48
-	}
33
+    private final String componentName;
34
+
35
+    private final ReactViewCreator viewCreator;
36
+    private NavigationOptions options;
37
+    private ReactComponent component;
38
+
39
+    public ComponentViewController(final Activity activity,
40
+                                   final String id,
41
+                                   final String componentName,
42
+                                   final ReactViewCreator viewCreator,
43
+                                   final NavigationOptions initialNavigationOptions) {
44
+        super(activity, id);
45
+        this.componentName = componentName;
46
+        this.viewCreator = viewCreator;
47
+        this.options = initialNavigationOptions;
48
+    }
49
 
49
 
50
     @RestrictTo(RestrictTo.Scope.TESTS)
50
     @RestrictTo(RestrictTo.Scope.TESTS)
51
     TopBar getTopBar() {
51
     TopBar getTopBar() {
52
         return component.getTopBar();
52
         return component.getTopBar();
53
     }
53
     }
54
 
54
 
55
-	@Override
56
-	public void destroy() {
57
-		super.destroy();
58
-		if (component != null) component.destroy();
59
-		component = null;
60
-	}
61
-
62
-	@Override
63
-	public void onViewAppeared() {
64
-		super.onViewAppeared();
65
-		ensureViewIsCreated();
66
-		component.applyOptions(options);
67
-		component.sendComponentStart();
68
-	}
69
-
70
-	@Override
71
-	public void onViewDisappear() {
72
-		super.onViewDisappear();
73
-		component.sendComponentStop();
74
-	}
75
-
76
-	@Override
77
-	protected boolean isViewShown() {
78
-		return super.isViewShown() && component.isReady();
79
-	}
80
-
81
-	@NonNull
82
-	@Override
83
-	protected View createView() {
84
-		component = (ReactComponent) viewCreator.create(getActivity(), getId(), componentName);
55
+    @Override
56
+    public void destroy() {
57
+        super.destroy();
58
+        if (component != null) component.destroy();
59
+        component = null;
60
+    }
61
+
62
+    @Override
63
+    public void onViewAppeared() {
64
+        super.onViewAppeared();
65
+        ensureViewIsCreated();
66
+        component.applyOptions(options);
67
+        component.sendComponentStart();
68
+    }
69
+
70
+    @Override
71
+    public void onViewDisappear() {
72
+        super.onViewDisappear();
73
+        component.sendComponentStop();
74
+    }
75
+
76
+    @Override
77
+    protected boolean isViewShown() {
78
+        return super.isViewShown() && component.isReady();
79
+    }
80
+
81
+    @NonNull
82
+    @Override
83
+    protected View createView() {
84
+        component = (ReactComponent) viewCreator.create(getActivity(), getId(), componentName);
85
         return component.asView();
85
         return component.asView();
86
-	}
86
+    }
87
 
87
 
88
-	@Override
89
-	public void mergeNavigationOptions(NavigationOptions options) {
90
-		this.options.mergeWith(options);
88
+    @Override
89
+    public void mergeNavigationOptions(NavigationOptions options) {
90
+        this.options.mergeWith(options);
91
         component.applyOptions(options);
91
         component.applyOptions(options);
92
-	}
92
+    }
93
+
94
+    NavigationOptions getOptions() {
95
+        return options;
96
+    }
93
 
97
 
94
-	NavigationOptions getOptions() {
95
-		return options;
96
-	}
98
+    ReactComponent getComponent() {
99
+        return component;
100
+    }
97
 }
101
 }

+ 12
- 16
lib/android/app/src/main/java/com/reactnativenavigation/views/ComponentLayout.java View File

1
 package com.reactnativenavigation.views;
1
 package com.reactnativenavigation.views;
2
 
2
 
3
-import android.annotation.SuppressLint;
4
-import android.content.Context;
5
-import android.os.Build;
6
-import android.support.annotation.RestrictTo;
7
-import android.view.View;
8
-import android.view.ViewGroup;
9
-import android.widget.RelativeLayout;
10
-
11
-import com.facebook.react.uimanager.events.EventDispatcher;
12
-import com.reactnativenavigation.parse.NavigationOptions;
13
-import com.reactnativenavigation.presentation.OptionsPresenter;
14
-import com.reactnativenavigation.viewcontrollers.ComponentViewController.IReactView;
15
-
16
-import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
17
-import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
3
+import android.annotation.*;
4
+import android.content.*;
5
+import android.view.*;
6
+import android.widget.*;
7
+
8
+import com.facebook.react.uimanager.events.*;
9
+import com.reactnativenavigation.parse.*;
10
+import com.reactnativenavigation.presentation.*;
11
+import com.reactnativenavigation.viewcontrollers.ComponentViewController.*;
12
+
13
+import static android.view.ViewGroup.LayoutParams.*;
18
 
14
 
19
 @SuppressLint("ViewConstructor")
15
 @SuppressLint("ViewConstructor")
20
 public class ComponentLayout extends RelativeLayout implements ReactComponent {
16
 public class ComponentLayout extends RelativeLayout implements ReactComponent {
25
 
21
 
26
 	public ComponentLayout(Context context, IReactView reactView, EventDispatcher eventDispatcher) {
22
 	public ComponentLayout(Context context, IReactView reactView, EventDispatcher eventDispatcher) {
27
 		super(context);
23
 		super(context);
28
-		this.topBar = new TopBar(context, this);
24
+		this.topBar = new TopBar(context, this, eventDispatcher);
29
 		this.reactView = reactView;
25
 		this.reactView = reactView;
30
         optionsPresenter = new OptionsPresenter(this);
26
         optionsPresenter = new OptionsPresenter(this);
31
         initViews();
27
         initViews();

+ 6
- 5
lib/android/app/src/main/java/com/reactnativenavigation/views/ComponentViewCreator.java View File

1
 package com.reactnativenavigation.views;
1
 package com.reactnativenavigation.views;
2
 
2
 
3
-import android.app.Activity;
3
+import android.app.*;
4
 
4
 
5
-import com.facebook.react.ReactInstanceManager;
6
-import com.reactnativenavigation.react.ReactComponentViewCreator;
7
-import com.reactnativenavigation.viewcontrollers.ComponentViewController;
5
+import com.facebook.react.*;
6
+import com.facebook.react.uimanager.*;
7
+import com.reactnativenavigation.react.*;
8
+import com.reactnativenavigation.viewcontrollers.*;
8
 
9
 
9
 public class ComponentViewCreator implements ComponentViewController.ReactViewCreator {
10
 public class ComponentViewCreator implements ComponentViewController.ReactViewCreator {
10
 
11
 
17
 	@Override
18
 	@Override
18
 	public ComponentViewController.IReactView create(Activity activity, String componentId, String componentName) {
19
 	public ComponentViewController.IReactView create(Activity activity, String componentId, String componentName) {
19
         ComponentViewController.IReactView reactView = new ReactComponentViewCreator(instanceManager).create(activity, componentId, componentName);
20
         ComponentViewController.IReactView reactView = new ReactComponentViewCreator(instanceManager).create(activity, componentId, componentName);
20
-        return new ComponentLayout(activity, reactView);
21
+        return new ComponentLayout(activity, reactView, instanceManager.getCurrentReactContext().getNativeModule(UIManagerModule.class).getEventDispatcher());
21
 	}
22
 	}
22
 }
23
 }

+ 28
- 31
lib/android/app/src/main/java/com/reactnativenavigation/views/TopBar.java View File

1
 package com.reactnativenavigation.views;
1
 package com.reactnativenavigation.views;
2
 
2
 
3
-import android.annotation.SuppressLint;
4
-import android.content.Context;
5
-import android.graphics.Typeface;
6
-import android.support.annotation.ColorInt;
7
-import android.support.annotation.Nullable;
8
-import android.support.design.widget.AppBarLayout;
3
+import android.annotation.*;
4
+import android.content.*;
5
+import android.graphics.*;
6
+import android.support.annotation.*;
7
+import android.support.design.widget.*;
9
 import android.support.v7.widget.Toolbar;
8
 import android.support.v7.widget.Toolbar;
10
-import android.util.Log;
11
-import android.view.Menu;
12
-import android.view.View;
13
-import android.view.ViewGroup;
14
-import android.widget.TextView;
15
-
16
-import com.facebook.react.uimanager.events.EventDispatcher;
17
-import com.reactnativenavigation.anim.TopBarAnimator;
18
-import com.reactnativenavigation.anim.TopBarCollapseBehavior;
9
+import android.util.*;
10
+import android.view.*;
11
+import android.widget.*;
12
+
13
+import com.facebook.react.uimanager.events.*;
14
+import com.reactnativenavigation.anim.*;
19
 import com.reactnativenavigation.parse.Button;
15
 import com.reactnativenavigation.parse.Button;
20
 import com.reactnativenavigation.parse.Color;
16
 import com.reactnativenavigation.parse.Color;
21
-import com.reactnativenavigation.parse.NavigationOptions;
17
+import com.reactnativenavigation.parse.*;
22
 import com.reactnativenavigation.parse.Number;
18
 import com.reactnativenavigation.parse.Number;
23
-import com.reactnativenavigation.viewcontrollers.toptabs.TopTabsViewPager;
24
-import com.reactnativenavigation.parse.Color;
19
+import com.reactnativenavigation.viewcontrollers.toptabs.*;
25
 
20
 
26
-import java.util.ArrayList;
21
+import java.util.*;
27
 
22
 
28
 @SuppressLint("ViewConstructor")
23
 @SuppressLint("ViewConstructor")
29
 public class TopBar extends AppBarLayout {
24
 public class TopBar extends AppBarLayout {
30
-	private final Toolbar titleBar;
25
+    private final Toolbar titleBar;
26
+    private final TopBarCollapseBehavior collapsingBehavior;
27
+    private final TopBarAnimator animator;
31
     private Component component;
28
     private Component component;
32
     private TopTabs topTabs;
29
     private TopTabs topTabs;
33
 
30
 
34
-    public TopBar(final Context context, Component component) {
31
+    public TopBar(final Context context, Component component, EventDispatcher eventDispatcher) {
35
         super(context);
32
         super(context);
36
         collapsingBehavior = new TopBarCollapseBehavior(eventDispatcher, this);
33
         collapsingBehavior = new TopBarCollapseBehavior(eventDispatcher, this);
37
         this.component = component;
34
         this.component = component;
38
         titleBar = new Toolbar(context);
35
         titleBar = new Toolbar(context);
39
         topTabs = new TopTabs(getContext());
36
         topTabs = new TopTabs(getContext());
40
-        animator = new TopBarAnimator(this, component != null ? component.getContentView() : null);
37
+        this.animator = new TopBarAnimator(this, component != null ? component.getContentView() : null);
41
         addView(titleBar);
38
         addView(titleBar);
42
     }
39
     }
43
 
40
 
121
         setLeftButton(leftButton);
118
         setLeftButton(leftButton);
122
     }
119
     }
123
 
120
 
124
-	private void setLeftButton(final Button button) {
125
-		TitleBarButton leftBarButton = new TitleBarButton(component, this.titleBar, button);
126
-		leftBarButton.applyNavigationIcon(getContext());
127
-	}
121
+    private void setLeftButton(final Button button) {
122
+        TitleBarButton leftBarButton = new TitleBarButton(component, this.titleBar, button);
123
+        leftBarButton.applyNavigationIcon(getContext());
124
+    }
128
 
125
 
129
     private void setRightButtons(ArrayList<Button> rightButtons) {
126
     private void setRightButtons(ArrayList<Button> rightButtons) {
130
         if (rightButtons == null || rightButtons.size() == 0) {
127
         if (rightButtons == null || rightButtons.size() == 0) {
134
         Menu menu = getTitleBar().getMenu();
131
         Menu menu = getTitleBar().getMenu();
135
         menu.clear();
132
         menu.clear();
136
 
133
 
137
-		for (int i = 0; i < rightButtons.size(); i++){
138
-	   		Button button = rightButtons.get(i);
139
-			TitleBarButton titleBarButton = new TitleBarButton(component, this.titleBar, button);
140
-			titleBarButton.addToMenu(getContext(), menu);
141
-       }
134
+        for (int i = 0; i < rightButtons.size(); i++) {
135
+            Button button = rightButtons.get(i);
136
+            TitleBarButton titleBarButton = new TitleBarButton(component, this.titleBar, button);
137
+            titleBarButton.addToMenu(getContext(), menu);
138
+        }
142
     }
139
     }
143
 
140
 
144
     public Toolbar getTitleBar() {
141
     public Toolbar getTitleBar() {

+ 14
- 18
lib/android/app/src/main/java/com/reactnativenavigation/views/TopTabsLayout.java View File

1
 package com.reactnativenavigation.views;
1
 package com.reactnativenavigation.views;
2
 
2
 
3
-import android.annotation.SuppressLint;
4
-import android.content.Context;
5
-import android.support.annotation.RestrictTo;
6
-import android.support.v4.view.ViewPager;
7
-import android.view.View;
8
-import android.view.ViewGroup;
9
-import android.widget.LinearLayout;
10
-import android.widget.RelativeLayout;
11
-
12
-import com.reactnativenavigation.parse.NavigationOptions;
13
-import com.reactnativenavigation.presentation.OptionsPresenter;
14
-import com.reactnativenavigation.utils.Task;
15
-import com.reactnativenavigation.viewcontrollers.toptabs.TopTabController;
16
-import com.reactnativenavigation.viewcontrollers.toptabs.TopTabsAdapter;
17
-import com.reactnativenavigation.viewcontrollers.toptabs.TopTabsViewPager;
18
-
19
-import java.util.List;
3
+import android.annotation.*;
4
+import android.content.*;
5
+import android.support.annotation.*;
6
+import android.support.v4.view.*;
7
+import android.view.*;
8
+import android.widget.*;
9
+
10
+import com.reactnativenavigation.parse.*;
11
+import com.reactnativenavigation.presentation.*;
12
+import com.reactnativenavigation.utils.*;
13
+import com.reactnativenavigation.viewcontrollers.toptabs.*;
14
+
15
+import java.util.*;
20
 
16
 
21
 @SuppressLint("ViewConstructor")
17
 @SuppressLint("ViewConstructor")
22
 public class TopTabsLayout extends RelativeLayout implements Component {
18
 public class TopTabsLayout extends RelativeLayout implements Component {
29
     public TopTabsLayout(Context context, List<TopTabController> tabs, TopTabsAdapter adapter) {
25
     public TopTabsLayout(Context context, List<TopTabController> tabs, TopTabsAdapter adapter) {
30
         super(context);
26
         super(context);
31
         this.tabs = tabs;
27
         this.tabs = tabs;
32
-        topBar = new TopBar(context, this);
28
+        topBar = new TopBar(context, this, null);
33
         topBar.setId(View.generateViewId());
29
         topBar.setId(View.generateViewId());
34
         viewPager = new TopTabsViewPager(context, tabs, adapter);
30
         viewPager = new TopTabsViewPager(context, tabs, adapter);
35
         optionsPresenter = new OptionsPresenter(this);
31
         optionsPresenter = new OptionsPresenter(this);

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/mocks/TestComponentLayout.java View File

18
 
18
 
19
     public TestComponentLayout(final Context context) {
19
     public TestComponentLayout(final Context context) {
20
 		super(context);
20
 		super(context);
21
-        topBar = new TopBar(context, this);
21
+        topBar = new TopBar(context, this, null);
22
         contentView = new View(context);
22
         contentView = new View(context);
23
         addView(topBar);
23
         addView(topBar);
24
         addView(contentView);
24
         addView(contentView);

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/mocks/TestComponentView.java View File

13
 
13
 
14
 	public TestComponentView(final Context context) {
14
 	public TestComponentView(final Context context) {
15
 		super(context);
15
 		super(context);
16
-		topBar = new TopBar(context, this);
16
+		topBar = new TopBar(context, this, null);
17
 
17
 
18
 	}
18
 	}
19
 
19
 

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/OptionsApplyingTest.java View File

140
 
140
 
141
     @Test
141
     @Test
142
     public void appliesDrawUnder() throws Exception {
142
     public void appliesDrawUnder() throws Exception {
143
-        assertThat(uut.getNavigationOptions()).isSameAs(initialNavigationOptions);
143
+        assertThat(uut.getOptions()).isSameAs(initialNavigationOptions);
144
         initialNavigationOptions.topBarOptions.title = "the title";
144
         initialNavigationOptions.topBarOptions.title = "the title";
145
         initialNavigationOptions.topBarOptions.drawBehind = NavigationOptions.BooleanOptions.False;
145
         initialNavigationOptions.topBarOptions.drawBehind = NavigationOptions.BooleanOptions.False;
146
         uut.onViewAppeared();
146
         uut.onViewAppeared();

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/views/TopBarTest.java View File

9
 public class TopBarTest extends BaseTest {
9
 public class TopBarTest extends BaseTest {
10
 	@Test
10
 	@Test
11
 	public void title() throws Exception {
11
 	public void title() throws Exception {
12
-		TopBar topBar = new TopBar(newActivity());
12
+		TopBar topBar = new TopBar(newActivity(), null, null);
13
 		assertThat(topBar.getTitle()).isEmpty();
13
 		assertThat(topBar.getTitle()).isEmpty();
14
 
14
 
15
 		topBar.setTitle("new title");
15
 		topBar.setTitle("new title");