PointerIconCompatTest.java revision ee6c89547f0f0321b66c7c21e1990e7870a45b3c
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 */
16package android.support.v4.view;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertNotNull;
20import static org.junit.Assert.assertNull;
21
22import android.app.Activity;
23import android.graphics.Bitmap;
24import android.os.Build;
25import android.support.compat.test.R;
26import android.support.test.annotation.UiThreadTest;
27import android.support.test.filters.SdkSuppress;
28import android.support.test.filters.SmallTest;
29import android.support.test.runner.AndroidJUnit4;
30import android.support.v4.BaseInstrumentationTestCase;
31import android.view.PointerIcon;
32import android.view.View;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37
38
39@RunWith(AndroidJUnit4.class)
40@SmallTest
41@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
42public class PointerIconCompatTest extends BaseInstrumentationTestCase<ViewCompatActivity> {
43
44    private View mView;
45    private Activity mActivity;
46
47    public PointerIconCompatTest() {
48        super(ViewCompatActivity.class);
49    }
50
51    @Before
52    public void setUp() {
53        mActivity = mActivityTestRule.getActivity();
54        mView = mActivity.findViewById(R.id.view);
55    }
56
57    private void compareSystemIcon(int type, int compatType) {
58        ViewCompat.setPointerIcon(mView, PointerIconCompat.getSystemIcon(mActivity, compatType));
59        assertEquals(PointerIcon.getSystemIcon(mActivity, type), mView.getPointerIcon());
60    }
61
62    @Test
63    @UiThreadTest
64    public void testSystemIcon() {
65        compareSystemIcon(PointerIcon.TYPE_ALIAS, PointerIconCompat.TYPE_ALIAS);
66        compareSystemIcon(PointerIcon.TYPE_ALL_SCROLL, PointerIconCompat.TYPE_ALL_SCROLL);
67        compareSystemIcon(PointerIcon.TYPE_ARROW, PointerIconCompat.TYPE_ARROW);
68        compareSystemIcon(PointerIcon.TYPE_CELL, PointerIconCompat.TYPE_CELL);
69        compareSystemIcon(PointerIcon.TYPE_CONTEXT_MENU, PointerIconCompat.TYPE_CONTEXT_MENU);
70        compareSystemIcon(PointerIcon.TYPE_COPY, PointerIconCompat.TYPE_COPY);
71        compareSystemIcon(PointerIcon.TYPE_CROSSHAIR, PointerIconCompat.TYPE_CROSSHAIR);
72        compareSystemIcon(PointerIcon.TYPE_DEFAULT, PointerIconCompat.TYPE_DEFAULT);
73        compareSystemIcon(PointerIcon.TYPE_GRAB, PointerIconCompat.TYPE_GRAB);
74        compareSystemIcon(PointerIcon.TYPE_GRABBING, PointerIconCompat.TYPE_GRABBING);
75        compareSystemIcon(PointerIcon.TYPE_HAND, PointerIconCompat.TYPE_HAND);
76        compareSystemIcon(PointerIcon.TYPE_HELP, PointerIconCompat.TYPE_HELP);
77        compareSystemIcon(PointerIcon.TYPE_HORIZONTAL_DOUBLE_ARROW,
78                PointerIconCompat.TYPE_HORIZONTAL_DOUBLE_ARROW);
79        compareSystemIcon(PointerIcon.TYPE_NO_DROP, PointerIconCompat.TYPE_NO_DROP);
80        compareSystemIcon(PointerIcon.TYPE_NULL, PointerIconCompat.TYPE_NULL);
81        compareSystemIcon(PointerIcon.TYPE_TEXT, PointerIconCompat.TYPE_TEXT);
82        compareSystemIcon(PointerIcon.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW,
83                PointerIconCompat.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW);
84        compareSystemIcon(PointerIcon.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW,
85                PointerIconCompat.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW);
86        compareSystemIcon(PointerIcon.TYPE_VERTICAL_DOUBLE_ARROW,
87                PointerIconCompat.TYPE_VERTICAL_DOUBLE_ARROW);
88        compareSystemIcon(PointerIcon.TYPE_VERTICAL_TEXT,
89                PointerIconCompat.TYPE_VERTICAL_TEXT);
90        compareSystemIcon(PointerIcon.TYPE_WAIT, PointerIconCompat.TYPE_WAIT);
91        compareSystemIcon(PointerIcon.TYPE_ZOOM_IN, PointerIconCompat.TYPE_ZOOM_IN);
92        compareSystemIcon(PointerIcon.TYPE_ZOOM_OUT, PointerIconCompat.TYPE_ZOOM_OUT);
93    }
94
95    @Test
96    @UiThreadTest
97    public void testNullIcon() {
98        ViewCompat.setPointerIcon(mView, null);
99        assertNull(mView.getPointerIcon());
100    }
101
102    @Test
103    @UiThreadTest
104    public void testBitmapIcon() {
105        Bitmap bitmap = Bitmap.createBitmap(16, 16, Bitmap.Config.ARGB_8888);
106        ViewCompat.setPointerIcon(mView, PointerIconCompat.create(bitmap, 0, 0));
107        assertNotNull(mView.getPointerIcon());
108    }
109
110    @Test
111    @UiThreadTest
112    public void testResourceIcon() {
113        ViewCompat.setPointerIcon(mView,
114                PointerIconCompat.load(mActivity.getResources(), R.drawable.pointer_icon));
115        assertNotNull(mView.getPointerIcon());
116    }
117}
118