Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Side by Side Diff: mojo/python/tests/bindings_serialization_deserialization_unittest.py

Issue 578263003: mojo: Add deserialization to python structs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Follow review Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/python/mojo/bindings/serialization.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import math
5 import unittest 6 import unittest
6 7
7 # pylint: disable=F0401 8 # pylint: disable=F0401
9 import mojo.bindings.reflection as reflection
8 import mojo.system 10 import mojo.system
9 11
10 # Generated files 12 # Generated files
11 # pylint: disable=F0401 13 # pylint: disable=F0401
14 import sample_import_mojom
15 import sample_import2_mojom
12 import sample_service_mojom 16 import sample_service_mojom
13 17
14 18
15 def _NewHandle(): 19 def _NewHandle():
16 return mojo.system.MessagePipe().handle0 20 return mojo.system.MessagePipe().handle0
17 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
18 def _NewFoo(): 66 def _NewFoo():
19 foo_instance = sample_service_mojom.Foo() 67 foo_instance = sample_service_mojom.Foo()
20 foo_instance.name = "Foo.name" 68 foo_instance.name = "Foo.name"
21 foo_instance.x = 23 69 foo_instance.x = 23
22 foo_instance.y = -23 70 foo_instance.y = -23
23 foo_instance.a = False 71 foo_instance.a = False
24 foo_instance.b = True 72 foo_instance.b = True
25 foo_instance.c = True 73 foo_instance.c = True
26 foo_instance.bar = sample_service_mojom.Bar() 74 foo_instance.bar = _NewBar()
27 foo_instance.extra_bars = [ 75 foo_instance.extra_bars = [
28 sample_service_mojom.Bar(), 76 _NewBar(),
29 sample_service_mojom.Bar() 77 _NewBar(),
30 ] 78 ]
31 foo_instance.data = 'Hello world' 79 foo_instance.data = 'Hello world'
32 foo_instance.source = _NewHandle() 80 foo_instance.source = _NewHandle()
33 foo_instance.input_streams = [ _NewHandle() ] 81 foo_instance.input_streams = [ _NewHandle() ]
34 foo_instance.output_streams = [ _NewHandle(), _NewHandle() ] 82 foo_instance.output_streams = [ _NewHandle(), _NewHandle() ]
35 foo_instance.array_of_array_of_bools = [ [ True, False ], [] ] 83 foo_instance.array_of_array_of_bools = [ [ True, False ], [] ]
36 foo_instance.multi_array_of_strings = [ 84 foo_instance.multi_array_of_strings = [
37 [ 85 [
38 [ "1", "2" ], 86 [ "1", "2" ],
39 [], 87 [],
40 [ "3", "4" ], 88 [ "3", "4" ],
41 ], 89 ],
42 [], 90 [],
43 ] 91 ]
44 foo_instance.array_of_bools = [ True, 0, 1, 2, 0 ] 92 foo_instance.array_of_bools = [ True, 0, 1, 2, 0, 0, 0, 0, 0, True ]
45 return foo_instance 93 return foo_instance
46 94
95
47 class SerializationDeserializationTest(unittest.TestCase): 96 class SerializationDeserializationTest(unittest.TestCase):
48 97
98 def testTestEquality(self):
99 self.assertFalse(_TestEquality(1, 2))
100
49 def testFooSerialization(self): 101 def testFooSerialization(self):
50 (data, _) = _NewFoo().Serialize() 102 (data, _) = _NewFoo().Serialize()
51 self.assertTrue(len(data)) 103 self.assertTrue(len(data))
52 self.assertEquals(len(data) % 8, 0) 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(memoryview(data), handles))
110
111 def testFooSerializationDeserialization(self):
112 foo1 = _NewFoo()
113 (data, handles) = foo1.Serialize()
114 foo2 = sample_service_mojom.Foo.Deserialize(memoryview(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(memoryview(data),
126 handles)
127 self.assertTrue(_TestEquality(v1, v2))
128
129 def testFooDeserializationError(self):
130 with self.assertRaises(Exception):
131 sample_service_mojom.Foo.Deserialize("", [])
OLDNEW
« no previous file with comments | « mojo/public/python/mojo/bindings/serialization.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698