summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Geoffrey Pitsch <gpitsch@google.com> 2017-10-26 10:25:36 -0400
committer Geoffrey Pitsch <gpitsch@google.com> 2017-10-27 09:24:16 -0400
commit028b263084b9ab70e15f8f2ed1df9545032b7dd0 (patch)
tree6fa241d4d14d646a540cd5f25d692bd4391073c2
parent9ea7f88e7ebb7d8b5b2428ea17919868dc359f00 (diff)
Fix crash when attempting to focus when no items are visible.
Judging from stack, monkey test is somehow getting into a state where no items are currently visible. Safeguard this edge case from crashing. Test: docsui unittest Change-Id: I10dee1dfd8373012f4a1bedcbda851653c12f5ee Fixes: 64589752
-rw-r--r--src/com/android/documentsui/FocusManager.java4
-rw-r--r--tests/common/com/android/documentsui/testing/TestGridLayoutManager.java42
-rw-r--r--tests/common/com/android/documentsui/testing/TestRecyclerView.java16
-rw-r--r--tests/unit/com/android/documentsui/FocusManagerTest.java11
4 files changed, 71 insertions, 2 deletions
diff --git a/src/com/android/documentsui/FocusManager.java b/src/com/android/documentsui/FocusManager.java
index 8f3795cad..097516e56 100644
--- a/src/com/android/documentsui/FocusManager.java
+++ b/src/com/android/documentsui/FocusManager.java
@@ -153,6 +153,10 @@ public final class FocusManager implements FocusHandler {
final int focusPos = (mScope.lastFocusPosition != RecyclerView.NO_POSITION)
? mScope.lastFocusPosition
: mScope.layout.findFirstVisibleItemPosition();
+ if (focusPos == RecyclerView.NO_POSITION) {
+ return false;
+ }
+
focusItem(focusPos);
return true;
}
diff --git a/tests/common/com/android/documentsui/testing/TestGridLayoutManager.java b/tests/common/com/android/documentsui/testing/TestGridLayoutManager.java
new file mode 100644
index 000000000..f883e5e58
--- /dev/null
+++ b/tests/common/com/android/documentsui/testing/TestGridLayoutManager.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.documentsui.testing;
+
+import android.content.Context;
+import android.support.v7.widget.GridLayoutManager;
+
+import org.mockito.Mockito;
+
+public class TestGridLayoutManager extends GridLayoutManager {
+
+ private int mFirstVisibleItemPosition;
+
+ public static TestGridLayoutManager create() {
+ final TestGridLayoutManager manager = Mockito.mock(TestGridLayoutManager.class,
+ Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
+ return manager;
+ }
+
+ @Override
+ public int findFirstVisibleItemPosition() { return mFirstVisibleItemPosition; }
+
+ public void setFirstVisibleItemPosition(int position) { mFirstVisibleItemPosition = position; }
+
+ private TestGridLayoutManager(Context context, int spanCount) {
+ super(context, spanCount);
+ }
+}
diff --git a/tests/common/com/android/documentsui/testing/TestRecyclerView.java b/tests/common/com/android/documentsui/testing/TestRecyclerView.java
index 8759a9a21..a1f8e7f26 100644
--- a/tests/common/com/android/documentsui/testing/TestRecyclerView.java
+++ b/tests/common/com/android/documentsui/testing/TestRecyclerView.java
@@ -17,6 +17,7 @@
package com.android.documentsui.testing;
import android.content.Context;
+import android.support.test.InstrumentationRegistry;
import android.support.v7.widget.RecyclerView;
import android.view.View;
@@ -32,6 +33,7 @@ public class TestRecyclerView extends RecyclerView {
private List<RecyclerView.ViewHolder> holders = new ArrayList<>();
private TestDocumentsAdapter adapter;
+ private RecyclerView.LayoutManager mLayoutManager;
public TestRecyclerView(Context context) {
super(context);
@@ -55,6 +57,16 @@ public class TestRecyclerView extends RecyclerView {
return adapter;
}
+ @Override
+ public void setLayoutManager(LayoutManager manager) {
+ mLayoutManager = manager;
+ }
+
+ @Override
+ public RecyclerView.LayoutManager getLayoutManager() {
+ return mLayoutManager;
+ }
+
public void setItems(List<String> modelIds) {
holders = new ArrayList<>();
for (String modelId: modelIds) {
@@ -64,8 +76,8 @@ public class TestRecyclerView extends RecyclerView {
}
public static TestRecyclerView create(List<String> modelIds) {
- final TestRecyclerView view = Mockito.mock(TestRecyclerView.class,
- Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
+ final TestRecyclerView view =
+ new TestRecyclerView(InstrumentationRegistry.getTargetContext());
view.holders = new ArrayList<>();
for (String modelId: modelIds) {
view.holders.add(new TestViewHolder(Views.createTestView()));
diff --git a/tests/unit/com/android/documentsui/FocusManagerTest.java b/tests/unit/com/android/documentsui/FocusManagerTest.java
index cd71db6cd..b386f82b3 100644
--- a/tests/unit/com/android/documentsui/FocusManagerTest.java
+++ b/tests/unit/com/android/documentsui/FocusManagerTest.java
@@ -16,6 +16,7 @@
package com.android.documentsui;
+import android.support.v7.widget.RecyclerView;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
@@ -25,6 +26,7 @@ import com.android.documentsui.selection.SelectionHelper;
import com.android.documentsui.testing.TestModel;
import com.android.documentsui.testing.SelectionHelpers;
import com.android.documentsui.testing.TestFeatures;
+import com.android.documentsui.testing.TestGridLayoutManager;
import com.android.documentsui.testing.TestRecyclerView;
import java.util.ArrayList;
@@ -39,12 +41,16 @@ public class FocusManagerTest extends AndroidTestCase {
private FocusManager mManager;
private TestRecyclerView mView;
+ private TestGridLayoutManager mTestGridLayoutManager;
private SelectionHelper mSelectionMgr;
private TestFeatures mFeatures;
@Override
public void setUp() throws Exception {
mView = TestRecyclerView.create(ITEMS);
+ mTestGridLayoutManager = TestGridLayoutManager.create();
+ mView.setLayoutManager(mTestGridLayoutManager);
+
mSelectionMgr = SelectionHelpers.createTestInstance(ITEMS);
mFeatures = new TestFeatures();
mManager = new FocusManager(mFeatures, mSelectionMgr, null, null, 0)
@@ -73,6 +79,11 @@ public class FocusManagerTest extends AndroidTestCase {
assertFalse(mManager.focusDirectoryList());
}
+ public void testFocusDirectoryList_noVisibleItems() {
+ mTestGridLayoutManager.setFirstVisibleItemPosition(RecyclerView.NO_POSITION);
+ assertFalse(mManager.focusDirectoryList());
+ }
+
public void testFocusDirectoryList_hasSelection() {
mSelectionMgr.select("0");
assertFalse(mManager.focusDirectoryList());