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