Home | History | Annotate | Download | only in omtp
      1 package com.android.phone.vvm.omtp;
      2 
      3 import android.content.ComponentName;
      4 import android.telecom.PhoneAccountHandle;
      5 import android.test.AndroidTestCase;
      6 import android.util.ArraySet;
      7 
      8 import java.util.Arrays;
      9 
     10 public class VisualVoicemailPreferencesTest extends AndroidTestCase {
     11 
     12     public void testWriteRead() {
     13         VisualVoicemailPreferences preferences = new VisualVoicemailPreferences(getContext(),
     14                 createFakeHandle("testWriteRead"));
     15         preferences.edit()
     16                 .putBoolean("boolean", true)
     17                 .putFloat("float", 0.5f)
     18                 .putInt("int", 123)
     19                 .putLong("long", 456)
     20                 .putString("string", "foo")
     21                 .putStringSet("stringset", new ArraySet<>(Arrays.asList("bar", "baz")))
     22                 .apply();
     23 
     24         assertTrue(preferences.contains("boolean"));
     25         assertTrue(preferences.contains("float"));
     26         assertTrue(preferences.contains("int"));
     27         assertTrue(preferences.contains("long"));
     28         assertTrue(preferences.contains("string"));
     29         assertTrue(preferences.contains("stringset"));
     30 
     31         assertEquals(true, preferences.getBoolean("boolean", false));
     32         assertEquals(0.5f, preferences.getFloat("float", 0));
     33         assertEquals(123, preferences.getInt("int", 0));
     34         assertEquals(456, preferences.getLong("long", 0));
     35         assertEquals("foo", preferences.getString("string", null));
     36         assertEquals(new ArraySet<>(Arrays.asList("bar", "baz")),
     37                 preferences.getStringSet("stringset", null));
     38     }
     39 
     40     public void testReadDefault() {
     41         VisualVoicemailPreferences preferences = new VisualVoicemailPreferences(getContext(),
     42                 createFakeHandle("testReadDefault"));
     43 
     44         assertFalse(preferences.contains("boolean"));
     45         assertFalse(preferences.contains("float"));
     46         assertFalse(preferences.contains("int"));
     47         assertFalse(preferences.contains("long"));
     48         assertFalse(preferences.contains("string"));
     49         assertFalse(preferences.contains("stringset"));
     50 
     51         assertEquals(true, preferences.getBoolean("boolean", true));
     52         assertEquals(2.5f, preferences.getFloat("float", 2.5f));
     53         assertEquals(321, preferences.getInt("int", 321));
     54         assertEquals(654, preferences.getLong("long", 654));
     55         assertEquals("foo2", preferences.getString("string", "foo2"));
     56         assertEquals(new ArraySet<>(Arrays.asList("bar2", "baz2")),
     57                 preferences.getStringSet(
     58                         "stringset", new ArraySet<>(Arrays.asList("bar2", "baz2"))));
     59     }
     60 
     61     public void testReadDefaultNull() {
     62         VisualVoicemailPreferences preferences = new VisualVoicemailPreferences(getContext(),
     63                 createFakeHandle("testReadDefaultNull"));
     64         assertNull(preferences.getString("string", null));
     65         assertNull(preferences.getStringSet("stringset", null));
     66     }
     67 
     68     public void testDifferentHandle() {
     69         VisualVoicemailPreferences preferences1 = new VisualVoicemailPreferences(getContext(),
     70                 createFakeHandle("testDifferentHandle1"));
     71         VisualVoicemailPreferences preferences2 = new VisualVoicemailPreferences(getContext(),
     72                 createFakeHandle("testDifferentHandle1"));
     73 
     74         preferences1.edit().putString("string", "foo");
     75         assertFalse(preferences2.contains("string"));
     76     }
     77 
     78     private PhoneAccountHandle createFakeHandle(String id) {
     79         return new PhoneAccountHandle(new ComponentName(getContext(), this.getClass()), id);
     80     }
     81 }
     82