Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2011 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.os;
     18 
     19 import java.util.concurrent.CountDownLatch;
     20 import java.util.concurrent.TimeUnit;
     21 
     22 import junit.framework.TestCase;
     23 
     24 import android.os.SystemProperties;
     25 import android.test.suitebuilder.annotation.SmallTest;
     26 
     27 public class SystemPropertiesTest extends TestCase {
     28     private static final String KEY = "sys.testkey";
     29     private static final String PERSIST_KEY = "persist.sys.testkey";
     30 
     31     @SmallTest
     32     public void testStressPersistPropertyConsistency() throws Exception {
     33         for (int i = 0; i < 100; ++i) {
     34             SystemProperties.set(PERSIST_KEY, Long.toString(i));
     35             long ret = SystemProperties.getLong(PERSIST_KEY, -1);
     36             assertEquals(i, ret);
     37         }
     38     }
     39 
     40     @SmallTest
     41     public void testStressMemoryPropertyConsistency() throws Exception {
     42         for (int i = 0; i < 100; ++i) {
     43             SystemProperties.set(KEY, Long.toString(i));
     44             long ret = SystemProperties.getLong(KEY, -1);
     45             assertEquals(i, ret);
     46         }
     47     }
     48 
     49     @SmallTest
     50     public void testProperties() throws Exception {
     51         String value;
     52 
     53         SystemProperties.set(KEY, "");
     54         value = SystemProperties.get(KEY, "default");
     55         assertEquals("default", value);
     56 
     57         // null default value is the same as "".
     58         SystemProperties.set(KEY, null);
     59         value = SystemProperties.get(KEY, "default");
     60         assertEquals("default", value);
     61 
     62         SystemProperties.set(KEY, "SA");
     63         value = SystemProperties.get(KEY, "default");
     64         assertEquals("SA", value);
     65 
     66         value = SystemProperties.get(KEY);
     67         assertEquals("SA", value);
     68 
     69         SystemProperties.set(KEY, "");
     70         value = SystemProperties.get(KEY, "default");
     71         assertEquals("default", value);
     72 
     73         // null value is the same as "".
     74         SystemProperties.set(KEY, "SA");
     75         SystemProperties.set(KEY, null);
     76         value = SystemProperties.get(KEY, "default");
     77         assertEquals("default", value);
     78 
     79         value = SystemProperties.get(KEY);
     80         assertEquals("", value);
     81     }
     82 
     83     private static void testInt(String setVal, int defValue, int expected) {
     84       SystemProperties.set(KEY, setVal);
     85       int value = SystemProperties.getInt(KEY, defValue);
     86       assertEquals(expected, value);
     87     }
     88 
     89     private static void testLong(String setVal, long defValue, long expected) {
     90       SystemProperties.set(KEY, setVal);
     91       long value = SystemProperties.getLong(KEY, defValue);
     92       assertEquals(expected, value);
     93     }
     94 
     95     @SmallTest
     96     public void testIntegralProperties() throws Exception {
     97         testInt("", 123, 123);
     98         testInt("", 0, 0);
     99         testInt("", -123, -123);
    100 
    101         testInt("123", 124, 123);
    102         testInt("0", 124, 0);
    103         testInt("-123", 124, -123);
    104 
    105         testLong("", 3147483647L, 3147483647L);
    106         testLong("", 0, 0);
    107         testLong("", -3147483647L, -3147483647L);
    108 
    109         testLong("3147483647", 124, 3147483647L);
    110         testLong("0", 124, 0);
    111         testLong("-3147483647", 124, -3147483647L);
    112     }
    113 
    114     @SmallTest
    115     @SuppressWarnings("null")
    116     public void testNullKey() throws Exception {
    117         try {
    118             SystemProperties.get(null);
    119             fail("Expected NullPointerException");
    120         } catch (NullPointerException npe) {
    121         }
    122 
    123         try {
    124             SystemProperties.get(null, "default");
    125             fail("Expected NullPointerException");
    126         } catch (NullPointerException npe) {
    127         }
    128 
    129         try {
    130             SystemProperties.set(null, "value");
    131             fail("Expected NullPointerException");
    132         } catch (NullPointerException npe) {
    133         }
    134 
    135         try {
    136             SystemProperties.getInt(null, 0);
    137             fail("Expected NullPointerException");
    138         } catch (NullPointerException npe) {
    139         }
    140 
    141         try {
    142             SystemProperties.getLong(null, 0);
    143             fail("Expected NullPointerException");
    144         } catch (NullPointerException npe) {
    145         }
    146     }
    147 
    148     @SmallTest
    149     public void testCallbacks() {
    150         // Latches are not really necessary, but are easy to use.
    151         final CountDownLatch wait1 = new CountDownLatch(1);
    152         final CountDownLatch wait2 = new CountDownLatch(1);
    153 
    154         Runnable r1 = new Runnable() {
    155             boolean done = false;
    156             @Override
    157             public void run() {
    158                 if (done) {
    159                     return;
    160                 }
    161                 done = true;
    162 
    163                 wait1.countDown();
    164                 throw new RuntimeException("test");
    165             }
    166         };
    167 
    168         Runnable r2 = new Runnable() {
    169             @Override
    170             public void run() {
    171                 wait2.countDown();
    172             }
    173         };
    174 
    175         SystemProperties.addChangeCallback(r1);
    176         SystemProperties.addChangeCallback(r2);
    177 
    178         SystemProperties.reportSyspropChanged();
    179 
    180         try {
    181             assertTrue(wait1.await(5, TimeUnit.SECONDS));
    182         } catch (InterruptedException e) {
    183             fail("InterruptedException");
    184         }
    185         try {
    186             assertTrue(wait2.await(5, TimeUnit.SECONDS));
    187         } catch (InterruptedException e) {
    188             fail("InterruptedException");
    189         }
    190     }
    191 }
    192