浏览代码

Set elevation 0 when creating TopBar

Apparently TopBar elevation property is animated. Meaning that if 4dp elevation is set, when the
view appears its elevation is animated from 0dp to 4dp.
This resulted in rare race condition where the onAnimationEnd callback was called after
we set the desired elevation value, and an unexpected elevation value was set.
Guy Carmeli 6 年前
父节点
当前提交
05dacbd072

+ 3
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/topbar/TopBarController.java 查看文件

@@ -1,6 +1,7 @@
1 1
 package com.reactnativenavigation.viewcontrollers.topbar;
2 2
 
3 3
 import android.content.Context;
4
+import android.os.Build;
4 5
 import android.support.v4.view.ViewPager;
5 6
 import android.view.View;
6 7
 
@@ -10,12 +11,14 @@ import com.reactnativenavigation.views.topbar.TopBar;
10 11
 
11 12
 
12 13
 public class TopBarController {
14
+    private static final int INITIAL_ELEVATION = 0;
13 15
     private TopBar topBar;
14 16
 
15 17
     public View createView(Context context, StackLayout stackLayout) {
16 18
         if (topBar == null) {
17 19
             topBar = createTopBar(context, stackLayout);
18 20
             topBar.setId(CompatUtils.generateViewId());
21
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) topBar.setElevation(INITIAL_ELEVATION);
19 22
         }
20 23
         return topBar;
21 24
     }

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

@@ -2,7 +2,6 @@ package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4 4
 import android.content.Context;
5
-import android.support.annotation.NonNull;
6 5
 
7 6
 import com.reactnativenavigation.BaseTest;
8 7
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
@@ -20,31 +19,38 @@ import static org.mockito.Mockito.verify;
20 19
 public class TopBarControllerTest extends BaseTest {
21 20
 
22 21
     private TopBarController uut;
22
+    private TitleBar titleBar;
23
+    private TopBar topBar;
24
+    private Activity activity;
23 25
 
24 26
     @Override
25 27
     public void beforeEach() {
26
-        uut = new TopBarController();
27
-    }
28
-
29
-    @Test
30
-    public void clear() {
31
-        final TitleBar[] titleBar = new TitleBar[1];
28
+        activity = newActivity();
32 29
         uut = new TopBarController() {
33
-            @NonNull
34 30
             @Override
35 31
             protected TopBar createTopBar(Context context, StackLayout stackLayout) {
36
-                return new TopBar(context, stackLayout) {
32
+                topBar = spy(new TopBar(context, stackLayout) {
37 33
                     @Override
38 34
                     protected TitleBar createTitleBar(Context context) {
39
-                        titleBar[0] = spy(super.createTitleBar(context));
40
-                        return titleBar[0];
35
+                        titleBar = spy(super.createTitleBar(context));
36
+                        return titleBar;
41 37
                     }
42
-                };
38
+                });
39
+                return topBar;
43 40
             }
44 41
         };
45
-        Activity activity = newActivity();
42
+    }
43
+
44
+    @Test
45
+    public void createView_setElevationToCancelDefaultElevationAnimationWhichMightConflictWithElevationValueFromDefaultOptions() {
46
+        uut.createView(activity, Mockito.mock(StackLayout.class));
47
+        verify(topBar).setElevation(0);
48
+    }
49
+
50
+    @Test
51
+    public void clear() {
46 52
         uut.createView(activity, Mockito.mock(StackLayout.class));
47 53
         uut.clear();
48
-        verify(titleBar[0], times(1)).clear();
54
+        verify(titleBar, times(1)).clear();
49 55
     }
50 56
 }