Home | History | Annotate | Download | only in tests
      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.system
     10 
     11 # Generated files
     12 # pylint: disable=F0401
     13 import sample_import_mojom
     14 import sample_import2_mojom
     15 import sample_service_mojom
     16 
     17 
     18 class StructBindingsTest(unittest.TestCase):
     19 
     20   def testModule(self):
     21     self.assertEquals(sample_service_mojom.DefaultsTest.__module__,
     22                       'sample_service_mojom')
     23 
     24   def testDefaultsTest(self):
     25     defaults_test = sample_service_mojom.DefaultsTest()
     26     self.assertEquals(defaults_test.a0, -12)
     27     self.assertEquals(defaults_test.a1, 12)
     28     self.assertEquals(defaults_test.a2, 1234)
     29     self.assertEquals(defaults_test.a3, 34567)
     30     self.assertEquals(defaults_test.a4, 123456)
     31     self.assertEquals(defaults_test.a5, 3456789012)
     32     self.assertEquals(defaults_test.a6, -111111111111)
     33     self.assertEquals(defaults_test.a7, 9999999999999999999)
     34     self.assertEquals(defaults_test.a8, 0x12345)
     35     self.assertEquals(defaults_test.a9, -0x12345)
     36     self.assertEquals(defaults_test.a10, 1234)
     37     self.assertEquals(defaults_test.a11, True)
     38     self.assertEquals(defaults_test.a12, False)
     39     self.assertEquals(defaults_test.a13, 123.25)
     40     self.assertEquals(defaults_test.a14, 1234567890.123)
     41     self.assertEquals(defaults_test.a15, 1E10)
     42     self.assertEquals(defaults_test.a16, -1.2E+20)
     43     self.assertEquals(defaults_test.a17, 1.23E-20)
     44     self.assertEquals(defaults_test.a18, None)
     45     self.assertEquals(defaults_test.a19, None)
     46     self.assertEquals(defaults_test.a20, sample_service_mojom.Bar.Type.BOTH)
     47     self.assertEquals(defaults_test.a21, None)
     48     self.assertTrue(isinstance(defaults_test.a22, sample_import2_mojom.Thing))
     49     self.assertEquals(defaults_test.a23, 0xFFFFFFFFFFFFFFFF)
     50     self.assertEquals(defaults_test.a24, 0x123456789)
     51     self.assertEquals(defaults_test.a25, -0x123456789)
     52     self.assertEquals(defaults_test.a26, float('inf'))
     53     self.assertEquals(defaults_test.a27, float('-inf'))
     54     self.assertTrue(math.isnan(defaults_test.a28))
     55     self.assertEquals(defaults_test.a29, float('inf'))
     56     self.assertEquals(defaults_test.a30, float('-inf'))
     57     self.assertTrue(math.isnan(defaults_test.a31))
     58 
     59   def testNoAliasing(self):
     60     foo1 = sample_service_mojom.Foo()
     61     foo2 = sample_service_mojom.Foo()
     62     foo1.name = "foo1"
     63     foo2.name = "foo2"
     64     self.assertEquals(foo1.name, "foo1")
     65     self.assertEquals(foo2.name, "foo2")
     66 
     67     defaults_test1 = sample_service_mojom.DefaultsTest()
     68     defaults_test2 = sample_service_mojom.DefaultsTest()
     69     self.assertNotEquals(defaults_test1.a22, defaults_test2.a22)
     70 
     71   def testImmutableAttributeSet(self):
     72     foo_instance = sample_service_mojom.Foo()
     73     with self.assertRaises(AttributeError):
     74       foo_instance.new_attribute = None
     75     with self.assertRaises(AttributeError):
     76       del foo_instance.name
     77 
     78   def _TestIntegerField(self, entity, field_name, bits, signed):
     79     if signed:
     80       min_value = -(1 << (bits - 1))
     81       max_value = (1 << (bits - 1)) - 1
     82     else:
     83       min_value = 0
     84       max_value = (1 << bits) - 1
     85     entity.__setattr__(field_name, min_value)
     86     entity.__setattr__(field_name, max_value)
     87     with self.assertRaises(TypeError):
     88       entity.__setattr__(field_name, None)
     89     with self.assertRaises(OverflowError):
     90       entity.__setattr__(field_name, min_value - 1)
     91     with self.assertRaises(OverflowError):
     92       entity.__setattr__(field_name, max_value + 1)
     93     with self.assertRaises(TypeError):
     94       entity.__setattr__(field_name, 'hello world')
     95 
     96   def testTypes(self):
     97     defaults_test = sample_service_mojom.DefaultsTest()
     98     # Integer types
     99     self._TestIntegerField(defaults_test, 'a0', 8, True)
    100     self._TestIntegerField(defaults_test, 'a1', 8, False)
    101     self._TestIntegerField(defaults_test, 'a2', 16, True)
    102     self._TestIntegerField(defaults_test, 'a3', 16, False)
    103     self._TestIntegerField(defaults_test, 'a4', 32, True)
    104     self._TestIntegerField(defaults_test, 'a5', 32, False)
    105     self._TestIntegerField(defaults_test, 'a6', 64, True)
    106     self._TestIntegerField(defaults_test, 'a7', 64, False)
    107 
    108     # Boolean types
    109     defaults_test.a11 = False
    110     self.assertEquals(defaults_test.a11, False)
    111     defaults_test.a11 = None
    112     self.assertEquals(defaults_test.a11, False)
    113     defaults_test.a11 = []
    114     self.assertEquals(defaults_test.a11, False)
    115     defaults_test.a12 = True
    116     self.assertEquals(defaults_test.a12, True)
    117     defaults_test.a12 = 1
    118     self.assertEquals(defaults_test.a12, True)
    119     defaults_test.a12 = [[]]
    120     self.assertEquals(defaults_test.a12, True)
    121 
    122     # Floating point types
    123     with self.assertRaises(TypeError):
    124       defaults_test.a13 = 'hello'
    125     with self.assertRaises(TypeError):
    126       defaults_test.a14 = 'hello'
    127 
    128     # Array type
    129     defaults_test.a18 = None
    130     defaults_test.a18 = []
    131     defaults_test.a18 = [ 0 ]
    132     defaults_test.a18 = [ 255 ]
    133     defaults_test.a18 = [ 0, 255 ]
    134     with self.assertRaises(TypeError):
    135       defaults_test.a18 = [[]]
    136     with self.assertRaises(OverflowError):
    137       defaults_test.a18 = [ -1 ]
    138     with self.assertRaises(OverflowError):
    139       defaults_test.a18 = [ 256 ]
    140 
    141     # String type
    142     defaults_test.a19 = None
    143     defaults_test.a19 = ''
    144     defaults_test.a19 = 'hello world'
    145     with self.assertRaises(TypeError):
    146       defaults_test.a19 = [[]]
    147     with self.assertRaises(TypeError):
    148       defaults_test.a19 = [ -1 ]
    149     with self.assertRaises(TypeError):
    150       defaults_test.a19 = [ 256 ]
    151 
    152     # Structs
    153     defaults_test.a21 = None
    154     defaults_test.a21 = sample_import_mojom.Point()
    155     with self.assertRaises(TypeError):
    156       defaults_test.a21 = 1
    157     with self.assertRaises(TypeError):
    158       defaults_test.a21 = sample_import2_mojom.Thing()
    159 
    160     # Handles
    161     foo_instance = sample_service_mojom.Foo()
    162     foo_instance.source = None
    163     foo_instance.source = mojo.system.Handle()
    164     with self.assertRaises(TypeError):
    165       foo_instance.source = 1
    166     with self.assertRaises(TypeError):
    167       foo_instance.source = object()
    168 
    169   def testConstructor(self):
    170     bar_instance = sample_service_mojom.Bar()
    171     foo_instance = sample_service_mojom.Foo(name="Foo",
    172                                             x=-1,
    173                                             y=5,
    174                                             a=False,
    175                                             bar=bar_instance)
    176     self.assertEquals(foo_instance.name, "Foo")
    177     self.assertEquals(foo_instance.x, -1)
    178     self.assertEquals(foo_instance.y, 5)
    179     self.assertEquals(foo_instance.a, False)
    180     self.assertEquals(foo_instance.bar, bar_instance)
    181 
    182