Merge "Adding ActivityInputHandler and tests." into nyc-andromeda-dev
diff --git a/src/com/android/documentsui/files/ActivityInputHandler.java b/src/com/android/documentsui/files/ActivityInputHandler.java
new file mode 100644
index 0000000..a7b0e2c
--- /dev/null
+++ b/src/com/android/documentsui/files/ActivityInputHandler.java
@@ -0,0 +1,49 @@
+/*
+ * 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.files;
+
+import android.view.KeyEvent;
+
+import com.android.documentsui.selection.SelectionManager;
+import com.android.documentsui.ActionHandler;
+
+/**
+ * Used by {@link FilesActivity} to manage global keyboard shortcuts tied to file actions
+ */
+final class ActivityInputHandler {
+
+    private final SelectionManager mSelectionMgr;
+    private final ActionHandler mActions;
+
+    ActivityInputHandler(SelectionManager selectionMgr, ActionHandler actionHandler) {
+        mSelectionMgr = selectionMgr;
+        mActions = actionHandler;
+    }
+
+    boolean onKeyDown(int keyCode, KeyEvent event) {
+        if ((keyCode == KeyEvent.KEYCODE_DEL && event.isAltPressed())
+                || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) {
+            if (mSelectionMgr.hasSelection()) {
+                mActions.deleteSelectedDocuments();
+                return true;
+            } else {
+                return false;
+            }
+        }
+        return false;
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/documentsui/files/FilesActivity.java b/src/com/android/documentsui/files/FilesActivity.java
index 30bced7..c9fdc1f 100644
--- a/src/com/android/documentsui/files/FilesActivity.java
+++ b/src/com/android/documentsui/files/FilesActivity.java
@@ -78,6 +78,7 @@
     private DialogController mDialogs;
     private DocumentClipper mClipper;
     private ActionModeController mActionModeController;
+    private ActivityInputHandler mActivityInputHandler;
 
     public FilesActivity() {
         super(R.layout.files_activity, TAG);
@@ -121,6 +122,8 @@
                 mClipper,
                 DocumentsApplication.getClipStore(this));
 
+        mActivityInputHandler = new ActivityInputHandler(mSelectionMgr, mActions);
+
         RootsFragment.show(getFragmentManager(), null);
 
         final Intent intent = getIntent();
@@ -288,16 +291,8 @@
     @CallSuper
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
-        if ((keyCode == KeyEvent.KEYCODE_DEL && event.isAltPressed())
-                || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) {
-            if (mSelectionMgr.hasSelection()) {
-                mActions.deleteSelectedDocuments();
-                return true;
-            } else {
-                return false;
-            }
-        }
-        return super.onKeyDown(keyCode, event);
+        return mActivityInputHandler.onKeyDown(keyCode, event) ? true
+                : super.onKeyDown(keyCode, event);
     }
 
     @Override
diff --git a/tests/common/com/android/documentsui/testing/TestActionHandler.java b/tests/common/com/android/documentsui/testing/TestActionHandler.java
index fd199ce..69f716c 100644
--- a/tests/common/com/android/documentsui/testing/TestActionHandler.java
+++ b/tests/common/com/android/documentsui/testing/TestActionHandler.java
@@ -28,6 +28,7 @@
     public final TestEventHandler<DocumentDetails> open = new TestEventHandler<>();
     public final TestEventHandler<DocumentDetails> view = new TestEventHandler<>();
     public final TestEventHandler<DocumentDetails> preview = new TestEventHandler<>();
+    public boolean mDeleteHappened;
 
     public TestActionHandler() {
         this(TestEnv.create());
@@ -60,6 +61,11 @@
     }
 
     @Override
+    public void deleteSelectedDocuments() {
+        mDeleteHappened = true;
+    }
+
+    @Override
     public void openRoot(RootInfo root) {
         throw new UnsupportedOperationException();
     }
diff --git a/tests/unit/com/android/documentsui/dirlist/DragStartListenerTest.java b/tests/unit/com/android/documentsui/dirlist/DragStartListenerTest.java
index f94e10f..f2e044b 100644
--- a/tests/unit/com/android/documentsui/dirlist/DragStartListenerTest.java
+++ b/tests/unit/com/android/documentsui/dirlist/DragStartListenerTest.java
@@ -92,11 +92,6 @@
                 .primary();
     }
 
-    @Override
-    protected void tearDown() throws Exception {
-        mMultiSelectManager.clearSelection();
-    }
-
     public void testDragStarted_OnMouseMove() {
         assertTrue(mListener.onMouseDragEvent(mEvent.build()));
         assertTrue(mDragStarted);
diff --git a/tests/unit/com/android/documentsui/files/ActivityInputHandlerTest.java b/tests/unit/com/android/documentsui/files/ActivityInputHandlerTest.java
new file mode 100644
index 0000000..37a696f
--- /dev/null
+++ b/tests/unit/com/android/documentsui/files/ActivityInputHandlerTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.files;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import android.support.test.filters.MediumTest;
+import android.support.test.runner.AndroidJUnit4;
+import android.view.KeyEvent;
+import android.view.MotionEvent;
+
+import com.android.documentsui.dirlist.TestData;
+import com.android.documentsui.selection.SelectionManager;
+import com.android.documentsui.selection.SelectionProbe;
+import com.android.documentsui.testing.SelectionManagers;
+import com.android.documentsui.testing.TestActionHandler;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.List;
+
+@RunWith(AndroidJUnit4.class)
+@MediumTest
+public class ActivityInputHandlerTest {
+
+    private static final List<String> ITEMS = TestData.create(100);
+
+    private SelectionProbe mSelection;
+    private TestActionHandler mActionHandler;
+    private ActivityInputHandler mActivityInputHandler;
+
+    @Before
+    public void setUp() {
+        SelectionManager selectionMgr = SelectionManagers.createTestInstance(ITEMS);
+        mSelection = new SelectionProbe(selectionMgr);
+        mActionHandler = new TestActionHandler();
+        mActivityInputHandler = new ActivityInputHandler(selectionMgr, mActionHandler);
+    }
+
+    @Test
+    public void testDelete_noSelection() {
+        KeyEvent event = new KeyEvent(0, 0, MotionEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL, 0,
+                KeyEvent.META_ALT_ON);
+        assertFalse(mActivityInputHandler.onKeyDown(event.getKeyCode(), event));
+        assertFalse(mActionHandler.mDeleteHappened);
+    }
+
+    @Test
+    public void testDelete_hasSelection() {
+        mSelection.select(1);
+        KeyEvent event = new KeyEvent(0, 0, MotionEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL, 0,
+                KeyEvent.META_ALT_ON);
+        assertTrue(mActivityInputHandler.onKeyDown(event.getKeyCode(), event));
+        assertTrue(mActionHandler.mDeleteHappened);
+    }
+}