| OLD | NEW |
| 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 # DESCRIPTION : | 6 # DESCRIPTION : |
| 7 # | 7 # |
| 8 # This factory test allows execution of a test-based script, with the | 8 # This factory test allows execution of a test-based script, with the |
| 9 # stdout of the script displayed in a the testing widget via gtk | 9 # stdout of the script displayed in a the testing widget via gtk |
| 10 # label. Keyboard input will be passed to the script via its stdin. | 10 # label. Keyboard input will be passed to the script via its stdin. |
| 11 | 11 |
| 12 from autotest_lib.client.bin import test | |
| 13 from autotest_lib.client.common_lib import error | |
| 14 from autotest_lib.client.common_lib import factory_test | |
| 15 | |
| 16 import gobject | 12 import gobject |
| 17 import gtk | 13 import gtk |
| 18 import pango | 14 import pango |
| 19 import sys | 15 import sys |
| 20 import subprocess | 16 import subprocess |
| 21 | 17 |
| 18 from gtk import gdk |
| 19 |
| 20 from autotest_lib.client.bin import factory |
| 21 from autotest_lib.client.bin import factory_test |
| 22 from autotest_lib.client.bin import test |
| 23 from autotest_lib.client.common_lib import error |
| 24 from autotest_lib.client.common_lib import pexpect |
| 25 |
| 26 |
| 27 _MAX_LABEL_CHARS=256 |
| 28 |
| 22 | 29 |
| 23 class Script: | 30 class Script: |
| 24 | 31 |
| 25 def __init__(self, cmdline, label): | 32 def __init__(self, cmdline, label): |
| 26 self._cmdline | 33 self._cmdline = cmdline |
| 27 self._label = label | 34 self._label = label |
| 28 self._proc = subprocess.Popen(cmdline.split(), | 35 self._ibuf = '' |
| 29 stdin=subprocess.PIPE, | 36 self._proc = pexpect.spawn(cmdline) |
| 30 stdout=subprocess.PIPE) | 37 gobject.io_add_watch(self._proc.fileno(), gobject.IO_IN, self.recv) |
| 31 gobject.io_add_watch(self._proc.stdout, gobject.IO_IN, self.recv) | |
| 32 | 38 |
| 33 def recv(self, src, cond): | 39 def recv(self, src, cond): |
| 34 msg = self._proc.stdout.read() | 40 msg = self._proc.read_nonblocking(_MAX_LABEL_CHARS) |
| 41 factory.log('recv script msg %s' % repr(msg)) |
| 35 self._label.set_text(msg) | 42 self._label.set_text(msg) |
| 36 self._label.queue_draw() | 43 self._label.queue_draw() |
| 37 returncode = self._proc.poll() | 44 if not self._proc.isalive(): |
| 38 if returncode is not None: | 45 self._proc.close() |
| 39 gtk.main_quit() | 46 gtk.main_quit() |
| 40 if returncode is not 0: | 47 if self._proc.exitstatus is not 0: |
| 41 error.TestFail('%s script returned %d' % | 48 error.TestFail('%s script return code was %d' % |
| 42 (self._cmdline, returncode)) | 49 (self._cmdline, self._proc.exitstatus)) |
| 43 return True | 50 return True |
| 44 | 51 |
| 45 def send(self, msg): | 52 def send(self, char): |
| 46 print >> self._proc.stdin, msg | 53 if char != '\n': |
| 47 self._proc.stdin.flush() | 54 self._ibuf += char |
| 48 | |
| 49 def quit(self): | |
| 50 if self._proc.poll() is None: | |
| 51 return | 55 return |
| 52 factory_test.XXX('killing Script') | 56 factory.log('sending script %s' % repr(self._ibuf)) |
| 53 self._proc.kill() | 57 self._proc.sendline(self._ibuf) |
| 58 self._ibuf = '' |
| 54 | 59 |
| 55 | 60 |
| 56 class factory_ScriptWrapper(test.test): | 61 class factory_ScriptWrapper(test.test): |
| 57 version = 1 | 62 version = 1 |
| 58 | 63 |
| 59 def key_release_callback(self, widget, event): | 64 def key_release_callback(self, widget, event): |
| 60 char = event.keyval in range(32,127) and chr(event.keyval) or None | 65 char = event.keyval in range(32,127) and chr(event.keyval) or None |
| 61 factory_test.XXX_log('key_release_callback %s(%s)' % | 66 char = event.keyval == gdk.keyval_from_name('Return') and '\n' or char |
| 62 (event.keyval, char)) | 67 if not self._ft_state.exit_on_trigger(event) and char is not None: |
| 63 if not factory_test.test_switch_on_trigger(event): | |
| 64 self._script.send(char) | 68 self._script.send(char) |
| 65 return True | 69 return True |
| 66 | 70 |
| 67 def register_callbacks(self, window): | 71 def register_callbacks(self, window): |
| 68 window.connect('key-release-event', self.key_release_callback) | 72 window.connect('key-release-event', self.key_release_callback) |
| 69 window.add_events(gtk.gdk.KEY_RELEASE_MASK) | 73 window.add_events(gtk.gdk.KEY_RELEASE_MASK) |
| 70 | 74 |
| 71 def run_once(self, test_widget_size=None, trigger_set=None, | 75 def run_once(self, |
| 72 result_file_path=None, cmdline=None): | 76 test_widget_size=None, |
| 77 trigger_set=None, |
| 78 result_file_path=None, |
| 79 cmdline=None): |
| 73 | 80 |
| 74 factory_test.XXX_log('factory_ScriptWrapper') | 81 factory.log('%s run_once' % self.__class__) |
| 75 | 82 |
| 76 factory_test.init(trigger_set=trigger_set, | 83 self._ft_state = factory_test.State( |
| 77 result_file_path=result_file_path) | 84 trigger_set=trigger_set, |
| 85 result_file_path=result_file_path) |
| 78 | 86 |
| 79 label = gtk.Label('') | 87 label = gtk.Label('') |
| 80 label.modify_font(pango.FontDescription('courier new condensed 16')) | 88 label.modify_font(pango.FontDescription('courier new condensed 16')) |
| 81 label.set_alignment(0.5, 0.5) | 89 label.set_alignment(0.5, 0.5) |
| 82 label.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse('light green')) | 90 label.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse('light green')) |
| 83 | 91 |
| 84 test_widget = gtk.EventBox() | 92 test_widget = gtk.EventBox() |
| 85 test_widget.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('black')) | 93 test_widget.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('black')) |
| 86 test_widget.add(label) | 94 test_widget.add(label) |
| 87 | 95 |
| 88 self._script = Script(cmdline, label) | 96 self._script = Script(cmdline, label) |
| 89 | 97 |
| 90 factory_test.run_test_widget( | 98 self._ft_state.run_test_widget( |
| 91 test_widget=test_widget, | 99 test_widget=test_widget, |
| 92 test_widget_size=test_widget_size, | 100 test_widget_size=test_widget_size, |
| 93 window_registration_callback=self.register_callbacks, | 101 window_registration_callback=self.register_callbacks) |
| 94 cleanup_callback=self._script.quit) | |
| 95 | 102 |
| 96 factory_test.XXX_log('exiting factory_ScriptWrapper') | 103 factory.log('%s run_once finished' % self.__class__) |
| OLD | NEW |