michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: package org.mozilla.gecko.background.sync; michael@0: michael@0: import java.util.HashMap; michael@0: import java.util.HashSet; michael@0: import java.util.Map; michael@0: import java.util.Set; michael@0: michael@0: import org.mozilla.gecko.background.helpers.AndroidSyncTestCase; michael@0: import org.mozilla.gecko.sync.SyncConfiguration; michael@0: michael@0: import android.content.SharedPreferences; michael@0: michael@0: public class TestSyncConfiguration extends AndroidSyncTestCase { michael@0: public static final String TEST_PREFS_NAME = "test"; michael@0: michael@0: public SharedPreferences getPrefs(String name, int mode) { michael@0: return this.getApplicationContext().getSharedPreferences(name, mode); michael@0: } michael@0: michael@0: /** michael@0: * Ensure that declined engines persist through prefs. michael@0: */ michael@0: public void testDeclinedEngineNames() { michael@0: SyncConfiguration config = null; michael@0: SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0); michael@0: michael@0: config = newSyncConfiguration(); michael@0: config.declinedEngineNames = new HashSet(); michael@0: config.declinedEngineNames.add("test1"); michael@0: config.declinedEngineNames.add("test2"); michael@0: config.persistToPrefs(); michael@0: assertTrue(prefs.contains(SyncConfiguration.PREF_DECLINED_ENGINE_NAMES)); michael@0: config = newSyncConfiguration(); michael@0: Set expected = new HashSet(); michael@0: for (String name : new String[] { "test1", "test2" }) { michael@0: expected.add(name); michael@0: } michael@0: assertEquals(expected, config.declinedEngineNames); michael@0: michael@0: config.declinedEngineNames = null; michael@0: config.persistToPrefs(); michael@0: assertFalse(prefs.contains(SyncConfiguration.PREF_DECLINED_ENGINE_NAMES)); michael@0: config = newSyncConfiguration(); michael@0: assertNull(config.declinedEngineNames); michael@0: } michael@0: michael@0: public void testEnabledEngineNames() { michael@0: SyncConfiguration config = null; michael@0: SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0); michael@0: michael@0: config = newSyncConfiguration(); michael@0: config.enabledEngineNames = new HashSet(); michael@0: config.enabledEngineNames.add("test1"); michael@0: config.enabledEngineNames.add("test2"); michael@0: config.persistToPrefs(); michael@0: assertTrue(prefs.contains(SyncConfiguration.PREF_ENABLED_ENGINE_NAMES)); michael@0: config = newSyncConfiguration(); michael@0: Set expected = new HashSet(); michael@0: for (String name : new String[] { "test1", "test2" }) { michael@0: expected.add(name); michael@0: } michael@0: assertEquals(expected, config.enabledEngineNames); michael@0: michael@0: config.enabledEngineNames = null; michael@0: config.persistToPrefs(); michael@0: assertFalse(prefs.contains(SyncConfiguration.PREF_ENABLED_ENGINE_NAMES)); michael@0: config = newSyncConfiguration(); michael@0: assertNull(config.enabledEngineNames); michael@0: } michael@0: michael@0: public void testSyncID() { michael@0: SyncConfiguration config = null; michael@0: SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0); michael@0: michael@0: config = newSyncConfiguration(); michael@0: config.syncID = "test1"; michael@0: config.persistToPrefs(); michael@0: assertTrue(prefs.contains(SyncConfiguration.PREF_SYNC_ID)); michael@0: config = newSyncConfiguration(); michael@0: assertEquals("test1", config.syncID); michael@0: } michael@0: michael@0: public void testStoreSelectedEnginesToPrefs() { michael@0: SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0); michael@0: // Store engines, excluding history/forms special case. michael@0: Map expectedEngines = new HashMap(); michael@0: expectedEngines.put("test1", true); michael@0: expectedEngines.put("test2", false); michael@0: expectedEngines.put("test3", true); michael@0: michael@0: SyncConfiguration.storeSelectedEnginesToPrefs(prefs, expectedEngines); michael@0: michael@0: // Read values from selectedEngines. michael@0: assertTrue(prefs.contains(SyncConfiguration.PREF_USER_SELECTED_ENGINES_TO_SYNC)); michael@0: SyncConfiguration config = null; michael@0: config = newSyncConfiguration(); michael@0: config.loadFromPrefs(prefs); michael@0: assertEquals(expectedEngines, config.userSelectedEngines); michael@0: } michael@0: michael@0: /** michael@0: * Tests dependency of forms engine on history engine. michael@0: */ michael@0: public void testSelectedEnginesHistoryAndForms() { michael@0: SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0); michael@0: // Store engines, excluding history/forms special case. michael@0: Map storedEngines = new HashMap(); michael@0: storedEngines.put("history", true); michael@0: michael@0: SyncConfiguration.storeSelectedEnginesToPrefs(prefs, storedEngines); michael@0: michael@0: // Expected engines. michael@0: storedEngines.put("forms", true); michael@0: // Read values from selectedEngines. michael@0: assertTrue(prefs.contains(SyncConfiguration.PREF_USER_SELECTED_ENGINES_TO_SYNC)); michael@0: SyncConfiguration config = null; michael@0: config = newSyncConfiguration(); michael@0: config.loadFromPrefs(prefs); michael@0: assertEquals(storedEngines, config.userSelectedEngines); michael@0: } michael@0: michael@0: public void testsSelectedEnginesNoHistoryNorForms() { michael@0: SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0); michael@0: // Store engines, excluding history/forms special case. michael@0: Map storedEngines = new HashMap(); michael@0: storedEngines.put("forms", true); michael@0: michael@0: SyncConfiguration.storeSelectedEnginesToPrefs(prefs, storedEngines); michael@0: michael@0: // Read values from selectedEngines. michael@0: assertTrue(prefs.contains(SyncConfiguration.PREF_USER_SELECTED_ENGINES_TO_SYNC)); michael@0: SyncConfiguration config = null; michael@0: config = newSyncConfiguration(); michael@0: config.loadFromPrefs(prefs); michael@0: // Forms should not be selected if history is not present. michael@0: assertTrue(config.userSelectedEngines.isEmpty()); michael@0: } michael@0: michael@0: protected SyncConfiguration newSyncConfiguration() { michael@0: return new SyncConfiguration(null, null, getPrefs(TEST_PREFS_NAME, 0)); michael@0: } michael@0: }