1/*
2 * Copyright (C) 2015 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 android.support.v7.app;
18
19import org.junit.Test;
20
21import android.os.Build;
22import android.support.v7.appcompat.test.R;
23import android.support.v7.widget.AppCompatAutoCompleteTextView;
24import android.support.v7.widget.AppCompatButton;
25import android.support.v7.widget.AppCompatCheckBox;
26import android.support.v7.widget.AppCompatEditText;
27import android.support.v7.widget.AppCompatMultiAutoCompleteTextView;
28import android.support.v7.widget.AppCompatRadioButton;
29import android.support.v7.widget.AppCompatRatingBar;
30import android.support.v7.widget.AppCompatSpinner;
31import android.view.LayoutInflater;
32import android.view.View;
33
34public class LayoutInflaterFactoryTestCase extends BaseInstrumentationTestCase<AppCompatActivity> {
35
36    public LayoutInflaterFactoryTestCase() {
37        super(AppCompatActivity.class);
38    }
39
40    @Test
41    public void testAndroidThemeInflation() throws Throwable {
42        if (Build.VERSION.SDK_INT < 10) {
43            // Ignore this test if running on Gingerbread or below
44            return;
45        }
46        runTestOnUiThread(new Runnable() {
47            @Override
48            public void run() {
49                LayoutInflater inflater = LayoutInflater.from(getActivity());
50                View view = inflater.inflate(R.layout.layout_android_theme, null);
51                assertTrue("View has themed Context", view.getContext() != getActivity());
52            }
53        });
54    }
55
56    @Test
57    public void testAppThemeInflation() throws Throwable {
58        if (Build.VERSION.SDK_INT < 10) {
59            // Ignore this test if running on Gingerbread or below
60            return;
61        }
62        runTestOnUiThread(new Runnable() {
63            @Override
64            public void run() {
65                LayoutInflater inflater = LayoutInflater.from(getActivity());
66                View view = inflater.inflate(R.layout.layout_app_theme, null);
67                assertTrue("View has themed Context", view.getContext() != getActivity());
68            }
69        });
70    }
71
72    @Test
73    public void testSpinnerInflation() throws Throwable {
74        testAppCompatWidgetInflation(R.layout.layout_spinner, AppCompatSpinner.class);
75    }
76
77    @Test
78    public void testEditTextInflation() throws Throwable {
79        testAppCompatWidgetInflation(R.layout.layout_edittext, AppCompatEditText.class);
80    }
81
82    @Test
83    public void testButtonInflation() throws Throwable {
84        testAppCompatWidgetInflation(R.layout.layout_button, AppCompatButton.class);
85    }
86
87    @Test
88    public void testRadioButtonInflation() throws Throwable {
89        testAppCompatWidgetInflation(R.layout.layout_radiobutton, AppCompatRadioButton.class);
90    }
91
92    @Test
93    public void testCheckBoxInflation() throws Throwable {
94        testAppCompatWidgetInflation(R.layout.layout_checkbox, AppCompatCheckBox.class);
95    }
96
97    @Test
98    public void testActvInflation() throws Throwable {
99        testAppCompatWidgetInflation(R.layout.layout_actv, AppCompatAutoCompleteTextView.class);
100    }
101
102    @Test
103    public void testMactvInflation() throws Throwable {
104        testAppCompatWidgetInflation(R.layout.layout_mactv,
105                AppCompatMultiAutoCompleteTextView.class);
106    }
107
108    @Test
109    public void testRatingBarInflation() throws Throwable {
110        testAppCompatWidgetInflation(R.layout.layout_ratingbar, AppCompatRatingBar.class);
111    }
112
113    private void testAppCompatWidgetInflation(final int layout, final Class<?> expectedClass)
114            throws Throwable {
115        runTestOnUiThread(new Runnable() {
116            @Override
117            public void run() {
118                LayoutInflater inflater = LayoutInflater.from(getActivity());
119                View view = inflater.inflate(layout, null);
120                assertEquals("View is " + expectedClass.getSimpleName(), expectedClass,
121                        view.getClass());
122            }
123        });
124    }
125}
126