| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.test.suitebuilder.annotation.SmallTest; |
| 20 |
|
| 21 |
import junit.framework.TestCase; |
| 22 |
|
| 23 |
import java.util.Objects; |
| 24 |
import java.util.concurrent.CountDownLatch; |
| 25 |
import java.util.concurrent.TimeUnit; |
| 26 |
|
| 27 |
public class SystemPropertiesTest extends TestCase { |
| 28 |
private static final String KEY = "sys.testkey"; |
| 29 |
private static final String UNSET_KEY = "Aiw7woh6ie4toh7W"; |
| 30 |
private static final String PERSIST_KEY = "persist.sys.testkey"; |
| 31 |
|
| 32 |
@SmallTest |
| 33 |
public void testStressPersistPropertyConsistency() throws Exception { |
| 34 |
for (int i = 0; i < 100; ++i) { |
| 35 |
SystemProperties.set(PERSIST_KEY, Long.toString(i)); |
| 36 |
long ret = SystemProperties.getLong(PERSIST_KEY, -1); |
| 37 |
assertEquals(i, ret); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
@SmallTest |
| 42 |
public void testStressMemoryPropertyConsistency() throws Exception { |
| 43 |
for (int i = 0; i < 100; ++i) { |
| 44 |
SystemProperties.set(KEY, Long.toString(i)); |
| 45 |
long ret = SystemProperties.getLong(KEY, -1); |
| 46 |
assertEquals(i, ret); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
@SmallTest |
| 51 |
public void testProperties() throws Exception { |
| 52 |
String value; |
| 53 |
|
| 54 |
SystemProperties.set(KEY, ""); |
| 55 |
value = SystemProperties.get(KEY, "default"); |
| 56 |
assertEquals("default", value); |
| 57 |
|
| 58 |
|
| 59 |
SystemProperties.set(KEY, null); |
| 60 |
value = SystemProperties.get(KEY, "default"); |
| 61 |
assertEquals("default", value); |
| 62 |
|
| 63 |
SystemProperties.set(KEY, "SA"); |
| 64 |
value = SystemProperties.get(KEY, "default"); |
| 65 |
assertEquals("SA", value); |
| 66 |
|
| 67 |
value = SystemProperties.get(KEY); |
| 68 |
assertEquals("SA", value); |
| 69 |
|
| 70 |
SystemProperties.set(KEY, ""); |
| 71 |
value = SystemProperties.get(KEY, "default"); |
| 72 |
assertEquals("default", value); |
| 73 |
|
| 74 |
|
| 75 |
SystemProperties.set(KEY, "SA"); |
| 76 |
SystemProperties.set(KEY, null); |
| 77 |
value = SystemProperties.get(KEY, "default"); |
| 78 |
assertEquals("default", value); |
| 79 |
|
| 80 |
value = SystemProperties.get(KEY); |
| 81 |
assertEquals("", value); |
| 82 |
} |
| 83 |
|
| 84 |
private static void testInt(String setVal, int defValue, int expected) { |
| 85 |
SystemProperties.set(KEY, setVal); |
| 86 |
int value = SystemProperties.getInt(KEY, defValue); |
| 87 |
assertEquals(expected, value); |
| 88 |
} |
| 89 |
|
| 90 |
private static void testLong(String setVal, long defValue, long expected) { |
| 91 |
SystemProperties.set(KEY, setVal); |
| 92 |
long value = SystemProperties.getLong(KEY, defValue); |
| 93 |
assertEquals(expected, value); |
| 94 |
} |
| 95 |
|
| 96 |
@SmallTest |
| 97 |
public void testHandle() throws Exception { |
| 98 |
String value; |
| 99 |
SystemProperties.Handle handle = SystemProperties.find("doesnotexist_2341431"); |
| 100 |
assertNull(handle); |
| 101 |
SystemProperties.set(KEY, "abc"); |
| 102 |
handle = SystemProperties.find(KEY); |
| 103 |
assertNotNull(handle); |
| 104 |
value = handle.get(); |
| 105 |
assertEquals("abc", value); |
| 106 |
SystemProperties.set(KEY, "blarg"); |
| 107 |
value = handle.get(); |
| 108 |
assertEquals("blarg", value); |
| 109 |
SystemProperties.set(KEY, "1"); |
| 110 |
assertEquals(1, handle.getInt(-1)); |
| 111 |
assertEquals(1, handle.getLong(-1)); |
| 112 |
assertEquals(true, handle.getBoolean(false)); |
| 113 |
SystemProperties.set(KEY, ""); |
| 114 |
assertEquals(12345, handle.getInt(12345)); |
| 115 |
} |
| 116 |
|
| 117 |
@SmallTest |
| 118 |
public void testIntegralProperties() throws Exception { |
| 119 |
testInt("", 123, 123); |
| 120 |
testInt("", 0, 0); |
| 121 |
testInt("", -123, -123); |
| 122 |
|
| 123 |
testInt("123", 124, 123); |
| 124 |
testInt("0", 124, 0); |
| 125 |
testInt("-123", 124, -123); |
| 126 |
|
| 127 |
testLong("", 3147483647L, 3147483647L); |
| 128 |
testLong("", 0, 0); |
| 129 |
testLong("", -3147483647L, -3147483647L); |
| 130 |
|
| 131 |
testLong("3147483647", 124, 3147483647L); |
| 132 |
testLong("0", 124, 0); |
| 133 |
testLong("-3147483647", 124, -3147483647L); |
| 134 |
} |
| 135 |
|
| 136 |
@SmallTest |
| 137 |
public void testUnset() throws Exception { |
| 138 |
assertEquals("abc", SystemProperties.get(UNSET_KEY, "abc")); |
| 139 |
assertEquals(true, SystemProperties.getBoolean(UNSET_KEY, true)); |
| 140 |
assertEquals(false, SystemProperties.getBoolean(UNSET_KEY, false)); |
| 141 |
assertEquals(5, SystemProperties.getInt(UNSET_KEY, 5)); |
| 142 |
assertEquals(-10, SystemProperties.getLong(UNSET_KEY, -10)); |
| 143 |
} |
| 144 |
|
| 145 |
@SmallTest |
| 146 |
@SuppressWarnings("null") |
| 147 |
public void testNullKey() throws Exception { |
| 148 |
try { |
| 149 |
SystemProperties.get(null); |
| 150 |
fail("Expected NullPointerException"); |
| 151 |
} catch (NullPointerException npe) { |
| 152 |
} |
| 153 |
|
| 154 |
try { |
| 155 |
SystemProperties.get(null, "default"); |
| 156 |
fail("Expected NullPointerException"); |
| 157 |
} catch (NullPointerException npe) { |
| 158 |
} |
| 159 |
|
| 160 |
try { |
| 161 |
SystemProperties.set(null, "value"); |
| 162 |
fail("Expected NullPointerException"); |
| 163 |
} catch (NullPointerException npe) { |
| 164 |
} |
| 165 |
|
| 166 |
try { |
| 167 |
SystemProperties.getInt(null, 0); |
| 168 |
fail("Expected NullPointerException"); |
| 169 |
} catch (NullPointerException npe) { |
| 170 |
} |
| 171 |
|
| 172 |
try { |
| 173 |
SystemProperties.getLong(null, 0); |
| 174 |
fail("Expected NullPointerException"); |
| 175 |
} catch (NullPointerException npe) { |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
@SmallTest |
| 180 |
public void testCallbacks() { |
| 181 |
|
| 182 |
final CountDownLatch wait1 = new CountDownLatch(1); |
| 183 |
final CountDownLatch wait2 = new CountDownLatch(1); |
| 184 |
|
| 185 |
Runnable r1 = new Runnable() { |
| 186 |
boolean done = false; |
| 187 |
@Override |
| 188 |
public void run() { |
| 189 |
if (done) { |
| 190 |
return; |
| 191 |
} |
| 192 |
done = true; |
| 193 |
|
| 194 |
wait1.countDown(); |
| 195 |
throw new RuntimeException("test"); |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
Runnable r2 = new Runnable() { |
| 200 |
@Override |
| 201 |
public void run() { |
| 202 |
wait2.countDown(); |
| 203 |
} |
| 204 |
}; |
| 205 |
|
| 206 |
SystemProperties.addChangeCallback(r1); |
| 207 |
SystemProperties.addChangeCallback(r2); |
| 208 |
|
| 209 |
SystemProperties.reportSyspropChanged(); |
| 210 |
|
| 211 |
try { |
| 212 |
assertTrue(wait1.await(5, TimeUnit.SECONDS)); |
| 213 |
} catch (InterruptedException e) { |
| 214 |
fail("InterruptedException"); |
| 215 |
} |
| 216 |
try { |
| 217 |
assertTrue(wait2.await(5, TimeUnit.SECONDS)); |
| 218 |
} catch (InterruptedException e) { |
| 219 |
fail("InterruptedException"); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
@SmallTest |
| 224 |
public void testDigestOf() { |
| 225 |
final String empty = SystemProperties.digestOf(); |
| 226 |
final String finger = SystemProperties.digestOf("ro.build.fingerprint"); |
| 227 |
final String fingerBrand = SystemProperties.digestOf( |
| 228 |
"ro.build.fingerprint", "ro.product.brand"); |
| 229 |
final String brandFinger = SystemProperties.digestOf( |
| 230 |
"ro.product.brand", "ro.build.fingerprint"); |
| 231 |
|
| 232 |
|
| 233 |
assertTrue(Objects.equals(finger, SystemProperties.digestOf("ro.build.fingerprint"))); |
| 234 |
|
| 235 |
|
| 236 |
assertFalse(Objects.equals(empty, finger)); |
| 237 |
assertFalse(Objects.equals(empty, fingerBrand)); |
| 238 |
assertFalse(Objects.equals(finger, fingerBrand)); |
| 239 |
|
| 240 |
|
| 241 |
assertTrue(Objects.equals(fingerBrand, brandFinger)); |
| 242 |
} |
| 243 |
} |