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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/tests/background/junit3/src/sync/TestSyncConfiguration.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,145 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +package org.mozilla.gecko.background.sync;
     1.8 +
     1.9 +import java.util.HashMap;
    1.10 +import java.util.HashSet;
    1.11 +import java.util.Map;
    1.12 +import java.util.Set;
    1.13 +
    1.14 +import org.mozilla.gecko.background.helpers.AndroidSyncTestCase;
    1.15 +import org.mozilla.gecko.sync.SyncConfiguration;
    1.16 +
    1.17 +import android.content.SharedPreferences;
    1.18 +
    1.19 +public class TestSyncConfiguration extends AndroidSyncTestCase {
    1.20 +  public static final String TEST_PREFS_NAME = "test";
    1.21 +
    1.22 +  public SharedPreferences getPrefs(String name, int mode) {
    1.23 +    return this.getApplicationContext().getSharedPreferences(name, mode);
    1.24 +  }
    1.25 +
    1.26 +  /**
    1.27 +   * Ensure that declined engines persist through prefs.
    1.28 +   */
    1.29 +  public void testDeclinedEngineNames() {
    1.30 +    SyncConfiguration config = null;
    1.31 +    SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0);
    1.32 +
    1.33 +    config = newSyncConfiguration();
    1.34 +    config.declinedEngineNames = new HashSet<String>();
    1.35 +    config.declinedEngineNames.add("test1");
    1.36 +    config.declinedEngineNames.add("test2");
    1.37 +    config.persistToPrefs();
    1.38 +    assertTrue(prefs.contains(SyncConfiguration.PREF_DECLINED_ENGINE_NAMES));
    1.39 +    config = newSyncConfiguration();
    1.40 +    Set<String> expected = new HashSet<String>();
    1.41 +    for (String name : new String[] { "test1", "test2" }) {
    1.42 +      expected.add(name);
    1.43 +    }
    1.44 +    assertEquals(expected, config.declinedEngineNames);
    1.45 +
    1.46 +    config.declinedEngineNames = null;
    1.47 +    config.persistToPrefs();
    1.48 +    assertFalse(prefs.contains(SyncConfiguration.PREF_DECLINED_ENGINE_NAMES));
    1.49 +    config = newSyncConfiguration();
    1.50 +    assertNull(config.declinedEngineNames);
    1.51 +  }
    1.52 +
    1.53 +  public void testEnabledEngineNames() {
    1.54 +    SyncConfiguration config = null;
    1.55 +    SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0);
    1.56 +
    1.57 +    config = newSyncConfiguration();
    1.58 +    config.enabledEngineNames = new HashSet<String>();
    1.59 +    config.enabledEngineNames.add("test1");
    1.60 +    config.enabledEngineNames.add("test2");
    1.61 +    config.persistToPrefs();
    1.62 +    assertTrue(prefs.contains(SyncConfiguration.PREF_ENABLED_ENGINE_NAMES));
    1.63 +    config = newSyncConfiguration();
    1.64 +    Set<String> expected = new HashSet<String>();
    1.65 +    for (String name : new String[] { "test1", "test2" }) {
    1.66 +      expected.add(name);
    1.67 +    }
    1.68 +    assertEquals(expected, config.enabledEngineNames);
    1.69 +
    1.70 +    config.enabledEngineNames = null;
    1.71 +    config.persistToPrefs();
    1.72 +    assertFalse(prefs.contains(SyncConfiguration.PREF_ENABLED_ENGINE_NAMES));
    1.73 +    config = newSyncConfiguration();
    1.74 +    assertNull(config.enabledEngineNames);
    1.75 +  }
    1.76 +
    1.77 +  public void testSyncID() {
    1.78 +    SyncConfiguration config = null;
    1.79 +    SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0);
    1.80 +
    1.81 +    config = newSyncConfiguration();
    1.82 +    config.syncID = "test1";
    1.83 +    config.persistToPrefs();
    1.84 +    assertTrue(prefs.contains(SyncConfiguration.PREF_SYNC_ID));
    1.85 +    config = newSyncConfiguration();
    1.86 +    assertEquals("test1", config.syncID);
    1.87 +  }
    1.88 +
    1.89 +  public void testStoreSelectedEnginesToPrefs() {
    1.90 +    SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0);
    1.91 +    // Store engines, excluding history/forms special case.
    1.92 +    Map<String, Boolean> expectedEngines = new HashMap<String, Boolean>();
    1.93 +    expectedEngines.put("test1", true);
    1.94 +    expectedEngines.put("test2", false);
    1.95 +    expectedEngines.put("test3", true);
    1.96 +
    1.97 +    SyncConfiguration.storeSelectedEnginesToPrefs(prefs, expectedEngines);
    1.98 +
    1.99 +    // Read values from selectedEngines.
   1.100 +    assertTrue(prefs.contains(SyncConfiguration.PREF_USER_SELECTED_ENGINES_TO_SYNC));
   1.101 +    SyncConfiguration config = null;
   1.102 +    config = newSyncConfiguration();
   1.103 +    config.loadFromPrefs(prefs);
   1.104 +    assertEquals(expectedEngines, config.userSelectedEngines);
   1.105 +  }
   1.106 +
   1.107 +  /**
   1.108 +   * Tests dependency of forms engine on history engine.
   1.109 +   */
   1.110 +  public void testSelectedEnginesHistoryAndForms() {
   1.111 +    SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0);
   1.112 +    // Store engines, excluding history/forms special case.
   1.113 +    Map<String, Boolean> storedEngines = new HashMap<String, Boolean>();
   1.114 +    storedEngines.put("history", true);
   1.115 +
   1.116 +    SyncConfiguration.storeSelectedEnginesToPrefs(prefs, storedEngines);
   1.117 +
   1.118 +    // Expected engines.
   1.119 +    storedEngines.put("forms", true);
   1.120 +    // Read values from selectedEngines.
   1.121 +    assertTrue(prefs.contains(SyncConfiguration.PREF_USER_SELECTED_ENGINES_TO_SYNC));
   1.122 +    SyncConfiguration config = null;
   1.123 +    config = newSyncConfiguration();
   1.124 +    config.loadFromPrefs(prefs);
   1.125 +    assertEquals(storedEngines, config.userSelectedEngines);
   1.126 +  }
   1.127 +
   1.128 +  public void testsSelectedEnginesNoHistoryNorForms() {
   1.129 +    SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0);
   1.130 +    // Store engines, excluding history/forms special case.
   1.131 +    Map<String, Boolean> storedEngines = new HashMap<String, Boolean>();
   1.132 +    storedEngines.put("forms", true);
   1.133 +
   1.134 +    SyncConfiguration.storeSelectedEnginesToPrefs(prefs, storedEngines);
   1.135 +
   1.136 +    // Read values from selectedEngines.
   1.137 +    assertTrue(prefs.contains(SyncConfiguration.PREF_USER_SELECTED_ENGINES_TO_SYNC));
   1.138 +    SyncConfiguration config = null;
   1.139 +    config = newSyncConfiguration();
   1.140 +    config.loadFromPrefs(prefs);
   1.141 +    // Forms should not be selected if history is not present.
   1.142 +    assertTrue(config.userSelectedEngines.isEmpty());
   1.143 +  }
   1.144 +
   1.145 +  protected SyncConfiguration newSyncConfiguration() {
   1.146 +    return new SyncConfiguration(null, null, getPrefs(TEST_PREFS_NAME, 0));
   1.147 +  }
   1.148 +}

mercurial