# This program is free software: you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation, either version 3 of the License.# # This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.# # You should have received a copy of the GNU General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.import gtkimport gobjectimport randomdef b_clicked_cb(button): clicked = True button.props.label = "Ouch!"def c_clicked_cb(button): def set_label_async(): c.props.label = "Ouch!" gobject.timeout_add(random.randint(500, 5000), set_label_async)w = gtk.Window()v = gtk.VBox()b = gtk.Button("Click me")b.connect("clicked", b_clicked_cb)v.pack_start(b)c = gtk.Button("Async Operation")c.connect("clicked", c_clicked_cb)c.show()v.pack_start(c)w.add(v)w.show_all()class Sleep(object): def __init__(self, timeout=1000): self.timeout = timeout def schedule(self, iterator): gobject.timeout_add(self.timeout, run_test_case, iterator)class WaitForSignal(object): def __init__(self, obj, signame): self.obj = obj self.signame = signame self.iterator = None self.sigid = None def schedule(self, iterator): self.sigid = self.obj.connect(self.signame, self._handler) self.iterator = iterator def _handler(self, *args): run_test_case(self.iterator) self.obj.disconnect(self.sigid)def test_case(): b.activate() yield Sleep() assert b.props.label == "Ouch!" c.activate() yield WaitForSignal(c, "notify::label") assert c.props.label == "Ouch!"def run_test_case(iterator): print "Tick" try: scheduler = iterator.next() except StopIteration: print "Test Case Finished Successfully" gtk.main_quit() return False except Exception, e: print "An error occured" gtk.main_quit() return False scheduler.schedule(iterator) return Falsegobject.timeout_add(1000, run_test_case, test_case())gtk.main()