From 50b99379e2b0fe804748b095577c0483f0358a63 Mon Sep 17 00:00:00 2001 From: "Y.Hirano" Date: Mon, 25 Apr 2016 23:25:42 +0900 Subject: [PATCH 1/2] Remove app test. --- .../os/operando/garum/sample/ApplicationTest.java | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100755 app/src/androidTest/java/com/os/operando/garum/sample/ApplicationTest.java diff --git a/app/src/androidTest/java/com/os/operando/garum/sample/ApplicationTest.java b/app/src/androidTest/java/com/os/operando/garum/sample/ApplicationTest.java deleted file mode 100755 index 0ef4a5a..0000000 --- a/app/src/androidTest/java/com/os/operando/garum/sample/ApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.os.operando.garum.sample; - -import android.app.Application; -import android.test.ApplicationTestCase; - -/** - * Testing Fundamentals - */ -public class ApplicationTest extends ApplicationTestCase { - public ApplicationTest() { - super(Application.class); - } -} \ No newline at end of file From f12a0d7257ce88060d4c67d9955d51faeb454ddb Mon Sep 17 00:00:00 2001 From: "Y.Hirano" Date: Tue, 26 Apr 2016 00:51:11 +0900 Subject: [PATCH 2/2] Add some PrefModel tests. --- garum/build.gradle | 3 + .../os/operando/garum/ApplicationTest.java | 13 -- .../operando/garum/models/PrefModelTest1.java | 186 ++++++++++++++++++ .../operando/garum/models/PrefModelTest2.java | 167 ++++++++++++++++ .../operando/garum/models/PrefModelTest3.java | 112 +++++++++++ .../operando/garum/models/PrefModelTest4.java | 132 +++++++++++++ 6 files changed, 600 insertions(+), 13 deletions(-) delete mode 100755 garum/src/androidTest/java/com/os/operando/garum/ApplicationTest.java create mode 100644 garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest1.java create mode 100644 garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest2.java create mode 100644 garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest3.java create mode 100644 garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest4.java diff --git a/garum/build.gradle b/garum/build.gradle index 083bac2..e77bcca 100644 --- a/garum/build.gradle +++ b/garum/build.gradle @@ -27,4 +27,7 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('junit:junit:4.11') + androidTestCompile 'com.android.support.test:runner:0.5' + // Set this dependency to use JUnit 4 rules + androidTestCompile 'com.android.support.test:rules:0.5' } diff --git a/garum/src/androidTest/java/com/os/operando/garum/ApplicationTest.java b/garum/src/androidTest/java/com/os/operando/garum/ApplicationTest.java deleted file mode 100755 index 3e91397..0000000 --- a/garum/src/androidTest/java/com/os/operando/garum/ApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.os.operando.garum; - -import android.app.Application; -import android.test.ApplicationTestCase; - -/** - * Testing Fundamentals - */ -public class ApplicationTest extends ApplicationTestCase { - public ApplicationTest() { - super(Application.class); - } -} \ No newline at end of file diff --git a/garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest1.java b/garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest1.java new file mode 100644 index 0000000..20f5406 --- /dev/null +++ b/garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest1.java @@ -0,0 +1,186 @@ +package com.os.operando.garum.models; + +import android.app.Application; +import android.content.Context; +import android.content.SharedPreferences; +import android.support.test.runner.AndroidJUnit4; +import android.test.ApplicationTestCase; + +import com.os.operando.garum.Garum; +import com.os.operando.garum.annotations.Pref; +import com.os.operando.garum.annotations.PrefKey; + +import junit.framework.Assert; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +// Basic PrefModel test. +@RunWith(AndroidJUnit4.class) +public class PrefModelTest1 extends ApplicationTestCase { + + public PrefModelTest1() { + super(Application.class); + } + + @Before + public void setUp() throws Exception { + // Clear a model pref. + getContext().getSharedPreferences("model", Context.MODE_PRIVATE).edit().clear().commit(); + + Garum.initialize(getContext()); + } + + @After + public void tearDown() throws Exception { + // Clear a model pref. + getContext().getSharedPreferences("model", Context.MODE_PRIVATE).edit().clear().commit(); + } + + // Test of raw preferences. + @Test + public void test1() { + Model model = new Model(); + boolean result = model.save(); + Assert.assertTrue(result); // Check save + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertFalse(prefs.contains("stringValue")); // String value isn't exist if it's null. + Assert.assertTrue(prefs.contains("intValue")); + Assert.assertTrue(prefs.contains("floatValue")); + Assert.assertTrue(prefs.contains("longValue")); + Assert.assertTrue(prefs.contains("booleanValue")); + } + + // Test of #save method. + @Test + public void test2() { + Model model = new Model(); + model.stringValue = "keep it simple stupid."; + model.intValue = 34565; + model.floatValue = 543.22f; + model.longValue = 123432L; + model.booleanValue = true; + boolean result = model.save(); + Assert.assertTrue(result); // Check save + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertTrue(prefs.contains("stringValue")); + Assert.assertTrue(prefs.contains("intValue")); + Assert.assertTrue(prefs.contains("floatValue")); + Assert.assertTrue(prefs.contains("longValue")); + Assert.assertTrue(prefs.contains("booleanValue")); + + Assert.assertEquals("keep it simple stupid.", prefs.getString("stringValue", "DEFAULT")); + Assert.assertEquals(34565, prefs.getInt("intValue", 6503)); + Assert.assertEquals(543.22f, prefs.getFloat("floatValue", 204.3f)); + Assert.assertEquals(123432L, prefs.getLong("longValue", 3407L)); + Assert.assertEquals(true, prefs.getBoolean("booleanValue", false)); + } + + // Test of #apply method. + @Test + public void test3() { + Model model = new Model(); + model.stringValue = "keep it simple stupid."; + model.intValue = 34565; + model.floatValue = 543.22f; + model.longValue = 123432L; + model.booleanValue = true; + model.apply(); + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertTrue(prefs.contains("stringValue")); + Assert.assertTrue(prefs.contains("intValue")); + Assert.assertTrue(prefs.contains("floatValue")); + Assert.assertTrue(prefs.contains("longValue")); + Assert.assertTrue(prefs.contains("booleanValue")); + + Assert.assertEquals("keep it simple stupid.", prefs.getString("stringValue", "DEFAULT")); + Assert.assertEquals(34565, prefs.getInt("intValue", 6503)); + Assert.assertEquals(543.22f, prefs.getFloat("floatValue", 204.3f)); + Assert.assertEquals(123432L, prefs.getLong("longValue", 3407L)); + Assert.assertEquals(true, prefs.getBoolean("booleanValue", false)); + } + + // Test of load. + @Test + public void test4() { + Model model = new Model(); + model.stringValue = "keep it simple stupid."; + model.intValue = 34565; + model.floatValue = 543.22f; + model.longValue = 123432L; + model.booleanValue = true; + model.save(); + + // Load data + model = new Model(); + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertTrue(prefs.contains("stringValue")); + Assert.assertTrue(prefs.contains("intValue")); + Assert.assertTrue(prefs.contains("floatValue")); + Assert.assertTrue(prefs.contains("longValue")); + Assert.assertTrue(prefs.contains("booleanValue")); + + Assert.assertEquals("keep it simple stupid.", prefs.getString("stringValue", "DEFAULT")); + Assert.assertEquals(34565, prefs.getInt("intValue", 6503)); + Assert.assertEquals(543.22f, prefs.getFloat("floatValue", 204.3f)); + Assert.assertEquals(123432L, prefs.getLong("longValue", 3407L)); + Assert.assertEquals(true, prefs.getBoolean("booleanValue", false)); + + Assert.assertEquals("keep it simple stupid.", model.stringValue); + Assert.assertEquals(34565, model.intValue); + Assert.assertEquals(543.22f, model.floatValue); + Assert.assertEquals(123432L, model.longValue); + Assert.assertEquals(true, model.booleanValue); + } + + // Test of #clear method. + @Test + public void test5() { + Model model = new Model(); + model.stringValue = "keep it simple stupid."; + model.intValue = 34565; + model.floatValue = 543.22f; + model.longValue = 123432L; + model.booleanValue = true; + model.save(); + + model.clear(); + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertFalse(prefs.contains("stringValue")); + Assert.assertFalse(prefs.contains("intValue")); + Assert.assertFalse(prefs.contains("floatValue")); + Assert.assertFalse(prefs.contains("longValue")); + Assert.assertFalse(prefs.contains("booleanValue")); + + Assert.assertNull(model.stringValue); + Assert.assertEquals(0, model.intValue); + Assert.assertEquals(0f, model.floatValue); + Assert.assertEquals(0L, model.longValue); + Assert.assertEquals(false, model.booleanValue); + } + + @Pref(name = "model") + public static class Model extends PrefModel { + @PrefKey + public String stringValue; + + @PrefKey + public int intValue; + + @PrefKey + public float floatValue; + + @PrefKey + public long longValue; + + @PrefKey + public boolean booleanValue; + } +} \ No newline at end of file diff --git a/garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest2.java b/garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest2.java new file mode 100644 index 0000000..bd14656 --- /dev/null +++ b/garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest2.java @@ -0,0 +1,167 @@ +package com.os.operando.garum.models; + +import android.app.Application; +import android.content.Context; +import android.content.SharedPreferences; +import android.support.test.runner.AndroidJUnit4; +import android.test.ApplicationTestCase; + +import com.os.operando.garum.Garum; +import com.os.operando.garum.annotations.Pref; +import com.os.operando.garum.annotations.PrefKey; + +import junit.framework.Assert; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +// PrefName of PrefModel test. +@RunWith(AndroidJUnit4.class) +public class PrefModelTest2 extends ApplicationTestCase { + + public PrefModelTest2() { + super(Application.class); + } + + @Before + public void setUp() throws Exception { + // Clear a model pref. + getContext().getSharedPreferences("model", Context.MODE_PRIVATE).edit().clear().commit(); + + Garum.initialize(getContext()); + } + + @After + public void tearDown() throws Exception { + // Clear a model pref. + getContext().getSharedPreferences("model", Context.MODE_PRIVATE).edit().clear().commit(); + } + + // Test of raw preferences. + @Test + public void test1() { + Model model = new Model(); + boolean result = model.save(); + Assert.assertTrue(result); // Check save + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertFalse(prefs.contains("stringValue")); + Assert.assertFalse(prefs.contains("intValue")); + Assert.assertFalse(prefs.contains("floatValue")); + Assert.assertFalse(prefs.contains("longValue")); + Assert.assertFalse(prefs.contains("booleanValue")); + + Assert.assertFalse(prefs.contains("PrefA")); // String value isn't exist if it's null. + Assert.assertTrue(prefs.contains("PrefB")); + Assert.assertTrue(prefs.contains("PrefC")); + Assert.assertTrue(prefs.contains("PrefD")); + Assert.assertTrue(prefs.contains("PrefE")); + } + + // Test of raw preferences values. + @Test + public void test2() { + Model model = new Model(); + model.stringValue = "keep it simple stupid."; + model.intValue = 34565; + model.floatValue = 543.22f; + model.longValue = 123432L; + model.booleanValue = true; + boolean result = model.save(); + Assert.assertTrue(result); // Check save + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertTrue(prefs.contains("PrefA")); + Assert.assertTrue(prefs.contains("PrefB")); + Assert.assertTrue(prefs.contains("PrefC")); + Assert.assertTrue(prefs.contains("PrefD")); + Assert.assertTrue(prefs.contains("PrefE")); + + Assert.assertEquals("keep it simple stupid.", prefs.getString("PrefA", "DEFAULT")); + Assert.assertEquals(34565, prefs.getInt("PrefB", 6503)); + Assert.assertEquals(543.22f, prefs.getFloat("PrefC", 204.3f)); + Assert.assertEquals(123432L, prefs.getLong("PrefD", 3407L)); + Assert.assertEquals(true, prefs.getBoolean("PrefE", false)); + } + + // Test of load. + @Test + public void test3() { + Model model = new Model(); + model.stringValue = "keep it simple stupid."; + model.intValue = 34565; + model.floatValue = 543.22f; + model.longValue = 123432L; + model.booleanValue = true; + model.save(); + + // Load data + model = new Model(); + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertTrue(prefs.contains("PrefA")); + Assert.assertTrue(prefs.contains("PrefB")); + Assert.assertTrue(prefs.contains("PrefC")); + Assert.assertTrue(prefs.contains("PrefD")); + Assert.assertTrue(prefs.contains("PrefE")); + + Assert.assertEquals("keep it simple stupid.", prefs.getString("PrefA", "DEFAULT")); + Assert.assertEquals(34565, prefs.getInt("PrefB", 6503)); + Assert.assertEquals(543.22f, prefs.getFloat("PrefC", 204.3f)); + Assert.assertEquals(123432L, prefs.getLong("PrefD", 3407L)); + Assert.assertEquals(true, prefs.getBoolean("PrefE", false)); + + Assert.assertEquals("keep it simple stupid.", model.stringValue); + Assert.assertEquals(34565, model.intValue); + Assert.assertEquals(543.22f, model.floatValue); + Assert.assertEquals(123432L, model.longValue); + Assert.assertEquals(true, model.booleanValue); + } + + // Clear check. + @Test + public void test4() { + Model model = new Model(); + model.stringValue = "keep it simple stupid."; + model.intValue = 34565; + model.floatValue = 543.22f; + model.longValue = 123432L; + model.booleanValue = true; + model.save(); + + model.clear(); + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertFalse(prefs.contains("PrefA")); + Assert.assertFalse(prefs.contains("PrefB")); + Assert.assertFalse(prefs.contains("PrefC")); + Assert.assertFalse(prefs.contains("PrefD")); + Assert.assertFalse(prefs.contains("PrefE")); + + Assert.assertNull(model.stringValue); + Assert.assertEquals(0, model.intValue); + Assert.assertEquals(0f, model.floatValue); + Assert.assertEquals(0L, model.longValue); + Assert.assertEquals(false, model.booleanValue); + } + + @Pref(name = "model") + public static class Model extends PrefModel { + @PrefKey("PrefA") + public String stringValue; + + @PrefKey("PrefB") + public int intValue; + + @PrefKey("PrefC") + public float floatValue; + + @PrefKey("PrefD") + public long longValue; + + @PrefKey("PrefE") + public boolean booleanValue; + } +} \ No newline at end of file diff --git a/garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest3.java b/garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest3.java new file mode 100644 index 0000000..d6612f1 --- /dev/null +++ b/garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest3.java @@ -0,0 +1,112 @@ +package com.os.operando.garum.models; + +import android.app.Application; +import android.content.Context; +import android.content.SharedPreferences; +import android.support.test.runner.AndroidJUnit4; +import android.test.ApplicationTestCase; + +import com.os.operando.garum.Garum; +import com.os.operando.garum.annotations.DefaultBoolean; +import com.os.operando.garum.annotations.DefaultFloat; +import com.os.operando.garum.annotations.DefaultInt; +import com.os.operando.garum.annotations.DefaultLong; +import com.os.operando.garum.annotations.DefaultString; +import com.os.operando.garum.annotations.Pref; +import com.os.operando.garum.annotations.PrefKey; + +import junit.framework.Assert; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +// Default annotation test. +@RunWith(AndroidJUnit4.class) +public class PrefModelTest3 extends ApplicationTestCase { + + public PrefModelTest3() { + super(Application.class); + } + + @Before + public void setUp() throws Exception { + // Clear a model pref. + getContext().getSharedPreferences("model", Context.MODE_PRIVATE).edit().clear().commit(); + + Garum.initialize(getContext()); + } + + @After + public void tearDown() throws Exception { + // Clear a model pref. + getContext().getSharedPreferences("model", Context.MODE_PRIVATE).edit().clear().commit(); + } + + // Test of raw preferences. + @Test + public void test1() { + Model model = new Model(); + boolean result = model.save(); + Assert.assertTrue(result); // Check save + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertTrue(prefs.contains("stringValue")); + Assert.assertTrue(prefs.contains("intValue")); + Assert.assertTrue(prefs.contains("floatValue")); + Assert.assertTrue(prefs.contains("longValue")); + Assert.assertTrue(prefs.contains("booleanValue")); + + Assert.assertEquals("keep it simple stupid.", prefs.getString("stringValue", "DEFAULT")); + Assert.assertEquals(34565, prefs.getInt("intValue", 6503)); + Assert.assertEquals(543.22f, prefs.getFloat("floatValue", 204.3f)); + Assert.assertEquals(123432L, prefs.getLong("longValue", 3407L)); + Assert.assertEquals(true, prefs.getBoolean("booleanValue", false)); + } + + // Test of #clear method. + @Test + public void test2() { + Model model = new Model(); + model.save(); + + model.clear(); + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertFalse(prefs.contains("stringValue")); + Assert.assertFalse(prefs.contains("intValue")); + Assert.assertFalse(prefs.contains("floatValue")); + Assert.assertFalse(prefs.contains("longValue")); + Assert.assertFalse(prefs.contains("booleanValue")); + + Assert.assertEquals("keep it simple stupid.", model.stringValue); + Assert.assertEquals(34565, model.intValue); + Assert.assertEquals(543.22f, model.floatValue); + Assert.assertEquals(123432L, model.longValue); + Assert.assertEquals(true, model.booleanValue); + } + + @Pref(name = "model") + public static class Model extends PrefModel { + @PrefKey + @DefaultString("keep it simple stupid.") + public String stringValue; + + @PrefKey + @DefaultInt(34565) + public int intValue; + + @PrefKey + @DefaultFloat(543.22f) + public float floatValue; + + @PrefKey + @DefaultLong(123432L) + public long longValue; + + @PrefKey + @DefaultBoolean(true) + public boolean booleanValue; + } +} \ No newline at end of file diff --git a/garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest4.java b/garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest4.java new file mode 100644 index 0000000..647669e --- /dev/null +++ b/garum/src/androidTest/java/com/os/operando/garum/models/PrefModelTest4.java @@ -0,0 +1,132 @@ +package com.os.operando.garum.models; + +import android.app.Application; +import android.content.Context; +import android.content.SharedPreferences; +import android.support.test.runner.AndroidJUnit4; +import android.test.ApplicationTestCase; + +import com.os.operando.garum.Garum; +import com.os.operando.garum.annotations.Pref; +import com.os.operando.garum.annotations.PrefKey; + +import junit.framework.Assert; + +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.Date; + +@RunWith(AndroidJUnit4.class) +public class PrefModelTest4 extends ApplicationTestCase { + + public PrefModelTest4() { + super(Application.class); + } + + @Before + public void setUp() throws Exception { + // Clear a model pref. + getContext().getSharedPreferences("model", Context.MODE_PRIVATE).edit().clear().commit(); + + Garum.initialize(getContext()); + } + + @After + public void tearDown() throws Exception { + // Clear a model pref. + getContext().getSharedPreferences("model", Context.MODE_PRIVATE).edit().clear().commit(); + } + + // Test of raw preferences. + @Test + public void test1() { + Model model = new Model(); + boolean result = model.save(); + Assert.assertTrue(result); // Check save + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertFalse(prefs.contains("date")); + Assert.assertFalse(prefs.contains("json")); + + Assert.assertNull(model.date); + Assert.assertNull(model.json); + } + + @Test + public void test2() throws JSONException { + String jsonString = "{\"screen_name\":\"katty0324\",\"age\":23}"; + JSONObject jsonObject = new JSONObject(jsonString); + Date date = new Date(); + + Model model = new Model(); + model.date = date; + model.json = jsonObject; + model.save(); + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertTrue(prefs.contains("date")); + Assert.assertTrue(prefs.contains("json")); + } + + // Test of load. + @Test + public void test3() throws JSONException { + String jsonString = "{\"screen_name\":\"katty0324\",\"age\":23}"; + JSONObject jsonObject = new JSONObject(jsonString); + Date date = new Date(); + + Model model = new Model(); + model.date = date; + model.json = jsonObject; + model.save(); + + // Load data + model = new Model(); + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertTrue(prefs.contains("date")); + Assert.assertTrue(prefs.contains("json")); + + Assert.assertEquals(date, model.date); + Assert.assertEquals("katty0324", model.json.getString("screen_name")); + Assert.assertEquals("katty0324", jsonObject.getString("screen_name")); + Assert.assertEquals(23, model.json.getInt("age")); + Assert.assertEquals(23, jsonObject.getInt("age")); + } + + // Test of #clear method. + @Test + public void test4() throws JSONException { + String jsonString = "{\"screen_name\":\"katty0324\",\"age\":23}"; + JSONObject jsonObject = new JSONObject(jsonString); + Date date = new Date(); + + Model model = new Model(); + model.date = date; + model.json = jsonObject; + model.save(); + + model.clear(); + + SharedPreferences prefs = getContext().getSharedPreferences("model", Context.MODE_PRIVATE); + Assert.assertFalse(prefs.contains("date")); + Assert.assertFalse(prefs.contains("json")); + + Assert.assertNull(model.date); + Assert.assertNull(model.json); + } + + @Pref(name = "model") + public static class Model extends PrefModel { + @PrefKey + public Date date; + + @PrefKey + public JSONObject json; + } +} \ No newline at end of file