def test_set_get_id(self): l = Libevdev() id = l.id self.assertEqual(id["bustype"], 0) self.assertEqual(id["vendor"], 0) self.assertEqual(id["product"], 0) self.assertEqual(id["version"], 0) id["bustype"] = 1 id["vendor"] = 2 id["product"] = 3 id["version"] = 4 l.id = id id = l.id self.assertEqual(id["bustype"], 1) self.assertEqual(id["vendor"], 2) self.assertEqual(id["product"], 3) self.assertEqual(id["version"], 4) del id["bustype"] del id["vendor"] del id["product"] id["version"] = 5 l.id = id id = l.id self.assertEqual(id["bustype"], 1) self.assertEqual(id["vendor"], 2) self.assertEqual(id["product"], 3) self.assertEqual(id["version"], 5)
def test_prop_to_name_invalid(self): name = Libevdev.property_to_name(-1) self.assertIsNone(name) name = Libevdev.property_to_name(100) self.assertIsNone(name) with self.assertRaises(ctypes.ArgumentError): name = Libevdev.property_to_name("foo")
def testDeviceNode(self): dev = Libevdev() dev.name = "test device" dev.enable("EV_REL", "REL_X") dev.enable("EV_REL", "REL_Y") uinput = UinputDevice(dev) self.assertTrue(uinput.devnode.startswith("/dev/input/event"))
def testSyspath(self): dev = Libevdev() dev.name = "test device" dev.enable("EV_REL", "REL_X") dev.enable("EV_REL", "REL_Y") uinput = UinputDevice(dev) self.assertTrue(uinput.syspath.startswith("/sys/devices/virtual/input/input"))
def test_code_to_name_invalid(self): name = Libevdev.event_to_name(0, 1000) self.assertIsNone(name) name = Libevdev.event_to_name(0, -1) self.assertIsNone(name) with self.assertRaises(ctypes.ArgumentError): name = Libevdev.event_to_name(0, "foo")
def test_set_slot_value(self): l = Libevdev(self.fd) v = l.slot_value(l.current_slot, "ABS_MT_POSITION_X") v += 10 v2 = l.slot_value(l.current_slot, "ABS_MT_POSITION_X", v) self.assertEqual(v, v2) v2 = l.slot_value(l.current_slot, "ABS_MT_POSITION_X") self.assertEqual(v, v2)
def test_get_set_event_value(self): l = Libevdev(self.fd) v = l.event_value("EV_ABS", "ABS_Y") self.assertIsNotNone(v) v = l.event_value(0x03, 0x01, new_value=300) self.assertEqual(v, 300) v = l.event_value(0x03, 0x01) self.assertEqual(v, 300)
def test_absinfo(self): l = Libevdev(self.fd) a = l.absinfo("ABS_Y") self.assertTrue("minimum" in a) self.assertTrue("maximum" in a) self.assertTrue("resolution" in a) self.assertTrue("fuzz" in a) self.assertTrue("flat" in a) self.assertTrue("value" in a)
def is_identical(self, d1, d2): for t in range(Libevdev.event_to_value("EV_MAX")): max = Libevdev.type_max(t) if max is None: continue for c in range(max): if d1.has_event(t, c) != d1.has_event(t, c): return False return True
def test_set_clock_id(self): l = Libevdev(self.fd) try: import time clock = time.CLOCK_MONOTONIC except AttributeError: clock = 1 rc = l.set_clock_id(clock) self.assertEqual(rc, 0)
def test_init_fd(self): l = Libevdev(self.fd) fd = l.fd self.assertEqual(self.fd, fd) fd2 = open("/dev/input/event3", "rb") l.fd = fd2 fd = l.fd self.assertEqual(fd, fd2) fd2.close()
def test_prop_name(self): name = Libevdev.property_to_name(0) self.assertEqual(name, "INPUT_PROP_POINTER") prevname = None for i in range(5): name = Libevdev.property_to_name(i) self.assertIsNotNone(name) self.assertTrue(name.startswith("INPUT_PROP_")) self.assertNotEqual(prevname, name) prevname = name
def test_type_to_name(self): name = Libevdev.event_to_name(1) self.assertEqual(name, "EV_KEY") prevname = None for i in range(5): name = Libevdev.event_to_name(i) self.assertIsNotNone(name) self.assertTrue(name.startswith("EV_")) self.assertNotEqual(prevname, name) prevname = name
def test_set_get_name(self): l = Libevdev() name = l.name self.assertEqual(name, '') l.name = "foo" name = l.name self.assertEqual(name, "foo") l.name = None name = l.name self.assertEqual(name, "")
def test_set_absinfo(self): l = Libevdev(self.fd) real_a = l.absinfo("ABS_Y") a = l.absinfo("ABS_Y") a["minimum"] = 100 a["maximum"] = 200 a["fuzz"] = 300 a["flat"] = 400 a["resolution"] = 500 a["value"] = 600 a = l.absinfo("ABS_Y", new_values=a) self.assertEqual(a["minimum"], 100) self.assertEqual(a["maximum"], 200) self.assertEqual(a["fuzz"], 300) self.assertEqual(a["flat"], 400) self.assertEqual(a["resolution"], 500) self.assertEqual(a["value"], 600) l2 = Libevdev(self.fd) a2 = l2.absinfo("ABS_Y") self.assertNotEqual(a["minimum"], real_a["minimum"]) self.assertNotEqual(a["maximum"], real_a["maximum"]) self.assertNotEqual(a["fuzz"], real_a["fuzz"]) self.assertNotEqual(a["flat"], real_a["flat"]) self.assertNotEqual(a["resolution"], real_a["resolution"]) self.assertEqual(a2["value"], real_a["value"]) self.assertEqual(a2["minimum"], real_a["minimum"]) self.assertEqual(a2["maximum"], real_a["maximum"]) self.assertEqual(a2["fuzz"], real_a["fuzz"]) self.assertEqual(a2["flat"], real_a["flat"]) self.assertEqual(a2["resolution"], real_a["resolution"]) self.assertEqual(a2["value"], real_a["value"])
def test_set_get_phys(self): l = Libevdev() phys = l.phys self.assertIsNone(phys) l.phys = "foo" phys = l.phys self.assertEqual(phys, "foo") # libevdev issue: phys may be NULL (unlike the name) but we can't # set it to NULL. But the conversion code returns None for the empty # string, so let's test for that l.phys = None phys = l.phys self.assertEqual(phys, None)
def test_set_get_uniq(self): l = Libevdev() uniq = l.uniq self.assertIsNone(uniq) l.uniq = "foo" uniq = l.uniq self.assertEqual(uniq, "foo") # libevdev issue: phys may be NULL (unlike the name) but we can't # set it to NULL. But the conversion code returns None for the empty # string, so let's test for that l.uniq = None uniq = l.uniq self.assertEqual(uniq, None)
def test_ids(self): l = Libevdev(self.fd) id = l.id self.assertNotEqual(id["bustype"], 0) self.assertNotEqual(id["vendor"], 0) self.assertNotEqual(id["product"], 0) self.assertNotEqual(id["version"], 0)
def setUp(self): want_fd = None for i in range(20): try: fd = open("/dev/input/event{}".format(i), "rb") l = Libevdev(fd) if l.has_event("EV_ABS", "ABS_Y"): want_fd = fd break fd.close() except IOError: # Not all eventX nodes are guaranteed to exist pass self.assertIsNotNone(want_fd) self.fd = want_fd
def test_has_event(self): l = Libevdev(self.fd) self.assertTrue(l.has_event("EV_SYN", "SYN_REPORT")) type_supported = -1 for i in range(1, 5): if l.has_event(i): type_supported = i break self.assertGreater(type_supported, 0) codes_supported = 0 for i in range(150): if l.has_event(type_supported, i): codes_supported += 1 self.assertGreater(codes_supported, 0)
def testButton(self): dev = Libevdev() dev.name = "test device" dev.enable("EV_KEY", "BTN_LEFT") dev.enable("EV_KEY", "KEY_A") uinput = UinputDevice(dev) self.assertIsNotNone(uinput.devnode) with open(uinput.devnode) as f: newdev = Libevdev(f) self.assertTrue(self.is_identical(dev, newdev))
def test_has_property(self): """ Let's assume at least one between event0 and event10 is a device with at least one property set. May cause false negatives. """ props_supported = 0 for i in range(10): try: with open("/dev/input/event{}".format(i), "rb") as fd: l = Libevdev(fd) for p in range(6): if l.has_property(p): props_supported += 1 except IOError: # Not all eventX nodes are guaranteed to exist pass self.assertGreater(props_supported, 0)
def testRelative(self): dev = Libevdev() dev.name = "test device" dev.enable("EV_REL", "REL_X") dev.enable("EV_REL", "REL_Y") uinput = UinputDevice(dev) self.assertIsNotNone(uinput.devnode) with open(uinput.devnode) as f: newdev = Libevdev(f) self.assertTrue(self.is_identical(dev, newdev))
def testAbsolute(self): absinfo = {"minimum": 0, "maximum": 1} dev = Libevdev() dev.name = "test device" dev.enable("EV_ABS", "ABS_X", absinfo) dev.enable("EV_ABS", "ABS_Y", absinfo) uinput = UinputDevice(dev) self.assertIsNotNone(uinput.devnode) with open(uinput.devnode) as f: newdev = Libevdev(f) self.assertTrue(self.is_identical(dev, newdev))
def test_ctx_init(self): l = Libevdev() del l
def test_code_value_invalid(self): v = Libevdev.event_to_value("EV_REL", "KEY_ESC") self.assertIsNone(v)
def test_code_value(self): v = Libevdev.event_to_value("EV_REL", "REL_Y") self.assertEqual(v, 1) v = Libevdev.event_to_value(0, "SYN_DROPPED") self.assertEqual(v, 3)
def test_type_value_invalid(self): v = Libevdev.event_to_value("foo") self.assertIsNone(v) with self.assertRaises(AttributeError): v = Libevdev.event_to_value(0)
def test_type_to_value(self): v = Libevdev.event_to_value("EV_REL") self.assertEqual(v, 2)
def test_code_to_name(self): name = Libevdev.event_to_name(0, 0) self.assertEqual(name, "SYN_REPORT") name = Libevdev.event_to_name(1, 1) self.assertEqual(name, "KEY_ESC")