1/*
2 * Copyright 2018 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 androidx.core.view;
17
18import static org.hamcrest.CoreMatchers.equalTo;
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertNotNull;
21import static org.junit.Assert.assertNull;
22import static org.junit.Assert.assertSame;
23import static org.junit.Assert.assertThat;
24import static org.junit.Assert.assertTrue;
25
26import android.app.Activity;
27import android.support.test.filters.SmallTest;
28import android.support.test.runner.AndroidJUnit4;
29import android.support.v4.BaseInstrumentationTestCase;
30import android.view.Display;
31import android.view.View;
32
33import androidx.core.test.R;
34
35import org.junit.Before;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38
39import java.util.HashSet;
40import java.util.Set;
41
42@RunWith(AndroidJUnit4.class)
43@SmallTest
44public class ViewCompatTest extends BaseInstrumentationTestCase<ViewCompatActivity> {
45
46    private View mView;
47
48    public ViewCompatTest() {
49        super(ViewCompatActivity.class);
50    }
51
52    @Before
53    public void setUp() {
54        final Activity activity = mActivityTestRule.getActivity();
55        mView = activity.findViewById(R.id.view);
56    }
57
58    @Test
59    public void testConstants() {
60        // Compat constants must match core constants since they can be used interchangeably
61        // in various support lib calls.
62        assertEquals("LTR constants", View.LAYOUT_DIRECTION_LTR, ViewCompat.LAYOUT_DIRECTION_LTR);
63        assertEquals("RTL constants", View.LAYOUT_DIRECTION_RTL, ViewCompat.LAYOUT_DIRECTION_RTL);
64    }
65
66    @Test
67    public void testGetDisplay() {
68        final Display display = ViewCompat.getDisplay(mView);
69        assertNotNull(display);
70    }
71
72    @Test
73    public void testGetDisplay_returnsNullForUnattachedView() {
74        final View view = new View(mActivityTestRule.getActivity());
75        final Display display = ViewCompat.getDisplay(view);
76        assertNull(display);
77    }
78
79    @Test
80    public void testTransitionName() {
81        final View view = new View(mActivityTestRule.getActivity());
82        ViewCompat.setTransitionName(view, "abc");
83        assertEquals("abc", ViewCompat.getTransitionName(view));
84    }
85
86    @Test
87    public void testGenerateViewId() {
88        final int requestCount = 100;
89
90        Set<Integer> generatedIds = new HashSet<>();
91        for (int i = 0; i < requestCount; i++) {
92            int generatedId = ViewCompat.generateViewId();
93            assertTrue(isViewIdGenerated(generatedId));
94            generatedIds.add(generatedId);
95        }
96
97        assertThat(generatedIds.size(), equalTo(requestCount));
98    }
99
100    @Test
101    public void testRequireViewByIdFound() {
102        View container = mActivityTestRule.getActivity().findViewById(R.id.container);
103        assertSame(mView, ViewCompat.requireViewById(container, R.id.view));
104    }
105
106    @Test(expected = IllegalArgumentException.class)
107    public void testRequireViewByIdMissing() {
108        View container = mActivityTestRule.getActivity().findViewById(R.id.container);
109
110        // action_bar isn't present inside container
111        ViewCompat.requireViewById(container, R.id.action_bar);
112    }
113
114    @Test(expected = IllegalArgumentException.class)
115    public void testRequireViewByIdInvalid() {
116        View container = mActivityTestRule.getActivity().findViewById(R.id.container);
117
118        // NO_ID is always invalid
119        ViewCompat.requireViewById(container, View.NO_ID);
120    }
121
122    private static boolean isViewIdGenerated(int id) {
123        return (id & 0xFF000000) == 0 && (id & 0x00FFFFFF) != 0;
124    }
125}
126