contrib/mnit_test: move mnit_simple to contrib/mnit_test
[nit.git] / contrib / mnit_test / src / test_shared_preferences.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Test for the shared_preferences module of App.nit framework
18 module test_shared_preferences
19
20 import simple_android
21 import android::shared_preferences
22
23 redef class App
24 redef fun input ( ie )
25 do
26 if ie isa PointerEvent and ie.depressed then
27 test_shared_preferences
28 end
29 return super
30 end
31
32 fun test_shared_preferences
33 do
34 # Private mode tests
35 var sp = new SharedPreferences.privately(self, "test")
36 sp.add_bool("a_boolean", true)
37 sp.add_float("a_float", 66.6)
38 sp.add_int("an_int", 666)
39 sp.add_int("a_second_int", 666777)
40 sp.add_long("a_long", 6666666666)
41 sp.add_string("a_string", "A string")
42 sp["another_int"] = 85
43 sp["yet_another_string"] = "Another string"
44 sp.remove("a_second_int")
45
46 # Serialized object test
47 var my_point = new Point(10, 10)
48 sp["a_point"] = my_point
49 var my_deserialized_point = sp["a_point"]
50 assert my_point.to_s == my_deserialized_point.to_s
51
52 assert sp.bool("a_boolean", false) == true
53 assert sp.bool("wrong_boolean", false) == false
54 assert sp.float("a_float", 0.0) != 0.0
55 assert sp.float("wrong_float", 0.0) == 0.0
56 assert sp.int("an_int", 0) == 666
57 assert sp.int("a_second_int", 0) == 0
58 assert sp.long("a_long", 0) == 6666666666
59 assert sp.long("wrong_long", 0) == 0
60 assert sp.string("a_string", "ERROR!") == "A string"
61 assert sp.string("wrong_string", "ERROR!") == "ERROR!"
62 assert sp["another_int"] == 85
63 assert sp["yet_another_string"] == "Another string"
64 assert sp.has("an_int") == true
65 assert sp.has("a_second_int") == false
66
67 sp.clear
68 assert sp.all == null
69
70 sp.destroy
71 end
72 end