Merge "Fix broken unit test done by ag/1729776, and added unit test." into nyc-andromeda-dev
diff --git a/src/com/android/documentsui/dirlist/UserInputHandler.java b/src/com/android/documentsui/dirlist/UserInputHandler.java
index 6a05624..8da25cf 100644
--- a/src/com/android/documentsui/dirlist/UserInputHandler.java
+++ b/src/com/android/documentsui/dirlist/UserInputHandler.java
@@ -429,7 +429,7 @@
         // TODO: Refactor FocusManager to depend only on DocumentDetails so we can eliminate
         // difficult to test dependency on DocumentHolder.
 
-        boolean onKey(DocumentHolder doc, int keyCode, KeyEvent event) {
+        boolean onKey(@Nullable DocumentHolder doc, int keyCode, KeyEvent event) {
             // Only handle key-down events. This is simpler, consistent with most other UIs, and
             // enables the handling of repeated key events from holding down a key.
             if (event.getAction() != KeyEvent.ACTION_DOWN) {
@@ -441,12 +441,14 @@
                 return false;
             }
 
-            int itemType = doc.getItemViewType();
             // Ignore events sent to Addon Holders.
-            if (itemType == DocumentsAdapter.ITEM_TYPE_HEADER_MESSAGE
-                    || itemType == DocumentsAdapter.ITEM_TYPE_INFLATED_MESSAGE
-                    || itemType == DocumentsAdapter.ITEM_TYPE_SECTION_BREAK) {
-                return false;
+            if (doc != null) {
+                int itemType = doc.getItemViewType();
+                if (itemType == DocumentsAdapter.ITEM_TYPE_HEADER_MESSAGE
+                        || itemType == DocumentsAdapter.ITEM_TYPE_INFLATED_MESSAGE
+                        || itemType == DocumentsAdapter.ITEM_TYPE_SECTION_BREAK) {
+                    return false;
+                }
             }
 
             if (mFocusHandler.handleKey(doc, keyCode, event)) {
diff --git a/tests/common/com/android/documentsui/bots/DirectoryListBot.java b/tests/common/com/android/documentsui/bots/DirectoryListBot.java
index 0207557..4e9542e 100644
--- a/tests/common/com/android/documentsui/bots/DirectoryListBot.java
+++ b/tests/common/com/android/documentsui/bots/DirectoryListBot.java
@@ -108,7 +108,7 @@
         assertTrue(messageTextView.exists());
 
         String msg = String.valueOf(message);
-        assertEquals(String.format(msg, "TEST_ROOT_0"), messageTextView.getText());
+        assertEquals(msg, messageTextView.getText());
 
     }
 
diff --git a/tests/functional/com/android/documentsui/FilesActivityDefaultsUiTest.java b/tests/functional/com/android/documentsui/FilesActivityDefaultsUiTest.java
index aaace3e..f16ac29 100644
--- a/tests/functional/com/android/documentsui/FilesActivityDefaultsUiTest.java
+++ b/tests/functional/com/android/documentsui/FilesActivityDefaultsUiTest.java
@@ -19,9 +19,9 @@
 import static com.android.documentsui.StubProvider.ROOT_0_ID;
 import static com.android.documentsui.StubProvider.ROOT_1_ID;
 
-import android.content.Intent;
-import android.provider.DocumentsContract;
+import android.os.RemoteException;
 import android.support.test.filters.LargeTest;
+import android.support.v7.recyclerview.R;
 
 import com.android.documentsui.base.RootInfo;
 import com.android.documentsui.base.Shared;
@@ -35,6 +35,11 @@
     }
 
     @Override
+    protected void initTestFiles() throws RemoteException {
+        // Overriding to init with no items in test roots
+    }
+
+    @Override
     protected RootInfo getInitialRoot() {
         return null;  // test the default, unaffected state of the app.
     }
@@ -50,6 +55,18 @@
         }
     }
 
+    public void testNavigate_FromEmptyDirectory() throws Exception {
+        device.waitForIdle();
+
+        bots.roots.openRoot(rootDir0.title);
+
+        String msg = String.valueOf(context.getString(R.string.empty));
+        bots.directory.assertMessageTextView(msg);
+
+        // Check to make sure back button is properly handled by non-Doc type DocHolders
+        device.pressBack();
+    }
+
     public void testDefaultRoots() throws Exception {
         device.waitForIdle();