lib/android: Bundle do not need a context or to import native_app_glue
[nit.git] / examples / mnit_simple / src / test_bundle.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 bundle module of App.nit framework
18 module test_bundle
19
20 import simple_android
21 import android::bundle
22
23 redef class App
24 redef fun input ( ie )
25 do
26 if ie isa PointerEvent and ie.depressed then
27 test_bundle
28 end
29 return super
30 end
31
32 fun test_bundle
33 do
34 var bundle = new Bundle
35
36 bundle["anInt"] = 1
37 bundle["aFloat"] = 1.1
38 bundle["aString"] = "A string"
39 bundle["aBool"] = true
40
41 var int_array = new Array[Int]
42 var bool_array = new Array[Bool]
43
44 var value = true
45
46 for i in [0..5] do
47 int_array.add(i)
48 bool_array.add(value)
49 value = not value
50 end
51
52 bundle["anArrayOfInt"] = int_array
53 bundle["anArrayOfBool"] = bool_array
54
55 assert bundle.int("anInt", 0) == 1
56 assert bundle.int("wrongInt", 0) == 0
57 assert bundle.float("aFloat", 0.0) == 1.1
58 assert bundle.float("wrongFloat", 0.0) == 0.0
59 assert bundle.string("aString") == "A string"
60 assert bundle.string("wrongString") == null
61 assert bundle.bool("aBool", false)
62 assert bundle.bool("wrongBool", false) == false
63
64 var int_array_test = bundle.array_of_int("anArrayOfInt")
65 var bool_array_test = bundle.array_of_bool("anArrayOfBool")
66
67 value = true
68
69 for i in [0..5] do
70 assert int_array_test[i] == i
71 assert bool_array_test[i] == value
72 value = not value
73 end
74
75 assert bundle.size == 6
76 assert bundle.has("aBool")
77 assert not bundle.is_empty
78
79 bundle.remove("aString")
80 bundle.remove("anArrayOfBool")
81
82 assert bundle.string("aString") == null
83 assert bundle.array_of_bool("anArrayOfBool") == null
84
85 # Serializable tests
86 var p1 = new Point(10, 10)
87 bundle["aPoint"] = p1
88 var p2 = bundle.deserialize("aPoint")
89
90 assert p1.to_s == p2.to_s
91
92 var point_array = new Array[Point]
93
94 for i in [0..5] do point_array.add(new Point(i, i))
95
96 bundle["anArrayOfPoint"] = point_array
97
98 var deserialized_point_array = bundle.deserialize_array("anArrayOfPoint")
99
100 for i in [0..5] do
101 var point = new Point(i, i)
102 assert deserialized_point_array[i].to_s == point.to_s
103 end
104
105 bundle.clear
106
107 assert bundle.keys.is_empty
108 assert bundle.is_empty
109 end
110 end
111