Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate

Advertising

Anonymous
Saturday, November 27th, 2010 at 1:34:36am UTC 

  1. # This program is free software: you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation, either version 3 of the License.
  4. #
  5. # This program is distributed in the hope that it will be useful,
  6. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  8. # GNU General Public License for more details.
  9. #
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  12.  
  13. import gtk
  14. import gobject
  15. import random
  16.  
  17. def b_clicked_cb(button):
  18.     clicked = True
  19.     button.props.label = "Ouch!"
  20.  
  21. def c_clicked_cb(button):
  22.     def set_label_async():
  23.         c.props.label = "Ouch!"
  24.  
  25.     gobject.timeout_add(random.randint(500, 5000),
  26.         set_label_async)
  27.  
  28. w = gtk.Window()
  29. v = gtk.VBox()
  30. b = gtk.Button("Click me")
  31. b.connect("clicked", b_clicked_cb)
  32. v.pack_start(b)
  33. c = gtk.Button("Async Operation")
  34. c.connect("clicked", c_clicked_cb)
  35. c.show()
  36. v.pack_start(c)
  37. w.add(v)
  38. w.show_all()
  39.  
  40. class Sleep(object):
  41.  
  42.     def __init__(self, timeout=1000):
  43.         self.timeout = timeout
  44.  
  45.     def schedule(self, iterator):
  46.         gobject.timeout_add(self.timeout, run_test_case, iterator)
  47.  
  48. class WaitForSignal(object):
  49.  
  50.     def __init__(self, obj, signame):
  51.         self.obj = obj
  52.         self.signame = signame
  53.         self.iterator = None
  54.         self.sigid = None
  55.  
  56.     def schedule(self, iterator):
  57.         self.sigid = self.obj.connect(self.signame, self._handler)
  58.         self.iterator = iterator
  59.  
  60.     def _handler(self, *args):
  61.         run_test_case(self.iterator)
  62.         self.obj.disconnect(self.sigid)
  63.  
  64. def test_case():
  65.     b.activate()
  66.     yield Sleep()
  67.     assert b.props.label == "Ouch!"
  68.  
  69.     c.activate()
  70.     yield WaitForSignal(c, "notify::label")
  71.     assert c.props.label == "Ouch!"
  72.  
  73. def run_test_case(iterator):
  74.     print "Tick"
  75.     try:
  76.         scheduler = iterator.next()
  77.  
  78.     except StopIteration:
  79.         print "Test Case Finished Successfully"
  80.         gtk.main_quit()
  81.         return False
  82.  
  83.     except Exception, e:
  84.         print "An error occured"
  85.         gtk.main_quit()
  86.         return False
  87.  
  88.     scheduler.schedule(iterator)
  89.     return False
  90.  
  91. gobject.timeout_add(1000, run_test_case, test_case())
  92. gtk.main()

advertising

Update the Post

Soit mettez à jour ce message et ré-envoyez-le avec les modifications, soit créer un nouveau message.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will not expire by default. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right
worth-right