Index: tools/chrome_remote_control/chrome_remote_control/system_stub.py |
diff --git a/tools/chrome_remote_control/chrome_remote_control/system_stub.py b/tools/chrome_remote_control/chrome_remote_control/system_stub.py |
index a777a6c170113d1bf4fcb22ffabb1b4caab92691..88901c8340dbd6ef1566206630616900e2ffc16a 100644 |
--- a/tools/chrome_remote_control/chrome_remote_control/system_stub.py |
+++ b/tools/chrome_remote_control/chrome_remote_control/system_stub.py |
@@ -1,17 +1,65 @@ |
# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
# Use of this source code is governed by a BSD-style license that can be |
# found in the LICENSE file. |
-import os as real_os |
-import sys as real_sys |
-import subprocess as real_subprocess |
- |
"""Provides stubs for os, sys and subprocess for testing |
This test allows one to test code that itself uses os, sys, and subprocess. |
""" |
-class SysModuleStub(object): |
+ |
+import os |
+import subprocess |
+ |
+class Override(object): |
+ def __init__(self, base_module, **stubs): |
+ self._base_module = base_module |
+ self._stubs = stubs |
+ self._originals = {} |
+ |
+ def __enter__(self): |
+ for module_name, stub in self._stubs.iteritems(): |
+ self._originals[module_name] = getattr(self._base_module, module_name) |
+ setattr(self._base_module, module_name, stub) |
+ return self |
+ |
+ def __exit__(self, *args): |
+ for module_name, original in self._originals.iteritems(): |
+ setattr(self._base_module, module_name, original) |
+ |
nduca
2012/09/20 04:18:30
should __del__ throw an exception if they didn't e
|
+# adb not even found |
+# android_browser_finder not returning |
+class ADBCommandsStub(object): |
+ def __init__(self, module, device): |
+ self._module = module |
+ self._device = device |
+ self.is_root_enabled = True |
+ |
+ def RunShellCommand(self, args): |
+ if isinstance(args, basestring): |
+ import shlex |
+ args = shlex.split(args) |
+ handler = self._module.shell_command_handlers[args[0]] |
+ return handler(args) |
+ |
+ def IsRootEnabled(self): |
+ return self.is_root_enabled |
+ |
+class ADBCommandsModuleStub(object): |
def __init__(self): |
- self.platform = '' |
+ self.attached_devices = [] |
+ self.shell_command_handlers = {} |
+ |
+ def ADBCommandsStubConstructor(device=None): |
+ return ADBCommandsStub(self, device) |
+ self.ADBCommands = ADBCommandsStubConstructor |
+ |
+ def IsAndroidSupported(self): |
+ return True |
+ |
+ def GetAttachedDevices(self): |
+ return self.attached_devices |
+ |
+ def HasForwarder(self, adb): |
+ return True |
class OSPathModuleStub(object): |
def __init__(self, os_module_stub): |
@@ -22,13 +70,13 @@ class OSPathModuleStub(object): |
def join(self, *args): |
if self._os_module_stub.sys.platform.startswith('win'): |
- tmp = real_os.path.join(*args) |
+ tmp = os.path.join(*args) |
return tmp.replace('/', '\\') |
else: |
- return real_os.path.join(*args) |
+ return os.path.join(*args) |
def dirname(self, filename): |
- return real_os.path.dirname(filename) |
+ return os.path.dirname(filename) |
class OSModuleStub(object): |
def __init__(self, sys): |
@@ -37,6 +85,7 @@ class OSModuleStub(object): |
self.files = [] |
self.display = ':0' |
self.local_app_data = None |
+ self.devnull = os.devnull |
def getenv(self, name): |
if name == 'DISPLAY': |
@@ -56,8 +105,8 @@ class SubprocessModuleStub(object): |
def __init__(self): |
self.Popen_hook = None |
self.Popen_result = None |
- import subprocess as real_subprocess |
- self.PIPE = real_subprocess.PIPE |
+ self.PIPE = subprocess.PIPE |
+ self.call_hook = None |
def Popen(self, *args, **kwargs): |
assert self.Popen_hook or self.Popen_result |
@@ -65,3 +114,11 @@ class SubprocessModuleStub(object): |
return self.Popen_hook(*args, **kwargs) |
else: |
return self.Popen_result |
+ |
+ def call(self, *args, **kwargs): |
+ assert self.call_hook |
+ return self.call_hook(*args, **kwargs) |
+ |
+class SysModuleStub(object): |
+ def __init__(self): |
+ self.platform = '' |