Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / mojo / python / tests / bindings_serialization_deserialization_unittest.py
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import math
6 import unittest
7
8 # pylint: disable=F0401
9 import mojo.bindings.reflection as reflection
10 import mojo.system
11
12 # Generated files
13 # pylint: disable=F0401
14 import sample_import_mojom
15 import sample_import2_mojom
16 import sample_service_mojom
17
18
19 def _NewHandle():
20   return mojo.system.MessagePipe().handle0
21
22
23 def _TestEquality(x, y):
24   if x == y:
25     return True
26
27   if type(x) != type(y):
28     print '\n%r != %r. Element are not of the same type.' % (x, y)
29     return False
30
31   if isinstance(x, float) and math.isnan(x) and math.isnan(y):
32     return True
33
34   if hasattr(x, '__len__'):
35     if len(x) != len(y):
36       print '\n%r != %r. Iterables are not of the same size.' % (x, y)
37       return False
38     for (x1, y1) in zip(x, y):
39       if not _TestEquality(x1, y1):
40         return False
41     return True
42
43   if (hasattr(x, '__metaclass__') and
44       x.__metaclass__ == reflection.MojoStructType):
45     properties = [p for p in dir(x) if not p.startswith('_')]
46     for p in properties:
47       p1 = getattr(x, p)
48       p2 = getattr(y, p)
49       if not hasattr(p1, '__call__') and not _TestEquality(p1, p2):
50         print '\n%r != %r. Not equal for property %r.' % (x, y, p)
51         return False
52     return True
53
54   return False
55
56
57 def _NewBar():
58   bar_instance = sample_service_mojom.Bar()
59   bar_instance.alpha = 22
60   bar_instance.beta = 87
61   bar_instance.gamma = 122
62   bar_instance.type = sample_service_mojom.Bar.Type.BOTH
63   return bar_instance
64
65
66 def _NewFoo():
67   foo_instance = sample_service_mojom.Foo()
68   foo_instance.name = "Foo.name"
69   foo_instance.x = 23
70   foo_instance.y = -23
71   foo_instance.a = False
72   foo_instance.b = True
73   foo_instance.c = True
74   foo_instance.bar = _NewBar()
75   foo_instance.extra_bars = [
76       _NewBar(),
77       _NewBar(),
78   ]
79   foo_instance.data = 'Hello world'
80   foo_instance.source = _NewHandle()
81   foo_instance.input_streams = [ _NewHandle() ]
82   foo_instance.output_streams = [ _NewHandle(), _NewHandle() ]
83   foo_instance.array_of_array_of_bools = [ [ True, False ], [] ]
84   foo_instance.multi_array_of_strings = [
85       [
86           [ "1", "2" ],
87           [],
88           [ "3", "4" ],
89       ],
90       [],
91   ]
92   foo_instance.array_of_bools = [ True, 0, 1, 2, 0, 0, 0, 0, 0, True ]
93   return foo_instance
94
95
96 class SerializationDeserializationTest(unittest.TestCase):
97
98   def testTestEquality(self):
99     self.assertFalse(_TestEquality(1, 2))
100
101   def testFooSerialization(self):
102     (data, _) = _NewFoo().Serialize()
103     self.assertTrue(len(data))
104     self.assertEquals(len(data) % 8, 0)
105
106   def testFooDeserialization(self):
107     (data, handles) = _NewFoo().Serialize()
108     self.assertTrue(
109         sample_service_mojom.Foo.Deserialize(data, handles))
110
111   def testFooSerializationDeserialization(self):
112     foo1 = _NewFoo()
113     (data, handles) = foo1.Serialize()
114     foo2 = sample_service_mojom.Foo.Deserialize(data, handles)
115     self.assertTrue(_TestEquality(foo1, foo2))
116
117   def testDefaultsTestSerializationDeserialization(self):
118     v1 = sample_service_mojom.DefaultsTest()
119     v1.a18 = []
120     v1.a19 = ""
121     v1.a21 = sample_import_mojom.Point()
122     v1.a22.location = sample_import_mojom.Point()
123     v1.a22.size = sample_import2_mojom.Size()
124     (data, handles) = v1.Serialize()
125     v2 = sample_service_mojom.DefaultsTest.Deserialize(data, handles)
126     self.assertTrue(_TestEquality(v1, v2))
127
128   def testFooDeserializationError(self):
129     with self.assertRaises(Exception):
130       sample_service_mojom.Foo.Deserialize("", [])