File: test.cmake

package info (click to toggle)
android-platform-tools-base 2.2.2-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 113,928 kB
  • sloc: java: 696,396; xml: 45,920; cpp: 2,526; ansic: 1,432; sh: 508; lisp: 110; javascript: 108; makefile: 17
file content (105 lines) | stat: -rw-r--r-- 3,584 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Copyright (C) 2016 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

include(${CMAKE_CURRENT_LIST_DIR}/adb.cmake)

# Create targets for gtest
if(NOT GTEST_ROOT_DIR)
  message(FATAL_ERROR "GTEST_ROOT_DIR not set.")
  return()
endif()

add_library(gtest ${GTEST_ROOT_DIR}/src/gtest-all.cc
                  ${GTEST_ROOT_DIR}/src/gtest_main.cc)

target_include_directories(gtest PUBLIC ${GTEST_ROOT_DIR}
                                        ${GTEST_ROOT_DIR}/include)

# Create targets for gmock
if(NOT GMOCK_ROOT_DIR)
  message(FATAL_ERROR "GMOCK_ROOT_DIR not set.")
  return()
endif()

add_library(gmock ${GMOCK_ROOT_DIR}/src/gmock-all.cc)

target_include_directories(gmock PUBLIC ${GMOCK_ROOT_DIR}
                                        ${GMOCK_ROOT_DIR}/include
                                        ${GTEST_ROOT_DIR}/include)

add_dependencies(gmock gtest)

# Collect the list of libraries required to be linked into every test
# executable
set(GTEST_LINK_LIBRARIES gtest
                         gmock)

if(ANDROID)
  set(GTEST_LINK_LIBRARIES ${GTEST_LINK_LIBRARIES} gnustl_static)
else()
  set(GTEST_LINK_LIBRARIES ${GTEST_LINK_LIBRARIES} pthread)
endif()

# Copy test data files to generated directory.
if(ANDROID)
  run_adb_command(copy-testdata
                  push ${CMAKE_CURRENT_SOURCE_DIR}/testdata/* /data/local/tmp)
else()
  add_custom_target(copy-testdata
                    COMMAND ${CMAKE_COMMAND} -E copy_directory
                            ${CMAKE_CURRENT_SOURCE_DIR}/testdata
                            ${CMAKE_CURRENT_BINARY_DIR})
endif()

if(ANDROID)
  function(run_unit_test name)
    file(RELATIVE_PATH file_path ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR})

    # Copy the test file to the android device
    run_adb_command(push-${name}
                    push ${CMAKE_CURRENT_BINARY_DIR}/${name} /data/local/tmp/${file_path}/${name})
    add_dependencies(push-${name} ${name})

    # Run the test on the android device
    run_adb_command(check-${name}
                    shell 'cd /data/local/tmp/${file_path} && /data/local/tmp/${file_path}/${name}')
    add_dependencies(check-${name} push-${name})
  endfunction()
else()
  function(run_unit_test name)
    add_custom_target(check-${name}
                      COMMAND ${name})
    add_dependencies(check-${name} ${name})
  endfunction()
endif()

# Cretae target to run all unit test
add_custom_target(check)

# Create function for adding unit tests
function(add_unit_test name)
  # Compile the executable for the test
  add_executable(${name} ${ARGN})
  target_include_directories(${name} PUBLIC ${GTEST_ROOT_DIR}/include
                                            ${GMOCK_ROOT_DIR}/include)
  set_target_properties(${name} PROPERTIES
                        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

  # Create custome target for running the test and set it as a dependency of
  # check so it is included in it
  run_unit_test(${name})
  add_dependencies(check-${name} copy-testdata)
  add_dependencies(check check-${name})
endfunction()