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
17package com.android.documentsui.dirlist;
18
19import android.content.Context;
20import android.database.Cursor;
21import android.graphics.Rect;
22import android.os.SystemClock;
23import android.test.AndroidTestCase;
24import android.test.suitebuilder.annotation.SmallTest;
25import android.view.KeyEvent;
26import android.view.LayoutInflater;
27import android.view.MotionEvent;
28import android.view.MotionEvent.PointerCoords;
29import android.view.MotionEvent.PointerProperties;
30
31import com.android.documentsui.R;
32import com.android.documentsui.State;
33
34@SmallTest
35public class DocumentHolderTest extends AndroidTestCase {
36
37    DocumentHolder mHolder;
38    TestListener mListener;
39
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
55    public void testClickActivates() {
56        click();
57        mListener.assertSelected();
58    }
59
60    public void testTapActivates() {
61        tap();
62        mListener.assertActivated();
63    }
64
65    public void click() {
66        mHolder.onSingleTapUp(createEvent(MotionEvent.TOOL_TYPE_MOUSE));
67    }
68
69    public void tap() {
70        mHolder.onSingleTapUp(createEvent(MotionEvent.TOOL_TYPE_FINGER));
71    }
72
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
112        public void assertActivated() {
113            assertTrue(mActivated);
114            assertFalse(mSelected);
115        }
116
117        public void assertSelected() {
118            assertTrue(mSelected);
119            assertFalse(mActivated);
120        }
121
122        @Override
123        public boolean onActivate(DocumentHolder doc) {
124            mActivated = true;
125            return true;
126        }
127
128        @Override
129        public boolean onSelect(DocumentHolder doc) {
130            mSelected = true;
131            return true;
132        }
133
134        @Override
135        public boolean onKey(DocumentHolder doc, int keyCode, KeyEvent event) {
136            return false;
137        }
138
139    }
140}
141