mobile/android/tests/background/junit3/src/sync/TestSyncConfiguration.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 package org.mozilla.gecko.background.sync;
     6 import java.util.HashMap;
     7 import java.util.HashSet;
     8 import java.util.Map;
     9 import java.util.Set;
    11 import org.mozilla.gecko.background.helpers.AndroidSyncTestCase;
    12 import org.mozilla.gecko.sync.SyncConfiguration;
    14 import android.content.SharedPreferences;
    16 public class TestSyncConfiguration extends AndroidSyncTestCase {
    17   public static final String TEST_PREFS_NAME = "test";
    19   public SharedPreferences getPrefs(String name, int mode) {
    20     return this.getApplicationContext().getSharedPreferences(name, mode);
    21   }
    23   /**
    24    * Ensure that declined engines persist through prefs.
    25    */
    26   public void testDeclinedEngineNames() {
    27     SyncConfiguration config = null;
    28     SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0);
    30     config = newSyncConfiguration();
    31     config.declinedEngineNames = new HashSet<String>();
    32     config.declinedEngineNames.add("test1");
    33     config.declinedEngineNames.add("test2");
    34     config.persistToPrefs();
    35     assertTrue(prefs.contains(SyncConfiguration.PREF_DECLINED_ENGINE_NAMES));
    36     config = newSyncConfiguration();
    37     Set<String> expected = new HashSet<String>();
    38     for (String name : new String[] { "test1", "test2" }) {
    39       expected.add(name);
    40     }
    41     assertEquals(expected, config.declinedEngineNames);
    43     config.declinedEngineNames = null;
    44     config.persistToPrefs();
    45     assertFalse(prefs.contains(SyncConfiguration.PREF_DECLINED_ENGINE_NAMES));
    46     config = newSyncConfiguration();
    47     assertNull(config.declinedEngineNames);
    48   }
    50   public void testEnabledEngineNames() {
    51     SyncConfiguration config = null;
    52     SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0);
    54     config = newSyncConfiguration();
    55     config.enabledEngineNames = new HashSet<String>();
    56     config.enabledEngineNames.add("test1");
    57     config.enabledEngineNames.add("test2");
    58     config.persistToPrefs();
    59     assertTrue(prefs.contains(SyncConfiguration.PREF_ENABLED_ENGINE_NAMES));
    60     config = newSyncConfiguration();
    61     Set<String> expected = new HashSet<String>();
    62     for (String name : new String[] { "test1", "test2" }) {
    63       expected.add(name);
    64     }
    65     assertEquals(expected, config.enabledEngineNames);
    67     config.enabledEngineNames = null;
    68     config.persistToPrefs();
    69     assertFalse(prefs.contains(SyncConfiguration.PREF_ENABLED_ENGINE_NAMES));
    70     config = newSyncConfiguration();
    71     assertNull(config.enabledEngineNames);
    72   }
    74   public void testSyncID() {
    75     SyncConfiguration config = null;
    76     SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0);
    78     config = newSyncConfiguration();
    79     config.syncID = "test1";
    80     config.persistToPrefs();
    81     assertTrue(prefs.contains(SyncConfiguration.PREF_SYNC_ID));
    82     config = newSyncConfiguration();
    83     assertEquals("test1", config.syncID);
    84   }
    86   public void testStoreSelectedEnginesToPrefs() {
    87     SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0);
    88     // Store engines, excluding history/forms special case.
    89     Map<String, Boolean> expectedEngines = new HashMap<String, Boolean>();
    90     expectedEngines.put("test1", true);
    91     expectedEngines.put("test2", false);
    92     expectedEngines.put("test3", true);
    94     SyncConfiguration.storeSelectedEnginesToPrefs(prefs, expectedEngines);
    96     // Read values from selectedEngines.
    97     assertTrue(prefs.contains(SyncConfiguration.PREF_USER_SELECTED_ENGINES_TO_SYNC));
    98     SyncConfiguration config = null;
    99     config = newSyncConfiguration();
   100     config.loadFromPrefs(prefs);
   101     assertEquals(expectedEngines, config.userSelectedEngines);
   102   }
   104   /**
   105    * Tests dependency of forms engine on history engine.
   106    */
   107   public void testSelectedEnginesHistoryAndForms() {
   108     SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0);
   109     // Store engines, excluding history/forms special case.
   110     Map<String, Boolean> storedEngines = new HashMap<String, Boolean>();
   111     storedEngines.put("history", true);
   113     SyncConfiguration.storeSelectedEnginesToPrefs(prefs, storedEngines);
   115     // Expected engines.
   116     storedEngines.put("forms", true);
   117     // Read values from selectedEngines.
   118     assertTrue(prefs.contains(SyncConfiguration.PREF_USER_SELECTED_ENGINES_TO_SYNC));
   119     SyncConfiguration config = null;
   120     config = newSyncConfiguration();
   121     config.loadFromPrefs(prefs);
   122     assertEquals(storedEngines, config.userSelectedEngines);
   123   }
   125   public void testsSelectedEnginesNoHistoryNorForms() {
   126     SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0);
   127     // Store engines, excluding history/forms special case.
   128     Map<String, Boolean> storedEngines = new HashMap<String, Boolean>();
   129     storedEngines.put("forms", true);
   131     SyncConfiguration.storeSelectedEnginesToPrefs(prefs, storedEngines);
   133     // Read values from selectedEngines.
   134     assertTrue(prefs.contains(SyncConfiguration.PREF_USER_SELECTED_ENGINES_TO_SYNC));
   135     SyncConfiguration config = null;
   136     config = newSyncConfiguration();
   137     config.loadFromPrefs(prefs);
   138     // Forms should not be selected if history is not present.
   139     assertTrue(config.userSelectedEngines.isEmpty());
   140   }
   142   protected SyncConfiguration newSyncConfiguration() {
   143     return new SyncConfiguration(null, null, getPrefs(TEST_PREFS_NAME, 0));
   144   }
   145 }

mercurial