Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2017 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 package androidx.appcompat.app;
     17 
     18 import static org.junit.Assert.assertEquals;
     19 
     20 import android.support.test.filters.SmallTest;
     21 import android.support.test.rule.ActivityTestRule;
     22 import android.view.ViewGroup;
     23 import android.widget.ScrollView;
     24 
     25 import androidx.appcompat.app.inflater.CustomViewInflater;
     26 import androidx.appcompat.test.R;
     27 import androidx.appcompat.widget.AppCompatButton;
     28 import androidx.appcompat.widget.AppCompatRadioButton;
     29 import androidx.appcompat.widget.AppCompatSpinner;
     30 import androidx.appcompat.widget.AppCompatTextView;
     31 
     32 import org.junit.Before;
     33 import org.junit.Rule;
     34 import org.junit.Test;
     35 
     36 /**
     37  * Testing the custom view inflation where app-specific views are used for specific
     38  * views in layouts specified in XML.
     39  */
     40 @SmallTest
     41 public class AppCompatInflaterCustomTest {
     42     private ViewGroup mContainer;
     43     private AppCompatInflaterCustomActivity mActivity;
     44 
     45     @Rule
     46     public final ActivityTestRule<AppCompatInflaterCustomActivity> mActivityTestRule =
     47             new ActivityTestRule<>(AppCompatInflaterCustomActivity.class);
     48 
     49     @Before
     50     public void setUp() {
     51         mActivity = mActivityTestRule.getActivity();
     52         mContainer = mActivity.findViewById(R.id.container);
     53     }
     54 
     55     @Test
     56     public void testViewClasses() {
     57         // View defined as <Button> should be inflated as CustomButton
     58         assertEquals(CustomViewInflater.CustomButton.class,
     59                 mContainer.findViewById(R.id.button).getClass());
     60 
     61         // View defined as <AppCompatButton> should be inflated as AppCompatButton
     62         assertEquals(AppCompatButton.class, mContainer.findViewById(R.id.ac_button).getClass());
     63 
     64         // View defined as <TextView> should be inflated as CustomTextView
     65         assertEquals(CustomViewInflater.CustomTextView.class,
     66                 mContainer.findViewById(R.id.textview).getClass());
     67 
     68         // View defined as <AppCompatTextView> should be inflated as AppCompatTextView
     69         assertEquals(AppCompatTextView.class, mContainer.findViewById(R.id.ac_textview).getClass());
     70 
     71         // View defined as <RadioButton> should be inflated as AppCompatRadioButton
     72         assertEquals(AppCompatRadioButton.class,
     73                 mContainer.findViewById(R.id.radiobutton).getClass());
     74 
     75         // View defined as <ImageButton> should be inflated as CustomImageButton
     76         assertEquals(CustomViewInflater.CustomImageButton.class,
     77                 mContainer.findViewById(R.id.imagebutton).getClass());
     78 
     79         // View defined as <Spinner> should be inflated as AppCompatSpinner
     80         assertEquals(AppCompatSpinner.class, mContainer.findViewById(R.id.spinner).getClass());
     81 
     82         // View defined as <ToggleButton> should be inflated as CustomToggleButton
     83         assertEquals(CustomViewInflater.CustomToggleButton.class,
     84                 mContainer.findViewById(R.id.togglebutton).getClass());
     85 
     86         // View defined as <ScrollView> should be inflated as ScrollView
     87         assertEquals(ScrollView.class,
     88                 mContainer.findViewById(R.id.scrollview).getClass());
     89     }
     90 
     91 }
     92