| 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 import logging, os, utils, signal, time | 5 import logging, os, utils, signal, time |
| 6 from autotest_lib.client.bin import chromeos_constants, test | 6 from autotest_lib.client.bin import chromeos_constants, test |
| 7 from autotest_lib.client.common_lib import error, site_ui | 7 from autotest_lib.client.common_lib import error, site_ui |
| 8 | 8 |
| 9 | 9 |
| 10 def setup_autox(test): | 10 def setup_autox(test): |
| 11 test.job.setup_dep(['autox']) | 11 test.job.setup_dep(['autox']) |
| 12 # create a empty srcdir to prevent the error that checks .version file | 12 # create a empty srcdir to prevent the error that checks .version file |
| 13 if not os.path.exists(test.srcdir): | 13 if not os.path.exists(test.srcdir): |
| 14 os.mkdir(test.srcdir) | 14 os.mkdir(test.srcdir) |
| 15 | 15 |
| 16 | 16 |
| 17 def logged_in(): | 17 def logged_in(): |
| 18 # this file is created when the session_manager emits start-user-session | 18 # this file is created when the session_manager emits start-user-session |
| 19 # and removed when the session_manager emits stop-user-session | 19 # and removed when the session_manager emits stop-user-session |
| 20 return os.path.exists(chromeos_constants.LOGGED_IN_MAGIC_FILE) | 20 return os.path.exists(chromeos_constants.LOGGED_IN_MAGIC_FILE) |
| 21 | 21 |
| 22 | 22 |
| 23 def attempt_login(test, script_file, timeout = 10): | 23 def attempt_login(test, script_file, timeout=10): |
| 24 dep = 'autox' | 24 dep = 'autox' |
| 25 dep_dir = os.path.join(test.autodir, 'deps', dep) | 25 dep_dir = os.path.join(test.autodir, 'deps', dep) |
| 26 test.job.install_pkg(dep, 'dep', dep_dir) | 26 test.job.install_pkg(dep, 'dep', dep_dir) |
| 27 | 27 |
| 28 autox_binary = '%s/%s' % (dep_dir, 'autox') | 28 autox_binary = '%s/%s' % (dep_dir, 'autox') |
| 29 autox_script = os.path.join(test.job.configdir, script_file) | 29 autox_script = os.path.join(test.job.configdir, script_file) |
| 30 | 30 |
| 31 try: | 31 try: |
| 32 utils.system(site_ui.xcommand('%s %s' % (autox_binary, autox_script))) | 32 utils.system(site_ui.xcommand('%s %s' % (autox_binary, autox_script))) |
| 33 except error.CmdError, e: | 33 except error.CmdError, e: |
| 34 logging.debug(e) | 34 logging.debug(e) |
| 35 raise error.TestFail('AutoX program failed to login for test user') | 35 raise error.TestFail('AutoX program failed to login for test user') |
| 36 | 36 |
| 37 start_time = time.time() | 37 start_time = time.time() |
| 38 while time.time() - start_time < timeout: | 38 while time.time() - start_time < timeout: |
| 39 if logged_in(): | 39 if logged_in(): |
| 40 break | 40 break |
| 41 time.sleep(1) | 41 time.sleep(1) |
| 42 else: | 42 else: |
| 43 return False | 43 return False |
| 44 return True | 44 return True |
| 45 | 45 |
| 46 | 46 |
| 47 def attempt_logout(timeout = 10): | 47 def attempt_logout(timeout=10): |
| 48 # Gracefully exiting chrome causes the user's session to end. | 48 # Gracefully exiting chrome causes the user's session to end. |
| 49 utils.system('pkill -TERM ^%s$' % chromeos_constants.BROWSER) | 49 utils.system('pkill -TERM -o ^%s$' % chromeos_constants.BROWSER) |
| 50 start_time = time.time() | 50 start_time = time.time() |
| 51 while time.time() - start_time < timeout: | 51 while time.time() - start_time < timeout: |
| 52 if not logged_in(): | 52 if not logged_in(): |
| 53 break | 53 break |
| 54 time.sleep(1) | 54 time.sleep(1) |
| 55 else: | 55 else: |
| 56 return False | 56 return False |
| 57 return True | 57 return True |
| 58 | 58 |
| 59 | 59 |
| 60 def wait_for_browser(timeout = 10): | 60 def wait_for_browser(timeout=10): |
| 61 # Wait until the login manager is back up before trying to use it. | 61 # Wait until the login manager is back up before trying to use it. |
| 62 # I don't use utils.system here because I don't want to fail | 62 # I don't use utils.system here because I don't want to fail |
| 63 # if pgrep returns non-zero, I just want to wait and try again. | 63 # if pgrep returns non-zero, I just want to wait and try again. |
| 64 start_time = time.time() | 64 start_time = time.time() |
| 65 while time.time() - start_time < timeout: | 65 while time.time() - start_time < timeout: |
| 66 if os.system('pgrep ^%s$' % chromeos_constants.BROWSER): | 66 if os.system('pgrep ^%s$' % chromeos_constants.BROWSER): |
| 67 break; | 67 break; |
| 68 time.sleep(1) | 68 time.sleep(1) |
| 69 else: | 69 else: |
| 70 return False | 70 return False |
| 71 return True | 71 return True |
| 72 | 72 |
| 73 | 73 |
| 74 def wait_for_screensaver(timeout = 10, raise_error = True): | 74 def wait_for_screensaver(timeout=10, raise_error=True): |
| 75 # Wait until the screensaver starts | 75 # Wait until the screensaver starts |
| 76 start_time = time.time() | 76 start_time = time.time() |
| 77 while time.time() - start_time < timeout: | 77 while time.time() - start_time < timeout: |
| 78 if 0 == os.system(site_ui.xcommand('xscreensaver-command -version')): | 78 if 0 == os.system(site_ui.xcommand('xscreensaver-command -version')): |
| 79 break | 79 break |
| 80 time.sleep(1) | 80 time.sleep(1) |
| 81 else: | 81 else: |
| 82 if raise_error: | 82 if raise_error: |
| 83 raise error.TestFail('Unable to communicate with ' + | 83 raise error.TestFail('Unable to communicate with ' + |
| 84 'xscreensaver after %i seconds' % | 84 'xscreensaver after %i seconds' % |
| 85 time.time() - start_time) | 85 time.time() - start_time) |
| 86 return False | 86 return False |
| 87 | 87 |
| 88 return True | 88 return True |
| 89 | 89 |
| 90 | 90 |
| 91 def nuke_login_manager(): | 91 def nuke_login_manager(): |
| 92 nuke_process_by_name('session_manager') | 92 nuke_process_by_name('session_manager') |
| 93 wait_for_browser() | 93 wait_for_browser() |
| 94 | 94 |
| 95 def nuke_process_by_name(name, with_prejudice=False): | 95 def nuke_process_by_name(name, with_prejudice=False): |
| 96 pid = int(utils.system_output('pgrep -o ^%s$' % name)) | 96 pid = int(utils.system_output('pgrep -o ^%s$' % name)) |
| 97 if with_prejudice: | 97 if with_prejudice: |
| 98 utils.nuke_pid(pid, [signal.SIGKILL]) | 98 utils.nuke_pid(pid, [signal.SIGKILL]) |
| 99 else: | 99 else: |
| 100 utils.nuke_pid(pid) | 100 utils.nuke_pid(pid) |
| OLD | NEW |