Skip to content

Commit f8fbd67

Browse files
authored
(For David) Pass config down from open (#57)
* Pass down config from open * Adds test * Only update config if null * Add fallback test * Addresses PR feedback * Moves appStateCollector back out
1 parent c3bdf1f commit f8fbd67

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

sift/src/main/java/siftscience/android/Sift.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,15 @@ public static void open(@NonNull Context context,
7070
appStateCollector = new AppStateCollector(instance, c);
7171
unboundUserId = null;
7272
hasUnboundUserId = false;
73+
} else {
74+
if (config != null) {
75+
instance.setConfig(config);
76+
}
7377
}
7478
}
7579

7680
appStateCollector.setActivityName(activityName == null ?
7781
context.getClass().getSimpleName() : activityName);
78-
7982
}
8083

8184
public static void open(@NonNull Context context, String activityName) {

sift/src/main/java/siftscience/android/SiftImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,19 +271,19 @@ public UnarchiveTask(boolean hasUnboundUserId) {
271271
public void run() {
272272
String archive;
273273

274-
// Unarchive Sift config
275-
archive = archives.getString(ArchiveKey.CONFIG.key, null);
276-
config = unarchiveConfig(archive);
277-
Log.d(TAG, String.format("Unarchived Sift.Config: %s", archive));
274+
// Unarchive Sift config if we don't have one
275+
if (config == null) {
276+
archive = archives.getString(ArchiveKey.CONFIG.key, null);
277+
config = unarchiveConfig(archive);
278+
Log.d(TAG, String.format("Unarchived Sift.Config: %s", archive));
279+
}
278280

279281
// Unarchive User ID if we didn't have an unbound one from the Sift class
280282
if (!this.hasUnboundUserId) {
281283
userId = archives.getString(ArchiveKey.USER_ID.key, null);
282284
Log.d(TAG, String.format("Unarchived User ID: %s", userId));
283285
}
284286

285-
286-
287287
// Unarchive Queues
288288
for (Map.Entry<String, ?> entry : archives.getAll().entrySet()) {
289289
String identifier = ArchiveKey.getQueueIdentifier(entry.getKey());

sift/src/test/java/siftscience/android/SiftTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,43 @@ public void testSift() throws Exception {
209209
assertTrue(preferences.fields.isEmpty());
210210
}
211211

212+
@Test
213+
public void testConfigPrecedence() {
214+
MemorySharedPreferences preferences = new MemorySharedPreferences();
215+
216+
Sift.Config c1 = new Sift.Config.Builder().withAccountId("sandbox").build();
217+
Sift.Config c2 = new Sift.Config.Builder().withAccountId("prod").build();
218+
219+
MemorySharedPreferences.Editor editor = preferences.edit();
220+
editor.putString("config", Sift.GSON.toJson(c1));
221+
editor.apply();
222+
223+
SiftImpl sift = new SiftImpl(
224+
mockContext(preferences), c2, "", false, mockTaskManager());
225+
226+
assertEquals(c2.accountId, sift.getConfig().accountId);
227+
228+
sift.setConfig(c1);
229+
230+
assertEquals(c1.accountId, sift.getConfig().accountId);
231+
}
232+
233+
@Test
234+
public void testConfigFallback() {
235+
MemorySharedPreferences preferences = new MemorySharedPreferences();
236+
237+
Sift.Config c1 = new Sift.Config.Builder().withAccountId("sandbox").build();
238+
239+
MemorySharedPreferences.Editor editor = preferences.edit();
240+
editor.putString("config", Sift.GSON.toJson(c1));
241+
editor.apply();
242+
243+
SiftImpl sift = new SiftImpl(
244+
mockContext(preferences), null, "", false, mockTaskManager());
245+
246+
assertEquals(c1.accountId, sift.getConfig().accountId);
247+
}
248+
212249
@Test
213250
public void testUnarchiveUnknownProperty() throws IOException {
214251
String jsonAsString =

0 commit comments

Comments
 (0)