1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.documentsui.dirlist; 18 19 import static org.junit.Assert.assertFalse; 20 21 import android.support.test.filters.SmallTest; 22 import android.support.test.runner.AndroidJUnit4; 23 import android.support.v7.widget.RecyclerView; 24 import android.view.MotionEvent; 25 26 import com.android.documentsui.base.Events.InputEvent; 27 import com.android.documentsui.selection.SelectionManager; 28 import com.android.documentsui.selection.SelectionProbe; 29 import com.android.documentsui.testing.SelectionManagers; 30 import com.android.documentsui.testing.TestActionHandler; 31 import com.android.documentsui.testing.TestEvent; 32 import com.android.documentsui.testing.TestEvent.Builder; 33 import com.android.documentsui.testing.TestEventHandler; 34 import com.android.documentsui.testing.TestPredicate; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 import java.util.List; 41 42 @RunWith(AndroidJUnit4.class) 43 @SmallTest 44 public final class UserInputHandler_TouchTest { 45 46 private static final List<String> ITEMS = TestData.create(100); 47 48 private UserInputHandler<TestEvent> mInputHandler; 49 private TestActionHandler mActionHandler; 50 51 private SelectionProbe mSelection; 52 private TestPredicate<DocumentDetails> mCanSelect; 53 private TestEventHandler<InputEvent> mRightClickHandler; 54 private TestEventHandler<InputEvent> mDragAndDropHandler; 55 private TestEventHandler<InputEvent> mGestureSelectHandler; 56 private TestEventHandler<Void> mPerformHapticFeedback; 57 58 private Builder mEvent; 59 60 @Before 61 public void setUp() { 62 SelectionManager selectionMgr = SelectionManagers.createTestInstance(ITEMS); 63 64 mActionHandler = new TestActionHandler(); 65 66 mSelection = new SelectionProbe(selectionMgr); 67 mCanSelect = new TestPredicate<>(); 68 mRightClickHandler = new TestEventHandler<>(); 69 mDragAndDropHandler = new TestEventHandler<>(); 70 mGestureSelectHandler = new TestEventHandler<>(); 71 mPerformHapticFeedback = new TestEventHandler<>(); 72 73 mInputHandler = new UserInputHandler<>( 74 mActionHandler, 75 new TestFocusHandler(), 76 selectionMgr, 77 (MotionEvent event) -> { 78 throw new UnsupportedOperationException("Not exercised in tests."); 79 }, 80 mCanSelect, 81 mRightClickHandler::accept, 82 mDragAndDropHandler::accept, 83 mGestureSelectHandler::accept, 84 () -> mPerformHapticFeedback.accept(null)); 85 86 mEvent = TestEvent.builder(); 87 } 88 89 @Test 90 public void testTap_ActivatesWhenNoExistingSelection() { 91 mInputHandler.onSingleTapUp(mEvent.at(11).build()); 92 mActionHandler.open.assertLastArgument(mEvent.build().getDocumentDetails()); 93 } 94 95 @Test 96 public void testScroll_shouldNotBeTrapped() { 97 assertFalse(mInputHandler.onScroll(mEvent.build())); 98 } 99 100 @Test 101 public void testLongPress_StartsSelectionMode() { 102 mCanSelect.nextReturn(true); 103 TestEvent event = mEvent.at(7).build(); 104 mInputHandler.onLongPress(event); 105 mSelection.assertSelection(7); 106 mPerformHapticFeedback.assertCalled(); 107 } 108 109 @Test 110 public void testLongPress_SecondPressAddsSelection() { 111 mCanSelect.nextReturn(true); 112 TestEvent event1 = mEvent.at(7).build(); 113 TestEvent event2 = mEvent.at(99).build(); 114 TestEvent event3 = mEvent.at(13).build(); 115 mInputHandler.onLongPress(event1); 116 mPerformHapticFeedback.assertCalled(); 117 mPerformHapticFeedback.reset(); 118 mInputHandler.onLongPress(event2); 119 mPerformHapticFeedback.assertCalled(); 120 mPerformHapticFeedback.reset(); 121 mInputHandler.onLongPress(event3); 122 mPerformHapticFeedback.assertCalled(); 123 mPerformHapticFeedback.reset(); 124 mSelection.assertSelection(7, 13, 99); 125 } 126 127 @Test 128 public void testTap_UnselectsSelectedItem() { 129 mInputHandler.onLongPress(mEvent.at(7).build()); 130 mInputHandler.onSingleTapUp(mEvent.at(7).build()); 131 mSelection.assertNoSelection(); 132 } 133 134 @Test 135 public void testTapOff_ClearsSelection() { 136 mInputHandler.onLongPress(mEvent.at(7).build()); 137 mInputHandler.onSingleTapUp(mEvent.at(11).build()); 138 mInputHandler.onSingleTapUp(mEvent.at(RecyclerView.NO_POSITION).build()); 139 mSelection.assertNoSelection(); 140 } 141 } 142