blob: 7317e9f01750af44ece3924b698b30d93d547c5b [file] [log] [blame]
# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# This factory test allows execution of a test-based script, with the
# stdout of the script displayed in a the testing widget via gtk
# label. Keyboard input will be passed to the script via its stdin.
import imp
import gobject
import gtk
import pango
import sys
import subprocess
from gtk import gdk
from autotest_lib.client.bin import test
from autotest_lib.client.common_lib import error
from autotest_lib.client.common_lib import pexpect
from autotest_lib.client.cros import factory_setup_modules
from cros.factory.test import factory
from cros.factory.test import ui as ful
class Script:
def __init__(self, cmdline, pexpect, label):
self._cmdline = cmdline
self._label = label
self._ibuf = ''
self._proc = pexpect.spawn(cmdline)
gobject.io_add_watch(self._proc.fileno(), gobject.IO_IN, self.recv)
def recv(self, src, cond):
msg = self._proc.read_nonblocking(_MAX_LABEL_CHARS)
factory.log('recv script msg %s' % repr(msg))
if not self._proc.isalive():
if self._proc.exitstatus is not 0:
error.TestFail('%s script return code was %d' %
(self._cmdline, self._proc.exitstatus))
return True
def send(self, char):
if char != '\n':
self._ibuf += char
factory.log('sending script %s' % repr(self._ibuf))
self._ibuf = ''
class factory_ScriptWrapper(test.test):
version = 1
def key_release_callback(self, widget, event):
char = event.keyval in range(32,127) and chr(event.keyval) or None
char = event.keyval == gdk.keyval_from_name('Return') and '\n' or char
if char is not None:
return True
def register_callbacks(self, window):
window.connect('key-release-event', self.key_release_callback)
def run_once(self, cmdline=None):
factory.log('%s run_once' % self.__class__)
label = ful.make_label('', alignment=(0.5, 0.5))
test_widget = gtk.EventBox()
test_widget.modify_bg(gtk.STATE_NORMAL, ful.BLACK)
self._script = Script(cmdline, pexpect, label)
ful.run_test_widget(self.job, test_widget,
factory.log('%s run_once finished' % self.__class__)