• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# This module has common mock objects and functions used in unit tests for
16# mobly.controllers.android_device module.
17
18import logging
19import os
20from unittest import mock
21
22DEFAULT_MOCK_PROPERTIES = {
23    'ro.build.id': 'AB42',
24    'ro.build.type': 'userdebug',
25    'ro.build.fingerprint': 'FakeModel:Dessert/AB42/1234567:userdebug/dev-keys',
26    'ro.build.product': 'FakeModel',
27    'ro.build.version.codename': 'Z',
28    'ro.build.version.incremental': '1234567',
29    'ro.build.version.sdk': '28',
30    'ro.product.name': 'FakeModel',
31    'ro.debuggable': '1',
32    'sys.boot_completed': '1',
33    'ro.build.characteristics': 'emulator,phone',
34    'ro.hardware': 'marlin',
35}
36
37
38class Error(Exception):
39  pass
40
41
42def get_mock_ads(num):
43  """Generates a list of mock AndroidDevice objects.
44
45  The serial number of each device will be integer 0 through num - 1.
46
47  Args:
48    num: An integer that is the number of mock AndroidDevice objects to
49      create.
50  """
51  ads = []
52  for i in range(num):
53    ad = mock.MagicMock(name='AndroidDevice', serial=str(i), h_port=None)
54    ad.skip_logcat = False
55    ads.append(ad)
56  return ads
57
58
59def get_all_instances():
60  return get_mock_ads(5)
61
62
63def get_instances(serials):
64  ads = []
65  for serial in serials:
66    ad = mock.MagicMock(name='AndroidDevice', serial=serial, h_port=None)
67    ads.append(ad)
68  return ads
69
70
71def get_instances_with_configs(dicts):
72  return get_instances([d['serial'] for d in dicts])
73
74
75def list_adb_devices():
76  return [ad.serial for ad in get_mock_ads(5)]
77
78
79class MockAdbProxy:
80  """Mock class that swaps out calls to adb with mock calls."""
81
82  def __init__(
83      self,
84      serial='',
85      fail_br=False,
86      fail_br_before_N=False,
87      mock_properties=None,
88      installed_packages=None,
89      instrumented_packages=None,
90      adb_detectable=True,
91  ):
92    self.serial = serial
93    self.fail_br = fail_br
94    self.fail_br_before_N = fail_br_before_N
95    self.getprops_call_count = 0
96    if mock_properties is None:
97      self.mock_properties = DEFAULT_MOCK_PROPERTIES.copy()
98    else:
99      self.mock_properties = mock_properties
100    if installed_packages is None:
101      installed_packages = []
102    self.installed_packages = installed_packages
103    if instrumented_packages is None:
104      instrumented_packages = []
105    self.adb_detectable = adb_detectable
106    self.installed_packages = installed_packages
107    self.instrumented_packages = instrumented_packages
108
109  def shell(self, params, timeout=None):
110    if params == 'id -u':
111      return b'root'
112    elif params == 'bugreportz':
113      if self.fail_br:
114        return b'OMG I died!\n'
115      return b'OK:/path/bugreport.zip\n'
116    elif params == 'bugreportz -v':
117      if self.fail_br_before_N:
118        return b'/system/bin/sh: bugreportz: not found'
119      return b'1.1'
120    elif 'pm list package' in params:
121      packages = self.installed_packages + [
122          package for package, _, _ in self.instrumented_packages
123      ]
124      return bytes(
125          '\n'.join(['package:%s' % package for package in packages]), 'utf-8'
126      )
127    elif 'pm list instrumentation' in params:
128      return bytes(
129          '\n'.join(
130              [
131                  'instrumentation:%s/%s (target=%s)'
132                  % (package, runner, target)
133                  for package, runner, target in self.instrumented_packages
134              ]
135          ),
136          'utf-8',
137      )
138    elif 'which' in params:
139      return b''
140
141  def getprop(self, params):
142    if params in self.mock_properties:
143      return self.mock_properties[params]
144
145  def getprops(self, params):
146    self.getprops_call_count = self.getprops_call_count + 1
147    return self.mock_properties
148
149  def bugreport(self, args, shell=False, timeout=None):
150    expected = os.path.join(
151        logging.log_path,
152        'AndroidDevice%s' % self.serial,
153        'BugReports',
154        'bugreport,test_something,%s,fakemodel,sometime' % self.serial,
155    )
156    if expected not in args:
157      raise Error('"Expected "%s", got "%s"' % (expected, args))
158
159  def devices(self):
160    out = b'xxxx\tdevice\nyyyy\tdevice'
161    if self.adb_detectable:
162      out += f'\n{self.serial}\tdevice'.encode()
163    return out
164
165  def __getattr__(self, name):
166    """All calls to the none-existent functions in adb proxy would
167    simply return the adb command string.
168    """
169
170    def adb_call(*args, **kwargs):
171      arg_str = ' '.join(str(elem) for elem in args)
172      return arg_str
173
174    return adb_call
175
176
177class MockFastbootProxy:
178  """Mock class that swaps out calls to adb with mock calls."""
179
180  def __init__(self, serial):
181    self.serial = serial
182
183  def devices(self):
184    return b'xxxx device\nyyyy device'
185
186  def __getattr__(self, name):
187    def fastboot_call(*args):
188      arg_str = ' '.join(str(elem) for elem in args)
189      return arg_str
190
191    return fastboot_call
192