Skip to content

Commit a711810

Browse files
committed
Fixing issue 123, adding long prefs and related functional tests
1 parent b02b81b commit a711810

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/SharedPrefProcessor.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.googlecode.androidannotations.annotations.sharedpreferences.DefaultBoolean;
3131
import com.googlecode.androidannotations.annotations.sharedpreferences.DefaultFloat;
3232
import com.googlecode.androidannotations.annotations.sharedpreferences.DefaultInt;
33+
import com.googlecode.androidannotations.annotations.sharedpreferences.DefaultLong;
3334
import com.googlecode.androidannotations.annotations.sharedpreferences.DefaultString;
3435
import com.googlecode.androidannotations.annotations.sharedpreferences.SharedPref;
3536
import com.googlecode.androidannotations.annotations.sharedpreferences.SharedPref.Scope;
@@ -41,6 +42,7 @@
4142
import com.googlecode.androidannotations.api.sharedpreferences.IntPrefEditorField;
4243
import com.googlecode.androidannotations.api.sharedpreferences.IntPrefField;
4344
import com.googlecode.androidannotations.api.sharedpreferences.LongPrefEditorField;
45+
import com.googlecode.androidannotations.api.sharedpreferences.LongPrefField;
4446
import com.googlecode.androidannotations.api.sharedpreferences.SharedPreferencesHelper;
4547
import com.googlecode.androidannotations.api.sharedpreferences.StringPrefEditorField;
4648
import com.googlecode.androidannotations.api.sharedpreferences.StringPrefField;
@@ -193,8 +195,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder activiti
193195
defaultValue = JExpr.lit(false);
194196
}
195197
addFieldHelperMethod(helperClass, fieldName, defaultValue, BooleanPrefField.class, "booleanField");
196-
}
197-
if ("float".equals(returnType)) {
198+
} else if ("float".equals(returnType)) {
198199
JExpression defaultValue;
199200
DefaultFloat defaultAnnotation = method.getAnnotation(DefaultFloat.class);
200201
if (defaultAnnotation != null) {
@@ -203,8 +204,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder activiti
203204
defaultValue = JExpr.lit(0f);
204205
}
205206
addFieldHelperMethod(helperClass, fieldName, defaultValue, FloatPrefField.class, "floatField");
206-
}
207-
if ("int".equals(returnType)) {
207+
} else if ("int".equals(returnType)) {
208208
JExpression defaultValue;
209209
DefaultInt defaultAnnotation = method.getAnnotation(DefaultInt.class);
210210
if (defaultAnnotation != null) {
@@ -213,8 +213,16 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder activiti
213213
defaultValue = JExpr.lit(0);
214214
}
215215
addFieldHelperMethod(helperClass, fieldName, defaultValue, IntPrefField.class, "intField");
216-
}
217-
if ("java.lang.String".equals(returnType)) {
216+
} else if ("long".equals(returnType)) {
217+
JExpression defaultValue;
218+
DefaultLong defaultAnnotation = method.getAnnotation(DefaultLong.class);
219+
if (defaultAnnotation != null) {
220+
defaultValue = JExpr.lit(defaultAnnotation.value());
221+
} else {
222+
defaultValue = JExpr.lit(0l);
223+
}
224+
addFieldHelperMethod(helperClass, fieldName, defaultValue, LongPrefField.class, "longField");
225+
} else if ("java.lang.String".equals(returnType)) {
218226
JExpression defaultValue;
219227
DefaultString defaultAnnotation = method.getAnnotation(DefaultString.class);
220228
if (defaultAnnotation != null) {

AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/prefs/SomePrefs.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ public interface SomePrefs {
2828

2929
@DefaultInt(42)
3030
int age();
31+
32+
long lastUpdated();
33+
3134
}

AndroidAnnotations/functional-test-1-5/src/test/java/com/googlecode/androidannotations/test15/prefs/PrefsActivityTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ public void putInt() {
6262
somePrefs.age().put(42);
6363
assertThat(sharedPref.getInt("age", 0)).isEqualTo(42);
6464
}
65+
66+
@Test
67+
public void putLong() {
68+
long now = System.currentTimeMillis();
69+
somePrefs.lastUpdated().put(now);
70+
assertThat(sharedPref.getLong("lastUpdated", 0)).isEqualTo(now);
71+
}
72+
73+
@Test
74+
public void editLong() {
75+
long now = System.currentTimeMillis();
76+
77+
somePrefs.edit().lastUpdated().put(now).apply();
78+
79+
assertThat(sharedPref.getLong("lastUpdated", 0)).isEqualTo(now);
80+
}
6581

6682
@Test
6783
public void putTwoValuesChained() {
@@ -122,6 +138,15 @@ public void getString() {
122138

123139
assertThat(somePrefs.name().get()).isEqualTo("Something");
124140
}
141+
142+
@Test
143+
public void getLong() {
144+
long now = System.currentTimeMillis();
145+
146+
sharedPref.edit().putLong("lastUpdated", now).commit();
147+
148+
assertThat(somePrefs.lastUpdated().get()).isEqualTo(now);
149+
}
125150

126151
@Test
127152
public void defaultValue() {

0 commit comments

Comments
 (0)