Skip to content

Commit

Permalink
Set elevation 0 when creating TopBar
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
guyca committed Jan 7, 2019
1 parent c746621 commit 05dacbd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.reactnativenavigation.viewcontrollers.topbar;

import android.content.Context;
import android.os.Build;
import android.support.v4.view.ViewPager;
import android.view.View;

Expand All @@ -10,12 +11,14 @@


public class TopBarController {
private static final int INITIAL_ELEVATION = 0;
private TopBar topBar;

public View createView(Context context, StackLayout stackLayout) {
if (topBar == null) {
topBar = createTopBar(context, stackLayout);
topBar.setId(CompatUtils.generateViewId());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) topBar.setElevation(INITIAL_ELEVATION);
}
return topBar;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;

import com.reactnativenavigation.BaseTest;
import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
Expand All @@ -20,31 +19,38 @@
public class TopBarControllerTest extends BaseTest {

private TopBarController uut;
private TitleBar titleBar;
private TopBar topBar;
private Activity activity;

@Override
public void beforeEach() {
uut = new TopBarController();
}

@Test
public void clear() {
final TitleBar[] titleBar = new TitleBar[1];
activity = newActivity();
uut = new TopBarController() {
@NonNull
@Override
protected TopBar createTopBar(Context context, StackLayout stackLayout) {
return new TopBar(context, stackLayout) {
topBar = spy(new TopBar(context, stackLayout) {
@Override
protected TitleBar createTitleBar(Context context) {
titleBar[0] = spy(super.createTitleBar(context));
return titleBar[0];
titleBar = spy(super.createTitleBar(context));
return titleBar;
}
};
});
return topBar;
}
};
Activity activity = newActivity();
}

@Test
public void createView_setElevationToCancelDefaultElevationAnimationWhichMightConflictWithElevationValueFromDefaultOptions() {
uut.createView(activity, Mockito.mock(StackLayout.class));
verify(topBar).setElevation(0);
}

@Test
public void clear() {
uut.createView(activity, Mockito.mock(StackLayout.class));
uut.clear();
verify(titleBar[0], times(1)).clear();
verify(titleBar, times(1)).clear();
}
}

0 comments on commit 05dacbd

Please sign in to comment.