Home | History | Annotate | Download | only in cts
      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 
     17 package android.view.cts;
     18 
     19 import static org.junit.Assert.assertFalse;
     20 import static org.junit.Assert.assertTrue;
     21 
     22 import android.app.Activity;
     23 import android.content.res.ColorStateList;
     24 import android.graphics.Color;
     25 import android.graphics.drawable.ColorDrawable;
     26 import android.graphics.drawable.Drawable;
     27 import android.graphics.drawable.RippleDrawable;
     28 import android.support.test.annotation.UiThreadTest;
     29 import android.support.test.filters.MediumTest;
     30 import android.support.test.rule.ActivityTestRule;
     31 import android.support.test.runner.AndroidJUnit4;
     32 import android.view.View;
     33 import android.widget.Button;
     34 import android.widget.EditText;
     35 import android.widget.ImageView;
     36 import android.widget.LinearLayout;
     37 import android.widget.ListView;
     38 
     39 import org.junit.Rule;
     40 import org.junit.Test;
     41 import org.junit.runner.RunWith;
     42 
     43 @MediumTest
     44 @RunWith(AndroidJUnit4.class)
     45 public class View_DefaultFocusHighlightTest {
     46 
     47     @Rule
     48     public ActivityTestRule<DefaultFocusHighlightCtsActivity> mActivityRule =
     49             new ActivityTestRule<>(DefaultFocusHighlightCtsActivity.class);
     50 
     51     @UiThreadTest
     52     @Test
     53     public void testSettersAndGetters() {
     54         Activity activity = mActivityRule.getActivity();
     55         if (!View.isDefaultFocusHighlightEnabled()) {
     56             // Skip the test when config_useDefaultFocusHighlight is false
     57             return;
     58         }
     59 
     60         View view = activity.findViewById(R.id.view);
     61         ListView listView = (ListView) activity.findViewById(R.id.listview);
     62         EditText editText = (EditText) activity.findViewById(R.id.edittext);
     63         Button button = (Button) activity.findViewById(R.id.button);
     64         LinearLayout linearLayout = (LinearLayout) activity.findViewById(R.id.linearlayout);
     65 
     66         assertTrue(view.getDefaultFocusHighlightEnabled());
     67         assertFalse(listView.getDefaultFocusHighlightEnabled());
     68         assertFalse(editText.getDefaultFocusHighlightEnabled());
     69         assertTrue(button.getDefaultFocusHighlightEnabled());
     70         assertTrue(linearLayout.getDefaultFocusHighlightEnabled());
     71 
     72         view.setDefaultFocusHighlightEnabled(false);
     73         listView.setDefaultFocusHighlightEnabled(true);
     74         editText.setDefaultFocusHighlightEnabled(true);
     75         button.setDefaultFocusHighlightEnabled(false);
     76         linearLayout.setDefaultFocusHighlightEnabled(false);
     77 
     78         assertFalse(view.getDefaultFocusHighlightEnabled());
     79         assertTrue(listView.getDefaultFocusHighlightEnabled());
     80         assertTrue(editText.getDefaultFocusHighlightEnabled());
     81         assertFalse(button.getDefaultFocusHighlightEnabled());
     82         assertFalse(linearLayout.getDefaultFocusHighlightEnabled());
     83     }
     84 
     85     @UiThreadTest
     86     @Test
     87     public void testInflating() {
     88         Activity activity = mActivityRule.getActivity();
     89         if (!View.isDefaultFocusHighlightEnabled()) {
     90             // Skip the test when config_useDefaultFocusHighlight is false
     91             return;
     92         }
     93 
     94         View view = activity.findViewById(R.id.view_to_inflate);
     95         ListView listView = (ListView) activity.findViewById(R.id.listview_to_inflate);
     96         EditText editText = (EditText) activity.findViewById(R.id.edittext_to_inflate);
     97         Button button = (Button) activity.findViewById(R.id.button_to_inflate);
     98         LinearLayout linearLayout = (LinearLayout) activity.findViewById(
     99                 R.id.linearlayout_to_inflate);
    100 
    101         assertFalse(view.getDefaultFocusHighlightEnabled());
    102         assertTrue(listView.getDefaultFocusHighlightEnabled());
    103         assertTrue(editText.getDefaultFocusHighlightEnabled());
    104         assertFalse(button.getDefaultFocusHighlightEnabled());
    105         assertFalse(linearLayout.getDefaultFocusHighlightEnabled());
    106     }
    107 
    108     @UiThreadTest
    109     @Test
    110     public void testIsDefaultFocusHighlightNeeded() {
    111         Activity activity = mActivityRule.getActivity();
    112         if (!View.isDefaultFocusHighlightEnabled()) {
    113             // Skip the test when config_useDefaultFocusHighlight is false
    114             return;
    115         }
    116 
    117         final Button button = (Button) activity.findViewById(R.id.button_to_test_highlight_needed);
    118         final ImageView imageView =
    119                 (ImageView) activity.findViewById(R.id.image_view_to_test_highlight_needed);
    120 
    121         final Drawable[] drawables = new Drawable[] {
    122                 null,  // null
    123                 new ColorDrawable(Color.WHITE), // not stateful
    124                 new RippleDrawable(ColorStateList.valueOf(Color.WHITE), null, null) // stateful
    125         };
    126         final boolean[] lackFocusState = new boolean[]{
    127                 true, // for null
    128                 true, // for not stateful
    129                 false, // for stateful
    130         };
    131 
    132         boolean isNeeded, expected;
    133 
    134         // View
    135         for (int i = 0; i < drawables.length; i++) {
    136             for (int j = 0; j < drawables.length; j++) {
    137                 // Turn on default focus highlight.
    138                 button.setDefaultFocusHighlightEnabled(true);
    139                 isNeeded = button.isDefaultFocusHighlightNeeded(drawables[i], drawables[j]);
    140                 expected = lackFocusState[i] && lackFocusState[j];
    141                 assertTrue(isNeeded == expected);
    142                 // Turn off default focus highlight.
    143                 button.setDefaultFocusHighlightEnabled(false);
    144                 isNeeded = button.isDefaultFocusHighlightNeeded(drawables[i], drawables[j]);
    145                 assertFalse(isNeeded);
    146             }
    147         }
    148 
    149         // ImageView
    150         for (int k = 0; k < drawables.length; k++) {
    151             imageView.setImageDrawable(drawables[k]);
    152             for (int i = 0; i < drawables.length; i++) {
    153                 for (int j = 0; j < drawables.length; j++) {
    154                     // Turn on default focus highlight.
    155                     imageView.setDefaultFocusHighlightEnabled(true);
    156                     isNeeded = imageView.isDefaultFocusHighlightNeeded(drawables[i], drawables[j]);
    157                     expected = lackFocusState[i] && lackFocusState[j] && lackFocusState[k];
    158                     assertTrue(isNeeded == expected);
    159                     // Turn off default focus highlight.
    160                     imageView.setDefaultFocusHighlightEnabled(false);
    161                     isNeeded = imageView.isDefaultFocusHighlightNeeded(drawables[i], drawables[j]);
    162                     assertFalse(isNeeded);
    163                 }
    164             }
    165         }
    166     }
    167 }
    168