Rework mock ps start/stop; test BuddyAppeared signals

This commit is contained in:
Dan Williams 2007-04-23 13:12:10 -04:00
parent 86ab6ca36b
commit 857aa1dcbf
2 changed files with 113 additions and 27 deletions

View File

@ -180,6 +180,7 @@ class TestOwner(TestBuddy):
_PRESENCE_SERVICE = "org.laptop.Sugar.Presence" _PRESENCE_SERVICE = "org.laptop.Sugar.Presence"
_PRESENCE_INTERFACE = "org.laptop.Sugar.Presence" _PRESENCE_INTERFACE = "org.laptop.Sugar.Presence"
_PRESENCE_TEST_INTERFACE = "org.laptop.Sugar.Presence._Test"
_PRESENCE_PATH = "/org/laptop/Sugar/Presence" _PRESENCE_PATH = "/org/laptop/Sugar/Presence"
class TestPresenceService(dbus.service.Object): class TestPresenceService(dbus.service.Object):
@ -269,6 +270,24 @@ class TestPresenceService(dbus.service.Object):
def GetPreferredConnection(self): def GetPreferredConnection(self):
return "bar.baz.foo", "/bar/baz/foo" return "bar.baz.foo", "/bar/baz/foo"
# Private methods used for testing
@dbus.service.method(_PRESENCE_TEST_INTERFACE, in_signature="ayss")
def AddBuddy(self, pubkey, nick, color):
pubkey = ''.join([chr(item) for item in pubkey])
objid = self._get_next_object_id()
buddy = TestBuddy(self._bus_name, objid, pubkey, nick, color)
self._buddies[pubkey] = buddy
self.BuddyAppeared(buddy._object_path)
@dbus.service.method(_PRESENCE_TEST_INTERFACE, in_signature="ay")
def RemoveBuddy(self, pubkey):
pubkey = ''.join([chr(item) for item in pubkey])
if self._buddies.has_key(pubkey):
del self._buddies[pubkey]
self.BuddyDisappeared(buddy._object_path)
return
raise NotFoundError("Buddy not found")
def main(): def main():
loop = gobject.MainLoop() loop = gobject.MainLoop()
ps = TestPresenceService() ps = TestPresenceService()

View File

@ -23,34 +23,42 @@ from sugar.presence import presenceservice
import mockps import mockps
class PSBindingsTestCase(unittest.TestCase): def start_ps():
def setUp(self):
argv = ["mockps.py", "mockps.py"] argv = ["mockps.py", "mockps.py"]
(self._pspid, stdin, stdout, stderr) = gobject.spawn_async(argv, flags=gobject.SPAWN_LEAVE_DESCRIPTORS_OPEN) (pid, stdin, stdout, stderr) = gobject.spawn_async(argv, flags=gobject.SPAWN_LEAVE_DESCRIPTORS_OPEN)
print "Presence service started, pid %d" % self._pspid
# Wait until it shows up on the bus # Wait until it shows up on the bus
tries = 0 tries = 0
bus = dbus.SessionBus() bus = dbus.SessionBus()
while tries < 5: while tries < 10:
time.sleep(0.5)
bus_object = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus') bus_object = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
try: try:
if bus_object.GetNameOwner(presenceservice.DBUS_SERVICE, dbus_interface='org.freedesktop.DBus'): if bus_object.GetNameOwner(presenceservice.DBUS_SERVICE, dbus_interface='org.freedesktop.DBus'):
break break
except dbus.exceptions.DBusException, err: except dbus.exceptions.DBusException, err:
pass pass
time.sleep(1)
tries += 1 tries += 1
if tries >= 5: if tries >= 5:
self.tearDown() stop_ps(pid)
raise RuntimeError("Couldn't start the mock presence service") raise RuntimeError("Couldn't start the mock presence service")
return pid
def stop_ps(pid):
if pid >= 0:
os.kill(pid, 15)
class BuddyTests(unittest.TestCase):
def setUp(self):
self._pspid = start_ps()
def tearDown(self): def tearDown(self):
if self._pspid >= 0: if self._pspid > 0:
os.kill(self._pspid, 15) stop_ps(self._pspid)
self._pspid = -1 self._pspid = -1
print "Presence service stopped."
def _handle_error(self, err, user_data): def _handle_error(self, err, user_data):
user_data["success"] = False user_data["success"] = False
@ -59,7 +67,7 @@ class PSBindingsTestCase(unittest.TestCase):
def _testOwner_helper(self, user_data): def _testOwner_helper(self, user_data):
try: try:
ps = presenceservice.PresenceService(False) ps = presenceservice.get_instance(False)
except RuntimeError, err: except RuntimeError, err:
self._handle_error(err, user_data) self._handle_error(err, user_data)
return False return False
@ -88,13 +96,72 @@ class PSBindingsTestCase(unittest.TestCase):
assert owner.props.nick == mockps._OWNER_NICK, "Owner nickname doesn't match expected" assert owner.props.nick == mockps._OWNER_NICK, "Owner nickname doesn't match expected"
assert owner.props.color == mockps._OWNER_COLOR, "Owner color doesn't match expected" assert owner.props.color == mockps._OWNER_COLOR, "Owner color doesn't match expected"
_BA_PUBKEY = "akjadskjjfahfdahfdsahjfhfewaew3253232832832q098qewa98fdsafa98fa"
_BA_NICK = "BuddyAppearedTestBuddy"
_BA_COLOR = "#23adfb,#56bb11"
def _testBuddyAppeared_helper_timeout(self, user_data):
self._handle_error("Timeout waiting for buddy-appeared signal", user_data)
return False
def _testBuddyAppeared_helper_cb(self, ps, buddy, user_data):
user_data["buddy"] = buddy
user_data["success"] = True
gtk.main_quit()
def _testBuddyAppeared_helper(self, user_data):
ps = presenceservice.get_instance(False)
ps.connect('buddy-appeared', self._testBuddyAppeared_helper_cb, user_data)
# Wait 5 seconds max for signal to be emitted
gobject.timeout_add(5000, self._testBuddyAppeared_helper_timeout, user_data)
busobj = dbus.SessionBus().get_object(mockps._PRESENCE_SERVICE,
mockps._PRESENCE_PATH)
try:
testps = dbus.Interface(busobj, mockps._PRESENCE_TEST_INTERFACE)
except dbus.exceptions.DBusException, err:
self._handle_error(err, user_data)
return False
try:
testps.AddBuddy(self._BA_PUBKEY, self._BA_NICK, self._BA_COLOR)
except dbus.exceptions.DBusException, err:
self._handle_error(err, user_data)
return False
return False
def testBuddyAppeared(self):
ps = presenceservice.get_instance(False)
assert ps, "Couldn't get presence service"
user_data = {"success": False, "err": "", "buddy": None, "ps": ps}
gobject.idle_add(self._testBuddyAppeared_helper, user_data)
gtk.main()
assert user_data["success"] == True, user_data["err"]
assert user_data["buddy"], "Buddy was not received"
buddy = user_data["buddy"]
assert buddy.props.key == self._BA_PUBKEY, "Public key doesn't match expected"
assert buddy.props.nick == self._BA_NICK, "Nickname doesn't match expected"
assert buddy.props.color == self._BA_COLOR, "Color doesn't match expected"
# Try to get buddy by public key
buddy2 = ps.get_buddy(self._BA_PUBKEY)
assert buddy2, "Couldn't get buddy by public key"
assert buddy2.props.key == self._BA_PUBKEY, "Public key doesn't match expected"
assert buddy2.props.nick == self._BA_NICK, "Nickname doesn't match expected"
assert buddy2.props.color == self._BA_COLOR, "Color doesn't match expected"
def addToSuite(suite): def addToSuite(suite):
suite.addTest(PSBindingsTestCase("testOwner")) suite.addTest(BuddyTests("testOwner"))
suite.addTest(BuddyTests("testBuddyAppeared"))
addToSuite = staticmethod(addToSuite) addToSuite = staticmethod(addToSuite)
def main(): def main():
suite = unittest.TestSuite() suite = unittest.TestSuite()
PSBindingsTestCase.addToSuite(suite) BuddyTests.addToSuite(suite)
runner = unittest.TextTestRunner() runner = unittest.TextTestRunner()
runner.run(suite) runner.run(suite)