Browse Source

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 years ago
parent
commit
05dacbd072

+ 3
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/topbar/TopBarController.java View File

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

+ 20
- 14
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/TopBarControllerTest.java View File

2
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
 import android.content.Context;
4
 import android.content.Context;
5
-import android.support.annotation.NonNull;
6
 
5
 
7
 import com.reactnativenavigation.BaseTest;
6
 import com.reactnativenavigation.BaseTest;
8
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
7
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
20
 public class TopBarControllerTest extends BaseTest {
19
 public class TopBarControllerTest extends BaseTest {
21
 
20
 
22
     private TopBarController uut;
21
     private TopBarController uut;
22
+    private TitleBar titleBar;
23
+    private TopBar topBar;
24
+    private Activity activity;
23
 
25
 
24
     @Override
26
     @Override
25
     public void beforeEach() {
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
         uut = new TopBarController() {
29
         uut = new TopBarController() {
33
-            @NonNull
34
             @Override
30
             @Override
35
             protected TopBar createTopBar(Context context, StackLayout stackLayout) {
31
             protected TopBar createTopBar(Context context, StackLayout stackLayout) {
36
-                return new TopBar(context, stackLayout) {
32
+                topBar = spy(new TopBar(context, stackLayout) {
37
                     @Override
33
                     @Override
38
                     protected TitleBar createTitleBar(Context context) {
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
         uut.createView(activity, Mockito.mock(StackLayout.class));
52
         uut.createView(activity, Mockito.mock(StackLayout.class));
47
         uut.clear();
53
         uut.clear();
48
-        verify(titleBar[0], times(1)).clear();
54
+        verify(titleBar, times(1)).clear();
49
     }
55
     }
50
 }
56
 }