Skip to content

Commit

Permalink
Ensuring SharedPrefs.context gets set during initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Inderjeet Singh committed Jul 29, 2018
1 parent 0fa25e4 commit f6d85dc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repositories {
}
```

In your app build.gradle, add: `compile "com.github.PeelTechnologies:android-typesafe-prefs:1.1.4"`
In your app build.gradle, add: `compile "com.github.PeelTechnologies:android-typesafe-prefs:1.1.5"`

# User Guide
TypedKey can take arbitrarily complex Java object that Gson can serialize/deserialize. For example, `TypedKey<Customer>` may represent a class with nested fields for `Address`, name, phone numbers, etc.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.github.PeelTechnologies</groupId>
<artifactId>android-typesafe-prefs</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/peel/prefs/SharedPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static void init(Context context, Gson gson, String prefsFileName, int ma
public synchronized static void init(Prefs defaultPrefs, Prefs... prefsList) {
Objects.requireNonNull(defaultPrefs);
SharedPrefs.defaultPrefs = defaultPrefs;
SharedPrefs.context = defaultPrefs.context();
if (prefsList != null) {
String defaultPrefsFileName = defaultPrefs.getPrefsFileName(); // can be null
for (Prefs prefs : prefsList) {
Expand Down
18 changes: 16 additions & 2 deletions src/test/java/com/peel/prefs/SharedPrefsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.peel.prefs;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

Expand All @@ -39,10 +40,10 @@
@PrepareForTest({Context.class, SharedPreferences.class, PreferenceManager.class})
public class SharedPrefsTest {

private static final Gson gson = new Gson();
private final Gson gson = new Gson();

@Test
public void testInitWIthCustomDefaultProps() {
public void testInitWithCustomDefaultProps() {
Context context = AndroidFixtures.createMockContext(null, "my_props");
SharedPrefs.TestAccess.init(context, gson, "my_props", 3);
SharedPrefs.put("userId", String.class, "1999999");
Expand All @@ -61,6 +62,19 @@ public void getPutWithKeyNameAndClass() {
assertNull(SharedPrefs.get("userId", String.class));
}

@Test
public void testInitContext() {
Context context = AndroidFixtures.createMockContext(null, "my_props", "my_props1");
SharedPrefs.TestAccess.init(context, gson);
assertNotNull(SharedPrefs.context());

SharedPrefs.TestAccess.init(context, gson, "my_props", 3);
assertNotNull(SharedPrefs.context());

SharedPrefs.TestAccess.init(new Prefs(context, gson, "my_props", 2));
assertNotNull(SharedPrefs.context());
}

@Test
public void testInitWithNonNullDefaultPrefs() {
Context context = AndroidFixtures.createMockContext(null, "my_props1", "my_props2");
Expand Down

0 comments on commit f6d85dc

Please sign in to comment.