Home | History | Annotate | Download | only in cts
      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 
     17 package android.widget.cts;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertSame;
     22 import static org.junit.Assert.assertTrue;
     23 
     24 import android.app.Activity;
     25 import android.app.Instrumentation;
     26 import android.content.res.ColorStateList;
     27 import android.graphics.Color;
     28 import android.graphics.PorterDuff.Mode;
     29 import android.graphics.Rect;
     30 import android.graphics.Typeface;
     31 import android.graphics.drawable.Drawable;
     32 import android.support.test.InstrumentationRegistry;
     33 import android.support.test.annotation.UiThreadTest;
     34 import android.support.test.filters.MediumTest;
     35 import android.support.test.rule.ActivityTestRule;
     36 import android.support.test.runner.AndroidJUnit4;
     37 import android.view.ContextThemeWrapper;
     38 import android.view.ViewGroup;
     39 import android.widget.Switch;
     40 import android.widget.cts.util.TestUtils;
     41 
     42 import com.android.compatibility.common.util.WidgetTestUtils;
     43 
     44 import org.junit.Before;
     45 import org.junit.Rule;
     46 import org.junit.Test;
     47 import org.junit.runner.RunWith;
     48 
     49 /**
     50  * Test {@link Switch}.
     51  */
     52 @MediumTest
     53 @RunWith(AndroidJUnit4.class)
     54 public class SwitchTest {
     55     private Instrumentation mInstrumentation;
     56     private Activity mActivity;
     57     private Switch mSwitch;
     58 
     59     @Rule
     60     public ActivityTestRule<SwitchCtsActivity> mActivityRule =
     61             new ActivityTestRule<>(SwitchCtsActivity.class);
     62 
     63     @Before
     64     public void setup() {
     65         mInstrumentation = InstrumentationRegistry.getInstrumentation();
     66         mActivity = mActivityRule.getActivity();
     67     }
     68 
     69     private Switch findSwitchById(int id) {
     70         return (Switch) mActivity.findViewById(id);
     71     }
     72 
     73     @UiThreadTest
     74     @Test
     75     public void testConstructor() {
     76         new Switch(mActivity);
     77 
     78         new Switch(mActivity, null);
     79 
     80         new Switch(mActivity, null, android.R.attr.switchStyle);
     81 
     82         new Switch(mActivity, null, 0, android.R.style.Widget_Material_CompoundButton_Switch);
     83 
     84         new Switch(mActivity, null, 0, android.R.style.Widget_Material_Light_CompoundButton_Switch);
     85     }
     86 
     87     @UiThreadTest
     88     @Test
     89     public void testAccessThumbTint() {
     90         mSwitch = findSwitchById(R.id.switch1);
     91 
     92         // These are the default set in layout XML
     93         assertEquals(Color.WHITE, mSwitch.getThumbTintList().getDefaultColor());
     94         assertEquals(Mode.SRC_OVER, mSwitch.getThumbTintMode());
     95 
     96         ColorStateList colors = ColorStateList.valueOf(Color.RED);
     97         mSwitch.setThumbTintList(colors);
     98         mSwitch.setThumbTintMode(Mode.XOR);
     99 
    100         assertSame(colors, mSwitch.getThumbTintList());
    101         assertEquals(Mode.XOR, mSwitch.getThumbTintMode());
    102     }
    103 
    104     @UiThreadTest
    105     @Test
    106     public void testAccessTrackTint() {
    107         mSwitch = findSwitchById(R.id.switch1);
    108 
    109         // These are the default set in layout XML
    110         assertEquals(Color.BLACK, mSwitch.getTrackTintList().getDefaultColor());
    111         assertEquals(Mode.SRC_ATOP, mSwitch.getTrackTintMode());
    112 
    113         ColorStateList colors = ColorStateList.valueOf(Color.RED);
    114         mSwitch.setTrackTintList(colors);
    115         mSwitch.setTrackTintMode(Mode.XOR);
    116 
    117         assertSame(colors, mSwitch.getTrackTintList());
    118         assertEquals(Mode.XOR, mSwitch.getTrackTintMode());
    119     }
    120 
    121     @Test
    122     public void testAccessThumbDrawable() throws Throwable {
    123         mSwitch = findSwitchById(R.id.switch2);
    124 
    125         // This is the default set in layout XML
    126         Drawable thumbDrawable = mSwitch.getThumbDrawable();
    127         TestUtils.assertAllPixelsOfColor("Thumb drawable should be blue", thumbDrawable,
    128                 thumbDrawable.getIntrinsicWidth(), thumbDrawable.getIntrinsicHeight(), false,
    129                 Color.BLUE, 1, true);
    130 
    131         // Change thumb drawable to red
    132         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    133                 () -> mSwitch.setThumbDrawable(mActivity.getDrawable(R.drawable.icon_red)));
    134         thumbDrawable = mSwitch.getThumbDrawable();
    135         TestUtils.assertAllPixelsOfColor("Thumb drawable should be red", thumbDrawable,
    136                 thumbDrawable.getIntrinsicWidth(), thumbDrawable.getIntrinsicHeight(), false,
    137                 Color.RED, 1, true);
    138 
    139         // Change thumb drawable to green
    140         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    141                 () -> mSwitch.setThumbResource(R.drawable.icon_green));
    142         thumbDrawable = mSwitch.getThumbDrawable();
    143         TestUtils.assertAllPixelsOfColor("Thumb drawable should be green", thumbDrawable,
    144                 thumbDrawable.getIntrinsicWidth(), thumbDrawable.getIntrinsicHeight(), false,
    145                 Color.GREEN, 1, true);
    146 
    147         // Now tint the latest (green) thumb drawable with 50% blue SRC_OVER
    148         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch, () -> {
    149             mSwitch.setThumbTintList(ColorStateList.valueOf(0x800000FF));
    150             mSwitch.setThumbTintMode(Mode.SRC_OVER);
    151         });
    152         thumbDrawable = mSwitch.getThumbDrawable();
    153         TestUtils.assertAllPixelsOfColor("Thumb drawable should be green / blue", thumbDrawable,
    154                 thumbDrawable.getIntrinsicWidth(), thumbDrawable.getIntrinsicHeight(), false,
    155                 TestUtils.compositeColors(0x800000FF, Color.GREEN), 1, true);
    156     }
    157 
    158     @Test
    159     public void testAccessTrackDrawable() throws Throwable {
    160         mSwitch = findSwitchById(R.id.switch2);
    161         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    162             () -> mSwitch.setTrackTintMode(Mode.DST));
    163 
    164         // This is the default set in layout XML
    165         Drawable trackDrawable = mSwitch.getTrackDrawable();
    166         Rect trackDrawableBounds = trackDrawable.getBounds();
    167         TestUtils.assertAllPixelsOfColor("Track drawable should be 50% red", trackDrawable,
    168                 trackDrawableBounds.width(), trackDrawableBounds.height(), false,
    169                 0x80FF0000, 1, true);
    170 
    171         // Change track drawable to blue
    172         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    173                 () -> mSwitch.setTrackDrawable(mActivity.getDrawable(R.drawable.blue_fill)));
    174         trackDrawable = mSwitch.getTrackDrawable();
    175         trackDrawableBounds = trackDrawable.getBounds();
    176         TestUtils.assertAllPixelsOfColor("Track drawable should be blue", trackDrawable,
    177                 trackDrawableBounds.width(), trackDrawableBounds.height(), false,
    178                 Color.BLUE, 1, true);
    179 
    180         // Change track drawable to green
    181         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    182                 () -> mSwitch.setTrackResource(R.drawable.green_fill));
    183         trackDrawable = mSwitch.getTrackDrawable();
    184         trackDrawableBounds = trackDrawable.getBounds();
    185         TestUtils.assertAllPixelsOfColor("Track drawable should be green", trackDrawable,
    186                 trackDrawableBounds.width(), trackDrawableBounds.height(), false,
    187                 Color.GREEN, 1, true);
    188 
    189         // Now tint the latest (green) track drawable with 50% blue SRC_OVER
    190         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch, () -> {
    191             mSwitch.setTrackTintList(ColorStateList.valueOf(0x800000FF));
    192             mSwitch.setTrackTintMode(Mode.SRC_OVER);
    193         });
    194         trackDrawable = mSwitch.getTrackDrawable();
    195         trackDrawableBounds = trackDrawable.getBounds();
    196         TestUtils.assertAllPixelsOfColor("Track drawable should be green / blue", trackDrawable,
    197                 trackDrawableBounds.width(), trackDrawableBounds.height(), false,
    198                 TestUtils.compositeColors(0x800000FF, Color.GREEN), 1, true);
    199     }
    200 
    201     @Test
    202     public void testAccessText() throws Throwable {
    203         // Run text-related tests on a Holo-themed switch, since under Material themes we
    204         // are not showing texts by default.
    205         mSwitch = new Switch(new ContextThemeWrapper(mActivity, android.R.style.Theme_Holo_Light));
    206         final ViewGroup container = (ViewGroup) mActivity.findViewById(R.id.container);
    207         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, container,
    208                 () -> container.addView(mSwitch));
    209 
    210         // Set "on" text and verify it
    211         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    212                 () -> mSwitch.setTextOn("Text on"));
    213         assertEquals("Text on", mSwitch.getTextOn());
    214 
    215         // Set "off" text and verify it
    216         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    217                 () -> mSwitch.setTextOff("Text off"));
    218         assertEquals("Text off", mSwitch.getTextOff());
    219 
    220         // Turn text display on
    221         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    222                 () -> mSwitch.setShowText(true));
    223         assertTrue(mSwitch.getShowText());
    224 
    225         // Use custom text appearance. Since we don't have APIs to query this facet of Switch,
    226         // just test that it's not crashing.
    227         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    228                 () -> mSwitch.setSwitchTextAppearance(mActivity, R.style.TextAppearance_WithColor));
    229 
    230         // Use custom typeface. Since we don't have APIs to query this facet of Switch,
    231         // just test that it's not crashing.
    232         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    233                 () -> mSwitch.setSwitchTypeface(Typeface.MONOSPACE));
    234 
    235         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    236                 () -> mSwitch.setSwitchTypeface(Typeface.SERIF, Typeface.ITALIC));
    237 
    238         // Set and verify padding between the thumb and the text
    239         final int thumbTextPadding = mActivity.getResources().getDimensionPixelSize(
    240                 R.dimen.switch_thumb_text_padding);
    241         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    242                 () -> mSwitch.setThumbTextPadding(thumbTextPadding));
    243         assertEquals(thumbTextPadding, mSwitch.getThumbTextPadding());
    244 
    245         // Set and verify padding between the switch and the text
    246         final int switchPadding = mActivity.getResources().getDimensionPixelSize(
    247                 R.dimen.switch_padding);
    248         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    249                 () -> mSwitch.setSwitchPadding(switchPadding));
    250         assertEquals(switchPadding, mSwitch.getSwitchPadding());
    251 
    252         // Turn text display off
    253         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    254                 () -> mSwitch.setShowText(false));
    255         assertFalse(mSwitch.getShowText());
    256     }
    257 
    258     @Test
    259     public void testAccessMinWidth() throws Throwable {
    260         mSwitch = findSwitchById(R.id.switch3);
    261 
    262         // Set custom min width on the switch and verify that it's at least that wide
    263         final int switchMinWidth = mActivity.getResources().getDimensionPixelSize(
    264                 R.dimen.switch_min_width);
    265         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    266                 () -> mSwitch.setSwitchMinWidth(switchMinWidth));
    267         assertEquals(switchMinWidth, mSwitch.getSwitchMinWidth());
    268         assertTrue(mSwitch.getWidth() >= switchMinWidth);
    269 
    270         // And set another (larger) min width
    271         final int switchMinWidth2 = mActivity.getResources().getDimensionPixelSize(
    272                 R.dimen.switch_min_width2);
    273         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    274                 () -> mSwitch.setSwitchMinWidth(switchMinWidth2));
    275         assertEquals(switchMinWidth2, mSwitch.getSwitchMinWidth());
    276         assertTrue(mSwitch.getWidth() >= switchMinWidth2);
    277     }
    278 
    279     @Test
    280     public void testAccessSplitTrack() throws Throwable {
    281         mSwitch = findSwitchById(R.id.switch3);
    282 
    283         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    284                 () -> mSwitch.setSplitTrack(true));
    285         assertTrue(mSwitch.getSplitTrack());
    286 
    287         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mSwitch,
    288                 () -> mSwitch.setSplitTrack(false));
    289         assertFalse(mSwitch.getSplitTrack());
    290     }
    291 }
    292