1# Copyright 2018 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
15from unittest import mock
16
17from mobly import base_instrumentation_test
18from mobly import config_parser
19from mobly.controllers import android_device
20from mobly.controllers.android_device_lib import adb
21
22# A mock test package for instrumentation.
23MOCK_TEST_PACKAGE = 'com.my.package.test'
24
25
26class MockInstrumentationTest(
27    base_instrumentation_test.BaseInstrumentationTestClass
28):
29
30  def __init__(self, tmp_dir, user_params={}):
31    mock_test_run_configs = config_parser.TestRunConfig()
32    mock_test_run_configs.summary_writer = mock.Mock()
33    mock_test_run_configs.log_path = tmp_dir
34    mock_test_run_configs.user_params = user_params
35    mock_test_run_configs.reporter = mock.MagicMock()
36    super().__init__(mock_test_run_configs)
37
38  def run_mock_instrumentation_test(self, instrumentation_output, prefix):
39    def fake_instrument(package, options=None, runner=None, handler=None):
40      for line in instrumentation_output.splitlines():
41        handler(line)
42      return instrumentation_output
43
44    mock_device = mock.Mock(spec=android_device.AndroidDevice)
45    mock_device.adb = mock.Mock(spec=adb.AdbProxy)
46    mock_device.adb.instrument = fake_instrument
47    return self.run_instrumentation_test(
48        mock_device, MOCK_TEST_PACKAGE, prefix=prefix
49    )
50