1#
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import android_device
18import json
19import unittest
20
21_DATA_NAME = 'dataName'
22_DATA_TYPE = 'dataType'
23_DATA_FILE = 'dataFile'
24
25class _TradefedTestClass(unittest.TestCase):
26    """ A base test class to extends to receive a device object for testing in python
27
28        All tests should extends this class to be properly supported by Tradefed.
29    """
30
31    def setUpDevice(self, serial, stream, options):
32        """ Setter method that will allow the test to receive the device object
33
34        Args:
35            serial: The serial of the device allocated for the test.
36            stream: The output stream.
37            options: Additional options given to the tests that can be used.
38        """
39        self.serial = serial
40        self.stream = stream
41        self.extra_options = options
42        self.android_device = android_device.AndroidTestDevice(serial, stream)
43
44    def logFileToTradefed(self, name, filePath, fileType):
45        """ Callback to log a file that will be picked up by Tradefed.
46
47        Args:
48            name: The name under which log the particular data.
49            filePath: Absolute file path of the file to be logged.
50            fileType: The type of the file. (TEXT, PNG, etc.)
51        """
52        resp = {_DATA_NAME: name, _DATA_TYPE: fileType, _DATA_FILE: filePath}
53        self.stream.write('TEST_LOG %s\n' % json.dumps(resp))
54