Exemplo n.º 1
0
    def test_struct_return_send(self):
        "Methods returning structs of different sizes by value can be handled when using send_message."
        Example = ObjCClass('Example')
        example = Example.alloc().init()

        class struct_int_sized(Structure):
            _fields_ = [("x", c_char * 4)]

        self.assertEqual(
            send_message(example, "intSizedStruct",
                         restype=struct_int_sized).x, b"abc")

        class struct_oddly_sized(Structure):
            _fields_ = [("x", c_char * 5)]

        self.assertEqual(
            send_message(example,
                         "oddlySizedStruct",
                         restype=struct_oddly_sized).x, b"abcd")

        class struct_large(Structure):
            _fields_ = [("x", c_char * 17)]

        self.assertEqual(
            send_message(example, "largeStruct", restype=struct_large).x,
            b"abcdefghijklmnop")
Exemplo n.º 2
0
    def test_struct_return_send(self):
        "Methods returning structs of different sizes by value can be handled when using send_message."
        Example = ObjCClass('Example')
        example = Example.alloc().init()

        self.assertEqual(
            send_message(example,
                         "intSizedStruct",
                         restype=struct_int_sized,
                         argtypes=[]).x,
            b"abc",
        )
        self.assertEqual(
            send_message(example,
                         "oddlySizedStruct",
                         restype=struct_oddly_sized,
                         argtypes=[]).x,
            b"abcd",
        )
        self.assertEqual(
            send_message(example,
                         "largeStruct",
                         restype=struct_large,
                         argtypes=[]).x,
            b"abcdefghijklmnop",
        )
Exemplo n.º 3
0
    def test_struct_return_send(self):
        "Methods returning structs of different sizes by value can be handled when using send_message."
        Example = ObjCClass('Example')
        example = Example.alloc().init()

        self.assertEqual(send_message(example, "intSizedStruct", restype=struct_int_sized).x, b"abc")
        self.assertEqual(send_message(example, "oddlySizedStruct", restype=struct_oddly_sized).x, b"abcd")
        self.assertEqual(send_message(example, "largeStruct", restype=struct_large).x, b"abcdefghijklmnop")
Exemplo n.º 4
0
 def flip(i, back=False):
     old, new = (opened[i], closed[i]) if back else (closed[i], opened[i])
     send_message(UIView,
                  b"transitionFromView:toView:duration:options:completion:",
                  old,
                  new,
                  NSTimeInterval(0.3),
                  UIViewAnimationOptionTransitionFlipFromBottom,
                  None)
Exemplo n.º 5
0
    def test_method_send(self):
        "An instance method can be invoked with send_message."
        Example = ObjCClass('Example')

        obj = Example.alloc().init()

        self.assertEqual(
            send_message(obj, "accessBaseIntField", restype=c_int,
                         argtypes=[]), 22)
        self.assertEqual(
            send_message(obj, "accessIntField", restype=c_int, argtypes=[]),
            33)

        send_message(obj,
                     "mutateBaseIntFieldWithValue:",
                     8888,
                     restype=None,
                     argtypes=[c_int])
        send_message(obj,
                     "mutateIntFieldWithValue:",
                     9999,
                     restype=None,
                     argtypes=[c_int])

        self.assertEqual(
            send_message(obj, "accessBaseIntField", restype=c_int,
                         argtypes=[]), 8888)
        self.assertEqual(
            send_message(obj, "accessIntField", restype=c_int, argtypes=[]),
            9999)
Exemplo n.º 6
0
    def test_send_sel(self):
        """send_message accepts a SEL object as the selector parameter."""
        Example = ObjCClass('Example')

        obj = Example.alloc().init()

        self.assertEqual(send_message(obj, SEL("accessIntField"), restype=c_int, argtypes=[]), 33)
Exemplo n.º 7
0
 def test_double_method_send(self):
     "A method with a double argument can be handled by send_message."
     Example = ObjCClass('Example')
     example = Example.alloc().init()
     self.assertAlmostEqual(
         send_message(example,
                      "areaOfCircle:",
                      1.5,
                      restype=c_double,
                      argtypes=[c_double]), 1.5 * math.pi, 5)
Exemplo n.º 8
0
 def test_float_method_send(self):
     "A method with a float argument can be handled by send_message."
     Example = ObjCClass('Example')
     example = Example.alloc().init()
     self.assertEqual(
         send_message(example,
                      "areaOfSquare:",
                      1.5,
                      restype=c_float,
                      argtypes=[c_float]), 2.25)
Exemplo n.º 9
0
    def create(self):
        self.native = TogaNumericTextField.alloc().init()
        self.native.interface = self.interface
        self.native.delegate = self.native
        self.native.borderStyle = UITextBorderStyle.RoundedRect
        # FIXME: See Rubicon #96
        # self.native.keyboardType = UIKeyboardType.DecimalPad
        send_message(self.native,
                     'setKeyboardType:',
                     UIKeyboardType.DecimalPad.value,
                     restype=None,
                     argtypes=[c_int])

        # Make the text field respond to any content change.
        self.native.addTarget(self.native,
                              action=SEL('textFieldDidChange:'),
                              forControlEvents=UIControlEventEditingChanged)

        # Add the layout constraints
        self.add_constraints()
Exemplo n.º 10
0
 def test_double_method_send(self):
     "A method with a double argument can be handled by send_message."
     Example = ObjCClass('Example')
     example = Example.alloc().init()
     self.assertAlmostEqual(
         send_message(
             example, "areaOfCircle:", 1.5,
             restype=c_double,
             argtypes=[c_double]
         ),
         1.5 * math.pi, 5
     )
Exemplo n.º 11
0
    def test_method_send(self):
        "An instance method can be invoked with send_message."
        Example = ObjCClass('Example')

        obj = Example.alloc().init()

        self.assertEqual(send_message(obj, "accessBaseIntField", restype=c_int), 22)
        self.assertEqual(send_message(obj, "accessIntField", restype=c_int), 33)

        send_message(obj, "mutateBaseIntFieldWithValue:", 8888, restype=None, argtypes=[c_int])
        send_message(obj, "mutateIntFieldWithValue:", 9999, restype=None, argtypes=[c_int])

        self.assertEqual(send_message(obj, "accessBaseIntField", restype=c_int), 8888)
        self.assertEqual(send_message(obj, "accessIntField", restype=c_int), 9999)
Exemplo n.º 12
0
    def test_method_incorrect_argument_count_send(self):
        """Attempting to call a method with send_message with an incorrect number of arguments throws an exception."""

        Example = ObjCClass('Example')
        obj = Example.alloc().init()

        with self.assertRaises(TypeError):
            send_message(obj,
                         'accessIntField',
                         'extra argument 1',
                         restype=c_int,
                         argtypes=[])

        with self.assertRaises(TypeError):
            send_message(obj,
                         'mutateIntFieldWithValue:',
                         restype=None,
                         argtypes=[c_int])

        with self.assertRaises(TypeError):
            send_message(obj,
                         'mutateIntFieldWithValue:',
                         123,
                         'extra_argument',
                         restype=None,
                         argtypes=[c_int])
Exemplo n.º 13
0
    def test_method_varargs_send(self):
        """A variadic method can be called using send_message."""

        NSString = ObjCClass('NSString')
        formatted = send_message(
            NSString,
            'stringWithFormat:',
            at('This is a %@ with %@'),
            varargs=[at('string'), at('placeholders')],
            restype=objc_id,
            argtypes=[objc_id],
        )
        self.assertEqual(str(ObjCInstance(formatted)), 'This is a string with placeholders')
Exemplo n.º 14
0
    def create(self):
        self.native = TogaNumericTextField.alloc().init()
        self.native.interface = self.interface
        self.native.delegate = self.native
        self.native.borderStyle = UITextBorderStyle.RoundedRect
        # FIXME: See Rubicon #96
        # self.native.keyboardType = UIKeyboardType.DecimalPad
        send_message(
            self.native,
            'setKeyboardType:',
            UIKeyboardType.DecimalPad.value,
            restype=None,
            argtypes=[c_int]
        )

        # Make the text field respond to any content change.
        self.native.addTarget(
            self.native,
            action=SEL('textFieldDidChange:'),
            forControlEvents=UIControlEventEditingChanged
        )

        # Add the layout constraints
        self.add_constraints()
Exemplo n.º 15
0
 def test_float_method_send(self):
     "A method with a float argument can be handled by send_message."
     Example = ObjCClass('Example')
     example = Example.alloc().init()
     self.assertEqual(send_message(example, "areaOfSquare:", 1.5, restype=c_float, argtypes=[c_float]), 2.25)
Exemplo n.º 16
0
 def from_python(cls, s):
     b = s.encode('utf8')
     obj = send_message(ObjCClass("NSString"), b'stringWithUTF8String:', b)
     return cls(obj)