From d53a3128e8107569636da407d7cb75e458e22779 Mon Sep 17 00:00:00 2001 From: Doris Ling Date: Thu, 27 Jul 2017 11:14:44 -0700 Subject: [PATCH] Fix null pointer exception when updating action bar shadow. Check for valid activity when trying to access the action bar as the activity can become null when in monkey test. Change-Id: I684d873b9eabb9d8461e99bb4385d411a48c0c52 Fix: 64084651 Test: make RunSettingsRoboTests --- .../settings/widget/ActionBarShadowController.java | 2 +- .../widget/ActionBarShadowControllerTest.java | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/com/android/settings/widget/ActionBarShadowController.java b/src/com/android/settings/widget/ActionBarShadowController.java index 0c6b02fedb4..3ffa0ac9e1b 100644 --- a/src/com/android/settings/widget/ActionBarShadowController.java +++ b/src/com/android/settings/widget/ActionBarShadowController.java @@ -121,7 +121,7 @@ public class ActionBarShadowController implements LifecycleObserver, OnStart, On final boolean shouldShowShadow = view.canScrollVertically(-1); if (mAnchorView != null) { mAnchorView.setElevation(shouldShowShadow ? ELEVATION_HIGH : ELEVATION_LOW); - } else { + } else if (mActivity != null) { // activity can become null when running monkey final ActionBar actionBar = mActivity.getActionBar(); if (actionBar != null) { actionBar.setElevation(shouldShowShadow ? ELEVATION_HIGH : ELEVATION_LOW); diff --git a/tests/robotests/src/com/android/settings/widget/ActionBarShadowControllerTest.java b/tests/robotests/src/com/android/settings/widget/ActionBarShadowControllerTest.java index be50d77eb8d..41ba1e4447c 100644 --- a/tests/robotests/src/com/android/settings/widget/ActionBarShadowControllerTest.java +++ b/tests/robotests/src/com/android/settings/widget/ActionBarShadowControllerTest.java @@ -104,4 +104,15 @@ public class ActionBarShadowControllerTest { verify(mRecyclerView, times(2)).addOnScrollListener(any()); } + @Test + public void onScrolled_nullAnchorViewAndActivity_shouldNotCrash() { + final Activity activity = null; + final ActionBarShadowController controller = + ActionBarShadowController.attachToRecyclerView(activity, mLifecycle, mRecyclerView); + + // Scroll + controller.mScrollChangeWatcher.onScrolled(mRecyclerView, 10 /* dx */, 10 /* dy */); + // no crash + } + }