Bug 1690340 - Part 4: Insert the "Page Source" before the "Extensions for Developers...
[gecko.git] / testing / mozbase / mozdevice / tests / conftest.py
blob68434f7f4cba6fc32f31f4d276a77b01917ab756
1 from __future__ import absolute_import, print_function
3 import sys
4 from random import randint, seed
6 import mozdevice
7 import pytest
8 from mock import patch
9 from six import StringIO
11 # set up required module-level variables/objects
12 seed(1488590)
15 def random_tcp_port():
16 """Returns a pseudo-random integer generated from a seed.
18 :returns: int: pseudo-randomly generated integer
19 """
20 return randint(8000, 12000)
23 @pytest.fixture(autouse=True)
24 def mock_command_output(monkeypatch):
25 """Monkeypatches the ADBDevice.command_output() method call.
27 Instead of calling the concrete method implemented in adb.py::ADBDevice,
28 this method simply returns a string representation of the command that was
29 received.
31 :param object monkeypatch: pytest provided fixture for mocking.
32 """
34 def command_output_wrapper(object, cmd, timeout):
35 """Actual monkeypatch implementation of the comand_output method call.
37 :param object object: placeholder object representing ADBDevice
38 :param str cmd: command to be executed
39 :param timeout: unused parameter to represent timeout threshold
40 :returns: string - string representation of command to be executed
41 """
42 print(str(cmd))
43 return str(cmd)
45 monkeypatch.setattr(mozdevice.ADBDevice, "command_output", command_output_wrapper)
48 @pytest.fixture(autouse=True)
49 def mock_shell_output(monkeypatch):
50 """Monkeypatches the ADBDevice.shell_output() method call.
52 Instead of returning the output of an adb call, this method will
53 return appropriate string output. Content of the string output is
54 in line with the calling method's expectations.
56 :param object monkeypatch: pytest provided fixture for mocking.
57 """
59 def shell_output_wrapper(
60 object, cmd, env=None, cwd=None, timeout=None, enable_run_as=False
62 """Actual monkeypatch implementation of the shell_output method call.
64 :param object object: placeholder object representing ADBDevice
65 :param str cmd: command to be executed
66 :param env: contains the environment variable
67 :type env: dict or None
68 :param cwd: The directory from which to execute.
69 :type cwd: str or None
70 :param timeout: unused parameter tp represent timeout threshold
71 :param enable_run_as: bool determining if run_as <app> is to be used
72 :returns: string - string representation of a simulated call to adb
73 """
74 if "pm list package error" in cmd:
75 return "Error: Could not access the Package Manager"
76 elif "pm list package none" in cmd:
77 return ""
78 elif "pm list package" in cmd:
79 apps = ["org.mozilla.fennec", "org.mozilla.geckoview_example"]
80 return ("package:{}\n" * len(apps)).format(*apps)
81 else:
82 print(str(cmd))
83 return str(cmd)
85 monkeypatch.setattr(mozdevice.ADBDevice, "shell_output", shell_output_wrapper)
88 @pytest.fixture(autouse=True)
89 def mock_is_path_internal_storage(monkeypatch):
90 """Monkeypatches the ADBDevice.is_path_internal_storage() method call.
92 Instead of returning the outcome of whether the path provided is
93 internal storage or external, this will always return True.
95 :param object monkeypatch: pytest provided fixture for mocking.
96 """
98 def is_path_internal_storage_wrapper(object, path, timeout=None):
99 """Actual monkeypatch implementation of the is_path_internal_storage() call.
101 :param str path: The path to test.
102 :param timeout: The maximum time in
103 seconds for any spawned adb process to complete before
104 throwing an ADBTimeoutError. This timeout is per adb call. The
105 total time spent may exceed this value. If it is not
106 specified, the value set in the ADBDevice constructor is used.
107 :returns: boolean
109 :raises: * ADBTimeoutError
110 * ADBError
112 if "internal_storage" in path:
113 return True
114 return False
116 monkeypatch.setattr(
117 mozdevice.ADBDevice,
118 "is_path_internal_storage",
119 is_path_internal_storage_wrapper,
123 @pytest.fixture(autouse=True)
124 def mock_enable_run_as_for_path(monkeypatch):
125 """Monkeypatches the ADBDevice.enable_run_as_for_path(path) method.
127 Always return True
129 :param object monkeypatch: pytest provided fixture for mocking.
132 def enable_run_as_for_path_wrapper(object, path):
133 """Actual monkeypatch implementation of the enable_run_as_for_path() call.
135 :param str path: The path to test.
136 :returns: boolean
138 return True
140 monkeypatch.setattr(
141 mozdevice.ADBDevice, "enable_run_as_for_path", enable_run_as_for_path_wrapper
145 @pytest.fixture(autouse=True)
146 def mock_shell_bool(monkeypatch):
147 """Monkeypatches the ADBDevice.shell_bool() method call.
149 Instead of returning the output of an adb call, this method will
150 return appropriate string output. Content of the string output is
151 in line with the calling method's expectations.
153 :param object monkeypatch: pytest provided fixture for mocking.
156 def shell_bool_wrapper(
157 object, cmd, env=None, cwd=None, timeout=None, enable_run_as=False
159 """Actual monkeypatch implementation of the shell_bool method call.
161 :param object object: placeholder object representing ADBDevice
162 :param str cmd: command to be executed
163 :param env: contains the environment variable
164 :type env: dict or None
165 :param cwd: The directory from which to execute.
166 :type cwd: str or None
167 :param timeout: unused parameter tp represent timeout threshold
168 :param enable_run_as: bool determining if run_as <app> is to be used
169 :returns: string - string representation of a simulated call to adb
171 print(cmd)
172 return str(cmd)
174 monkeypatch.setattr(mozdevice.ADBDevice, "shell_bool", shell_bool_wrapper)
177 @pytest.fixture(autouse=True)
178 def mock_adb_object():
179 """Patches the __init__ method call when instantiating ADBDevice.
181 ADBDevice normally requires instantiated objects in order to execute
182 its commands.
184 With a pytest-mock patch, we are able to mock the initialization of
185 the ADBDevice object. By yielding the instantiated mock object,
186 unit tests can be run that call methods that require an instantiated
187 object.
189 :yields: ADBDevice - mock instance of ADBDevice object
191 with patch.object(mozdevice.ADBDevice, "__init__", lambda self: None):
192 yield mozdevice.ADBDevice()
195 @pytest.fixture
196 def redirect_stdout_and_assert():
197 """Redirects the stdout pipe temporarily to a StringIO stream.
199 This is useful to assert on methods that do not return
200 a value, such as most ADBDevice methods.
202 The original stdout pipe is preserved throughout the process.
204 :returns: _wrapper method
207 def _wrapper(func, **kwargs):
208 """Implements the stdout sleight-of-hand.
210 After preserving the original sys.stdout, it is switched
211 to use cStringIO.StringIO.
213 Method with no return value is called, and the stdout
214 pipe is switched back to the original sys.stdout.
216 The expected outcome is received as part of the kwargs.
217 This is asserted against a sanitized output from the method
218 under test.
220 :param object func: method under test
221 :param dict kwargs: dictionary of function parameters
223 original_stdout = sys.stdout
224 sys.stdout = testing_stdout = StringIO()
225 expected_text = kwargs.pop("text")
226 func(**kwargs)
227 sys.stdout = original_stdout
228 assert expected_text in testing_stdout.getvalue().rstrip()
230 return _wrapper