Exemplo n.º 1
0
 def testDefaultsTestSerializationDeserialization(self):
     v1 = sample_service_mojom.DefaultsTest()
     v1.a18 = []
     v1.a19 = ""
     v1.a21 = sample_import_mojom.Point()
     v1.a22.location = sample_import_mojom.Point()
     v1.a22.size = sample_import2_mojom.Size()
     (data, handles) = v1.Serialize()
     context = serialization.RootDeserializationContext(data, handles)
     v2 = sample_service_mojom.DefaultsTest.Deserialize(context)
     # NaN needs to be a special case.
     self.assertNotEquals(v1, v2)
     self.assertTrue(math.isnan(v2.a28))
     self.assertTrue(math.isnan(v2.a31))
     v1.a28 = v2.a28 = v1.a31 = v2.a31 = 0
     self.assertEquals(v1, v2)
 def testDefaultsTest(self):
     defaults_test = sample_service_mojom.DefaultsTest()
     self.assertEquals(defaults_test.a0, -12)
     self.assertEquals(defaults_test.a1, 12)
     self.assertEquals(defaults_test.a2, 1234)
     self.assertEquals(defaults_test.a3, 34567)
     self.assertEquals(defaults_test.a4, 123456)
     self.assertEquals(defaults_test.a5, 3456789012)
     self.assertEquals(defaults_test.a6, -111111111111)
     self.assertEquals(defaults_test.a7, 9999999999999999999)
     self.assertEquals(defaults_test.a8, 0x12345)
     self.assertEquals(defaults_test.a9, -0x12345)
     self.assertEquals(defaults_test.a10, 1234)
     self.assertEquals(defaults_test.a11, True)
     self.assertEquals(defaults_test.a12, False)
     self.assertEquals(defaults_test.a13, 123.25)
     self.assertEquals(defaults_test.a14, 1234567890.123)
     self.assertEquals(defaults_test.a15, 1E10)
     self.assertEquals(defaults_test.a16, -1.2E+20)
     self.assertEquals(defaults_test.a17, 1.23E-20)
     self.assertEquals(defaults_test.a18, None)
     self.assertEquals(defaults_test.a19, None)
     self.assertEquals(defaults_test.a20,
                       sample_service_mojom.Bar.Type.BOTH)
     self.assertEquals(defaults_test.a21, None)
     self.assertTrue(
         isinstance(defaults_test.a22, sample_import2_mojom.Thing))
     self.assertEquals(defaults_test.a23, 0xFFFFFFFFFFFFFFFF)
     self.assertEquals(defaults_test.a24, 0x123456789)
     self.assertEquals(defaults_test.a25, -0x123456789)
     self.assertEquals(defaults_test.a26, float('inf'))
     self.assertEquals(defaults_test.a27, float('-inf'))
     self.assertTrue(math.isnan(defaults_test.a28))
     self.assertEquals(defaults_test.a29, float('inf'))
     self.assertEquals(defaults_test.a30, float('-inf'))
     self.assertTrue(math.isnan(defaults_test.a31))
    def testTypes(self):
        defaults_test = sample_service_mojom.DefaultsTest()
        # Integer types
        self._TestIntegerField(defaults_test, 'a0', 8, True)
        self._TestIntegerField(defaults_test, 'a1', 8, False)
        self._TestIntegerField(defaults_test, 'a2', 16, True)
        self._TestIntegerField(defaults_test, 'a3', 16, False)
        self._TestIntegerField(defaults_test, 'a4', 32, True)
        self._TestIntegerField(defaults_test, 'a5', 32, False)
        self._TestIntegerField(defaults_test, 'a6', 64, True)
        self._TestIntegerField(defaults_test, 'a7', 64, False)

        # Boolean types
        defaults_test.a11 = False
        self.assertEquals(defaults_test.a11, False)
        defaults_test.a11 = None
        self.assertEquals(defaults_test.a11, False)
        defaults_test.a11 = []
        self.assertEquals(defaults_test.a11, False)
        defaults_test.a12 = True
        self.assertEquals(defaults_test.a12, True)
        defaults_test.a12 = 1
        self.assertEquals(defaults_test.a12, True)
        defaults_test.a12 = [[]]
        self.assertEquals(defaults_test.a12, True)

        # Floating point types
        with self.assertRaises(TypeError):
            defaults_test.a13 = 'hello'
        with self.assertRaises(TypeError):
            defaults_test.a14 = 'hello'

        # Array type
        defaults_test.a18 = None
        defaults_test.a18 = []
        defaults_test.a18 = [0]
        defaults_test.a18 = [255]
        defaults_test.a18 = [0, 255]
        with self.assertRaises(TypeError):
            defaults_test.a18 = [[]]
        with self.assertRaises(OverflowError):
            defaults_test.a18 = [-1]
        with self.assertRaises(OverflowError):
            defaults_test.a18 = [256]

        # String type
        defaults_test.a19 = None
        defaults_test.a19 = ''
        defaults_test.a19 = 'hello world'
        with self.assertRaises(TypeError):
            defaults_test.a19 = [[]]
        with self.assertRaises(TypeError):
            defaults_test.a19 = [-1]
        with self.assertRaises(TypeError):
            defaults_test.a19 = [256]

        # Structs
        defaults_test.a21 = None
        defaults_test.a21 = sample_import_mojom.Point()
        with self.assertRaises(TypeError):
            defaults_test.a21 = 1
        with self.assertRaises(TypeError):
            defaults_test.a21 = sample_import2_mojom.Thing()

        # Handles
        foo_instance = sample_service_mojom.Foo()
        foo_instance.source = None
        foo_instance.source = mojo.system.Handle()
        with self.assertRaises(TypeError):
            foo_instance.source = 1
        with self.assertRaises(TypeError):
            foo_instance.source = object()