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 android.content.Context; 20 import android.database.Cursor; 21 import android.graphics.Rect; 22 import android.os.SystemClock; 23 import android.test.AndroidTestCase; 24 import android.test.suitebuilder.annotation.SmallTest; 25 import android.view.KeyEvent; 26 import android.view.LayoutInflater; 27 import android.view.MotionEvent; 28 import android.view.MotionEvent.PointerCoords; 29 import android.view.MotionEvent.PointerProperties; 30 31 import com.android.documentsui.R; 32 import com.android.documentsui.State; 33 34 @SmallTest 35 public class DocumentHolderTest extends AndroidTestCase { 36 37 DocumentHolder mHolder; 38 TestListener mListener; 39 setUp()40 public void setUp() throws Exception { 41 Context context = getContext(); 42 LayoutInflater inflater = LayoutInflater.from(context); 43 mHolder = new DocumentHolder(getContext(), inflater.inflate(R.layout.item_doc_list, null)) { 44 @Override 45 public void bind(Cursor cursor, String modelId, State state) {} 46 }; 47 48 mListener = new TestListener(); 49 mHolder.addEventListener(mListener); 50 51 mHolder.itemView.requestLayout(); 52 mHolder.itemView.invalidate(); 53 } 54 testClickActivates()55 public void testClickActivates() { 56 click(); 57 mListener.assertSelected(); 58 } 59 testTapActivates()60 public void testTapActivates() { 61 tap(); 62 mListener.assertActivated(); 63 } 64 click()65 public void click() { 66 mHolder.onSingleTapUp(createEvent(MotionEvent.TOOL_TYPE_MOUSE)); 67 } 68 tap()69 public void tap() { 70 mHolder.onSingleTapUp(createEvent(MotionEvent.TOOL_TYPE_FINGER)); 71 } 72 createEvent(int tooltype)73 public MotionEvent createEvent(int tooltype) { 74 long time = SystemClock.uptimeMillis(); 75 76 PointerProperties properties[] = new PointerProperties[] { 77 new PointerProperties() 78 }; 79 properties[0].toolType = tooltype; 80 81 PointerCoords coords[] = new PointerCoords[] { 82 new PointerCoords() 83 }; 84 85 Rect rect = new Rect(); 86 mHolder.itemView.getHitRect(rect); 87 coords[0].x = rect.left; 88 coords[0].y = rect.top; 89 90 return MotionEvent.obtain( 91 time, // down time 92 time, // event time 93 MotionEvent.ACTION_UP, // action 94 1, // pointer count 95 properties, // pointer properties 96 coords, // pointer coords 97 0, // metastate 98 0, // button state 99 0, // xprecision 100 0, // yprecision 101 0, // deviceid 102 0, // edgeflags 103 0, // source 104 0 // flags 105 ); 106 } 107 108 private class TestListener implements DocumentHolder.EventListener { 109 private boolean mActivated = false; 110 private boolean mSelected = false; 111 assertActivated()112 public void assertActivated() { 113 assertTrue(mActivated); 114 assertFalse(mSelected); 115 } 116 assertSelected()117 public void assertSelected() { 118 assertTrue(mSelected); 119 assertFalse(mActivated); 120 } 121 122 @Override onActivate(DocumentHolder doc)123 public boolean onActivate(DocumentHolder doc) { 124 mActivated = true; 125 return true; 126 } 127 128 @Override onSelect(DocumentHolder doc)129 public boolean onSelect(DocumentHolder doc) { 130 mSelected = true; 131 return true; 132 } 133 134 @Override onKey(DocumentHolder doc, int keyCode, KeyEvent event)135 public boolean onKey(DocumentHolder doc, int keyCode, KeyEvent event) { 136 return false; 137 } 138 139 } 140 } 141