# Path: Peach/Utilities/ADB.py
# class AccessDebugBridge(object):
#     def __init__(self, adbPath="adb", deviceSerial=None, isEmulator=False):
#     def verifyADB(self):
#     def verifyDevice(self):
#     def runCmd(self, args):
#     def checkCmd(self, args, timeout=None):
#     def getProcessList(self):
#     def killProcess(self, appname, forceKill=False):
#     def reboot(self, wait=False):
#     def getCurrentTime(self):
#     def getDeviceInformation(self, directive="all"):
#     def makeDeviceReport(self):
#     def makeBugReport(self):
#     def getPID(self, procName):
#     def getPIDs(self, procName):
#     def shell(self, command):
#     def command(self, command):
# 
# Path: Peach/Utilities/network.py
# def cleanupSocketProcesses():
# def getUnboundPort(host="", minRange=30000, maxRange=35000, socket_type=1, assignOnlyOnce=False):
# def runHTTPDThread():
# 
# Path: Peach/agent.py
# class Monitor(object):
#     """
#     Extend from this to implement a Monitor.
#     Monitors are run by an Agent and must operate in an async manner.
#     Any blocking tasks must be performed in another thread.
# 
#     Agent: onTestStarting()
#     Agent: onTestFinished()
#     Agent: redoTest()
#     Agent: Sending redoTest result [False]
#     Agent: detectFault()
#     Agent: Detected fault!
#     Agent: Sending detectFault result [True]
#     Agent: getMonitorData()
#     Agent: onFault()
#     Agent: stopRun()
#     Agent: onShutdown()
#     """
# 
#     def __init__(self, args):
#         """
#         Arguments are supplied via the Peach XML file.
# 
#         @type	args: Dictionary
#         @param	args: Dictionary of parameters
#         """
#         self._name = None
# 
#     def OnTestStarting(self):
#         """Called right before start of test case or variation."""
#         pass
# 
#     def OnTestFinished(self):
#         """Called right after a test case or variation."""
#         pass
# 
#     def GetMonitorData(self):
#         """Get any monitored data from a test case."""
#         return None
# 
#     def RedoTest(self):
#         """Should the current test be re-performed."""
#         return False
# 
#     def DetectedFault(self):
#         """Check if a fault was detected."""
#         return False
# 
#     def OnFault(self):
#         """Called when a fault was detected."""
#         pass
# 
#     def OnShutdown(self):
#         """Called when Agent is shutting down, typically at end of a test run or when a Stop-Run occurs."""
#         pass
# 
#     def StopRun(self):
#         """Return True to force test run to fail. This should return True if an unrecoverable error occurs."""
#         return False
# 
#     def PublisherCall(self, method):
#         """Called when a call action is being performed.
#         Call actions are used to launch programs, this gives the monitor a chance to determine if it should be
#         running the program under a debugger instead. Note: This is a bit of a hack to get this working.
#         """
#         pass
# 
# def MonitorDebug(monitor, msg):
#     print("Monitor[%s]: %s" % (monitor, highlight.info(msg)))
# 
# Path: Peach/Utilities/Gaia/gaia.py
# class LockScreen(object):
# class GaiaApp(object):
# class GaiaApps(object):
# class GaiaDevice(object):
#     def __init__(self, marionette):
#     def is_locked(self):
#     def unlock(self):
#     def __init__(self, origin=None, name=None, frame=None, src=None):
#     def __init__(self, marionette):
#     def get_permission(self, app_name, permission_name):
#     def set_permission(self, app_name, permission_name, value):
#     def launch(self, name, switch_to_frame=True, url=None):
#     def uninstall(self, name):
#     def kill(self, app):
#     def kill_all(self):
#     def runningApps(self):
#     def switch_to_frame(self, app_frame, url=None, timeout=30):
#             def check(now):
#             def check(now):
#     def __init__(self, marionette):
#     def manager(self):
#     def is_android_build(self):
#     def push_file(self, source, count=1, destination='', progress=None):
#     def restart_b2g(self):
#     def start_b2g(self):
#     def stop_b2g(self):


import os
import sys
import time
import subprocess
from Peach.Utilities import ADB, network
from Peach.agent import Monitor, MonitorDebug
    from marionette import Marionette
from Peach.Utilities.Gaia import gaia
                [self.emulatorStartScript], cwd=os.path.dirname(self.emulatorStartScript), shell=True)

            # adb shell setprop net.dns1 10.0.2.3

            self._isBootFinished()
            self.monitoringProcessId = self.adb.getPID(self.monitoredProcessName)

            print("Forwarding TCP port %d ..." % self.forwardedPortADB)
            self.adb.command(["forward", "tcp:%d" % self.forwardedPortADB, "tcp:%d" % self.forwardedPortADB])

            self.isEmulatorInitialized = True

        time.sleep(20)

        if self.crashSuccess:
            print("Restarting %s ..." % self.monitoredProcessName)
            self.adb.killProcess(self.monitoredProcessName, True)
            time.sleep(40)
            self.monitoringProcessId = self.adb.getPID(self.monitoredProcessName)
            self.crashSuccess = False
            self.debugLogData = str()
            self.adb.checkCmd(["logcat", "-c"])

        print("Starting Marionette session ...")
        marionette = Marionette('localhost', self.forwardedPortADB)
        print(marionette.status())
        marionette.start_session()
        marionette.set_script_timeout(self.scriptTimeout)
        marionette.switch_to_frame()


# Path: Peach/Utilities/ADB.py
# class AccessDebugBridge(object):
#     def __init__(self, adbPath="adb", deviceSerial=None, isEmulator=False):
#     def verifyADB(self):
#     def verifyDevice(self):
#     def runCmd(self, args):
#     def checkCmd(self, args, timeout=None):
#     def getProcessList(self):
#     def killProcess(self, appname, forceKill=False):
#     def reboot(self, wait=False):
#     def getCurrentTime(self):
#     def getDeviceInformation(self, directive="all"):
#     def makeDeviceReport(self):
#     def makeBugReport(self):
#     def getPID(self, procName):
#     def getPIDs(self, procName):
#     def shell(self, command):
#     def command(self, command):
# 
# Path: Peach/Utilities/network.py
# def cleanupSocketProcesses():
# def getUnboundPort(host="", minRange=30000, maxRange=35000, socket_type=1, assignOnlyOnce=False):
# def runHTTPDThread():
# 
# Path: Peach/agent.py
# class Monitor(object):
#     """
#     Extend from this to implement a Monitor.
#     Monitors are run by an Agent and must operate in an async manner.
#     Any blocking tasks must be performed in another thread.
# 
#     Agent: onTestStarting()
#     Agent: onTestFinished()
#     Agent: redoTest()
#     Agent: Sending redoTest result [False]
#     Agent: detectFault()
#     Agent: Detected fault!
#     Agent: Sending detectFault result [True]
#     Agent: getMonitorData()
#     Agent: onFault()
#     Agent: stopRun()
#     Agent: onShutdown()
#     """
# 
#     def __init__(self, args):
#         """
#         Arguments are supplied via the Peach XML file.
# 
#         @type	args: Dictionary
#         @param	args: Dictionary of parameters
#         """
#         self._name = None
# 
#     def OnTestStarting(self):
#         """Called right before start of test case or variation."""
#         pass
# 
#     def OnTestFinished(self):
#         """Called right after a test case or variation."""
#         pass
# 
#     def GetMonitorData(self):
#         """Get any monitored data from a test case."""
#         return None
# 
#     def RedoTest(self):
#         """Should the current test be re-performed."""
#         return False
# 
#     def DetectedFault(self):
#         """Check if a fault was detected."""
#         return False
# 
#     def OnFault(self):
#         """Called when a fault was detected."""
#         pass
# 
#     def OnShutdown(self):
#         """Called when Agent is shutting down, typically at end of a test run or when a Stop-Run occurs."""
#         pass
# 
#     def StopRun(self):
#         """Return True to force test run to fail. This should return True if an unrecoverable error occurs."""
#         return False
# 
#     def PublisherCall(self, method):
#         """Called when a call action is being performed.
#         Call actions are used to launch programs, this gives the monitor a chance to determine if it should be
#         running the program under a debugger instead. Note: This is a bit of a hack to get this working.
#         """
#         pass
# 
# def MonitorDebug(monitor, msg):
#     print("Monitor[%s]: %s" % (monitor, highlight.info(msg)))
# 
# Path: Peach/Utilities/Gaia/gaia.py
# class LockScreen(object):
# class GaiaApp(object):
# class GaiaApps(object):
# class GaiaDevice(object):
#     def __init__(self, marionette):
#     def is_locked(self):
#     def unlock(self):
#     def __init__(self, origin=None, name=None, frame=None, src=None):
#     def __init__(self, marionette):
#     def get_permission(self, app_name, permission_name):
#     def set_permission(self, app_name, permission_name, value):
#     def launch(self, name, switch_to_frame=True, url=None):
#     def uninstall(self, name):
#     def kill(self, app):
#     def kill_all(self):
#     def runningApps(self):
#     def switch_to_frame(self, app_frame, url=None, timeout=30):
#             def check(now):
#             def check(now):
#     def __init__(self, marionette):
#     def manager(self):
#     def is_android_build(self):
#     def push_file(self, source, count=1, destination='', progress=None):
#     def restart_b2g(self):
#     def start_b2g(self):
#     def stop_b2g(self):


# Path: Peach/Agent/b2g.py
import os
import sys
import time
import subprocess
from Peach.Utilities import ADB, network
from Peach.agent import Monitor, MonitorDebug
    from marionette import Marionette
from Peach.Utilities.Gaia import gaia

                [self.emulatorStartScript], cwd=os.path.dirname(self.emulatorStartScript), shell=True)

            # adb shell setprop net.dns1 10.0.2.3

            self._isBootFinished()
            self.monitoringProcessId = self.adb.getPID(self.monitoredProcessName)

            print("Forwarding TCP port %d ..." % self.forwardedPortADB)
            self.adb.command(["forward", "tcp:%d" % self.forwardedPortADB, "tcp:%d" % self.forwardedPortADB])

            self.isEmulatorInitialized = True

        time.sleep(20)

        if self.crashSuccess:
            print("Restarting %s ..." % self.monitoredProcessName)
            self.adb.killProcess(self.monitoredProcessName, True)
            time.sleep(40)
            self.monitoringProcessId = self.adb.getPID(self.monitoredProcessName)
            self.crashSuccess = False
            self.debugLogData = str()
            self.adb.checkCmd(["logcat", "-c"])

        print("Starting Marionette session ...")
        marionette = Marionette('localhost', self.forwardedPortADB)
        print(marionette.status())
        marionette.start_session()
        marionette.set_script_timeout(self.scriptTimeout)
        marionette.switch_to_frame()


        lock = gaia.LockScreen(marionette)