• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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;
18 
19 import junit.framework.TestCase;
20 import android.test.suitebuilder.annotation.SmallTest;
21 
22 /**
23  * Tests for {@link StateSet}
24  */
25 
26 public class StateSetTest extends TestCase {
27 
28     @SmallTest
testStateSetPositiveMatches()29     public void testStateSetPositiveMatches() throws Exception {
30          int[] stateSpec = new int[2];
31          int[] stateSet = new int[3];
32          // Single states in both sets - match
33          stateSpec[0] = 1;
34          stateSet[0] = 1;
35          assertTrue(StateSet.stateSetMatches(stateSpec, stateSet));
36          // Single states in both sets - non-match
37          stateSet[0] = 2;
38          assertFalse(StateSet.stateSetMatches(stateSpec, stateSet));
39          // Add another state to the spec which the stateSet doesn't match
40          stateSpec[1] = 2;
41          assertFalse(StateSet.stateSetMatches(stateSpec, stateSet));
42          // Add the missing matching element to the stateSet
43          stateSet[1] = 1;
44          assertTrue(StateSet.stateSetMatches(stateSpec, stateSet));
45          // Add an irrelevent state to the stateSpec
46          stateSet[2] = 12345;
47          assertTrue(StateSet.stateSetMatches(stateSpec, stateSet));
48      }
49 
50      @SmallTest
testStatesSetMatchMixEmUp()51      public void testStatesSetMatchMixEmUp() throws Exception {
52          int[] stateSpec = new int[2];
53          int[] stateSet = new int[2];
54          // One element in stateSpec which we must match and one which we must
55          // not match.  stateSet only contains the match.
56          stateSpec[0] = 1;
57          stateSpec[1] = -2;
58          stateSet[0] = 1;
59          assertTrue(StateSet.stateSetMatches(stateSpec, stateSet));
60          // stateSet now contains just the element we must not match
61          stateSet[0] = 2;
62          assertFalse(StateSet.stateSetMatches(stateSpec, stateSet));
63          // Add another matching state to the the stateSet.  We still fail
64          // because stateSet contains a must-not-match element
65          stateSet[1] = 1;
66          assertFalse(StateSet.stateSetMatches(stateSpec, stateSet));
67          // Switch the must-not-match element in stateSet with a don't care
68          stateSet[0] = 12345;
69          assertTrue(StateSet.stateSetMatches(stateSpec, stateSet));
70      }
71 
72      @SmallTest
testStateSetNegativeMatches()73      public void testStateSetNegativeMatches() throws Exception {
74          int[] stateSpec = new int[2];
75          int[] stateSet = new int[3];
76          // Single states in both sets - match
77          stateSpec[0] = -1;
78          stateSet[0] = 2;
79          assertTrue(StateSet.stateSetMatches(stateSpec, stateSet));
80          // Add another arrelevent state to the stateSet
81          stateSet[1] = 12345;
82          assertTrue(StateSet.stateSetMatches(stateSpec, stateSet));
83          // Single states in both sets - non-match
84          stateSet[0] = 1;
85          assertFalse(StateSet.stateSetMatches(stateSpec, stateSet));
86          // Add another state to the spec which the stateSet doesn't match
87          stateSpec[1] = -2;
88          assertFalse(StateSet.stateSetMatches(stateSpec, stateSet));
89          // Add an irrelevent state to the stateSet
90          stateSet[2] = 12345;
91          assertFalse(StateSet.stateSetMatches(stateSpec, stateSet));
92      }
93 
94      @SmallTest
testEmptySetMatchesNegtives()95      public void testEmptySetMatchesNegtives() throws Exception {
96          int[] stateSpec = {-12345, -6789};
97          int[] stateSet = new int[0];
98          assertTrue(StateSet.stateSetMatches(stateSpec, stateSet));
99          int[] stateSet2 = {0};
100          assertTrue(StateSet.stateSetMatches(stateSpec, stateSet2));
101      }
102 
103      @SmallTest
testEmptySetFailsPositives()104      public void testEmptySetFailsPositives() throws Exception {
105          int[] stateSpec = {12345};
106          int[] stateSet = new int[0];
107          assertFalse(StateSet.stateSetMatches(stateSpec, stateSet));
108          int[] stateSet2 = {0};
109          assertFalse(StateSet.stateSetMatches(stateSpec, stateSet2));
110      }
111 
112      @SmallTest
testEmptySetMatchesWildcard()113      public void testEmptySetMatchesWildcard() throws Exception {
114          int[] stateSpec = StateSet.WILD_CARD;
115          int[] stateSet = new int[0];
116          assertTrue(StateSet.stateSetMatches(stateSpec, stateSet));
117          int[] stateSet2 = {0};
118          assertTrue(StateSet.stateSetMatches(stateSpec, stateSet2));
119      }
120 
121      @SmallTest
testSingleStatePositiveMatches()122      public void testSingleStatePositiveMatches() throws Exception {
123          int[] stateSpec = new int[2];
124          int state;
125          //  match
126          stateSpec[0] = 1;
127          state = 1;
128          assertTrue(StateSet.stateSetMatches(stateSpec, state));
129          // non-match
130          state = 2;
131          assertFalse(StateSet.stateSetMatches(stateSpec, state));
132          // add irrelevant must-not-match
133          stateSpec[1] = -12345;
134          assertFalse(StateSet.stateSetMatches(stateSpec, state));
135      }
136 
137      @SmallTest
testSingleStateNegativeMatches()138      public void testSingleStateNegativeMatches() throws Exception {
139          int[] stateSpec = new int[2];
140          int state;
141          //  match
142          stateSpec[0] = -1;
143          state = 1;
144          assertFalse(StateSet.stateSetMatches(stateSpec, state));
145          // non-match
146          state = 2;
147          assertTrue(StateSet.stateSetMatches(stateSpec, state));
148          // add irrelevant must-not-match
149          stateSpec[1] = -12345;
150          assertTrue(StateSet.stateSetMatches(stateSpec, state));
151      }
152 
153      @SmallTest
testZeroStateOnlyMatchesDefault()154      public void testZeroStateOnlyMatchesDefault() throws Exception {
155          int[] stateSpec = new int[3];
156          int state = 0;
157          //  non-match
158          stateSpec[0] = 1;
159          assertFalse(StateSet.stateSetMatches(stateSpec, state));
160          // non-match
161          stateSpec[1] = -1;
162          assertFalse(StateSet.stateSetMatches(stateSpec, state));
163          // match
164          stateSpec = StateSet.WILD_CARD;
165          assertTrue(StateSet.stateSetMatches(stateSpec, state));
166      }
167 
168      @SmallTest
testNullStateOnlyMatchesDefault()169      public void testNullStateOnlyMatchesDefault() throws Exception {
170          int[] stateSpec = new int[3];
171          int[] stateSet = null;
172          //  non-match
173          stateSpec[0] = 1;
174          assertFalse(StateSet.stateSetMatches(stateSpec, stateSet));
175          // non-match
176          stateSpec[1] = -1;
177          assertFalse(StateSet.stateSetMatches(stateSpec, stateSet));
178          // match
179          stateSpec = StateSet.WILD_CARD;
180          assertTrue(StateSet.stateSetMatches(stateSpec, stateSet));
181      }
182 }
183