OSDN Git Service

Fix null pointer exception when updating action bar shadow.
authorDoris Ling <dling@google.com>
Thu, 27 Jul 2017 18:14:44 +0000 (11:14 -0700)
committerDoris Ling <dling@google.com>
Thu, 27 Jul 2017 18:14:44 +0000 (11:14 -0700)
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

src/com/android/settings/widget/ActionBarShadowController.java
tests/robotests/src/com/android/settings/widget/ActionBarShadowControllerTest.java

index 0c6b02f..3ffa0ac 100644 (file)
@@ -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);
index be50d77..41ba1e4 100644 (file)
@@ -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
+    }
+
 }