Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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.util.cts;
     18 
     19 import android.R;
     20 import android.test.AndroidTestCase;
     21 import android.util.StateSet;
     22 import dalvik.annotation.TestTargets;
     23 import dalvik.annotation.TestTargetNew;
     24 import dalvik.annotation.TestLevel;
     25 import dalvik.annotation.TestTargetClass;
     26 
     27 /**
     28  * Test StateSet
     29  */
     30 @TestTargetClass(StateSet.class)
     31 public class StateSetTest extends AndroidTestCase {
     32 
     33     @Override
     34     protected void setUp() throws Exception {
     35         super.setUp();
     36     }
     37 
     38     @TestTargetNew(
     39         level = TestLevel.COMPLETE,
     40         notes = "Test trim state set.",
     41         method = "trimStateSet",
     42         args = {int[].class, int.class}
     43     )
     44     public void testTrimStateSet() {
     45         // state set's old size is equal to new size
     46         int[] stateSet = {1, 2, 3};
     47         assertEquals(stateSet, StateSet.trimStateSet(stateSet, 3));
     48 
     49         int [] stateSet2 = StateSet.trimStateSet(stateSet, 2);
     50         assertEquals(2, stateSet2.length);
     51         for (int i : stateSet2) {
     52             assertEquals(stateSet2[i - 1], stateSet[i - 1]);
     53         }
     54     }
     55 
     56     @TestTargetNew(
     57         level = TestLevel.COMPLETE,
     58         notes = "test dump state set",
     59         method = "dump",
     60         args = {int[].class}
     61     )
     62     public void testDump() {
     63         int[] stateSet = {R.attr.state_window_focused,
     64                           R.attr.state_pressed,
     65                           R.attr.state_selected,
     66                           R.attr.state_focused,
     67                           R.attr.state_enabled,
     68                           1234325}; // irrelevant value
     69         String string = StateSet.dump(stateSet);
     70         assertEquals("W P S F E ", string);
     71     }
     72 
     73     @TestTargets({
     74         @TestTargetNew(
     75             level = TestLevel.COMPLETE,
     76             notes = "",
     77             method = "stateSetMatches",
     78             args = {int[].class, int[].class}
     79         ),
     80         @TestTargetNew(
     81             level = TestLevel.COMPLETE,
     82             notes = "",
     83             method = "isWildCard",
     84             args = {int[].class}
     85         ),
     86         @TestTargetNew(
     87             level = TestLevel.COMPLETE,
     88             notes = "",
     89             method = "stateSetMatches",
     90             args = {int[].class, int.class}
     91         )
     92     })
     93     public void testStateSetMatches() throws Exception {
     94          int[] stateSpec1 = new int[2];
     95          int[] stateSet1 = new int[3];
     96          // Single states in both sets - match
     97          stateSpec1[0] = 1;
     98          stateSet1[0] = 1;
     99          assertTrue(StateSet.stateSetMatches(stateSpec1, stateSet1));
    100          // Single states in both sets - non-match
    101          stateSet1[0] = 2;
    102          assertFalse(StateSet.stateSetMatches(stateSpec1, stateSet1));
    103          // Add another state to the spec which the stateSet doesn't match
    104          stateSpec1[1] = 2;
    105          assertFalse(StateSet.stateSetMatches(stateSpec1, stateSet1));
    106          // Add the missing matching element to the stateSet
    107          stateSet1[1] = 1;
    108          assertTrue(StateSet.stateSetMatches(stateSpec1, stateSet1));
    109          // Add an irrelevent state to the stateSpec
    110          stateSet1[2] = 12345;
    111          assertTrue(StateSet.stateSetMatches(stateSpec1, stateSet1));
    112 
    113 
    114         int[] stateSpec2 = new int[2];
    115         int[] stateSet2 = new int[2];
    116          // One element in stateSpec which we must match and one which we must
    117          // not match.  stateSet only contains the match.
    118          stateSpec2[0] = 1;
    119          stateSpec2[1] = -2;
    120          stateSet2[0] = 1;
    121          assertTrue(StateSet.stateSetMatches(stateSpec2, stateSet2));
    122          // stateSet now contains just the element we must not match
    123          stateSet2[0] = 2;
    124          assertFalse(StateSet.stateSetMatches(stateSpec2, stateSet2));
    125          // Add another matching state to the the stateSet.  We still fail
    126          // because stateSet contains a must-not-match element
    127          stateSet2[1] = 1;
    128          assertFalse(StateSet.stateSetMatches(stateSpec2, stateSet2));
    129          // Switch the must-not-match element in stateSet with a don't care
    130          stateSet2[0] = 12345;
    131          assertTrue(StateSet.stateSetMatches(stateSpec2, stateSet2));
    132 
    133 
    134         int[] stateSpec3 = new int[2];
    135         int[] stateSet3 = new int[3];
    136         // Single states in both sets - match
    137         stateSpec3[0] = -1;
    138         stateSet3[0] = 2;
    139         assertTrue(StateSet.stateSetMatches(stateSpec3, stateSet3));
    140         // Add another arrelevent state to the stateSet
    141         stateSet3[1] = 12345;
    142         assertTrue(StateSet.stateSetMatches(stateSpec3, stateSet3));
    143         // Single states in both sets - non-match
    144         stateSet3[0] = 1;
    145         assertFalse(StateSet.stateSetMatches(stateSpec3, stateSet3));
    146         // Add another state to the spec which the stateSet doesn't match
    147         stateSpec3[1] = -2;
    148         assertFalse(StateSet.stateSetMatches(stateSpec3, stateSet3));
    149         // Add an irrelevent state to the stateSet
    150         stateSet3[2] = 12345;
    151         assertFalse(StateSet.stateSetMatches(stateSpec3, stateSet3));
    152 
    153 
    154         int[] stateSpec4 = {-12345, -6789};
    155         int[] stateSet4 = new int[0];
    156         assertTrue(StateSet.stateSetMatches(stateSpec4, stateSet4));
    157         int[] stateSet4b = {0};
    158         assertTrue(StateSet.stateSetMatches(stateSpec4, stateSet4b));
    159 
    160 
    161         int[] stateSpec5 = {12345};
    162         int[] stateSet5a = new int[0];
    163         assertFalse(StateSet.stateSetMatches(stateSpec5, stateSet5a));
    164         int[] stateSet5b = {0};
    165         assertFalse(StateSet.stateSetMatches(stateSpec5, stateSet5b));
    166 
    167 
    168         int[] stateSpec6 = StateSet.WILD_CARD;
    169         int[] stateSet6a = new int[0];
    170         assertTrue(StateSet.stateSetMatches(stateSpec6, stateSet6a));
    171         int[] stateSet6b = {0};
    172         assertTrue(StateSet.stateSetMatches(stateSpec6, stateSet6b));
    173 
    174 
    175         int[] stateSpec7 = new int[3];
    176         int[] stateSet7 = null;
    177         //  non-match
    178         stateSpec7[0] = 1;
    179         assertFalse(StateSet.stateSetMatches(stateSpec7, stateSet7));
    180         // non-match
    181         stateSpec7[1] = -1;
    182         assertFalse(StateSet.stateSetMatches(stateSpec7, stateSet7));
    183         // match
    184         stateSpec7 = StateSet.WILD_CARD;
    185         assertTrue(StateSet.stateSetMatches(stateSpec7, stateSet7));
    186 
    187 
    188         int[] stateSpec8 = new int[2];
    189         int state1;
    190         //  match
    191         stateSpec8[0] = 1;
    192         state1 = 1;
    193         assertTrue(StateSet.stateSetMatches(stateSpec8, state1));
    194         // non-match
    195         state1 = 2;
    196         assertFalse(StateSet.stateSetMatches(stateSpec8, state1));
    197         // add irrelevant must-not-match
    198         stateSpec8[1] = -12345;
    199         assertFalse(StateSet.stateSetMatches(stateSpec8, state1));
    200 
    201 
    202         int[] stateSpec9 = new int[2];
    203         int state2;
    204         //  match
    205         stateSpec9[0] = -1;
    206         state2 = 1;
    207         assertFalse(StateSet.stateSetMatches(stateSpec9, state2));
    208         // non-match
    209         state2 = 2;
    210         assertTrue(StateSet.stateSetMatches(stateSpec9, state2));
    211         // add irrelevant must-not-match
    212         stateSpec9[1] = -12345;
    213         assertTrue(StateSet.stateSetMatches(stateSpec9, state2));
    214 
    215 
    216         int[] stateSpec10 = new int[3];
    217         int state3 = 0;
    218         //  non-match
    219         stateSpec10[0] = 1;
    220         assertFalse(StateSet.stateSetMatches(stateSpec10, state3));
    221         // non-match
    222         stateSpec10[1] = -1;
    223         assertFalse(StateSet.stateSetMatches(stateSpec10, state3));
    224         // match
    225         stateSpec10 = StateSet.WILD_CARD;
    226         assertTrue(StateSet.stateSetMatches(stateSpec10, state3));
    227     }
    228 
    229 }
    230