1#!/usr/bin/python 2# 3# 4# Copyright 2011, The Android Open Source Project 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17 18"""TestSuite for running C/C++ Android tests using gtest framework.""" 19 20# python imports 21import os 22import re 23 24# local imports 25import logger 26import run_command 27import test_suite 28 29 30class GTestSuite(test_suite.AbstractTestSuite): 31 """A test suite for running gtest on device.""" 32 33 def __init__(self): 34 test_suite.AbstractTestSuite.__init__(self) 35 self._target_exec_path = None 36 37 def GetTargetExecPath(self): 38 """Get the target path to gtest executable.""" 39 return self._target_exec_path 40 41 def SetTargetExecPath(self, path): 42 self._target_exec_path = path 43 return self 44 45 def Run(self, options, adb): 46 """Run the provided gtest test suite. 47 48 Args: 49 options: command line options 50 adb: adb interface 51 """ 52 shell_cmd = adb.PreviewShellCommand(self.GetTargetExecPath()) 53 logger.Log(shell_cmd) 54 if not options.preview: 55 # gtest will log to test results to stdout, so no need to do any 56 # extra processing 57 run_command.RunCommand(shell_cmd, return_output=False) 58 59 60class GTestFactory(test_suite.AbstractTestFactory): 61 62 def __init__(self, test_root_path, build_path): 63 test_suite.AbstractTestFactory.__init__(self, test_root_path, 64 build_path) 65 66 def CreateTests(self, sub_tests_path=None): 67 """Create tests found in sub_tests_path. 68 69 Looks for test files matching a pattern, and assumes each one is a separate 70 binary on target. 71 72 Test files must match one of the following pattern: 73 - test_*.[c|cc|cpp] 74 - *_test.[c|cc|cpp] 75 - *_unittest.[c|cc|cpp] 76 77 """ 78 if not sub_tests_path: 79 sub_tests_path = self.GetTestRootPath() 80 test_file_list = [] 81 if os.path.isfile(sub_tests_path): 82 self._EvaluateFile(test_file_list, os.path.basename(sub_tests_path)) 83 else: 84 os.path.walk(sub_tests_path, self._CollectTestSources, test_file_list) 85 # TODO: obtain this from makefile instead of hardcoding 86 target_root_path = os.path.join('/data', 'nativetest') 87 test_suites = [] 88 for test_file in test_file_list: 89 logger.SilentLog('Creating gtest suite for file %s' % test_file) 90 suite = GTestSuite() 91 suite.SetBuildPath(self.GetBuildPath()) 92 suite.SetTargetExecPath(os.path.join(target_root_path, test_file)) 93 test_suites.append(suite) 94 return test_suites 95 96 def _CollectTestSources(self, test_list, dirname, files): 97 """For each directory, find tests source file and add them to the list. 98 99 Test files must match one of the following pattern: 100 - test_*.[cc|cpp] 101 - *_test.[cc|cpp] 102 - *_unittest.[cc|cpp] 103 104 This method is a callback for os.path.walk. 105 106 Args: 107 test_list: Where new tests should be inserted. 108 dirname: Current directory. 109 files: List of files in the current directory. 110 """ 111 for f in files: 112 self._EvaluateFile(test_list, f) 113 114 def _EvaluateFile(self, test_list, file): 115 (name, ext) = os.path.splitext(file) 116 if ext == ".cc" or ext == ".cpp" or ext == ".c": 117 if re.search("_test$|_test_$|_unittest$|_unittest_$|^test_", name): 118 logger.SilentLog("Found native test file %s" % file) 119 test_list.append(name) 120