lib/android: Added tests to mnit_simple
authorFrédéric Vachon <fredvac@gmail.com>
Fri, 13 Jun 2014 17:40:24 +0000 (13:40 -0400)
committerFrédéric Vachon <fredvac@gmail.com>
Fri, 13 Jun 2014 17:40:24 +0000 (13:40 -0400)
Signed-off-by: Frédéric Vachon <fredvac@gmail.com>

examples/mnit_simple/src/simple_android.nit

index 157bc7e..1f17806 100644 (file)
@@ -21,6 +21,7 @@ end
 
 import simple
 import mnit_android
+import android::shared_preferences
 
 in "Java" `{
        import android.content.Context;
@@ -30,11 +31,54 @@ in "Java" `{
 redef class App
        redef fun input( ie )
        do
-               if ie isa PointerEvent and ie.depressed then do_java_stuff
-
+               if ie isa PointerEvent and ie.depressed then
+                       do_java_stuff
+                       test_shared_preferences
+               end     
                return super
        end
 
+       fun test_shared_preferences
+       do
+               # Private mode tests
+               var sp = new SharedPreferences.privately(self, "test")
+               sp.add_bool("a_boolean",  true)
+               sp.add_float("a_float", 66.6)
+               sp.add_int("an_int", 666)
+               sp.add_int("a_second_int", 666777)
+               sp.add_long("a_long", 6666666666)
+               sp.add_string("a_string", "A string")
+               sp["another_int"] = 85
+               sp["yet_another_string"] = "Another string"
+               sp.remove("a_second_int")
+
+               # Serialized object test
+               var my_point = new Point(10, 10)
+               sp["a_point"] = my_point
+               var my_deserialized_point = sp.deserialize("a_point")
+               assert my_point.to_s == my_deserialized_point.to_s
+               
+               assert sp.bool("a_boolean", false) == true
+               assert sp.bool("wrong_boolean", false) == false
+               assert sp.float("a_float", 0.0) != 0.0
+               assert sp.float("wrong_float", 0.0) == 0.0
+               assert sp.int("an_int", 0) == 666
+               assert sp.int("a_second_int", 0) == 0
+               assert sp.long("a_long", 0) == 6666666666
+               assert sp.long("wrong_long", 0) == 0
+               assert sp.string("a_string", "ERROR!") == "A string"
+               assert sp.string("wrong_string", "ERROR!") == "ERROR!"
+               assert sp.long("another_int", 0) == 85
+               assert sp.string("yet_another_string", "ERROR!") == "Another string"
+               assert sp.has("an_int") == true
+               assert sp.has("a_second_int") == false
+
+               sp.clear
+               assert sp.all == null
+
+               sp.destroy
+       end
+
        fun do_java_stuff import native_activity in "Java" `{
                // + Log (no context needed)
                android.util.Log.d("mnit_simple", "Java within NIT!!!");
@@ -61,3 +105,20 @@ redef class App
                });
        `}
 end
+
+class Point
+       auto_serializable
+       super Serializable
+
+       var x: Int
+       var y: Int
+
+       init(x, y: Int)
+       do
+               self.x = x
+               self.y = y
+       end
+
+       redef fun to_s do return "({x}, {y})"
+end
+