1#!/usr/bin/python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6
7import sys
8import time
9
10
11class RPCListener(object):
12
13  def __init__(self, shutdown_callback):
14    self.shutdown_callback = shutdown_callback
15    self.prefix = '|||| '
16    self.ever_failed = False
17    self.start_time = time.time()
18
19  def Log(self, message):
20    # Display the number of milliseconds since startup.
21    # This gives us additional data for debugging bot behavior.
22    prefix = '[%6s ms] ' % int((time.time()-self.start_time)*1000) + self.prefix
23    lines = [line.rstrip() for line in message.split('\n')]
24    text = ''.join(['%s%s\n' % (prefix, line) for line in lines])
25    sys.stdout.write(text)
26
27  def TestLog(self, message):
28    self.Log(message)
29    return 'OK'
30
31  # Something went very wrong on the server side, everything is horked?
32  # Only called locally.
33  def ServerError(self, message):
34    self.Log('\n[SERVER_ERROR] %s' % (message,))
35    self.ever_failed = True
36    self._TestingDone()
37    return 'OK'
38
39  # Does nothing.  Called to prevent timeouts.  (The server resets the timeout
40  # every time it receives a GET request.)
41  def Ping(self):
42    return 'OK'
43
44  # This happens automatically, as long as the renderer process has not crashed.
45  def JavaScriptIsAlive(self):
46    return 'OK'
47
48  def Shutdown(self, message, passed):
49    self.Log(message)
50    # This check looks slightly backwards, but this is intentional.
51    # Everything but passed.lower() == 'true' is considered a failure.  This
52    # means that if the test runner sends garbage, it will be a failure.
53    # NOTE in interactive mode this function may be called multiple times.
54    # ever_failed is designed to be set and never reset - if any of the runs
55    # fail, the an error code will be returned to the command line.
56    # In summary, the tester is biased towards failure - it should scream "FAIL"
57    # if things are not 100% correct.  False positives must be avoided.
58    if passed.lower() != 'true':
59      self.ever_failed = True
60    close_browser = self._TestingDone()
61    if close_browser:
62      return 'Die, please'
63    else:
64      return 'OK'
65
66  def _TestingDone(self):
67    return self.shutdown_callback()
68