|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 package org.mozilla.gecko.background.sync; |
|
5 |
|
6 import java.util.HashMap; |
|
7 import java.util.HashSet; |
|
8 import java.util.Map; |
|
9 import java.util.Set; |
|
10 |
|
11 import org.mozilla.gecko.background.helpers.AndroidSyncTestCase; |
|
12 import org.mozilla.gecko.sync.SyncConfiguration; |
|
13 |
|
14 import android.content.SharedPreferences; |
|
15 |
|
16 public class TestSyncConfiguration extends AndroidSyncTestCase { |
|
17 public static final String TEST_PREFS_NAME = "test"; |
|
18 |
|
19 public SharedPreferences getPrefs(String name, int mode) { |
|
20 return this.getApplicationContext().getSharedPreferences(name, mode); |
|
21 } |
|
22 |
|
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); |
|
29 |
|
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); |
|
42 |
|
43 config.declinedEngineNames = null; |
|
44 config.persistToPrefs(); |
|
45 assertFalse(prefs.contains(SyncConfiguration.PREF_DECLINED_ENGINE_NAMES)); |
|
46 config = newSyncConfiguration(); |
|
47 assertNull(config.declinedEngineNames); |
|
48 } |
|
49 |
|
50 public void testEnabledEngineNames() { |
|
51 SyncConfiguration config = null; |
|
52 SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0); |
|
53 |
|
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); |
|
66 |
|
67 config.enabledEngineNames = null; |
|
68 config.persistToPrefs(); |
|
69 assertFalse(prefs.contains(SyncConfiguration.PREF_ENABLED_ENGINE_NAMES)); |
|
70 config = newSyncConfiguration(); |
|
71 assertNull(config.enabledEngineNames); |
|
72 } |
|
73 |
|
74 public void testSyncID() { |
|
75 SyncConfiguration config = null; |
|
76 SharedPreferences prefs = getPrefs(TEST_PREFS_NAME, 0); |
|
77 |
|
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 } |
|
85 |
|
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); |
|
93 |
|
94 SyncConfiguration.storeSelectedEnginesToPrefs(prefs, expectedEngines); |
|
95 |
|
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 } |
|
103 |
|
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); |
|
112 |
|
113 SyncConfiguration.storeSelectedEnginesToPrefs(prefs, storedEngines); |
|
114 |
|
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 } |
|
124 |
|
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); |
|
130 |
|
131 SyncConfiguration.storeSelectedEnginesToPrefs(prefs, storedEngines); |
|
132 |
|
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 } |
|
141 |
|
142 protected SyncConfiguration newSyncConfiguration() { |
|
143 return new SyncConfiguration(null, null, getPrefs(TEST_PREFS_NAME, 0)); |
|
144 } |
|
145 } |